summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/ilo/core/ilo_state_urb.c
blob: cbd150c71c929eb7c89857aa5d507fec8e155869 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2012-2015 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_debug.h"
#include "ilo_state_urb.h"

struct urb_configuration {
   uint8_t vs_pcb_alloc_kb;
   uint8_t hs_pcb_alloc_kb;
   uint8_t ds_pcb_alloc_kb;
   uint8_t gs_pcb_alloc_kb;
   uint8_t ps_pcb_alloc_kb;

   uint8_t urb_offset_8kb;

   uint8_t vs_urb_alloc_8kb;
   uint8_t hs_urb_alloc_8kb;
   uint8_t ds_urb_alloc_8kb;
   uint8_t gs_urb_alloc_8kb;

   uint8_t vs_entry_rows;
   uint8_t hs_entry_rows;
   uint8_t ds_entry_rows;
   uint8_t gs_entry_rows;

   int vs_entry_count;
   int hs_entry_count;
   int ds_entry_count;
   int gs_entry_count;
};

static void
urb_alloc_gen7_pcb(const struct ilo_dev *dev,
                   const struct ilo_state_urb_info *info,
                   struct urb_configuration *conf)
{
   /*
    * From the Haswell PRM, volume 2b, page 940:
    *
    *     "[0,16] (0KB - 16KB) Increments of 1KB DevHSW:GT1, DevHSW:GT2
    *      [0,32] (0KB - 32KB) Increments of 2KB DevHSW:GT3"
    */
   const uint8_t increment_kb =
      (ilo_dev_gen(dev) >= ILO_GEN(8) ||
       (ilo_dev_gen(dev) == ILO_GEN(7.5) && dev->gt == 3)) ? 2 : 1;

   ILO_DEV_ASSERT(dev, 7, 8);

   /*
    * Keep the strategy simple as we do not know the workloads and how
    * expensive it is to change the configuration frequently.
    */
   if (info->hs_const_data || info->ds_const_data) {
      conf->vs_pcb_alloc_kb = increment_kb * 4;
      conf->hs_pcb_alloc_kb = increment_kb * 3;
      conf->ds_pcb_alloc_kb = increment_kb * 3;
      conf->gs_pcb_alloc_kb = increment_kb * 3;
      conf->ps_pcb_alloc_kb = increment_kb * 3;
   } else if (info->gs_const_data) {
      conf->vs_pcb_alloc_kb = increment_kb * 6;
      conf->gs_pcb_alloc_kb = increment_kb * 5;
      conf->ps_pcb_alloc_kb = increment_kb * 5;
   } else {
      conf->vs_pcb_alloc_kb = increment_kb * 8;
      conf->ps_pcb_alloc_kb = increment_kb * 8;
   }

   conf->urb_offset_8kb = increment_kb * 16 / 8;
}

static void
urb_alloc_gen6_urb(const struct ilo_dev *dev,
                   const struct ilo_state_urb_info *info,
                   struct urb_configuration *conf)
{
   /*
    * From the Ivy Bridge PRM, volume 2 part 1, page 34:
    *
    *     "(VS URB Starting Address) Offset from the start of the URB memory
    *      where VS starts its allocation, specified in multiples of 8 KB."
    *
    * Same for other stages.
    */
   const int space_avail_8kb = dev->urb_size / 8192 - conf->urb_offset_8kb;

   /*
    * From the Sandy Bridge PRM, volume 2 part 1, page 173:
    *
    *     "Programming Note: If the GS stage is enabled, software must always
    *      allocate at least one GS URB Entry. This is true even if the GS
    *      thread never needs to output vertices to the urb, e.g., when only
    *      performing stream output. This is an artifact of the need to pass
    *      the GS thread an initial destination URB handle."
    */
   const bool force_gs_alloc =
      (ilo_dev_gen(dev) == ILO_GEN(6) && info->gs_enable);

   ILO_DEV_ASSERT(dev, 6, 8);

   if (info->hs_entry_size || info->ds_entry_size) {
      conf->vs_urb_alloc_8kb = space_avail_8kb / 4;
      conf->hs_urb_alloc_8kb = space_avail_8kb / 4;
      conf->ds_urb_alloc_8kb = space_avail_8kb / 4;
      conf->gs_urb_alloc_8kb = space_avail_8kb / 4;

      if (space_avail_8kb % 4) {
         assert(space_avail_8kb % 2 == 0);
         conf->vs_urb_alloc_8kb++;
         conf->gs_urb_alloc_8kb++;
      }
   } else if (info->gs_entry_size || force_gs_alloc) {
      assert(space_avail_8kb % 2 == 0);
      conf->vs_urb_alloc_8kb = space_avail_8kb / 2;
      conf->gs_urb_alloc_8kb = space_avail_8kb / 2;
   } else {
      conf->vs_urb_alloc_8kb = space_avail_8kb;
   }
}

