perf_event_cpu.c 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) 2012 ARM Limited
 *
 * Author: Will Deacon <will.deacon@arm.com>
 */
#define pr_fmt(fmt) "CPU PMU: " fmt

#include <linux/bitmap.h>
#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/platform_device.h>
26
#include <linux/slab.h>
27
#include <linux/spinlock.h>
28 29
#include <linux/irq.h>
#include <linux/irqdesc.h>
30 31 32 33 34 35 36 37

#include <asm/cputype.h>
#include <asm/irq_regs.h>
#include <asm/pmu.h>

/* Set at runtime when we know what CPU type we are. */
static struct arm_pmu *cpu_pmu;

38
static DEFINE_PER_CPU(struct arm_pmu *, percpu_pmu);
39 40 41 42 43 44 45 46 47 48 49 50 51
static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);

/*
 * Despite the names, these two functions are CPU-specific and are used
 * by the OProfile/perf code.
 */
const char *perf_pmu_name(void)
{
	if (!cpu_pmu)
		return NULL;

52
	return cpu_pmu->name;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
}
EXPORT_SYMBOL_GPL(perf_pmu_name);

int perf_num_counters(void)
{
	int max_events = 0;

	if (cpu_pmu != NULL)
		max_events = cpu_pmu->num_events;

	return max_events;
}
EXPORT_SYMBOL_GPL(perf_num_counters);

/* Include the PMU-specific implementations. */
#include "perf_event_xscale.c"
#include "perf_event_v6.c"
#include "perf_event_v7.c"

static struct pmu_hw_events *cpu_pmu_get_cpu_events(void)
{
74
	return this_cpu_ptr(&cpu_hw_events);
75 76
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static void cpu_pmu_enable_percpu_irq(void *data)
{
	struct arm_pmu *cpu_pmu = data;
	struct platform_device *pmu_device = cpu_pmu->plat_device;
	int irq = platform_get_irq(pmu_device, 0);

	enable_percpu_irq(irq, IRQ_TYPE_NONE);
	cpumask_set_cpu(smp_processor_id(), &cpu_pmu->active_irqs);
}

static void cpu_pmu_disable_percpu_irq(void *data)
{
	struct arm_pmu *cpu_pmu = data;
	struct platform_device *pmu_device = cpu_pmu->plat_device;
	int irq = platform_get_irq(pmu_device, 0);

	cpumask_clear_cpu(smp_processor_id(), &cpu_pmu->active_irqs);
	disable_percpu_irq(irq);
}

97
static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
98 99 100 101 102 103
{
	int i, irq, irqs;
	struct platform_device *pmu_device = cpu_pmu->plat_device;

	irqs = min(pmu_device->num_resources, num_possible_cpus());

104 105 106 107 108 109 110 111 112 113 114 115
	irq = platform_get_irq(pmu_device, 0);
	if (irq >= 0 && irq_is_percpu(irq)) {
		on_each_cpu(cpu_pmu_disable_percpu_irq, cpu_pmu, 1);
		free_percpu_irq(irq, &percpu_pmu);
	} else {
		for (i = 0; i < irqs; ++i) {
			if (!cpumask_test_and_clear_cpu(i, &cpu_pmu->active_irqs))
				continue;
			irq = platform_get_irq(pmu_device, i);
			if (irq >= 0)
				free_irq(irq, cpu_pmu);
		}
116 117 118
	}
}

119
static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
120 121 122 123 124 125 126 127 128
{
	int i, err, irq, irqs;
	struct platform_device *pmu_device = cpu_pmu->plat_device;

	if (!pmu_device)
		return -ENODEV;

	irqs = min(pmu_device->num_resources, num_possible_cpus());
	if (irqs < 1) {
129 130
		printk_once("perf/ARM: No irqs for PMU defined, sampling events not supported\n");
		return 0;
131 132
	}

133 134 135
	irq = platform_get_irq(pmu_device, 0);
	if (irq >= 0 && irq_is_percpu(irq)) {
		err = request_percpu_irq(irq, handler, "arm-pmu", &percpu_pmu);
136 137 138 139 140
		if (err) {
			pr_err("unable to request IRQ%d for ARM PMU counters\n",
				irq);
			return err;
		}
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
		on_each_cpu(cpu_pmu_enable_percpu_irq, cpu_pmu, 1);
	} else {
		for (i = 0; i < irqs; ++i) {
			err = 0;
			irq = platform_get_irq(pmu_device, i);
			if (irq < 0)
				continue;

			/*
			 * If we have a single PMU interrupt that we can't shift,
			 * assume that we're running on a uniprocessor machine and
			 * continue. Otherwise, continue without this interrupt.
			 */
			if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
				pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
					    irq, i);
				continue;
			}

			err = request_irq(irq, handler,
					  IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
					  cpu_pmu);
			if (err) {
				pr_err("unable to request IRQ%d for ARM PMU counters\n",
					irq);
				return err;
			}

			cpumask_set_cpu(i, &cpu_pmu->active_irqs);
		}
171 172 173 174 175
	}

	return 0;
}

