1 /**************************************************************************
3 * Copyright (c) 2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
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
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 Hellström <thomas-at-tungstengraphics-dot-com>
34 * Free the old memory node unless it's a pinned region and we
35 * have not been requested to free also pinned regions.
38 static void drm_bo_free_old_node(struct drm_buffer_object *bo)
40 struct drm_bo_mem_reg *old_mem = &bo->mem;
42 if (old_mem->mm_node && (old_mem->mm_node != bo->pinned_node)) {
43 mutex_lock(&bo->dev->struct_mutex);
44 drm_mm_put_block(old_mem->mm_node);
45 old_mem->mm_node = NULL;
46 mutex_unlock(&bo->dev->struct_mutex);
48 old_mem->mm_node = NULL;
51 int drm_bo_move_ttm(struct drm_buffer_object *bo,
52 int evict, int no_wait, struct drm_bo_mem_reg *new_mem)
54 struct drm_ttm *ttm = bo->ttm;
55 struct drm_bo_mem_reg *old_mem = &bo->mem;
56 uint64_t save_flags = old_mem->flags;
57 uint64_t save_mask = old_mem->mask;
60 if (old_mem->mem_type == DRM_BO_MEM_TT) {
66 drm_bo_free_old_node(bo);
67 DRM_FLAG_MASKED(old_mem->flags,
68 DRM_BO_FLAG_CACHED | DRM_BO_FLAG_MAPPABLE |
69 DRM_BO_FLAG_MEM_LOCAL, DRM_BO_MASK_MEMTYPE);
70 old_mem->mem_type = DRM_BO_MEM_LOCAL;
71 save_flags = old_mem->flags;
73 if (new_mem->mem_type != DRM_BO_MEM_LOCAL) {
74 ret = drm_bind_ttm(ttm, new_mem);
80 new_mem->mm_node = NULL;
81 old_mem->mask = save_mask;
82 DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
85 EXPORT_SYMBOL(drm_bo_move_ttm);
88 * \c Return a kernel virtual address to the buffer object PCI memory.
90 * \param bo The buffer object.
91 * \return Failure indication.
93 * Returns -EINVAL if the buffer object is currently not mappable.
94 * Returns -ENOMEM if the ioremap operation failed.
95 * Otherwise returns zero.
97 * After a successfull call, bo->iomap contains the virtual address, or NULL
98 * if the buffer object content is not accessible through PCI space.
99 * Call bo->mutex locked.
102 int drm_mem_reg_ioremap(struct drm_device *dev, struct drm_bo_mem_reg *mem,
105 struct drm_buffer_manager *bm = &dev->bm;
106 struct drm_mem_type_manager *man = &bm->man[mem->mem_type];
107 unsigned long bus_offset;
108 unsigned long bus_size;
109 unsigned long bus_base;
114 ret = drm_bo_pci_offset(dev, mem, &bus_base, &bus_offset, &bus_size);
115 if (ret || bus_size == 0)
118 if (!(man->flags & _DRM_FLAG_NEEDS_IOREMAP))
119 addr = (void *)(((u8 *) man->io_addr) + bus_offset);
121 addr = ioremap_nocache(bus_base + bus_offset, bus_size);
128 EXPORT_SYMBOL(drm_mem_reg_ioremap);
131 * \c Unmap mapping obtained using drm_bo_ioremap
133 * \param bo The buffer object.
135 * Call bo->mutex locked.
138 void drm_mem_reg_iounmap(struct drm_device *dev, struct drm_bo_mem_reg *mem,
141 struct drm_buffer_manager *bm;
142 struct drm_mem_type_manager *man;
145 man = &bm->man[mem->mem_type];
147 if (virtual && (man->flags & _DRM_FLAG_NEEDS_IOREMAP))
151 static int drm_copy_io_page(void *dst, void *src, unsigned long page)
154 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
156 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
159 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
160 iowrite32(ioread32(srcP++), dstP++);
164 static int drm_copy_io_ttm_page(struct drm_ttm *ttm, void *src,
167 struct page *d = drm_ttm_get_page(ttm, page);
173 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
178 memcpy_fromio(dst, src, PAGE_SIZE);
183 static int drm_copy_ttm_io_page(struct drm_ttm *ttm, void *dst, unsigned long page)
185 struct page *s = drm_ttm_get_page(ttm, page);
191 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
196 memcpy_toio(dst, src, PAGE_SIZE);
201 int drm_bo_move_memcpy(struct drm_buffer_object *bo,
202 int evict, int no_wait, struct drm_bo_mem_reg *new_mem)
204 struct drm_device *dev = bo->dev;
205 struct drm_mem_type_manager *man = &dev->bm.man[new_mem->mem_type];
206 struct drm_ttm *ttm = bo->ttm;
207 struct drm_bo_mem_reg *old_mem = &bo->mem;
208 struct drm_bo_mem_reg old_copy = *old_mem;
212 uint64_t save_flags = old_mem->flags;
213 uint64_t save_mask = old_mem->mask;
216 unsigned long add = 0;
219 ret = drm_mem_reg_ioremap(dev, old_mem, &old_iomap);
222 ret = drm_mem_reg_ioremap(dev, new_mem, &new_iomap);
226 if (old_iomap == NULL && new_iomap == NULL)
228 if (old_iomap == NULL && ttm == NULL)
234 if ((old_mem->mem_type == new_mem->mem_type) &&
235 (new_mem->mm_node->start <
236 old_mem->mm_node->start + old_mem->mm_node->size)) {
238 add = new_mem->num_pages - 1;
241 for (i = 0; i < new_mem->num_pages; ++i) {
242 page = i * dir + add;
243 if (old_iomap == NULL)
244 ret = drm_copy_ttm_io_page(ttm, new_iomap, page);
245 else if (new_iomap == NULL)
246 ret = drm_copy_io_ttm_page(ttm, old_iomap, page);
248 ret = drm_copy_io_page(new_iomap, old_iomap, page);
254 drm_bo_free_old_node(bo);
257 new_mem->mm_node = NULL;
258 old_mem->mask = save_mask;
259 DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
261 if ((man->flags & _DRM_FLAG_MEMTYPE_FIXED) && (ttm != NULL)) {
263 drm_destroy_ttm(ttm);
268 drm_mem_reg_iounmap(dev, new_mem, new_iomap);
270 drm_mem_reg_iounmap(dev, &old_copy, old_iomap);
273 EXPORT_SYMBOL(drm_bo_move_memcpy);
276 * Transfer a buffer object's memory and LRU status to a newly
277 * created object. User-space references remains with the old
278 * object. Call bo->mutex locked.
281 int drm_buffer_object_transfer(struct drm_buffer_object *bo,
282 struct drm_buffer_object **new_obj)
284 struct drm_buffer_object *fbo;
285 struct drm_device *dev = bo->dev;
286 struct drm_buffer_manager *bm = &dev->bm;
288 fbo = drm_ctl_calloc(1, sizeof(*fbo), DRM_MEM_BUFOBJ);
293 mutex_init(&fbo->mutex);
294 mutex_lock(&fbo->mutex);
295 mutex_lock(&dev->struct_mutex);
297 DRM_INIT_WAITQUEUE(&bo->event_queue);
298 INIT_LIST_HEAD(&fbo->ddestroy);
299 INIT_LIST_HEAD(&fbo->lru);
300 INIT_LIST_HEAD(&fbo->pinned_lru);
301 #ifdef DRM_ODD_MM_COMPAT
302 INIT_LIST_HEAD(&fbo->vma_list);
303 INIT_LIST_HEAD(&fbo->p_mm_list);
306 fbo->fence = drm_fence_reference_locked(bo->fence);
307 fbo->pinned_node = NULL;
308 fbo->mem.mm_node->private = (void *)fbo;
309 atomic_set(&fbo->usage, 1);
310 atomic_inc(&bm->count);
311 mutex_unlock(&dev->struct_mutex);
312 mutex_unlock(&fbo->mutex);
319 * Since move is underway, we need to block signals in this function.
320 * We cannot restart until it has finished.
323 int drm_bo_move_accel_cleanup(struct drm_buffer_object *bo,
324 int evict, int no_wait, uint32_t fence_class,
325 uint32_t fence_type, uint32_t fence_flags,
326 struct drm_bo_mem_reg *new_mem)
328 struct drm_device *dev = bo->dev;
329 struct drm_mem_type_manager *man = &dev->bm.man[new_mem->mem_type];
330 struct drm_bo_mem_reg *old_mem = &bo->mem;
332 uint64_t save_flags = old_mem->flags;
333 uint64_t save_mask = old_mem->mask;
334 struct drm_buffer_object *old_obj;
337 drm_fence_usage_deref_unlocked(&bo->fence);
338 ret = drm_fence_object_create(dev, fence_class, fence_type,
339 fence_flags | DRM_FENCE_FLAG_EMIT,
341 bo->fence_type = fence_type;
345 #ifdef DRM_ODD_MM_COMPAT
347 * In this mode, we don't allow pipelining a copy blit,
348 * since the buffer will be accessible from user space
349 * the moment we return and rebuild the page tables.
351 * With normal vm operation, page tables are rebuilt
352 * on demand using fault(), which waits for buffer idle.
356 if (evict || ((bo->mem.mm_node == bo->pinned_node) &&
357 bo->mem.mm_node != NULL))
360 ret = drm_bo_wait(bo, 0, 1, 0);
364 drm_bo_free_old_node(bo);
366 if ((man->flags & _DRM_FLAG_MEMTYPE_FIXED) && (bo->ttm != NULL)) {
367 drm_ttm_unbind(bo->ttm);
368 drm_destroy_ttm(bo->ttm);
373 /* This should help pipeline ordinary buffer moves.
375 * Hang old buffer memory on a new buffer object,
376 * and leave it to be released when the GPU
377 * operation has completed.
380 ret = drm_buffer_object_transfer(bo, &old_obj);
385 if (!(man->flags & _DRM_FLAG_MEMTYPE_FIXED))
390 mutex_lock(&dev->struct_mutex);
391 list_del_init(&old_obj->lru);
392 DRM_FLAG_MASKED(bo->priv_flags, 0, _DRM_BO_FLAG_UNFENCED);
393 drm_bo_add_to_lru(old_obj);
395 drm_bo_usage_deref_locked(&old_obj);
396 mutex_unlock(&dev->struct_mutex);
401 new_mem->mm_node = NULL;
402 old_mem->mask = save_mask;
403 DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
406 EXPORT_SYMBOL(drm_bo_move_accel_cleanup);
408 int drm_bo_same_page(unsigned long offset,
409 unsigned long offset2)
411 return (offset & PAGE_MASK) == (offset2 & PAGE_MASK);
413 EXPORT_SYMBOL(drm_bo_same_page);
415 unsigned long drm_bo_offset_end(unsigned long offset,
418 offset = (offset + PAGE_SIZE) & PAGE_MASK;
419 return (end < offset) ? end : offset;
421 EXPORT_SYMBOL(drm_bo_offset_end);
423 static pgprot_t drm_kernel_io_prot(uint32_t map_type)
425 pgprot_t tmp = PAGE_KERNEL;
427 #if defined(__i386__) || defined(__x86_64__)
430 if (drm_use_pat() && map_type == _DRM_TTM) {
431 pgprot_val(tmp) |= _PAGE_PAT;
435 if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
436 pgprot_val(tmp) |= _PAGE_PCD;
437 pgprot_val(tmp) &= ~_PAGE_PWT;
439 #elif defined(__powerpc__)
440 pgprot_val(tmp) |= _PAGE_NO_CACHE;
441 if (map_type == _DRM_REGISTERS)
442 pgprot_val(tmp) |= _PAGE_GUARDED;
444 #if defined(__ia64__)
445 if (map_type == _DRM_TTM)
446 tmp = pgprot_writecombine(tmp);
448 tmp = pgprot_noncached(tmp);
453 static int drm_bo_ioremap(struct drm_buffer_object *bo, unsigned long bus_base,
454 unsigned long bus_offset, unsigned long bus_size,
455 struct drm_bo_kmap_obj *map)
457 struct drm_device *dev = bo->dev;
458 struct drm_bo_mem_reg *mem = &bo->mem;
459 struct drm_mem_type_manager *man = &dev->bm.man[mem->mem_type];
461 if (!(man->flags & _DRM_FLAG_NEEDS_IOREMAP)) {
462 map->bo_kmap_type = bo_map_premapped;
463 map->virtual = (void *)(((u8 *) man->io_addr) + bus_offset);
465 map->bo_kmap_type = bo_map_iomap;
466 map->virtual = ioremap_nocache(bus_base + bus_offset, bus_size);
468 return (!map->virtual) ? -ENOMEM : 0;
471 static int drm_bo_kmap_ttm(struct drm_buffer_object *bo,
472 unsigned long start_page, unsigned long num_pages,
473 struct drm_bo_kmap_obj *map)
475 struct drm_device *dev = bo->dev;
476 struct drm_bo_mem_reg *mem = &bo->mem;
477 struct drm_mem_type_manager *man = &dev->bm.man[mem->mem_type];
479 struct drm_ttm *ttm = bo->ttm;
485 if (num_pages == 1 && (mem->flags & DRM_BO_FLAG_CACHED)) {
488 * We're mapping a single page, and the desired
489 * page protection is consistent with the bo.
492 map->bo_kmap_type = bo_map_kmap;
493 map->page = drm_ttm_get_page(ttm, start_page);
494 map->virtual = kmap(map->page);
497 * Populate the part we're mapping;
500 for (i = start_page; i < start_page + num_pages; ++i) {
501 d = drm_ttm_get_page(ttm, i);
507 * We need to use vmap to get the desired page protection
508 * or to make the buffer object look contigous.
511 prot = (mem->flags & DRM_BO_FLAG_CACHED) ?
513 drm_kernel_io_prot(man->drm_bus_maptype);
514 map->bo_kmap_type = bo_map_vmap;
515 map->virtual = vmap(ttm->pages + start_page,
518 return (!map->virtual) ? -ENOMEM : 0;
522 * This function is to be used for kernel mapping of buffer objects.
523 * It chooses the appropriate mapping method depending on the memory type
524 * and caching policy the buffer currently has.
525 * Mapping multiple pages or buffers that live in io memory is a bit slow and
526 * consumes vmalloc space. Be restrictive with such mappings.
527 * Mapping single pages usually returns the logical kernel address,
529 * BUG may use slower temporary mappings for high memory pages or
530 * uncached / write-combined pages.
532 * The function fills in a drm_bo_kmap_obj which can be used to return the
533 * kernel virtual address of the buffer.
535 * Code servicing a non-priviliged user request is only allowed to map one
536 * page at a time. We might need to implement a better scheme to stop such
537 * processes from consuming all vmalloc space.
540 int drm_bo_kmap(struct drm_buffer_object *bo, unsigned long start_page,
541 unsigned long num_pages, struct drm_bo_kmap_obj *map)
544 unsigned long bus_base;
545 unsigned long bus_offset;
546 unsigned long bus_size;
550 if (num_pages > bo->num_pages)
552 if (start_page > bo->num_pages)
555 if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
558 ret = drm_bo_pci_offset(bo->dev, &bo->mem, &bus_base,
559 &bus_offset, &bus_size);
565 return drm_bo_kmap_ttm(bo, start_page, num_pages, map);
567 bus_offset += start_page << PAGE_SHIFT;
568 bus_size = num_pages << PAGE_SHIFT;
569 return drm_bo_ioremap(bo, bus_base, bus_offset, bus_size, map);
572 EXPORT_SYMBOL(drm_bo_kmap);
574 void drm_bo_kunmap(struct drm_bo_kmap_obj *map)
579 switch (map->bo_kmap_type) {
581 iounmap(map->virtual);
584 vunmap(map->virtual);
589 case bo_map_premapped:
597 EXPORT_SYMBOL(drm_bo_kunmap);