1592ffb21SWarner Losh /**************************************************************************
2592ffb21SWarner Losh *
3592ffb21SWarner Losh * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4592ffb21SWarner Losh * All Rights Reserved.
5592ffb21SWarner Losh *
6592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
7592ffb21SWarner Losh * copy of this software and associated documentation files (the
8592ffb21SWarner Losh * "Software"), to deal in the Software without restriction, including
9592ffb21SWarner Losh * without limitation the rights to use, copy, modify, merge, publish,
10592ffb21SWarner Losh * distribute, sub license, and/or sell copies of the Software, and to
11592ffb21SWarner Losh * permit persons to whom the Software is furnished to do so, subject to
12592ffb21SWarner Losh * the following conditions:
13592ffb21SWarner Losh *
14592ffb21SWarner Losh * The above copyright notice and this permission notice (including the
15592ffb21SWarner Losh * next paragraph) shall be included in all copies or substantial portions
16592ffb21SWarner Losh * of the Software.
17592ffb21SWarner Losh *
18592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21592ffb21SWarner Losh * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22592ffb21SWarner Losh * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23592ffb21SWarner Losh * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24592ffb21SWarner Losh * USE OR OTHER DEALINGS IN THE SOFTWARE.
25592ffb21SWarner Losh *
26592ffb21SWarner Losh **************************************************************************/
27592ffb21SWarner Losh /*
28592ffb21SWarner Losh * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29592ffb21SWarner Losh */
30592ffb21SWarner Losh
31592ffb21SWarner Losh #include <sys/cdefs.h>
32592ffb21SWarner Losh #include <dev/drm2/drmP.h>
33592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_module.h>
34592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_bo_driver.h>
35592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_placement.h>
36592ffb21SWarner Losh #include <vm/vm_pageout.h>
37592ffb21SWarner Losh
38592ffb21SWarner Losh #define TTM_ASSERT_LOCKED(param)
39592ffb21SWarner Losh #define TTM_DEBUG(fmt, arg...)
40592ffb21SWarner Losh #define TTM_BO_HASH_ORDER 13
41592ffb21SWarner Losh
42592ffb21SWarner Losh static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
43592ffb21SWarner Losh static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
44592ffb21SWarner Losh static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob);
45592ffb21SWarner Losh
46592ffb21SWarner Losh MALLOC_DEFINE(M_TTM_BO, "ttm_bo", "TTM Buffer Objects");
47592ffb21SWarner Losh
ttm_mem_type_from_flags(uint32_t flags,uint32_t * mem_type)48592ffb21SWarner Losh static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
49592ffb21SWarner Losh {
50592ffb21SWarner Losh int i;
51592ffb21SWarner Losh
52592ffb21SWarner Losh for (i = 0; i <= TTM_PL_PRIV5; i++)
53592ffb21SWarner Losh if (flags & (1 << i)) {
54592ffb21SWarner Losh *mem_type = i;
55592ffb21SWarner Losh return 0;
56592ffb21SWarner Losh }
57592ffb21SWarner Losh return -EINVAL;
58592ffb21SWarner Losh }
59592ffb21SWarner Losh
ttm_mem_type_debug(struct ttm_bo_device * bdev,int mem_type)60592ffb21SWarner Losh static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
61592ffb21SWarner Losh {
62592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bdev->man[mem_type];
63592ffb21SWarner Losh
64592ffb21SWarner Losh printf(" has_type: %d\n", man->has_type);
65592ffb21SWarner Losh printf(" use_type: %d\n", man->use_type);
66592ffb21SWarner Losh printf(" flags: 0x%08X\n", man->flags);
67592ffb21SWarner Losh printf(" gpu_offset: 0x%08lX\n", man->gpu_offset);
68592ffb21SWarner Losh printf(" size: %ju\n", (uintmax_t)man->size);
69592ffb21SWarner Losh printf(" available_caching: 0x%08X\n", man->available_caching);
70592ffb21SWarner Losh printf(" default_caching: 0x%08X\n", man->default_caching);
71592ffb21SWarner Losh if (mem_type != TTM_PL_SYSTEM)
72592ffb21SWarner Losh (*man->func->debug)(man, TTM_PFX);
73592ffb21SWarner Losh }
74592ffb21SWarner Losh
ttm_bo_mem_space_debug(struct ttm_buffer_object * bo,struct ttm_placement * placement)75592ffb21SWarner Losh static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
76592ffb21SWarner Losh struct ttm_placement *placement)
77592ffb21SWarner Losh {
78592ffb21SWarner Losh int i, ret, mem_type;
79592ffb21SWarner Losh
80592ffb21SWarner Losh printf("No space for %p (%lu pages, %luK, %luM)\n",
81592ffb21SWarner Losh bo, bo->mem.num_pages, bo->mem.size >> 10,
82592ffb21SWarner Losh bo->mem.size >> 20);
83592ffb21SWarner Losh for (i = 0; i < placement->num_placement; i++) {
84592ffb21SWarner Losh ret = ttm_mem_type_from_flags(placement->placement[i],
85592ffb21SWarner Losh &mem_type);
86592ffb21SWarner Losh if (ret)
87592ffb21SWarner Losh return;
88592ffb21SWarner Losh printf(" placement[%d]=0x%08X (%d)\n",
89592ffb21SWarner Losh i, placement->placement[i], mem_type);
90592ffb21SWarner Losh ttm_mem_type_debug(bo->bdev, mem_type);
91592ffb21SWarner Losh }
92592ffb21SWarner Losh }
93592ffb21SWarner Losh
94592ffb21SWarner Losh #if 0
95592ffb21SWarner Losh static ssize_t ttm_bo_global_show(struct ttm_bo_global *glob,
96592ffb21SWarner Losh char *buffer)
97592ffb21SWarner Losh {
98592ffb21SWarner Losh
99592ffb21SWarner Losh return snprintf(buffer, PAGE_SIZE, "%lu\n",
100592ffb21SWarner Losh (unsigned long) atomic_read(&glob->bo_count));
101592ffb21SWarner Losh }
102592ffb21SWarner Losh #endif
103592ffb21SWarner Losh
ttm_bo_type_flags(unsigned type)104592ffb21SWarner Losh static inline uint32_t ttm_bo_type_flags(unsigned type)
105592ffb21SWarner Losh {
106592ffb21SWarner Losh return 1 << (type);
107592ffb21SWarner Losh }
108592ffb21SWarner Losh
ttm_bo_release_list(struct ttm_buffer_object * bo)109592ffb21SWarner Losh static void ttm_bo_release_list(struct ttm_buffer_object *bo)
110592ffb21SWarner Losh {
111592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
112592ffb21SWarner Losh size_t acc_size = bo->acc_size;
113592ffb21SWarner Losh
114592ffb21SWarner Losh MPASS(atomic_read(&bo->list_kref) == 0);
115592ffb21SWarner Losh MPASS(atomic_read(&bo->kref) == 0);
116592ffb21SWarner Losh MPASS(atomic_read(&bo->cpu_writers) == 0);
117592ffb21SWarner Losh MPASS(bo->sync_obj == NULL);
118592ffb21SWarner Losh MPASS(bo->mem.mm_node == NULL);
119592ffb21SWarner Losh MPASS(list_empty(&bo->lru));
120592ffb21SWarner Losh MPASS(list_empty(&bo->ddestroy));
121592ffb21SWarner Losh
122592ffb21SWarner Losh if (bo->ttm)
123592ffb21SWarner Losh ttm_tt_destroy(bo->ttm);
124592ffb21SWarner Losh atomic_dec(&bo->glob->bo_count);
125592ffb21SWarner Losh if (bo->destroy)
126592ffb21SWarner Losh bo->destroy(bo);
127592ffb21SWarner Losh else {
128592ffb21SWarner Losh free(bo, M_TTM_BO);
129592ffb21SWarner Losh }
130592ffb21SWarner Losh ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
131592ffb21SWarner Losh }
132592ffb21SWarner Losh
133592ffb21SWarner Losh static int
ttm_bo_wait_unreserved_locked(struct ttm_buffer_object * bo,bool interruptible)134592ffb21SWarner Losh ttm_bo_wait_unreserved_locked(struct ttm_buffer_object *bo, bool interruptible)
135592ffb21SWarner Losh {
136592ffb21SWarner Losh const char *wmsg;
137592ffb21SWarner Losh int flags, ret;
138592ffb21SWarner Losh
139592ffb21SWarner Losh ret = 0;
140592ffb21SWarner Losh if (interruptible) {
141592ffb21SWarner Losh flags = PCATCH;
142592ffb21SWarner Losh wmsg = "ttbowi";
143592ffb21SWarner Losh } else {
144592ffb21SWarner Losh flags = 0;
145592ffb21SWarner Losh wmsg = "ttbowu";
146592ffb21SWarner Losh }
147592ffb21SWarner Losh while (ttm_bo_is_reserved(bo)) {
148592ffb21SWarner Losh ret = -msleep(bo, &bo->glob->lru_lock, flags, wmsg, 0);
149592ffb21SWarner Losh if (ret == -EINTR || ret == -ERESTART)
150592ffb21SWarner Losh ret = -ERESTARTSYS;
151592ffb21SWarner Losh if (ret != 0)
152592ffb21SWarner Losh break;
153592ffb21SWarner Losh }
154592ffb21SWarner Losh return (ret);
155592ffb21SWarner Losh }
156592ffb21SWarner Losh
ttm_bo_add_to_lru(struct ttm_buffer_object * bo)157592ffb21SWarner Losh void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
158592ffb21SWarner Losh {
159592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
160592ffb21SWarner Losh struct ttm_mem_type_manager *man;
161592ffb21SWarner Losh
162592ffb21SWarner Losh MPASS(ttm_bo_is_reserved(bo));
163592ffb21SWarner Losh
164592ffb21SWarner Losh if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
165592ffb21SWarner Losh
166592ffb21SWarner Losh MPASS(list_empty(&bo->lru));
167592ffb21SWarner Losh
168592ffb21SWarner Losh man = &bdev->man[bo->mem.mem_type];
169592ffb21SWarner Losh list_add_tail(&bo->lru, &man->lru);
170592ffb21SWarner Losh refcount_acquire(&bo->list_kref);
171592ffb21SWarner Losh
172592ffb21SWarner Losh if (bo->ttm != NULL) {
173592ffb21SWarner Losh list_add_tail(&bo->swap, &bo->glob->swap_lru);
174592ffb21SWarner Losh refcount_acquire(&bo->list_kref);
175592ffb21SWarner Losh }
176592ffb21SWarner Losh }
177592ffb21SWarner Losh }
178592ffb21SWarner Losh
ttm_bo_del_from_lru(struct ttm_buffer_object * bo)179592ffb21SWarner Losh int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
180592ffb21SWarner Losh {
181592ffb21SWarner Losh int put_count = 0;
182592ffb21SWarner Losh
183592ffb21SWarner Losh if (!list_empty(&bo->swap)) {
184592ffb21SWarner Losh list_del_init(&bo->swap);
185592ffb21SWarner Losh ++put_count;
186592ffb21SWarner Losh }
187592ffb21SWarner Losh if (!list_empty(&bo->lru)) {
188592ffb21SWarner Losh list_del_init(&bo->lru);
189592ffb21SWarner Losh ++put_count;
190592ffb21SWarner Losh }
191592ffb21SWarner Losh
192592ffb21SWarner Losh /*
193592ffb21SWarner Losh * TODO: Add a driver hook to delete from
194592ffb21SWarner Losh * driver-specific LRU's here.
195592ffb21SWarner Losh */
196592ffb21SWarner Losh
197592ffb21SWarner Losh return put_count;
198592ffb21SWarner Losh }
199592ffb21SWarner Losh
ttm_bo_reserve_nolru(struct ttm_buffer_object * bo,bool interruptible,bool no_wait,bool use_sequence,uint32_t sequence)200592ffb21SWarner Losh int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
201592ffb21SWarner Losh bool interruptible,
202592ffb21SWarner Losh bool no_wait, bool use_sequence, uint32_t sequence)
203592ffb21SWarner Losh {
204592ffb21SWarner Losh int ret;
205592ffb21SWarner Losh
206592ffb21SWarner Losh while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
207592ffb21SWarner Losh /**
208592ffb21SWarner Losh * Deadlock avoidance for multi-bo reserving.
209592ffb21SWarner Losh */
210592ffb21SWarner Losh if (use_sequence && bo->seq_valid) {
211592ffb21SWarner Losh /**
212592ffb21SWarner Losh * We've already reserved this one.
213592ffb21SWarner Losh */
214592ffb21SWarner Losh if (unlikely(sequence == bo->val_seq))
215592ffb21SWarner Losh return -EDEADLK;
216592ffb21SWarner Losh /**
217592ffb21SWarner Losh * Already reserved by a thread that will not back
218592ffb21SWarner Losh * off for us. We need to back off.
219592ffb21SWarner Losh */
220592ffb21SWarner Losh if (unlikely(sequence - bo->val_seq < (1U << 31)))
221592ffb21SWarner Losh return -EAGAIN;
222592ffb21SWarner Losh }
223592ffb21SWarner Losh
224592ffb21SWarner Losh if (no_wait)
225592ffb21SWarner Losh return -EBUSY;
226592ffb21SWarner Losh
227592ffb21SWarner Losh ret = ttm_bo_wait_unreserved_locked(bo, interruptible);
228592ffb21SWarner Losh
229592ffb21SWarner Losh if (unlikely(ret))
230592ffb21SWarner Losh return ret;
231592ffb21SWarner Losh }
232592ffb21SWarner Losh
233592ffb21SWarner Losh if (use_sequence) {
234592ffb21SWarner Losh bool wake_up = false;
235592ffb21SWarner Losh /**
236592ffb21SWarner Losh * Wake up waiters that may need to recheck for deadlock,
237592ffb21SWarner Losh * if we decreased the sequence number.
238592ffb21SWarner Losh */
239592ffb21SWarner Losh if (unlikely((bo->val_seq - sequence < (1U << 31))
240592ffb21SWarner Losh || !bo->seq_valid))
241592ffb21SWarner Losh wake_up = true;
242592ffb21SWarner Losh
243592ffb21SWarner Losh /*
244592ffb21SWarner Losh * In the worst case with memory ordering these values can be
245592ffb21SWarner Losh * seen in the wrong order. However since we call wake_up_all
246592ffb21SWarner Losh * in that case, this will hopefully not pose a problem,
247592ffb21SWarner Losh * and the worst case would only cause someone to accidentally
248592ffb21SWarner Losh * hit -EAGAIN in ttm_bo_reserve when they see old value of
249592ffb21SWarner Losh * val_seq. However this would only happen if seq_valid was
250592ffb21SWarner Losh * written before val_seq was, and just means some slightly
251592ffb21SWarner Losh * increased cpu usage
252592ffb21SWarner Losh */
253592ffb21SWarner Losh bo->val_seq = sequence;
254592ffb21SWarner Losh bo->seq_valid = true;
255592ffb21SWarner Losh if (wake_up)
256592ffb21SWarner Losh wakeup(bo);
257592ffb21SWarner Losh } else {
258592ffb21SWarner Losh bo->seq_valid = false;
259592ffb21SWarner Losh }
260592ffb21SWarner Losh
261592ffb21SWarner Losh return 0;
262592ffb21SWarner Losh }
263592ffb21SWarner Losh
ttm_bo_list_ref_sub(struct ttm_buffer_object * bo,int count,bool never_free)264592ffb21SWarner Losh void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
265592ffb21SWarner Losh bool never_free)
266592ffb21SWarner Losh {
267592ffb21SWarner Losh u_int old;
268592ffb21SWarner Losh
269592ffb21SWarner Losh old = atomic_fetchadd_int(&bo->list_kref, -count);
270592ffb21SWarner Losh if (old <= count) {
271592ffb21SWarner Losh if (never_free)
272592ffb21SWarner Losh panic("ttm_bo_ref_buf");
273592ffb21SWarner Losh ttm_bo_release_list(bo);
274592ffb21SWarner Losh }
275592ffb21SWarner Losh }
276592ffb21SWarner Losh
ttm_bo_reserve(struct ttm_buffer_object * bo,bool interruptible,bool no_wait,bool use_sequence,uint32_t sequence)277592ffb21SWarner Losh int ttm_bo_reserve(struct ttm_buffer_object *bo,
278592ffb21SWarner Losh bool interruptible,
279592ffb21SWarner Losh bool no_wait, bool use_sequence, uint32_t sequence)
280592ffb21SWarner Losh {
281592ffb21SWarner Losh struct ttm_bo_global *glob = bo->glob;
282592ffb21SWarner Losh int put_count = 0;
283592ffb21SWarner Losh int ret;
284592ffb21SWarner Losh
285592ffb21SWarner Losh mtx_lock(&bo->glob->lru_lock);
286592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_sequence,
287592ffb21SWarner Losh sequence);
288592ffb21SWarner Losh if (likely(ret == 0)) {
289592ffb21SWarner Losh put_count = ttm_bo_del_from_lru(bo);
290592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
291592ffb21SWarner Losh ttm_bo_list_ref_sub(bo, put_count, true);
292592ffb21SWarner Losh } else
293592ffb21SWarner Losh mtx_unlock(&bo->glob->lru_lock);
294592ffb21SWarner Losh
295592ffb21SWarner Losh return ret;
296592ffb21SWarner Losh }
297592ffb21SWarner Losh
ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object * bo,bool interruptible,uint32_t sequence)298592ffb21SWarner Losh int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
299592ffb21SWarner Losh bool interruptible, uint32_t sequence)
300592ffb21SWarner Losh {
301592ffb21SWarner Losh bool wake_up = false;
302592ffb21SWarner Losh int ret;
303592ffb21SWarner Losh
304592ffb21SWarner Losh while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
305592ffb21SWarner Losh if (bo->seq_valid && sequence == bo->val_seq) {
306592ffb21SWarner Losh DRM_ERROR(
307592ffb21SWarner Losh "%s: bo->seq_valid && sequence == bo->val_seq",
308592ffb21SWarner Losh __func__);
309592ffb21SWarner Losh }
310592ffb21SWarner Losh
311592ffb21SWarner Losh ret = ttm_bo_wait_unreserved_locked(bo, interruptible);
312592ffb21SWarner Losh
313592ffb21SWarner Losh if (unlikely(ret))
314592ffb21SWarner Losh return ret;
315592ffb21SWarner Losh }
316592ffb21SWarner Losh
317592ffb21SWarner Losh if ((bo->val_seq - sequence < (1U << 31)) || !bo->seq_valid)
318592ffb21SWarner Losh wake_up = true;
319592ffb21SWarner Losh
320592ffb21SWarner Losh /**
321592ffb21SWarner Losh * Wake up waiters that may need to recheck for deadlock,
322592ffb21SWarner Losh * if we decreased the sequence number.
323592ffb21SWarner Losh */
324592ffb21SWarner Losh bo->val_seq = sequence;
325592ffb21SWarner Losh bo->seq_valid = true;
326592ffb21SWarner Losh if (wake_up)
327592ffb21SWarner Losh wakeup(bo);
328592ffb21SWarner Losh
329592ffb21SWarner Losh return 0;
330592ffb21SWarner Losh }
331592ffb21SWarner Losh
ttm_bo_reserve_slowpath(struct ttm_buffer_object * bo,bool interruptible,uint32_t sequence)332592ffb21SWarner Losh int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
333592ffb21SWarner Losh bool interruptible, uint32_t sequence)
334592ffb21SWarner Losh {
335592ffb21SWarner Losh struct ttm_bo_global *glob = bo->glob;
336592ffb21SWarner Losh int put_count, ret;
337592ffb21SWarner Losh
338592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
339592ffb21SWarner Losh ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence);
340592ffb21SWarner Losh if (likely(!ret)) {
341592ffb21SWarner Losh put_count = ttm_bo_del_from_lru(bo);
342592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
343592ffb21SWarner Losh ttm_bo_list_ref_sub(bo, put_count, true);
344592ffb21SWarner Losh } else
345592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
346592ffb21SWarner Losh return ret;
347592ffb21SWarner Losh }
348592ffb21SWarner Losh
ttm_bo_unreserve_locked(struct ttm_buffer_object * bo)349592ffb21SWarner Losh void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
350592ffb21SWarner Losh {
351592ffb21SWarner Losh ttm_bo_add_to_lru(bo);
352592ffb21SWarner Losh atomic_set(&bo->reserved, 0);
353592ffb21SWarner Losh wakeup(bo);
354592ffb21SWarner Losh }
355592ffb21SWarner Losh
ttm_bo_unreserve(struct ttm_buffer_object * bo)356592ffb21SWarner Losh void ttm_bo_unreserve(struct ttm_buffer_object *bo)
357592ffb21SWarner Losh {
358592ffb21SWarner Losh struct ttm_bo_global *glob = bo->glob;
359592ffb21SWarner Losh
360592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
361592ffb21SWarner Losh ttm_bo_unreserve_locked(bo);
362592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
363592ffb21SWarner Losh }
364592ffb21SWarner Losh
365592ffb21SWarner Losh /*
366592ffb21SWarner Losh * Call bo->mutex locked.
367592ffb21SWarner Losh */
ttm_bo_add_ttm(struct ttm_buffer_object * bo,bool zero_alloc)368592ffb21SWarner Losh static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
369592ffb21SWarner Losh {
370592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
371592ffb21SWarner Losh struct ttm_bo_global *glob = bo->glob;
372592ffb21SWarner Losh int ret = 0;
373592ffb21SWarner Losh uint32_t page_flags = 0;
374592ffb21SWarner Losh
375592ffb21SWarner Losh TTM_ASSERT_LOCKED(&bo->mutex);
376592ffb21SWarner Losh bo->ttm = NULL;
377592ffb21SWarner Losh
378592ffb21SWarner Losh if (bdev->need_dma32)
379592ffb21SWarner Losh page_flags |= TTM_PAGE_FLAG_DMA32;
380592ffb21SWarner Losh
381592ffb21SWarner Losh switch (bo->type) {
382592ffb21SWarner Losh case ttm_bo_type_device:
383592ffb21SWarner Losh if (zero_alloc)
384592ffb21SWarner Losh page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
385592ffb21SWarner Losh case ttm_bo_type_kernel:
386592ffb21SWarner Losh bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
387592ffb21SWarner Losh page_flags, glob->dummy_read_page);
388592ffb21SWarner Losh if (unlikely(bo->ttm == NULL))
389592ffb21SWarner Losh ret = -ENOMEM;
390592ffb21SWarner Losh break;
391592ffb21SWarner Losh case ttm_bo_type_sg:
392592ffb21SWarner Losh bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
393592ffb21SWarner Losh page_flags | TTM_PAGE_FLAG_SG,
394592ffb21SWarner Losh glob->dummy_read_page);
395592ffb21SWarner Losh if (unlikely(bo->ttm == NULL)) {
396592ffb21SWarner Losh ret = -ENOMEM;
397592ffb21SWarner Losh break;
398592ffb21SWarner Losh }
399592ffb21SWarner Losh bo->ttm->sg = bo->sg;
400592ffb21SWarner Losh break;
401592ffb21SWarner Losh default:
402592ffb21SWarner Losh printf("[TTM] Illegal buffer object type\n");
403592ffb21SWarner Losh ret = -EINVAL;
404592ffb21SWarner Losh break;
405592ffb21SWarner Losh }
406592ffb21SWarner Losh
407592ffb21SWarner Losh return ret;
408592ffb21SWarner Losh }
409592ffb21SWarner Losh
ttm_bo_handle_move_mem(struct ttm_buffer_object * bo,struct ttm_mem_reg * mem,bool evict,bool interruptible,bool no_wait_gpu)410592ffb21SWarner Losh static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
411592ffb21SWarner Losh struct ttm_mem_reg *mem,
412592ffb21SWarner Losh bool evict, bool interruptible,
413592ffb21SWarner Losh bool no_wait_gpu)
414592ffb21SWarner Losh {
415592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
416592ffb21SWarner Losh bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
417592ffb21SWarner Losh bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
418592ffb21SWarner Losh struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
419592ffb21SWarner Losh struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
420592ffb21SWarner Losh int ret = 0;
421592ffb21SWarner Losh
422592ffb21SWarner Losh if (old_is_pci || new_is_pci ||
423592ffb21SWarner Losh ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
424592ffb21SWarner Losh ret = ttm_mem_io_lock(old_man, true);
425592ffb21SWarner Losh if (unlikely(ret != 0))
426592ffb21SWarner Losh goto out_err;
427592ffb21SWarner Losh ttm_bo_unmap_virtual_locked(bo);
428592ffb21SWarner Losh ttm_mem_io_unlock(old_man);
429592ffb21SWarner Losh }
430592ffb21SWarner Losh
431592ffb21SWarner Losh /*
432592ffb21SWarner Losh * Create and bind a ttm if required.
433592ffb21SWarner Losh */
434592ffb21SWarner Losh
435592ffb21SWarner Losh if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
436592ffb21SWarner Losh if (bo->ttm == NULL) {
437592ffb21SWarner Losh bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
438592ffb21SWarner Losh ret = ttm_bo_add_ttm(bo, zero);
439592ffb21SWarner Losh if (ret)
440592ffb21SWarner Losh goto out_err;
441592ffb21SWarner Losh }
442592ffb21SWarner Losh
443592ffb21SWarner Losh ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
444592ffb21SWarner Losh if (ret)
445592ffb21SWarner Losh goto out_err;
446592ffb21SWarner Losh
447592ffb21SWarner Losh if (mem->mem_type != TTM_PL_SYSTEM) {
448592ffb21SWarner Losh ret = ttm_tt_bind(bo->ttm, mem);
449592ffb21SWarner Losh if (ret)
450592ffb21SWarner Losh goto out_err;
451592ffb21SWarner Losh }
452592ffb21SWarner Losh
453592ffb21SWarner Losh if (bo->mem.mem_type == TTM_PL_SYSTEM) {
454592ffb21SWarner Losh if (bdev->driver->move_notify)
455592ffb21SWarner Losh bdev->driver->move_notify(bo, mem);
456592ffb21SWarner Losh bo->mem = *mem;
457592ffb21SWarner Losh mem->mm_node = NULL;
458592ffb21SWarner Losh goto moved;
459592ffb21SWarner Losh }
460592ffb21SWarner Losh }
461592ffb21SWarner Losh
462592ffb21SWarner Losh if (bdev->driver->move_notify)
463592ffb21SWarner Losh bdev->driver->move_notify(bo, mem);
464592ffb21SWarner Losh
465592ffb21SWarner Losh if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
466592ffb21SWarner Losh !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
467592ffb21SWarner Losh ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
468592ffb21SWarner Losh else if (bdev->driver->move)
469592ffb21SWarner Losh ret = bdev->driver->move(bo, evict, interruptible,
470592ffb21SWarner Losh no_wait_gpu, mem);
471592ffb21SWarner Losh else
472592ffb21SWarner Losh ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
473592ffb21SWarner Losh
474592ffb21SWarner Losh if (ret) {
475592ffb21SWarner Losh if (bdev->driver->move_notify) {
476592ffb21SWarner Losh struct ttm_mem_reg tmp_mem = *mem;
477592ffb21SWarner Losh *mem = bo->mem;
478592ffb21SWarner Losh bo->mem = tmp_mem;
479592ffb21SWarner Losh bdev->driver->move_notify(bo, mem);
480592ffb21SWarner Losh bo->mem = *mem;
481592ffb21SWarner Losh *mem = tmp_mem;
482592ffb21SWarner Losh }
483592ffb21SWarner Losh
484592ffb21SWarner Losh goto out_err;
485592ffb21SWarner Losh }
486592ffb21SWarner Losh
487592ffb21SWarner Losh moved:
488592ffb21SWarner Losh if (bo->evicted) {
489592ffb21SWarner Losh ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
490592ffb21SWarner Losh if (ret)
491592ffb21SWarner Losh printf("[TTM] Can not flush read caches\n");
492592ffb21SWarner Losh bo->evicted = false;
493592ffb21SWarner Losh }
494592ffb21SWarner Losh
495592ffb21SWarner Losh if (bo->mem.mm_node) {
496592ffb21SWarner Losh bo->offset = (bo->mem.start << PAGE_SHIFT) +
497592ffb21SWarner Losh bdev->man[bo->mem.mem_type].gpu_offset;
498592ffb21SWarner Losh bo->cur_placement = bo->mem.placement;
499592ffb21SWarner Losh } else
500592ffb21SWarner Losh bo->offset = 0;
501592ffb21SWarner Losh
502592ffb21SWarner Losh return 0;
503592ffb21SWarner Losh
504592ffb21SWarner Losh out_err:
505592ffb21SWarner Losh new_man = &bdev->man[bo->mem.mem_type];
506592ffb21SWarner Losh if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
507592ffb21SWarner Losh ttm_tt_unbind(bo->ttm);
508592ffb21SWarner Losh ttm_tt_destroy(bo->ttm);
509592ffb21SWarner Losh bo->ttm = NULL;
510592ffb21SWarner Losh }
511592ffb21SWarner Losh
512592ffb21SWarner Losh return ret;
513592ffb21SWarner Losh }
514592ffb21SWarner Losh
515592ffb21SWarner Losh /**
516592ffb21SWarner Losh * Call bo::reserved.
517592ffb21SWarner Losh * Will release GPU memory type usage on destruction.
518592ffb21SWarner Losh * This is the place to put in driver specific hooks to release
519592ffb21SWarner Losh * driver private resources.
520592ffb21SWarner Losh * Will release the bo::reserved lock.
521592ffb21SWarner Losh */
522592ffb21SWarner Losh
ttm_bo_cleanup_memtype_use(struct ttm_buffer_object * bo)523592ffb21SWarner Losh static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
524592ffb21SWarner Losh {
525592ffb21SWarner Losh if (bo->bdev->driver->move_notify)
526592ffb21SWarner Losh bo->bdev->driver->move_notify(bo, NULL);
527592ffb21SWarner Losh
528592ffb21SWarner Losh if (bo->ttm) {
529592ffb21SWarner Losh ttm_tt_unbind(bo->ttm);
530592ffb21SWarner Losh ttm_tt_destroy(bo->ttm);
531592ffb21SWarner Losh bo->ttm = NULL;
532592ffb21SWarner Losh }
533592ffb21SWarner Losh ttm_bo_mem_put(bo, &bo->mem);
534592ffb21SWarner Losh
535592ffb21SWarner Losh atomic_set(&bo->reserved, 0);
536592ffb21SWarner Losh wakeup(&bo);
537592ffb21SWarner Losh
538592ffb21SWarner Losh /*
539592ffb21SWarner Losh * Since the final reference to this bo may not be dropped by
540592ffb21SWarner Losh * the current task we have to put a memory barrier here to make
541592ffb21SWarner Losh * sure the changes done in this function are always visible.
542592ffb21SWarner Losh *
543592ffb21SWarner Losh * This function only needs protection against the final kref_put.
544592ffb21SWarner Losh */
545592ffb21SWarner Losh mb();
546592ffb21SWarner Losh }
547592ffb21SWarner Losh
ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object * bo)548592ffb21SWarner Losh static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
549592ffb21SWarner Losh {
550592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
551592ffb21SWarner Losh struct ttm_bo_global *glob = bo->glob;
552592ffb21SWarner Losh struct ttm_bo_driver *driver = bdev->driver;
553592ffb21SWarner Losh void *sync_obj = NULL;
554592ffb21SWarner Losh int put_count;
555592ffb21SWarner Losh int ret;
556592ffb21SWarner Losh
557592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
558592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
559592ffb21SWarner Losh
560592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
561592ffb21SWarner Losh (void) ttm_bo_wait(bo, false, false, true);
562592ffb21SWarner Losh if (!ret && !bo->sync_obj) {
563592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
564592ffb21SWarner Losh put_count = ttm_bo_del_from_lru(bo);
565592ffb21SWarner Losh
566592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
567592ffb21SWarner Losh ttm_bo_cleanup_memtype_use(bo);
568592ffb21SWarner Losh
569592ffb21SWarner Losh ttm_bo_list_ref_sub(bo, put_count, true);
570592ffb21SWarner Losh
571592ffb21SWarner Losh return;
572592ffb21SWarner Losh }
573592ffb21SWarner Losh if (bo->sync_obj)
574592ffb21SWarner Losh sync_obj = driver->sync_obj_ref(bo->sync_obj);
575592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
576592ffb21SWarner Losh
577592ffb21SWarner Losh if (!ret) {
578592ffb21SWarner Losh atomic_set(&bo->reserved, 0);
579592ffb21SWarner Losh wakeup(bo);
580592ffb21SWarner Losh }
581592ffb21SWarner Losh
582592ffb21SWarner Losh refcount_acquire(&bo->list_kref);
583592ffb21SWarner Losh list_add_tail(&bo->ddestroy, &bdev->ddestroy);
584592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
585592ffb21SWarner Losh
586592ffb21SWarner Losh if (sync_obj) {
587592ffb21SWarner Losh driver->sync_obj_flush(sync_obj);
588592ffb21SWarner Losh driver->sync_obj_unref(&sync_obj);
589592ffb21SWarner Losh }
590592ffb21SWarner Losh taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
591592ffb21SWarner Losh ((hz / 100) < 1) ? 1 : hz / 100);
592592ffb21SWarner Losh }
593592ffb21SWarner Losh
594592ffb21SWarner Losh /**
595592ffb21SWarner Losh * function ttm_bo_cleanup_refs_and_unlock
596592ffb21SWarner Losh * If bo idle, remove from delayed- and lru lists, and unref.
597592ffb21SWarner Losh * If not idle, do nothing.
598592ffb21SWarner Losh *
599592ffb21SWarner Losh * Must be called with lru_lock and reservation held, this function
600592ffb21SWarner Losh * will drop both before returning.
601592ffb21SWarner Losh *
602592ffb21SWarner Losh * @interruptible Any sleeps should occur interruptibly.
603592ffb21SWarner Losh * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
604592ffb21SWarner Losh */
605592ffb21SWarner Losh
ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object * bo,bool interruptible,bool no_wait_gpu)606592ffb21SWarner Losh static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
607592ffb21SWarner Losh bool interruptible,
608592ffb21SWarner Losh bool no_wait_gpu)
609592ffb21SWarner Losh {
610592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
611592ffb21SWarner Losh struct ttm_bo_driver *driver = bdev->driver;
612592ffb21SWarner Losh struct ttm_bo_global *glob = bo->glob;
613592ffb21SWarner Losh int put_count;
614592ffb21SWarner Losh int ret;
615592ffb21SWarner Losh
616592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
617592ffb21SWarner Losh ret = ttm_bo_wait(bo, false, false, true);
618592ffb21SWarner Losh
619592ffb21SWarner Losh if (ret && !no_wait_gpu) {
620592ffb21SWarner Losh void *sync_obj;
621592ffb21SWarner Losh
622592ffb21SWarner Losh /*
623592ffb21SWarner Losh * Take a reference to the fence and unreserve,
624592ffb21SWarner Losh * at this point the buffer should be dead, so
625592ffb21SWarner Losh * no new sync objects can be attached.
626592ffb21SWarner Losh */
627592ffb21SWarner Losh sync_obj = driver->sync_obj_ref(bo->sync_obj);
628592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
629592ffb21SWarner Losh
630592ffb21SWarner Losh atomic_set(&bo->reserved, 0);
631592ffb21SWarner Losh wakeup(bo);
632592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
633592ffb21SWarner Losh
634592ffb21SWarner Losh ret = driver->sync_obj_wait(sync_obj, false, interruptible);
635592ffb21SWarner Losh driver->sync_obj_unref(&sync_obj);
636592ffb21SWarner Losh if (ret)
637592ffb21SWarner Losh return ret;
638592ffb21SWarner Losh
639592ffb21SWarner Losh /*
640592ffb21SWarner Losh * remove sync_obj with ttm_bo_wait, the wait should be
641592ffb21SWarner Losh * finished, and no new wait object should have been added.
642592ffb21SWarner Losh */
643592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
644592ffb21SWarner Losh ret = ttm_bo_wait(bo, false, false, true);
645592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
646592ffb21SWarner Losh if (ret)
647592ffb21SWarner Losh return ret;
648592ffb21SWarner Losh
649592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
650592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
651592ffb21SWarner Losh
652592ffb21SWarner Losh /*
653592ffb21SWarner Losh * We raced, and lost, someone else holds the reservation now,
654592ffb21SWarner Losh * and is probably busy in ttm_bo_cleanup_memtype_use.
655592ffb21SWarner Losh *
656592ffb21SWarner Losh * Even if it's not the case, because we finished waiting any
657592ffb21SWarner Losh * delayed destruction would succeed, so just return success
658592ffb21SWarner Losh * here.
659592ffb21SWarner Losh */
660592ffb21SWarner Losh if (ret) {
661592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
662592ffb21SWarner Losh return 0;
663592ffb21SWarner Losh }
664592ffb21SWarner Losh } else
665592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
666592ffb21SWarner Losh
667592ffb21SWarner Losh if (ret || unlikely(list_empty(&bo->ddestroy))) {
668592ffb21SWarner Losh atomic_set(&bo->reserved, 0);
669592ffb21SWarner Losh wakeup(bo);
670592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
671592ffb21SWarner Losh return ret;
672592ffb21SWarner Losh }
673592ffb21SWarner Losh
674592ffb21SWarner Losh put_count = ttm_bo_del_from_lru(bo);
675592ffb21SWarner Losh list_del_init(&bo->ddestroy);
676592ffb21SWarner Losh ++put_count;
677592ffb21SWarner Losh
678592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
679592ffb21SWarner Losh ttm_bo_cleanup_memtype_use(bo);
680592ffb21SWarner Losh
681592ffb21SWarner Losh ttm_bo_list_ref_sub(bo, put_count, true);
682592ffb21SWarner Losh
683592ffb21SWarner Losh return 0;
684592ffb21SWarner Losh }
685592ffb21SWarner Losh
686592ffb21SWarner Losh /**
687592ffb21SWarner Losh * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
688592ffb21SWarner Losh * encountered buffers.
689592ffb21SWarner Losh */
690592ffb21SWarner Losh
ttm_bo_delayed_delete(struct ttm_bo_device * bdev,bool remove_all)691592ffb21SWarner Losh static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
692592ffb21SWarner Losh {
693592ffb21SWarner Losh struct ttm_bo_global *glob = bdev->glob;
694592ffb21SWarner Losh struct ttm_buffer_object *entry = NULL;
695592ffb21SWarner Losh int ret = 0;
696592ffb21SWarner Losh
697592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
698592ffb21SWarner Losh if (list_empty(&bdev->ddestroy))
699592ffb21SWarner Losh goto out_unlock;
700592ffb21SWarner Losh
701592ffb21SWarner Losh entry = list_first_entry(&bdev->ddestroy,
702592ffb21SWarner Losh struct ttm_buffer_object, ddestroy);
703592ffb21SWarner Losh refcount_acquire(&entry->list_kref);
704592ffb21SWarner Losh
705592ffb21SWarner Losh for (;;) {
706592ffb21SWarner Losh struct ttm_buffer_object *nentry = NULL;
707592ffb21SWarner Losh
708592ffb21SWarner Losh if (entry->ddestroy.next != &bdev->ddestroy) {
709592ffb21SWarner Losh nentry = list_first_entry(&entry->ddestroy,
710592ffb21SWarner Losh struct ttm_buffer_object, ddestroy);
711592ffb21SWarner Losh refcount_acquire(&nentry->list_kref);
712592ffb21SWarner Losh }
713592ffb21SWarner Losh
714592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
715592ffb21SWarner Losh if (remove_all && ret) {
716592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(entry, false, false,
717592ffb21SWarner Losh false, 0);
718592ffb21SWarner Losh }
719592ffb21SWarner Losh
720592ffb21SWarner Losh if (!ret)
721592ffb21SWarner Losh ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
722592ffb21SWarner Losh !remove_all);
723592ffb21SWarner Losh else
724592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
725592ffb21SWarner Losh
726592ffb21SWarner Losh if (refcount_release(&entry->list_kref))
727592ffb21SWarner Losh ttm_bo_release_list(entry);
728592ffb21SWarner Losh entry = nentry;
729592ffb21SWarner Losh
730592ffb21SWarner Losh if (ret || !entry)
731592ffb21SWarner Losh goto out;
732592ffb21SWarner Losh
733592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
734592ffb21SWarner Losh if (list_empty(&entry->ddestroy))
735592ffb21SWarner Losh break;
736592ffb21SWarner Losh }
737592ffb21SWarner Losh
738592ffb21SWarner Losh out_unlock:
739592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
740592ffb21SWarner Losh out:
741592ffb21SWarner Losh if (entry && refcount_release(&entry->list_kref))
742592ffb21SWarner Losh ttm_bo_release_list(entry);
743592ffb21SWarner Losh return ret;
744592ffb21SWarner Losh }
745592ffb21SWarner Losh
ttm_bo_delayed_workqueue(void * arg,int pending __unused)746592ffb21SWarner Losh static void ttm_bo_delayed_workqueue(void *arg, int pending __unused)
747592ffb21SWarner Losh {
748592ffb21SWarner Losh struct ttm_bo_device *bdev = arg;
749592ffb21SWarner Losh
750592ffb21SWarner Losh if (ttm_bo_delayed_delete(bdev, false)) {
751592ffb21SWarner Losh taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
752592ffb21SWarner Losh ((hz / 100) < 1) ? 1 : hz / 100);
753592ffb21SWarner Losh }
754592ffb21SWarner Losh }
755592ffb21SWarner Losh
ttm_bo_release(struct ttm_buffer_object * bo)756592ffb21SWarner Losh static void ttm_bo_release(struct ttm_buffer_object *bo)
757592ffb21SWarner Losh {
758592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
759592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
760592ffb21SWarner Losh
761592ffb21SWarner Losh rw_wlock(&bdev->vm_lock);
762592ffb21SWarner Losh if (likely(bo->vm_node != NULL)) {
763592ffb21SWarner Losh RB_REMOVE(ttm_bo_device_buffer_objects,
764592ffb21SWarner Losh &bdev->addr_space_rb, bo);
765592ffb21SWarner Losh drm_mm_put_block(bo->vm_node);
766592ffb21SWarner Losh bo->vm_node = NULL;
767592ffb21SWarner Losh }
768592ffb21SWarner Losh rw_wunlock(&bdev->vm_lock);
769592ffb21SWarner Losh ttm_mem_io_lock(man, false);
770592ffb21SWarner Losh ttm_mem_io_free_vm(bo);
771592ffb21SWarner Losh ttm_mem_io_unlock(man);
772592ffb21SWarner Losh ttm_bo_cleanup_refs_or_queue(bo);
773592ffb21SWarner Losh if (refcount_release(&bo->list_kref))
774592ffb21SWarner Losh ttm_bo_release_list(bo);
775592ffb21SWarner Losh }
776592ffb21SWarner Losh
ttm_bo_unref(struct ttm_buffer_object ** p_bo)777592ffb21SWarner Losh void ttm_bo_unref(struct ttm_buffer_object **p_bo)
778592ffb21SWarner Losh {
779592ffb21SWarner Losh struct ttm_buffer_object *bo = *p_bo;
780592ffb21SWarner Losh
781592ffb21SWarner Losh *p_bo = NULL;
782592ffb21SWarner Losh if (refcount_release(&bo->kref))
783592ffb21SWarner Losh ttm_bo_release(bo);
784592ffb21SWarner Losh }
785592ffb21SWarner Losh
ttm_bo_lock_delayed_workqueue(struct ttm_bo_device * bdev)786592ffb21SWarner Losh int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
787592ffb21SWarner Losh {
788592ffb21SWarner Losh int pending;
789592ffb21SWarner Losh
790592ffb21SWarner Losh if (taskqueue_cancel_timeout(taskqueue_thread, &bdev->wq, &pending))
791592ffb21SWarner Losh taskqueue_drain_timeout(taskqueue_thread, &bdev->wq);
792592ffb21SWarner Losh return (pending);
793592ffb21SWarner Losh }
794592ffb21SWarner Losh
ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device * bdev,int resched)795592ffb21SWarner Losh void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
796592ffb21SWarner Losh {
797592ffb21SWarner Losh if (resched) {
798592ffb21SWarner Losh taskqueue_enqueue_timeout(taskqueue_thread, &bdev->wq,
799592ffb21SWarner Losh ((hz / 100) < 1) ? 1 : hz / 100);
800592ffb21SWarner Losh }
801592ffb21SWarner Losh }
802592ffb21SWarner Losh
ttm_bo_evict(struct ttm_buffer_object * bo,bool interruptible,bool no_wait_gpu)803592ffb21SWarner Losh static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
804592ffb21SWarner Losh bool no_wait_gpu)
805592ffb21SWarner Losh {
806592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
807592ffb21SWarner Losh struct ttm_mem_reg evict_mem;
808592ffb21SWarner Losh struct ttm_placement placement;
809592ffb21SWarner Losh int ret = 0;
810592ffb21SWarner Losh
811592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
812592ffb21SWarner Losh ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
813592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
814592ffb21SWarner Losh
815592ffb21SWarner Losh if (unlikely(ret != 0)) {
816592ffb21SWarner Losh if (ret != -ERESTARTSYS) {
817592ffb21SWarner Losh printf("[TTM] Failed to expire sync object before buffer eviction\n");
818592ffb21SWarner Losh }
819592ffb21SWarner Losh goto out;
820592ffb21SWarner Losh }
821592ffb21SWarner Losh
822592ffb21SWarner Losh MPASS(ttm_bo_is_reserved(bo));
823592ffb21SWarner Losh
824592ffb21SWarner Losh evict_mem = bo->mem;
825592ffb21SWarner Losh evict_mem.mm_node = NULL;
826592ffb21SWarner Losh evict_mem.bus.io_reserved_vm = false;
827592ffb21SWarner Losh evict_mem.bus.io_reserved_count = 0;
828592ffb21SWarner Losh
829592ffb21SWarner Losh placement.fpfn = 0;
830592ffb21SWarner Losh placement.lpfn = 0;
831592ffb21SWarner Losh placement.num_placement = 0;
832592ffb21SWarner Losh placement.num_busy_placement = 0;
833592ffb21SWarner Losh bdev->driver->evict_flags(bo, &placement);
834592ffb21SWarner Losh ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
835592ffb21SWarner Losh no_wait_gpu);
836592ffb21SWarner Losh if (ret) {
837592ffb21SWarner Losh if (ret != -ERESTARTSYS) {
838592ffb21SWarner Losh printf("[TTM] Failed to find memory space for buffer 0x%p eviction\n",
839592ffb21SWarner Losh bo);
840592ffb21SWarner Losh ttm_bo_mem_space_debug(bo, &placement);
841592ffb21SWarner Losh }
842592ffb21SWarner Losh goto out;
843592ffb21SWarner Losh }
844592ffb21SWarner Losh
845592ffb21SWarner Losh ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
846592ffb21SWarner Losh no_wait_gpu);
847592ffb21SWarner Losh if (ret) {
848592ffb21SWarner Losh if (ret != -ERESTARTSYS)
849592ffb21SWarner Losh printf("[TTM] Buffer eviction failed\n");
850592ffb21SWarner Losh ttm_bo_mem_put(bo, &evict_mem);
851592ffb21SWarner Losh goto out;
852592ffb21SWarner Losh }
853592ffb21SWarner Losh bo->evicted = true;
854592ffb21SWarner Losh out:
855592ffb21SWarner Losh return ret;
856592ffb21SWarner Losh }
857592ffb21SWarner Losh
ttm_mem_evict_first(struct ttm_bo_device * bdev,uint32_t mem_type,bool interruptible,bool no_wait_gpu)858592ffb21SWarner Losh static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
859592ffb21SWarner Losh uint32_t mem_type,
860592ffb21SWarner Losh bool interruptible,
861592ffb21SWarner Losh bool no_wait_gpu)
862592ffb21SWarner Losh {
863592ffb21SWarner Losh struct ttm_bo_global *glob = bdev->glob;
864592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bdev->man[mem_type];
865592ffb21SWarner Losh struct ttm_buffer_object *bo;
866592ffb21SWarner Losh int ret = -EBUSY, put_count;
867592ffb21SWarner Losh
868592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
869592ffb21SWarner Losh list_for_each_entry(bo, &man->lru, lru) {
870592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
871592ffb21SWarner Losh if (!ret)
872592ffb21SWarner Losh break;
873592ffb21SWarner Losh }
874592ffb21SWarner Losh
875592ffb21SWarner Losh if (ret) {
876592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
877592ffb21SWarner Losh return ret;
878592ffb21SWarner Losh }
879592ffb21SWarner Losh
880592ffb21SWarner Losh refcount_acquire(&bo->list_kref);
881592ffb21SWarner Losh
882592ffb21SWarner Losh if (!list_empty(&bo->ddestroy)) {
883592ffb21SWarner Losh ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
884592ffb21SWarner Losh no_wait_gpu);
885592ffb21SWarner Losh if (refcount_release(&bo->list_kref))
886592ffb21SWarner Losh ttm_bo_release_list(bo);
887592ffb21SWarner Losh return ret;
888592ffb21SWarner Losh }
889592ffb21SWarner Losh
890592ffb21SWarner Losh put_count = ttm_bo_del_from_lru(bo);
891592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
892592ffb21SWarner Losh
893592ffb21SWarner Losh MPASS(ret == 0);
894592ffb21SWarner Losh
895592ffb21SWarner Losh ttm_bo_list_ref_sub(bo, put_count, true);
896592ffb21SWarner Losh
897592ffb21SWarner Losh ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
898592ffb21SWarner Losh ttm_bo_unreserve(bo);
899592ffb21SWarner Losh
900592ffb21SWarner Losh if (refcount_release(&bo->list_kref))
901592ffb21SWarner Losh ttm_bo_release_list(bo);
902592ffb21SWarner Losh return ret;
903592ffb21SWarner Losh }
904592ffb21SWarner Losh
ttm_bo_mem_put(struct ttm_buffer_object * bo,struct ttm_mem_reg * mem)905592ffb21SWarner Losh void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
906592ffb21SWarner Losh {
907592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
908592ffb21SWarner Losh
909592ffb21SWarner Losh if (mem->mm_node)
910592ffb21SWarner Losh (*man->func->put_node)(man, mem);
911592ffb21SWarner Losh }
912592ffb21SWarner Losh
913592ffb21SWarner Losh /**
914592ffb21SWarner Losh * Repeatedly evict memory from the LRU for @mem_type until we create enough
915592ffb21SWarner Losh * space, or we've evicted everything and there isn't enough space.
916592ffb21SWarner Losh */
ttm_bo_mem_force_space(struct ttm_buffer_object * bo,uint32_t mem_type,struct ttm_placement * placement,struct ttm_mem_reg * mem,bool interruptible,bool no_wait_gpu)917592ffb21SWarner Losh static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
918592ffb21SWarner Losh uint32_t mem_type,
919592ffb21SWarner Losh struct ttm_placement *placement,
920592ffb21SWarner Losh struct ttm_mem_reg *mem,
921592ffb21SWarner Losh bool interruptible,
922592ffb21SWarner Losh bool no_wait_gpu)
923592ffb21SWarner Losh {
924592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
925592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bdev->man[mem_type];
926592ffb21SWarner Losh int ret;
927592ffb21SWarner Losh
928592ffb21SWarner Losh do {
929592ffb21SWarner Losh ret = (*man->func->get_node)(man, bo, placement, mem);
930592ffb21SWarner Losh if (unlikely(ret != 0))
931592ffb21SWarner Losh return ret;
932592ffb21SWarner Losh if (mem->mm_node)
933592ffb21SWarner Losh break;
934592ffb21SWarner Losh ret = ttm_mem_evict_first(bdev, mem_type,
935592ffb21SWarner Losh interruptible, no_wait_gpu);
936592ffb21SWarner Losh if (unlikely(ret != 0))
937592ffb21SWarner Losh return ret;
938592ffb21SWarner Losh } while (1);
939592ffb21SWarner Losh if (mem->mm_node == NULL)
940592ffb21SWarner Losh return -ENOMEM;
941592ffb21SWarner Losh mem->mem_type = mem_type;
942592ffb21SWarner Losh return 0;
943592ffb21SWarner Losh }
944592ffb21SWarner Losh
ttm_bo_select_caching(struct ttm_mem_type_manager * man,uint32_t cur_placement,uint32_t proposed_placement)945592ffb21SWarner Losh static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
946592ffb21SWarner Losh uint32_t cur_placement,
947592ffb21SWarner Losh uint32_t proposed_placement)
948592ffb21SWarner Losh {
949592ffb21SWarner Losh uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
950592ffb21SWarner Losh uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
951592ffb21SWarner Losh
952592ffb21SWarner Losh /**
953592ffb21SWarner Losh * Keep current caching if possible.
954592ffb21SWarner Losh */
955592ffb21SWarner Losh
956592ffb21SWarner Losh if ((cur_placement & caching) != 0)
957592ffb21SWarner Losh result |= (cur_placement & caching);
958592ffb21SWarner Losh else if ((man->default_caching & caching) != 0)
959592ffb21SWarner Losh result |= man->default_caching;
960592ffb21SWarner Losh else if ((TTM_PL_FLAG_CACHED & caching) != 0)
961592ffb21SWarner Losh result |= TTM_PL_FLAG_CACHED;
962592ffb21SWarner Losh else if ((TTM_PL_FLAG_WC & caching) != 0)
963592ffb21SWarner Losh result |= TTM_PL_FLAG_WC;
964592ffb21SWarner Losh else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
965592ffb21SWarner Losh result |= TTM_PL_FLAG_UNCACHED;
966592ffb21SWarner Losh
967592ffb21SWarner Losh return result;
968592ffb21SWarner Losh }
969592ffb21SWarner Losh
ttm_bo_mt_compatible(struct ttm_mem_type_manager * man,uint32_t mem_type,uint32_t proposed_placement,uint32_t * masked_placement)970592ffb21SWarner Losh static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
971592ffb21SWarner Losh uint32_t mem_type,
972592ffb21SWarner Losh uint32_t proposed_placement,
973592ffb21SWarner Losh uint32_t *masked_placement)
974592ffb21SWarner Losh {
975592ffb21SWarner Losh uint32_t cur_flags = ttm_bo_type_flags(mem_type);
976592ffb21SWarner Losh
977592ffb21SWarner Losh if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
978592ffb21SWarner Losh return false;
979592ffb21SWarner Losh
980592ffb21SWarner Losh if ((proposed_placement & man->available_caching) == 0)
981592ffb21SWarner Losh return false;
982592ffb21SWarner Losh
983592ffb21SWarner Losh cur_flags |= (proposed_placement & man->available_caching);
984592ffb21SWarner Losh
985592ffb21SWarner Losh *masked_placement = cur_flags;
986592ffb21SWarner Losh return true;
987592ffb21SWarner Losh }
988592ffb21SWarner Losh
989592ffb21SWarner Losh /**
990592ffb21SWarner Losh * Creates space for memory region @mem according to its type.
991592ffb21SWarner Losh *
992592ffb21SWarner Losh * This function first searches for free space in compatible memory types in
993592ffb21SWarner Losh * the priority order defined by the driver. If free space isn't found, then
994592ffb21SWarner Losh * ttm_bo_mem_force_space is attempted in priority order to evict and find
995592ffb21SWarner Losh * space.
996592ffb21SWarner Losh */
ttm_bo_mem_space(struct ttm_buffer_object * bo,struct ttm_placement * placement,struct ttm_mem_reg * mem,bool interruptible,bool no_wait_gpu)997592ffb21SWarner Losh int ttm_bo_mem_space(struct ttm_buffer_object *bo,
998592ffb21SWarner Losh struct ttm_placement *placement,
999592ffb21SWarner Losh struct ttm_mem_reg *mem,
1000592ffb21SWarner Losh bool interruptible,
1001592ffb21SWarner Losh bool no_wait_gpu)
1002592ffb21SWarner Losh {
1003592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
1004592ffb21SWarner Losh struct ttm_mem_type_manager *man;
1005592ffb21SWarner Losh uint32_t mem_type = TTM_PL_SYSTEM;
1006592ffb21SWarner Losh uint32_t cur_flags = 0;
1007592ffb21SWarner Losh bool type_found = false;
1008592ffb21SWarner Losh bool type_ok = false;
1009592ffb21SWarner Losh bool has_erestartsys = false;
1010592ffb21SWarner Losh int i, ret;
1011592ffb21SWarner Losh
1012592ffb21SWarner Losh mem->mm_node = NULL;
1013592ffb21SWarner Losh for (i = 0; i < placement->num_placement; ++i) {
1014592ffb21SWarner Losh ret = ttm_mem_type_from_flags(placement->placement[i],
1015592ffb21SWarner Losh &mem_type);
1016592ffb21SWarner Losh if (ret)
1017592ffb21SWarner Losh return ret;
1018592ffb21SWarner Losh man = &bdev->man[mem_type];
1019592ffb21SWarner Losh
1020592ffb21SWarner Losh type_ok = ttm_bo_mt_compatible(man,
1021592ffb21SWarner Losh mem_type,
1022592ffb21SWarner Losh placement->placement[i],
1023592ffb21SWarner Losh &cur_flags);
1024592ffb21SWarner Losh
1025592ffb21SWarner Losh if (!type_ok)
1026592ffb21SWarner Losh continue;
1027592ffb21SWarner Losh
1028592ffb21SWarner Losh cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1029592ffb21SWarner Losh cur_flags);
1030592ffb21SWarner Losh /*
1031592ffb21SWarner Losh * Use the access and other non-mapping-related flag bits from
1032592ffb21SWarner Losh * the memory placement flags to the current flags
1033592ffb21SWarner Losh */
1034592ffb21SWarner Losh ttm_flag_masked(&cur_flags, placement->placement[i],
1035592ffb21SWarner Losh ~TTM_PL_MASK_MEMTYPE);
1036592ffb21SWarner Losh
1037592ffb21SWarner Losh if (mem_type == TTM_PL_SYSTEM)
1038592ffb21SWarner Losh break;
1039592ffb21SWarner Losh
1040592ffb21SWarner Losh if (man->has_type && man->use_type) {
1041592ffb21SWarner Losh type_found = true;
1042592ffb21SWarner Losh ret = (*man->func->get_node)(man, bo, placement, mem);
1043592ffb21SWarner Losh if (unlikely(ret))
1044592ffb21SWarner Losh return ret;
1045592ffb21SWarner Losh }
1046592ffb21SWarner Losh if (mem->mm_node)
1047592ffb21SWarner Losh break;
1048592ffb21SWarner Losh }
1049592ffb21SWarner Losh
1050592ffb21SWarner Losh if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
1051592ffb21SWarner Losh mem->mem_type = mem_type;
1052592ffb21SWarner Losh mem->placement = cur_flags;
1053592ffb21SWarner Losh return 0;
1054592ffb21SWarner Losh }
1055592ffb21SWarner Losh
1056592ffb21SWarner Losh if (!type_found)
1057592ffb21SWarner Losh return -EINVAL;
1058592ffb21SWarner Losh
1059592ffb21SWarner Losh for (i = 0; i < placement->num_busy_placement; ++i) {
1060592ffb21SWarner Losh ret = ttm_mem_type_from_flags(placement->busy_placement[i],
1061592ffb21SWarner Losh &mem_type);
1062592ffb21SWarner Losh if (ret)
1063592ffb21SWarner Losh return ret;
1064592ffb21SWarner Losh man = &bdev->man[mem_type];
1065592ffb21SWarner Losh if (!man->has_type)
1066592ffb21SWarner Losh continue;
1067592ffb21SWarner Losh if (!ttm_bo_mt_compatible(man,
1068592ffb21SWarner Losh mem_type,
1069592ffb21SWarner Losh placement->busy_placement[i],
1070592ffb21SWarner Losh &cur_flags))
1071592ffb21SWarner Losh continue;
1072592ffb21SWarner Losh
1073592ffb21SWarner Losh cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1074592ffb21SWarner Losh cur_flags);
1075592ffb21SWarner Losh /*
1076592ffb21SWarner Losh * Use the access and other non-mapping-related flag bits from
1077592ffb21SWarner Losh * the memory placement flags to the current flags
1078592ffb21SWarner Losh */
1079592ffb21SWarner Losh ttm_flag_masked(&cur_flags, placement->busy_placement[i],
1080592ffb21SWarner Losh ~TTM_PL_MASK_MEMTYPE);
1081592ffb21SWarner Losh
1082592ffb21SWarner Losh
1083592ffb21SWarner Losh if (mem_type == TTM_PL_SYSTEM) {
1084592ffb21SWarner Losh mem->mem_type = mem_type;
1085592ffb21SWarner Losh mem->placement = cur_flags;
1086592ffb21SWarner Losh mem->mm_node = NULL;
1087592ffb21SWarner Losh return 0;
1088592ffb21SWarner Losh }
1089592ffb21SWarner Losh
1090592ffb21SWarner Losh ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
1091592ffb21SWarner Losh interruptible, no_wait_gpu);
1092592ffb21SWarner Losh if (ret == 0 && mem->mm_node) {
1093592ffb21SWarner Losh mem->placement = cur_flags;
1094592ffb21SWarner Losh return 0;
1095592ffb21SWarner Losh }
1096592ffb21SWarner Losh if (ret == -ERESTARTSYS)
1097592ffb21SWarner Losh has_erestartsys = true;
1098592ffb21SWarner Losh }
1099592ffb21SWarner Losh ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
1100592ffb21SWarner Losh return ret;
1101592ffb21SWarner Losh }
1102592ffb21SWarner Losh
1103592ffb21SWarner Losh static
ttm_bo_move_buffer(struct ttm_buffer_object * bo,struct ttm_placement * placement,bool interruptible,bool no_wait_gpu)1104592ffb21SWarner Losh int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1105592ffb21SWarner Losh struct ttm_placement *placement,
1106592ffb21SWarner Losh bool interruptible,
1107592ffb21SWarner Losh bool no_wait_gpu)
1108592ffb21SWarner Losh {
1109592ffb21SWarner Losh int ret = 0;
1110592ffb21SWarner Losh struct ttm_mem_reg mem;
1111592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
1112592ffb21SWarner Losh
1113592ffb21SWarner Losh MPASS(ttm_bo_is_reserved(bo));
1114592ffb21SWarner Losh
1115592ffb21SWarner Losh /*
1116592ffb21SWarner Losh * FIXME: It's possible to pipeline buffer moves.
1117592ffb21SWarner Losh * Have the driver move function wait for idle when necessary,
1118592ffb21SWarner Losh * instead of doing it here.
1119592ffb21SWarner Losh */
1120592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
1121592ffb21SWarner Losh ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1122592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
1123592ffb21SWarner Losh if (ret)
1124592ffb21SWarner Losh return ret;
1125592ffb21SWarner Losh mem.num_pages = bo->num_pages;
1126592ffb21SWarner Losh mem.size = mem.num_pages << PAGE_SHIFT;
1127592ffb21SWarner Losh mem.page_alignment = bo->mem.page_alignment;
1128592ffb21SWarner Losh mem.bus.io_reserved_vm = false;
1129592ffb21SWarner Losh mem.bus.io_reserved_count = 0;
1130592ffb21SWarner Losh /*
1131592ffb21SWarner Losh * Determine where to move the buffer.
1132592ffb21SWarner Losh */
1133592ffb21SWarner Losh ret = ttm_bo_mem_space(bo, placement, &mem,
1134592ffb21SWarner Losh interruptible, no_wait_gpu);
1135592ffb21SWarner Losh if (ret)
1136592ffb21SWarner Losh goto out_unlock;
1137592ffb21SWarner Losh ret = ttm_bo_handle_move_mem(bo, &mem, false,
1138592ffb21SWarner Losh interruptible, no_wait_gpu);
1139592ffb21SWarner Losh out_unlock:
1140592ffb21SWarner Losh if (ret && mem.mm_node)
1141592ffb21SWarner Losh ttm_bo_mem_put(bo, &mem);
1142592ffb21SWarner Losh return ret;
1143592ffb21SWarner Losh }
1144592ffb21SWarner Losh
ttm_bo_mem_compat(struct ttm_placement * placement,struct ttm_mem_reg * mem)1145592ffb21SWarner Losh static int ttm_bo_mem_compat(struct ttm_placement *placement,
1146592ffb21SWarner Losh struct ttm_mem_reg *mem)
1147592ffb21SWarner Losh {
1148592ffb21SWarner Losh int i;
1149592ffb21SWarner Losh
1150592ffb21SWarner Losh if (mem->mm_node && placement->lpfn != 0 &&
1151592ffb21SWarner Losh (mem->start < placement->fpfn ||
1152592ffb21SWarner Losh mem->start + mem->num_pages > placement->lpfn))
1153592ffb21SWarner Losh return -1;
1154592ffb21SWarner Losh
1155592ffb21SWarner Losh for (i = 0; i < placement->num_placement; i++) {
1156592ffb21SWarner Losh if ((placement->placement[i] & mem->placement &
1157592ffb21SWarner Losh TTM_PL_MASK_CACHING) &&
1158592ffb21SWarner Losh (placement->placement[i] & mem->placement &
1159592ffb21SWarner Losh TTM_PL_MASK_MEM))
1160592ffb21SWarner Losh return i;
1161592ffb21SWarner Losh }
1162592ffb21SWarner Losh return -1;
1163592ffb21SWarner Losh }
1164592ffb21SWarner Losh
ttm_bo_validate(struct ttm_buffer_object * bo,struct ttm_placement * placement,bool interruptible,bool no_wait_gpu)1165592ffb21SWarner Losh int ttm_bo_validate(struct ttm_buffer_object *bo,
1166592ffb21SWarner Losh struct ttm_placement *placement,
1167592ffb21SWarner Losh bool interruptible,
1168592ffb21SWarner Losh bool no_wait_gpu)
1169592ffb21SWarner Losh {
1170592ffb21SWarner Losh int ret;
1171592ffb21SWarner Losh
1172592ffb21SWarner Losh MPASS(ttm_bo_is_reserved(bo));
1173592ffb21SWarner Losh /* Check that range is valid */
1174592ffb21SWarner Losh if (placement->lpfn || placement->fpfn)
1175592ffb21SWarner Losh if (placement->fpfn > placement->lpfn ||
1176592ffb21SWarner Losh (placement->lpfn - placement->fpfn) < bo->num_pages)
1177592ffb21SWarner Losh return -EINVAL;
1178592ffb21SWarner Losh /*
1179592ffb21SWarner Losh * Check whether we need to move buffer.
1180592ffb21SWarner Losh */
1181592ffb21SWarner Losh ret = ttm_bo_mem_compat(placement, &bo->mem);
1182592ffb21SWarner Losh if (ret < 0) {
1183592ffb21SWarner Losh ret = ttm_bo_move_buffer(bo, placement, interruptible,
1184592ffb21SWarner Losh no_wait_gpu);
1185592ffb21SWarner Losh if (ret)
1186592ffb21SWarner Losh return ret;
1187592ffb21SWarner Losh } else {
1188592ffb21SWarner Losh /*
1189592ffb21SWarner Losh * Use the access and other non-mapping-related flag bits from
1190592ffb21SWarner Losh * the compatible memory placement flags to the active flags
1191592ffb21SWarner Losh */
1192592ffb21SWarner Losh ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1193592ffb21SWarner Losh ~TTM_PL_MASK_MEMTYPE);
1194592ffb21SWarner Losh }
1195592ffb21SWarner Losh /*
1196592ffb21SWarner Losh * We might need to add a TTM.
1197592ffb21SWarner Losh */
1198592ffb21SWarner Losh if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1199592ffb21SWarner Losh ret = ttm_bo_add_ttm(bo, true);
1200592ffb21SWarner Losh if (ret)
1201592ffb21SWarner Losh return ret;
1202592ffb21SWarner Losh }
1203592ffb21SWarner Losh return 0;
1204592ffb21SWarner Losh }
1205592ffb21SWarner Losh
ttm_bo_check_placement(struct ttm_buffer_object * bo,struct ttm_placement * placement)1206592ffb21SWarner Losh int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1207592ffb21SWarner Losh struct ttm_placement *placement)
1208592ffb21SWarner Losh {
1209592ffb21SWarner Losh MPASS(!((placement->fpfn || placement->lpfn) &&
1210592ffb21SWarner Losh (bo->mem.num_pages > (placement->lpfn - placement->fpfn))));
1211592ffb21SWarner Losh
1212592ffb21SWarner Losh return 0;
1213592ffb21SWarner Losh }
1214592ffb21SWarner Losh
ttm_bo_init(struct ttm_bo_device * bdev,struct ttm_buffer_object * bo,unsigned long size,enum ttm_bo_type type,struct ttm_placement * placement,uint32_t page_alignment,bool interruptible,struct vm_object * persistent_swap_storage,size_t acc_size,struct sg_table * sg,void (* destroy)(struct ttm_buffer_object *))1215592ffb21SWarner Losh int ttm_bo_init(struct ttm_bo_device *bdev,
1216592ffb21SWarner Losh struct ttm_buffer_object *bo,
1217592ffb21SWarner Losh unsigned long size,
1218592ffb21SWarner Losh enum ttm_bo_type type,
1219592ffb21SWarner Losh struct ttm_placement *placement,
1220592ffb21SWarner Losh uint32_t page_alignment,
1221592ffb21SWarner Losh bool interruptible,
1222592ffb21SWarner Losh struct vm_object *persistent_swap_storage,
1223592ffb21SWarner Losh size_t acc_size,
1224592ffb21SWarner Losh struct sg_table *sg,
1225592ffb21SWarner Losh void (*destroy) (struct ttm_buffer_object *))
1226592ffb21SWarner Losh {
1227592ffb21SWarner Losh int ret = 0;
1228592ffb21SWarner Losh unsigned long num_pages;
1229592ffb21SWarner Losh struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1230592ffb21SWarner Losh
1231592ffb21SWarner Losh ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1232592ffb21SWarner Losh if (ret) {
1233592ffb21SWarner Losh printf("[TTM] Out of kernel memory\n");
1234592ffb21SWarner Losh if (destroy)
1235592ffb21SWarner Losh (*destroy)(bo);
1236592ffb21SWarner Losh else
1237592ffb21SWarner Losh free(bo, M_TTM_BO);
1238592ffb21SWarner Losh return -ENOMEM;
1239592ffb21SWarner Losh }
1240592ffb21SWarner Losh
1241592ffb21SWarner Losh num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1242592ffb21SWarner Losh if (num_pages == 0) {
1243592ffb21SWarner Losh printf("[TTM] Illegal buffer object size\n");
1244592ffb21SWarner Losh if (destroy)
1245592ffb21SWarner Losh (*destroy)(bo);
1246592ffb21SWarner Losh else
1247592ffb21SWarner Losh free(bo, M_TTM_BO);
1248592ffb21SWarner Losh ttm_mem_global_free(mem_glob, acc_size);
1249592ffb21SWarner Losh return -EINVAL;
1250592ffb21SWarner Losh }
1251592ffb21SWarner Losh bo->destroy = destroy;
1252592ffb21SWarner Losh
1253592ffb21SWarner Losh refcount_init(&bo->kref, 1);
1254592ffb21SWarner Losh refcount_init(&bo->list_kref, 1);
1255592ffb21SWarner Losh atomic_set(&bo->cpu_writers, 0);
1256592ffb21SWarner Losh atomic_set(&bo->reserved, 1);
1257592ffb21SWarner Losh INIT_LIST_HEAD(&bo->lru);
1258592ffb21SWarner Losh INIT_LIST_HEAD(&bo->ddestroy);
1259592ffb21SWarner Losh INIT_LIST_HEAD(&bo->swap);
1260592ffb21SWarner Losh INIT_LIST_HEAD(&bo->io_reserve_lru);
1261592ffb21SWarner Losh bo->bdev = bdev;
1262592ffb21SWarner Losh bo->glob = bdev->glob;
1263592ffb21SWarner Losh bo->type = type;
1264592ffb21SWarner Losh bo->num_pages = num_pages;
1265592ffb21SWarner Losh bo->mem.size = num_pages << PAGE_SHIFT;
1266592ffb21SWarner Losh bo->mem.mem_type = TTM_PL_SYSTEM;
1267592ffb21SWarner Losh bo->mem.num_pages = bo->num_pages;
1268592ffb21SWarner Losh bo->mem.mm_node = NULL;
1269592ffb21SWarner Losh bo->mem.page_alignment = page_alignment;
1270592ffb21SWarner Losh bo->mem.bus.io_reserved_vm = false;
1271592ffb21SWarner Losh bo->mem.bus.io_reserved_count = 0;
1272592ffb21SWarner Losh bo->priv_flags = 0;
1273592ffb21SWarner Losh bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1274592ffb21SWarner Losh bo->seq_valid = false;
1275592ffb21SWarner Losh bo->persistent_swap_storage = persistent_swap_storage;
1276592ffb21SWarner Losh bo->acc_size = acc_size;
1277592ffb21SWarner Losh bo->sg = sg;
1278592ffb21SWarner Losh atomic_inc(&bo->glob->bo_count);
1279592ffb21SWarner Losh
1280592ffb21SWarner Losh ret = ttm_bo_check_placement(bo, placement);
1281592ffb21SWarner Losh if (unlikely(ret != 0))
1282592ffb21SWarner Losh goto out_err;
1283592ffb21SWarner Losh
1284592ffb21SWarner Losh /*
1285592ffb21SWarner Losh * For ttm_bo_type_device buffers, allocate
1286592ffb21SWarner Losh * address space from the device.
1287592ffb21SWarner Losh */
1288592ffb21SWarner Losh if (bo->type == ttm_bo_type_device ||
1289592ffb21SWarner Losh bo->type == ttm_bo_type_sg) {
1290592ffb21SWarner Losh ret = ttm_bo_setup_vm(bo);
1291592ffb21SWarner Losh if (ret)
1292592ffb21SWarner Losh goto out_err;
1293592ffb21SWarner Losh }
1294592ffb21SWarner Losh
1295592ffb21SWarner Losh ret = ttm_bo_validate(bo, placement, interruptible, false);
1296592ffb21SWarner Losh if (ret)
1297592ffb21SWarner Losh goto out_err;
1298592ffb21SWarner Losh
1299592ffb21SWarner Losh ttm_bo_unreserve(bo);
1300592ffb21SWarner Losh return 0;
1301592ffb21SWarner Losh
1302592ffb21SWarner Losh out_err:
1303592ffb21SWarner Losh ttm_bo_unreserve(bo);
1304592ffb21SWarner Losh ttm_bo_unref(&bo);
1305592ffb21SWarner Losh
1306592ffb21SWarner Losh return ret;
1307592ffb21SWarner Losh }
1308592ffb21SWarner Losh
ttm_bo_acc_size(struct ttm_bo_device * bdev,unsigned long bo_size,unsigned struct_size)1309592ffb21SWarner Losh size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1310592ffb21SWarner Losh unsigned long bo_size,
1311592ffb21SWarner Losh unsigned struct_size)
1312592ffb21SWarner Losh {
1313592ffb21SWarner Losh unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1314592ffb21SWarner Losh size_t size = 0;
1315592ffb21SWarner Losh
1316592ffb21SWarner Losh size += ttm_round_pot(struct_size);
1317592ffb21SWarner Losh size += PAGE_ALIGN(npages * sizeof(void *));
1318592ffb21SWarner Losh size += ttm_round_pot(sizeof(struct ttm_tt));
1319592ffb21SWarner Losh return size;
1320592ffb21SWarner Losh }
1321592ffb21SWarner Losh
ttm_bo_dma_acc_size(struct ttm_bo_device * bdev,unsigned long bo_size,unsigned struct_size)1322592ffb21SWarner Losh size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1323592ffb21SWarner Losh unsigned long bo_size,
1324592ffb21SWarner Losh unsigned struct_size)
1325592ffb21SWarner Losh {
1326592ffb21SWarner Losh unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1327592ffb21SWarner Losh size_t size = 0;
1328592ffb21SWarner Losh
1329592ffb21SWarner Losh size += ttm_round_pot(struct_size);
1330592ffb21SWarner Losh size += PAGE_ALIGN(npages * sizeof(void *));
1331592ffb21SWarner Losh size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1332592ffb21SWarner Losh size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1333592ffb21SWarner Losh return size;
1334592ffb21SWarner Losh }
1335592ffb21SWarner Losh
ttm_bo_create(struct ttm_bo_device * bdev,unsigned long size,enum ttm_bo_type type,struct ttm_placement * placement,uint32_t page_alignment,bool interruptible,struct vm_object * persistent_swap_storage,struct ttm_buffer_object ** p_bo)1336592ffb21SWarner Losh int ttm_bo_create(struct ttm_bo_device *bdev,
1337592ffb21SWarner Losh unsigned long size,
1338592ffb21SWarner Losh enum ttm_bo_type type,
1339592ffb21SWarner Losh struct ttm_placement *placement,
1340592ffb21SWarner Losh uint32_t page_alignment,
1341592ffb21SWarner Losh bool interruptible,
1342592ffb21SWarner Losh struct vm_object *persistent_swap_storage,
1343592ffb21SWarner Losh struct ttm_buffer_object **p_bo)
1344592ffb21SWarner Losh {
1345592ffb21SWarner Losh struct ttm_buffer_object *bo;
1346592ffb21SWarner Losh size_t acc_size;
1347592ffb21SWarner Losh int ret;
1348592ffb21SWarner Losh
1349592ffb21SWarner Losh bo = malloc(sizeof(*bo), M_TTM_BO, M_WAITOK | M_ZERO);
1350592ffb21SWarner Losh acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1351592ffb21SWarner Losh ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1352592ffb21SWarner Losh interruptible, persistent_swap_storage, acc_size,
1353592ffb21SWarner Losh NULL, NULL);
1354592ffb21SWarner Losh if (likely(ret == 0))
1355592ffb21SWarner Losh *p_bo = bo;
1356592ffb21SWarner Losh
1357592ffb21SWarner Losh return ret;
1358592ffb21SWarner Losh }
1359592ffb21SWarner Losh
ttm_bo_force_list_clean(struct ttm_bo_device * bdev,unsigned mem_type,bool allow_errors)1360592ffb21SWarner Losh static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1361592ffb21SWarner Losh unsigned mem_type, bool allow_errors)
1362592ffb21SWarner Losh {
1363592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1364592ffb21SWarner Losh struct ttm_bo_global *glob = bdev->glob;
1365592ffb21SWarner Losh int ret;
1366592ffb21SWarner Losh
1367592ffb21SWarner Losh /*
1368592ffb21SWarner Losh * Can't use standard list traversal since we're unlocking.
1369592ffb21SWarner Losh */
1370592ffb21SWarner Losh
1371592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
1372592ffb21SWarner Losh while (!list_empty(&man->lru)) {
1373592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
1374592ffb21SWarner Losh ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1375592ffb21SWarner Losh if (ret) {
1376592ffb21SWarner Losh if (allow_errors) {
1377592ffb21SWarner Losh return ret;
1378592ffb21SWarner Losh } else {
1379592ffb21SWarner Losh printf("[TTM] Cleanup eviction failed\n");
1380592ffb21SWarner Losh }
1381592ffb21SWarner Losh }
1382592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
1383592ffb21SWarner Losh }
1384592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
1385592ffb21SWarner Losh return 0;
1386592ffb21SWarner Losh }
1387592ffb21SWarner Losh
ttm_bo_clean_mm(struct ttm_bo_device * bdev,unsigned mem_type)1388592ffb21SWarner Losh int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1389592ffb21SWarner Losh {
1390592ffb21SWarner Losh struct ttm_mem_type_manager *man;
1391592ffb21SWarner Losh int ret = -EINVAL;
1392592ffb21SWarner Losh
1393592ffb21SWarner Losh if (mem_type >= TTM_NUM_MEM_TYPES) {
1394592ffb21SWarner Losh printf("[TTM] Illegal memory type %d\n", mem_type);
1395592ffb21SWarner Losh return ret;
1396592ffb21SWarner Losh }
1397592ffb21SWarner Losh man = &bdev->man[mem_type];
1398592ffb21SWarner Losh
1399592ffb21SWarner Losh if (!man->has_type) {
1400592ffb21SWarner Losh printf("[TTM] Trying to take down uninitialized memory manager type %u\n",
1401592ffb21SWarner Losh mem_type);
1402592ffb21SWarner Losh return ret;
1403592ffb21SWarner Losh }
1404592ffb21SWarner Losh
1405592ffb21SWarner Losh man->use_type = false;
1406592ffb21SWarner Losh man->has_type = false;
1407592ffb21SWarner Losh
1408592ffb21SWarner Losh ret = 0;
1409592ffb21SWarner Losh if (mem_type > 0) {
1410592ffb21SWarner Losh ttm_bo_force_list_clean(bdev, mem_type, false);
1411592ffb21SWarner Losh
1412592ffb21SWarner Losh ret = (*man->func->takedown)(man);
1413592ffb21SWarner Losh }
1414592ffb21SWarner Losh
1415592ffb21SWarner Losh return ret;
1416592ffb21SWarner Losh }
1417592ffb21SWarner Losh
ttm_bo_evict_mm(struct ttm_bo_device * bdev,unsigned mem_type)1418592ffb21SWarner Losh int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1419592ffb21SWarner Losh {
1420592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1421592ffb21SWarner Losh
1422592ffb21SWarner Losh if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1423592ffb21SWarner Losh printf("[TTM] Illegal memory manager memory type %u\n", mem_type);
1424592ffb21SWarner Losh return -EINVAL;
1425592ffb21SWarner Losh }
1426592ffb21SWarner Losh
1427592ffb21SWarner Losh if (!man->has_type) {
1428592ffb21SWarner Losh printf("[TTM] Memory type %u has not been initialized\n", mem_type);
1429592ffb21SWarner Losh return 0;
1430592ffb21SWarner Losh }
1431592ffb21SWarner Losh
1432592ffb21SWarner Losh return ttm_bo_force_list_clean(bdev, mem_type, true);
1433592ffb21SWarner Losh }
1434592ffb21SWarner Losh
ttm_bo_init_mm(struct ttm_bo_device * bdev,unsigned type,unsigned long p_size)1435592ffb21SWarner Losh int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1436592ffb21SWarner Losh unsigned long p_size)
1437592ffb21SWarner Losh {
1438592ffb21SWarner Losh int ret = -EINVAL;
1439592ffb21SWarner Losh struct ttm_mem_type_manager *man;
1440592ffb21SWarner Losh
1441592ffb21SWarner Losh MPASS(type < TTM_NUM_MEM_TYPES);
1442592ffb21SWarner Losh man = &bdev->man[type];
1443592ffb21SWarner Losh MPASS(!man->has_type);
1444592ffb21SWarner Losh man->io_reserve_fastpath = true;
1445592ffb21SWarner Losh man->use_io_reserve_lru = false;
1446592ffb21SWarner Losh sx_init(&man->io_reserve_mutex, "ttmman");
1447592ffb21SWarner Losh INIT_LIST_HEAD(&man->io_reserve_lru);
1448592ffb21SWarner Losh
1449592ffb21SWarner Losh ret = bdev->driver->init_mem_type(bdev, type, man);
1450592ffb21SWarner Losh if (ret)
1451592ffb21SWarner Losh return ret;
1452592ffb21SWarner Losh man->bdev = bdev;
1453592ffb21SWarner Losh
1454592ffb21SWarner Losh ret = 0;
1455592ffb21SWarner Losh if (type != TTM_PL_SYSTEM) {
1456592ffb21SWarner Losh ret = (*man->func->init)(man, p_size);
1457592ffb21SWarner Losh if (ret)
1458592ffb21SWarner Losh return ret;
1459592ffb21SWarner Losh }
1460592ffb21SWarner Losh man->has_type = true;
1461592ffb21SWarner Losh man->use_type = true;
1462592ffb21SWarner Losh man->size = p_size;
1463592ffb21SWarner Losh
1464592ffb21SWarner Losh INIT_LIST_HEAD(&man->lru);
1465592ffb21SWarner Losh
1466592ffb21SWarner Losh return 0;
1467592ffb21SWarner Losh }
1468592ffb21SWarner Losh
ttm_bo_global_kobj_release(struct ttm_bo_global * glob)1469592ffb21SWarner Losh static void ttm_bo_global_kobj_release(struct ttm_bo_global *glob)
1470592ffb21SWarner Losh {
1471592ffb21SWarner Losh
1472592ffb21SWarner Losh ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1473592ffb21SWarner Losh vm_page_free(glob->dummy_read_page);
1474592ffb21SWarner Losh }
1475592ffb21SWarner Losh
ttm_bo_global_release(struct drm_global_reference * ref)1476592ffb21SWarner Losh void ttm_bo_global_release(struct drm_global_reference *ref)
1477592ffb21SWarner Losh {
1478592ffb21SWarner Losh struct ttm_bo_global *glob = ref->object;
1479592ffb21SWarner Losh
1480592ffb21SWarner Losh if (refcount_release(&glob->kobj_ref))
1481592ffb21SWarner Losh ttm_bo_global_kobj_release(glob);
1482592ffb21SWarner Losh }
1483592ffb21SWarner Losh
ttm_bo_global_init(struct drm_global_reference * ref)1484592ffb21SWarner Losh int ttm_bo_global_init(struct drm_global_reference *ref)
1485592ffb21SWarner Losh {
1486592ffb21SWarner Losh struct ttm_bo_global_ref *bo_ref =
1487592ffb21SWarner Losh container_of(ref, struct ttm_bo_global_ref, ref);
1488592ffb21SWarner Losh struct ttm_bo_global *glob = ref->object;
148984c39222SMark Johnston int ret;
1490592ffb21SWarner Losh int tries;
1491592ffb21SWarner Losh
1492592ffb21SWarner Losh sx_init(&glob->device_list_mutex, "ttmdlm");
1493592ffb21SWarner Losh mtx_init(&glob->lru_lock, "ttmlru", NULL, MTX_DEF);
1494592ffb21SWarner Losh glob->mem_glob = bo_ref->mem_glob;
1495592ffb21SWarner Losh tries = 0;
1496592ffb21SWarner Losh retry:
149784c39222SMark Johnston glob->dummy_read_page = vm_page_alloc_noobj_contig(0, 1, 0,
149884c39222SMark Johnston VM_MAX_ADDRESS, PAGE_SIZE, 0, VM_MEMATTR_UNCACHEABLE);
1499592ffb21SWarner Losh
1500592ffb21SWarner Losh if (unlikely(glob->dummy_read_page == NULL)) {
1501*2619c5ccSJason A. Harmening if (tries < 1 && (vm_page_reclaim_contig(0, 1, 0,
1502*2619c5ccSJason A. Harmening VM_MAX_ADDRESS, PAGE_SIZE, 0) == 0)) {
1503592ffb21SWarner Losh tries++;
1504592ffb21SWarner Losh goto retry;
1505592ffb21SWarner Losh }
1506592ffb21SWarner Losh ret = -ENOMEM;
1507592ffb21SWarner Losh goto out_no_drp;
1508592ffb21SWarner Losh }
1509592ffb21SWarner Losh
1510592ffb21SWarner Losh INIT_LIST_HEAD(&glob->swap_lru);
1511592ffb21SWarner Losh INIT_LIST_HEAD(&glob->device_list);
1512592ffb21SWarner Losh
1513592ffb21SWarner Losh ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1514592ffb21SWarner Losh ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1515592ffb21SWarner Losh if (unlikely(ret != 0)) {
1516592ffb21SWarner Losh printf("[TTM] Could not register buffer object swapout\n");
1517592ffb21SWarner Losh goto out_no_shrink;
1518592ffb21SWarner Losh }
1519592ffb21SWarner Losh
1520592ffb21SWarner Losh atomic_set(&glob->bo_count, 0);
1521592ffb21SWarner Losh
1522592ffb21SWarner Losh refcount_init(&glob->kobj_ref, 1);
1523592ffb21SWarner Losh return (0);
1524592ffb21SWarner Losh
1525592ffb21SWarner Losh out_no_shrink:
1526592ffb21SWarner Losh vm_page_free(glob->dummy_read_page);
1527592ffb21SWarner Losh out_no_drp:
1528592ffb21SWarner Losh free(glob, M_DRM_GLOBAL);
1529592ffb21SWarner Losh return ret;
1530592ffb21SWarner Losh }
1531592ffb21SWarner Losh
ttm_bo_device_release(struct ttm_bo_device * bdev)1532592ffb21SWarner Losh int ttm_bo_device_release(struct ttm_bo_device *bdev)
1533592ffb21SWarner Losh {
1534592ffb21SWarner Losh int ret = 0;
1535592ffb21SWarner Losh unsigned i = TTM_NUM_MEM_TYPES;
1536592ffb21SWarner Losh struct ttm_mem_type_manager *man;
1537592ffb21SWarner Losh struct ttm_bo_global *glob = bdev->glob;
1538592ffb21SWarner Losh
1539592ffb21SWarner Losh while (i--) {
1540592ffb21SWarner Losh man = &bdev->man[i];
1541592ffb21SWarner Losh if (man->has_type) {
1542592ffb21SWarner Losh man->use_type = false;
1543592ffb21SWarner Losh if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1544592ffb21SWarner Losh ret = -EBUSY;
1545592ffb21SWarner Losh printf("[TTM] DRM memory manager type %d is not clean\n",
1546592ffb21SWarner Losh i);
1547592ffb21SWarner Losh }
1548592ffb21SWarner Losh man->has_type = false;
1549592ffb21SWarner Losh }
1550592ffb21SWarner Losh }
1551592ffb21SWarner Losh
1552592ffb21SWarner Losh sx_xlock(&glob->device_list_mutex);
1553592ffb21SWarner Losh list_del(&bdev->device_list);
1554592ffb21SWarner Losh sx_xunlock(&glob->device_list_mutex);
1555592ffb21SWarner Losh
1556592ffb21SWarner Losh if (taskqueue_cancel_timeout(taskqueue_thread, &bdev->wq, NULL))
1557592ffb21SWarner Losh taskqueue_drain_timeout(taskqueue_thread, &bdev->wq);
1558592ffb21SWarner Losh
1559592ffb21SWarner Losh while (ttm_bo_delayed_delete(bdev, true))
1560592ffb21SWarner Losh ;
1561592ffb21SWarner Losh
1562592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
1563592ffb21SWarner Losh if (list_empty(&bdev->ddestroy))
1564592ffb21SWarner Losh TTM_DEBUG("Delayed destroy list was clean\n");
1565592ffb21SWarner Losh
1566592ffb21SWarner Losh if (list_empty(&bdev->man[0].lru))
1567592ffb21SWarner Losh TTM_DEBUG("Swap list was clean\n");
1568592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
1569592ffb21SWarner Losh
1570592ffb21SWarner Losh MPASS(drm_mm_clean(&bdev->addr_space_mm));
1571592ffb21SWarner Losh rw_wlock(&bdev->vm_lock);
1572592ffb21SWarner Losh drm_mm_takedown(&bdev->addr_space_mm);
1573592ffb21SWarner Losh rw_wunlock(&bdev->vm_lock);
1574592ffb21SWarner Losh
1575592ffb21SWarner Losh return ret;
1576592ffb21SWarner Losh }
1577592ffb21SWarner Losh
ttm_bo_device_init(struct ttm_bo_device * bdev,struct ttm_bo_global * glob,struct ttm_bo_driver * driver,uint64_t file_page_offset,bool need_dma32)1578592ffb21SWarner Losh int ttm_bo_device_init(struct ttm_bo_device *bdev,
1579592ffb21SWarner Losh struct ttm_bo_global *glob,
1580592ffb21SWarner Losh struct ttm_bo_driver *driver,
1581592ffb21SWarner Losh uint64_t file_page_offset,
1582592ffb21SWarner Losh bool need_dma32)
1583592ffb21SWarner Losh {
1584592ffb21SWarner Losh int ret = -EINVAL;
1585592ffb21SWarner Losh
1586592ffb21SWarner Losh rw_init(&bdev->vm_lock, "ttmvml");
1587592ffb21SWarner Losh bdev->driver = driver;
1588592ffb21SWarner Losh
1589592ffb21SWarner Losh memset(bdev->man, 0, sizeof(bdev->man));
1590592ffb21SWarner Losh
1591592ffb21SWarner Losh /*
1592592ffb21SWarner Losh * Initialize the system memory buffer type.
1593592ffb21SWarner Losh * Other types need to be driver / IOCTL initialized.
1594592ffb21SWarner Losh */
1595592ffb21SWarner Losh ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1596592ffb21SWarner Losh if (unlikely(ret != 0))
1597592ffb21SWarner Losh goto out_no_sys;
1598592ffb21SWarner Losh
1599592ffb21SWarner Losh RB_INIT(&bdev->addr_space_rb);
1600592ffb21SWarner Losh ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1601592ffb21SWarner Losh if (unlikely(ret != 0))
1602592ffb21SWarner Losh goto out_no_addr_mm;
1603592ffb21SWarner Losh
1604592ffb21SWarner Losh TIMEOUT_TASK_INIT(taskqueue_thread, &bdev->wq, 0,
1605592ffb21SWarner Losh ttm_bo_delayed_workqueue, bdev);
1606592ffb21SWarner Losh INIT_LIST_HEAD(&bdev->ddestroy);
1607592ffb21SWarner Losh bdev->dev_mapping = NULL;
1608592ffb21SWarner Losh bdev->glob = glob;
1609592ffb21SWarner Losh bdev->need_dma32 = need_dma32;
1610592ffb21SWarner Losh bdev->val_seq = 0;
1611592ffb21SWarner Losh mtx_init(&bdev->fence_lock, "ttmfence", NULL, MTX_DEF);
1612592ffb21SWarner Losh sx_xlock(&glob->device_list_mutex);
1613592ffb21SWarner Losh list_add_tail(&bdev->device_list, &glob->device_list);
1614592ffb21SWarner Losh sx_xunlock(&glob->device_list_mutex);
1615592ffb21SWarner Losh
1616592ffb21SWarner Losh return 0;
1617592ffb21SWarner Losh out_no_addr_mm:
1618592ffb21SWarner Losh ttm_bo_clean_mm(bdev, 0);
1619592ffb21SWarner Losh out_no_sys:
1620592ffb21SWarner Losh return ret;
1621592ffb21SWarner Losh }
1622592ffb21SWarner Losh
1623592ffb21SWarner Losh /*
1624592ffb21SWarner Losh * buffer object vm functions.
1625592ffb21SWarner Losh */
1626592ffb21SWarner Losh
ttm_mem_reg_is_pci(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem)1627592ffb21SWarner Losh bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1628592ffb21SWarner Losh {
1629592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1630592ffb21SWarner Losh
1631592ffb21SWarner Losh if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1632592ffb21SWarner Losh if (mem->mem_type == TTM_PL_SYSTEM)
1633592ffb21SWarner Losh return false;
1634592ffb21SWarner Losh
1635592ffb21SWarner Losh if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1636592ffb21SWarner Losh return false;
1637592ffb21SWarner Losh
1638592ffb21SWarner Losh if (mem->placement & TTM_PL_FLAG_CACHED)
1639592ffb21SWarner Losh return false;
1640592ffb21SWarner Losh }
1641592ffb21SWarner Losh return true;
1642592ffb21SWarner Losh }
1643592ffb21SWarner Losh
ttm_bo_unmap_virtual_locked(struct ttm_buffer_object * bo)1644592ffb21SWarner Losh void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1645592ffb21SWarner Losh {
1646592ffb21SWarner Losh
1647592ffb21SWarner Losh ttm_bo_release_mmap(bo);
1648592ffb21SWarner Losh ttm_mem_io_free_vm(bo);
1649592ffb21SWarner Losh }
1650592ffb21SWarner Losh
ttm_bo_unmap_virtual(struct ttm_buffer_object * bo)1651592ffb21SWarner Losh void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1652592ffb21SWarner Losh {
1653592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
1654592ffb21SWarner Losh struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1655592ffb21SWarner Losh
1656592ffb21SWarner Losh ttm_mem_io_lock(man, false);
1657592ffb21SWarner Losh ttm_bo_unmap_virtual_locked(bo);
1658592ffb21SWarner Losh ttm_mem_io_unlock(man);
1659592ffb21SWarner Losh }
1660592ffb21SWarner Losh
ttm_bo_vm_insert_rb(struct ttm_buffer_object * bo)1661592ffb21SWarner Losh static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1662592ffb21SWarner Losh {
1663592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
1664592ffb21SWarner Losh
1665592ffb21SWarner Losh /* The caller acquired bdev->vm_lock. */
1666592ffb21SWarner Losh RB_INSERT(ttm_bo_device_buffer_objects, &bdev->addr_space_rb, bo);
1667592ffb21SWarner Losh }
1668592ffb21SWarner Losh
1669592ffb21SWarner Losh /**
1670592ffb21SWarner Losh * ttm_bo_setup_vm:
1671592ffb21SWarner Losh *
1672592ffb21SWarner Losh * @bo: the buffer to allocate address space for
1673592ffb21SWarner Losh *
1674592ffb21SWarner Losh * Allocate address space in the drm device so that applications
1675592ffb21SWarner Losh * can mmap the buffer and access the contents. This only
1676592ffb21SWarner Losh * applies to ttm_bo_type_device objects as others are not
1677592ffb21SWarner Losh * placed in the drm device address space.
1678592ffb21SWarner Losh */
1679592ffb21SWarner Losh
ttm_bo_setup_vm(struct ttm_buffer_object * bo)1680592ffb21SWarner Losh static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1681592ffb21SWarner Losh {
1682592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
1683592ffb21SWarner Losh int ret;
1684592ffb21SWarner Losh
1685592ffb21SWarner Losh retry_pre_get:
1686592ffb21SWarner Losh ret = drm_mm_pre_get(&bdev->addr_space_mm);
1687592ffb21SWarner Losh if (unlikely(ret != 0))
1688592ffb21SWarner Losh return ret;
1689592ffb21SWarner Losh
1690592ffb21SWarner Losh rw_wlock(&bdev->vm_lock);
1691592ffb21SWarner Losh bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1692592ffb21SWarner Losh bo->mem.num_pages, 0, 0);
1693592ffb21SWarner Losh
1694592ffb21SWarner Losh if (unlikely(bo->vm_node == NULL)) {
1695592ffb21SWarner Losh ret = -ENOMEM;
1696592ffb21SWarner Losh goto out_unlock;
1697592ffb21SWarner Losh }
1698592ffb21SWarner Losh
1699592ffb21SWarner Losh bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1700592ffb21SWarner Losh bo->mem.num_pages, 0);
1701592ffb21SWarner Losh
1702592ffb21SWarner Losh if (unlikely(bo->vm_node == NULL)) {
1703592ffb21SWarner Losh rw_wunlock(&bdev->vm_lock);
1704592ffb21SWarner Losh goto retry_pre_get;
1705592ffb21SWarner Losh }
1706592ffb21SWarner Losh
1707592ffb21SWarner Losh ttm_bo_vm_insert_rb(bo);
1708592ffb21SWarner Losh rw_wunlock(&bdev->vm_lock);
1709592ffb21SWarner Losh bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1710592ffb21SWarner Losh
1711592ffb21SWarner Losh return 0;
1712592ffb21SWarner Losh out_unlock:
1713592ffb21SWarner Losh rw_wunlock(&bdev->vm_lock);
1714592ffb21SWarner Losh return ret;
1715592ffb21SWarner Losh }
1716592ffb21SWarner Losh
ttm_bo_wait(struct ttm_buffer_object * bo,bool lazy,bool interruptible,bool no_wait)1717592ffb21SWarner Losh int ttm_bo_wait(struct ttm_buffer_object *bo,
1718592ffb21SWarner Losh bool lazy, bool interruptible, bool no_wait)
1719592ffb21SWarner Losh {
1720592ffb21SWarner Losh struct ttm_bo_driver *driver = bo->bdev->driver;
1721592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
1722592ffb21SWarner Losh void *sync_obj;
1723592ffb21SWarner Losh int ret = 0;
1724592ffb21SWarner Losh
1725592ffb21SWarner Losh if (likely(bo->sync_obj == NULL))
1726592ffb21SWarner Losh return 0;
1727592ffb21SWarner Losh
1728592ffb21SWarner Losh while (bo->sync_obj) {
1729592ffb21SWarner Losh
1730592ffb21SWarner Losh if (driver->sync_obj_signaled(bo->sync_obj)) {
1731592ffb21SWarner Losh void *tmp_obj = bo->sync_obj;
1732592ffb21SWarner Losh bo->sync_obj = NULL;
1733592ffb21SWarner Losh clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1734592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
1735592ffb21SWarner Losh driver->sync_obj_unref(&tmp_obj);
1736592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
1737592ffb21SWarner Losh continue;
1738592ffb21SWarner Losh }
1739592ffb21SWarner Losh
1740592ffb21SWarner Losh if (no_wait)
1741592ffb21SWarner Losh return -EBUSY;
1742592ffb21SWarner Losh
1743592ffb21SWarner Losh sync_obj = driver->sync_obj_ref(bo->sync_obj);
1744592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
1745592ffb21SWarner Losh ret = driver->sync_obj_wait(sync_obj,
1746592ffb21SWarner Losh lazy, interruptible);
1747592ffb21SWarner Losh if (unlikely(ret != 0)) {
1748592ffb21SWarner Losh driver->sync_obj_unref(&sync_obj);
1749592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
1750592ffb21SWarner Losh return ret;
1751592ffb21SWarner Losh }
1752592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
1753592ffb21SWarner Losh if (likely(bo->sync_obj == sync_obj)) {
1754592ffb21SWarner Losh void *tmp_obj = bo->sync_obj;
1755592ffb21SWarner Losh bo->sync_obj = NULL;
1756592ffb21SWarner Losh clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1757592ffb21SWarner Losh &bo->priv_flags);
1758592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
1759592ffb21SWarner Losh driver->sync_obj_unref(&sync_obj);
1760592ffb21SWarner Losh driver->sync_obj_unref(&tmp_obj);
1761592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
1762592ffb21SWarner Losh } else {
1763592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
1764592ffb21SWarner Losh driver->sync_obj_unref(&sync_obj);
1765592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
1766592ffb21SWarner Losh }
1767592ffb21SWarner Losh }
1768592ffb21SWarner Losh return 0;
1769592ffb21SWarner Losh }
1770592ffb21SWarner Losh
ttm_bo_synccpu_write_grab(struct ttm_buffer_object * bo,bool no_wait)1771592ffb21SWarner Losh int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1772592ffb21SWarner Losh {
1773592ffb21SWarner Losh struct ttm_bo_device *bdev = bo->bdev;
1774592ffb21SWarner Losh int ret = 0;
1775592ffb21SWarner Losh
1776592ffb21SWarner Losh /*
1777592ffb21SWarner Losh * Using ttm_bo_reserve makes sure the lru lists are updated.
1778592ffb21SWarner Losh */
1779592ffb21SWarner Losh
1780592ffb21SWarner Losh ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1781592ffb21SWarner Losh if (unlikely(ret != 0))
1782592ffb21SWarner Losh return ret;
1783592ffb21SWarner Losh mtx_lock(&bdev->fence_lock);
1784592ffb21SWarner Losh ret = ttm_bo_wait(bo, false, true, no_wait);
1785592ffb21SWarner Losh mtx_unlock(&bdev->fence_lock);
1786592ffb21SWarner Losh if (likely(ret == 0))
1787592ffb21SWarner Losh atomic_inc(&bo->cpu_writers);
1788592ffb21SWarner Losh ttm_bo_unreserve(bo);
1789592ffb21SWarner Losh return ret;
1790592ffb21SWarner Losh }
1791592ffb21SWarner Losh
ttm_bo_synccpu_write_release(struct ttm_buffer_object * bo)1792592ffb21SWarner Losh void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1793592ffb21SWarner Losh {
1794592ffb21SWarner Losh atomic_dec(&bo->cpu_writers);
1795592ffb21SWarner Losh }
1796592ffb21SWarner Losh
1797592ffb21SWarner Losh /**
1798592ffb21SWarner Losh * A buffer object shrink method that tries to swap out the first
1799592ffb21SWarner Losh * buffer object on the bo_global::swap_lru list.
1800592ffb21SWarner Losh */
1801592ffb21SWarner Losh
ttm_bo_swapout(struct ttm_mem_shrink * shrink)1802592ffb21SWarner Losh static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1803592ffb21SWarner Losh {
1804592ffb21SWarner Losh struct ttm_bo_global *glob =
1805592ffb21SWarner Losh container_of(shrink, struct ttm_bo_global, shrink);
1806592ffb21SWarner Losh struct ttm_buffer_object *bo;
1807592ffb21SWarner Losh int ret = -EBUSY;
1808592ffb21SWarner Losh int put_count;
1809592ffb21SWarner Losh uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1810592ffb21SWarner Losh
1811592ffb21SWarner Losh mtx_lock(&glob->lru_lock);
1812592ffb21SWarner Losh list_for_each_entry(bo, &glob->swap_lru, swap) {
1813592ffb21SWarner Losh ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
1814592ffb21SWarner Losh if (!ret)
1815592ffb21SWarner Losh break;
1816592ffb21SWarner Losh }
1817592ffb21SWarner Losh
1818592ffb21SWarner Losh if (ret) {
1819592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
1820592ffb21SWarner Losh return ret;
1821592ffb21SWarner Losh }
1822592ffb21SWarner Losh
1823592ffb21SWarner Losh refcount_acquire(&bo->list_kref);
1824592ffb21SWarner Losh
1825592ffb21SWarner Losh if (!list_empty(&bo->ddestroy)) {
1826592ffb21SWarner Losh ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1827592ffb21SWarner Losh if (refcount_release(&bo->list_kref))
1828592ffb21SWarner Losh ttm_bo_release_list(bo);
1829592ffb21SWarner Losh return ret;
1830592ffb21SWarner Losh }
1831592ffb21SWarner Losh
1832592ffb21SWarner Losh put_count = ttm_bo_del_from_lru(bo);
1833592ffb21SWarner Losh mtx_unlock(&glob->lru_lock);
1834592ffb21SWarner Losh
1835592ffb21SWarner Losh ttm_bo_list_ref_sub(bo, put_count, true);
1836592ffb21SWarner Losh
1837592ffb21SWarner Losh /**
1838592ffb21SWarner Losh * Wait for GPU, then move to system cached.
1839592ffb21SWarner Losh */
1840592ffb21SWarner Losh
1841592ffb21SWarner Losh mtx_lock(&bo->bdev->fence_lock);
1842592ffb21SWarner Losh ret = ttm_bo_wait(bo, false, false, false);
1843592ffb21SWarner Losh mtx_unlock(&bo->bdev->fence_lock);
1844592ffb21SWarner Losh
1845592ffb21SWarner Losh if (unlikely(ret != 0))
1846592ffb21SWarner Losh goto out;
1847592ffb21SWarner Losh
1848592ffb21SWarner Losh if ((bo->mem.placement & swap_placement) != swap_placement) {
1849592ffb21SWarner Losh struct ttm_mem_reg evict_mem;
1850592ffb21SWarner Losh
1851592ffb21SWarner Losh evict_mem = bo->mem;
1852592ffb21SWarner Losh evict_mem.mm_node = NULL;
1853592ffb21SWarner Losh evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1854592ffb21SWarner Losh evict_mem.mem_type = TTM_PL_SYSTEM;
1855592ffb21SWarner Losh
1856592ffb21SWarner Losh ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1857592ffb21SWarner Losh false, false);
1858592ffb21SWarner Losh if (unlikely(ret != 0))
1859592ffb21SWarner Losh goto out;
1860592ffb21SWarner Losh }
1861592ffb21SWarner Losh
1862592ffb21SWarner Losh ttm_bo_unmap_virtual(bo);
1863592ffb21SWarner Losh
1864592ffb21SWarner Losh /**
1865592ffb21SWarner Losh * Swap out. Buffer will be swapped in again as soon as
1866592ffb21SWarner Losh * anyone tries to access a ttm page.
1867592ffb21SWarner Losh */
1868592ffb21SWarner Losh
1869592ffb21SWarner Losh if (bo->bdev->driver->swap_notify)
1870592ffb21SWarner Losh bo->bdev->driver->swap_notify(bo);
1871592ffb21SWarner Losh
1872592ffb21SWarner Losh ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1873592ffb21SWarner Losh out:
1874592ffb21SWarner Losh
1875592ffb21SWarner Losh /**
1876592ffb21SWarner Losh *
1877592ffb21SWarner Losh * Unreserve without putting on LRU to avoid swapping out an
1878592ffb21SWarner Losh * already swapped buffer.
1879592ffb21SWarner Losh */
1880592ffb21SWarner Losh
1881592ffb21SWarner Losh atomic_set(&bo->reserved, 0);
1882592ffb21SWarner Losh wakeup(bo);
1883592ffb21SWarner Losh if (refcount_release(&bo->list_kref))
1884592ffb21SWarner Losh ttm_bo_release_list(bo);
1885592ffb21SWarner Losh return ret;
1886592ffb21SWarner Losh }
1887592ffb21SWarner Losh
ttm_bo_swapout_all(struct ttm_bo_device * bdev)1888592ffb21SWarner Losh void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1889592ffb21SWarner Losh {
1890592ffb21SWarner Losh while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1891592ffb21SWarner Losh ;
1892592ffb21SWarner Losh }
1893