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 mutex_unlock(&bo->dev->struct_mutex);
47 old_mem->mm_node = NULL;
50 int drm_bo_move_ttm(struct drm_buffer_object *bo,
51 int evict, int no_wait, struct drm_bo_mem_reg *new_mem)
53 struct drm_ttm *ttm = bo->ttm;
54 struct drm_bo_mem_reg *old_mem = &bo->mem;
55 uint64_t save_flags = old_mem->flags;
56 uint64_t save_proposed_flags = old_mem->proposed_flags;
59 if (old_mem->mem_type != DRM_BO_MEM_LOCAL) {
65 drm_bo_free_old_node(bo);
66 DRM_FLAG_MASKED(old_mem->flags,
67 DRM_BO_FLAG_CACHED | DRM_BO_FLAG_MAPPABLE |
68 DRM_BO_FLAG_MEM_LOCAL, DRM_BO_MASK_MEMTYPE);
69 old_mem->mem_type = DRM_BO_MEM_LOCAL;
70 save_flags = old_mem->flags;
72 if (new_mem->mem_type != DRM_BO_MEM_LOCAL) {
73 ret = drm_ttm_bind(ttm, new_mem);
79 new_mem->mm_node = NULL;
80 old_mem->proposed_flags = save_proposed_flags;
81 DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
84 EXPORT_SYMBOL(drm_bo_move_ttm);
87 * \c Return a kernel virtual address to the buffer object PCI memory.
89 * \param bo The buffer object.
90 * \return Failure indication.
92 * Returns -EINVAL if the buffer object is currently not mappable.
93 * Returns -ENOMEM if the ioremap operation failed.
94 * Otherwise returns zero.
96 * After a successfull call, bo->iomap contains the virtual address, or NULL
97 * if the buffer object content is not accessible through PCI space.
98 * Call bo->mutex locked.
101 int drm_mem_reg_ioremap(struct drm_device *dev, struct drm_bo_mem_reg *mem,
104 struct drm_buffer_manager *bm = &dev->bm;
105 struct drm_mem_type_manager *man = &bm->man[mem->mem_type];
106 unsigned long bus_offset;
107 unsigned long bus_size;
108 unsigned long bus_base;
113 ret = drm_bo_pci_offset(dev, mem, &bus_base, &bus_offset, &bus_size);
114 if (ret || bus_size == 0)
117 if (!(man->flags & _DRM_FLAG_NEEDS_IOREMAP))
118 addr = (void *)(((u8 *) man->io_addr) + bus_offset);
120 addr = ioremap_nocache(bus_base + bus_offset, bus_size);
127 EXPORT_SYMBOL(drm_mem_reg_ioremap);
130 * \c Unmap mapping obtained using drm_bo_ioremap
132 * \param bo The buffer object.
134 * Call bo->mutex locked.
137 void drm_mem_reg_iounmap(struct drm_device *dev, struct drm_bo_mem_reg *mem,
140 struct drm_buffer_manager *bm;
141 struct drm_mem_type_manager *man;
144 man = &bm->man[mem->mem_type];
146 if (virtual && (man->flags & _DRM_FLAG_NEEDS_IOREMAP))
150 static int drm_copy_io_page(void *dst, void *src, unsigned long page)
153 (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
155 (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
158 for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
159 iowrite32(ioread32(srcP++), dstP++);
163 static int drm_copy_io_ttm_page(struct drm_ttm *ttm, void *src,
166 struct page *d = drm_ttm_get_page(ttm, page);
172 src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
177 memcpy_fromio(dst, src, PAGE_SIZE);
182 static int drm_copy_ttm_io_page(struct drm_ttm *ttm, void *dst, unsigned long page)
184 struct page *s = drm_ttm_get_page(ttm, page);
190 dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
195 memcpy_toio(dst, src, PAGE_SIZE);
200 int drm_bo_move_memcpy(struct drm_buffer_object *bo,
201 int evict, int no_wait, struct drm_bo_mem_reg *new_mem)
203 struct drm_device *dev = bo->dev;
204 struct drm_mem_type_manager *man = &dev->bm.man[new_mem->mem_type];
205 struct drm_ttm *ttm = bo->ttm;
206 struct drm_bo_mem_reg *old_mem = &bo->mem;
207 struct drm_bo_mem_reg old_copy = *old_mem;
211 uint64_t save_flags = old_mem->flags;
212 uint64_t save_proposed_flags = old_mem->proposed_flags;
215 unsigned long add = 0;
218 ret = drm_mem_reg_ioremap(dev, old_mem, &old_iomap);
221 ret = drm_mem_reg_ioremap(dev, new_mem, &new_iomap);
225 if (old_iomap == NULL && new_iomap == NULL)
227 if (old_iomap == NULL && ttm == NULL)
233 if ((old_mem->mem_type == new_mem->mem_type) &&
234 (new_mem->mm_node->start <
235 old_mem->mm_node->start + old_mem->mm_node->size)) {
237 add = new_mem->num_pages - 1;
240 for (i = 0; i < new_mem->num_pages; ++i) {
241 page = i * dir + add;
242 if (old_iomap == NULL)
243 ret = drm_copy_ttm_io_page(ttm, new_iomap, page);
244 else if (new_iomap == NULL)
245 ret = drm_copy_io_ttm_page(ttm, old_iomap, page);
247 ret = drm_copy_io_page(new_iomap, old_iomap, page);
253 drm_bo_free_old_node(bo);
256 new_mem->mm_node = NULL;
257 old_mem->proposed_flags = save_proposed_flags;
258 DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
260 if ((man->flags & _DRM_FLAG_MEMTYPE_FIXED) && (ttm != NULL)) {
262 drm_ttm_destroy(ttm);
267 drm_mem_reg_iounmap(dev, new_mem, new_iomap);
269 drm_mem_reg_iounmap(dev, &old_copy, old_iomap);
272 EXPORT_SYMBOL(drm_bo_move_memcpy);
275 * Transfer a buffer object's memory and LRU status to a newly
276 * created object. User-space references remains with the old
277 * object. Call bo->mutex locked.
280 int drm_buffer_object_transfer(struct drm_buffer_object *bo,
281 struct drm_buffer_object **new_obj)
283 struct drm_buffer_object *fbo;
284 struct drm_device *dev = bo->dev;
285 struct drm_buffer_manager *bm = &dev->bm;
287 fbo = drm_ctl_calloc(1, sizeof(*fbo), DRM_MEM_BUFOBJ);
292 mutex_init(&fbo->mutex);
293 mutex_lock(&fbo->mutex);
294 mutex_lock(&dev->struct_mutex);
296 DRM_INIT_WAITQUEUE(&bo->event_queue);
297 INIT_LIST_HEAD(&fbo->ddestroy);
298 INIT_LIST_HEAD(&fbo->lru);
299 INIT_LIST_HEAD(&fbo->pinned_lru);
300 #ifdef DRM_ODD_MM_COMPAT
301 INIT_LIST_HEAD(&fbo->vma_list);
302 INIT_LIST_HEAD(&fbo->p_mm_list);
305 fbo->fence = drm_fence_reference_locked(bo->fence);
306 fbo->pinned_node = NULL;
307 fbo->mem.mm_node->private = (void *)fbo;
308 atomic_set(&fbo->usage, 1);
309 atomic_inc(&bm->count);
310 mutex_unlock(&dev->struct_mutex);
311 mutex_unlock(&fbo->mutex);
318 * Since move is underway, we need to block signals in this function.
319 * We cannot restart until it has finished.
322 int drm_bo_move_accel_cleanup(struct drm_buffer_object *bo,
323 int evict, int no_wait, uint32_t fence_class,
324 uint32_t fence_type, uint32_t fence_flags,
325 struct drm_bo_mem_reg *new_mem)
327 struct drm_device *dev = bo->dev;
328 struct drm_mem_type_manager *man = &dev->bm.man[new_mem->mem_type];
329 struct drm_bo_mem_reg *old_mem = &bo->mem;
331 uint64_t save_flags = old_mem->flags;
332 uint64_t save_proposed_flags = old_mem->proposed_flags;
333 struct drm_buffer_object *old_obj;
336 drm_fence_usage_deref_unlocked(&bo->fence);
337 ret = drm_fence_object_create(dev, fence_class, fence_type,
338 fence_flags | DRM_FENCE_FLAG_EMIT,
340 bo->fence_type = fence_type;
344 #ifdef DRM_ODD_MM_COMPAT
346 * In this mode, we don't allow pipelining a copy blit,
347 * since the buffer will be accessible from user space
348 * the moment we return and rebuild the page tables.
350 * With normal vm operation, page tables are rebuilt
351 * on demand using fault(), which waits for buffer idle.
355 if (evict || ((bo->mem.mm_node == bo->pinned_node) &&
356 bo->mem.mm_node != NULL))
359 ret = drm_bo_wait(bo, 0, 1, 0);
363 drm_bo_free_old_node(bo);
365 if ((man->flags & _DRM_FLAG_MEMTYPE_FIXED) && (bo->ttm != NULL)) {
366 drm_ttm_unbind(bo->ttm);
367 drm_ttm_destroy(bo->ttm);
372 /* This should help pipeline ordinary buffer moves.
374 * Hang old buffer memory on a new buffer object,
375 * and leave it to be released when the GPU
376 * operation has completed.
379 ret = drm_buffer_object_transfer(bo, &old_obj);
384 if (!(man->flags & _DRM_FLAG_MEMTYPE_FIXED))
389 mutex_lock(&dev->struct_mutex);
390 list_del_init(&old_obj->lru);
391 DRM_FLAG_MASKED(bo->priv_flags, 0, _DRM_BO_FLAG_UNFENCED);
392 drm_bo_add_to_lru(old_obj);
394 drm_bo_usage_deref_locked(&old_obj);
395 mutex_unlock(&dev->struct_mutex);
400 new_mem->mm_node = NULL;
401 old_mem->proposed_flags = save_proposed_flags;
402 DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
405 EXPORT_SYMBOL(drm_bo_move_accel_cleanup);
407 int drm_bo_same_page(unsigned long offset,
408 unsigned long offset2)
410 return (offset & PAGE_MASK) == (offset2 & PAGE_MASK);
412 EXPORT_SYMBOL(drm_bo_same_page);
414 unsigned long drm_bo_offset_end(unsigned long offset,
417 offset = (offset + PAGE_SIZE) & PAGE_MASK;
418 return (end < offset) ? end : offset;
420 EXPORT_SYMBOL(drm_bo_offset_end);
422 static pgprot_t drm_kernel_io_prot(uint32_t map_type)
424 pgprot_t tmp = PAGE_KERNEL;
426 #if defined(__i386__) || defined(__x86_64__)
429 if (drm_use_pat() && map_type == _DRM_TTM) {
430 pgprot_val(tmp) |= _PAGE_PAT;
434 if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
435 pgprot_val(tmp) |= _PAGE_PCD;
436 pgprot_val(tmp) &= ~_PAGE_PWT;
438 #elif defined(__powerpc__)
439 pgprot_val(tmp) |= _PAGE_NO_CACHE;
440 if (map_type == _DRM_REGISTERS)
441 pgprot_val(tmp) |= _PAGE_GUARDED;
443 #if defined(__ia64__)
444 if (map_type == _DRM_TTM)
445 tmp = pgprot_writecombine(tmp);
447 tmp = pgprot_noncached(tmp);
452 static int drm_bo_ioremap(struct drm_buffer_object *bo, unsigned long bus_base,
453 unsigned long bus_offset, unsigned long bus_size,
454 struct drm_bo_kmap_obj *map)
456 struct drm_device *dev = bo->dev;
457 struct drm_bo_mem_reg *mem = &bo->mem;
458 struct drm_mem_type_manager *man = &dev->bm.man[mem->mem_type];
460 if (!(man->flags & _DRM_FLAG_NEEDS_IOREMAP)) {
461 map->bo_kmap_type = bo_map_premapped;
462 map->virtual = (void *)(((u8 *) man->io_addr) + bus_offset);
464 map->bo_kmap_type = bo_map_iomap;
465 map->virtual = ioremap_nocache(bus_base + bus_offset, bus_size);
467 return (!map->virtual) ? -ENOMEM : 0;
470 static int drm_bo_kmap_ttm(struct drm_buffer_object *bo,
471 unsigned long start_page, unsigned long num_pages,
472 struct drm_bo_kmap_obj *map)
474 struct drm_device *dev = bo->dev;
475 struct drm_bo_mem_reg *mem = &bo->mem;
476 struct drm_mem_type_manager *man = &dev->bm.man[mem->mem_type];
478 struct drm_ttm *ttm = bo->ttm;
484 if (num_pages == 1 && (mem->flags & DRM_BO_FLAG_CACHED)) {
487 * We're mapping a single page, and the desired
488 * page protection is consistent with the bo.
491 map->bo_kmap_type = bo_map_kmap;
492 map->page = drm_ttm_get_page(ttm, start_page);
493 map->virtual = kmap(map->page);
496 * Populate the part we're mapping;
499 for (i = start_page; i < start_page + num_pages; ++i) {
500 d = drm_ttm_get_page(ttm, i);
506 * We need to use vmap to get the desired page protection
507 * or to make the buffer object look contigous.
510 prot = (mem->flags & DRM_BO_FLAG_CACHED) ?
512 drm_kernel_io_prot(man->drm_bus_maptype);
513 map->bo_kmap_type = bo_map_vmap;
514 map->virtual = vmap(ttm->pages + start_page,
517 return (!map->virtual) ? -ENOMEM : 0;
521 * This function is to be used for kernel mapping of buffer objects.
522 * It chooses the appropriate mapping method depending on the memory type
523 * and caching policy the buffer currently has.
524 * Mapping multiple pages or buffers that live in io memory is a bit slow and
525 * consumes vmalloc space. Be restrictive with such mappings.
526 * Mapping single pages usually returns the logical kernel address,
528 * BUG may use slower temporary mappings for high memory pages or
529 * uncached / write-combined pages.
531 * The function fills in a drm_bo_kmap_obj which can be used to return the
532 * kernel virtual address of the buffer.
534 * Code servicing a non-priviliged user request is only allowed to map one
535 * page at a time. We might need to implement a better scheme to stop such
536 * processes from consuming all vmalloc space.
539 int drm_bo_kmap(struct drm_buffer_object *bo, unsigned long start_page,
540 unsigned long num_pages, struct drm_bo_kmap_obj *map)
543 unsigned long bus_base;
544 unsigned long bus_offset;
545 unsigned long bus_size;
549 if (num_pages > bo->num_pages)
551 if (start_page > bo->num_pages)
554 if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
557 ret = drm_bo_pci_offset(bo->dev, &bo->mem, &bus_base,
558 &bus_offset, &bus_size);
564 return drm_bo_kmap_ttm(bo, start_page, num_pages, map);
566 bus_offset += start_page << PAGE_SHIFT;
567 bus_size = num_pages << PAGE_SHIFT;
568 return drm_bo_ioremap(bo, bus_base, bus_offset, bus_size, map);
571 EXPORT_SYMBOL(drm_bo_kmap);
573 void drm_bo_kunmap(struct drm_bo_kmap_obj *map)
578 switch (map->bo_kmap_type) {
580 iounmap(map->virtual);
583 vunmap(map->virtual);
588 case bo_map_premapped:
596 EXPORT_SYMBOL(drm_bo_kunmap);
598 int drm_bo_pfn_prot(struct drm_buffer_object *bo,
599 unsigned long dst_offset,
603 struct drm_bo_mem_reg *mem = &bo->mem;
604 struct drm_device *dev = bo->dev;
605 unsigned long bus_offset;
606 unsigned long bus_size;
607 unsigned long bus_base;
608 struct drm_mem_type_manager *man = &dev->bm.man[mem->mem_type];
611 ret = drm_bo_pci_offset(dev, mem, &bus_base, &bus_offset,
617 *pfn = (bus_base + bus_offset + dst_offset) >> PAGE_SHIFT;
621 *pfn = page_to_pfn(drm_ttm_get_page(bo->ttm, dst_offset >> PAGE_SHIFT));
623 *prot = (mem->flags & DRM_BO_FLAG_CACHED) ?
624 PAGE_KERNEL : drm_kernel_io_prot(man->drm_bus_maptype);
628 EXPORT_SYMBOL(drm_bo_pfn_prot);