summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/i965/brw_aub.c
blob: f851a5b795500304408040d1fd026e9ec49b9c77 (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
/*
 Copyright (C) Intel Corp.  2006.  All Rights Reserved.
 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
 develop this 3D driver.
 
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
 
 The above copyright notice and this permission notice (including the
 next paragraph) shall be included in all copies or substantial
 portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 **********************************************************************/
 /*
  * Authors:
  *   Keith Whitwell <keith@tungstengraphics.com>
  */

#include "brw_context.h"
#include "brw_aub.h"
#include "intel_regions.h"
#include <stdio.h>

extern char *__progname;


/* Registers to control page table
 */
#define PGETBL_CTL       0x2020
#define PGETBL_ENABLED   0x1

#define NR_GTT_ENTRIES  65536	/* 256 mb */

#define FAIL										\
do {											\
   fprintf(stderr, "failed to write aub data at %s/%d\n", __FUNCTION__, __LINE__);	\
   exit(1);										\
} while (0)


/* Emit the headers at the top of each aubfile.  Initialize the GTT.
 */
static void init_aubfile( FILE *aub_file )
{   
   struct aub_file_header fh;
   struct aub_block_header bh;
   unsigned int data;

   static int nr;
   
   nr++;

   /* Emit the aub header:
    */
   memset(&fh, 0, sizeof(fh));

   fh.instruction_type = AUB_FILE_HEADER;
   fh.minor = 0x0;
   fh.major = 0x7;
   memcpy(fh.application, __progname, sizeof(fh.application));
   fh.day = (nr>>24) & 0xff;
   fh.month = 0x0;
   fh.year = 0x0;
   fh.timezone = 0x0;
   fh.second = nr & 0xff;
   fh.minute = (nr>>8) & 0xff;
   fh.hour = (nr>>16) & 0xff;
   fh.comment_length = 0x0;   

   if (fwrite(&fh, sizeof(fh), 1, aub_file) < 0) 
      FAIL;
         
   /* Setup the GTT starting at main memory address zero (!):
    */
   memset(&bh, 0, sizeof(bh));
   
   bh.instruction_type = AUB_BLOCK_HEADER;
   bh.operation = BH_MMI0_WRITE32;
   bh.type = 0x0;
   bh.address_space = ADDR_GTT;	/* ??? */
   bh.general_state_type = 0x0;
   bh.surface_state_type = 0x0;
   bh.address = PGETBL_CTL;
   bh.length = 0x4;

   if (fwrite(&bh, sizeof(bh), 1, aub_file) < 0) 
      FAIL;

   data = 0x0 | PGETBL_ENABLED;

   if (fwrite(&data, sizeof(data), 1, aub_file) < 0) 
      FAIL;
}


static void init_aub_gtt( struct brw_context *brw,
			  GLuint start_offset, 
			  GLuint size )
{
   FILE *aub_file = brw->intel.aub_file;
   struct aub_block_header bh;
   unsigned int i;

   assert(start_offset + size < NR_GTT_ENTRIES * 4096);


   memset(&bh, 0, sizeof(bh));
   
   bh.instruction_type = AUB_BLOCK_HEADER;
   bh.operation = BH_DATA_WRITE;
   bh.type = 0x0;
   bh.address_space = ADDR_MAIN;
   bh.general_state_type = 0x0;
   bh.surface_state_type = 0x0;
   bh.address =  start_offset / 4096 * 4;
   bh.length = size / 4096 * 4;

   if (fwrite(&bh, sizeof(bh), 1, aub_file) < 0) 
      FAIL;

   for (i = 0; i < size / 4096; i++) {
      GLuint data = brw->next_free_page | 1;

      brw->next_free_page += 4096;

      if (fwrite(&data, sizeof(data), 1, aub_file) < 0) 
	 FAIL;
   }

}

static void write_block_header( FILE *aub_file,
				struct aub_block_header *bh,
				const GLuint *data,
				GLuint sz )
{
   sz = (sz + 3) & ~3;

   if (fwrite(bh, sizeof(*bh), 1, aub_file) < 0) 
      FAIL;

   if (fwrite(data, sz, 1, aub_file) < 0) 
      FAIL;

   fflush(aub_file);
}


static void write_dump_bmp( FILE *aub_file,
			    struct aub_dump_bmp *db )
{
   if (fwrite(db, sizeof(*db), 1, aub_file) < 0) 
      FAIL;

   fflush(aub_file);
}



static void brw_aub_gtt_data( struct intel_context *intel,
			      GLuint offset,
			      const void *data,
			      GLuint sz,
			      GLuint type,
			      GLuint state_type )
{
   struct aub_block_header bh;

   bh.instruction_type = AUB_BLOCK_HEADER;
   bh.operation = BH_DATA_WRITE;
   bh.type = type;
   bh.address_space = ADDR_GTT;
   bh.pad0 = 0;

   if (type == DW_GENERAL_STATE) {
      bh.general_state_type = state_type;
      bh.surface_state_type = 0;
   }
   else {
      bh.general_state_type = 0;
      bh.surface_state_type = state_type;
   }

   bh.pad1 = 0;
   bh.address = offset;
   bh.length = sz;

   write_block_header(intel->aub_file, &bh, data, sz);
}



static void brw_aub_gtt_cmds( struct intel_context *intel,
			      GLuint offset,
			      const void *data,
			      GLuint sz )
{
   struct brw_context *brw = brw_context(&intel->ctx);
   struct aub_block_header bh;   
   GLuint type = CW_PRIMARY_RING_A;
   

   bh.instruction_type = AUB_BLOCK_HEADER;
   bh.operation = BH_COMMAND_WRITE;
   bh.type = type;
   bh.address_space = ADDR_GTT;
   bh.pad0 = 0;
   bh.general_state_type = 0;
   bh.surface_state_type = 0;
   bh.pad1 = 0;
   bh.address = offset;
   bh.length = sz;

   write_block_header(brw->intel.aub_file, &bh, data, sz);
}

static void brw_aub_dump_bmp( struct intel_context *intel,
			      GLuint buffer )
{
   struct brw_context *brw = brw_context(&intel->ctx);
   intelScreenPrivate *intelScreen = brw->intel.intelScreen;
   struct aub_dump_bmp db;
   GLuint format;

   if (intelScreen->cpp == 4)
      format = 0x7;
   else
      format = 0x3;


   if (buffer == 0) {
      db.instruction_type = AUB_DUMP_BMP;
      db.xmin = 0;
      db.ymin = 0;
      db.format = format;
      db.bpp = intelScreen->cpp * 8;
      db.pitch = intelScreen->front.pitch / intelScreen->cpp;
      db.xsize = intelScreen->width;
      db.ysize = intelScreen->height;
      db.addr = intelScreen->front.offset;
      db.unknown = 0x0;		/* 4: xmajor tiled, 0: not tiled */

      write_dump_bmp(brw->intel.aub_file, &db);
   }
   else {
      db.instruction_type = AUB_DUMP_BMP;
      db.xmin = 0;
      db.ymin = 0;
      db.format = format;
      db.bpp = intel->back_region->cpp * 8;
      db.pitch = intel->back_region->pitch;
      db.xsize = intel->back_region->pitch;
      db.ysize = intel->back_region->height;
      db.addr = intelScreen->back.offset;
      db.unknown = intel->back_region->tiled ? 0x4 : 0x0;

      write_dump_bmp(brw->intel.aub_file, &db);
   }
}

/* Attempt to prevent monster aubfiles by closing and reopening when
 * the state pools wrap.
 */
static void brw_aub_wrap( struct intel_context *intel )
{
   struct brw_context *brw = brw_context(&intel->ctx);   
   if (intel->aub_file) {
      brw_aub_destroy(brw);
      brw_aub_init(brw);
   }
   brw->wrap = 1;		/* ??? */
}


int brw_aub_init( struct brw_context *brw )
{
   struct intel_context *intel = &brw->intel;
   intelScreenPrivate *intelScreen = intel->intelScreen;
   char filename[80];
   int val;
   static int i = 0;

   i++;

   if (_mesa_getenv("INTEL_REPLAY"))
      return 0;

   if (_mesa_getenv("INTEL_AUBFILE")) {
      val = snprintf(filename, sizeof(filename), "%s%d.aub", _mesa_getenv("INTEL_AUBFILE"), i%4);
      _mesa_printf("--> Aub file: %s\n", filename);
      brw->intel.aub_file = fopen(filename, "w");
   }
   else if (_mesa_getenv("INTEL_AUB")) {
      val = snprintf(filename, sizeof(filename), "%s.aub", __progname);
      if (val < 0 || val > sizeof(filename)) 
	 strcpy(filename, "default.aub");   
   
      _mesa_printf("--> Aub file: %s\n", filename);
      brw->intel.aub_file = fopen(filename, "w");
   }
   else {
      return 0;
   }

   if (!brw->intel.aub_file) {
      _mesa_printf("couldn't open aubfile\n");
      exit(1);
   }

   brw->intel.vtbl.aub_commands = brw_aub_gtt_cmds;
   brw->intel.vtbl.aub_dump_bmp = brw_aub_dump_bmp;
   brw->intel.vtbl.aub_gtt_data = brw_aub_gtt_data;
   brw->intel.vtbl.aub_wrap = brw_aub_wrap;
   
   init_aubfile(brw->intel.aub_file);

   /* The GTT is located starting address zero in main memory.  Pages
    * to populate the gtt start after this point.
    */
   brw->next_free_page = (NR_GTT_ENTRIES * 4 + 4095) & ~4095;

   /* More or less correspond with all the agp regions mapped by the
    * driver:
    */
   init_aub_gtt(brw, 0, 4096*4); /* so new fulsim doesn't crash */
   init_aub_gtt(brw, intelScreen->front.offset, intelScreen->back.size);
   init_aub_gtt(brw, intelScreen->back.offset, intelScreen->back.size);
   init_aub_gtt(brw, intelScreen->depth.offset, intelScreen->back.size);
   init_aub_gtt(brw, intelScreen->tex.offset, intelScreen->tex.size);

   return 0;
}

void brw_aub_destroy( struct brw_context *brw )
{
   if (brw->intel.aub_file) {
      fclose(brw->intel.aub_file);
      brw->intel.aub_file = NULL;
   }
}