Subversion Repositories Kolibri OS

Rev

Rev 6104 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5078 serge 1
/*
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 
29
 *    Thomas Hellstrom 
30
 *    Dave Airlie
31
 */
32
#include 
33
#include 
34
#include 
35
#include 
6938 serge 36
#include 
5078 serge 37
#include "radeon.h"
38
#include "radeon_trace.h"
39
 
40
 
41
int radeon_ttm_init(struct radeon_device *rdev);
42
void radeon_ttm_fini(struct radeon_device *rdev);
43
static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
44
 
45
/*
46
 * To exclude mutual BO access we rely on bo_reserve exclusion, as all
47
 * function are calling it.
48
 */
49
 
50
static void radeon_update_memory_usage(struct radeon_bo *bo,
51
				       unsigned mem_type, int sign)
52
{
53
	struct radeon_device *rdev = bo->rdev;
54
	u64 size = (u64)bo->tbo.num_pages << PAGE_SHIFT;
55
 
56
	switch (mem_type) {
57
	case TTM_PL_TT:
58
		if (sign > 0)
59
			__atomic_add_fetch(&rdev->gtt_usage.counter, size,__ATOMIC_RELAXED);
60
		else
61
			__atomic_sub_fetch(&rdev->gtt_usage.counter, size,__ATOMIC_RELAXED);
62
		break;
63
	case TTM_PL_VRAM:
64
		if (sign > 0)
65
			__atomic_add_fetch(&rdev->vram_usage.counter, size,__ATOMIC_RELAXED);
66
		else
67
			__atomic_sub_fetch(&rdev->vram_usage.counter, size,__ATOMIC_RELAXED );
68
		break;
69
	}
70
}
71
 
72
static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
73
{
74
	struct radeon_bo *bo;
75
 
76
	bo = container_of(tbo, struct radeon_bo, tbo);
77
 
78
	radeon_update_memory_usage(bo, bo->tbo.mem.mem_type, -1);
79
 
80
	mutex_lock(&bo->rdev->gem.mutex);
81
	list_del_init(&bo->list);
82
	mutex_unlock(&bo->rdev->gem.mutex);
83
	radeon_bo_clear_surface_reg(bo);
84
	WARN_ON(!list_empty(&bo->va));
85
	drm_gem_object_release(&bo->gem_base);
86
	kfree(bo);
87
}
88
 
89
bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo)
90
{
91
	if (bo->destroy == &radeon_ttm_bo_destroy)
92
		return true;
93
	return false;
94
}
95
 
