gallium: Remove do_flip argument from surface_copy
[mesa.git] / src / gallium / winsys / xlib / 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 "xlib_brw_aub.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_state.h"
37 #include "util/u_debug.h"
38 #include "util/u_memory.h"
39 #include "softpipe/sp_texture.h"
40
41
42 struct brw_aubfile {
43 FILE *file;
44 unsigned next_free_page;
45 };
46
47
48 extern char *__progname;
49
50
51 struct aub_file_header {
52 unsigned int instruction_type;
53 unsigned int pad0:16;
54 unsigned int minor:8;
55 unsigned int major:8;
56 unsigned char application[8*4];
57 unsigned int day:8;
58 unsigned int month:8;
59 unsigned int year:16;
60 unsigned int timezone:8;
61 unsigned int second:8;
62 unsigned int minute:8;
63 unsigned int hour:8;
64 unsigned int comment_length:16;
65 unsigned int pad1:16;
66 };
67
68 struct aub_block_header {
69 unsigned int instruction_type;
70 unsigned int operation:8;
71 unsigned int type:8;
72 unsigned int address_space:8;
73 unsigned int pad0:8;
74 unsigned int general_state_type:8;
75 unsigned int surface_state_type:8;
76 unsigned int pad1:16;
77 unsigned int address;
78 unsigned int length;
79 };
80
81 struct aub_dump_bmp {
82 unsigned int instruction_type;
83 unsigned int xmin:16;
84 unsigned int ymin:16;
85 unsigned int pitch:16;
86 unsigned int bpp:8;
87 unsigned int format:8;
88 unsigned int xsize:16;
89 unsigned int ysize:16;
90 unsigned int addr;
91 unsigned int unknown;
92 };
93
94 enum bh_operation {
95 BH_COMMENT,
96 BH_DATA_WRITE,
97 BH_COMMAND_WRITE,
98 BH_MMI0_WRITE32,
99 BH_END_SCENE,
100 BH_CONFIG_MEMORY_MAP,
101 BH_MAX_OPERATION
102 };
103
104 enum command_write_type {
105 CW_HWB_RING = 1,
106 CW_PRIMARY_RING_A,
107 CW_PRIMARY_RING_B, /* XXX - disagreement with listaub! */
108 CW_PRIMARY_RING_C,
109 CW_MAX_TYPE
110 };
111
112 enum memory_map_type {
113 MM_DEFAULT,
114 MM_DYNAMIC,
115 MM_MAX_TYPE
116 };
117
118 enum address_space {
119 ADDR_GTT,
120 ADDR_LOCAL,
121 ADDR_MAIN,
122 ADDR_MAX
123 };
124
125
126 #define AUB_FILE_HEADER 0xe085000b
127 #define AUB_BLOCK_HEADER 0xe0c10003
128 #define AUB_DUMP_BMP 0xe09e0004
129
130 /* Registers to control page table
131 */
132 #define PGETBL_CTL 0x2020
133 #define PGETBL_ENABLED 0x1
134
135 #define NR_GTT_ENTRIES 65536 /* 256 mb */
136
137 #define FAIL \
138 do { \
139 fprintf(stderr, "failed to write aub data at %s/%d\n", __FUNCTION__, __LINE__); \
140 exit(1); \
141 } while (0)
142
143
144 /* Emit the headers at the top of each aubfile. Initialize the GTT.
145 */
146 static void init_aubfile( FILE *aub_file )
147 {
148 struct aub_file_header fh;
149 struct aub_block_header bh;
150 unsigned int data;
151
152 static int nr;
153
154 nr++;
155
156 /* Emit the aub header:
157 */
158 memset(&fh, 0, sizeof(fh));
159
160 fh.instruction_type = AUB_FILE_HEADER;
161 fh.minor = 0x0;
162 fh.major = 0x7;
163 memcpy(fh.application, __progname, sizeof(fh.application));
164 fh.day = (nr>>24) & 0xff;
165 fh.month = 0x0;
166 fh.year = 0x0;
167 fh.timezone = 0x0;
168 fh.second = nr & 0xff;
169 fh.minute = (nr>>8) & 0xff;
170 fh.hour = (nr>>16) & 0xff;
171 fh.comment_length = 0x0;
172
173 if (fwrite(&fh, sizeof(fh), 1, aub_file) < 0)
174 FAIL;
175
176 /* Setup the GTT starting at main memory address zero (!):
177 */
178 memset(&bh, 0, sizeof(bh));
179
180 bh.instruction_type = AUB_BLOCK_HEADER;
181 bh.operation = BH_MMI0_WRITE32;
182 bh.type = 0x0;
183 bh.address_space = ADDR_GTT; /* ??? */
184 bh.general_state_type = 0x0;
185 bh.surface_state_type = 0x0;
186 bh.address = PGETBL_CTL;
187 bh.length = 0x4;
188
189 if (fwrite(&bh, sizeof(bh), 1, aub_file) < 0)
190 FAIL;
191
192 data = 0x0 | PGETBL_ENABLED;
193
194 if (fwrite(&data, sizeof(data), 1, aub_file) < 0)
195 FAIL;
196 }
197
198
199 static void init_aub_gtt( struct brw_aubfile *aubfile,
200 unsigned start_offset,
201 unsigned size )
202 {
203 FILE *aub_file = aubfile->file;
204 struct aub_block_header bh;
205 unsigned int i;
206
207 assert(start_offset + size < NR_GTT_ENTRIES * 4096);
208
209
210 memset(&bh, 0, sizeof(bh));
211
212 bh.instruction_type = AUB_BLOCK_HEADER;
213 bh.operation = BH_DATA_WRITE;
214 bh.type = 0x0;
215 bh.address_space = ADDR_MAIN;
216 bh.general_state_type = 0x0;
217 bh.surface_state_type = 0x0;
218 bh.address = start_offset / 4096 * 4;
219 bh.length = size / 4096 * 4;
220
221 if (fwrite(&bh, sizeof(bh), 1, aub_file) < 0)
222 FAIL;
223
224 for (i = 0; i < size / 4096; i++) {
225 unsigned data = aubfile->next_free_page | 1;
226
227 aubfile->next_free_page += 4096;
228
229 if (fwrite(&data, sizeof(data), 1, aub_file) < 0)
230 FAIL;
231 }
232
233 }
234
235 static void write_block_header( FILE *aub_file,
236 struct aub_block_header *bh,
237 const unsigned *data,
238 unsigned sz )
239 {
240 sz = (sz + 3) & ~3;
241
242 if (fwrite(bh, sizeof(*bh), 1, aub_file) < 0)
243 FAIL;
244
245 if (fwrite(data, sz, 1, aub_file) < 0)
246 FAIL;
247
248 fflush(aub_file);
249 }
250
251
252 static void write_dump_bmp( FILE *aub_file,
253 struct aub_dump_bmp *db )
254 {
255 if (fwrite(db, sizeof(*db), 1, aub_file) < 0)
256 FAIL;
257
258 fflush(aub_file);
259 }
260
261
262
263 void brw_aub_gtt_data( struct brw_aubfile *aubfile,
264 unsigned offset,
265 const void *data,
266 unsigned sz,
267 unsigned type,
268 unsigned state_type )
269 {
270 struct aub_block_header bh;
271
272 bh.instruction_type = AUB_BLOCK_HEADER;
273 bh.operation = BH_DATA_WRITE;
274 bh.type = type;
275 bh.address_space = ADDR_GTT;
276 bh.pad0 = 0;
277
278 if (type == DW_GENERAL_STATE) {
279 bh.general_state_type = state_type;
280 bh.surface_state_type = 0;
281 }
282 else {
283 bh.general_state_type = 0;
284 bh.surface_state_type = state_type;
285 }
286
287 bh.pad1 = 0;
288 bh.address = offset;
289 bh.length = sz;
290
291 write_block_header(aubfile->file, &bh, data, sz);
292 }
293
294
295
296 void brw_aub_gtt_cmds( struct brw_aubfile *aubfile,
297 unsigned offset,
298 const void *data,
299 unsigned sz )
300 {
301 struct aub_block_header bh;
302 unsigned type = CW_PRIMARY_RING_A;
303
304
305 bh.instruction_type = AUB_BLOCK_HEADER;
306 bh.operation = BH_COMMAND_WRITE;
307 bh.type = type;
308 bh.address_space = ADDR_GTT;
309 bh.pad0 = 0;
310 bh.general_state_type = 0;
311 bh.surface_state_type = 0;
312 bh.pad1 = 0;
313 bh.address = offset;
314 bh.length = sz;
315
316 write_block_header(aubfile->file, &bh, data, sz);
317 }
318
319 void brw_aub_dump_bmp( struct brw_aubfile *aubfile,
320 struct pipe_surface *surface,
321 unsigned gtt_offset )
322 {
323 struct aub_dump_bmp db;
324 unsigned format;
325
326 assert(surface->texture->block.width == 1);
327 assert(surface->texture->block.height == 1);
328
329 if (surface->texture->block.size == 4)
330 format = 0x7;
331 else
332 format = 0x3;
333
334 db.instruction_type = AUB_DUMP_BMP;
335 db.xmin = 0;
336 db.ymin = 0;
337 db.format = format;
338 db.bpp = surface->texture->block.size * 8;
339 db.pitch = softpipe_texture(surface->texture)->stride[surface->level] /
340 surface->texture->block.size;
341 db.xsize = surface->width;
342 db.ysize = surface->height;
343 db.addr = gtt_offset;
344 db.unknown = /* surface->tiled ? 0x4 : */ 0x0;
345
346 write_dump_bmp(aubfile->file, &db);
347 }
348
349
350
351 struct brw_aubfile *brw_aubfile_create( void )
352 {
353 struct brw_aubfile *aubfile = CALLOC_STRUCT(brw_aubfile);
354 char filename[80];
355 int val;
356 static int i = 0;
357
358 i++;
359
360 if (getenv("INTEL_AUBFILE")) {
361 val = snprintf(filename, sizeof(filename), "%s%d.aub", getenv("INTEL_AUBFILE"), i%4);
362 debug_printf("--> Aub file: %s\n", filename);
363 aubfile->file = fopen(filename, "w");
364 }
365 else {
366 val = snprintf(filename, sizeof(filename), "%s.aub", __progname);
367 if (val < 0 || val > sizeof(filename))
368 strcpy(filename, "default.aub");
369
370 debug_printf("--> Aub file: %s\n", filename);
371 aubfile->file = fopen(filename, "w");
372 }
373
374 if (!aubfile->file) {
375 debug_printf("couldn't open aubfile\n");
376 exit(1);
377 }
378
379 init_aubfile(aubfile->file);
380
381 /* The GTT is located starting address zero in main memory. Pages
382 * to populate the gtt start after this point.
383 */
384 aubfile->next_free_page = (NR_GTT_ENTRIES * 4 + 4095) & ~4095;
385
386 /* More or less correspond with all the agp regions mapped by the
387 * driver:
388 */
389 init_aub_gtt(aubfile, 0, 4096*4);
390 init_aub_gtt(aubfile, AUB_BUF_START, AUB_BUF_SIZE);
391
392 return aubfile;
393 }
394
395 void brw_aub_destroy( struct brw_aubfile *aubfile )
396 {
397 fclose(aubfile->file);
398 FREE(aubfile);
399 }