static bool
urb_init_gen6_vs_entry(const struct ilo_dev *dev,
                       const struct ilo_state_urb_info *info,
                       struct urb_configuration *conf)
{
   /*
    * From the Sandy Bridge PRM, volume 2 part 1, page 28:
    *
    *     "(VS URB Entry Allocation Size)
    *      Range [0,4] = [1,5] 1024-bit URB rows"
    *
    *     "(VS Number of URB Entries)
    *      Range [24,256] in multiples of 4
    *            [24, 128] in multiples of 4[DevSNBGT1]"
    */
   const int max_entry_count = (dev->gt == 2) ? 256 : 252;
   const int row_size = 1024 / 8;
   int row_count, entry_count;
   int entry_size;

   ILO_DEV_ASSERT(dev, 6, 6);

   /* VE and VS share the same VUE for each vertex */
   entry_size = info->vs_entry_size;
   if (entry_size < info->ve_entry_size)
      entry_size = info->ve_entry_size;

   row_count = (entry_size + row_size - 1) / row_size;
   if (row_count > 5)
      return false;
   else if (!row_count)
      row_count++;

   entry_count = conf->vs_urb_alloc_8kb * 8192 / (row_size * row_count);
   if (entry_count > max_entry_count)
      entry_count = max_entry_count;
   entry_count &= ~3;
   assert(entry_count >= 24);

   conf->vs_entry_rows = row_count;
   conf->vs_entry_count = entry_count;

   return true;
}

static bool
urb_init_gen6_gs_entry(const struct ilo_dev *dev,
                       const struct ilo_state_urb_info *info,
                       struct urb_configuration *conf)
{
   /*
    * From the Sandy Bridge PRM, volume 2 part 1, page 29:
    *
    *     "(GS Number of URB Entries)
    *      Range [0,256] in multiples of 4
    *            [0, 254] in multiples of 4[DevSNBGT1]"
    *
    *     "(GS URB Entry Allocation Size)
    *      Range [0,4] = [1,5] 1024-bit URB rows"
    */
   const int max_entry_count = (dev->gt == 2) ? 256 : 252;
   const int row_size = 1024 / 8;
   int row_count, entry_count;

   ILO_DEV_ASSERT(dev, 6, 6);

   row_count = (info->gs_entry_size + row_size - 1) / row_size;
   if (row_count > 5)
      return false;
   else if (!row_count)
      row_count++;

   entry_count = conf->gs_urb_alloc_8kb * 8192 / (row_size * row_count);
   if (entry_count > max_entry_count)
      entry_count = max_entry_count;
   entry_count &= ~3;

   conf->gs_entry_rows = row_count;
   conf->gs_entry_count = entry_count;

   return true;
}

static bool
urb_init_gen7_vs_entry(const struct ilo_dev *dev,
                       const struct ilo_state_urb_info *info,
                       struct urb_configuration *conf)
{
   /*
    * From the Ivy Bridge PRM, volume 2 part 1, page 34-35:
    *
    *     "VS URB Entry Allocation Size equal to 4(5 512-bit URB rows) may
    *      cause performance to decrease due to banking in the URB. Element
    *      sizes of 16 to 20 should be programmed with six 512-bit URB rows."
    *
    *     "(VS URB Entry Allocation Size)
    *      Format: U9-1 count of 512-bit units"
    *
    *     "(VS Number of URB Entries)
    *      [32,704]
    *      [32,512]
    *
    *      Programming Restriction: VS Number of URB Entries must be divisible
    *      by 8 if the VS URB Entry Allocation Size is less than 9 512-bit URB
    *      entries."2:0" = reserved "000b""
    *
    * From the Haswell PRM, volume 2b, page 847:
    *
    *     "(VS Number of URB Entries)
    *      [64,1664] DevHSW:GT3
    *      [64,1664] DevHSW:GT2
    *      [32,640]  DevHSW:GT1"
    */
   const int row_size = 512 / 8;
   int row_count, entry_count;
   int entry_size;
   int max_entry_count, min_entry_count;

   ILO_DEV_ASSERT(dev, 7, 8);

