blob: 1d067743fee068174e3a5e85eb0e2d47b5b712ee [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"
37
Jerome Glisse771fe6b2009-06-05 14:42:42 +020038
39int radeon_ttm_init(struct radeon_device *rdev);
40void radeon_ttm_fini(struct radeon_device *rdev);
Jerome Glisse4c788672009-11-20 14:29:23 +010041static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020042
43/*
44 * To exclude mutual BO access we rely on bo_reserve exclusion, as all
45 * function are calling it.
46 */
47
Jerome Glisse4c788672009-11-20 14:29:23 +010048static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020049{
Jerome Glisse4c788672009-11-20 14:29:23 +010050 struct radeon_bo *bo;
51
52 bo = container_of(tbo, struct radeon_bo, tbo);
53 mutex_lock(&bo->rdev->gem.mutex);
54 list_del_init(&bo->list);
55 mutex_unlock(&bo->rdev->gem.mutex);
56 radeon_bo_clear_surface_reg(bo);
57 kfree(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020058}
59
Jerome Glissed03d8582009-12-14 21:02:09 +010060bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo)
61{
62 if (bo->destroy == &radeon_ttm_bo_destroy)
63 return true;
64 return false;
65}
66
Jerome Glisse312ea8d2009-12-07 15:52:58 +010067void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
68{
69 u32 c = 0;
70
71 rbo->placement.fpfn = 0;
Jerome Glissec919b372010-08-10 17:41:31 -040072 rbo->placement.lpfn = rbo->rdev->mc.active_vram_size >> PAGE_SHIFT;
Jerome Glisse312ea8d2009-12-07 15:52:58 +010073 rbo->placement.placement = rbo->placements;
74 rbo->placement.busy_placement = rbo->placements;
75 if (domain & RADEON_GEM_DOMAIN_VRAM)
76 rbo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
77 TTM_PL_FLAG_VRAM;
78 if (domain & RADEON_GEM_DOMAIN_GTT)
79 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
80 if (domain & RADEON_GEM_DOMAIN_CPU)
81 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
Jerome Glisse9fb03e62009-12-11 15:13:22 +010082 if (!c)
83 rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
Jerome Glisse312ea8d2009-12-07 15:52:58 +010084 rbo->placement.num_placement = c;
85 rbo->placement.num_busy_placement = c;
86}
87
Jerome Glisse4c788672009-11-20 14:29:23 +010088int radeon_bo_create(struct radeon_device *rdev, struct drm_gem_object *gobj,
Alex Deucher268b2512010-11-17 19:00:26 -050089 unsigned long size, int byte_align, bool kernel, u32 domain,
90 struct radeon_bo **bo_ptr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020091{
Jerome Glisse4c788672009-11-20 14:29:23 +010092 struct radeon_bo *bo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020093 enum ttm_bo_type type;
Alex Deucher268b2512010-11-17 19:00:26 -050094 int page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020095 int r;
96
97 if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
98 rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
99 }
100 if (kernel) {
101 type = ttm_bo_type_kernel;
102 } else {
103 type = ttm_bo_type_device;
104 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100105 *bo_ptr = NULL;
Michel Dänzer2b66b502010-11-09 11:50:05 +0100106
107retry:
Jerome Glisse4c788672009-11-20 14:29:23 +0100108 bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
109 if (bo == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200110 return -ENOMEM;
Jerome Glisse4c788672009-11-20 14:29:23 +0100111 bo->rdev = rdev;
112 bo->gobj = gobj;
113 bo->surface_reg = -1;
114 INIT_LIST_HEAD(&bo->list);
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100115 radeon_ttm_placement_from_domain(bo, domain);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100116 /* Kernel allocation are uninterruptible */
Matthew Garrett5876dd22010-04-26 15:52:20 -0400117 mutex_lock(&rdev->vram_mutex);
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100118 r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
Alex Deucher268b2512010-11-17 19:00:26 -0500119 &bo->placement, page_align, 0, !kernel, NULL, size,
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100120 &radeon_ttm_bo_destroy);
Matthew Garrett5876dd22010-04-26 15:52:20 -0400121 mutex_unlock(&rdev->vram_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200122 if (unlikely(r != 0)) {
Michel Dänzere3765732010-07-08 12:43:28 +1000123 if (r != -ERESTARTSYS) {
124 if (domain == RADEON_GEM_DOMAIN_VRAM) {
125 domain |= RADEON_GEM_DOMAIN_GTT;
126 goto retry;
127 }
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100128 dev_err(rdev->dev,
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100129 "object_init failed for (%lu, 0x%08X)\n",
130 size, domain);
Michel Dänzere3765732010-07-08 12:43:28 +1000131 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200132 return r;
133 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100134 *bo_ptr = bo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200135 if (gobj) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100136 mutex_lock(&bo->rdev->gem.mutex);
137 list_add_tail(&bo->list, &rdev->gem.objects);
138 mutex_unlock(&bo->rdev->gem.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200139 }
140 return 0;
141}
142
Jerome Glisse4c788672009-11-20 14:29:23 +0100143int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200144{
Jerome Glisse4c788672009-11-20 14:29:23 +0100145 bool is_iomem;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200146 int r;
147
Jerome Glisse4c788672009-11-20 14:29:23 +0100148 if (bo->kptr) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200149 if (ptr) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100150 *ptr = bo->kptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200151 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200152 return 0;
153 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100154 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200155 if (r) {
156 return r;
157 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100158 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200159 if (ptr) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100160 *ptr = bo->kptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200161 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100162 radeon_bo_check_tiling(bo, 0, 0);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163 return 0;
164}
165
Jerome Glisse4c788672009-11-20 14:29:23 +0100166void radeon_bo_kunmap(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200167{
Jerome Glisse4c788672009-11-20 14:29:23 +0100168 if (bo->kptr == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169 return;
Jerome Glisse4c788672009-11-20 14:29:23 +0100170 bo->kptr = NULL;
171 radeon_bo_check_tiling(bo, 0, 0);
172 ttm_bo_kunmap(&bo->kmap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200173}
174
Jerome Glisse4c788672009-11-20 14:29:23 +0100175void radeon_bo_unref(struct radeon_bo **bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200176{
Jerome Glisse4c788672009-11-20 14:29:23 +0100177 struct ttm_buffer_object *tbo;
Dave Airlief4b7fb92010-04-29 18:37:59 +1000178 struct radeon_device *rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200179
Jerome Glisse4c788672009-11-20 14:29:23 +0100180 if ((*bo) == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200181 return;
Dave Airlief4b7fb92010-04-29 18:37:59 +1000182 rdev = (*bo)->rdev;
Jerome Glisse4c788672009-11-20 14:29:23 +0100183 tbo = &((*bo)->tbo);
Dave Airlief4b7fb92010-04-29 18:37:59 +1000184 mutex_lock(&rdev->vram_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100185 ttm_bo_unref(&tbo);
Dave Airlief4b7fb92010-04-29 18:37:59 +1000186 mutex_unlock(&rdev->vram_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100187 if (tbo == NULL)
188 *bo = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200189}
190
Jerome Glisse4c788672009-11-20 14:29:23 +0100191int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200192{
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100193 int r, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200194
Jerome Glisse4c788672009-11-20 14:29:23 +0100195 if (bo->pin_count) {
196 bo->pin_count++;
197 if (gpu_addr)
198 *gpu_addr = radeon_bo_gpu_offset(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200199 return 0;
200 }
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100201 radeon_ttm_placement_from_domain(bo, domain);
Michel Dänzer3ca82da2010-03-26 19:18:55 +0000202 if (domain == RADEON_GEM_DOMAIN_VRAM) {
203 /* force to pin into visible video ram */
204 bo->placement.lpfn = bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
205 }
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100206 for (i = 0; i < bo->placement.num_placement; i++)
207 bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000208 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100209 if (likely(r == 0)) {
210 bo->pin_count = 1;
211 if (gpu_addr != NULL)
212 *gpu_addr = radeon_bo_gpu_offset(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200213 }
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100214 if (unlikely(r != 0))
Jerome Glisse4c788672009-11-20 14:29:23 +0100215 dev_err(bo->rdev->dev, "%p pin failed\n", bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200216 return r;
217}
218
Jerome Glisse4c788672009-11-20 14:29:23 +0100219int radeon_bo_unpin(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200220{
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100221 int r, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222
Jerome Glisse4c788672009-11-20 14:29:23 +0100223 if (!bo->pin_count) {
224 dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
225 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200226 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100227 bo->pin_count--;
228 if (bo->pin_count)
229 return 0;
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100230 for (i = 0; i < bo->placement.num_placement; i++)
231 bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000232 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100233 if (unlikely(r != 0))
Jerome Glisse4c788672009-11-20 14:29:23 +0100234 dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100235 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200236}
237
Jerome Glisse4c788672009-11-20 14:29:23 +0100238int radeon_bo_evict_vram(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200239{
Dave Airlied796d842010-01-25 13:08:08 +1000240 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
241 if (0 && (rdev->flags & RADEON_IS_IGP)) {
Alex Deucher06b64762010-01-05 11:27:29 -0500242 if (rdev->mc.igp_sideport_enabled == false)
243 /* Useless to evict on IGP chips */
244 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200245 }
246 return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
247}
248
Jerome Glisse4c788672009-11-20 14:29:23 +0100249void radeon_bo_force_delete(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200250{
Jerome Glisse4c788672009-11-20 14:29:23 +0100251 struct radeon_bo *bo, *n;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200252 struct drm_gem_object *gobj;
253
254 if (list_empty(&rdev->gem.objects)) {
255 return;
256 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100257 dev_err(rdev->dev, "Userspace still has active objects !\n");
258 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200259 mutex_lock(&rdev->ddev->struct_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100260 gobj = bo->gobj;
261 dev_err(rdev->dev, "%p %p %lu %lu force free\n",
262 gobj, bo, (unsigned long)gobj->size,
263 *((unsigned long *)&gobj->refcount));
264 mutex_lock(&bo->rdev->gem.mutex);
265 list_del_init(&bo->list);
266 mutex_unlock(&bo->rdev->gem.mutex);
267 radeon_bo_unref(&bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200268 gobj->driver_private = NULL;
269 drm_gem_object_unreference(gobj);
270 mutex_unlock(&rdev->ddev->struct_mutex);
271 }
272}
273
Jerome Glisse4c788672009-11-20 14:29:23 +0100274int radeon_bo_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200275{
Jerome Glissea4d68272009-09-11 13:00:43 +0200276 /* Add an MTRR for the VRAM */
277 rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
278 MTRR_TYPE_WRCOMB, 1);
279 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
280 rdev->mc.mc_vram_size >> 20,
281 (unsigned long long)rdev->mc.aper_size >> 20);
282 DRM_INFO("RAM width %dbits %cDR\n",
283 rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200284 return radeon_ttm_init(rdev);
285}
286
Jerome Glisse4c788672009-11-20 14:29:23 +0100287void radeon_bo_fini(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200288{
289 radeon_ttm_fini(rdev);
290}
291
Jerome Glisse4c788672009-11-20 14:29:23 +0100292void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
293 struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200294{
295 if (lobj->wdomain) {
296 list_add(&lobj->list, head);
297 } else {
298 list_add_tail(&lobj->list, head);
299 }
300}
301
Jerome Glisse4c788672009-11-20 14:29:23 +0100302int radeon_bo_list_reserve(struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200303{
Jerome Glisse4c788672009-11-20 14:29:23 +0100304 struct radeon_bo_list *lobj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200305 int r;
306
Dave Airlie9d8401f2009-10-08 09:28:19 +1000307 list_for_each_entry(lobj, head, list){
Jerome Glisse4c788672009-11-20 14:29:23 +0100308 r = radeon_bo_reserve(lobj->bo, false);
309 if (unlikely(r != 0))
310 return r;
Jerome Glissee8652752010-05-19 16:05:50 +0200311 lobj->reserved = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200312 }
313 return 0;
314}
315
Jerome Glisse4c788672009-11-20 14:29:23 +0100316void radeon_bo_list_unreserve(struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200317{
Jerome Glisse4c788672009-11-20 14:29:23 +0100318 struct radeon_bo_list *lobj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200319
Dave Airlie9d8401f2009-10-08 09:28:19 +1000320 list_for_each_entry(lobj, head, list) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100321 /* only unreserve object we successfully reserved */
Jerome Glissee8652752010-05-19 16:05:50 +0200322 if (lobj->reserved && radeon_bo_is_reserved(lobj->bo))
Jerome Glisse4c788672009-11-20 14:29:23 +0100323 radeon_bo_unreserve(lobj->bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200324 }
325}
326
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100327int radeon_bo_list_validate(struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200328{
Jerome Glisse4c788672009-11-20 14:29:23 +0100329 struct radeon_bo_list *lobj;
330 struct radeon_bo *bo;
Michel Dänzere3765732010-07-08 12:43:28 +1000331 u32 domain;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200332 int r;
333
Jerome Glissee8652752010-05-19 16:05:50 +0200334 list_for_each_entry(lobj, head, list) {
335 lobj->reserved = false;
336 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100337 r = radeon_bo_list_reserve(head);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200338 if (unlikely(r != 0)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200339 return r;
340 }
Dave Airlie9d8401f2009-10-08 09:28:19 +1000341 list_for_each_entry(lobj, head, list) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100342 bo = lobj->bo;
343 if (!bo->pin_count) {
Michel Dänzere3765732010-07-08 12:43:28 +1000344 domain = lobj->wdomain ? lobj->wdomain : lobj->rdomain;
345
346 retry:
347 radeon_ttm_placement_from_domain(bo, domain);
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100348 r = ttm_bo_validate(&bo->tbo, &bo->placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000349 true, false, false);
Michel Dänzere3765732010-07-08 12:43:28 +1000350 if (unlikely(r)) {
351 if (r != -ERESTARTSYS && domain == RADEON_GEM_DOMAIN_VRAM) {
352 domain |= RADEON_GEM_DOMAIN_GTT;
353 goto retry;
354 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200355 return r;
Michel Dänzere3765732010-07-08 12:43:28 +1000356 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200357 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100358 lobj->gpu_offset = radeon_bo_gpu_offset(bo);
359 lobj->tiling_flags = bo->tiling_flags;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200360 }
361 return 0;
362}
363
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100364void radeon_bo_list_fence(struct list_head *head, void *fence)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200365{
Jerome Glisse4c788672009-11-20 14:29:23 +0100366 struct radeon_bo_list *lobj;
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100367 struct radeon_bo *bo;
368 struct radeon_fence *old_fence = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200369
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100370 list_for_each_entry(lobj, head, list) {
371 bo = lobj->bo;
372 spin_lock(&bo->tbo.lock);
373 old_fence = (struct radeon_fence *)bo->tbo.sync_obj;
374 bo->tbo.sync_obj = radeon_fence_ref(fence);
375 bo->tbo.sync_obj_arg = NULL;
376 spin_unlock(&bo->tbo.lock);
377 if (old_fence) {
378 radeon_fence_unref(&old_fence);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200379 }
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100380 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200381}
382
Jerome Glisse4c788672009-11-20 14:29:23 +0100383int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200384 struct vm_area_struct *vma)
385{
Jerome Glisse4c788672009-11-20 14:29:23 +0100386 return ttm_fbdev_mmap(vma, &bo->tbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200387}
388
Dave Airlie550e2d92009-12-09 14:15:38 +1000389int radeon_bo_get_surface_reg(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200390{
Jerome Glisse4c788672009-11-20 14:29:23 +0100391 struct radeon_device *rdev = bo->rdev;
Dave Airliee024e112009-06-24 09:48:08 +1000392 struct radeon_surface_reg *reg;
Jerome Glisse4c788672009-11-20 14:29:23 +0100393 struct radeon_bo *old_object;
Dave Airliee024e112009-06-24 09:48:08 +1000394 int steal;
395 int i;
396
Jerome Glisse4c788672009-11-20 14:29:23 +0100397 BUG_ON(!atomic_read(&bo->tbo.reserved));
398
399 if (!bo->tiling_flags)
Dave Airliee024e112009-06-24 09:48:08 +1000400 return 0;
401
Jerome Glisse4c788672009-11-20 14:29:23 +0100402 if (bo->surface_reg >= 0) {
403 reg = &rdev->surface_regs[bo->surface_reg];
404 i = bo->surface_reg;
Dave Airliee024e112009-06-24 09:48:08 +1000405 goto out;
406 }
407
408 steal = -1;
409 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
410
411 reg = &rdev->surface_regs[i];
Jerome Glisse4c788672009-11-20 14:29:23 +0100412 if (!reg->bo)
Dave Airliee024e112009-06-24 09:48:08 +1000413 break;
414
Jerome Glisse4c788672009-11-20 14:29:23 +0100415 old_object = reg->bo;
Dave Airliee024e112009-06-24 09:48:08 +1000416 if (old_object->pin_count == 0)
417 steal = i;
418 }
419
420 /* if we are all out */
421 if (i == RADEON_GEM_MAX_SURFACES) {
422 if (steal == -1)
423 return -ENOMEM;
424 /* find someone with a surface reg and nuke their BO */
425 reg = &rdev->surface_regs[steal];
Jerome Glisse4c788672009-11-20 14:29:23 +0100426 old_object = reg->bo;
Dave Airliee024e112009-06-24 09:48:08 +1000427 /* blow away the mapping */
428 DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
Jerome Glisse4c788672009-11-20 14:29:23 +0100429 ttm_bo_unmap_virtual(&old_object->tbo);
Dave Airliee024e112009-06-24 09:48:08 +1000430 old_object->surface_reg = -1;
431 i = steal;
432 }
433
Jerome Glisse4c788672009-11-20 14:29:23 +0100434 bo->surface_reg = i;
435 reg->bo = bo;
Dave Airliee024e112009-06-24 09:48:08 +1000436
437out:
Jerome Glisse4c788672009-11-20 14:29:23 +0100438 radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
Ben Skeggsd961db72010-08-05 10:48:18 +1000439 bo->tbo.mem.start << PAGE_SHIFT,
Jerome Glisse4c788672009-11-20 14:29:23 +0100440 bo->tbo.num_pages << PAGE_SHIFT);
Dave Airliee024e112009-06-24 09:48:08 +1000441 return 0;
442}
443
Jerome Glisse4c788672009-11-20 14:29:23 +0100444static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
Dave Airliee024e112009-06-24 09:48:08 +1000445{
Jerome Glisse4c788672009-11-20 14:29:23 +0100446 struct radeon_device *rdev = bo->rdev;
Dave Airliee024e112009-06-24 09:48:08 +1000447 struct radeon_surface_reg *reg;
448
Jerome Glisse4c788672009-11-20 14:29:23 +0100449 if (bo->surface_reg == -1)
Dave Airliee024e112009-06-24 09:48:08 +1000450 return;
451
Jerome Glisse4c788672009-11-20 14:29:23 +0100452 reg = &rdev->surface_regs[bo->surface_reg];
453 radeon_clear_surface_reg(rdev, bo->surface_reg);
Dave Airliee024e112009-06-24 09:48:08 +1000454
Jerome Glisse4c788672009-11-20 14:29:23 +0100455 reg->bo = NULL;
456 bo->surface_reg = -1;
Dave Airliee024e112009-06-24 09:48:08 +1000457}
458
Jerome Glisse4c788672009-11-20 14:29:23 +0100459int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
460 uint32_t tiling_flags, uint32_t pitch)
Dave Airliee024e112009-06-24 09:48:08 +1000461{
Jerome Glisse4c788672009-11-20 14:29:23 +0100462 int r;
463
464 r = radeon_bo_reserve(bo, false);
465 if (unlikely(r != 0))
466 return r;
467 bo->tiling_flags = tiling_flags;
468 bo->pitch = pitch;
469 radeon_bo_unreserve(bo);
470 return 0;
Dave Airliee024e112009-06-24 09:48:08 +1000471}
472
Jerome Glisse4c788672009-11-20 14:29:23 +0100473void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
474 uint32_t *tiling_flags,
475 uint32_t *pitch)
Dave Airliee024e112009-06-24 09:48:08 +1000476{
Jerome Glisse4c788672009-11-20 14:29:23 +0100477 BUG_ON(!atomic_read(&bo->tbo.reserved));
Dave Airliee024e112009-06-24 09:48:08 +1000478 if (tiling_flags)
Jerome Glisse4c788672009-11-20 14:29:23 +0100479 *tiling_flags = bo->tiling_flags;
Dave Airliee024e112009-06-24 09:48:08 +1000480 if (pitch)
Jerome Glisse4c788672009-11-20 14:29:23 +0100481 *pitch = bo->pitch;
Dave Airliee024e112009-06-24 09:48:08 +1000482}
483
Jerome Glisse4c788672009-11-20 14:29:23 +0100484int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
485 bool force_drop)
Dave Airliee024e112009-06-24 09:48:08 +1000486{
Jerome Glisse4c788672009-11-20 14:29:23 +0100487 BUG_ON(!atomic_read(&bo->tbo.reserved));
488
489 if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
Dave Airliee024e112009-06-24 09:48:08 +1000490 return 0;
491
492 if (force_drop) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100493 radeon_bo_clear_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000494 return 0;
495 }
496
Jerome Glisse4c788672009-11-20 14:29:23 +0100497 if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
Dave Airliee024e112009-06-24 09:48:08 +1000498 if (!has_moved)
499 return 0;
500
Jerome Glisse4c788672009-11-20 14:29:23 +0100501 if (bo->surface_reg >= 0)
502 radeon_bo_clear_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000503 return 0;
504 }
505
Jerome Glisse4c788672009-11-20 14:29:23 +0100506 if ((bo->surface_reg >= 0) && !has_moved)
Dave Airliee024e112009-06-24 09:48:08 +1000507 return 0;
508
Jerome Glisse4c788672009-11-20 14:29:23 +0100509 return radeon_bo_get_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000510}
511
512void radeon_bo_move_notify(struct ttm_buffer_object *bo,
Jerome Glissed03d8582009-12-14 21:02:09 +0100513 struct ttm_mem_reg *mem)
Dave Airliee024e112009-06-24 09:48:08 +1000514{
Jerome Glissed03d8582009-12-14 21:02:09 +0100515 struct radeon_bo *rbo;
516 if (!radeon_ttm_bo_is_radeon_bo(bo))
517 return;
518 rbo = container_of(bo, struct radeon_bo, tbo);
Jerome Glisse4c788672009-11-20 14:29:23 +0100519 radeon_bo_check_tiling(rbo, 0, 1);
Dave Airliee024e112009-06-24 09:48:08 +1000520}
521
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200522int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
Dave Airliee024e112009-06-24 09:48:08 +1000523{
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200524 struct radeon_device *rdev;
Jerome Glissed03d8582009-12-14 21:02:09 +0100525 struct radeon_bo *rbo;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200526 unsigned long offset, size;
527 int r;
528
Jerome Glissed03d8582009-12-14 21:02:09 +0100529 if (!radeon_ttm_bo_is_radeon_bo(bo))
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200530 return 0;
Jerome Glissed03d8582009-12-14 21:02:09 +0100531 rbo = container_of(bo, struct radeon_bo, tbo);
Jerome Glisse4c788672009-11-20 14:29:23 +0100532 radeon_bo_check_tiling(rbo, 0, 0);
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200533 rdev = rbo->rdev;
534 if (bo->mem.mem_type == TTM_PL_VRAM) {
535 size = bo->mem.num_pages << PAGE_SHIFT;
Ben Skeggsd961db72010-08-05 10:48:18 +1000536 offset = bo->mem.start << PAGE_SHIFT;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200537 if ((offset + size) > rdev->mc.visible_vram_size) {
538 /* hurrah the memory is not visible ! */
539 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
540 rbo->placement.lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT;
541 r = ttm_bo_validate(bo, &rbo->placement, false, true, false);
542 if (unlikely(r != 0))
543 return r;
Ben Skeggsd961db72010-08-05 10:48:18 +1000544 offset = bo->mem.start << PAGE_SHIFT;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200545 /* this should not happen */
546 if ((offset + size) > rdev->mc.visible_vram_size)
547 return -EINVAL;
548 }
549 }
550 return 0;
Dave Airliee024e112009-06-24 09:48:08 +1000551}