intel_rdt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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(-ENODEV);
  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. rdt_domain_reconfigure_cdp(r);
  492. if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
  493. kfree(d);
  494. return;
  495. }
  496. if (r->mon_capable && domain_setup_mon_state(r, d)) {
  497. kfree(d);
  498. return;
  499. }
  500. list_add_tail(&d->list, add_pos);
  501. /*
  502. * If resctrl is mounted, add
  503. * per domain monitor data directories.
  504. */
  505. if (static_branch_unlikely(&rdt_mon_enable_key))
  506. mkdir_mondata_subdir_allrdtgrp(r, d);
  507. }
  508. static void domain_remove_cpu(int cpu, struct rdt_resource *r)
  509. {
  510. int id = get_cache_id(cpu, r->cache_level);
  511. struct rdt_domain *d;
  512. d = rdt_find_domain(r, id, NULL);
  513. if (IS_ERR_OR_NULL(d)) {
  514. pr_warn("Could't find cache id for cpu %d\n", cpu);
  515. return;
  516. }
  517. cpumask_clear_cpu(cpu, &d->cpu_mask);
  518. if (cpumask_empty(&d->cpu_mask)) {
  519. /*
  520. * If resctrl is mounted, remove all the
  521. * per domain monitor data directories.
  522. */
  523. if (static_branch_unlikely(&rdt_mon_enable_key))
  524. rmdir_mondata_subdir_allrdtgrp(r, d->id);
  525. list_del(&d->list);
  526. if (r->mon_capable && is_mbm_enabled())
  527. cancel_delayed_work(&d->mbm_over);
  528. if (is_llc_occupancy_enabled() && has_busy_rmid(r, d)) {
  529. /*
  530. * When a package is going down, forcefully
  531. * decrement rmid->ebusy. There is no way to know
  532. * that the L3 was flushed and hence may lead to
  533. * incorrect counts in rare scenarios, but leaving
  534. * the RMID as busy creates RMID leaks if the
  535. * package never comes back.
  536. */
  537. __check_limbo(d, true);
  538. cancel_delayed_work(&d->cqm_limbo);
  539. }
  540. /*
  541. * rdt_domain "d" is going to be freed below, so clear
  542. * its pointer from pseudo_lock_region struct.
  543. */
  544. if (d->plr)
  545. d->plr->d = NULL;
  546. kfree(d->ctrl_val);
  547. kfree(d->mbps_val);
  548. kfree(d->rmid_busy_llc);
  549. kfree(d->mbm_total);
  550. kfree(d->mbm_local);
  551. kfree(d);
  552. return;
  553. }
  554. if (r == &rdt_resources_all[RDT_RESOURCE_L3]) {
  555. if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
  556. cancel_delayed_work(&d->mbm_over);
  557. mbm_setup_overflow_handler(d, 0);
  558. }
  559. if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
  560. has_busy_rmid(r, d)) {
  561. cancel_delayed_work(&d->cqm_limbo);
  562. cqm_setup_limbo_handler(d, 0);
  563. }
  564. }
  565. }
  566. static void clear_closid_rmid(int cpu)
  567. {
  568. struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
  569. state->default_closid = 0;
  570. state->default_rmid = 0;
  571. state->cur_closid = 0;
  572. state->cur_rmid = 0;
  573. wrmsr(IA32_PQR_ASSOC, 0, 0);
  574. }
  575. static int intel_rdt_online_cpu(unsigned int cpu)
  576. {
  577. struct rdt_resource *r;
  578. mutex_lock(&rdtgroup_mutex);
  579. for_each_capable_rdt_resource(r)
  580. domain_add_cpu(cpu, r);
  581. /* The cpu is set in default rdtgroup after online. */
  582. cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
  583. clear_closid_rmid(cpu);
  584. mutex_unlock(&rdtgroup_mutex);
  585. return 0;
  586. }
  587. static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
  588. {
  589. struct rdtgroup *cr;
  590. list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
  591. if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) {
  592. break;
  593. }
  594. }
  595. }
  596. static int intel_rdt_offline_cpu(unsigned int cpu)
  597. {
  598. struct rdtgroup *rdtgrp;
  599. struct rdt_resource *r;
  600. mutex_lock(&rdtgroup_mutex);
  601. for_each_capable_rdt_resource(r)
  602. domain_remove_cpu(cpu, r);
  603. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  604. if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) {
  605. clear_childcpus(rdtgrp, cpu);
  606. break;
  607. }
  608. }
  609. clear_closid_rmid(cpu);
  610. mutex_unlock(&rdtgroup_mutex);
  611. return 0;
  612. }
  613. /*
  614. * Choose a width for the resource name and resource data based on the
  615. * resource that has widest name and cbm.
  616. */
  617. static __init void rdt_init_padding(void)
  618. {
  619. struct rdt_resource *r;
  620. int cl;
  621. for_each_alloc_capable_rdt_resource(r) {
  622. cl = strlen(r->name);
  623. if (cl > max_name_width)
  624. max_name_width = cl;
  625. if (r->data_width > max_data_width)
  626. max_data_width = r->data_width;
  627. }
  628. }
  629. enum {
  630. RDT_FLAG_CMT,
  631. RDT_FLAG_MBM_TOTAL,
  632. RDT_FLAG_MBM_LOCAL,
  633. RDT_FLAG_L3_CAT,
  634. RDT_FLAG_L3_CDP,
  635. RDT_FLAG_L2_CAT,
  636. RDT_FLAG_L2_CDP,
  637. RDT_FLAG_MBA,
  638. };
  639. #define RDT_OPT(idx, n, f) \
  640. [idx] = { \
  641. .name = n, \
  642. .flag = f \
  643. }
  644. struct rdt_options {
  645. char *name;
  646. int flag;
  647. bool force_off, force_on;
  648. };
  649. static struct rdt_options rdt_options[] __initdata = {
  650. RDT_OPT(RDT_FLAG_CMT, "cmt", X86_FEATURE_CQM_OCCUP_LLC),
  651. RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
  652. RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
  653. RDT_OPT(RDT_FLAG_L3_CAT, "l3cat", X86_FEATURE_CAT_L3),
  654. RDT_OPT(RDT_FLAG_L3_CDP, "l3cdp", X86_FEATURE_CDP_L3),
  655. RDT_OPT(RDT_FLAG_L2_CAT, "l2cat", X86_FEATURE_CAT_L2),
  656. RDT_OPT(RDT_FLAG_L2_CDP, "l2cdp", X86_FEATURE_CDP_L2),
  657. RDT_OPT(RDT_FLAG_MBA, "mba", X86_FEATURE_MBA),
  658. };
  659. #define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
  660. static int __init set_rdt_options(char *str)
  661. {
  662. struct rdt_options *o;
  663. bool force_off;
  664. char *tok;
  665. if (*str == '=')
  666. str++;
  667. while ((tok = strsep(&str, ",")) != NULL) {
  668. force_off = *tok == '!';
  669. if (force_off)
  670. tok++;
  671. for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
  672. if (strcmp(tok, o->name) == 0) {
  673. if (force_off)
  674. o->force_off = true;
  675. else
  676. o->force_on = true;
  677. break;
  678. }
  679. }
  680. }
  681. return 1;
  682. }
  683. __setup("rdt", set_rdt_options);
  684. static bool __init rdt_cpu_has(int flag)
  685. {
  686. bool ret = boot_cpu_has(flag);
  687. struct rdt_options *o;
  688. if (!ret)
  689. return ret;
  690. for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
  691. if (flag == o->flag) {
  692. if (o->force_off)
  693. ret = false;
  694. if (o->force_on)
  695. ret = true;
  696. break;
  697. }
  698. }
  699. return ret;
  700. }
  701. static __init bool get_rdt_alloc_resources(void)
  702. {
  703. bool ret = false;
  704. if (rdt_alloc_capable)
  705. return true;
  706. if (!boot_cpu_has(X86_FEATURE_RDT_A))
  707. return false;
  708. if (rdt_cpu_has(X86_FEATURE_CAT_L3)) {
  709. rdt_get_cache_alloc_cfg(1, &rdt_resources_all[RDT_RESOURCE_L3]);
  710. if (rdt_cpu_has(X86_FEATURE_CDP_L3))
  711. rdt_get_cdp_l3_config();
  712. ret = true;
  713. }
  714. if (rdt_cpu_has(X86_FEATURE_CAT_L2)) {
  715. /* CPUID 0x10.2 fields are same format at 0x10.1 */
  716. rdt_get_cache_alloc_cfg(2, &rdt_resources_all[RDT_RESOURCE_L2]);
  717. if (rdt_cpu_has(X86_FEATURE_CDP_L2))
  718. rdt_get_cdp_l2_config();
  719. ret = true;
  720. }
  721. if (rdt_cpu_has(X86_FEATURE_MBA)) {
  722. if (rdt_get_mem_config(&rdt_resources_all[RDT_RESOURCE_MBA]))
  723. ret = true;
  724. }
  725. return ret;
  726. }
  727. static __init bool get_rdt_mon_resources(void)
  728. {
  729. if (rdt_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
  730. rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
  731. if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
  732. rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
  733. if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
  734. rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
  735. if (!rdt_mon_features)
  736. return false;
  737. return !rdt_get_mon_l3_config(&rdt_resources_all[RDT_RESOURCE_L3]);
  738. }
  739. static __init void rdt_quirks(void)
  740. {
  741. switch (boot_cpu_data.x86_model) {
  742. case INTEL_FAM6_HASWELL_X:
  743. if (!rdt_options[RDT_FLAG_L3_CAT].force_off)
  744. cache_alloc_hsw_probe();
  745. break;
  746. case INTEL_FAM6_SKYLAKE_X:
  747. if (boot_cpu_data.x86_stepping <= 4)
  748. set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
  749. else
  750. set_rdt_options("!l3cat");
  751. }
  752. }
  753. static __init bool get_rdt_resources(void)
  754. {
  755. rdt_quirks();
  756. rdt_alloc_capable = get_rdt_alloc_resources();
  757. rdt_mon_capable = get_rdt_mon_resources();
  758. return (rdt_mon_capable || rdt_alloc_capable);
  759. }
  760. static enum cpuhp_state rdt_online;
  761. static int __init intel_rdt_late_init(void)
  762. {
  763. struct rdt_resource *r;
  764. int state, ret;
  765. if (!get_rdt_resources())
  766. return -ENODEV;
  767. rdt_init_padding();
  768. state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  769. "x86/rdt/cat:online:",
  770. intel_rdt_online_cpu, intel_rdt_offline_cpu);
  771. if (state < 0)
  772. return state;
  773. ret = rdtgroup_init();
  774. if (ret) {
  775. cpuhp_remove_state(state);
  776. return ret;
  777. }
  778. rdt_online = state;
  779. for_each_alloc_capable_rdt_resource(r)
  780. pr_info("Intel RDT %s allocation detected\n", r->name);
  781. for_each_mon_capable_rdt_resource(r)
  782. pr_info("Intel RDT %s monitoring detected\n", r->name);
  783. return 0;
  784. }
  785. late_initcall(intel_rdt_late_init);
  786. static void __exit intel_rdt_exit(void)
  787. {
  788. cpuhp_remove_state(rdt_online);
  789. rdtgroup_exit();
  790. }
  791. __exitcall(intel_rdt_exit);