| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Hypervisor supplied "gpci" ("get performance counter info") performance |
|---|
| 3 | 4 | * counter support |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * Author: Cody P Schafer <cody@linux.vnet.ibm.com> |
|---|
| 6 | 7 | * Copyright 2014 IBM Corporation. |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is free software; you can redistribute it and/or |
|---|
| 9 | | - * modify it under the terms of the GNU General Public License |
|---|
| 10 | | - * as published by the Free Software Foundation; either version |
|---|
| 11 | | - * 2 of the License, or (at your option) any later version. |
|---|
| 12 | 8 | */ |
|---|
| 13 | 9 | |
|---|
| 14 | 10 | #define pr_fmt(fmt) "hv-gpci: " fmt |
|---|
| .. | .. |
|---|
| 51 | 47 | EVENT_DEFINE_RANGE_FORMAT(length, config1, 24, 31); |
|---|
| 52 | 48 | /* u32, byte offset */ |
|---|
| 53 | 49 | EVENT_DEFINE_RANGE_FORMAT(offset, config1, 32, 63); |
|---|
| 50 | + |
|---|
| 51 | +static cpumask_t hv_gpci_cpumask; |
|---|
| 54 | 52 | |
|---|
| 55 | 53 | static struct attribute *format_attrs[] = { |
|---|
| 56 | 54 | &format_attr_request.attr, |
|---|
| .. | .. |
|---|
| 98 | 96 | return sprintf(page, "0x%x\n", COUNTER_INFO_VERSION_CURRENT); |
|---|
| 99 | 97 | } |
|---|
| 100 | 98 | |
|---|
| 99 | +static ssize_t cpumask_show(struct device *dev, |
|---|
| 100 | + struct device_attribute *attr, char *buf) |
|---|
| 101 | +{ |
|---|
| 102 | + return cpumap_print_to_pagebuf(true, buf, &hv_gpci_cpumask); |
|---|
| 103 | +} |
|---|
| 104 | + |
|---|
| 101 | 105 | static DEVICE_ATTR_RO(kernel_version); |
|---|
| 106 | +static DEVICE_ATTR_RO(cpumask); |
|---|
| 107 | + |
|---|
| 102 | 108 | HV_CAPS_ATTR(version, "0x%x\n"); |
|---|
| 103 | 109 | HV_CAPS_ATTR(ga, "%d\n"); |
|---|
| 104 | 110 | HV_CAPS_ATTR(expanded, "%d\n"); |
|---|
| .. | .. |
|---|
| 115 | 121 | NULL, |
|---|
| 116 | 122 | }; |
|---|
| 117 | 123 | |
|---|
| 124 | +static struct attribute *cpumask_attrs[] = { |
|---|
| 125 | + &dev_attr_cpumask.attr, |
|---|
| 126 | + NULL, |
|---|
| 127 | +}; |
|---|
| 128 | + |
|---|
| 129 | +static struct attribute_group cpumask_attr_group = { |
|---|
| 130 | + .attrs = cpumask_attrs, |
|---|
| 131 | +}; |
|---|
| 132 | + |
|---|
| 118 | 133 | static struct attribute_group interface_group = { |
|---|
| 119 | 134 | .name = "interface", |
|---|
| 120 | 135 | .attrs = interface_attrs, |
|---|
| .. | .. |
|---|
| 124 | 139 | &format_group, |
|---|
| 125 | 140 | &event_group, |
|---|
| 126 | 141 | &interface_group, |
|---|
| 142 | + &cpumask_attr_group, |
|---|
| 127 | 143 | NULL, |
|---|
| 128 | 144 | }; |
|---|
| 129 | 145 | |
|---|
| 130 | | -#define HGPCI_REQ_BUFFER_SIZE 4096 |
|---|
| 131 | | -#define HGPCI_MAX_DATA_BYTES \ |
|---|
| 132 | | - (HGPCI_REQ_BUFFER_SIZE - sizeof(struct hv_get_perf_counter_info_params)) |
|---|
| 133 | | - |
|---|
| 134 | 146 | static DEFINE_PER_CPU(char, hv_gpci_reqb[HGPCI_REQ_BUFFER_SIZE]) __aligned(sizeof(uint64_t)); |
|---|
| 135 | | - |
|---|
| 136 | | -struct hv_gpci_request_buffer { |
|---|
| 137 | | - struct hv_get_perf_counter_info_params params; |
|---|
| 138 | | - uint8_t bytes[HGPCI_MAX_DATA_BYTES]; |
|---|
| 139 | | -} __packed; |
|---|
| 140 | 147 | |
|---|
| 141 | 148 | static unsigned long single_gpci_request(u32 req, u32 starting_index, |
|---|
| 142 | 149 | u16 secondary_index, u8 version_in, u32 offset, u8 length, |
|---|
| .. | .. |
|---|
| 232 | 239 | return -EINVAL; |
|---|
| 233 | 240 | } |
|---|
| 234 | 241 | |
|---|
| 235 | | - /* unsupported modes and filters */ |
|---|
| 236 | | - if (event->attr.exclude_user || |
|---|
| 237 | | - event->attr.exclude_kernel || |
|---|
| 238 | | - event->attr.exclude_hv || |
|---|
| 239 | | - event->attr.exclude_idle || |
|---|
| 240 | | - event->attr.exclude_host || |
|---|
| 241 | | - event->attr.exclude_guest) |
|---|
| 242 | | - return -EINVAL; |
|---|
| 243 | | - |
|---|
| 244 | 242 | /* no branch sampling */ |
|---|
| 245 | 243 | if (has_branch_stack(event)) |
|---|
| 246 | 244 | return -EOPNOTSUPP; |
|---|
| .. | .. |
|---|
| 285 | 283 | .start = h_gpci_event_start, |
|---|
| 286 | 284 | .stop = h_gpci_event_stop, |
|---|
| 287 | 285 | .read = h_gpci_event_update, |
|---|
| 286 | + .capabilities = PERF_PMU_CAP_NO_EXCLUDE, |
|---|
| 288 | 287 | }; |
|---|
| 288 | + |
|---|
| 289 | +static int ppc_hv_gpci_cpu_online(unsigned int cpu) |
|---|
| 290 | +{ |
|---|
| 291 | + if (cpumask_empty(&hv_gpci_cpumask)) |
|---|
| 292 | + cpumask_set_cpu(cpu, &hv_gpci_cpumask); |
|---|
| 293 | + |
|---|
| 294 | + return 0; |
|---|
| 295 | +} |
|---|
| 296 | + |
|---|
| 297 | +static int ppc_hv_gpci_cpu_offline(unsigned int cpu) |
|---|
| 298 | +{ |
|---|
| 299 | + int target; |
|---|
| 300 | + |
|---|
| 301 | + /* Check if exiting cpu is used for collecting gpci events */ |
|---|
| 302 | + if (!cpumask_test_and_clear_cpu(cpu, &hv_gpci_cpumask)) |
|---|
| 303 | + return 0; |
|---|
| 304 | + |
|---|
| 305 | + /* Find a new cpu to collect gpci events */ |
|---|
| 306 | + target = cpumask_last(cpu_active_mask); |
|---|
| 307 | + |
|---|
| 308 | + if (target < 0 || target >= nr_cpu_ids) { |
|---|
| 309 | + pr_err("hv_gpci: CPU hotplug init failed\n"); |
|---|
| 310 | + return -1; |
|---|
| 311 | + } |
|---|
| 312 | + |
|---|
| 313 | + /* Migrate gpci events to the new target */ |
|---|
| 314 | + cpumask_set_cpu(target, &hv_gpci_cpumask); |
|---|
| 315 | + perf_pmu_migrate_context(&h_gpci_pmu, cpu, target); |
|---|
| 316 | + |
|---|
| 317 | + return 0; |
|---|
| 318 | +} |
|---|
| 319 | + |
|---|
| 320 | +static int hv_gpci_cpu_hotplug_init(void) |
|---|
| 321 | +{ |
|---|
| 322 | + return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_HV_GPCI_ONLINE, |
|---|
| 323 | + "perf/powerpc/hv_gcpi:online", |
|---|
| 324 | + ppc_hv_gpci_cpu_online, |
|---|
| 325 | + ppc_hv_gpci_cpu_offline); |
|---|
| 326 | +} |
|---|
| 289 | 327 | |
|---|
| 290 | 328 | static int hv_gpci_init(void) |
|---|
| 291 | 329 | { |
|---|
| .. | .. |
|---|
| 307 | 345 | return -ENODEV; |
|---|
| 308 | 346 | } |
|---|
| 309 | 347 | |
|---|
| 348 | + /* init cpuhotplug */ |
|---|
| 349 | + r = hv_gpci_cpu_hotplug_init(); |
|---|
| 350 | + if (r) |
|---|
| 351 | + return r; |
|---|
| 352 | + |
|---|
| 310 | 353 | /* sampling not supported */ |
|---|
| 311 | 354 | h_gpci_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT; |
|---|
| 312 | 355 | |
|---|