96
void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
97
{
6104 serge 98
	u32 c = 0, i;
5078 serge 99
 
6104 serge 100
	rbo->placement.placement = rbo->placements;
101
	rbo->placement.busy_placement = rbo->placements;
5271 serge 102
	if (domain & RADEON_GEM_DOMAIN_VRAM) {
103
		/* Try placing BOs which don't need CPU access outside of the
104
		 * CPU accessible part of VRAM
105
		 */
106
		if ((rbo->flags & RADEON_GEM_NO_CPU_ACCESS) &&
107
		    rbo->rdev->mc.visible_vram_size < rbo->rdev->mc.real_vram_size) {
108
			rbo->placements[c].fpfn =
109
				rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
110
			rbo->placements[c++].flags = TTM_PL_FLAG_WC |
111
						     TTM_PL_FLAG_UNCACHED |
6104 serge 112
						     TTM_PL_FLAG_VRAM;
5271 serge 113
		}
114
 
115
		rbo->placements[c].fpfn = 0;
116
		rbo->placements[c++].flags = TTM_PL_FLAG_WC |
117
					     TTM_PL_FLAG_UNCACHED |
118
					     TTM_PL_FLAG_VRAM;
119
	}
120
 
6104 serge 121
	if (domain & RADEON_GEM_DOMAIN_GTT) {
5078 serge 122
		if (rbo->flags & RADEON_GEM_GTT_UC) {
5271 serge 123
			rbo->placements[c].fpfn = 0;
124
			rbo->placements[c++].flags = TTM_PL_FLAG_UNCACHED |
125
				TTM_PL_FLAG_TT;
126
 
5078 serge 127
		} else if ((rbo->flags & RADEON_GEM_GTT_WC) ||
128
			   (rbo->rdev->flags & RADEON_IS_AGP)) {
5271 serge 129
			rbo->placements[c].fpfn = 0;
130
			rbo->placements[c++].flags = TTM_PL_FLAG_WC |
131
				TTM_PL_FLAG_UNCACHED |
5078 serge 132
				TTM_PL_FLAG_TT;
6104 serge 133
		} else {
5271 serge 134
			rbo->placements[c].fpfn = 0;
135
			rbo->placements[c++].flags = TTM_PL_FLAG_CACHED |
136
						     TTM_PL_FLAG_TT;
6104 serge 137
		}
138
	}
5271 serge 139
 
6104 serge 140
	if (domain & RADEON_GEM_DOMAIN_CPU) {
5078 serge 141
		if (rbo->flags & RADEON_GEM_GTT_UC) {
5271 serge 142
			rbo->placements[c].fpfn = 0;
143
			rbo->placements[c++].flags = TTM_PL_FLAG_UNCACHED |
144
				TTM_PL_FLAG_SYSTEM;
145
 
5078 serge 146
		} else if ((rbo->flags & RADEON_GEM_GTT_WC) ||
147
		    rbo->rdev->flags & RADEON_IS_AGP) {
5271 serge 148
			rbo->placements[c].fpfn = 0;
149
			rbo->placements[c++].flags = TTM_PL_FLAG_WC |
150
				TTM_PL_FLAG_UNCACHED |
5078 serge 151
				TTM_PL_FLAG_SYSTEM;
6104 serge 152
		} else {
5271 serge 153
			rbo->placements[c].fpfn = 0;
154
			rbo->placements[c++].flags = TTM_PL_FLAG_CACHED |
155
						     TTM_PL_FLAG_SYSTEM;
6104 serge 156
		}
157
	}
5271 serge 158
	if (!c) {
159
		rbo->placements[c].fpfn = 0;
160
		rbo->placements[c++].flags = TTM_PL_MASK_CACHING |
161
					     TTM_PL_FLAG_SYSTEM;
162
	}
163
 
6104 serge 164
	rbo->placement.num_placement = c;
165
	rbo->placement.num_busy_placement = c;
5078 serge 166
 
5271 serge 167
	for (i = 0; i < c; ++i) {
168
		if ((rbo->flags & RADEON_GEM_CPU_ACCESS) &&
169
		    (rbo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
170
		    !rbo->placements[i].fpfn)
171
			rbo->placements[i].lpfn =
172
				rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
173
		else
174
			rbo->placements[i].lpfn = 0;
175
	}
5078 serge 176
}
177
 
178
int radeon_bo_create(struct radeon_device *rdev,
5271 serge 179
		     unsigned long size, int byte_align, bool kernel,
180
		     u32 domain, u32 flags, struct sg_table *sg,
181
		     struct reservation_object *resv,
182
		     struct radeon_bo **bo_ptr)
5078 serge 183
{
6104 serge 184
	struct radeon_bo *bo;
5078 serge 185
	enum ttm_bo_type type;
6104 serge 186
	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
187
	size_t acc_size;
188
	int r;
5078 serge 189
 
190
	size = ALIGN(size, PAGE_SIZE);
191
 
192
	if (kernel) {
193
		type = ttm_bo_type_kernel;
194
	} else if (sg) {
195
		type = ttm_bo_type_sg;
196
	} else {
197
		type = ttm_bo_type_device;
198
	}
199
	*bo_ptr = NULL;
200
 
201
	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
202
				       sizeof(struct radeon_bo));
203
 
6104 serge 204
	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
205
	if (bo == NULL)
206
		return -ENOMEM;
207
	r = drm_gem_object_init(rdev->ddev, &bo->gem_base, size);
208
	if (unlikely(r)) {
209
		kfree(bo);
210
		return r;
211
	}
212
	bo->rdev = rdev;
213
	bo->surface_reg = -1;
214
	INIT_LIST_HEAD(&bo->list);
215
	INIT_LIST_HEAD(&bo->va);
216
	bo->initial_domain = domain & (RADEON_GEM_DOMAIN_VRAM |
217
	                               RADEON_GEM_DOMAIN_GTT |
218
	                               RADEON_GEM_DOMAIN_CPU);
5078 serge 219
 
220
	bo->flags = flags;
221
	/* PCI GART is always snooped */
222
	if (!(rdev->flags & RADEON_IS_PCIE))
223
		bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
224
 
6104 serge 225
	/* Write-combined CPU mappings of GTT cause GPU hangs with RV6xx
226
	 * See https://bugs.freedesktop.org/show_bug.cgi?id=91268
227
	 */
228
	if (rdev->family >= CHIP_RV610 && rdev->family <= CHIP_RV635)
229
		bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
230
 
5271 serge 231
#ifdef CONFIG_X86_32
232
	/* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
233
	 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
234
	 */
6104 serge 235
	bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
236
#elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
237
	/* Don't try to enable write-combining when it can't work, or things
238
	 * may be slow
239
	 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
240
	 */
241
 
242
#warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
243
	 thanks to write-combining
244
 
245
	if (bo->flags & RADEON_GEM_GTT_WC)
246
		DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
247
			      "better performance thanks to write-combining\n");
