hv-gpci.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Hypervisor supplied "gpci" ("get performance counter info") performance
  3. * counter support
  4. *
  5. * Author: Cody P Schafer <cody@linux.vnet.ibm.com>
  6. * Copyright 2014 IBM Corporation.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #define pr_fmt(fmt) "hv-gpci: " fmt
  14. #include <linux/init.h>
  15. #include <linux/perf_event.h>
  16. #include <asm/firmware.h>
  17. #include <asm/hvcall.h>
  18. #include <asm/io.h>
  19. #include "hv-gpci.h"
  20. #include "hv-common.h"
  21. /*
  22. * Example usage:
  23. * perf stat -e 'hv_gpci/counter_info_version=3,offset=0,length=8,
  24. * secondary_index=0,starting_index=0xffffffff,request=0x10/' ...
  25. */
  26. /* u32 */
  27. EVENT_DEFINE_RANGE_FORMAT(request, config, 0, 31);
  28. /* u32 */
  29. /*
  30. * Note that starting_index, phys_processor_idx, sibling_part_id,
  31. * hw_chip_id, partition_id all refer to the same bit range. They
  32. * are basically aliases for the starting_index. The specific alias
  33. * used depends on the event. See REQUEST_IDX_KIND in hv-gpci-requests.h
  34. */
  35. EVENT_DEFINE_RANGE_FORMAT(starting_index, config, 32, 63);
  36. EVENT_DEFINE_RANGE_FORMAT_LITE(phys_processor_idx, config, 32, 63);
  37. EVENT_DEFINE_RANGE_FORMAT_LITE(sibling_part_id, config, 32, 63);
  38. EVENT_DEFINE_RANGE_FORMAT_LITE(hw_chip_id, config, 32, 63);
  39. EVENT_DEFINE_RANGE_FORMAT_LITE(partition_id, config, 32, 63);
  40. /* u16 */
  41. EVENT_DEFINE_RANGE_FORMAT(secondary_index, config1, 0, 15);
  42. /* u8 */
  43. EVENT_DEFINE_RANGE_FORMAT(counter_info_version, config1, 16, 23);
  44. /* u8, bytes of data (1-8) */
  45. EVENT_DEFINE_RANGE_FORMAT(length, config1, 24, 31);
  46. /* u32, byte offset */
  47. EVENT_DEFINE_RANGE_FORMAT(offset, config1, 32, 63);
  48. static struct attribute *format_attrs[] = {
  49. &format_attr_request.attr,
  50. &format_attr_starting_index.attr,
  51. &format_attr_phys_processor_idx.attr,
  52. &format_attr_sibling_part_id.attr,
  53. &format_attr_hw_chip_id.attr,
  54. &format_attr_partition_id.attr,
  55. &format_attr_secondary_index.attr,
  56. &format_attr_counter_info_version.attr,
  57. &format_attr_offset.attr,
  58. &format_attr_length.attr,
  59. NULL,
  60. };
  61. static struct attribute_group format_group = {
  62. .name = "format",
  63. .attrs = format_attrs,
  64. };
  65. static struct attribute_group event_group = {
  66. .name = "events",
  67. .attrs = hv_gpci_event_attrs,
  68. };
  69. #define HV_CAPS_ATTR(_name, _format) \
  70. static ssize_t _name##_show(struct device *dev, \
  71. struct device_attribute *attr, \
  72. char *page) \
  73. { \
  74. struct hv_perf_caps caps; \
  75. unsigned long hret = hv_perf_caps_get(&caps); \
  76. if (hret) \
  77. return -EIO; \
  78. \
  79. return sprintf(page, _format, caps._name); \
  80. } \
  81. static struct device_attribute hv_caps_attr_##_name = __ATTR_RO(_name)
  82. static ssize_t kernel_version_show(struct device *dev,
  83. struct device_attribute *attr,
  84. char *page)
  85. {
  86. return sprintf(page, "0x%x\n", COUNTER_INFO_VERSION_CURRENT);
  87. }
  88. static DEVICE_ATTR_RO(kernel_version);
  89. HV_CAPS_ATTR(version, "0x%x\n");
  90. HV_CAPS_ATTR(ga, "%d\n");
  91. HV_CAPS_ATTR(expanded, "%d\n");
  92. HV_CAPS_ATTR(lab, "%d\n");
  93. HV_CAPS_ATTR(collect_privileged, "%d\n");
  94. static struct attribute *interface_attrs[] = {
  95. &dev_attr_kernel_version.attr,
  96. &hv_caps_attr_version.attr,
  97. &hv_caps_attr_ga.attr,
  98. &hv_caps_attr_expanded.attr,
  99. &hv_caps_attr_lab.attr,
  100. &hv_caps_attr_collect_privileged.attr,
  101. NULL,
  102. };
  103. static struct attribute_group interface_group = {
  104. .name = "interface",
  105. .attrs = interface_attrs,
  106. };
  107. static const struct attribute_group *attr_groups[] = {
  108. &format_group,
  109. &event_group,
  110. &interface_group,
  111. NULL,
  112. };
  113. #define HGPCI_REQ_BUFFER_SIZE 4096
  114. #define HGPCI_MAX_DATA_BYTES \
  115. (HGPCI_REQ_BUFFER_SIZE - sizeof(struct hv_get_perf_counter_info_params))
  116. static DEFINE_PER_CPU(char, hv_gpci_reqb[HGPCI_REQ_BUFFER_SIZE]) __aligned(sizeof(uint64_t));
  117. struct hv_gpci_request_buffer {
  118. struct hv_get_perf_counter_info_params params;
  119. uint8_t bytes[HGPCI_MAX_DATA_BYTES];
  120. } __packed;
  121. static unsigned long single_gpci_request(u32 req, u32 starting_index,
  122. u16 secondary_index, u8 version_in, u32 offset, u8 length,
  123. u64 *value)
  124. {
  125. unsigned long ret;
  126. size_t i;
  127. u64 count;
  128. struct hv_gpci_request_buffer *arg;
  129. arg = (void *)get_cpu_var(hv_gpci_reqb);
  130. memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
  131. arg->params.counter_request = cpu_to_be32(req);
  132. arg->params.starting_index = cpu_to_be32(starting_index);
  133. arg->params.secondary_index = cpu_to_be16(secondary_index);
  134. arg->params.counter_info_version_in = version_in;
  135. ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
  136. virt_to_phys(arg), HGPCI_REQ_BUFFER_SIZE);
  137. if (ret) {
  138. pr_devel("hcall failed: 0x%lx\n", ret);
  139. goto out;
  140. }
  141. /*
  142. * we verify offset and length are within the zeroed buffer at event
  143. * init.
  144. */
  145. count = 0;
  146. for (i = offset; i < offset + length; i++)
  147. count |= arg->bytes[i] << (i - offset);
  148. *value = count;
  149. out:
  150. put_cpu_var(hv_gpci_reqb);
  151. return ret;
  152. }
  153. static u64 h_gpci_get_value(struct perf_event *event)
  154. {
  155. u64 count;
  156. unsigned long ret = single_gpci_request(event_get_request(event),
  157. event_get_starting_index(event),
  158. event_get_secondary_index(event),
  159. event_get_counter_info_version(event),
  160. event_get_offset(event),
  161. event_get_length(event),
  162. &count);
  163. if (ret)
  164. return 0;
  165. return count;
  166. }
  167. static void h_gpci_event_update(struct perf_event *event)
  168. {
  169. s64 prev;
  170. u64 now = h_gpci_get_value(event);
  171. prev = local64_xchg(&event->hw.prev_count, now);
  172. local64_add(now - prev, &event->count);
  173. }
  174. static void h_gpci_event_start(struct perf_event *event, int flags)
  175. {
  176. local64_set(&event->hw.prev_count, h_gpci_get_value(event));
  177. }
  178. static void h_gpci_event_stop(struct perf_event *event, int flags)
  179. {
  180. h_gpci_event_update(event);
  181. }
  182. static int h_gpci_event_add(struct perf_event *event, int flags)
  183. {
  184. if (flags & PERF_EF_START)
  185. h_gpci_event_start(event, flags);
  186. return 0;
  187. }
  188. static int h_gpci_event_init(struct perf_event *event)
  189. {
  190. u64 count;
  191. u8 length;
  192. /* Not our event */
  193. if (event->attr.type != event->pmu->type)
  194. return -ENOENT;
  195. /* config2 is unused */
  196. if (event->attr.config2) {
  197. pr_devel("config2 set when reserved\n");
  198. return -EINVAL;
  199. }
  200. /* unsupported modes and filters */
  201. if (event->attr.exclude_user ||
  202. event->attr.exclude_kernel ||
  203. event->attr.exclude_hv ||
  204. event->attr.exclude_idle ||
  205. event->attr.exclude_host ||
  206. event->attr.exclude_guest)
  207. return -EINVAL;
  208. /* no branch sampling */
  209. if (has_branch_stack(event))
  210. return -EOPNOTSUPP;
  211. length = event_get_length(event);
  212. if (length < 1 || length > 8) {
  213. pr_devel("length invalid\n");
  214. return -EINVAL;
  215. }
  216. /* last byte within the buffer? */
  217. if ((event_get_offset(event) + length) > HGPCI_MAX_DATA_BYTES) {
  218. pr_devel("request outside of buffer: %zu > %zu\n",
  219. (size_t)event_get_offset(event) + length,
  220. HGPCI_MAX_DATA_BYTES);
  221. return -EINVAL;
  222. }
  223. /* check if the request works... */
  224. if (single_gpci_request(event_get_request(event),
  225. event_get_starting_index(event),
  226. event_get_secondary_index(event),
  227. event_get_counter_info_version(event),
  228. event_get_offset(event),
  229. length,
  230. &count)) {
  231. pr_devel("gpci hcall failed\n");
  232. return -EINVAL;
  233. }
  234. return 0;
  235. }
  236. static struct pmu h_gpci_pmu = {
  237. .task_ctx_nr = perf_invalid_context,
  238. .name = "hv_gpci",
  239. .attr_groups = attr_groups,
  240. .event_init = h_gpci_event_init,
  241. .add = h_gpci_event_add,
  242. .del = h_gpci_event_stop,
  243. .start = h_gpci_event_start,
  244. .stop = h_gpci_event_stop,
  245. .read = h_gpci_event_update,
  246. };
  247. static int hv_gpci_init(void)
  248. {
  249. int r;
  250. unsigned long hret;
  251. struct hv_perf_caps caps;
  252. hv_gpci_assert_offsets_correct();
  253. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  254. pr_debug("not a virtualized system, not enabling\n");
  255. return -ENODEV;
  256. }
  257. hret = hv_perf_caps_get(&caps);
  258. if (hret) {
  259. pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
  260. hret);
  261. return -ENODEV;
  262. }
  263. /* sampling not supported */
  264. h_gpci_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  265. r = perf_pmu_register(&h_gpci_pmu, h_gpci_pmu.name, -1);
  266. if (r)
  267. return r;
  268. return 0;
  269. }
  270. device_initcall(hv_gpci_init);