radeon_object.c 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright 2009 Jerome Glisse.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 */
/*
 * Authors:
 *    Jerome Glisse <glisse@freedesktop.org>
 *    Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
 *    Dave Airlie
 */
#include <linux/list.h>
33
#include <linux/slab.h>
34 35 36
#include <drm/drmP.h>
#include "radeon_drm.h"
#include "radeon.h"
37
#include "radeon_trace.h"
38 39 40 41


int radeon_ttm_init(struct radeon_device *rdev);
void radeon_ttm_fini(struct radeon_device *rdev);
42
static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
43 44 45 46 47 48

/*
 * To exclude mutual BO access we rely on bo_reserve exclusion, as all
 * function are calling it.
 */

49
static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
50
{
51
	struct radeon_bo *bo;
52

53 54 55 56 57 58
	bo = container_of(tbo, struct radeon_bo, tbo);
	mutex_lock(&bo->rdev->gem.mutex);
	list_del_init(&bo->list);
	mutex_unlock(&bo->rdev->gem.mutex);
	radeon_bo_clear_surface_reg(bo);
	kfree(bo);
59 60
}

61 62 63 64 65 66 67
bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo)
{
	if (bo->destroy == &radeon_ttm_bo_destroy)
		return true;
	return false;
}

68 69 70 71 72
void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
{
	u32 c = 0;

	rbo->placement.fpfn = 0;
73
	rbo->placement.lpfn = 0;
74 75 76 77 78 79 80 81 82
	rbo->placement.placement = rbo->placements;
	rbo->placement.busy_placement = rbo->placements;
	if (domain & RADEON_GEM_DOMAIN_VRAM)
		rbo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
					TTM_PL_FLAG_VRAM;
	if (domain & RADEON_GEM_DOMAIN_GTT)
		rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
	if (domain & RADEON_GEM_DOMAIN_CPU)
		rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
83 84
	if (!c)
		rbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
85 86 87 88
	rbo->placement.num_placement = c;
	rbo->placement.num_busy_placement = c;
}

89
int radeon_bo_create(struct radeon_device *rdev, struct drm_gem_object *gobj,
90 91
		     unsigned long size, int byte_align, bool kernel, u32 domain,
		     struct radeon_bo **bo_ptr)
92
{
93
	struct radeon_bo *bo;
94
	enum ttm_bo_type type;
95 96
	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
	unsigned long max_size = 0;
97 98 99 100 101 102 103 104 105 106
	int r;

	if (unlikely(rdev->mman.bdev.dev_mapping == NULL)) {
		rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
	}
	if (kernel) {
		type = ttm_bo_type_kernel;
	} else {
		type = ttm_bo_type_device;
	}
107
	*bo_ptr = NULL;
108

109 110 111 112 113 114 115 116
	/* maximun bo size is the minimun btw visible vram and gtt size */
	max_size = min(rdev->mc.visible_vram_size, rdev->mc.gtt_size);
	if ((page_align << PAGE_SHIFT) >= max_size) {
		printk(KERN_WARNING "%s:%d alloc size %ldM bigger than %ldMb limit\n",
			__func__, __LINE__, page_align  >> (20 - PAGE_SHIFT), max_size >> 20);
		return -ENOMEM;
	}

117
retry:
118 119
	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
	if (bo == NULL)
120
		return -ENOMEM;
121 122 123 124
	bo->rdev = rdev;
	bo->gobj = gobj;
	bo->surface_reg = -1;
	INIT_LIST_HEAD(&bo->list);
125
	radeon_ttm_placement_from_domain(bo, domain);
126
	/* Kernel allocation are uninterruptible */
127
	mutex_lock(&rdev->vram_mutex);
128
	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
129
			&bo->placement, page_align, 0, !kernel, NULL, size,
130
			&radeon_ttm_bo_destroy);
131
	mutex_unlock(&rdev->vram_mutex);
132
	if (unlikely(r != 0)) {
133 134 135 136 137
		if (r != -ERESTARTSYS) {
			if (domain == RADEON_GEM_DOMAIN_VRAM) {
				domain |= RADEON_GEM_DOMAIN_GTT;
				goto retry;
			}
138
			dev_err(rdev->dev,
139 140
				"object_init failed for (%lu, 0x%08X)\n",
				size, domain);
141
		}
142 143
		return r;
	}