   /*
    * From the Ivy Bridge PRM, volume 2 part 1, page 35:
    *
    *     "Programming Restriction: As the VS URB entry serves as both the
    *      per-vertex input and output of the VS shader, the VS URB Allocation
    *      Size must be sized to the maximum of the vertex input and output
    *      structures."
    *
    * From the Ivy Bridge PRM, volume 2 part 1, page 42:
    *
    *     "If the VS function is enabled, the VF-written VUEs are not required
    *      to have Vertex Headers, as the VS-incoming vertices are guaranteed
    *      to be consumed by the VS (i.e., the VS thread is responsible for
    *      overwriting the input vertex data)."
    *
    * VE and VS share the same VUE for each vertex.
    */
   entry_size = info->vs_entry_size;
   if (entry_size < info->ve_entry_size)
      entry_size = info->ve_entry_size;

   row_count = (entry_size + row_size - 1) / row_size;
   if (row_count == 5 || !row_count)
      row_count++;

   entry_count = conf->vs_urb_alloc_8kb * 8192 / (row_size * row_count);
   if (row_count < 9)
      entry_count &= ~7;

   switch (ilo_dev_gen(dev)) {
   case ILO_GEN(8):
   case ILO_GEN(7.5):
      max_entry_count = (dev->gt >= 2) ? 1664 : 640;
      min_entry_count = (dev->gt >= 2) ? 64 : 32;
      break;
   case ILO_GEN(7):
      max_entry_count = (dev->gt == 2) ? 704 : 512;
      min_entry_count = 32;
      break;
   default:
      assert(!"unexpected gen");
      return false;
      break;
   }

   if (entry_count > max_entry_count)
      entry_count = max_entry_count;
   else if (entry_count < min_entry_count)
      return false;

   conf->vs_entry_rows = row_count;
   conf->vs_entry_count = entry_count;

   return true;
}

static bool
urb_init_gen7_hs_entry(const struct ilo_dev *dev,
                       const struct ilo_state_urb_info *info,
                       struct urb_configuration *conf)
{
   /*
    * From the Ivy Bridge PRM, volume 2 part 1, page 37:
    *
    *     "HS Number of URB Entries must be divisible by 8 if the HS URB Entry
    *      Allocation Size is less than 9 512-bit URB
    *      entries."2:0" = reserved "000"
    *
    *      [0,64]
    *      [0,32]"
    *
    * From the Haswell PRM, volume 2b, page 849:
    *
    *     "(HS Number of URB Entries)
    *      [0,128] DevHSW:GT2
    *      [0,64]  DevHSW:GT1"
    */
   const int row_size = 512 / 8;
   int row_count, entry_count;
   int max_entry_count;

   ILO_DEV_ASSERT(dev, 7, 8);

   row_count = (info->hs_entry_size + row_size - 1) / row_size;
   if (!row_count)
      row_count++;

   entry_count = conf->hs_urb_alloc_8kb * 8192 / (row_size * row_count);
   if (row_count < 9)
      entry_count &= ~7;

   switch (ilo_dev_gen(dev)) {
   case ILO_GEN(8):
   case ILO_GEN(7.5):
      max_entry_count = (dev->gt >= 2) ? 128 : 64;
      break;
   case ILO_GEN(7):
      max_entry_count = (dev->gt == 2) ? 64 : 32;
      break;
   default:
      assert(!"unexpected gen");
      return false;
      break;
   }

   if (entry_count > max_entry_count)
      entry_count = max_entry_count;
   else if (info->hs_entry_size && !entry_count)
      return false;

   conf->hs_entry_rows = row_count;
   conf->hs_entry_count = entry_count;

   return true;
}

static bool
urb_init_gen7_ds_entry(const struct ilo_dev *dev,
                       const struct ilo_state_urb_info *info,
                       struct urb_configuration *conf)
{
   /*
    * From the Ivy Bridge PRM, volume 2 part 1, page 38:
    *
    *     "(DS URB Entry Allocation Size)
    *      [0,9]"
    *
    *     "(DS Number of URB Entries) If Domain Shader Thread Dispatch is
    *      Enabled then the minimum number handles that must be allocated is
    *      138 URB entries.
    *      "2:0" = reserved "000"
    *
    *      [0,448]
    *      [0,288]
    *
    *      DS Number of URB Entries must be divisible by 8 if the DS URB Entry
    *      Allocation Size is less than 9 512-bit URB entries.If Domain Shader
    *      Thread Dispatch is Enabled then the minimum number of handles that
    *      must be allocated is 10 URB entries."
    *
    * From the Haswell PRM, volume 2b, page 851:
    *
    *     "(DS Number of URB Entries)
    *      [0,960] DevHSW:GT2
    *      [0,384] DevHSW:GT1"
    */
   const int row_size = 512 / 8;
   int row_count, entry_count;
   int max_entry_count;