248
	bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
5271 serge 249
#endif
5078 serge 250
 
6104 serge 251
	radeon_ttm_placement_from_domain(bo, domain);
5078 serge 252
	/* Kernel allocation are uninterruptible */
5346 serge 253
	down_read(&rdev->pm.mclk_lock);
5078 serge 254
	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
255
			&bo->placement, page_align, !kernel, NULL,
5271 serge 256
			acc_size, sg, resv, &radeon_ttm_bo_destroy);
5346 serge 257
	up_read(&rdev->pm.mclk_lock);
5078 serge 258
	if (unlikely(r != 0)) {
259
		return r;
260
	}
261
	*bo_ptr = bo;
262
 
263
	trace_radeon_bo_create(bo);
264
 
265
	return 0;
266
}
267
 
268
int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
269
{
270
	bool is_iomem;
271
	int r;
272
 
273
	if (bo->kptr) {
274
		if (ptr) {
275
			*ptr = bo->kptr;
276
		}
277
		return 0;
278
	}
279
	r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
280
	if (r) {
281
		return r;
282
	}
283
	bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
284
	if (ptr) {
285
		*ptr = bo->kptr;
286
	}
287
	radeon_bo_check_tiling(bo, 0, 0);
288
	return 0;
289
}
290
 
291
void radeon_bo_kunmap(struct radeon_bo *bo)
292
{
293
	if (bo->kptr == NULL)
294
		return;
295
	bo->kptr = NULL;
296
	radeon_bo_check_tiling(bo, 0, 0);
297
	ttm_bo_kunmap(&bo->kmap);
298
}
299
 
300
struct radeon_bo *radeon_bo_ref(struct radeon_bo *bo)
301
{
302
	if (bo == NULL)
303
		return NULL;
304
 
305
	ttm_bo_reference(&bo->tbo);
306
	return bo;
307
}
308
 
309
void radeon_bo_unref(struct radeon_bo **bo)
310
{
311
	struct ttm_buffer_object *tbo;
312
	struct radeon_device *rdev;
313
 
314
	if ((*bo) == NULL)
315
		return;
316
	rdev = (*bo)->rdev;
317
	tbo = &((*bo)->tbo);
318
	ttm_bo_unref(&tbo);
319
	if (tbo == NULL)
320
		*bo = NULL;
321
}
322
 
323
int radeon_bo_pin_restricted(struct radeon_bo *bo, u32 domain, u64 max_offset,
6104 serge 324
			     u64 *gpu_addr)
