blob: 2ce00be697eaa668ce453db78782660800b1f1d8 [file] [log] [blame]
Jamie Iles0f4f0672010-02-02 20:23:15 +01001/*
2 * linux/arch/arm/kernel/pmu.c
3 *
4 * Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
Will Deacon49c006b2010-04-29 17:13:24 +01005 * Copyright (C) 2010 ARM Ltd, Will Deacon
Jamie Iles0f4f0672010-02-02 20:23:15 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
Will Deacon49c006b2010-04-29 17:13:24 +010013#define pr_fmt(fmt) "PMU: " fmt
14
Jamie Iles0f4f0672010-02-02 20:23:15 +010015#include <linux/cpumask.h>
16#include <linux/err.h>
17#include <linux/interrupt.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
Mark Rutlande73c34c2011-06-22 15:33:55 +010020#include <linux/of_device.h>
Will Deacon49c006b2010-04-29 17:13:24 +010021#include <linux/platform_device.h>
Jamie Iles0f4f0672010-02-02 20:23:15 +010022
23#include <asm/pmu.h>
24
Jamie Iles0f4f0672010-02-02 20:23:15 +010025static volatile long pmu_lock;
26
Will Deacon49c006b2010-04-29 17:13:24 +010027static struct platform_device *pmu_devices[ARM_NUM_PMU_DEVICES];
28
Mark Rutlandf12482c2011-06-22 15:30:51 +010029static int __devinit pmu_register(struct platform_device *pdev,
30 enum arm_pmu_type type)
Jamie Iles0f4f0672010-02-02 20:23:15 +010031{
Mark Rutlandf12482c2011-06-22 15:30:51 +010032 if (type < 0 || type >= ARM_NUM_PMU_DEVICES) {
Will Deacon49c006b2010-04-29 17:13:24 +010033 pr_warning("received registration request for unknown "
Mark Rutlandf12482c2011-06-22 15:30:51 +010034 "device %d\n", type);
Will Deacon49c006b2010-04-29 17:13:24 +010035 return -EINVAL;
36 }
37
Mark Rutlandae0c3752011-06-22 15:32:48 +010038 if (pmu_devices[type]) {
39 pr_warning("rejecting duplicate registration of PMU device "
40 "type %d.", type);
41 return -ENOSPC;
42 }
Will Deacon49c006b2010-04-29 17:13:24 +010043
Mark Rutlandae0c3752011-06-22 15:32:48 +010044 pr_info("registered new PMU device of type %d\n", type);
Mark Rutlandf12482c2011-06-22 15:30:51 +010045 pmu_devices[type] = pdev;
Will Deacon49c006b2010-04-29 17:13:24 +010046 return 0;
47}
48
Mark Rutlande73c34c2011-06-22 15:33:55 +010049#define OF_MATCH_PMU(_name, _type) { \
50 .compatible = _name, \
51 .data = (void *)_type, \
52}
53
54#define OF_MATCH_CPU(name) OF_MATCH_PMU(name, ARM_PMU_DEVICE_CPU)
55
56static struct of_device_id armpmu_of_device_ids[] = {
57 OF_MATCH_CPU("arm,cortex-a9-pmu"),
58 OF_MATCH_CPU("arm,cortex-a8-pmu"),
59 OF_MATCH_CPU("arm,arm1136-pmu"),
60 OF_MATCH_CPU("arm,arm1176-pmu"),
61 {},
62};
63
64enum arm_pmu_type armpmu_device_type(struct platform_device *pdev)
65{
66 const struct of_device_id *of_id;
67
68 /* provided by of_device_id table */
69 if (pdev->dev.of_node) {
70 of_id = of_match_device(armpmu_of_device_ids, &pdev->dev);
71 BUG_ON(!of_id);
72 return (enum arm_pmu_type)of_id->data;
73 }
74
75 /* Provided by a 'legacy' platform_device */
76 return ARM_PMU_DEVICE_CPU;
77}
78
Mark Rutlandf12482c2011-06-22 15:30:51 +010079static int __devinit armpmu_device_probe(struct platform_device *pdev)
80{
Mark Rutlande73c34c2011-06-22 15:33:55 +010081 return pmu_register(pdev, armpmu_device_type(pdev));
Mark Rutlandf12482c2011-06-22 15:30:51 +010082}
83
84static struct platform_driver armpmu_driver = {
Will Deacon49c006b2010-04-29 17:13:24 +010085 .driver = {
86 .name = "arm-pmu",
Mark Rutlande73c34c2011-06-22 15:33:55 +010087 .of_match_table = armpmu_of_device_ids,
Will Deacon49c006b2010-04-29 17:13:24 +010088 },
Mark Rutlandf12482c2011-06-22 15:30:51 +010089 .probe = armpmu_device_probe,
Will Deacon49c006b2010-04-29 17:13:24 +010090};
91
92static int __init register_pmu_driver(void)
93{
Mark Rutlandf12482c2011-06-22 15:30:51 +010094 return platform_driver_register(&armpmu_driver);
Will Deacon49c006b2010-04-29 17:13:24 +010095}
96device_initcall(register_pmu_driver);
97
98struct platform_device *
99reserve_pmu(enum arm_pmu_type device)
100{
101 struct platform_device *pdev;
102
103 if (test_and_set_bit_lock(device, &pmu_lock)) {
104 pdev = ERR_PTR(-EBUSY);
105 } else if (pmu_devices[device] == NULL) {
106 clear_bit_unlock(device, &pmu_lock);
107 pdev = ERR_PTR(-ENODEV);
108 } else {
109 pdev = pmu_devices[device];
110 }
111
112 return pdev;
Jamie Iles0f4f0672010-02-02 20:23:15 +0100113}
114EXPORT_SYMBOL_GPL(reserve_pmu);
115
116int
Mark Rutlandf12482c2011-06-22 15:30:51 +0100117release_pmu(enum arm_pmu_type device)
Jamie Iles0f4f0672010-02-02 20:23:15 +0100118{
Mark Rutlandf12482c2011-06-22 15:30:51 +0100119 if (WARN_ON(!pmu_devices[device]))
Jamie Iles0f4f0672010-02-02 20:23:15 +0100120 return -EINVAL;
Mark Rutlandf12482c2011-06-22 15:30:51 +0100121 clear_bit_unlock(device, &pmu_lock);
Jamie Iles0f4f0672010-02-02 20:23:15 +0100122 return 0;
123}
124EXPORT_SYMBOL_GPL(release_pmu);
125
126static int
127set_irq_affinity(int irq,
128 unsigned int cpu)
129{
130#ifdef CONFIG_SMP
131 int err = irq_set_affinity(irq, cpumask_of(cpu));
132 if (err)
133 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
134 irq, cpu);
135 return err;
136#else
Will Deacon71efb062011-02-18 16:21:06 +0100137 return -EINVAL;
Jamie Iles0f4f0672010-02-02 20:23:15 +0100138#endif
139}
140
Will Deacon49c006b2010-04-29 17:13:24 +0100141static int
142init_cpu_pmu(void)
Jamie Iles0f4f0672010-02-02 20:23:15 +0100143{
Will Deacon71efb062011-02-18 16:21:06 +0100144 int i, irqs, err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100145 struct platform_device *pdev = pmu_devices[ARM_PMU_DEVICE_CPU];
Jamie Iles0f4f0672010-02-02 20:23:15 +0100146
Will Deacon71efb062011-02-18 16:21:06 +0100147 if (!pdev)
148 return -ENODEV;
Will Deacon49c006b2010-04-29 17:13:24 +0100149
Will Deacon71efb062011-02-18 16:21:06 +0100150 irqs = pdev->num_resources;
151
152 /*
153 * If we have a single PMU interrupt that we can't shift, assume that
154 * we're running on a uniprocessor machine and continue.
155 */
156 if (irqs == 1 && !irq_can_set_affinity(platform_get_irq(pdev, 0)))
157 return 0;
158
159 for (i = 0; i < irqs; ++i) {
Will Deacon49c006b2010-04-29 17:13:24 +0100160 err = set_irq_affinity(platform_get_irq(pdev, i), i);
Jamie Iles0f4f0672010-02-02 20:23:15 +0100161 if (err)
162 break;
163 }
164
Will Deacon49c006b2010-04-29 17:13:24 +0100165 return err;
166}
167
168int
169init_pmu(enum arm_pmu_type device)
170{
171 int err = 0;
172
173 switch (device) {
174 case ARM_PMU_DEVICE_CPU:
175 err = init_cpu_pmu();
176 break;
177 default:
178 pr_warning("attempt to initialise unknown device %d\n",
179 device);
180 err = -EINVAL;
181 }
182
Jamie Iles0f4f0672010-02-02 20:23:15 +0100183 return err;
184}
185EXPORT_SYMBOL_GPL(init_pmu);