intel_rdt.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. struct rdt_resource rdt_resources_all[] = {
  36. {
  37. .name = "L3",
  38. .domains = domain_init(RDT_RESOURCE_L3),
  39. .msr_base = IA32_L3_CBM_BASE,
  40. .min_cbm_bits = 1,
  41. .cache_level = 3,
  42. .cbm_idx_multi = 1,
  43. .cbm_idx_offset = 0
  44. },
  45. {
  46. .name = "L3DATA",
  47. .domains = domain_init(RDT_RESOURCE_L3DATA),
  48. .msr_base = IA32_L3_CBM_BASE,
  49. .min_cbm_bits = 1,
  50. .cache_level = 3,
  51. .cbm_idx_multi = 2,
  52. .cbm_idx_offset = 0
  53. },
  54. {
  55. .name = "L3CODE",
  56. .domains = domain_init(RDT_RESOURCE_L3CODE),
  57. .msr_base = IA32_L3_CBM_BASE,
  58. .min_cbm_bits = 1,
  59. .cache_level = 3,
  60. .cbm_idx_multi = 2,
  61. .cbm_idx_offset = 1
  62. },
  63. {
  64. .name = "L2",
  65. .domains = domain_init(RDT_RESOURCE_L2),
  66. .msr_base = IA32_L2_CBM_BASE,
  67. .min_cbm_bits = 1,
  68. .cache_level = 2,
  69. .cbm_idx_multi = 1,
  70. .cbm_idx_offset = 0
  71. },
  72. };
  73. static int cbm_idx(struct rdt_resource *r, int closid)
  74. {
  75. return closid * r->cbm_idx_multi + r->cbm_idx_offset;
  76. }
  77. /*
  78. * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
  79. * as they do not have CPUID enumeration support for Cache allocation.
  80. * The check for Vendor/Family/Model is not enough to guarantee that
  81. * the MSRs won't #GP fault because only the following SKUs support
  82. * CAT:
  83. * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz
  84. * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz
  85. * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz
  86. * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz
  87. * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz
  88. * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz
  89. *
  90. * Probe by trying to write the first of the L3 cach mask registers
  91. * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
  92. * is always 20 on hsw server parts. The minimum cache bitmask length
  93. * allowed for HSW server is always 2 bits. Hardcode all of them.
  94. */
  95. static inline bool cache_alloc_hsw_probe(void)
  96. {
  97. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  98. boot_cpu_data.x86 == 6 &&
  99. boot_cpu_data.x86_model == INTEL_FAM6_HASWELL_X) {
  100. struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
  101. u32 l, h, max_cbm = BIT_MASK(20) - 1;
  102. if (wrmsr_safe(IA32_L3_CBM_BASE, max_cbm, 0))
  103. return false;
  104. rdmsr(IA32_L3_CBM_BASE, l, h);
  105. /* If all the bits were set in MSR, return success */
  106. if (l != max_cbm)
  107. return false;
  108. r->num_closid = 4;
  109. r->cbm_len = 20;
  110. r->max_cbm = max_cbm;
  111. r->min_cbm_bits = 2;
  112. r->capable = true;
  113. r->enabled = true;
  114. return true;
  115. }
  116. return false;
  117. }
  118. static void rdt_get_config(int idx, struct rdt_resource *r)
  119. {
  120. union cpuid_0x10_1_eax eax;
  121. union cpuid_0x10_1_edx edx;
  122. u32 ebx, ecx;
  123. cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
  124. r->num_closid = edx.split.cos_max + 1;
  125. r->cbm_len = eax.split.cbm_len + 1;
  126. r->max_cbm = BIT_MASK(eax.split.cbm_len + 1) - 1;
  127. r->capable = true;
  128. r->enabled = true;
  129. }
  130. static void rdt_get_cdp_l3_config(int type)
  131. {
  132. struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
  133. struct rdt_resource *r = &rdt_resources_all[type];
  134. r->num_closid = r_l3->num_closid / 2;
  135. r->cbm_len = r_l3->cbm_len;
  136. r->max_cbm = r_l3->max_cbm;
  137. r->capable = true;
  138. /*
  139. * By default, CDP is disabled. CDP can be enabled by mount parameter
  140. * "cdp" during resctrl file system mount time.
  141. */
  142. r->enabled = false;
  143. }
  144. static inline bool get_rdt_resources(void)
  145. {
  146. bool ret = false;
  147. if (cache_alloc_hsw_probe())
  148. return true;
  149. if (!boot_cpu_has(X86_FEATURE_RDT_A))
  150. return false;
  151. if (boot_cpu_has(X86_FEATURE_CAT_L3)) {
  152. rdt_get_config(1, &rdt_resources_all[RDT_RESOURCE_L3]);
  153. if (boot_cpu_has(X86_FEATURE_CDP_L3)) {
  154. rdt_get_cdp_l3_config(RDT_RESOURCE_L3DATA);
  155. rdt_get_cdp_l3_config(RDT_RESOURCE_L3CODE);
  156. }
  157. ret = true;
  158. }
  159. if (boot_cpu_has(X86_FEATURE_CAT_L2)) {
  160. /* CPUID 0x10.2 fields are same format at 0x10.1 */
  161. rdt_get_config(2, &rdt_resources_all[RDT_RESOURCE_L2]);
  162. ret = true;
  163. }
  164. return ret;
  165. }
  166. static int get_cache_id(int cpu, int level)
  167. {
  168. struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
  169. int i;
  170. for (i = 0; i < ci->num_leaves; i++) {
  171. if (ci->info_list[i].level == level)
  172. return ci->info_list[i].id;
  173. }
  174. return -1;
  175. }
  176. void rdt_cbm_update(void *arg)
  177. {
  178. struct msr_param *m = (struct msr_param *)arg;
  179. struct rdt_resource *r = m->res;
  180. int i, cpu = smp_processor_id();
  181. struct rdt_domain *d;
  182. list_for_each_entry(d, &r->domains, list) {
  183. /* Find the domain that contains this CPU */
  184. if (cpumask_test_cpu(cpu, &d->cpu_mask))
  185. goto found;
  186. }
  187. pr_info_once("cpu %d not found in any domain for resource %s\n",
  188. cpu, r->name);
  189. return;
  190. found:
  191. for (i = m->low; i < m->high; i++) {
  192. int idx = cbm_idx(r, i);
  193. wrmsrl(r->msr_base + idx, d->cbm[i]);
  194. }
  195. }
  196. /*
  197. * rdt_find_domain - Find a domain in a resource that matches input resource id
  198. *
  199. * Search resource r's domain list to find the resource id. If the resource
  200. * id is found in a domain, return the domain. Otherwise, if requested by
  201. * caller, return the first domain whose id is bigger than the input id.
  202. * The domain list is sorted by id in ascending order.
  203. */
  204. static struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
  205. struct list_head **pos)
  206. {
  207. struct rdt_domain *d;
  208. struct list_head *l;
  209. if (id < 0)
  210. return ERR_PTR(id);
  211. list_for_each(l, &r->domains) {
  212. d = list_entry(l, struct rdt_domain, list);
  213. /* When id is found, return its domain. */
  214. if (id == d->id)
  215. return d;
  216. /* Stop searching when finding id's position in sorted list. */
  217. if (id < d->id)
  218. break;
  219. }
  220. if (pos)
  221. *pos = l;
  222. return NULL;
  223. }
  224. /*
  225. * domain_add_cpu - Add a cpu to a resource's domain list.
  226. *
  227. * If an existing domain in the resource r's domain list matches the cpu's
  228. * resource id, add the cpu in the domain.
  229. *
  230. * Otherwise, a new domain is allocated and inserted into the right position
  231. * in the domain list sorted by id in ascending order.
  232. *
  233. * The order in the domain list is visible to users when we print entries
  234. * in the schemata file and schemata input is validated to have the same order
  235. * as this list.
  236. */
  237. static void domain_add_cpu(int cpu, struct rdt_resource *r)
  238. {
  239. int i, id = get_cache_id(cpu, r->cache_level);
  240. struct list_head *add_pos = NULL;
  241. struct rdt_domain *d;
  242. d = rdt_find_domain(r, id, &add_pos);
  243. if (IS_ERR(d)) {
  244. pr_warn("Could't find cache id for cpu %d\n", cpu);
  245. return;
  246. }
  247. if (d) {
  248. cpumask_set_cpu(cpu, &d->cpu_mask);
  249. return;
  250. }
  251. d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
  252. if (!d)
  253. return;
  254. d->id = id;
  255. d->cbm = kmalloc_array(r->num_closid, sizeof(*d->cbm), GFP_KERNEL);
  256. if (!d->cbm) {
  257. kfree(d);
  258. return;
  259. }
  260. for (i = 0; i < r->num_closid; i++) {
  261. int idx = cbm_idx(r, i);
  262. d->cbm[i] = r->max_cbm;
  263. wrmsrl(r->msr_base + idx, d->cbm[i]);
  264. }
  265. cpumask_set_cpu(cpu, &d->cpu_mask);
  266. list_add_tail(&d->list, add_pos);
  267. r->num_domains++;
  268. }
  269. static void domain_remove_cpu(int cpu, struct rdt_resource *r)
  270. {
  271. int id = get_cache_id(cpu, r->cache_level);
  272. struct rdt_domain *d;
  273. d = rdt_find_domain(r, id, NULL);
  274. if (IS_ERR_OR_NULL(d)) {
  275. pr_warn("Could't find cache id for cpu %d\n", cpu);
  276. return;
  277. }
  278. cpumask_clear_cpu(cpu, &d->cpu_mask);
  279. if (cpumask_empty(&d->cpu_mask)) {
  280. r->num_domains--;
  281. kfree(d->cbm);
  282. list_del(&d->list);
  283. kfree(d);
  284. }
  285. }
  286. static void clear_closid(int cpu)
  287. {
  288. struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
  289. per_cpu(cpu_closid, cpu) = 0;
  290. state->closid = 0;
  291. wrmsr(MSR_IA32_PQR_ASSOC, state->rmid, 0);
  292. }
  293. static int intel_rdt_online_cpu(unsigned int cpu)
  294. {
  295. struct rdt_resource *r;
  296. mutex_lock(&rdtgroup_mutex);
  297. for_each_capable_rdt_resource(r)
  298. domain_add_cpu(cpu, r);
  299. /* The cpu is set in default rdtgroup after online. */
  300. cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
  301. clear_closid(cpu);
  302. mutex_unlock(&rdtgroup_mutex);
  303. return 0;
  304. }
  305. static int intel_rdt_offline_cpu(unsigned int cpu)
  306. {
  307. struct rdtgroup *rdtgrp;
  308. struct rdt_resource *r;
  309. mutex_lock(&rdtgroup_mutex);
  310. for_each_capable_rdt_resource(r)
  311. domain_remove_cpu(cpu, r);
  312. list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
  313. if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask))
  314. break;
  315. }
  316. clear_closid(cpu);
  317. mutex_unlock(&rdtgroup_mutex);
  318. return 0;
  319. }
  320. static int __init intel_rdt_late_init(void)
  321. {
  322. struct rdt_resource *r;
  323. int state, ret;
  324. if (!get_rdt_resources())
  325. return -ENODEV;
  326. state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  327. "x86/rdt/cat:online:",
  328. intel_rdt_online_cpu, intel_rdt_offline_cpu);
  329. if (state < 0)
  330. return state;
  331. ret = rdtgroup_init();
  332. if (ret) {
  333. cpuhp_remove_state(state);
  334. return ret;
  335. }
  336. for_each_capable_rdt_resource(r)
  337. pr_info("Intel RDT %s allocation detected\n", r->name);
  338. return 0;
  339. }
  340. late_initcall(intel_rdt_late_init);