   ILO_DEV_ASSERT(dev, 7, 8);

   row_count = (info->ds_entry_size + row_size - 1) / row_size;
   if (row_count > 10)
      return false;
   else if (!row_count)
      row_count++;

   entry_count = conf->ds_urb_alloc_8kb * 8192 / (row_size * row_count);
   if (row_count < 9)
      entry_count &= ~7;

   switch (ilo_dev_gen(dev)) {
   case ILO_GEN(8):
   case ILO_GEN(7.5):
      max_entry_count = (dev->gt >= 2) ? 960 : 384;
      break;
   case ILO_GEN(7):
      max_entry_count = (dev->gt == 2) ? 448 : 288;
      break;
   default:
      assert(!"unexpected gen");
      return false;
      break;
   }

   if (entry_count > max_entry_count)
      entry_count = max_entry_count;
   else if (info->ds_entry_size && entry_count < 10)
      return false;

   conf->ds_entry_rows = row_count;
   conf->ds_entry_count = entry_count;

   return true;
}

static bool
urb_init_gen7_gs_entry(const struct ilo_dev *dev,
                       const struct ilo_state_urb_info *info,
                       struct urb_configuration *conf)
{
   /*
    * From the Ivy Bridge PRM, volume 2 part 1, page 40:
    *
    *     "(GS Number of URB Entries) GS Number of URB Entries must be
    *      divisible by 8 if the GS URB Entry Allocation Size is less than 9
    *      512-bit URB entries.
    *      "2:0" = reserved "000"
    *
    *      [0,320]
    *      [0,192]"
    *
    * From the Ivy Bridge PRM, volume 2 part 1, page 171:
    *
    *     "(DUAL_INSTANCE and DUAL_OBJECT) The GS must be allocated at least
    *      two URB handles or behavior is UNDEFINED."
    *
    * From the Haswell PRM, volume 2b, page 853:
    *
    *     "(GS Number of URB Entries)
    *      [0,640] DevHSW:GT2
    *      [0,256] DevHSW:GT1
    *
    *      Only if GS is disabled can this field be programmed to 0.  If GS is
    *      enabled this field shall be programmed to a value greater than 0.
    *      For GS Dispatch Mode "Single", this field shall be programmed to a
    *      value greater than or equal to 1. For other GS Dispatch Modes,
    *      refer to the definition of Dispatch Mode (3DSTATE_GS) for minimum
    *      values of this field."
    */
   const int row_size = 512 / 8;
   int row_count, entry_count;
   int max_entry_count;

   ILO_DEV_ASSERT(dev, 7, 8);

   row_count = (info->gs_entry_size + row_size - 1) / row_size;
   if (!row_count)
      row_count++;

   entry_count = conf->gs_urb_alloc_8kb * 8192 / (row_size * row_count);
   if (row_count < 9)
      entry_count &= ~7;

   switch (ilo_dev_gen(dev)) {
   case ILO_GEN(8):
   case ILO_GEN(7.5):
      max_entry_count = (dev->gt >= 2) ? 640 : 256;
      break;
   case ILO_GEN(7):
      max_entry_count = (dev->gt == 2) ? 320 : 192;
      break;
   default:
      assert(!"unexpected gen");
      return false;
      break;
   }

   if (entry_count > max_entry_count)
      entry_count = max_entry_count;
   else if (info->gs_entry_size && entry_count < 2)
      return false;

   conf->gs_entry_rows = row_count;
   conf->gs_entry_count = entry_count;

   return true;
}

static bool
urb_get_gen6_configuration(const struct ilo_dev *dev,
                           const struct ilo_state_urb_info *info,
                           struct urb_configuration *conf)
{
   ILO_DEV_ASSERT(dev, 6, 8);

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

   if (ilo_dev_gen(dev) >= ILO_GEN(7))
      urb_alloc_gen7_pcb(dev, info, conf);

   urb_alloc_gen6_urb(dev, info, conf);

