intel_rdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/slab.h>
  26. #include <linux/err.h>
  27. #include <linux/cacheinfo.h>
  28. #include <linux/cpuhotplug.h>
  29. #include <asm/intel-family.h>
  30. #include <asm/intel_rdt.h>
  31. #define MAX_MBA_BW 100u
  32. #define MBA_IS_LINEAR 0x4
  33. /* Mutex to protect rdtgroup access. */
  34. DEFINE_MUTEX(rdtgroup_mutex);
  35. DEFINE_PER_CPU_READ_MOSTLY(int, cpu_closid);
  36. /*
  37. * Used to store the max resource name width and max resource data width
  38. * to display the schemata in a tabular format
  39. */
  40. int max_name_width, max_data_width;
  41. static void
  42. mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
  43. static void
  44. cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
  45. #define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains)
  46. struct rdt_resource rdt_resources_all[] = {
  47. {
  48. .name = "L3",
  49. .domains = domain_init(RDT_RESOURCE_L3),
  50. .msr_base = IA32_L3_CBM_BASE,
  51. .msr_update = cat_wrmsr,
  52. .cache_level = 3,
  53. .cache = {
  54. .min_cbm_bits = 1,
  55. .cbm_idx_mult = 1,
  56. .cbm_idx_offset = 0,
  57. },
  58. .parse_ctrlval = parse_cbm,
  59. .format_str = "%d=%0*x",
  60. },
  61. {
  62. .name = "L3DATA",
  63. .domains = domain_init(RDT_RESOURCE_L3DATA),
  64. .msr_base = IA32_L3_CBM_BASE,
  65. .msr_update = cat_wrmsr,
  66. .cache_level = 3,
  67. .cache = {
  68. .min_cbm_bits = 1,
  69. .cbm_idx_mult = 2,
  70. .cbm_idx_offset = 0,
  71. },
  72. .parse_ctrlval = parse_cbm,
  73. .format_str = "%d=%0*x",
  74. },
  75. {
  76. .name = "L3CODE",
  77. .domains = domain_init(RDT_RESOURCE_L3CODE),
  78. .msr_base = IA32_L3_CBM_BASE,
  79. .msr_update = cat_wrmsr,
  80. .cache_level = 3,
  81. .cache = {
  82. .min_cbm_bits = 1,
  83. .cbm_idx_mult = 2,
  84. .cbm_idx_offset = 1,
  85. },
  86. .parse_ctrlval = parse_cbm,
  87. .format_str = "%d=%0*x",
  88. },
  89. {
  90. .name = "L2",
  91. .domains = domain_init(RDT_RESOURCE_L2),
  92. .msr_base = IA32_L2_CBM_BASE,
  93. .msr_update = cat_wrmsr,
  94. .cache_level = 2,
  95. .cache = {
  96. .min_cbm_bits = 1,
  97. .cbm_idx_mult = 1,
  98. .cbm_idx_offset = 0,
  99. },
  100. .parse_ctrlval = parse_cbm,
  101. .format_str = "%d=%0*x",
  102. },
  103. {
  104. .name = "MB",
  105. .domains = domain_init(RDT_RESOURCE_MBA),
  106. .msr_base = IA32_MBA_THRTL_BASE,
  107. .msr_update = mba_wrmsr,
  108. .cache_level = 3,
  109. .parse_ctrlval = parse_bw,
  110. .format_str = "%d=%*d",
  111. },
  112. };
  113. static unsigned int cbm_idx(struct rdt_resource *r, unsigned int closid)
  114. {
  115. return closid * r->cache.cbm_idx_mult + r->cache.cbm_idx_offset;
  116. }
  117. /*
  118. * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
  119. * as they do not have CPUID enumeration support for Cache allocation.
  120. * The check for Vendor/Family/Model is not enough to guarantee that
  121. * the MSRs won't #GP fault because only the following SKUs support
  122. * CAT:
  123. * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz
  124. * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz
  125. * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz
  126. * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz
  127. * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz
  128. * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz
  129. *
  130. * Probe by trying to write the first of the L3 cach mask registers
  131. * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
  132. * is always 20 on hsw server parts. The minimum cache bitmask length
  133. * allowed for HSW server is always 2 bits. Hardcode all of them.
  134. */
  135. static inline bool cache_alloc_hsw_probe(void)
  136. {
  137. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  138. boot_cpu_data.x86 == 6 &&
  139. boot_cpu_data.x86_model == INTEL_FAM6_HASWELL_X) {
  140. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
  141. u32 l, h, max_cbm = BIT_MASK(20) - 1;
  142. if (wrmsr_safe(IA32_L3_CBM_BASE, max_cbm, 0))
  143. return false;
  144. rdmsr(IA32_L3_CBM_BASE, l, h);
  145. /* If all the bits were set in MSR, return success */
  146. if (l != max_cbm)
  147. return false;
  148. r->num_closid = 4;
  149. r->default_ctrl = max_cbm;
  150. r->cache.cbm_len = 20;
  151. r->cache.min_cbm_bits = 2;
  152. r->capable = true;
  153. r->enabled = true;
  154. return true;
  155. }
  156. return false;
  157. }
  158. /*
  159. * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
  160. * exposed to user interface and the h/w understandable delay values.
  161. *
  162. * The non-linear delay values have the granularity of power of two
  163. * and also the h/w does not guarantee a curve for configured delay
  164. * values vs. actual b/w enforced.
  165. * Hence we need a mapping that is pre calibrated so the user can
  166. * express the memory b/w as a percentage value.
  167. */
  168. static inline bool rdt_get_mb_table(struct rdt_resource *r)
  169. {
  170. /*
  171. * There are no Intel SKUs as of now to support non-linear delay.
  172. */
  173. pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
  174. boot_cpu_data.x86, boot_cpu_data.x86_model);
  175. return false;
  176. }
  177. static bool rdt_get_mem_config(struct rdt_resource *r)
  178. {
  179. union cpuid_0x10_3_eax eax;
  180. union cpuid_0x10_x_edx edx;
  181. u32 ebx, ecx;
  182. cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
  183. r->num_closid = edx.split.cos_max + 1;
  184. r->membw.max_delay = eax.split.max_delay + 1;
  185. r->default_ctrl = MAX_MBA_BW;
  186. if (ecx & MBA_IS_LINEAR) {
  187. r->membw.delay_linear = true;
  188. r->membw.min_bw = MAX_MBA_BW - r->membw.max_delay;
  189. r->membw.bw_gran = MAX_MBA_BW - r->membw.max_delay;
  190. } else {
  191. if (!rdt_get_mb_table(r))
  192. return false;
  193. }
  194. r->data_width = 3;
  195. rdt_get_mba_infofile(r);
  196. r->capable = true;
  197. r->enabled = true;
  198. return true;
  199. }
  200. static void rdt_get_cache_config(int idx, struct rdt_resource *r)
  201. {
  202. union cpuid_0x10_1_eax eax;
  203. union cpuid_0x10_x_edx edx;
  204. u32 ebx, ecx;
  205. cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
  206. r->num_closid = edx.split.cos_max + 1;
  207. r->cache.cbm_len = eax.split.cbm_len + 1;
  208. r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
  209. r->data_width = (r->cache.cbm_len + 3) / 4;
  210. rdt_get_cache_infofile(r);
  211. r->capable = true;
  212. r->enabled = true;
  213. }
  214. static void rdt_get_cdp_l3_config(int type)
  215. {
  216. struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
  217. struct rdt_resource *r = &rdt_resources_all[type];
  218. r->num_closid = r_l3->num_closid / 2;
  219. r->cache.cbm_len = r_l3->cache.cbm_len;
  220. r->default_ctrl = r_l3->default_ctrl;
  221. r->data_width = (r->cache.cbm_len + 3) / 4;
  222. r->capable = true;
  223. /*
  224. * By default, CDP is disabled. CDP can be enabled by mount parameter
  225. * "cdp" during resctrl file system mount time.
  226. */
  227. r->enabled = false;
  228. }
  229. static int get_cache_id(int cpu, int level)
  230. {
  231. struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
  232. int i;
  233. for (i = 0; i < ci->num_leaves; i++) {
  234. if (ci->info_list[i].level == level)
  235. return ci->info_list[i].id;
  236. }
  237. return -1;
  238. }
  239. /*
  240. * Map the memory b/w percentage value to delay values
  241. * that can be written to QOS_MSRs.
  242. * There are currently no SKUs which support non linear delay values.
  243. */
  244. static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
  245. {
  246. if (r->membw.delay_linear)
  247. return MAX_MBA_BW - bw;
  248. pr_warn_once("Non Linear delay-bw map not supported but queried\n");
  249. return r->default_ctrl;
  250. }
  251. static void
  252. mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
  253. {
  254. unsigned int i;
  255. /* Write the delay values for mba. */
  256. for (i = m->low; i < m->high; i++)
  257. wrmsrl(r->msr_base + i, delay_bw_map(d->ctrl_val[i], r));
  258. }
  259. static void
  260. cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
  261. {
  262. unsigned int i;
  263. for (i = m->low; i < m->high; i++)
  264. wrmsrl(r->msr_base + cbm_idx(r, i), d->ctrl_val[i]);
  265. }
  266. void rdt_ctrl_update(void *arg)
  267. {
  268. struct msr_param *m = arg;
  269. struct rdt_resource *r = m->res;
  270. int cpu = smp_processor_id();
  271. struct rdt_domain *d;
  272. list_for_each_entry(d, &r->domains, list) {
  273. /* Find the domain that contains this CPU */
  274. if (cpumask_test_cpu(cpu, &d->cpu_mask)) {
  275. r->msr_update(d, m, r);
  276. return;
  277. }
  278. }
  279. pr_warn_once("cpu %d not found in any domain for resource %s\n",
  280. cpu, r->name);
  281. }
  282. /*
  283. * rdt_find_domain - Find a domain in a resource that matches input resource id
  284. *
  285. * Search resource r's domain list to find the resource id. If the resource
  286. * id is found in a domain, return the domain. Otherwise, if requested by
  287. * caller, return the first domain whose id is bigger than the input id.
  288. * The domain list is sorted by id in ascending order.
  289. */
  290. static struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
  291. struct list_head **pos)
  292. {
  293. struct rdt_domain *d;
  294. struct list_head *l;
  295. if (id < 0)
  296. return ERR_PTR(id);
  297. list_for_each(l, &r->domains) {
  298. d = list_entry(l, struct rdt_domain, list);
  299. /* When id is found, return its domain. */
  300. if (id == d->id)
  301. return d;
  302. /* Stop searching when finding id's position in sorted list. */
  303. if (id < d->id)
  304. break;
  305. }
  306. if (pos)
  307. *pos = l;
  308. return NULL;
  309. }
  310. static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
  311. {
  312. struct msr_param m;
  313. u32 *dc;
  314. int i;
  315. dc = kmalloc_array(r->num_closid, sizeof(*d->ctrl_val), GFP_KERNEL);
  316. if (!dc)
  317. return -ENOMEM;
  318. d->ctrl_val = dc;
  319. /*
  320. * Initialize the Control MSRs to having no control.
  321. * For Cache Allocation: Set all bits in cbm
  322. * For Memory Allocation: Set b/w requested to 100
  323. */
  324. for (i = 0; i < r->num_closid; i++, dc++)
  325. *dc = r->default_ctrl;
  326. m.low = 0;
  327. m.high = r->num_closid;
  328. r->msr_update(d, &m, r);
  329. return 0;
  330. }
  331. /*
  332. * domain_add_cpu - Add a cpu to a resource's domain list.
  333. *
  334. * If an existing domain in the resource r's domain list matches the cpu's
  335. * resource id, add the cpu in the domain.
  336. *
  337. * Otherwise, a new domain is allocated and inserted into the right position
  338. * in the domain list sorted by id in ascending order.
  339. *
  340. * The order in the domain list is visible to users when we print entries
  341. * in the schemata file and schemata input is validated to have the same order
  342. * as this list.
  343. */
  344. static void domain_add_cpu(int cpu, struct rdt_resource *r)
  345. {
  346. int id = get_cache_id(cpu, r->cache_level);
  347. struct list_head *add_pos = NULL;
  348. struct rdt_domain *d;
  349. d = rdt_find_domain(r, id, &add_pos);
  350. if (IS_ERR(d)) {
  351. pr_warn("Could't find cache id for cpu %d\n", cpu);
  352. return;
  353. }
  354. if (d) {
  355. cpumask_set_cpu(cpu, &d->cpu_mask);
  356. return;
  357. }
  358. d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
  359. if (!d)
  360. return;
  361. d->id = id;
  362. if (domain_setup_ctrlval(r, d)) {
  363. kfree(d);
  364. return;
  365. }
  366. cpumask_set_cpu(cpu, &d->cpu_mask);
  367. list_add_tail(&d->list, add_pos);
  368. }
  369. static void domain_remove_cpu(int cpu, struct rdt_resource *r)
  370. {
  371. int id = get_cache_id(cpu, r->cache_level);
  372. struct rdt_domain *d;
  373. d = rdt_find_domain(r, id, NULL);
  374. if (IS_ERR_OR_NULL(d)) {
  375. pr_warn("Could't find cache id for cpu %d\n", cpu);
  376. return;
  377. }
  378. cpumask_clear_cpu(cpu, &d->cpu_mask);
  379. if (cpumask_empty(&d->cpu_mask)) {
  380. kfree(d->ctrl_val);
  381. list_del(&d->list);
  382. kfree(d);
  383. }
  384. }
  385. static void clear_closid(int cpu)
  386. {
  387. struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
  388. per_cpu(cpu_closid, cpu) = 0;
  389. state->closid = 0;
  390. wrmsr(MSR_IA32_PQR_ASSOC, state->rmid, 0);
  391. }
  392. static int intel_rdt_online_cpu(unsigned int cpu)
  393. {
  394. struct rdt_resource *r;
  395. mutex_lock(&rdtgroup_mutex);
  396. for_each_capable_rdt_resource(r)
  397. domain_add_cpu(cpu, r);
  398. /* The cpu is set in default rdtgroup after online. */
  399. cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
  400. clear_closid(cpu);
  401. mutex_unlock(&rdtgroup_mutex);
  402. return 0;
  403. }
  404. static int intel_rdt_offline_cpu(unsigned int cpu)
  405. {
  406. struct rdtgroup *rdtgrp;
  407. struct rdt_resource *r;
  408. mutex_lock(&rdtgroup_mutex);
  409. for_each_capable_rdt_resource(r)
  410. domain_remove_cpu(cpu, r);
  411. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  412. if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask))
  413. break;
  414. }
  415. clear_closid(cpu);
  416. mutex_unlock(&rdtgroup_mutex);
  417. return 0;
  418. }
  419. /*
  420. * Choose a width for the resource name and resource data based on the
  421. * resource that has widest name and cbm.
  422. */
  423. static __init void rdt_init_padding(void)
  424. {
  425. struct rdt_resource *r;
  426. int cl;
  427. for_each_capable_rdt_resource(r) {
  428. cl = strlen(r->name);
  429. if (cl > max_name_width)
  430. max_name_width = cl;
  431. if (r->data_width > max_data_width)
  432. max_data_width = r->data_width;
  433. }
  434. }
  435. static __init bool get_rdt_resources(void)
  436. {
  437. bool ret = false;
  438. if (cache_alloc_hsw_probe())
  439. return true;
  440. if (!boot_cpu_has(X86_FEATURE_RDT_A))
  441. return false;
  442. if (boot_cpu_has(X86_FEATURE_CAT_L3)) {
  443. rdt_get_cache_config(1, &rdt_resources_all[RDT_RESOURCE_L3]);
  444. if (boot_cpu_has(X86_FEATURE_CDP_L3)) {
  445. rdt_get_cdp_l3_config(RDT_RESOURCE_L3DATA);
  446. rdt_get_cdp_l3_config(RDT_RESOURCE_L3CODE);
  447. }
  448. ret = true;
  449. }
  450. if (boot_cpu_has(X86_FEATURE_CAT_L2)) {
  451. /* CPUID 0x10.2 fields are same format at 0x10.1 */
  452. rdt_get_cache_config(2, &rdt_resources_all[RDT_RESOURCE_L2]);
  453. ret = true;
  454. }
  455. if (boot_cpu_has(X86_FEATURE_MBA)) {
  456. if (rdt_get_mem_config(&rdt_resources_all[RDT_RESOURCE_MBA]))
  457. ret = true;
  458. }
  459. return ret;
  460. }
  461. static int __init intel_rdt_late_init(void)
  462. {
  463. struct rdt_resource *r;
  464. int state, ret;
  465. if (!get_rdt_resources())
  466. return -ENODEV;
  467. rdt_init_padding();
  468. state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  469. "x86/rdt/cat:online:",
  470. intel_rdt_online_cpu, intel_rdt_offline_cpu);
  471. if (state < 0)
  472. return state;
  473. ret = rdtgroup_init();
  474. if (ret) {
  475. cpuhp_remove_state(state);
  476. return ret;
  477. }
  478. for_each_capable_rdt_resource(r)
  479. pr_info("Intel RDT %s allocation detected\n", r->name);
  480. return 0;
  481. }
  482. late_initcall(intel_rdt_late_init);