pmu.c 4.3 KB
Newer Older
1 2 3 4
/*
 *  linux/arch/arm/kernel/pmu.c
 *
 *  Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
5
 *  Copyright (C) 2010 ARM Ltd, Will Deacon
6 7 8 9 10 11 12
 *
 * 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.
 *
 */

13 14
#define pr_fmt(fmt) "PMU: " fmt

15 16 17 18 19
#include <linux/cpumask.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
20
#include <linux/of_device.h>
21
#include <linux/platform_device.h>
22 23 24

#include <asm/pmu.h>

25 26 27 28
static volatile long pmu_lock;

static struct platform_device *pmu_devices[ARM_NUM_PMU_DEVICES];

29 30
static int __devinit pmu_register(struct platform_device *pdev,
					enum arm_pmu_type type)
31
{
32
	if (type < 0 || type >= ARM_NUM_PMU_DEVICES) {
33
		pr_warning("received registration request for unknown "
34
				"device %d\n", type);
35 36 37
		return -EINVAL;
	}

38 39 40 41 42
	if (pmu_devices[type]) {
		pr_warning("rejecting duplicate registration of PMU device "
			"type %d.", type);
		return -ENOSPC;
	}
43

44
	pr_info("registered new PMU device of type %d\n", type);
45
	pmu_devices[type] = pdev;
46 47 48
	return 0;
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#define OF_MATCH_PMU(_name, _type) {	\
	.compatible = _name,		\
	.data = (void *)_type,		\
}

#define OF_MATCH_CPU(name)	OF_MATCH_PMU(name, ARM_PMU_DEVICE_CPU)

static struct of_device_id armpmu_of_device_ids[] = {
	OF_MATCH_CPU("arm,cortex-a9-pmu"),
	OF_MATCH_CPU("arm,cortex-a8-pmu"),
	OF_MATCH_CPU("arm,arm1136-pmu"),
	OF_MATCH_CPU("arm,arm1176-pmu"),
	{},
};

64 65 66 67 68 69 70 71 72 73 74 75
#define PLAT_MATCH_PMU(_name, _type) {	\
	.name		= _name,	\
	.driver_data	= _type,	\
}

#define PLAT_MATCH_CPU(_name)	PLAT_MATCH_PMU(_name, ARM_PMU_DEVICE_CPU)

static struct platform_device_id armpmu_plat_device_ids[] = {
	PLAT_MATCH_CPU("arm-pmu"),
	{},
};

76 77 78
enum arm_pmu_type armpmu_device_type(struct platform_device *pdev)
{
	const struct of_device_id	*of_id;
79
	const struct platform_device_id *pdev_id;
80 81 82 83 84 85 86 87

	/* provided by of_device_id table */
	if (pdev->dev.of_node) {
		of_id = of_match_device(armpmu_of_device_ids, &pdev->dev);
		BUG_ON(!of_id);
		return (enum arm_pmu_type)of_id->data;
	}

88 89 90 91
	/* Provided by platform_device_id table */
	pdev_id = platform_get_device_id(pdev);
	BUG_ON(!pdev_id);
	return pdev_id->driver_data;
92 93
}

94 95
static int __devinit armpmu_device_probe(struct platform_device *pdev)
{
96
	return pmu_register(pdev, armpmu_device_type(pdev));
97 98 99
}

static struct platform_driver armpmu_driver = {
100 101
	.driver		= {
		.name	= "arm-pmu",
102
		.of_match_table = armpmu_of_device_ids,
103
	},
104
	.probe		= armpmu_device_probe,
105
	.id_table	= armpmu_plat_device_ids,
106 107
};

108 109
static int __init register_pmu_driver(void)
{
110
	return platform_driver_register(&armpmu_driver);
111 112
}
device_initcall(register_pmu_driver);
113

114 115
struct platform_device *
reserve_pmu(enum arm_pmu_type device)
116
{
117 118 119 120 121 122 123 124 125 126 127 128
	struct platform_device *pdev;

	if (test_and_set_bit_lock(device, &pmu_lock)) {
		pdev = ERR_PTR(-EBUSY);
	} else if (pmu_devices[device] == NULL) {
		clear_bit_unlock(device, &pmu_lock);
		pdev = ERR_PTR(-ENODEV);
	} else {
		pdev = pmu_devices[device];
	}

	return pdev;
129 130 131 132
}
EXPORT_SYMBOL_GPL(reserve_pmu);

int
133
release_pmu(enum arm_pmu_type device)
134
{
135
	if (WARN_ON(!pmu_devices[device]))
136
		return -EINVAL;
137
	clear_bit_unlock(device, &pmu_lock);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	return 0;
}
EXPORT_SYMBOL_GPL(release_pmu);

static int
set_irq_affinity(int irq,
		 unsigned int cpu)
{
#ifdef CONFIG_SMP
	int err = irq_set_affinity(irq, cpumask_of(cpu));
	if (err)
		pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
			   irq, cpu);
	return err;
#else
153
	return -EINVAL;
154 155 156
#endif
}

157 158
static int
init_cpu_pmu(void)
159
{
160
	int i, irqs, err = 0;
161 162
	struct platform_device *pdev = pmu_devices[ARM_PMU_DEVICE_CPU];

163 164 165 166 167 168 169 170 171 172 173
	if (!pdev)
		return -ENODEV;

	irqs = pdev->num_resources;

	/*
	 * If we have a single PMU interrupt that we can't shift, assume that
	 * we're running on a uniprocessor machine and continue.
	 */
	if (irqs == 1 && !irq_can_set_affinity(platform_get_irq(pdev, 0)))
		return 0;
174

175
	for (i = 0; i < irqs; ++i) {
176
		err = set_irq_affinity(platform_get_irq(pdev, i), i);
177 178 179 180
		if (err)
			break;
	}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	return err;
}

int
init_pmu(enum arm_pmu_type device)
{
	int err = 0;

	switch (device) {
	case ARM_PMU_DEVICE_CPU:
		err = init_cpu_pmu();
		break;
	default:
		pr_warning("attempt to initialise unknown device %d\n",
				device);
		err = -EINVAL;
	}

199 200 201
	return err;
}
EXPORT_SYMBOL_GPL(init_pmu);