1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Hypervisor supplied "24x7" performance counter support
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Author: Cody P Schafer <cody@linux.vnet.ibm.com>
6*4882a593Smuzhiyun * Copyright 2014 IBM Corporation.
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #define pr_fmt(fmt) "hv-24x7: " fmt
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/perf_event.h>
12*4882a593Smuzhiyun #include <linux/rbtree.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/vmalloc.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <asm/cputhreads.h>
18*4882a593Smuzhiyun #include <asm/firmware.h>
19*4882a593Smuzhiyun #include <asm/hvcall.h>
20*4882a593Smuzhiyun #include <asm/io.h>
21*4882a593Smuzhiyun #include <linux/byteorder/generic.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <asm/rtas.h>
24*4882a593Smuzhiyun #include "hv-24x7.h"
25*4882a593Smuzhiyun #include "hv-24x7-catalog.h"
26*4882a593Smuzhiyun #include "hv-common.h"
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* Version of the 24x7 hypervisor API that we should use in this machine. */
29*4882a593Smuzhiyun static int interface_version;
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* Whether we have to aggregate result data for some domains. */
32*4882a593Smuzhiyun static bool aggregate_result_elements;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static cpumask_t hv_24x7_cpumask;
35*4882a593Smuzhiyun
domain_is_valid(unsigned domain)36*4882a593Smuzhiyun static bool domain_is_valid(unsigned domain)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun switch (domain) {
39*4882a593Smuzhiyun #define DOMAIN(n, v, x, c) \
40*4882a593Smuzhiyun case HV_PERF_DOMAIN_##n: \
41*4882a593Smuzhiyun /* fall through */
42*4882a593Smuzhiyun #include "hv-24x7-domains.h"
43*4882a593Smuzhiyun #undef DOMAIN
44*4882a593Smuzhiyun return true;
45*4882a593Smuzhiyun default:
46*4882a593Smuzhiyun return false;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
is_physical_domain(unsigned domain)50*4882a593Smuzhiyun static bool is_physical_domain(unsigned domain)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun switch (domain) {
53*4882a593Smuzhiyun #define DOMAIN(n, v, x, c) \
54*4882a593Smuzhiyun case HV_PERF_DOMAIN_##n: \
55*4882a593Smuzhiyun return c;
56*4882a593Smuzhiyun #include "hv-24x7-domains.h"
57*4882a593Smuzhiyun #undef DOMAIN
58*4882a593Smuzhiyun default:
59*4882a593Smuzhiyun return false;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun * The Processor Module Information system parameter allows transferring
65*4882a593Smuzhiyun * of certain processor module information from the platform to the OS.
66*4882a593Smuzhiyun * Refer PAPR+ document to get parameter token value as '43'.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define PROCESSOR_MODULE_INFO 43
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun static u32 phys_sockets; /* Physical sockets */
72*4882a593Smuzhiyun static u32 phys_chipspersocket; /* Physical chips per socket*/
73*4882a593Smuzhiyun static u32 phys_coresperchip; /* Physical cores per chip */
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /*
76*4882a593Smuzhiyun * read_24x7_sys_info()
77*4882a593Smuzhiyun * Retrieve the number of sockets and chips per socket and cores per
78*4882a593Smuzhiyun * chip details through the get-system-parameter rtas call.
79*4882a593Smuzhiyun */
read_24x7_sys_info(void)80*4882a593Smuzhiyun void read_24x7_sys_info(void)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun int call_status, len, ntypes;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun spin_lock(&rtas_data_buf_lock);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * Making system parameter: chips and sockets and cores per chip
88*4882a593Smuzhiyun * default to 1.
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun phys_sockets = 1;
91*4882a593Smuzhiyun phys_chipspersocket = 1;
92*4882a593Smuzhiyun phys_coresperchip = 1;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
95*4882a593Smuzhiyun NULL,
96*4882a593Smuzhiyun PROCESSOR_MODULE_INFO,
97*4882a593Smuzhiyun __pa(rtas_data_buf),
98*4882a593Smuzhiyun RTAS_DATA_BUF_SIZE);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun if (call_status != 0) {
101*4882a593Smuzhiyun pr_err("Error calling get-system-parameter %d\n",
102*4882a593Smuzhiyun call_status);
103*4882a593Smuzhiyun } else {
104*4882a593Smuzhiyun len = be16_to_cpup((__be16 *)&rtas_data_buf[0]);
105*4882a593Smuzhiyun if (len < 8)
106*4882a593Smuzhiyun goto out;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun ntypes = be16_to_cpup((__be16 *)&rtas_data_buf[2]);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun if (!ntypes)
111*4882a593Smuzhiyun goto out;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun phys_sockets = be16_to_cpup((__be16 *)&rtas_data_buf[4]);
114*4882a593Smuzhiyun phys_chipspersocket = be16_to_cpup((__be16 *)&rtas_data_buf[6]);
115*4882a593Smuzhiyun phys_coresperchip = be16_to_cpup((__be16 *)&rtas_data_buf[8]);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun out:
119*4882a593Smuzhiyun spin_unlock(&rtas_data_buf_lock);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* Domains for which more than one result element are returned for each event. */
domain_needs_aggregation(unsigned int domain)123*4882a593Smuzhiyun static bool domain_needs_aggregation(unsigned int domain)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun return aggregate_result_elements &&
126*4882a593Smuzhiyun (domain == HV_PERF_DOMAIN_PHYS_CORE ||
127*4882a593Smuzhiyun (domain >= HV_PERF_DOMAIN_VCPU_HOME_CORE &&
128*4882a593Smuzhiyun domain <= HV_PERF_DOMAIN_VCPU_REMOTE_NODE));
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
domain_name(unsigned domain)131*4882a593Smuzhiyun static const char *domain_name(unsigned domain)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun if (!domain_is_valid(domain))
134*4882a593Smuzhiyun return NULL;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun switch (domain) {
137*4882a593Smuzhiyun case HV_PERF_DOMAIN_PHYS_CHIP: return "Physical Chip";
138*4882a593Smuzhiyun case HV_PERF_DOMAIN_PHYS_CORE: return "Physical Core";
139*4882a593Smuzhiyun case HV_PERF_DOMAIN_VCPU_HOME_CORE: return "VCPU Home Core";
140*4882a593Smuzhiyun case HV_PERF_DOMAIN_VCPU_HOME_CHIP: return "VCPU Home Chip";
141*4882a593Smuzhiyun case HV_PERF_DOMAIN_VCPU_HOME_NODE: return "VCPU Home Node";
142*4882a593Smuzhiyun case HV_PERF_DOMAIN_VCPU_REMOTE_NODE: return "VCPU Remote Node";
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun WARN_ON_ONCE(domain);
146*4882a593Smuzhiyun return NULL;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
catalog_entry_domain_is_valid(unsigned domain)149*4882a593Smuzhiyun static bool catalog_entry_domain_is_valid(unsigned domain)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun /* POWER8 doesn't support virtual domains. */
152*4882a593Smuzhiyun if (interface_version == 1)
153*4882a593Smuzhiyun return is_physical_domain(domain);
154*4882a593Smuzhiyun else
155*4882a593Smuzhiyun return domain_is_valid(domain);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * TODO: Merging events:
160*4882a593Smuzhiyun * - Think of the hcall as an interface to a 4d array of counters:
161*4882a593Smuzhiyun * - x = domains
162*4882a593Smuzhiyun * - y = indexes in the domain (core, chip, vcpu, node, etc)
163*4882a593Smuzhiyun * - z = offset into the counter space
164*4882a593Smuzhiyun * - w = lpars (guest vms, "logical partitions")
165*4882a593Smuzhiyun * - A single request is: x,y,y_last,z,z_last,w,w_last
166*4882a593Smuzhiyun * - this means we can retrieve a rectangle of counters in y,z for a single x.
167*4882a593Smuzhiyun *
168*4882a593Smuzhiyun * - Things to consider (ignoring w):
169*4882a593Smuzhiyun * - input cost_per_request = 16
170*4882a593Smuzhiyun * - output cost_per_result(ys,zs) = 8 + 8 * ys + ys * zs
171*4882a593Smuzhiyun * - limited number of requests per hcall (must fit into 4K bytes)
172*4882a593Smuzhiyun * - 4k = 16 [buffer header] - 16 [request size] * request_count
173*4882a593Smuzhiyun * - 255 requests per hcall
174*4882a593Smuzhiyun * - sometimes it will be more efficient to read extra data and discard
175*4882a593Smuzhiyun */
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * Example usage:
179*4882a593Smuzhiyun * perf stat -e 'hv_24x7/domain=2,offset=8,vcpu=0,lpar=0xffffffff/'
180*4882a593Smuzhiyun */
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /* u3 0-6, one of HV_24X7_PERF_DOMAIN */
183*4882a593Smuzhiyun EVENT_DEFINE_RANGE_FORMAT(domain, config, 0, 3);
184*4882a593Smuzhiyun /* u16 */
185*4882a593Smuzhiyun EVENT_DEFINE_RANGE_FORMAT(core, config, 16, 31);
186*4882a593Smuzhiyun EVENT_DEFINE_RANGE_FORMAT(chip, config, 16, 31);
187*4882a593Smuzhiyun EVENT_DEFINE_RANGE_FORMAT(vcpu, config, 16, 31);
188*4882a593Smuzhiyun /* u32, see "data_offset" */
189*4882a593Smuzhiyun EVENT_DEFINE_RANGE_FORMAT(offset, config, 32, 63);
190*4882a593Smuzhiyun /* u16 */
191*4882a593Smuzhiyun EVENT_DEFINE_RANGE_FORMAT(lpar, config1, 0, 15);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun EVENT_DEFINE_RANGE(reserved1, config, 4, 15);
194*4882a593Smuzhiyun EVENT_DEFINE_RANGE(reserved2, config1, 16, 63);
195*4882a593Smuzhiyun EVENT_DEFINE_RANGE(reserved3, config2, 0, 63);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun static struct attribute *format_attrs[] = {
198*4882a593Smuzhiyun &format_attr_domain.attr,
199*4882a593Smuzhiyun &format_attr_offset.attr,
200*4882a593Smuzhiyun &format_attr_core.attr,
201*4882a593Smuzhiyun &format_attr_chip.attr,
202*4882a593Smuzhiyun &format_attr_vcpu.attr,
203*4882a593Smuzhiyun &format_attr_lpar.attr,
204*4882a593Smuzhiyun NULL,
205*4882a593Smuzhiyun };
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun static struct attribute_group format_group = {
208*4882a593Smuzhiyun .name = "format",
209*4882a593Smuzhiyun .attrs = format_attrs,
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun static struct attribute_group event_group = {
213*4882a593Smuzhiyun .name = "events",
214*4882a593Smuzhiyun /* .attrs is set in init */
215*4882a593Smuzhiyun };
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun static struct attribute_group event_desc_group = {
218*4882a593Smuzhiyun .name = "event_descs",
219*4882a593Smuzhiyun /* .attrs is set in init */
220*4882a593Smuzhiyun };
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun static struct attribute_group event_long_desc_group = {
223*4882a593Smuzhiyun .name = "event_long_descs",
224*4882a593Smuzhiyun /* .attrs is set in init */
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun static struct kmem_cache *hv_page_cache;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun DEFINE_PER_CPU(int, hv_24x7_txn_flags);
230*4882a593Smuzhiyun DEFINE_PER_CPU(int, hv_24x7_txn_err);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun struct hv_24x7_hw {
233*4882a593Smuzhiyun struct perf_event *events[255];
234*4882a593Smuzhiyun };
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun DEFINE_PER_CPU(struct hv_24x7_hw, hv_24x7_hw);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /*
239*4882a593Smuzhiyun * request_buffer and result_buffer are not required to be 4k aligned,
240*4882a593Smuzhiyun * but are not allowed to cross any 4k boundary. Aligning them to 4k is
241*4882a593Smuzhiyun * the simplest way to ensure that.
242*4882a593Smuzhiyun */
243*4882a593Smuzhiyun #define H24x7_DATA_BUFFER_SIZE 4096
244*4882a593Smuzhiyun DEFINE_PER_CPU(char, hv_24x7_reqb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096);
245*4882a593Smuzhiyun DEFINE_PER_CPU(char, hv_24x7_resb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096);
246*4882a593Smuzhiyun
max_num_requests(int interface_version)247*4882a593Smuzhiyun static unsigned int max_num_requests(int interface_version)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun return (H24x7_DATA_BUFFER_SIZE - sizeof(struct hv_24x7_request_buffer))
250*4882a593Smuzhiyun / H24x7_REQUEST_SIZE(interface_version);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
event_name(struct hv_24x7_event_data * ev,int * len)253*4882a593Smuzhiyun static char *event_name(struct hv_24x7_event_data *ev, int *len)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun *len = be16_to_cpu(ev->event_name_len) - 2;
256*4882a593Smuzhiyun return (char *)ev->remainder;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
event_desc(struct hv_24x7_event_data * ev,int * len)259*4882a593Smuzhiyun static char *event_desc(struct hv_24x7_event_data *ev, int *len)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun unsigned nl = be16_to_cpu(ev->event_name_len);
262*4882a593Smuzhiyun __be16 *desc_len = (__be16 *)(ev->remainder + nl - 2);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun *len = be16_to_cpu(*desc_len) - 2;
265*4882a593Smuzhiyun return (char *)ev->remainder + nl;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
event_long_desc(struct hv_24x7_event_data * ev,int * len)268*4882a593Smuzhiyun static char *event_long_desc(struct hv_24x7_event_data *ev, int *len)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun unsigned nl = be16_to_cpu(ev->event_name_len);
271*4882a593Smuzhiyun __be16 *desc_len_ = (__be16 *)(ev->remainder + nl - 2);
272*4882a593Smuzhiyun unsigned desc_len = be16_to_cpu(*desc_len_);
273*4882a593Smuzhiyun __be16 *long_desc_len = (__be16 *)(ev->remainder + nl + desc_len - 2);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun *len = be16_to_cpu(*long_desc_len) - 2;
276*4882a593Smuzhiyun return (char *)ev->remainder + nl + desc_len;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
event_fixed_portion_is_within(struct hv_24x7_event_data * ev,void * end)279*4882a593Smuzhiyun static bool event_fixed_portion_is_within(struct hv_24x7_event_data *ev,
280*4882a593Smuzhiyun void *end)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun void *start = ev;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun return (start + offsetof(struct hv_24x7_event_data, remainder)) < end;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * Things we don't check:
289*4882a593Smuzhiyun * - padding for desc, name, and long/detailed desc is required to be '\0'
290*4882a593Smuzhiyun * bytes.
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * Return NULL if we pass end,
293*4882a593Smuzhiyun * Otherwise return the address of the byte just following the event.
294*4882a593Smuzhiyun */
event_end(struct hv_24x7_event_data * ev,void * end)295*4882a593Smuzhiyun static void *event_end(struct hv_24x7_event_data *ev, void *end)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun void *start = ev;
298*4882a593Smuzhiyun __be16 *dl_, *ldl_;
299*4882a593Smuzhiyun unsigned dl, ldl;
300*4882a593Smuzhiyun unsigned nl = be16_to_cpu(ev->event_name_len);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun if (nl < 2) {
303*4882a593Smuzhiyun pr_debug("%s: name length too short: %d", __func__, nl);
304*4882a593Smuzhiyun return NULL;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun if (start + nl > end) {
308*4882a593Smuzhiyun pr_debug("%s: start=%p + nl=%u > end=%p",
309*4882a593Smuzhiyun __func__, start, nl, end);
310*4882a593Smuzhiyun return NULL;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun dl_ = (__be16 *)(ev->remainder + nl - 2);
314*4882a593Smuzhiyun if (!IS_ALIGNED((uintptr_t)dl_, 2))
315*4882a593Smuzhiyun pr_warn("desc len not aligned %p", dl_);
316*4882a593Smuzhiyun dl = be16_to_cpu(*dl_);
317*4882a593Smuzhiyun if (dl < 2) {
318*4882a593Smuzhiyun pr_debug("%s: desc len too short: %d", __func__, dl);
319*4882a593Smuzhiyun return NULL;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun if (start + nl + dl > end) {
323*4882a593Smuzhiyun pr_debug("%s: (start=%p + nl=%u + dl=%u)=%p > end=%p",
324*4882a593Smuzhiyun __func__, start, nl, dl, start + nl + dl, end);
325*4882a593Smuzhiyun return NULL;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun ldl_ = (__be16 *)(ev->remainder + nl + dl - 2);
329*4882a593Smuzhiyun if (!IS_ALIGNED((uintptr_t)ldl_, 2))
330*4882a593Smuzhiyun pr_warn("long desc len not aligned %p", ldl_);
331*4882a593Smuzhiyun ldl = be16_to_cpu(*ldl_);
332*4882a593Smuzhiyun if (ldl < 2) {
333*4882a593Smuzhiyun pr_debug("%s: long desc len too short (ldl=%u)",
334*4882a593Smuzhiyun __func__, ldl);
335*4882a593Smuzhiyun return NULL;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun if (start + nl + dl + ldl > end) {
339*4882a593Smuzhiyun pr_debug("%s: start=%p + nl=%u + dl=%u + ldl=%u > end=%p",
340*4882a593Smuzhiyun __func__, start, nl, dl, ldl, end);
341*4882a593Smuzhiyun return NULL;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun return start + nl + dl + ldl;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
h_get_24x7_catalog_page_(unsigned long phys_4096,unsigned long version,unsigned long index)347*4882a593Smuzhiyun static long h_get_24x7_catalog_page_(unsigned long phys_4096,
348*4882a593Smuzhiyun unsigned long version, unsigned long index)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun pr_devel("h_get_24x7_catalog_page(0x%lx, %lu, %lu)",
351*4882a593Smuzhiyun phys_4096, version, index);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun WARN_ON(!IS_ALIGNED(phys_4096, 4096));
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun return plpar_hcall_norets(H_GET_24X7_CATALOG_PAGE,
356*4882a593Smuzhiyun phys_4096, version, index);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
h_get_24x7_catalog_page(char page[],u64 version,u32 index)359*4882a593Smuzhiyun static long h_get_24x7_catalog_page(char page[], u64 version, u32 index)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun return h_get_24x7_catalog_page_(virt_to_phys(page),
362*4882a593Smuzhiyun version, index);
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /*
366*4882a593Smuzhiyun * Each event we find in the catalog, will have a sysfs entry. Format the
367*4882a593Smuzhiyun * data for this sysfs entry based on the event's domain.
368*4882a593Smuzhiyun *
369*4882a593Smuzhiyun * Events belonging to the Chip domain can only be monitored in that domain.
370*4882a593Smuzhiyun * i.e the domain for these events is a fixed/knwon value.
371*4882a593Smuzhiyun *
372*4882a593Smuzhiyun * Events belonging to the Core domain can be monitored either in the physical
373*4882a593Smuzhiyun * core or in one of the virtual CPU domains. So the domain value for these
374*4882a593Smuzhiyun * events must be specified by the user (i.e is a required parameter). Format
375*4882a593Smuzhiyun * the Core events with 'domain=?' so the perf-tool can error check required
376*4882a593Smuzhiyun * parameters.
377*4882a593Smuzhiyun *
378*4882a593Smuzhiyun * NOTE: For the Core domain events, rather than making domain a required
379*4882a593Smuzhiyun * parameter we could default it to PHYS_CORE and allowe users to
380*4882a593Smuzhiyun * override the domain to one of the VCPU domains.
381*4882a593Smuzhiyun *
382*4882a593Smuzhiyun * However, this can make the interface a little inconsistent.
383*4882a593Smuzhiyun *
384*4882a593Smuzhiyun * If we set domain=2 (PHYS_CHIP) and allow user to override this field
385*4882a593Smuzhiyun * the user may be tempted to also modify the "offset=x" field in which
386*4882a593Smuzhiyun * can lead to confusing usage. Consider the HPM_PCYC (offset=0x18) and
387*4882a593Smuzhiyun * HPM_INST (offset=0x20) events. With:
388*4882a593Smuzhiyun *
389*4882a593Smuzhiyun * perf stat -e hv_24x7/HPM_PCYC,offset=0x20/
390*4882a593Smuzhiyun *
391*4882a593Smuzhiyun * we end up monitoring HPM_INST, while the command line has HPM_PCYC.
392*4882a593Smuzhiyun *
393*4882a593Smuzhiyun * By not assigning a default value to the domain for the Core events,
394*4882a593Smuzhiyun * we can have simple guidelines:
395*4882a593Smuzhiyun *
396*4882a593Smuzhiyun * - Specifying values for parameters with "=?" is required.
397*4882a593Smuzhiyun *
398*4882a593Smuzhiyun * - Specifying (i.e overriding) values for other parameters
399*4882a593Smuzhiyun * is undefined.
400*4882a593Smuzhiyun */
event_fmt(struct hv_24x7_event_data * event,unsigned domain)401*4882a593Smuzhiyun static char *event_fmt(struct hv_24x7_event_data *event, unsigned domain)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun const char *sindex;
404*4882a593Smuzhiyun const char *lpar;
405*4882a593Smuzhiyun const char *domain_str;
406*4882a593Smuzhiyun char buf[8];
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun switch (domain) {
409*4882a593Smuzhiyun case HV_PERF_DOMAIN_PHYS_CHIP:
410*4882a593Smuzhiyun snprintf(buf, sizeof(buf), "%d", domain);
411*4882a593Smuzhiyun domain_str = buf;
412*4882a593Smuzhiyun lpar = "0x0";
413*4882a593Smuzhiyun sindex = "chip";
414*4882a593Smuzhiyun break;
415*4882a593Smuzhiyun case HV_PERF_DOMAIN_PHYS_CORE:
416*4882a593Smuzhiyun domain_str = "?";
417*4882a593Smuzhiyun lpar = "0x0";
418*4882a593Smuzhiyun sindex = "core";
419*4882a593Smuzhiyun break;
420*4882a593Smuzhiyun default:
421*4882a593Smuzhiyun domain_str = "?";
422*4882a593Smuzhiyun lpar = "?";
423*4882a593Smuzhiyun sindex = "vcpu";
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun return kasprintf(GFP_KERNEL,
427*4882a593Smuzhiyun "domain=%s,offset=0x%x,%s=?,lpar=%s",
428*4882a593Smuzhiyun domain_str,
429*4882a593Smuzhiyun be16_to_cpu(event->event_counter_offs) +
430*4882a593Smuzhiyun be16_to_cpu(event->event_group_record_offs),
431*4882a593Smuzhiyun sindex,
432*4882a593Smuzhiyun lpar);
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun /* Avoid trusting fw to NUL terminate strings */
memdup_to_str(char * maybe_str,int max_len,gfp_t gfp)436*4882a593Smuzhiyun static char *memdup_to_str(char *maybe_str, int max_len, gfp_t gfp)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun return kasprintf(gfp, "%.*s", max_len, maybe_str);
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
device_show_string(struct device * dev,struct device_attribute * attr,char * buf)441*4882a593Smuzhiyun static ssize_t device_show_string(struct device *dev,
442*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun struct dev_ext_attribute *d;
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun d = container_of(attr, struct dev_ext_attribute, attr);
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun return sprintf(buf, "%s\n", (char *)d->var);
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)451*4882a593Smuzhiyun static ssize_t cpumask_show(struct device *dev,
452*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun return cpumap_print_to_pagebuf(true, buf, &hv_24x7_cpumask);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
sockets_show(struct device * dev,struct device_attribute * attr,char * buf)457*4882a593Smuzhiyun static ssize_t sockets_show(struct device *dev,
458*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun return sprintf(buf, "%d\n", phys_sockets);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
chipspersocket_show(struct device * dev,struct device_attribute * attr,char * buf)463*4882a593Smuzhiyun static ssize_t chipspersocket_show(struct device *dev,
464*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun return sprintf(buf, "%d\n", phys_chipspersocket);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
coresperchip_show(struct device * dev,struct device_attribute * attr,char * buf)469*4882a593Smuzhiyun static ssize_t coresperchip_show(struct device *dev,
470*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun return sprintf(buf, "%d\n", phys_coresperchip);
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun
device_str_attr_create_(char * name,char * str)475*4882a593Smuzhiyun static struct attribute *device_str_attr_create_(char *name, char *str)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun struct dev_ext_attribute *attr = kzalloc(sizeof(*attr), GFP_KERNEL);
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun if (!attr)
480*4882a593Smuzhiyun return NULL;
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun sysfs_attr_init(&attr->attr.attr);
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun attr->var = str;
485*4882a593Smuzhiyun attr->attr.attr.name = name;
486*4882a593Smuzhiyun attr->attr.attr.mode = 0444;
487*4882a593Smuzhiyun attr->attr.show = device_show_string;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun return &attr->attr.attr;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /*
493*4882a593Smuzhiyun * Allocate and initialize strings representing event attributes.
494*4882a593Smuzhiyun *
495*4882a593Smuzhiyun * NOTE: The strings allocated here are never destroyed and continue to
496*4882a593Smuzhiyun * exist till shutdown. This is to allow us to create as many events
497*4882a593Smuzhiyun * from the catalog as possible, even if we encounter errors with some.
498*4882a593Smuzhiyun * In case of changes to error paths in future, these may need to be
499*4882a593Smuzhiyun * freed by the caller.
500*4882a593Smuzhiyun */
device_str_attr_create(char * name,int name_max,int name_nonce,char * str,size_t str_max)501*4882a593Smuzhiyun static struct attribute *device_str_attr_create(char *name, int name_max,
502*4882a593Smuzhiyun int name_nonce,
503*4882a593Smuzhiyun char *str, size_t str_max)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun char *n;
506*4882a593Smuzhiyun char *s = memdup_to_str(str, str_max, GFP_KERNEL);
507*4882a593Smuzhiyun struct attribute *a;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun if (!s)
510*4882a593Smuzhiyun return NULL;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun if (!name_nonce)
513*4882a593Smuzhiyun n = kasprintf(GFP_KERNEL, "%.*s", name_max, name);
514*4882a593Smuzhiyun else
515*4882a593Smuzhiyun n = kasprintf(GFP_KERNEL, "%.*s__%d", name_max, name,
516*4882a593Smuzhiyun name_nonce);
517*4882a593Smuzhiyun if (!n)
518*4882a593Smuzhiyun goto out_s;
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun a = device_str_attr_create_(n, s);
521*4882a593Smuzhiyun if (!a)
522*4882a593Smuzhiyun goto out_n;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun return a;
525*4882a593Smuzhiyun out_n:
526*4882a593Smuzhiyun kfree(n);
527*4882a593Smuzhiyun out_s:
528*4882a593Smuzhiyun kfree(s);
529*4882a593Smuzhiyun return NULL;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
event_to_attr(unsigned ix,struct hv_24x7_event_data * event,unsigned domain,int nonce)532*4882a593Smuzhiyun static struct attribute *event_to_attr(unsigned ix,
533*4882a593Smuzhiyun struct hv_24x7_event_data *event,
534*4882a593Smuzhiyun unsigned domain,
535*4882a593Smuzhiyun int nonce)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun int event_name_len;
538*4882a593Smuzhiyun char *ev_name, *a_ev_name, *val;
539*4882a593Smuzhiyun struct attribute *attr;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun if (!domain_is_valid(domain)) {
542*4882a593Smuzhiyun pr_warn("catalog event %u has invalid domain %u\n",
543*4882a593Smuzhiyun ix, domain);
544*4882a593Smuzhiyun return NULL;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun val = event_fmt(event, domain);
548*4882a593Smuzhiyun if (!val)
549*4882a593Smuzhiyun return NULL;
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun ev_name = event_name(event, &event_name_len);
552*4882a593Smuzhiyun if (!nonce)
553*4882a593Smuzhiyun a_ev_name = kasprintf(GFP_KERNEL, "%.*s",
554*4882a593Smuzhiyun (int)event_name_len, ev_name);
555*4882a593Smuzhiyun else
556*4882a593Smuzhiyun a_ev_name = kasprintf(GFP_KERNEL, "%.*s__%d",
557*4882a593Smuzhiyun (int)event_name_len, ev_name, nonce);
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun if (!a_ev_name)
560*4882a593Smuzhiyun goto out_val;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun attr = device_str_attr_create_(a_ev_name, val);
563*4882a593Smuzhiyun if (!attr)
564*4882a593Smuzhiyun goto out_name;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun return attr;
567*4882a593Smuzhiyun out_name:
568*4882a593Smuzhiyun kfree(a_ev_name);
569*4882a593Smuzhiyun out_val:
570*4882a593Smuzhiyun kfree(val);
571*4882a593Smuzhiyun return NULL;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun
event_to_desc_attr(struct hv_24x7_event_data * event,int nonce)574*4882a593Smuzhiyun static struct attribute *event_to_desc_attr(struct hv_24x7_event_data *event,
575*4882a593Smuzhiyun int nonce)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun int nl, dl;
578*4882a593Smuzhiyun char *name = event_name(event, &nl);
579*4882a593Smuzhiyun char *desc = event_desc(event, &dl);
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /* If there isn't a description, don't create the sysfs file */
582*4882a593Smuzhiyun if (!dl)
583*4882a593Smuzhiyun return NULL;
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun return device_str_attr_create(name, nl, nonce, desc, dl);
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun static struct attribute *
event_to_long_desc_attr(struct hv_24x7_event_data * event,int nonce)589*4882a593Smuzhiyun event_to_long_desc_attr(struct hv_24x7_event_data *event, int nonce)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun int nl, dl;
592*4882a593Smuzhiyun char *name = event_name(event, &nl);
593*4882a593Smuzhiyun char *desc = event_long_desc(event, &dl);
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun /* If there isn't a description, don't create the sysfs file */
596*4882a593Smuzhiyun if (!dl)
597*4882a593Smuzhiyun return NULL;
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun return device_str_attr_create(name, nl, nonce, desc, dl);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun
event_data_to_attrs(unsigned ix,struct attribute ** attrs,struct hv_24x7_event_data * event,int nonce)602*4882a593Smuzhiyun static int event_data_to_attrs(unsigned ix, struct attribute **attrs,
603*4882a593Smuzhiyun struct hv_24x7_event_data *event, int nonce)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun *attrs = event_to_attr(ix, event, event->domain, nonce);
606*4882a593Smuzhiyun if (!*attrs)
607*4882a593Smuzhiyun return -1;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun return 0;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun /* */
613*4882a593Smuzhiyun struct event_uniq {
614*4882a593Smuzhiyun struct rb_node node;
615*4882a593Smuzhiyun const char *name;
616*4882a593Smuzhiyun int nl;
617*4882a593Smuzhiyun unsigned ct;
618*4882a593Smuzhiyun unsigned domain;
619*4882a593Smuzhiyun };
620*4882a593Smuzhiyun
memord(const void * d1,size_t s1,const void * d2,size_t s2)621*4882a593Smuzhiyun static int memord(const void *d1, size_t s1, const void *d2, size_t s2)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun if (s1 < s2)
624*4882a593Smuzhiyun return 1;
625*4882a593Smuzhiyun if (s1 > s2)
626*4882a593Smuzhiyun return -1;
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun return memcmp(d1, d2, s1);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
ev_uniq_ord(const void * v1,size_t s1,unsigned d1,const void * v2,size_t s2,unsigned d2)631*4882a593Smuzhiyun static int ev_uniq_ord(const void *v1, size_t s1, unsigned d1, const void *v2,
632*4882a593Smuzhiyun size_t s2, unsigned d2)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun int r = memord(v1, s1, v2, s2);
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun if (r)
637*4882a593Smuzhiyun return r;
638*4882a593Smuzhiyun if (d1 > d2)
639*4882a593Smuzhiyun return 1;
640*4882a593Smuzhiyun if (d2 > d1)
641*4882a593Smuzhiyun return -1;
642*4882a593Smuzhiyun return 0;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun
event_uniq_add(struct rb_root * root,const char * name,int nl,unsigned domain)645*4882a593Smuzhiyun static int event_uniq_add(struct rb_root *root, const char *name, int nl,
646*4882a593Smuzhiyun unsigned domain)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun struct rb_node **new = &(root->rb_node), *parent = NULL;
649*4882a593Smuzhiyun struct event_uniq *data;
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun /* Figure out where to put new node */
652*4882a593Smuzhiyun while (*new) {
653*4882a593Smuzhiyun struct event_uniq *it;
654*4882a593Smuzhiyun int result;
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun it = rb_entry(*new, struct event_uniq, node);
657*4882a593Smuzhiyun result = ev_uniq_ord(name, nl, domain, it->name, it->nl,
658*4882a593Smuzhiyun it->domain);
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun parent = *new;
661*4882a593Smuzhiyun if (result < 0)
662*4882a593Smuzhiyun new = &((*new)->rb_left);
663*4882a593Smuzhiyun else if (result > 0)
664*4882a593Smuzhiyun new = &((*new)->rb_right);
665*4882a593Smuzhiyun else {
666*4882a593Smuzhiyun it->ct++;
667*4882a593Smuzhiyun pr_info("found a duplicate event %.*s, ct=%u\n", nl,
668*4882a593Smuzhiyun name, it->ct);
669*4882a593Smuzhiyun return it->ct;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun data = kmalloc(sizeof(*data), GFP_KERNEL);
674*4882a593Smuzhiyun if (!data)
675*4882a593Smuzhiyun return -ENOMEM;
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun *data = (struct event_uniq) {
678*4882a593Smuzhiyun .name = name,
679*4882a593Smuzhiyun .nl = nl,
680*4882a593Smuzhiyun .ct = 0,
681*4882a593Smuzhiyun .domain = domain,
682*4882a593Smuzhiyun };
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun /* Add new node and rebalance tree. */
685*4882a593Smuzhiyun rb_link_node(&data->node, parent, new);
686*4882a593Smuzhiyun rb_insert_color(&data->node, root);
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun /* data->ct */
689*4882a593Smuzhiyun return 0;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun
event_uniq_destroy(struct rb_root * root)692*4882a593Smuzhiyun static void event_uniq_destroy(struct rb_root *root)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun /*
695*4882a593Smuzhiyun * the strings we point to are in the giant block of memory filled by
696*4882a593Smuzhiyun * the catalog, and are freed separately.
697*4882a593Smuzhiyun */
698*4882a593Smuzhiyun struct event_uniq *pos, *n;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun rbtree_postorder_for_each_entry_safe(pos, n, root, node)
701*4882a593Smuzhiyun kfree(pos);
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun /*
706*4882a593Smuzhiyun * ensure the event structure's sizes are self consistent and don't cause us to
707*4882a593Smuzhiyun * read outside of the event
708*4882a593Smuzhiyun *
709*4882a593Smuzhiyun * On success, return the event length in bytes.
710*4882a593Smuzhiyun * Otherwise, return -1 (and print as appropriate).
711*4882a593Smuzhiyun */
catalog_event_len_validate(struct hv_24x7_event_data * event,size_t event_idx,size_t event_data_bytes,size_t event_entry_count,size_t offset,void * end)712*4882a593Smuzhiyun static ssize_t catalog_event_len_validate(struct hv_24x7_event_data *event,
713*4882a593Smuzhiyun size_t event_idx,
714*4882a593Smuzhiyun size_t event_data_bytes,
715*4882a593Smuzhiyun size_t event_entry_count,
716*4882a593Smuzhiyun size_t offset, void *end)
717*4882a593Smuzhiyun {
718*4882a593Smuzhiyun ssize_t ev_len;
719*4882a593Smuzhiyun void *ev_end, *calc_ev_end;
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun if (offset >= event_data_bytes)
722*4882a593Smuzhiyun return -1;
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun if (event_idx >= event_entry_count) {
725*4882a593Smuzhiyun pr_devel("catalog event data has %zu bytes of padding after last event\n",
726*4882a593Smuzhiyun event_data_bytes - offset);
727*4882a593Smuzhiyun return -1;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun if (!event_fixed_portion_is_within(event, end)) {
731*4882a593Smuzhiyun pr_warn("event %zu fixed portion is not within range\n",
732*4882a593Smuzhiyun event_idx);
733*4882a593Smuzhiyun return -1;
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun ev_len = be16_to_cpu(event->length);
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun if (ev_len % 16)
739*4882a593Smuzhiyun pr_info("event %zu has length %zu not divisible by 16: event=%pK\n",
740*4882a593Smuzhiyun event_idx, ev_len, event);
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun ev_end = (__u8 *)event + ev_len;
743*4882a593Smuzhiyun if (ev_end > end) {
744*4882a593Smuzhiyun pr_warn("event %zu has .length=%zu, ends after buffer end: ev_end=%pK > end=%pK, offset=%zu\n",
745*4882a593Smuzhiyun event_idx, ev_len, ev_end, end,
746*4882a593Smuzhiyun offset);
747*4882a593Smuzhiyun return -1;
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun calc_ev_end = event_end(event, end);
751*4882a593Smuzhiyun if (!calc_ev_end) {
752*4882a593Smuzhiyun pr_warn("event %zu has a calculated length which exceeds buffer length %zu: event=%pK end=%pK, offset=%zu\n",
753*4882a593Smuzhiyun event_idx, event_data_bytes, event, end,
754*4882a593Smuzhiyun offset);
755*4882a593Smuzhiyun return -1;
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun if (calc_ev_end > ev_end) {
759*4882a593Smuzhiyun pr_warn("event %zu exceeds it's own length: event=%pK, end=%pK, offset=%zu, calc_ev_end=%pK\n",
760*4882a593Smuzhiyun event_idx, event, ev_end, offset, calc_ev_end);
761*4882a593Smuzhiyun return -1;
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun return ev_len;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun #define MAX_4K (SIZE_MAX / 4096)
768*4882a593Smuzhiyun
create_events_from_catalog(struct attribute *** events_,struct attribute *** event_descs_,struct attribute *** event_long_descs_)769*4882a593Smuzhiyun static int create_events_from_catalog(struct attribute ***events_,
770*4882a593Smuzhiyun struct attribute ***event_descs_,
771*4882a593Smuzhiyun struct attribute ***event_long_descs_)
772*4882a593Smuzhiyun {
773*4882a593Smuzhiyun long hret;
774*4882a593Smuzhiyun size_t catalog_len, catalog_page_len, event_entry_count,
775*4882a593Smuzhiyun event_data_len, event_data_offs,
776*4882a593Smuzhiyun event_data_bytes, junk_events, event_idx, event_attr_ct, i,
777*4882a593Smuzhiyun attr_max, event_idx_last, desc_ct, long_desc_ct;
778*4882a593Smuzhiyun ssize_t ct, ev_len;
779*4882a593Smuzhiyun uint64_t catalog_version_num;
780*4882a593Smuzhiyun struct attribute **events, **event_descs, **event_long_descs;
781*4882a593Smuzhiyun struct hv_24x7_catalog_page_0 *page_0 =
782*4882a593Smuzhiyun kmem_cache_alloc(hv_page_cache, GFP_KERNEL);
783*4882a593Smuzhiyun void *page = page_0;
784*4882a593Smuzhiyun void *event_data, *end;
785*4882a593Smuzhiyun struct hv_24x7_event_data *event;
786*4882a593Smuzhiyun struct rb_root ev_uniq = RB_ROOT;
787*4882a593Smuzhiyun int ret = 0;
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun if (!page) {
790*4882a593Smuzhiyun ret = -ENOMEM;
791*4882a593Smuzhiyun goto e_out;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun hret = h_get_24x7_catalog_page(page, 0, 0);
795*4882a593Smuzhiyun if (hret) {
796*4882a593Smuzhiyun ret = -EIO;
797*4882a593Smuzhiyun goto e_free;
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun catalog_version_num = be64_to_cpu(page_0->version);
801*4882a593Smuzhiyun catalog_page_len = be32_to_cpu(page_0->length);
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun if (MAX_4K < catalog_page_len) {
804*4882a593Smuzhiyun pr_err("invalid page count: %zu\n", catalog_page_len);
805*4882a593Smuzhiyun ret = -EIO;
806*4882a593Smuzhiyun goto e_free;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun catalog_len = catalog_page_len * 4096;
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun event_entry_count = be16_to_cpu(page_0->event_entry_count);
812*4882a593Smuzhiyun event_data_offs = be16_to_cpu(page_0->event_data_offs);
813*4882a593Smuzhiyun event_data_len = be16_to_cpu(page_0->event_data_len);
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun pr_devel("cv %llu cl %zu eec %zu edo %zu edl %zu\n",
816*4882a593Smuzhiyun catalog_version_num, catalog_len,
817*4882a593Smuzhiyun event_entry_count, event_data_offs, event_data_len);
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun if ((MAX_4K < event_data_len)
820*4882a593Smuzhiyun || (MAX_4K < event_data_offs)
821*4882a593Smuzhiyun || (MAX_4K - event_data_offs < event_data_len)) {
822*4882a593Smuzhiyun pr_err("invalid event data offs %zu and/or len %zu\n",
823*4882a593Smuzhiyun event_data_offs, event_data_len);
824*4882a593Smuzhiyun ret = -EIO;
825*4882a593Smuzhiyun goto e_free;
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun if ((event_data_offs + event_data_len) > catalog_page_len) {
829*4882a593Smuzhiyun pr_err("event data %zu-%zu does not fit inside catalog 0-%zu\n",
830*4882a593Smuzhiyun event_data_offs,
831*4882a593Smuzhiyun event_data_offs + event_data_len,
832*4882a593Smuzhiyun catalog_page_len);
833*4882a593Smuzhiyun ret = -EIO;
834*4882a593Smuzhiyun goto e_free;
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun if (SIZE_MAX - 1 < event_entry_count) {
838*4882a593Smuzhiyun pr_err("event_entry_count %zu is invalid\n", event_entry_count);
839*4882a593Smuzhiyun ret = -EIO;
840*4882a593Smuzhiyun goto e_free;
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun event_data_bytes = event_data_len * 4096;
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun /*
846*4882a593Smuzhiyun * event data can span several pages, events can cross between these
847*4882a593Smuzhiyun * pages. Use vmalloc to make this easier.
848*4882a593Smuzhiyun */
849*4882a593Smuzhiyun event_data = vmalloc(event_data_bytes);
850*4882a593Smuzhiyun if (!event_data) {
851*4882a593Smuzhiyun pr_err("could not allocate event data\n");
852*4882a593Smuzhiyun ret = -ENOMEM;
853*4882a593Smuzhiyun goto e_free;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun end = event_data + event_data_bytes;
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun /*
859*4882a593Smuzhiyun * using vmalloc_to_phys() like this only works if PAGE_SIZE is
860*4882a593Smuzhiyun * divisible by 4096
861*4882a593Smuzhiyun */
862*4882a593Smuzhiyun BUILD_BUG_ON(PAGE_SIZE % 4096);
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun for (i = 0; i < event_data_len; i++) {
865*4882a593Smuzhiyun hret = h_get_24x7_catalog_page_(
866*4882a593Smuzhiyun vmalloc_to_phys(event_data + i * 4096),
867*4882a593Smuzhiyun catalog_version_num,
868*4882a593Smuzhiyun i + event_data_offs);
869*4882a593Smuzhiyun if (hret) {
870*4882a593Smuzhiyun pr_err("Failed to get event data in page %zu: rc=%ld\n",
871*4882a593Smuzhiyun i + event_data_offs, hret);
872*4882a593Smuzhiyun ret = -EIO;
873*4882a593Smuzhiyun goto e_event_data;
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun /*
878*4882a593Smuzhiyun * scan the catalog to determine the number of attributes we need, and
879*4882a593Smuzhiyun * verify it at the same time.
880*4882a593Smuzhiyun */
881*4882a593Smuzhiyun for (junk_events = 0, event = event_data, event_idx = 0, attr_max = 0;
882*4882a593Smuzhiyun ;
883*4882a593Smuzhiyun event_idx++, event = (void *)event + ev_len) {
884*4882a593Smuzhiyun size_t offset = (void *)event - (void *)event_data;
885*4882a593Smuzhiyun char *name;
886*4882a593Smuzhiyun int nl;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun ev_len = catalog_event_len_validate(event, event_idx,
889*4882a593Smuzhiyun event_data_bytes,
890*4882a593Smuzhiyun event_entry_count,
891*4882a593Smuzhiyun offset, end);
892*4882a593Smuzhiyun if (ev_len < 0)
893*4882a593Smuzhiyun break;
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun name = event_name(event, &nl);
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun if (event->event_group_record_len == 0) {
898*4882a593Smuzhiyun pr_devel("invalid event %zu (%.*s): group_record_len == 0, skipping\n",
899*4882a593Smuzhiyun event_idx, nl, name);
900*4882a593Smuzhiyun junk_events++;
901*4882a593Smuzhiyun continue;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun if (!catalog_entry_domain_is_valid(event->domain)) {
905*4882a593Smuzhiyun pr_info("event %zu (%.*s) has invalid domain %d\n",
906*4882a593Smuzhiyun event_idx, nl, name, event->domain);
907*4882a593Smuzhiyun junk_events++;
908*4882a593Smuzhiyun continue;
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun attr_max++;
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun event_idx_last = event_idx;
915*4882a593Smuzhiyun if (event_idx_last != event_entry_count)
916*4882a593Smuzhiyun pr_warn("event buffer ended before listed # of events were parsed (got %zu, wanted %zu, junk %zu)\n",
917*4882a593Smuzhiyun event_idx_last, event_entry_count, junk_events);
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun events = kmalloc_array(attr_max + 1, sizeof(*events), GFP_KERNEL);
920*4882a593Smuzhiyun if (!events) {
921*4882a593Smuzhiyun ret = -ENOMEM;
922*4882a593Smuzhiyun goto e_event_data;
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun event_descs = kmalloc_array(event_idx + 1, sizeof(*event_descs),
926*4882a593Smuzhiyun GFP_KERNEL);
927*4882a593Smuzhiyun if (!event_descs) {
928*4882a593Smuzhiyun ret = -ENOMEM;
929*4882a593Smuzhiyun goto e_event_attrs;
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun event_long_descs = kmalloc_array(event_idx + 1,
933*4882a593Smuzhiyun sizeof(*event_long_descs), GFP_KERNEL);
934*4882a593Smuzhiyun if (!event_long_descs) {
935*4882a593Smuzhiyun ret = -ENOMEM;
936*4882a593Smuzhiyun goto e_event_descs;
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun /* Iterate over the catalog filling in the attribute vector */
940*4882a593Smuzhiyun for (junk_events = 0, event_attr_ct = 0, desc_ct = 0, long_desc_ct = 0,
941*4882a593Smuzhiyun event = event_data, event_idx = 0;
942*4882a593Smuzhiyun event_idx < event_idx_last;
943*4882a593Smuzhiyun event_idx++, ev_len = be16_to_cpu(event->length),
944*4882a593Smuzhiyun event = (void *)event + ev_len) {
945*4882a593Smuzhiyun char *name;
946*4882a593Smuzhiyun int nl;
947*4882a593Smuzhiyun int nonce;
948*4882a593Smuzhiyun /*
949*4882a593Smuzhiyun * these are the only "bad" events that are intermixed and that
950*4882a593Smuzhiyun * we can ignore without issue. make sure to skip them here
951*4882a593Smuzhiyun */
952*4882a593Smuzhiyun if (event->event_group_record_len == 0)
953*4882a593Smuzhiyun continue;
954*4882a593Smuzhiyun if (!catalog_entry_domain_is_valid(event->domain))
955*4882a593Smuzhiyun continue;
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun name = event_name(event, &nl);
958*4882a593Smuzhiyun nonce = event_uniq_add(&ev_uniq, name, nl, event->domain);
959*4882a593Smuzhiyun ct = event_data_to_attrs(event_idx, events + event_attr_ct,
960*4882a593Smuzhiyun event, nonce);
961*4882a593Smuzhiyun if (ct < 0) {
962*4882a593Smuzhiyun pr_warn("event %zu (%.*s) creation failure, skipping\n",
963*4882a593Smuzhiyun event_idx, nl, name);
964*4882a593Smuzhiyun junk_events++;
965*4882a593Smuzhiyun } else {
966*4882a593Smuzhiyun event_attr_ct++;
967*4882a593Smuzhiyun event_descs[desc_ct] = event_to_desc_attr(event, nonce);
968*4882a593Smuzhiyun if (event_descs[desc_ct])
969*4882a593Smuzhiyun desc_ct++;
970*4882a593Smuzhiyun event_long_descs[long_desc_ct] =
971*4882a593Smuzhiyun event_to_long_desc_attr(event, nonce);
972*4882a593Smuzhiyun if (event_long_descs[long_desc_ct])
973*4882a593Smuzhiyun long_desc_ct++;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun pr_info("read %zu catalog entries, created %zu event attrs (%zu failures), %zu descs\n",
978*4882a593Smuzhiyun event_idx, event_attr_ct, junk_events, desc_ct);
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun events[event_attr_ct] = NULL;
981*4882a593Smuzhiyun event_descs[desc_ct] = NULL;
982*4882a593Smuzhiyun event_long_descs[long_desc_ct] = NULL;
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun event_uniq_destroy(&ev_uniq);
985*4882a593Smuzhiyun vfree(event_data);
986*4882a593Smuzhiyun kmem_cache_free(hv_page_cache, page);
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun *events_ = events;
989*4882a593Smuzhiyun *event_descs_ = event_descs;
990*4882a593Smuzhiyun *event_long_descs_ = event_long_descs;
991*4882a593Smuzhiyun return 0;
992*4882a593Smuzhiyun
993*4882a593Smuzhiyun e_event_descs:
994*4882a593Smuzhiyun kfree(event_descs);
995*4882a593Smuzhiyun e_event_attrs:
996*4882a593Smuzhiyun kfree(events);
997*4882a593Smuzhiyun e_event_data:
998*4882a593Smuzhiyun vfree(event_data);
999*4882a593Smuzhiyun e_free:
1000*4882a593Smuzhiyun kmem_cache_free(hv_page_cache, page);
1001*4882a593Smuzhiyun e_out:
1002*4882a593Smuzhiyun *events_ = NULL;
1003*4882a593Smuzhiyun *event_descs_ = NULL;
1004*4882a593Smuzhiyun *event_long_descs_ = NULL;
1005*4882a593Smuzhiyun return ret;
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun
catalog_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t offset,size_t count)1008*4882a593Smuzhiyun static ssize_t catalog_read(struct file *filp, struct kobject *kobj,
1009*4882a593Smuzhiyun struct bin_attribute *bin_attr, char *buf,
1010*4882a593Smuzhiyun loff_t offset, size_t count)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun long hret;
1013*4882a593Smuzhiyun ssize_t ret = 0;
1014*4882a593Smuzhiyun size_t catalog_len = 0, catalog_page_len = 0;
1015*4882a593Smuzhiyun loff_t page_offset = 0;
1016*4882a593Smuzhiyun loff_t offset_in_page;
1017*4882a593Smuzhiyun size_t copy_len;
1018*4882a593Smuzhiyun uint64_t catalog_version_num = 0;
1019*4882a593Smuzhiyun void *page = kmem_cache_alloc(hv_page_cache, GFP_USER);
1020*4882a593Smuzhiyun struct hv_24x7_catalog_page_0 *page_0 = page;
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun if (!page)
1023*4882a593Smuzhiyun return -ENOMEM;
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun hret = h_get_24x7_catalog_page(page, 0, 0);
1026*4882a593Smuzhiyun if (hret) {
1027*4882a593Smuzhiyun ret = -EIO;
1028*4882a593Smuzhiyun goto e_free;
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun catalog_version_num = be64_to_cpu(page_0->version);
1032*4882a593Smuzhiyun catalog_page_len = be32_to_cpu(page_0->length);
1033*4882a593Smuzhiyun catalog_len = catalog_page_len * 4096;
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun page_offset = offset / 4096;
1036*4882a593Smuzhiyun offset_in_page = offset % 4096;
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun if (page_offset >= catalog_page_len)
1039*4882a593Smuzhiyun goto e_free;
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun if (page_offset != 0) {
1042*4882a593Smuzhiyun hret = h_get_24x7_catalog_page(page, catalog_version_num,
1043*4882a593Smuzhiyun page_offset);
1044*4882a593Smuzhiyun if (hret) {
1045*4882a593Smuzhiyun ret = -EIO;
1046*4882a593Smuzhiyun goto e_free;
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun copy_len = 4096 - offset_in_page;
1051*4882a593Smuzhiyun if (copy_len > count)
1052*4882a593Smuzhiyun copy_len = count;
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun memcpy(buf, page+offset_in_page, copy_len);
1055*4882a593Smuzhiyun ret = copy_len;
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun e_free:
1058*4882a593Smuzhiyun if (hret)
1059*4882a593Smuzhiyun pr_err("h_get_24x7_catalog_page(ver=%lld, page=%lld) failed:"
1060*4882a593Smuzhiyun " rc=%ld\n",
1061*4882a593Smuzhiyun catalog_version_num, page_offset, hret);
1062*4882a593Smuzhiyun kmem_cache_free(hv_page_cache, page);
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun pr_devel("catalog_read: offset=%lld(%lld) count=%zu "
1065*4882a593Smuzhiyun "catalog_len=%zu(%zu) => %zd\n", offset, page_offset,
1066*4882a593Smuzhiyun count, catalog_len, catalog_page_len, ret);
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun return ret;
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun
domains_show(struct device * dev,struct device_attribute * attr,char * page)1071*4882a593Smuzhiyun static ssize_t domains_show(struct device *dev, struct device_attribute *attr,
1072*4882a593Smuzhiyun char *page)
1073*4882a593Smuzhiyun {
1074*4882a593Smuzhiyun int d, n, count = 0;
1075*4882a593Smuzhiyun const char *str;
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun for (d = 0; d < HV_PERF_DOMAIN_MAX; d++) {
1078*4882a593Smuzhiyun str = domain_name(d);
1079*4882a593Smuzhiyun if (!str)
1080*4882a593Smuzhiyun continue;
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun n = sprintf(page, "%d: %s\n", d, str);
1083*4882a593Smuzhiyun if (n < 0)
1084*4882a593Smuzhiyun break;
1085*4882a593Smuzhiyun
1086*4882a593Smuzhiyun count += n;
1087*4882a593Smuzhiyun page += n;
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun return count;
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun #define PAGE_0_ATTR(_name, _fmt, _expr) \
1093*4882a593Smuzhiyun static ssize_t _name##_show(struct device *dev, \
1094*4882a593Smuzhiyun struct device_attribute *dev_attr, \
1095*4882a593Smuzhiyun char *buf) \
1096*4882a593Smuzhiyun { \
1097*4882a593Smuzhiyun long hret; \
1098*4882a593Smuzhiyun ssize_t ret = 0; \
1099*4882a593Smuzhiyun void *page = kmem_cache_alloc(hv_page_cache, GFP_USER); \
1100*4882a593Smuzhiyun struct hv_24x7_catalog_page_0 *page_0 = page; \
1101*4882a593Smuzhiyun if (!page) \
1102*4882a593Smuzhiyun return -ENOMEM; \
1103*4882a593Smuzhiyun hret = h_get_24x7_catalog_page(page, 0, 0); \
1104*4882a593Smuzhiyun if (hret) { \
1105*4882a593Smuzhiyun ret = -EIO; \
1106*4882a593Smuzhiyun goto e_free; \
1107*4882a593Smuzhiyun } \
1108*4882a593Smuzhiyun ret = sprintf(buf, _fmt, _expr); \
1109*4882a593Smuzhiyun e_free: \
1110*4882a593Smuzhiyun kmem_cache_free(hv_page_cache, page); \
1111*4882a593Smuzhiyun return ret; \
1112*4882a593Smuzhiyun } \
1113*4882a593Smuzhiyun static DEVICE_ATTR_RO(_name)
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun PAGE_0_ATTR(catalog_version, "%lld\n",
1116*4882a593Smuzhiyun (unsigned long long)be64_to_cpu(page_0->version));
1117*4882a593Smuzhiyun PAGE_0_ATTR(catalog_len, "%lld\n",
1118*4882a593Smuzhiyun (unsigned long long)be32_to_cpu(page_0->length) * 4096);
1119*4882a593Smuzhiyun static BIN_ATTR_RO(catalog, 0/* real length varies */);
1120*4882a593Smuzhiyun static DEVICE_ATTR_RO(domains);
1121*4882a593Smuzhiyun static DEVICE_ATTR_RO(sockets);
1122*4882a593Smuzhiyun static DEVICE_ATTR_RO(chipspersocket);
1123*4882a593Smuzhiyun static DEVICE_ATTR_RO(coresperchip);
1124*4882a593Smuzhiyun static DEVICE_ATTR_RO(cpumask);
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun static struct bin_attribute *if_bin_attrs[] = {
1127*4882a593Smuzhiyun &bin_attr_catalog,
1128*4882a593Smuzhiyun NULL,
1129*4882a593Smuzhiyun };
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun static struct attribute *cpumask_attrs[] = {
1132*4882a593Smuzhiyun &dev_attr_cpumask.attr,
1133*4882a593Smuzhiyun NULL,
1134*4882a593Smuzhiyun };
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun static struct attribute_group cpumask_attr_group = {
1137*4882a593Smuzhiyun .attrs = cpumask_attrs,
1138*4882a593Smuzhiyun };
1139*4882a593Smuzhiyun
1140*4882a593Smuzhiyun static struct attribute *if_attrs[] = {
1141*4882a593Smuzhiyun &dev_attr_catalog_len.attr,
1142*4882a593Smuzhiyun &dev_attr_catalog_version.attr,
1143*4882a593Smuzhiyun &dev_attr_domains.attr,
1144*4882a593Smuzhiyun &dev_attr_sockets.attr,
1145*4882a593Smuzhiyun &dev_attr_chipspersocket.attr,
1146*4882a593Smuzhiyun &dev_attr_coresperchip.attr,
1147*4882a593Smuzhiyun NULL,
1148*4882a593Smuzhiyun };
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun static struct attribute_group if_group = {
1151*4882a593Smuzhiyun .name = "interface",
1152*4882a593Smuzhiyun .bin_attrs = if_bin_attrs,
1153*4882a593Smuzhiyun .attrs = if_attrs,
1154*4882a593Smuzhiyun };
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun static const struct attribute_group *attr_groups[] = {
1157*4882a593Smuzhiyun &format_group,
1158*4882a593Smuzhiyun &event_group,
1159*4882a593Smuzhiyun &event_desc_group,
1160*4882a593Smuzhiyun &event_long_desc_group,
1161*4882a593Smuzhiyun &if_group,
1162*4882a593Smuzhiyun &cpumask_attr_group,
1163*4882a593Smuzhiyun NULL,
1164*4882a593Smuzhiyun };
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun /*
1167*4882a593Smuzhiyun * Start the process for a new H_GET_24x7_DATA hcall.
1168*4882a593Smuzhiyun */
init_24x7_request(struct hv_24x7_request_buffer * request_buffer,struct hv_24x7_data_result_buffer * result_buffer)1169*4882a593Smuzhiyun static void init_24x7_request(struct hv_24x7_request_buffer *request_buffer,
1170*4882a593Smuzhiyun struct hv_24x7_data_result_buffer *result_buffer)
1171*4882a593Smuzhiyun {
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun memset(request_buffer, 0, H24x7_DATA_BUFFER_SIZE);
1174*4882a593Smuzhiyun memset(result_buffer, 0, H24x7_DATA_BUFFER_SIZE);
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun request_buffer->interface_version = interface_version;
1177*4882a593Smuzhiyun /* memset above set request_buffer->num_requests to 0 */
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun /*
1181*4882a593Smuzhiyun * Commit (i.e perform) the H_GET_24x7_DATA hcall using the data collected
1182*4882a593Smuzhiyun * by 'init_24x7_request()' and 'add_event_to_24x7_request()'.
1183*4882a593Smuzhiyun */
make_24x7_request(struct hv_24x7_request_buffer * request_buffer,struct hv_24x7_data_result_buffer * result_buffer)1184*4882a593Smuzhiyun static int make_24x7_request(struct hv_24x7_request_buffer *request_buffer,
1185*4882a593Smuzhiyun struct hv_24x7_data_result_buffer *result_buffer)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun long ret;
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun /*
1190*4882a593Smuzhiyun * NOTE: Due to variable number of array elements in request and
1191*4882a593Smuzhiyun * result buffer(s), sizeof() is not reliable. Use the actual
1192*4882a593Smuzhiyun * allocated buffer size, H24x7_DATA_BUFFER_SIZE.
1193*4882a593Smuzhiyun */
1194*4882a593Smuzhiyun ret = plpar_hcall_norets(H_GET_24X7_DATA,
1195*4882a593Smuzhiyun virt_to_phys(request_buffer), H24x7_DATA_BUFFER_SIZE,
1196*4882a593Smuzhiyun virt_to_phys(result_buffer), H24x7_DATA_BUFFER_SIZE);
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun if (ret) {
1199*4882a593Smuzhiyun struct hv_24x7_request *req;
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun req = request_buffer->requests;
1202*4882a593Smuzhiyun pr_notice_ratelimited("hcall failed: [%d %#x %#x %d] => ret 0x%lx (%ld) detail=0x%x failing ix=%x\n",
1203*4882a593Smuzhiyun req->performance_domain, req->data_offset,
1204*4882a593Smuzhiyun req->starting_ix, req->starting_lpar_ix,
1205*4882a593Smuzhiyun ret, ret, result_buffer->detailed_rc,
1206*4882a593Smuzhiyun result_buffer->failing_request_ix);
1207*4882a593Smuzhiyun return -EIO;
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun return 0;
1211*4882a593Smuzhiyun }
1212*4882a593Smuzhiyun
1213*4882a593Smuzhiyun /*
1214*4882a593Smuzhiyun * Add the given @event to the next slot in the 24x7 request_buffer.
1215*4882a593Smuzhiyun *
1216*4882a593Smuzhiyun * Note that H_GET_24X7_DATA hcall allows reading several counters'
1217*4882a593Smuzhiyun * values in a single HCALL. We expect the caller to add events to the
1218*4882a593Smuzhiyun * request buffer one by one, make the HCALL and process the results.
1219*4882a593Smuzhiyun */
add_event_to_24x7_request(struct perf_event * event,struct hv_24x7_request_buffer * request_buffer)1220*4882a593Smuzhiyun static int add_event_to_24x7_request(struct perf_event *event,
1221*4882a593Smuzhiyun struct hv_24x7_request_buffer *request_buffer)
1222*4882a593Smuzhiyun {
1223*4882a593Smuzhiyun u16 idx;
1224*4882a593Smuzhiyun int i;
1225*4882a593Smuzhiyun size_t req_size;
1226*4882a593Smuzhiyun struct hv_24x7_request *req;
1227*4882a593Smuzhiyun
1228*4882a593Smuzhiyun if (request_buffer->num_requests >=
1229*4882a593Smuzhiyun max_num_requests(request_buffer->interface_version)) {
1230*4882a593Smuzhiyun pr_devel("Too many requests for 24x7 HCALL %d\n",
1231*4882a593Smuzhiyun request_buffer->num_requests);
1232*4882a593Smuzhiyun return -EINVAL;
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun
1235*4882a593Smuzhiyun switch (event_get_domain(event)) {
1236*4882a593Smuzhiyun case HV_PERF_DOMAIN_PHYS_CHIP:
1237*4882a593Smuzhiyun idx = event_get_chip(event);
1238*4882a593Smuzhiyun break;
1239*4882a593Smuzhiyun case HV_PERF_DOMAIN_PHYS_CORE:
1240*4882a593Smuzhiyun idx = event_get_core(event);
1241*4882a593Smuzhiyun break;
1242*4882a593Smuzhiyun default:
1243*4882a593Smuzhiyun idx = event_get_vcpu(event);
1244*4882a593Smuzhiyun }
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun req_size = H24x7_REQUEST_SIZE(request_buffer->interface_version);
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun i = request_buffer->num_requests++;
1249*4882a593Smuzhiyun req = (void *) request_buffer->requests + i * req_size;
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun req->performance_domain = event_get_domain(event);
1252*4882a593Smuzhiyun req->data_size = cpu_to_be16(8);
1253*4882a593Smuzhiyun req->data_offset = cpu_to_be32(event_get_offset(event));
1254*4882a593Smuzhiyun req->starting_lpar_ix = cpu_to_be16(event_get_lpar(event));
1255*4882a593Smuzhiyun req->max_num_lpars = cpu_to_be16(1);
1256*4882a593Smuzhiyun req->starting_ix = cpu_to_be16(idx);
1257*4882a593Smuzhiyun req->max_ix = cpu_to_be16(1);
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun if (request_buffer->interface_version > 1) {
1260*4882a593Smuzhiyun if (domain_needs_aggregation(req->performance_domain))
1261*4882a593Smuzhiyun req->max_num_thread_groups = -1;
1262*4882a593Smuzhiyun else if (req->performance_domain != HV_PERF_DOMAIN_PHYS_CHIP) {
1263*4882a593Smuzhiyun req->starting_thread_group_ix = idx % 2;
1264*4882a593Smuzhiyun req->max_num_thread_groups = 1;
1265*4882a593Smuzhiyun }
1266*4882a593Smuzhiyun }
1267*4882a593Smuzhiyun
1268*4882a593Smuzhiyun return 0;
1269*4882a593Smuzhiyun }
1270*4882a593Smuzhiyun
1271*4882a593Smuzhiyun /**
1272*4882a593Smuzhiyun * get_count_from_result - get event count from all result elements in result
1273*4882a593Smuzhiyun *
1274*4882a593Smuzhiyun * If the event corresponding to this result needs aggregation of the result
1275*4882a593Smuzhiyun * element values, then this function does that.
1276*4882a593Smuzhiyun *
1277*4882a593Smuzhiyun * @event: Event associated with @res.
1278*4882a593Smuzhiyun * @resb: Result buffer containing @res.
1279*4882a593Smuzhiyun * @res: Result to work on.
1280*4882a593Smuzhiyun * @countp: Output variable containing the event count.
1281*4882a593Smuzhiyun * @next: Optional output variable pointing to the next result in @resb.
1282*4882a593Smuzhiyun */
get_count_from_result(struct perf_event * event,struct hv_24x7_data_result_buffer * resb,struct hv_24x7_result * res,u64 * countp,struct hv_24x7_result ** next)1283*4882a593Smuzhiyun static int get_count_from_result(struct perf_event *event,
1284*4882a593Smuzhiyun struct hv_24x7_data_result_buffer *resb,
1285*4882a593Smuzhiyun struct hv_24x7_result *res, u64 *countp,
1286*4882a593Smuzhiyun struct hv_24x7_result **next)
1287*4882a593Smuzhiyun {
1288*4882a593Smuzhiyun u16 num_elements = be16_to_cpu(res->num_elements_returned);
1289*4882a593Smuzhiyun u16 data_size = be16_to_cpu(res->result_element_data_size);
1290*4882a593Smuzhiyun unsigned int data_offset;
1291*4882a593Smuzhiyun void *element_data;
1292*4882a593Smuzhiyun int i;
1293*4882a593Smuzhiyun u64 count;
1294*4882a593Smuzhiyun
1295*4882a593Smuzhiyun /*
1296*4882a593Smuzhiyun * We can bail out early if the result is empty.
1297*4882a593Smuzhiyun */
1298*4882a593Smuzhiyun if (!num_elements) {
1299*4882a593Smuzhiyun pr_debug("Result of request %hhu is empty, nothing to do\n",
1300*4882a593Smuzhiyun res->result_ix);
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun if (next)
1303*4882a593Smuzhiyun *next = (struct hv_24x7_result *) res->elements;
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun return -ENODATA;
1306*4882a593Smuzhiyun }
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun /*
1309*4882a593Smuzhiyun * Since we always specify 1 as the maximum for the smallest resource
1310*4882a593Smuzhiyun * we're requesting, there should to be only one element per result.
1311*4882a593Smuzhiyun * Except when an event needs aggregation, in which case there are more.
1312*4882a593Smuzhiyun */
1313*4882a593Smuzhiyun if (num_elements != 1 &&
1314*4882a593Smuzhiyun !domain_needs_aggregation(event_get_domain(event))) {
1315*4882a593Smuzhiyun pr_err("Error: result of request %hhu has %hu elements\n",
1316*4882a593Smuzhiyun res->result_ix, num_elements);
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun return -EIO;
1319*4882a593Smuzhiyun }
1320*4882a593Smuzhiyun
1321*4882a593Smuzhiyun if (data_size != sizeof(u64)) {
1322*4882a593Smuzhiyun pr_debug("Error: result of request %hhu has data of %hu bytes\n",
1323*4882a593Smuzhiyun res->result_ix, data_size);
1324*4882a593Smuzhiyun
1325*4882a593Smuzhiyun return -ENOTSUPP;
1326*4882a593Smuzhiyun }
1327*4882a593Smuzhiyun
1328*4882a593Smuzhiyun if (resb->interface_version == 1)
1329*4882a593Smuzhiyun data_offset = offsetof(struct hv_24x7_result_element_v1,
1330*4882a593Smuzhiyun element_data);
1331*4882a593Smuzhiyun else
1332*4882a593Smuzhiyun data_offset = offsetof(struct hv_24x7_result_element_v2,
1333*4882a593Smuzhiyun element_data);
1334*4882a593Smuzhiyun
1335*4882a593Smuzhiyun /* Go through the result elements in the result. */
1336*4882a593Smuzhiyun for (i = count = 0, element_data = res->elements + data_offset;
1337*4882a593Smuzhiyun i < num_elements;
1338*4882a593Smuzhiyun i++, element_data += data_size + data_offset)
1339*4882a593Smuzhiyun count += be64_to_cpu(*((u64 *) element_data));
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun *countp = count;
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun /* The next result is after the last result element. */
1344*4882a593Smuzhiyun if (next)
1345*4882a593Smuzhiyun *next = element_data - data_offset;
1346*4882a593Smuzhiyun
1347*4882a593Smuzhiyun return 0;
1348*4882a593Smuzhiyun }
1349*4882a593Smuzhiyun
single_24x7_request(struct perf_event * event,u64 * count)1350*4882a593Smuzhiyun static int single_24x7_request(struct perf_event *event, u64 *count)
1351*4882a593Smuzhiyun {
1352*4882a593Smuzhiyun int ret;
1353*4882a593Smuzhiyun struct hv_24x7_request_buffer *request_buffer;
1354*4882a593Smuzhiyun struct hv_24x7_data_result_buffer *result_buffer;
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(*request_buffer) > 4096);
1357*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(*result_buffer) > 4096);
1358*4882a593Smuzhiyun
1359*4882a593Smuzhiyun request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
1360*4882a593Smuzhiyun result_buffer = (void *)get_cpu_var(hv_24x7_resb);
1361*4882a593Smuzhiyun
1362*4882a593Smuzhiyun init_24x7_request(request_buffer, result_buffer);
1363*4882a593Smuzhiyun
1364*4882a593Smuzhiyun ret = add_event_to_24x7_request(event, request_buffer);
1365*4882a593Smuzhiyun if (ret)
1366*4882a593Smuzhiyun goto out;
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun ret = make_24x7_request(request_buffer, result_buffer);
1369*4882a593Smuzhiyun if (ret)
1370*4882a593Smuzhiyun goto out;
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun /* process result from hcall */
1373*4882a593Smuzhiyun ret = get_count_from_result(event, result_buffer,
1374*4882a593Smuzhiyun result_buffer->results, count, NULL);
1375*4882a593Smuzhiyun
1376*4882a593Smuzhiyun out:
1377*4882a593Smuzhiyun put_cpu_var(hv_24x7_reqb);
1378*4882a593Smuzhiyun put_cpu_var(hv_24x7_resb);
1379*4882a593Smuzhiyun return ret;
1380*4882a593Smuzhiyun }
1381*4882a593Smuzhiyun
1382*4882a593Smuzhiyun
h_24x7_event_init(struct perf_event * event)1383*4882a593Smuzhiyun static int h_24x7_event_init(struct perf_event *event)
1384*4882a593Smuzhiyun {
1385*4882a593Smuzhiyun struct hv_perf_caps caps;
1386*4882a593Smuzhiyun unsigned domain;
1387*4882a593Smuzhiyun unsigned long hret;
1388*4882a593Smuzhiyun u64 ct;
1389*4882a593Smuzhiyun
1390*4882a593Smuzhiyun /* Not our event */
1391*4882a593Smuzhiyun if (event->attr.type != event->pmu->type)
1392*4882a593Smuzhiyun return -ENOENT;
1393*4882a593Smuzhiyun
1394*4882a593Smuzhiyun /* Unused areas must be 0 */
1395*4882a593Smuzhiyun if (event_get_reserved1(event) ||
1396*4882a593Smuzhiyun event_get_reserved2(event) ||
1397*4882a593Smuzhiyun event_get_reserved3(event)) {
1398*4882a593Smuzhiyun pr_devel("reserved set when forbidden 0x%llx(0x%llx) 0x%llx(0x%llx) 0x%llx(0x%llx)\n",
1399*4882a593Smuzhiyun event->attr.config,
1400*4882a593Smuzhiyun event_get_reserved1(event),
1401*4882a593Smuzhiyun event->attr.config1,
1402*4882a593Smuzhiyun event_get_reserved2(event),
1403*4882a593Smuzhiyun event->attr.config2,
1404*4882a593Smuzhiyun event_get_reserved3(event));
1405*4882a593Smuzhiyun return -EINVAL;
1406*4882a593Smuzhiyun }
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun /* no branch sampling */
1409*4882a593Smuzhiyun if (has_branch_stack(event))
1410*4882a593Smuzhiyun return -EOPNOTSUPP;
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun /* offset must be 8 byte aligned */
1413*4882a593Smuzhiyun if (event_get_offset(event) % 8) {
1414*4882a593Smuzhiyun pr_devel("bad alignment\n");
1415*4882a593Smuzhiyun return -EINVAL;
1416*4882a593Smuzhiyun }
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun domain = event_get_domain(event);
1419*4882a593Smuzhiyun if (domain >= HV_PERF_DOMAIN_MAX) {
1420*4882a593Smuzhiyun pr_devel("invalid domain %d\n", domain);
1421*4882a593Smuzhiyun return -EINVAL;
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun hret = hv_perf_caps_get(&caps);
1425*4882a593Smuzhiyun if (hret) {
1426*4882a593Smuzhiyun pr_devel("could not get capabilities: rc=%ld\n", hret);
1427*4882a593Smuzhiyun return -EIO;
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun /* Physical domains & other lpars require extra capabilities */
1431*4882a593Smuzhiyun if (!caps.collect_privileged && (is_physical_domain(domain) ||
1432*4882a593Smuzhiyun (event_get_lpar(event) != event_get_lpar_max()))) {
1433*4882a593Smuzhiyun pr_devel("hv permissions disallow: is_physical_domain:%d, lpar=0x%llx\n",
1434*4882a593Smuzhiyun is_physical_domain(domain),
1435*4882a593Smuzhiyun event_get_lpar(event));
1436*4882a593Smuzhiyun return -EACCES;
1437*4882a593Smuzhiyun }
1438*4882a593Smuzhiyun
1439*4882a593Smuzhiyun /* Get the initial value of the counter for this event */
1440*4882a593Smuzhiyun if (single_24x7_request(event, &ct)) {
1441*4882a593Smuzhiyun pr_devel("test hcall failed\n");
1442*4882a593Smuzhiyun return -EIO;
1443*4882a593Smuzhiyun }
1444*4882a593Smuzhiyun (void)local64_xchg(&event->hw.prev_count, ct);
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun return 0;
1447*4882a593Smuzhiyun }
1448*4882a593Smuzhiyun
h_24x7_get_value(struct perf_event * event)1449*4882a593Smuzhiyun static u64 h_24x7_get_value(struct perf_event *event)
1450*4882a593Smuzhiyun {
1451*4882a593Smuzhiyun u64 ct;
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun if (single_24x7_request(event, &ct))
1454*4882a593Smuzhiyun /* We checked this in event init, shouldn't fail here... */
1455*4882a593Smuzhiyun return 0;
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun return ct;
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun
update_event_count(struct perf_event * event,u64 now)1460*4882a593Smuzhiyun static void update_event_count(struct perf_event *event, u64 now)
1461*4882a593Smuzhiyun {
1462*4882a593Smuzhiyun s64 prev;
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun prev = local64_xchg(&event->hw.prev_count, now);
1465*4882a593Smuzhiyun local64_add(now - prev, &event->count);
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun
h_24x7_event_read(struct perf_event * event)1468*4882a593Smuzhiyun static void h_24x7_event_read(struct perf_event *event)
1469*4882a593Smuzhiyun {
1470*4882a593Smuzhiyun u64 now;
1471*4882a593Smuzhiyun struct hv_24x7_request_buffer *request_buffer;
1472*4882a593Smuzhiyun struct hv_24x7_hw *h24x7hw;
1473*4882a593Smuzhiyun int txn_flags;
1474*4882a593Smuzhiyun
1475*4882a593Smuzhiyun txn_flags = __this_cpu_read(hv_24x7_txn_flags);
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun /*
1478*4882a593Smuzhiyun * If in a READ transaction, add this counter to the list of
1479*4882a593Smuzhiyun * counters to read during the next HCALL (i.e commit_txn()).
1480*4882a593Smuzhiyun * If not in a READ transaction, go ahead and make the HCALL
1481*4882a593Smuzhiyun * to read this counter by itself.
1482*4882a593Smuzhiyun */
1483*4882a593Smuzhiyun
1484*4882a593Smuzhiyun if (txn_flags & PERF_PMU_TXN_READ) {
1485*4882a593Smuzhiyun int i;
1486*4882a593Smuzhiyun int ret;
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun if (__this_cpu_read(hv_24x7_txn_err))
1489*4882a593Smuzhiyun return;
1490*4882a593Smuzhiyun
1491*4882a593Smuzhiyun request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun ret = add_event_to_24x7_request(event, request_buffer);
1494*4882a593Smuzhiyun if (ret) {
1495*4882a593Smuzhiyun __this_cpu_write(hv_24x7_txn_err, ret);
1496*4882a593Smuzhiyun } else {
1497*4882a593Smuzhiyun /*
1498*4882a593Smuzhiyun * Associate the event with the HCALL request index,
1499*4882a593Smuzhiyun * so ->commit_txn() can quickly find/update count.
1500*4882a593Smuzhiyun */
1501*4882a593Smuzhiyun i = request_buffer->num_requests - 1;
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun h24x7hw = &get_cpu_var(hv_24x7_hw);
1504*4882a593Smuzhiyun h24x7hw->events[i] = event;
1505*4882a593Smuzhiyun put_cpu_var(h24x7hw);
1506*4882a593Smuzhiyun }
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun put_cpu_var(hv_24x7_reqb);
1509*4882a593Smuzhiyun } else {
1510*4882a593Smuzhiyun now = h_24x7_get_value(event);
1511*4882a593Smuzhiyun update_event_count(event, now);
1512*4882a593Smuzhiyun }
1513*4882a593Smuzhiyun }
1514*4882a593Smuzhiyun
h_24x7_event_start(struct perf_event * event,int flags)1515*4882a593Smuzhiyun static void h_24x7_event_start(struct perf_event *event, int flags)
1516*4882a593Smuzhiyun {
1517*4882a593Smuzhiyun if (flags & PERF_EF_RELOAD)
1518*4882a593Smuzhiyun local64_set(&event->hw.prev_count, h_24x7_get_value(event));
1519*4882a593Smuzhiyun }
1520*4882a593Smuzhiyun
h_24x7_event_stop(struct perf_event * event,int flags)1521*4882a593Smuzhiyun static void h_24x7_event_stop(struct perf_event *event, int flags)
1522*4882a593Smuzhiyun {
1523*4882a593Smuzhiyun h_24x7_event_read(event);
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun
h_24x7_event_add(struct perf_event * event,int flags)1526*4882a593Smuzhiyun static int h_24x7_event_add(struct perf_event *event, int flags)
1527*4882a593Smuzhiyun {
1528*4882a593Smuzhiyun if (flags & PERF_EF_START)
1529*4882a593Smuzhiyun h_24x7_event_start(event, flags);
1530*4882a593Smuzhiyun
1531*4882a593Smuzhiyun return 0;
1532*4882a593Smuzhiyun }
1533*4882a593Smuzhiyun
1534*4882a593Smuzhiyun /*
1535*4882a593Smuzhiyun * 24x7 counters only support READ transactions. They are
1536*4882a593Smuzhiyun * always counting and dont need/support ADD transactions.
1537*4882a593Smuzhiyun * Cache the flags, but otherwise ignore transactions that
1538*4882a593Smuzhiyun * are not PERF_PMU_TXN_READ.
1539*4882a593Smuzhiyun */
h_24x7_event_start_txn(struct pmu * pmu,unsigned int flags)1540*4882a593Smuzhiyun static void h_24x7_event_start_txn(struct pmu *pmu, unsigned int flags)
1541*4882a593Smuzhiyun {
1542*4882a593Smuzhiyun struct hv_24x7_request_buffer *request_buffer;
1543*4882a593Smuzhiyun struct hv_24x7_data_result_buffer *result_buffer;
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun /* We should not be called if we are already in a txn */
1546*4882a593Smuzhiyun WARN_ON_ONCE(__this_cpu_read(hv_24x7_txn_flags));
1547*4882a593Smuzhiyun
1548*4882a593Smuzhiyun __this_cpu_write(hv_24x7_txn_flags, flags);
1549*4882a593Smuzhiyun if (flags & ~PERF_PMU_TXN_READ)
1550*4882a593Smuzhiyun return;
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
1553*4882a593Smuzhiyun result_buffer = (void *)get_cpu_var(hv_24x7_resb);
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun init_24x7_request(request_buffer, result_buffer);
1556*4882a593Smuzhiyun
1557*4882a593Smuzhiyun put_cpu_var(hv_24x7_resb);
1558*4882a593Smuzhiyun put_cpu_var(hv_24x7_reqb);
1559*4882a593Smuzhiyun }
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun /*
1562*4882a593Smuzhiyun * Clean up transaction state.
1563*4882a593Smuzhiyun *
1564*4882a593Smuzhiyun * NOTE: Ignore state of request and result buffers for now.
1565*4882a593Smuzhiyun * We will initialize them during the next read/txn.
1566*4882a593Smuzhiyun */
reset_txn(void)1567*4882a593Smuzhiyun static void reset_txn(void)
1568*4882a593Smuzhiyun {
1569*4882a593Smuzhiyun __this_cpu_write(hv_24x7_txn_flags, 0);
1570*4882a593Smuzhiyun __this_cpu_write(hv_24x7_txn_err, 0);
1571*4882a593Smuzhiyun }
1572*4882a593Smuzhiyun
1573*4882a593Smuzhiyun /*
1574*4882a593Smuzhiyun * 24x7 counters only support READ transactions. They are always counting
1575*4882a593Smuzhiyun * and dont need/support ADD transactions. Clear ->txn_flags but otherwise
1576*4882a593Smuzhiyun * ignore transactions that are not of type PERF_PMU_TXN_READ.
1577*4882a593Smuzhiyun *
1578*4882a593Smuzhiyun * For READ transactions, submit all pending 24x7 requests (i.e requests
1579*4882a593Smuzhiyun * that were queued by h_24x7_event_read()), to the hypervisor and update
1580*4882a593Smuzhiyun * the event counts.
1581*4882a593Smuzhiyun */
h_24x7_event_commit_txn(struct pmu * pmu)1582*4882a593Smuzhiyun static int h_24x7_event_commit_txn(struct pmu *pmu)
1583*4882a593Smuzhiyun {
1584*4882a593Smuzhiyun struct hv_24x7_request_buffer *request_buffer;
1585*4882a593Smuzhiyun struct hv_24x7_data_result_buffer *result_buffer;
1586*4882a593Smuzhiyun struct hv_24x7_result *res, *next_res;
1587*4882a593Smuzhiyun u64 count;
1588*4882a593Smuzhiyun int i, ret, txn_flags;
1589*4882a593Smuzhiyun struct hv_24x7_hw *h24x7hw;
1590*4882a593Smuzhiyun
1591*4882a593Smuzhiyun txn_flags = __this_cpu_read(hv_24x7_txn_flags);
1592*4882a593Smuzhiyun WARN_ON_ONCE(!txn_flags);
1593*4882a593Smuzhiyun
1594*4882a593Smuzhiyun ret = 0;
1595*4882a593Smuzhiyun if (txn_flags & ~PERF_PMU_TXN_READ)
1596*4882a593Smuzhiyun goto out;
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun ret = __this_cpu_read(hv_24x7_txn_err);
1599*4882a593Smuzhiyun if (ret)
1600*4882a593Smuzhiyun goto out;
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
1603*4882a593Smuzhiyun result_buffer = (void *)get_cpu_var(hv_24x7_resb);
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun ret = make_24x7_request(request_buffer, result_buffer);
1606*4882a593Smuzhiyun if (ret)
1607*4882a593Smuzhiyun goto put_reqb;
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun h24x7hw = &get_cpu_var(hv_24x7_hw);
1610*4882a593Smuzhiyun
1611*4882a593Smuzhiyun /* Go through results in the result buffer to update event counts. */
1612*4882a593Smuzhiyun for (i = 0, res = result_buffer->results;
1613*4882a593Smuzhiyun i < result_buffer->num_results; i++, res = next_res) {
1614*4882a593Smuzhiyun struct perf_event *event = h24x7hw->events[res->result_ix];
1615*4882a593Smuzhiyun
1616*4882a593Smuzhiyun ret = get_count_from_result(event, result_buffer, res, &count,
1617*4882a593Smuzhiyun &next_res);
1618*4882a593Smuzhiyun if (ret)
1619*4882a593Smuzhiyun break;
1620*4882a593Smuzhiyun
1621*4882a593Smuzhiyun update_event_count(event, count);
1622*4882a593Smuzhiyun }
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun put_cpu_var(hv_24x7_hw);
1625*4882a593Smuzhiyun
1626*4882a593Smuzhiyun put_reqb:
1627*4882a593Smuzhiyun put_cpu_var(hv_24x7_resb);
1628*4882a593Smuzhiyun put_cpu_var(hv_24x7_reqb);
1629*4882a593Smuzhiyun out:
1630*4882a593Smuzhiyun reset_txn();
1631*4882a593Smuzhiyun return ret;
1632*4882a593Smuzhiyun }
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun /*
1635*4882a593Smuzhiyun * 24x7 counters only support READ transactions. They are always counting
1636*4882a593Smuzhiyun * and dont need/support ADD transactions. However, regardless of type
1637*4882a593Smuzhiyun * of transaction, all we need to do is cleanup, so we don't have to check
1638*4882a593Smuzhiyun * the type of transaction.
1639*4882a593Smuzhiyun */
h_24x7_event_cancel_txn(struct pmu * pmu)1640*4882a593Smuzhiyun static void h_24x7_event_cancel_txn(struct pmu *pmu)
1641*4882a593Smuzhiyun {
1642*4882a593Smuzhiyun WARN_ON_ONCE(!__this_cpu_read(hv_24x7_txn_flags));
1643*4882a593Smuzhiyun reset_txn();
1644*4882a593Smuzhiyun }
1645*4882a593Smuzhiyun
1646*4882a593Smuzhiyun static struct pmu h_24x7_pmu = {
1647*4882a593Smuzhiyun .task_ctx_nr = perf_invalid_context,
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun .name = "hv_24x7",
1650*4882a593Smuzhiyun .attr_groups = attr_groups,
1651*4882a593Smuzhiyun .event_init = h_24x7_event_init,
1652*4882a593Smuzhiyun .add = h_24x7_event_add,
1653*4882a593Smuzhiyun .del = h_24x7_event_stop,
1654*4882a593Smuzhiyun .start = h_24x7_event_start,
1655*4882a593Smuzhiyun .stop = h_24x7_event_stop,
1656*4882a593Smuzhiyun .read = h_24x7_event_read,
1657*4882a593Smuzhiyun .start_txn = h_24x7_event_start_txn,
1658*4882a593Smuzhiyun .commit_txn = h_24x7_event_commit_txn,
1659*4882a593Smuzhiyun .cancel_txn = h_24x7_event_cancel_txn,
1660*4882a593Smuzhiyun .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
1661*4882a593Smuzhiyun };
1662*4882a593Smuzhiyun
ppc_hv_24x7_cpu_online(unsigned int cpu)1663*4882a593Smuzhiyun static int ppc_hv_24x7_cpu_online(unsigned int cpu)
1664*4882a593Smuzhiyun {
1665*4882a593Smuzhiyun if (cpumask_empty(&hv_24x7_cpumask))
1666*4882a593Smuzhiyun cpumask_set_cpu(cpu, &hv_24x7_cpumask);
1667*4882a593Smuzhiyun
1668*4882a593Smuzhiyun return 0;
1669*4882a593Smuzhiyun }
1670*4882a593Smuzhiyun
ppc_hv_24x7_cpu_offline(unsigned int cpu)1671*4882a593Smuzhiyun static int ppc_hv_24x7_cpu_offline(unsigned int cpu)
1672*4882a593Smuzhiyun {
1673*4882a593Smuzhiyun int target;
1674*4882a593Smuzhiyun
1675*4882a593Smuzhiyun /* Check if exiting cpu is used for collecting 24x7 events */
1676*4882a593Smuzhiyun if (!cpumask_test_and_clear_cpu(cpu, &hv_24x7_cpumask))
1677*4882a593Smuzhiyun return 0;
1678*4882a593Smuzhiyun
1679*4882a593Smuzhiyun /* Find a new cpu to collect 24x7 events */
1680*4882a593Smuzhiyun target = cpumask_last(cpu_active_mask);
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun if (target < 0 || target >= nr_cpu_ids) {
1683*4882a593Smuzhiyun pr_err("hv_24x7: CPU hotplug init failed\n");
1684*4882a593Smuzhiyun return -1;
1685*4882a593Smuzhiyun }
1686*4882a593Smuzhiyun
1687*4882a593Smuzhiyun /* Migrate 24x7 events to the new target */
1688*4882a593Smuzhiyun cpumask_set_cpu(target, &hv_24x7_cpumask);
1689*4882a593Smuzhiyun perf_pmu_migrate_context(&h_24x7_pmu, cpu, target);
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun return 0;
1692*4882a593Smuzhiyun }
1693*4882a593Smuzhiyun
hv_24x7_cpu_hotplug_init(void)1694*4882a593Smuzhiyun static int hv_24x7_cpu_hotplug_init(void)
1695*4882a593Smuzhiyun {
1696*4882a593Smuzhiyun return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_HV_24x7_ONLINE,
1697*4882a593Smuzhiyun "perf/powerpc/hv_24x7:online",
1698*4882a593Smuzhiyun ppc_hv_24x7_cpu_online,
1699*4882a593Smuzhiyun ppc_hv_24x7_cpu_offline);
1700*4882a593Smuzhiyun }
1701*4882a593Smuzhiyun
hv_24x7_init(void)1702*4882a593Smuzhiyun static int hv_24x7_init(void)
1703*4882a593Smuzhiyun {
1704*4882a593Smuzhiyun int r;
1705*4882a593Smuzhiyun unsigned long hret;
1706*4882a593Smuzhiyun struct hv_perf_caps caps;
1707*4882a593Smuzhiyun
1708*4882a593Smuzhiyun if (!firmware_has_feature(FW_FEATURE_LPAR)) {
1709*4882a593Smuzhiyun pr_debug("not a virtualized system, not enabling\n");
1710*4882a593Smuzhiyun return -ENODEV;
1711*4882a593Smuzhiyun } else if (!cur_cpu_spec->oprofile_cpu_type)
1712*4882a593Smuzhiyun return -ENODEV;
1713*4882a593Smuzhiyun
1714*4882a593Smuzhiyun /* POWER8 only supports v1, while POWER9 only supports v2. */
1715*4882a593Smuzhiyun if (!strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power8"))
1716*4882a593Smuzhiyun interface_version = 1;
1717*4882a593Smuzhiyun else {
1718*4882a593Smuzhiyun interface_version = 2;
1719*4882a593Smuzhiyun
1720*4882a593Smuzhiyun /* SMT8 in POWER9 needs to aggregate result elements. */
1721*4882a593Smuzhiyun if (threads_per_core == 8)
1722*4882a593Smuzhiyun aggregate_result_elements = true;
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun
1725*4882a593Smuzhiyun hret = hv_perf_caps_get(&caps);
1726*4882a593Smuzhiyun if (hret) {
1727*4882a593Smuzhiyun pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
1728*4882a593Smuzhiyun hret);
1729*4882a593Smuzhiyun return -ENODEV;
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun
1732*4882a593Smuzhiyun hv_page_cache = kmem_cache_create("hv-page-4096", 4096, 4096, 0, NULL);
1733*4882a593Smuzhiyun if (!hv_page_cache)
1734*4882a593Smuzhiyun return -ENOMEM;
1735*4882a593Smuzhiyun
1736*4882a593Smuzhiyun /* sampling not supported */
1737*4882a593Smuzhiyun h_24x7_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
1738*4882a593Smuzhiyun
1739*4882a593Smuzhiyun r = create_events_from_catalog(&event_group.attrs,
1740*4882a593Smuzhiyun &event_desc_group.attrs,
1741*4882a593Smuzhiyun &event_long_desc_group.attrs);
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun if (r)
1744*4882a593Smuzhiyun return r;
1745*4882a593Smuzhiyun
1746*4882a593Smuzhiyun /* init cpuhotplug */
1747*4882a593Smuzhiyun r = hv_24x7_cpu_hotplug_init();
1748*4882a593Smuzhiyun if (r)
1749*4882a593Smuzhiyun return r;
1750*4882a593Smuzhiyun
1751*4882a593Smuzhiyun r = perf_pmu_register(&h_24x7_pmu, h_24x7_pmu.name, -1);
1752*4882a593Smuzhiyun if (r)
1753*4882a593Smuzhiyun return r;
1754*4882a593Smuzhiyun
1755*4882a593Smuzhiyun read_24x7_sys_info();
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun return 0;
1758*4882a593Smuzhiyun }
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun device_initcall(hv_24x7_init);
1761