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