Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Jerome Glisse. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 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: |
| 12 | * |
| 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. |
| 20 | * |
| 21 | * The above copyright notice and this permission notice (including the |
| 22 | * next paragraph) shall be included in all copies or substantial portions |
| 23 | * of the Software. |
| 24 | * |
| 25 | */ |
| 26 | /* |
| 27 | * Authors: |
| 28 | * Jerome Glisse <glisse@freedesktop.org> |
| 29 | * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com> |
| 30 | * Dave Airlie |
| 31 | */ |
| 32 | #include <ttm/ttm_bo_api.h> |
| 33 | #include <ttm/ttm_bo_driver.h> |
| 34 | #include <ttm/ttm_placement.h> |
| 35 | #include <ttm/ttm_module.h> |
Pauli Nieminen | 8d7cddc | 2010-04-01 12:44:59 +0000 | [diff] [blame] | 36 | #include <ttm/ttm_page_alloc.h> |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 37 | #include <drm/drmP.h> |
| 38 | #include <drm/radeon_drm.h> |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 39 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
Chris Metcalf | 4cfe762 | 2013-02-01 13:44:33 -0500 | [diff] [blame] | 41 | #include <linux/swiotlb.h> |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 42 | #include <linux/swap.h> |
| 43 | #include <linux/pagemap.h> |
Christian König | 2014b56 | 2013-12-18 21:07:39 +0100 | [diff] [blame] | 44 | #include <linux/debugfs.h> |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 45 | #include "radeon_reg.h" |
| 46 | #include "radeon.h" |
| 47 | |
| 48 | #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT) |
| 49 | |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 50 | static int radeon_ttm_debugfs_init(struct radeon_device *rdev); |
Christian König | 2014b56 | 2013-12-18 21:07:39 +0100 | [diff] [blame] | 51 | static void radeon_ttm_debugfs_fini(struct radeon_device *rdev); |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 52 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 53 | static struct radeon_device *radeon_get_rdev(struct ttm_bo_device *bdev) |
| 54 | { |
| 55 | struct radeon_mman *mman; |
| 56 | struct radeon_device *rdev; |
| 57 | |
| 58 | mman = container_of(bdev, struct radeon_mman, bdev); |
| 59 | rdev = container_of(mman, struct radeon_device, mman); |
| 60 | return rdev; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * Global memory. |
| 66 | */ |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 67 | static int radeon_ttm_mem_global_init(struct drm_global_reference *ref) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 68 | { |
| 69 | return ttm_mem_global_init(ref->object); |
| 70 | } |
| 71 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 72 | static void radeon_ttm_mem_global_release(struct drm_global_reference *ref) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 73 | { |
| 74 | ttm_mem_global_release(ref->object); |
| 75 | } |
| 76 | |
| 77 | static int radeon_ttm_global_init(struct radeon_device *rdev) |
| 78 | { |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 79 | struct drm_global_reference *global_ref; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 80 | int r; |
| 81 | |
| 82 | rdev->mman.mem_global_referenced = false; |
| 83 | global_ref = &rdev->mman.mem_global_ref; |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 84 | global_ref->global_type = DRM_GLOBAL_TTM_MEM; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 85 | global_ref->size = sizeof(struct ttm_mem_global); |
| 86 | global_ref->init = &radeon_ttm_mem_global_init; |
| 87 | global_ref->release = &radeon_ttm_mem_global_release; |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 88 | r = drm_global_item_ref(global_ref); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 89 | if (r != 0) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 90 | DRM_ERROR("Failed setting up TTM memory accounting " |
| 91 | "subsystem.\n"); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 92 | return r; |
| 93 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 94 | |
| 95 | rdev->mman.bo_global_ref.mem_glob = |
| 96 | rdev->mman.mem_global_ref.object; |
| 97 | global_ref = &rdev->mman.bo_global_ref.ref; |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 98 | global_ref->global_type = DRM_GLOBAL_TTM_BO; |
Thomas Hellstrom | 7f5f4db | 2009-08-20 10:29:08 +0200 | [diff] [blame] | 99 | global_ref->size = sizeof(struct ttm_bo_global); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 100 | global_ref->init = &ttm_bo_global_init; |
| 101 | global_ref->release = &ttm_bo_global_release; |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 102 | r = drm_global_item_ref(global_ref); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 103 | if (r != 0) { |
| 104 | DRM_ERROR("Failed setting up TTM BO subsystem.\n"); |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 105 | drm_global_item_unref(&rdev->mman.mem_global_ref); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 106 | return r; |
| 107 | } |
| 108 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 109 | rdev->mman.mem_global_referenced = true; |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static void radeon_ttm_global_fini(struct radeon_device *rdev) |
| 114 | { |
| 115 | if (rdev->mman.mem_global_referenced) { |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 116 | drm_global_item_unref(&rdev->mman.bo_global_ref.ref); |
| 117 | drm_global_item_unref(&rdev->mman.mem_global_ref); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 118 | rdev->mman.mem_global_referenced = false; |
| 119 | } |
| 120 | } |
| 121 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 122 | static int radeon_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) |
| 123 | { |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int radeon_init_mem_type(struct ttm_bo_device *bdev, uint32_t type, |
| 128 | struct ttm_mem_type_manager *man) |
| 129 | { |
| 130 | struct radeon_device *rdev; |
| 131 | |
| 132 | rdev = radeon_get_rdev(bdev); |
| 133 | |
| 134 | switch (type) { |
| 135 | case TTM_PL_SYSTEM: |
| 136 | /* System memory */ |
| 137 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; |
| 138 | man->available_caching = TTM_PL_MASK_CACHING; |
| 139 | man->default_caching = TTM_PL_FLAG_CACHED; |
| 140 | break; |
| 141 | case TTM_PL_TT: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 142 | man->func = &ttm_bo_manager_func; |
Jerome Glisse | d594e46 | 2010-02-17 21:54:29 +0000 | [diff] [blame] | 143 | man->gpu_offset = rdev->mc.gtt_start; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 144 | man->available_caching = TTM_PL_MASK_CACHING; |
| 145 | man->default_caching = TTM_PL_FLAG_CACHED; |
Michel Dänzer | 55c9327 | 2009-06-15 16:56:11 +0200 | [diff] [blame] | 146 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA; |
Daniel Vetter | a7fb8a2 | 2015-09-09 16:45:52 +0200 | [diff] [blame] | 147 | #if IS_ENABLED(CONFIG_AGP) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 148 | if (rdev->flags & RADEON_IS_AGP) { |
Daniel Vetter | d990675 | 2013-12-11 11:34:35 +0100 | [diff] [blame] | 149 | if (!rdev->ddev->agp) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 150 | DRM_ERROR("AGP is not enabled for memory type %u\n", |
| 151 | (unsigned)type); |
| 152 | return -EINVAL; |
| 153 | } |
Michel Dänzer | 55c9327 | 2009-06-15 16:56:11 +0200 | [diff] [blame] | 154 | if (!rdev->ddev->agp->cant_use_aperture) |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 155 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 156 | man->available_caching = TTM_PL_FLAG_UNCACHED | |
| 157 | TTM_PL_FLAG_WC; |
| 158 | man->default_caching = TTM_PL_FLAG_WC; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 159 | } |
Jerome Glisse | 0c321c7 | 2010-04-07 10:21:27 +0000 | [diff] [blame] | 160 | #endif |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 161 | break; |
| 162 | case TTM_PL_VRAM: |
| 163 | /* "On-card" video ram */ |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 164 | man->func = &ttm_bo_manager_func; |
Jerome Glisse | d594e46 | 2010-02-17 21:54:29 +0000 | [diff] [blame] | 165 | man->gpu_offset = rdev->mc.vram_start; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 166 | man->flags = TTM_MEMTYPE_FLAG_FIXED | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 167 | TTM_MEMTYPE_FLAG_MAPPABLE; |
| 168 | man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC; |
| 169 | man->default_caching = TTM_PL_FLAG_WC; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 170 | break; |
| 171 | default: |
| 172 | DRM_ERROR("Unsupported memory type %u\n", (unsigned)type); |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 178 | static void radeon_evict_flags(struct ttm_buffer_object *bo, |
| 179 | struct ttm_placement *placement) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 180 | { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 181 | static struct ttm_place placements = { |
| 182 | .fpfn = 0, |
| 183 | .lpfn = 0, |
| 184 | .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM |
| 185 | }; |
| 186 | |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 187 | struct radeon_bo *rbo; |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 188 | |
| 189 | if (!radeon_ttm_bo_is_radeon_bo(bo)) { |
Jerome Glisse | d03d858 | 2009-12-14 21:02:09 +0100 | [diff] [blame] | 190 | placement->placement = &placements; |
| 191 | placement->busy_placement = &placements; |
| 192 | placement->num_placement = 1; |
| 193 | placement->num_busy_placement = 1; |
| 194 | return; |
| 195 | } |
| 196 | rbo = container_of(bo, struct radeon_bo, tbo); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 197 | switch (bo->mem.mem_type) { |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 198 | case TTM_PL_VRAM: |
Alex Deucher | 5e5c21c | 2014-12-03 00:03:49 -0500 | [diff] [blame] | 199 | if (rbo->rdev->ring[radeon_copy_ring_index(rbo->rdev)].ready == false) |
Dave Airlie | 9270eb1 | 2010-01-13 09:21:49 +1000 | [diff] [blame] | 200 | radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU); |
Michel Dänzer | 2a85aed | 2014-10-09 18:55:04 +0900 | [diff] [blame] | 201 | else if (rbo->rdev->mc.visible_vram_size < rbo->rdev->mc.real_vram_size && |
| 202 | bo->mem.start < (rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT)) { |
| 203 | unsigned fpfn = rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT; |
| 204 | int i; |
| 205 | |
| 206 | /* Try evicting to the CPU inaccessible part of VRAM |
| 207 | * first, but only set GTT as busy placement, so this |
| 208 | * BO will be evicted to GTT rather than causing other |
| 209 | * BOs to be evicted from VRAM |
| 210 | */ |
| 211 | radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM | |
| 212 | RADEON_GEM_DOMAIN_GTT); |
| 213 | rbo->placement.num_busy_placement = 0; |
| 214 | for (i = 0; i < rbo->placement.num_placement; i++) { |
| 215 | if (rbo->placements[i].flags & TTM_PL_FLAG_VRAM) { |
| 216 | if (rbo->placements[0].fpfn < fpfn) |
| 217 | rbo->placements[0].fpfn = fpfn; |
| 218 | } else { |
| 219 | rbo->placement.busy_placement = |
| 220 | &rbo->placements[i]; |
| 221 | rbo->placement.num_busy_placement = 1; |
| 222 | } |
| 223 | } |
| 224 | } else |
Dave Airlie | 9270eb1 | 2010-01-13 09:21:49 +1000 | [diff] [blame] | 225 | radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT); |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 226 | break; |
| 227 | case TTM_PL_TT: |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 228 | default: |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 229 | radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 230 | } |
Jerome Glisse | eaa5fd1 | 2009-12-09 21:57:37 +0100 | [diff] [blame] | 231 | *placement = rbo->placement; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int radeon_verify_access(struct ttm_buffer_object *bo, struct file *filp) |
| 235 | { |
David Herrmann | acb4652 | 2013-08-25 18:28:59 +0200 | [diff] [blame] | 236 | struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo); |
| 237 | |
Jérôme Glisse | b5dcec6 | 2016-04-19 09:07:50 -0400 | [diff] [blame] | 238 | if (radeon_ttm_tt_has_userptr(bo->ttm)) |
| 239 | return -EPERM; |
David Herrmann | d9a1f0b | 2016-09-01 14:48:33 +0200 | [diff] [blame] | 240 | return drm_vma_node_verify_access(&rbo->gem_base.vma_node, |
| 241 | filp->private_data); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void radeon_move_null(struct ttm_buffer_object *bo, |
| 245 | struct ttm_mem_reg *new_mem) |
| 246 | { |
| 247 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 248 | |
| 249 | BUG_ON(old_mem->mm_node != NULL); |
| 250 | *old_mem = *new_mem; |
| 251 | new_mem->mm_node = NULL; |
| 252 | } |
| 253 | |
| 254 | static int radeon_move_blit(struct ttm_buffer_object *bo, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 255 | bool evict, bool no_wait_gpu, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 256 | struct ttm_mem_reg *new_mem, |
| 257 | struct ttm_mem_reg *old_mem) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 258 | { |
| 259 | struct radeon_device *rdev; |
| 260 | uint64_t old_start, new_start; |
Christian König | 876dc9f | 2012-05-08 14:24:01 +0200 | [diff] [blame] | 261 | struct radeon_fence *fence; |
Christian König | 57d20a4 | 2014-09-04 20:01:53 +0200 | [diff] [blame] | 262 | unsigned num_pages; |
Christian König | 876dc9f | 2012-05-08 14:24:01 +0200 | [diff] [blame] | 263 | int r, ridx; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 264 | |
| 265 | rdev = radeon_get_rdev(bo->bdev); |
Christian König | 876dc9f | 2012-05-08 14:24:01 +0200 | [diff] [blame] | 266 | ridx = radeon_copy_ring_index(rdev); |
Christian König | 13f479b | 2016-08-17 09:46:42 +0200 | [diff] [blame] | 267 | old_start = (u64)old_mem->start << PAGE_SHIFT; |
| 268 | new_start = (u64)new_mem->start << PAGE_SHIFT; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 269 | |
| 270 | switch (old_mem->mem_type) { |
| 271 | case TTM_PL_VRAM: |
Jerome Glisse | d594e46 | 2010-02-17 21:54:29 +0000 | [diff] [blame] | 272 | old_start += rdev->mc.vram_start; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 273 | break; |
| 274 | case TTM_PL_TT: |
Jerome Glisse | d594e46 | 2010-02-17 21:54:29 +0000 | [diff] [blame] | 275 | old_start += rdev->mc.gtt_start; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 276 | break; |
| 277 | default: |
| 278 | DRM_ERROR("Unknown placement %d\n", old_mem->mem_type); |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | switch (new_mem->mem_type) { |
| 282 | case TTM_PL_VRAM: |
Jerome Glisse | d594e46 | 2010-02-17 21:54:29 +0000 | [diff] [blame] | 283 | new_start += rdev->mc.vram_start; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 284 | break; |
| 285 | case TTM_PL_TT: |
Jerome Glisse | d594e46 | 2010-02-17 21:54:29 +0000 | [diff] [blame] | 286 | new_start += rdev->mc.gtt_start; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 287 | break; |
| 288 | default: |
| 289 | DRM_ERROR("Unknown placement %d\n", old_mem->mem_type); |
| 290 | return -EINVAL; |
| 291 | } |
Christian König | 876dc9f | 2012-05-08 14:24:01 +0200 | [diff] [blame] | 292 | if (!rdev->ring[ridx].ready) { |
Alex Deucher | 3000bf3 | 2012-01-05 22:11:07 -0500 | [diff] [blame] | 293 | DRM_ERROR("Trying to move memory with ring turned off.\n"); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 294 | return -EINVAL; |
| 295 | } |
Alex Deucher | 003cefe | 2011-09-16 12:04:08 -0400 | [diff] [blame] | 296 | |
| 297 | BUILD_BUG_ON((PAGE_SIZE % RADEON_GPU_PAGE_SIZE) != 0); |
| 298 | |
Christian König | 57d20a4 | 2014-09-04 20:01:53 +0200 | [diff] [blame] | 299 | num_pages = new_mem->num_pages * (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); |
| 300 | fence = radeon_copy(rdev, old_start, new_start, num_pages, bo->resv); |
| 301 | if (IS_ERR(fence)) |
| 302 | return PTR_ERR(fence); |
| 303 | |
Christian König | 74561cd | 2016-06-15 13:44:00 +0200 | [diff] [blame] | 304 | r = ttm_bo_move_accel_cleanup(bo, &fence->base, evict, new_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 305 | radeon_fence_unref(&fence); |
| 306 | return r; |
| 307 | } |
| 308 | |
| 309 | static int radeon_move_vram_ram(struct ttm_buffer_object *bo, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 310 | bool evict, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 311 | bool no_wait_gpu, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 312 | struct ttm_mem_reg *new_mem) |
| 313 | { |
| 314 | struct radeon_device *rdev; |
| 315 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 316 | struct ttm_mem_reg tmp_mem; |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 317 | struct ttm_place placements; |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 318 | struct ttm_placement placement; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 319 | int r; |
| 320 | |
| 321 | rdev = radeon_get_rdev(bo->bdev); |
| 322 | tmp_mem = *new_mem; |
| 323 | tmp_mem.mm_node = NULL; |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 324 | placement.num_placement = 1; |
| 325 | placement.placement = &placements; |
| 326 | placement.num_busy_placement = 1; |
| 327 | placement.busy_placement = &placements; |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 328 | placements.fpfn = 0; |
| 329 | placements.lpfn = 0; |
| 330 | placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT; |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 331 | r = ttm_bo_mem_space(bo, &placement, &tmp_mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 332 | interruptible, no_wait_gpu); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 333 | if (unlikely(r)) { |
| 334 | return r; |
| 335 | } |
Dave Airlie | df67bed | 2009-10-30 13:31:26 +1000 | [diff] [blame] | 336 | |
| 337 | r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement); |
| 338 | if (unlikely(r)) { |
| 339 | goto out_cleanup; |
| 340 | } |
| 341 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 342 | r = ttm_tt_bind(bo->ttm, &tmp_mem); |
| 343 | if (unlikely(r)) { |
| 344 | goto out_cleanup; |
| 345 | } |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 346 | r = radeon_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 347 | if (unlikely(r)) { |
| 348 | goto out_cleanup; |
| 349 | } |
Michel Dänzer | 4e2f0ca | 2016-08-08 12:28:25 +0900 | [diff] [blame] | 350 | r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, new_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 351 | out_cleanup: |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 352 | ttm_bo_mem_put(bo, &tmp_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 353 | return r; |
| 354 | } |
| 355 | |
| 356 | static int radeon_move_ram_vram(struct ttm_buffer_object *bo, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 357 | bool evict, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 358 | bool no_wait_gpu, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 359 | struct ttm_mem_reg *new_mem) |
| 360 | { |
| 361 | struct radeon_device *rdev; |
| 362 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 363 | struct ttm_mem_reg tmp_mem; |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 364 | struct ttm_placement placement; |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 365 | struct ttm_place placements; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 366 | int r; |
| 367 | |
| 368 | rdev = radeon_get_rdev(bo->bdev); |
| 369 | tmp_mem = *new_mem; |
| 370 | tmp_mem.mm_node = NULL; |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 371 | placement.num_placement = 1; |
| 372 | placement.placement = &placements; |
| 373 | placement.num_busy_placement = 1; |
| 374 | placement.busy_placement = &placements; |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 375 | placements.fpfn = 0; |
| 376 | placements.lpfn = 0; |
| 377 | placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 378 | r = ttm_bo_mem_space(bo, &placement, &tmp_mem, |
| 379 | interruptible, no_wait_gpu); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 380 | if (unlikely(r)) { |
| 381 | return r; |
| 382 | } |
Michel Dänzer | 4e2f0ca | 2016-08-08 12:28:25 +0900 | [diff] [blame] | 383 | r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, &tmp_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 384 | if (unlikely(r)) { |
| 385 | goto out_cleanup; |
| 386 | } |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 387 | r = radeon_move_blit(bo, true, no_wait_gpu, new_mem, old_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 388 | if (unlikely(r)) { |
| 389 | goto out_cleanup; |
| 390 | } |
| 391 | out_cleanup: |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 392 | ttm_bo_mem_put(bo, &tmp_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 393 | return r; |
| 394 | } |
| 395 | |
| 396 | static int radeon_bo_move(struct ttm_buffer_object *bo, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 397 | bool evict, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 398 | bool no_wait_gpu, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 399 | struct ttm_mem_reg *new_mem) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 400 | { |
| 401 | struct radeon_device *rdev; |
Michel Dänzer | e1a575a | 2016-03-28 16:39:14 +0900 | [diff] [blame] | 402 | struct radeon_bo *rbo; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 403 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 404 | int r; |
| 405 | |
Christian König | 88932a7 | 2016-06-06 10:17:53 +0200 | [diff] [blame] | 406 | r = ttm_bo_wait(bo, interruptible, no_wait_gpu); |
| 407 | if (r) |
| 408 | return r; |
| 409 | |
Michel Dänzer | e1a575a | 2016-03-28 16:39:14 +0900 | [diff] [blame] | 410 | /* Can't move a pinned BO */ |
| 411 | rbo = container_of(bo, struct radeon_bo, tbo); |
| 412 | if (WARN_ON_ONCE(rbo->pin_count > 0)) |
| 413 | return -EINVAL; |
| 414 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 415 | rdev = radeon_get_rdev(bo->bdev); |
| 416 | if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
| 417 | radeon_move_null(bo, new_mem); |
| 418 | return 0; |
| 419 | } |
| 420 | if ((old_mem->mem_type == TTM_PL_TT && |
| 421 | new_mem->mem_type == TTM_PL_SYSTEM) || |
| 422 | (old_mem->mem_type == TTM_PL_SYSTEM && |
| 423 | new_mem->mem_type == TTM_PL_TT)) { |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 424 | /* bind is enough */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 425 | radeon_move_null(bo, new_mem); |
| 426 | return 0; |
| 427 | } |
Alex Deucher | 27cd776 | 2012-02-23 17:53:42 -0500 | [diff] [blame] | 428 | if (!rdev->ring[radeon_copy_ring_index(rdev)].ready || |
| 429 | rdev->asic->copy.copy == NULL) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 430 | /* use memcpy */ |
Michel Dänzer | 1ab2e10 | 2009-07-28 12:30:56 +0200 | [diff] [blame] | 431 | goto memcpy; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | if (old_mem->mem_type == TTM_PL_VRAM && |
| 435 | new_mem->mem_type == TTM_PL_SYSTEM) { |
Michel Dänzer | 1ab2e10 | 2009-07-28 12:30:56 +0200 | [diff] [blame] | 436 | r = radeon_move_vram_ram(bo, evict, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 437 | no_wait_gpu, new_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 438 | } else if (old_mem->mem_type == TTM_PL_SYSTEM && |
| 439 | new_mem->mem_type == TTM_PL_VRAM) { |
Michel Dänzer | 1ab2e10 | 2009-07-28 12:30:56 +0200 | [diff] [blame] | 440 | r = radeon_move_ram_vram(bo, evict, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 441 | no_wait_gpu, new_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 442 | } else { |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 443 | r = radeon_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 444 | } |
Michel Dänzer | 1ab2e10 | 2009-07-28 12:30:56 +0200 | [diff] [blame] | 445 | |
| 446 | if (r) { |
| 447 | memcpy: |
Michel Dänzer | 4499f2a | 2016-08-08 12:28:26 +0900 | [diff] [blame] | 448 | r = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, new_mem); |
Marek Olšák | 67e8e3f | 2014-03-02 00:56:18 +0100 | [diff] [blame] | 449 | if (r) { |
| 450 | return r; |
| 451 | } |
Michel Dänzer | 1ab2e10 | 2009-07-28 12:30:56 +0200 | [diff] [blame] | 452 | } |
Marek Olšák | 67e8e3f | 2014-03-02 00:56:18 +0100 | [diff] [blame] | 453 | |
| 454 | /* update statistics */ |
| 455 | atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &rdev->num_bytes_moved); |
| 456 | return 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 459 | static int radeon_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 460 | { |
| 461 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 462 | struct radeon_device *rdev = radeon_get_rdev(bdev); |
| 463 | |
| 464 | mem->bus.addr = NULL; |
| 465 | mem->bus.offset = 0; |
| 466 | mem->bus.size = mem->num_pages << PAGE_SHIFT; |
| 467 | mem->bus.base = 0; |
| 468 | mem->bus.is_iomem = false; |
| 469 | if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) |
| 470 | return -EINVAL; |
| 471 | switch (mem->mem_type) { |
| 472 | case TTM_PL_SYSTEM: |
| 473 | /* system memory */ |
| 474 | return 0; |
| 475 | case TTM_PL_TT: |
Daniel Vetter | a7fb8a2 | 2015-09-09 16:45:52 +0200 | [diff] [blame] | 476 | #if IS_ENABLED(CONFIG_AGP) |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 477 | if (rdev->flags & RADEON_IS_AGP) { |
| 478 | /* RADEON_IS_AGP is set only if AGP is active */ |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 479 | mem->bus.offset = mem->start << PAGE_SHIFT; |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 480 | mem->bus.base = rdev->mc.agp_base; |
Michel Dänzer | 365048f | 2010-05-19 12:46:22 +0200 | [diff] [blame] | 481 | mem->bus.is_iomem = !rdev->ddev->agp->cant_use_aperture; |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 482 | } |
| 483 | #endif |
| 484 | break; |
| 485 | case TTM_PL_VRAM: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 486 | mem->bus.offset = mem->start << PAGE_SHIFT; |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 487 | /* check if it's visible */ |
| 488 | if ((mem->bus.offset + mem->bus.size) > rdev->mc.visible_vram_size) |
| 489 | return -EINVAL; |
| 490 | mem->bus.base = rdev->mc.aper_base; |
| 491 | mem->bus.is_iomem = true; |
Jay Estabrook | ffb57c4 | 2011-07-06 23:57:13 +0000 | [diff] [blame] | 492 | #ifdef __alpha__ |
| 493 | /* |
| 494 | * Alpha: use bus.addr to hold the ioremap() return, |
| 495 | * so we can modify bus.base below. |
| 496 | */ |
| 497 | if (mem->placement & TTM_PL_FLAG_WC) |
| 498 | mem->bus.addr = |
| 499 | ioremap_wc(mem->bus.base + mem->bus.offset, |
| 500 | mem->bus.size); |
| 501 | else |
| 502 | mem->bus.addr = |
| 503 | ioremap_nocache(mem->bus.base + mem->bus.offset, |
| 504 | mem->bus.size); |
| 505 | |
| 506 | /* |
| 507 | * Alpha: Use just the bus offset plus |
| 508 | * the hose/domain memory base for bus.base. |
| 509 | * It then can be used to build PTEs for VRAM |
| 510 | * access, as done in ttm_bo_vm_fault(). |
| 511 | */ |
| 512 | mem->bus.base = (mem->bus.base & 0x0ffffffffUL) + |
| 513 | rdev->ddev->hose->dense_mem_base; |
| 514 | #endif |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 515 | break; |
| 516 | default: |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static void radeon_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 523 | { |
| 524 | } |
| 525 | |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 526 | /* |
| 527 | * TTM backend functions. |
| 528 | */ |
| 529 | struct radeon_ttm_tt { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 530 | struct ttm_dma_tt ttm; |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 531 | struct radeon_device *rdev; |
| 532 | u64 offset; |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 533 | |
| 534 | uint64_t userptr; |
| 535 | struct mm_struct *usermm; |
| 536 | uint32_t userflags; |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 537 | }; |
| 538 | |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 539 | /* prepare the sg table with the user pages */ |
| 540 | static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm) |
| 541 | { |
| 542 | struct radeon_device *rdev = radeon_get_rdev(ttm->bdev); |
| 543 | struct radeon_ttm_tt *gtt = (void *)ttm; |
| 544 | unsigned pinned = 0, nents; |
| 545 | int r; |
| 546 | |
| 547 | int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY); |
| 548 | enum dma_data_direction direction = write ? |
| 549 | DMA_BIDIRECTIONAL : DMA_TO_DEVICE; |
| 550 | |
| 551 | if (current->mm != gtt->usermm) |
| 552 | return -EPERM; |
| 553 | |
Christian König | ddd00e3 | 2014-08-07 09:36:01 +0200 | [diff] [blame] | 554 | if (gtt->userflags & RADEON_GEM_USERPTR_ANONONLY) { |
| 555 | /* check that we only pin down anonymous memory |
| 556 | to prevent problems with writeback */ |
| 557 | unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE; |
| 558 | struct vm_area_struct *vma; |
| 559 | vma = find_vma(gtt->usermm, gtt->userptr); |
| 560 | if (!vma || vma->vm_file || vma->vm_end < end) |
| 561 | return -EPERM; |
| 562 | } |
| 563 | |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 564 | do { |
| 565 | unsigned num_pages = ttm->num_pages - pinned; |
| 566 | uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE; |
| 567 | struct page **pages = ttm->pages + pinned; |
| 568 | |
Lorenzo Stoakes | 768ae30 | 2016-10-13 01:20:16 +0100 | [diff] [blame] | 569 | r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0, |
| 570 | pages, NULL); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 571 | if (r < 0) |
| 572 | goto release_pages; |
| 573 | |
| 574 | pinned += r; |
| 575 | |
| 576 | } while (pinned < ttm->num_pages); |
| 577 | |
| 578 | r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0, |
| 579 | ttm->num_pages << PAGE_SHIFT, |
| 580 | GFP_KERNEL); |
| 581 | if (r) |
| 582 | goto release_sg; |
| 583 | |
| 584 | r = -ENOMEM; |
| 585 | nents = dma_map_sg(rdev->dev, ttm->sg->sgl, ttm->sg->nents, direction); |
| 586 | if (nents != ttm->sg->nents) |
| 587 | goto release_sg; |
| 588 | |
| 589 | drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, |
| 590 | gtt->ttm.dma_address, ttm->num_pages); |
| 591 | |
| 592 | return 0; |
| 593 | |
| 594 | release_sg: |
| 595 | kfree(ttm->sg); |
| 596 | |
| 597 | release_pages: |
| 598 | release_pages(ttm->pages, pinned, 0); |
| 599 | return r; |
| 600 | } |
| 601 | |
| 602 | static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm) |
| 603 | { |
| 604 | struct radeon_device *rdev = radeon_get_rdev(ttm->bdev); |
| 605 | struct radeon_ttm_tt *gtt = (void *)ttm; |
monk.liu | db12973 | 2015-05-05 09:24:17 +0200 | [diff] [blame] | 606 | struct sg_page_iter sg_iter; |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 607 | |
| 608 | int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY); |
| 609 | enum dma_data_direction direction = write ? |
| 610 | DMA_BIDIRECTIONAL : DMA_TO_DEVICE; |
| 611 | |
Christian König | 863653f | 2015-03-31 17:36:57 +0200 | [diff] [blame] | 612 | /* double check that we don't free the table twice */ |
| 613 | if (!ttm->sg->sgl) |
| 614 | return; |
| 615 | |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 616 | /* free the sg table and pages again */ |
| 617 | dma_unmap_sg(rdev->dev, ttm->sg->sgl, ttm->sg->nents, direction); |
| 618 | |
monk.liu | db12973 | 2015-05-05 09:24:17 +0200 | [diff] [blame] | 619 | for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) { |
| 620 | struct page *page = sg_page_iter_page(&sg_iter); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 621 | if (!(gtt->userflags & RADEON_GEM_USERPTR_READONLY)) |
| 622 | set_page_dirty(page); |
| 623 | |
| 624 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 625 | put_page(page); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | sg_free_table(ttm->sg); |
| 629 | } |
| 630 | |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 631 | static int radeon_ttm_backend_bind(struct ttm_tt *ttm, |
| 632 | struct ttm_mem_reg *bo_mem) |
| 633 | { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 634 | struct radeon_ttm_tt *gtt = (void*)ttm; |
Michel Dänzer | 77497f2 | 2014-07-17 19:01:07 +0900 | [diff] [blame] | 635 | uint32_t flags = RADEON_GART_PAGE_VALID | RADEON_GART_PAGE_READ | |
| 636 | RADEON_GART_PAGE_WRITE; |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 637 | int r; |
| 638 | |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 639 | if (gtt->userptr) { |
| 640 | radeon_ttm_tt_pin_userptr(ttm); |
| 641 | flags &= ~RADEON_GART_PAGE_WRITE; |
| 642 | } |
| 643 | |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 644 | gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT); |
| 645 | if (!ttm->num_pages) { |
| 646 | WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n", |
| 647 | ttm->num_pages, bo_mem, ttm); |
| 648 | } |
Michel Dänzer | 77497f2 | 2014-07-17 19:01:07 +0900 | [diff] [blame] | 649 | if (ttm->caching_state == tt_cached) |
| 650 | flags |= RADEON_GART_PAGE_SNOOP; |
| 651 | r = radeon_gart_bind(gtt->rdev, gtt->offset, ttm->num_pages, |
| 652 | ttm->pages, gtt->ttm.dma_address, flags); |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 653 | if (r) { |
| 654 | DRM_ERROR("failed to bind %lu pages at 0x%08X\n", |
| 655 | ttm->num_pages, (unsigned)gtt->offset); |
| 656 | return r; |
| 657 | } |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int radeon_ttm_backend_unbind(struct ttm_tt *ttm) |
| 662 | { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 663 | struct radeon_ttm_tt *gtt = (void *)ttm; |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 664 | |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 665 | radeon_gart_unbind(gtt->rdev, gtt->offset, ttm->num_pages); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 666 | |
| 667 | if (gtt->userptr) |
| 668 | radeon_ttm_tt_unpin_userptr(ttm); |
| 669 | |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static void radeon_ttm_backend_destroy(struct ttm_tt *ttm) |
| 674 | { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 675 | struct radeon_ttm_tt *gtt = (void *)ttm; |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 676 | |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 677 | ttm_dma_tt_fini(>t->ttm); |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 678 | kfree(gtt); |
| 679 | } |
| 680 | |
| 681 | static struct ttm_backend_func radeon_backend_func = { |
| 682 | .bind = &radeon_ttm_backend_bind, |
| 683 | .unbind = &radeon_ttm_backend_unbind, |
| 684 | .destroy = &radeon_ttm_backend_destroy, |
| 685 | }; |
| 686 | |
Lauri Kasanen | 1109ca0 | 2012-08-31 13:43:50 -0400 | [diff] [blame] | 687 | static struct ttm_tt *radeon_ttm_tt_create(struct ttm_bo_device *bdev, |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 688 | unsigned long size, uint32_t page_flags, |
| 689 | struct page *dummy_read_page) |
| 690 | { |
| 691 | struct radeon_device *rdev; |
| 692 | struct radeon_ttm_tt *gtt; |
| 693 | |
| 694 | rdev = radeon_get_rdev(bdev); |
Daniel Vetter | a7fb8a2 | 2015-09-09 16:45:52 +0200 | [diff] [blame] | 695 | #if IS_ENABLED(CONFIG_AGP) |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 696 | if (rdev->flags & RADEON_IS_AGP) { |
| 697 | return ttm_agp_tt_create(bdev, rdev->ddev->agp->bridge, |
| 698 | size, page_flags, dummy_read_page); |
| 699 | } |
| 700 | #endif |
| 701 | |
| 702 | gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL); |
| 703 | if (gtt == NULL) { |
| 704 | return NULL; |
| 705 | } |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 706 | gtt->ttm.ttm.func = &radeon_backend_func; |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 707 | gtt->rdev = rdev; |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 708 | if (ttm_dma_tt_init(>t->ttm, bdev, size, page_flags, dummy_read_page)) { |
| 709 | kfree(gtt); |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 710 | return NULL; |
| 711 | } |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 712 | return >t->ttm.ttm; |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Christian König | 3840a65 | 2014-09-17 04:00:05 -0600 | [diff] [blame] | 715 | static struct radeon_ttm_tt *radeon_ttm_tt_to_gtt(struct ttm_tt *ttm) |
| 716 | { |
| 717 | if (!ttm || ttm->func != &radeon_backend_func) |
| 718 | return NULL; |
| 719 | return (struct radeon_ttm_tt *)ttm; |
| 720 | } |
| 721 | |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 722 | static int radeon_ttm_tt_populate(struct ttm_tt *ttm) |
| 723 | { |
Christian König | 3840a65 | 2014-09-17 04:00:05 -0600 | [diff] [blame] | 724 | struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 725 | struct radeon_device *rdev; |
| 726 | unsigned i; |
| 727 | int r; |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 728 | bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 729 | |
| 730 | if (ttm->state != tt_unpopulated) |
| 731 | return 0; |
| 732 | |
Christian König | 3840a65 | 2014-09-17 04:00:05 -0600 | [diff] [blame] | 733 | if (gtt && gtt->userptr) { |
Maninder Singh | 69ee241 | 2015-06-19 09:35:23 +0530 | [diff] [blame] | 734 | ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 735 | if (!ttm->sg) |
| 736 | return -ENOMEM; |
| 737 | |
| 738 | ttm->page_flags |= TTM_PAGE_FLAG_SG; |
| 739 | ttm->state = tt_unbound; |
| 740 | return 0; |
| 741 | } |
| 742 | |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 743 | if (slave && ttm->sg) { |
| 744 | drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, |
| 745 | gtt->ttm.dma_address, ttm->num_pages); |
| 746 | ttm->state = tt_unbound; |
| 747 | return 0; |
| 748 | } |
| 749 | |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 750 | rdev = radeon_get_rdev(ttm->bdev); |
Daniel Vetter | a7fb8a2 | 2015-09-09 16:45:52 +0200 | [diff] [blame] | 751 | #if IS_ENABLED(CONFIG_AGP) |
Jerome Glisse | dea7e0a | 2012-01-03 17:37:37 -0500 | [diff] [blame] | 752 | if (rdev->flags & RADEON_IS_AGP) { |
| 753 | return ttm_agp_tt_populate(ttm); |
| 754 | } |
| 755 | #endif |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 756 | |
| 757 | #ifdef CONFIG_SWIOTLB |
| 758 | if (swiotlb_nr_tbl()) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 759 | return ttm_dma_populate(>t->ttm, rdev->dev); |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 760 | } |
| 761 | #endif |
| 762 | |
| 763 | r = ttm_pool_populate(ttm); |
| 764 | if (r) { |
| 765 | return r; |
| 766 | } |
| 767 | |
| 768 | for (i = 0; i < ttm->num_pages; i++) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 769 | gtt->ttm.dma_address[i] = pci_map_page(rdev->pdev, ttm->pages[i], |
| 770 | 0, PAGE_SIZE, |
| 771 | PCI_DMA_BIDIRECTIONAL); |
| 772 | if (pci_dma_mapping_error(rdev->pdev, gtt->ttm.dma_address[i])) { |
Rasmus Villemoes | bc3f5d8 | 2016-02-15 19:41:47 +0100 | [diff] [blame] | 773 | while (i--) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 774 | pci_unmap_page(rdev->pdev, gtt->ttm.dma_address[i], |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 775 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 776 | gtt->ttm.dma_address[i] = 0; |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 777 | } |
| 778 | ttm_pool_unpopulate(ttm); |
| 779 | return -EFAULT; |
| 780 | } |
| 781 | } |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static void radeon_ttm_tt_unpopulate(struct ttm_tt *ttm) |
| 786 | { |
| 787 | struct radeon_device *rdev; |
Christian König | 3840a65 | 2014-09-17 04:00:05 -0600 | [diff] [blame] | 788 | struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 789 | unsigned i; |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 790 | bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); |
| 791 | |
Christian König | 3840a65 | 2014-09-17 04:00:05 -0600 | [diff] [blame] | 792 | if (gtt && gtt->userptr) { |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 793 | kfree(ttm->sg); |
| 794 | ttm->page_flags &= ~TTM_PAGE_FLAG_SG; |
| 795 | return; |
| 796 | } |
| 797 | |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 798 | if (slave) |
| 799 | return; |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 800 | |
| 801 | rdev = radeon_get_rdev(ttm->bdev); |
Daniel Vetter | a7fb8a2 | 2015-09-09 16:45:52 +0200 | [diff] [blame] | 802 | #if IS_ENABLED(CONFIG_AGP) |
Jerome Glisse | dea7e0a | 2012-01-03 17:37:37 -0500 | [diff] [blame] | 803 | if (rdev->flags & RADEON_IS_AGP) { |
| 804 | ttm_agp_tt_unpopulate(ttm); |
| 805 | return; |
| 806 | } |
| 807 | #endif |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 808 | |
| 809 | #ifdef CONFIG_SWIOTLB |
| 810 | if (swiotlb_nr_tbl()) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 811 | ttm_dma_unpopulate(>t->ttm, rdev->dev); |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 812 | return; |
| 813 | } |
| 814 | #endif |
| 815 | |
| 816 | for (i = 0; i < ttm->num_pages; i++) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 817 | if (gtt->ttm.dma_address[i]) { |
| 818 | pci_unmap_page(rdev->pdev, gtt->ttm.dma_address[i], |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 819 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | ttm_pool_unpopulate(ttm); |
| 824 | } |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 825 | |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 826 | int radeon_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr, |
| 827 | uint32_t flags) |
| 828 | { |
Christian König | 3840a65 | 2014-09-17 04:00:05 -0600 | [diff] [blame] | 829 | struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 830 | |
| 831 | if (gtt == NULL) |
| 832 | return -EINVAL; |
| 833 | |
| 834 | gtt->userptr = addr; |
| 835 | gtt->usermm = current->mm; |
| 836 | gtt->userflags = flags; |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | bool radeon_ttm_tt_has_userptr(struct ttm_tt *ttm) |
| 841 | { |
Christian König | 3840a65 | 2014-09-17 04:00:05 -0600 | [diff] [blame] | 842 | struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 843 | |
| 844 | if (gtt == NULL) |
| 845 | return false; |
| 846 | |
| 847 | return !!gtt->userptr; |
| 848 | } |
| 849 | |
| 850 | bool radeon_ttm_tt_is_readonly(struct ttm_tt *ttm) |
| 851 | { |
Christian König | 3840a65 | 2014-09-17 04:00:05 -0600 | [diff] [blame] | 852 | struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 853 | |
| 854 | if (gtt == NULL) |
| 855 | return false; |
| 856 | |
| 857 | return !!(gtt->userflags & RADEON_GEM_USERPTR_READONLY); |
| 858 | } |
| 859 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 860 | static struct ttm_bo_driver radeon_bo_driver = { |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 861 | .ttm_tt_create = &radeon_ttm_tt_create, |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 862 | .ttm_tt_populate = &radeon_ttm_tt_populate, |
| 863 | .ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 864 | .invalidate_caches = &radeon_invalidate_caches, |
| 865 | .init_mem_type = &radeon_init_mem_type, |
| 866 | .evict_flags = &radeon_evict_flags, |
| 867 | .move = &radeon_bo_move, |
| 868 | .verify_access = &radeon_verify_access, |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 869 | .move_notify = &radeon_bo_move_notify, |
| 870 | .fault_reserve_notify = &radeon_bo_fault_reserve_notify, |
Jerome Glisse | 0a2d50e | 2010-04-09 14:39:24 +0200 | [diff] [blame] | 871 | .io_mem_reserve = &radeon_ttm_io_mem_reserve, |
| 872 | .io_mem_free = &radeon_ttm_io_mem_free, |
Christian König | 98c2872 | 2016-04-06 11:12:07 +0200 | [diff] [blame] | 873 | .lru_tail = &ttm_bo_default_lru_tail, |
| 874 | .swap_lru_tail = &ttm_bo_default_swap_lru_tail, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 875 | }; |
| 876 | |
| 877 | int radeon_ttm_init(struct radeon_device *rdev) |
| 878 | { |
| 879 | int r; |
| 880 | |
| 881 | r = radeon_ttm_global_init(rdev); |
| 882 | if (r) { |
| 883 | return r; |
| 884 | } |
| 885 | /* No others user of address space so set it to 0 */ |
| 886 | r = ttm_bo_device_init(&rdev->mman.bdev, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 887 | rdev->mman.bo_global_ref.ref.object, |
David Herrmann | 44d847b | 2013-08-13 19:10:30 +0200 | [diff] [blame] | 888 | &radeon_bo_driver, |
| 889 | rdev->ddev->anon_inode->i_mapping, |
| 890 | DRM_FILE_PAGE_OFFSET, |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 891 | rdev->need_dma32); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 892 | if (r) { |
| 893 | DRM_ERROR("failed initializing buffer object driver(%d).\n", r); |
| 894 | return r; |
| 895 | } |
Jerome Glisse | 0a0c759 | 2009-12-11 20:36:19 +0100 | [diff] [blame] | 896 | rdev->mman.initialized = true; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 897 | r = ttm_bo_init_mm(&rdev->mman.bdev, TTM_PL_VRAM, |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 898 | rdev->mc.real_vram_size >> PAGE_SHIFT); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 899 | if (r) { |
| 900 | DRM_ERROR("Failed initializing VRAM heap.\n"); |
| 901 | return r; |
| 902 | } |
Lauri Kasanen | 14eedc3 | 2014-02-28 20:50:23 +0200 | [diff] [blame] | 903 | /* Change the size here instead of the init above so only lpfn is affected */ |
| 904 | radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size); |
| 905 | |
Daniel Vetter | 441921d | 2011-02-18 17:59:16 +0100 | [diff] [blame] | 906 | r = radeon_bo_create(rdev, 256 * 1024, PAGE_SIZE, true, |
Maarten Lankhorst | 831b696 | 2014-09-18 14:11:56 +0200 | [diff] [blame] | 907 | RADEON_GEM_DOMAIN_VRAM, 0, NULL, |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 908 | NULL, &rdev->stollen_vga_memory); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 909 | if (r) { |
| 910 | return r; |
| 911 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 912 | r = radeon_bo_reserve(rdev->stollen_vga_memory, false); |
| 913 | if (r) |
| 914 | return r; |
| 915 | r = radeon_bo_pin(rdev->stollen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL); |
| 916 | radeon_bo_unreserve(rdev->stollen_vga_memory); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 917 | if (r) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 918 | radeon_bo_unref(&rdev->stollen_vga_memory); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 919 | return r; |
| 920 | } |
| 921 | DRM_INFO("radeon: %uM of VRAM memory ready\n", |
Niels Ole Salscheider | fc98603 | 2013-05-18 21:19:23 +0200 | [diff] [blame] | 922 | (unsigned) (rdev->mc.real_vram_size / (1024 * 1024))); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 923 | r = ttm_bo_init_mm(&rdev->mman.bdev, TTM_PL_TT, |
Jerome Glisse | 312ea8d | 2009-12-07 15:52:58 +0100 | [diff] [blame] | 924 | rdev->mc.gtt_size >> PAGE_SHIFT); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 925 | if (r) { |
| 926 | DRM_ERROR("Failed initializing GTT heap.\n"); |
| 927 | return r; |
| 928 | } |
| 929 | DRM_INFO("radeon: %uM of GTT memory ready.\n", |
Jerome Glisse | 3ce0a23 | 2009-09-08 10:10:24 +1000 | [diff] [blame] | 930 | (unsigned)(rdev->mc.gtt_size / (1024 * 1024))); |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 931 | |
| 932 | r = radeon_ttm_debugfs_init(rdev); |
| 933 | if (r) { |
| 934 | DRM_ERROR("Failed to init debugfs\n"); |
| 935 | return r; |
| 936 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | void radeon_ttm_fini(struct radeon_device *rdev) |
| 941 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 942 | int r; |
| 943 | |
Jerome Glisse | 0a0c759 | 2009-12-11 20:36:19 +0100 | [diff] [blame] | 944 | if (!rdev->mman.initialized) |
| 945 | return; |
Christian König | 2014b56 | 2013-12-18 21:07:39 +0100 | [diff] [blame] | 946 | radeon_ttm_debugfs_fini(rdev); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 947 | if (rdev->stollen_vga_memory) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 948 | r = radeon_bo_reserve(rdev->stollen_vga_memory, false); |
| 949 | if (r == 0) { |
| 950 | radeon_bo_unpin(rdev->stollen_vga_memory); |
| 951 | radeon_bo_unreserve(rdev->stollen_vga_memory); |
| 952 | } |
| 953 | radeon_bo_unref(&rdev->stollen_vga_memory); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 954 | } |
| 955 | ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_VRAM); |
| 956 | ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_TT); |
| 957 | ttm_bo_device_release(&rdev->mman.bdev); |
| 958 | radeon_gart_fini(rdev); |
| 959 | radeon_ttm_global_fini(rdev); |
Jerome Glisse | 0a0c759 | 2009-12-11 20:36:19 +0100 | [diff] [blame] | 960 | rdev->mman.initialized = false; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 961 | DRM_INFO("radeon: ttm finalized\n"); |
| 962 | } |
| 963 | |
Dave Airlie | 5359533 | 2011-03-14 09:47:24 +1000 | [diff] [blame] | 964 | /* this should only be called at bootup or when userspace |
| 965 | * isn't running */ |
| 966 | void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size) |
| 967 | { |
| 968 | struct ttm_mem_type_manager *man; |
| 969 | |
| 970 | if (!rdev->mman.initialized) |
| 971 | return; |
| 972 | |
| 973 | man = &rdev->mman.bdev.man[TTM_PL_VRAM]; |
| 974 | /* this just adjusts TTM size idea, which sets lpfn to the correct value */ |
| 975 | man->size = size >> PAGE_SHIFT; |
| 976 | } |
| 977 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 978 | static struct vm_operations_struct radeon_ttm_vm_ops; |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 979 | static const struct vm_operations_struct *ttm_vm_ops = NULL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 980 | |
| 981 | static int radeon_ttm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 982 | { |
| 983 | struct ttm_buffer_object *bo; |
Matthew Garrett | 5876dd2 | 2010-04-26 15:52:20 -0400 | [diff] [blame] | 984 | struct radeon_device *rdev; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 985 | int r; |
| 986 | |
Matthew Garrett | 5876dd2 | 2010-04-26 15:52:20 -0400 | [diff] [blame] | 987 | bo = (struct ttm_buffer_object *)vma->vm_private_data; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 988 | if (bo == NULL) { |
| 989 | return VM_FAULT_NOPAGE; |
| 990 | } |
Matthew Garrett | 5876dd2 | 2010-04-26 15:52:20 -0400 | [diff] [blame] | 991 | rdev = radeon_get_rdev(bo->bdev); |
Christian König | db7fce3 | 2012-05-11 14:57:18 +0200 | [diff] [blame] | 992 | down_read(&rdev->pm.mclk_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 993 | r = ttm_vm_ops->fault(vma, vmf); |
Christian König | db7fce3 | 2012-05-11 14:57:18 +0200 | [diff] [blame] | 994 | up_read(&rdev->pm.mclk_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 995 | return r; |
| 996 | } |
| 997 | |
| 998 | int radeon_mmap(struct file *filp, struct vm_area_struct *vma) |
| 999 | { |
| 1000 | struct drm_file *file_priv; |
| 1001 | struct radeon_device *rdev; |
| 1002 | int r; |
| 1003 | |
| 1004 | if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) { |
Daniel Vetter | 884c6da | 2014-09-23 15:46:47 +0200 | [diff] [blame] | 1005 | return -EINVAL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
Joe Perches | 40b3be3 | 2010-09-04 18:52:42 -0700 | [diff] [blame] | 1008 | file_priv = filp->private_data; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 1009 | rdev = file_priv->minor->dev->dev_private; |
| 1010 | if (rdev == NULL) { |
| 1011 | return -EINVAL; |
| 1012 | } |
| 1013 | r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev); |
| 1014 | if (unlikely(r != 0)) { |
| 1015 | return r; |
| 1016 | } |
| 1017 | if (unlikely(ttm_vm_ops == NULL)) { |
| 1018 | ttm_vm_ops = vma->vm_ops; |
| 1019 | radeon_ttm_vm_ops = *ttm_vm_ops; |
| 1020 | radeon_ttm_vm_ops.fault = &radeon_ttm_fault; |
| 1021 | } |
| 1022 | vma->vm_ops = &radeon_ttm_vm_ops; |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1026 | #if defined(CONFIG_DEBUG_FS) |
Christian König | 893d6e6 | 2013-12-12 09:42:40 +0100 | [diff] [blame] | 1027 | |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1028 | static int radeon_mm_dump_table(struct seq_file *m, void *data) |
| 1029 | { |
| 1030 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
Christian König | 893d6e6 | 2013-12-12 09:42:40 +0100 | [diff] [blame] | 1031 | unsigned ttm_pl = *(int *)node->info_ent->data; |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1032 | struct drm_device *dev = node->minor->dev; |
| 1033 | struct radeon_device *rdev = dev->dev_private; |
Christian König | 893d6e6 | 2013-12-12 09:42:40 +0100 | [diff] [blame] | 1034 | struct drm_mm *mm = (struct drm_mm *)rdev->mman.bdev.man[ttm_pl].priv; |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1035 | int ret; |
| 1036 | struct ttm_bo_global *glob = rdev->mman.bdev.glob; |
| 1037 | |
| 1038 | spin_lock(&glob->lru_lock); |
| 1039 | ret = drm_mm_dump_table(m, mm); |
| 1040 | spin_unlock(&glob->lru_lock); |
| 1041 | return ret; |
| 1042 | } |
Christian König | 893d6e6 | 2013-12-12 09:42:40 +0100 | [diff] [blame] | 1043 | |
| 1044 | static int ttm_pl_vram = TTM_PL_VRAM; |
| 1045 | static int ttm_pl_tt = TTM_PL_TT; |
| 1046 | |
| 1047 | static struct drm_info_list radeon_ttm_debugfs_list[] = { |
| 1048 | {"radeon_vram_mm", radeon_mm_dump_table, 0, &ttm_pl_vram}, |
| 1049 | {"radeon_gtt_mm", radeon_mm_dump_table, 0, &ttm_pl_tt}, |
| 1050 | {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL}, |
| 1051 | #ifdef CONFIG_SWIOTLB |
| 1052 | {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL} |
| 1053 | #endif |
| 1054 | }; |
| 1055 | |
Christian König | 2014b56 | 2013-12-18 21:07:39 +0100 | [diff] [blame] | 1056 | static int radeon_ttm_vram_open(struct inode *inode, struct file *filep) |
| 1057 | { |
| 1058 | struct radeon_device *rdev = inode->i_private; |
| 1059 | i_size_write(inode, rdev->mc.mc_vram_size); |
| 1060 | filep->private_data = inode->i_private; |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | static ssize_t radeon_ttm_vram_read(struct file *f, char __user *buf, |
| 1065 | size_t size, loff_t *pos) |
| 1066 | { |
| 1067 | struct radeon_device *rdev = f->private_data; |
| 1068 | ssize_t result = 0; |
| 1069 | int r; |
| 1070 | |
| 1071 | if (size & 0x3 || *pos & 0x3) |
| 1072 | return -EINVAL; |
| 1073 | |
| 1074 | while (size) { |
| 1075 | unsigned long flags; |
| 1076 | uint32_t value; |
| 1077 | |
| 1078 | if (*pos >= rdev->mc.mc_vram_size) |
| 1079 | return result; |
| 1080 | |
| 1081 | spin_lock_irqsave(&rdev->mmio_idx_lock, flags); |
| 1082 | WREG32(RADEON_MM_INDEX, ((uint32_t)*pos) | 0x80000000); |
| 1083 | if (rdev->family >= CHIP_CEDAR) |
| 1084 | WREG32(EVERGREEN_MM_INDEX_HI, *pos >> 31); |
| 1085 | value = RREG32(RADEON_MM_DATA); |
| 1086 | spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags); |
| 1087 | |
| 1088 | r = put_user(value, (uint32_t *)buf); |
| 1089 | if (r) |
| 1090 | return r; |
| 1091 | |
| 1092 | result += 4; |
| 1093 | buf += 4; |
| 1094 | *pos += 4; |
| 1095 | size -= 4; |
| 1096 | } |
| 1097 | |
| 1098 | return result; |
| 1099 | } |
| 1100 | |
| 1101 | static const struct file_operations radeon_ttm_vram_fops = { |
| 1102 | .owner = THIS_MODULE, |
| 1103 | .open = radeon_ttm_vram_open, |
| 1104 | .read = radeon_ttm_vram_read, |
| 1105 | .llseek = default_llseek |
| 1106 | }; |
| 1107 | |
Christian König | dd66d20 | 2013-12-18 21:07:40 +0100 | [diff] [blame] | 1108 | static int radeon_ttm_gtt_open(struct inode *inode, struct file *filep) |
| 1109 | { |
| 1110 | struct radeon_device *rdev = inode->i_private; |
| 1111 | i_size_write(inode, rdev->mc.gtt_size); |
| 1112 | filep->private_data = inode->i_private; |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | static ssize_t radeon_ttm_gtt_read(struct file *f, char __user *buf, |
| 1117 | size_t size, loff_t *pos) |
| 1118 | { |
| 1119 | struct radeon_device *rdev = f->private_data; |
| 1120 | ssize_t result = 0; |
| 1121 | int r; |
| 1122 | |
| 1123 | while (size) { |
| 1124 | loff_t p = *pos / PAGE_SIZE; |
| 1125 | unsigned off = *pos & ~PAGE_MASK; |
Paul Bolle | 0d997b6 | 2014-03-04 10:34:48 +0100 | [diff] [blame] | 1126 | size_t cur_size = min_t(size_t, size, PAGE_SIZE - off); |
Christian König | dd66d20 | 2013-12-18 21:07:40 +0100 | [diff] [blame] | 1127 | struct page *page; |
| 1128 | void *ptr; |
| 1129 | |
| 1130 | if (p >= rdev->gart.num_cpu_pages) |
| 1131 | return result; |
| 1132 | |
| 1133 | page = rdev->gart.pages[p]; |
| 1134 | if (page) { |
| 1135 | ptr = kmap(page); |
| 1136 | ptr += off; |
| 1137 | |
| 1138 | r = copy_to_user(buf, ptr, cur_size); |
| 1139 | kunmap(rdev->gart.pages[p]); |
| 1140 | } else |
| 1141 | r = clear_user(buf, cur_size); |
| 1142 | |
| 1143 | if (r) |
| 1144 | return -EFAULT; |
| 1145 | |
| 1146 | result += cur_size; |
| 1147 | buf += cur_size; |
| 1148 | *pos += cur_size; |
| 1149 | size -= cur_size; |
| 1150 | } |
| 1151 | |
| 1152 | return result; |
| 1153 | } |
| 1154 | |
| 1155 | static const struct file_operations radeon_ttm_gtt_fops = { |
| 1156 | .owner = THIS_MODULE, |
| 1157 | .open = radeon_ttm_gtt_open, |
| 1158 | .read = radeon_ttm_gtt_read, |
| 1159 | .llseek = default_llseek |
| 1160 | }; |
| 1161 | |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1162 | #endif |
| 1163 | |
| 1164 | static int radeon_ttm_debugfs_init(struct radeon_device *rdev) |
| 1165 | { |
Mikael Pettersson | f4e45d0 | 2009-09-28 18:27:23 +0200 | [diff] [blame] | 1166 | #if defined(CONFIG_DEBUG_FS) |
Christian König | 2014b56 | 2013-12-18 21:07:39 +0100 | [diff] [blame] | 1167 | unsigned count; |
| 1168 | |
| 1169 | struct drm_minor *minor = rdev->ddev->primary; |
| 1170 | struct dentry *ent, *root = minor->debugfs_root; |
| 1171 | |
| 1172 | ent = debugfs_create_file("radeon_vram", S_IFREG | S_IRUGO, root, |
| 1173 | rdev, &radeon_ttm_vram_fops); |
| 1174 | if (IS_ERR(ent)) |
| 1175 | return PTR_ERR(ent); |
| 1176 | rdev->mman.vram = ent; |
| 1177 | |
Christian König | dd66d20 | 2013-12-18 21:07:40 +0100 | [diff] [blame] | 1178 | ent = debugfs_create_file("radeon_gtt", S_IFREG | S_IRUGO, root, |
| 1179 | rdev, &radeon_ttm_gtt_fops); |
| 1180 | if (IS_ERR(ent)) |
| 1181 | return PTR_ERR(ent); |
| 1182 | rdev->mman.gtt = ent; |
| 1183 | |
Christian König | 2014b56 | 2013-12-18 21:07:39 +0100 | [diff] [blame] | 1184 | count = ARRAY_SIZE(radeon_ttm_debugfs_list); |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1185 | |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 1186 | #ifdef CONFIG_SWIOTLB |
Christian König | 893d6e6 | 2013-12-12 09:42:40 +0100 | [diff] [blame] | 1187 | if (!swiotlb_nr_tbl()) |
| 1188 | --count; |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 1189 | #endif |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1190 | |
Christian König | 893d6e6 | 2013-12-12 09:42:40 +0100 | [diff] [blame] | 1191 | return radeon_debugfs_add_files(rdev, radeon_ttm_debugfs_list, count); |
| 1192 | #else |
| 1193 | |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1194 | return 0; |
Christian König | 893d6e6 | 2013-12-12 09:42:40 +0100 | [diff] [blame] | 1195 | #endif |
Dave Airlie | fa8a123 | 2009-08-26 13:13:37 +1000 | [diff] [blame] | 1196 | } |
Christian König | 2014b56 | 2013-12-18 21:07:39 +0100 | [diff] [blame] | 1197 | |
| 1198 | static void radeon_ttm_debugfs_fini(struct radeon_device *rdev) |
| 1199 | { |
| 1200 | #if defined(CONFIG_DEBUG_FS) |
| 1201 | |
| 1202 | debugfs_remove(rdev->mman.vram); |
| 1203 | rdev->mman.vram = NULL; |
Christian König | dd66d20 | 2013-12-18 21:07:40 +0100 | [diff] [blame] | 1204 | |
| 1205 | debugfs_remove(rdev->mman.gtt); |
| 1206 | rdev->mman.gtt = NULL; |
Christian König | 2014b56 | 2013-12-18 21:07:39 +0100 | [diff] [blame] | 1207 | #endif |
| 1208 | } |