Fenghua Yu | 78e99b4 | 2016-10-22 06:19:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Resource Director Technology(RDT) |
| 3 | * - Cache Allocation code. |
| 4 | * |
| 5 | * Copyright (C) 2016 Intel Corporation |
| 6 | * |
| 7 | * Authors: |
| 8 | * Fenghua Yu <fenghua.yu@intel.com> |
| 9 | * Tony Luck <tony.luck@intel.com> |
| 10 | * Vikas Shivappa <vikas.shivappa@intel.com> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms and conditions of the GNU General Public License, |
| 14 | * version 2, as published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 19 | * more details. |
| 20 | * |
| 21 | * More information about RDT be found in the Intel (R) x86 Architecture |
| 22 | * Software Developer Manual June 2016, volume 3, section 17.17. |
| 23 | */ |
| 24 | |
| 25 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 26 | |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/err.h> |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 29 | #include <linux/cacheinfo.h> |
| 30 | #include <linux/cpuhotplug.h> |
Fenghua Yu | 78e99b4 | 2016-10-22 06:19:53 -0700 | [diff] [blame] | 31 | |
Fenghua Yu | 113c609 | 2016-10-22 06:19:54 -0700 | [diff] [blame] | 32 | #include <asm/intel-family.h> |
Vikas Shivappa | 0583020 | 2017-07-25 14:14:23 -0700 | [diff] [blame] | 33 | #include <asm/intel_rdt_sched.h> |
| 34 | #include "intel_rdt.h" |
Fenghua Yu | 113c609 | 2016-10-22 06:19:54 -0700 | [diff] [blame] | 35 | |
Vikas Shivappa | 05b9341 | 2017-04-07 17:33:53 -0700 | [diff] [blame] | 36 | #define MAX_MBA_BW 100u |
| 37 | #define MBA_IS_LINEAR 0x4 |
| 38 | |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 39 | /* Mutex to protect rdtgroup access. */ |
| 40 | DEFINE_MUTEX(rdtgroup_mutex); |
| 41 | |
Vikas Shivappa | de016df | 2017-04-03 14:44:17 -0700 | [diff] [blame] | 42 | /* |
Vikas Shivappa | c39a0e2 | 2017-07-25 14:14:20 -0700 | [diff] [blame] | 43 | * The cached intel_pqr_state is strictly per CPU and can never be |
| 44 | * updated from a remote CPU. Functions which modify the state |
| 45 | * are called with interrupts disabled and no preemption, which |
| 46 | * is sufficient for the protection. |
| 47 | */ |
| 48 | DEFINE_PER_CPU(struct intel_pqr_state, pqr_state); |
| 49 | |
Vikas Shivappa | b09d981 | 2017-07-25 14:14:35 -0700 | [diff] [blame] | 50 | DEFINE_PER_CPU_READ_MOSTLY(struct intel_pqr_state, rdt_cpu_default); |
| 51 | |
Vikas Shivappa | c39a0e2 | 2017-07-25 14:14:20 -0700 | [diff] [blame] | 52 | /* |
Vikas Shivappa | de016df | 2017-04-03 14:44:17 -0700 | [diff] [blame] | 53 | * Used to store the max resource name width and max resource data width |
| 54 | * to display the schemata in a tabular format |
| 55 | */ |
| 56 | int max_name_width, max_data_width; |
| 57 | |
Vikas Shivappa | 6a445ed | 2017-07-25 14:14:27 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Global boolean for rdt_alloc which is true if any |
| 60 | * resource allocation is enabled. |
| 61 | */ |
| 62 | bool rdt_alloc_capable; |
| 63 | |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 64 | static void |
Vikas Shivappa | 05b9341 | 2017-04-07 17:33:53 -0700 | [diff] [blame] | 65 | mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r); |
| 66 | static void |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 67 | cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r); |
| 68 | |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 69 | #define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains) |
| 70 | |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 71 | struct rdt_resource rdt_resources_all[] = { |
Vikas Shivappa | dd13185 | 2017-07-25 14:14:26 -0700 | [diff] [blame] | 72 | [RDT_RESOURCE_L3] = |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 73 | { |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 74 | .name = "L3", |
| 75 | .domains = domain_init(RDT_RESOURCE_L3), |
| 76 | .msr_base = IA32_L3_CBM_BASE, |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 77 | .msr_update = cat_wrmsr, |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 78 | .cache_level = 3, |
| 79 | .cache = { |
| 80 | .min_cbm_bits = 1, |
| 81 | .cbm_idx_mult = 1, |
| 82 | .cbm_idx_offset = 0, |
| 83 | }, |
Vikas Shivappa | c6ea67d | 2017-04-07 17:33:56 -0700 | [diff] [blame] | 84 | .parse_ctrlval = parse_cbm, |
| 85 | .format_str = "%d=%0*x", |
Tony luck | 5dc1d5c | 2017-07-25 14:14:29 -0700 | [diff] [blame] | 86 | .fflags = RFTYPE_RES_CACHE, |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 87 | }, |
Vikas Shivappa | dd13185 | 2017-07-25 14:14:26 -0700 | [diff] [blame] | 88 | [RDT_RESOURCE_L3DATA] = |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 89 | { |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 90 | .name = "L3DATA", |
| 91 | .domains = domain_init(RDT_RESOURCE_L3DATA), |
| 92 | .msr_base = IA32_L3_CBM_BASE, |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 93 | .msr_update = cat_wrmsr, |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 94 | .cache_level = 3, |
| 95 | .cache = { |
| 96 | .min_cbm_bits = 1, |
| 97 | .cbm_idx_mult = 2, |
| 98 | .cbm_idx_offset = 0, |
| 99 | }, |
Vikas Shivappa | c6ea67d | 2017-04-07 17:33:56 -0700 | [diff] [blame] | 100 | .parse_ctrlval = parse_cbm, |
| 101 | .format_str = "%d=%0*x", |
Tony luck | 5dc1d5c | 2017-07-25 14:14:29 -0700 | [diff] [blame] | 102 | .fflags = RFTYPE_RES_CACHE, |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 103 | }, |
Vikas Shivappa | dd13185 | 2017-07-25 14:14:26 -0700 | [diff] [blame] | 104 | [RDT_RESOURCE_L3CODE] = |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 105 | { |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 106 | .name = "L3CODE", |
| 107 | .domains = domain_init(RDT_RESOURCE_L3CODE), |
| 108 | .msr_base = IA32_L3_CBM_BASE, |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 109 | .msr_update = cat_wrmsr, |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 110 | .cache_level = 3, |
| 111 | .cache = { |
| 112 | .min_cbm_bits = 1, |
| 113 | .cbm_idx_mult = 2, |
| 114 | .cbm_idx_offset = 1, |
| 115 | }, |
Vikas Shivappa | c6ea67d | 2017-04-07 17:33:56 -0700 | [diff] [blame] | 116 | .parse_ctrlval = parse_cbm, |
| 117 | .format_str = "%d=%0*x", |
Tony luck | 5dc1d5c | 2017-07-25 14:14:29 -0700 | [diff] [blame] | 118 | .fflags = RFTYPE_RES_CACHE, |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 119 | }, |
Vikas Shivappa | dd13185 | 2017-07-25 14:14:26 -0700 | [diff] [blame] | 120 | [RDT_RESOURCE_L2] = |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 121 | { |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 122 | .name = "L2", |
| 123 | .domains = domain_init(RDT_RESOURCE_L2), |
| 124 | .msr_base = IA32_L2_CBM_BASE, |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 125 | .msr_update = cat_wrmsr, |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 126 | .cache_level = 2, |
| 127 | .cache = { |
| 128 | .min_cbm_bits = 1, |
| 129 | .cbm_idx_mult = 1, |
| 130 | .cbm_idx_offset = 0, |
| 131 | }, |
Vikas Shivappa | c6ea67d | 2017-04-07 17:33:56 -0700 | [diff] [blame] | 132 | .parse_ctrlval = parse_cbm, |
| 133 | .format_str = "%d=%0*x", |
Tony luck | 5dc1d5c | 2017-07-25 14:14:29 -0700 | [diff] [blame] | 134 | .fflags = RFTYPE_RES_CACHE, |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 135 | }, |
Vikas Shivappa | dd13185 | 2017-07-25 14:14:26 -0700 | [diff] [blame] | 136 | [RDT_RESOURCE_MBA] = |
Vikas Shivappa | 05b9341 | 2017-04-07 17:33:53 -0700 | [diff] [blame] | 137 | { |
| 138 | .name = "MB", |
| 139 | .domains = domain_init(RDT_RESOURCE_MBA), |
| 140 | .msr_base = IA32_MBA_THRTL_BASE, |
| 141 | .msr_update = mba_wrmsr, |
| 142 | .cache_level = 3, |
Vikas Shivappa | 64e8ed3 | 2017-04-07 17:33:57 -0700 | [diff] [blame] | 143 | .parse_ctrlval = parse_bw, |
| 144 | .format_str = "%d=%*d", |
Tony luck | 5dc1d5c | 2017-07-25 14:14:29 -0700 | [diff] [blame] | 145 | .fflags = RFTYPE_RES_MB, |
Vikas Shivappa | 05b9341 | 2017-04-07 17:33:53 -0700 | [diff] [blame] | 146 | }, |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 149 | static unsigned int cbm_idx(struct rdt_resource *r, unsigned int closid) |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 150 | { |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 151 | return closid * r->cache.cbm_idx_mult + r->cache.cbm_idx_offset; |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Fenghua Yu | 113c609 | 2016-10-22 06:19:54 -0700 | [diff] [blame] | 154 | /* |
| 155 | * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs |
| 156 | * as they do not have CPUID enumeration support for Cache allocation. |
| 157 | * The check for Vendor/Family/Model is not enough to guarantee that |
| 158 | * the MSRs won't #GP fault because only the following SKUs support |
| 159 | * CAT: |
| 160 | * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz |
| 161 | * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz |
| 162 | * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz |
| 163 | * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz |
| 164 | * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz |
| 165 | * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz |
| 166 | * |
| 167 | * Probe by trying to write the first of the L3 cach mask registers |
| 168 | * and checking that the bits stick. Max CLOSids is always 4 and max cbm length |
| 169 | * is always 20 on hsw server parts. The minimum cache bitmask length |
| 170 | * allowed for HSW server is always 2 bits. Hardcode all of them. |
| 171 | */ |
| 172 | static inline bool cache_alloc_hsw_probe(void) |
| 173 | { |
| 174 | if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && |
| 175 | boot_cpu_data.x86 == 6 && |
| 176 | boot_cpu_data.x86_model == INTEL_FAM6_HASWELL_X) { |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 177 | struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3]; |
Fenghua Yu | 113c609 | 2016-10-22 06:19:54 -0700 | [diff] [blame] | 178 | u32 l, h, max_cbm = BIT_MASK(20) - 1; |
| 179 | |
| 180 | if (wrmsr_safe(IA32_L3_CBM_BASE, max_cbm, 0)) |
| 181 | return false; |
| 182 | rdmsr(IA32_L3_CBM_BASE, l, h); |
| 183 | |
| 184 | /* If all the bits were set in MSR, return success */ |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 185 | if (l != max_cbm) |
| 186 | return false; |
| 187 | |
| 188 | r->num_closid = 4; |
Vikas Shivappa | 2545e9f | 2017-04-07 17:33:51 -0700 | [diff] [blame] | 189 | r->default_ctrl = max_cbm; |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 190 | r->cache.cbm_len = 20; |
| 191 | r->cache.min_cbm_bits = 2; |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 192 | r->alloc_capable = true; |
| 193 | r->alloc_enabled = true; |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 194 | |
| 195 | return true; |
Fenghua Yu | 113c609 | 2016-10-22 06:19:54 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return false; |
| 199 | } |
| 200 | |
Vikas Shivappa | 05b9341 | 2017-04-07 17:33:53 -0700 | [diff] [blame] | 201 | /* |
| 202 | * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values |
| 203 | * exposed to user interface and the h/w understandable delay values. |
| 204 | * |
| 205 | * The non-linear delay values have the granularity of power of two |
| 206 | * and also the h/w does not guarantee a curve for configured delay |
| 207 | * values vs. actual b/w enforced. |
| 208 | * Hence we need a mapping that is pre calibrated so the user can |
| 209 | * express the memory b/w as a percentage value. |
| 210 | */ |
| 211 | static inline bool rdt_get_mb_table(struct rdt_resource *r) |
| 212 | { |
| 213 | /* |
| 214 | * There are no Intel SKUs as of now to support non-linear delay. |
| 215 | */ |
| 216 | pr_info("MBA b/w map not implemented for cpu:%d, model:%d", |
| 217 | boot_cpu_data.x86, boot_cpu_data.x86_model); |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | static bool rdt_get_mem_config(struct rdt_resource *r) |
| 223 | { |
| 224 | union cpuid_0x10_3_eax eax; |
| 225 | union cpuid_0x10_x_edx edx; |
| 226 | u32 ebx, ecx; |
| 227 | |
| 228 | cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full); |
| 229 | r->num_closid = edx.split.cos_max + 1; |
| 230 | r->membw.max_delay = eax.split.max_delay + 1; |
| 231 | r->default_ctrl = MAX_MBA_BW; |
| 232 | if (ecx & MBA_IS_LINEAR) { |
| 233 | r->membw.delay_linear = true; |
| 234 | r->membw.min_bw = MAX_MBA_BW - r->membw.max_delay; |
| 235 | r->membw.bw_gran = MAX_MBA_BW - r->membw.max_delay; |
| 236 | } else { |
| 237 | if (!rdt_get_mb_table(r)) |
| 238 | return false; |
| 239 | } |
| 240 | r->data_width = 3; |
| 241 | |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 242 | r->alloc_capable = true; |
| 243 | r->alloc_enabled = true; |
Vikas Shivappa | 05b9341 | 2017-04-07 17:33:53 -0700 | [diff] [blame] | 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
Vikas Shivappa | 6a445ed | 2017-07-25 14:14:27 -0700 | [diff] [blame] | 248 | static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r) |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 249 | { |
| 250 | union cpuid_0x10_1_eax eax; |
Vikas Shivappa | 2545e9f | 2017-04-07 17:33:51 -0700 | [diff] [blame] | 251 | union cpuid_0x10_x_edx edx; |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 252 | u32 ebx, ecx; |
| 253 | |
| 254 | cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full); |
| 255 | r->num_closid = edx.split.cos_max + 1; |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 256 | r->cache.cbm_len = eax.split.cbm_len + 1; |
Vikas Shivappa | 2545e9f | 2017-04-07 17:33:51 -0700 | [diff] [blame] | 257 | r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1; |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 258 | r->data_width = (r->cache.cbm_len + 3) / 4; |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 259 | r->alloc_capable = true; |
| 260 | r->alloc_enabled = true; |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static void rdt_get_cdp_l3_config(int type) |
| 264 | { |
| 265 | struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3]; |
| 266 | struct rdt_resource *r = &rdt_resources_all[type]; |
| 267 | |
| 268 | r->num_closid = r_l3->num_closid / 2; |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 269 | r->cache.cbm_len = r_l3->cache.cbm_len; |
Vikas Shivappa | 2545e9f | 2017-04-07 17:33:51 -0700 | [diff] [blame] | 270 | r->default_ctrl = r_l3->default_ctrl; |
Thomas Gleixner | d3e11b4 | 2017-04-14 13:00:36 +0200 | [diff] [blame] | 271 | r->data_width = (r->cache.cbm_len + 3) / 4; |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 272 | r->alloc_capable = true; |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 273 | /* |
| 274 | * By default, CDP is disabled. CDP can be enabled by mount parameter |
| 275 | * "cdp" during resctrl file system mount time. |
| 276 | */ |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 277 | r->alloc_enabled = false; |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 280 | static int get_cache_id(int cpu, int level) |
| 281 | { |
| 282 | struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu); |
| 283 | int i; |
| 284 | |
| 285 | for (i = 0; i < ci->num_leaves; i++) { |
| 286 | if (ci->info_list[i].level == level) |
| 287 | return ci->info_list[i].id; |
| 288 | } |
| 289 | |
| 290 | return -1; |
| 291 | } |
| 292 | |
Vikas Shivappa | 05b9341 | 2017-04-07 17:33:53 -0700 | [diff] [blame] | 293 | /* |
| 294 | * Map the memory b/w percentage value to delay values |
| 295 | * that can be written to QOS_MSRs. |
| 296 | * There are currently no SKUs which support non linear delay values. |
| 297 | */ |
| 298 | static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r) |
| 299 | { |
| 300 | if (r->membw.delay_linear) |
| 301 | return MAX_MBA_BW - bw; |
| 302 | |
| 303 | pr_warn_once("Non Linear delay-bw map not supported but queried\n"); |
| 304 | return r->default_ctrl; |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r) |
| 309 | { |
| 310 | unsigned int i; |
| 311 | |
| 312 | /* Write the delay values for mba. */ |
| 313 | for (i = m->low; i < m->high; i++) |
| 314 | wrmsrl(r->msr_base + i, delay_bw_map(d->ctrl_val[i], r)); |
| 315 | } |
| 316 | |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 317 | static void |
| 318 | cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r) |
| 319 | { |
| 320 | unsigned int i; |
| 321 | |
| 322 | for (i = m->low; i < m->high; i++) |
| 323 | wrmsrl(r->msr_base + cbm_idx(r, i), d->ctrl_val[i]); |
| 324 | } |
| 325 | |
Vikas Shivappa | edf6fa1 | 2017-07-25 14:14:28 -0700 | [diff] [blame] | 326 | struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r) |
| 327 | { |
| 328 | struct rdt_domain *d; |
| 329 | |
| 330 | list_for_each_entry(d, &r->domains, list) { |
| 331 | /* Find the domain that contains this CPU */ |
| 332 | if (cpumask_test_cpu(cpu, &d->cpu_mask)) |
| 333 | return d; |
| 334 | } |
| 335 | |
| 336 | return NULL; |
| 337 | } |
| 338 | |
Vikas Shivappa | 2545e9f | 2017-04-07 17:33:51 -0700 | [diff] [blame] | 339 | void rdt_ctrl_update(void *arg) |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 340 | { |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 341 | struct msr_param *m = arg; |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 342 | struct rdt_resource *r = m->res; |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 343 | int cpu = smp_processor_id(); |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 344 | struct rdt_domain *d; |
| 345 | |
| 346 | list_for_each_entry(d, &r->domains, list) { |
| 347 | /* Find the domain that contains this CPU */ |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 348 | if (cpumask_test_cpu(cpu, &d->cpu_mask)) { |
| 349 | r->msr_update(d, m, r); |
| 350 | return; |
| 351 | } |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 352 | } |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 353 | pr_warn_once("cpu %d not found in any domain for resource %s\n", |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 354 | cpu, r->name); |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* |
| 358 | * rdt_find_domain - Find a domain in a resource that matches input resource id |
| 359 | * |
| 360 | * Search resource r's domain list to find the resource id. If the resource |
| 361 | * id is found in a domain, return the domain. Otherwise, if requested by |
| 362 | * caller, return the first domain whose id is bigger than the input id. |
| 363 | * The domain list is sorted by id in ascending order. |
| 364 | */ |
| 365 | static struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id, |
| 366 | struct list_head **pos) |
| 367 | { |
| 368 | struct rdt_domain *d; |
| 369 | struct list_head *l; |
| 370 | |
| 371 | if (id < 0) |
| 372 | return ERR_PTR(id); |
| 373 | |
| 374 | list_for_each(l, &r->domains) { |
| 375 | d = list_entry(l, struct rdt_domain, list); |
| 376 | /* When id is found, return its domain. */ |
| 377 | if (id == d->id) |
| 378 | return d; |
| 379 | /* Stop searching when finding id's position in sorted list. */ |
| 380 | if (id < d->id) |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | if (pos) |
| 385 | *pos = l; |
| 386 | |
| 387 | return NULL; |
| 388 | } |
| 389 | |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 390 | static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d) |
| 391 | { |
| 392 | struct msr_param m; |
| 393 | u32 *dc; |
| 394 | int i; |
| 395 | |
| 396 | dc = kmalloc_array(r->num_closid, sizeof(*d->ctrl_val), GFP_KERNEL); |
| 397 | if (!dc) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | d->ctrl_val = dc; |
| 401 | |
| 402 | /* |
| 403 | * Initialize the Control MSRs to having no control. |
| 404 | * For Cache Allocation: Set all bits in cbm |
| 405 | * For Memory Allocation: Set b/w requested to 100 |
| 406 | */ |
| 407 | for (i = 0; i < r->num_closid; i++, dc++) |
| 408 | *dc = r->default_ctrl; |
| 409 | |
| 410 | m.low = 0; |
| 411 | m.high = r->num_closid; |
| 412 | r->msr_update(d, &m, r); |
| 413 | return 0; |
| 414 | } |
| 415 | |
Vikas Shivappa | edf6fa1 | 2017-07-25 14:14:28 -0700 | [diff] [blame] | 416 | static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d) |
| 417 | { |
| 418 | if (is_llc_occupancy_enabled()) { |
| 419 | d->rmid_busy_llc = kcalloc(BITS_TO_LONGS(r->num_rmid), |
| 420 | sizeof(unsigned long), |
| 421 | GFP_KERNEL); |
| 422 | if (!d->rmid_busy_llc) |
| 423 | return -ENOMEM; |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 429 | /* |
| 430 | * domain_add_cpu - Add a cpu to a resource's domain list. |
| 431 | * |
| 432 | * If an existing domain in the resource r's domain list matches the cpu's |
| 433 | * resource id, add the cpu in the domain. |
| 434 | * |
| 435 | * Otherwise, a new domain is allocated and inserted into the right position |
| 436 | * in the domain list sorted by id in ascending order. |
| 437 | * |
| 438 | * The order in the domain list is visible to users when we print entries |
| 439 | * in the schemata file and schemata input is validated to have the same order |
| 440 | * as this list. |
| 441 | */ |
| 442 | static void domain_add_cpu(int cpu, struct rdt_resource *r) |
| 443 | { |
Thomas Gleixner | 0921c54 | 2017-04-14 14:14:31 +0200 | [diff] [blame] | 444 | int id = get_cache_id(cpu, r->cache_level); |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 445 | struct list_head *add_pos = NULL; |
| 446 | struct rdt_domain *d; |
| 447 | |
| 448 | d = rdt_find_domain(r, id, &add_pos); |
| 449 | if (IS_ERR(d)) { |
| 450 | pr_warn("Could't find cache id for cpu %d\n", cpu); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | if (d) { |
| 455 | cpumask_set_cpu(cpu, &d->cpu_mask); |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu)); |
| 460 | if (!d) |
| 461 | return; |
| 462 | |
| 463 | d->id = id; |
| 464 | |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 465 | if (r->alloc_capable && domain_setup_ctrlval(r, d)) { |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 466 | kfree(d); |
| 467 | return; |
| 468 | } |
| 469 | |
Vikas Shivappa | edf6fa1 | 2017-07-25 14:14:28 -0700 | [diff] [blame] | 470 | if (r->mon_capable && domain_setup_mon_state(r, d)) { |
| 471 | kfree(d); |
| 472 | return; |
| 473 | } |
| 474 | |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 475 | cpumask_set_cpu(cpu, &d->cpu_mask); |
| 476 | list_add_tail(&d->list, add_pos); |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void domain_remove_cpu(int cpu, struct rdt_resource *r) |
| 480 | { |
| 481 | int id = get_cache_id(cpu, r->cache_level); |
| 482 | struct rdt_domain *d; |
| 483 | |
| 484 | d = rdt_find_domain(r, id, NULL); |
| 485 | if (IS_ERR_OR_NULL(d)) { |
| 486 | pr_warn("Could't find cache id for cpu %d\n", cpu); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | cpumask_clear_cpu(cpu, &d->cpu_mask); |
| 491 | if (cpumask_empty(&d->cpu_mask)) { |
Vikas Shivappa | 2545e9f | 2017-04-07 17:33:51 -0700 | [diff] [blame] | 492 | kfree(d->ctrl_val); |
Vikas Shivappa | edf6fa1 | 2017-07-25 14:14:28 -0700 | [diff] [blame] | 493 | kfree(d->rmid_busy_llc); |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 494 | list_del(&d->list); |
| 495 | kfree(d); |
| 496 | } |
| 497 | } |
| 498 | |
Tony Luck | 12e0110 | 2016-10-28 15:04:45 -0700 | [diff] [blame] | 499 | static void clear_closid(int cpu) |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 500 | { |
| 501 | struct intel_pqr_state *state = this_cpu_ptr(&pqr_state); |
Tony Luck | 12e0110 | 2016-10-28 15:04:45 -0700 | [diff] [blame] | 502 | |
Vikas Shivappa | b09d981 | 2017-07-25 14:14:35 -0700 | [diff] [blame] | 503 | per_cpu(rdt_cpu_default.closid, cpu) = 0; |
Tony Luck | 12e0110 | 2016-10-28 15:04:45 -0700 | [diff] [blame] | 504 | state->closid = 0; |
Vikas Shivappa | 0583020 | 2017-07-25 14:14:23 -0700 | [diff] [blame] | 505 | wrmsr(IA32_PQR_ASSOC, state->rmid, 0); |
Tony Luck | 12e0110 | 2016-10-28 15:04:45 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static int intel_rdt_online_cpu(unsigned int cpu) |
| 509 | { |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 510 | struct rdt_resource *r; |
| 511 | |
| 512 | mutex_lock(&rdtgroup_mutex); |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 513 | for_each_alloc_capable_rdt_resource(r) |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 514 | domain_add_cpu(cpu, r); |
Tony Luck | 12e0110 | 2016-10-28 15:04:45 -0700 | [diff] [blame] | 515 | /* The cpu is set in default rdtgroup after online. */ |
| 516 | cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask); |
| 517 | clear_closid(cpu); |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 518 | mutex_unlock(&rdtgroup_mutex); |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static int intel_rdt_offline_cpu(unsigned int cpu) |
Fenghua Yu | 78e99b4 | 2016-10-22 06:19:53 -0700 | [diff] [blame] | 524 | { |
Tony Luck | 12e0110 | 2016-10-28 15:04:45 -0700 | [diff] [blame] | 525 | struct rdtgroup *rdtgrp; |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 526 | struct rdt_resource *r; |
| 527 | |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 528 | mutex_lock(&rdtgroup_mutex); |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 529 | for_each_alloc_capable_rdt_resource(r) |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 530 | domain_remove_cpu(cpu, r); |
Tony Luck | 12e0110 | 2016-10-28 15:04:45 -0700 | [diff] [blame] | 531 | list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) { |
| 532 | if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) |
| 533 | break; |
| 534 | } |
| 535 | clear_closid(cpu); |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 536 | mutex_unlock(&rdtgroup_mutex); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
Thomas Gleixner | 70a1ee9 | 2017-04-14 14:07:47 +0200 | [diff] [blame] | 541 | /* |
| 542 | * Choose a width for the resource name and resource data based on the |
| 543 | * resource that has widest name and cbm. |
| 544 | */ |
| 545 | static __init void rdt_init_padding(void) |
| 546 | { |
| 547 | struct rdt_resource *r; |
| 548 | int cl; |
| 549 | |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 550 | for_each_alloc_capable_rdt_resource(r) { |
Thomas Gleixner | 70a1ee9 | 2017-04-14 14:07:47 +0200 | [diff] [blame] | 551 | cl = strlen(r->name); |
| 552 | if (cl > max_name_width) |
| 553 | max_name_width = cl; |
| 554 | |
| 555 | if (r->data_width > max_data_width) |
| 556 | max_data_width = r->data_width; |
| 557 | } |
| 558 | } |
| 559 | |
Vikas Shivappa | 6a445ed | 2017-07-25 14:14:27 -0700 | [diff] [blame] | 560 | static __init bool get_rdt_alloc_resources(void) |
Thomas Gleixner | 70a1ee9 | 2017-04-14 14:07:47 +0200 | [diff] [blame] | 561 | { |
| 562 | bool ret = false; |
| 563 | |
| 564 | if (cache_alloc_hsw_probe()) |
| 565 | return true; |
| 566 | |
| 567 | if (!boot_cpu_has(X86_FEATURE_RDT_A)) |
| 568 | return false; |
| 569 | |
| 570 | if (boot_cpu_has(X86_FEATURE_CAT_L3)) { |
Vikas Shivappa | 6a445ed | 2017-07-25 14:14:27 -0700 | [diff] [blame] | 571 | rdt_get_cache_alloc_cfg(1, &rdt_resources_all[RDT_RESOURCE_L3]); |
Thomas Gleixner | 70a1ee9 | 2017-04-14 14:07:47 +0200 | [diff] [blame] | 572 | if (boot_cpu_has(X86_FEATURE_CDP_L3)) { |
| 573 | rdt_get_cdp_l3_config(RDT_RESOURCE_L3DATA); |
| 574 | rdt_get_cdp_l3_config(RDT_RESOURCE_L3CODE); |
| 575 | } |
| 576 | ret = true; |
| 577 | } |
| 578 | if (boot_cpu_has(X86_FEATURE_CAT_L2)) { |
| 579 | /* CPUID 0x10.2 fields are same format at 0x10.1 */ |
Vikas Shivappa | 6a445ed | 2017-07-25 14:14:27 -0700 | [diff] [blame] | 580 | rdt_get_cache_alloc_cfg(2, &rdt_resources_all[RDT_RESOURCE_L2]); |
Thomas Gleixner | 70a1ee9 | 2017-04-14 14:07:47 +0200 | [diff] [blame] | 581 | ret = true; |
| 582 | } |
Vikas Shivappa | ab66a33 | 2017-04-07 17:33:52 -0700 | [diff] [blame] | 583 | |
Vikas Shivappa | 05b9341 | 2017-04-07 17:33:53 -0700 | [diff] [blame] | 584 | if (boot_cpu_has(X86_FEATURE_MBA)) { |
| 585 | if (rdt_get_mem_config(&rdt_resources_all[RDT_RESOURCE_MBA])) |
| 586 | ret = true; |
| 587 | } |
Thomas Gleixner | 70a1ee9 | 2017-04-14 14:07:47 +0200 | [diff] [blame] | 588 | return ret; |
| 589 | } |
| 590 | |
Vikas Shivappa | 6a445ed | 2017-07-25 14:14:27 -0700 | [diff] [blame] | 591 | static __init bool get_rdt_mon_resources(void) |
| 592 | { |
| 593 | if (boot_cpu_has(X86_FEATURE_CQM_OCCUP_LLC)) |
| 594 | rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID); |
| 595 | if (boot_cpu_has(X86_FEATURE_CQM_MBM_TOTAL)) |
| 596 | rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID); |
| 597 | if (boot_cpu_has(X86_FEATURE_CQM_MBM_LOCAL)) |
| 598 | rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID); |
| 599 | |
| 600 | if (!rdt_mon_features) |
| 601 | return false; |
| 602 | |
| 603 | return !rdt_get_mon_l3_config(&rdt_resources_all[RDT_RESOURCE_L3]); |
| 604 | } |
| 605 | |
| 606 | static __init bool get_rdt_resources(void) |
| 607 | { |
| 608 | rdt_alloc_capable = get_rdt_alloc_resources(); |
| 609 | rdt_mon_capable = get_rdt_mon_resources(); |
| 610 | |
| 611 | return (rdt_mon_capable || rdt_alloc_capable); |
| 612 | } |
| 613 | |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 614 | static int __init intel_rdt_late_init(void) |
| 615 | { |
| 616 | struct rdt_resource *r; |
Fenghua Yu | 5ff193f | 2016-10-28 15:04:42 -0700 | [diff] [blame] | 617 | int state, ret; |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 618 | |
Fenghua Yu | 78e99b4 | 2016-10-22 06:19:53 -0700 | [diff] [blame] | 619 | if (!get_rdt_resources()) |
| 620 | return -ENODEV; |
| 621 | |
Thomas Gleixner | 06b57e4 | 2017-04-14 14:06:26 +0200 | [diff] [blame] | 622 | rdt_init_padding(); |
| 623 | |
Tony Luck | 2264d9c | 2016-10-28 15:04:41 -0700 | [diff] [blame] | 624 | state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, |
| 625 | "x86/rdt/cat:online:", |
| 626 | intel_rdt_online_cpu, intel_rdt_offline_cpu); |
| 627 | if (state < 0) |
| 628 | return state; |
| 629 | |
Fenghua Yu | 5ff193f | 2016-10-28 15:04:42 -0700 | [diff] [blame] | 630 | ret = rdtgroup_init(); |
| 631 | if (ret) { |
| 632 | cpuhp_remove_state(state); |
| 633 | return ret; |
| 634 | } |
| 635 | |
Vikas Shivappa | 1b5c0b7 | 2017-07-25 14:14:25 -0700 | [diff] [blame] | 636 | for_each_alloc_capable_rdt_resource(r) |
Fenghua Yu | c1c7c3f | 2016-10-22 06:19:55 -0700 | [diff] [blame] | 637 | pr_info("Intel RDT %s allocation detected\n", r->name); |
Fenghua Yu | 78e99b4 | 2016-10-22 06:19:53 -0700 | [diff] [blame] | 638 | |
Vikas Shivappa | 6a445ed | 2017-07-25 14:14:27 -0700 | [diff] [blame] | 639 | for_each_mon_capable_rdt_resource(r) |
| 640 | pr_info("Intel RDT %s monitoring detected\n", r->name); |
| 641 | |
Fenghua Yu | 78e99b4 | 2016-10-22 06:19:53 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | late_initcall(intel_rdt_late_init); |