intel_rdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Resource Director Technology(RDT)
  3. * - Cache Allocation code.
  4. *
  5. * Copyright (C) 2016 Intel Corporation
  6. *
  7. * Authors:
  8. * Fenghua Yu <fenghua.yu@intel.com>
  9. * Tony Luck <tony.luck@intel.com>
  10. * Vikas Shivappa <vikas.shivappa@intel.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * More information about RDT be found in the Intel (R) x86 Architecture
  22. * Software Developer Manual June 2016, volume 3, section 17.17.
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/slab.h>
  26. #include <linux/err.h>
  27. #include <linux/cacheinfo.h>
  28. #include <linux/cpuhotplug.h>
  29. #include <asm/intel-family.h>
  30. #include <asm/intel_rdt.h>
  31. /* Mutex to protect rdtgroup access. */
  32. DEFINE_MUTEX(rdtgroup_mutex);
  33. DEFINE_PER_CPU_READ_MOSTLY(int, cpu_closid);
  34. #define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains)
  35. /*
  36. * Used to store the max resource name width and max resource data width
  37. * to display the schemata in a tabular format
  38. */
  39. int max_name_width, max_data_width;
  40. struct rdt_resource rdt_resources_all[] = {
  41. {
  42. .name = "L3",
  43. .domains = domain_init(RDT_RESOURCE_L3),
  44. .msr_base = IA32_L3_CBM_BASE,
  45. .min_cbm_bits = 1,
  46. .cache_level = 3,
  47. .cbm_idx_multi = 1,
  48. .cbm_idx_offset = 0
  49. },
  50. {
  51. .name = "L3DATA",
  52. .domains = domain_init(RDT_RESOURCE_L3DATA),
  53. .msr_base = IA32_L3_CBM_BASE,
  54. .min_cbm_bits = 1,
  55. .cache_level = 3,
  56. .cbm_idx_multi = 2,
  57. .cbm_idx_offset = 0
  58. },
  59. {
  60. .name = "L3CODE",
  61. .domains = domain_init(RDT_RESOURCE_L3CODE),
  62. .msr_base = IA32_L3_CBM_BASE,
  63. .min_cbm_bits = 1,
  64. .cache_level = 3,
  65. .cbm_idx_multi = 2,
  66. .cbm_idx_offset = 1
  67. },
  68. {
  69. .name = "L2",
  70. .domains = domain_init(RDT_RESOURCE_L2),
  71. .msr_base = IA32_L2_CBM_BASE,
  72. .min_cbm_bits = 1,
  73. .cache_level = 2,
  74. .cbm_idx_multi = 1,
  75. .cbm_idx_offset = 0
  76. },
  77. };
  78. static int cbm_idx(struct rdt_resource *r, int closid)
  79. {
  80. return closid * r->cbm_idx_multi + r->cbm_idx_offset;
  81. }
  82. /*
  83. * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
  84. * as they do not have CPUID enumeration support for Cache allocation.
  85. * The check for Vendor/Family/Model is not enough to guarantee that
  86. * the MSRs won't #GP fault because only the following SKUs support
  87. * CAT:
  88. * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz
  89. * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz
  90. * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz
  91. * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz
  92. * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz
  93. * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz
  94. *
  95. * Probe by trying to write the first of the L3 cach mask registers
  96. * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
  97. * is always 20 on hsw server parts. The minimum cache bitmask length
  98. * allowed for HSW server is always 2 bits. Hardcode all of them.
  99. */
  100. static inline bool cache_alloc_hsw_probe(void)
  101. {
  102. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  103. boot_cpu_data.x86 == 6 &&
  104. boot_cpu_data.x86_model == INTEL_FAM6_HASWELL_X) {
  105. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
  106. u32 l, h, max_cbm = BIT_MASK(20) - 1;
  107. if (wrmsr_safe(IA32_L3_CBM_BASE, max_cbm, 0))
  108. return false;
  109. rdmsr(IA32_L3_CBM_BASE, l, h);
  110. /* If all the bits were set in MSR, return success */
  111. if (l != max_cbm)
  112. return false;
  113. r->num_closid = 4;
  114. r->cbm_len = 20;
  115. r->max_cbm = max_cbm;
  116. r->min_cbm_bits = 2;
  117. r->capable = true;
  118. r->enabled = true;
  119. return true;
  120. }
  121. return false;
  122. }
  123. static void rdt_get_config(int idx, struct rdt_resource *r)
  124. {
  125. union cpuid_0x10_1_eax eax;
  126. union cpuid_0x10_1_edx edx;
  127. u32 ebx, ecx;
  128. cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
  129. r->num_closid = edx.split.cos_max + 1;
  130. r->cbm_len = eax.split.cbm_len + 1;
  131. r->max_cbm = BIT_MASK(eax.split.cbm_len + 1) - 1;
  132. r->data_width = (r->cbm_len + 3) / 4;
  133. r->capable = true;
  134. r->enabled = true;
  135. }
  136. static void rdt_get_cdp_l3_config(int type)
  137. {
  138. struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
  139. struct rdt_resource *r = &rdt_resources_all[type];
  140. r->num_closid = r_l3->num_closid / 2;
  141. r->cbm_len = r_l3->cbm_len;
  142. r->max_cbm = r_l3->max_cbm;
  143. r->data_width = (r->cbm_len + 3) / 4;
  144. r->capable = true;
  145. /*
  146. * By default, CDP is disabled. CDP can be enabled by mount parameter
  147. * "cdp" during resctrl file system mount time.
  148. */
  149. r->enabled = false;
  150. }
  151. /**
  152. * Choose a width for the resource name
  153. * and resource data based on the resource that has
  154. * widest name and cbm.
  155. */
  156. static void rdt_init_padding(void)
  157. {
  158. struct rdt_resource *r;
  159. int cl;
  160. for_each_enabled_rdt_resource(r) {
  161. cl = strlen(r->name);
  162. if (cl > max_name_width)
  163. max_name_width = cl;
  164. if (r->data_width > max_data_width)
  165. max_data_width = r->data_width;
  166. }
  167. }
  168. static inline bool get_rdt_resources(void)
  169. {
  170. bool ret = false;
  171. if (cache_alloc_hsw_probe())
  172. return true;
  173. if (!boot_cpu_has(X86_FEATURE_RDT_A))
  174. return false;
  175. if (boot_cpu_has(X86_FEATURE_CAT_L3)) {
  176. rdt_get_config(1, &rdt_resources_all[RDT_RESOURCE_L3]);
  177. if (boot_cpu_has(X86_FEATURE_CDP_L3)) {
  178. rdt_get_cdp_l3_config(RDT_RESOURCE_L3DATA);
  179. rdt_get_cdp_l3_config(RDT_RESOURCE_L3CODE);
  180. }
  181. ret = true;
  182. }
  183. if (boot_cpu_has(X86_FEATURE_CAT_L2)) {
  184. /* CPUID 0x10.2 fields are same format at 0x10.1 */
  185. rdt_get_config(2, &rdt_resources_all[RDT_RESOURCE_L2]);
  186. ret = true;
  187. }
  188. return ret;
  189. }
  190. static int get_cache_id(int cpu, int level)
  191. {
  192. struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
  193. int i;
  194. for (i = 0; i < ci->num_leaves; i++) {
  195. if (ci->info_list[i].level == level)
  196. return ci->info_list[i].id;
  197. }
  198. return -1;
  199. }
  200. void rdt_cbm_update(void *arg)
  201. {
  202. struct msr_param *m = (struct msr_param *)arg;
  203. struct rdt_resource *r = m->res;
  204. int i, cpu = smp_processor_id();
  205. struct rdt_domain *d;
  206. list_for_each_entry(d, &r->domains, list) {
  207. /* Find the domain that contains this CPU */
  208. if (cpumask_test_cpu(cpu, &d->cpu_mask))
  209. goto found;
  210. }
  211. pr_info_once("cpu %d not found in any domain for resource %s\n",
  212. cpu, r->name);
  213. return;
  214. found:
  215. for (i = m->low; i < m->high; i++) {
  216. int idx = cbm_idx(r, i);
  217. wrmsrl(r->msr_base + idx, d->cbm[i]);
  218. }
  219. }
  220. /*
  221. * rdt_find_domain - Find a domain in a resource that matches input resource id
  222. *
  223. * Search resource r's domain list to find the resource id. If the resource
  224. * id is found in a domain, return the domain. Otherwise, if requested by
  225. * caller, return the first domain whose id is bigger than the input id.
  226. * The domain list is sorted by id in ascending order.
  227. */
  228. static struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
  229. struct list_head **pos)
  230. {
  231. struct rdt_domain *d;
  232. struct list_head *l;
  233. if (id < 0)
  234. return ERR_PTR(id);
  235. list_for_each(l, &r->domains) {
  236. d = list_entry(l, struct rdt_domain, list);
  237. /* When id is found, return its domain. */
  238. if (id == d->id)
  239. return d;
  240. /* Stop searching when finding id's position in sorted list. */
  241. if (id < d->id)
  242. break;
  243. }
  244. if (pos)
  245. *pos = l;
  246. return NULL;
  247. }
  248. /*
  249. * domain_add_cpu - Add a cpu to a resource's domain list.
  250. *
  251. * If an existing domain in the resource r's domain list matches the cpu's
  252. * resource id, add the cpu in the domain.
  253. *
  254. * Otherwise, a new domain is allocated and inserted into the right position
  255. * in the domain list sorted by id in ascending order.
  256. *
  257. * The order in the domain list is visible to users when we print entries
  258. * in the schemata file and schemata input is validated to have the same order
  259. * as this list.
  260. */
  261. static void domain_add_cpu(int cpu, struct rdt_resource *r)
  262. {
  263. int i, id = get_cache_id(cpu, r->cache_level);
  264. struct list_head *add_pos = NULL;
  265. struct rdt_domain *d;
  266. d = rdt_find_domain(r, id, &add_pos);
  267. if (IS_ERR(d)) {
  268. pr_warn("Could't find cache id for cpu %d\n", cpu);
  269. return;
  270. }
  271. if (d) {
  272. cpumask_set_cpu(cpu, &d->cpu_mask);
  273. return;
  274. }
  275. d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
  276. if (!d)
  277. return;
  278. d->id = id;
  279. d->cbm = kmalloc_array(r->num_closid, sizeof(*d->cbm), GFP_KERNEL);
  280. if (!d->cbm) {
  281. kfree(d);
  282. return;
  283. }
  284. for (i = 0; i < r->num_closid; i++) {
  285. int idx = cbm_idx(r, i);
  286. d->cbm[i] = r->max_cbm;
  287. wrmsrl(r->msr_base + idx, d->cbm[i]);
  288. }
  289. cpumask_set_cpu(cpu, &d->cpu_mask);
  290. list_add_tail(&d->list, add_pos);
  291. }
  292. static void domain_remove_cpu(int cpu, struct rdt_resource *r)
  293. {
  294. int id = get_cache_id(cpu, r->cache_level);
  295. struct rdt_domain *d;
  296. d = rdt_find_domain(r, id, NULL);
  297. if (IS_ERR_OR_NULL(d)) {
  298. pr_warn("Could't find cache id for cpu %d\n", cpu);
  299. return;
  300. }
  301. cpumask_clear_cpu(cpu, &d->cpu_mask);
  302. if (cpumask_empty(&d->cpu_mask)) {
  303. kfree(d->cbm);
  304. list_del(&d->list);
  305. kfree(d);
  306. }
  307. }
  308. static void clear_closid(int cpu)
  309. {
  310. struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
  311. per_cpu(cpu_closid, cpu) = 0;
  312. state->closid = 0;
  313. wrmsr(MSR_IA32_PQR_ASSOC, state->rmid, 0);
  314. }
  315. static int intel_rdt_online_cpu(unsigned int cpu)
  316. {
  317. struct rdt_resource *r;
  318. mutex_lock(&rdtgroup_mutex);
  319. for_each_capable_rdt_resource(r)
  320. domain_add_cpu(cpu, r);
  321. /* The cpu is set in default rdtgroup after online. */
  322. cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
  323. clear_closid(cpu);
  324. mutex_unlock(&rdtgroup_mutex);
  325. return 0;
  326. }
  327. static int intel_rdt_offline_cpu(unsigned int cpu)
  328. {
  329. struct rdtgroup *rdtgrp;
  330. struct rdt_resource *r;
  331. mutex_lock(&rdtgroup_mutex);
  332. for_each_capable_rdt_resource(r)
  333. domain_remove_cpu(cpu, r);
  334. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  335. if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask))
  336. break;
  337. }
  338. clear_closid(cpu);
  339. mutex_unlock(&rdtgroup_mutex);
  340. return 0;
  341. }
  342. static int __init intel_rdt_late_init(void)
  343. {
  344. struct rdt_resource *r;
  345. int state, ret;
  346. if (!get_rdt_resources())
  347. return -ENODEV;
  348. rdt_init_padding();
  349. state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  350. "x86/rdt/cat:online:",
  351. intel_rdt_online_cpu, intel_rdt_offline_cpu);
  352. if (state < 0)
  353. return state;
  354. ret = rdtgroup_init();
  355. if (ret) {
  356. cpuhp_remove_state(state);
  357. return ret;
  358. }
  359. for_each_capable_rdt_resource(r)
  360. pr_info("Intel RDT %s allocation detected\n", r->name);
  361. return 0;
  362. }
  363. late_initcall(intel_rdt_late_init);