Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / winsys / xlib / brw_aub.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "brw_aub.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_debug.h"
38 #include "util/u_memory.h"
39
40
41 struct brw_aubfile {
42 FILE *file;
43 unsigned next_free_page;
44 };
45
46
47 extern char *__progname;
48
49
50 struct aub_file_header {
51 unsigned int instruction_type;
52 unsigned int pad0:16;
53 unsigned int minor:8;
54 unsigned int major:8;
55 unsigned char application[8*4];
56 unsigned int day:8;
57 unsigned int month:8;
58 unsigned int year:16;
59 unsigned int timezone:8;
60 unsigned int second:8;
61 unsigned int minute:8;
62 unsigned int hour:8;
63 unsigned int comment_length:16;
64 unsigned int pad1:16;
65 };
66
67 struct aub_block_header {
68 unsigned int instruction_type;
69 unsigned int operation:8;
70 unsigned int type:8;
71 unsigned int address_space:8;
72 unsigned int pad0:8;
73 unsigned int general_state_type:8;
74 unsigned int surface_state_type:8;
75 unsigned int pad1:16;
76 unsigned int address;
77 unsigned int length;
78 };
79
80 struct aub_dump_bmp {
81 unsigned int instruction_type;
82 unsigned int xmin:16;
83 unsigned int ymin:16;
84 unsigned int pitch:16;
85 unsigned int bpp:8;
86 unsigned int format:8;
87 unsigned int xsize:16;
88 unsigned int ysize:16;
89 unsigned int addr;
90 unsigned int unknown;
91 };
92
93 enum bh_operation {
94 BH_COMMENT,
95 BH_DATA_WRITE,
96 BH_COMMAND_WRITE,
97 BH_MMI0_WRITE32,
98 BH_END_SCENE,
99 BH_CONFIG_MEMORY_MAP,
100 BH_MAX_OPERATION
101 };
102
103 enum command_write_type {
104 CW_HWB_RING = 1,
105 CW_PRIMARY_RING_A,
106 CW_PRIMARY_RING_B, /* XXX - disagreement with listaub! */
107 CW_PRIMARY_RING_C,
108 CW_MAX_TYPE
109 };
110
111 enum memory_map_type {
112 MM_DEFAULT,
113 MM_DYNAMIC,
114 MM_MAX_TYPE
115 };
116
117 enum address_space {
118 ADDR_GTT,
119 ADDR_LOCAL,
120 ADDR_MAIN,
121 ADDR_MAX
122 };
123
124
125 #define AUB_FILE_HEADER 0xe085000b
126 #define AUB_BLOCK_HEADER 0xe0c10003
127 #define AUB_DUMP_BMP 0xe09e0004
128
129 /* Registers to control page table
130 */
131 #define PGETBL_CTL 0x2020
132 #define PGETBL_ENABLED 0x1
133
134 #define NR_GTT_ENTRIES 65536 /* 256 mb */
135
136 #define FAIL \
137 do { \
138 fprintf(stderr, "failed to write aub data at %s/%d\n", __FUNCTION__, __LINE__); \
139 exit(1); \
140 } while (0)
141
142
143 /* Emit the headers at the top of each aubfile. Initialize the GTT.
144 */
145 static void init_aubfile( FILE *aub_file )
146 {
147 struct aub_file_header fh;
148 struct aub_block_header bh;
149 unsigned int data;
150
151 static int nr;
152
153 nr++;
154
155 /* Emit the aub header:
156 */
157 memset(&fh, 0, sizeof(fh));
158
159 fh.instruction_type = AUB_FILE_HEADER;
160 fh.minor = 0x0;
161 fh.major = 0x7;
162 memcpy(fh.application, __progname, sizeof(fh.application));
163 fh.day = (nr>>24) & 0xff;
164 fh.month = 0x0;
165 fh.year = 0x0;
166 fh.timezone = 0x0;
167 fh.second = nr & 0xff;
168 fh.minute = (nr>>8) & 0xff;
169 fh.hour = (nr>>16) & 0xff;
170 fh.comment_length = 0x0;
171
172 if (fwrite(&fh, sizeof(fh), 1, aub_file) < 0)
173 FAIL;
174
175 /* Setup the GTT starting at main memory address zero (!):
176 */
177 memset(&bh, 0, sizeof(bh));
178
179 bh.instruction_type = AUB_BLOCK_HEADER;
180 bh.operation = BH_MMI0_WRITE32;
181 bh.type = 0x0;
182 bh.address_space = ADDR_GTT; /* ??? */
183 bh.general_state_type = 0x0;
184 bh.surface_state_type = 0x0;
185 bh.address = PGETBL_CTL;
186 bh.length = 0x4;
187
188 if (fwrite(&bh, sizeof(bh), 1, aub_file) < 0)
189 FAIL;
190
191 data = 0x0 | PGETBL_ENABLED;
192
193 if (fwrite(&data, sizeof(data), 1, aub_file) < 0)
194 FAIL;
195 }
196
197
198 static void init_aub_gtt( struct brw_aubfile *aubfile,
199 unsigned start_offset,
200 unsigned size )
201 {
202 FILE *aub_file = aubfile->file;
203 struct aub_block_header bh;
204 unsigned int i;
205
206 assert(start_offset + size < NR_GTT_ENTRIES * 4096);
207
208
209 memset(&bh, 0, sizeof(bh));
210
211 bh.instruction_type = AUB_BLOCK_HEADER;
212 bh.operation = BH_DATA_WRITE;
213 bh.type = 0x0;
214 bh.address_space = ADDR_MAIN;
215 bh.general_state_type = 0x0;
216 bh.surface_state_type = 0x0;
217 bh.address = start_offset / 4096 * 4;
218 bh.length = size / 4096 * 4;
219
220 if (fwrite(&bh, sizeof(bh), 1, aub_file) < 0)
221 FAIL;
222
223 for (i = 0; i < size / 4096; i++) {
224 unsigned data = aubfile->next_free_page | 1;
225
226 aubfile->next_free_page += 4096;
227
228 if (fwrite(&data, sizeof(data), 1, aub_file) < 0)
229 FAIL;
230 }
231
232 }
233
234 static void write_block_header( FILE *aub_file,
235 struct aub_block_header *bh,
236 const unsigned *data,
237 unsigned sz )
238 {
239 sz = (sz + 3) & ~3;
240
241 if (fwrite(bh, sizeof(*bh), 1, aub_file) < 0)
242 FAIL;
243
244 if (fwrite(data, sz, 1, aub_file) < 0)
245 FAIL;
246
247 fflush(aub_file);
248 }
249
250
251 static void write_dump_bmp( FILE *aub_file,
252 struct aub_dump_bmp *db )
253 {
254 if (fwrite(db, sizeof(*db), 1, aub_file) < 0)
255 FAIL;
256
257 fflush(aub_file);
258 }
259
260
261
262 void brw_aub_gtt_data( struct brw_aubfile *aubfile,
263 unsigned offset,
264 const void *data,
265 unsigned sz,
266 unsigned type,
267 unsigned state_type )
268 {
269 struct aub_block_header bh;
270
271 bh.instruction_type = AUB_BLOCK_HEADER;
272 bh.operation = BH_DATA_WRITE;
273 bh.type = type;
274 bh.address_space = ADDR_GTT;
275 bh.pad0 = 0;
276
277 if (type == DW_GENERAL_STATE) {
278 bh.general_state_type = state_type;
279 bh.surface_state_type = 0;
280 }
281 else {
282 bh.general_state_type = 0;
283 bh.surface_state_type = state_type;
284 }
285
286 bh.pad1 = 0;
287 bh.address = offset;
288 bh.length = sz;
289
290 write_block_header(aubfile->file, &bh, data, sz);
291 }
292
293
294
295 void brw_aub_gtt_cmds( struct brw_aubfile *aubfile,
296 unsigned offset,
297 const void *data,
298 unsigned sz )
299 {
300 struct aub_block_header bh;
301 unsigned type = CW_PRIMARY_RING_A;
302
303
304 bh.instruction_type = AUB_BLOCK_HEADER;
305 bh.operation = BH_COMMAND_WRITE;
306 bh.type = type;
307 bh.address_space = ADDR_GTT;
308 bh.pad0 = 0;
309 bh.general_state_type = 0;
310 bh.surface_state_type = 0;
311 bh.pad1 = 0;
312 bh.address = offset;
313 bh.length = sz;
314
315 write_block_header(aubfile->file, &bh, data, sz);
316 }
317
318 void brw_aub_dump_bmp( struct brw_aubfile *aubfile,
319 struct pipe_surface *surface,
320 unsigned gtt_offset )
321 {
322 struct aub_dump_bmp db;
323 unsigned format;
324
325 assert(surface->block.width == 1);
326 assert(surface->block.height == 1);
327
328 if (surface->block.size == 4)
329 format = 0x7;
330 else
331 format = 0x3;
332
333 db.instruction_type = AUB_DUMP_BMP;
334 db.xmin = 0;
335 db.ymin = 0;
336 db.format = format;
337 db.bpp = surface->block.size * 8;
338 db.pitch = surface->stride/surface->block.size;
339 db.xsize = surface->width;
340 db.ysize = surface->height;
341 db.addr = gtt_offset;
342 db.unknown = /* surface->tiled ? 0x4 : */ 0x0;
343
344 write_dump_bmp(aubfile->file, &db);
345 }
346
347
348
349 struct brw_aubfile *brw_aubfile_create( void )
350 {
351 struct brw_aubfile *aubfile = CALLOC_STRUCT(brw_aubfile);
352 char filename[80];
353 int val;
354 static int i = 0;
355
356 i++;
357
358 if (getenv("INTEL_AUBFILE")) {
359 val = snprintf(filename, sizeof(filename), "%s%d.aub", getenv("INTEL_AUBFILE"), i%4);
360 debug_printf("--> Aub file: %s\n", filename);
361 aubfile->file = fopen(filename, "w");
362 }
363 else {
364 val = snprintf(filename, sizeof(filename), "%s.aub", __progname);
365 if (val < 0 || val > sizeof(filename))
366 strcpy(filename, "default.aub");
367
368 debug_printf("--> Aub file: %s\n", filename);
369 aubfile->file = fopen(filename, "w");
370 }
371
372 if (!aubfile->file) {
373 debug_printf("couldn't open aubfile\n");
374 exit(1);
375 }
376
377 init_aubfile(aubfile->file);
378
379 /* The GTT is located starting address zero in main memory. Pages
380 * to populate the gtt start after this point.
381 */
382 aubfile->next_free_page = (NR_GTT_ENTRIES * 4 + 4095) & ~4095;
383
384 /* More or less correspond with all the agp regions mapped by the
385 * driver:
386 */
387 init_aub_gtt(aubfile, 0, 4096*4);
388 init_aub_gtt(aubfile, AUB_BUF_START, AUB_BUF_SIZE);
389
390 return aubfile;
391 }
392
393 void brw_aub_destroy( struct brw_aubfile *aubfile )
394 {
395 fclose(aubfile->file);
396 FREE(aubfile);
397 }