OSDN Git Service

anv: Rework pipeline caching
[android-x86/external-mesa.git] / src / intel / vulkan / genX_l3.c
1 /*
2  * Copyright (c) 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include "anv_private.h"
25
26 #include "genxml/gen_macros.h"
27 #include "genxml/genX_pack.h"
28
29 /**
30  * Chunk of L3 cache reserved for some specific purpose.
31  */
32 enum anv_l3_partition {
33    /** Shared local memory. */
34    L3P_SLM = 0,
35    /** Unified return buffer. */
36    L3P_URB,
37    /** Union of DC and RO. */
38    L3P_ALL,
39    /** Data cluster RW partition. */
40    L3P_DC,
41    /** Union of IS, C and T. */
42    L3P_RO,
43    /** Instruction and state cache. */
44    L3P_IS,
45    /** Constant cache. */
46    L3P_C,
47    /** Texture cache. */
48    L3P_T,
49    /** Number of supported L3 partitions. */
50    NUM_L3P
51 };
52
53 /**
54  * L3 configuration represented as the number of ways allocated for each
55  * partition.  \sa get_l3_way_size().
56  */
57 struct anv_l3_config {
58    unsigned n[NUM_L3P];
59 };
60
61 #if GEN_GEN == 7
62
63 /**
64  * IVB/HSW validated L3 configurations.  The first entry will be used as
65  * default by gen7_restore_default_l3_config(), otherwise the ordering is
66  * unimportant.
67  */
68 static const struct anv_l3_config ivb_l3_configs[] = {
69    /* SLM URB ALL DC  RO  IS   C   T */
70    {{  0, 32,  0,  0, 32,  0,  0,  0 }},
71    {{  0, 32,  0, 16, 16,  0,  0,  0 }},
72    {{  0, 32,  0,  4,  0,  8,  4, 16 }},
73    {{  0, 28,  0,  8,  0,  8,  4, 16 }},
74    {{  0, 28,  0, 16,  0,  8,  4,  8 }},
75    {{  0, 28,  0,  8,  0, 16,  4,  8 }},
76    {{  0, 28,  0,  0,  0, 16,  4, 16 }},
77    {{  0, 32,  0,  0,  0, 16,  0, 16 }},
78    {{  0, 28,  0,  4, 32,  0,  0,  0 }},
79    {{ 16, 16,  0, 16, 16,  0,  0,  0 }},
80    {{ 16, 16,  0,  8,  0,  8,  8,  8 }},
81    {{ 16, 16,  0,  4,  0,  8,  4, 16 }},
82    {{ 16, 16,  0,  4,  0, 16,  4,  8 }},
83    {{ 16, 16,  0,  0, 32,  0,  0,  0 }},
84    {{ 0 }}
85 };
86
87 #endif
88
89 #if GEN_GEN == 7 && !GEN_IS_HASWELL
90
91 /**
92  * VLV validated L3 configurations.  \sa ivb_l3_configs.
93  */
94 static const struct anv_l3_config vlv_l3_configs[] = {
95    /* SLM URB ALL DC  RO  IS   C   T */
96    {{  0, 64,  0,  0, 32,  0,  0,  0 }},
97    {{  0, 80,  0,  0, 16,  0,  0,  0 }},
98    {{  0, 80,  0,  8,  8,  0,  0,  0 }},
99    {{  0, 64,  0, 16, 16,  0,  0,  0 }},
100    {{  0, 60,  0,  4, 32,  0,  0,  0 }},
101    {{ 32, 32,  0, 16, 16,  0,  0,  0 }},
102    {{ 32, 40,  0,  8, 16,  0,  0,  0 }},
103    {{ 32, 40,  0, 16,  8,  0,  0,  0 }},
104    {{ 0 }}
105 };
106
107 #endif
108
109 #if GEN_GEN == 8
110
111 /**
112  * BDW validated L3 configurations.  \sa ivb_l3_configs.
113  */
114 static const struct anv_l3_config bdw_l3_configs[] = {
115    /* SLM URB ALL DC  RO  IS   C   T */
116    {{  0, 48, 48,  0,  0,  0,  0,  0 }},
117    {{  0, 48,  0, 16, 32,  0,  0,  0 }},
118    {{  0, 32,  0, 16, 48,  0,  0,  0 }},
119    {{  0, 32,  0,  0, 64,  0,  0,  0 }},
120    {{  0, 32, 64,  0,  0,  0,  0,  0 }},
121    {{ 24, 16, 48,  0,  0,  0,  0,  0 }},
122    {{ 24, 16,  0, 16, 32,  0,  0,  0 }},
123    {{ 24, 16,  0, 32, 16,  0,  0,  0 }},
124    {{ 0 }}
125 };
126
127 #endif
128
129 #if GEN_GEN == 8 || GEN_GEN == 9
130
131 /**
132  * CHV/SKL validated L3 configurations.  \sa ivb_l3_configs.
133  */
134 static const struct anv_l3_config chv_l3_configs[] = {
135    /* SLM URB ALL DC  RO  IS   C   T */
136    {{  0, 48, 48,  0,  0,  0,  0,  0 }},
137    {{  0, 48,  0, 16, 32,  0,  0,  0 }},
138    {{  0, 32,  0, 16, 48,  0,  0,  0 }},
139    {{  0, 32,  0,  0, 64,  0,  0,  0 }},
140    {{  0, 32, 64,  0,  0,  0,  0,  0 }},
141    {{ 32, 16, 48,  0,  0,  0,  0,  0 }},
142    {{ 32, 16,  0, 16, 32,  0,  0,  0 }},
143    {{ 32, 16,  0, 32, 16,  0,  0,  0 }},
144    {{ 0 }}
145 };
146
147 #endif
148
149 /**
150  * Return a zero-terminated array of validated L3 configurations for the
151  * specified device.
152  */
153 static inline const struct anv_l3_config *
154 get_l3_configs(const struct brw_device_info *devinfo)
155 {
156    assert(devinfo->gen == GEN_GEN);
157 #if GEN_IS_HASWELL
158    return ivb_l3_configs;
159 #elif GEN_GEN == 7
160    return (devinfo->is_baytrail ? vlv_l3_configs : ivb_l3_configs);
161 #elif GEN_GEN == 8
162    return (devinfo->is_cherryview ? chv_l3_configs : bdw_l3_configs);
163 #elif GEN_GEN == 9
164    return chv_l3_configs;
165 #else
166 #error GEN not supported
167 #endif
168 }
169
170 /**
171  * Return the size of an L3 way in KB.
172  */
173 static unsigned
174 get_l3_way_size(const struct brw_device_info *devinfo)
175 {
176    if (devinfo->is_baytrail)
177       return 2;
178
179    else if (devinfo->is_cherryview || devinfo->gt == 1)
180       return 4;
181
182    else
183       return 8 * devinfo->num_slices;
184 }
185
186 /**
187  * L3 configuration represented as a vector of weights giving the desired
188  * relative size of each partition.  The scale is arbitrary, only the ratios
189  * between weights will have an influence on the selection of the closest L3
190  * configuration.
191  */
192 struct anv_l3_weights {
193    float w[NUM_L3P];
194 };
195
196 /**
197  * L1-normalize a vector of L3 partition weights.
198  */
199 static struct anv_l3_weights
200 norm_l3_weights(struct anv_l3_weights w)
201 {
202    float sz = 0;
203
204    for (unsigned i = 0; i < NUM_L3P; i++)
205       sz += w.w[i];
206
207    for (unsigned i = 0; i < NUM_L3P; i++)
208       w.w[i] /= sz;
209
210    return w;
211 }
212
213 /**
214  * Get the relative partition weights of the specified L3 configuration.
215  */
216 static struct anv_l3_weights
217 get_config_l3_weights(const struct anv_l3_config *cfg)
218 {
219    if (cfg) {
220       struct anv_l3_weights w;
221
222       for (unsigned i = 0; i < NUM_L3P; i++)
223          w.w[i] = cfg->n[i];
224
225       return norm_l3_weights(w);
226    } else {
227       const struct anv_l3_weights w = { { 0 } };
228       return w;
229    }
230 }
231
232 /**
233  * Distance between two L3 configurations represented as vectors of weights.
234  * Usually just the L1 metric except when the two configurations are
235  * considered incompatible in which case the distance will be infinite.  Note
236  * that the compatibility condition is asymmetric -- They will be considered
237  * incompatible whenever the reference configuration \p w0 requires SLM, DC,
238  * or URB but \p w1 doesn't provide it.
239  */
240 static float
241 diff_l3_weights(struct anv_l3_weights w0, struct anv_l3_weights w1)
242 {
243    if ((w0.w[L3P_SLM] && !w1.w[L3P_SLM]) ||
244        (w0.w[L3P_DC] && !w1.w[L3P_DC] && !w1.w[L3P_ALL]) ||
245        (w0.w[L3P_URB] && !w1.w[L3P_URB])) {
246       return HUGE_VALF;
247
248    } else {
249       float dw = 0;
250
251       for (unsigned i = 0; i < NUM_L3P; i++)
252          dw += fabs(w0.w[i] - w1.w[i]);
253
254       return dw;
255    }
256 }
257
258 /**
259  * Return the closest validated L3 configuration for the specified device and
260  * weight vector.
261  */
262 static const struct anv_l3_config *
263 get_l3_config(const struct brw_device_info *devinfo, struct anv_l3_weights w0)
264 {
265    const struct anv_l3_config *const cfgs = get_l3_configs(devinfo);
266    const struct anv_l3_config *cfg_best = NULL;
267    float dw_best = HUGE_VALF;
268
269    for (const struct anv_l3_config *cfg = cfgs; cfg->n[L3P_URB]; cfg++) {
270       const float dw = diff_l3_weights(w0, get_config_l3_weights(cfg));
271
272       if (dw < dw_best) {
273          cfg_best = cfg;
274          dw_best = dw;
275       }
276    }
277
278    return cfg_best;
279 }
280
281 /**
282  * Return a reasonable default L3 configuration for the specified device based
283  * on whether SLM and DC are required.  In the non-SLM non-DC case the result
284  * is intended to approximately resemble the hardware defaults.
285  */
286 static struct anv_l3_weights
287 get_default_l3_weights(const struct brw_device_info *devinfo,
288                        bool needs_dc, bool needs_slm)
289 {
290    struct anv_l3_weights w = {{ 0 }};
291
292    w.w[L3P_SLM] = needs_slm;
293    w.w[L3P_URB] = 1.0;
294
295    if (devinfo->gen >= 8) {
296       w.w[L3P_ALL] = 1.0;
297    } else {
298       w.w[L3P_DC] = needs_dc ? 0.1 : 0;
299       w.w[L3P_RO] = devinfo->is_baytrail ? 0.5 : 1.0;
300    }
301
302    return norm_l3_weights(w);
303 }
304
305 /**
306  * Calculate the desired L3 partitioning based on the current state of the
307  * pipeline.  For now this simply returns the conservative defaults calculated
308  * by get_default_l3_weights(), but we could probably do better by gathering
309  * more statistics from the pipeline state (e.g. guess of expected URB usage
310  * and bound surfaces), or by using feed-back from performance counters.
311  */
312 static struct anv_l3_weights
313 get_pipeline_state_l3_weights(const struct anv_pipeline *pipeline)
314 {
315    bool needs_dc = false, needs_slm = false;
316
317    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
318       if (!anv_pipeline_has_stage(pipeline, i))
319          continue;
320
321       const struct brw_stage_prog_data *prog_data =
322          anv_shader_bin_get_prog_data(pipeline->shaders[i]);
323
324       needs_dc |= pipeline->needs_data_cache;
325       needs_slm |= prog_data->total_shared;
326    }
327
328    return get_default_l3_weights(&pipeline->device->info,
329                                  needs_dc, needs_slm);
330 }
331
332 #define emit_lri(batch, reg, imm)                               \
333    anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {     \
334       lri.RegisterOffset = __anv_reg_num(reg);                  \
335       lri.DataDWord = imm;                                      \
336    }
337
338 #define IVB_L3SQCREG1_SQGHPCI_DEFAULT     0x00730000
339 #define VLV_L3SQCREG1_SQGHPCI_DEFAULT     0x00d30000
340 #define HSW_L3SQCREG1_SQGHPCI_DEFAULT     0x00610000
341
342 /**
343  * Program the hardware to use the specified L3 configuration.
344  */
345 static void
346 setup_l3_config(struct anv_cmd_buffer *cmd_buffer/*, struct brw_context *brw*/,
347                 const struct anv_l3_config *cfg)
348 {
349    const bool has_slm = cfg->n[L3P_SLM];
350
351    /* According to the hardware docs, the L3 partitioning can only be changed
352     * while the pipeline is completely drained and the caches are flushed,
353     * which involves a first PIPE_CONTROL flush which stalls the pipeline...
354     */
355    anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
356       pc.DCFlushEnable = true;
357       pc.PostSyncOperation = NoWrite;
358       pc.CommandStreamerStallEnable = true;
359    }
360
361    /* ...followed by a second pipelined PIPE_CONTROL that initiates
362     * invalidation of the relevant caches.  Note that because RO invalidation
363     * happens at the top of the pipeline (i.e. right away as the PIPE_CONTROL
364     * command is processed by the CS) we cannot combine it with the previous
365     * stalling flush as the hardware documentation suggests, because that
366     * would cause the CS to stall on previous rendering *after* RO
367     * invalidation and wouldn't prevent the RO caches from being polluted by
368     * concurrent rendering before the stall completes.  This intentionally
369     * doesn't implement the SKL+ hardware workaround suggesting to enable CS
370     * stall on PIPE_CONTROLs with the texture cache invalidation bit set for
371     * GPGPU workloads because the previous and subsequent PIPE_CONTROLs
372     * already guarantee that there is no concurrent GPGPU kernel execution
373     * (see SKL HSD 2132585).
374     */
375    anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
376       pc.TextureCacheInvalidationEnable = true;
377       pc.ConstantCacheInvalidationEnable = true;
378       pc.InstructionCacheInvalidateEnable = true;
379       pc.StateCacheInvalidationEnable = true;
380       pc.PostSyncOperation = NoWrite;
381    }
382
383    /* Now send a third stalling flush to make sure that invalidation is
384     * complete when the L3 configuration registers are modified.
385     */
386    anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
387       pc.DCFlushEnable = true;
388       pc.PostSyncOperation = NoWrite;
389       pc.CommandStreamerStallEnable = true;
390    }
391
392 #if GEN_GEN >= 8
393
394    assert(!cfg->n[L3P_IS] && !cfg->n[L3P_C] && !cfg->n[L3P_T]);
395
396    uint32_t l3cr;
397    anv_pack_struct(&l3cr, GENX(L3CNTLREG),
398                    .SLMEnable = has_slm,
399                    .URBAllocation = cfg->n[L3P_URB],
400                    .ROAllocation = cfg->n[L3P_RO],
401                    .DCAllocation = cfg->n[L3P_DC],
402                    .AllAllocation = cfg->n[L3P_ALL]);
403
404    /* Set up the L3 partitioning. */
405    emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG), l3cr);
406
407 #else
408
409    const bool has_dc = cfg->n[L3P_DC] || cfg->n[L3P_ALL];
410    const bool has_is = cfg->n[L3P_IS] || cfg->n[L3P_RO] || cfg->n[L3P_ALL];
411    const bool has_c = cfg->n[L3P_C] || cfg->n[L3P_RO] || cfg->n[L3P_ALL];
412    const bool has_t = cfg->n[L3P_T] || cfg->n[L3P_RO] || cfg->n[L3P_ALL];
413
414    assert(!cfg->n[L3P_ALL]);
415
416    /* When enabled SLM only uses a portion of the L3 on half of the banks,
417     * the matching space on the remaining banks has to be allocated to a
418     * client (URB for all validated configurations) set to the
419     * lower-bandwidth 2-bank address hashing mode.
420     */
421    const struct brw_device_info *devinfo = &cmd_buffer->device->info;
422    const bool urb_low_bw = has_slm && !devinfo->is_baytrail;
423    assert(!urb_low_bw || cfg->n[L3P_URB] == cfg->n[L3P_SLM]);
424
425    /* Minimum number of ways that can be allocated to the URB. */
426    const unsigned n0_urb = (devinfo->is_baytrail ? 32 : 0);
427    assert(cfg->n[L3P_URB] >= n0_urb);
428
429    uint32_t l3sqcr1, l3cr2, l3cr3;
430    anv_pack_struct(&l3sqcr1, GENX(L3SQCREG1),
431                    .ConvertDC_UC = !has_dc,
432                    .ConvertIS_UC = !has_is,
433                    .ConvertC_UC = !has_c,
434                    .ConvertT_UC = !has_t);
435    l3sqcr1 |=
436       GEN_IS_HASWELL ? HSW_L3SQCREG1_SQGHPCI_DEFAULT :
437       devinfo->is_baytrail ? VLV_L3SQCREG1_SQGHPCI_DEFAULT :
438       IVB_L3SQCREG1_SQGHPCI_DEFAULT;
439
440    anv_pack_struct(&l3cr2, GENX(L3CNTLREG2),
441                    .SLMEnable = has_slm,
442                    .URBLowBandwidth = urb_low_bw,
443                    .URBAllocation = cfg->n[L3P_URB],
444 #if !GEN_IS_HASWELL
445                    .ALLAllocation = cfg->n[L3P_ALL],
446 #endif
447                    .ROAllocation = cfg->n[L3P_RO],
448                    .DCAllocation = cfg->n[L3P_DC]);
449
450    anv_pack_struct(&l3cr3, GENX(L3CNTLREG3),
451                    .ISAllocation = cfg->n[L3P_IS],
452                    .ISLowBandwidth = 0,
453                    .CAllocation = cfg->n[L3P_C],
454                    .CLowBandwidth = 0,
455                    .TAllocation = cfg->n[L3P_T],
456                    .TLowBandwidth = 0);
457
458    /* Set up the L3 partitioning. */
459    emit_lri(&cmd_buffer->batch, GENX(L3SQCREG1), l3sqcr1);
460    emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG2), l3cr2);
461    emit_lri(&cmd_buffer->batch, GENX(L3CNTLREG3), l3cr3);
462
463 #if GEN_IS_HASWELL
464    if (cmd_buffer->device->instance->physicalDevice.cmd_parser_version >= 4) {
465       /* Enable L3 atomics on HSW if we have a DC partition, otherwise keep
466        * them disabled to avoid crashing the system hard.
467        */
468       uint32_t scratch1, chicken3;
469       anv_pack_struct(&scratch1, GENX(SCRATCH1),
470                       .L3AtomicDisable = !has_dc);
471       anv_pack_struct(&chicken3, GENX(CHICKEN3),
472                       .L3AtomicDisable = !has_dc);
473       emit_lri(&cmd_buffer->batch, GENX(SCRATCH1), scratch1);
474       emit_lri(&cmd_buffer->batch, GENX(CHICKEN3), chicken3);
475    }
476 #endif
477
478 #endif
479
480 }
481
482 /**
483  * Return the unit brw_context::urb::size is expressed in, in KB.  \sa
484  * brw_device_info::urb::size.
485  */
486 static unsigned
487 get_urb_size_scale(const struct brw_device_info *devinfo)
488 {
489    return (devinfo->gen >= 8 ? devinfo->num_slices : 1);
490 }
491
492 void
493 genX(setup_pipeline_l3_config)(struct anv_pipeline *pipeline)
494 {
495    const struct anv_l3_weights w = get_pipeline_state_l3_weights(pipeline);
496    const struct brw_device_info *devinfo = &pipeline->device->info;
497    const struct anv_l3_config *const cfg = get_l3_config(devinfo, w);
498    pipeline->urb.l3_config = cfg;
499
500    unsigned sz = cfg->n[L3P_URB] * get_l3_way_size(devinfo);
501
502 #if GEN_GEN == 9
503    /* From the SKL "L3 Allocation and Programming" documentation:
504     *
505     * "URB is limited to 1008KB due to programming restrictions.  This is not
506     * a restriction of the L3 implementation, but of the FF and other clients.
507     * Therefore, in a GT4 implementation it is possible for the programmed
508     * allocation of the L3 data array to provide 3*384KB=1152KB for URB, but
509     * only 1008KB of this will be used."
510     */
511    sz = MIN2(1008, sz);
512 #endif
513
514    pipeline->urb.total_size = sz / get_urb_size_scale(devinfo);
515 }
516
517 /**
518  * Print out the specified L3 configuration.
519  */
520 static void
521 dump_l3_config(const struct anv_l3_config *cfg)
522 {
523    fprintf(stderr, "SLM=%d URB=%d ALL=%d DC=%d RO=%d IS=%d C=%d T=%d\n",
524            cfg->n[L3P_SLM], cfg->n[L3P_URB], cfg->n[L3P_ALL],
525            cfg->n[L3P_DC], cfg->n[L3P_RO],
526            cfg->n[L3P_IS], cfg->n[L3P_C], cfg->n[L3P_T]);
527 }
528
529 void
530 genX(cmd_buffer_config_l3)(struct anv_cmd_buffer *cmd_buffer,
531                            const struct anv_pipeline *pipeline)
532 {
533    struct anv_cmd_state *state = &cmd_buffer->state;
534    const struct anv_l3_config *const cfg = pipeline->urb.l3_config;
535    assert(cfg);
536    if (cfg != state->current_l3_config) {
537       setup_l3_config(cmd_buffer, cfg);
538       state->current_l3_config = cfg;
539
540       if (unlikely(INTEL_DEBUG & DEBUG_L3)) {
541          fprintf(stderr, "L3 config transition: ");
542          dump_l3_config(cfg);
543       }
544    }
545 }