5078 serge 325
{
6104 serge 326
	int r, i;
5078 serge 327
 
6104 serge 328
	if (bo->pin_count) {
329
		bo->pin_count++;
330
		if (gpu_addr)
331
			*gpu_addr = radeon_bo_gpu_offset(bo);
5078 serge 332
 
6104 serge 333
		if (max_offset != 0) {
334
			u64 domain_start;
5078 serge 335
 
6104 serge 336
			if (domain == RADEON_GEM_DOMAIN_VRAM)
337
				domain_start = bo->rdev->mc.vram_start;
338
			else
339
				domain_start = bo->rdev->mc.gtt_start;
340
			WARN_ON_ONCE(max_offset <
341
				     (radeon_bo_gpu_offset(bo) - domain_start));
342
		}
5078 serge 343
 
6104 serge 344
		return 0;
345
	}
346
	radeon_ttm_placement_from_domain(bo, domain);
5271 serge 347
	for (i = 0; i < bo->placement.num_placement; i++) {
6104 serge 348
		/* force to pin into visible video ram */
5271 serge 349
		if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
350
		    !(bo->flags & RADEON_GEM_NO_CPU_ACCESS) &&
351
		    (!max_offset || max_offset > bo->rdev->mc.visible_vram_size))
352
			bo->placements[i].lpfn =
353
				bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
354
		else
355
			bo->placements[i].lpfn = max_offset >> PAGE_SHIFT;
356
 
357
		bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
6104 serge 358
	}
5078 serge 359
 
360
	r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
6104 serge 361
	if (likely(r == 0)) {
362
		bo->pin_count = 1;
363
		if (gpu_addr != NULL)
364
			*gpu_addr = radeon_bo_gpu_offset(bo);
5078 serge 365
		if (domain == RADEON_GEM_DOMAIN_VRAM)
366
			bo->rdev->vram_pin_size += radeon_bo_size(bo);
367
		else
368
			bo->rdev->gart_pin_size += radeon_bo_size(bo);
369
	} else {
370
		dev_err(bo->rdev->dev, "%p pin failed\n", bo);
6104 serge 371
	}
372
	return r;
5078 serge 373
}
374
 
375
int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
376
{
6104 serge 377
	return radeon_bo_pin_restricted(bo, domain, 0, gpu_addr);
5078 serge 378
}
379
 
380
int radeon_bo_unpin(struct radeon_bo *bo)
381
{
6104 serge 382
	int r, i;
5078 serge 383
 
6104 serge 384
	if (!bo->pin_count) {
385
		dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
386
		return 0;
387
	}
388
	bo->pin_count--;
389
	if (bo->pin_count)
390
		return 0;
5271 serge 391
	for (i = 0; i < bo->placement.num_placement; i++) {
392
		bo->placements[i].lpfn = 0;
393
		bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
394
	}
5078 serge 395
	r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
396
	if (likely(r == 0)) {
397
		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
398
			bo->rdev->vram_pin_size -= radeon_bo_size(bo);
399
		else
400
			bo->rdev->gart_pin_size -= radeon_bo_size(bo);
401
	} else {
402
		dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
403
	}
404
    return r;
405
}
406
 
407
int radeon_bo_init(struct radeon_device *rdev)
408
{
409
	/* Add an MTRR for the VRAM */
410
	DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
411
		rdev->mc.mc_vram_size >> 20,
412
		(unsigned long long)rdev->mc.aper_size >> 20);
413
	DRM_INFO("RAM width %dbits %cDR\n",
414
			rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
415
	return radeon_ttm_init(rdev);
416
}
417
 
418
void radeon_bo_fini(struct radeon_device *rdev)
419
{
420
//   radeon_ttm_fini(rdev);
421
//   arch_phys_wc_del(rdev->mc.vram_mtrr);
422
}
423
 
424
/* Returns how many bytes TTM can move per IB.
425
 */
