Subversion Repositories Kolibri OS

Rev

Rev 1179 | Rev 1428 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1117 serge 1
/*
2
 * Copyright 2008 Advanced Micro Devices, Inc.
3
 * Copyright 2008 Red Hat Inc.
4
 * Copyright 2009 Jerome Glisse.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
 * OTHER DEALINGS IN THE SOFTWARE.
23
 *
24
 * Authors: Dave Airlie
25
 *          Alex Deucher
26
 *          Jerome Glisse
27
 */
1179 serge 28
#include 
1125 serge 29
#include "drmP.h"
1117 serge 30
#include "radeon_drm.h"
31
#include "radeon_reg.h"
32
#include "radeon.h"
33
#include "atom.h"
34
 
35
int radeon_debugfs_ib_init(struct radeon_device *rdev);
36
 
37
/*
38
 * IB.
39
 */
1120 serge 40
 
41
#if 0
42
 
1117 serge 43
int radeon_ib_get(struct radeon_device *rdev, struct radeon_ib **ib)
44
{
45
	struct radeon_fence *fence;
46
	struct radeon_ib *nib;
47
	unsigned long i;
48
	int r = 0;
49
 
50
	*ib = NULL;
51
	r = radeon_fence_create(rdev, &fence);
52
	if (r) {
53
		DRM_ERROR("failed to create fence for new IB\n");
54
		return r;
55
	}
56
    mutex_lock(&rdev->ib_pool.mutex);
57
	i = find_first_zero_bit(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
58
	if (i < RADEON_IB_POOL_SIZE) {
59
		set_bit(i, rdev->ib_pool.alloc_bm);
60
		rdev->ib_pool.ibs[i].length_dw = 0;
61
		*ib = &rdev->ib_pool.ibs[i];
1179 serge 62
		mutex_unlock(&rdev->ib_pool.mutex);
1117 serge 63
		goto out;
64
	}
65
	if (list_empty(&rdev->ib_pool.scheduled_ibs)) {
66
		/* we go do nothings here */
1179 serge 67
		mutex_unlock(&rdev->ib_pool.mutex);
1117 serge 68
		DRM_ERROR("all IB allocated none scheduled.\n");
69
		r = -EINVAL;
70
		goto out;
71
	}
72
	/* get the first ib on the scheduled list */
73
	nib = list_entry(rdev->ib_pool.scheduled_ibs.next,
74
			 struct radeon_ib, list);
75
	if (nib->fence == NULL) {
76
		/* we go do nothings here */
1179 serge 77
		mutex_unlock(&rdev->ib_pool.mutex);
1117 serge 78
		DRM_ERROR("IB %lu scheduled without a fence.\n", nib->idx);
79
		r = -EINVAL;
80
		goto out;
81
	}
1179 serge 82
	mutex_unlock(&rdev->ib_pool.mutex);
83
 
1117 serge 84
	r = radeon_fence_wait(nib->fence, false);
85
	if (r) {
86
		DRM_ERROR("radeon: IB(%lu:0x%016lX:%u)\n", nib->idx,
87
			  (unsigned long)nib->gpu_addr, nib->length_dw);
88
		DRM_ERROR("radeon: GPU lockup detected, fail to get a IB\n");
89
		goto out;
90
	}
91
	radeon_fence_unref(&nib->fence);
1179 serge 92
 
1117 serge 93
	nib->length_dw = 0;
1179 serge 94
 
95
	/* scheduled list is accessed here */
96
	mutex_lock(&rdev->ib_pool.mutex);
1117 serge 97
	list_del(&nib->list);
98
	INIT_LIST_HEAD(&nib->list);
1179 serge 99
	mutex_unlock(&rdev->ib_pool.mutex);
100
 
1117 serge 101
	*ib = nib;
102
out:
103
	if (r) {
104
		radeon_fence_unref(&fence);
105
	} else {
106
		(*ib)->fence = fence;
107
	}
108
	return r;
109
}
110
 
111
void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
112
{
113
	struct radeon_ib *tmp = *ib;
114
 
115
	*ib = NULL;
116
	if (tmp == NULL) {
117
		return;
118
	}
119
	mutex_lock(&rdev->ib_pool.mutex);
120
	if (!list_empty(&tmp->list) && !radeon_fence_signaled(tmp->fence)) {
121
		/* IB is scheduled & not signaled don't do anythings */
122
		mutex_unlock(&rdev->ib_pool.mutex);
123
		return;
124
	}
125
	list_del(&tmp->list);
126
	INIT_LIST_HEAD(&tmp->list);
1179 serge 127
	if (tmp->fence)
1117 serge 128
		radeon_fence_unref(&tmp->fence);
1179 serge 129
 
1117 serge 130
	tmp->length_dw = 0;
131
	clear_bit(tmp->idx, rdev->ib_pool.alloc_bm);
132
	mutex_unlock(&rdev->ib_pool.mutex);
133
}
134
 
135
int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
136
{
137
	int r = 0;
138
 
139
	if (!ib->length_dw || !rdev->cp.ready) {
140
		/* TODO: Nothings in the ib we should report. */
141
		DRM_ERROR("radeon: couldn't schedule IB(%lu).\n", ib->idx);
142
		return -EINVAL;
143
	}
1179 serge 144
 
145
	/* 64 dwords should be enough for fence too */
1117 serge 146
	r = radeon_ring_lock(rdev, 64);
147
	if (r) {
148
		DRM_ERROR("radeon: scheduling IB failled (%d).\n", r);
149
		return r;
150
	}
1179 serge 151
	radeon_ring_ib_execute(rdev, ib);
1117 serge 152
	radeon_fence_emit(rdev, ib->fence);
1179 serge 153
	mutex_lock(&rdev->ib_pool.mutex);
1117 serge 154
	list_add_tail(&ib->list, &rdev->ib_pool.scheduled_ibs);
155
	mutex_unlock(&rdev->ib_pool.mutex);
1179 serge 156
	radeon_ring_unlock_commit(rdev);
1117 serge 157
	return 0;
158
}
1120 serge 159
#endif
1117 serge 160
 
161
int radeon_ib_pool_init(struct radeon_device *rdev)
162
{
163
	void *ptr;
164
	uint64_t gpu_addr;
165
	int i;
166
	int r = 0;
167
 
1179 serge 168
	if (rdev->ib_pool.robj)
169
		return 0;
1117 serge 170
	/* Allocate 1M object buffer */
171
	INIT_LIST_HEAD(&rdev->ib_pool.scheduled_ibs);
1404 serge 172
	r = radeon_bo_create(rdev, NULL,  RADEON_IB_POOL_SIZE*64*1024,
1117 serge 173
				 true, RADEON_GEM_DOMAIN_GTT,
1404 serge 174
				&rdev->ib_pool.robj);
1117 serge 175
	if (r) {
176
		DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
177
		return r;
178
	}
1404 serge 179
	r = radeon_bo_reserve(rdev->ib_pool.robj, false);
180
	if (unlikely(r != 0))
181
		return r;
182
	r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
1117 serge 183
	if (r) {
1404 serge 184
		radeon_bo_unreserve(rdev->ib_pool.robj);
1117 serge 185
		DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
186
		return r;
187
	}
1404 serge 188
	r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
189
	radeon_bo_unreserve(rdev->ib_pool.robj);
1117 serge 190
	if (r) {
191
		DRM_ERROR("radeon: failed to map ib poll (%d).\n", r);
192
		return r;
193
	}
194
	for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
195
		unsigned offset;
196
 
197
		offset = i * 64 * 1024;
198
		rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
199
		rdev->ib_pool.ibs[i].ptr = ptr + offset;
200
		rdev->ib_pool.ibs[i].idx = i;
201
		rdev->ib_pool.ibs[i].length_dw = 0;
202
		INIT_LIST_HEAD(&rdev->ib_pool.ibs[i].list);
203
	}
204
	bitmap_zero(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
205
	rdev->ib_pool.ready = true;
206
	DRM_INFO("radeon: ib pool ready.\n");
1129 serge 207
	if (radeon_debugfs_ib_init(rdev)) {
208
		DRM_ERROR("Failed to register debugfs file for IB !\n");
209
	}
1117 serge 210
	return r;
211
}
212
 
213
void radeon_ib_pool_fini(struct radeon_device *rdev)
214
{
1404 serge 215
	int r;
216
 
1117 serge 217
	if (!rdev->ib_pool.ready) {
218
		return;
219
	}
1179 serge 220
	mutex_lock(&rdev->ib_pool.mutex);
1117 serge 221
	bitmap_zero(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
222
	if (rdev->ib_pool.robj) {
1404 serge 223
		r = radeon_bo_reserve(rdev->ib_pool.robj, false);
224
		if (likely(r == 0)) {
225
			radeon_bo_kunmap(rdev->ib_pool.robj);
226
			radeon_bo_unpin(rdev->ib_pool.robj);
227
			radeon_bo_unreserve(rdev->ib_pool.robj);
228
		}
229
		radeon_bo_unref(&rdev->ib_pool.robj);
1117 serge 230
		rdev->ib_pool.robj = NULL;
231
	}
1179 serge 232
	mutex_unlock(&rdev->ib_pool.mutex);
1117 serge 233
}
234
 
1120 serge 235
 
1117 serge 236
/*
237
 * Ring.
238
 */
239
void radeon_ring_free_size(struct radeon_device *rdev)
240
{
1179 serge 241
	if (rdev->family >= CHIP_R600)
242
		rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
243
	else
1117 serge 244
	rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
245
	/* This works because ring_size is a power of 2 */
246
	rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
247
	rdev->cp.ring_free_dw -= rdev->cp.wptr;
248
	rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
249
	if (!rdev->cp.ring_free_dw) {
250
		rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
251
	}
252
}
253
 
254
int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
255
{
256
	int r;
257
 
258
	/* Align requested size with padding so unlock_commit can
259
	 * pad safely */
260
	ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
1179 serge 261
	mutex_lock(&rdev->cp.mutex);
1117 serge 262
	while (ndw > (rdev->cp.ring_free_dw - 1)) {
263
		radeon_ring_free_size(rdev);
264
		if (ndw < rdev->cp.ring_free_dw) {
265
			break;
266
		}
267
//        r = radeon_fence_wait_next(rdev);
268
//       if (r) {
269
//           mutex_unlock(&rdev->cp.mutex);
270
//           return r;
271
//       }
272
	}
273
	rdev->cp.count_dw = ndw;
274
	rdev->cp.wptr_old = rdev->cp.wptr;
275
	return 0;
276
}
277
 
278
void radeon_ring_unlock_commit(struct radeon_device *rdev)
279
{
280
	unsigned count_dw_pad;
281
	unsigned i;
282
 
283
	/* We pad to match fetch size */
284
	count_dw_pad = (rdev->cp.align_mask + 1) -
285
		       (rdev->cp.wptr & rdev->cp.align_mask);
286
	for (i = 0; i < count_dw_pad; i++) {
1179 serge 287
		radeon_ring_write(rdev, 2 << 30);
1117 serge 288
	}
289
	DRM_MEMORYBARRIER();
1179 serge 290
	radeon_cp_commit(rdev);
291
	mutex_unlock(&rdev->cp.mutex);
1117 serge 292
}
293
 
294
void radeon_ring_unlock_undo(struct radeon_device *rdev)
295
{
296
	rdev->cp.wptr = rdev->cp.wptr_old;
1179 serge 297
	mutex_unlock(&rdev->cp.mutex);
1117 serge 298
}
299
 
300
int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
301
{
302
	int r;
303
 
1179 serge 304
    ENTER();
1117 serge 305
 
306
	rdev->cp.ring_size = ring_size;
1120 serge 307
    /* Allocate ring buffer */
1117 serge 308
	if (rdev->cp.ring_obj == NULL) {
1404 serge 309
		r = radeon_bo_create(rdev, NULL, rdev->cp.ring_size, true,
1117 serge 310
					 RADEON_GEM_DOMAIN_GTT,
311
					 &rdev->cp.ring_obj);
312
		if (r) {
1404 serge 313
			dev_err(rdev->dev, "(%d) ring create failed\n", r);
1117 serge 314
			return r;
315
		}
1404 serge 316
		r = radeon_bo_reserve(rdev->cp.ring_obj, false);
317
		if (unlikely(r != 0))
318
			return r;
319
		r = radeon_bo_pin(rdev->cp.ring_obj, RADEON_GEM_DOMAIN_GTT,
1117 serge 320
				      &rdev->cp.gpu_addr);
321
		if (r) {
1404 serge 322
			radeon_bo_unreserve(rdev->cp.ring_obj);
323
			dev_err(rdev->dev, "(%d) ring pin failed\n", r);
1117 serge 324
			return r;
325
		}
1404 serge 326
		r = radeon_bo_kmap(rdev->cp.ring_obj,
1117 serge 327
				       (void **)&rdev->cp.ring);
1404 serge 328
		radeon_bo_unreserve(rdev->cp.ring_obj);
1117 serge 329
		if (r) {
1404 serge 330
			dev_err(rdev->dev, "(%d) ring map failed\n", r);
1117 serge 331
			return r;
332
		}
333
	}
334
	rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
335
	rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
1119 serge 336
 
1179 serge 337
    LEAVE();
1119 serge 338
 
1117 serge 339
	return 0;
340
}
341
 
342
void radeon_ring_fini(struct radeon_device *rdev)
343
{
1404 serge 344
	int r;
345
 
1179 serge 346
	mutex_lock(&rdev->cp.mutex);
1117 serge 347
   if (rdev->cp.ring_obj) {
1404 serge 348
		r = radeon_bo_reserve(rdev->cp.ring_obj, false);
349
		if (likely(r == 0)) {
350
			radeon_bo_kunmap(rdev->cp.ring_obj);
351
			radeon_bo_unpin(rdev->cp.ring_obj);
352
			radeon_bo_unreserve(rdev->cp.ring_obj);
353
		}
354
		radeon_bo_unref(&rdev->cp.ring_obj);
1117 serge 355
       rdev->cp.ring = NULL;
356
		rdev->cp.ring_obj = NULL;
357
	}
1179 serge 358
	mutex_unlock(&rdev->cp.mutex);
1117 serge 359
}
360
 
361
 
362
/*
363
 * Debugfs info
364
 */
365
#if defined(CONFIG_DEBUG_FS)
366
static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
367
{
368
	struct drm_info_node *node = (struct drm_info_node *) m->private;
369
	struct radeon_ib *ib = node->info_ent->data;
370
	unsigned i;
371
 
372
	if (ib == NULL) {
373
		return 0;
374
	}
375
	seq_printf(m, "IB %04lu\n", ib->idx);
376
	seq_printf(m, "IB fence %p\n", ib->fence);
377
	seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
378
	for (i = 0; i < ib->length_dw; i++) {
379
		seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
380
	}
381
	return 0;
382
}
383
 
384
static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
385
static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
386
#endif
387
 
388
int radeon_debugfs_ib_init(struct radeon_device *rdev)
389
{
390
#if defined(CONFIG_DEBUG_FS)
391
	unsigned i;
392
 
393
	for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
394
		sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
395
		radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
396
		radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
397
		radeon_debugfs_ib_list[i].driver_features = 0;
398
		radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
399
	}
400
	return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
401
					RADEON_IB_POOL_SIZE);
402
#else
403
	return 0;
404
#endif
405
}