blob: 90f739478a1b5257b488d120245d77d4cf561a70 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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 Nieminen8d7cddc2010-04-01 12:44:59 +000036#include <ttm/ttm_page_alloc.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020037#include <drm/drmP.h>
38#include <drm/radeon_drm.h>
Dave Airliefa8a1232009-08-26 13:13:37 +100039#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Chris Metcalf4cfe7622013-02-01 13:44:33 -050041#include <linux/swiotlb.h>
Christian Königf72a113a2014-08-07 09:36:00 +020042#include <linux/swap.h>
43#include <linux/pagemap.h>
Christian König2014b562013-12-18 21:07:39 +010044#include <linux/debugfs.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020045#include "radeon_reg.h"
46#include "radeon.h"
47
48#define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
49
Dave Airliefa8a1232009-08-26 13:13:37 +100050static int radeon_ttm_debugfs_init(struct radeon_device *rdev);
Christian König2014b562013-12-18 21:07:39 +010051static void radeon_ttm_debugfs_fini(struct radeon_device *rdev);
Dave Airliefa8a1232009-08-26 13:13:37 +100052
Jerome Glisse771fe6b2009-06-05 14:42:42 +020053static 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 Airlieba4420c2010-03-09 10:56:52 +100067static int radeon_ttm_mem_global_init(struct drm_global_reference *ref)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020068{
69 return ttm_mem_global_init(ref->object);
70}
71
Dave Airlieba4420c2010-03-09 10:56:52 +100072static void radeon_ttm_mem_global_release(struct drm_global_reference *ref)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020073{
74 ttm_mem_global_release(ref->object);
75}
76
77static int radeon_ttm_global_init(struct radeon_device *rdev)
78{
Dave Airlieba4420c2010-03-09 10:56:52 +100079 struct drm_global_reference *global_ref;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020080 int r;
81
82 rdev->mman.mem_global_referenced = false;
83 global_ref = &rdev->mman.mem_global_ref;
Dave Airlieba4420c2010-03-09 10:56:52 +100084 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020085 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 Airlieba4420c2010-03-09 10:56:52 +100088 r = drm_global_item_ref(global_ref);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020089 if (r != 0) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +020090 DRM_ERROR("Failed setting up TTM memory accounting "
91 "subsystem.\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +020092 return r;
93 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +020094
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 Airlieba4420c2010-03-09 10:56:52 +100098 global_ref->global_type = DRM_GLOBAL_TTM_BO;
Thomas Hellstrom7f5f4db2009-08-20 10:29:08 +020099 global_ref->size = sizeof(struct ttm_bo_global);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200100 global_ref->init = &ttm_bo_global_init;
101 global_ref->release = &ttm_bo_global_release;
Dave Airlieba4420c2010-03-09 10:56:52 +1000102 r = drm_global_item_ref(global_ref);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200103 if (r != 0) {
104 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
Dave Airlieba4420c2010-03-09 10:56:52 +1000105 drm_global_item_unref(&rdev->mman.mem_global_ref);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200106 return r;
107 }
108
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200109 rdev->mman.mem_global_referenced = true;
110 return 0;
111}
112
113static void radeon_ttm_global_fini(struct radeon_device *rdev)
114{
115 if (rdev->mman.mem_global_referenced) {
Dave Airlieba4420c2010-03-09 10:56:52 +1000116 drm_global_item_unref(&rdev->mman.bo_global_ref.ref);
117 drm_global_item_unref(&rdev->mman.mem_global_ref);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200118 rdev->mman.mem_global_referenced = false;
119 }
120}
121
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200122static int radeon_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
123{
124 return 0;
125}
126
127static 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 Skeggsd961db72010-08-05 10:48:18 +1000142 man->func = &ttm_bo_manager_func;
Jerome Glissed594e462010-02-17 21:54:29 +0000143 man->gpu_offset = rdev->mc.gtt_start;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200144 man->available_caching = TTM_PL_MASK_CACHING;
145 man->default_caching = TTM_PL_FLAG_CACHED;
Michel Dänzer55c93272009-06-15 16:56:11 +0200146 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200147#if IS_ENABLED(CONFIG_AGP)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200148 if (rdev->flags & RADEON_IS_AGP) {
Daniel Vetterd9906752013-12-11 11:34:35 +0100149 if (!rdev->ddev->agp) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200150 DRM_ERROR("AGP is not enabled for memory type %u\n",
151 (unsigned)type);
152 return -EINVAL;
153 }
Michel Dänzer55c93272009-06-15 16:56:11 +0200154 if (!rdev->ddev->agp->cant_use_aperture)
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200155 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200156 man->available_caching = TTM_PL_FLAG_UNCACHED |
157 TTM_PL_FLAG_WC;
158 man->default_caching = TTM_PL_FLAG_WC;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200159 }
Jerome Glisse0c321c72010-04-07 10:21:27 +0000160#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200161 break;
162 case TTM_PL_VRAM:
163 /* "On-card" video ram */
Ben Skeggsd961db72010-08-05 10:48:18 +1000164 man->func = &ttm_bo_manager_func;
Jerome Glissed594e462010-02-17 21:54:29 +0000165 man->gpu_offset = rdev->mc.vram_start;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200166 man->flags = TTM_MEMTYPE_FLAG_FIXED |
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200167 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 Glisse771fe6b2009-06-05 14:42:42 +0200170 break;
171 default:
172 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
173 return -EINVAL;
174 }
175 return 0;
176}
177
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100178static void radeon_evict_flags(struct ttm_buffer_object *bo,
179 struct ttm_placement *placement)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200180{
Christian Königf1217ed2014-08-27 13:16:04 +0200181 static struct ttm_place placements = {
182 .fpfn = 0,
183 .lpfn = 0,
184 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
185 };
186
Jerome Glissed03d8582009-12-14 21:02:09 +0100187 struct radeon_bo *rbo;
Jerome Glissed03d8582009-12-14 21:02:09 +0100188
189 if (!radeon_ttm_bo_is_radeon_bo(bo)) {
Jerome Glissed03d8582009-12-14 21:02:09 +0100190 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 Glisse771fe6b2009-06-05 14:42:42 +0200197 switch (bo->mem.mem_type) {
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100198 case TTM_PL_VRAM:
Alex Deucher5e5c21c2014-12-03 00:03:49 -0500199 if (rbo->rdev->ring[radeon_copy_ring_index(rbo->rdev)].ready == false)
Dave Airlie9270eb12010-01-13 09:21:49 +1000200 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
Michel Dänzer2a85aed2014-10-09 18:55:04 +0900201 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 Airlie9270eb12010-01-13 09:21:49 +1000225 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100226 break;
227 case TTM_PL_TT:
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200228 default:
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100229 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_CPU);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230 }
Jerome Glisseeaa5fd12009-12-09 21:57:37 +0100231 *placement = rbo->placement;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200232}
233
234static int radeon_verify_access(struct ttm_buffer_object *bo, struct file *filp)
235{
David Herrmannacb46522013-08-25 18:28:59 +0200236 struct radeon_bo *rbo = container_of(bo, struct radeon_bo, tbo);
237
Jérôme Glisseb5dcec62016-04-19 09:07:50 -0400238 if (radeon_ttm_tt_has_userptr(bo->ttm))
239 return -EPERM;
David Herrmannacb46522013-08-25 18:28:59 +0200240 return drm_vma_node_verify_access(&rbo->gem_base.vma_node, filp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200241}
242
243static void radeon_move_null(struct ttm_buffer_object *bo,
244 struct ttm_mem_reg *new_mem)
245{
246 struct ttm_mem_reg *old_mem = &bo->mem;
247
248 BUG_ON(old_mem->mm_node != NULL);
249 *old_mem = *new_mem;
250 new_mem->mm_node = NULL;
251}
252
253static int radeon_move_blit(struct ttm_buffer_object *bo,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000254 bool evict, bool no_wait_gpu,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000255 struct ttm_mem_reg *new_mem,
256 struct ttm_mem_reg *old_mem)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200257{
258 struct radeon_device *rdev;
259 uint64_t old_start, new_start;
Christian König876dc9f2012-05-08 14:24:01 +0200260 struct radeon_fence *fence;
Christian König57d20a42014-09-04 20:01:53 +0200261 unsigned num_pages;
Christian König876dc9f2012-05-08 14:24:01 +0200262 int r, ridx;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200263
264 rdev = radeon_get_rdev(bo->bdev);
Christian König876dc9f2012-05-08 14:24:01 +0200265 ridx = radeon_copy_ring_index(rdev);
Ben Skeggsd961db72010-08-05 10:48:18 +1000266 old_start = old_mem->start << PAGE_SHIFT;
267 new_start = new_mem->start << PAGE_SHIFT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200268
269 switch (old_mem->mem_type) {
270 case TTM_PL_VRAM:
Jerome Glissed594e462010-02-17 21:54:29 +0000271 old_start += rdev->mc.vram_start;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200272 break;
273 case TTM_PL_TT:
Jerome Glissed594e462010-02-17 21:54:29 +0000274 old_start += rdev->mc.gtt_start;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200275 break;
276 default:
277 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
278 return -EINVAL;
279 }
280 switch (new_mem->mem_type) {
281 case TTM_PL_VRAM:
Jerome Glissed594e462010-02-17 21:54:29 +0000282 new_start += rdev->mc.vram_start;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200283 break;
284 case TTM_PL_TT:
Jerome Glissed594e462010-02-17 21:54:29 +0000285 new_start += rdev->mc.gtt_start;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200286 break;
287 default:
288 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
289 return -EINVAL;
290 }
Christian König876dc9f2012-05-08 14:24:01 +0200291 if (!rdev->ring[ridx].ready) {
Alex Deucher3000bf32012-01-05 22:11:07 -0500292 DRM_ERROR("Trying to move memory with ring turned off.\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200293 return -EINVAL;
294 }
Alex Deucher003cefe2011-09-16 12:04:08 -0400295
296 BUILD_BUG_ON((PAGE_SIZE % RADEON_GPU_PAGE_SIZE) != 0);
297
Christian König57d20a42014-09-04 20:01:53 +0200298 num_pages = new_mem->num_pages * (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
299 fence = radeon_copy(rdev, old_start, new_start, num_pages, bo->resv);
300 if (IS_ERR(fence))
301 return PTR_ERR(fence);
302
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200303 r = ttm_bo_move_accel_cleanup(bo, &fence->base,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000304 evict, no_wait_gpu, new_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200305 radeon_fence_unref(&fence);
306 return r;
307}
308
309static int radeon_move_vram_ram(struct ttm_buffer_object *bo,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000310 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000311 bool no_wait_gpu,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200312 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önigf1217ed2014-08-27 13:16:04 +0200317 struct ttm_place placements;
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100318 struct ttm_placement placement;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200319 int r;
320
321 rdev = radeon_get_rdev(bo->bdev);
322 tmp_mem = *new_mem;
323 tmp_mem.mm_node = NULL;
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100324 placement.num_placement = 1;
325 placement.placement = &placements;
326 placement.num_busy_placement = 1;
327 placement.busy_placement = &placements;
Christian Königf1217ed2014-08-27 13:16:04 +0200328 placements.fpfn = 0;
329 placements.lpfn = 0;
330 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100331 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000332 interruptible, no_wait_gpu);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200333 if (unlikely(r)) {
334 return r;
335 }
Dave Airliedf67bed2009-10-30 13:31:26 +1000336
337 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
338 if (unlikely(r)) {
339 goto out_cleanup;
340 }
341
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200342 r = ttm_tt_bind(bo->ttm, &tmp_mem);
343 if (unlikely(r)) {
344 goto out_cleanup;
345 }
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000346 r = radeon_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200347 if (unlikely(r)) {
348 goto out_cleanup;
349 }
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000350 r = ttm_bo_move_ttm(bo, true, no_wait_gpu, new_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351out_cleanup:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000352 ttm_bo_mem_put(bo, &tmp_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200353 return r;
354}
355
356static int radeon_move_ram_vram(struct ttm_buffer_object *bo,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000357 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000358 bool no_wait_gpu,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200359 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 Glisse312ea8d2009-12-07 15:52:58 +0100364 struct ttm_placement placement;
Christian Königf1217ed2014-08-27 13:16:04 +0200365 struct ttm_place placements;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200366 int r;
367
368 rdev = radeon_get_rdev(bo->bdev);
369 tmp_mem = *new_mem;
370 tmp_mem.mm_node = NULL;
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100371 placement.num_placement = 1;
372 placement.placement = &placements;
373 placement.num_busy_placement = 1;
374 placement.busy_placement = &placements;
Christian Königf1217ed2014-08-27 13:16:04 +0200375 placements.fpfn = 0;
376 placements.lpfn = 0;
377 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000378 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
379 interruptible, no_wait_gpu);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200380 if (unlikely(r)) {
381 return r;
382 }
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000383 r = ttm_bo_move_ttm(bo, true, no_wait_gpu, &tmp_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200384 if (unlikely(r)) {
385 goto out_cleanup;
386 }
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000387 r = radeon_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200388 if (unlikely(r)) {
389 goto out_cleanup;
390 }
391out_cleanup:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000392 ttm_bo_mem_put(bo, &tmp_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200393 return r;
394}
395
396static int radeon_bo_move(struct ttm_buffer_object *bo,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000397 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000398 bool no_wait_gpu,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000399 struct ttm_mem_reg *new_mem)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200400{
401 struct radeon_device *rdev;
Michel Dänzere1a575a2016-03-28 16:39:14 +0900402 struct radeon_bo *rbo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200403 struct ttm_mem_reg *old_mem = &bo->mem;
404 int r;
405
Michel Dänzere1a575a2016-03-28 16:39:14 +0900406 /* Can't move a pinned BO */
407 rbo = container_of(bo, struct radeon_bo, tbo);
408 if (WARN_ON_ONCE(rbo->pin_count > 0))
409 return -EINVAL;
410
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200411 rdev = radeon_get_rdev(bo->bdev);
412 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
413 radeon_move_null(bo, new_mem);
414 return 0;
415 }
416 if ((old_mem->mem_type == TTM_PL_TT &&
417 new_mem->mem_type == TTM_PL_SYSTEM) ||
418 (old_mem->mem_type == TTM_PL_SYSTEM &&
419 new_mem->mem_type == TTM_PL_TT)) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200420 /* bind is enough */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200421 radeon_move_null(bo, new_mem);
422 return 0;
423 }
Alex Deucher27cd7762012-02-23 17:53:42 -0500424 if (!rdev->ring[radeon_copy_ring_index(rdev)].ready ||
425 rdev->asic->copy.copy == NULL) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200426 /* use memcpy */
Michel Dänzer1ab2e102009-07-28 12:30:56 +0200427 goto memcpy;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200428 }
429
430 if (old_mem->mem_type == TTM_PL_VRAM &&
431 new_mem->mem_type == TTM_PL_SYSTEM) {
Michel Dänzer1ab2e102009-07-28 12:30:56 +0200432 r = radeon_move_vram_ram(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000433 no_wait_gpu, new_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200434 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
435 new_mem->mem_type == TTM_PL_VRAM) {
Michel Dänzer1ab2e102009-07-28 12:30:56 +0200436 r = radeon_move_ram_vram(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000437 no_wait_gpu, new_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200438 } else {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000439 r = radeon_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200440 }
Michel Dänzer1ab2e102009-07-28 12:30:56 +0200441
442 if (r) {
443memcpy:
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000444 r = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
Marek Olšák67e8e3f2014-03-02 00:56:18 +0100445 if (r) {
446 return r;
447 }
Michel Dänzer1ab2e102009-07-28 12:30:56 +0200448 }
Marek Olšák67e8e3f2014-03-02 00:56:18 +0100449
450 /* update statistics */
451 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &rdev->num_bytes_moved);
452 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200453}
454
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200455static int radeon_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
456{
457 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
458 struct radeon_device *rdev = radeon_get_rdev(bdev);
459
460 mem->bus.addr = NULL;
461 mem->bus.offset = 0;
462 mem->bus.size = mem->num_pages << PAGE_SHIFT;
463 mem->bus.base = 0;
464 mem->bus.is_iomem = false;
465 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
466 return -EINVAL;
467 switch (mem->mem_type) {
468 case TTM_PL_SYSTEM:
469 /* system memory */
470 return 0;
471 case TTM_PL_TT:
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200472#if IS_ENABLED(CONFIG_AGP)
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200473 if (rdev->flags & RADEON_IS_AGP) {
474 /* RADEON_IS_AGP is set only if AGP is active */
Ben Skeggsd961db72010-08-05 10:48:18 +1000475 mem->bus.offset = mem->start << PAGE_SHIFT;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200476 mem->bus.base = rdev->mc.agp_base;
Michel Dänzer365048f2010-05-19 12:46:22 +0200477 mem->bus.is_iomem = !rdev->ddev->agp->cant_use_aperture;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200478 }
479#endif
480 break;
481 case TTM_PL_VRAM:
Ben Skeggsd961db72010-08-05 10:48:18 +1000482 mem->bus.offset = mem->start << PAGE_SHIFT;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200483 /* check if it's visible */
484 if ((mem->bus.offset + mem->bus.size) > rdev->mc.visible_vram_size)
485 return -EINVAL;
486 mem->bus.base = rdev->mc.aper_base;
487 mem->bus.is_iomem = true;
Jay Estabrookffb57c42011-07-06 23:57:13 +0000488#ifdef __alpha__
489 /*
490 * Alpha: use bus.addr to hold the ioremap() return,
491 * so we can modify bus.base below.
492 */
493 if (mem->placement & TTM_PL_FLAG_WC)
494 mem->bus.addr =
495 ioremap_wc(mem->bus.base + mem->bus.offset,
496 mem->bus.size);
497 else
498 mem->bus.addr =
499 ioremap_nocache(mem->bus.base + mem->bus.offset,
500 mem->bus.size);
501
502 /*
503 * Alpha: Use just the bus offset plus
504 * the hose/domain memory base for bus.base.
505 * It then can be used to build PTEs for VRAM
506 * access, as done in ttm_bo_vm_fault().
507 */
508 mem->bus.base = (mem->bus.base & 0x0ffffffffUL) +
509 rdev->ddev->hose->dense_mem_base;
510#endif
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200511 break;
512 default:
513 return -EINVAL;
514 }
515 return 0;
516}
517
518static void radeon_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
519{
520}
521
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400522/*
523 * TTM backend functions.
524 */
525struct radeon_ttm_tt {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500526 struct ttm_dma_tt ttm;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400527 struct radeon_device *rdev;
528 u64 offset;
Christian Königf72a113a2014-08-07 09:36:00 +0200529
530 uint64_t userptr;
531 struct mm_struct *usermm;
532 uint32_t userflags;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400533};
534
Christian Königf72a113a2014-08-07 09:36:00 +0200535/* prepare the sg table with the user pages */
536static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
537{
538 struct radeon_device *rdev = radeon_get_rdev(ttm->bdev);
539 struct radeon_ttm_tt *gtt = (void *)ttm;
540 unsigned pinned = 0, nents;
541 int r;
542
543 int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
544 enum dma_data_direction direction = write ?
545 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
546
547 if (current->mm != gtt->usermm)
548 return -EPERM;
549
Christian Königddd00e32014-08-07 09:36:01 +0200550 if (gtt->userflags & RADEON_GEM_USERPTR_ANONONLY) {
551 /* check that we only pin down anonymous memory
552 to prevent problems with writeback */
553 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
554 struct vm_area_struct *vma;
555 vma = find_vma(gtt->usermm, gtt->userptr);
556 if (!vma || vma->vm_file || vma->vm_end < end)
557 return -EPERM;
558 }
559
Christian Königf72a113a2014-08-07 09:36:00 +0200560 do {
561 unsigned num_pages = ttm->num_pages - pinned;
562 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
563 struct page **pages = ttm->pages + pinned;
564
Dave Hansend4edcf02016-02-12 13:01:56 -0800565 r = get_user_pages(userptr, num_pages, write, 0, pages, NULL);
Christian Königf72a113a2014-08-07 09:36:00 +0200566 if (r < 0)
567 goto release_pages;
568
569 pinned += r;
570
571 } while (pinned < ttm->num_pages);
572
573 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
574 ttm->num_pages << PAGE_SHIFT,
575 GFP_KERNEL);
576 if (r)
577 goto release_sg;
578
579 r = -ENOMEM;
580 nents = dma_map_sg(rdev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
581 if (nents != ttm->sg->nents)
582 goto release_sg;
583
584 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
585 gtt->ttm.dma_address, ttm->num_pages);
586
587 return 0;
588
589release_sg:
590 kfree(ttm->sg);
591
592release_pages:
593 release_pages(ttm->pages, pinned, 0);
594 return r;
595}
596
597static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
598{
599 struct radeon_device *rdev = radeon_get_rdev(ttm->bdev);
600 struct radeon_ttm_tt *gtt = (void *)ttm;
monk.liudb129732015-05-05 09:24:17 +0200601 struct sg_page_iter sg_iter;
Christian Königf72a113a2014-08-07 09:36:00 +0200602
603 int write = !(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
604 enum dma_data_direction direction = write ?
605 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
606
Christian König863653f2015-03-31 17:36:57 +0200607 /* double check that we don't free the table twice */
608 if (!ttm->sg->sgl)
609 return;
610
Christian Königf72a113a2014-08-07 09:36:00 +0200611 /* free the sg table and pages again */
612 dma_unmap_sg(rdev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
613
monk.liudb129732015-05-05 09:24:17 +0200614 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
615 struct page *page = sg_page_iter_page(&sg_iter);
Christian Königf72a113a2014-08-07 09:36:00 +0200616 if (!(gtt->userflags & RADEON_GEM_USERPTR_READONLY))
617 set_page_dirty(page);
618
619 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300620 put_page(page);
Christian Königf72a113a2014-08-07 09:36:00 +0200621 }
622
623 sg_free_table(ttm->sg);
624}
625
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400626static int radeon_ttm_backend_bind(struct ttm_tt *ttm,
627 struct ttm_mem_reg *bo_mem)
628{
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500629 struct radeon_ttm_tt *gtt = (void*)ttm;
Michel Dänzer77497f22014-07-17 19:01:07 +0900630 uint32_t flags = RADEON_GART_PAGE_VALID | RADEON_GART_PAGE_READ |
631 RADEON_GART_PAGE_WRITE;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400632 int r;
633
Christian Königf72a113a2014-08-07 09:36:00 +0200634 if (gtt->userptr) {
635 radeon_ttm_tt_pin_userptr(ttm);
636 flags &= ~RADEON_GART_PAGE_WRITE;
637 }
638
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400639 gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
640 if (!ttm->num_pages) {
641 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
642 ttm->num_pages, bo_mem, ttm);
643 }
Michel Dänzer77497f22014-07-17 19:01:07 +0900644 if (ttm->caching_state == tt_cached)
645 flags |= RADEON_GART_PAGE_SNOOP;
646 r = radeon_gart_bind(gtt->rdev, gtt->offset, ttm->num_pages,
647 ttm->pages, gtt->ttm.dma_address, flags);
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400648 if (r) {
649 DRM_ERROR("failed to bind %lu pages at 0x%08X\n",
650 ttm->num_pages, (unsigned)gtt->offset);
651 return r;
652 }
653 return 0;
654}
655
656static int radeon_ttm_backend_unbind(struct ttm_tt *ttm)
657{
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500658 struct radeon_ttm_tt *gtt = (void *)ttm;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400659
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400660 radeon_gart_unbind(gtt->rdev, gtt->offset, ttm->num_pages);
Christian Königf72a113a2014-08-07 09:36:00 +0200661
662 if (gtt->userptr)
663 radeon_ttm_tt_unpin_userptr(ttm);
664
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400665 return 0;
666}
667
668static void radeon_ttm_backend_destroy(struct ttm_tt *ttm)
669{
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500670 struct radeon_ttm_tt *gtt = (void *)ttm;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400671
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500672 ttm_dma_tt_fini(&gtt->ttm);
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400673 kfree(gtt);
674}
675
676static struct ttm_backend_func radeon_backend_func = {
677 .bind = &radeon_ttm_backend_bind,
678 .unbind = &radeon_ttm_backend_unbind,
679 .destroy = &radeon_ttm_backend_destroy,
680};
681
Lauri Kasanen1109ca02012-08-31 13:43:50 -0400682static struct ttm_tt *radeon_ttm_tt_create(struct ttm_bo_device *bdev,
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400683 unsigned long size, uint32_t page_flags,
684 struct page *dummy_read_page)
685{
686 struct radeon_device *rdev;
687 struct radeon_ttm_tt *gtt;
688
689 rdev = radeon_get_rdev(bdev);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200690#if IS_ENABLED(CONFIG_AGP)
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400691 if (rdev->flags & RADEON_IS_AGP) {
692 return ttm_agp_tt_create(bdev, rdev->ddev->agp->bridge,
693 size, page_flags, dummy_read_page);
694 }
695#endif
696
697 gtt = kzalloc(sizeof(struct radeon_ttm_tt), GFP_KERNEL);
698 if (gtt == NULL) {
699 return NULL;
700 }
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500701 gtt->ttm.ttm.func = &radeon_backend_func;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400702 gtt->rdev = rdev;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500703 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
704 kfree(gtt);
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400705 return NULL;
706 }
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500707 return &gtt->ttm.ttm;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400708}
709
Christian König3840a652014-09-17 04:00:05 -0600710static struct radeon_ttm_tt *radeon_ttm_tt_to_gtt(struct ttm_tt *ttm)
711{
712 if (!ttm || ttm->func != &radeon_backend_func)
713 return NULL;
714 return (struct radeon_ttm_tt *)ttm;
715}
716
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400717static int radeon_ttm_tt_populate(struct ttm_tt *ttm)
718{
Christian König3840a652014-09-17 04:00:05 -0600719 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm);
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400720 struct radeon_device *rdev;
721 unsigned i;
722 int r;
Alex Deucher40f5cf92012-05-10 18:33:13 -0400723 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400724
725 if (ttm->state != tt_unpopulated)
726 return 0;
727
Christian König3840a652014-09-17 04:00:05 -0600728 if (gtt && gtt->userptr) {
Maninder Singh69ee2412015-06-19 09:35:23 +0530729 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Christian Königf72a113a2014-08-07 09:36:00 +0200730 if (!ttm->sg)
731 return -ENOMEM;
732
733 ttm->page_flags |= TTM_PAGE_FLAG_SG;
734 ttm->state = tt_unbound;
735 return 0;
736 }
737
Alex Deucher40f5cf92012-05-10 18:33:13 -0400738 if (slave && ttm->sg) {
739 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
740 gtt->ttm.dma_address, ttm->num_pages);
741 ttm->state = tt_unbound;
742 return 0;
743 }
744
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400745 rdev = radeon_get_rdev(ttm->bdev);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200746#if IS_ENABLED(CONFIG_AGP)
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500747 if (rdev->flags & RADEON_IS_AGP) {
748 return ttm_agp_tt_populate(ttm);
749 }
750#endif
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400751
752#ifdef CONFIG_SWIOTLB
753 if (swiotlb_nr_tbl()) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500754 return ttm_dma_populate(&gtt->ttm, rdev->dev);
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400755 }
756#endif
757
758 r = ttm_pool_populate(ttm);
759 if (r) {
760 return r;
761 }
762
763 for (i = 0; i < ttm->num_pages; i++) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500764 gtt->ttm.dma_address[i] = pci_map_page(rdev->pdev, ttm->pages[i],
765 0, PAGE_SIZE,
766 PCI_DMA_BIDIRECTIONAL);
767 if (pci_dma_mapping_error(rdev->pdev, gtt->ttm.dma_address[i])) {
Rasmus Villemoesbc3f5d82016-02-15 19:41:47 +0100768 while (i--) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500769 pci_unmap_page(rdev->pdev, gtt->ttm.dma_address[i],
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400770 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500771 gtt->ttm.dma_address[i] = 0;
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400772 }
773 ttm_pool_unpopulate(ttm);
774 return -EFAULT;
775 }
776 }
777 return 0;
778}
779
780static void radeon_ttm_tt_unpopulate(struct ttm_tt *ttm)
781{
782 struct radeon_device *rdev;
Christian König3840a652014-09-17 04:00:05 -0600783 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm);
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400784 unsigned i;
Alex Deucher40f5cf92012-05-10 18:33:13 -0400785 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
786
Christian König3840a652014-09-17 04:00:05 -0600787 if (gtt && gtt->userptr) {
Christian Königf72a113a2014-08-07 09:36:00 +0200788 kfree(ttm->sg);
789 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
790 return;
791 }
792
Alex Deucher40f5cf92012-05-10 18:33:13 -0400793 if (slave)
794 return;
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400795
796 rdev = radeon_get_rdev(ttm->bdev);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200797#if IS_ENABLED(CONFIG_AGP)
Jerome Glissedea7e0a2012-01-03 17:37:37 -0500798 if (rdev->flags & RADEON_IS_AGP) {
799 ttm_agp_tt_unpopulate(ttm);
800 return;
801 }
802#endif
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400803
804#ifdef CONFIG_SWIOTLB
805 if (swiotlb_nr_tbl()) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500806 ttm_dma_unpopulate(&gtt->ttm, rdev->dev);
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400807 return;
808 }
809#endif
810
811 for (i = 0; i < ttm->num_pages; i++) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500812 if (gtt->ttm.dma_address[i]) {
813 pci_unmap_page(rdev->pdev, gtt->ttm.dma_address[i],
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400814 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
815 }
816 }
817
818 ttm_pool_unpopulate(ttm);
819}
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400820
Christian Königf72a113a2014-08-07 09:36:00 +0200821int radeon_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
822 uint32_t flags)
823{
Christian König3840a652014-09-17 04:00:05 -0600824 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm);
Christian Königf72a113a2014-08-07 09:36:00 +0200825
826 if (gtt == NULL)
827 return -EINVAL;
828
829 gtt->userptr = addr;
830 gtt->usermm = current->mm;
831 gtt->userflags = flags;
832 return 0;
833}
834
835bool radeon_ttm_tt_has_userptr(struct ttm_tt *ttm)
836{
Christian König3840a652014-09-17 04:00:05 -0600837 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm);
Christian Königf72a113a2014-08-07 09:36:00 +0200838
839 if (gtt == NULL)
840 return false;
841
842 return !!gtt->userptr;
843}
844
845bool radeon_ttm_tt_is_readonly(struct ttm_tt *ttm)
846{
Christian König3840a652014-09-17 04:00:05 -0600847 struct radeon_ttm_tt *gtt = radeon_ttm_tt_to_gtt(ttm);
Christian Königf72a113a2014-08-07 09:36:00 +0200848
849 if (gtt == NULL)
850 return false;
851
852 return !!(gtt->userflags & RADEON_GEM_USERPTR_READONLY);
853}
854
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200855static struct ttm_bo_driver radeon_bo_driver = {
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400856 .ttm_tt_create = &radeon_ttm_tt_create,
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400857 .ttm_tt_populate = &radeon_ttm_tt_populate,
858 .ttm_tt_unpopulate = &radeon_ttm_tt_unpopulate,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200859 .invalidate_caches = &radeon_invalidate_caches,
860 .init_mem_type = &radeon_init_mem_type,
861 .evict_flags = &radeon_evict_flags,
862 .move = &radeon_bo_move,
863 .verify_access = &radeon_verify_access,
Dave Airliee024e112009-06-24 09:48:08 +1000864 .move_notify = &radeon_bo_move_notify,
865 .fault_reserve_notify = &radeon_bo_fault_reserve_notify,
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200866 .io_mem_reserve = &radeon_ttm_io_mem_reserve,
867 .io_mem_free = &radeon_ttm_io_mem_free,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200868};
869
870int radeon_ttm_init(struct radeon_device *rdev)
871{
872 int r;
873
874 r = radeon_ttm_global_init(rdev);
875 if (r) {
876 return r;
877 }
878 /* No others user of address space so set it to 0 */
879 r = ttm_bo_device_init(&rdev->mman.bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200880 rdev->mman.bo_global_ref.ref.object,
David Herrmann44d847b2013-08-13 19:10:30 +0200881 &radeon_bo_driver,
882 rdev->ddev->anon_inode->i_mapping,
883 DRM_FILE_PAGE_OFFSET,
Dave Airliead49f502009-07-10 22:36:26 +1000884 rdev->need_dma32);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200885 if (r) {
886 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
887 return r;
888 }
Jerome Glisse0a0c7592009-12-11 20:36:19 +0100889 rdev->mman.initialized = true;
Jerome Glisse4c788672009-11-20 14:29:23 +0100890 r = ttm_bo_init_mm(&rdev->mman.bdev, TTM_PL_VRAM,
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100891 rdev->mc.real_vram_size >> PAGE_SHIFT);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200892 if (r) {
893 DRM_ERROR("Failed initializing VRAM heap.\n");
894 return r;
895 }
Lauri Kasanen14eedc32014-02-28 20:50:23 +0200896 /* Change the size here instead of the init above so only lpfn is affected */
897 radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
898
Daniel Vetter441921d2011-02-18 17:59:16 +0100899 r = radeon_bo_create(rdev, 256 * 1024, PAGE_SIZE, true,
Maarten Lankhorst831b6962014-09-18 14:11:56 +0200900 RADEON_GEM_DOMAIN_VRAM, 0, NULL,
Alex Deucher40f5cf92012-05-10 18:33:13 -0400901 NULL, &rdev->stollen_vga_memory);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200902 if (r) {
903 return r;
904 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100905 r = radeon_bo_reserve(rdev->stollen_vga_memory, false);
906 if (r)
907 return r;
908 r = radeon_bo_pin(rdev->stollen_vga_memory, RADEON_GEM_DOMAIN_VRAM, NULL);
909 radeon_bo_unreserve(rdev->stollen_vga_memory);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200910 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100911 radeon_bo_unref(&rdev->stollen_vga_memory);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200912 return r;
913 }
914 DRM_INFO("radeon: %uM of VRAM memory ready\n",
Niels Ole Salscheiderfc986032013-05-18 21:19:23 +0200915 (unsigned) (rdev->mc.real_vram_size / (1024 * 1024)));
Jerome Glisse4c788672009-11-20 14:29:23 +0100916 r = ttm_bo_init_mm(&rdev->mman.bdev, TTM_PL_TT,
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100917 rdev->mc.gtt_size >> PAGE_SHIFT);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200918 if (r) {
919 DRM_ERROR("Failed initializing GTT heap.\n");
920 return r;
921 }
922 DRM_INFO("radeon: %uM of GTT memory ready.\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000923 (unsigned)(rdev->mc.gtt_size / (1024 * 1024)));
Dave Airliefa8a1232009-08-26 13:13:37 +1000924
925 r = radeon_ttm_debugfs_init(rdev);
926 if (r) {
927 DRM_ERROR("Failed to init debugfs\n");
928 return r;
929 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200930 return 0;
931}
932
933void radeon_ttm_fini(struct radeon_device *rdev)
934{
Jerome Glisse4c788672009-11-20 14:29:23 +0100935 int r;
936
Jerome Glisse0a0c7592009-12-11 20:36:19 +0100937 if (!rdev->mman.initialized)
938 return;
Christian König2014b562013-12-18 21:07:39 +0100939 radeon_ttm_debugfs_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200940 if (rdev->stollen_vga_memory) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100941 r = radeon_bo_reserve(rdev->stollen_vga_memory, false);
942 if (r == 0) {
943 radeon_bo_unpin(rdev->stollen_vga_memory);
944 radeon_bo_unreserve(rdev->stollen_vga_memory);
945 }
946 radeon_bo_unref(&rdev->stollen_vga_memory);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200947 }
948 ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_VRAM);
949 ttm_bo_clean_mm(&rdev->mman.bdev, TTM_PL_TT);
950 ttm_bo_device_release(&rdev->mman.bdev);
951 radeon_gart_fini(rdev);
952 radeon_ttm_global_fini(rdev);
Jerome Glisse0a0c7592009-12-11 20:36:19 +0100953 rdev->mman.initialized = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200954 DRM_INFO("radeon: ttm finalized\n");
955}
956
Dave Airlie53595332011-03-14 09:47:24 +1000957/* this should only be called at bootup or when userspace
958 * isn't running */
959void radeon_ttm_set_active_vram_size(struct radeon_device *rdev, u64 size)
960{
961 struct ttm_mem_type_manager *man;
962
963 if (!rdev->mman.initialized)
964 return;
965
966 man = &rdev->mman.bdev.man[TTM_PL_VRAM];
967 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
968 man->size = size >> PAGE_SHIFT;
969}
970
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200971static struct vm_operations_struct radeon_ttm_vm_ops;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400972static const struct vm_operations_struct *ttm_vm_ops = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200973
974static int radeon_ttm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
975{
976 struct ttm_buffer_object *bo;
Matthew Garrett5876dd22010-04-26 15:52:20 -0400977 struct radeon_device *rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200978 int r;
979
Matthew Garrett5876dd22010-04-26 15:52:20 -0400980 bo = (struct ttm_buffer_object *)vma->vm_private_data;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200981 if (bo == NULL) {
982 return VM_FAULT_NOPAGE;
983 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400984 rdev = radeon_get_rdev(bo->bdev);
Christian Königdb7fce32012-05-11 14:57:18 +0200985 down_read(&rdev->pm.mclk_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200986 r = ttm_vm_ops->fault(vma, vmf);
Christian Königdb7fce32012-05-11 14:57:18 +0200987 up_read(&rdev->pm.mclk_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200988 return r;
989}
990
991int radeon_mmap(struct file *filp, struct vm_area_struct *vma)
992{
993 struct drm_file *file_priv;
994 struct radeon_device *rdev;
995 int r;
996
997 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) {
Daniel Vetter884c6da2014-09-23 15:46:47 +0200998 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200999 }
1000
Joe Perches40b3be32010-09-04 18:52:42 -07001001 file_priv = filp->private_data;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001002 rdev = file_priv->minor->dev->dev_private;
1003 if (rdev == NULL) {
1004 return -EINVAL;
1005 }
1006 r = ttm_bo_mmap(filp, vma, &rdev->mman.bdev);
1007 if (unlikely(r != 0)) {
1008 return r;
1009 }
1010 if (unlikely(ttm_vm_ops == NULL)) {
1011 ttm_vm_ops = vma->vm_ops;
1012 radeon_ttm_vm_ops = *ttm_vm_ops;
1013 radeon_ttm_vm_ops.fault = &radeon_ttm_fault;
1014 }
1015 vma->vm_ops = &radeon_ttm_vm_ops;
1016 return 0;
1017}
1018
Dave Airliefa8a1232009-08-26 13:13:37 +10001019#if defined(CONFIG_DEBUG_FS)
Christian König893d6e62013-12-12 09:42:40 +01001020
Dave Airliefa8a1232009-08-26 13:13:37 +10001021static int radeon_mm_dump_table(struct seq_file *m, void *data)
1022{
1023 struct drm_info_node *node = (struct drm_info_node *)m->private;
Christian König893d6e62013-12-12 09:42:40 +01001024 unsigned ttm_pl = *(int *)node->info_ent->data;
Dave Airliefa8a1232009-08-26 13:13:37 +10001025 struct drm_device *dev = node->minor->dev;
1026 struct radeon_device *rdev = dev->dev_private;
Christian König893d6e62013-12-12 09:42:40 +01001027 struct drm_mm *mm = (struct drm_mm *)rdev->mman.bdev.man[ttm_pl].priv;
Dave Airliefa8a1232009-08-26 13:13:37 +10001028 int ret;
1029 struct ttm_bo_global *glob = rdev->mman.bdev.glob;
1030
1031 spin_lock(&glob->lru_lock);
1032 ret = drm_mm_dump_table(m, mm);
1033 spin_unlock(&glob->lru_lock);
1034 return ret;
1035}
Christian König893d6e62013-12-12 09:42:40 +01001036
1037static int ttm_pl_vram = TTM_PL_VRAM;
1038static int ttm_pl_tt = TTM_PL_TT;
1039
1040static struct drm_info_list radeon_ttm_debugfs_list[] = {
1041 {"radeon_vram_mm", radeon_mm_dump_table, 0, &ttm_pl_vram},
1042 {"radeon_gtt_mm", radeon_mm_dump_table, 0, &ttm_pl_tt},
1043 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1044#ifdef CONFIG_SWIOTLB
1045 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1046#endif
1047};
1048
Christian König2014b562013-12-18 21:07:39 +01001049static int radeon_ttm_vram_open(struct inode *inode, struct file *filep)
1050{
1051 struct radeon_device *rdev = inode->i_private;
1052 i_size_write(inode, rdev->mc.mc_vram_size);
1053 filep->private_data = inode->i_private;
1054 return 0;
1055}
1056
1057static ssize_t radeon_ttm_vram_read(struct file *f, char __user *buf,
1058 size_t size, loff_t *pos)
1059{
1060 struct radeon_device *rdev = f->private_data;
1061 ssize_t result = 0;
1062 int r;
1063
1064 if (size & 0x3 || *pos & 0x3)
1065 return -EINVAL;
1066
1067 while (size) {
1068 unsigned long flags;
1069 uint32_t value;
1070
1071 if (*pos >= rdev->mc.mc_vram_size)
1072 return result;
1073
1074 spin_lock_irqsave(&rdev->mmio_idx_lock, flags);
1075 WREG32(RADEON_MM_INDEX, ((uint32_t)*pos) | 0x80000000);
1076 if (rdev->family >= CHIP_CEDAR)
1077 WREG32(EVERGREEN_MM_INDEX_HI, *pos >> 31);
1078 value = RREG32(RADEON_MM_DATA);
1079 spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags);
1080
1081 r = put_user(value, (uint32_t *)buf);
1082 if (r)
1083 return r;
1084
1085 result += 4;
1086 buf += 4;
1087 *pos += 4;
1088 size -= 4;
1089 }
1090
1091 return result;
1092}
1093
1094static const struct file_operations radeon_ttm_vram_fops = {
1095 .owner = THIS_MODULE,
1096 .open = radeon_ttm_vram_open,
1097 .read = radeon_ttm_vram_read,
1098 .llseek = default_llseek
1099};
1100
Christian Königdd66d202013-12-18 21:07:40 +01001101static int radeon_ttm_gtt_open(struct inode *inode, struct file *filep)
1102{
1103 struct radeon_device *rdev = inode->i_private;
1104 i_size_write(inode, rdev->mc.gtt_size);
1105 filep->private_data = inode->i_private;
1106 return 0;
1107}
1108
1109static ssize_t radeon_ttm_gtt_read(struct file *f, char __user *buf,
1110 size_t size, loff_t *pos)
1111{
1112 struct radeon_device *rdev = f->private_data;
1113 ssize_t result = 0;
1114 int r;
1115
1116 while (size) {
1117 loff_t p = *pos / PAGE_SIZE;
1118 unsigned off = *pos & ~PAGE_MASK;
Paul Bolle0d997b62014-03-04 10:34:48 +01001119 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
Christian Königdd66d202013-12-18 21:07:40 +01001120 struct page *page;
1121 void *ptr;
1122
1123 if (p >= rdev->gart.num_cpu_pages)
1124 return result;
1125
1126 page = rdev->gart.pages[p];
1127 if (page) {
1128 ptr = kmap(page);
1129 ptr += off;
1130
1131 r = copy_to_user(buf, ptr, cur_size);
1132 kunmap(rdev->gart.pages[p]);
1133 } else
1134 r = clear_user(buf, cur_size);
1135
1136 if (r)
1137 return -EFAULT;
1138
1139 result += cur_size;
1140 buf += cur_size;
1141 *pos += cur_size;
1142 size -= cur_size;
1143 }
1144
1145 return result;
1146}
1147
1148static const struct file_operations radeon_ttm_gtt_fops = {
1149 .owner = THIS_MODULE,
1150 .open = radeon_ttm_gtt_open,
1151 .read = radeon_ttm_gtt_read,
1152 .llseek = default_llseek
1153};
1154
Dave Airliefa8a1232009-08-26 13:13:37 +10001155#endif
1156
1157static int radeon_ttm_debugfs_init(struct radeon_device *rdev)
1158{
Mikael Petterssonf4e45d02009-09-28 18:27:23 +02001159#if defined(CONFIG_DEBUG_FS)
Christian König2014b562013-12-18 21:07:39 +01001160 unsigned count;
1161
1162 struct drm_minor *minor = rdev->ddev->primary;
1163 struct dentry *ent, *root = minor->debugfs_root;
1164
1165 ent = debugfs_create_file("radeon_vram", S_IFREG | S_IRUGO, root,
1166 rdev, &radeon_ttm_vram_fops);
1167 if (IS_ERR(ent))
1168 return PTR_ERR(ent);
1169 rdev->mman.vram = ent;
1170
Christian Königdd66d202013-12-18 21:07:40 +01001171 ent = debugfs_create_file("radeon_gtt", S_IFREG | S_IRUGO, root,
1172 rdev, &radeon_ttm_gtt_fops);
1173 if (IS_ERR(ent))
1174 return PTR_ERR(ent);
1175 rdev->mman.gtt = ent;
1176
Christian König2014b562013-12-18 21:07:39 +01001177 count = ARRAY_SIZE(radeon_ttm_debugfs_list);
Dave Airliefa8a1232009-08-26 13:13:37 +10001178
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -04001179#ifdef CONFIG_SWIOTLB
Christian König893d6e62013-12-12 09:42:40 +01001180 if (!swiotlb_nr_tbl())
1181 --count;
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -04001182#endif
Dave Airliefa8a1232009-08-26 13:13:37 +10001183
Christian König893d6e62013-12-12 09:42:40 +01001184 return radeon_debugfs_add_files(rdev, radeon_ttm_debugfs_list, count);
1185#else
1186
Dave Airliefa8a1232009-08-26 13:13:37 +10001187 return 0;
Christian König893d6e62013-12-12 09:42:40 +01001188#endif
Dave Airliefa8a1232009-08-26 13:13:37 +10001189}
Christian König2014b562013-12-18 21:07:39 +01001190
1191static void radeon_ttm_debugfs_fini(struct radeon_device *rdev)
1192{
1193#if defined(CONFIG_DEBUG_FS)
1194
1195 debugfs_remove(rdev->mman.vram);
1196 rdev->mman.vram = NULL;
Christian Königdd66d202013-12-18 21:07:40 +01001197
1198 debugfs_remove(rdev->mman.gtt);
1199 rdev->mman.gtt = NULL;
Christian König2014b562013-12-18 21:07:39 +01001200#endif
1201}