426
static u64 radeon_bo_get_threshold_for_moves(struct radeon_device *rdev)
427
{
428
	u64 real_vram_size = rdev->mc.real_vram_size;
429
	u64 vram_usage = atomic64_read(&rdev->vram_usage);
430
 
431
	/* This function is based on the current VRAM usage.
432
	 *
433
	 * - If all of VRAM is free, allow relocating the number of bytes that
434
	 *   is equal to 1/4 of the size of VRAM for this IB.
435
 
436
	 * - If more than one half of VRAM is occupied, only allow relocating
437
	 *   1 MB of data for this IB.
438
	 *
439
	 * - From 0 to one half of used VRAM, the threshold decreases
440
	 *   linearly.
441
	 *         __________________
442
	 * 1/4 of -|\               |
443
	 * VRAM    | \              |
444
	 *         |  \             |
445
	 *         |   \            |
446
	 *         |    \           |
447
	 *         |     \          |
448
	 *         |      \         |
449
	 *         |       \________|1 MB
450
	 *         |----------------|
451
	 *    VRAM 0 %             100 %
452
	 *         used            used
453
	 *
454
	 * Note: It's a threshold, not a limit. The threshold must be crossed
455
	 * for buffer relocations to stop, so any buffer of an arbitrary size
456
	 * can be moved as long as the threshold isn't crossed before
457
	 * the relocation takes place. We don't want to disable buffer
458
	 * relocations completely.
459
	 *
460
	 * The idea is that buffers should be placed in VRAM at creation time
461
	 * and TTM should only do a minimum number of relocations during
462
	 * command submission. In practice, you need to submit at least
463
	 * a dozen IBs to move all buffers to VRAM if they are in GTT.
464
	 *
465
	 * Also, things can get pretty crazy under memory pressure and actual
466
	 * VRAM usage can change a lot, so playing safe even at 50% does
467
	 * consistently increase performance.
468
	 */
469
 
470
	u64 half_vram = real_vram_size >> 1;
471
	u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
472
	u64 bytes_moved_threshold = half_free_vram >> 1;
473
	return max(bytes_moved_threshold, 1024*1024ull);
474
}
475
 
476
int radeon_bo_list_validate(struct radeon_device *rdev,
477
			    struct ww_acquire_ctx *ticket,
478
			    struct list_head *head, int ring)
479
{
5271 serge 480
	struct radeon_bo_list *lobj;
481
	struct list_head duplicates;
5078 serge 482
	int r;
483
	u64 bytes_moved = 0, initial_bytes_moved;
484
	u64 bytes_moved_threshold = radeon_bo_get_threshold_for_moves(rdev);
485
 
5271 serge 486
	INIT_LIST_HEAD(&duplicates);
487
	r = ttm_eu_reserve_buffers(ticket, head, true, &duplicates);
5078 serge 488
	if (unlikely(r != 0)) {
489
		return r;
490
	}
491
 
492
	list_for_each_entry(lobj, head, tv.head) {
5271 serge 493
		struct radeon_bo *bo = lobj->robj;
5078 serge 494
		if (!bo->pin_count) {
495
			u32 domain = lobj->prefered_domains;
5271 serge 496
			u32 allowed = lobj->allowed_domains;
5078 serge 497
			u32 current_domain =
498
				radeon_mem_type_to_domain(bo->tbo.mem.mem_type);
499
 
500
			/* Check if this buffer will be moved and don't move it
501
			 * if we have moved too many buffers for this IB already.
502
			 *
503
			 * Note that this allows moving at least one buffer of
504
			 * any size, because it doesn't take the current "bo"
505
			 * into account. We don't want to disallow buffer moves
506
			 * completely.
507
			 */
5271 serge 508
			if ((allowed & current_domain) != 0 &&
5078 serge 509
			    (domain & current_domain) == 0 && /* will be moved */
510
			    bytes_moved > bytes_moved_threshold) {
511
				/* don't move it */
512
				domain = current_domain;
513
			}
514
 
515
		retry:
516
			radeon_ttm_placement_from_domain(bo, domain);
517
			if (ring == R600_RING_TYPE_UVD_INDEX)
5271 serge 518
				radeon_uvd_force_into_uvd_segment(bo, allowed);
5078 serge 519
 
520
			initial_bytes_moved = atomic64_read(&rdev->num_bytes_moved);
521
			r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
522
			bytes_moved += atomic64_read(&rdev->num_bytes_moved) -
523
				       initial_bytes_moved;
524
 
525
			if (unlikely(r)) {
526
				if (r != -ERESTARTSYS &&
527
				    domain != lobj->allowed_domains) {
528
					domain = lobj->allowed_domains;
529
					goto retry;
530
				}
531
				ttm_eu_backoff_reservation(ticket, head);
532
				return r;
533
			}
534
		}
535
		lobj->gpu_offset = radeon_bo_gpu_offset(bo);
536
		lobj->tiling_flags = bo->tiling_flags;
537
	}
5271 serge 538
 
539
	list_for_each_entry(lobj, &duplicates, tv.head) {
540
		lobj->gpu_offset = radeon_bo_gpu_offset(lobj->robj);
541
		lobj->tiling_flags = lobj->robj->tiling_flags;
542
	}
543
 
5078 serge 544
	return 0;
545
}
546
 
