intel_rdt_ctrlmondata.c 12 KB

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