hv-gpci.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 GPCI_MAX_DATA_BYTES \
  114. (1024 - sizeof(struct hv_get_perf_counter_info_params))
  115. static unsigned long single_gpci_request(u32 req, u32 starting_index,
  116. u16 secondary_index, u8 version_in, u32 offset, u8 length,
  117. u64 *value)
  118. {
  119. unsigned long ret;
  120. size_t i;
  121. u64 count;
  122. struct {
  123. struct hv_get_perf_counter_info_params params;
  124. uint8_t bytes[GPCI_MAX_DATA_BYTES];
  125. } __packed __aligned(sizeof(uint64_t)) arg = {
  126. .params = {
  127. .counter_request = cpu_to_be32(req),
  128. .starting_index = cpu_to_be32(starting_index),
  129. .secondary_index = cpu_to_be16(secondary_index),
  130. .counter_info_version_in = version_in,
  131. }
  132. };
  133. ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
  134. virt_to_phys(&arg), sizeof(arg));
  135. if (ret) {
  136. pr_devel("hcall failed: 0x%lx\n", ret);
  137. return ret;
  138. }
  139. /*
  140. * we verify offset and length are within the zeroed buffer at event
  141. * init.
  142. */
  143. count = 0;
  144. for (i = offset; i < offset + length; i++)
  145. count |= arg.bytes[i] << (i - offset);
  146. *value = count;
  147. return ret;
  148. }
  149. static u64 h_gpci_get_value(struct perf_event *event)
  150. {
  151. u64 count;
  152. unsigned long ret = single_gpci_request(event_get_request(event),
  153. event_get_starting_index(event),
  154. event_get_secondary_index(event),
  155. event_get_counter_info_version(event),
  156. event_get_offset(event),
  157. event_get_length(event),
  158. &count);
  159. if (ret)
  160. return 0;
  161. return count;
  162. }
  163. static void h_gpci_event_update(struct perf_event *event)
  164. {
  165. s64 prev;
  166. u64 now = h_gpci_get_value(event);
  167. prev = local64_xchg(&event->hw.prev_count, now);
  168. local64_add(now - prev, &event->count);
  169. }
  170. static void h_gpci_event_start(struct perf_event *event, int flags)
  171. {
  172. local64_set(&event->hw.prev_count, h_gpci_get_value(event));
  173. }
  174. static void h_gpci_event_stop(struct perf_event *event, int flags)
  175. {
  176. h_gpci_event_update(event);
  177. }
  178. static int h_gpci_event_add(struct perf_event *event, int flags)
  179. {
  180. if (flags & PERF_EF_START)
  181. h_gpci_event_start(event, flags);
  182. return 0;
  183. }
  184. static int h_gpci_event_init(struct perf_event *event)
  185. {
  186. u64 count;
  187. u8 length;
  188. /* Not our event */
  189. if (event->attr.type != event->pmu->type)
  190. return -ENOENT;
  191. /* config2 is unused */
  192. if (event->attr.config2) {
  193. pr_devel("config2 set when reserved\n");
  194. return -EINVAL;
  195. }
  196. /* unsupported modes and filters */
  197. if (event->attr.exclude_user ||
  198. event->attr.exclude_kernel ||
  199. event->attr.exclude_hv ||
  200. event->attr.exclude_idle ||
  201. event->attr.exclude_host ||
  202. event->attr.exclude_guest)
  203. return -EINVAL;
  204. /* no branch sampling */
  205. if (has_branch_stack(event))
  206. return -EOPNOTSUPP;
  207. length = event_get_length(event);
  208. if (length < 1 || length > 8) {
  209. pr_devel("length invalid\n");
  210. return -EINVAL;
  211. }
  212. /* last byte within the buffer? */
  213. if ((event_get_offset(event) + length) > GPCI_MAX_DATA_BYTES) {
  214. pr_devel("request outside of buffer: %zu > %zu\n",
  215. (size_t)event_get_offset(event) + length,
  216. GPCI_MAX_DATA_BYTES);
  217. return -EINVAL;
  218. }
  219. /* check if the request works... */
  220. if (single_gpci_request(event_get_request(event),
  221. event_get_starting_index(event),
  222. event_get_secondary_index(event),
  223. event_get_counter_info_version(event),
  224. event_get_offset(event),
  225. length,
  226. &count)) {
  227. pr_devel("gpci hcall failed\n");
  228. return -EINVAL;
  229. }
  230. return 0;
  231. }
  232. static struct pmu h_gpci_pmu = {
  233. .task_ctx_nr = perf_invalid_context,
  234. .name = "hv_gpci",
  235. .attr_groups = attr_groups,
  236. .event_init = h_gpci_event_init,
  237. .add = h_gpci_event_add,
  238. .del = h_gpci_event_stop,
  239. .start = h_gpci_event_start,
  240. .stop = h_gpci_event_stop,
  241. .read = h_gpci_event_update,
  242. };
  243. static int hv_gpci_init(void)
  244. {
  245. int r;
  246. unsigned long hret;
  247. struct hv_perf_caps caps;
  248. hv_gpci_assert_offsets_correct();
  249. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  250. pr_debug("not a virtualized system, not enabling\n");
  251. return -ENODEV;
  252. }
  253. hret = hv_perf_caps_get(&caps);
  254. if (hret) {
  255. pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
  256. hret);
  257. return -ENODEV;
  258. }
  259. /* sampling not supported */
  260. h_gpci_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  261. r = perf_pmu_register(&h_gpci_pmu, h_gpci_pmu.name, -1);
  262. if (r)
  263. return r;
  264. return 0;
  265. }
  266. device_initcall(hv_gpci_init);