547
int radeon_bo_get_surface_reg(struct radeon_bo *bo)
548
{
549
	struct radeon_device *rdev = bo->rdev;
550
	struct radeon_surface_reg *reg;
551
	struct radeon_bo *old_object;
552
	int steal;
553
	int i;
554
 
555
	lockdep_assert_held(&bo->tbo.resv->lock.base);
556
 
557
	if (!bo->tiling_flags)
558
		return 0;
559
 
560
	if (bo->surface_reg >= 0) {
561
		reg = &rdev->surface_regs[bo->surface_reg];
562
		i = bo->surface_reg;
563
		goto out;
564
	}
565
 
566
	steal = -1;
567
	for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
568
 
569
		reg = &rdev->surface_regs[i];
570
		if (!reg->bo)
571
			break;
572
 
573
		old_object = reg->bo;
574
		if (old_object->pin_count == 0)
575
			steal = i;
576
	}
577
 
578
	/* if we are all out */
579
	if (i == RADEON_GEM_MAX_SURFACES) {
580
		if (steal == -1)
581
			return -ENOMEM;
582
		/* find someone with a surface reg and nuke their BO */
583
		reg = &rdev->surface_regs[steal];
584
		old_object = reg->bo;
585
		/* blow away the mapping */
586
		DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
587
		ttm_bo_unmap_virtual(&old_object->tbo);
588
		old_object->surface_reg = -1;
589
		i = steal;
590
	}
591
 
592
	bo->surface_reg = i;
593
	reg->bo = bo;
594
 
595
out:
596
	radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
597
			       bo->tbo.mem.start << PAGE_SHIFT,
598
			       bo->tbo.num_pages << PAGE_SHIFT);
599
	return 0;
600
}
601
 
602
static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
603
{
604
	struct radeon_device *rdev = bo->rdev;
605
	struct radeon_surface_reg *reg;
606
 
607
	if (bo->surface_reg == -1)
608
		return;
609
 
610
	reg = &rdev->surface_regs[bo->surface_reg];
611
	radeon_clear_surface_reg(rdev, bo->surface_reg);
612
 
613
	reg->bo = NULL;
614
	bo->surface_reg = -1;
615
}
616
 
617
int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
618
				uint32_t tiling_flags, uint32_t pitch)