144
	*bo_ptr = bo;
145
	if (gobj) {
146 147 148
		mutex_lock(&bo->rdev->gem.mutex);
		list_add_tail(&bo->list, &rdev->gem.objects);
		mutex_unlock(&bo->rdev->gem.mutex);
149
	}
150
	trace_radeon_bo_create(bo);
151 152 153
	return 0;
}

154
int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
155
{
156
	bool is_iomem;
157 158
	int r;

159
	if (bo->kptr) {
160
		if (ptr) {
161
			*ptr = bo->kptr;
162 163 164
		}
		return 0;
	}
165
	r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
166 167 168
	if (r) {
		return r;
	}
169
	bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
170
	if (ptr) {
171
		*ptr = bo->kptr;
172
	}
173
	radeon_bo_check_tiling(bo, 0, 0);
174 175 176
	return 0;
}

177
void radeon_bo_kunmap(struct radeon_bo *bo)
178
{
179
	if (bo->kptr == NULL)
180
		return;
181 182 183
	bo->kptr = NULL;
	radeon_bo_check_tiling(bo, 0, 0);
	ttm_bo_kunmap(&bo->kmap);
184 185
}

186
void radeon_bo_unref(struct radeon_bo **bo)
187
{
188
	struct ttm_buffer_object *tbo;
189
	struct radeon_device *rdev;
190

191
	if ((*bo) == NULL)
192
		return;
193
	rdev = (*bo)->rdev;
194
	tbo = &((*bo)->tbo);
195
	mutex_lock(&rdev->vram_mutex);
196
	ttm_bo_unref(&tbo);
197
	mutex_unlock(&rdev->vram_mutex);
198 199
	if (tbo == NULL)
		*bo = NULL;
200 201
}

202
int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
203
{
204
	int r, i;
205

206 207 208 209
	if (bo->pin_count) {
		bo->pin_count++;
		if (gpu_addr)
			*gpu_addr = radeon_bo_gpu_offset(bo);
210 211
		return 0;
	}
212
	radeon_ttm_placement_from_domain(bo, domain);
213 214 215 216
	if (domain == RADEON_GEM_DOMAIN_VRAM) {
		/* force to pin into visible video ram */
		bo->placement.lpfn = bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
	}
217 218
	for (i = 0; i < bo->placement.num_placement; i++)
		bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
219
	r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
220 221 222 223
	if (likely(r == 0)) {
		bo->pin_count = 1;
		if (gpu_addr != NULL)
			*gpu_addr = radeon_bo_gpu_offset(bo);
224
	}
225
	if (unlikely(r != 0))
226
		dev_err(bo->rdev->dev, "%p pin failed\n", bo);
227 228 229
	return r;
}

230
int radeon_bo_unpin(struct radeon_bo *bo)
231
{
232
	int r, i;
233

234 235 236
	if (!bo->pin_count) {
		dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
		return 0;
237
	}
238 239 240
	bo->pin_count--;
	if (bo->pin_count)
		return 0;
241 242
	for (i = 0; i < bo->placement.num_placement; i++)
		bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
243
	r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false, false);
244
	if (unlikely(r != 0))
245
		dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
246
	return r;
247 248
}

249
int radeon_bo_evict_vram(struct radeon_device *rdev)
250
{
251 252
	/* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
	if (0 && (rdev->flags & RADEON_IS_IGP)) {
253 254 255
		if (rdev->mc.igp_sideport_enabled == false)
			/* Useless to evict on IGP chips */
			return 0;
256 257 258 259
	}
	return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
}

