blob: de6b1b0860c2b2cd97f407410ba31f9381b019f1 [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>
Will Deacon49c006b2010-04-29 17:13:24 +010020#include <linux/platform_device.h>
Jamie Iles0f4f0672010-02-02 20:23:15 +010021
22#include <asm/pmu.h>
23
Jamie Iles0f4f0672010-02-02 20:23:15 +010024static volatile long pmu_lock;
25
Will Deacon49c006b2010-04-29 17:13:24 +010026static struct platform_device *pmu_devices[ARM_NUM_PMU_DEVICES];
27
Mark Rutlandf12482c2011-06-22 15:30:51 +010028static int __devinit pmu_register(struct platform_device *pdev,
29 enum arm_pmu_type type)
Jamie Iles0f4f0672010-02-02 20:23:15 +010030{
Mark Rutlandf12482c2011-06-22 15:30:51 +010031 if (type < 0 || type >= ARM_NUM_PMU_DEVICES) {
Will Deacon49c006b2010-04-29 17:13:24 +010032 pr_warning("received registration request for unknown "
Mark Rutlandf12482c2011-06-22 15:30:51 +010033 "device %d\n", type);
Will Deacon49c006b2010-04-29 17:13:24 +010034 return -EINVAL;
35 }
36
Mark Rutlandae0c3752011-06-22 15:32:48 +010037 if (pmu_devices[type]) {
38 pr_warning("rejecting duplicate registration of PMU device "
39 "type %d.", type);
40 return -ENOSPC;
41 }
Will Deacon49c006b2010-04-29 17:13:24 +010042
Mark Rutlandae0c3752011-06-22 15:32:48 +010043 pr_info("registered new PMU device of type %d\n", type);
Mark Rutlandf12482c2011-06-22 15:30:51 +010044 pmu_devices[type] = pdev;
Will Deacon49c006b2010-04-29 17:13:24 +010045 return 0;
46}
47
Mark Rutlandf12482c2011-06-22 15:30:51 +010048static int __devinit armpmu_device_probe(struct platform_device *pdev)
49{
50 return pmu_register(pdev, ARM_PMU_DEVICE_CPU);
51}
52
53static struct platform_driver armpmu_driver = {
Will Deacon49c006b2010-04-29 17:13:24 +010054 .driver = {
55 .name = "arm-pmu",
56 },
Mark Rutlandf12482c2011-06-22 15:30:51 +010057 .probe = armpmu_device_probe,
Will Deacon49c006b2010-04-29 17:13:24 +010058};
59
60static int __init register_pmu_driver(void)
61{
Mark Rutlandf12482c2011-06-22 15:30:51 +010062 return platform_driver_register(&armpmu_driver);
Will Deacon49c006b2010-04-29 17:13:24 +010063}
64device_initcall(register_pmu_driver);
65
66struct platform_device *
67reserve_pmu(enum arm_pmu_type device)
68{
69 struct platform_device *pdev;
70
71 if (test_and_set_bit_lock(device, &pmu_lock)) {
72 pdev = ERR_PTR(-EBUSY);
73 } else if (pmu_devices[device] == NULL) {
74 clear_bit_unlock(device, &pmu_lock);
75 pdev = ERR_PTR(-ENODEV);
76 } else {
77 pdev = pmu_devices[device];
78 }
79
80 return pdev;
Jamie Iles0f4f0672010-02-02 20:23:15 +010081}
82EXPORT_SYMBOL_GPL(reserve_pmu);
83
84int
Mark Rutlandf12482c2011-06-22 15:30:51 +010085release_pmu(enum arm_pmu_type device)
Jamie Iles0f4f0672010-02-02 20:23:15 +010086{
Mark Rutlandf12482c2011-06-22 15:30:51 +010087 if (WARN_ON(!pmu_devices[device]))
Jamie Iles0f4f0672010-02-02 20:23:15 +010088 return -EINVAL;
Mark Rutlandf12482c2011-06-22 15:30:51 +010089 clear_bit_unlock(device, &pmu_lock);
Jamie Iles0f4f0672010-02-02 20:23:15 +010090 return 0;
91}
92EXPORT_SYMBOL_GPL(release_pmu);
93
94static int
95set_irq_affinity(int irq,
96 unsigned int cpu)
97{
98#ifdef CONFIG_SMP
99 int err = irq_set_affinity(irq, cpumask_of(cpu));
100 if (err)
101 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
102 irq, cpu);
103 return err;
104#else
Will Deacon71efb062011-02-18 16:21:06 +0100105 return -EINVAL;
Jamie Iles0f4f0672010-02-02 20:23:15 +0100106#endif
107}
108
Will Deacon49c006b2010-04-29 17:13:24 +0100109static int
110init_cpu_pmu(void)
Jamie Iles0f4f0672010-02-02 20:23:15 +0100111{
Will Deacon71efb062011-02-18 16:21:06 +0100112 int i, irqs, err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100113 struct platform_device *pdev = pmu_devices[ARM_PMU_DEVICE_CPU];
Jamie Iles0f4f0672010-02-02 20:23:15 +0100114
Will Deacon71efb062011-02-18 16:21:06 +0100115 if (!pdev)
116 return -ENODEV;
Will Deacon49c006b2010-04-29 17:13:24 +0100117
Will Deacon71efb062011-02-18 16:21:06 +0100118 irqs = pdev->num_resources;
119
120 /*
121 * If we have a single PMU interrupt that we can't shift, assume that
122 * we're running on a uniprocessor machine and continue.
123 */
124 if (irqs == 1 && !irq_can_set_affinity(platform_get_irq(pdev, 0)))
125 return 0;
126
127 for (i = 0; i < irqs; ++i) {
Will Deacon49c006b2010-04-29 17:13:24 +0100128 err = set_irq_affinity(platform_get_irq(pdev, i), i);
Jamie Iles0f4f0672010-02-02 20:23:15 +0100129 if (err)
130 break;
131 }
132
Will Deacon49c006b2010-04-29 17:13:24 +0100133 return err;
134}
135
136int
137init_pmu(enum arm_pmu_type device)
138{
139 int err = 0;
140
141 switch (device) {
142 case ARM_PMU_DEVICE_CPU:
143 err = init_cpu_pmu();
144 break;
145 default:
146 pr_warning("attempt to initialise unknown device %d\n",
147 device);
148 err = -EINVAL;
149 }
150
Jamie Iles0f4f0672010-02-02 20:23:15 +0100151 return err;
152}
153EXPORT_SYMBOL_GPL(init_pmu);