   if (ilo_dev_gen(dev) >= ILO_GEN(7)) {
      if (!urb_init_gen7_vs_entry(dev, info, conf) ||
          !urb_init_gen7_hs_entry(dev, info, conf) ||
          !urb_init_gen7_ds_entry(dev, info, conf) ||
          !urb_init_gen7_gs_entry(dev, info, conf))
         return false;
   } else {
      if (!urb_init_gen6_vs_entry(dev, info, conf) ||
          !urb_init_gen6_gs_entry(dev, info, conf))
         return false;
   }

   return true;
}

static bool
urb_set_gen7_3dstate_push_constant_alloc(struct ilo_state_urb *urb,
                                         const struct ilo_dev *dev,
                                         const struct ilo_state_urb_info *info,
                                         const struct urb_configuration *conf)
{
   uint32_t dw1[5];
   uint8_t sizes_kb[5], offset_kb;
   int i;

   ILO_DEV_ASSERT(dev, 7, 8);

   sizes_kb[0] = conf->vs_pcb_alloc_kb;
   sizes_kb[1] = conf->hs_pcb_alloc_kb;
   sizes_kb[2] = conf->ds_pcb_alloc_kb;
   sizes_kb[3] = conf->gs_pcb_alloc_kb;
   sizes_kb[4] = conf->ps_pcb_alloc_kb;
   offset_kb = 0;

   for (i = 0; i < 5; i++) {
      /* careful for the valid range of offsets */
      if (sizes_kb[i]) {
         dw1[i] = offset_kb << GEN7_PCB_ALLOC_DW1_OFFSET__SHIFT |
                  sizes_kb[i] << GEN7_PCB_ALLOC_DW1_SIZE__SHIFT;
         offset_kb += sizes_kb[i];
      } else {
         dw1[i] = 0;
      }
   }

   STATIC_ASSERT(ARRAY_SIZE(urb->pcb) >= 5);
   memcpy(urb->pcb, dw1, sizeof(dw1));

   return true;
}

static bool
urb_set_gen6_3DSTATE_URB(struct ilo_state_urb *urb,
                         const struct ilo_dev *dev,
                         const struct ilo_state_urb_info *info,
                         const struct urb_configuration *conf)
{
   uint32_t dw1, dw2;

   ILO_DEV_ASSERT(dev, 6, 6);

   assert(conf->vs_entry_rows && conf->gs_entry_rows);

   dw1 = (conf->vs_entry_rows - 1) << GEN6_URB_DW1_VS_ENTRY_SIZE__SHIFT |
         conf->vs_entry_count << GEN6_URB_DW1_VS_ENTRY_COUNT__SHIFT;
   dw2 = conf->gs_entry_count << GEN6_URB_DW2_GS_ENTRY_COUNT__SHIFT |
         (conf->gs_entry_rows - 1) << GEN6_URB_DW2_GS_ENTRY_SIZE__SHIFT;

   STATIC_ASSERT(ARRAY_SIZE(urb->urb) >= 2);
   urb->urb[0] = dw1;
   urb->urb[1] = dw2;

   return true;
}

static bool
urb_set_gen7_3dstate_urb(struct ilo_state_urb *urb,
                         const struct ilo_dev *dev,
                         const struct ilo_state_urb_info *info,
                         const struct urb_configuration *conf)
{
   uint32_t dw1[4];
   struct {
      uint8_t alloc_8kb;
      uint8_t entry_rows;
      int entry_count;
   } stages[4];
   uint8_t offset_8kb;
   int i;

   ILO_DEV_ASSERT(dev, 7, 8);

   stages[0].alloc_8kb = conf->vs_urb_alloc_8kb;
   stages[1].alloc_8kb = conf->hs_urb_alloc_8kb;
   stages[2].alloc_8kb = conf->ds_urb_alloc_8kb;
   stages[3].alloc_8kb = conf->gs_urb_alloc_8kb;

   stages[0].entry_rows = conf->vs_entry_rows;
   stages[1].entry_rows = conf->hs_entry_rows;
   stages[2].entry_rows = conf->ds_entry_rows;
   stages[3].entry_rows = conf->gs_entry_rows;

   stages[0].entry_count = conf->vs_entry_count;
   stages[1].entry_count = conf->hs_entry_count;
   stages[2].entry_count = conf->ds_entry_count;
   stages[3].entry_count = conf->gs_entry_count;

   offset_8kb = conf->urb_offset_8kb;

   for (i = 0; i < 4; i++) {
      /* careful for the valid range of offsets */
      if (stages[i].alloc_8kb) {
         assert(stages[i].entry_rows);
         dw1[i] =
            offset_8kb << GEN7_URB_DW1_OFFSET__SHIFT |
            (stages[i].entry_rows - 1) << GEN7_URB_DW1_ENTRY_SIZE__SHIFT |
            stages[i].entry_count << GEN7_URB_DW1_ENTRY_COUNT__SHIFT;
         offset_8kb += stages[i].alloc_8kb;
      } else {
         dw1[i] = 0;
      }
   }

   STATIC_ASSERT(ARRAY_SIZE(urb->urb) >= 4);
   memcpy(urb->urb, dw1, sizeof(dw1));

   return true;
}

