intel_rdt_schemata.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <asm/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. return false;
  43. ret = kstrtoul(buf, 10, &bw);
  44. if (ret)
  45. return false;
  46. if (bw < r->membw.min_bw || bw > r->default_ctrl)
  47. return false;
  48. *data = roundup(bw, (unsigned long)r->membw.bw_gran);
  49. return true;
  50. }
  51. int parse_bw(char *buf, struct rdt_resource *r, struct rdt_domain *d)
  52. {
  53. unsigned long data;
  54. if (d->have_new_ctrl)
  55. return -EINVAL;
  56. if (!bw_validate(buf, &data, r))
  57. return -EINVAL;
  58. d->new_ctrl = data;
  59. d->have_new_ctrl = true;
  60. return 0;
  61. }
  62. /*
  63. * Check whether a cache bit mask is valid. The SDM says:
  64. * Please note that all (and only) contiguous '1' combinations
  65. * are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
  66. * Additionally Haswell requires at least two bits set.
  67. */
  68. static bool cbm_validate(char *buf, unsigned long *data, struct rdt_resource *r)
  69. {
  70. unsigned long first_bit, zero_bit, val;
  71. unsigned int cbm_len = r->cache.cbm_len;
  72. int ret;
  73. ret = kstrtoul(buf, 16, &val);
  74. if (ret)
  75. return false;
  76. if (val == 0 || val > r->default_ctrl)
  77. return false;
  78. first_bit = find_first_bit(&val, cbm_len);
  79. zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
  80. if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)
  81. return false;
  82. if ((zero_bit - first_bit) < r->cache.min_cbm_bits)
  83. return false;
  84. *data = val;
  85. return true;
  86. }
  87. /*
  88. * Read one cache bit mask (hex). Check that it is valid for the current
  89. * resource type.
  90. */
  91. int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d)
  92. {
  93. unsigned long data;
  94. if (d->have_new_ctrl)
  95. return -EINVAL;
  96. if(!cbm_validate(buf, &data, r))
  97. return -EINVAL;
  98. d->new_ctrl = data;
  99. d->have_new_ctrl = true;
  100. return 0;
  101. }
  102. /*
  103. * For each domain in this resource we expect to find a series of:
  104. * id=mask
  105. * separated by ";". The "id" is in decimal, and must match one of
  106. * the "id"s for this resource.
  107. */
  108. static int parse_line(char *line, struct rdt_resource *r)
  109. {
  110. char *dom = NULL, *id;
  111. struct rdt_domain *d;
  112. unsigned long dom_id;
  113. next:
  114. if (!line || line[0] == '\0')
  115. return 0;
  116. dom = strsep(&line, ";");
  117. id = strsep(&dom, "=");
  118. if (!dom || kstrtoul(id, 10, &dom_id))
  119. return -EINVAL;
  120. dom = strim(dom);
  121. list_for_each_entry(d, &r->domains, list) {
  122. if (d->id == dom_id) {
  123. if (r->parse_ctrlval(dom, r, d))
  124. return -EINVAL;
  125. goto next;
  126. }
  127. }
  128. return -EINVAL;
  129. }
  130. static int update_domains(struct rdt_resource *r, int closid)
  131. {
  132. struct msr_param msr_param;
  133. cpumask_var_t cpu_mask;
  134. struct rdt_domain *d;
  135. int cpu;
  136. if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  137. return -ENOMEM;
  138. msr_param.low = closid;
  139. msr_param.high = msr_param.low + 1;
  140. msr_param.res = r;
  141. list_for_each_entry(d, &r->domains, list) {
  142. if (d->have_new_ctrl && d->new_ctrl != d->ctrl_val[closid]) {
  143. cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
  144. d->ctrl_val[closid] = d->new_ctrl;
  145. }
  146. }
  147. if (cpumask_empty(cpu_mask))
  148. goto done;
  149. cpu = get_cpu();
  150. /* Update CBM on this cpu if it's in cpu_mask. */
  151. if (cpumask_test_cpu(cpu, cpu_mask))
  152. rdt_ctrl_update(&msr_param);
  153. /* Update CBM on other cpus. */
  154. smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
  155. put_cpu();
  156. done:
  157. free_cpumask_var(cpu_mask);
  158. return 0;
  159. }
  160. static int rdtgroup_parse_resource(char *resname, char *tok, int closid)
  161. {
  162. struct rdt_resource *r;
  163. for_each_enabled_rdt_resource(r) {
  164. if (!strcmp(resname, r->name) && closid < r->num_closid)
  165. return parse_line(tok, r);
  166. }
  167. return -EINVAL;
  168. }
  169. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  170. char *buf, size_t nbytes, loff_t off)
  171. {
  172. struct rdtgroup *rdtgrp;
  173. struct rdt_domain *dom;
  174. struct rdt_resource *r;
  175. char *tok, *resname;
  176. int closid, ret = 0;
  177. /* Valid input requires a trailing newline */
  178. if (nbytes == 0 || buf[nbytes - 1] != '\n')
  179. return -EINVAL;
  180. buf[nbytes - 1] = '\0';
  181. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  182. if (!rdtgrp) {
  183. rdtgroup_kn_unlock(of->kn);
  184. return -ENOENT;
  185. }
  186. closid = rdtgrp->closid;
  187. for_each_enabled_rdt_resource(r) {
  188. list_for_each_entry(dom, &r->domains, list)
  189. dom->have_new_ctrl = false;
  190. }
  191. while ((tok = strsep(&buf, "\n")) != NULL) {
  192. resname = strim(strsep(&tok, ":"));
  193. if (!tok) {
  194. ret = -EINVAL;
  195. goto out;
  196. }
  197. ret = rdtgroup_parse_resource(resname, tok, closid);
  198. if (ret)
  199. goto out;
  200. }
  201. for_each_enabled_rdt_resource(r) {
  202. ret = update_domains(r, closid);
  203. if (ret)
  204. goto out;
  205. }
  206. out:
  207. rdtgroup_kn_unlock(of->kn);
  208. return ret ?: nbytes;
  209. }
  210. static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
  211. {
  212. struct rdt_domain *dom;
  213. bool sep = false;
  214. seq_printf(s, "%*s:", max_name_width, r->name);
  215. list_for_each_entry(dom, &r->domains, list) {
  216. if (sep)
  217. seq_puts(s, ";");
  218. seq_printf(s, r->format_str, dom->id, max_data_width,
  219. dom->ctrl_val[closid]);
  220. sep = true;
  221. }
  222. seq_puts(s, "\n");
  223. }
  224. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  225. struct seq_file *s, void *v)
  226. {
  227. struct rdtgroup *rdtgrp;
  228. struct rdt_resource *r;
  229. int closid, ret = 0;
  230. rdtgrp = rdtgroup_kn_lock_live(of->kn);
  231. if (rdtgrp) {
  232. closid = rdtgrp->closid;
  233. for_each_enabled_rdt_resource(r) {
  234. if (closid < r->num_closid)
  235. show_doms(s, r, closid);
  236. }
  237. } else {
  238. ret = -ENOENT;
  239. }
  240. rdtgroup_kn_unlock(of->kn);
  241. return ret;
  242. }