Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright 2010 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright 2012 Advanced Micro Devices, Inc., Robert Richter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Perf-events backend for OProfile.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/oprofile.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Per performance monitor configuration as set via oprofilefs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) struct op_counter_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	unsigned long count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	unsigned long enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	unsigned long event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	unsigned long unit_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	unsigned long kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	unsigned long user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct perf_event_attr attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int oprofile_perf_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static DEFINE_MUTEX(oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static struct op_counter_config *counter_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static DEFINE_PER_CPU(struct perf_event **, perf_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int num_counters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Overflow callback for oprofile.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static void op_overflow_handler(struct perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			struct perf_sample_data *data, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u32 cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	for (id = 0; id < num_counters; ++id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		if (per_cpu(perf_events, cpu)[id] == event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (id != num_counters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		oprofile_add_sample(regs, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		pr_warn("oprofile: ignoring spurious overflow on cpu %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * settings in counter_config. Attributes are created as `pinned' events and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * so are permanently scheduled on the PMU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static void op_perf_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	u32 size = sizeof(struct perf_event_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct perf_event_attr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	for (i = 0; i < num_counters; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		attr = &counter_config[i].attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		memset(attr, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		attr->type		= PERF_TYPE_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		attr->size		= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		attr->config		= counter_config[i].event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		attr->sample_period	= counter_config[i].count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		attr->pinned		= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static int op_create_counter(int cpu, int event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct perf_event *pevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (!counter_config[event].enabled || per_cpu(perf_events, cpu)[event])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 						  cpu, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 						  op_overflow_handler, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (IS_ERR(pevent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return PTR_ERR(pevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (pevent->state != PERF_EVENT_STATE_ACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		perf_event_release_kernel(pevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		pr_warn("oprofile: failed to enable event %d on CPU %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			event, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	per_cpu(perf_events, cpu)[event] = pevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void op_destroy_counter(int cpu, int event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct perf_event *pevent = per_cpu(perf_events, cpu)[event];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (pevent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		perf_event_release_kernel(pevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		per_cpu(perf_events, cpu)[event] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * Called by oprofile_perf_start to create active perf events based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * perviously configured attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int op_perf_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int cpu, event, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		for (event = 0; event < num_counters; ++event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			ret = op_create_counter(cpu, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * Called by oprofile_perf_stop at the end of a profiling run.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static void op_perf_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int cpu, event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	for_each_online_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		for (event = 0; event < num_counters; ++event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			op_destroy_counter(cpu, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int oprofile_perf_create_files(struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	for (i = 0; i < num_counters; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		char buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		snprintf(buf, sizeof buf, "%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		dir = oprofilefs_mkdir(root, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		oprofilefs_create_ulong(dir, "enabled", &counter_config[i].enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		oprofilefs_create_ulong(dir, "event", &counter_config[i].event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		oprofilefs_create_ulong(dir, "count", &counter_config[i].count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		oprofilefs_create_ulong(dir, "unit_mask", &counter_config[i].unit_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		oprofilefs_create_ulong(dir, "kernel", &counter_config[i].kernel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		oprofilefs_create_ulong(dir, "user", &counter_config[i].user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int oprofile_perf_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	raw_spin_lock(&oprofilefs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	op_perf_setup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	raw_spin_unlock(&oprofilefs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int oprofile_perf_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	int ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	mutex_lock(&oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (!oprofile_perf_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		op_perf_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		oprofile_perf_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	mutex_unlock(&oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static void oprofile_perf_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	mutex_lock(&oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (oprofile_perf_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		op_perf_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	oprofile_perf_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	mutex_unlock(&oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	mutex_lock(&oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (oprofile_perf_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		op_perf_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	mutex_unlock(&oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int oprofile_perf_resume(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	mutex_lock(&oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (oprofile_perf_enabled && op_perf_start())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		oprofile_perf_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	mutex_unlock(&oprofile_perf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static struct platform_driver oprofile_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		.name		= "oprofile-perf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	.resume		= oprofile_perf_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	.suspend	= oprofile_perf_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static struct platform_device *oprofile_pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int __init init_driverfs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	ret = platform_driver_register(&oprofile_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	oprofile_pdev =	platform_device_register_simple(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 				oprofile_driver.driver.name, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (IS_ERR(oprofile_pdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		ret = PTR_ERR(oprofile_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		platform_driver_unregister(&oprofile_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static void exit_driverfs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	platform_device_unregister(oprofile_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	platform_driver_unregister(&oprofile_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static inline int  init_driverfs(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static inline void exit_driverfs(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) void oprofile_perf_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	int cpu, id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	struct perf_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		for (id = 0; id < num_counters; ++id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			event = per_cpu(perf_events, cpu)[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			if (event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 				perf_event_release_kernel(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		kfree(per_cpu(perf_events, cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	kfree(counter_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	exit_driverfs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int __init oprofile_perf_init(struct oprofile_operations *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	int cpu, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	ret = init_driverfs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	num_counters = perf_num_counters();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (num_counters <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		pr_info("oprofile: no performance counters\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	counter_config = kcalloc(num_counters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			sizeof(struct op_counter_config), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (!counter_config) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		pr_info("oprofile: failed to allocate %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 				"counters\n", num_counters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		num_counters = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		per_cpu(perf_events, cpu) = kcalloc(num_counters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 				sizeof(struct perf_event *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		if (!per_cpu(perf_events, cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			pr_info("oprofile: failed to allocate %d perf events "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 					"for cpu %d\n", num_counters, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	ops->create_files	= oprofile_perf_create_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	ops->setup		= oprofile_perf_setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	ops->start		= oprofile_perf_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	ops->stop		= oprofile_perf_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	ops->shutdown		= oprofile_perf_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	ops->cpu_type		= op_name_from_perf_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (!ops->cpu_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		pr_info("oprofile: using %s\n", ops->cpu_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		oprofile_perf_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }