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