2 * linux/arch/arm/kernel/pmu.c
4 * Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
5 * Copyright (C) 2010 ARM Ltd, Will Deacon
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.
13 #define pr_fmt(fmt) "PMU: " fmt
15 #include <linux/cpumask.h>
16 #include <linux/err.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
25 static volatile long pmu_lock;
27 static struct platform_device *pmu_devices[ARM_NUM_PMU_DEVICES];
29 static int __devinit pmu_register(struct platform_device *pdev,
30 enum arm_pmu_type type)
32 if (type < 0 || type >= ARM_NUM_PMU_DEVICES) {
33 pr_warning("received registration request for unknown "
38 if (pmu_devices[type]) {
39 pr_warning("rejecting duplicate registration of PMU device "
44 pr_info("registered new PMU device of type %d\n", type);
45 pmu_devices[type] = pdev;
49 #define OF_MATCH_PMU(_name, _type) { \
50 .compatible = _name, \
51 .data = (void *)_type, \
54 #define OF_MATCH_CPU(name) OF_MATCH_PMU(name, ARM_PMU_DEVICE_CPU)
56 static 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"),
64 enum arm_pmu_type armpmu_device_type(struct platform_device *pdev)
66 const struct of_device_id *of_id;
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);
72 return (enum arm_pmu_type)of_id->data;
75 /* Provided by a 'legacy' platform_device */
76 return ARM_PMU_DEVICE_CPU;
79 static int __devinit armpmu_device_probe(struct platform_device *pdev)
81 return pmu_register(pdev, armpmu_device_type(pdev));
84 static struct platform_driver armpmu_driver = {
87 .of_match_table = armpmu_of_device_ids,
89 .probe = armpmu_device_probe,
92 static int __init register_pmu_driver(void)
94 return platform_driver_register(&armpmu_driver);
96 device_initcall(register_pmu_driver);
98 struct platform_device *
99 reserve_pmu(enum arm_pmu_type device)
101 struct platform_device *pdev;
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);
109 pdev = pmu_devices[device];
114 EXPORT_SYMBOL_GPL(reserve_pmu);
117 release_pmu(enum arm_pmu_type device)
119 if (WARN_ON(!pmu_devices[device]))
121 clear_bit_unlock(device, &pmu_lock);
124 EXPORT_SYMBOL_GPL(release_pmu);
127 set_irq_affinity(int irq,
131 int err = irq_set_affinity(irq, cpumask_of(cpu));
133 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
144 int i, irqs, err = 0;
145 struct platform_device *pdev = pmu_devices[ARM_PMU_DEVICE_CPU];
150 irqs = pdev->num_resources;
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.
156 if (irqs == 1 && !irq_can_set_affinity(platform_get_irq(pdev, 0)))
159 for (i = 0; i < irqs; ++i) {
160 err = set_irq_affinity(platform_get_irq(pdev, i), i);
169 init_pmu(enum arm_pmu_type device)
174 case ARM_PMU_DEVICE_CPU:
175 err = init_cpu_pmu();
178 pr_warning("attempt to initialise unknown device %d\n",
185 EXPORT_SYMBOL_GPL(init_pmu);