260
void radeon_bo_force_delete(struct radeon_device *rdev)
261
{
262
	struct radeon_bo *bo, *n;
263 264 265 266 267
	struct drm_gem_object *gobj;

	if (list_empty(&rdev->gem.objects)) {
		return;
	}
268 269
	dev_err(rdev->dev, "Userspace still has active objects !\n");
	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
270
		mutex_lock(&rdev->ddev->struct_mutex);
271 272 273 274 275 276 277 278
		gobj = bo->gobj;
		dev_err(rdev->dev, "%p %p %lu %lu force free\n",
			gobj, bo, (unsigned long)gobj->size,
			*((unsigned long *)&gobj->refcount));
		mutex_lock(&bo->rdev->gem.mutex);
		list_del_init(&bo->list);
		mutex_unlock(&bo->rdev->gem.mutex);
		radeon_bo_unref(&bo);
279 280 281 282 283 284
		gobj->driver_private = NULL;
		drm_gem_object_unreference(gobj);
		mutex_unlock(&rdev->ddev->struct_mutex);
	}
}

285
int radeon_bo_init(struct radeon_device *rdev)
286
{
287 288 289 290 291 292 293 294
	/* Add an MTRR for the VRAM */
	rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
			MTRR_TYPE_WRCOMB, 1);
	DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
		rdev->mc.mc_vram_size >> 20,
		(unsigned long long)rdev->mc.aper_size >> 20);
	DRM_INFO("RAM width %dbits %cDR\n",
			rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
295 296 297
	return radeon_ttm_init(rdev);
}

298
void radeon_bo_fini(struct radeon_device *rdev)
299 300 301 302
{
	radeon_ttm_fini(rdev);
}

303 304
void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
				struct list_head *head)
305 306
{
	if (lobj->wdomain) {
307
		list_add(&lobj->tv.head, head);
308
	} else {
309
		list_add_tail(&lobj->tv.head, head);
310 311 312
	}
}

313
int radeon_bo_list_validate(struct list_head *head)
314
{
315 316
	struct radeon_bo_list *lobj;
	struct radeon_bo *bo;
317
	u32 domain;
318 319
	int r;

320
	r = ttm_eu_reserve_buffers(head);
321 322 323
	if (unlikely(r != 0)) {
		return r;
	}
324
	list_for_each_entry(lobj, head, tv.head) {
325 326
		bo = lobj->bo;
		if (!bo->pin_count) {
327 328 329 330
			domain = lobj->wdomain ? lobj->wdomain : lobj->rdomain;
			
		retry:
			radeon_ttm_placement_from_domain(bo, domain);
331
			r = ttm_bo_validate(&bo->tbo, &bo->placement,
332
						true, false, false);
333 334 335 336 337
			if (unlikely(r)) {
				if (r != -ERESTARTSYS && domain == RADEON_GEM_DOMAIN_VRAM) {
					domain |= RADEON_GEM_DOMAIN_GTT;
					goto retry;
				}
338
				return r;
339
			}
340
		}
341 342
		lobj->gpu_offset = radeon_bo_gpu_offset(bo);
		lobj->tiling_flags = bo->tiling_flags;
343 344 345 346
	}
	return 0;
}

347
int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
348 349
			     struct vm_area_struct *vma)
{
350
	return ttm_fbdev_mmap(vma, &bo->tbo);
351 352
}

353
int radeon_bo_get_surface_reg(struct radeon_bo *bo)
354
{
355
	struct radeon_device *rdev = bo->rdev;
356
	struct radeon_surface_reg *reg;
357
	struct radeon_bo *old_object;
358 359 360
	int steal;
	int i;

361 362 363
	BUG_ON(!atomic_read(&bo->tbo.reserved));

	if (!bo->tiling_flags)
364 365
		return 0;

366 367 368
	if (bo->surface_reg >= 0) {
		reg = &rdev->surface_regs[bo->surface_reg];
		i = bo->surface_reg;
369 370 371 372 373 374 375
		goto out;
	}

	steal = -1;
	for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {

		reg = &rdev->surface_regs[i];
376
		if (!reg->bo)
377 378
			break;

379
		old_object = reg->bo;
380 381 382 383 384 385 386 387 388 389
		if (old_object->pin_count == 0)
			steal = i;
	}

	/* if we are all out */
	if (i == RADEON_GEM_MAX_SURFACES) {
		if (steal == -1)
			return -ENOMEM;
		/* find someone with a surface reg and nuke their BO */
		reg = &rdev->surface_regs[steal];
390
		old_object = reg->bo;
391 392
		/* blow away the mapping */
		DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
393
		ttm_bo_unmap_virtual(&old_object->tbo);
394 395 396 397
		old_object->surface_reg = -1;
		i = steal;
	}

398 399
	bo->surface_reg = i;
	reg->bo = bo;
400 401

out:
402
	radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
403
			       bo->tbo.mem.start << PAGE_SHIFT,
404
			       bo->tbo.num_pages << PAGE_SHIFT);
