2 * Copyright 2009 Jerome Glisse.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
33 #include <linux/dma-mapping.h>
34 #include <linux/pagemap.h>
35 #include <linux/pci.h>
36 #include <linux/seq_file.h>
37 #include <linux/slab.h>
38 #include <linux/swap.h>
39 #include <linux/swiotlb.h>
41 #include <drm/drm_device.h>
42 #include <drm/drm_file.h>
43 #include <drm/drm_prime.h>
44 #include <drm/radeon_drm.h>
45 #include <drm/ttm/ttm_bo.h>
46 #include <drm/ttm/ttm_placement.h>
47 #include <drm/ttm/ttm_range_manager.h>
48 #include <drm/ttm/ttm_tt.h>
50 #include "radeon_reg.h"
52 #include "radeon_ttm.h"
54 static void radeon_ttm_debugfs_init(struct radeon_device *rdev);
56 static int radeon_ttm_tt_bind(struct ttm_device *bdev, struct ttm_tt *ttm,
57 struct ttm_resource *bo_mem);
58 static void radeon_ttm_tt_unbind(struct ttm_device *bdev, struct ttm_tt *ttm);
60 struct radeon_device *radeon_get_rdev(struct ttm_device *bdev)
62 struct radeon_mman *mman;
63 struct radeon_device *rdev;
65 mman = container_of(bdev, struct radeon_mman, bdev);
66 rdev = container_of(mman, struct radeon_device, mman);
70 static int radeon_ttm_init_vram(struct radeon_device *rdev)
72 return ttm_range_man_init(&rdev->mman.bdev, TTM_PL_VRAM,
73 false, rdev->mc.real_vram_size >> PAGE_SHIFT);
76 static int radeon_ttm_init_gtt(struct radeon_device *rdev)
78 return ttm_range_man_init(&rdev->mman.bdev, TTM_PL_TT,
79 true, rdev->mc.gtt_size >> PAGE_SHIFT);
82 static void radeon_evict_flags(struct ttm_buffer_object *bo,
83 struct ttm_placement *placement)
85 static const struct ttm_place placements = {
88 .mem_type = TTM_PL_SYSTEM,
92 struct radeon_bo *rbo;
94 if (!radeon_ttm_bo_is_radeon_bo(bo)) {
95 placement->placement = &placements;
96 placement->busy_placement = &placements;
97 placement->num_placement = 1;
98 placement->num_busy_placement = 1;
101 rbo = container_of(bo, struct radeon_bo, tbo);
102 switch (bo->resource->mem_type) {
104 if (rbo->rdev->ring[radeon_copy_ring_index(rbo->rdev)].ready == false)
105 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
106 else if (rbo->rdev->mc.visible_vram_size < rbo->rdev->mc.real_vram_size &&
107 bo->resource->start < (rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT)) {
108 unsigned fpfn = rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
111 /* Try evicting to the CPU inaccessible part of VRAM
112 * first, but only set GTT as busy placement, so this
113 * BO will be evicted to GTT rather than causing other
114 * BOs to be evicted from VRAM
116 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM |
117 RADEON_GEM_DOMAIN_GTT);
118 rbo->placement.num_busy_placement = 0;
119 for (i = 0; i < rbo->placement.num_placement; i++) {
120 if (rbo->placements[i].mem_type == TTM_PL_VRAM) {
121 if (rbo->placements[i].fpfn < fpfn)
122 rbo->placements[i].fpfn = fpfn;
124 rbo->placement.busy_placement =
126 rbo->placement.num_busy_placement = 1;
130 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
134 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
136 *placement = rbo->placement;
139 static int radeon_move_blit(struct ttm_buffer_object *bo,
141 struct ttm_resource *new_mem,
142 struct ttm_resource *old_mem)
144 struct radeon_device *rdev;
145 uint64_t old_start, new_start;
146 struct radeon_fence *fence;
150 rdev = radeon_get_rdev(bo->bdev);
151 ridx = radeon_copy_ring_index(rdev);
152 old_start = (u64)old_mem->start << PAGE_SHIFT;
153 new_start = (u64)new_mem->start << PAGE_SHIFT;
155 switch (old_mem->mem_type) {
157 old_start += rdev->mc.vram_start;
160 old_start += rdev->mc.gtt_start;
163 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
166 switch (new_mem->mem_type) {
168 new_start += rdev->mc.vram_start;
171 new_start += rdev->mc.gtt_start;
174 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
177 if (!rdev->ring[ridx].ready) {
178 DRM_ERROR("Trying to move memory with ring turned off.\n");
182 BUILD_BUG_ON((PAGE_SIZE % RADEON_GPU_PAGE_SIZE) != 0);
184 num_pages = PFN_UP(new_mem->size) * (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
185 fence = radeon_copy(rdev, old_start, new_start, num_pages, bo->base.resv);
187 return PTR_ERR(fence);
189 r = ttm_bo_move_accel_cleanup(bo, &fence->base, evict, false, new_mem);
190 radeon_fence_unref(&fence);
194 static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
195 struct ttm_operation_ctx *ctx,
196 struct ttm_resource *new_mem,
197 struct ttm_place *hop)
199 struct ttm_resource *old_mem = bo->resource;
200 struct radeon_device *rdev;
201 struct radeon_bo *rbo;
204 if (new_mem->mem_type == TTM_PL_TT) {
205 r = radeon_ttm_tt_bind(bo->bdev, bo->ttm, new_mem);
210 r = ttm_bo_wait_ctx(bo, ctx);
214 /* Can't move a pinned BO */
215 rbo = container_of(bo, struct radeon_bo, tbo);
216 if (WARN_ON_ONCE(rbo->tbo.pin_count > 0))
219 rdev = radeon_get_rdev(bo->bdev);
220 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
221 ttm_bo_move_null(bo, new_mem);
224 if (old_mem->mem_type == TTM_PL_SYSTEM &&
225 new_mem->mem_type == TTM_PL_TT) {
226 ttm_bo_move_null(bo, new_mem);
230 if (old_mem->mem_type == TTM_PL_TT &&
231 new_mem->mem_type == TTM_PL_SYSTEM) {
232 radeon_ttm_tt_unbind(bo->bdev, bo->ttm);
233 ttm_resource_free(bo, &bo->resource);
234 ttm_bo_assign_mem(bo, new_mem);
237 if (rdev->ring[radeon_copy_ring_index(rdev)].ready &&
238 rdev->asic->copy.copy != NULL) {
239 if ((old_mem->mem_type == TTM_PL_SYSTEM &&
240 new_mem->mem_type == TTM_PL_VRAM) ||
241 (old_mem->mem_type == TTM_PL_VRAM &&
242 new_mem->mem_type == TTM_PL_SYSTEM)) {
245 hop->mem_type = TTM_PL_TT;
250 r = radeon_move_blit(bo, evict, new_mem, old_mem);
256 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
262 /* update statistics */
263 atomic64_add(bo->base.size, &rdev->num_bytes_moved);
264 radeon_bo_move_notify(bo);
268 static int radeon_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
270 struct radeon_device *rdev = radeon_get_rdev(bdev);
271 size_t bus_size = (size_t)mem->size;
273 switch (mem->mem_type) {
278 #if IS_ENABLED(CONFIG_AGP)
279 if (rdev->flags & RADEON_IS_AGP) {
280 /* RADEON_IS_AGP is set only if AGP is active */
281 mem->bus.offset = (mem->start << PAGE_SHIFT) +
283 mem->bus.is_iomem = !rdev->agp->cant_use_aperture;
284 mem->bus.caching = ttm_write_combined;
289 mem->bus.offset = mem->start << PAGE_SHIFT;
290 /* check if it's visible */
291 if ((mem->bus.offset + bus_size) > rdev->mc.visible_vram_size)
293 mem->bus.offset += rdev->mc.aper_base;
294 mem->bus.is_iomem = true;
295 mem->bus.caching = ttm_write_combined;
298 * Alpha: use bus.addr to hold the ioremap() return,
299 * so we can modify bus.base below.
301 mem->bus.addr = ioremap_wc(mem->bus.offset, bus_size);
306 * Alpha: Use just the bus offset plus
307 * the hose/domain memory base for bus.base.
308 * It then can be used to build PTEs for VRAM
309 * access, as done in ttm_bo_vm_fault().
311 mem->bus.offset = (mem->bus.offset & 0x0ffffffffUL) +
312 rdev->hose->dense_mem_base;
322 * TTM backend functions.
324 struct radeon_ttm_tt {
329 struct mm_struct *usermm;
334 /* prepare the sg table with the user pages */
335 static int radeon_ttm_tt_pin_userptr(struct ttm_device *bdev, struct ttm_tt *ttm)
337 struct radeon_device *rdev = radeon_get_rdev(bdev);
338 struct radeon_ttm_tt *gtt = (void *)ttm;
342 int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
343 enum dma_data_direction direction = write ?
344 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
346 if (current->mm != gtt->usermm)
349 if (gtt->userflags & RADEON_GEM_USERPTR_ANONONLY) {
350 /* check that we only pin down anonymous memory
351 to prevent problems with writeback */
352 unsigned long end = gtt->userptr + (u64)ttm->num_pages * PAGE_SIZE;
353 struct vm_area_struct *vma;
354 vma = find_vma(gtt->usermm, gtt->userptr);
355 if (!vma || vma->vm_file || vma->vm_end < end)
360 unsigned num_pages = ttm->num_pages - pinned;
361 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
362 struct page **pages = ttm->pages + pinned;
364 r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
371 } while (pinned < ttm->num_pages);
373 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
374 (u64)ttm->num_pages << PAGE_SHIFT,
379 r = dma_map_sgtable(rdev->dev, ttm->sg, direction, 0);
383 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
392 release_pages(ttm->pages, pinned);
396 static void radeon_ttm_tt_unpin_userptr(struct ttm_device *bdev, struct ttm_tt *ttm)
398 struct radeon_device *rdev = radeon_get_rdev(bdev);
399 struct radeon_ttm_tt *gtt = (void *)ttm;
400 struct sg_page_iter sg_iter;
402 int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
403 enum dma_data_direction direction = write ?
404 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
406 /* double check that we don't free the table twice */
407 if (!ttm->sg || !ttm->sg->sgl)
410 /* free the sg table and pages again */
411 dma_unmap_sgtable(rdev->dev, ttm->sg, direction, 0);
413 for_each_sgtable_page(ttm->sg, &sg_iter, 0) {
414 struct page *page = sg_page_iter_page(&sg_iter);
415 if (!(gtt->userflags & RADEON_GEM_USERPTR_READONLY))
416 set_page_dirty(page);
418 mark_page_accessed(page);
422 sg_free_table(ttm->sg);
425 static bool radeon_ttm_backend_is_bound(struct ttm_tt *ttm)
427 struct radeon_ttm_tt *gtt = (void*)ttm;
432 static int radeon_ttm_backend_bind(struct ttm_device *bdev,
434 struct ttm_resource *bo_mem)
436 struct radeon_ttm_tt *gtt = (void*)ttm;
437 struct radeon_device *rdev = radeon_get_rdev(bdev);
438 uint32_t flags = RADEON_GART_PAGE_VALID | RADEON_GART_PAGE_READ |
439 RADEON_GART_PAGE_WRITE;
446 radeon_ttm_tt_pin_userptr(bdev, ttm);
447 flags &= ~RADEON_GART_PAGE_WRITE;
450 gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
451 if (!ttm->num_pages) {
452 WARN(1, "nothing to bind %u pages for mreg %p back %p!\n",
453 ttm->num_pages, bo_mem, ttm);
455 if (ttm->caching == ttm_cached)
456 flags |= RADEON_GART_PAGE_SNOOP;
457 r = radeon_gart_bind(rdev, gtt->offset, ttm->num_pages,
458 ttm->pages, gtt->ttm.dma_address, flags);
460 DRM_ERROR("failed to bind %u pages at 0x%08X\n",
461 ttm->num_pages, (unsigned)gtt->offset);
468 static void radeon_ttm_backend_unbind(struct ttm_device *bdev, struct ttm_tt *ttm)
470 struct radeon_ttm_tt *gtt = (void *)ttm;
471 struct radeon_device *rdev = radeon_get_rdev(bdev);
474 radeon_ttm_tt_unpin_userptr(bdev, ttm);
479 radeon_gart_unbind(rdev, gtt->offset, ttm->num_pages);
484 static void radeon_ttm_backend_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
486 struct radeon_ttm_tt *gtt = (void *)ttm;
488 ttm_tt_fini(>t->ttm);
492 static struct ttm_tt *radeon_ttm_tt_create(struct ttm_buffer_object *bo,
495 struct radeon_ttm_tt *gtt;
496 enum ttm_caching caching;
497 struct radeon_bo *rbo;
498 #if IS_ENABLED(CONFIG_AGP)
499 struct radeon_device *rdev = radeon_get_rdev(bo->bdev);
501 if (rdev->flags & RADEON_IS_AGP) {
502 return ttm_agp_tt_create(bo, rdev->agp->bridge, page_flags);
505 rbo = container_of(bo, struct radeon_bo, tbo);
507 gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL);
512 if (rbo->flags & RADEON_GEM_GTT_UC)
513 caching = ttm_uncached;
514 else if (rbo->flags & RADEON_GEM_GTT_WC)
515 caching = ttm_write_combined;
517 caching = ttm_cached;
519 if (ttm_sg_tt_init(>t->ttm, bo, page_flags, caching)) {
526 static struct radeon_ttm_tt *radeon_ttm_tt_to_gtt(struct radeon_device *rdev,
529 #if IS_ENABLED(CONFIG_AGP)
530 if (rdev->flags & RADEON_IS_AGP)
536 return container_of(ttm, struct radeon_ttm_tt, ttm);
539 static int radeon_ttm_tt_populate(struct ttm_device *bdev,
541 struct ttm_operation_ctx *ctx)
543 struct radeon_device *rdev = radeon_get_rdev(bdev);
544 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
545 bool slave = !!(ttm->page_flags & TTM_TT_FLAG_EXTERNAL);
547 if (gtt && gtt->userptr) {
548 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
552 ttm->page_flags |= TTM_TT_FLAG_EXTERNAL;
556 if (slave && ttm->sg) {
557 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
562 return ttm_pool_alloc(&rdev->mman.bdev.pool, ttm, ctx);
565 static void radeon_ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm)
567 struct radeon_device *rdev = radeon_get_rdev(bdev);
568 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
569 bool slave = !!(ttm->page_flags & TTM_TT_FLAG_EXTERNAL);
571 radeon_ttm_tt_unbind(bdev, ttm);
573 if (gtt && gtt->userptr) {
575 ttm->page_flags &= ~TTM_TT_FLAG_EXTERNAL;
582 return ttm_pool_free(&rdev->mman.bdev.pool, ttm);
585 int radeon_ttm_tt_set_userptr(struct radeon_device *rdev,
586 struct ttm_tt *ttm, uint64_t addr,
589 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
595 gtt->usermm = current->mm;
596 gtt->userflags = flags;
600 bool radeon_ttm_tt_is_bound(struct ttm_device *bdev,
603 #if IS_ENABLED(CONFIG_AGP)
604 struct radeon_device *rdev = radeon_get_rdev(bdev);
605 if (rdev->flags & RADEON_IS_AGP)
606 return ttm_agp_is_bound(ttm);
608 return radeon_ttm_backend_is_bound(ttm);
611 static int radeon_ttm_tt_bind(struct ttm_device *bdev,
613 struct ttm_resource *bo_mem)
615 #if IS_ENABLED(CONFIG_AGP)
616 struct radeon_device *rdev = radeon_get_rdev(bdev);
621 #if IS_ENABLED(CONFIG_AGP)
622 if (rdev->flags & RADEON_IS_AGP)
623 return ttm_agp_bind(ttm, bo_mem);
626 return radeon_ttm_backend_bind(bdev, ttm, bo_mem);
629 static void radeon_ttm_tt_unbind(struct ttm_device *bdev,
632 #if IS_ENABLED(CONFIG_AGP)
633 struct radeon_device *rdev = radeon_get_rdev(bdev);
635 if (rdev->flags & RADEON_IS_AGP) {
640 radeon_ttm_backend_unbind(bdev, ttm);
643 static void radeon_ttm_tt_destroy(struct ttm_device *bdev,
646 #if IS_ENABLED(CONFIG_AGP)
647 struct radeon_device *rdev = radeon_get_rdev(bdev);
649 if (rdev->flags & RADEON_IS_AGP) {
650 ttm_agp_destroy(ttm);
654 radeon_ttm_backend_destroy(bdev, ttm);
657 bool radeon_ttm_tt_has_userptr(struct radeon_device *rdev,
660 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
665 return !!gtt->userptr;
668 bool radeon_ttm_tt_is_readonly(struct radeon_device *rdev,
671 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(rdev, ttm);
676 return !!(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
679 static struct ttm_device_funcs radeon_bo_driver = {
680 .ttm_tt_create = &radeon_ttm_tt_create,
681 .ttm_tt_populate = &radeon_ttm_tt_populate,
682 .ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate,
683 .ttm_tt_destroy = &radeon_ttm_tt_destroy,
684 .eviction_valuable = ttm_bo_eviction_valuable,
685 .evict_flags = &radeon_evict_flags,
686 .move = &radeon_bo_move,
687 .io_mem_reserve = &radeon_ttm_io_mem_reserve,
690 int radeon_ttm_init(struct radeon_device *rdev)
694 /* No others user of address space so set it to 0 */
695 r = ttm_device_init(&rdev->mman.bdev, &radeon_bo_driver, rdev->dev,
696 rdev->ddev->anon_inode->i_mapping,
697 rdev->ddev->vma_offset_manager,
699 dma_addressing_limited(&rdev->pdev->dev));
701 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
704 rdev->mman.initialized = true;
706 r = radeon_ttm_init_vram(rdev);
708 DRM_ERROR("Failed initializing VRAM heap.\n");
711 /* Change the size here instead of the init above so only lpfn is affected */
712 radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
714 r = radeon_bo_create(rdev, 256 * 1024, PAGE_SIZE, true,
715 RADEON_GEM_DOMAIN_VRAM, 0, NULL,
716 NULL, &rdev->stolen_vga_memory);
720 r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
723 r = radeon_bo_pin(rdev->stolen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL);
724 radeon_bo_unreserve(rdev->stolen_vga_memory);
726 radeon_bo_unref(&rdev->stolen_vga_memory);
729 DRM_INFO("radeon: %uM of VRAM memory ready\n",
730 (unsigned) (rdev->mc.real_vram_size / (1024 * 1024)));
732 r = radeon_ttm_init_gtt(rdev);
734 DRM_ERROR("Failed initializing GTT heap.\n");
737 DRM_INFO("radeon: %uM of GTT memory ready.\n",
738 (unsigned)(rdev->mc.gtt_size / (1024 * 1024)));
740 radeon_ttm_debugfs_init(rdev);
745 void radeon_ttm_fini(struct radeon_device *rdev)
749 if (!rdev->mman.initialized)
752 if (rdev->stolen_vga_memory) {
753 r = radeon_bo_reserve(rdev->stolen_vga_memory, false);
755 radeon_bo_unpin(rdev->stolen_vga_memory);
756 radeon_bo_unreserve(rdev->stolen_vga_memory);
758 radeon_bo_unref(&rdev->stolen_vga_memory);
760 ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_VRAM);
761 ttm_range_man_fini(&rdev->mman.bdev, TTM_PL_TT);
762 ttm_device_fini(&rdev->mman.bdev);
763 radeon_gart_fini(rdev);
764 rdev->mman.initialized = false;
765 DRM_INFO("radeon: ttm finalized\n");
768 /* this should only be called at bootup or when userspace
770 void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size)
772 struct ttm_resource_manager *man;
774 if (!rdev->mman.initialized)
777 man = ttm_manager_type(&rdev->mman.bdev, TTM_PL_VRAM);
778 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
779 man->size = size >> PAGE_SHIFT;
782 #if defined(CONFIG_DEBUG_FS)
784 static int radeon_ttm_page_pool_show(struct seq_file *m, void *data)
786 struct radeon_device *rdev = (struct radeon_device *)m->private;
788 return ttm_pool_debugfs(&rdev->mman.bdev.pool, m);
791 DEFINE_SHOW_ATTRIBUTE(radeon_ttm_page_pool);
793 static int radeon_ttm_vram_open(struct inode *inode, struct file *filep)
795 struct radeon_device *rdev = inode->i_private;
796 i_size_write(inode, rdev->mc.mc_vram_size);
797 filep->private_data = inode->i_private;
801 static ssize_t radeon_ttm_vram_read(struct file *f, char __user *buf,
802 size_t size, loff_t *pos)
804 struct radeon_device *rdev = f->private_data;
808 if (size & 0x3 || *pos & 0x3)
815 if (*pos >= rdev->mc.mc_vram_size)
818 spin_lock_irqsave(&rdev->mmio_idx_lock, flags);
819 WREG32(RADEON_MM_INDEX, ((uint32_t)*pos) | 0x80000000);
820 if (rdev->family >= CHIP_CEDAR)
821 WREG32(EVERGREEN_MM_INDEX_HI, *pos >> 31);
822 value = RREG32(RADEON_MM_DATA);
823 spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags);
825 r = put_user(value, (uint32_t __user *)buf);
838 static const struct file_operations radeon_ttm_vram_fops = {
839 .owner = THIS_MODULE,
840 .open = radeon_ttm_vram_open,
841 .read = radeon_ttm_vram_read,
842 .llseek = default_llseek
845 static int radeon_ttm_gtt_open(struct inode *inode, struct file *filep)
847 struct radeon_device *rdev = inode->i_private;
848 i_size_write(inode, rdev->mc.gtt_size);
849 filep->private_data = inode->i_private;
853 static ssize_t radeon_ttm_gtt_read(struct file *f, char __user *buf,
854 size_t size, loff_t *pos)
856 struct radeon_device *rdev = f->private_data;
861 loff_t p = *pos / PAGE_SIZE;
862 unsigned off = *pos & ~PAGE_MASK;
863 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
867 if (p >= rdev->gart.num_cpu_pages)
870 page = rdev->gart.pages[p];
872 ptr = kmap_local_page(page);
875 r = copy_to_user(buf, ptr, cur_size);
878 r = clear_user(buf, cur_size);
892 static const struct file_operations radeon_ttm_gtt_fops = {
893 .owner = THIS_MODULE,
894 .open = radeon_ttm_gtt_open,
895 .read = radeon_ttm_gtt_read,
896 .llseek = default_llseek
901 static void radeon_ttm_debugfs_init(struct radeon_device *rdev)
903 #if defined(CONFIG_DEBUG_FS)
904 struct drm_minor *minor = rdev->ddev->primary;
905 struct dentry *root = minor->debugfs_root;
907 debugfs_create_file("radeon_vram", 0444, root, rdev,
908 &radeon_ttm_vram_fops);
909 debugfs_create_file("radeon_gtt", 0444, root, rdev,
910 &radeon_ttm_gtt_fops);
911 debugfs_create_file("ttm_page_pool", 0444, root, rdev,
912 &radeon_ttm_page_pool_fops);
913 ttm_resource_manager_create_debugfs(ttm_manager_type(&rdev->mman.bdev,
915 root, "radeon_vram_mm");
916 ttm_resource_manager_create_debugfs(ttm_manager_type(&rdev->mman.bdev,
918 root, "radeon_gtt_mm");