176
static void cpu_pmu_init(struct arm_pmu *cpu_pmu)
177 178 179 180 181 182 183
{
	int cpu;
	for_each_possible_cpu(cpu) {
		struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
		events->events = per_cpu(hw_events, cpu);
		events->used_mask = per_cpu(used_mask, cpu);
		raw_spin_lock_init(&events->pmu_lock);
184
		per_cpu(percpu_pmu, cpu) = cpu_pmu;
185
	}
186 187 188 189

	cpu_pmu->get_hw_events	= cpu_pmu_get_cpu_events;
	cpu_pmu->request_irq	= cpu_pmu_request_irq;
	cpu_pmu->free_irq	= cpu_pmu_free_irq;
190 191

	/* Ensure the PMU has sane values out of reset. */
192
	if (cpu_pmu->reset)
193
		on_each_cpu(cpu_pmu->reset, cpu_pmu, 1);
194 195 196 197

	/* If no interrupts available, set the corresponding capability flag */
	if (!platform_get_irq(cpu_pmu->plat_device, 0))
		cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
198 199 200 201 202 203 204 205
}

/*
 * PMU hardware loses all context when a CPU goes offline.
 * When a CPU is hotplugged back in, since some hardware registers are
 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
 * junk values out of them.
 */
206 207
static int cpu_pmu_notify(struct notifier_block *b, unsigned long action,
			  void *hcpu)
208 209 210 211 212
{
	if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
		return NOTIFY_DONE;

	if (cpu_pmu && cpu_pmu->reset)
213
		cpu_pmu->reset(cpu_pmu);
214 215
	else
		return NOTIFY_DONE;
216 217 218 219

	return NOTIFY_OK;
}

220
static struct notifier_block cpu_pmu_hotplug_notifier = {
221 222 223 224 225 226
	.notifier_call = cpu_pmu_notify,
};

/*
 * PMU platform driver and devicetree bindings.
 */
227
static struct of_device_id cpu_pmu_of_device_ids[] = {
228
	{.compatible = "arm,cortex-a17-pmu",	.data = armv7_a17_pmu_init},
229
	{.compatible = "arm,cortex-a15-pmu",	.data = armv7_a15_pmu_init},
230
	{.compatible = "arm,cortex-a12-pmu",	.data = armv7_a12_pmu_init},
231 232 233 234 235 236 237
	{.compatible = "arm,cortex-a9-pmu",	.data = armv7_a9_pmu_init},
	{.compatible = "arm,cortex-a8-pmu",	.data = armv7_a8_pmu_init},
	{.compatible = "arm,cortex-a7-pmu",	.data = armv7_a7_pmu_init},
	{.compatible = "arm,cortex-a5-pmu",	.data = armv7_a5_pmu_init},
	{.compatible = "arm,arm11mpcore-pmu",	.data = armv6mpcore_pmu_init},
	{.compatible = "arm,arm1176-pmu",	.data = armv6pmu_init},
	{.compatible = "arm,arm1136-pmu",	.data = armv6pmu_init},
238
	{.compatible = "qcom,krait-pmu",	.data = krait_pmu_init},
239 240 241
	{},
};

