forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
kernel/arch/powerpc/perf/hv-gpci.c
....@@ -1,14 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Hypervisor supplied "gpci" ("get performance counter info") performance
34 * counter support
45 *
56 * Author: Cody P Schafer <cody@linux.vnet.ibm.com>
67 * 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.
128 */
139
1410 #define pr_fmt(fmt) "hv-gpci: " fmt
....@@ -51,6 +47,8 @@
5147 EVENT_DEFINE_RANGE_FORMAT(length, config1, 24, 31);
5248 /* u32, byte offset */
5349 EVENT_DEFINE_RANGE_FORMAT(offset, config1, 32, 63);
50
+
51
+static cpumask_t hv_gpci_cpumask;
5452
5553 static struct attribute *format_attrs[] = {
5654 &format_attr_request.attr,
....@@ -98,7 +96,15 @@
9896 return sprintf(page, "0x%x\n", COUNTER_INFO_VERSION_CURRENT);
9997 }
10098
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
+
101105 static DEVICE_ATTR_RO(kernel_version);
106
+static DEVICE_ATTR_RO(cpumask);
107
+
102108 HV_CAPS_ATTR(version, "0x%x\n");
103109 HV_CAPS_ATTR(ga, "%d\n");
104110 HV_CAPS_ATTR(expanded, "%d\n");
....@@ -115,6 +121,15 @@
115121 NULL,
116122 };
117123
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
+
118133 static struct attribute_group interface_group = {
119134 .name = "interface",
120135 .attrs = interface_attrs,
....@@ -124,19 +139,11 @@
124139 &format_group,
125140 &event_group,
126141 &interface_group,
142
+ &cpumask_attr_group,
127143 NULL,
128144 };
129145
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
-
134146 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;
140147
141148 static unsigned long single_gpci_request(u32 req, u32 starting_index,
142149 u16 secondary_index, u8 version_in, u32 offset, u8 length,
....@@ -232,15 +239,6 @@
232239 return -EINVAL;
233240 }
234241
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
-
244242 /* no branch sampling */
245243 if (has_branch_stack(event))
246244 return -EOPNOTSUPP;
....@@ -285,7 +283,47 @@
285283 .start = h_gpci_event_start,
286284 .stop = h_gpci_event_stop,
287285 .read = h_gpci_event_update,
286
+ .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
288287 };
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
+}
289327
290328 static int hv_gpci_init(void)
291329 {
....@@ -307,6 +345,11 @@
307345 return -ENODEV;
308346 }
309347
348
+ /* init cpuhotplug */
349
+ r = hv_gpci_cpu_hotplug_init();
350
+ if (r)
351
+ return r;
352
+
310353 /* sampling not supported */
311354 h_gpci_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
312355