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> |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 21 | |
| 22 | #include <asm/pmu.h> |
| 23 | |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 24 | static volatile long pmu_lock; |
| 25 | |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 26 | static struct platform_device *pmu_devices[ARM_NUM_PMU_DEVICES]; |
| 27 | |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 28 | static int __devinit pmu_register(struct platform_device *pdev, |
| 29 | enum arm_pmu_type type) |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 30 | { |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 31 | if (type < 0 || type >= ARM_NUM_PMU_DEVICES) { |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 32 | pr_warning("received registration request for unknown " |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 33 | "device %d\n", type); |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 34 | return -EINVAL; |
| 35 | } |
| 36 | |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 37 | if (pmu_devices[type]) |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 38 | pr_warning("registering new PMU device type %d overwrites " |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 39 | "previous registration!\n", type); |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 40 | else |
| 41 | pr_info("registered new PMU device of type %d\n", |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 42 | type); |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 43 | |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 44 | pmu_devices[type] = pdev; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 48 | static int __devinit armpmu_device_probe(struct platform_device *pdev) |
| 49 | { |
| 50 | return pmu_register(pdev, ARM_PMU_DEVICE_CPU); |
| 51 | } |
| 52 | |
| 53 | static struct platform_driver armpmu_driver = { |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 54 | .driver = { |
| 55 | .name = "arm-pmu", |
| 56 | }, |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 57 | .probe = armpmu_device_probe, |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static int __init register_pmu_driver(void) |
| 61 | { |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 62 | return platform_driver_register(&armpmu_driver); |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 63 | } |
| 64 | device_initcall(register_pmu_driver); |
| 65 | |
| 66 | struct platform_device * |
| 67 | reserve_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 Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 81 | } |
| 82 | EXPORT_SYMBOL_GPL(reserve_pmu); |
| 83 | |
| 84 | int |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 85 | release_pmu(enum arm_pmu_type device) |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 86 | { |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 87 | if (WARN_ON(!pmu_devices[device])) |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 88 | return -EINVAL; |
Mark Rutland | f12482c | 2011-06-22 15:30:51 +0100 | [diff] [blame^] | 89 | clear_bit_unlock(device, &pmu_lock); |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(release_pmu); |
| 93 | |
| 94 | static int |
| 95 | set_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 Deacon | 71efb06 | 2011-02-18 16:21:06 +0100 | [diff] [blame] | 105 | return -EINVAL; |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 106 | #endif |
| 107 | } |
| 108 | |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 109 | static int |
| 110 | init_cpu_pmu(void) |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 111 | { |
Will Deacon | 71efb06 | 2011-02-18 16:21:06 +0100 | [diff] [blame] | 112 | int i, irqs, err = 0; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 113 | struct platform_device *pdev = pmu_devices[ARM_PMU_DEVICE_CPU]; |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 114 | |
Will Deacon | 71efb06 | 2011-02-18 16:21:06 +0100 | [diff] [blame] | 115 | if (!pdev) |
| 116 | return -ENODEV; |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 117 | |
Will Deacon | 71efb06 | 2011-02-18 16:21:06 +0100 | [diff] [blame] | 118 | 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 Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 128 | err = set_irq_affinity(platform_get_irq(pdev, i), i); |
Jamie Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 129 | if (err) |
| 130 | break; |
| 131 | } |
| 132 | |
Will Deacon | 49c006b | 2010-04-29 17:13:24 +0100 | [diff] [blame] | 133 | return err; |
| 134 | } |
| 135 | |
| 136 | int |
| 137 | init_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 Iles | 0f4f067 | 2010-02-02 20:23:15 +0100 | [diff] [blame] | 151 | return err; |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(init_pmu); |