Rev 4569 | Rev 5271 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1404 | serge | 1 | /************************************************************************** |
2 | * |
||
3 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA |
||
4 | * All Rights Reserved. |
||
5 | * |
||
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
||
7 | * copy of this software and associated documentation files (the |
||
8 | * "Software"), to deal in the Software without restriction, including |
||
9 | * without limitation the rights to use, copy, modify, merge, publish, |
||
10 | * distribute, sub license, and/or sell copies of the Software, and to |
||
11 | * permit persons to whom the Software is furnished to do so, subject to |
||
12 | * the following conditions: |
||
13 | * |
||
14 | * The above copyright notice and this permission notice (including the |
||
15 | * next paragraph) shall be included in all copies or substantial portions |
||
16 | * of the Software. |
||
17 | * |
||
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
||
22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
||
23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
||
24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
25 | * |
||
26 | **************************************************************************/ |
||
27 | /* |
||
28 | * Authors: Thomas Hellstrom |
||
29 | */ |
||
4075 | Serge | 30 | |
31 | #define pr_fmt(fmt) "[TTM] " fmt |
||
32 | |||
33 | #include |
||
34 | #include |
||
35 | #include |
||
36 | #include |
||
37 | #include |
||
38 | #include |
||
39 | #include |
||
40 | #include |
||
41 | |||
5078 | serge | 42 | #define pr_err(fmt, ...) \ |
43 | printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) |
||
44 | |||
4112 | Serge | 45 | #define TTM_ASSERT_LOCKED(param) |
46 | #define TTM_DEBUG(fmt, arg...) |
||
47 | #define TTM_BO_HASH_ORDER 13 |
||
48 | |||
4075 | Serge | 49 | |
5078 | serge | 50 | |
51 | static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type) |
||
4075 | Serge | 52 | { |
5078 | serge | 53 | int i; |
4075 | Serge | 54 | |
5078 | serge | 55 | for (i = 0; i <= TTM_PL_PRIV5; i++) |
56 | if (flags & (1 << i)) { |
||
57 | *mem_type = i; |
||
4075 | Serge | 58 | return 0; |
5078 | serge | 59 | } |
60 | return -EINVAL; |
||
4075 | Serge | 61 | } |
62 | |||
63 | |||
64 | |||
65 | |||
66 | |||
67 | |||
68 | |||
69 | |||
70 | |||
71 | static inline uint32_t ttm_bo_type_flags(unsigned type) |
||
72 | { |
||
73 | return 1 << (type); |
||
74 | } |
||
75 | |||
76 | static void ttm_bo_release_list(struct kref *list_kref) |
||
77 | { |
||
78 | struct ttm_buffer_object *bo = |
||
79 | container_of(list_kref, struct ttm_buffer_object, list_kref); |
||
80 | struct ttm_bo_device *bdev = bo->bdev; |
||
81 | size_t acc_size = bo->acc_size; |
||
82 | |||
83 | BUG_ON(atomic_read(&bo->list_kref.refcount)); |
||
84 | BUG_ON(atomic_read(&bo->kref.refcount)); |
||
85 | BUG_ON(atomic_read(&bo->cpu_writers)); |
||
86 | BUG_ON(bo->sync_obj != NULL); |
||
87 | BUG_ON(bo->mem.mm_node != NULL); |
||
88 | BUG_ON(!list_empty(&bo->lru)); |
||
89 | BUG_ON(!list_empty(&bo->ddestroy)); |
||
90 | |||
91 | if (bo->ttm) |
||
92 | ttm_tt_destroy(bo->ttm); |
||
93 | atomic_dec(&bo->glob->bo_count); |
||
5078 | serge | 94 | if (bo->resv == &bo->ttm_resv) |
95 | reservation_object_fini(&bo->ttm_resv); |
||
96 | mutex_destroy(&bo->wu_mutex); |
||
4075 | Serge | 97 | if (bo->destroy) |
98 | bo->destroy(bo); |
||
99 | else { |
||
100 | kfree(bo); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) |
||
105 | { |
||
106 | struct ttm_bo_device *bdev = bo->bdev; |
||
107 | struct ttm_mem_type_manager *man; |
||
108 | |||
5078 | serge | 109 | lockdep_assert_held(&bo->resv->lock.base); |
4075 | Serge | 110 | |
111 | if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { |
||
112 | |||
113 | BUG_ON(!list_empty(&bo->lru)); |
||
114 | |||
115 | man = &bdev->man[bo->mem.mem_type]; |
||
116 | list_add_tail(&bo->lru, &man->lru); |
||
117 | kref_get(&bo->list_kref); |
||
118 | |||
119 | if (bo->ttm != NULL) { |
||
120 | list_add_tail(&bo->swap, &bo->glob->swap_lru); |
||
121 | kref_get(&bo->list_kref); |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | EXPORT_SYMBOL(ttm_bo_add_to_lru); |
||
126 | |||
127 | int ttm_bo_del_from_lru(struct ttm_buffer_object *bo) |
||
128 | { |
||
129 | int put_count = 0; |
||
130 | |||
131 | if (!list_empty(&bo->swap)) { |
||
132 | list_del_init(&bo->swap); |
||
133 | ++put_count; |
||
134 | } |
||
135 | if (!list_empty(&bo->lru)) { |
||
136 | list_del_init(&bo->lru); |
||
137 | ++put_count; |
||
138 | } |
||
139 | |||
140 | /* |
||
141 | * TODO: Add a driver hook to delete from |
||
142 | * driver-specific LRU's here. |
||
143 | */ |
||
144 | |||
145 | return put_count; |
||
146 | } |
||
147 | |||
148 | static void ttm_bo_ref_bug(struct kref *list_kref) |
||
149 | { |
||
150 | BUG(); |
||
151 | } |
||
152 | |||
153 | void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count, |
||
154 | bool never_free) |
||
155 | { |
||
156 | // kref_sub(&bo->list_kref, count, |
||
157 | // (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list); |
||
158 | } |
||
159 | |||
160 | void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo) |
||
161 | { |
||
162 | int put_count; |
||
163 | |||
164 | spin_lock(&bo->glob->lru_lock); |
||
165 | put_count = ttm_bo_del_from_lru(bo); |
||
166 | spin_unlock(&bo->glob->lru_lock); |
||
167 | ttm_bo_list_ref_sub(bo, put_count, true); |
||
168 | } |
||
169 | EXPORT_SYMBOL(ttm_bo_del_sub_from_lru); |
||
170 | |||
171 | /* |
||
172 | * Call bo->mutex locked. |
||
173 | */ |
||
174 | static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) |
||
175 | { |
||
176 | struct ttm_bo_device *bdev = bo->bdev; |
||
177 | struct ttm_bo_global *glob = bo->glob; |
||
178 | int ret = 0; |
||
179 | uint32_t page_flags = 0; |
||
180 | |||
4112 | Serge | 181 | TTM_ASSERT_LOCKED(&bo->mutex); |
4075 | Serge | 182 | bo->ttm = NULL; |
183 | |||
184 | if (bdev->need_dma32) |
||
185 | page_flags |= TTM_PAGE_FLAG_DMA32; |
||
186 | |||
187 | switch (bo->type) { |
||
188 | case ttm_bo_type_device: |
||
189 | if (zero_alloc) |
||
190 | page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; |
||
191 | case ttm_bo_type_kernel: |
||
192 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
||
193 | page_flags, glob->dummy_read_page); |
||
194 | if (unlikely(bo->ttm == NULL)) |
||
195 | ret = -ENOMEM; |
||
196 | break; |
||
197 | case ttm_bo_type_sg: |
||
198 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
||
199 | page_flags | TTM_PAGE_FLAG_SG, |
||
200 | glob->dummy_read_page); |
||
201 | if (unlikely(bo->ttm == NULL)) { |
||
202 | ret = -ENOMEM; |
||
203 | break; |
||
204 | } |
||
205 | bo->ttm->sg = bo->sg; |
||
206 | break; |
||
207 | default: |
||
208 | pr_err("Illegal buffer object type\n"); |
||
209 | ret = -EINVAL; |
||
210 | break; |
||
211 | } |
||
212 | |||
213 | return ret; |
||
214 | } |
||
215 | |||
216 | static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, |
||
217 | struct ttm_mem_reg *mem, |
||
218 | bool evict, bool interruptible, |
||
219 | bool no_wait_gpu) |
||
220 | { |
||
221 | struct ttm_bo_device *bdev = bo->bdev; |
||
222 | bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); |
||
223 | bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); |
||
224 | struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; |
||
225 | struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; |
||
226 | int ret = 0; |
||
227 | |||
228 | if (old_is_pci || new_is_pci || |
||
229 | ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) { |
||
230 | ret = ttm_mem_io_lock(old_man, true); |
||
231 | if (unlikely(ret != 0)) |
||
232 | goto out_err; |
||
233 | ttm_bo_unmap_virtual_locked(bo); |
||
234 | ttm_mem_io_unlock(old_man); |
||
235 | } |
||
236 | |||
237 | /* |
||
238 | * Create and bind a ttm if required. |
||
239 | */ |
||
240 | |||
241 | if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
||
242 | if (bo->ttm == NULL) { |
||
243 | bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED); |
||
244 | ret = ttm_bo_add_ttm(bo, zero); |
||
245 | if (ret) |
||
246 | goto out_err; |
||
247 | } |
||
248 | |||
249 | ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); |
||
250 | if (ret) |
||
251 | goto out_err; |
||
252 | |||
253 | if (mem->mem_type != TTM_PL_SYSTEM) { |
||
254 | ret = ttm_tt_bind(bo->ttm, mem); |
||
255 | if (ret) |
||
256 | goto out_err; |
||
257 | } |
||
258 | |||
259 | if (bo->mem.mem_type == TTM_PL_SYSTEM) { |
||
260 | if (bdev->driver->move_notify) |
||
261 | bdev->driver->move_notify(bo, mem); |
||
262 | bo->mem = *mem; |
||
263 | mem->mm_node = NULL; |
||
264 | goto moved; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | if (bdev->driver->move_notify) |
||
269 | bdev->driver->move_notify(bo, mem); |
||
270 | |||
271 | if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && |
||
272 | !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) |
||
273 | ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem); |
||
274 | else if (bdev->driver->move) |
||
275 | ret = bdev->driver->move(bo, evict, interruptible, |
||
276 | no_wait_gpu, mem); |
||
277 | else |
||
278 | ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem); |
||
279 | |||
280 | if (ret) { |
||
281 | if (bdev->driver->move_notify) { |
||
282 | struct ttm_mem_reg tmp_mem = *mem; |
||
283 | *mem = bo->mem; |
||
284 | bo->mem = tmp_mem; |
||
285 | bdev->driver->move_notify(bo, mem); |
||
286 | bo->mem = *mem; |
||
287 | *mem = tmp_mem; |
||
288 | } |
||
289 | |||
290 | goto out_err; |
||
291 | } |
||
292 | |||
293 | moved: |
||
294 | if (bo->evicted) { |
||
5078 | serge | 295 | if (bdev->driver->invalidate_caches) { |
4075 | Serge | 296 | ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); |
297 | if (ret) |
||
298 | pr_err("Can not flush read caches\n"); |
||
5078 | serge | 299 | } |
4075 | Serge | 300 | bo->evicted = false; |
301 | } |
||
302 | |||
303 | if (bo->mem.mm_node) { |
||
304 | bo->offset = (bo->mem.start << PAGE_SHIFT) + |
||
305 | bdev->man[bo->mem.mem_type].gpu_offset; |
||
306 | bo->cur_placement = bo->mem.placement; |
||
307 | } else |
||
308 | bo->offset = 0; |
||
309 | |||
310 | return 0; |
||
311 | |||
312 | out_err: |
||
313 | new_man = &bdev->man[bo->mem.mem_type]; |
||
314 | if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) { |
||
315 | ttm_tt_unbind(bo->ttm); |
||
316 | ttm_tt_destroy(bo->ttm); |
||
317 | bo->ttm = NULL; |
||
318 | } |
||
319 | |||
320 | return ret; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Call bo::reserved. |
||
325 | * Will release GPU memory type usage on destruction. |
||
326 | * This is the place to put in driver specific hooks to release |
||
327 | * driver private resources. |
||
328 | * Will release the bo::reserved lock. |
||
329 | */ |
||
330 | |||
331 | static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) |
||
332 | { |
||
333 | if (bo->bdev->driver->move_notify) |
||
334 | bo->bdev->driver->move_notify(bo, NULL); |
||
335 | |||
336 | if (bo->ttm) { |
||
337 | ttm_tt_unbind(bo->ttm); |
||
338 | ttm_tt_destroy(bo->ttm); |
||
339 | bo->ttm = NULL; |
||
340 | } |
||
341 | ttm_bo_mem_put(bo, &bo->mem); |
||
342 | |||
343 | ww_mutex_unlock (&bo->resv->lock); |
||
344 | } |
||
345 | |||
346 | static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo) |
||
347 | { |
||
348 | struct ttm_bo_device *bdev = bo->bdev; |
||
349 | struct ttm_bo_global *glob = bo->glob; |
||
350 | struct ttm_bo_driver *driver = bdev->driver; |
||
351 | void *sync_obj = NULL; |
||
352 | int put_count; |
||
353 | int ret; |
||
354 | |||
355 | spin_lock(&glob->lru_lock); |
||
5078 | serge | 356 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
4075 | Serge | 357 | |
358 | spin_lock(&bdev->fence_lock); |
||
359 | (void) ttm_bo_wait(bo, false, false, true); |
||
360 | if (!ret && !bo->sync_obj) { |
||
361 | spin_unlock(&bdev->fence_lock); |
||
362 | put_count = ttm_bo_del_from_lru(bo); |
||
363 | |||
364 | spin_unlock(&glob->lru_lock); |
||
365 | ttm_bo_cleanup_memtype_use(bo); |
||
366 | |||
367 | ttm_bo_list_ref_sub(bo, put_count, true); |
||
368 | |||
369 | return; |
||
370 | } |
||
371 | if (bo->sync_obj) |
||
372 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
||
373 | spin_unlock(&bdev->fence_lock); |
||
374 | |||
4569 | Serge | 375 | if (!ret) { |
376 | |||
377 | /* |
||
378 | * Make NO_EVICT bos immediately available to |
||
379 | * shrinkers, now that they are queued for |
||
380 | * destruction. |
||
381 | */ |
||
382 | if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) { |
||
383 | bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT; |
||
384 | ttm_bo_add_to_lru(bo); |
||
385 | } |
||
386 | |||
5078 | serge | 387 | __ttm_bo_unreserve(bo); |
4569 | Serge | 388 | } |
4075 | Serge | 389 | |
390 | kref_get(&bo->list_kref); |
||
391 | list_add_tail(&bo->ddestroy, &bdev->ddestroy); |
||
392 | spin_unlock(&glob->lru_lock); |
||
393 | |||
394 | if (sync_obj) { |
||
395 | driver->sync_obj_flush(sync_obj); |
||
396 | driver->sync_obj_unref(&sync_obj); |
||
397 | } |
||
5078 | serge | 398 | // schedule_delayed_work(&bdev->wq, |
399 | // ((HZ / 100) < 1) ? 1 : HZ / 100); |
||
4075 | Serge | 400 | } |
401 | |||
402 | /** |
||
403 | * function ttm_bo_cleanup_refs_and_unlock |
||
404 | * If bo idle, remove from delayed- and lru lists, and unref. |
||
405 | * If not idle, do nothing. |
||
1404 | serge | 406 | * |
4075 | Serge | 407 | * Must be called with lru_lock and reservation held, this function |
408 | * will drop both before returning. |
||
409 | * |
||
410 | * @interruptible Any sleeps should occur interruptibly. |
||
411 | * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. |
||
1404 | serge | 412 | */ |
413 | |||
4075 | Serge | 414 | static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, |
415 | bool interruptible, |
||
416 | bool no_wait_gpu) |
||
417 | { |
||
418 | struct ttm_bo_device *bdev = bo->bdev; |
||
419 | struct ttm_bo_driver *driver = bdev->driver; |
||
420 | struct ttm_bo_global *glob = bo->glob; |
||
421 | int put_count; |
||
422 | int ret; |
||
1404 | serge | 423 | |
4075 | Serge | 424 | spin_lock(&bdev->fence_lock); |
425 | ret = ttm_bo_wait(bo, false, false, true); |
||
1404 | serge | 426 | |
4075 | Serge | 427 | if (ret && !no_wait_gpu) { |
428 | void *sync_obj; |
||
1404 | serge | 429 | |
4075 | Serge | 430 | /* |
431 | * Take a reference to the fence and unreserve, |
||
432 | * at this point the buffer should be dead, so |
||
433 | * no new sync objects can be attached. |
||
434 | */ |
||
435 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
||
436 | spin_unlock(&bdev->fence_lock); |
||
437 | |||
5078 | serge | 438 | __ttm_bo_unreserve(bo); |
4075 | Serge | 439 | spin_unlock(&glob->lru_lock); |
440 | |||
441 | ret = driver->sync_obj_wait(sync_obj, false, interruptible); |
||
442 | driver->sync_obj_unref(&sync_obj); |
||
443 | if (ret) |
||
444 | return ret; |
||
445 | |||
446 | /* |
||
447 | * remove sync_obj with ttm_bo_wait, the wait should be |
||
448 | * finished, and no new wait object should have been added. |
||
449 | */ |
||
450 | spin_lock(&bdev->fence_lock); |
||
451 | ret = ttm_bo_wait(bo, false, false, true); |
||
452 | WARN_ON(ret); |
||
453 | spin_unlock(&bdev->fence_lock); |
||
454 | if (ret) |
||
455 | return ret; |
||
456 | |||
457 | spin_lock(&glob->lru_lock); |
||
5078 | serge | 458 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
4075 | Serge | 459 | |
460 | /* |
||
461 | * We raced, and lost, someone else holds the reservation now, |
||
462 | * and is probably busy in ttm_bo_cleanup_memtype_use. |
||
463 | * |
||
464 | * Even if it's not the case, because we finished waiting any |
||
465 | * delayed destruction would succeed, so just return success |
||
466 | * here. |
||
467 | */ |
||
468 | if (ret) { |
||
469 | spin_unlock(&glob->lru_lock); |
||
470 | return 0; |
||
471 | } |
||
472 | } else |
||
473 | spin_unlock(&bdev->fence_lock); |
||
474 | |||
475 | if (ret || unlikely(list_empty(&bo->ddestroy))) { |
||
5078 | serge | 476 | __ttm_bo_unreserve(bo); |
4075 | Serge | 477 | spin_unlock(&glob->lru_lock); |
478 | return ret; |
||
479 | } |
||
480 | |||
481 | put_count = ttm_bo_del_from_lru(bo); |
||
482 | list_del_init(&bo->ddestroy); |
||
483 | ++put_count; |
||
484 | |||
485 | spin_unlock(&glob->lru_lock); |
||
486 | ttm_bo_cleanup_memtype_use(bo); |
||
487 | |||
488 | ttm_bo_list_ref_sub(bo, put_count, true); |
||
489 | |||
490 | return 0; |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Traverse the delayed list, and call ttm_bo_cleanup_refs on all |
||
495 | * encountered buffers. |
||
496 | */ |
||
497 | |||
498 | static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) |
||
499 | { |
||
500 | struct ttm_bo_global *glob = bdev->glob; |
||
501 | struct ttm_buffer_object *entry = NULL; |
||
502 | int ret = 0; |
||
503 | |||
504 | spin_lock(&glob->lru_lock); |
||
505 | if (list_empty(&bdev->ddestroy)) |
||
506 | goto out_unlock; |
||
507 | |||
508 | entry = list_first_entry(&bdev->ddestroy, |
||
509 | struct ttm_buffer_object, ddestroy); |
||
510 | kref_get(&entry->list_kref); |
||
511 | |||
512 | for (;;) { |
||
513 | struct ttm_buffer_object *nentry = NULL; |
||
514 | |||
515 | if (entry->ddestroy.next != &bdev->ddestroy) { |
||
516 | nentry = list_first_entry(&entry->ddestroy, |
||
517 | struct ttm_buffer_object, ddestroy); |
||
518 | kref_get(&nentry->list_kref); |
||
519 | } |
||
520 | |||
5078 | serge | 521 | ret = __ttm_bo_reserve(entry, false, true, false, NULL); |
4075 | Serge | 522 | if (remove_all && ret) { |
523 | spin_unlock(&glob->lru_lock); |
||
5078 | serge | 524 | ret = __ttm_bo_reserve(entry, false, false, |
525 | false, NULL); |
||
4075 | Serge | 526 | spin_lock(&glob->lru_lock); |
527 | } |
||
528 | |||
529 | if (!ret) |
||
530 | ret = ttm_bo_cleanup_refs_and_unlock(entry, false, |
||
531 | !remove_all); |
||
532 | else |
||
533 | spin_unlock(&glob->lru_lock); |
||
534 | |||
535 | kref_put(&entry->list_kref, ttm_bo_release_list); |
||
536 | entry = nentry; |
||
537 | |||
538 | if (ret || !entry) |
||
539 | goto out; |
||
540 | |||
541 | spin_lock(&glob->lru_lock); |
||
542 | if (list_empty(&entry->ddestroy)) |
||
543 | break; |
||
544 | } |
||
545 | |||
546 | out_unlock: |
||
547 | spin_unlock(&glob->lru_lock); |
||
548 | out: |
||
549 | if (entry) |
||
550 | kref_put(&entry->list_kref, ttm_bo_release_list); |
||
551 | return ret; |
||
552 | } |
||
553 | |||
554 | static void ttm_bo_delayed_workqueue(struct work_struct *work) |
||
555 | { |
||
556 | struct ttm_bo_device *bdev = |
||
557 | container_of(work, struct ttm_bo_device, wq.work); |
||
558 | |||
559 | if (ttm_bo_delayed_delete(bdev, false)) { |
||
560 | schedule_delayed_work(&bdev->wq, |
||
561 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
||
562 | } |
||
563 | } |
||
564 | |||
565 | static void ttm_bo_release(struct kref *kref) |
||
566 | { |
||
567 | struct ttm_buffer_object *bo = |
||
568 | container_of(kref, struct ttm_buffer_object, kref); |
||
569 | struct ttm_bo_device *bdev = bo->bdev; |
||
570 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
||
571 | |||
4112 | Serge | 572 | drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node); |
4075 | Serge | 573 | ttm_mem_io_lock(man, false); |
5078 | serge | 574 | ttm_mem_io_free_vm(bo); |
4075 | Serge | 575 | ttm_mem_io_unlock(man); |
5078 | serge | 576 | ttm_bo_cleanup_refs_or_queue(bo); |
577 | kref_put(&bo->list_kref, ttm_bo_release_list); |
||
4075 | Serge | 578 | } |
579 | |||
580 | void ttm_bo_unref(struct ttm_buffer_object **p_bo) |
||
581 | { |
||
582 | struct ttm_buffer_object *bo = *p_bo; |
||
583 | |||
584 | *p_bo = NULL; |
||
585 | kref_put(&bo->kref, ttm_bo_release); |
||
586 | } |
||
587 | EXPORT_SYMBOL(ttm_bo_unref); |
||
588 | |||
589 | void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) |
||
590 | { |
||
591 | struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; |
||
592 | |||
593 | if (mem->mm_node) |
||
594 | (*man->func->put_node)(man, mem); |
||
595 | } |
||
596 | EXPORT_SYMBOL(ttm_bo_mem_put); |
||
597 | |||
598 | /** |
||
599 | * Repeatedly evict memory from the LRU for @mem_type until we create enough |
||
600 | * space, or we've evicted everything and there isn't enough space. |
||
601 | */ |
||
602 | static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, |
||
603 | uint32_t mem_type, |
||
604 | struct ttm_placement *placement, |
||
605 | struct ttm_mem_reg *mem, |
||
606 | bool interruptible, |
||
607 | bool no_wait_gpu) |
||
608 | { |
||
609 | struct ttm_bo_device *bdev = bo->bdev; |
||
610 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
||
611 | int ret; |
||
612 | |||
613 | do { |
||
5078 | serge | 614 | ret = (*man->func->get_node)(man, bo, placement, 0, mem); |
4075 | Serge | 615 | if (unlikely(ret != 0)) |
616 | return ret; |
||
617 | if (mem->mm_node) |
||
618 | break; |
||
5078 | serge | 619 | // ret = ttm_mem_evict_first(bdev, mem_type, |
620 | // interruptible, no_wait_gpu); |
||
621 | // if (unlikely(ret != 0)) |
||
622 | // return ret; |
||
4075 | Serge | 623 | } while (1); |
624 | if (mem->mm_node == NULL) |
||
625 | return -ENOMEM; |
||
626 | mem->mem_type = mem_type; |
||
627 | return 0; |
||
628 | } |
||
629 | |||
630 | static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, |
||
631 | uint32_t cur_placement, |
||
632 | uint32_t proposed_placement) |
||
633 | { |
||
634 | uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; |
||
635 | uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; |
||
636 | |||
637 | /** |
||
638 | * Keep current caching if possible. |
||
639 | */ |
||
640 | |||
641 | if ((cur_placement & caching) != 0) |
||
642 | result |= (cur_placement & caching); |
||
643 | else if ((man->default_caching & caching) != 0) |
||
644 | result |= man->default_caching; |
||
645 | else if ((TTM_PL_FLAG_CACHED & caching) != 0) |
||
646 | result |= TTM_PL_FLAG_CACHED; |
||
647 | else if ((TTM_PL_FLAG_WC & caching) != 0) |
||
648 | result |= TTM_PL_FLAG_WC; |
||
649 | else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) |
||
650 | result |= TTM_PL_FLAG_UNCACHED; |
||
651 | |||
652 | return result; |
||
653 | } |
||
654 | |||
655 | static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, |
||
656 | uint32_t mem_type, |
||
657 | uint32_t proposed_placement, |
||
658 | uint32_t *masked_placement) |
||
659 | { |
||
660 | uint32_t cur_flags = ttm_bo_type_flags(mem_type); |
||
661 | |||
662 | if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0) |
||
663 | return false; |
||
664 | |||
665 | if ((proposed_placement & man->available_caching) == 0) |
||
666 | return false; |
||
667 | |||
668 | cur_flags |= (proposed_placement & man->available_caching); |
||
669 | |||
670 | *masked_placement = cur_flags; |
||
671 | return true; |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Creates space for memory region @mem according to its type. |
||
676 | * |
||
677 | * This function first searches for free space in compatible memory types in |
||
678 | * the priority order defined by the driver. If free space isn't found, then |
||
679 | * ttm_bo_mem_force_space is attempted in priority order to evict and find |
||
680 | * space. |
||
681 | */ |
||
682 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, |
||
683 | struct ttm_placement *placement, |
||
684 | struct ttm_mem_reg *mem, |
||
685 | bool interruptible, |
||
686 | bool no_wait_gpu) |
||
687 | { |
||
688 | struct ttm_bo_device *bdev = bo->bdev; |
||
689 | struct ttm_mem_type_manager *man; |
||
690 | uint32_t mem_type = TTM_PL_SYSTEM; |
||
691 | uint32_t cur_flags = 0; |
||
692 | bool type_found = false; |
||
693 | bool type_ok = false; |
||
694 | bool has_erestartsys = false; |
||
695 | int i, ret; |
||
696 | |||
697 | mem->mm_node = NULL; |
||
698 | for (i = 0; i < placement->num_placement; ++i) { |
||
699 | ret = ttm_mem_type_from_flags(placement->placement[i], |
||
700 | &mem_type); |
||
701 | if (ret) |
||
702 | return ret; |
||
703 | man = &bdev->man[mem_type]; |
||
704 | |||
705 | type_ok = ttm_bo_mt_compatible(man, |
||
706 | mem_type, |
||
707 | placement->placement[i], |
||
708 | &cur_flags); |
||
709 | |||
710 | if (!type_ok) |
||
711 | continue; |
||
712 | |||
713 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
||
714 | cur_flags); |
||
715 | /* |
||
716 | * Use the access and other non-mapping-related flag bits from |
||
717 | * the memory placement flags to the current flags |
||
718 | */ |
||
719 | ttm_flag_masked(&cur_flags, placement->placement[i], |
||
720 | ~TTM_PL_MASK_MEMTYPE); |
||
721 | |||
722 | if (mem_type == TTM_PL_SYSTEM) |
||
723 | break; |
||
724 | |||
725 | if (man->has_type && man->use_type) { |
||
726 | type_found = true; |
||
5078 | serge | 727 | ret = (*man->func->get_node)(man, bo, placement, |
728 | cur_flags, mem); |
||
4075 | Serge | 729 | if (unlikely(ret)) |
730 | return ret; |
||
731 | } |
||
732 | if (mem->mm_node) |
||
733 | break; |
||
734 | } |
||
735 | |||
736 | if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) { |
||
737 | mem->mem_type = mem_type; |
||
738 | mem->placement = cur_flags; |
||
739 | return 0; |
||
740 | } |
||
741 | |||
742 | if (!type_found) |
||
743 | return -EINVAL; |
||
744 | |||
745 | for (i = 0; i < placement->num_busy_placement; ++i) { |
||
746 | ret = ttm_mem_type_from_flags(placement->busy_placement[i], |
||
747 | &mem_type); |
||
748 | if (ret) |
||
749 | return ret; |
||
750 | man = &bdev->man[mem_type]; |
||
751 | if (!man->has_type) |
||
752 | continue; |
||
753 | if (!ttm_bo_mt_compatible(man, |
||
754 | mem_type, |
||
755 | placement->busy_placement[i], |
||
756 | &cur_flags)) |
||
757 | continue; |
||
758 | |||
759 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
||
760 | cur_flags); |
||
761 | /* |
||
762 | * Use the access and other non-mapping-related flag bits from |
||
763 | * the memory placement flags to the current flags |
||
764 | */ |
||
765 | ttm_flag_masked(&cur_flags, placement->busy_placement[i], |
||
766 | ~TTM_PL_MASK_MEMTYPE); |
||
767 | |||
768 | if (mem_type == TTM_PL_SYSTEM) { |
||
769 | mem->mem_type = mem_type; |
||
770 | mem->placement = cur_flags; |
||
771 | mem->mm_node = NULL; |
||
772 | return 0; |
||
773 | } |
||
774 | |||
775 | ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem, |
||
776 | interruptible, no_wait_gpu); |
||
777 | if (ret == 0 && mem->mm_node) { |
||
778 | mem->placement = cur_flags; |
||
779 | return 0; |
||
780 | } |
||
781 | if (ret == -ERESTARTSYS) |
||
782 | has_erestartsys = true; |
||
783 | } |
||
784 | ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; |
||
785 | return ret; |
||
786 | } |
||
787 | EXPORT_SYMBOL(ttm_bo_mem_space); |
||
788 | |||
4569 | Serge | 789 | static int ttm_bo_move_buffer(struct ttm_buffer_object *bo, |
4075 | Serge | 790 | struct ttm_placement *placement, |
791 | bool interruptible, |
||
792 | bool no_wait_gpu) |
||
793 | { |
||
794 | int ret = 0; |
||
795 | struct ttm_mem_reg mem; |
||
796 | struct ttm_bo_device *bdev = bo->bdev; |
||
797 | |||
5078 | serge | 798 | lockdep_assert_held(&bo->resv->lock.base); |
4075 | Serge | 799 | |
800 | /* |
||
801 | * FIXME: It's possible to pipeline buffer moves. |
||
802 | * Have the driver move function wait for idle when necessary, |
||
803 | * instead of doing it here. |
||
804 | */ |
||
805 | spin_lock(&bdev->fence_lock); |
||
806 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
||
807 | spin_unlock(&bdev->fence_lock); |
||
808 | if (ret) |
||
809 | return ret; |
||
810 | mem.num_pages = bo->num_pages; |
||
811 | mem.size = mem.num_pages << PAGE_SHIFT; |
||
812 | mem.page_alignment = bo->mem.page_alignment; |
||
813 | mem.bus.io_reserved_vm = false; |
||
814 | mem.bus.io_reserved_count = 0; |
||
815 | /* |
||
816 | * Determine where to move the buffer. |
||
817 | */ |
||
818 | ret = ttm_bo_mem_space(bo, placement, &mem, |
||
819 | interruptible, no_wait_gpu); |
||
820 | if (ret) |
||
821 | goto out_unlock; |
||
822 | ret = ttm_bo_handle_move_mem(bo, &mem, false, |
||
823 | interruptible, no_wait_gpu); |
||
824 | out_unlock: |
||
825 | if (ret && mem.mm_node) |
||
826 | ttm_bo_mem_put(bo, &mem); |
||
827 | return ret; |
||
828 | } |
||
829 | |||
4569 | Serge | 830 | static bool ttm_bo_mem_compat(struct ttm_placement *placement, |
831 | struct ttm_mem_reg *mem, |
||
832 | uint32_t *new_flags) |
||
4075 | Serge | 833 | { |
834 | int i; |
||
835 | |||
836 | if (mem->mm_node && placement->lpfn != 0 && |
||
837 | (mem->start < placement->fpfn || |
||
838 | mem->start + mem->num_pages > placement->lpfn)) |
||
4569 | Serge | 839 | return false; |
4075 | Serge | 840 | |
841 | for (i = 0; i < placement->num_placement; i++) { |
||
4569 | Serge | 842 | *new_flags = placement->placement[i]; |
843 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
||
844 | (*new_flags & mem->placement & TTM_PL_MASK_MEM)) |
||
845 | return true; |
||
4075 | Serge | 846 | } |
4569 | Serge | 847 | |
848 | for (i = 0; i < placement->num_busy_placement; i++) { |
||
849 | *new_flags = placement->busy_placement[i]; |
||
850 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
||
851 | (*new_flags & mem->placement & TTM_PL_MASK_MEM)) |
||
852 | return true; |
||
853 | } |
||
854 | |||
855 | return false; |
||
4075 | Serge | 856 | } |
857 | |||
858 | int ttm_bo_validate(struct ttm_buffer_object *bo, |
||
859 | struct ttm_placement *placement, |
||
860 | bool interruptible, |
||
861 | bool no_wait_gpu) |
||
862 | { |
||
863 | int ret; |
||
4569 | Serge | 864 | uint32_t new_flags; |
4075 | Serge | 865 | |
5078 | serge | 866 | lockdep_assert_held(&bo->resv->lock.base); |
4075 | Serge | 867 | /* Check that range is valid */ |
868 | if (placement->lpfn || placement->fpfn) |
||
869 | if (placement->fpfn > placement->lpfn || |
||
870 | (placement->lpfn - placement->fpfn) < bo->num_pages) |
||
871 | return -EINVAL; |
||
872 | /* |
||
873 | * Check whether we need to move buffer. |
||
874 | */ |
||
4569 | Serge | 875 | if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { |
5078 | serge | 876 | ret = ttm_bo_move_buffer(bo, placement, interruptible, |
877 | no_wait_gpu); |
||
4075 | Serge | 878 | if (ret) |
879 | return ret; |
||
880 | } else { |
||
881 | /* |
||
882 | * Use the access and other non-mapping-related flag bits from |
||
883 | * the compatible memory placement flags to the active flags |
||
884 | */ |
||
4569 | Serge | 885 | ttm_flag_masked(&bo->mem.placement, new_flags, |
4075 | Serge | 886 | ~TTM_PL_MASK_MEMTYPE); |
887 | } |
||
888 | /* |
||
889 | * We might need to add a TTM. |
||
890 | */ |
||
891 | if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
||
892 | ret = ttm_bo_add_ttm(bo, true); |
||
893 | if (ret) |
||
894 | return ret; |
||
895 | } |
||
896 | return 0; |
||
897 | } |
||
898 | EXPORT_SYMBOL(ttm_bo_validate); |
||
899 | |||
900 | int ttm_bo_check_placement(struct ttm_buffer_object *bo, |
||
901 | struct ttm_placement *placement) |
||
902 | { |
||
903 | BUG_ON((placement->fpfn || placement->lpfn) && |
||
904 | (bo->mem.num_pages > (placement->lpfn - placement->fpfn))); |
||
905 | |||
906 | return 0; |
||
907 | } |
||
908 | |||
909 | int ttm_bo_init(struct ttm_bo_device *bdev, |
||
910 | struct ttm_buffer_object *bo, |
||
911 | unsigned long size, |
||
912 | enum ttm_bo_type type, |
||
913 | struct ttm_placement *placement, |
||
914 | uint32_t page_alignment, |
||
915 | bool interruptible, |
||
916 | struct file *persistent_swap_storage, |
||
917 | size_t acc_size, |
||
918 | struct sg_table *sg, |
||
919 | void (*destroy) (struct ttm_buffer_object *)) |
||
920 | { |
||
921 | int ret = 0; |
||
922 | unsigned long num_pages; |
||
923 | bool locked; |
||
924 | |||
925 | num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
||
926 | if (num_pages == 0) { |
||
927 | pr_err("Illegal buffer object size\n"); |
||
928 | if (destroy) |
||
929 | (*destroy)(bo); |
||
930 | else |
||
931 | kfree(bo); |
||
932 | return -EINVAL; |
||
933 | } |
||
934 | bo->destroy = destroy; |
||
935 | |||
936 | kref_init(&bo->kref); |
||
937 | kref_init(&bo->list_kref); |
||
938 | atomic_set(&bo->cpu_writers, 0); |
||
939 | INIT_LIST_HEAD(&bo->lru); |
||
940 | INIT_LIST_HEAD(&bo->ddestroy); |
||
941 | INIT_LIST_HEAD(&bo->swap); |
||
942 | INIT_LIST_HEAD(&bo->io_reserve_lru); |
||
4569 | Serge | 943 | mutex_init(&bo->wu_mutex); |
4075 | Serge | 944 | bo->bdev = bdev; |
945 | bo->glob = bdev->glob; |
||
946 | bo->type = type; |
||
947 | bo->num_pages = num_pages; |
||
948 | bo->mem.size = num_pages << PAGE_SHIFT; |
||
949 | bo->mem.mem_type = TTM_PL_SYSTEM; |
||
950 | bo->mem.num_pages = bo->num_pages; |
||
951 | bo->mem.mm_node = NULL; |
||
952 | bo->mem.page_alignment = page_alignment; |
||
953 | bo->mem.bus.io_reserved_vm = false; |
||
954 | bo->mem.bus.io_reserved_count = 0; |
||
955 | bo->priv_flags = 0; |
||
956 | bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); |
||
957 | bo->persistent_swap_storage = persistent_swap_storage; |
||
958 | bo->acc_size = acc_size; |
||
959 | bo->sg = sg; |
||
960 | bo->resv = &bo->ttm_resv; |
||
5078 | serge | 961 | reservation_object_init(bo->resv); |
4075 | Serge | 962 | atomic_inc(&bo->glob->bo_count); |
4112 | Serge | 963 | drm_vma_node_reset(&bo->vma_node); |
4075 | Serge | 964 | |
965 | ret = ttm_bo_check_placement(bo, placement); |
||
966 | |||
967 | /* |
||
968 | * For ttm_bo_type_device buffers, allocate |
||
969 | * address space from the device. |
||
970 | */ |
||
5078 | serge | 971 | if (likely(!ret) && |
972 | (bo->type == ttm_bo_type_device || |
||
973 | bo->type == ttm_bo_type_sg)) |
||
974 | ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node, |
||
975 | bo->mem.num_pages); |
||
4075 | Serge | 976 | |
5078 | serge | 977 | locked = ww_mutex_trylock(&bo->resv->lock); |
978 | WARN_ON(!locked); |
||
4075 | Serge | 979 | |
5078 | serge | 980 | if (likely(!ret)) |
981 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
||
4075 | Serge | 982 | |
5078 | serge | 983 | ttm_bo_unreserve(bo); |
4075 | Serge | 984 | |
5078 | serge | 985 | if (unlikely(ret)) |
986 | ttm_bo_unref(&bo); |
||
987 | |||
4075 | Serge | 988 | return ret; |
989 | } |
||
990 | EXPORT_SYMBOL(ttm_bo_init); |
||
991 | |||
992 | size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, |
||
993 | unsigned long bo_size, |
||
994 | unsigned struct_size) |
||
995 | { |
||
996 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
||
997 | size_t size = 0; |
||
998 | |||
999 | size += ttm_round_pot(struct_size); |
||
1000 | size += PAGE_ALIGN(npages * sizeof(void *)); |
||
1001 | size += ttm_round_pot(sizeof(struct ttm_tt)); |
||
1002 | return size; |
||
1003 | } |
||
1004 | EXPORT_SYMBOL(ttm_bo_acc_size); |
||
1005 | |||
1006 | size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, |
||
1007 | unsigned long bo_size, |
||
1008 | unsigned struct_size) |
||
1009 | { |
||
1010 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
||
1011 | size_t size = 0; |
||
1012 | |||
1013 | size += ttm_round_pot(struct_size); |
||
1014 | size += PAGE_ALIGN(npages * sizeof(void *)); |
||
1015 | size += PAGE_ALIGN(npages * sizeof(dma_addr_t)); |
||
1016 | size += ttm_round_pot(sizeof(struct ttm_dma_tt)); |
||
1017 | return size; |
||
1018 | } |
||
1019 | EXPORT_SYMBOL(ttm_bo_dma_acc_size); |
||
1020 | |||
1404 | serge | 1021 | int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, |
4075 | Serge | 1022 | unsigned long p_size) |
1404 | serge | 1023 | { |
1024 | int ret = -EINVAL; |
||
1025 | struct ttm_mem_type_manager *man; |
||
1026 | |||
4075 | Serge | 1027 | BUG_ON(type >= TTM_NUM_MEM_TYPES); |
1404 | serge | 1028 | man = &bdev->man[type]; |
4075 | Serge | 1029 | BUG_ON(man->has_type); |
1030 | man->io_reserve_fastpath = true; |
||
1031 | man->use_io_reserve_lru = false; |
||
1032 | mutex_init(&man->io_reserve_mutex); |
||
1033 | INIT_LIST_HEAD(&man->io_reserve_lru); |
||
1404 | serge | 1034 | |
1035 | ret = bdev->driver->init_mem_type(bdev, type, man); |
||
1036 | if (ret) |
||
1037 | return ret; |
||
4075 | Serge | 1038 | man->bdev = bdev; |
1404 | serge | 1039 | |
1040 | ret = 0; |
||
1041 | if (type != TTM_PL_SYSTEM) { |
||
4075 | Serge | 1042 | ret = (*man->func->init)(man, p_size); |
1404 | serge | 1043 | if (ret) |
1044 | return ret; |
||
1045 | } |
||
1046 | man->has_type = true; |
||
1047 | man->use_type = true; |
||
1048 | man->size = p_size; |
||
1049 | |||
1050 | INIT_LIST_HEAD(&man->lru); |
||
1051 | |||
1052 | return 0; |
||
1053 | } |
||
5078 | serge | 1054 | EXPORT_SYMBOL(ttm_bo_init_mm); |
4075 | Serge | 1055 | void ttm_bo_global_release(struct drm_global_reference *ref) |
1404 | serge | 1056 | { |
4075 | Serge | 1057 | struct ttm_bo_global *glob = ref->object; |
1058 | |||
1059 | } |
||
1060 | EXPORT_SYMBOL(ttm_bo_global_release); |
||
1061 | |||
1062 | int ttm_bo_global_init(struct drm_global_reference *ref) |
||
1063 | { |
||
1404 | serge | 1064 | struct ttm_bo_global_ref *bo_ref = |
1065 | container_of(ref, struct ttm_bo_global_ref, ref); |
||
1066 | struct ttm_bo_global *glob = ref->object; |
||
1067 | int ret; |
||
1068 | |||
4075 | Serge | 1069 | mutex_init(&glob->device_list_mutex); |
1070 | spin_lock_init(&glob->lru_lock); |
||
1404 | serge | 1071 | glob->mem_glob = bo_ref->mem_glob; |
5078 | serge | 1072 | glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); |
1404 | serge | 1073 | |
1074 | if (unlikely(glob->dummy_read_page == NULL)) { |
||
1075 | ret = -ENOMEM; |
||
1076 | goto out_no_drp; |
||
1077 | } |
||
1078 | |||
1079 | INIT_LIST_HEAD(&glob->swap_lru); |
||
1080 | INIT_LIST_HEAD(&glob->device_list); |
||
1081 | |||
4075 | Serge | 1082 | atomic_set(&glob->bo_count, 0); |
1404 | serge | 1083 | |
4075 | Serge | 1084 | return 0; |
1404 | serge | 1085 | |
1086 | out_no_drp: |
||
1087 | kfree(glob); |
||
1088 | return ret; |
||
1089 | } |
||
4112 | Serge | 1090 | EXPORT_SYMBOL(ttm_bo_global_init); |
1404 | serge | 1091 | |
4075 | Serge | 1092 | int ttm_bo_device_init(struct ttm_bo_device *bdev, |
1093 | struct ttm_bo_global *glob, |
||
1094 | struct ttm_bo_driver *driver, |
||
5078 | serge | 1095 | struct address_space *mapping, |
4075 | Serge | 1096 | uint64_t file_page_offset, |
1097 | bool need_dma32) |
||
1098 | { |
||
1099 | int ret = -EINVAL; |
||
1100 | |||
1101 | bdev->driver = driver; |
||
1102 | |||
1103 | memset(bdev->man, 0, sizeof(bdev->man)); |
||
1104 | |||
1105 | /* |
||
1106 | * Initialize the system memory buffer type. |
||
1107 | * Other types need to be driver / IOCTL initialized. |
||
1108 | */ |
||
1109 | ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); |
||
1110 | if (unlikely(ret != 0)) |
||
1111 | goto out_no_sys; |
||
1112 | |||
4112 | Serge | 1113 | drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset, |
1114 | 0x10000000); |
||
5078 | serge | 1115 | INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); |
4075 | Serge | 1116 | INIT_LIST_HEAD(&bdev->ddestroy); |
5078 | serge | 1117 | bdev->dev_mapping = mapping; |
4075 | Serge | 1118 | bdev->glob = glob; |
1119 | bdev->need_dma32 = need_dma32; |
||
1120 | bdev->val_seq = 0; |
||
1121 | spin_lock_init(&bdev->fence_lock); |
||
1122 | mutex_lock(&glob->device_list_mutex); |
||
1123 | list_add_tail(&bdev->device_list, &glob->device_list); |
||
1124 | mutex_unlock(&glob->device_list_mutex); |
||
1125 | |||
1126 | return 0; |
||
1127 | out_no_sys: |
||
1128 | return ret; |
||
1129 | } |
||
1130 | EXPORT_SYMBOL(ttm_bo_device_init); |
||
1131 | |||
1132 | /* |
||
1133 | * buffer object vm functions. |
||
1134 | */ |
||
1135 | |||
1136 | bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
||
1137 | { |
||
1138 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
||
1139 | |||
1140 | if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
||
1141 | if (mem->mem_type == TTM_PL_SYSTEM) |
||
1142 | return false; |
||
1143 | |||
1144 | if (man->flags & TTM_MEMTYPE_FLAG_CMA) |
||
1145 | return false; |
||
1146 | |||
1147 | if (mem->placement & TTM_PL_FLAG_CACHED) |
||
1148 | return false; |
||
1149 | } |
||
1150 | return true; |
||
1151 | } |
||
1152 | |||
5078 | serge | 1153 | void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) |
1154 | { |
||
1155 | struct ttm_bo_device *bdev = bo->bdev; |
||
1156 | |||
1157 | drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping); |
||
1158 | ttm_mem_io_free_vm(bo); |
||
1159 | } |
||
1160 | |||
1161 | void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) |
||
1162 | { |
||
1163 | struct ttm_bo_device *bdev = bo->bdev; |
||
1164 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
||
1165 | |||
1166 | ttm_mem_io_lock(man, false); |
||
1167 | ttm_bo_unmap_virtual_locked(bo); |
||
1168 | ttm_mem_io_unlock(man); |
||
1169 | } |
||
1170 | |||
1171 | |||
1172 | EXPORT_SYMBOL(ttm_bo_unmap_virtual); |
||
1173 | |||
1174 | |||
4569 | Serge | 1175 | int ttm_bo_wait(struct ttm_buffer_object *bo, |
1176 | bool lazy, bool interruptible, bool no_wait) |
||
1177 | { |
||
1178 | struct ttm_bo_driver *driver = bo->bdev->driver; |
||
1179 | struct ttm_bo_device *bdev = bo->bdev; |
||
1180 | void *sync_obj; |
||
1181 | int ret = 0; |
||
1182 | |||
1183 | if (likely(bo->sync_obj == NULL)) |
||
1184 | return 0; |
||
1185 | |||
5078 | serge | 1186 | while (bo->sync_obj) { |
4569 | Serge | 1187 | |
5078 | serge | 1188 | if (driver->sync_obj_signaled(bo->sync_obj)) { |
1189 | void *tmp_obj = bo->sync_obj; |
||
1190 | bo->sync_obj = NULL; |
||
1191 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags); |
||
1192 | spin_unlock(&bdev->fence_lock); |
||
1193 | driver->sync_obj_unref(&tmp_obj); |
||
1194 | spin_lock(&bdev->fence_lock); |
||
1195 | continue; |
||
1196 | } |
||
4569 | Serge | 1197 | |
5078 | serge | 1198 | if (no_wait) |
1199 | return -EBUSY; |
||
4569 | Serge | 1200 | |
5078 | serge | 1201 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
1202 | spin_unlock(&bdev->fence_lock); |
||
1203 | ret = driver->sync_obj_wait(sync_obj, |
||
1204 | lazy, interruptible); |
||
1205 | if (unlikely(ret != 0)) { |
||
1206 | driver->sync_obj_unref(&sync_obj); |
||
1207 | spin_lock(&bdev->fence_lock); |
||
4569 | Serge | 1208 | return ret; |
5078 | serge | 1209 | } |
1210 | spin_lock(&bdev->fence_lock); |
||
1211 | if (likely(bo->sync_obj == sync_obj)) { |
||
1212 | void *tmp_obj = bo->sync_obj; |
||
1213 | bo->sync_obj = NULL; |
||
1214 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, |
||
1215 | &bo->priv_flags); |
||
1216 | spin_unlock(&bdev->fence_lock); |
||
1217 | driver->sync_obj_unref(&sync_obj); |
||
1218 | driver->sync_obj_unref(&tmp_obj); |
||
1219 | spin_lock(&bdev->fence_lock); |
||
1220 | } else { |
||
1221 | spin_unlock(&bdev->fence_lock); |
||
1222 | driver->sync_obj_unref(&sync_obj); |
||
1223 | spin_lock(&bdev->fence_lock); |
||
1224 | } |
||
1225 | } |
||
1226 | return 0; |
||
4569 | Serge | 1227 | } |
5078 | serge | 1228 | EXPORT_SYMBOL(ttm_bo_wait);><>>>>>><>>>>>><>><>><>><>><>=> |
4569 | Serge | 1229 |