Staging: netwave: delete the driver
[linux/fpc-iii.git] / drivers / gpu / drm / ttm / ttm_bo_util.c
blobd764e82e799b4a264618a723ad66d46aa3e9b217
1 /**************************************************************************
3 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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:
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.
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include "ttm/ttm_bo_driver.h"
32 #include "ttm/ttm_placement.h"
33 #include <linux/io.h>
34 #include <linux/highmem.h>
35 #include <linux/wait.h>
36 #include <linux/slab.h>
37 #include <linux/vmalloc.h>
38 #include <linux/module.h>
40 void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
42 struct ttm_mem_reg *old_mem = &bo->mem;
44 if (old_mem->mm_node) {
45 spin_lock(&bo->glob->lru_lock);
46 drm_mm_put_block(old_mem->mm_node);
47 spin_unlock(&bo->glob->lru_lock);
49 old_mem->mm_node = NULL;
52 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
53 bool evict, bool no_wait, struct ttm_mem_reg *new_mem)
55 struct ttm_tt *ttm = bo->ttm;
56 struct ttm_mem_reg *old_mem = &bo->mem;
57 int ret;
59 if (old_mem->mem_type != TTM_PL_SYSTEM) {
60 ttm_tt_unbind(ttm);
61 ttm_bo_free_old_node(bo);
62 ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
63 TTM_PL_MASK_MEM);
64 old_mem->mem_type = TTM_PL_SYSTEM;
67 ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
68 if (unlikely(ret != 0))
69 return ret;
71 if (new_mem->mem_type != TTM_PL_SYSTEM) {
72 ret = ttm_tt_bind(ttm, new_mem);
73 if (unlikely(ret != 0))
74 return ret;
77 *old_mem = *new_mem;
78 new_mem->mm_node = NULL;
80 return 0;
82 EXPORT_SYMBOL(ttm_bo_move_ttm);
84 int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
85 void **virtual)
87 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
88 unsigned long bus_offset;
89 unsigned long bus_size;
90 unsigned long bus_base;
91 int ret;
92 void *addr;
94 *virtual = NULL;
95 ret = ttm_bo_pci_offset(bdev, mem, &bus_base, &bus_offset, &bus_size);
96 if (ret || bus_size == 0)
97 return ret;
99 if (!(man->flags & TTM_MEMTYPE_FLAG_NEEDS_IOREMAP))
100 addr = (void *)(((u8 *) man->io_addr) + bus_offset);
101 else {
102 if (mem->placement & TTM_PL_FLAG_WC)
103 addr = ioremap_wc(bus_base + bus_offset, bus_size);
104 else
105 addr = ioremap_nocache(bus_base + bus_offset, bus_size);
106 if (!addr)
107 return -ENOMEM;
109 *virtual = addr;
110 return 0;
113 void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
114 void *virtual)
116 struct ttm_mem_type_manager *man;
118 man = &bdev->man[mem->mem_type];
120 if (virtual && (man->flags & TTM_MEMTYPE_FLAG_NEEDS_IOREMAP))
121 iounmap(virtual);
124 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
126 uint32_t *dstP =
127 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
128 uint32_t *srcP =
129 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
131 int i;
132 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
133 iowrite32(ioread32(srcP++), dstP++);
134 return 0;
137 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
138 unsigned long page,
139 pgprot_t prot)
141 struct page *d = ttm_tt_get_page(ttm, page);
142 void *dst;
144 if (!d)
145 return -ENOMEM;
147 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
149 #ifdef CONFIG_X86
150 dst = kmap_atomic_prot(d, KM_USER0, prot);
151 #else
152 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
153 dst = vmap(&d, 1, 0, prot);
154 else
155 dst = kmap(d);
156 #endif
157 if (!dst)
158 return -ENOMEM;
160 memcpy_fromio(dst, src, PAGE_SIZE);
162 #ifdef CONFIG_X86
163 kunmap_atomic(dst, KM_USER0);
164 #else
165 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
166 vunmap(dst);
167 else
168 kunmap(d);
169 #endif
171 return 0;
174 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
175 unsigned long page,
176 pgprot_t prot)
178 struct page *s = ttm_tt_get_page(ttm, page);
179 void *src;
181 if (!s)
182 return -ENOMEM;
184 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
185 #ifdef CONFIG_X86
186 src = kmap_atomic_prot(s, KM_USER0, prot);
187 #else
188 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
189 src = vmap(&s, 1, 0, prot);
190 else
191 src = kmap(s);
192 #endif
193 if (!src)
194 return -ENOMEM;
196 memcpy_toio(dst, src, PAGE_SIZE);
198 #ifdef CONFIG_X86
199 kunmap_atomic(src, KM_USER0);
200 #else
201 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
202 vunmap(src);
203 else
204 kunmap(s);
205 #endif
207 return 0;
210 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
211 bool evict, bool no_wait, struct ttm_mem_reg *new_mem)
213 struct ttm_bo_device *bdev = bo->bdev;
214 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
215 struct ttm_tt *ttm = bo->ttm;
216 struct ttm_mem_reg *old_mem = &bo->mem;
217 struct ttm_mem_reg old_copy = *old_mem;
218 void *old_iomap;
219 void *new_iomap;
220 int ret;
221 unsigned long i;
222 unsigned long page;
223 unsigned long add = 0;
224 int dir;
226 ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
227 if (ret)
228 return ret;
229 ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
230 if (ret)
231 goto out;
233 if (old_iomap == NULL && new_iomap == NULL)
234 goto out2;
235 if (old_iomap == NULL && ttm == NULL)
236 goto out2;
238 add = 0;
239 dir = 1;
241 if ((old_mem->mem_type == new_mem->mem_type) &&
242 (new_mem->mm_node->start <
243 old_mem->mm_node->start + old_mem->mm_node->size)) {
244 dir = -1;
245 add = new_mem->num_pages - 1;
248 for (i = 0; i < new_mem->num_pages; ++i) {
249 page = i * dir + add;
250 if (old_iomap == NULL) {
251 pgprot_t prot = ttm_io_prot(old_mem->placement,
252 PAGE_KERNEL);
253 ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
254 prot);
255 } else if (new_iomap == NULL) {
256 pgprot_t prot = ttm_io_prot(new_mem->placement,
257 PAGE_KERNEL);
258 ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
259 prot);
260 } else
261 ret = ttm_copy_io_page(new_iomap, old_iomap, page);
262 if (ret)
263 goto out1;
265 mb();
266 out2:
267 ttm_bo_free_old_node(bo);
269 *old_mem = *new_mem;
270 new_mem->mm_node = NULL;
272 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) {
273 ttm_tt_unbind(ttm);
274 ttm_tt_destroy(ttm);
275 bo->ttm = NULL;
278 out1:
279 ttm_mem_reg_iounmap(bdev, new_mem, new_iomap);
280 out:
281 ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
282 return ret;
284 EXPORT_SYMBOL(ttm_bo_move_memcpy);
286 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
288 kfree(bo);
292 * ttm_buffer_object_transfer
294 * @bo: A pointer to a struct ttm_buffer_object.
295 * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
296 * holding the data of @bo with the old placement.
298 * This is a utility function that may be called after an accelerated move
299 * has been scheduled. A new buffer object is created as a placeholder for
300 * the old data while it's being copied. When that buffer object is idle,
301 * it can be destroyed, releasing the space of the old placement.
302 * Returns:
303 * !0: Failure.
306 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
307 struct ttm_buffer_object **new_obj)
309 struct ttm_buffer_object *fbo;
310 struct ttm_bo_device *bdev = bo->bdev;
311 struct ttm_bo_driver *driver = bdev->driver;
313 fbo = kzalloc(sizeof(*fbo), GFP_KERNEL);
314 if (!fbo)
315 return -ENOMEM;
317 *fbo = *bo;
320 * Fix up members that we shouldn't copy directly:
321 * TODO: Explicit member copy would probably be better here.
324 spin_lock_init(&fbo->lock);
325 init_waitqueue_head(&fbo->event_queue);
326 INIT_LIST_HEAD(&fbo->ddestroy);
327 INIT_LIST_HEAD(&fbo->lru);
328 INIT_LIST_HEAD(&fbo->swap);
329 fbo->vm_node = NULL;
331 fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
332 if (fbo->mem.mm_node)
333 fbo->mem.mm_node->private = (void *)fbo;
334 kref_init(&fbo->list_kref);
335 kref_init(&fbo->kref);
336 fbo->destroy = &ttm_transfered_destroy;
338 *new_obj = fbo;
339 return 0;
342 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
344 #if defined(__i386__) || defined(__x86_64__)
345 if (caching_flags & TTM_PL_FLAG_WC)
346 tmp = pgprot_writecombine(tmp);
347 else if (boot_cpu_data.x86 > 3)
348 tmp = pgprot_noncached(tmp);
350 #elif defined(__powerpc__)
351 if (!(caching_flags & TTM_PL_FLAG_CACHED)) {
352 pgprot_val(tmp) |= _PAGE_NO_CACHE;
353 if (caching_flags & TTM_PL_FLAG_UNCACHED)
354 pgprot_val(tmp) |= _PAGE_GUARDED;
356 #endif
357 #if defined(__ia64__)
358 if (caching_flags & TTM_PL_FLAG_WC)
359 tmp = pgprot_writecombine(tmp);
360 else
361 tmp = pgprot_noncached(tmp);
362 #endif
363 #if defined(__sparc__)
364 if (!(caching_flags & TTM_PL_FLAG_CACHED))
365 tmp = pgprot_noncached(tmp);
366 #endif
367 return tmp;
369 EXPORT_SYMBOL(ttm_io_prot);
371 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
372 unsigned long bus_base,
373 unsigned long bus_offset,
374 unsigned long bus_size,
375 struct ttm_bo_kmap_obj *map)
377 struct ttm_bo_device *bdev = bo->bdev;
378 struct ttm_mem_reg *mem = &bo->mem;
379 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
381 if (!(man->flags & TTM_MEMTYPE_FLAG_NEEDS_IOREMAP)) {
382 map->bo_kmap_type = ttm_bo_map_premapped;
383 map->virtual = (void *)(((u8 *) man->io_addr) + bus_offset);
384 } else {
385 map->bo_kmap_type = ttm_bo_map_iomap;
386 if (mem->placement & TTM_PL_FLAG_WC)
387 map->virtual = ioremap_wc(bus_base + bus_offset,
388 bus_size);
389 else
390 map->virtual = ioremap_nocache(bus_base + bus_offset,
391 bus_size);
393 return (!map->virtual) ? -ENOMEM : 0;
396 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
397 unsigned long start_page,
398 unsigned long num_pages,
399 struct ttm_bo_kmap_obj *map)
401 struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
402 struct ttm_tt *ttm = bo->ttm;
403 struct page *d;
404 int i;
406 BUG_ON(!ttm);
407 if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
409 * We're mapping a single page, and the desired
410 * page protection is consistent with the bo.
413 map->bo_kmap_type = ttm_bo_map_kmap;
414 map->page = ttm_tt_get_page(ttm, start_page);
415 map->virtual = kmap(map->page);
416 } else {
418 * Populate the part we're mapping;
420 for (i = start_page; i < start_page + num_pages; ++i) {
421 d = ttm_tt_get_page(ttm, i);
422 if (!d)
423 return -ENOMEM;
427 * We need to use vmap to get the desired page protection
428 * or to make the buffer object look contiguous.
430 prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
431 PAGE_KERNEL :
432 ttm_io_prot(mem->placement, PAGE_KERNEL);
433 map->bo_kmap_type = ttm_bo_map_vmap;
434 map->virtual = vmap(ttm->pages + start_page, num_pages,
435 0, prot);
437 return (!map->virtual) ? -ENOMEM : 0;
440 int ttm_bo_kmap(struct ttm_buffer_object *bo,
441 unsigned long start_page, unsigned long num_pages,
442 struct ttm_bo_kmap_obj *map)
444 int ret;
445 unsigned long bus_base;
446 unsigned long bus_offset;
447 unsigned long bus_size;
449 BUG_ON(!list_empty(&bo->swap));
450 map->virtual = NULL;
451 if (num_pages > bo->num_pages)
452 return -EINVAL;
453 if (start_page > bo->num_pages)
454 return -EINVAL;
455 #if 0
456 if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
457 return -EPERM;
458 #endif
459 ret = ttm_bo_pci_offset(bo->bdev, &bo->mem, &bus_base,
460 &bus_offset, &bus_size);
461 if (ret)
462 return ret;
463 if (bus_size == 0) {
464 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
465 } else {
466 bus_offset += start_page << PAGE_SHIFT;
467 bus_size = num_pages << PAGE_SHIFT;
468 return ttm_bo_ioremap(bo, bus_base, bus_offset, bus_size, map);
471 EXPORT_SYMBOL(ttm_bo_kmap);
473 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
475 if (!map->virtual)
476 return;
477 switch (map->bo_kmap_type) {
478 case ttm_bo_map_iomap:
479 iounmap(map->virtual);
480 break;
481 case ttm_bo_map_vmap:
482 vunmap(map->virtual);
483 break;
484 case ttm_bo_map_kmap:
485 kunmap(map->page);
486 break;
487 case ttm_bo_map_premapped:
488 break;
489 default:
490 BUG();
492 map->virtual = NULL;
493 map->page = NULL;
495 EXPORT_SYMBOL(ttm_bo_kunmap);
497 int ttm_bo_pfn_prot(struct ttm_buffer_object *bo,
498 unsigned long dst_offset,
499 unsigned long *pfn, pgprot_t *prot)
501 struct ttm_mem_reg *mem = &bo->mem;
502 struct ttm_bo_device *bdev = bo->bdev;
503 unsigned long bus_offset;
504 unsigned long bus_size;
505 unsigned long bus_base;
506 int ret;
507 ret = ttm_bo_pci_offset(bdev, mem, &bus_base, &bus_offset,
508 &bus_size);
509 if (ret)
510 return -EINVAL;
511 if (bus_size != 0)
512 *pfn = (bus_base + bus_offset + dst_offset) >> PAGE_SHIFT;
513 else
514 if (!bo->ttm)
515 return -EINVAL;
516 else
517 *pfn = page_to_pfn(ttm_tt_get_page(bo->ttm,
518 dst_offset >>
519 PAGE_SHIFT));
520 *prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
521 PAGE_KERNEL : ttm_io_prot(mem->placement, PAGE_KERNEL);
523 return 0;
526 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
527 void *sync_obj,
528 void *sync_obj_arg,
529 bool evict, bool no_wait,
530 struct ttm_mem_reg *new_mem)
532 struct ttm_bo_device *bdev = bo->bdev;
533 struct ttm_bo_driver *driver = bdev->driver;
534 struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
535 struct ttm_mem_reg *old_mem = &bo->mem;
536 int ret;
537 struct ttm_buffer_object *ghost_obj;
538 void *tmp_obj = NULL;
540 spin_lock(&bo->lock);
541 if (bo->sync_obj) {
542 tmp_obj = bo->sync_obj;
543 bo->sync_obj = NULL;
545 bo->sync_obj = driver->sync_obj_ref(sync_obj);
546 bo->sync_obj_arg = sync_obj_arg;
547 if (evict) {
548 ret = ttm_bo_wait(bo, false, false, false);
549 spin_unlock(&bo->lock);
550 if (tmp_obj)
551 driver->sync_obj_unref(&tmp_obj);
552 if (ret)
553 return ret;
555 ttm_bo_free_old_node(bo);
556 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
557 (bo->ttm != NULL)) {
558 ttm_tt_unbind(bo->ttm);
559 ttm_tt_destroy(bo->ttm);
560 bo->ttm = NULL;
562 } else {
564 * This should help pipeline ordinary buffer moves.
566 * Hang old buffer memory on a new buffer object,
567 * and leave it to be released when the GPU
568 * operation has completed.
571 set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
572 spin_unlock(&bo->lock);
573 if (tmp_obj)
574 driver->sync_obj_unref(&tmp_obj);
576 ret = ttm_buffer_object_transfer(bo, &ghost_obj);
577 if (ret)
578 return ret;
581 * If we're not moving to fixed memory, the TTM object
582 * needs to stay alive. Otherwhise hang it on the ghost
583 * bo to be unbound and destroyed.
586 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
587 ghost_obj->ttm = NULL;
588 else
589 bo->ttm = NULL;
591 ttm_bo_unreserve(ghost_obj);
592 ttm_bo_unref(&ghost_obj);
595 *old_mem = *new_mem;
596 new_mem->mm_node = NULL;
598 return 0;
600 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);