blob: 8bdf0ba2983a79a0fb721f7e8a17f0a236e72771 [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 <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020034#include <drm/drmP.h>
35#include "radeon_drm.h"
36#include "radeon.h"
Dave Airlie99ee7fa2010-11-23 11:47:49 +100037#include "radeon_trace.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020038
Jerome Glisse771fe6b2009-06-05 14:42:42 +020039
40int radeon_ttm_init(struct radeon_device *rdev);
41void radeon_ttm_fini(struct radeon_device *rdev);
Jerome Glisse4c788672009-11-20 14:29:23 +010042static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020043
44/*
45 * To exclude mutual BO access we rely on bo_reserve exclusion, as all
46 * function are calling it.
47 */
48
Jerome Glisse4c788672009-11-20 14:29:23 +010049static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020050{
Jerome Glisse4c788672009-11-20 14:29:23 +010051 struct radeon_bo *bo;
52
53 bo = container_of(tbo, struct radeon_bo, tbo);
54 mutex_lock(&bo->rdev->gem.mutex);
55 list_del_init(&bo->list);
56 mutex_unlock(&bo->rdev->gem.mutex);
57 radeon_bo_clear_surface_reg(bo);
58 kfree(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020059}
60
Jerome Glissed03d8582009-12-14 21:02:09 +010061bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo)
62{
63 if (bo->destroy == &radeon_ttm_bo_destroy)
64 return true;
65 return false;
66}
67
Jerome Glisse312ea8d2009-12-07 15:52:58 +010068void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
69{
70 u32 c = 0;
71
72 rbo->placement.fpfn = 0;
Jerome Glissec919b372010-08-10 17:41:31 -040073 rbo->placement.lpfn = rbo->rdev->mc.active_vram_size >> PAGE_SHIFT;
Jerome Glisse312ea8d2009-12-07 15:52:58 +010074 rbo->placement.placement = rbo->placements;
75 rbo->placement.busy_placement = rbo->placements;
76 if (domain & RADEON_GEM_DOMAIN_VRAM)
77 rbo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
78 TTM_PL_FLAG_VRAM;
79 if (domain & RADEON_GEM_DOMAIN_GTT)
80 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
81 if (domain & RADEON_GEM_DOMAIN_CPU)
82 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
Jerome Glisse9fb03e62009-12-11 15:13:22 +010083 if (!c)
84 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
Jerome Glisse312ea8d2009-12-07 15:52:58 +010085 rbo->placement.num_placement = c;
86 rbo->placement.num_busy_placement = c;
87}
88
Jerome Glisse4c788672009-11-20 14:29:23 +010089int radeon_bo_create(struct radeon_device *rdev, struct drm_gem_object *gobj,
Alex Deucher268b2512010-11-17 19:00:26 -050090 unsigned long size, int byte_align, bool kernel, u32 domain,
91 struct radeon_bo **bo_ptr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020092{
Jerome Glisse4c788672009-11-20 14:29:23 +010093 struct radeon_bo *bo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020094 enum ttm_bo_type type;
Alex Deucher268b2512010-11-17 19:00:26 -050095 int page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020096 int r;
97
98 if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
99 rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
100 }
101 if (kernel) {
102 type = ttm_bo_type_kernel;
103 } else {
104 type = ttm_bo_type_device;
105 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100106 *bo_ptr = NULL;
Michel Dänzer2b66b502010-11-09 11:50:05 +0100107
108retry:
Jerome Glisse4c788672009-11-20 14:29:23 +0100109 bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
110 if (bo == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200111 return -ENOMEM;
Jerome Glisse4c788672009-11-20 14:29:23 +0100112 bo->rdev = rdev;
113 bo->gobj = gobj;
114 bo->surface_reg = -1;
115 INIT_LIST_HEAD(&bo->list);
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100116 radeon_ttm_placement_from_domain(bo, domain);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100117 /* Kernel allocation are uninterruptible */
Matthew Garrett5876dd22010-04-26 15:52:20 -0400118 mutex_lock(&rdev->vram_mutex);
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100119 r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
Alex Deucher268b2512010-11-17 19:00:26 -0500120 &bo->placement, page_align, 0, !kernel, NULL, size,
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100121 &radeon_ttm_bo_destroy);
Matthew Garrett5876dd22010-04-26 15:52:20 -0400122 mutex_unlock(&rdev->vram_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200123 if (unlikely(r != 0)) {
Michel Dänzere3765732010-07-08 12:43:28 +1000124 if (r != -ERESTARTSYS) {
125 if (domain == RADEON_GEM_DOMAIN_VRAM) {
126 domain |= RADEON_GEM_DOMAIN_GTT;
127 goto retry;
128 }
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100129 dev_err(rdev->dev,
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100130 "object_init failed for (%lu, 0x%08X)\n",
131 size, domain);
Michel Dänzere3765732010-07-08 12:43:28 +1000132 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200133 return r;
134 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100135 *bo_ptr = bo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200136 if (gobj) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100137 mutex_lock(&bo->rdev->gem.mutex);
138 list_add_tail(&bo->list, &rdev->gem.objects);
139 mutex_unlock(&bo->rdev->gem.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200140 }
Dave Airlie99ee7fa2010-11-23 11:47:49 +1000141 trace_radeon_bo_create(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142 return 0;
143}
144
Jerome Glisse4c788672009-11-20 14:29:23 +0100145int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200146{
Jerome Glisse4c788672009-11-20 14:29:23 +0100147 bool is_iomem;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200148 int r;
149
Jerome Glisse4c788672009-11-20 14:29:23 +0100150 if (bo->kptr) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200151 if (ptr) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100152 *ptr = bo->kptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200153 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200154 return 0;
155 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100156 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200157 if (r) {
158 return r;
159 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100160 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200161 if (ptr) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100162 *ptr = bo->kptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100164 radeon_bo_check_tiling(bo, 0, 0);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200165 return 0;
166}
167
Jerome Glisse4c788672009-11-20 14:29:23 +0100168void radeon_bo_kunmap(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169{
Jerome Glisse4c788672009-11-20 14:29:23 +0100170 if (bo->kptr == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200171 return;
Jerome Glisse4c788672009-11-20 14:29:23 +0100172 bo->kptr = NULL;
173 radeon_bo_check_tiling(bo, 0, 0);
174 ttm_bo_kunmap(&bo->kmap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175}
176
Jerome Glisse4c788672009-11-20 14:29:23 +0100177void radeon_bo_unref(struct radeon_bo **bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200178{
Jerome Glisse4c788672009-11-20 14:29:23 +0100179 struct ttm_buffer_object *tbo;
Dave Airlief4b7fb92010-04-29 18:37:59 +1000180 struct radeon_device *rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200181
Jerome Glisse4c788672009-11-20 14:29:23 +0100182 if ((*bo) == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200183 return;
Dave Airlief4b7fb92010-04-29 18:37:59 +1000184 rdev = (*bo)->rdev;
Jerome Glisse4c788672009-11-20 14:29:23 +0100185 tbo = &((*bo)->tbo);
Dave Airlief4b7fb92010-04-29 18:37:59 +1000186 mutex_lock(&rdev->vram_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100187 ttm_bo_unref(&tbo);
Dave Airlief4b7fb92010-04-29 18:37:59 +1000188 mutex_unlock(&rdev->vram_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100189 if (tbo == NULL)
190 *bo = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200191}
192
Jerome Glisse4c788672009-11-20 14:29:23 +0100193int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200194{
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100195 int r, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200196
Jerome Glisse4c788672009-11-20 14:29:23 +0100197 if (bo->pin_count) {
198 bo->pin_count++;
199 if (gpu_addr)
200 *gpu_addr = radeon_bo_gpu_offset(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200201 return 0;
202 }
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100203 radeon_ttm_placement_from_domain(bo, domain);
Michel Dänzer3ca82da2010-03-26 19:18:55 +0000204 if (domain == RADEON_GEM_DOMAIN_VRAM) {
205 /* force to pin into visible video ram */
206 bo->placement.lpfn = bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
207 }
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100208 for (i = 0; i < bo->placement.num_placement; i++)
209 bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000210 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100211 if (likely(r == 0)) {
212 bo->pin_count = 1;
213 if (gpu_addr != NULL)
214 *gpu_addr = radeon_bo_gpu_offset(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200215 }
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100216 if (unlikely(r != 0))
Jerome Glisse4c788672009-11-20 14:29:23 +0100217 dev_err(bo->rdev->dev, "%p pin failed\n", bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200218 return r;
219}
220
Jerome Glisse4c788672009-11-20 14:29:23 +0100221int radeon_bo_unpin(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222{
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100223 int r, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200224
Jerome Glisse4c788672009-11-20 14:29:23 +0100225 if (!bo->pin_count) {
226 dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
227 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200228 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100229 bo->pin_count--;
230 if (bo->pin_count)
231 return 0;
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100232 for (i = 0; i < bo->placement.num_placement; i++)
233 bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000234 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100235 if (unlikely(r != 0))
Jerome Glisse4c788672009-11-20 14:29:23 +0100236 dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100237 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200238}
239
Jerome Glisse4c788672009-11-20 14:29:23 +0100240int radeon_bo_evict_vram(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200241{
Dave Airlied796d842010-01-25 13:08:08 +1000242 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
243 if (0 && (rdev->flags & RADEON_IS_IGP)) {
Alex Deucher06b64762010-01-05 11:27:29 -0500244 if (rdev->mc.igp_sideport_enabled == false)
245 /* Useless to evict on IGP chips */
246 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200247 }
248 return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
249}
250
Jerome Glisse4c788672009-11-20 14:29:23 +0100251void radeon_bo_force_delete(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200252{
Jerome Glisse4c788672009-11-20 14:29:23 +0100253 struct radeon_bo *bo, *n;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200254 struct drm_gem_object *gobj;
255
256 if (list_empty(&rdev->gem.objects)) {
257 return;
258 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100259 dev_err(rdev->dev, "Userspace still has active objects !\n");
260 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200261 mutex_lock(&rdev->ddev->struct_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100262 gobj = bo->gobj;
263 dev_err(rdev->dev, "%p %p %lu %lu force free\n",
264 gobj, bo, (unsigned long)gobj->size,
265 *((unsigned long *)&gobj->refcount));
266 mutex_lock(&bo->rdev->gem.mutex);
267 list_del_init(&bo->list);
268 mutex_unlock(&bo->rdev->gem.mutex);
269 radeon_bo_unref(&bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270 gobj->driver_private = NULL;
271 drm_gem_object_unreference(gobj);
272 mutex_unlock(&rdev->ddev->struct_mutex);
273 }
274}
275
Jerome Glisse4c788672009-11-20 14:29:23 +0100276int radeon_bo_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200277{
Jerome Glissea4d68272009-09-11 13:00:43 +0200278 /* Add an MTRR for the VRAM */
279 rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
280 MTRR_TYPE_WRCOMB, 1);
281 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
282 rdev->mc.mc_vram_size >> 20,
283 (unsigned long long)rdev->mc.aper_size >> 20);
284 DRM_INFO("RAM width %dbits %cDR\n",
285 rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200286 return radeon_ttm_init(rdev);
287}
288
Jerome Glisse4c788672009-11-20 14:29:23 +0100289void radeon_bo_fini(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200290{
291 radeon_ttm_fini(rdev);
292}
293
Jerome Glisse4c788672009-11-20 14:29:23 +0100294void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
295 struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200296{
297 if (lobj->wdomain) {
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000298 list_add(&lobj->tv.head, head);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200299 } else {
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000300 list_add_tail(&lobj->tv.head, head);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200301 }
302}
303
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100304int radeon_bo_list_validate(struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200305{
Jerome Glisse4c788672009-11-20 14:29:23 +0100306 struct radeon_bo_list *lobj;
307 struct radeon_bo *bo;
Michel Dänzere3765732010-07-08 12:43:28 +1000308 u32 domain;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200309 int r;
310
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000311 r = ttm_eu_reserve_buffers(head);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200312 if (unlikely(r != 0)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200313 return r;
314 }
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000315 list_for_each_entry(lobj, head, tv.head) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100316 bo = lobj->bo;
317 if (!bo->pin_count) {
Michel Dänzere3765732010-07-08 12:43:28 +1000318 domain = lobj->wdomain ? lobj->wdomain : lobj->rdomain;
319
320 retry:
321 radeon_ttm_placement_from_domain(bo, domain);
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100322 r = ttm_bo_validate(&bo->tbo, &bo->placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000323 true, false, false);
Michel Dänzere3765732010-07-08 12:43:28 +1000324 if (unlikely(r)) {
325 if (r != -ERESTARTSYS && domain == RADEON_GEM_DOMAIN_VRAM) {
326 domain |= RADEON_GEM_DOMAIN_GTT;
327 goto retry;
328 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200329 return r;
Michel Dänzere3765732010-07-08 12:43:28 +1000330 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200331 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100332 lobj->gpu_offset = radeon_bo_gpu_offset(bo);
333 lobj->tiling_flags = bo->tiling_flags;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200334 }
335 return 0;
336}
337
Jerome Glisse4c788672009-11-20 14:29:23 +0100338int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200339 struct vm_area_struct *vma)
340{
Jerome Glisse4c788672009-11-20 14:29:23 +0100341 return ttm_fbdev_mmap(vma, &bo->tbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200342}
343
Dave Airlie550e2d92009-12-09 14:15:38 +1000344int radeon_bo_get_surface_reg(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200345{
Jerome Glisse4c788672009-11-20 14:29:23 +0100346 struct radeon_device *rdev = bo->rdev;
Dave Airliee024e112009-06-24 09:48:08 +1000347 struct radeon_surface_reg *reg;
Jerome Glisse4c788672009-11-20 14:29:23 +0100348 struct radeon_bo *old_object;
Dave Airliee024e112009-06-24 09:48:08 +1000349 int steal;
350 int i;
351
Jerome Glisse4c788672009-11-20 14:29:23 +0100352 BUG_ON(!atomic_read(&bo->tbo.reserved));
353
354 if (!bo->tiling_flags)
Dave Airliee024e112009-06-24 09:48:08 +1000355 return 0;
356
Jerome Glisse4c788672009-11-20 14:29:23 +0100357 if (bo->surface_reg >= 0) {
358 reg = &rdev->surface_regs[bo->surface_reg];
359 i = bo->surface_reg;
Dave Airliee024e112009-06-24 09:48:08 +1000360 goto out;
361 }
362
363 steal = -1;
364 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
365
366 reg = &rdev->surface_regs[i];
Jerome Glisse4c788672009-11-20 14:29:23 +0100367 if (!reg->bo)
Dave Airliee024e112009-06-24 09:48:08 +1000368 break;
369
Jerome Glisse4c788672009-11-20 14:29:23 +0100370 old_object = reg->bo;
Dave Airliee024e112009-06-24 09:48:08 +1000371 if (old_object->pin_count == 0)
372 steal = i;
373 }
374
375 /* if we are all out */
376 if (i == RADEON_GEM_MAX_SURFACES) {
377 if (steal == -1)
378 return -ENOMEM;
379 /* find someone with a surface reg and nuke their BO */
380 reg = &rdev->surface_regs[steal];
Jerome Glisse4c788672009-11-20 14:29:23 +0100381 old_object = reg->bo;
Dave Airliee024e112009-06-24 09:48:08 +1000382 /* blow away the mapping */
383 DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
Jerome Glisse4c788672009-11-20 14:29:23 +0100384 ttm_bo_unmap_virtual(&old_object->tbo);
Dave Airliee024e112009-06-24 09:48:08 +1000385 old_object->surface_reg = -1;
386 i = steal;
387 }
388
Jerome Glisse4c788672009-11-20 14:29:23 +0100389 bo->surface_reg = i;
390 reg->bo = bo;
Dave Airliee024e112009-06-24 09:48:08 +1000391
392out:
Jerome Glisse4c788672009-11-20 14:29:23 +0100393 radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
Ben Skeggsd961db72010-08-05 10:48:18 +1000394 bo->tbo.mem.start << PAGE_SHIFT,
Jerome Glisse4c788672009-11-20 14:29:23 +0100395 bo->tbo.num_pages << PAGE_SHIFT);
Dave Airliee024e112009-06-24 09:48:08 +1000396 return 0;
397}
398
Jerome Glisse4c788672009-11-20 14:29:23 +0100399static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
Dave Airliee024e112009-06-24 09:48:08 +1000400{
Jerome Glisse4c788672009-11-20 14:29:23 +0100401 struct radeon_device *rdev = bo->rdev;
Dave Airliee024e112009-06-24 09:48:08 +1000402 struct radeon_surface_reg *reg;
403
Jerome Glisse4c788672009-11-20 14:29:23 +0100404 if (bo->surface_reg == -1)
Dave Airliee024e112009-06-24 09:48:08 +1000405 return;
406
Jerome Glisse4c788672009-11-20 14:29:23 +0100407 reg = &rdev->surface_regs[bo->surface_reg];
408 radeon_clear_surface_reg(rdev, bo->surface_reg);
Dave Airliee024e112009-06-24 09:48:08 +1000409
Jerome Glisse4c788672009-11-20 14:29:23 +0100410 reg->bo = NULL;
411 bo->surface_reg = -1;
Dave Airliee024e112009-06-24 09:48:08 +1000412}
413
Jerome Glisse4c788672009-11-20 14:29:23 +0100414int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
415 uint32_t tiling_flags, uint32_t pitch)
Dave Airliee024e112009-06-24 09:48:08 +1000416{
Jerome Glisse4c788672009-11-20 14:29:23 +0100417 int r;
418
419 r = radeon_bo_reserve(bo, false);
420 if (unlikely(r != 0))
421 return r;
422 bo->tiling_flags = tiling_flags;
423 bo->pitch = pitch;
424 radeon_bo_unreserve(bo);
425 return 0;
Dave Airliee024e112009-06-24 09:48:08 +1000426}
427
Jerome Glisse4c788672009-11-20 14:29:23 +0100428void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
429 uint32_t *tiling_flags,
430 uint32_t *pitch)
Dave Airliee024e112009-06-24 09:48:08 +1000431{
Jerome Glisse4c788672009-11-20 14:29:23 +0100432 BUG_ON(!atomic_read(&bo->tbo.reserved));
Dave Airliee024e112009-06-24 09:48:08 +1000433 if (tiling_flags)
Jerome Glisse4c788672009-11-20 14:29:23 +0100434 *tiling_flags = bo->tiling_flags;
Dave Airliee024e112009-06-24 09:48:08 +1000435 if (pitch)
Jerome Glisse4c788672009-11-20 14:29:23 +0100436 *pitch = bo->pitch;
Dave Airliee024e112009-06-24 09:48:08 +1000437}
438
Jerome Glisse4c788672009-11-20 14:29:23 +0100439int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
440 bool force_drop)
Dave Airliee024e112009-06-24 09:48:08 +1000441{
Jerome Glisse4c788672009-11-20 14:29:23 +0100442 BUG_ON(!atomic_read(&bo->tbo.reserved));
443
444 if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
Dave Airliee024e112009-06-24 09:48:08 +1000445 return 0;
446
447 if (force_drop) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100448 radeon_bo_clear_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000449 return 0;
450 }
451
Jerome Glisse4c788672009-11-20 14:29:23 +0100452 if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
Dave Airliee024e112009-06-24 09:48:08 +1000453 if (!has_moved)
454 return 0;
455
Jerome Glisse4c788672009-11-20 14:29:23 +0100456 if (bo->surface_reg >= 0)
457 radeon_bo_clear_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000458 return 0;
459 }
460
Jerome Glisse4c788672009-11-20 14:29:23 +0100461 if ((bo->surface_reg >= 0) && !has_moved)
Dave Airliee024e112009-06-24 09:48:08 +1000462 return 0;
463
Jerome Glisse4c788672009-11-20 14:29:23 +0100464 return radeon_bo_get_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000465}
466
467void radeon_bo_move_notify(struct ttm_buffer_object *bo,
Jerome Glissed03d8582009-12-14 21:02:09 +0100468 struct ttm_mem_reg *mem)
Dave Airliee024e112009-06-24 09:48:08 +1000469{
Jerome Glissed03d8582009-12-14 21:02:09 +0100470 struct radeon_bo *rbo;
471 if (!radeon_ttm_bo_is_radeon_bo(bo))
472 return;
473 rbo = container_of(bo, struct radeon_bo, tbo);
Jerome Glisse4c788672009-11-20 14:29:23 +0100474 radeon_bo_check_tiling(rbo, 0, 1);
Dave Airliee024e112009-06-24 09:48:08 +1000475}
476
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200477int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
Dave Airliee024e112009-06-24 09:48:08 +1000478{
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200479 struct radeon_device *rdev;
Jerome Glissed03d8582009-12-14 21:02:09 +0100480 struct radeon_bo *rbo;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200481 unsigned long offset, size;
482 int r;
483
Jerome Glissed03d8582009-12-14 21:02:09 +0100484 if (!radeon_ttm_bo_is_radeon_bo(bo))
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200485 return 0;
Jerome Glissed03d8582009-12-14 21:02:09 +0100486 rbo = container_of(bo, struct radeon_bo, tbo);
Jerome Glisse4c788672009-11-20 14:29:23 +0100487 radeon_bo_check_tiling(rbo, 0, 0);
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200488 rdev = rbo->rdev;
489 if (bo->mem.mem_type == TTM_PL_VRAM) {
490 size = bo->mem.num_pages << PAGE_SHIFT;
Ben Skeggsd961db72010-08-05 10:48:18 +1000491 offset = bo->mem.start << PAGE_SHIFT;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200492 if ((offset + size) > rdev->mc.visible_vram_size) {
493 /* hurrah the memory is not visible ! */
494 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
495 rbo->placement.lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT;
496 r = ttm_bo_validate(bo, &rbo->placement, false, true, false);
497 if (unlikely(r != 0))
498 return r;
Ben Skeggsd961db72010-08-05 10:48:18 +1000499 offset = bo->mem.start << PAGE_SHIFT;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200500 /* this should not happen */
501 if ((offset + size) > rdev->mc.visible_vram_size)
502 return -EINVAL;
503 }
504 }
505 return 0;
Dave Airliee024e112009-06-24 09:48:08 +1000506}