242
static struct platform_device_id cpu_pmu_plat_device_ids[] = {
243 244 245 246 247 248 249
	{.name = "arm-pmu"},
	{},
};

/*
 * CPU PMU identification and probing.
 */
250
static int probe_current_pmu(struct arm_pmu *pmu)
251 252
{
	int cpu = get_cpu();
253 254
	unsigned long implementor = read_cpuid_implementor();
	unsigned long part_number = read_cpuid_part_number();
255
	int ret = -ENODEV;
256 257 258 259

	pr_info("probing PMU on CPU %d\n", cpu);

	/* ARM Ltd CPUs. */
260
	if (implementor == ARM_CPU_IMP_ARM) {
261
		switch (part_number) {
262 263 264
		case ARM_CPU_PART_ARM1136:
		case ARM_CPU_PART_ARM1156:
		case ARM_CPU_PART_ARM1176:
265
			ret = armv6pmu_init(pmu);
266
			break;
267
		case ARM_CPU_PART_ARM11MPCORE:
268
			ret = armv6mpcore_pmu_init(pmu);
269
			break;
270
		case ARM_CPU_PART_CORTEX_A8:
271
			ret = armv7_a8_pmu_init(pmu);
272
			break;
273
		case ARM_CPU_PART_CORTEX_A9:
274
			ret = armv7_a9_pmu_init(pmu);
275 276 277
			break;
		}
	/* Intel CPUs [xscale]. */
278 279 280
	} else if (implementor == ARM_CPU_IMP_INTEL) {
		switch (xscale_cpu_arch_version()) {
		case ARM_CPU_XSCALE_ARCH_V1:
281
			ret = xscale1pmu_init(pmu);
282
			break;
283
		case ARM_CPU_XSCALE_ARCH_V2:
284
			ret = xscale2pmu_init(pmu);
285 286 287 288 289
			break;
		}
	}

	put_cpu();
290
	return ret;
291 292
}

293
static int cpu_pmu_device_probe(struct platform_device *pdev)
294 295
{
	const struct of_device_id *of_id;
296
	const int (*init_fn)(struct arm_pmu *);
297
	struct device_node *node = pdev->dev.of_node;
298 299
	struct arm_pmu *pmu;
	int ret = -ENODEV;
300 301 302 303 304 305

	if (cpu_pmu) {
		pr_info("attempt to register multiple PMU devices!");
		return -ENOSPC;
	}

306 307 308 309 310 311
	pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
	if (!pmu) {
		pr_info("failed to allocate PMU device!");
		return -ENOMEM;
	}

312 313 314
	cpu_pmu = pmu;
	cpu_pmu->plat_device = pdev;

315 316
	if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) {
		init_fn = of_id->data;
317
		ret = init_fn(pmu);
318
	} else {
319
		ret = probe_current_pmu(pmu);
320 321
	}

322
	if (ret) {
323 324
		pr_info("failed to probe PMU!");
		goto out_free;
325
	}
326 327

	cpu_pmu_init(cpu_pmu);
328
	ret = armpmu_register(cpu_pmu, PERF_TYPE_RAW);
329

330 331 332 333 334 335 336
	if (!ret)
		return 0;

out_free:
	pr_info("failed to register PMU devices!");
	kfree(pmu);
	return ret;
337 338 339 340 341 342 343 344 345 346 347 348 349 350
}

static struct platform_driver cpu_pmu_driver = {
	.driver		= {
		.name	= "arm-pmu",
		.pm	= &armpmu_dev_pm_ops,
		.of_match_table = cpu_pmu_of_device_ids,
	},
	.probe		= cpu_pmu_device_probe,
	.id_table	= cpu_pmu_plat_device_ids,
};

static int __init register_pmu_driver(void)
{
351 352 353 354 355 356 357 358 359 360 361
	int err;

	err = register_cpu_notifier(&cpu_pmu_hotplug_notifier);
	if (err)
		return err;

	err = platform_driver_register(&cpu_pmu_driver);
	if (err)
		unregister_cpu_notifier(&cpu_pmu_hotplug_notifier);

	return err;
362 363
}
device_initcall(register_pmu_driver);