Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/kernel/pmu.c |
| 3 | * |
| 4 | * Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 5 | * Copyright (C) 2010 ARM Ltd, Will Deacon |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 6 | * |
| 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 Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 13 | #define pr_fmt(fmt) "PMU: " fmt |
| 14 | |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 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> |
Mark Rutland | e73c34c | 2011-06-22 15:33:55 +0100 | [diff] [blame] | 20 | #include <linux/of_device.h> |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 22 | |
| 23 | #include <asm/pmu.h> |
| 24 | |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 25 | static volatile long pmu_lock; |
| 26 | |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 27 | static struct platform_device *pmu_devices[ARM_NUM_PMU_DEVICES]; |
| 28 | |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame] | 29 | static int __devinit pmu_register(struct platform_device *pdev, |
| 30 | enum arm_pmu_type type) |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 31 | { |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame] | 32 | if (type < 0 || type >= ARM_NUM_PMU_DEVICES) { |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 33 | pr_warning("received registration request for unknown " |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 34 | "PMU device type %d\n", type); |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 35 | return -EINVAL; |
| 36 | } |
| 37 | |
Mark Rutland | ae0c375 | 2011-06-22 15:32:48 +0100 | [diff] [blame] | 38 | if (pmu_devices[type]) { |
| 39 | pr_warning("rejecting duplicate registration of PMU device " |
| 40 | "type %d.", type); |
| 41 | return -ENOSPC; |
| 42 | } |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 43 | |
Mark Rutland | ae0c375 | 2011-06-22 15:32:48 +0100 | [diff] [blame] | 44 | pr_info("registered new PMU device of type %d\n", type); |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame] | 45 | pmu_devices[type] = pdev; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Mark Rutland | e73c34c | 2011-06-22 15:33:55 +0100 | [diff] [blame] | 49 | #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 | |
| 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"), |
| 61 | {}, |
| 62 | }; |
| 63 | |
Mark Rutland | e4b6381 | 2011-06-22 15:34:56 +0100 | [diff] [blame] | 64 | #define PLAT_MATCH_PMU(_name, _type) { \ |
| 65 | .name = _name, \ |
| 66 | .driver_data = _type, \ |
| 67 | } |
| 68 | |
| 69 | #define PLAT_MATCH_CPU(_name) PLAT_MATCH_PMU(_name, ARM_PMU_DEVICE_CPU) |
| 70 | |
| 71 | static struct platform_device_id armpmu_plat_device_ids[] = { |
| 72 | PLAT_MATCH_CPU("arm-pmu"), |
| 73 | {}, |
| 74 | }; |
| 75 | |
Mark Rutland | e73c34c | 2011-06-22 15:33:55 +0100 | [diff] [blame] | 76 | enum arm_pmu_type armpmu_device_type(struct platform_device *pdev) |
| 77 | { |
| 78 | const struct of_device_id *of_id; |
Mark Rutland | e4b6381 | 2011-06-22 15:34:56 +0100 | [diff] [blame] | 79 | const struct platform_device_id *pdev_id; |
Mark Rutland | e73c34c | 2011-06-22 15:33:55 +0100 | [diff] [blame] | 80 | |
| 81 | /* provided by of_device_id table */ |
| 82 | if (pdev->dev.of_node) { |
| 83 | of_id = of_match_device(armpmu_of_device_ids, &pdev->dev); |
| 84 | BUG_ON(!of_id); |
| 85 | return (enum arm_pmu_type)of_id->data; |
| 86 | } |
| 87 | |
Mark Rutland | e4b6381 | 2011-06-22 15:34:56 +0100 | [diff] [blame] | 88 | /* Provided by platform_device_id table */ |
| 89 | pdev_id = platform_get_device_id(pdev); |
| 90 | BUG_ON(!pdev_id); |
| 91 | return pdev_id->driver_data; |
Mark Rutland | e73c34c | 2011-06-22 15:33:55 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame] | 94 | static int __devinit armpmu_device_probe(struct platform_device *pdev) |
| 95 | { |
Mark Rutland | e73c34c | 2011-06-22 15:33:55 +0100 | [diff] [blame] | 96 | return pmu_register(pdev, armpmu_device_type(pdev)); |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static struct platform_driver armpmu_driver = { |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 100 | .driver = { |
| 101 | .name = "arm-pmu", |
Mark Rutland | e73c34c | 2011-06-22 15:33:55 +0100 | [diff] [blame] | 102 | .of_match_table = armpmu_of_device_ids, |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 103 | }, |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame] | 104 | .probe = armpmu_device_probe, |
Mark Rutland | e4b6381 | 2011-06-22 15:34:56 +0100 | [diff] [blame] | 105 | .id_table = armpmu_plat_device_ids, |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static int __init register_pmu_driver(void) |
| 109 | { |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame] | 110 | return platform_driver_register(&armpmu_driver); |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 111 | } |
| 112 | device_initcall(register_pmu_driver); |
| 113 | |
| 114 | struct platform_device * |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 115 | reserve_pmu(enum arm_pmu_type type) |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 116 | { |
| 117 | struct platform_device *pdev; |
| 118 | |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 119 | if (test_and_set_bit_lock(type, &pmu_lock)) { |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 120 | pdev = ERR_PTR(-EBUSY); |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 121 | } else if (pmu_devices[type] == NULL) { |
| 122 | clear_bit_unlock(type, &pmu_lock); |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 123 | pdev = ERR_PTR(-ENODEV); |
| 124 | } else { |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 125 | pdev = pmu_devices[type]; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return pdev; |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 129 | } |
| 130 | EXPORT_SYMBOL_GPL(reserve_pmu); |
| 131 | |
| 132 | int |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 133 | release_pmu(enum arm_pmu_type type) |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 134 | { |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 135 | if (WARN_ON(!pmu_devices[type])) |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 136 | return -EINVAL; |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 137 | clear_bit_unlock(type, &pmu_lock); |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | EXPORT_SYMBOL_GPL(release_pmu); |
| 141 | |
| 142 | static int |
| 143 | set_irq_affinity(int irq, |
| 144 | unsigned int cpu) |
| 145 | { |
| 146 | #ifdef CONFIG_SMP |
| 147 | int err = irq_set_affinity(irq, cpumask_of(cpu)); |
| 148 | if (err) |
| 149 | pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n", |
| 150 | irq, cpu); |
| 151 | return err; |
| 152 | #else |
Will Deacon | 71efb06 | 2011-02-18 16:21:06 +0100 | [diff] [blame] | 153 | return -EINVAL; |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 154 | #endif |
| 155 | } |
| 156 | |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 157 | static int |
| 158 | init_cpu_pmu(void) |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 159 | { |
Will Deacon | 71efb06 | 2011-02-18 16:21:06 +0100 | [diff] [blame] | 160 | int i, irqs, err = 0; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 161 | struct platform_device *pdev = pmu_devices[ARM_PMU_DEVICE_CPU]; |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 162 | |
Will Deacon | 71efb06 | 2011-02-18 16:21:06 +0100 | [diff] [blame] | 163 | if (!pdev) |
| 164 | return -ENODEV; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 165 | |
Will Deacon | 71efb06 | 2011-02-18 16:21:06 +0100 | [diff] [blame] | 166 | irqs = pdev->num_resources; |
| 167 | |
| 168 | /* |
| 169 | * If we have a single PMU interrupt that we can't shift, assume that |
| 170 | * we're running on a uniprocessor machine and continue. |
| 171 | */ |
| 172 | if (irqs == 1 && !irq_can_set_affinity(platform_get_irq(pdev, 0))) |
| 173 | return 0; |
| 174 | |
| 175 | for (i = 0; i < irqs; ++i) { |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 176 | err = set_irq_affinity(platform_get_irq(pdev, i), i); |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 177 | if (err) |
| 178 | break; |
| 179 | } |
| 180 | |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 181 | return err; |
| 182 | } |
| 183 | |
| 184 | int |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 185 | init_pmu(enum arm_pmu_type type) |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 186 | { |
| 187 | int err = 0; |
| 188 | |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 189 | switch (type) { |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 190 | case ARM_PMU_DEVICE_CPU: |
| 191 | err = init_cpu_pmu(); |
| 192 | break; |
| 193 | default: |
Mark Rutland | 7fdd3c4 | 2011-08-12 10:42:48 +0100 | [diff] [blame^] | 194 | pr_warning("attempt to initialise PMU of unknown " |
| 195 | "type %d\n", type); |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 196 | err = -EINVAL; |
| 197 | } |
| 198 | |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 199 | return err; |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(init_pmu); |