summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/r300/radeon_mm.c
blob: a18d69122d0283b67a88c7e915455453d0d1831f (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/sem.h>

#include "r300_context.h"
#include "r300_cmdbuf.h"
#include "radeon_mm.h"

#ifdef USER_BUFFERS

void radeon_mm_init(r300ContextPtr rmesa)
{
	rmesa->rmm = malloc(sizeof(struct radeon_memory_manager));
	memset(rmesa->rmm, 0, sizeof(struct radeon_memory_manager));
	rmesa->rmm->u_size = 512; //2048;
	
	rmesa->rmm->u_list = malloc(rmesa->rmm->u_size *sizeof(*rmesa->rmm->u_list));
	memset(rmesa->rmm->u_list, 0, rmesa->rmm->u_size*sizeof(*rmesa->rmm->u_list));
}

void *radeon_mm_ptr(r300ContextPtr rmesa, int id)
{
	return rmesa->rmm->u_list[id].ptr;
}

int radeon_mm_find(r300ContextPtr rmesa, void *ptr)
{
	int i;
	int id = 0;
	
	for (i=1; i < rmesa->rmm->u_size+1; i++)
		if(rmesa->rmm->u_list[i].ptr &&
		   ptr >= rmesa->rmm->u_list[i].ptr &&
	    	   ptr < rmesa->rmm->u_list[i].ptr + rmesa->rmm->u_list[i].size)
			break;
	
	if (i < rmesa->rmm->u_size + 1)
		return i;
	
	fprintf(stderr, "%p failed\n", ptr);
	return 0;
}

//#define MM_DEBUG
int radeon_mm_alloc(r300ContextPtr rmesa, int alignment, int size)
{
	drm_radeon_mem_alloc_t alloc;
	int offset, ret;
	int i, free=-1;
	int done_age;
	drm_radeon_mem_free_t memfree;
	int tries=0, tries2=0;
	static int t=0;
	static int bytes_wasted=0, allocated=0;
	
	if(size < 4096)
		bytes_wasted += 4096 - size;
	
	allocated += size;
	
#if 0
	if (t != time(NULL)) {
		t = time(NULL);
		fprintf(stderr, "slots used %d, wasted %d kb, allocated %d\n", rmesa->rmm->u_last, bytes_wasted/1024, allocated/1024);
	}
#endif
	
	memfree.region = RADEON_MEM_REGION_GART;
			
	again:
	
	done_age = rmesa->radeon.radeonScreen->scratch[2];
	
	for (i = rmesa->rmm->u_last + 1; i > 0; i --) {
		if (rmesa->rmm->u_list[i].ptr == NULL) {
			free = i;
			continue;
		}
		
		if (rmesa->rmm->u_list[i].h_pending == 0 &&
			rmesa->rmm->u_list[i].pending && rmesa->rmm->u_list[i].age <= done_age) {
			memfree.region_offset = (char *)rmesa->rmm->u_list[i].ptr -
						(char *)rmesa->radeon.radeonScreen->gartTextures.map;

			ret = drmCommandWrite(rmesa->radeon.radeonScreen->driScreen->fd,
					      DRM_RADEON_FREE, &memfree, sizeof(memfree));

			if (ret) {
				fprintf(stderr, "Failed to free at %p\n", rmesa->rmm->u_list[i].ptr);
				fprintf(stderr, "ret = %s\n", strerror(-ret));
				exit(1);
			} else {
#ifdef MM_DEBUG
				fprintf(stderr, "really freed %d at age %x\n", i, rmesa->radeon.radeonScreen->scratch[2]);
#endif
				if (i == rmesa->rmm->u_last)
					rmesa->rmm->u_last --;
				
					if(rmesa->rmm->u_list[i].size < 4096)
						bytes_wasted -= 4096 - rmesa->rmm->u_list[i].size;

				allocated -= rmesa->rmm->u_list[i].size;
				rmesa->rmm->u_list[i].pending = 0;
				rmesa->rmm->u_list[i].ptr = NULL;
				
				if (rmesa->rmm->u_list[i].fb) {
					LOCK_HARDWARE(&(rmesa->radeon));
					ret = mmFreeMem(rmesa->rmm->u_list[i].fb);
					UNLOCK_HARDWARE(&(rmesa->radeon));
					
					if (ret != 0)
						fprintf(stderr, "failed to free!\n");
					rmesa->rmm->u_list[i].fb = NULL;
				}
				rmesa->rmm->u_list[i].ref_count = 0;
				free = i;
			}
		}
	}
	done:
	rmesa->rmm->u_head = i;
	
	if (free == -1) {
		WARN_ONCE("Ran out of slots!\n");
		//usleep(100);
		r300FlushCmdBuf(rmesa, __FUNCTION__);
		tries++;
		if(tries>100){
			WARN_ONCE("Ran out of slots!\n");
			exit(1);
		}
		goto again;
	}
		
	alloc.region = RADEON_MEM_REGION_GART;
	alloc.alignment = alignment;
	alloc.size = size;
	alloc.region_offset = &offset;

	ret = drmCommandWriteRead( rmesa->radeon.dri.fd, DRM_RADEON_ALLOC, &alloc, sizeof(alloc));
   	if (ret) {
		WARN_ONCE("Ran out of mem!\n");
		r300FlushCmdBuf(rmesa, __FUNCTION__);
		//usleep(100);
		tries2++;
		tries = 0;
		if(tries2>100){
			WARN_ONCE("Ran out of GART memory!\n");
			exit(1);
		}
		goto again;
	}
	
	i = free;
	
	if (i > rmesa->rmm->u_last)
		rmesa->rmm->u_last = i;
	
	rmesa->rmm->u_list[i].ptr = ((GLubyte *)rmesa->radeon.radeonScreen->gartTextures.map) + offset;
	rmesa->rmm->u_list[i].size = size;
	rmesa->rmm->u_list[i].age = 0;
	rmesa->rmm->u_list[i].fb = NULL;
	//fprintf(stderr, "alloc %p at id %d\n", rmesa->rmm->u_list[i].ptr, i);
	
#ifdef MM_DEBUG
	fprintf(stderr, "allocated %d at age %x\n", i, rmesa->radeon.radeonScreen->scratch[2]);
#endif
	
	return i;
}

#include "r300_emit.h"
void emit_lin_cp(r300ContextPtr rmesa, unsigned long dst, unsigned long src, unsigned long size)
{
	LOCAL_VARS
	int cp_size;
	
	
	while (size > 0){
		cp_size = size;
		if(cp_size > /*8190*/4096)
			cp_size = /*8190*/4096;
		
		reg_start(0x146c,1);
		e32(0x52cc32fb);
	
		reg_start(0x15ac,1);
		e32(src);
		e32(cp_size);
	
		reg_start(0x1704,0);
		e32(0x0);

		reg_start(0x1404,1);
		e32(dst);
		e32(cp_size);
	
		reg_start(0x1700,0);
		e32(0x0);
	
		reg_start(0x1640,3);
		e32(0x00000000);
		e32(0x00001fff);
		e32(0x00000000);
		e32(0x00001fff);

		start_packet3(RADEON_CP_PACKET3_UNK1B, 2);
		e32(0 << 16 | 0);
		e32(0 << 16 | 0);
		e32(cp_size << 16 | 0x1);
		
		dst += cp_size;
		src += cp_size;
		size -= cp_size;
	}
	
	reg_start(0x4e4c,0);
	e32(0x0000000a);
	
	reg_start(0x342c,0);
	e32(0x00000005);
	
	reg_start(0x1720,0);
	e32(0x00010000);
}

void radeon_mm_use(r300ContextPtr rmesa, int id)
{
#ifdef MM_DEBUG
	fprintf(stderr, "%s: %d at age %x\n", __FUNCTION__, id, rmesa->radeon.radeonScreen->scratch[2]);
#endif	
	drm_r300_cmd_header_t *cmd;
	
	if(id == 0)
		return;
	
#if 0 /* FB VBOs. Needs further changes... */
	rmesa->rmm->u_list[id].ref_count ++;
	if (rmesa->rmm->u_list[id].ref_count > 100 && rmesa->rmm->u_list[id].fb == NULL &&
		rmesa->rmm->u_list[id].size != RADEON_BUFFER_SIZE*16 /*&& rmesa->rmm->u_list[id].size > 40*/) {
		driTexHeap *heap;
		struct mem_block *mb;
				
		LOCK_HARDWARE(&(rmesa->radeon));
	
		heap = rmesa->texture_heaps[0];
			
		mb = mmAllocMem(heap->memory_heap, rmesa->rmm->u_list[id].size, heap->alignmentShift, 0);
	
		UNLOCK_HARDWARE(&(rmesa->radeon));
	
		if (mb) {
			rmesa->rmm->u_list[id].fb = mb;
			
			emit_lin_cp(rmesa, rmesa->radeon.radeonScreen->texOffset[0] + rmesa->rmm->u_list[id].fb->ofs,
					r300GartOffsetFromVirtual(rmesa, rmesa->rmm->u_list[id].ptr),
					rmesa->rmm->u_list[id].size);
		} else {
			WARN_ONCE("Upload to fb failed, %d, %d\n", rmesa->rmm->u_list[id].size, id);
		}
		//fprintf(stderr, "Upload to fb! %d, %d\n", rmesa->rmm->u_list[id].ref_count, id);
	}
	/*if (rmesa->rmm->u_list[id].fb) {
		emit_lin_cp(rmesa, rmesa->radeon.radeonScreen->texOffset[0] + rmesa->rmm->u_list[id].fb->ofs,
				r300GartOffsetFromVirtual(rmesa, rmesa->rmm->u_list[id].ptr),
				rmesa->rmm->u_list[id].size);
	}*/
#endif
		
	cmd = r300AllocCmdBuf(rmesa, 4, __FUNCTION__);
	cmd[0].scratch.cmd_type = R300_CMD_SCRATCH;
	cmd[0].scratch.reg = 2;
	cmd[0].scratch.n_bufs = 1;
	cmd[0].scratch.flags = 0;
	cmd[1].u = (unsigned long)(&rmesa->rmm->vb_age);
	cmd[2].u = (unsigned long)(&rmesa->rmm->u_list[id].age);
	cmd[3].u = /*id*/0;
	
	LOCK_HARDWARE(&rmesa->radeon); /* Protect from DRM. */
	rmesa->rmm->u_list[id].h_pending ++;
	UNLOCK_HARDWARE(&rmesa->radeon);
}

unsigned long radeon_mm_offset(r300ContextPtr rmesa, int id)
{
	unsigned long offset;
	
	if (rmesa->rmm->u_list[id].fb) {
		offset = rmesa->radeon.radeonScreen->texOffset[0] + rmesa->rmm->u_list[id].fb->ofs;
	} else {
		offset = (char *)rmesa->rmm->u_list[id].ptr -
			(char *)rmesa->radeon.radeonScreen->gartTextures.map;
		offset += rmesa->radeon.radeonScreen->gart_texture_offset;
	}
	
	return offset;
}

int radeon_mm_on_card(r300ContextPtr rmesa, int id)
{
	if (rmesa->rmm->u_list[id].fb)
		return GL_TRUE;
	
	return GL_FALSE;
}
		
void *radeon_mm_map(r300ContextPtr rmesa, int id, int access)
{
#ifdef MM_DEBUG
	fprintf(stderr, "%s: %d at age %x\n", __FUNCTION__, id, rmesa->radeon.radeonScreen->scratch[2]);
#endif	
	void *ptr;
	int tries = 0;
	
	rmesa->rmm->u_list[id].ref_count = 0;
	if (rmesa->rmm->u_list[id].fb) {
		WARN_ONCE("Mapping fb!\n");
		/* Idle gart only and do upload on unmap */
		//rmesa->rmm->u_list[id].fb = NULL;
		
		
		if(rmesa->rmm->u_list[id].mapped == 1)
			WARN_ONCE("buffer %d already mapped\n", id);
	
		rmesa->rmm->u_list[id].mapped = 1;
		ptr = radeon_mm_ptr(rmesa, id);
		
		return ptr;
	}
	
	if (access == RADEON_MM_R) {
		
		if(rmesa->rmm->u_list[id].mapped == 1)
			WARN_ONCE("buffer %d already mapped\n", id);
	
		rmesa->rmm->u_list[id].mapped = 1;
		ptr = radeon_mm_ptr(rmesa, id);
		
		return ptr;
	}
	
	
	if (rmesa->rmm->u_list[id].h_pending)
		r300FlushCmdBuf(rmesa, __FUNCTION__);
	
	if (rmesa->rmm->u_list[id].h_pending) {
		return NULL;
	}
	
	while(rmesa->rmm->u_list[id].age > rmesa->radeon.radeonScreen->scratch[2] && tries++ < 1000)
		usleep(10);
	
	if (tries >= 1000) {
		fprintf(stderr, "Idling failed (%x vs %x)\n",
				rmesa->rmm->u_list[id].age, rmesa->radeon.radeonScreen->scratch[2]);
		return NULL;
	}
	
	if(rmesa->rmm->u_list[id].mapped == 1)
		WARN_ONCE("buffer %d already mapped\n", id);
	
	rmesa->rmm->u_list[id].mapped = 1;
	ptr = radeon_mm_ptr(rmesa, id);
	
	return ptr;
}

void radeon_mm_unmap(r300ContextPtr rmesa, int id)
{
#ifdef MM_DEBUG
	fprintf(stderr, "%s: %d at age %x\n", __FUNCTION__, id, rmesa->radeon.radeonScreen->scratch[2]);
#endif	
	
	if(rmesa->rmm->u_list[id].mapped == 0)
		WARN_ONCE("buffer %d not mapped\n", id);
	
	rmesa->rmm->u_list[id].mapped = 0;
	
	if (rmesa->rmm->u_list[id].fb)
		emit_lin_cp(rmesa, rmesa->radeon.radeonScreen->texOffset[0] + rmesa->rmm->u_list[id].fb->ofs,
				r300GartOffsetFromVirtual(rmesa, rmesa->rmm->u_list[id].ptr),
				rmesa->rmm->u_list[id].size);
}

void radeon_mm_free(r300ContextPtr rmesa, int id)
{
#ifdef MM_DEBUG
	fprintf(stderr, "%s: %d at age %x\n", __FUNCTION__, id, rmesa->radeon.radeonScreen->scratch[2]);
#endif	
	
	if(id == 0)
		return;
	
	if(rmesa->rmm->u_list[id].ptr == NULL){
		WARN_ONCE("Not allocated!\n");
		return ;
	}
	
	if(rmesa->rmm->u_list[id].pending){
		WARN_ONCE("%p already pended!\n", rmesa->rmm->u_list[id].ptr);
		return ;
	}
			
	rmesa->rmm->u_list[id].pending = 1;
}
#endif