bool
ilo_state_urb_init(struct ilo_state_urb *urb,
                   const struct ilo_dev *dev,
                   const struct ilo_state_urb_info *info)
{
   assert(ilo_is_zeroed(urb, sizeof(*urb)));
   return ilo_state_urb_set_info(urb, dev, info);
}

bool
ilo_state_urb_init_for_rectlist(struct ilo_state_urb *urb,
                                const struct ilo_dev *dev,
                                uint8_t vf_attr_count)
{
   struct ilo_state_urb_info info;

   memset(&info, 0, sizeof(info));
   info.ve_entry_size = sizeof(uint32_t) * 4 * vf_attr_count;

   return ilo_state_urb_init(urb, dev, &info);
}

bool
ilo_state_urb_set_info(struct ilo_state_urb *urb,
                       const struct ilo_dev *dev,
                       const struct ilo_state_urb_info *info)
{
   struct urb_configuration conf;
   bool ret = true;

   ret &= urb_get_gen6_configuration(dev, info, &conf);
   if (ilo_dev_gen(dev) >= ILO_GEN(7)) {
      ret &= urb_set_gen7_3dstate_push_constant_alloc(urb, dev, info, &conf);
      ret &= urb_set_gen7_3dstate_urb(urb, dev, info, &conf);
   } else {
      ret &= urb_set_gen6_3DSTATE_URB(urb, dev, info, &conf);
   }

   assert(ret);

   return ret;
}

void
ilo_state_urb_full_delta(const struct ilo_state_urb *urb,
                         const struct ilo_dev *dev,
                         struct ilo_state_urb_delta *delta)
{
   if (ilo_dev_gen(dev) >= ILO_GEN(7)) {
      delta->dirty = ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_VS |
                     ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_HS |
                     ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_DS |
                     ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_GS |
                     ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_PS |
                     ILO_STATE_URB_3DSTATE_URB_VS |
                     ILO_STATE_URB_3DSTATE_URB_HS |
                     ILO_STATE_URB_3DSTATE_URB_DS |
                     ILO_STATE_URB_3DSTATE_URB_GS;
   } else {
      delta->dirty = ILO_STATE_URB_3DSTATE_URB_VS |
                     ILO_STATE_URB_3DSTATE_URB_GS;
   }
}

void
ilo_state_urb_get_delta(const struct ilo_state_urb *urb,
                        const struct ilo_dev *dev,
                        const struct ilo_state_urb *old,
                        struct ilo_state_urb_delta *delta)
{
   delta->dirty = 0;

   if (ilo_dev_gen(dev) >= ILO_GEN(7)) {
      if (memcmp(urb->pcb, old->pcb, sizeof(urb->pcb))) {
         delta->dirty |= ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_VS |
                         ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_HS |
                         ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_DS |
                         ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_GS |
                         ILO_STATE_URB_3DSTATE_PUSH_CONSTANT_ALLOC_PS;
      }

      /*
       * From the Ivy Bridge PRM, volume 2 part 1, page 34:
       *
       *     "3DSTATE_URB_HS, 3DSTATE_URB_DS, and 3DSTATE_URB_GS must also be
       *      programmed in order for the programming of this state
       *      (3DSTATE_URB_VS) to be valid."
       *
       * The same is true for the other three states.
       */
      if (memcmp(urb->urb, old->urb, sizeof(urb->urb))) {
         delta->dirty |= ILO_STATE_URB_3DSTATE_URB_VS |
                         ILO_STATE_URB_3DSTATE_URB_HS |
                         ILO_STATE_URB_3DSTATE_URB_DS |
                         ILO_STATE_URB_3DSTATE_URB_GS;
      }
   } else {
      if (memcmp(urb->urb, old->urb, sizeof(uint32_t) * 2)) {
         delta->dirty |= ILO_STATE_URB_3DSTATE_URB_VS |
                         ILO_STATE_URB_3DSTATE_URB_GS;
      }
   }
}