2 * Hypervisor supplied "gpci" ("get performance counter info") performance
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>
23 #include "hv-common.h"
27 * perf stat -e 'hv_gpci/counter_info_version=3,offset=0,length=8,
28 * secondary_index=0,starting_index=0xffffffff,request=0x10/' ...
32 EVENT_DEFINE_RANGE_FORMAT(request
, config
, 0, 31);
34 EVENT_DEFINE_RANGE_FORMAT(starting_index
, config
, 32, 63);
36 EVENT_DEFINE_RANGE_FORMAT(secondary_index
, config1
, 0, 15);
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
,
55 static struct attribute_group format_group
= {
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, \
65 struct hv_perf_caps caps; \
66 unsigned long hret = hv_perf_caps_get(&caps); \
70 return sprintf(page, _format, caps._name); \
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
,
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
,
98 static struct attribute_group interface_group
= {
100 .attrs
= interface_attrs
,
103 static const struct attribute_group
*attr_groups
[] = {
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
,
121 struct hv_get_perf_counter_info_params params
;
122 uint8_t bytes
[GPCI_MAX_DATA_BYTES
];
123 } __packed
__aligned(sizeof(uint64_t)) arg
= {
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
));
135 pr_devel("hcall failed: 0x%lx\n", ret
);
140 * we verify offset and length are within the zeroed buffer at event
144 for (i
= offset
; i
< offset
+ length
; i
++)
145 count
|= arg
.bytes
[i
] << (i
- offset
);
151 static u64
h_gpci_get_value(struct perf_event
*event
)
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
),
166 static void h_gpci_event_update(struct perf_event
*event
)
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
);
192 static int h_gpci_event_init(struct perf_event
*event
)
198 if (event
->attr
.type
!= event
->pmu
->type
)
201 /* config2 is unused */
202 if (event
->attr
.config2
) {
203 pr_devel("config2 set when reserved\n");
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 is_sampling_event(event
)) /* no sampling */
217 /* no branch sampling */
218 if (has_branch_stack(event
))
221 length
= event_get_length(event
);
222 if (length
< 1 || length
> 8) {
223 pr_devel("length invalid\n");
227 /* last byte within the buffer? */
228 if ((event_get_offset(event
) + length
) > GPCI_MAX_DATA_BYTES
) {
229 pr_devel("request outside of buffer: %zu > %zu\n",
230 (size_t)event_get_offset(event
) + length
,
231 GPCI_MAX_DATA_BYTES
);
235 /* check if the request works... */
236 if (single_gpci_request(event_get_request(event
),
237 event_get_starting_index(event
),
238 event_get_secondary_index(event
),
239 event_get_counter_info_version(event
),
240 event_get_offset(event
),
243 pr_devel("gpci hcall failed\n");
250 static int h_gpci_event_idx(struct perf_event
*event
)
255 static struct pmu h_gpci_pmu
= {
256 .task_ctx_nr
= perf_invalid_context
,
259 .attr_groups
= attr_groups
,
260 .event_init
= h_gpci_event_init
,
261 .add
= h_gpci_event_add
,
262 .del
= h_gpci_event_stop
,
263 .start
= h_gpci_event_start
,
264 .stop
= h_gpci_event_stop
,
265 .read
= h_gpci_event_update
,
266 .event_idx
= h_gpci_event_idx
,
269 static int hv_gpci_init(void)
273 struct hv_perf_caps caps
;
275 if (!firmware_has_feature(FW_FEATURE_LPAR
)) {
276 pr_debug("not a virtualized system, not enabling\n");
280 hret
= hv_perf_caps_get(&caps
);
282 pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
287 r
= perf_pmu_register(&h_gpci_pmu
, h_gpci_pmu
.name
, -1);
294 device_initcall(hv_gpci_init
);