Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 1 | /* |
| 2 | * RackMac vu-meter driver |
| 3 | * |
| 4 | * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp. |
| 5 | * <benh@kernel.crashing.org> |
| 6 | * |
| 7 | * Released under the term of the GNU GPL v2. |
| 8 | * |
| 9 | * Support the CPU-meter LEDs of the Xserve G5 |
| 10 | * |
| 11 | * TODO: Implement PWM to do variable intensity and provide userland |
| 12 | * interface for fun. Also, the CPU-meter could be made nicer by being |
| 13 | * a bit less "immediate" but giving instead a more average load over |
| 14 | * time. Patches welcome :-) |
| 15 | * |
| 16 | */ |
| 17 | #undef DEBUG |
| 18 | |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 22 | #include <linux/device.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/pci.h> |
| 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/kernel_stat.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 28 | #include <linux/of_address.h> |
| 29 | #include <linux/of_irq.h> |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 30 | |
| 31 | #include <asm/io.h> |
| 32 | #include <asm/prom.h> |
| 33 | #include <asm/machdep.h> |
| 34 | #include <asm/pmac_feature.h> |
| 35 | #include <asm/dbdma.h> |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 36 | #include <asm/macio.h> |
| 37 | #include <asm/keylargo.h> |
| 38 | |
| 39 | /* Number of samples in a sample buffer */ |
| 40 | #define SAMPLE_COUNT 256 |
| 41 | |
| 42 | /* CPU meter sampling rate in ms */ |
| 43 | #define CPU_SAMPLING_RATE 250 |
| 44 | |
| 45 | struct rackmeter_dma { |
| 46 | struct dbdma_cmd cmd[4] ____cacheline_aligned; |
| 47 | u32 mark ____cacheline_aligned; |
| 48 | u32 buf1[SAMPLE_COUNT] ____cacheline_aligned; |
| 49 | u32 buf2[SAMPLE_COUNT] ____cacheline_aligned; |
| 50 | } ____cacheline_aligned; |
| 51 | |
| 52 | struct rackmeter_cpu { |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 53 | struct delayed_work sniffer; |
| 54 | struct rackmeter *rm; |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 55 | cputime64_t prev_wall; |
| 56 | cputime64_t prev_idle; |
| 57 | int zero; |
| 58 | } ____cacheline_aligned; |
| 59 | |
| 60 | struct rackmeter { |
| 61 | struct macio_dev *mdev; |
| 62 | unsigned int irq; |
| 63 | struct device_node *i2s; |
| 64 | u8 *ubuf; |
| 65 | struct dbdma_regs __iomem *dma_regs; |
| 66 | void __iomem *i2s_regs; |
| 67 | dma_addr_t dma_buf_p; |
| 68 | struct rackmeter_dma *dma_buf_v; |
| 69 | int stale_irq; |
| 70 | struct rackmeter_cpu cpu[2]; |
| 71 | int paused; |
| 72 | struct mutex sem; |
| 73 | }; |
| 74 | |
| 75 | /* To be set as a tunable */ |
| 76 | static int rackmeter_ignore_nice; |
| 77 | |
| 78 | /* This GPIO is whacked by the OS X driver when initializing */ |
| 79 | #define RACKMETER_MAGIC_GPIO 0x78 |
| 80 | |
| 81 | /* This is copied from cpufreq_ondemand, maybe we should put it in |
| 82 | * a common header somewhere |
| 83 | */ |
| 84 | static inline cputime64_t get_cpu_idle_time(unsigned int cpu) |
| 85 | { |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 86 | u64 retval; |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 87 | |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 88 | retval = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE] + |
| 89 | kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT]; |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 90 | |
| 91 | if (rackmeter_ignore_nice) |
Glauber Costa | 3292beb | 2011-11-28 14:45:17 -0200 | [diff] [blame] | 92 | retval += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 93 | |
| 94 | return retval; |
| 95 | } |
| 96 | |
| 97 | static void rackmeter_setup_i2s(struct rackmeter *rm) |
| 98 | { |
| 99 | struct macio_chip *macio = rm->mdev->bus->chip; |
| 100 | |
| 101 | /* First whack magic GPIO */ |
| 102 | pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, RACKMETER_MAGIC_GPIO, 5); |
| 103 | |
| 104 | |
| 105 | /* Call feature code to enable the sound channel and the proper |
| 106 | * clock sources |
| 107 | */ |
| 108 | pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, rm->i2s, 0, 1); |
| 109 | |
| 110 | /* Power i2s and stop i2s clock. We whack MacIO FCRs directly for now. |
| 111 | * This is a bit racy, thus we should add new platform functions to |
| 112 | * handle that. snd-aoa needs that too |
| 113 | */ |
| 114 | MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_ENABLE); |
| 115 | MACIO_BIC(KEYLARGO_FCR1, KL1_I2S0_CLK_ENABLE_BIT); |
| 116 | (void)MACIO_IN32(KEYLARGO_FCR1); |
| 117 | udelay(10); |
| 118 | |
| 119 | /* Then setup i2s. For now, we use the same magic value that |
| 120 | * the OS X driver seems to use. We might want to play around |
| 121 | * with the clock divisors later |
| 122 | */ |
| 123 | out_le32(rm->i2s_regs + 0x10, 0x01fa0000); |
| 124 | (void)in_le32(rm->i2s_regs + 0x10); |
| 125 | udelay(10); |
| 126 | |
| 127 | /* Fully restart i2s*/ |
| 128 | MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_CELL_ENABLE | |
| 129 | KL1_I2S0_CLK_ENABLE_BIT); |
| 130 | (void)MACIO_IN32(KEYLARGO_FCR1); |
| 131 | udelay(10); |
| 132 | } |
| 133 | |
| 134 | static void rackmeter_set_default_pattern(struct rackmeter *rm) |
| 135 | { |
| 136 | int i; |
| 137 | |
| 138 | for (i = 0; i < 16; i++) { |
| 139 | if (i < 8) |
| 140 | rm->ubuf[i] = (i & 1) * 255; |
| 141 | else |
| 142 | rm->ubuf[i] = ((~i) & 1) * 255; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static void rackmeter_do_pause(struct rackmeter *rm, int pause) |
| 147 | { |
| 148 | struct rackmeter_dma *rdma = rm->dma_buf_v; |
| 149 | |
| 150 | pr_debug("rackmeter: %s\n", pause ? "paused" : "started"); |
| 151 | |
| 152 | rm->paused = pause; |
| 153 | if (pause) { |
| 154 | DBDMA_DO_STOP(rm->dma_regs); |
| 155 | return; |
| 156 | } |
Aaro Koskinen | 4f7bef7 | 2016-04-10 22:53:48 +0300 | [diff] [blame^] | 157 | memset(rdma->buf1, 0, ARRAY_SIZE(rdma->buf1)); |
| 158 | memset(rdma->buf2, 0, ARRAY_SIZE(rdma->buf2)); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 159 | |
| 160 | rm->dma_buf_v->mark = 0; |
| 161 | |
| 162 | mb(); |
| 163 | out_le32(&rm->dma_regs->cmdptr_hi, 0); |
| 164 | out_le32(&rm->dma_regs->cmdptr, rm->dma_buf_p); |
| 165 | out_le32(&rm->dma_regs->control, (RUN << 16) | RUN); |
| 166 | } |
| 167 | |
| 168 | static void rackmeter_setup_dbdma(struct rackmeter *rm) |
| 169 | { |
| 170 | struct rackmeter_dma *db = rm->dma_buf_v; |
| 171 | struct dbdma_cmd *cmd = db->cmd; |
| 172 | |
| 173 | /* Make sure dbdma is reset */ |
| 174 | DBDMA_DO_RESET(rm->dma_regs); |
| 175 | |
Al Viro | 193d073 | 2007-02-09 16:39:05 +0000 | [diff] [blame] | 176 | pr_debug("rackmeter: mark offset=0x%zx\n", |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 177 | offsetof(struct rackmeter_dma, mark)); |
Al Viro | 193d073 | 2007-02-09 16:39:05 +0000 | [diff] [blame] | 178 | pr_debug("rackmeter: buf1 offset=0x%zx\n", |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 179 | offsetof(struct rackmeter_dma, buf1)); |
Al Viro | 193d073 | 2007-02-09 16:39:05 +0000 | [diff] [blame] | 180 | pr_debug("rackmeter: buf2 offset=0x%zx\n", |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 181 | offsetof(struct rackmeter_dma, buf2)); |
| 182 | |
| 183 | /* Prepare 4 dbdma commands for the 2 buffers */ |
| 184 | memset(cmd, 0, 4 * sizeof(struct dbdma_cmd)); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 185 | cmd->req_count = cpu_to_le16(4); |
| 186 | cmd->command = cpu_to_le16(STORE_WORD | INTR_ALWAYS | KEY_SYSTEM); |
| 187 | cmd->phy_addr = cpu_to_le32(rm->dma_buf_p + |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 188 | offsetof(struct rackmeter_dma, mark)); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 189 | cmd->cmd_dep = cpu_to_le32(0x02000000); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 190 | cmd++; |
| 191 | |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 192 | cmd->req_count = cpu_to_le16(SAMPLE_COUNT * 4); |
| 193 | cmd->command = cpu_to_le16(OUTPUT_MORE); |
| 194 | cmd->phy_addr = cpu_to_le32(rm->dma_buf_p + |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 195 | offsetof(struct rackmeter_dma, buf1)); |
| 196 | cmd++; |
| 197 | |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 198 | cmd->req_count = cpu_to_le16(4); |
| 199 | cmd->command = cpu_to_le16(STORE_WORD | INTR_ALWAYS | KEY_SYSTEM); |
| 200 | cmd->phy_addr = cpu_to_le32(rm->dma_buf_p + |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 201 | offsetof(struct rackmeter_dma, mark)); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 202 | cmd->cmd_dep = cpu_to_le32(0x01000000); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 203 | cmd++; |
| 204 | |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 205 | cmd->req_count = cpu_to_le16(SAMPLE_COUNT * 4); |
| 206 | cmd->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS); |
| 207 | cmd->phy_addr = cpu_to_le32(rm->dma_buf_p + |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 208 | offsetof(struct rackmeter_dma, buf2)); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 209 | cmd->cmd_dep = cpu_to_le32(rm->dma_buf_p); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 210 | |
| 211 | rackmeter_do_pause(rm, 0); |
| 212 | } |
| 213 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 214 | static void rackmeter_do_timer(struct work_struct *work) |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 215 | { |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 216 | struct rackmeter_cpu *rcpu = |
| 217 | container_of(work, struct rackmeter_cpu, sniffer.work); |
| 218 | struct rackmeter *rm = rcpu->rm; |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 219 | unsigned int cpu = smp_processor_id(); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 220 | cputime64_t cur_jiffies, total_idle_ticks; |
| 221 | unsigned int total_ticks, idle_ticks; |
| 222 | int i, offset, load, cumm, pause; |
| 223 | |
| 224 | cur_jiffies = jiffies64_to_cputime64(get_jiffies_64()); |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 225 | total_ticks = (unsigned int) (cur_jiffies - rcpu->prev_wall); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 226 | rcpu->prev_wall = cur_jiffies; |
| 227 | |
| 228 | total_idle_ticks = get_cpu_idle_time(cpu); |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 229 | idle_ticks = (unsigned int) (total_idle_ticks - rcpu->prev_idle); |
Aaro Koskinen | c796d1d9 | 2016-04-10 22:53:47 +0300 | [diff] [blame] | 230 | idle_ticks = min(idle_ticks, total_ticks); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 231 | rcpu->prev_idle = total_idle_ticks; |
| 232 | |
| 233 | /* We do a very dumb calculation to update the LEDs for now, |
| 234 | * we'll do better once we have actual PWM implemented |
| 235 | */ |
| 236 | load = (9 * (total_ticks - idle_ticks)) / total_ticks; |
| 237 | |
| 238 | offset = cpu << 3; |
| 239 | cumm = 0; |
| 240 | for (i = 0; i < 8; i++) { |
| 241 | u8 ub = (load > i) ? 0xff : 0; |
| 242 | rm->ubuf[i + offset] = ub; |
| 243 | cumm |= ub; |
| 244 | } |
| 245 | rcpu->zero = (cumm == 0); |
| 246 | |
| 247 | /* Now check if LEDs are all 0, we can stop DMA */ |
| 248 | pause = (rm->cpu[0].zero && rm->cpu[1].zero); |
| 249 | if (pause != rm->paused) { |
| 250 | mutex_lock(&rm->sem); |
| 251 | pause = (rm->cpu[0].zero && rm->cpu[1].zero); |
| 252 | rackmeter_do_pause(rm, pause); |
| 253 | mutex_unlock(&rm->sem); |
| 254 | } |
| 255 | schedule_delayed_work_on(cpu, &rcpu->sniffer, |
| 256 | msecs_to_jiffies(CPU_SAMPLING_RATE)); |
| 257 | } |
| 258 | |
Greg Kroah-Hartman | 1da42fb | 2012-12-21 15:03:50 -0800 | [diff] [blame] | 259 | static void rackmeter_init_cpu_sniffer(struct rackmeter *rm) |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 260 | { |
| 261 | unsigned int cpu; |
| 262 | |
| 263 | /* This driver works only with 1 or 2 CPUs numbered 0 and 1, |
| 264 | * but that's really all we have on Apple Xserve. It doesn't |
| 265 | * play very nice with CPU hotplug neither but we don't do that |
| 266 | * on those machines yet |
| 267 | */ |
| 268 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 269 | rm->cpu[0].rm = rm; |
| 270 | INIT_DELAYED_WORK(&rm->cpu[0].sniffer, rackmeter_do_timer); |
| 271 | rm->cpu[1].rm = rm; |
| 272 | INIT_DELAYED_WORK(&rm->cpu[1].sniffer, rackmeter_do_timer); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 273 | |
| 274 | for_each_online_cpu(cpu) { |
| 275 | struct rackmeter_cpu *rcpu; |
| 276 | |
| 277 | if (cpu > 1) |
| 278 | continue; |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 279 | rcpu = &rm->cpu[cpu]; |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 280 | rcpu->prev_idle = get_cpu_idle_time(cpu); |
| 281 | rcpu->prev_wall = jiffies64_to_cputime64(get_jiffies_64()); |
| 282 | schedule_delayed_work_on(cpu, &rm->cpu[cpu].sniffer, |
| 283 | msecs_to_jiffies(CPU_SAMPLING_RATE)); |
| 284 | } |
| 285 | } |
| 286 | |
Grant Likely | e1f4dea | 2011-02-28 09:59:31 +0000 | [diff] [blame] | 287 | static void rackmeter_stop_cpu_sniffer(struct rackmeter *rm) |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 288 | { |
Tejun Heo | afe2c51 | 2010-12-14 16:21:17 +0100 | [diff] [blame] | 289 | cancel_delayed_work_sync(&rm->cpu[0].sniffer); |
| 290 | cancel_delayed_work_sync(&rm->cpu[1].sniffer); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 291 | } |
| 292 | |
Greg Kroah-Hartman | 1da42fb | 2012-12-21 15:03:50 -0800 | [diff] [blame] | 293 | static int rackmeter_setup(struct rackmeter *rm) |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 294 | { |
| 295 | pr_debug("rackmeter: setting up i2s..\n"); |
| 296 | rackmeter_setup_i2s(rm); |
| 297 | |
| 298 | pr_debug("rackmeter: setting up default pattern..\n"); |
| 299 | rackmeter_set_default_pattern(rm); |
| 300 | |
| 301 | pr_debug("rackmeter: setting up dbdma..\n"); |
| 302 | rackmeter_setup_dbdma(rm); |
| 303 | |
| 304 | pr_debug("rackmeter: start CPU measurements..\n"); |
| 305 | rackmeter_init_cpu_sniffer(rm); |
| 306 | |
| 307 | printk(KERN_INFO "RackMeter initialized\n"); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* XXX FIXME: No PWM yet, this is 0/1 */ |
| 313 | static u32 rackmeter_calc_sample(struct rackmeter *rm, unsigned int index) |
| 314 | { |
| 315 | int led; |
| 316 | u32 sample = 0; |
| 317 | |
| 318 | for (led = 0; led < 16; led++) { |
| 319 | sample >>= 1; |
| 320 | sample |= ((rm->ubuf[led] >= 0x80) << 15); |
| 321 | } |
| 322 | return (sample << 17) | (sample >> 15); |
| 323 | } |
| 324 | |
| 325 | static irqreturn_t rackmeter_irq(int irq, void *arg) |
| 326 | { |
| 327 | struct rackmeter *rm = arg; |
| 328 | struct rackmeter_dma *db = rm->dma_buf_v; |
| 329 | unsigned int mark, i; |
| 330 | u32 *buf; |
| 331 | |
| 332 | /* Flush PCI buffers with an MMIO read. Maybe we could actually |
| 333 | * check the status one day ... in case things go wrong, though |
| 334 | * this never happened to me |
| 335 | */ |
| 336 | (void)in_le32(&rm->dma_regs->status); |
| 337 | |
| 338 | /* Make sure the CPU gets us in order */ |
| 339 | rmb(); |
| 340 | |
| 341 | /* Read mark */ |
| 342 | mark = db->mark; |
| 343 | if (mark != 1 && mark != 2) { |
| 344 | printk(KERN_WARNING "rackmeter: Incorrect DMA mark 0x%08x\n", |
| 345 | mark); |
| 346 | /* We allow for 3 errors like that (stale DBDMA irqs) */ |
| 347 | if (++rm->stale_irq > 3) { |
| 348 | printk(KERN_ERR "rackmeter: Too many errors," |
| 349 | " stopping DMA\n"); |
| 350 | DBDMA_DO_RESET(rm->dma_regs); |
| 351 | } |
| 352 | return IRQ_HANDLED; |
| 353 | } |
| 354 | |
| 355 | /* Next buffer we need to fill is mark value */ |
| 356 | buf = mark == 1 ? db->buf1 : db->buf2; |
| 357 | |
| 358 | /* Fill it now. This routine converts the 8 bits depth sample array |
| 359 | * into the PWM bitmap for each LED. |
| 360 | */ |
| 361 | for (i = 0; i < SAMPLE_COUNT; i++) |
| 362 | buf[i] = rackmeter_calc_sample(rm, i); |
| 363 | |
| 364 | |
| 365 | return IRQ_HANDLED; |
| 366 | } |
| 367 | |
Greg Kroah-Hartman | 1da42fb | 2012-12-21 15:03:50 -0800 | [diff] [blame] | 368 | static int rackmeter_probe(struct macio_dev* mdev, |
| 369 | const struct of_device_id *match) |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 370 | { |
| 371 | struct device_node *i2s = NULL, *np = NULL; |
| 372 | struct rackmeter *rm = NULL; |
| 373 | struct resource ri2s, rdma; |
| 374 | int rc = -ENODEV; |
| 375 | |
| 376 | pr_debug("rackmeter_probe()\n"); |
| 377 | |
| 378 | /* Get i2s-a node */ |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 379 | while ((i2s = of_get_next_child(mdev->ofdev.dev.of_node, i2s)) != NULL) |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 380 | if (strcmp(i2s->name, "i2s-a") == 0) |
| 381 | break; |
| 382 | if (i2s == NULL) { |
| 383 | pr_debug(" i2s-a child not found\n"); |
| 384 | goto bail; |
| 385 | } |
| 386 | /* Get lightshow or virtual sound */ |
| 387 | while ((np = of_get_next_child(i2s, np)) != NULL) { |
| 388 | if (strcmp(np->name, "lightshow") == 0) |
| 389 | break; |
| 390 | if ((strcmp(np->name, "sound") == 0) && |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 391 | of_get_property(np, "virtual", NULL) != NULL) |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | if (np == NULL) { |
| 395 | pr_debug(" lightshow or sound+virtual child not found\n"); |
| 396 | goto bail; |
| 397 | } |
| 398 | |
| 399 | /* Create and initialize our instance data */ |
| 400 | rm = kzalloc(sizeof(struct rackmeter), GFP_KERNEL); |
| 401 | if (rm == NULL) { |
| 402 | printk(KERN_ERR "rackmeter: failed to allocate memory !\n"); |
| 403 | rc = -ENOMEM; |
| 404 | goto bail_release; |
| 405 | } |
| 406 | rm->mdev = mdev; |
| 407 | rm->i2s = i2s; |
| 408 | mutex_init(&rm->sem); |
| 409 | dev_set_drvdata(&mdev->ofdev.dev, rm); |
| 410 | /* Check resources availability. We need at least resource 0 and 1 */ |
| 411 | #if 0 /* Use that when i2s-a is finally an mdev per-se */ |
| 412 | if (macio_resource_count(mdev) < 2 || macio_irq_count(mdev) < 2) { |
| 413 | printk(KERN_ERR |
| 414 | "rackmeter: found match but lacks resources: %s" |
| 415 | " (%d resources, %d interrupts)\n", |
| 416 | mdev->ofdev.node->full_name); |
| 417 | rc = -ENXIO; |
| 418 | goto bail_free; |
| 419 | } |
| 420 | if (macio_request_resources(mdev, "rackmeter")) { |
| 421 | printk(KERN_ERR |
| 422 | "rackmeter: failed to request resources: %s\n", |
| 423 | mdev->ofdev.node->full_name); |
| 424 | rc = -EBUSY; |
| 425 | goto bail_free; |
| 426 | } |
| 427 | rm->irq = macio_irq(mdev, 1); |
| 428 | #else |
| 429 | rm->irq = irq_of_parse_and_map(i2s, 1); |
| 430 | if (rm->irq == NO_IRQ || |
| 431 | of_address_to_resource(i2s, 0, &ri2s) || |
| 432 | of_address_to_resource(i2s, 1, &rdma)) { |
| 433 | printk(KERN_ERR |
| 434 | "rackmeter: found match but lacks resources: %s", |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 435 | mdev->ofdev.dev.of_node->full_name); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 436 | rc = -ENXIO; |
| 437 | goto bail_free; |
| 438 | } |
| 439 | #endif |
| 440 | |
| 441 | pr_debug(" i2s @0x%08x\n", (unsigned int)ri2s.start); |
| 442 | pr_debug(" dma @0x%08x\n", (unsigned int)rdma.start); |
| 443 | pr_debug(" irq %d\n", rm->irq); |
| 444 | |
| 445 | rm->ubuf = (u8 *)__get_free_page(GFP_KERNEL); |
| 446 | if (rm->ubuf == NULL) { |
| 447 | printk(KERN_ERR |
| 448 | "rackmeter: failed to allocate samples page !\n"); |
| 449 | rc = -ENOMEM; |
| 450 | goto bail_release; |
| 451 | } |
| 452 | |
| 453 | rm->dma_buf_v = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev, |
| 454 | sizeof(struct rackmeter_dma), |
| 455 | &rm->dma_buf_p, GFP_KERNEL); |
| 456 | if (rm->dma_buf_v == NULL) { |
| 457 | printk(KERN_ERR |
| 458 | "rackmeter: failed to allocate dma buffer !\n"); |
| 459 | rc = -ENOMEM; |
| 460 | goto bail_free_samples; |
| 461 | } |
| 462 | #if 0 |
| 463 | rm->i2s_regs = ioremap(macio_resource_start(mdev, 0), 0x1000); |
| 464 | #else |
| 465 | rm->i2s_regs = ioremap(ri2s.start, 0x1000); |
| 466 | #endif |
| 467 | if (rm->i2s_regs == NULL) { |
| 468 | printk(KERN_ERR |
| 469 | "rackmeter: failed to map i2s registers !\n"); |
| 470 | rc = -ENXIO; |
| 471 | goto bail_free_dma; |
| 472 | } |
| 473 | #if 0 |
| 474 | rm->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x100); |
| 475 | #else |
| 476 | rm->dma_regs = ioremap(rdma.start, 0x100); |
| 477 | #endif |
| 478 | if (rm->dma_regs == NULL) { |
| 479 | printk(KERN_ERR |
| 480 | "rackmeter: failed to map dma registers !\n"); |
| 481 | rc = -ENXIO; |
| 482 | goto bail_unmap_i2s; |
| 483 | } |
| 484 | |
| 485 | rc = rackmeter_setup(rm); |
| 486 | if (rc) { |
| 487 | printk(KERN_ERR |
| 488 | "rackmeter: failed to initialize !\n"); |
| 489 | rc = -ENXIO; |
| 490 | goto bail_unmap_dma; |
| 491 | } |
| 492 | |
| 493 | rc = request_irq(rm->irq, rackmeter_irq, 0, "rackmeter", rm); |
| 494 | if (rc != 0) { |
| 495 | printk(KERN_ERR |
| 496 | "rackmeter: failed to request interrupt !\n"); |
| 497 | goto bail_stop_dma; |
| 498 | } |
| 499 | of_node_put(np); |
| 500 | return 0; |
| 501 | |
| 502 | bail_stop_dma: |
| 503 | DBDMA_DO_RESET(rm->dma_regs); |
| 504 | bail_unmap_dma: |
| 505 | iounmap(rm->dma_regs); |
| 506 | bail_unmap_i2s: |
| 507 | iounmap(rm->i2s_regs); |
| 508 | bail_free_dma: |
| 509 | dma_free_coherent(&macio_get_pci_dev(mdev)->dev, |
| 510 | sizeof(struct rackmeter_dma), |
| 511 | rm->dma_buf_v, rm->dma_buf_p); |
| 512 | bail_free_samples: |
| 513 | free_page((unsigned long)rm->ubuf); |
| 514 | bail_release: |
| 515 | #if 0 |
| 516 | macio_release_resources(mdev); |
| 517 | #endif |
| 518 | bail_free: |
| 519 | kfree(rm); |
| 520 | bail: |
| 521 | of_node_put(i2s); |
| 522 | of_node_put(np); |
| 523 | dev_set_drvdata(&mdev->ofdev.dev, NULL); |
| 524 | return rc; |
| 525 | } |
| 526 | |
Greg Kroah-Hartman | 1da42fb | 2012-12-21 15:03:50 -0800 | [diff] [blame] | 527 | static int rackmeter_remove(struct macio_dev* mdev) |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 528 | { |
| 529 | struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev); |
| 530 | |
| 531 | /* Stop CPU sniffer timer & work queues */ |
| 532 | rackmeter_stop_cpu_sniffer(rm); |
| 533 | |
| 534 | /* Clear reference to private data */ |
| 535 | dev_set_drvdata(&mdev->ofdev.dev, NULL); |
| 536 | |
| 537 | /* Stop/reset dbdma */ |
| 538 | DBDMA_DO_RESET(rm->dma_regs); |
| 539 | |
| 540 | /* Release the IRQ */ |
| 541 | free_irq(rm->irq, rm); |
| 542 | |
| 543 | /* Unmap registers */ |
| 544 | iounmap(rm->dma_regs); |
| 545 | iounmap(rm->i2s_regs); |
| 546 | |
| 547 | /* Free DMA */ |
| 548 | dma_free_coherent(&macio_get_pci_dev(mdev)->dev, |
| 549 | sizeof(struct rackmeter_dma), |
| 550 | rm->dma_buf_v, rm->dma_buf_p); |
| 551 | |
| 552 | /* Free samples */ |
| 553 | free_page((unsigned long)rm->ubuf); |
| 554 | |
| 555 | #if 0 |
| 556 | /* Release resources */ |
| 557 | macio_release_resources(mdev); |
| 558 | #endif |
| 559 | |
| 560 | /* Get rid of me */ |
| 561 | kfree(rm); |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static int rackmeter_shutdown(struct macio_dev* mdev) |
| 567 | { |
| 568 | struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev); |
| 569 | |
| 570 | if (rm == NULL) |
| 571 | return -ENODEV; |
| 572 | |
| 573 | /* Stop CPU sniffer timer & work queues */ |
| 574 | rackmeter_stop_cpu_sniffer(rm); |
| 575 | |
| 576 | /* Stop/reset dbdma */ |
| 577 | DBDMA_DO_RESET(rm->dma_regs); |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static struct of_device_id rackmeter_match[] = { |
| 583 | { .name = "i2s" }, |
| 584 | { } |
| 585 | }; |
Luis de Bethencourt | 6735b2e | 2015-10-20 16:13:53 +0100 | [diff] [blame] | 586 | MODULE_DEVICE_TABLE(of, rackmeter_match); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 587 | |
Al Viro | 31421a6 | 2008-11-22 17:35:24 +0000 | [diff] [blame] | 588 | static struct macio_driver rackmeter_driver = { |
Benjamin Herrenschmidt | c2cdf6a | 2010-06-02 17:09:18 +1000 | [diff] [blame] | 589 | .driver = { |
| 590 | .name = "rackmeter", |
| 591 | .owner = THIS_MODULE, |
| 592 | .of_match_table = rackmeter_match, |
| 593 | }, |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 594 | .probe = rackmeter_probe, |
Greg Kroah-Hartman | 1da42fb | 2012-12-21 15:03:50 -0800 | [diff] [blame] | 595 | .remove = rackmeter_remove, |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 596 | .shutdown = rackmeter_shutdown, |
| 597 | }; |
| 598 | |
| 599 | |
| 600 | static int __init rackmeter_init(void) |
| 601 | { |
| 602 | pr_debug("rackmeter_init()\n"); |
| 603 | |
Al Viro | 31421a6 | 2008-11-22 17:35:24 +0000 | [diff] [blame] | 604 | return macio_register_driver(&rackmeter_driver); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void __exit rackmeter_exit(void) |
| 608 | { |
| 609 | pr_debug("rackmeter_exit()\n"); |
| 610 | |
Al Viro | 31421a6 | 2008-11-22 17:35:24 +0000 | [diff] [blame] | 611 | macio_unregister_driver(&rackmeter_driver); |
Benjamin Herrenschmidt | 3e00a5a | 2006-11-16 14:03:33 +1100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | module_init(rackmeter_init); |
| 615 | module_exit(rackmeter_exit); |
| 616 | |
| 617 | |
| 618 | MODULE_LICENSE("GPL"); |
| 619 | MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); |
| 620 | MODULE_DESCRIPTION("RackMeter: Support vu-meter on XServe front panel"); |