xref: /freebsd/sys/dev/drm2/ttm/ttm_bo_util.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1592ffb21SWarner Losh /**************************************************************************
2592ffb21SWarner Losh  *
3592ffb21SWarner Losh  * Copyright (c) 2007-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_bo_driver.h>
34592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_placement.h>
35592ffb21SWarner Losh #include <sys/sf_buf.h>
36592ffb21SWarner Losh 
ttm_bo_free_old_node(struct ttm_buffer_object * bo)37592ffb21SWarner Losh void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
38592ffb21SWarner Losh {
39592ffb21SWarner Losh 	ttm_bo_mem_put(bo, &bo->mem);
40592ffb21SWarner Losh }
41592ffb21SWarner Losh 
ttm_bo_move_ttm(struct ttm_buffer_object * bo,bool evict,bool no_wait_gpu,struct ttm_mem_reg * new_mem)42592ffb21SWarner Losh int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
43592ffb21SWarner Losh 		    bool evict,
44592ffb21SWarner Losh 		    bool no_wait_gpu, struct ttm_mem_reg *new_mem)
45592ffb21SWarner Losh {
46592ffb21SWarner Losh 	struct ttm_tt *ttm = bo->ttm;
47592ffb21SWarner Losh 	struct ttm_mem_reg *old_mem = &bo->mem;
48592ffb21SWarner Losh 	int ret;
49592ffb21SWarner Losh 
50592ffb21SWarner Losh 	if (old_mem->mem_type != TTM_PL_SYSTEM) {
51592ffb21SWarner Losh 		ttm_tt_unbind(ttm);
52592ffb21SWarner Losh 		ttm_bo_free_old_node(bo);
53592ffb21SWarner Losh 		ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
54592ffb21SWarner Losh 				TTM_PL_MASK_MEM);
55592ffb21SWarner Losh 		old_mem->mem_type = TTM_PL_SYSTEM;
56592ffb21SWarner Losh 	}
57592ffb21SWarner Losh 
58592ffb21SWarner Losh 	ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
59592ffb21SWarner Losh 	if (unlikely(ret != 0))
60592ffb21SWarner Losh 		return ret;
61592ffb21SWarner Losh 
62592ffb21SWarner Losh 	if (new_mem->mem_type != TTM_PL_SYSTEM) {
63592ffb21SWarner Losh 		ret = ttm_tt_bind(ttm, new_mem);
64592ffb21SWarner Losh 		if (unlikely(ret != 0))
65592ffb21SWarner Losh 			return ret;
66592ffb21SWarner Losh 	}
67592ffb21SWarner Losh 
68592ffb21SWarner Losh 	*old_mem = *new_mem;
69592ffb21SWarner Losh 	new_mem->mm_node = NULL;
70592ffb21SWarner Losh 
71592ffb21SWarner Losh 	return 0;
72592ffb21SWarner Losh }
73592ffb21SWarner Losh 
ttm_mem_io_lock(struct ttm_mem_type_manager * man,bool interruptible)74592ffb21SWarner Losh int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
75592ffb21SWarner Losh {
76592ffb21SWarner Losh 	if (likely(man->io_reserve_fastpath))
77592ffb21SWarner Losh 		return 0;
78592ffb21SWarner Losh 
79592ffb21SWarner Losh 	if (interruptible) {
80592ffb21SWarner Losh 		if (sx_xlock_sig(&man->io_reserve_mutex))
81592ffb21SWarner Losh 			return (-EINTR);
82592ffb21SWarner Losh 		else
83592ffb21SWarner Losh 			return (0);
84592ffb21SWarner Losh 	}
85592ffb21SWarner Losh 
86592ffb21SWarner Losh 	sx_xlock(&man->io_reserve_mutex);
87592ffb21SWarner Losh 	return 0;
88592ffb21SWarner Losh }
89592ffb21SWarner Losh 
ttm_mem_io_unlock(struct ttm_mem_type_manager * man)90592ffb21SWarner Losh void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
91592ffb21SWarner Losh {
92592ffb21SWarner Losh 	if (likely(man->io_reserve_fastpath))
93592ffb21SWarner Losh 		return;
94592ffb21SWarner Losh 
95592ffb21SWarner Losh 	sx_xunlock(&man->io_reserve_mutex);
96592ffb21SWarner Losh }
97592ffb21SWarner Losh 
ttm_mem_io_evict(struct ttm_mem_type_manager * man)98592ffb21SWarner Losh static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
99592ffb21SWarner Losh {
100592ffb21SWarner Losh 	struct ttm_buffer_object *bo;
101592ffb21SWarner Losh 
102592ffb21SWarner Losh 	if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
103592ffb21SWarner Losh 		return -EAGAIN;
104592ffb21SWarner Losh 
105592ffb21SWarner Losh 	bo = list_first_entry(&man->io_reserve_lru,
106592ffb21SWarner Losh 			      struct ttm_buffer_object,
107592ffb21SWarner Losh 			      io_reserve_lru);
108592ffb21SWarner Losh 	list_del_init(&bo->io_reserve_lru);
109592ffb21SWarner Losh 	ttm_bo_unmap_virtual_locked(bo);
110592ffb21SWarner Losh 
111592ffb21SWarner Losh 	return 0;
112592ffb21SWarner Losh }
113592ffb21SWarner Losh 
ttm_mem_io_reserve(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem)114592ffb21SWarner Losh static int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
115592ffb21SWarner Losh 			      struct ttm_mem_reg *mem)
116592ffb21SWarner Losh {
117592ffb21SWarner Losh 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
118592ffb21SWarner Losh 	int ret = 0;
119592ffb21SWarner Losh 
120592ffb21SWarner Losh 	if (!bdev->driver->io_mem_reserve)
121592ffb21SWarner Losh 		return 0;
122592ffb21SWarner Losh 	if (likely(man->io_reserve_fastpath))
123592ffb21SWarner Losh 		return bdev->driver->io_mem_reserve(bdev, mem);
124592ffb21SWarner Losh 
125592ffb21SWarner Losh 	if (bdev->driver->io_mem_reserve &&
126592ffb21SWarner Losh 	    mem->bus.io_reserved_count++ == 0) {
127592ffb21SWarner Losh retry:
128592ffb21SWarner Losh 		ret = bdev->driver->io_mem_reserve(bdev, mem);
129592ffb21SWarner Losh 		if (ret == -EAGAIN) {
130592ffb21SWarner Losh 			ret = ttm_mem_io_evict(man);
131592ffb21SWarner Losh 			if (ret == 0)
132592ffb21SWarner Losh 				goto retry;
133592ffb21SWarner Losh 		}
134592ffb21SWarner Losh 	}
135592ffb21SWarner Losh 	return ret;
136592ffb21SWarner Losh }
137592ffb21SWarner Losh 
ttm_mem_io_free(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem)138592ffb21SWarner Losh static void ttm_mem_io_free(struct ttm_bo_device *bdev,
139592ffb21SWarner Losh 			    struct ttm_mem_reg *mem)
140592ffb21SWarner Losh {
141592ffb21SWarner Losh 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
142592ffb21SWarner Losh 
143592ffb21SWarner Losh 	if (likely(man->io_reserve_fastpath))
144592ffb21SWarner Losh 		return;
145592ffb21SWarner Losh 
146592ffb21SWarner Losh 	if (bdev->driver->io_mem_reserve &&
147592ffb21SWarner Losh 	    --mem->bus.io_reserved_count == 0 &&
148592ffb21SWarner Losh 	    bdev->driver->io_mem_free)
149592ffb21SWarner Losh 		bdev->driver->io_mem_free(bdev, mem);
150592ffb21SWarner Losh 
151592ffb21SWarner Losh }
152592ffb21SWarner Losh 
ttm_mem_io_reserve_vm(struct ttm_buffer_object * bo)153592ffb21SWarner Losh int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
154592ffb21SWarner Losh {
155592ffb21SWarner Losh 	struct ttm_mem_reg *mem = &bo->mem;
156592ffb21SWarner Losh 	int ret;
157592ffb21SWarner Losh 
158592ffb21SWarner Losh 	if (!mem->bus.io_reserved_vm) {
159592ffb21SWarner Losh 		struct ttm_mem_type_manager *man =
160592ffb21SWarner Losh 			&bo->bdev->man[mem->mem_type];
161592ffb21SWarner Losh 
162592ffb21SWarner Losh 		ret = ttm_mem_io_reserve(bo->bdev, mem);
163592ffb21SWarner Losh 		if (unlikely(ret != 0))
164592ffb21SWarner Losh 			return ret;
165592ffb21SWarner Losh 		mem->bus.io_reserved_vm = true;
166592ffb21SWarner Losh 		if (man->use_io_reserve_lru)
167592ffb21SWarner Losh 			list_add_tail(&bo->io_reserve_lru,
168592ffb21SWarner Losh 				      &man->io_reserve_lru);
169592ffb21SWarner Losh 	}
170592ffb21SWarner Losh 	return 0;
171592ffb21SWarner Losh }
172592ffb21SWarner Losh 
ttm_mem_io_free_vm(struct ttm_buffer_object * bo)173592ffb21SWarner Losh void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
174592ffb21SWarner Losh {
175592ffb21SWarner Losh 	struct ttm_mem_reg *mem = &bo->mem;
176592ffb21SWarner Losh 
177592ffb21SWarner Losh 	if (mem->bus.io_reserved_vm) {
178592ffb21SWarner Losh 		mem->bus.io_reserved_vm = false;
179592ffb21SWarner Losh 		list_del_init(&bo->io_reserve_lru);
180592ffb21SWarner Losh 		ttm_mem_io_free(bo->bdev, mem);
181592ffb21SWarner Losh 	}
182592ffb21SWarner Losh }
183592ffb21SWarner Losh 
184592ffb21SWarner Losh static
ttm_mem_reg_ioremap(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem,void ** virtual)185592ffb21SWarner Losh int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
186592ffb21SWarner Losh 			void **virtual)
187592ffb21SWarner Losh {
188592ffb21SWarner Losh 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
189592ffb21SWarner Losh 	int ret;
190592ffb21SWarner Losh 	void *addr;
191592ffb21SWarner Losh 
192592ffb21SWarner Losh 	*virtual = NULL;
193592ffb21SWarner Losh 	(void) ttm_mem_io_lock(man, false);
194592ffb21SWarner Losh 	ret = ttm_mem_io_reserve(bdev, mem);
195592ffb21SWarner Losh 	ttm_mem_io_unlock(man);
196592ffb21SWarner Losh 	if (ret || !mem->bus.is_iomem)
197592ffb21SWarner Losh 		return ret;
198592ffb21SWarner Losh 
199592ffb21SWarner Losh 	if (mem->bus.addr) {
200592ffb21SWarner Losh 		addr = mem->bus.addr;
201592ffb21SWarner Losh 	} else {
202592ffb21SWarner Losh 		addr = pmap_mapdev_attr(mem->bus.base + mem->bus.offset,
203592ffb21SWarner Losh 		    mem->bus.size, (mem->placement & TTM_PL_FLAG_WC) ?
204592ffb21SWarner Losh 		    VM_MEMATTR_WRITE_COMBINING : VM_MEMATTR_UNCACHEABLE);
205592ffb21SWarner Losh 		if (!addr) {
206592ffb21SWarner Losh 			(void) ttm_mem_io_lock(man, false);
207592ffb21SWarner Losh 			ttm_mem_io_free(bdev, mem);
208592ffb21SWarner Losh 			ttm_mem_io_unlock(man);
209592ffb21SWarner Losh 			return -ENOMEM;
210592ffb21SWarner Losh 		}
211592ffb21SWarner Losh 	}
212592ffb21SWarner Losh 	*virtual = addr;
213592ffb21SWarner Losh 	return 0;
214592ffb21SWarner Losh }
215592ffb21SWarner Losh 
216592ffb21SWarner Losh static
ttm_mem_reg_iounmap(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem,void * virtual)217592ffb21SWarner Losh void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
218592ffb21SWarner Losh 			 void *virtual)
219592ffb21SWarner Losh {
220592ffb21SWarner Losh 	struct ttm_mem_type_manager *man;
221592ffb21SWarner Losh 
222592ffb21SWarner Losh 	man = &bdev->man[mem->mem_type];
223592ffb21SWarner Losh 
224592ffb21SWarner Losh 	if (virtual && mem->bus.addr == NULL)
225*7ae99f80SJohn Baldwin 		pmap_unmapdev(virtual, mem->bus.size);
226592ffb21SWarner Losh 	(void) ttm_mem_io_lock(man, false);
227592ffb21SWarner Losh 	ttm_mem_io_free(bdev, mem);
228592ffb21SWarner Losh 	ttm_mem_io_unlock(man);
229592ffb21SWarner Losh }
230592ffb21SWarner Losh 
ttm_copy_io_page(void * dst,void * src,unsigned long page)231592ffb21SWarner Losh static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
232592ffb21SWarner Losh {
233592ffb21SWarner Losh 	uint32_t *dstP =
234592ffb21SWarner Losh 	    (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
235592ffb21SWarner Losh 	uint32_t *srcP =
236592ffb21SWarner Losh 	    (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
237592ffb21SWarner Losh 
238592ffb21SWarner Losh 	int i;
239592ffb21SWarner Losh 	for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
240592ffb21SWarner Losh 		/* iowrite32(ioread32(srcP++), dstP++); */
241592ffb21SWarner Losh 		*dstP++ = *srcP++;
242592ffb21SWarner Losh 	return 0;
243592ffb21SWarner Losh }
244592ffb21SWarner Losh 
ttm_copy_io_ttm_page(struct ttm_tt * ttm,void * src,unsigned long page,vm_memattr_t prot)245592ffb21SWarner Losh static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
246592ffb21SWarner Losh 				unsigned long page,
247592ffb21SWarner Losh 				vm_memattr_t prot)
248592ffb21SWarner Losh {
249592ffb21SWarner Losh 	vm_page_t d = ttm->pages[page];
250592ffb21SWarner Losh 	void *dst;
251592ffb21SWarner Losh 
252592ffb21SWarner Losh 	if (!d)
253592ffb21SWarner Losh 		return -ENOMEM;
254592ffb21SWarner Losh 
255592ffb21SWarner Losh 	src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
256592ffb21SWarner Losh 
257592ffb21SWarner Losh 	/* XXXKIB can't sleep ? */
258592ffb21SWarner Losh 	dst = pmap_mapdev_attr(VM_PAGE_TO_PHYS(d), PAGE_SIZE, prot);
259592ffb21SWarner Losh 	if (!dst)
260592ffb21SWarner Losh 		return -ENOMEM;
261592ffb21SWarner Losh 
262592ffb21SWarner Losh 	memcpy(dst, src, PAGE_SIZE);
263592ffb21SWarner Losh 
264*7ae99f80SJohn Baldwin 	pmap_unmapdev(dst, PAGE_SIZE);
265592ffb21SWarner Losh 
266592ffb21SWarner Losh 	return 0;
267592ffb21SWarner Losh }
268592ffb21SWarner Losh 
ttm_copy_ttm_io_page(struct ttm_tt * ttm,void * dst,unsigned long page,vm_memattr_t prot)269592ffb21SWarner Losh static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
270592ffb21SWarner Losh 				unsigned long page,
271592ffb21SWarner Losh 				vm_memattr_t prot)
272592ffb21SWarner Losh {
273592ffb21SWarner Losh 	vm_page_t s = ttm->pages[page];
274592ffb21SWarner Losh 	void *src;
275592ffb21SWarner Losh 
276592ffb21SWarner Losh 	if (!s)
277592ffb21SWarner Losh 		return -ENOMEM;
278592ffb21SWarner Losh 
279592ffb21SWarner Losh 	dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
280592ffb21SWarner Losh 	src = pmap_mapdev_attr(VM_PAGE_TO_PHYS(s), PAGE_SIZE, prot);
281592ffb21SWarner Losh 	if (!src)
282592ffb21SWarner Losh 		return -ENOMEM;
283592ffb21SWarner Losh 
284592ffb21SWarner Losh 	memcpy(dst, src, PAGE_SIZE);
285592ffb21SWarner Losh 
286*7ae99f80SJohn Baldwin 	pmap_unmapdev(src, PAGE_SIZE);
287592ffb21SWarner Losh 
288592ffb21SWarner Losh 	return 0;
289592ffb21SWarner Losh }
290592ffb21SWarner Losh 
ttm_bo_move_memcpy(struct ttm_buffer_object * bo,bool evict,bool no_wait_gpu,struct ttm_mem_reg * new_mem)291592ffb21SWarner Losh int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
292592ffb21SWarner Losh 		       bool evict, bool no_wait_gpu,
293592ffb21SWarner Losh 		       struct ttm_mem_reg *new_mem)
294592ffb21SWarner Losh {
295592ffb21SWarner Losh 	struct ttm_bo_device *bdev = bo->bdev;
296592ffb21SWarner Losh 	struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
297592ffb21SWarner Losh 	struct ttm_tt *ttm = bo->ttm;
298592ffb21SWarner Losh 	struct ttm_mem_reg *old_mem = &bo->mem;
299592ffb21SWarner Losh 	struct ttm_mem_reg old_copy = *old_mem;
300592ffb21SWarner Losh 	void *old_iomap;
301592ffb21SWarner Losh 	void *new_iomap;
302592ffb21SWarner Losh 	int ret;
303592ffb21SWarner Losh 	unsigned long i;
304592ffb21SWarner Losh 	unsigned long page;
305592ffb21SWarner Losh 	unsigned long add = 0;
306592ffb21SWarner Losh 	int dir;
307592ffb21SWarner Losh 
308592ffb21SWarner Losh 	ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
309592ffb21SWarner Losh 	if (ret)
310592ffb21SWarner Losh 		return ret;
311592ffb21SWarner Losh 	ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
312592ffb21SWarner Losh 	if (ret)
313592ffb21SWarner Losh 		goto out;
314592ffb21SWarner Losh 
315592ffb21SWarner Losh 	if (old_iomap == NULL && new_iomap == NULL)
316592ffb21SWarner Losh 		goto out2;
317592ffb21SWarner Losh 	if (old_iomap == NULL && ttm == NULL)
318592ffb21SWarner Losh 		goto out2;
319592ffb21SWarner Losh 
320592ffb21SWarner Losh 	if (ttm->state == tt_unpopulated) {
321592ffb21SWarner Losh 		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
322592ffb21SWarner Losh 		if (ret) {
323592ffb21SWarner Losh 			/* if we fail here don't nuke the mm node
324592ffb21SWarner Losh 			 * as the bo still owns it */
325592ffb21SWarner Losh 			old_copy.mm_node = NULL;
326592ffb21SWarner Losh 			goto out1;
327592ffb21SWarner Losh 		}
328592ffb21SWarner Losh 	}
329592ffb21SWarner Losh 
330592ffb21SWarner Losh 	add = 0;
331592ffb21SWarner Losh 	dir = 1;
332592ffb21SWarner Losh 
333592ffb21SWarner Losh 	if ((old_mem->mem_type == new_mem->mem_type) &&
334592ffb21SWarner Losh 	    (new_mem->start < old_mem->start + old_mem->size)) {
335592ffb21SWarner Losh 		dir = -1;
336592ffb21SWarner Losh 		add = new_mem->num_pages - 1;
337592ffb21SWarner Losh 	}
338592ffb21SWarner Losh 
339592ffb21SWarner Losh 	for (i = 0; i < new_mem->num_pages; ++i) {
340592ffb21SWarner Losh 		page = i * dir + add;
341592ffb21SWarner Losh 		if (old_iomap == NULL) {
342592ffb21SWarner Losh 			vm_memattr_t prot = ttm_io_prot(old_mem->placement);
343592ffb21SWarner Losh 			ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
344592ffb21SWarner Losh 						   prot);
345592ffb21SWarner Losh 		} else if (new_iomap == NULL) {
346592ffb21SWarner Losh 			vm_memattr_t prot = ttm_io_prot(new_mem->placement);
347592ffb21SWarner Losh 			ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
348592ffb21SWarner Losh 						   prot);
349592ffb21SWarner Losh 		} else
350592ffb21SWarner Losh 			ret = ttm_copy_io_page(new_iomap, old_iomap, page);
351592ffb21SWarner Losh 		if (ret) {
352592ffb21SWarner Losh 			/* failing here, means keep old copy as-is */
353592ffb21SWarner Losh 			old_copy.mm_node = NULL;
354592ffb21SWarner Losh 			goto out1;
355592ffb21SWarner Losh 		}
356592ffb21SWarner Losh 	}
357592ffb21SWarner Losh 	mb();
358592ffb21SWarner Losh out2:
359592ffb21SWarner Losh 	old_copy = *old_mem;
360592ffb21SWarner Losh 	*old_mem = *new_mem;
361592ffb21SWarner Losh 	new_mem->mm_node = NULL;
362592ffb21SWarner Losh 
363592ffb21SWarner Losh 	if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) {
364592ffb21SWarner Losh 		ttm_tt_unbind(ttm);
365592ffb21SWarner Losh 		ttm_tt_destroy(ttm);
366592ffb21SWarner Losh 		bo->ttm = NULL;
367592ffb21SWarner Losh 	}
368592ffb21SWarner Losh 
369592ffb21SWarner Losh out1:
370592ffb21SWarner Losh 	ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
371592ffb21SWarner Losh out:
372592ffb21SWarner Losh 	ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
373592ffb21SWarner Losh 	ttm_bo_mem_put(bo, &old_copy);
374592ffb21SWarner Losh 	return ret;
375592ffb21SWarner Losh }
376592ffb21SWarner Losh 
377592ffb21SWarner Losh MALLOC_DEFINE(M_TTM_TRANSF_OBJ, "ttm_transf_obj", "TTM Transfer Objects");
378592ffb21SWarner Losh 
ttm_transfered_destroy(struct ttm_buffer_object * bo)379592ffb21SWarner Losh static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
380592ffb21SWarner Losh {
381592ffb21SWarner Losh 	free(bo, M_TTM_TRANSF_OBJ);
382592ffb21SWarner Losh }
383592ffb21SWarner Losh 
384592ffb21SWarner Losh /**
385592ffb21SWarner Losh  * ttm_buffer_object_transfer
386592ffb21SWarner Losh  *
387592ffb21SWarner Losh  * @bo: A pointer to a struct ttm_buffer_object.
388592ffb21SWarner Losh  * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
389592ffb21SWarner Losh  * holding the data of @bo with the old placement.
390592ffb21SWarner Losh  *
391592ffb21SWarner Losh  * This is a utility function that may be called after an accelerated move
392592ffb21SWarner Losh  * has been scheduled. A new buffer object is created as a placeholder for
393592ffb21SWarner Losh  * the old data while it's being copied. When that buffer object is idle,
394592ffb21SWarner Losh  * it can be destroyed, releasing the space of the old placement.
395592ffb21SWarner Losh  * Returns:
396592ffb21SWarner Losh  * !0: Failure.
397592ffb21SWarner Losh  */
398592ffb21SWarner Losh 
399592ffb21SWarner Losh static int
ttm_buffer_object_transfer(struct ttm_buffer_object * bo,struct ttm_buffer_object ** new_obj)400592ffb21SWarner Losh ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
401592ffb21SWarner Losh     struct ttm_buffer_object **new_obj)
402592ffb21SWarner Losh {
403592ffb21SWarner Losh 	struct ttm_buffer_object *fbo;
404592ffb21SWarner Losh 	struct ttm_bo_device *bdev = bo->bdev;
405592ffb21SWarner Losh 	struct ttm_bo_driver *driver = bdev->driver;
406592ffb21SWarner Losh 
407592ffb21SWarner Losh 	fbo = malloc(sizeof(*fbo), M_TTM_TRANSF_OBJ, M_WAITOK);
408592ffb21SWarner Losh 	*fbo = *bo;
409592ffb21SWarner Losh 
410592ffb21SWarner Losh 	/**
411592ffb21SWarner Losh 	 * Fix up members that we shouldn't copy directly:
412592ffb21SWarner Losh 	 * TODO: Explicit member copy would probably be better here.
413592ffb21SWarner Losh 	 */
414592ffb21SWarner Losh 
415592ffb21SWarner Losh 	INIT_LIST_HEAD(&fbo->ddestroy);
416592ffb21SWarner Losh 	INIT_LIST_HEAD(&fbo->lru);
417592ffb21SWarner Losh 	INIT_LIST_HEAD(&fbo->swap);
418592ffb21SWarner Losh 	INIT_LIST_HEAD(&fbo->io_reserve_lru);
419592ffb21SWarner Losh 	fbo->vm_node = NULL;
420592ffb21SWarner Losh 	atomic_set(&fbo->cpu_writers, 0);
421592ffb21SWarner Losh 
422592ffb21SWarner Losh 	mtx_lock(&bdev->fence_lock);
423592ffb21SWarner Losh 	if (bo->sync_obj)
424592ffb21SWarner Losh 		fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
425592ffb21SWarner Losh 	else
426592ffb21SWarner Losh 		fbo->sync_obj = NULL;
427592ffb21SWarner Losh 	mtx_unlock(&bdev->fence_lock);
428592ffb21SWarner Losh 	refcount_init(&fbo->list_kref, 1);
429592ffb21SWarner Losh 	refcount_init(&fbo->kref, 1);
430592ffb21SWarner Losh 	fbo->destroy = &ttm_transfered_destroy;
431592ffb21SWarner Losh 	fbo->acc_size = 0;
432592ffb21SWarner Losh 
433592ffb21SWarner Losh 	*new_obj = fbo;
434592ffb21SWarner Losh 	return 0;
435592ffb21SWarner Losh }
436592ffb21SWarner Losh 
437592ffb21SWarner Losh vm_memattr_t
ttm_io_prot(uint32_t caching_flags)438592ffb21SWarner Losh ttm_io_prot(uint32_t caching_flags)
439592ffb21SWarner Losh {
440592ffb21SWarner Losh #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__) || 	\
441592ffb21SWarner Losh  defined(__arm__)
442592ffb21SWarner Losh 	if (caching_flags & TTM_PL_FLAG_WC)
443592ffb21SWarner Losh 		return (VM_MEMATTR_WRITE_COMBINING);
444592ffb21SWarner Losh 	else
445592ffb21SWarner Losh 		/*
446592ffb21SWarner Losh 		 * We do not support i386, look at the linux source
447592ffb21SWarner Losh 		 * for the reason of the comment.
448592ffb21SWarner Losh 		 */
449592ffb21SWarner Losh 		return (VM_MEMATTR_UNCACHEABLE);
450592ffb21SWarner Losh #else
451592ffb21SWarner Losh #error Port me
452592ffb21SWarner Losh #endif
453592ffb21SWarner Losh }
454592ffb21SWarner Losh 
ttm_bo_ioremap(struct ttm_buffer_object * bo,unsigned long offset,unsigned long size,struct ttm_bo_kmap_obj * map)455592ffb21SWarner Losh static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
456592ffb21SWarner Losh 			  unsigned long offset,
457592ffb21SWarner Losh 			  unsigned long size,
458592ffb21SWarner Losh 			  struct ttm_bo_kmap_obj *map)
459592ffb21SWarner Losh {
460592ffb21SWarner Losh 	struct ttm_mem_reg *mem = &bo->mem;
461592ffb21SWarner Losh 
462592ffb21SWarner Losh 	if (bo->mem.bus.addr) {
463592ffb21SWarner Losh 		map->bo_kmap_type = ttm_bo_map_premapped;
464592ffb21SWarner Losh 		map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
465592ffb21SWarner Losh 	} else {
466592ffb21SWarner Losh 		map->bo_kmap_type = ttm_bo_map_iomap;
467592ffb21SWarner Losh 		map->virtual = pmap_mapdev_attr(bo->mem.bus.base +
468592ffb21SWarner Losh 		    bo->mem.bus.offset + offset, size,
469592ffb21SWarner Losh 		    (mem->placement & TTM_PL_FLAG_WC) ?
470592ffb21SWarner Losh 		    VM_MEMATTR_WRITE_COMBINING : VM_MEMATTR_UNCACHEABLE);
471592ffb21SWarner Losh 		map->size = size;
472592ffb21SWarner Losh 	}
473592ffb21SWarner Losh 	return (!map->virtual) ? -ENOMEM : 0;
474592ffb21SWarner Losh }
475592ffb21SWarner Losh 
ttm_bo_kmap_ttm(struct ttm_buffer_object * bo,unsigned long start_page,unsigned long num_pages,struct ttm_bo_kmap_obj * map)476592ffb21SWarner Losh static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
477592ffb21SWarner Losh 			   unsigned long start_page,
478592ffb21SWarner Losh 			   unsigned long num_pages,
479592ffb21SWarner Losh 			   struct ttm_bo_kmap_obj *map)
480592ffb21SWarner Losh {
481592ffb21SWarner Losh 	struct ttm_mem_reg *mem = &bo->mem;
482592ffb21SWarner Losh 	vm_memattr_t prot;
483592ffb21SWarner Losh 	struct ttm_tt *ttm = bo->ttm;
484592ffb21SWarner Losh 	int i, ret;
485592ffb21SWarner Losh 
486592ffb21SWarner Losh 	MPASS(ttm != NULL);
487592ffb21SWarner Losh 
488592ffb21SWarner Losh 	if (ttm->state == tt_unpopulated) {
489592ffb21SWarner Losh 		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
490592ffb21SWarner Losh 		if (ret)
491592ffb21SWarner Losh 			return ret;
492592ffb21SWarner Losh 	}
493592ffb21SWarner Losh 
494592ffb21SWarner Losh 	if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
495592ffb21SWarner Losh 		/*
496592ffb21SWarner Losh 		 * We're mapping a single page, and the desired
497592ffb21SWarner Losh 		 * page protection is consistent with the bo.
498592ffb21SWarner Losh 		 */
499592ffb21SWarner Losh 
500592ffb21SWarner Losh 		map->bo_kmap_type = ttm_bo_map_kmap;
501592ffb21SWarner Losh 		map->page = ttm->pages[start_page];
502592ffb21SWarner Losh 		map->sf = sf_buf_alloc(map->page, 0);
503592ffb21SWarner Losh 		map->virtual = (void *)sf_buf_kva(map->sf);
504592ffb21SWarner Losh 	} else {
505592ffb21SWarner Losh 		/*
506592ffb21SWarner Losh 		 * We need to use vmap to get the desired page protection
507592ffb21SWarner Losh 		 * or to make the buffer object look contiguous.
508592ffb21SWarner Losh 		 */
509592ffb21SWarner Losh 		prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
510592ffb21SWarner Losh 			VM_MEMATTR_DEFAULT : ttm_io_prot(mem->placement);
511592ffb21SWarner Losh 		map->bo_kmap_type = ttm_bo_map_vmap;
512592ffb21SWarner Losh 		map->num_pages = num_pages;
513592ffb21SWarner Losh 		map->virtual = (void *)kva_alloc(num_pages * PAGE_SIZE);
514592ffb21SWarner Losh 		if (map->virtual != NULL) {
515592ffb21SWarner Losh 			for (i = 0; i < num_pages; i++) {
516592ffb21SWarner Losh 				/* XXXKIB hack */
517592ffb21SWarner Losh 				pmap_page_set_memattr(ttm->pages[start_page +
518592ffb21SWarner Losh 				    i], prot);
519592ffb21SWarner Losh 			}
520592ffb21SWarner Losh 			pmap_qenter((vm_offset_t)map->virtual,
521592ffb21SWarner Losh 			    &ttm->pages[start_page], num_pages);
522592ffb21SWarner Losh 		}
523592ffb21SWarner Losh 	}
524592ffb21SWarner Losh 	return (!map->virtual) ? -ENOMEM : 0;
525592ffb21SWarner Losh }
526592ffb21SWarner Losh 
ttm_bo_kmap(struct ttm_buffer_object * bo,unsigned long start_page,unsigned long num_pages,struct ttm_bo_kmap_obj * map)527592ffb21SWarner Losh int ttm_bo_kmap(struct ttm_buffer_object *bo,
528592ffb21SWarner Losh 		unsigned long start_page, unsigned long num_pages,
529592ffb21SWarner Losh 		struct ttm_bo_kmap_obj *map)
530592ffb21SWarner Losh {
531592ffb21SWarner Losh 	struct ttm_mem_type_manager *man =
532592ffb21SWarner Losh 		&bo->bdev->man[bo->mem.mem_type];
533592ffb21SWarner Losh 	unsigned long offset, size;
534592ffb21SWarner Losh 	int ret;
535592ffb21SWarner Losh 
536592ffb21SWarner Losh 	MPASS(list_empty(&bo->swap));
537592ffb21SWarner Losh 	map->virtual = NULL;
538592ffb21SWarner Losh 	map->bo = bo;
539592ffb21SWarner Losh 	if (num_pages > bo->num_pages)
540592ffb21SWarner Losh 		return -EINVAL;
541592ffb21SWarner Losh 	if (start_page > bo->num_pages)
542592ffb21SWarner Losh 		return -EINVAL;
543592ffb21SWarner Losh #if 0
544592ffb21SWarner Losh 	if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
545592ffb21SWarner Losh 		return -EPERM;
546592ffb21SWarner Losh #endif
547592ffb21SWarner Losh 	(void) ttm_mem_io_lock(man, false);
548592ffb21SWarner Losh 	ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
549592ffb21SWarner Losh 	ttm_mem_io_unlock(man);
550592ffb21SWarner Losh 	if (ret)
551592ffb21SWarner Losh 		return ret;
552592ffb21SWarner Losh 	if (!bo->mem.bus.is_iomem) {
553592ffb21SWarner Losh 		return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
554592ffb21SWarner Losh 	} else {
555592ffb21SWarner Losh 		offset = start_page << PAGE_SHIFT;
556592ffb21SWarner Losh 		size = num_pages << PAGE_SHIFT;
557592ffb21SWarner Losh 		return ttm_bo_ioremap(bo, offset, size, map);
558592ffb21SWarner Losh 	}
559592ffb21SWarner Losh }
560592ffb21SWarner Losh 
ttm_bo_kunmap(struct ttm_bo_kmap_obj * map)561592ffb21SWarner Losh void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
562592ffb21SWarner Losh {
563592ffb21SWarner Losh 	struct ttm_buffer_object *bo = map->bo;
564592ffb21SWarner Losh 	struct ttm_mem_type_manager *man =
565592ffb21SWarner Losh 		&bo->bdev->man[bo->mem.mem_type];
566592ffb21SWarner Losh 
567592ffb21SWarner Losh 	if (!map->virtual)
568592ffb21SWarner Losh 		return;
569592ffb21SWarner Losh 	switch (map->bo_kmap_type) {
570592ffb21SWarner Losh 	case ttm_bo_map_iomap:
571*7ae99f80SJohn Baldwin 		pmap_unmapdev(map->virtual, map->size);
572592ffb21SWarner Losh 		break;
573592ffb21SWarner Losh 	case ttm_bo_map_vmap:
574592ffb21SWarner Losh 		pmap_qremove((vm_offset_t)(map->virtual), map->num_pages);
575592ffb21SWarner Losh 		kva_free((vm_offset_t)map->virtual,
576592ffb21SWarner Losh 		    map->num_pages * PAGE_SIZE);
577592ffb21SWarner Losh 		break;
578592ffb21SWarner Losh 	case ttm_bo_map_kmap:
579592ffb21SWarner Losh 		sf_buf_free(map->sf);
580592ffb21SWarner Losh 		break;
581592ffb21SWarner Losh 	case ttm_bo_map_premapped:
582592ffb21SWarner Losh 		break;
583592ffb21SWarner Losh 	default:
584592ffb21SWarner Losh 		MPASS(0);
585592ffb21SWarner Losh 	}
586592ffb21SWarner Losh 	(void) ttm_mem_io_lock(man, false);
587592ffb21SWarner Losh 	ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
588592ffb21SWarner Losh 	ttm_mem_io_unlock(man);
589592ffb21SWarner Losh 	map->virtual = NULL;
590592ffb21SWarner Losh 	map->page = NULL;
591592ffb21SWarner Losh 	map->sf = NULL;
592592ffb21SWarner Losh }
593592ffb21SWarner Losh 
ttm_bo_move_accel_cleanup(struct ttm_buffer_object * bo,void * sync_obj,bool evict,bool no_wait_gpu,struct ttm_mem_reg * new_mem)594592ffb21SWarner Losh int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
595592ffb21SWarner Losh 			      void *sync_obj,
596592ffb21SWarner Losh 			      bool evict,
597592ffb21SWarner Losh 			      bool no_wait_gpu,
598592ffb21SWarner Losh 			      struct ttm_mem_reg *new_mem)
599592ffb21SWarner Losh {
600592ffb21SWarner Losh 	struct ttm_bo_device *bdev = bo->bdev;
601592ffb21SWarner Losh 	struct ttm_bo_driver *driver = bdev->driver;
602592ffb21SWarner Losh 	struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
603592ffb21SWarner Losh 	struct ttm_mem_reg *old_mem = &bo->mem;
604592ffb21SWarner Losh 	int ret;
605592ffb21SWarner Losh 	struct ttm_buffer_object *ghost_obj;
606592ffb21SWarner Losh 	void *tmp_obj = NULL;
607592ffb21SWarner Losh 
608592ffb21SWarner Losh 	mtx_lock(&bdev->fence_lock);
609592ffb21SWarner Losh 	if (bo->sync_obj) {
610592ffb21SWarner Losh 		tmp_obj = bo->sync_obj;
611592ffb21SWarner Losh 		bo->sync_obj = NULL;
612592ffb21SWarner Losh 	}
613592ffb21SWarner Losh 	bo->sync_obj = driver->sync_obj_ref(sync_obj);
614592ffb21SWarner Losh 	if (evict) {
615592ffb21SWarner Losh 		ret = ttm_bo_wait(bo, false, false, false);
616592ffb21SWarner Losh 		mtx_unlock(&bdev->fence_lock);
617592ffb21SWarner Losh 		if (tmp_obj)
618592ffb21SWarner Losh 			driver->sync_obj_unref(&tmp_obj);
619592ffb21SWarner Losh 		if (ret)
620592ffb21SWarner Losh 			return ret;
621592ffb21SWarner Losh 
622592ffb21SWarner Losh 		if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
623592ffb21SWarner Losh 		    (bo->ttm != NULL)) {
624592ffb21SWarner Losh 			ttm_tt_unbind(bo->ttm);
625592ffb21SWarner Losh 			ttm_tt_destroy(bo->ttm);
626592ffb21SWarner Losh 			bo->ttm = NULL;
627592ffb21SWarner Losh 		}
628592ffb21SWarner Losh 		ttm_bo_free_old_node(bo);
629592ffb21SWarner Losh 	} else {
630592ffb21SWarner Losh 		/**
631592ffb21SWarner Losh 		 * This should help pipeline ordinary buffer moves.
632592ffb21SWarner Losh 		 *
633592ffb21SWarner Losh 		 * Hang old buffer memory on a new buffer object,
634592ffb21SWarner Losh 		 * and leave it to be released when the GPU
635592ffb21SWarner Losh 		 * operation has completed.
636592ffb21SWarner Losh 		 */
637592ffb21SWarner Losh 
638592ffb21SWarner Losh 		set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
639592ffb21SWarner Losh 		mtx_unlock(&bdev->fence_lock);
640592ffb21SWarner Losh 		if (tmp_obj)
641592ffb21SWarner Losh 			driver->sync_obj_unref(&tmp_obj);
642592ffb21SWarner Losh 
643592ffb21SWarner Losh 		ret = ttm_buffer_object_transfer(bo, &ghost_obj);
644592ffb21SWarner Losh 		if (ret)
645592ffb21SWarner Losh 			return ret;
646592ffb21SWarner Losh 
647592ffb21SWarner Losh 		/**
648592ffb21SWarner Losh 		 * If we're not moving to fixed memory, the TTM object
649592ffb21SWarner Losh 		 * needs to stay alive. Otherwhise hang it on the ghost
650592ffb21SWarner Losh 		 * bo to be unbound and destroyed.
651592ffb21SWarner Losh 		 */
652592ffb21SWarner Losh 
653592ffb21SWarner Losh 		if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
654592ffb21SWarner Losh 			ghost_obj->ttm = NULL;
655592ffb21SWarner Losh 		else
656592ffb21SWarner Losh 			bo->ttm = NULL;
657592ffb21SWarner Losh 
658592ffb21SWarner Losh 		ttm_bo_unreserve(ghost_obj);
659592ffb21SWarner Losh 		ttm_bo_unref(&ghost_obj);
660592ffb21SWarner Losh 	}
661592ffb21SWarner Losh 
662592ffb21SWarner Losh 	*old_mem = *new_mem;
663592ffb21SWarner Losh 	new_mem->mm_node = NULL;
664592ffb21SWarner Losh 
665592ffb21SWarner Losh 	return 0;
666592ffb21SWarner Losh }
667