intel_rdt_ctrlmondata.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
  52. r->membw.min_bw, r->default_ctrl);
  53. return false;
  54. }
  55. *data = roundup(bw, (unsigned long)r->membw.bw_gran);
  56. return true;
  57. }
  58. int parse_bw(char *buf, struct rdt_resource *r, struct rdt_domain *d)
  59. {
  60. unsigned long data;
  61. if (d->have_new_ctrl) {
  62. rdt_last_cmd_printf("duplicate domain %d\n", d->id);
  63. return -EINVAL;
  64. }
  65. if (!bw_validate(buf, &data, r))
  66. return -EINVAL;
  67. d->new_ctrl = data;
  68. d->have_new_ctrl = true;
  69. return 0;
  70. }
  71. /*
  72. * Check whether a cache bit mask is valid. The SDM says:
  73. * Please note that all (and only) contiguous '1' combinations
  74. * are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
  75. * Additionally Haswell requires at least two bits set.
  76. */
  77. static bool cbm_validate(char *buf, unsigned long *data, struct rdt_resource *r)
  78. {
  79. unsigned long first_bit, zero_bit, val;
  80. unsigned int cbm_len = r->cache.cbm_len;
  81. int ret;
  82. ret = kstrtoul(buf, 16, &val);
  83. if (ret) {
  84. rdt_last_cmd_printf("non-hex character in mask %s\n", buf);
  85. return false;
  86. }
  87. if (val == 0 || val > r->default_ctrl) {
  88. rdt_last_cmd_puts("mask out of range\n");
  89. return false;
  90. }
  91. first_bit = find_first_bit(&val, cbm_len);
  92. zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
  93. if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len) {
  94. rdt_last_cmd_printf("mask %lx has non-consecutive 1-bits\n", val);
  95. return false;
  96. }
  97. if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
  98. rdt_last_cmd_printf("Need at least %d bits in mask\n",
  99. r->cache.min_cbm_bits);
  100. return false;
  101. }
  102. *data = val;
  103. return true;
  104. }
  105. /*
  106. * Read one cache bit mask (hex). Check that it is valid for the current
  107. * resource type.
  108. */
  109. int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d)
  110. {
  111. unsigned long data;
  112. if (d->have_new_ctrl) {
  113. rdt_last_cmd_printf("duplicate domain %d\n", d->id);
  114. return -EINVAL;
  115. }
  116. if(!cbm_validate(buf, &data, r))
  117. return -EINVAL;
  118. d->new_ctrl = data;
  119. d->have_new_ctrl = true;
  120. return 0;
  121. }
  122. /*
  123. * For each domain in this resource we expect to find a series of:
  124. * id=mask
  125. * separated by ";". The "id" is in decimal, and must match one of
  126. * the "id"s for this resource.
  127. */
  128. static int parse_line(char *line, struct rdt_resource *r)
  129. {
  130. char *dom = NULL, *id;
  131. struct rdt_domain *d;
  132. unsigned long dom_id;
  133. next:
  134. if (!line || line[0] == '\0')
  135. return 0;
  136. dom = strsep(&line, ";");
  137. id = strsep(&dom, "=");
  138. if (!dom || kstrtoul(id, 10, &dom_id)) {
  139. rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
  140. return -EINVAL;
  141. }
  142. dom = strim(dom);
  143. list_for_each_entry(d, &r->domains, list) {
  144. if (d->id == dom_id) {
  145. if (r->parse_ctrlval(dom, r, d))
  146. return -EINVAL;
  147. goto next;
  148. }
  149. }
  150. return -EINVAL;
  151. }
  152. static int update_domains(struct rdt_resource *r, int closid)
  153. {
  154. struct msr_param msr_param;
  155. cpumask_var_t cpu_mask;
  156. struct rdt_domain *d;
  157. int cpu;
  158. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  159. return -ENOMEM;
  160. msr_param.low = closid;
  161. msr_param.high = msr_param.low + 1;
  162. msr_param.res = r;
  163. list_for_each_entry(d, &r->domains, list) {
  164. if (d->have_new_ctrl && d->new_ctrl != d->ctrl_val[closid]) {
  165. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  166. d->ctrl_val[closid] = d->new_ctrl;
  167. }
  168. }
  169. if (cpumask_empty(cpu_mask))
  170. goto done;
  171. cpu = get_cpu();
  172. /* Update CBM on this cpu if it's in cpu_mask. */
  173. if (cpumask_test_cpu(cpu, cpu_mask))
  174. rdt_ctrl_update(&msr_param);
  175. /* Update CBM on other cpus. */
  176. smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
  177. put_cpu();
  178. done:
  179. free_cpumask_var(cpu_mask);
  180. return 0;
  181. }
  182. static int rdtgroup_parse_resource(char *resname, char *tok, int closid)
  183. {
  184. struct rdt_resource *r;
  185. for_each_alloc_enabled_rdt_resource(r) {
  186. if (!strcmp(resname, r->name) && closid < r->num_closid)
  187. return parse_line(tok, r);
  188. }
  189. rdt_last_cmd_printf("unknown/unsupported resource name '%s'\n", resname);
  190. return -EINVAL;
  191. }
  192. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  193. char *buf, size_t nbytes, loff_t off)
  194. {
  195. struct rdtgroup *rdtgrp;
  196. struct rdt_domain *dom;
  197. struct rdt_resource *r;
  198. char *tok, *resname;
  199. int closid, ret = 0;
  200. /* Valid input requires a trailing newline */
  201. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  202. return -EINVAL;
  203. buf[nbytes - 1] = '\0';
  204. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  205. if (!rdtgrp) {
  206. rdtgroup_kn_unlock(of->kn);
  207. return -ENOENT;
  208. }
  209. rdt_last_cmd_clear();
  210. closid = rdtgrp->closid;
  211. for_each_alloc_enabled_rdt_resource(r) {
  212. list_for_each_entry(dom, &r->domains, list)
  213. dom->have_new_ctrl = false;
  214. }
  215. while ((tok = strsep(&buf, "\n")) != NULL) {
  216. resname = strim(strsep(&tok, ":"));
  217. if (!tok) {
  218. rdt_last_cmd_puts("Missing ':'\n");
  219. ret = -EINVAL;
  220. goto out;
  221. }
  222. if (tok[0] == '\0') {
  223. rdt_last_cmd_printf("Missing '%s' value\n", resname);
  224. ret = -EINVAL;
  225. goto out;
  226. }
  227. ret = rdtgroup_parse_resource(resname, tok, closid);
  228. if (ret)
  229. goto out;
  230. }
  231. for_each_alloc_enabled_rdt_resource(r) {
  232. ret = update_domains(r, closid);
  233. if (ret)
  234. goto out;
  235. }
  236. out:
  237. rdtgroup_kn_unlock(of->kn);
  238. return ret ?: nbytes;
  239. }
  240. static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
  241. {
  242. struct rdt_domain *dom;
  243. bool sep = false;
  244. seq_printf(s, "%*s:", max_name_width, r->name);
  245. list_for_each_entry(dom, &r->domains, list) {
  246. if (sep)
  247. seq_puts(s, ";");
  248. seq_printf(s, r->format_str, dom->id, max_data_width,
  249. dom->ctrl_val[closid]);
  250. sep = true;
  251. }
  252. seq_puts(s, "\n");
  253. }
  254. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  255. struct seq_file *s, void *v)
  256. {
  257. struct rdtgroup *rdtgrp;
  258. struct rdt_resource *r;
  259. int ret = 0;
  260. u32 closid;
  261. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  262. if (rdtgrp) {
  263. closid = rdtgrp->closid;
  264. for_each_alloc_enabled_rdt_resource(r) {
  265. if (closid < r->num_closid)
  266. show_doms(s, r, closid);
  267. }
  268. } else {
  269. ret = -ENOENT;
  270. }
  271. rdtgroup_kn_unlock(of->kn);
  272. return ret;
  273. }
  274. void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
  275. struct rdtgroup *rdtgrp, int evtid, int first)
  276. {
  277. /*
  278. * setup the parameters to send to the IPI to read the data.
  279. */
  280. rr->rgrp = rdtgrp;
  281. rr->evtid = evtid;
  282. rr->d = d;
  283. rr->val = 0;
  284. rr->first = first;
  285. smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
  286. }
  287. int rdtgroup_mondata_show(struct seq_file *m, void *arg)
  288. {
  289. struct kernfs_open_file *of = m->private;
  290. u32 resid, evtid, domid;
  291. struct rdtgroup *rdtgrp;
  292. struct rdt_resource *r;
  293. union mon_data_bits md;
  294. struct rdt_domain *d;
  295. struct rmid_read rr;
  296. int ret = 0;
  297. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  298. md.priv = of->kn->priv;
  299. resid = md.u.rid;
  300. domid = md.u.domid;
  301. evtid = md.u.evtid;
  302. r = &rdt_resources_all[resid];
  303. d = rdt_find_domain(r, domid, NULL);
  304. if (!d) {
  305. ret = -ENOENT;
  306. goto out;
  307. }
  308. mon_event_read(&rr, d, rdtgrp, evtid, false);
  309. if (rr.val & RMID_VAL_ERROR)
  310. seq_puts(m, "Error\n");
  311. else if (rr.val & RMID_VAL_UNAVAIL)
  312. seq_puts(m, "Unavailable\n");
  313. else
  314. seq_printf(m, "%llu\n", rr.val * r->mon_scale);
  315. out:
  316. rdtgroup_kn_unlock(of->kn);
  317. return ret;
  318. }