17ec681f3Smrg/* 27ec681f3Smrg * Copyright (c) 2015 Intel Corporation 37ec681f3Smrg * 47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 57ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 67ec681f3Smrg * to deal in the Software without restriction, including without limitation 77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 97ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 107ec681f3Smrg * 117ec681f3Smrg * The above copyright notice and this permission notice (including the next 127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 137ec681f3Smrg * Software. 147ec681f3Smrg * 157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 217ec681f3Smrg * IN THE SOFTWARE. 227ec681f3Smrg */ 237ec681f3Smrg 247ec681f3Smrg#include <stdlib.h> 257ec681f3Smrg#include <math.h> 267ec681f3Smrg 277ec681f3Smrg#include "util/macros.h" 287ec681f3Smrg#include "main/macros.h" 297ec681f3Smrg 307ec681f3Smrg#include "intel_l3_config.h" 317ec681f3Smrg 327ec681f3Smrgstruct intel_l3_list { 337ec681f3Smrg const struct intel_l3_config *configs; 347ec681f3Smrg int length; 357ec681f3Smrg}; 367ec681f3Smrg 377ec681f3Smrg#define DECLARE_L3_LIST(hw) \ 387ec681f3Smrg struct intel_l3_list hw##_l3_list = \ 397ec681f3Smrg { .configs = hw##_l3_configs, .length = ARRAY_SIZE(hw##_l3_configs) } 407ec681f3Smrg 417ec681f3Smrg/** 427ec681f3Smrg * IVB/HSW validated L3 configurations. The first entry will be used as 437ec681f3Smrg * default by gfx7_restore_default_l3_config(), otherwise the ordering is 447ec681f3Smrg * unimportant. 457ec681f3Smrg */ 467ec681f3Smrgstatic const struct intel_l3_config ivb_l3_configs[] = { 477ec681f3Smrg /* SLM URB ALL DC RO IS C T */ 487ec681f3Smrg {{ 0, 32, 0, 0, 32, 0, 0, 0 }}, 497ec681f3Smrg {{ 0, 32, 0, 16, 16, 0, 0, 0 }}, 507ec681f3Smrg {{ 0, 32, 0, 4, 0, 8, 4, 16 }}, 517ec681f3Smrg {{ 0, 28, 0, 8, 0, 8, 4, 16 }}, 527ec681f3Smrg {{ 0, 28, 0, 16, 0, 8, 4, 8 }}, 537ec681f3Smrg {{ 0, 28, 0, 8, 0, 16, 4, 8 }}, 547ec681f3Smrg {{ 0, 28, 0, 0, 0, 16, 4, 16 }}, 557ec681f3Smrg {{ 0, 32, 0, 0, 0, 16, 0, 16 }}, 567ec681f3Smrg {{ 0, 28, 0, 4, 32, 0, 0, 0 }}, 577ec681f3Smrg {{ 16, 16, 0, 16, 16, 0, 0, 0 }}, 587ec681f3Smrg {{ 16, 16, 0, 8, 0, 8, 8, 8 }}, 597ec681f3Smrg {{ 16, 16, 0, 4, 0, 8, 4, 16 }}, 607ec681f3Smrg {{ 16, 16, 0, 4, 0, 16, 4, 8 }}, 617ec681f3Smrg {{ 16, 16, 0, 0, 32, 0, 0, 0 }}, 627ec681f3Smrg}; 637ec681f3SmrgDECLARE_L3_LIST(ivb); 647ec681f3Smrg 657ec681f3Smrg/** 667ec681f3Smrg * VLV validated L3 configurations. \sa ivb_l3_configs. 677ec681f3Smrg */ 687ec681f3Smrgstatic const struct intel_l3_config vlv_l3_configs[] = { 697ec681f3Smrg /* SLM URB ALL DC RO IS C T */ 707ec681f3Smrg {{ 0, 64, 0, 0, 32, 0, 0, 0 }}, 717ec681f3Smrg {{ 0, 80, 0, 0, 16, 0, 0, 0 }}, 727ec681f3Smrg {{ 0, 80, 0, 8, 8, 0, 0, 0 }}, 737ec681f3Smrg {{ 0, 64, 0, 16, 16, 0, 0, 0 }}, 747ec681f3Smrg {{ 0, 60, 0, 4, 32, 0, 0, 0 }}, 757ec681f3Smrg {{ 32, 32, 0, 16, 16, 0, 0, 0 }}, 767ec681f3Smrg {{ 32, 40, 0, 8, 16, 0, 0, 0 }}, 777ec681f3Smrg {{ 32, 40, 0, 16, 8, 0, 0, 0 }}, 787ec681f3Smrg}; 797ec681f3SmrgDECLARE_L3_LIST(vlv); 807ec681f3Smrg 817ec681f3Smrg/** 827ec681f3Smrg * BDW validated L3 configurations. \sa ivb_l3_configs. 837ec681f3Smrg */ 847ec681f3Smrgstatic const struct intel_l3_config bdw_l3_configs[] = { 857ec681f3Smrg /* SLM URB ALL DC RO IS C T */ 867ec681f3Smrg {{ 0, 48, 48, 0, 0, 0, 0, 0 }}, 877ec681f3Smrg {{ 0, 48, 0, 16, 32, 0, 0, 0 }}, 887ec681f3Smrg {{ 0, 32, 0, 16, 48, 0, 0, 0 }}, 897ec681f3Smrg {{ 0, 32, 0, 0, 64, 0, 0, 0 }}, 907ec681f3Smrg {{ 0, 32, 64, 0, 0, 0, 0, 0 }}, 917ec681f3Smrg {{ 24, 16, 48, 0, 0, 0, 0, 0 }}, 927ec681f3Smrg {{ 24, 16, 0, 16, 32, 0, 0, 0 }}, 937ec681f3Smrg {{ 24, 16, 0, 32, 16, 0, 0, 0 }}, 947ec681f3Smrg}; 957ec681f3SmrgDECLARE_L3_LIST(bdw); 967ec681f3Smrg 977ec681f3Smrg/** 987ec681f3Smrg * CHV/SKL validated L3 configurations. \sa ivb_l3_configs. 997ec681f3Smrg */ 1007ec681f3Smrgstatic const struct intel_l3_config chv_l3_configs[] = { 1017ec681f3Smrg /* SLM URB ALL DC RO IS C T */ 1027ec681f3Smrg {{ 0, 48, 48, 0, 0, 0, 0, 0 }}, 1037ec681f3Smrg {{ 0, 48, 0, 16, 32, 0, 0, 0 }}, 1047ec681f3Smrg {{ 0, 32, 0, 16, 48, 0, 0, 0 }}, 1057ec681f3Smrg {{ 0, 32, 0, 0, 64, 0, 0, 0 }}, 1067ec681f3Smrg {{ 0, 32, 64, 0, 0, 0, 0, 0 }}, 1077ec681f3Smrg {{ 32, 16, 48, 0, 0, 0, 0, 0 }}, 1087ec681f3Smrg {{ 32, 16, 0, 16, 32, 0, 0, 0 }}, 1097ec681f3Smrg {{ 32, 16, 0, 32, 16, 0, 0, 0 }}, 1107ec681f3Smrg}; 1117ec681f3SmrgDECLARE_L3_LIST(chv); 1127ec681f3Smrg 1137ec681f3Smrg/** 1147ec681f3Smrg * BXT 2x6 validated L3 configurations. \sa ivb_l3_configs. 1157ec681f3Smrg */ 1167ec681f3Smrgstatic const struct intel_l3_config bxt_2x6_l3_configs[] = { 1177ec681f3Smrg /* SLM URB ALL DC RO IS C T */ 1187ec681f3Smrg {{ 0, 32, 48, 0, 0, 0, 0, 0 }}, 1197ec681f3Smrg {{ 0, 32, 0, 8, 40, 0, 0, 0 }}, 1207ec681f3Smrg {{ 0, 32, 0, 32, 16, 0, 0, 0 }}, 1217ec681f3Smrg {{ 16, 16, 48, 0, 0, 0, 0, 0 }}, 1227ec681f3Smrg {{ 16, 16, 0, 40, 8, 0, 0, 0 }}, 1237ec681f3Smrg {{ 16, 16, 0, 16, 32, 0, 0, 0 }}, 1247ec681f3Smrg}; 1257ec681f3SmrgDECLARE_L3_LIST(bxt_2x6); 1267ec681f3Smrg 1277ec681f3Smrg/** 1287ec681f3Smrg * ICL validated L3 configurations. \sa icl_l3_configs. 1297ec681f3Smrg * Zeroth entry in below table has been commented out intentionally 1307ec681f3Smrg * due to known issues with this configuration. Many other entries 1317ec681f3Smrg * suggested by h/w specification aren't added here because they 1327ec681f3Smrg * do under allocation of L3 cache with below partitioning. 1337ec681f3Smrg */ 1347ec681f3Smrgstatic const struct intel_l3_config icl_l3_configs[] = { 1357ec681f3Smrg /* SLM URB ALL DC RO IS C T */ 1367ec681f3Smrg /*{{ 0, 16, 80, 0, 0, 0, 0, 0 }},*/ 1377ec681f3Smrg {{ 0, 32, 64, 0, 0, 0, 0, 0 }}, 1387ec681f3Smrg}; 1397ec681f3SmrgDECLARE_L3_LIST(icl); 1407ec681f3Smrg 1417ec681f3Smrg/** 1427ec681f3Smrg * TGL validated L3 configurations. \sa tgl_l3_configs. 1437ec681f3Smrg */ 1447ec681f3Smrgstatic const struct intel_l3_config tgl_l3_configs[] = { 1457ec681f3Smrg /* SLM URB ALL DC RO IS C T */ 1467ec681f3Smrg {{ 0, 32, 88, 0, 0, 0, 0, 0 }}, 1477ec681f3Smrg {{ 0, 16, 104, 0, 0, 0, 0, 0 }}, 1487ec681f3Smrg}; 1497ec681f3SmrgDECLARE_L3_LIST(tgl); 1507ec681f3Smrg 1517ec681f3Smrg/** 1527ec681f3Smrg * Empty L3 configurations. \sa empty_l3_configs. 1537ec681f3Smrg */ 1547ec681f3Smrgstatic const struct intel_l3_config empty_l3_configs[] = { 1557ec681f3Smrg /* No configurations. L3FullWayAllocationEnable is always set. */ 1567ec681f3Smrg}; 1577ec681f3SmrgDECLARE_L3_LIST(empty); 1587ec681f3Smrg 1597ec681f3Smrg/** 1607ec681f3Smrg * Return a zero-terminated array of validated L3 configurations for the 1617ec681f3Smrg * specified device. 1627ec681f3Smrg */ 1637ec681f3Smrgstatic const struct intel_l3_list * 1647ec681f3Smrgget_l3_list(const struct intel_device_info *devinfo) 1657ec681f3Smrg{ 1667ec681f3Smrg switch (devinfo->ver) { 1677ec681f3Smrg case 7: 1687ec681f3Smrg return (devinfo->is_baytrail ? &vlv_l3_list : &ivb_l3_list); 1697ec681f3Smrg 1707ec681f3Smrg case 8: 1717ec681f3Smrg return (devinfo->is_cherryview ? &chv_l3_list : &bdw_l3_list); 1727ec681f3Smrg 1737ec681f3Smrg case 9: 1747ec681f3Smrg if (devinfo->l3_banks == 1) 1757ec681f3Smrg return &bxt_2x6_l3_list; 1767ec681f3Smrg return &chv_l3_list; 1777ec681f3Smrg 1787ec681f3Smrg case 11: 1797ec681f3Smrg return &icl_l3_list; 1807ec681f3Smrg 1817ec681f3Smrg case 12: 1827ec681f3Smrg if (devinfo->is_dg1 || devinfo->is_dg2) 1837ec681f3Smrg return &empty_l3_list; 1847ec681f3Smrg else 1857ec681f3Smrg return &tgl_l3_list; 1867ec681f3Smrg 1877ec681f3Smrg default: 1887ec681f3Smrg unreachable("Not implemented"); 1897ec681f3Smrg } 1907ec681f3Smrg} 1917ec681f3Smrg 1927ec681f3Smrg/** 1937ec681f3Smrg * L1-normalize a vector of L3 partition weights. 1947ec681f3Smrg */ 1957ec681f3Smrgstatic struct intel_l3_weights 1967ec681f3Smrgnorm_l3_weights(struct intel_l3_weights w) 1977ec681f3Smrg{ 1987ec681f3Smrg float sz = 0; 1997ec681f3Smrg 2007ec681f3Smrg for (unsigned i = 0; i < INTEL_NUM_L3P; i++) 2017ec681f3Smrg sz += w.w[i]; 2027ec681f3Smrg 2037ec681f3Smrg for (unsigned i = 0; i < INTEL_NUM_L3P; i++) 2047ec681f3Smrg w.w[i] /= sz; 2057ec681f3Smrg 2067ec681f3Smrg return w; 2077ec681f3Smrg} 2087ec681f3Smrg 2097ec681f3Smrg/** 2107ec681f3Smrg * Get the relative partition weights of the specified L3 configuration. 2117ec681f3Smrg */ 2127ec681f3Smrgstruct intel_l3_weights 2137ec681f3Smrgintel_get_l3_config_weights(const struct intel_l3_config *cfg) 2147ec681f3Smrg{ 2157ec681f3Smrg if (cfg) { 2167ec681f3Smrg struct intel_l3_weights w; 2177ec681f3Smrg 2187ec681f3Smrg for (unsigned i = 0; i < INTEL_NUM_L3P; i++) 2197ec681f3Smrg w.w[i] = cfg->n[i]; 2207ec681f3Smrg 2217ec681f3Smrg return norm_l3_weights(w); 2227ec681f3Smrg } else { 2237ec681f3Smrg const struct intel_l3_weights w = { { 0 } }; 2247ec681f3Smrg return w; 2257ec681f3Smrg } 2267ec681f3Smrg} 2277ec681f3Smrg 2287ec681f3Smrg/** 2297ec681f3Smrg * Distance between two L3 configurations represented as vectors of weights. 2307ec681f3Smrg * Usually just the L1 metric except when the two configurations are 2317ec681f3Smrg * considered incompatible in which case the distance will be infinite. Note 2327ec681f3Smrg * that the compatibility condition is asymmetric -- They will be considered 2337ec681f3Smrg * incompatible whenever the reference configuration \p w0 requires SLM, DC, 2347ec681f3Smrg * or URB but \p w1 doesn't provide it. 2357ec681f3Smrg */ 2367ec681f3Smrgfloat 2377ec681f3Smrgintel_diff_l3_weights(struct intel_l3_weights w0, struct intel_l3_weights w1) 2387ec681f3Smrg{ 2397ec681f3Smrg if ((w0.w[INTEL_L3P_SLM] && !w1.w[INTEL_L3P_SLM]) || 2407ec681f3Smrg (w0.w[INTEL_L3P_DC] && !w1.w[INTEL_L3P_DC] && !w1.w[INTEL_L3P_ALL]) || 2417ec681f3Smrg (w0.w[INTEL_L3P_URB] && !w1.w[INTEL_L3P_URB])) { 2427ec681f3Smrg return HUGE_VALF; 2437ec681f3Smrg 2447ec681f3Smrg } else { 2457ec681f3Smrg float dw = 0; 2467ec681f3Smrg 2477ec681f3Smrg for (unsigned i = 0; i < INTEL_NUM_L3P; i++) 2487ec681f3Smrg dw += fabsf(w0.w[i] - w1.w[i]); 2497ec681f3Smrg 2507ec681f3Smrg return dw; 2517ec681f3Smrg } 2527ec681f3Smrg} 2537ec681f3Smrg 2547ec681f3Smrg/** 2557ec681f3Smrg * Return a reasonable default L3 configuration for the specified device based 2567ec681f3Smrg * on whether SLM and DC are required. In the non-SLM non-DC case the result 2577ec681f3Smrg * is intended to approximately resemble the hardware defaults. 2587ec681f3Smrg */ 2597ec681f3Smrgstruct intel_l3_weights 2607ec681f3Smrgintel_get_default_l3_weights(const struct intel_device_info *devinfo, 2617ec681f3Smrg bool needs_dc, bool needs_slm) 2627ec681f3Smrg{ 2637ec681f3Smrg struct intel_l3_weights w = {{ 0 }}; 2647ec681f3Smrg 2657ec681f3Smrg w.w[INTEL_L3P_SLM] = devinfo->ver < 11 && needs_slm; 2667ec681f3Smrg w.w[INTEL_L3P_URB] = 1.0; 2677ec681f3Smrg 2687ec681f3Smrg if (devinfo->ver >= 8) { 2697ec681f3Smrg w.w[INTEL_L3P_ALL] = 1.0; 2707ec681f3Smrg } else { 2717ec681f3Smrg w.w[INTEL_L3P_DC] = needs_dc ? 0.1 : 0; 2727ec681f3Smrg w.w[INTEL_L3P_RO] = devinfo->is_baytrail ? 0.5 : 1.0; 2737ec681f3Smrg } 2747ec681f3Smrg 2757ec681f3Smrg return norm_l3_weights(w); 2767ec681f3Smrg} 2777ec681f3Smrg 2787ec681f3Smrg/** 2797ec681f3Smrg * Get the default L3 configuration 2807ec681f3Smrg */ 2817ec681f3Smrgconst struct intel_l3_config * 2827ec681f3Smrgintel_get_default_l3_config(const struct intel_device_info *devinfo) 2837ec681f3Smrg{ 2847ec681f3Smrg /* For efficiency assume that the first entry of the array matches the 2857ec681f3Smrg * default configuration. 2867ec681f3Smrg */ 2877ec681f3Smrg const struct intel_l3_list *const list = get_l3_list(devinfo); 2887ec681f3Smrg assert(list->length > 0 || devinfo->ver >= 12); 2897ec681f3Smrg if (list->length > 0) { 2907ec681f3Smrg const struct intel_l3_config *const cfg = &list->configs[0]; 2917ec681f3Smrg assert(cfg == intel_get_l3_config(devinfo, 2927ec681f3Smrg intel_get_default_l3_weights(devinfo, false, false))); 2937ec681f3Smrg return cfg; 2947ec681f3Smrg } else { 2957ec681f3Smrg return NULL; 2967ec681f3Smrg } 2977ec681f3Smrg} 2987ec681f3Smrg 2997ec681f3Smrg/** 3007ec681f3Smrg * Return the closest validated L3 configuration for the specified device and 3017ec681f3Smrg * weight vector. 3027ec681f3Smrg */ 3037ec681f3Smrgconst struct intel_l3_config * 3047ec681f3Smrgintel_get_l3_config(const struct intel_device_info *devinfo, 3057ec681f3Smrg struct intel_l3_weights w0) 3067ec681f3Smrg{ 3077ec681f3Smrg const struct intel_l3_list *const list = get_l3_list(devinfo); 3087ec681f3Smrg const struct intel_l3_config *const cfgs = list->configs; 3097ec681f3Smrg const struct intel_l3_config *cfg_best = NULL; 3107ec681f3Smrg float dw_best = HUGE_VALF; 3117ec681f3Smrg 3127ec681f3Smrg for (int i = 0; i < list->length; i++) { 3137ec681f3Smrg const struct intel_l3_config *cfg = &cfgs[i]; 3147ec681f3Smrg const float dw = intel_diff_l3_weights(w0, intel_get_l3_config_weights(cfg)); 3157ec681f3Smrg 3167ec681f3Smrg if (dw < dw_best) { 3177ec681f3Smrg cfg_best = cfg; 3187ec681f3Smrg dw_best = dw; 3197ec681f3Smrg } 3207ec681f3Smrg } 3217ec681f3Smrg 3227ec681f3Smrg assert(cfg_best || devinfo->ver >= 12); 3237ec681f3Smrg return cfg_best; 3247ec681f3Smrg} 3257ec681f3Smrg 3267ec681f3Smrg/** 3277ec681f3Smrg * Return the size of an L3 way in KB. 3287ec681f3Smrg */ 3297ec681f3Smrgstatic unsigned 3307ec681f3Smrgget_l3_way_size(const struct intel_device_info *devinfo) 3317ec681f3Smrg{ 3327ec681f3Smrg const unsigned way_size_per_bank = 3337ec681f3Smrg (devinfo->ver >= 9 && devinfo->l3_banks == 1) || devinfo->ver >= 11 ? 3347ec681f3Smrg 4 : 2; 3357ec681f3Smrg 3367ec681f3Smrg assert(devinfo->l3_banks); 3377ec681f3Smrg return way_size_per_bank * devinfo->l3_banks; 3387ec681f3Smrg} 3397ec681f3Smrg 3407ec681f3Smrg/** 3417ec681f3Smrg * Return the unit brw_context::urb::size is expressed in, in KB. \sa 3427ec681f3Smrg * intel_device_info::urb::size. 3437ec681f3Smrg */ 3447ec681f3Smrgstatic unsigned 3457ec681f3Smrgget_urb_size_scale(const struct intel_device_info *devinfo) 3467ec681f3Smrg{ 3477ec681f3Smrg return (devinfo->ver >= 8 ? devinfo->num_slices : 1); 3487ec681f3Smrg} 3497ec681f3Smrg 3507ec681f3Smrgunsigned 3517ec681f3Smrgintel_get_l3_config_urb_size(const struct intel_device_info *devinfo, 3527ec681f3Smrg const struct intel_l3_config *cfg) 3537ec681f3Smrg{ 3547ec681f3Smrg /* We don't have to program the URB size in DG1, it's a fixed value. */ 3557ec681f3Smrg if (devinfo->is_dg1) 3567ec681f3Smrg return devinfo->urb.size; 3577ec681f3Smrg 3587ec681f3Smrg /* From the SKL "L3 Allocation and Programming" documentation: 3597ec681f3Smrg * 3607ec681f3Smrg * "URB is limited to 1008KB due to programming restrictions. This is not 3617ec681f3Smrg * a restriction of the L3 implementation, but of the FF and other clients. 3627ec681f3Smrg * Therefore, in a GT4 implementation it is possible for the programmed 3637ec681f3Smrg * allocation of the L3 data array to provide 3*384KB=1152KB for URB, but 3647ec681f3Smrg * only 1008KB of this will be used." 3657ec681f3Smrg */ 3667ec681f3Smrg const unsigned max = (devinfo->ver == 9 ? 1008 : ~0); 3677ec681f3Smrg return MIN2(max, cfg->n[INTEL_L3P_URB] * get_l3_way_size(devinfo)) / 3687ec681f3Smrg get_urb_size_scale(devinfo); 3697ec681f3Smrg} 3707ec681f3Smrg 3717ec681f3Smrg/** 3727ec681f3Smrg * Print out the specified L3 configuration. 3737ec681f3Smrg */ 3747ec681f3Smrgvoid 3757ec681f3Smrgintel_dump_l3_config(const struct intel_l3_config *cfg, FILE *fp) 3767ec681f3Smrg{ 3777ec681f3Smrg fprintf(stderr, "SLM=%d URB=%d ALL=%d DC=%d RO=%d IS=%d C=%d T=%d\n", 3787ec681f3Smrg cfg->n[INTEL_L3P_SLM], cfg->n[INTEL_L3P_URB], cfg->n[INTEL_L3P_ALL], 3797ec681f3Smrg cfg->n[INTEL_L3P_DC], cfg->n[INTEL_L3P_RO], 3807ec681f3Smrg cfg->n[INTEL_L3P_IS], cfg->n[INTEL_L3P_C], cfg->n[INTEL_L3P_T]); 3817ec681f3Smrg} 382