intel_rdt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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 MBA_IS_LINEAR 0x4
  33. #define MBA_MAX_MBPS U32_MAX
  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=%*u",
  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. bool is_mba_sc(struct rdt_resource *r)
  216. {
  217. if (!r)
  218. return rdt_resources_all[RDT_RESOURCE_MBA].membw.mba_sc;
  219. return r->membw.mba_sc;
  220. }
  221. /*
  222. * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
  223. * exposed to user interface and the h/w understandable delay values.
  224. *
  225. * The non-linear delay values have the granularity of power of two
  226. * and also the h/w does not guarantee a curve for configured delay
  227. * values vs. actual b/w enforced.
  228. * Hence we need a mapping that is pre calibrated so the user can
  229. * express the memory b/w as a percentage value.
  230. */
  231. static inline bool rdt_get_mb_table(struct rdt_resource *r)
  232. {
  233. /*
  234. * There are no Intel SKUs as of now to support non-linear delay.
  235. */
  236. pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
  237. boot_cpu_data.x86, boot_cpu_data.x86_model);
  238. return false;
  239. }
  240. static bool rdt_get_mem_config(struct rdt_resource *r)
  241. {
  242. union cpuid_0x10_3_eax eax;
  243. union cpuid_0x10_x_edx edx;
  244. u32 ebx, ecx;
  245. cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
  246. r->num_closid = edx.split.cos_max + 1;
  247. r->membw.max_delay = eax.split.max_delay + 1;
  248. r->default_ctrl = MAX_MBA_BW;
  249. if (ecx & MBA_IS_LINEAR) {
  250. r->membw.delay_linear = true;
  251. r->membw.min_bw = MAX_MBA_BW - r->membw.max_delay;
  252. r->membw.bw_gran = MAX_MBA_BW - r->membw.max_delay;
  253. } else {
  254. if (!rdt_get_mb_table(r))
  255. return false;
  256. }
  257. r->data_width = 3;
  258. r->alloc_capable = true;
  259. r->alloc_enabled = true;
  260. return true;
  261. }
  262. static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
  263. {
  264. union cpuid_0x10_1_eax eax;
  265. union cpuid_0x10_x_edx edx;
  266. u32 ebx, ecx;
  267. cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
  268. r->num_closid = edx.split.cos_max + 1;
  269. r->cache.cbm_len = eax.split.cbm_len + 1;
  270. r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
  271. r->cache.shareable_bits = ebx & r->default_ctrl;
  272. r->data_width = (r->cache.cbm_len + 3) / 4;
  273. r->alloc_capable = true;
  274. r->alloc_enabled = true;
  275. }
  276. static void rdt_get_cdp_config(int level, int type)
  277. {
  278. struct rdt_resource *r_l = &rdt_resources_all[level];
  279. struct rdt_resource *r = &rdt_resources_all[type];
  280. r->num_closid = r_l->num_closid / 2;
  281. r->cache.cbm_len = r_l->cache.cbm_len;
  282. r->default_ctrl = r_l->default_ctrl;
  283. r->cache.shareable_bits = r_l->cache.shareable_bits;
  284. r->data_width = (r->cache.cbm_len + 3) / 4;
  285. r->alloc_capable = true;
  286. /*
  287. * By default, CDP is disabled. CDP can be enabled by mount parameter
  288. * "cdp" during resctrl file system mount time.
  289. */
  290. r->alloc_enabled = false;
  291. }
  292. static void rdt_get_cdp_l3_config(void)
  293. {
  294. rdt_get_cdp_config(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA);
  295. rdt_get_cdp_config(RDT_RESOURCE_L3, RDT_RESOURCE_L3CODE);
  296. }
  297. static void rdt_get_cdp_l2_config(void)
  298. {
  299. rdt_get_cdp_config(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA);
  300. rdt_get_cdp_config(RDT_RESOURCE_L2, RDT_RESOURCE_L2CODE);
  301. }
  302. static int get_cache_id(int cpu, int level)
  303. {
  304. struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
  305. int i;
  306. for (i = 0; i < ci->num_leaves; i++) {
  307. if (ci->info_list[i].level == level)
  308. return ci->info_list[i].id;
  309. }
  310. return -1;
  311. }
  312. /*
  313. * Map the memory b/w percentage value to delay values
  314. * that can be written to QOS_MSRs.
  315. * There are currently no SKUs which support non linear delay values.
  316. */
  317. u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
  318. {
  319. if (r->membw.delay_linear)
  320. return MAX_MBA_BW - bw;
  321. pr_warn_once("Non Linear delay-bw map not supported but queried\n");
  322. return r->default_ctrl;
  323. }
  324. static void
  325. mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
  326. {
  327. unsigned int i;
  328. /* Write the delay values for mba. */
  329. for (i = m->low; i < m->high; i++)
  330. wrmsrl(r->msr_base + i, delay_bw_map(d->ctrl_val[i], r));
  331. }
  332. static void
  333. cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
  334. {
  335. unsigned int i;
  336. for (i = m->low; i < m->high; i++)
  337. wrmsrl(r->msr_base + cbm_idx(r, i), d->ctrl_val[i]);
  338. }
  339. struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r)
  340. {
  341. struct rdt_domain *d;
  342. list_for_each_entry(d, &r->domains, list) {
  343. /* Find the domain that contains this CPU */
  344. if (cpumask_test_cpu(cpu, &d->cpu_mask))
  345. return d;
  346. }
  347. return NULL;
  348. }
  349. void rdt_ctrl_update(void *arg)
  350. {
  351. struct msr_param *m = arg;
  352. struct rdt_resource *r = m->res;
  353. int cpu = smp_processor_id();
  354. struct rdt_domain *d;
  355. d = get_domain_from_cpu(cpu, r);
  356. if (d) {
  357. r->msr_update(d, m, r);
  358. return;
  359. }
  360. pr_warn_once("cpu %d not found in any domain for resource %s\n",
  361. cpu, r->name);
  362. }
  363. /*
  364. * rdt_find_domain - Find a domain in a resource that matches input resource id
  365. *
  366. * Search resource r's domain list to find the resource id. If the resource
  367. * id is found in a domain, return the domain. Otherwise, if requested by
  368. * caller, return the first domain whose id is bigger than the input id.
  369. * The domain list is sorted by id in ascending order.
  370. */
  371. struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
  372. struct list_head **pos)
  373. {
  374. struct rdt_domain *d;
  375. struct list_head *l;
  376. if (id < 0)
  377. return ERR_PTR(id);
  378. list_for_each(l, &r->domains) {
  379. d = list_entry(l, struct rdt_domain, list);
  380. /* When id is found, return its domain. */
  381. if (id == d->id)
  382. return d;
  383. /* Stop searching when finding id's position in sorted list. */
  384. if (id < d->id)
  385. break;
  386. }
  387. if (pos)
  388. *pos = l;
  389. return NULL;
  390. }
  391. void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm)
  392. {
  393. int i;
  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. * and the bandwidth in MBps to U32_MAX
  399. */
  400. for (i = 0; i < r->num_closid; i++, dc++, dm++) {
  401. *dc = r->default_ctrl;
  402. *dm = MBA_MAX_MBPS;
  403. }
  404. }
  405. static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
  406. {
  407. struct msr_param m;
  408. u32 *dc, *dm;
  409. dc = kmalloc_array(r->num_closid, sizeof(*d->ctrl_val), GFP_KERNEL);
  410. if (!dc)
  411. return -ENOMEM;
  412. dm = kmalloc_array(r->num_closid, sizeof(*d->mbps_val), GFP_KERNEL);
  413. if (!dm) {
  414. kfree(dc);
  415. return -ENOMEM;
  416. }
  417. d->ctrl_val = dc;
  418. d->mbps_val = dm;
  419. setup_default_ctrlval(r, dc, dm);
  420. m.low = 0;
  421. m.high = r->num_closid;
  422. r->msr_update(d, &m, r);
  423. return 0;
  424. }
  425. static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
  426. {
  427. size_t tsize;
  428. if (is_llc_occupancy_enabled()) {
  429. d->rmid_busy_llc = kcalloc(BITS_TO_LONGS(r->num_rmid),
  430. sizeof(unsigned long),
  431. GFP_KERNEL);
  432. if (!d->rmid_busy_llc)
  433. return -ENOMEM;
  434. INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
  435. }
  436. if (is_mbm_total_enabled()) {
  437. tsize = sizeof(*d->mbm_total);
  438. d->mbm_total = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
  439. if (!d->mbm_total) {
  440. kfree(d->rmid_busy_llc);
  441. return -ENOMEM;
  442. }
  443. }
  444. if (is_mbm_local_enabled()) {
  445. tsize = sizeof(*d->mbm_local);
  446. d->mbm_local = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
  447. if (!d->mbm_local) {
  448. kfree(d->rmid_busy_llc);
  449. kfree(d->mbm_total);
  450. return -ENOMEM;
  451. }
  452. }
  453. if (is_mbm_enabled()) {
  454. INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
  455. mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL);
  456. }
  457. return 0;
  458. }
  459. /*
  460. * domain_add_cpu - Add a cpu to a resource's domain list.
  461. *
  462. * If an existing domain in the resource r's domain list matches the cpu's
  463. * resource id, add the cpu in the domain.
  464. *
  465. * Otherwise, a new domain is allocated and inserted into the right position
  466. * in the domain list sorted by id in ascending order.
  467. *
  468. * The order in the domain list is visible to users when we print entries
  469. * in the schemata file and schemata input is validated to have the same order
  470. * as this list.
  471. */
  472. static void domain_add_cpu(int cpu, struct rdt_resource *r)
  473. {
  474. int id = get_cache_id(cpu, r->cache_level);
  475. struct list_head *add_pos = NULL;
  476. struct rdt_domain *d;
  477. d = rdt_find_domain(r, id, &add_pos);
  478. if (IS_ERR(d)) {
  479. pr_warn("Could't find cache id for cpu %d\n", cpu);
  480. return;
  481. }
  482. if (d) {
  483. cpumask_set_cpu(cpu, &d->cpu_mask);
  484. return;
  485. }
  486. d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
  487. if (!d)
  488. return;
  489. d->id = id;
  490. cpumask_set_cpu(cpu, &d->cpu_mask);
  491. if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
  492. kfree(d);
  493. return;
  494. }
  495. if (r->mon_capable && domain_setup_mon_state(r, d)) {
  496. kfree(d);
  497. return;
  498. }
  499. list_add_tail(&d->list, add_pos);
  500. /*
  501. * If resctrl is mounted, add
  502. * per domain monitor data directories.
  503. */
  504. if (static_branch_unlikely(&rdt_mon_enable_key))
  505. mkdir_mondata_subdir_allrdtgrp(r, d);
  506. }
  507. static void domain_remove_cpu(int cpu, struct rdt_resource *r)
  508. {
  509. int id = get_cache_id(cpu, r->cache_level);
  510. struct rdt_domain *d;
  511. d = rdt_find_domain(r, id, NULL);
  512. if (IS_ERR_OR_NULL(d)) {
  513. pr_warn("Could't find cache id for cpu %d\n", cpu);
  514. return;
  515. }
  516. cpumask_clear_cpu(cpu, &d->cpu_mask);
  517. if (cpumask_empty(&d->cpu_mask)) {
  518. /*
  519. * If resctrl is mounted, remove all the
  520. * per domain monitor data directories.
  521. */
  522. if (static_branch_unlikely(&rdt_mon_enable_key))
  523. rmdir_mondata_subdir_allrdtgrp(r, d->id);
  524. list_del(&d->list);
  525. if (is_mbm_enabled())
  526. cancel_delayed_work(&d->mbm_over);
  527. if (is_llc_occupancy_enabled() && has_busy_rmid(r, d)) {
  528. /*
  529. * When a package is going down, forcefully
  530. * decrement rmid->ebusy. There is no way to know
  531. * that the L3 was flushed and hence may lead to
  532. * incorrect counts in rare scenarios, but leaving
  533. * the RMID as busy creates RMID leaks if the
  534. * package never comes back.
  535. */
  536. __check_limbo(d, true);
  537. cancel_delayed_work(&d->cqm_limbo);
  538. }
  539. kfree(d->ctrl_val);
  540. kfree(d->mbps_val);
  541. kfree(d->rmid_busy_llc);
  542. kfree(d->mbm_total);
  543. kfree(d->mbm_local);
  544. kfree(d);
  545. return;
  546. }
  547. if (r == &rdt_resources_all[RDT_RESOURCE_L3]) {
  548. if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
  549. cancel_delayed_work(&d->mbm_over);
  550. mbm_setup_overflow_handler(d, 0);
  551. }
  552. if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
  553. has_busy_rmid(r, d)) {
  554. cancel_delayed_work(&d->cqm_limbo);
  555. cqm_setup_limbo_handler(d, 0);
  556. }
  557. }
  558. }
  559. static void clear_closid_rmid(int cpu)
  560. {
  561. struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
  562. state->default_closid = 0;
  563. state->default_rmid = 0;
  564. state->cur_closid = 0;
  565. state->cur_rmid = 0;
  566. wrmsr(IA32_PQR_ASSOC, 0, 0);
  567. }
  568. static int intel_rdt_online_cpu(unsigned int cpu)
  569. {
  570. struct rdt_resource *r;
  571. mutex_lock(&rdtgroup_mutex);
  572. for_each_capable_rdt_resource(r)
  573. domain_add_cpu(cpu, r);
  574. /* The cpu is set in default rdtgroup after online. */
  575. cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
  576. clear_closid_rmid(cpu);
  577. mutex_unlock(&rdtgroup_mutex);
  578. return 0;
  579. }
  580. static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
  581. {
  582. struct rdtgroup *cr;
  583. list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
  584. if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) {
  585. break;
  586. }
  587. }
  588. }
  589. static int intel_rdt_offline_cpu(unsigned int cpu)
  590. {
  591. struct rdtgroup *rdtgrp;
  592. struct rdt_resource *r;
  593. mutex_lock(&rdtgroup_mutex);
  594. for_each_capable_rdt_resource(r)
  595. domain_remove_cpu(cpu, r);
  596. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  597. if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) {
  598. clear_childcpus(rdtgrp, cpu);
  599. break;
  600. }
  601. }
  602. clear_closid_rmid(cpu);
  603. mutex_unlock(&rdtgroup_mutex);
  604. return 0;
  605. }
  606. /*
  607. * Choose a width for the resource name and resource data based on the
  608. * resource that has widest name and cbm.
  609. */
  610. static __init void rdt_init_padding(void)
  611. {
  612. struct rdt_resource *r;
  613. int cl;
  614. for_each_alloc_capable_rdt_resource(r) {
  615. cl = strlen(r->name);
  616. if (cl > max_name_width)
  617. max_name_width = cl;
  618. if (r->data_width > max_data_width)
  619. max_data_width = r->data_width;
  620. }
  621. }
  622. enum {
  623. RDT_FLAG_CMT,
  624. RDT_FLAG_MBM_TOTAL,
  625. RDT_FLAG_MBM_LOCAL,
  626. RDT_FLAG_L3_CAT,
  627. RDT_FLAG_L3_CDP,
  628. RDT_FLAG_L2_CAT,
  629. RDT_FLAG_L2_CDP,
  630. RDT_FLAG_MBA,
  631. };
  632. #define RDT_OPT(idx, n, f) \
  633. [idx] = { \
  634. .name = n, \
  635. .flag = f \
  636. }
  637. struct rdt_options {
  638. char *name;
  639. int flag;
  640. bool force_off, force_on;
  641. };
  642. static struct rdt_options rdt_options[] __initdata = {
  643. RDT_OPT(RDT_FLAG_CMT, "cmt", X86_FEATURE_CQM_OCCUP_LLC),
  644. RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
  645. RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
  646. RDT_OPT(RDT_FLAG_L3_CAT, "l3cat", X86_FEATURE_CAT_L3),
  647. RDT_OPT(RDT_FLAG_L3_CDP, "l3cdp", X86_FEATURE_CDP_L3),
  648. RDT_OPT(RDT_FLAG_L2_CAT, "l2cat", X86_FEATURE_CAT_L2),
  649. RDT_OPT(RDT_FLAG_L2_CDP, "l2cdp", X86_FEATURE_CDP_L2),
  650. RDT_OPT(RDT_FLAG_MBA, "mba", X86_FEATURE_MBA),
  651. };
  652. #define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
  653. static int __init set_rdt_options(char *str)
  654. {
  655. struct rdt_options *o;
  656. bool force_off;
  657. char *tok;
  658. if (*str == '=')
  659. str++;
  660. while ((tok = strsep(&str, ",")) != NULL) {
  661. force_off = *tok == '!';
  662. if (force_off)
  663. tok++;
  664. for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
  665. if (strcmp(tok, o->name) == 0) {
  666. if (force_off)
  667. o->force_off = true;
  668. else
  669. o->force_on = true;
  670. break;
  671. }
  672. }
  673. }
  674. return 1;
  675. }
  676. __setup("rdt", set_rdt_options);
  677. static bool __init rdt_cpu_has(int flag)
  678. {
  679. bool ret = boot_cpu_has(flag);
  680. struct rdt_options *o;
  681. if (!ret)
  682. return ret;
  683. for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
  684. if (flag == o->flag) {
  685. if (o->force_off)
  686. ret = false;
  687. if (o->force_on)
  688. ret = true;
  689. break;
  690. }
  691. }
  692. return ret;
  693. }
  694. static __init bool get_rdt_alloc_resources(void)
  695. {
  696. bool ret = false;
  697. if (rdt_alloc_capable)
  698. return true;
  699. if (!boot_cpu_has(X86_FEATURE_RDT_A))
  700. return false;
  701. if (rdt_cpu_has(X86_FEATURE_CAT_L3)) {
  702. rdt_get_cache_alloc_cfg(1, &rdt_resources_all[RDT_RESOURCE_L3]);
  703. if (rdt_cpu_has(X86_FEATURE_CDP_L3))
  704. rdt_get_cdp_l3_config();
  705. ret = true;
  706. }
  707. if (rdt_cpu_has(X86_FEATURE_CAT_L2)) {
  708. /* CPUID 0x10.2 fields are same format at 0x10.1 */
  709. rdt_get_cache_alloc_cfg(2, &rdt_resources_all[RDT_RESOURCE_L2]);
  710. if (rdt_cpu_has(X86_FEATURE_CDP_L2))
  711. rdt_get_cdp_l2_config();
  712. ret = true;
  713. }
  714. if (rdt_cpu_has(X86_FEATURE_MBA)) {
  715. if (rdt_get_mem_config(&rdt_resources_all[RDT_RESOURCE_MBA]))
  716. ret = true;
  717. }
  718. return ret;
  719. }
  720. static __init bool get_rdt_mon_resources(void)
  721. {
  722. if (rdt_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
  723. rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
  724. if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
  725. rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
  726. if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
  727. rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
  728. if (!rdt_mon_features)
  729. return false;
  730. return !rdt_get_mon_l3_config(&rdt_resources_all[RDT_RESOURCE_L3]);
  731. }
  732. static __init void rdt_quirks(void)
  733. {
  734. switch (boot_cpu_data.x86_model) {
  735. case INTEL_FAM6_HASWELL_X:
  736. if (!rdt_options[RDT_FLAG_L3_CAT].force_off)
  737. cache_alloc_hsw_probe();
  738. break;
  739. case INTEL_FAM6_SKYLAKE_X:
  740. if (boot_cpu_data.x86_stepping <= 4)
  741. set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
  742. else
  743. set_rdt_options("!l3cat");
  744. }
  745. }
  746. static __init bool get_rdt_resources(void)
  747. {
  748. rdt_quirks();
  749. rdt_alloc_capable = get_rdt_alloc_resources();
  750. rdt_mon_capable = get_rdt_mon_resources();
  751. return (rdt_mon_capable || rdt_alloc_capable);
  752. }
  753. static int __init intel_rdt_late_init(void)
  754. {
  755. struct rdt_resource *r;
  756. int state, ret;
  757. if (!get_rdt_resources())
  758. return -ENODEV;
  759. rdt_init_padding();
  760. state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  761. "x86/rdt/cat:online:",
  762. intel_rdt_online_cpu, intel_rdt_offline_cpu);
  763. if (state < 0)
  764. return state;
  765. ret = rdtgroup_init();
  766. if (ret) {
  767. cpuhp_remove_state(state);
  768. return ret;
  769. }
  770. for_each_alloc_capable_rdt_resource(r)
  771. pr_info("Intel RDT %s allocation detected\n", r->name);
  772. for_each_mon_capable_rdt_resource(r)
  773. pr_info("Intel RDT %s monitoring detected\n", r->name);
  774. return 0;
  775. }
  776. late_initcall(intel_rdt_late_init);