intel_rdt.c 22 KB

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