blob: e7a519f1849b7b72d0d21fd3af44c753a5dfb81c [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änzer4e2f0ca2016-08-08 12:28:25 +090048 bool interruptible, bool no_wait_gpu,
49 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änzer7b8082b2016-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,
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900332 bool interruptible, bool no_wait_gpu,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000333 struct ttm_mem_reg *new_mem)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200334{
335 struct ttm_bo_device *bdev = bo->bdev;
336 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
337 struct ttm_tt *ttm = bo->ttm;
338 struct ttm_mem_reg *old_mem = &bo->mem;
Thomas Hellstrome22469c2011-10-17 13:27:34 +0200339 struct ttm_mem_reg old_copy = *old_mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200340 void *old_iomap;
341 void *new_iomap;
342 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343 unsigned long i;
344 unsigned long page;
345 unsigned long add = 0;
346 int dir;
347
Christian König77dfc282016-06-06 10:17:54 +0200348 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
349 if (ret)
350 return ret;
351
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200352 ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
353 if (ret)
354 return ret;
355 ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
356 if (ret)
357 goto out;
358
Thomas Hellstromda95c782013-10-30 03:29:50 -0700359 /*
360 * Single TTM move. NOP.
361 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200362 if (old_iomap == NULL && new_iomap == NULL)
363 goto out2;
Thomas Hellstromda95c782013-10-30 03:29:50 -0700364
365 /*
Thomas Hellstrom0bc25422013-11-17 23:30:38 -0800366 * Don't move nonexistent data. Clear destination instead.
Thomas Hellstromda95c782013-10-30 03:29:50 -0700367 */
Thomas Hellstrom0bc25422013-11-17 23:30:38 -0800368 if (old_iomap == NULL &&
Thomas Hellstrom2e6d8b42013-12-21 22:23:02 +0100369 (ttm == NULL || (ttm->state == tt_unpopulated &&
370 !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
Thomas Hellstrom0bc25422013-11-17 23:30:38 -0800371 memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200372 goto out2;
Thomas Hellstrom0bc25422013-11-17 23:30:38 -0800373 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374
Thomas Hellstromda95c782013-10-30 03:29:50 -0700375 /*
376 * TTM might be null for moves within the same region.
Jakob Bornecrantz9a0599d2013-10-30 02:46:56 -0700377 */
378 if (ttm && ttm->state == tt_unpopulated) {
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400379 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
Thomas Hellstromda95c782013-10-30 03:29:50 -0700380 if (ret)
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400381 goto out1;
382 }
383
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200384 add = 0;
385 dir = 1;
386
387 if ((old_mem->mem_type == new_mem->mem_type) &&
Ben Skeggsd961db72010-08-05 10:48:18 +1000388 (new_mem->start < old_mem->start + old_mem->size)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200389 dir = -1;
390 add = new_mem->num_pages - 1;
391 }
392
393 for (i = 0; i < new_mem->num_pages; ++i) {
394 page = i * dir + add;
Thomas Hellstrom542c6f62009-07-24 09:57:34 +0200395 if (old_iomap == NULL) {
396 pgprot_t prot = ttm_io_prot(old_mem->placement,
397 PAGE_KERNEL);
398 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
399 prot);
400 } else if (new_iomap == NULL) {
401 pgprot_t prot = ttm_io_prot(new_mem->placement,
402 PAGE_KERNEL);
403 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
404 prot);
405 } else
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200406 ret = ttm_copy_io_page(new_iomap, old_iomap, page);
Thomas Hellstromda95c782013-10-30 03:29:50 -0700407 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200408 goto out1;
409 }
410 mb();
411out2:
Thomas Hellstromeba67092010-11-11 09:41:57 +0100412 old_copy = *old_mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 *old_mem = *new_mem;
414 new_mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200415
Christian König4279cb12016-06-06 10:17:51 +0200416 if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200417 ttm_tt_destroy(ttm);
418 bo->ttm = NULL;
419 }
420
421out1:
Thomas Hellstromeba67092010-11-11 09:41:57 +0100422 ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200423out:
424 ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
Thomas Hellstromda95c782013-10-30 03:29:50 -0700425
426 /*
427 * On error, keep the mm node!
428 */
429 if (!ret)
430 ttm_bo_mem_put(bo, &old_copy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200431 return ret;
432}
433EXPORT_SYMBOL(ttm_bo_move_memcpy);
434
435static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
436{
437 kfree(bo);
438}
439
440/**
441 * ttm_buffer_object_transfer
442 *
443 * @bo: A pointer to a struct ttm_buffer_object.
444 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
445 * holding the data of @bo with the old placement.
446 *
447 * This is a utility function that may be called after an accelerated move
448 * has been scheduled. A new buffer object is created as a placeholder for
449 * the old data while it's being copied. When that buffer object is idle,
450 * it can be destroyed, releasing the space of the old placement.
451 * Returns:
452 * !0: Failure.
453 */
454
455static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
456 struct ttm_buffer_object **new_obj)
457{
458 struct ttm_buffer_object *fbo;
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200459 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200460
Daniel Vetterff7c60c2013-01-14 15:08:14 +0100461 fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200462 if (!fbo)
463 return -ENOMEM;
464
465 *fbo = *bo;
466
467 /**
468 * Fix up members that we shouldn't copy directly:
469 * TODO: Explicit member copy would probably be better here.
470 */
471
Monk Liu7e96a132017-08-11 13:50:51 +0800472 atomic_inc(&bo->glob->bo_count);
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önig4d98e5e2017-10-30 14:57:43 +0100477 mutex_init(&fbo->wu_mutex);
Christian König5bc73062016-06-15 13:44:01 +0200478 fbo->moving = NULL;
David Herrmann72525b32013-07-24 21:08:53 +0200479 drm_vma_node_reset(&fbo->vma_node);
Francisco Jerez0fbecd42010-09-21 02:15:15 +0200480 atomic_set(&fbo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200481
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200482 kref_init(&fbo->list_kref);
483 kref_init(&fbo->kref);
484 fbo->destroy = &ttm_transfered_destroy;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500485 fbo->acc_size = 0;
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200486 fbo->resv = &fbo->ttm_resv;
487 reservation_object_init(fbo->resv);
488 ret = ww_mutex_trylock(&fbo->resv->lock);
489 WARN_ON(!ret);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200490
491 *new_obj = fbo;
492 return 0;
493}
494
495pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
496{
Benjamin Herrenschmidt94318d52014-09-04 17:47:23 +1000497 /* Cached mappings need no adjustment */
498 if (caching_flags & TTM_PL_FLAG_CACHED)
499 return tmp;
500
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200501#if defined(__i386__) || defined(__x86_64__)
502 if (caching_flags & TTM_PL_FLAG_WC)
503 tmp = pgprot_writecombine(tmp);
504 else if (boot_cpu_data.x86 > 3)
505 tmp = pgprot_noncached(tmp);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200506#endif
Alexandre Courbotf135b972015-07-01 17:32:29 +0900507#if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
508 defined(__powerpc__)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200509 if (caching_flags & TTM_PL_FLAG_WC)
510 tmp = pgprot_writecombine(tmp);
511 else
512 tmp = pgprot_noncached(tmp);
513#endif
Huacai Chen04cf55e2012-08-11 09:32:17 +0000514#if defined(__sparc__) || defined(__mips__)
Benjamin Herrenschmidt94318d52014-09-04 17:47:23 +1000515 tmp = pgprot_noncached(tmp);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200516#endif
517 return tmp;
518}
Thomas Hellstrom4bfd75c2009-12-06 21:46:27 +0100519EXPORT_SYMBOL(ttm_io_prot);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200520
521static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
Jerome Glisse82c5da62010-04-09 14:39:23 +0200522 unsigned long offset,
523 unsigned long size,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200524 struct ttm_bo_kmap_obj *map)
525{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200526 struct ttm_mem_reg *mem = &bo->mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200527
Jerome Glisse82c5da62010-04-09 14:39:23 +0200528 if (bo->mem.bus.addr) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200529 map->bo_kmap_type = ttm_bo_map_premapped;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200530 map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200531 } else {
532 map->bo_kmap_type = ttm_bo_map_iomap;
533 if (mem->placement & TTM_PL_FLAG_WC)
Jerome Glisse82c5da62010-04-09 14:39:23 +0200534 map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
535 size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200536 else
Jerome Glisse82c5da62010-04-09 14:39:23 +0200537 map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
538 size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200539 }
540 return (!map->virtual) ? -ENOMEM : 0;
541}
542
543static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
544 unsigned long start_page,
545 unsigned long num_pages,
546 struct ttm_bo_kmap_obj *map)
547{
548 struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
549 struct ttm_tt *ttm = bo->ttm;
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400550 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200551
552 BUG_ON(!ttm);
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400553
554 if (ttm->state == tt_unpopulated) {
555 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
556 if (ret)
557 return ret;
558 }
559
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200560 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
561 /*
562 * We're mapping a single page, and the desired
563 * page protection is consistent with the bo.
564 */
565
566 map->bo_kmap_type = ttm_bo_map_kmap;
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400567 map->page = ttm->pages[start_page];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200568 map->virtual = kmap(map->page);
569 } else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200570 /*
571 * We need to use vmap to get the desired page protection
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200572 * or to make the buffer object look contiguous.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200573 */
Benjamin Herrenschmidt94318d52014-09-04 17:47:23 +1000574 prot = ttm_io_prot(mem->placement, PAGE_KERNEL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200575 map->bo_kmap_type = ttm_bo_map_vmap;
576 map->virtual = vmap(ttm->pages + start_page, num_pages,
577 0, prot);
578 }
579 return (!map->virtual) ? -ENOMEM : 0;
580}
581
582int ttm_bo_kmap(struct ttm_buffer_object *bo,
583 unsigned long start_page, unsigned long num_pages,
584 struct ttm_bo_kmap_obj *map)
585{
Thomas Hellstromeba67092010-11-11 09:41:57 +0100586 struct ttm_mem_type_manager *man =
587 &bo->bdev->man[bo->mem.mem_type];
Jerome Glisse82c5da62010-04-09 14:39:23 +0200588 unsigned long offset, size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200589 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200590
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200591 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,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100648 struct dma_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
Chris Wilsonf54d1862016-10-25 13:00:45 +0100678 dma_fence_put(bo->moving);
679 bo->moving = dma_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,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100710 struct dma_fence *fence, bool evict,
Christian König3ddf4ad2016-06-15 13:44:03 +0200711 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
Chris Wilsonf54d1862016-10-25 13:00:45 +0100734 dma_fence_put(bo->moving);
735 bo->moving = dma_fence_get(fence);
Christian König3ddf4ad2016-06-15 13:44:03 +0200736
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);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100765 if (!from->move || dma_fence_is_later(fence, from->move)) {
766 dma_fence_put(from->move);
767 from->move = dma_fence_get(fence);
Christian König3ddf4ad2016-06-15 13:44:03 +0200768 }
769 spin_unlock(&from->move_lock);
770
771 ttm_bo_free_old_node(bo);
772
Chris Wilsonf54d1862016-10-25 13:00:45 +0100773 dma_fence_put(bo->moving);
774 bo->moving = dma_fence_get(fence);
Christian König3ddf4ad2016-06-15 13:44:03 +0200775
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);