blob: c26106066ec2fdf39a3a1d7514fa210dec147421 [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;
72 rbo->placement.lpfn = 0;
73 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,
89 unsigned long size, 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;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020094 int r;
95
96 if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
97 rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
98 }
99 if (kernel) {
100 type = ttm_bo_type_kernel;
101 } else {
102 type = ttm_bo_type_device;
103 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100104 *bo_ptr = NULL;
105 bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
106 if (bo == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200107 return -ENOMEM;
Jerome Glisse4c788672009-11-20 14:29:23 +0100108 bo->rdev = rdev;
109 bo->gobj = gobj;
110 bo->surface_reg = -1;
111 INIT_LIST_HEAD(&bo->list);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200112
Michel Dänzere376573f2010-07-08 12:43:28 +1000113retry:
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100114 radeon_ttm_placement_from_domain(bo, domain);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100115 /* Kernel allocation are uninterruptible */
Matthew Garrett5876dd22010-04-26 15:52:20 -0400116 mutex_lock(&rdev->vram_mutex);
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100117 r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
118 &bo->placement, 0, 0, !kernel, NULL, size,
119 &radeon_ttm_bo_destroy);
Matthew Garrett5876dd22010-04-26 15:52:20 -0400120 mutex_unlock(&rdev->vram_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200121 if (unlikely(r != 0)) {
Michel Dänzere376573f2010-07-08 12:43:28 +1000122 if (r != -ERESTARTSYS) {
123 if (domain == RADEON_GEM_DOMAIN_VRAM) {
124 domain |= RADEON_GEM_DOMAIN_GTT;
125 goto retry;
126 }
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100127 dev_err(rdev->dev,
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100128 "object_init failed for (%lu, 0x%08X)\n",
129 size, domain);
Michel Dänzere376573f2010-07-08 12:43:28 +1000130 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200131 return r;
132 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100133 *bo_ptr = bo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200134 if (gobj) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100135 mutex_lock(&bo->rdev->gem.mutex);
136 list_add_tail(&bo->list, &rdev->gem.objects);
137 mutex_unlock(&bo->rdev->gem.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200138 }
139 return 0;
140}
141
Jerome Glisse4c788672009-11-20 14:29:23 +0100142int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200143{
Jerome Glisse4c788672009-11-20 14:29:23 +0100144 bool is_iomem;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200145 int r;
146
Jerome Glisse4c788672009-11-20 14:29:23 +0100147 if (bo->kptr) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200148 if (ptr) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100149 *ptr = bo->kptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200150 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200151 return 0;
152 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100153 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200154 if (r) {
155 return r;
156 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100157 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200158 if (ptr) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100159 *ptr = bo->kptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200160 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100161 radeon_bo_check_tiling(bo, 0, 0);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200162 return 0;
163}
164
Jerome Glisse4c788672009-11-20 14:29:23 +0100165void radeon_bo_kunmap(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200166{
Jerome Glisse4c788672009-11-20 14:29:23 +0100167 if (bo->kptr == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200168 return;
Jerome Glisse4c788672009-11-20 14:29:23 +0100169 bo->kptr = NULL;
170 radeon_bo_check_tiling(bo, 0, 0);
171 ttm_bo_kunmap(&bo->kmap);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200172}
173
Jerome Glisse4c788672009-11-20 14:29:23 +0100174void radeon_bo_unref(struct radeon_bo **bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175{
Jerome Glisse4c788672009-11-20 14:29:23 +0100176 struct ttm_buffer_object *tbo;
Dave Airlief4b7fb92010-04-29 18:37:59 +1000177 struct radeon_device *rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200178
Jerome Glisse4c788672009-11-20 14:29:23 +0100179 if ((*bo) == NULL)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200180 return;
Dave Airlief4b7fb92010-04-29 18:37:59 +1000181 rdev = (*bo)->rdev;
Jerome Glisse4c788672009-11-20 14:29:23 +0100182 tbo = &((*bo)->tbo);
Dave Airlief4b7fb92010-04-29 18:37:59 +1000183 mutex_lock(&rdev->vram_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100184 ttm_bo_unref(&tbo);
Dave Airlief4b7fb92010-04-29 18:37:59 +1000185 mutex_unlock(&rdev->vram_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100186 if (tbo == NULL)
187 *bo = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200188}
189
Jerome Glisse4c788672009-11-20 14:29:23 +0100190int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200191{
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100192 int r, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200193
Jerome Glisse4c788672009-11-20 14:29:23 +0100194 if (bo->pin_count) {
195 bo->pin_count++;
196 if (gpu_addr)
197 *gpu_addr = radeon_bo_gpu_offset(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200198 return 0;
199 }
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100200 radeon_ttm_placement_from_domain(bo, domain);
Michel Dänzer3ca82da2010-03-26 19:18:55 +0000201 if (domain == RADEON_GEM_DOMAIN_VRAM) {
202 /* force to pin into visible video ram */
203 bo->placement.lpfn = bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
204 }
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100205 for (i = 0; i < bo->placement.num_placement; i++)
206 bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000207 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100208 if (likely(r == 0)) {
209 bo->pin_count = 1;
210 if (gpu_addr != NULL)
211 *gpu_addr = radeon_bo_gpu_offset(bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200212 }
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100213 if (unlikely(r != 0))
Jerome Glisse4c788672009-11-20 14:29:23 +0100214 dev_err(bo->rdev->dev, "%p pin failed\n", bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200215 return r;
216}
217
Jerome Glisse4c788672009-11-20 14:29:23 +0100218int radeon_bo_unpin(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200219{
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100220 int r, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200221
Jerome Glisse4c788672009-11-20 14:29:23 +0100222 if (!bo->pin_count) {
223 dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
224 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200225 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100226 bo->pin_count--;
227 if (bo->pin_count)
228 return 0;
Jerome Glisse312ea8d2009-12-07 15:52:58 +0100229 for (i = 0; i < bo->placement.num_placement; i++)
230 bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000231 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100232 if (unlikely(r != 0))
Jerome Glisse4c788672009-11-20 14:29:23 +0100233 dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100234 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200235}
236
Jerome Glisse4c788672009-11-20 14:29:23 +0100237int radeon_bo_evict_vram(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200238{
Dave Airlied796d842010-01-25 13:08:08 +1000239 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
240 if (0 && (rdev->flags & RADEON_IS_IGP)) {
Alex Deucher06b64762010-01-05 11:27:29 -0500241 if (rdev->mc.igp_sideport_enabled == false)
242 /* Useless to evict on IGP chips */
243 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200244 }
245 return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
246}
247
Jerome Glisse4c788672009-11-20 14:29:23 +0100248void radeon_bo_force_delete(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200249{
Jerome Glisse4c788672009-11-20 14:29:23 +0100250 struct radeon_bo *bo, *n;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200251 struct drm_gem_object *gobj;
252
253 if (list_empty(&rdev->gem.objects)) {
254 return;
255 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100256 dev_err(rdev->dev, "Userspace still has active objects !\n");
257 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200258 mutex_lock(&rdev->ddev->struct_mutex);
Jerome Glisse4c788672009-11-20 14:29:23 +0100259 gobj = bo->gobj;
260 dev_err(rdev->dev, "%p %p %lu %lu force free\n",
261 gobj, bo, (unsigned long)gobj->size,
262 *((unsigned long *)&gobj->refcount));
263 mutex_lock(&bo->rdev->gem.mutex);
264 list_del_init(&bo->list);
265 mutex_unlock(&bo->rdev->gem.mutex);
266 radeon_bo_unref(&bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200267 gobj->driver_private = NULL;
268 drm_gem_object_unreference(gobj);
269 mutex_unlock(&rdev->ddev->struct_mutex);
270 }
271}
272
Jerome Glisse4c788672009-11-20 14:29:23 +0100273int radeon_bo_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200274{
Jerome Glissea4d68272009-09-11 13:00:43 +0200275 /* Add an MTRR for the VRAM */
276 rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
277 MTRR_TYPE_WRCOMB, 1);
278 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
279 rdev->mc.mc_vram_size >> 20,
280 (unsigned long long)rdev->mc.aper_size >> 20);
281 DRM_INFO("RAM width %dbits %cDR\n",
282 rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200283 return radeon_ttm_init(rdev);
284}
285
Jerome Glisse4c788672009-11-20 14:29:23 +0100286void radeon_bo_fini(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200287{
288 radeon_ttm_fini(rdev);
289}
290
Jerome Glisse4c788672009-11-20 14:29:23 +0100291void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
292 struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200293{
294 if (lobj->wdomain) {
295 list_add(&lobj->list, head);
296 } else {
297 list_add_tail(&lobj->list, head);
298 }
299}
300
Jerome Glisse4c788672009-11-20 14:29:23 +0100301int radeon_bo_list_reserve(struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200302{
Jerome Glisse4c788672009-11-20 14:29:23 +0100303 struct radeon_bo_list *lobj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200304 int r;
305
Dave Airlie9d8401f2009-10-08 09:28:19 +1000306 list_for_each_entry(lobj, head, list){
Jerome Glisse4c788672009-11-20 14:29:23 +0100307 r = radeon_bo_reserve(lobj->bo, false);
308 if (unlikely(r != 0))
309 return r;
Jerome Glissee8652752010-05-19 16:05:50 +0200310 lobj->reserved = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200311 }
312 return 0;
313}
314
Jerome Glisse4c788672009-11-20 14:29:23 +0100315void radeon_bo_list_unreserve(struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200316{
Jerome Glisse4c788672009-11-20 14:29:23 +0100317 struct radeon_bo_list *lobj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200318
Dave Airlie9d8401f2009-10-08 09:28:19 +1000319 list_for_each_entry(lobj, head, list) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100320 /* only unreserve object we successfully reserved */
Jerome Glissee8652752010-05-19 16:05:50 +0200321 if (lobj->reserved && radeon_bo_is_reserved(lobj->bo))
Jerome Glisse4c788672009-11-20 14:29:23 +0100322 radeon_bo_unreserve(lobj->bo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200323 }
324}
325
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100326int radeon_bo_list_validate(struct list_head *head)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200327{
Jerome Glisse4c788672009-11-20 14:29:23 +0100328 struct radeon_bo_list *lobj;
329 struct radeon_bo *bo;
Michel Dänzere376573f2010-07-08 12:43:28 +1000330 u32 domain;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200331 int r;
332
Jerome Glissee8652752010-05-19 16:05:50 +0200333 list_for_each_entry(lobj, head, list) {
334 lobj->reserved = false;
335 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100336 r = radeon_bo_list_reserve(head);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200337 if (unlikely(r != 0)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200338 return r;
339 }
Dave Airlie9d8401f2009-10-08 09:28:19 +1000340 list_for_each_entry(lobj, head, list) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100341 bo = lobj->bo;
342 if (!bo->pin_count) {
Michel Dänzere376573f2010-07-08 12:43:28 +1000343 domain = lobj->wdomain ? lobj->wdomain : lobj->rdomain;
344
345 retry:
346 radeon_ttm_placement_from_domain(bo, domain);
Jerome Glisse1fb107f2009-12-10 17:16:28 +0100347 r = ttm_bo_validate(&bo->tbo, &bo->placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000348 true, false, false);
Michel Dänzere376573f2010-07-08 12:43:28 +1000349 if (unlikely(r)) {
350 if (r != -ERESTARTSYS && domain == RADEON_GEM_DOMAIN_VRAM) {
351 domain |= RADEON_GEM_DOMAIN_GTT;
352 goto retry;
353 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200354 return r;
Michel Dänzere376573f2010-07-08 12:43:28 +1000355 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200356 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100357 lobj->gpu_offset = radeon_bo_gpu_offset(bo);
358 lobj->tiling_flags = bo->tiling_flags;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200359 }
360 return 0;
361}
362
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100363void radeon_bo_list_fence(struct list_head *head, void *fence)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200364{
Jerome Glisse4c788672009-11-20 14:29:23 +0100365 struct radeon_bo_list *lobj;
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100366 struct radeon_bo *bo;
367 struct radeon_fence *old_fence = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200368
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100369 list_for_each_entry(lobj, head, list) {
370 bo = lobj->bo;
371 spin_lock(&bo->tbo.lock);
372 old_fence = (struct radeon_fence *)bo->tbo.sync_obj;
373 bo->tbo.sync_obj = radeon_fence_ref(fence);
374 bo->tbo.sync_obj_arg = NULL;
375 spin_unlock(&bo->tbo.lock);
376 if (old_fence) {
377 radeon_fence_unref(&old_fence);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200378 }
Jerome Glisse6cb8e1f2010-02-15 21:36:33 +0100379 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200380}
381
Jerome Glisse4c788672009-11-20 14:29:23 +0100382int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200383 struct vm_area_struct *vma)
384{
Jerome Glisse4c788672009-11-20 14:29:23 +0100385 return ttm_fbdev_mmap(vma, &bo->tbo);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386}
387
Dave Airlie550e2d92009-12-09 14:15:38 +1000388int radeon_bo_get_surface_reg(struct radeon_bo *bo)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200389{
Jerome Glisse4c788672009-11-20 14:29:23 +0100390 struct radeon_device *rdev = bo->rdev;
Dave Airliee024e112009-06-24 09:48:08 +1000391 struct radeon_surface_reg *reg;
Jerome Glisse4c788672009-11-20 14:29:23 +0100392 struct radeon_bo *old_object;
Dave Airliee024e112009-06-24 09:48:08 +1000393 int steal;
394 int i;
395
Jerome Glisse4c788672009-11-20 14:29:23 +0100396 BUG_ON(!atomic_read(&bo->tbo.reserved));
397
398 if (!bo->tiling_flags)
Dave Airliee024e112009-06-24 09:48:08 +1000399 return 0;
400
Jerome Glisse4c788672009-11-20 14:29:23 +0100401 if (bo->surface_reg >= 0) {
402 reg = &rdev->surface_regs[bo->surface_reg];
403 i = bo->surface_reg;
Dave Airliee024e112009-06-24 09:48:08 +1000404 goto out;
405 }
406
407 steal = -1;
408 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
409
410 reg = &rdev->surface_regs[i];
Jerome Glisse4c788672009-11-20 14:29:23 +0100411 if (!reg->bo)
Dave Airliee024e112009-06-24 09:48:08 +1000412 break;
413
Jerome Glisse4c788672009-11-20 14:29:23 +0100414 old_object = reg->bo;
Dave Airliee024e112009-06-24 09:48:08 +1000415 if (old_object->pin_count == 0)
416 steal = i;
417 }
418
419 /* if we are all out */
420 if (i == RADEON_GEM_MAX_SURFACES) {
421 if (steal == -1)
422 return -ENOMEM;
423 /* find someone with a surface reg and nuke their BO */
424 reg = &rdev->surface_regs[steal];
Jerome Glisse4c788672009-11-20 14:29:23 +0100425 old_object = reg->bo;
Dave Airliee024e112009-06-24 09:48:08 +1000426 /* blow away the mapping */
427 DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
Jerome Glisse4c788672009-11-20 14:29:23 +0100428 ttm_bo_unmap_virtual(&old_object->tbo);
Dave Airliee024e112009-06-24 09:48:08 +1000429 old_object->surface_reg = -1;
430 i = steal;
431 }
432
Jerome Glisse4c788672009-11-20 14:29:23 +0100433 bo->surface_reg = i;
434 reg->bo = bo;
Dave Airliee024e112009-06-24 09:48:08 +1000435
436out:
Jerome Glisse4c788672009-11-20 14:29:23 +0100437 radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
Ben Skeggsd961db72010-08-05 10:48:18 +1000438 bo->tbo.mem.start << PAGE_SHIFT,
Jerome Glisse4c788672009-11-20 14:29:23 +0100439 bo->tbo.num_pages << PAGE_SHIFT);
Dave Airliee024e112009-06-24 09:48:08 +1000440 return 0;
441}
442
Jerome Glisse4c788672009-11-20 14:29:23 +0100443static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
Dave Airliee024e112009-06-24 09:48:08 +1000444{
Jerome Glisse4c788672009-11-20 14:29:23 +0100445 struct radeon_device *rdev = bo->rdev;
Dave Airliee024e112009-06-24 09:48:08 +1000446 struct radeon_surface_reg *reg;
447
Jerome Glisse4c788672009-11-20 14:29:23 +0100448 if (bo->surface_reg == -1)
Dave Airliee024e112009-06-24 09:48:08 +1000449 return;
450
Jerome Glisse4c788672009-11-20 14:29:23 +0100451 reg = &rdev->surface_regs[bo->surface_reg];
452 radeon_clear_surface_reg(rdev, bo->surface_reg);
Dave Airliee024e112009-06-24 09:48:08 +1000453
Jerome Glisse4c788672009-11-20 14:29:23 +0100454 reg->bo = NULL;
455 bo->surface_reg = -1;
Dave Airliee024e112009-06-24 09:48:08 +1000456}
457
Jerome Glisse4c788672009-11-20 14:29:23 +0100458int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
459 uint32_t tiling_flags, uint32_t pitch)
Dave Airliee024e112009-06-24 09:48:08 +1000460{
Jerome Glisse4c788672009-11-20 14:29:23 +0100461 int r;
462
463 r = radeon_bo_reserve(bo, false);
464 if (unlikely(r != 0))
465 return r;
466 bo->tiling_flags = tiling_flags;
467 bo->pitch = pitch;
468 radeon_bo_unreserve(bo);
469 return 0;
Dave Airliee024e112009-06-24 09:48:08 +1000470}
471
Jerome Glisse4c788672009-11-20 14:29:23 +0100472void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
473 uint32_t *tiling_flags,
474 uint32_t *pitch)
Dave Airliee024e112009-06-24 09:48:08 +1000475{
Jerome Glisse4c788672009-11-20 14:29:23 +0100476 BUG_ON(!atomic_read(&bo->tbo.reserved));
Dave Airliee024e112009-06-24 09:48:08 +1000477 if (tiling_flags)
Jerome Glisse4c788672009-11-20 14:29:23 +0100478 *tiling_flags = bo->tiling_flags;
Dave Airliee024e112009-06-24 09:48:08 +1000479 if (pitch)
Jerome Glisse4c788672009-11-20 14:29:23 +0100480 *pitch = bo->pitch;
Dave Airliee024e112009-06-24 09:48:08 +1000481}
482
Jerome Glisse4c788672009-11-20 14:29:23 +0100483int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
484 bool force_drop)
Dave Airliee024e112009-06-24 09:48:08 +1000485{
Jerome Glisse4c788672009-11-20 14:29:23 +0100486 BUG_ON(!atomic_read(&bo->tbo.reserved));
487
488 if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
Dave Airliee024e112009-06-24 09:48:08 +1000489 return 0;
490
491 if (force_drop) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100492 radeon_bo_clear_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000493 return 0;
494 }
495
Jerome Glisse4c788672009-11-20 14:29:23 +0100496 if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
Dave Airliee024e112009-06-24 09:48:08 +1000497 if (!has_moved)
498 return 0;
499
Jerome Glisse4c788672009-11-20 14:29:23 +0100500 if (bo->surface_reg >= 0)
501 radeon_bo_clear_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000502 return 0;
503 }
504
Jerome Glisse4c788672009-11-20 14:29:23 +0100505 if ((bo->surface_reg >= 0) && !has_moved)
Dave Airliee024e112009-06-24 09:48:08 +1000506 return 0;
507
Jerome Glisse4c788672009-11-20 14:29:23 +0100508 return radeon_bo_get_surface_reg(bo);
Dave Airliee024e112009-06-24 09:48:08 +1000509}
510
511void radeon_bo_move_notify(struct ttm_buffer_object *bo,
Jerome Glissed03d8582009-12-14 21:02:09 +0100512 struct ttm_mem_reg *mem)
Dave Airliee024e112009-06-24 09:48:08 +1000513{
Jerome Glissed03d8582009-12-14 21:02:09 +0100514 struct radeon_bo *rbo;
515 if (!radeon_ttm_bo_is_radeon_bo(bo))
516 return;
517 rbo = container_of(bo, struct radeon_bo, tbo);
Jerome Glisse4c788672009-11-20 14:29:23 +0100518 radeon_bo_check_tiling(rbo, 0, 1);
Dave Airliee024e112009-06-24 09:48:08 +1000519}
520
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200521int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
Dave Airliee024e112009-06-24 09:48:08 +1000522{
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200523 struct radeon_device *rdev;
Jerome Glissed03d8582009-12-14 21:02:09 +0100524 struct radeon_bo *rbo;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200525 unsigned long offset, size;
526 int r;
527
Jerome Glissed03d8582009-12-14 21:02:09 +0100528 if (!radeon_ttm_bo_is_radeon_bo(bo))
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200529 return 0;
Jerome Glissed03d8582009-12-14 21:02:09 +0100530 rbo = container_of(bo, struct radeon_bo, tbo);
Jerome Glisse4c788672009-11-20 14:29:23 +0100531 radeon_bo_check_tiling(rbo, 0, 0);
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200532 rdev = rbo->rdev;
533 if (bo->mem.mem_type == TTM_PL_VRAM) {
534 size = bo->mem.num_pages << PAGE_SHIFT;
Ben Skeggsd961db72010-08-05 10:48:18 +1000535 offset = bo->mem.start << PAGE_SHIFT;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200536 if ((offset + size) > rdev->mc.visible_vram_size) {
537 /* hurrah the memory is not visible ! */
538 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
539 rbo->placement.lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT;
540 r = ttm_bo_validate(bo, &rbo->placement, false, true, false);
541 if (unlikely(r != 0))
542 return r;
Ben Skeggsd961db72010-08-05 10:48:18 +1000543 offset = bo->mem.start << PAGE_SHIFT;
Jerome Glisse0a2d50e2010-04-09 14:39:24 +0200544 /* this should not happen */
545 if ((offset + size) > rdev->mc.visible_vram_size)
546 return -EINVAL;
547 }
548 }
549 return 0;
Dave Airliee024e112009-06-24 09:48:08 +1000550}