619
{
620
	struct radeon_device *rdev = bo->rdev;
621
	int r;
622
 
623
	if (rdev->family >= CHIP_CEDAR) {
624
		unsigned bankw, bankh, mtaspect, tilesplit, stilesplit;
625
 
626
		bankw = (tiling_flags >> RADEON_TILING_EG_BANKW_SHIFT) & RADEON_TILING_EG_BANKW_MASK;
627
		bankh = (tiling_flags >> RADEON_TILING_EG_BANKH_SHIFT) & RADEON_TILING_EG_BANKH_MASK;
628
		mtaspect = (tiling_flags >> RADEON_TILING_EG_MACRO_TILE_ASPECT_SHIFT) & RADEON_TILING_EG_MACRO_TILE_ASPECT_MASK;
629
		tilesplit = (tiling_flags >> RADEON_TILING_EG_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_TILE_SPLIT_MASK;
630
		stilesplit = (tiling_flags >> RADEON_TILING_EG_STENCIL_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_STENCIL_TILE_SPLIT_MASK;
631
		switch (bankw) {
632
		case 0:
633
		case 1:
634
		case 2:
635
		case 4:
636
		case 8:
637
			break;
638
		default:
639
			return -EINVAL;
640
		}
641
		switch (bankh) {
642
		case 0:
643
		case 1:
644
		case 2:
645
		case 4:
646
		case 8:
647
			break;
648
		default:
649
			return -EINVAL;
650
		}
651
		switch (mtaspect) {
652
		case 0:
653
		case 1:
654
		case 2:
655
		case 4:
656
		case 8:
657
			break;
658
		default:
659
			return -EINVAL;
660
		}
661
		if (tilesplit > 6) {
662
			return -EINVAL;
663
		}
664
		if (stilesplit > 6) {
665
			return -EINVAL;
666
		}
667
	}
668
	r = radeon_bo_reserve(bo, false);
669
	if (unlikely(r != 0))
670
		return r;
671
	bo->tiling_flags = tiling_flags;
672
	bo->pitch = pitch;
673
	radeon_bo_unreserve(bo);
674
	return 0;
675
}
676
 
677
void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
678
				uint32_t *tiling_flags,
679
				uint32_t *pitch)
680
{
681
	lockdep_assert_held(&bo->tbo.resv->lock.base);
682
 
683
	if (tiling_flags)
684
		*tiling_flags = bo->tiling_flags;
685
	if (pitch)
686
		*pitch = bo->pitch;
687
}
688
 
689
int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
690
				bool force_drop)
691
{
692
	if (!force_drop)
693
		lockdep_assert_held(&bo->tbo.resv->lock.base);
694
 
695
	if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
696
		return 0;
697
 
698
	if (force_drop) {
699
		radeon_bo_clear_surface_reg(bo);
700
		return 0;
701
	}
702
 
703
	if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
704
		if (!has_moved)
705
			return 0;
706
 
707
		if (bo->surface_reg >= 0)
708
			radeon_bo_clear_surface_reg(bo);
709
		return 0;
710
	}
711
 
712
	if ((bo->surface_reg >= 0) && !has_moved)
713
		return 0;
714
 
715
	return radeon_bo_get_surface_reg(bo);
716
}
717
 
718
void radeon_bo_move_notify(struct ttm_buffer_object *bo,
719
			   struct ttm_mem_reg *new_mem)
720
{
721
	struct radeon_bo *rbo;
722
 
723
	if (!radeon_ttm_bo_is_radeon_bo(bo))
724
		return;
725
 
726
	rbo = container_of(bo, struct radeon_bo, tbo);
727
	radeon_bo_check_tiling(rbo, 0, 1);
728
	radeon_vm_bo_invalidate(rbo->rdev, rbo);
729
 
730
	/* update statistics */
731
	if (!new_mem)
732
		return;
733
 
734
	radeon_update_memory_usage(rbo, bo->mem.mem_type, -1);
735
	radeon_update_memory_usage(rbo, new_mem->mem_type, 1);
736
}
737
int radeon_bo_wait(struct radeon_bo *bo, u32 *mem_type, bool no_wait)
738
{
739
	int r;
740
 
741
	r = ttm_bo_reserve(&bo->tbo, true, no_wait, false, NULL);
742
	if (unlikely(r != 0))
743
		return r;
744
	if (mem_type)
745
		*mem_type = bo->tbo.mem.mem_type;
5271 serge 746
 
6104 serge 747
	r = ttm_bo_wait(&bo->tbo, true, true, no_wait);
5078 serge 748
	ttm_bo_unreserve(&bo->tbo);
749
	return r;
750
}
5271 serge 751
 
752
/**
753
 * radeon_bo_fence - add fence to buffer object
754
 *
755
 * @bo: buffer object in question
756
 * @fence: fence to add
757
 * @shared: true if fence should be added shared
758
 *
759
 */
760
void radeon_bo_fence(struct radeon_bo *bo, struct radeon_fence *fence,
761
                     bool shared)
762
{
763
	struct reservation_object *resv = bo->tbo.resv;
764
 
765
	if (shared)
766
		reservation_object_add_shared_fence(resv, &fence->base);
767
	else
768
		reservation_object_add_excl_fence(resv, &fence->base);
769
}