summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/ilo/ilo_builder.c
blob: d068e5bafad2473628da71f98a5fc3fdecb0730b (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2014 LunarG, Inc.
 *
 * 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 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 AUTHORS OR COPYRIGHT HOLDERS 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:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include "ilo_builder.h"
#include "ilo_builder_render.h" /* for ilo_builder_batch_patch_sba() */

enum ilo_builder_writer_flags {
   /*
    * When this bit is set, ilo_builder_begin() will not realllocate.  New
    * data will be appended instead.
    */
   WRITER_FLAG_APPEND    = 1 << 0,

   /*
    * When this bit is set, the writer grows when full.  When not, callers
    * must make sure the writer never needs to grow.
    */
   WRITER_FLAG_GROW      = 1 << 1,

   /*
    * The writer will be mapped directly.
    */
   WRITER_FLAG_MAP       = 1 << 2,
};

/**
 * Set the initial size and flags of a writer.
 */
static void
ilo_builder_writer_init(struct ilo_builder *builder,
                        enum ilo_builder_writer_type which)
{
   struct ilo_builder_writer *writer = &builder->writers[which];

   switch (which) {
   case ILO_BUILDER_WRITER_BATCH:
      writer->size = sizeof(uint32_t) * 8192;
      break;
   case ILO_BUILDER_WRITER_INSTRUCTION:
      /*
       * The EUs pretch some instructions.  But since the kernel invalidates
       * the instruction cache between batch buffers, we can set
       * WRITER_FLAG_APPEND without worrying the EUs would see invalid
       * instructions prefetched.
       */
      writer->flags = WRITER_FLAG_APPEND | WRITER_FLAG_GROW;
      writer->size = 8192;
      break;
   default:
      assert(!"unknown builder writer");
      return;
      break;
   }

   if (builder->dev->has_llc)
      writer->flags |= WRITER_FLAG_MAP;
}

/**
 * Free all resources used by a writer.  Note that the initial size is not
 * reset.
 */
static void
ilo_builder_writer_reset(struct ilo_builder *builder,
                         enum ilo_builder_writer_type which)
{
   struct ilo_builder_writer *writer = &builder->writers[which];

   if (writer->ptr) {
      if (writer->flags & WRITER_FLAG_MAP)
         intel_bo_unmap(writer->bo);
      else
         FREE(writer->ptr);

      writer->ptr = NULL;
   }

   intel_bo_unref(writer->bo);
   writer->bo = NULL;

   writer->used = 0;
   writer->stolen = 0;

   if (writer->items) {
      FREE(writer->items);
      writer->item_alloc = 0;
      writer->item_used = 0;
   }
}

/**
 * Discard everything written so far.
 */
void
ilo_builder_writer_discard(struct ilo_builder *builder,
                           enum ilo_builder_writer_type which)
{
   struct ilo_builder_writer *writer = &builder->writers[which];

   intel_bo_truncate_relocs(writer->bo, 0);
   writer->used = 0;
   writer->stolen = 0;
   writer->item_used = 0;
}

static struct intel_bo *
alloc_writer_bo(struct intel_winsys *winsys,
                enum ilo_builder_writer_type which,
                unsigned size)
{
   static const char *writer_names[ILO_BUILDER_WRITER_COUNT] = {
      [ILO_BUILDER_WRITER_BATCH] = "batch",
      [ILO_BUILDER_WRITER_INSTRUCTION] = "instruction",
   };

   return intel_winsys_alloc_bo(winsys, writer_names[which], size, true);
}

static void *
map_writer_bo(struct intel_bo *bo, unsigned flags)
{
   assert(flags & WRITER_FLAG_MAP);

   if (flags & WRITER_FLAG_APPEND)
      return intel_bo_map_gtt_async(bo);
   else
      return intel_bo_map(bo, true);
}

/**
 * Allocate and map the buffer for writing.
 */
static bool
ilo_builder_writer_alloc_and_map(struct ilo_builder *builder,
                                 enum ilo_builder_writer_type which)
{
   struct ilo_builder_writer *writer = &builder->writers[which];

   /* allocate a new bo when not appending */
   if (!(writer->flags & WRITER_FLAG_APPEND) || !writer->bo) {
      struct intel_bo *bo;

      bo = alloc_writer_bo(builder->winsys, which, writer->size);
      if (bo) {
         intel_bo_unref(writer->bo);
         writer->bo = bo;
      } else if (writer->bo) {
         /* reuse the old bo */
         ilo_builder_writer_discard(builder, which);
      } else {
         return false;
      }

      writer->used = 0;
      writer->stolen = 0;
      writer->item_used = 0;
   }

   /* map the bo or allocate the staging system memory */
   if (writer->flags & WRITER_FLAG_MAP)
      writer->ptr = map_writer_bo(writer->bo, writer->flags);
   else if (!writer->ptr)
      writer->ptr = MALLOC(writer->size);

   return (writer->ptr != NULL);
}

/**
 * Unmap the buffer for submission.
 */
static bool
ilo_builder_writer_unmap(struct ilo_builder *builder,
                         enum ilo_builder_writer_type which)
{
   struct ilo_builder_writer *writer = &builder->writers[which];
   unsigned offset;
   int err = 0;

   if (writer->flags & WRITER_FLAG_MAP) {
      intel_bo_unmap(writer->bo);
      writer->ptr = NULL;
      return true;
   }

   offset = builder->begin_used[which];
   if (writer->used > offset) {
      err = intel_bo_pwrite(writer->bo, offset, writer->used - offset,
            (char *) writer->ptr + offset);
   }

   if (writer->stolen && !err) {
      const unsigned offset = writer->size - writer->stolen;
      err = intel_bo_pwrite(writer->bo, offset, writer->stolen,
            (const char *) writer->ptr + offset);
   }

   /* keep writer->ptr */

   return !err;
}

/**
 * Grow a mapped writer to at least \p new_size.
 */
bool
ilo_builder_writer_grow(struct ilo_builder *builder,
                        enum ilo_builder_writer_type which,
                        unsigned new_size, bool preserve)
{
   struct ilo_builder_writer *writer = &builder->writers[which];
   struct intel_bo *new_bo;
   void *new_ptr;

   if (!(writer->flags & WRITER_FLAG_GROW))
      return false;

   /* stolen data may already be referenced and cannot be moved */
   if (writer->stolen)
      return false;

   if (new_size < writer->size << 1)
      new_size = writer->size << 1;
   /* STATE_BASE_ADDRESS requires page-aligned buffers */
   new_size = align(new_size, 4096);

   new_bo = alloc_writer_bo(builder->winsys, which, new_size);
   if (!new_bo)
      return false;

   /* map and copy the data over */
   if (writer->flags & WRITER_FLAG_MAP) {
      new_ptr = map_writer_bo(new_bo, writer->flags);

      /*
       * When WRITER_FLAG_APPEND and WRITER_FLAG_GROW are both set, we may end
       * up copying between two GTT-mapped BOs.  That is slow.  The issue
       * could be solved by adding intel_bo_map_async(), or callers may choose
       * to manually grow the writer without preserving the data.
       */
      if (new_ptr && preserve)
         memcpy(new_ptr, writer->ptr, writer->used);
   } else if (preserve) {
      new_ptr = REALLOC(writer->ptr, writer->size, new_size);
   } else {
      new_ptr = MALLOC(new_size);
   }

   if (!new_ptr) {
      intel_bo_unref(new_bo);
      return false;
   }

   if (writer->flags & WRITER_FLAG_MAP)
      intel_bo_unmap(writer->bo);
   else if (!preserve)
      FREE(writer->ptr);

   intel_bo_unref(writer->bo);

   writer->size = new_size;
   writer->bo = new_bo;
   writer->ptr = new_ptr;

   return true;
}

/**
 * Record an item for later decoding.
 */
bool
ilo_builder_writer_record(struct ilo_builder *builder,
                          enum ilo_builder_writer_type which,
                          enum ilo_builder_item_type type,
                          unsigned offset, unsigned size)
{
   struct ilo_builder_writer *writer = &builder->writers[which];
   struct ilo_builder_item *item;

   if (writer->item_used == writer->item_alloc) {
      const unsigned new_alloc = (writer->item_alloc) ?
         writer->item_alloc << 1 : 256;
      struct ilo_builder_item *items;

      items = REALLOC(writer->items,
            sizeof(writer->items[0]) * writer->item_alloc,
            sizeof(writer->items[0]) * new_alloc);
      if (!items)
         return false;

      writer->items = items;
      writer->item_alloc = new_alloc;
   }

   item = &writer->items[writer->item_used++];
   item->type = type;
   item->offset = offset;
   item->size = size;

   return true;
}

/**
 * Initialize the builder.
 */
void
ilo_builder_init(struct ilo_builder *builder,
                 const struct ilo_dev_info *dev,
                 struct intel_winsys *winsys)
{
   int i;

   memset(builder, 0, sizeof(*builder));

   builder->dev = dev;
   builder->winsys = winsys;

   /* gen6_SURFACE_STATE() may override this */
   switch (ilo_dev_gen(dev)) {
   case ILO_GEN(8):
      builder->mocs = GEN8_MOCS_MT_WB | GEN8_MOCS_CT_L3;
      break;
   case ILO_GEN(7.5):
   case ILO_GEN(7):
      builder->mocs = GEN7_MOCS_L3_WB;
      break;
   default:
      builder->mocs = 0;
      break;
   }

   for (i = 0; i < ILO_BUILDER_WRITER_COUNT; i++)
      ilo_builder_writer_init(builder, i);
}

/**
 * Reset the builder and free all resources used.  After resetting, the
 * builder behaves as if it is newly initialized, except for potentially
 * larger initial bo sizes.
 */
void
ilo_builder_reset(struct ilo_builder *builder)
{
   int i;

   for (i = 0; i < ILO_BUILDER_WRITER_COUNT; i++)
      ilo_builder_writer_reset(builder, i);
}

/**
 * Allocate and map the BOs.  It may re-allocate or reuse existing BOs if
 * there is any.
 *
 * Most builder functions can only be called after ilo_builder_begin() and
 * before ilo_builder_end().
 */
bool
ilo_builder_begin(struct ilo_builder *builder)
{
   int i;

   for (i = 0; i < ILO_BUILDER_WRITER_COUNT; i++) {
      if (!ilo_builder_writer_alloc_and_map(builder, i)) {
         ilo_builder_reset(builder);
         return false;
      }

      builder->begin_used[i] = builder->writers[i].used;
   }

   builder->unrecoverable_error = false;
   builder->sba_instruction_pos = 0;

   return true;
}

/**
 * Unmap BOs and make sure the written data landed the BOs.  The batch buffer
 * ready for submission is returned.
 */
struct intel_bo *
ilo_builder_end(struct ilo_builder *builder, unsigned *used)
{
   struct ilo_builder_writer *bat;
   int i;

   ilo_builder_batch_patch_sba(builder);

   assert(ilo_builder_validate(builder, 0, NULL));

   for (i = 0; i < ILO_BUILDER_WRITER_COUNT; i++) {
      if (!ilo_builder_writer_unmap(builder, i))
         builder->unrecoverable_error = true;
   }

   if (builder->unrecoverable_error)
      return NULL;

   bat = &builder->writers[ILO_BUILDER_WRITER_BATCH];

   *used = bat->used;

   return bat->bo;
}

/**
 * Return true if the builder is in a valid state, after accounting for the
 * additional BOs specified.  The additional BOs can be listed to avoid
 * snapshotting and restoring when they are known ahead of time.
 *
 * The number of additional BOs should not be more than a few.  Like two, for
 * copying between two BOs.
 *
 * Callers must make sure the builder is in a valid state when
 * ilo_builder_end() is called.
 */
bool
ilo_builder_validate(struct ilo_builder *builder,
                     unsigned bo_count, struct intel_bo **bos)
{
   const unsigned max_bo_count = 2;
   struct intel_bo *bos_to_submit[ILO_BUILDER_WRITER_COUNT + max_bo_count];
   int i;

   for (i = 0; i < ILO_BUILDER_WRITER_COUNT; i++)
      bos_to_submit[i] = builder->writers[i].bo;

   if (bo_count) {
      assert(bo_count <= max_bo_count);
      if (bo_count > max_bo_count)
         return false;

      memcpy(&bos_to_submit[ILO_BUILDER_WRITER_COUNT],
            bos, sizeof(*bos) * bo_count);
      i += bo_count;
   }

   return intel_winsys_can_submit_bo(builder->winsys, bos_to_submit, i);
}

/**
 * Take a snapshot of the writer state.
 */
void
ilo_builder_batch_snapshot(const struct ilo_builder *builder,
                           struct ilo_builder_snapshot *snapshot)
{
   const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
   const struct ilo_builder_writer *writer = &builder->writers[which];

   snapshot->reloc_count = intel_bo_get_reloc_count(writer->bo);
   snapshot->used = writer->used;
   snapshot->stolen = writer->stolen;
   snapshot->item_used = writer->item_used;
}

/**
 * Restore the writer state to when the snapshot was taken, except that it
 * does not (unnecessarily) shrink BOs or the item array.
 */
void
ilo_builder_batch_restore(struct ilo_builder *builder,
                          const struct ilo_builder_snapshot *snapshot)
{
   const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
   struct ilo_builder_writer *writer = &builder->writers[which];

   intel_bo_truncate_relocs(writer->bo, snapshot->reloc_count);
   writer->used = snapshot->used;
   writer->stolen = snapshot->stolen;
   writer->item_used = snapshot->item_used;
}