KVM: PPC: Book3S HV: Fix endianness of instruction obtained from HEIR register
[linux-2.6/btrfs-unstable.git] / arch / powerpc / perf / hv-gpci.c
bloba051fe946c63a2eecc88b041e0ce67f14f329b4b
1 /*
2 * Hypervisor supplied "gpci" ("get performance counter info") performance
3 * counter support
5 * Author: Cody P Schafer <cody@linux.vnet.ibm.com>
6 * Copyright 2014 IBM Corporation.
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.
14 #define pr_fmt(fmt) "hv-gpci: " fmt
16 #include <linux/init.h>
17 #include <linux/perf_event.h>
18 #include <asm/firmware.h>
19 #include <asm/hvcall.h>
20 #include <asm/io.h>
22 #include "hv-gpci.h"
23 #include "hv-common.h"
26 * Example usage:
27 * perf stat -e 'hv_gpci/counter_info_version=3,offset=0,length=8,
28 * secondary_index=0,starting_index=0xffffffff,request=0x10/' ...
31 /* u32 */
32 EVENT_DEFINE_RANGE_FORMAT(request, config, 0, 31);
33 /* u32 */
34 EVENT_DEFINE_RANGE_FORMAT(starting_index, config, 32, 63);
35 /* u16 */
36 EVENT_DEFINE_RANGE_FORMAT(secondary_index, config1, 0, 15);
37 /* u8 */
38 EVENT_DEFINE_RANGE_FORMAT(counter_info_version, config1, 16, 23);
39 /* u8, bytes of data (1-8) */
40 EVENT_DEFINE_RANGE_FORMAT(length, config1, 24, 31);
41 /* u32, byte offset */
42 EVENT_DEFINE_RANGE_FORMAT(offset, config1, 32, 63);
44 static struct attribute *format_attrs[] = {
45 &format_attr_request.attr,
46 &format_attr_starting_index.attr,
47 &format_attr_secondary_index.attr,
48 &format_attr_counter_info_version.attr,
50 &format_attr_offset.attr,
51 &format_attr_length.attr,
52 NULL,
55 static struct attribute_group format_group = {
56 .name = "format",
57 .attrs = format_attrs,
60 #define HV_CAPS_ATTR(_name, _format) \
61 static ssize_t _name##_show(struct device *dev, \
62 struct device_attribute *attr, \
63 char *page) \
64 { \
65 struct hv_perf_caps caps; \
66 unsigned long hret = hv_perf_caps_get(&caps); \
67 if (hret) \
68 return -EIO; \
70 return sprintf(page, _format, caps._name); \
71 } \
72 static struct device_attribute hv_caps_attr_##_name = __ATTR_RO(_name)
74 static ssize_t kernel_version_show(struct device *dev,
75 struct device_attribute *attr,
76 char *page)
78 return sprintf(page, "0x%x\n", COUNTER_INFO_VERSION_CURRENT);
81 static DEVICE_ATTR_RO(kernel_version);
82 HV_CAPS_ATTR(version, "0x%x\n");
83 HV_CAPS_ATTR(ga, "%d\n");
84 HV_CAPS_ATTR(expanded, "%d\n");
85 HV_CAPS_ATTR(lab, "%d\n");
86 HV_CAPS_ATTR(collect_privileged, "%d\n");
88 static struct attribute *interface_attrs[] = {
89 &dev_attr_kernel_version.attr,
90 &hv_caps_attr_version.attr,
91 &hv_caps_attr_ga.attr,
92 &hv_caps_attr_expanded.attr,
93 &hv_caps_attr_lab.attr,
94 &hv_caps_attr_collect_privileged.attr,
95 NULL,
98 static struct attribute_group interface_group = {
99 .name = "interface",
100 .attrs = interface_attrs,
103 static const struct attribute_group *attr_groups[] = {
104 &format_group,
105 &interface_group,
106 NULL,
109 #define GPCI_MAX_DATA_BYTES \
110 (1024 - sizeof(struct hv_get_perf_counter_info_params))
112 static unsigned long single_gpci_request(u32 req, u32 starting_index,
113 u16 secondary_index, u8 version_in, u32 offset, u8 length,
114 u64 *value)
116 unsigned long ret;
117 size_t i;
118 u64 count;
120 struct {
121 struct hv_get_perf_counter_info_params params;
122 uint8_t bytes[GPCI_MAX_DATA_BYTES];
123 } __packed __aligned(sizeof(uint64_t)) arg = {
124 .params = {
125 .counter_request = cpu_to_be32(req),
126 .starting_index = cpu_to_be32(starting_index),
127 .secondary_index = cpu_to_be16(secondary_index),
128 .counter_info_version_in = version_in,
132 ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
133 virt_to_phys(&arg), sizeof(arg));
134 if (ret) {
135 pr_devel("hcall failed: 0x%lx\n", ret);
136 return ret;
140 * we verify offset and length are within the zeroed buffer at event
141 * init.
143 count = 0;
144 for (i = offset; i < offset + length; i++)
145 count |= arg.bytes[i] << (i - offset);
147 *value = count;
148 return ret;
151 static u64 h_gpci_get_value(struct perf_event *event)
153 u64 count;
154 unsigned long ret = single_gpci_request(event_get_request(event),
155 event_get_starting_index(event),
156 event_get_secondary_index(event),
157 event_get_counter_info_version(event),
158 event_get_offset(event),
159 event_get_length(event),
160 &count);
161 if (ret)
162 return 0;
163 return count;
166 static void h_gpci_event_update(struct perf_event *event)
168 s64 prev;
169 u64 now = h_gpci_get_value(event);
170 prev = local64_xchg(&event->hw.prev_count, now);
171 local64_add(now - prev, &event->count);
174 static void h_gpci_event_start(struct perf_event *event, int flags)
176 local64_set(&event->hw.prev_count, h_gpci_get_value(event));
179 static void h_gpci_event_stop(struct perf_event *event, int flags)
181 h_gpci_event_update(event);
184 static int h_gpci_event_add(struct perf_event *event, int flags)
186 if (flags & PERF_EF_START)
187 h_gpci_event_start(event, flags);
189 return 0;
192 static int h_gpci_event_init(struct perf_event *event)
194 u64 count;
195 u8 length;
197 /* Not our event */
198 if (event->attr.type != event->pmu->type)
199 return -ENOENT;
201 /* config2 is unused */
202 if (event->attr.config2) {
203 pr_devel("config2 set when reserved\n");
204 return -EINVAL;
207 /* unsupported modes and filters */
208 if (event->attr.exclude_user ||
209 event->attr.exclude_kernel ||
210 event->attr.exclude_hv ||
211 event->attr.exclude_idle ||
212 event->attr.exclude_host ||
213 event->attr.exclude_guest)
214 return -EINVAL;
216 /* no branch sampling */
217 if (has_branch_stack(event))
218 return -EOPNOTSUPP;
220 length = event_get_length(event);
221 if (length < 1 || length > 8) {
222 pr_devel("length invalid\n");
223 return -EINVAL;
226 /* last byte within the buffer? */
227 if ((event_get_offset(event) + length) > GPCI_MAX_DATA_BYTES) {
228 pr_devel("request outside of buffer: %zu > %zu\n",
229 (size_t)event_get_offset(event) + length,
230 GPCI_MAX_DATA_BYTES);
231 return -EINVAL;
234 /* check if the request works... */
235 if (single_gpci_request(event_get_request(event),
236 event_get_starting_index(event),
237 event_get_secondary_index(event),
238 event_get_counter_info_version(event),
239 event_get_offset(event),
240 length,
241 &count)) {
242 pr_devel("gpci hcall failed\n");
243 return -EINVAL;
246 return 0;
249 static struct pmu h_gpci_pmu = {
250 .task_ctx_nr = perf_invalid_context,
252 .name = "hv_gpci",
253 .attr_groups = attr_groups,
254 .event_init = h_gpci_event_init,
255 .add = h_gpci_event_add,
256 .del = h_gpci_event_stop,
257 .start = h_gpci_event_start,
258 .stop = h_gpci_event_stop,
259 .read = h_gpci_event_update,
262 static int hv_gpci_init(void)
264 int r;
265 unsigned long hret;
266 struct hv_perf_caps caps;
268 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
269 pr_debug("not a virtualized system, not enabling\n");
270 return -ENODEV;
273 hret = hv_perf_caps_get(&caps);
274 if (hret) {
275 pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
276 hret);
277 return -ENODEV;
280 /* sampling not supported */
281 h_gpci_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
283 r = perf_pmu_register(&h_gpci_pmu, h_gpci_pmu.name, -1);
284 if (r)
285 return r;
287 return 0;
290 device_initcall(hv_gpci_init);