intel_rdt_ctrlmondata.c 8.8 KB

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