intel_rdt_ctrlmondata.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * More information about RDT be found in the Intel (R) x86 Architecture
  21. * Software Developer Manual June 2016, volume 3, section 17.17.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/cpu.h>
  25. #include <linux/kernfs.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/slab.h>
  28. #include "intel_rdt.h"
  29. /*
  30. * Check whether MBA bandwidth percentage value is correct. The value is
  31. * checked against the minimum and max bandwidth values specified by the
  32. * hardware. The allocated bandwidth percentage is rounded to the next
  33. * control step available on the hardware.
  34. */
  35. static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
  36. {
  37. unsigned long bw;
  38. int ret;
  39. /*
  40. * Only linear delay values is supported for current Intel SKUs.
  41. */
  42. if (!r->membw.delay_linear) {
  43. rdt_last_cmd_puts("No support for non-linear MB domains\n");
  44. return false;
  45. }
  46. ret = kstrtoul(buf, 10, &bw);
  47. if (ret) {
  48. rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
  49. return false;
  50. }
  51. if ((bw < r->membw.min_bw || bw > r->default_ctrl) &&
  52. !is_mba_sc(r)) {
  53. rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
  54. r->membw.min_bw, r->default_ctrl);
  55. return false;
  56. }
  57. *data = roundup(bw, (unsigned long)r->membw.bw_gran);
  58. return true;
  59. }
  60. int parse_bw(struct rdt_parse_data *data, struct rdt_resource *r,
  61. struct rdt_domain *d)
  62. {
  63. unsigned long bw_val;
  64. if (d->have_new_ctrl) {
  65. rdt_last_cmd_printf("duplicate domain %d\n", d->id);
  66. return -EINVAL;
  67. }
  68. if (!bw_validate(data->buf, &bw_val, r))
  69. return -EINVAL;
  70. d->new_ctrl = bw_val;
  71. d->have_new_ctrl = true;
  72. return 0;
  73. }
  74. /*
  75. * Check whether a cache bit mask is valid. The SDM says:
  76. * Please note that all (and only) contiguous '1' combinations
  77. * are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
  78. * Additionally Haswell requires at least two bits set.
  79. */
  80. static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
  81. {
  82. unsigned long first_bit, zero_bit, val;
  83. unsigned int cbm_len = r->cache.cbm_len;
  84. int ret;
  85. ret = kstrtoul(buf, 16, &val);
  86. if (ret) {
  87. rdt_last_cmd_printf("non-hex character in mask %s\n", buf);
  88. return false;
  89. }
  90. if (val == 0 || val > r->default_ctrl) {
  91. rdt_last_cmd_puts("mask out of range\n");
  92. return false;
  93. }
  94. first_bit = find_first_bit(&val, cbm_len);
  95. zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
  96. if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len) {
  97. rdt_last_cmd_printf("mask %lx has non-consecutive 1-bits\n", val);
  98. return false;
  99. }
  100. if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
  101. rdt_last_cmd_printf("Need at least %d bits in mask\n",
  102. r->cache.min_cbm_bits);
  103. return false;
  104. }
  105. *data = val;
  106. return true;
  107. }
  108. /*
  109. * Read one cache bit mask (hex). Check that it is valid for the current
  110. * resource type.
  111. */
  112. int parse_cbm(struct rdt_parse_data *data, struct rdt_resource *r,
  113. struct rdt_domain *d)
  114. {
  115. struct rdtgroup *rdtgrp = data->rdtgrp;
  116. u32 cbm_val;
  117. if (d->have_new_ctrl) {
  118. rdt_last_cmd_printf("duplicate domain %d\n", d->id);
  119. return -EINVAL;
  120. }
  121. /*
  122. * Cannot set up more than one pseudo-locked region in a cache
  123. * hierarchy.
  124. */
  125. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
  126. rdtgroup_pseudo_locked_in_hierarchy(d)) {
  127. rdt_last_cmd_printf("pseudo-locked region in hierarchy\n");
  128. return -EINVAL;
  129. }
  130. if (!cbm_validate(data->buf, &cbm_val, r))
  131. return -EINVAL;
  132. if ((rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
  133. rdtgrp->mode == RDT_MODE_SHAREABLE) &&
  134. rdtgroup_cbm_overlaps_pseudo_locked(d, cbm_val)) {
  135. rdt_last_cmd_printf("CBM overlaps with pseudo-locked region\n");
  136. return -EINVAL;
  137. }
  138. /*
  139. * The CBM may not overlap with the CBM of another closid if
  140. * either is exclusive.
  141. */
  142. if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, true)) {
  143. rdt_last_cmd_printf("overlaps with exclusive group\n");
  144. return -EINVAL;
  145. }
  146. if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, false)) {
  147. if (rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
  148. rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  149. rdt_last_cmd_printf("overlaps with other group\n");
  150. return -EINVAL;
  151. }
  152. }
  153. d->new_ctrl = cbm_val;
  154. d->have_new_ctrl = true;
  155. return 0;
  156. }
  157. /*
  158. * For each domain in this resource we expect to find a series of:
  159. * id=mask
  160. * separated by ";". The "id" is in decimal, and must match one of
  161. * the "id"s for this resource.
  162. */
  163. static int parse_line(char *line, struct rdt_resource *r,
  164. struct rdtgroup *rdtgrp)
  165. {
  166. struct rdt_parse_data data;
  167. char *dom = NULL, *id;
  168. struct rdt_domain *d;
  169. unsigned long dom_id;
  170. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
  171. r->rid == RDT_RESOURCE_MBA) {
  172. rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
  173. return -EINVAL;
  174. }
  175. next:
  176. if (!line || line[0] == '\0')
  177. return 0;
  178. dom = strsep(&line, ";");
  179. id = strsep(&dom, "=");
  180. if (!dom || kstrtoul(id, 10, &dom_id)) {
  181. rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
  182. return -EINVAL;
  183. }
  184. dom = strim(dom);
  185. list_for_each_entry(d, &r->domains, list) {
  186. if (d->id == dom_id) {
  187. data.buf = dom;
  188. data.rdtgrp = rdtgrp;
  189. if (r->parse_ctrlval(&data, r, d))
  190. return -EINVAL;
  191. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  192. /*
  193. * In pseudo-locking setup mode and just
  194. * parsed a valid CBM that should be
  195. * pseudo-locked. Only one locked region per
  196. * resource group and domain so just do
  197. * the required initialization for single
  198. * region and return.
  199. */
  200. rdtgrp->plr->r = r;
  201. rdtgrp->plr->d = d;
  202. rdtgrp->plr->cbm = d->new_ctrl;
  203. d->plr = rdtgrp->plr;
  204. return 0;
  205. }
  206. goto next;
  207. }
  208. }
  209. return -EINVAL;
  210. }
  211. int update_domains(struct rdt_resource *r, int closid)
  212. {
  213. struct msr_param msr_param;
  214. cpumask_var_t cpu_mask;
  215. struct rdt_domain *d;
  216. bool mba_sc;
  217. u32 *dc;
  218. int cpu;
  219. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  220. return -ENOMEM;
  221. msr_param.low = closid;
  222. msr_param.high = msr_param.low + 1;
  223. msr_param.res = r;
  224. mba_sc = is_mba_sc(r);
  225. list_for_each_entry(d, &r->domains, list) {
  226. dc = !mba_sc ? d->ctrl_val : d->mbps_val;
  227. if (d->have_new_ctrl && d->new_ctrl != dc[closid]) {
  228. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  229. dc[closid] = d->new_ctrl;
  230. }
  231. }
  232. /*
  233. * Avoid writing the control msr with control values when
  234. * MBA software controller is enabled
  235. */
  236. if (cpumask_empty(cpu_mask) || mba_sc)
  237. goto done;
  238. cpu = get_cpu();
  239. /* Update CBM on this cpu if it's in cpu_mask. */
  240. if (cpumask_test_cpu(cpu, cpu_mask))
  241. rdt_ctrl_update(&msr_param);
  242. /* Update CBM on other cpus. */
  243. smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
  244. put_cpu();
  245. done:
  246. free_cpumask_var(cpu_mask);
  247. return 0;
  248. }
  249. static int rdtgroup_parse_resource(char *resname, char *tok,
  250. struct rdtgroup *rdtgrp)
  251. {
  252. struct rdt_resource *r;
  253. for_each_alloc_enabled_rdt_resource(r) {
  254. if (!strcmp(resname, r->name) && rdtgrp->closid < r->num_closid)
  255. return parse_line(tok, r, rdtgrp);
  256. }
  257. rdt_last_cmd_printf("unknown/unsupported resource name '%s'\n", resname);
  258. return -EINVAL;
  259. }
  260. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  261. char *buf, size_t nbytes, loff_t off)
  262. {
  263. struct rdtgroup *rdtgrp;
  264. struct rdt_domain *dom;
  265. struct rdt_resource *r;
  266. char *tok, *resname;
  267. int ret = 0;
  268. /* Valid input requires a trailing newline */
  269. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  270. return -EINVAL;
  271. buf[nbytes - 1] = '\0';
  272. cpus_read_lock();
  273. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  274. if (!rdtgrp) {
  275. rdtgroup_kn_unlock(of->kn);
  276. cpus_read_unlock();
  277. return -ENOENT;
  278. }
  279. rdt_last_cmd_clear();
  280. /*
  281. * No changes to pseudo-locked region allowed. It has to be removed
  282. * and re-created instead.
  283. */
  284. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  285. ret = -EINVAL;
  286. rdt_last_cmd_puts("resource group is pseudo-locked\n");
  287. goto out;
  288. }
  289. for_each_alloc_enabled_rdt_resource(r) {
  290. list_for_each_entry(dom, &r->domains, list)
  291. dom->have_new_ctrl = false;
  292. }
  293. while ((tok = strsep(&buf, "\n")) != NULL) {
  294. resname = strim(strsep(&tok, ":"));
  295. if (!tok) {
  296. rdt_last_cmd_puts("Missing ':'\n");
  297. ret = -EINVAL;
  298. goto out;
  299. }
  300. if (tok[0] == '\0') {
  301. rdt_last_cmd_printf("Missing '%s' value\n", resname);
  302. ret = -EINVAL;
  303. goto out;
  304. }
  305. ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
  306. if (ret)
  307. goto out;
  308. }
  309. for_each_alloc_enabled_rdt_resource(r) {
  310. ret = update_domains(r, rdtgrp->closid);
  311. if (ret)
  312. goto out;
  313. }
  314. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  315. /*
  316. * If pseudo-locking fails we keep the resource group in
  317. * mode RDT_MODE_PSEUDO_LOCKSETUP with its class of service
  318. * active and updated for just the domain the pseudo-locked
  319. * region was requested for.
  320. */
  321. ret = rdtgroup_pseudo_lock_create(rdtgrp);
  322. }
  323. out:
  324. rdtgroup_kn_unlock(of->kn);
  325. cpus_read_unlock();
  326. return ret ?: nbytes;
  327. }
  328. static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
  329. {
  330. struct rdt_domain *dom;
  331. bool sep = false;
  332. u32 ctrl_val;
  333. seq_printf(s, "%*s:", max_name_width, r->name);
  334. list_for_each_entry(dom, &r->domains, list) {
  335. if (sep)
  336. seq_puts(s, ";");
  337. ctrl_val = (!is_mba_sc(r) ? dom->ctrl_val[closid] :
  338. dom->mbps_val[closid]);
  339. seq_printf(s, r->format_str, dom->id, max_data_width,
  340. ctrl_val);
  341. sep = true;
  342. }
  343. seq_puts(s, "\n");
  344. }
  345. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  346. struct seq_file *s, void *v)
  347. {
  348. struct rdtgroup *rdtgrp;
  349. struct rdt_resource *r;
  350. int ret = 0;
  351. u32 closid;
  352. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  353. if (rdtgrp) {
  354. if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
  355. for_each_alloc_enabled_rdt_resource(r)
  356. seq_printf(s, "%s:uninitialized\n", r->name);
  357. } else if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
  358. if (!rdtgrp->plr->d) {
  359. rdt_last_cmd_clear();
  360. rdt_last_cmd_puts("Cache domain offline\n");
  361. ret = -ENODEV;
  362. } else {
  363. seq_printf(s, "%s:%d=%x\n",
  364. rdtgrp->plr->r->name,
  365. rdtgrp->plr->d->id,
  366. rdtgrp->plr->cbm);
  367. }
  368. } else {
  369. closid = rdtgrp->closid;
  370. for_each_alloc_enabled_rdt_resource(r) {
  371. if (closid < r->num_closid)
  372. show_doms(s, r, closid);
  373. }
  374. }
  375. } else {
  376. ret = -ENOENT;
  377. }
  378. rdtgroup_kn_unlock(of->kn);
  379. return ret;
  380. }
  381. void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
  382. struct rdtgroup *rdtgrp, int evtid, int first)
  383. {
  384. /*
  385. * setup the parameters to send to the IPI to read the data.
  386. */
  387. rr->rgrp = rdtgrp;
  388. rr->evtid = evtid;
  389. rr->d = d;
  390. rr->val = 0;
  391. rr->first = first;
  392. smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
  393. }
  394. int rdtgroup_mondata_show(struct seq_file *m, void *arg)
  395. {
  396. struct kernfs_open_file *of = m->private;
  397. u32 resid, evtid, domid;
  398. struct rdtgroup *rdtgrp;
  399. struct rdt_resource *r;
  400. union mon_data_bits md;
  401. struct rdt_domain *d;
  402. struct rmid_read rr;
  403. int ret = 0;
  404. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  405. if (!rdtgrp) {
  406. ret = -ENOENT;
  407. goto out;
  408. }
  409. md.priv = of->kn->priv;
  410. resid = md.u.rid;
  411. domid = md.u.domid;
  412. evtid = md.u.evtid;
  413. r = &rdt_resources_all[resid];
  414. d = rdt_find_domain(r, domid, NULL);
  415. if (IS_ERR_OR_NULL(d)) {
  416. ret = -ENOENT;
  417. goto out;
  418. }
  419. mon_event_read(&rr, d, rdtgrp, evtid, false);
  420. if (rr.val & RMID_VAL_ERROR)
  421. seq_puts(m, "Error\n");
  422. else if (rr.val & RMID_VAL_UNAVAIL)
  423. seq_puts(m, "Unavailable\n");
  424. else
  425. seq_printf(m, "%llu\n", rr.val * r->mon_scale);
  426. out:
  427. rdtgroup_kn_unlock(of->kn);
  428. return ret;
  429. }