1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hypervisor supplied "gpci" ("get performance counter info") performance
7 * Copyright 2014 IBM Corporation.
10 #define pr_fmt(fmt) "hv-gpci: " fmt
12 #include <linux/init.h>
13 #include <linux/perf_event.h>
14 #include <asm/firmware.h>
15 #include <asm/hvcall.h>
19 #include "hv-common.h"
23 * perf stat -e 'hv_gpci/counter_info_version=3,offset=0,length=8,
24 * secondary_index=0,starting_index=0xffffffff,request=0x10/' ...
28 EVENT_DEFINE_RANGE_FORMAT(request, config, 0, 31);
31 * Note that starting_index, phys_processor_idx, sibling_part_id,
32 * hw_chip_id, partition_id all refer to the same bit range. They
33 * are basically aliases for the starting_index. The specific alias
34 * used depends on the event. See REQUEST_IDX_KIND in hv-gpci-requests.h
36 EVENT_DEFINE_RANGE_FORMAT(starting_index, config, 32, 63);
37 EVENT_DEFINE_RANGE_FORMAT_LITE(phys_processor_idx, config, 32, 63);
38 EVENT_DEFINE_RANGE_FORMAT_LITE(sibling_part_id, config, 32, 63);
39 EVENT_DEFINE_RANGE_FORMAT_LITE(hw_chip_id, config, 32, 63);
40 EVENT_DEFINE_RANGE_FORMAT_LITE(partition_id, config, 32, 63);
43 EVENT_DEFINE_RANGE_FORMAT(secondary_index, config1, 0, 15);
45 EVENT_DEFINE_RANGE_FORMAT(counter_info_version, config1, 16, 23);
46 /* u8, bytes of data (1-8) */
47 EVENT_DEFINE_RANGE_FORMAT(length, config1, 24, 31);
48 /* u32, byte offset */
49 EVENT_DEFINE_RANGE_FORMAT(offset, config1, 32, 63);
51 static cpumask_t hv_gpci_cpumask;
53 static struct attribute *format_attrs[] = {
54 &format_attr_request.attr,
55 &format_attr_starting_index.attr,
56 &format_attr_phys_processor_idx.attr,
57 &format_attr_sibling_part_id.attr,
58 &format_attr_hw_chip_id.attr,
59 &format_attr_partition_id.attr,
60 &format_attr_secondary_index.attr,
61 &format_attr_counter_info_version.attr,
63 &format_attr_offset.attr,
64 &format_attr_length.attr,
68 static const struct attribute_group format_group = {
70 .attrs = format_attrs,
73 static struct attribute_group event_group = {
75 /* .attrs is set in init */
78 #define HV_CAPS_ATTR(_name, _format) \
79 static ssize_t _name##_show(struct device *dev, \
80 struct device_attribute *attr, \
83 struct hv_perf_caps caps; \
84 unsigned long hret = hv_perf_caps_get(&caps); \
88 return sprintf(page, _format, caps._name); \
90 static struct device_attribute hv_caps_attr_##_name = __ATTR_RO(_name)
92 static ssize_t kernel_version_show(struct device *dev,
93 struct device_attribute *attr,
96 return sprintf(page, "0x%x\n", COUNTER_INFO_VERSION_CURRENT);
99 static ssize_t cpumask_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
102 return cpumap_print_to_pagebuf(true, buf, &hv_gpci_cpumask);
105 /* Interface attribute array index to store system information */
106 #define INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR 6
107 #define INTERFACE_PROCESSOR_CONFIG_ATTR 7
108 #define INTERFACE_AFFINITY_DOMAIN_VIA_VP_ATTR 8
109 #define INTERFACE_AFFINITY_DOMAIN_VIA_DOM_ATTR 9
110 #define INTERFACE_AFFINITY_DOMAIN_VIA_PAR_ATTR 10
111 #define INTERFACE_NULL_ATTR 11
113 /* Counter request value to retrieve system information */
115 PROCESSOR_BUS_TOPOLOGY,
117 AFFINITY_DOMAIN_VIA_VP, /* affinity domain via virtual processor */
118 AFFINITY_DOMAIN_VIA_DOM, /* affinity domain via domain */
119 AFFINITY_DOMAIN_VIA_PAR, /* affinity domain via partition */
122 static int sysinfo_counter_request[] = {
123 [PROCESSOR_BUS_TOPOLOGY] = 0xD0,
124 [PROCESSOR_CONFIG] = 0x90,
125 [AFFINITY_DOMAIN_VIA_VP] = 0xA0,
126 [AFFINITY_DOMAIN_VIA_DOM] = 0xB0,
127 [AFFINITY_DOMAIN_VIA_PAR] = 0xB1,
130 static DEFINE_PER_CPU(char, hv_gpci_reqb[HGPCI_REQ_BUFFER_SIZE]) __aligned(sizeof(uint64_t));
132 static unsigned long systeminfo_gpci_request(u32 req, u32 starting_index,
133 u16 secondary_index, char *buf,
134 size_t *n, struct hv_gpci_request_buffer *arg)
139 arg->params.counter_request = cpu_to_be32(req);
140 arg->params.starting_index = cpu_to_be32(starting_index);
141 arg->params.secondary_index = cpu_to_be16(secondary_index);
143 ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
144 virt_to_phys(arg), HGPCI_REQ_BUFFER_SIZE);
147 * ret value as 'H_PARAMETER' corresponds to 'GEN_BUF_TOO_SMALL',
148 * which means that the current buffer size cannot accommodate
149 * all the information and a partial buffer returned.
150 * hcall fails incase of ret value other than H_SUCCESS or H_PARAMETER.
152 * ret value as H_AUTHORITY implies that partition is not permitted to retrieve
153 * performance information, and required to set
154 * "Enable Performance Information Collection" option.
156 if (ret == H_AUTHORITY)
160 * hcall can fail with other possible ret value like H_PRIVILEGE/H_HARDWARE
161 * because of invalid buffer-length/address or due to some hardware
164 if (ret && (ret != H_PARAMETER))
168 * hcall H_GET_PERF_COUNTER_INFO populates the 'returned_values'
169 * to show the total number of counter_value array elements
170 * returned via hcall.
171 * hcall also populates 'cv_element_size' corresponds to individual
172 * counter_value array element size. Below loop go through all
173 * counter_value array elements as per their size and add it to
176 for (i = 0; i < be16_to_cpu(arg->params.returned_values); i++) {
177 j = i * be16_to_cpu(arg->params.cv_element_size);
179 for (; j < (i + 1) * be16_to_cpu(arg->params.cv_element_size); j++)
180 *n += sprintf(buf + *n, "%02x", (u8)arg->bytes[j]);
181 *n += sprintf(buf + *n, "\n");
184 if (*n >= PAGE_SIZE) {
185 pr_info("System information exceeds PAGE_SIZE\n");
192 static ssize_t processor_bus_topology_show(struct device *dev, struct device_attribute *attr,
195 struct hv_gpci_request_buffer *arg;
199 arg = (void *)get_cpu_var(hv_gpci_reqb);
200 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
203 * Pass the counter request value 0xD0 corresponds to request
204 * type 'Processor_bus_topology', to retrieve
205 * the system topology information.
206 * starting_index value implies the starting hardware
209 ret = systeminfo_gpci_request(sysinfo_counter_request[PROCESSOR_BUS_TOPOLOGY],
215 if (ret != H_PARAMETER)
219 * ret value as 'H_PARAMETER' corresponds to 'GEN_BUF_TOO_SMALL', which
220 * implies that buffer can't accommodate all information, and a partial buffer
221 * returned. To handle that, we need to make subsequent requests
222 * with next starting index to retrieve additional (missing) data.
223 * Below loop do subsequent hcalls with next starting index and add it
224 * to buffer util we get all the information.
226 while (ret == H_PARAMETER) {
227 int returned_values = be16_to_cpu(arg->params.returned_values);
228 int elementsize = be16_to_cpu(arg->params.cv_element_size);
229 int last_element = (returned_values - 1) * elementsize;
232 * Since the starting index value is part of counter_value
233 * buffer elements, use the starting index value in the last
234 * element and add 1 to make subsequent hcalls.
236 u32 starting_index = arg->bytes[last_element + 3] +
237 (arg->bytes[last_element + 2] << 8) +
238 (arg->bytes[last_element + 1] << 16) +
239 (arg->bytes[last_element] << 24) + 1;
241 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
243 ret = systeminfo_gpci_request(sysinfo_counter_request[PROCESSOR_BUS_TOPOLOGY],
244 starting_index, 0, buf, &n, arg);
249 if (ret != H_PARAMETER)
256 put_cpu_var(hv_gpci_reqb);
260 static ssize_t processor_config_show(struct device *dev, struct device_attribute *attr,
263 struct hv_gpci_request_buffer *arg;
267 arg = (void *)get_cpu_var(hv_gpci_reqb);
268 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
271 * Pass the counter request value 0x90 corresponds to request
272 * type 'Processor_config', to retrieve
273 * the system processor information.
274 * starting_index value implies the starting hardware
277 ret = systeminfo_gpci_request(sysinfo_counter_request[PROCESSOR_CONFIG],
283 if (ret != H_PARAMETER)
287 * ret value as 'H_PARAMETER' corresponds to 'GEN_BUF_TOO_SMALL', which
288 * implies that buffer can't accommodate all information, and a partial buffer
289 * returned. To handle that, we need to take subsequent requests
290 * with next starting index to retrieve additional (missing) data.
291 * Below loop do subsequent hcalls with next starting index and add it
292 * to buffer util we get all the information.
294 while (ret == H_PARAMETER) {
295 int returned_values = be16_to_cpu(arg->params.returned_values);
296 int elementsize = be16_to_cpu(arg->params.cv_element_size);
297 int last_element = (returned_values - 1) * elementsize;
300 * Since the starting index is part of counter_value
301 * buffer elements, use the starting index value in the last
302 * element and add 1 to subsequent hcalls.
304 u32 starting_index = arg->bytes[last_element + 3] +
305 (arg->bytes[last_element + 2] << 8) +
306 (arg->bytes[last_element + 1] << 16) +
307 (arg->bytes[last_element] << 24) + 1;
309 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
311 ret = systeminfo_gpci_request(sysinfo_counter_request[PROCESSOR_CONFIG],
312 starting_index, 0, buf, &n, arg);
317 if (ret != H_PARAMETER)
324 put_cpu_var(hv_gpci_reqb);
328 static ssize_t affinity_domain_via_virtual_processor_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
331 struct hv_gpci_request_buffer *arg;
335 arg = (void *)get_cpu_var(hv_gpci_reqb);
336 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
339 * Pass the counter request 0xA0 corresponds to request
340 * type 'Affinity_domain_information_by_virutal_processor',
341 * to retrieve the system affinity domain information.
342 * starting_index value refers to the starting hardware
345 ret = systeminfo_gpci_request(sysinfo_counter_request[AFFINITY_DOMAIN_VIA_VP],
351 if (ret != H_PARAMETER)
355 * ret value as 'H_PARAMETER' corresponds to 'GEN_BUF_TOO_SMALL', which
356 * implies that buffer can't accommodate all information, and a partial buffer
357 * returned. To handle that, we need to take subsequent requests
358 * with next secondary index to retrieve additional (missing) data.
359 * Below loop do subsequent hcalls with next secondary index and add it
360 * to buffer util we get all the information.
362 while (ret == H_PARAMETER) {
363 int returned_values = be16_to_cpu(arg->params.returned_values);
364 int elementsize = be16_to_cpu(arg->params.cv_element_size);
365 int last_element = (returned_values - 1) * elementsize;
368 * Since the starting index and secondary index type is part of the
369 * counter_value buffer elements, use the starting index value in the
370 * last array element as subsequent starting index, and use secondary index
371 * value in the last array element plus 1 as subsequent secondary index.
372 * For counter request '0xA0', starting index points to partition id
373 * and secondary index points to corresponding virtual processor index.
375 u32 starting_index = arg->bytes[last_element + 1] + (arg->bytes[last_element] << 8);
376 u16 secondary_index = arg->bytes[last_element + 3] +
377 (arg->bytes[last_element + 2] << 8) + 1;
379 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
381 ret = systeminfo_gpci_request(sysinfo_counter_request[AFFINITY_DOMAIN_VIA_VP],
382 starting_index, secondary_index, buf, &n, arg);
387 if (ret != H_PARAMETER)
394 put_cpu_var(hv_gpci_reqb);
398 static ssize_t affinity_domain_via_domain_show(struct device *dev, struct device_attribute *attr,
401 struct hv_gpci_request_buffer *arg;
405 arg = (void *)get_cpu_var(hv_gpci_reqb);
406 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
409 * Pass the counter request 0xB0 corresponds to request
410 * type 'Affinity_domain_information_by_domain',
411 * to retrieve the system affinity domain information.
412 * starting_index value refers to the starting hardware
415 ret = systeminfo_gpci_request(sysinfo_counter_request[AFFINITY_DOMAIN_VIA_DOM],
421 if (ret != H_PARAMETER)
425 * ret value as 'H_PARAMETER' corresponds to 'GEN_BUF_TOO_SMALL', which
426 * implies that buffer can't accommodate all information, and a partial buffer
427 * returned. To handle that, we need to take subsequent requests
428 * with next starting index to retrieve additional (missing) data.
429 * Below loop do subsequent hcalls with next starting index and add it
430 * to buffer util we get all the information.
432 while (ret == H_PARAMETER) {
433 int returned_values = be16_to_cpu(arg->params.returned_values);
434 int elementsize = be16_to_cpu(arg->params.cv_element_size);
435 int last_element = (returned_values - 1) * elementsize;
438 * Since the starting index value is part of counter_value
439 * buffer elements, use the starting index value in the last
440 * element and add 1 to make subsequent hcalls.
442 u32 starting_index = arg->bytes[last_element + 1] +
443 (arg->bytes[last_element] << 8) + 1;
445 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
447 ret = systeminfo_gpci_request(sysinfo_counter_request[AFFINITY_DOMAIN_VIA_DOM],
448 starting_index, 0, buf, &n, arg);
453 if (ret != H_PARAMETER)
460 put_cpu_var(hv_gpci_reqb);
464 static void affinity_domain_via_partition_result_parse(int returned_values,
465 int element_size, char *buf, size_t *last_element,
466 size_t *n, struct hv_gpci_request_buffer *arg)
470 uint16_t total_affinity_domain_ele, size_of_each_affinity_domain_ele;
473 * hcall H_GET_PERF_COUNTER_INFO populates the 'returned_values'
474 * to show the total number of counter_value array elements
475 * returned via hcall.
476 * Unlike other request types, the data structure returned by this
477 * request is variable-size. For this counter request type,
478 * hcall populates 'cv_element_size' corresponds to minimum size of
479 * the structure returned i.e; the size of the structure with no domain
480 * information. Below loop go through all counter_value array
481 * to determine the number and size of each domain array element and
482 * add it to the output buffer.
484 while (i < returned_values) {
486 for (; k < j + element_size; k++)
487 *n += sprintf(buf + *n, "%02x", (u8)arg->bytes[k]);
488 *n += sprintf(buf + *n, "\n");
490 total_affinity_domain_ele = (u8)arg->bytes[k - 2] << 8 | (u8)arg->bytes[k - 3];
491 size_of_each_affinity_domain_ele = (u8)arg->bytes[k] << 8 | (u8)arg->bytes[k - 1];
493 for (l = 0; l < total_affinity_domain_ele; l++) {
494 for (m = 0; m < size_of_each_affinity_domain_ele; m++) {
495 *n += sprintf(buf + *n, "%02x", (u8)arg->bytes[k]);
498 *n += sprintf(buf + *n, "\n");
501 *n += sprintf(buf + *n, "\n");
509 static ssize_t affinity_domain_via_partition_show(struct device *dev, struct device_attribute *attr,
512 struct hv_gpci_request_buffer *arg;
515 size_t last_element = 0;
518 arg = (void *)get_cpu_var(hv_gpci_reqb);
519 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
522 * Pass the counter request value 0xB1 corresponds to counter request
523 * type 'Affinity_domain_information_by_partition',
524 * to retrieve the system affinity domain by partition information.
525 * starting_index value refers to the starting hardware
528 arg->params.counter_request = cpu_to_be32(sysinfo_counter_request[AFFINITY_DOMAIN_VIA_PAR]);
529 arg->params.starting_index = cpu_to_be32(0);
531 ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
532 virt_to_phys(arg), HGPCI_REQ_BUFFER_SIZE);
537 if (ret && (ret != H_PARAMETER))
541 * ret value as 'H_PARAMETER' implies that the current buffer size
542 * can't accommodate all the information, and a partial buffer
543 * returned. To handle that, we need to make subsequent requests
544 * with next starting index to retrieve additional (missing) data.
545 * Below loop do subsequent hcalls with next starting index and add it
546 * to buffer util we get all the information.
548 while (ret == H_PARAMETER) {
549 affinity_domain_via_partition_result_parse(
550 be16_to_cpu(arg->params.returned_values) - 1,
551 be16_to_cpu(arg->params.cv_element_size), buf,
552 &last_element, &n, arg);
554 if (n >= PAGE_SIZE) {
555 put_cpu_var(hv_gpci_reqb);
556 pr_debug("System information exceeds PAGE_SIZE\n");
561 * Since the starting index value is part of counter_value
562 * buffer elements, use the starting_index value in the last
563 * element and add 1 to make subsequent hcalls.
565 starting_index = (u8)arg->bytes[last_element] << 8 |
566 (u8)arg->bytes[last_element + 1];
568 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
569 arg->params.counter_request = cpu_to_be32(
570 sysinfo_counter_request[AFFINITY_DOMAIN_VIA_PAR]);
571 arg->params.starting_index = cpu_to_be32(starting_index);
573 ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
574 virt_to_phys(arg), HGPCI_REQ_BUFFER_SIZE);
576 if (ret && (ret != H_PARAMETER))
581 affinity_domain_via_partition_result_parse(
582 be16_to_cpu(arg->params.returned_values),
583 be16_to_cpu(arg->params.cv_element_size),
584 buf, &last_element, &n, arg);
586 put_cpu_var(hv_gpci_reqb);
590 put_cpu_var(hv_gpci_reqb);
593 * ret value as 'H_PARAMETER' corresponds to 'GEN_BUF_TOO_SMALL',
594 * which means that the current buffer size cannot accommodate
595 * all the information and a partial buffer returned.
596 * hcall fails incase of ret value other than H_SUCCESS or H_PARAMETER.
598 * ret value as H_AUTHORITY implies that partition is not permitted to retrieve
599 * performance information, and required to set
600 * "Enable Performance Information Collection" option.
602 if (ret == H_AUTHORITY)
606 * hcall can fail with other possible ret value like H_PRIVILEGE/H_HARDWARE
607 * because of invalid buffer-length/address or due to some hardware
613 static DEVICE_ATTR_RO(kernel_version);
614 static DEVICE_ATTR_RO(cpumask);
616 HV_CAPS_ATTR(version, "0x%x\n");
617 HV_CAPS_ATTR(ga, "%d\n");
618 HV_CAPS_ATTR(expanded, "%d\n");
619 HV_CAPS_ATTR(lab, "%d\n");
620 HV_CAPS_ATTR(collect_privileged, "%d\n");
622 static struct attribute *interface_attrs[] = {
623 &dev_attr_kernel_version.attr,
624 &hv_caps_attr_version.attr,
625 &hv_caps_attr_ga.attr,
626 &hv_caps_attr_expanded.attr,
627 &hv_caps_attr_lab.attr,
628 &hv_caps_attr_collect_privileged.attr,
630 * This NULL is a placeholder for the processor_bus_topology
631 * attribute, set in init function if applicable.
635 * This NULL is a placeholder for the processor_config
636 * attribute, set in init function if applicable.
640 * This NULL is a placeholder for the affinity_domain_via_virtual_processor
641 * attribute, set in init function if applicable.
645 * This NULL is a placeholder for the affinity_domain_via_domain
646 * attribute, set in init function if applicable.
650 * This NULL is a placeholder for the affinity_domain_via_partition
651 * attribute, set in init function if applicable.
657 static struct attribute *cpumask_attrs[] = {
658 &dev_attr_cpumask.attr,
662 static const struct attribute_group cpumask_attr_group = {
663 .attrs = cpumask_attrs,
666 static const struct attribute_group interface_group = {
668 .attrs = interface_attrs,
671 static const struct attribute_group *attr_groups[] = {
679 static unsigned long single_gpci_request(u32 req, u32 starting_index,
680 u16 secondary_index, u8 version_in, u32 offset, u8 length,
686 struct hv_gpci_request_buffer *arg;
688 arg = (void *)get_cpu_var(hv_gpci_reqb);
689 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
691 arg->params.counter_request = cpu_to_be32(req);
692 arg->params.starting_index = cpu_to_be32(starting_index);
693 arg->params.secondary_index = cpu_to_be16(secondary_index);
694 arg->params.counter_info_version_in = version_in;
696 ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
697 virt_to_phys(arg), HGPCI_REQ_BUFFER_SIZE);
700 * ret value as 'H_PARAMETER' with detail_rc as 'GEN_BUF_TOO_SMALL',
701 * specifies that the current buffer size cannot accommodate
702 * all the information and a partial buffer returned.
703 * Since in this function we are only accessing data for a given starting index,
704 * we don't need to accommodate whole data and can get required count by
705 * accessing first entry data.
706 * Hence hcall fails only incase the ret value is other than H_SUCCESS or
707 * H_PARAMETER with detail_rc value as GEN_BUF_TOO_SMALL(0x1B).
709 if (ret == H_PARAMETER && be32_to_cpu(arg->params.detail_rc) == 0x1B)
713 pr_devel("hcall failed: 0x%lx\n", ret);
718 * we verify offset and length are within the zeroed buffer at event
722 for (i = offset; i < offset + length; i++)
723 count |= (u64)(arg->bytes[i]) << ((length - 1 - (i - offset)) * 8);
727 put_cpu_var(hv_gpci_reqb);
731 static u64 h_gpci_get_value(struct perf_event *event)
734 unsigned long ret = single_gpci_request(event_get_request(event),
735 event_get_starting_index(event),
736 event_get_secondary_index(event),
737 event_get_counter_info_version(event),
738 event_get_offset(event),
739 event_get_length(event),
746 static void h_gpci_event_update(struct perf_event *event)
749 u64 now = h_gpci_get_value(event);
750 prev = local64_xchg(&event->hw.prev_count, now);
751 local64_add(now - prev, &event->count);
754 static void h_gpci_event_start(struct perf_event *event, int flags)
756 local64_set(&event->hw.prev_count, h_gpci_get_value(event));
759 static void h_gpci_event_stop(struct perf_event *event, int flags)
761 h_gpci_event_update(event);
764 static int h_gpci_event_add(struct perf_event *event, int flags)
766 if (flags & PERF_EF_START)
767 h_gpci_event_start(event, flags);
772 static int h_gpci_event_init(struct perf_event *event)
779 if (event->attr.type != event->pmu->type)
782 /* config2 is unused */
783 if (event->attr.config2) {
784 pr_devel("config2 set when reserved\n");
788 /* no branch sampling */
789 if (has_branch_stack(event))
792 length = event_get_length(event);
793 if (length < 1 || length > 8) {
794 pr_devel("length invalid\n");
798 /* last byte within the buffer? */
799 if ((event_get_offset(event) + length) > HGPCI_MAX_DATA_BYTES) {
800 pr_devel("request outside of buffer: %zu > %zu\n",
801 (size_t)event_get_offset(event) + length,
802 HGPCI_MAX_DATA_BYTES);
806 /* check if the request works... */
807 ret = single_gpci_request(event_get_request(event),
808 event_get_starting_index(event),
809 event_get_secondary_index(event),
810 event_get_counter_info_version(event),
811 event_get_offset(event),
816 * ret value as H_AUTHORITY implies that partition is not permitted to retrieve
817 * performance information, and required to set
818 * "Enable Performance Information Collection" option.
820 if (ret == H_AUTHORITY)
824 pr_devel("gpci hcall failed\n");
831 static struct pmu h_gpci_pmu = {
832 .task_ctx_nr = perf_invalid_context,
835 .attr_groups = attr_groups,
836 .event_init = h_gpci_event_init,
837 .add = h_gpci_event_add,
838 .del = h_gpci_event_stop,
839 .start = h_gpci_event_start,
840 .stop = h_gpci_event_stop,
841 .read = h_gpci_event_update,
842 .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
845 static int ppc_hv_gpci_cpu_online(unsigned int cpu)
847 if (cpumask_empty(&hv_gpci_cpumask))
848 cpumask_set_cpu(cpu, &hv_gpci_cpumask);
853 static int ppc_hv_gpci_cpu_offline(unsigned int cpu)
857 /* Check if exiting cpu is used for collecting gpci events */
858 if (!cpumask_test_and_clear_cpu(cpu, &hv_gpci_cpumask))
861 /* Find a new cpu to collect gpci events */
862 target = cpumask_last(cpu_active_mask);
864 if (target < 0 || target >= nr_cpu_ids) {
865 pr_err("hv_gpci: CPU hotplug init failed\n");
869 /* Migrate gpci events to the new target */
870 cpumask_set_cpu(target, &hv_gpci_cpumask);
871 perf_pmu_migrate_context(&h_gpci_pmu, cpu, target);
876 static int hv_gpci_cpu_hotplug_init(void)
878 return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_HV_GPCI_ONLINE,
879 "perf/powerpc/hv_gcpi:online",
880 ppc_hv_gpci_cpu_online,
881 ppc_hv_gpci_cpu_offline);
884 static struct device_attribute *sysinfo_device_attr_create(int
885 sysinfo_interface_group_index, u32 req)
887 struct device_attribute *attr = NULL;
889 struct hv_gpci_request_buffer *arg;
891 if (sysinfo_interface_group_index < INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR ||
892 sysinfo_interface_group_index >= INTERFACE_NULL_ATTR) {
893 pr_info("Wrong interface group index for system information\n");
897 /* Check for given counter request value support */
898 arg = (void *)get_cpu_var(hv_gpci_reqb);
899 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
901 arg->params.counter_request = cpu_to_be32(req);
903 ret = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
904 virt_to_phys(arg), HGPCI_REQ_BUFFER_SIZE);
906 put_cpu_var(hv_gpci_reqb);
909 * Add given counter request value attribute in the interface_attrs
910 * attribute array, only for valid return types.
912 if (!ret || ret == H_AUTHORITY || ret == H_PARAMETER) {
913 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
917 sysfs_attr_init(&attr->attr);
918 attr->attr.mode = 0444;
920 switch (sysinfo_interface_group_index) {
921 case INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR:
922 attr->attr.name = "processor_bus_topology";
923 attr->show = processor_bus_topology_show;
925 case INTERFACE_PROCESSOR_CONFIG_ATTR:
926 attr->attr.name = "processor_config";
927 attr->show = processor_config_show;
929 case INTERFACE_AFFINITY_DOMAIN_VIA_VP_ATTR:
930 attr->attr.name = "affinity_domain_via_virtual_processor";
931 attr->show = affinity_domain_via_virtual_processor_show;
933 case INTERFACE_AFFINITY_DOMAIN_VIA_DOM_ATTR:
934 attr->attr.name = "affinity_domain_via_domain";
935 attr->show = affinity_domain_via_domain_show;
937 case INTERFACE_AFFINITY_DOMAIN_VIA_PAR_ATTR:
938 attr->attr.name = "affinity_domain_via_partition";
939 attr->show = affinity_domain_via_partition_show;
943 pr_devel("hcall failed, with error: 0x%lx\n", ret);
948 static void add_sysinfo_interface_files(void)
951 struct device_attribute *attr[INTERFACE_NULL_ATTR - INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR];
954 sysfs_count = INTERFACE_NULL_ATTR - INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR;
956 /* Get device attribute for a given counter request value */
957 for (i = 0; i < sysfs_count; i++) {
958 attr[i] = sysinfo_device_attr_create(i + INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR,
959 sysinfo_counter_request[i]);
965 /* Add sysinfo interface attributes in the interface_attrs attribute array */
966 for (i = 0; i < sysfs_count; i++)
967 interface_attrs[i + INTERFACE_PROCESSOR_BUS_TOPOLOGY_ATTR] = &attr[i]->attr;
973 * The sysinfo interface attributes will be added, only if hcall passed for
974 * all the counter request values. Free the device attribute array incase
975 * of any hcall failure.
985 static int hv_gpci_init(void)
989 struct hv_perf_caps caps;
990 struct hv_gpci_request_buffer *arg;
992 hv_gpci_assert_offsets_correct();
994 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
995 pr_debug("not a virtualized system, not enabling\n");
999 hret = hv_perf_caps_get(&caps);
1001 pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
1006 /* init cpuhotplug */
1007 r = hv_gpci_cpu_hotplug_init();
1011 /* sampling not supported */
1012 h_gpci_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
1014 arg = (void *)get_cpu_var(hv_gpci_reqb);
1015 memset(arg, 0, HGPCI_REQ_BUFFER_SIZE);
1018 * hcall H_GET_PERF_COUNTER_INFO populates the output
1019 * counter_info_version value based on the system hypervisor.
1020 * Pass the counter request 0x10 corresponds to request type
1021 * 'Dispatch_timebase_by_processor', to get the supported
1022 * counter_info_version.
1024 arg->params.counter_request = cpu_to_be32(0x10);
1026 r = plpar_hcall_norets(H_GET_PERF_COUNTER_INFO,
1027 virt_to_phys(arg), HGPCI_REQ_BUFFER_SIZE);
1029 pr_devel("hcall failed, can't get supported counter_info_version: 0x%x\n", r);
1030 arg->params.counter_info_version_out = 0x8;
1034 * Use counter_info_version_out value to assign
1035 * required hv-gpci event list.
1037 if (arg->params.counter_info_version_out >= 0x8)
1038 event_group.attrs = hv_gpci_event_attrs;
1040 event_group.attrs = hv_gpci_event_attrs_v6;
1042 put_cpu_var(hv_gpci_reqb);
1044 r = perf_pmu_register(&h_gpci_pmu, h_gpci_pmu.name, -1);
1048 /* sysinfo interface files are only available for power10 and above platforms */
1049 if (PVR_VER(mfspr(SPRN_PVR)) >= PVR_POWER10)
1050 add_sysinfo_interface_files();
1055 device_initcall(hv_gpci_init);