blob: f157a9efd220864858489ee8e8c4fe71ba68900c [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/ttm/ttm_bo_driver.h>
32#include <drm/ttm/ttm_placement.h>
David Herrmann72525b32013-07-24 21:08:53 +020033#include <drm/drm_vma_manager.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020034#include <linux/io.h>
35#include <linux/highmem.h>
36#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020038#include <linux/vmalloc.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020039#include <linux/module.h>
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +020040#include <linux/reservation.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020041
42void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
43{
Ben Skeggs42311ff2010-08-04 12:07:08 +100044 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020045}
46
47int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
Michel Dänzer34b58352016-08-05 18:36:10 +090048 bool evict, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +000049 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020050{
51 struct ttm_tt *ttm = bo->ttm;
52 struct ttm_mem_reg *old_mem = &bo->mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020053 int ret;
54
55 if (old_mem->mem_type != TTM_PL_SYSTEM) {
Michel Dänzer34b58352016-08-05 18:36:10 +090056 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
57
58 if (unlikely(ret != 0)) {
59 if (ret != -ERESTARTSYS)
60 pr_err("Failed to expire sync object before unbinding TTM\n");
61 return ret;
62 }
63
Christian König2ff2bf12016-07-21 12:18:19 +020064 ttm_tt_unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020065 ttm_bo_free_old_node(bo);
66 ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
67 TTM_PL_MASK_MEM);
68 old_mem->mem_type = TTM_PL_SYSTEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020069 }
70
71 ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
72 if (unlikely(ret != 0))
73 return ret;
74
75 if (new_mem->mem_type != TTM_PL_SYSTEM) {
76 ret = ttm_tt_bind(ttm, new_mem);
77 if (unlikely(ret != 0))
78 return ret;
79 }
80
81 *old_mem = *new_mem;
82 new_mem->mm_node = NULL;
Austin Yuan110b20c2010-01-21 13:45:40 +080083
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020084 return 0;
85}
86EXPORT_SYMBOL(ttm_bo_move_ttm);
87
Thomas Hellstromeba67092010-11-11 09:41:57 +010088int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
Jerome Glisse82c5da62010-04-09 14:39:23 +020089{
Thomas Hellstromeba67092010-11-11 09:41:57 +010090 if (likely(man->io_reserve_fastpath))
91 return 0;
92
93 if (interruptible)
94 return mutex_lock_interruptible(&man->io_reserve_mutex);
95
96 mutex_lock(&man->io_reserve_mutex);
97 return 0;
98}
Dave Airlieafe68042013-01-22 13:56:04 +100099EXPORT_SYMBOL(ttm_mem_io_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100100
101void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
102{
103 if (likely(man->io_reserve_fastpath))
104 return;
105
106 mutex_unlock(&man->io_reserve_mutex);
107}
Dave Airlieafe68042013-01-22 13:56:04 +1000108EXPORT_SYMBOL(ttm_mem_io_unlock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100109
110static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
111{
112 struct ttm_buffer_object *bo;
113
114 if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
115 return -EAGAIN;
116
117 bo = list_first_entry(&man->io_reserve_lru,
118 struct ttm_buffer_object,
119 io_reserve_lru);
120 list_del_init(&bo->io_reserve_lru);
121 ttm_bo_unmap_virtual_locked(bo);
122
123 return 0;
124}
125
Dave Airlieafe68042013-01-22 13:56:04 +1000126
127int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
128 struct ttm_mem_reg *mem)
Thomas Hellstromeba67092010-11-11 09:41:57 +0100129{
130 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
131 int ret = 0;
132
133 if (!bdev->driver->io_mem_reserve)
134 return 0;
135 if (likely(man->io_reserve_fastpath))
136 return bdev->driver->io_mem_reserve(bdev, mem);
137
138 if (bdev->driver->io_mem_reserve &&
139 mem->bus.io_reserved_count++ == 0) {
140retry:
141 ret = bdev->driver->io_mem_reserve(bdev, mem);
142 if (ret == -EAGAIN) {
143 ret = ttm_mem_io_evict(man);
144 if (ret == 0)
145 goto retry;
146 }
147 }
148 return ret;
149}
Dave Airlieafe68042013-01-22 13:56:04 +1000150EXPORT_SYMBOL(ttm_mem_io_reserve);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100151
Dave Airlieafe68042013-01-22 13:56:04 +1000152void ttm_mem_io_free(struct ttm_bo_device *bdev,
153 struct ttm_mem_reg *mem)
Thomas Hellstromeba67092010-11-11 09:41:57 +0100154{
155 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
156
157 if (likely(man->io_reserve_fastpath))
158 return;
159
160 if (bdev->driver->io_mem_reserve &&
161 --mem->bus.io_reserved_count == 0 &&
162 bdev->driver->io_mem_free)
163 bdev->driver->io_mem_free(bdev, mem);
164
165}
Dave Airlieafe68042013-01-22 13:56:04 +1000166EXPORT_SYMBOL(ttm_mem_io_free);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100167
168int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
169{
170 struct ttm_mem_reg *mem = &bo->mem;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200171 int ret;
172
Thomas Hellstromeba67092010-11-11 09:41:57 +0100173 if (!mem->bus.io_reserved_vm) {
174 struct ttm_mem_type_manager *man =
175 &bo->bdev->man[mem->mem_type];
176
177 ret = ttm_mem_io_reserve(bo->bdev, mem);
Jerome Glisse82c5da62010-04-09 14:39:23 +0200178 if (unlikely(ret != 0))
179 return ret;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100180 mem->bus.io_reserved_vm = true;
181 if (man->use_io_reserve_lru)
182 list_add_tail(&bo->io_reserve_lru,
183 &man->io_reserve_lru);
Jerome Glisse82c5da62010-04-09 14:39:23 +0200184 }
185 return 0;
186}
187
Thomas Hellstromeba67092010-11-11 09:41:57 +0100188void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
Jerome Glisse82c5da62010-04-09 14:39:23 +0200189{
Thomas Hellstromeba67092010-11-11 09:41:57 +0100190 struct ttm_mem_reg *mem = &bo->mem;
191
192 if (mem->bus.io_reserved_vm) {
193 mem->bus.io_reserved_vm = false;
194 list_del_init(&bo->io_reserve_lru);
195 ttm_mem_io_free(bo->bdev, mem);
Jerome Glisse82c5da62010-04-09 14:39:23 +0200196 }
197}
198
Rashika Kheriadcbff152014-01-06 22:14:27 +0530199static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200200 void **virtual)
201{
Thomas Hellstromeba67092010-11-11 09:41:57 +0100202 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200203 int ret;
204 void *addr;
205
206 *virtual = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100207 (void) ttm_mem_io_lock(man, false);
Jerome Glisse82c5da62010-04-09 14:39:23 +0200208 ret = ttm_mem_io_reserve(bdev, mem);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100209 ttm_mem_io_unlock(man);
Jerome Glisse9e51159c2010-05-05 11:02:44 +0200210 if (ret || !mem->bus.is_iomem)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200211 return ret;
212
Jerome Glisse82c5da62010-04-09 14:39:23 +0200213 if (mem->bus.addr) {
214 addr = mem->bus.addr;
215 } else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200216 if (mem->placement & TTM_PL_FLAG_WC)
Jerome Glisse82c5da62010-04-09 14:39:23 +0200217 addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200218 else
Jerome Glisse82c5da62010-04-09 14:39:23 +0200219 addr = ioremap_nocache(mem->bus.base + mem->bus.offset, mem->bus.size);
220 if (!addr) {
Thomas Hellstromeba67092010-11-11 09:41:57 +0100221 (void) ttm_mem_io_lock(man, false);
Jerome Glisse82c5da62010-04-09 14:39:23 +0200222 ttm_mem_io_free(bdev, mem);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100223 ttm_mem_io_unlock(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200224 return -ENOMEM;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200225 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200226 }
227 *virtual = addr;
228 return 0;
229}
230
Rashika Kheriadcbff152014-01-06 22:14:27 +0530231static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200232 void *virtual)
233{
234 struct ttm_mem_type_manager *man;
235
236 man = &bdev->man[mem->mem_type];
237
Jerome Glisse0c321c72010-04-07 10:21:27 +0000238 if (virtual && mem->bus.addr == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200239 iounmap(virtual);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100240 (void) ttm_mem_io_lock(man, false);
Jerome Glisse82c5da62010-04-09 14:39:23 +0200241 ttm_mem_io_free(bdev, mem);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100242 ttm_mem_io_unlock(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200243}
244
245static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
246{
247 uint32_t *dstP =
248 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
249 uint32_t *srcP =
250 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
251
252 int i;
253 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
254 iowrite32(ioread32(srcP++), dstP++);
255 return 0;
256}
257
258static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200259 unsigned long page,
260 pgprot_t prot)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200261{
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400262 struct page *d = ttm->pages[page];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200263 void *dst;
264
265 if (!d)
266 return -ENOMEM;
267
268 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200269
270#ifdef CONFIG_X86
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700271 dst = kmap_atomic_prot(d, prot);
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200272#else
Thomas Hellstrom6d0897b2009-07-31 10:47:51 +0200273 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200274 dst = vmap(&d, 1, 0, prot);
275 else
276 dst = kmap(d);
277#endif
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200278 if (!dst)
279 return -ENOMEM;
280
281 memcpy_fromio(dst, src, PAGE_SIZE);
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200282
283#ifdef CONFIG_X86
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700284 kunmap_atomic(dst);
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200285#else
Thomas Hellstrom6d0897b2009-07-31 10:47:51 +0200286 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200287 vunmap(dst);
288 else
289 kunmap(d);
290#endif
291
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292 return 0;
293}
294
295static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200296 unsigned long page,
297 pgprot_t prot)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298{
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400299 struct page *s = ttm->pages[page];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200300 void *src;
301
302 if (!s)
303 return -ENOMEM;
304
305 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200306#ifdef CONFIG_X86
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700307 src = kmap_atomic_prot(s, prot);
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200308#else
Thomas Hellstrom6d0897b2009-07-31 10:47:51 +0200309 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200310 src = vmap(&s, 1, 0, prot);
311 else
312 src = kmap(s);
313#endif
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200314 if (!src)
315 return -ENOMEM;
316
317 memcpy_toio(dst, src, PAGE_SIZE);
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200318
319#ifdef CONFIG_X86
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700320 kunmap_atomic(src);
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200321#else
Thomas Hellstrom6d0897b2009-07-31 10:47:51 +0200322 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200323 vunmap(src);
324 else
325 kunmap(s);
326#endif
327
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200328 return 0;
329}
330
331int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
Christian König77dfc282016-06-06 10:17:54 +0200332 bool evict, bool interruptible,
333 bool no_wait_gpu,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000334 struct ttm_mem_reg *new_mem)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200335{
336 struct ttm_bo_device *bdev = bo->bdev;
337 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
338 struct ttm_tt *ttm = bo->ttm;
339 struct ttm_mem_reg *old_mem = &bo->mem;
Thomas Hellstrome22469c2011-10-17 13:27:34 +0200340 struct ttm_mem_reg old_copy = *old_mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200341 void *old_iomap;
342 void *new_iomap;
343 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344 unsigned long i;
345 unsigned long page;
346 unsigned long add = 0;
347 int dir;
348
Christian König77dfc282016-06-06 10:17:54 +0200349 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
350 if (ret)
351 return ret;
352
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200353 ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
354 if (ret)
355 return ret;
356 ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
357 if (ret)
358 goto out;
359
Thomas Hellstromda95c782013-10-30 03:29:50 -0700360 /*
361 * Single TTM move. NOP.
362 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200363 if (old_iomap == NULL && new_iomap == NULL)
364 goto out2;
Thomas Hellstromda95c782013-10-30 03:29:50 -0700365
366 /*
Thomas Hellstrom0bc25422013-11-17 23:30:38 -0800367 * Don't move nonexistent data. Clear destination instead.
Thomas Hellstromda95c782013-10-30 03:29:50 -0700368 */
Thomas Hellstrom0bc25422013-11-17 23:30:38 -0800369 if (old_iomap == NULL &&
Thomas Hellstrom2e6d8b42013-12-21 22:23:02 +0100370 (ttm == NULL || (ttm->state == tt_unpopulated &&
371 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
Thomas Hellstrom0bc25422013-11-17 23:30:38 -0800372 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200373 goto out2;
Thomas Hellstrom0bc25422013-11-17 23:30:38 -0800374 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200375
Thomas Hellstromda95c782013-10-30 03:29:50 -0700376 /*
377 * TTM might be null for moves within the same region.
Jakob Bornecrantz9a0599d2013-10-30 02:46:56 -0700378 */
379 if (ttm && ttm->state == tt_unpopulated) {
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400380 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
Thomas Hellstromda95c782013-10-30 03:29:50 -0700381 if (ret)
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400382 goto out1;
383 }
384
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200385 add = 0;
386 dir = 1;
387
388 if ((old_mem->mem_type == new_mem->mem_type) &&
Ben Skeggsd961db72010-08-05 10:48:18 +1000389 (new_mem->start < old_mem->start + old_mem->size)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390 dir = -1;
391 add = new_mem->num_pages - 1;
392 }
393
394 for (i = 0; i < new_mem->num_pages; ++i) {
395 page = i * dir + add;
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200396 if (old_iomap == NULL) {
397 pgprot_t prot = ttm_io_prot(old_mem->placement,
398 PAGE_KERNEL);
399 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
400 prot);
401 } else if (new_iomap == NULL) {
402 pgprot_t prot = ttm_io_prot(new_mem->placement,
403 PAGE_KERNEL);
404 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
405 prot);
406 } else
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200407 ret = ttm_copy_io_page(new_iomap, old_iomap, page);
Thomas Hellstromda95c782013-10-30 03:29:50 -0700408 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200409 goto out1;
410 }
411 mb();
412out2:
Thomas Hellstromeba67092010-11-11 09:41:57 +0100413 old_copy = *old_mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200414 *old_mem = *new_mem;
415 new_mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200416
Christian König4279cb12016-06-06 10:17:51 +0200417 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200418 ttm_tt_destroy(ttm);
419 bo->ttm = NULL;
420 }
421
422out1:
Thomas Hellstromeba67092010-11-11 09:41:57 +0100423 ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200424out:
425 ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
Thomas Hellstromda95c782013-10-30 03:29:50 -0700426
427 /*
428 * On error, keep the mm node!
429 */
430 if (!ret)
431 ttm_bo_mem_put(bo, &old_copy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200432 return ret;
433}
434EXPORT_SYMBOL(ttm_bo_move_memcpy);
435
436static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
437{
438 kfree(bo);
439}
440
441/**
442 * ttm_buffer_object_transfer
443 *
444 * @bo: A pointer to a struct ttm_buffer_object.
445 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
446 * holding the data of @bo with the old placement.
447 *
448 * This is a utility function that may be called after an accelerated move
449 * has been scheduled. A new buffer object is created as a placeholder for
450 * the old data while it's being copied. When that buffer object is idle,
451 * it can be destroyed, releasing the space of the old placement.
452 * Returns:
453 * !0: Failure.
454 */
455
456static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
457 struct ttm_buffer_object **new_obj)
458{
459 struct ttm_buffer_object *fbo;
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200460 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200461
Daniel Vetterff7c60c2013-01-14 15:08:14 +0100462 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200463 if (!fbo)
464 return -ENOMEM;
465
466 *fbo = *bo;
467
468 /**
469 * Fix up members that we shouldn't copy directly:
470 * TODO: Explicit member copy would probably be better here.
471 */
472
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200473 INIT_LIST_HEAD(&fbo->ddestroy);
474 INIT_LIST_HEAD(&fbo->lru);
475 INIT_LIST_HEAD(&fbo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100476 INIT_LIST_HEAD(&fbo->io_reserve_lru);
Christian König5bc73062016-06-15 13:44:01 +0200477 fbo->moving = NULL;
David Herrmann72525b32013-07-24 21:08:53 +0200478 drm_vma_node_reset(&fbo->vma_node);
Francisco Jerez0fbecd42010-09-21 02:15:15 +0200479 atomic_set(&fbo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200480
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200481 kref_init(&fbo->list_kref);
482 kref_init(&fbo->kref);
483 fbo->destroy = &ttm_transfered_destroy;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500484 fbo->acc_size = 0;
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200485 fbo->resv = &fbo->ttm_resv;
486 reservation_object_init(fbo->resv);
487 ret = ww_mutex_trylock(&fbo->resv->lock);
488 WARN_ON(!ret);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200489
490 *new_obj = fbo;
491 return 0;
492}
493
494pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
495{
Benjamin Herrenschmidt94318d52014-09-04 17:47:23 +1000496 /* Cached mappings need no adjustment */
497 if (caching_flags & TTM_PL_FLAG_CACHED)
498 return tmp;
499
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200500#if defined(__i386__) || defined(__x86_64__)
501 if (caching_flags & TTM_PL_FLAG_WC)
502 tmp = pgprot_writecombine(tmp);
503 else if (boot_cpu_data.x86 > 3)
504 tmp = pgprot_noncached(tmp);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200505#endif
Alexandre Courbotf135b972015-07-01 17:32:29 +0900506#if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
507 defined(__powerpc__)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200508 if (caching_flags & TTM_PL_FLAG_WC)
509 tmp = pgprot_writecombine(tmp);
510 else
511 tmp = pgprot_noncached(tmp);
512#endif
Huacai Chen04cf55e2012-08-11 09:32:17 +0000513#if defined(__sparc__) || defined(__mips__)
Benjamin Herrenschmidt94318d52014-09-04 17:47:23 +1000514 tmp = pgprot_noncached(tmp);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200515#endif
516 return tmp;
517}
Thomas Hellstrom4bfd75c2009-12-06 21:46:27 +0100518EXPORT_SYMBOL(ttm_io_prot);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200519
520static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
Jerome Glisse82c5da62010-04-09 14:39:23 +0200521 unsigned long offset,
522 unsigned long size,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200523 struct ttm_bo_kmap_obj *map)
524{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200525 struct ttm_mem_reg *mem = &bo->mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200526
Jerome Glisse82c5da62010-04-09 14:39:23 +0200527 if (bo->mem.bus.addr) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200528 map->bo_kmap_type = ttm_bo_map_premapped;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200529 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200530 } else {
531 map->bo_kmap_type = ttm_bo_map_iomap;
532 if (mem->placement & TTM_PL_FLAG_WC)
Jerome Glisse82c5da62010-04-09 14:39:23 +0200533 map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
534 size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200535 else
Jerome Glisse82c5da62010-04-09 14:39:23 +0200536 map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
537 size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200538 }
539 return (!map->virtual) ? -ENOMEM : 0;
540}
541
542static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
543 unsigned long start_page,
544 unsigned long num_pages,
545 struct ttm_bo_kmap_obj *map)
546{
547 struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
548 struct ttm_tt *ttm = bo->ttm;
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400549 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200550
551 BUG_ON(!ttm);
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400552
553 if (ttm->state == tt_unpopulated) {
554 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
555 if (ret)
556 return ret;
557 }
558
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200559 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
560 /*
561 * We're mapping a single page, and the desired
562 * page protection is consistent with the bo.
563 */
564
565 map->bo_kmap_type = ttm_bo_map_kmap;
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400566 map->page = ttm->pages[start_page];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200567 map->virtual = kmap(map->page);
568 } else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200569 /*
570 * We need to use vmap to get the desired page protection
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200571 * or to make the buffer object look contiguous.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200572 */
Benjamin Herrenschmidt94318d52014-09-04 17:47:23 +1000573 prot = ttm_io_prot(mem->placement, PAGE_KERNEL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200574 map->bo_kmap_type = ttm_bo_map_vmap;
575 map->virtual = vmap(ttm->pages + start_page, num_pages,
576 0, prot);
577 }
578 return (!map->virtual) ? -ENOMEM : 0;
579}
580
581int ttm_bo_kmap(struct ttm_buffer_object *bo,
582 unsigned long start_page, unsigned long num_pages,
583 struct ttm_bo_kmap_obj *map)
584{
Thomas Hellstromeba67092010-11-11 09:41:57 +0100585 struct ttm_mem_type_manager *man =
586 &bo->bdev->man[bo->mem.mem_type];
Jerome Glisse82c5da62010-04-09 14:39:23 +0200587 unsigned long offset, size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200588 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200589
590 BUG_ON(!list_empty(&bo->swap));
591 map->virtual = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200592 map->bo = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200593 if (num_pages > bo->num_pages)
594 return -EINVAL;
595 if (start_page > bo->num_pages)
596 return -EINVAL;
597#if 0
Daniel Vetter4cda8782013-12-11 11:34:46 +0100598 if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200599 return -EPERM;
600#endif
Thomas Hellstromeba67092010-11-11 09:41:57 +0100601 (void) ttm_mem_io_lock(man, false);
Jerome Glisse82c5da62010-04-09 14:39:23 +0200602 ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100603 ttm_mem_io_unlock(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200604 if (ret)
605 return ret;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200606 if (!bo->mem.bus.is_iomem) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200607 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
608 } else {
Jerome Glisse82c5da62010-04-09 14:39:23 +0200609 offset = start_page << PAGE_SHIFT;
610 size = num_pages << PAGE_SHIFT;
611 return ttm_bo_ioremap(bo, offset, size, map);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200612 }
613}
614EXPORT_SYMBOL(ttm_bo_kmap);
615
616void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
617{
Thomas Hellstromeba67092010-11-11 09:41:57 +0100618 struct ttm_buffer_object *bo = map->bo;
619 struct ttm_mem_type_manager *man =
620 &bo->bdev->man[bo->mem.mem_type];
621
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200622 if (!map->virtual)
623 return;
624 switch (map->bo_kmap_type) {
625 case ttm_bo_map_iomap:
626 iounmap(map->virtual);
627 break;
628 case ttm_bo_map_vmap:
629 vunmap(map->virtual);
630 break;
631 case ttm_bo_map_kmap:
632 kunmap(map->page);
633 break;
634 case ttm_bo_map_premapped:
635 break;
636 default:
637 BUG();
638 }
Thomas Hellstromeba67092010-11-11 09:41:57 +0100639 (void) ttm_mem_io_lock(man, false);
640 ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
641 ttm_mem_io_unlock(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200642 map->virtual = NULL;
643 map->page = NULL;
644}
645EXPORT_SYMBOL(ttm_bo_kunmap);
646
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200647int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200648 struct fence *fence,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000649 bool evict,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200650 struct ttm_mem_reg *new_mem)
651{
652 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200653 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
654 struct ttm_mem_reg *old_mem = &bo->mem;
655 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656 struct ttm_buffer_object *ghost_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200658 reservation_object_add_excl_fence(bo->resv, fence);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200659 if (evict) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200660 ret = ttm_bo_wait(bo, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200661 if (ret)
662 return ret;
663
Christian König4279cb12016-06-06 10:17:51 +0200664 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200665 ttm_tt_destroy(bo->ttm);
666 bo->ttm = NULL;
667 }
Ben Skeggseac20952011-08-22 03:15:04 +0000668 ttm_bo_free_old_node(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200669 } else {
670 /**
671 * This should help pipeline ordinary buffer moves.
672 *
673 * Hang old buffer memory on a new buffer object,
674 * and leave it to be released when the GPU
675 * operation has completed.
676 */
677
Christian König5bc73062016-06-15 13:44:01 +0200678 fence_put(bo->moving);
679 bo->moving = fence_get(fence);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200680
Daniel Vetterff7c60c2013-01-14 15:08:14 +0100681 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200682 if (ret)
683 return ret;
684
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200685 reservation_object_add_excl_fence(ghost_obj->resv, fence);
686
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200687 /**
688 * If we're not moving to fixed memory, the TTM object
689 * needs to stay alive. Otherwhise hang it on the ghost
690 * bo to be unbound and destroyed.
691 */
692
693 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
694 ghost_obj->ttm = NULL;
695 else
696 bo->ttm = NULL;
697
698 ttm_bo_unreserve(ghost_obj);
699 ttm_bo_unref(&ghost_obj);
700 }
701
702 *old_mem = *new_mem;
703 new_mem->mm_node = NULL;
Austin Yuan110b20c2010-01-21 13:45:40 +0800704
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200705 return 0;
706}
707EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
Christian König3ddf4ad2016-06-15 13:44:03 +0200708
709int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
710 struct fence *fence, bool evict,
711 struct ttm_mem_reg *new_mem)
712{
713 struct ttm_bo_device *bdev = bo->bdev;
714 struct ttm_mem_reg *old_mem = &bo->mem;
715
716 struct ttm_mem_type_manager *from = &bdev->man[old_mem->mem_type];
717 struct ttm_mem_type_manager *to = &bdev->man[new_mem->mem_type];
718
719 int ret;
720
721 reservation_object_add_excl_fence(bo->resv, fence);
722
723 if (!evict) {
724 struct ttm_buffer_object *ghost_obj;
725
726 /**
727 * This should help pipeline ordinary buffer moves.
728 *
729 * Hang old buffer memory on a new buffer object,
730 * and leave it to be released when the GPU
731 * operation has completed.
732 */
733
734 fence_put(bo->moving);
735 bo->moving = fence_get(fence);
736
737 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
738 if (ret)
739 return ret;
740
741 reservation_object_add_excl_fence(ghost_obj->resv, fence);
742
743 /**
744 * If we're not moving to fixed memory, the TTM object
745 * needs to stay alive. Otherwhise hang it on the ghost
746 * bo to be unbound and destroyed.
747 */
748
749 if (!(to->flags & TTM_MEMTYPE_FLAG_FIXED))
750 ghost_obj->ttm = NULL;
751 else
752 bo->ttm = NULL;
753
754 ttm_bo_unreserve(ghost_obj);
755 ttm_bo_unref(&ghost_obj);
756
757 } else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) {
758
759 /**
760 * BO doesn't have a TTM we need to bind/unbind. Just remember
761 * this eviction and free up the allocation
762 */
763
764 spin_lock(&from->move_lock);
Christian König847b6cf2016-06-24 21:51:03 +0200765 if (!from->move || fence_is_later(fence, from->move)) {
Christian König3ddf4ad2016-06-15 13:44:03 +0200766 fence_put(from->move);
767 from->move = fence_get(fence);
768 }
769 spin_unlock(&from->move_lock);
770
771 ttm_bo_free_old_node(bo);
772
773 fence_put(bo->moving);
774 bo->moving = fence_get(fence);
775
776 } else {
777 /**
778 * Last resort, wait for the move to be completed.
779 *
780 * Should never happen in pratice.
781 */
782
783 ret = ttm_bo_wait(bo, false, false);
784 if (ret)
785 return ret;
786
787 if (to->flags & TTM_MEMTYPE_FLAG_FIXED) {
788 ttm_tt_destroy(bo->ttm);
789 bo->ttm = NULL;
790 }
791 ttm_bo_free_old_node(bo);
792 }
793
794 *old_mem = *new_mem;
795 new_mem->mm_node = NULL;
796
797 return 0;
798}
799EXPORT_SYMBOL(ttm_bo_pipeline_move);