405 406 407
	return 0;
}

408
static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
409
{
410
	struct radeon_device *rdev = bo->rdev;
411 412
	struct radeon_surface_reg *reg;

413
	if (bo->surface_reg == -1)
414 415
		return;

416 417
	reg = &rdev->surface_regs[bo->surface_reg];
	radeon_clear_surface_reg(rdev, bo->surface_reg);
418

419 420
	reg->bo = NULL;
	bo->surface_reg = -1;
421 422
}

423 424
int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
				uint32_t tiling_flags, uint32_t pitch)
425
{
426 427 428 429 430 431 432 433 434
	int r;

	r = radeon_bo_reserve(bo, false);
	if (unlikely(r != 0))
		return r;
	bo->tiling_flags = tiling_flags;
	bo->pitch = pitch;
	radeon_bo_unreserve(bo);
	return 0;
435 436
}

437 438 439
void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
				uint32_t *tiling_flags,
				uint32_t *pitch)
440
{
441
	BUG_ON(!atomic_read(&bo->tbo.reserved));
442
	if (tiling_flags)
443
		*tiling_flags = bo->tiling_flags;
444
	if (pitch)
445
		*pitch = bo->pitch;
446 447
}

448 449
int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
				bool force_drop)
450
{
451 452 453
	BUG_ON(!atomic_read(&bo->tbo.reserved));

	if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
454 455 456
		return 0;

	if (force_drop) {
457
		radeon_bo_clear_surface_reg(bo);
458 459 460
		return 0;
	}

461
	if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
462 463 464
		if (!has_moved)
			return 0;

465 466
		if (bo->surface_reg >= 0)
			radeon_bo_clear_surface_reg(bo);
467 468 469
		return 0;
	}

470
	if ((bo->surface_reg >= 0) && !has_moved)
471 472
		return 0;

473
	return radeon_bo_get_surface_reg(bo);
474 475 476
}

void radeon_bo_move_notify(struct ttm_buffer_object *bo,
477
			   struct ttm_mem_reg *mem)
478
{
479 480 481 482
	struct radeon_bo *rbo;
	if (!radeon_ttm_bo_is_radeon_bo(bo))
		return;
	rbo = container_of(bo, struct radeon_bo, tbo);
483
	radeon_bo_check_tiling(rbo, 0, 1);
484 485
}

486
int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
487
{
488
	struct radeon_device *rdev;
489
	struct radeon_bo *rbo;
490 491 492
	unsigned long offset, size;
	int r;

493
	if (!radeon_ttm_bo_is_radeon_bo(bo))
494
		return 0;
495
	rbo = container_of(bo, struct radeon_bo, tbo);
496
	radeon_bo_check_tiling(rbo, 0, 0);
497 498 499
	rdev = rbo->rdev;
	if (bo->mem.mem_type == TTM_PL_VRAM) {
		size = bo->mem.num_pages << PAGE_SHIFT;
500
		offset = bo->mem.start << PAGE_SHIFT;
501 502 503 504 505 506 507
		if ((offset + size) > rdev->mc.visible_vram_size) {
			/* hurrah the memory is not visible ! */
			radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
			rbo->placement.lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT;
			r = ttm_bo_validate(bo, &rbo->placement, false, true, false);
			if (unlikely(r != 0))
				return r;
508
			offset = bo->mem.start << PAGE_SHIFT;
509 510 511 512 513 514
			/* this should not happen */
			if ((offset + size) > rdev->mc.visible_vram_size)
				return -EINVAL;
		}
	}
	return 0;
515
}