intel_rdt.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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_sched.h>
  31. #include "intel_rdt.h"
  32. #define MAX_MBA_BW 100u
  33. #define MBA_IS_LINEAR 0x4
  34. /* Mutex to protect rdtgroup access. */
  35. DEFINE_MUTEX(rdtgroup_mutex);
  36. /*
  37. * The cached intel_pqr_state is strictly per CPU and can never be
  38. * updated from a remote CPU. Functions which modify the state
  39. * are called with interrupts disabled and no preemption, which
  40. * is sufficient for the protection.
  41. */
  42. DEFINE_PER_CPU(struct intel_pqr_state, pqr_state);
  43. /*
  44. * Used to store the max resource name width and max resource data width
  45. * to display the schemata in a tabular format
  46. */
  47. int max_name_width, max_data_width;
  48. /*
  49. * Global boolean for rdt_alloc which is true if any
  50. * resource allocation is enabled.
  51. */
  52. bool rdt_alloc_capable;
  53. static void
  54. mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
  55. static void
  56. cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
  57. #define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains)
  58. struct rdt_resource rdt_resources_all[] = {
  59. [RDT_RESOURCE_L3] =
  60. {
  61. .rid = RDT_RESOURCE_L3,
  62. .name = "L3",
  63. .domains = domain_init(RDT_RESOURCE_L3),
  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 = 1,
  70. .cbm_idx_offset = 0,
  71. },
  72. .parse_ctrlval = parse_cbm,
  73. .format_str = "%d=%0*x",
  74. .fflags = RFTYPE_RES_CACHE,
  75. },
  76. [RDT_RESOURCE_L3DATA] =
  77. {
  78. .rid = RDT_RESOURCE_L3DATA,
  79. .name = "L3DATA",
  80. .domains = domain_init(RDT_RESOURCE_L3DATA),
  81. .msr_base = IA32_L3_CBM_BASE,
  82. .msr_update = cat_wrmsr,
  83. .cache_level = 3,
  84. .cache = {
  85. .min_cbm_bits = 1,
  86. .cbm_idx_mult = 2,
  87. .cbm_idx_offset = 0,
  88. },
  89. .parse_ctrlval = parse_cbm,
  90. .format_str = "%d=%0*x",
  91. .fflags = RFTYPE_RES_CACHE,
  92. },
  93. [RDT_RESOURCE_L3CODE] =
  94. {
  95. .rid = RDT_RESOURCE_L3CODE,
  96. .name = "L3CODE",
  97. .domains = domain_init(RDT_RESOURCE_L3CODE),
  98. .msr_base = IA32_L3_CBM_BASE,
  99. .msr_update = cat_wrmsr,
  100. .cache_level = 3,
  101. .cache = {
  102. .min_cbm_bits = 1,
  103. .cbm_idx_mult = 2,
  104. .cbm_idx_offset = 1,
  105. },
  106. .parse_ctrlval = parse_cbm,
  107. .format_str = "%d=%0*x",
  108. .fflags = RFTYPE_RES_CACHE,
  109. },
  110. [RDT_RESOURCE_L2] =
  111. {
  112. .rid = RDT_RESOURCE_L2,
  113. .name = "L2",
  114. .domains = domain_init(RDT_RESOURCE_L2),
  115. .msr_base = IA32_L2_CBM_BASE,
  116. .msr_update = cat_wrmsr,
  117. .cache_level = 2,
  118. .cache = {
  119. .min_cbm_bits = 1,
  120. .cbm_idx_mult = 1,
  121. .cbm_idx_offset = 0,
  122. },
  123. .parse_ctrlval = parse_cbm,
  124. .format_str = "%d=%0*x",
  125. .fflags = RFTYPE_RES_CACHE,
  126. },
  127. [RDT_RESOURCE_L2DATA] =
  128. {
  129. .rid = RDT_RESOURCE_L2DATA,
  130. .name = "L2DATA",
  131. .domains = domain_init(RDT_RESOURCE_L2DATA),
  132. .msr_base = IA32_L2_CBM_BASE,
  133. .msr_update = cat_wrmsr,
  134. .cache_level = 2,
  135. .cache = {
  136. .min_cbm_bits = 1,
  137. .cbm_idx_mult = 2,
  138. .cbm_idx_offset = 0,
  139. },
  140. .parse_ctrlval = parse_cbm,
  141. .format_str = "%d=%0*x",
  142. .fflags = RFTYPE_RES_CACHE,
  143. },
  144. [RDT_RESOURCE_L2CODE] =
  145. {
  146. .rid = RDT_RESOURCE_L2CODE,
  147. .name = "L2CODE",
  148. .domains = domain_init(RDT_RESOURCE_L2CODE),
  149. .msr_base = IA32_L2_CBM_BASE,
  150. .msr_update = cat_wrmsr,
  151. .cache_level = 2,
  152. .cache = {
  153. .min_cbm_bits = 1,
  154. .cbm_idx_mult = 2,
  155. .cbm_idx_offset = 1,
  156. },
  157. .parse_ctrlval = parse_cbm,
  158. .format_str = "%d=%0*x",
  159. .fflags = RFTYPE_RES_CACHE,
  160. },
  161. [RDT_RESOURCE_MBA] =
  162. {
  163. .rid = RDT_RESOURCE_MBA,
  164. .name = "MB",
  165. .domains = domain_init(RDT_RESOURCE_MBA),
  166. .msr_base = IA32_MBA_THRTL_BASE,
  167. .msr_update = mba_wrmsr,
  168. .cache_level = 3,
  169. .parse_ctrlval = parse_bw,
  170. .format_str = "%d=%*d",
  171. .fflags = RFTYPE_RES_MB,
  172. },
  173. };
  174. static unsigned int cbm_idx(struct rdt_resource *r, unsigned int closid)
  175. {
  176. return closid * r->cache.cbm_idx_mult + r->cache.cbm_idx_offset;
  177. }
  178. /*
  179. * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
  180. * as they do not have CPUID enumeration support for Cache allocation.
  181. * The check for Vendor/Family/Model is not enough to guarantee that
  182. * the MSRs won't #GP fault because only the following SKUs support
  183. * CAT:
  184. * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz
  185. * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz
  186. * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz
  187. * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz
  188. * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz
  189. * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz
  190. *
  191. * Probe by trying to write the first of the L3 cach mask registers
  192. * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
  193. * is always 20 on hsw server parts. The minimum cache bitmask length
  194. * allowed for HSW server is always 2 bits. Hardcode all of them.
  195. */
  196. static inline void cache_alloc_hsw_probe(void)
  197. {
  198. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
  199. u32 l, h, max_cbm = BIT_MASK(20) - 1;
  200. if (wrmsr_safe(IA32_L3_CBM_BASE, max_cbm, 0))
  201. return;
  202. rdmsr(IA32_L3_CBM_BASE, l, h);
  203. /* If all the bits were set in MSR, return success */
  204. if (l != max_cbm)
  205. return;
  206. r->num_closid = 4;
  207. r->default_ctrl = max_cbm;
  208. r->cache.cbm_len = 20;
  209. r->cache.shareable_bits = 0xc0000;
  210. r->cache.min_cbm_bits = 2;
  211. r->alloc_capable = true;
  212. r->alloc_enabled = true;
  213. rdt_alloc_capable = true;
  214. }
  215. /*
  216. * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
  217. * exposed to user interface and the h/w understandable delay values.
  218. *
  219. * The non-linear delay values have the granularity of power of two
  220. * and also the h/w does not guarantee a curve for configured delay
  221. * values vs. actual b/w enforced.
  222. * Hence we need a mapping that is pre calibrated so the user can
  223. * express the memory b/w as a percentage value.
  224. */
  225. static inline bool rdt_get_mb_table(struct rdt_resource *r)
  226. {
  227. /*
  228. * There are no Intel SKUs as of now to support non-linear delay.
  229. */
  230. pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
  231. boot_cpu_data.x86, boot_cpu_data.x86_model);
  232. return false;
  233. }
  234. static bool rdt_get_mem_config(struct rdt_resource *r)
  235. {
  236. union cpuid_0x10_3_eax eax;
  237. union cpuid_0x10_x_edx edx;
  238. u32 ebx, ecx;
  239. cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
  240. r->num_closid = edx.split.cos_max + 1;
  241. r->membw.max_delay = eax.split.max_delay + 1;
  242. r->default_ctrl = MAX_MBA_BW;
  243. if (ecx & MBA_IS_LINEAR) {
  244. r->membw.delay_linear = true;
  245. r->membw.min_bw = MAX_MBA_BW - r->membw.max_delay;
  246. r->membw.bw_gran = MAX_MBA_BW - r->membw.max_delay;
  247. } else {
  248. if (!rdt_get_mb_table(r))
  249. return false;
  250. }
  251. r->data_width = 3;
  252. r->alloc_capable = true;
  253. r->alloc_enabled = true;
  254. return true;
  255. }
  256. static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
  257. {
  258. union cpuid_0x10_1_eax eax;
  259. union cpuid_0x10_x_edx edx;
  260. u32 ebx, ecx;
  261. cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
  262. r->num_closid = edx.split.cos_max + 1;
  263. r->cache.cbm_len = eax.split.cbm_len + 1;
  264. r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
  265. r->cache.shareable_bits = ebx & r->default_ctrl;
  266. r->data_width = (r->cache.cbm_len + 3) / 4;
  267. r->alloc_capable = true;
  268. r->alloc_enabled = true;
  269. }
  270. static void rdt_get_cdp_config(int level, int type)
  271. {
  272. struct rdt_resource *r_l = &rdt_resources_all[level];
  273. struct rdt_resource *r = &rdt_resources_all[type];
  274. r->num_closid = r_l->num_closid / 2;
  275. r->cache.cbm_len = r_l->cache.cbm_len;
  276. r->default_ctrl = r_l->default_ctrl;
  277. r->cache.shareable_bits = r_l->cache.shareable_bits;
  278. r->data_width = (r->cache.cbm_len + 3) / 4;
  279. r->alloc_capable = true;
  280. /*
  281. * By default, CDP is disabled. CDP can be enabled by mount parameter
  282. * "cdp" during resctrl file system mount time.
  283. */
  284. r->alloc_enabled = false;
  285. }
  286. static void rdt_get_cdp_l3_config(void)
  287. {
  288. rdt_get_cdp_config(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA);
  289. rdt_get_cdp_config(RDT_RESOURCE_L3, RDT_RESOURCE_L3CODE);
  290. }
  291. static void rdt_get_cdp_l2_config(void)
  292. {
  293. rdt_get_cdp_config(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA);
  294. rdt_get_cdp_config(RDT_RESOURCE_L2, RDT_RESOURCE_L2CODE);
  295. }
  296. static int get_cache_id(int cpu, int level)
  297. {
  298. struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
  299. int i;
  300. for (i = 0; i < ci->num_leaves; i++) {
  301. if (ci->info_list[i].level == level)
  302. return ci->info_list[i].id;
  303. }
  304. return -1;
  305. }
  306. /*
  307. * Map the memory b/w percentage value to delay values
  308. * that can be written to QOS_MSRs.
  309. * There are currently no SKUs which support non linear delay values.
  310. */
  311. static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
  312. {
  313. if (r->membw.delay_linear)
  314. return MAX_MBA_BW - bw;
  315. pr_warn_once("Non Linear delay-bw map not supported but queried\n");
  316. return r->default_ctrl;
  317. }
  318. static void
  319. mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
  320. {
  321. unsigned int i;
  322. /* Write the delay values for mba. */
  323. for (i = m->low; i < m->high; i++)
  324. wrmsrl(r->msr_base + i, delay_bw_map(d->ctrl_val[i], r));
  325. }
  326. static void
  327. cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
  328. {
  329. unsigned int i;
  330. for (i = m->low; i < m->high; i++)
  331. wrmsrl(r->msr_base + cbm_idx(r, i), d->ctrl_val[i]);
  332. }
  333. struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r)
  334. {
  335. struct rdt_domain *d;
  336. list_for_each_entry(d, &r->domains, list) {
  337. /* Find the domain that contains this CPU */
  338. if (cpumask_test_cpu(cpu, &d->cpu_mask))
  339. return d;
  340. }
  341. return NULL;
  342. }
  343. void rdt_ctrl_update(void *arg)
  344. {
  345. struct msr_param *m = arg;
  346. struct rdt_resource *r = m->res;
  347. int cpu = smp_processor_id();
  348. struct rdt_domain *d;
  349. d = get_domain_from_cpu(cpu, r);
  350. if (d) {
  351. r->msr_update(d, m, r);
  352. return;
  353. }
  354. pr_warn_once("cpu %d not found in any domain for resource %s\n",
  355. cpu, r->name);
  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. 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. if (id < 0)
  371. return ERR_PTR(id);
  372. list_for_each(l, &r->domains) {
  373. d = list_entry(l, struct rdt_domain, list);
  374. /* When id is found, return its domain. */
  375. if (id == d->id)
  376. return d;
  377. /* Stop searching when finding id's position in sorted list. */
  378. if (id < d->id)
  379. break;
  380. }
  381. if (pos)
  382. *pos = l;
  383. return NULL;
  384. }
  385. static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
  386. {
  387. struct msr_param m;
  388. u32 *dc;
  389. int i;
  390. dc = kmalloc_array(r->num_closid, sizeof(*d->ctrl_val), GFP_KERNEL);
  391. if (!dc)
  392. return -ENOMEM;
  393. d->ctrl_val = dc;
  394. /*
  395. * Initialize the Control MSRs to having no control.
  396. * For Cache Allocation: Set all bits in cbm
  397. * For Memory Allocation: Set b/w requested to 100
  398. */
  399. for (i = 0; i < r->num_closid; i++, dc++)
  400. *dc = r->default_ctrl;
  401. m.low = 0;
  402. m.high = r->num_closid;
  403. r->msr_update(d, &m, r);
  404. return 0;
  405. }
  406. static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
  407. {
  408. size_t tsize;
  409. if (is_llc_occupancy_enabled()) {
  410. d->rmid_busy_llc = kcalloc(BITS_TO_LONGS(r->num_rmid),
  411. sizeof(unsigned long),
  412. GFP_KERNEL);
  413. if (!d->rmid_busy_llc)
  414. return -ENOMEM;
  415. INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
  416. }
  417. if (is_mbm_total_enabled()) {
  418. tsize = sizeof(*d->mbm_total);
  419. d->mbm_total = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
  420. if (!d->mbm_total) {
  421. kfree(d->rmid_busy_llc);
  422. return -ENOMEM;
  423. }
  424. }
  425. if (is_mbm_local_enabled()) {
  426. tsize = sizeof(*d->mbm_local);
  427. d->mbm_local = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
  428. if (!d->mbm_local) {
  429. kfree(d->rmid_busy_llc);
  430. kfree(d->mbm_total);
  431. return -ENOMEM;
  432. }
  433. }
  434. if (is_mbm_enabled()) {
  435. INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
  436. mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL);
  437. }
  438. return 0;
  439. }
  440. /*
  441. * domain_add_cpu - Add a cpu to a resource's domain list.
  442. *
  443. * If an existing domain in the resource r's domain list matches the cpu's
  444. * resource id, add the cpu in the domain.
  445. *
  446. * Otherwise, a new domain is allocated and inserted into the right position
  447. * in the domain list sorted by id in ascending order.
  448. *
  449. * The order in the domain list is visible to users when we print entries
  450. * in the schemata file and schemata input is validated to have the same order
  451. * as this list.
  452. */
  453. static void domain_add_cpu(int cpu, struct rdt_resource *r)
  454. {
  455. int id = get_cache_id(cpu, r->cache_level);
  456. struct list_head *add_pos = NULL;
  457. struct rdt_domain *d;
  458. d = rdt_find_domain(r, id, &add_pos);
  459. if (IS_ERR(d)) {
  460. pr_warn("Could't find cache id for cpu %d\n", cpu);
  461. return;
  462. }
  463. if (d) {
  464. cpumask_set_cpu(cpu, &d->cpu_mask);
  465. return;
  466. }
  467. d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
  468. if (!d)
  469. return;
  470. d->id = id;
  471. cpumask_set_cpu(cpu, &d->cpu_mask);
  472. if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
  473. kfree(d);
  474. return;
  475. }
  476. if (r->mon_capable && domain_setup_mon_state(r, d)) {
  477. kfree(d);
  478. return;
  479. }
  480. list_add_tail(&d->list, add_pos);
  481. /*
  482. * If resctrl is mounted, add
  483. * per domain monitor data directories.
  484. */
  485. if (static_branch_unlikely(&rdt_mon_enable_key))
  486. mkdir_mondata_subdir_allrdtgrp(r, d);
  487. }
  488. static void domain_remove_cpu(int cpu, struct rdt_resource *r)
  489. {
  490. int id = get_cache_id(cpu, r->cache_level);
  491. struct rdt_domain *d;
  492. d = rdt_find_domain(r, id, NULL);
  493. if (IS_ERR_OR_NULL(d)) {
  494. pr_warn("Could't find cache id for cpu %d\n", cpu);
  495. return;
  496. }
  497. cpumask_clear_cpu(cpu, &d->cpu_mask);
  498. if (cpumask_empty(&d->cpu_mask)) {
  499. /*
  500. * If resctrl is mounted, remove all the
  501. * per domain monitor data directories.
  502. */
  503. if (static_branch_unlikely(&rdt_mon_enable_key))
  504. rmdir_mondata_subdir_allrdtgrp(r, d->id);
  505. list_del(&d->list);
  506. if (is_mbm_enabled())
  507. cancel_delayed_work(&d->mbm_over);
  508. if (is_llc_occupancy_enabled() && has_busy_rmid(r, d)) {
  509. /*
  510. * When a package is going down, forcefully
  511. * decrement rmid->ebusy. There is no way to know
  512. * that the L3 was flushed and hence may lead to
  513. * incorrect counts in rare scenarios, but leaving
  514. * the RMID as busy creates RMID leaks if the
  515. * package never comes back.
  516. */
  517. __check_limbo(d, true);
  518. cancel_delayed_work(&d->cqm_limbo);
  519. }
  520. kfree(d->ctrl_val);
  521. kfree(d->rmid_busy_llc);
  522. kfree(d->mbm_total);
  523. kfree(d->mbm_local);
  524. kfree(d);
  525. return;
  526. }
  527. if (r == &rdt_resources_all[RDT_RESOURCE_L3]) {
  528. if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
  529. cancel_delayed_work(&d->mbm_over);
  530. mbm_setup_overflow_handler(d, 0);
  531. }
  532. if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
  533. has_busy_rmid(r, d)) {
  534. cancel_delayed_work(&d->cqm_limbo);
  535. cqm_setup_limbo_handler(d, 0);
  536. }
  537. }
  538. }
  539. static void clear_closid_rmid(int cpu)
  540. {
  541. struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
  542. state->default_closid = 0;
  543. state->default_rmid = 0;
  544. state->cur_closid = 0;
  545. state->cur_rmid = 0;
  546. wrmsr(IA32_PQR_ASSOC, 0, 0);
  547. }
  548. static int intel_rdt_online_cpu(unsigned int cpu)
  549. {
  550. struct rdt_resource *r;
  551. mutex_lock(&rdtgroup_mutex);
  552. for_each_capable_rdt_resource(r)
  553. domain_add_cpu(cpu, r);
  554. /* The cpu is set in default rdtgroup after online. */
  555. cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
  556. clear_closid_rmid(cpu);
  557. mutex_unlock(&rdtgroup_mutex);
  558. return 0;
  559. }
  560. static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
  561. {
  562. struct rdtgroup *cr;
  563. list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
  564. if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) {
  565. break;
  566. }
  567. }
  568. }
  569. static int intel_rdt_offline_cpu(unsigned int cpu)
  570. {
  571. struct rdtgroup *rdtgrp;
  572. struct rdt_resource *r;
  573. mutex_lock(&rdtgroup_mutex);
  574. for_each_capable_rdt_resource(r)
  575. domain_remove_cpu(cpu, r);
  576. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  577. if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) {
  578. clear_childcpus(rdtgrp, cpu);
  579. break;
  580. }
  581. }
  582. clear_closid_rmid(cpu);
  583. mutex_unlock(&rdtgroup_mutex);
  584. return 0;
  585. }
  586. /*
  587. * Choose a width for the resource name and resource data based on the
  588. * resource that has widest name and cbm.
  589. */
  590. static __init void rdt_init_padding(void)
  591. {
  592. struct rdt_resource *r;
  593. int cl;
  594. for_each_alloc_capable_rdt_resource(r) {
  595. cl = strlen(r->name);
  596. if (cl > max_name_width)
  597. max_name_width = cl;
  598. if (r->data_width > max_data_width)
  599. max_data_width = r->data_width;
  600. }
  601. }
  602. enum {
  603. RDT_FLAG_CMT,
  604. RDT_FLAG_MBM_TOTAL,
  605. RDT_FLAG_MBM_LOCAL,
  606. RDT_FLAG_L3_CAT,
  607. RDT_FLAG_L3_CDP,
  608. RDT_FLAG_L2_CAT,
  609. RDT_FLAG_L2_CDP,
  610. RDT_FLAG_MBA,
  611. };
  612. #define RDT_OPT(idx, n, f) \
  613. [idx] = { \
  614. .name = n, \
  615. .flag = f \
  616. }
  617. struct rdt_options {
  618. char *name;
  619. int flag;
  620. bool force_off, force_on;
  621. };
  622. static struct rdt_options rdt_options[] __initdata = {
  623. RDT_OPT(RDT_FLAG_CMT, "cmt", X86_FEATURE_CQM_OCCUP_LLC),
  624. RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
  625. RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
  626. RDT_OPT(RDT_FLAG_L3_CAT, "l3cat", X86_FEATURE_CAT_L3),
  627. RDT_OPT(RDT_FLAG_L3_CDP, "l3cdp", X86_FEATURE_CDP_L3),
  628. RDT_OPT(RDT_FLAG_L2_CAT, "l2cat", X86_FEATURE_CAT_L2),
  629. RDT_OPT(RDT_FLAG_L2_CDP, "l2cdp", X86_FEATURE_CDP_L2),
  630. RDT_OPT(RDT_FLAG_MBA, "mba", X86_FEATURE_MBA),
  631. };
  632. #define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
  633. static int __init set_rdt_options(char *str)
  634. {
  635. struct rdt_options *o;
  636. bool force_off;
  637. char *tok;
  638. if (*str == '=')
  639. str++;
  640. while ((tok = strsep(&str, ",")) != NULL) {
  641. force_off = *tok == '!';
  642. if (force_off)
  643. tok++;
  644. for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
  645. if (strcmp(tok, o->name) == 0) {
  646. if (force_off)
  647. o->force_off = true;
  648. else
  649. o->force_on = true;
  650. break;
  651. }
  652. }
  653. }
  654. return 1;
  655. }
  656. __setup("rdt", set_rdt_options);
  657. static bool __init rdt_cpu_has(int flag)
  658. {
  659. bool ret = boot_cpu_has(flag);
  660. struct rdt_options *o;
  661. if (!ret)
  662. return ret;
  663. for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
  664. if (flag == o->flag) {
  665. if (o->force_off)
  666. ret = false;
  667. if (o->force_on)
  668. ret = true;
  669. break;
  670. }
  671. }
  672. return ret;
  673. }
  674. static __init bool get_rdt_alloc_resources(void)
  675. {
  676. bool ret = false;
  677. if (rdt_alloc_capable)
  678. return true;
  679. if (!boot_cpu_has(X86_FEATURE_RDT_A))
  680. return false;
  681. if (rdt_cpu_has(X86_FEATURE_CAT_L3)) {
  682. rdt_get_cache_alloc_cfg(1, &rdt_resources_all[RDT_RESOURCE_L3]);
  683. if (rdt_cpu_has(X86_FEATURE_CDP_L3))
  684. rdt_get_cdp_l3_config();
  685. ret = true;
  686. }
  687. if (rdt_cpu_has(X86_FEATURE_CAT_L2)) {
  688. /* CPUID 0x10.2 fields are same format at 0x10.1 */
  689. rdt_get_cache_alloc_cfg(2, &rdt_resources_all[RDT_RESOURCE_L2]);
  690. if (rdt_cpu_has(X86_FEATURE_CDP_L2))
  691. rdt_get_cdp_l2_config();
  692. ret = true;
  693. }
  694. if (rdt_cpu_has(X86_FEATURE_MBA)) {
  695. if (rdt_get_mem_config(&rdt_resources_all[RDT_RESOURCE_MBA]))
  696. ret = true;
  697. }
  698. return ret;
  699. }
  700. static __init bool get_rdt_mon_resources(void)
  701. {
  702. if (rdt_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
  703. rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
  704. if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
  705. rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
  706. if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
  707. rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
  708. if (!rdt_mon_features)
  709. return false;
  710. return !rdt_get_mon_l3_config(&rdt_resources_all[RDT_RESOURCE_L3]);
  711. }
  712. static __init void rdt_quirks(void)
  713. {
  714. switch (boot_cpu_data.x86_model) {
  715. case INTEL_FAM6_HASWELL_X:
  716. if (!rdt_options[RDT_FLAG_L3_CAT].force_off)
  717. cache_alloc_hsw_probe();
  718. break;
  719. case INTEL_FAM6_SKYLAKE_X:
  720. if (boot_cpu_data.x86_stepping <= 4)
  721. set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
  722. }
  723. }
  724. static __init bool get_rdt_resources(void)
  725. {
  726. rdt_quirks();
  727. rdt_alloc_capable = get_rdt_alloc_resources();
  728. rdt_mon_capable = get_rdt_mon_resources();
  729. return (rdt_mon_capable || rdt_alloc_capable);
  730. }
  731. static int __init intel_rdt_late_init(void)
  732. {
  733. struct rdt_resource *r;
  734. int state, ret;
  735. if (!get_rdt_resources())
  736. return -ENODEV;
  737. rdt_init_padding();
  738. state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  739. "x86/rdt/cat:online:",
  740. intel_rdt_online_cpu, intel_rdt_offline_cpu);
  741. if (state < 0)
  742. return state;
  743. ret = rdtgroup_init();
  744. if (ret) {
  745. cpuhp_remove_state(state);
  746. return ret;
  747. }
  748. for_each_alloc_capable_rdt_resource(r)
  749. pr_info("Intel RDT %s allocation detected\n", r->name);
  750. for_each_mon_capable_rdt_resource(r)
  751. pr_info("Intel RDT %s monitoring detected\n", r->name);
  752. return 0;
  753. }
  754. late_initcall(intel_rdt_late_init);