2 * RackMac vu-meter driver
4 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
7 * Released under the term of the GNU GPL v2.
9 * Support the CPU-meter LEDs of the Xserve G5
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 :-)
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>
30 #include <asm/machdep.h>
31 #include <asm/pmac_feature.h>
32 #include <asm/dbdma.h>
33 #include <asm/macio.h>
34 #include <asm/keylargo.h>
36 /* Number of samples in a sample buffer */
37 #define SAMPLE_COUNT 256
39 /* CPU meter sampling rate in ms */
40 #define CPU_SAMPLING_RATE 250
42 struct 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
;
49 struct rackmeter_cpu
{
50 struct delayed_work sniffer
;
52 cputime64_t prev_wall
;
53 cputime64_t prev_idle
;
55 } ____cacheline_aligned
;
58 struct macio_dev
*mdev
;
60 struct device_node
*i2s
;
62 struct dbdma_regs __iomem
*dma_regs
;
63 void __iomem
*i2s_regs
;
65 struct rackmeter_dma
*dma_buf_v
;
67 struct rackmeter_cpu cpu
[2];
72 /* To be set as a tunable */
73 static int rackmeter_ignore_nice
;
75 /* This GPIO is whacked by the OS X driver when initializing */
76 #define RACKMETER_MAGIC_GPIO 0x78
78 /* This is copied from cpufreq_ondemand, maybe we should put it in
79 * a common header somewhere
81 static inline cputime64_t
get_cpu_idle_time(unsigned int cpu
)
85 retval
= cputime64_add(kstat_cpu(cpu
).cpustat
.idle
,
86 kstat_cpu(cpu
).cpustat
.iowait
);
88 if (rackmeter_ignore_nice
)
89 retval
= cputime64_add(retval
, kstat_cpu(cpu
).cpustat
.nice
);
94 static void rackmeter_setup_i2s(struct rackmeter
*rm
)
96 struct macio_chip
*macio
= rm
->mdev
->bus
->chip
;
98 /* First whack magic GPIO */
99 pmac_call_feature(PMAC_FTR_WRITE_GPIO
, NULL
, RACKMETER_MAGIC_GPIO
, 5);
102 /* Call feature code to enable the sound channel and the proper
105 pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE
, rm
->i2s
, 0, 1);
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
111 MACIO_BIS(KEYLARGO_FCR1
, KL1_I2S0_ENABLE
);
112 MACIO_BIC(KEYLARGO_FCR1
, KL1_I2S0_CLK_ENABLE_BIT
);
113 (void)MACIO_IN32(KEYLARGO_FCR1
);
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
120 out_le32(rm
->i2s_regs
+ 0x10, 0x01fa0000);
121 (void)in_le32(rm
->i2s_regs
+ 0x10);
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
);
131 static void rackmeter_set_default_pattern(struct rackmeter
*rm
)
135 for (i
= 0; i
< 16; i
++) {
137 rm
->ubuf
[i
] = (i
& 1) * 255;
139 rm
->ubuf
[i
] = ((~i
) & 1) * 255;
143 static void rackmeter_do_pause(struct rackmeter
*rm
, int pause
)
145 struct rackmeter_dma
*rdma
= rm
->dma_buf_v
;
147 pr_debug("rackmeter: %s\n", pause
? "paused" : "started");
151 DBDMA_DO_STOP(rm
->dma_regs
);
154 memset(rdma
->buf1
, 0, SAMPLE_COUNT
& sizeof(u32
));
155 memset(rdma
->buf2
, 0, SAMPLE_COUNT
& sizeof(u32
));
157 rm
->dma_buf_v
->mark
= 0;
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
);
165 static void rackmeter_setup_dbdma(struct rackmeter
*rm
)
167 struct rackmeter_dma
*db
= rm
->dma_buf_v
;
168 struct dbdma_cmd
*cmd
= db
->cmd
;
170 /* Make sure dbdma is reset */
171 DBDMA_DO_RESET(rm
->dma_regs
);
173 pr_debug("rackmeter: mark offset=0x%zx\n",
174 offsetof(struct rackmeter_dma
, mark
));
175 pr_debug("rackmeter: buf1 offset=0x%zx\n",
176 offsetof(struct rackmeter_dma
, buf1
));
177 pr_debug("rackmeter: buf2 offset=0x%zx\n",
178 offsetof(struct rackmeter_dma
, buf2
));
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);
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
));
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);
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
);
208 rackmeter_do_pause(rm
, 0);
211 static void rackmeter_do_timer(struct work_struct
*work
)
213 struct rackmeter_cpu
*rcpu
=
214 container_of(work
, struct rackmeter_cpu
, sniffer
.work
);
215 struct rackmeter
*rm
= rcpu
->rm
;
216 unsigned int cpu
= smp_processor_id();
217 cputime64_t cur_jiffies
, total_idle_ticks
;
218 unsigned int total_ticks
, idle_ticks
;
219 int i
, offset
, load
, cumm
, pause
;
221 cur_jiffies
= jiffies64_to_cputime64(get_jiffies_64());
222 total_ticks
= (unsigned int)cputime64_sub(cur_jiffies
,
224 rcpu
->prev_wall
= cur_jiffies
;
226 total_idle_ticks
= get_cpu_idle_time(cpu
);
227 idle_ticks
= (unsigned int) cputime64_sub(total_idle_ticks
,
229 rcpu
->prev_idle
= total_idle_ticks
;
231 /* We do a very dumb calculation to update the LEDs for now,
232 * we'll do better once we have actual PWM implemented
234 load
= (9 * (total_ticks
- idle_ticks
)) / total_ticks
;
238 for (i
= 0; i
< 8; i
++) {
239 u8 ub
= (load
> i
) ? 0xff : 0;
240 rm
->ubuf
[i
+ offset
] = ub
;
243 rcpu
->zero
= (cumm
== 0);
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
);
253 schedule_delayed_work_on(cpu
, &rcpu
->sniffer
,
254 msecs_to_jiffies(CPU_SAMPLING_RATE
));
257 static void __devinit
rackmeter_init_cpu_sniffer(struct rackmeter
*rm
)
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
268 INIT_DELAYED_WORK(&rm
->cpu
[0].sniffer
, rackmeter_do_timer
);
270 INIT_DELAYED_WORK(&rm
->cpu
[1].sniffer
, rackmeter_do_timer
);
272 for_each_online_cpu(cpu
) {
273 struct rackmeter_cpu
*rcpu
;
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
));
285 static void __devexit
rackmeter_stop_cpu_sniffer(struct rackmeter
*rm
)
287 cancel_rearming_delayed_work(&rm
->cpu
[0].sniffer
);
288 cancel_rearming_delayed_work(&rm
->cpu
[1].sniffer
);
291 static int __devinit
rackmeter_setup(struct rackmeter
*rm
)
293 pr_debug("rackmeter: setting up i2s..\n");
294 rackmeter_setup_i2s(rm
);
296 pr_debug("rackmeter: setting up default pattern..\n");
297 rackmeter_set_default_pattern(rm
);
299 pr_debug("rackmeter: setting up dbdma..\n");
300 rackmeter_setup_dbdma(rm
);
302 pr_debug("rackmeter: start CPU measurements..\n");
303 rackmeter_init_cpu_sniffer(rm
);
305 printk(KERN_INFO
"RackMeter initialized\n");
310 /* XXX FIXME: No PWM yet, this is 0/1 */
311 static u32
rackmeter_calc_sample(struct rackmeter
*rm
, unsigned int index
)
316 for (led
= 0; led
< 16; led
++) {
318 sample
|= ((rm
->ubuf
[led
] >= 0x80) << 15);
320 return (sample
<< 17) | (sample
>> 15);
323 static irqreturn_t
rackmeter_irq(int irq
, void *arg
)
325 struct rackmeter
*rm
= arg
;
326 struct rackmeter_dma
*db
= rm
->dma_buf_v
;
327 unsigned int mark
, i
;
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
334 (void)in_le32(&rm
->dma_regs
->status
);
336 /* Make sure the CPU gets us in order */
341 if (mark
!= 1 && mark
!= 2) {
342 printk(KERN_WARNING
"rackmeter: Incorrect DMA mark 0x%08x\n",
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,"
348 DBDMA_DO_RESET(rm
->dma_regs
);
353 /* Next buffer we need to fill is mark value */
354 buf
= mark
== 1 ? db
->buf1
: db
->buf2
;
356 /* Fill it now. This routine converts the 8 bits depth sample array
357 * into the PWM bitmap for each LED.
359 for (i
= 0; i
< SAMPLE_COUNT
; i
++)
360 buf
[i
] = rackmeter_calc_sample(rm
, i
);
366 static int __devinit
rackmeter_probe(struct macio_dev
* mdev
,
367 const struct of_device_id
*match
)
369 struct device_node
*i2s
= NULL
, *np
= NULL
;
370 struct rackmeter
*rm
= NULL
;
371 struct resource ri2s
, rdma
;
374 pr_debug("rackmeter_probe()\n");
377 while ((i2s
= of_get_next_child(mdev
->ofdev
.node
, i2s
)) != NULL
)
378 if (strcmp(i2s
->name
, "i2s-a") == 0)
381 pr_debug(" i2s-a child not found\n");
384 /* Get lightshow or virtual sound */
385 while ((np
= of_get_next_child(i2s
, np
)) != NULL
) {
386 if (strcmp(np
->name
, "lightshow") == 0)
388 if ((strcmp(np
->name
, "sound") == 0) &&
389 of_get_property(np
, "virtual", NULL
) != NULL
)
393 pr_debug(" lightshow or sound+virtual child not found\n");
397 /* Create and initialize our instance data */
398 rm
= kzalloc(sizeof(struct rackmeter
), GFP_KERNEL
);
400 printk(KERN_ERR
"rackmeter: failed to allocate memory !\n");
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) {
412 "rackmeter: found match but lacks resources: %s"
413 " (%d resources, %d interrupts)\n",
414 mdev
->ofdev
.node
->full_name
);
418 if (macio_request_resources(mdev
, "rackmeter")) {
420 "rackmeter: failed to request resources: %s\n",
421 mdev
->ofdev
.node
->full_name
);
425 rm
->irq
= macio_irq(mdev
, 1);
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
)) {
432 "rackmeter: found match but lacks resources: %s",
433 mdev
->ofdev
.node
->full_name
);
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
);
443 rm
->ubuf
= (u8
*)__get_free_page(GFP_KERNEL
);
444 if (rm
->ubuf
== NULL
) {
446 "rackmeter: failed to allocate samples page !\n");
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
) {
456 "rackmeter: failed to allocate dma buffer !\n");
458 goto bail_free_samples
;
461 rm
->i2s_regs
= ioremap(macio_resource_start(mdev
, 0), 0x1000);
463 rm
->i2s_regs
= ioremap(ri2s
.start
, 0x1000);
465 if (rm
->i2s_regs
== NULL
) {
467 "rackmeter: failed to map i2s registers !\n");
472 rm
->dma_regs
= ioremap(macio_resource_start(mdev
, 1), 0x100);
474 rm
->dma_regs
= ioremap(rdma
.start
, 0x100);
476 if (rm
->dma_regs
== NULL
) {
478 "rackmeter: failed to map dma registers !\n");
483 rc
= rackmeter_setup(rm
);
486 "rackmeter: failed to initialize !\n");
491 rc
= request_irq(rm
->irq
, rackmeter_irq
, 0, "rackmeter", rm
);
494 "rackmeter: failed to request interrupt !\n");
501 DBDMA_DO_RESET(rm
->dma_regs
);
503 iounmap(rm
->dma_regs
);
505 iounmap(rm
->i2s_regs
);
507 dma_free_coherent(&macio_get_pci_dev(mdev
)->dev
,
508 sizeof(struct rackmeter_dma
),
509 rm
->dma_buf_v
, rm
->dma_buf_p
);
511 free_page((unsigned long)rm
->ubuf
);
514 macio_release_resources(mdev
);
521 dev_set_drvdata(&mdev
->ofdev
.dev
, NULL
);
525 static int __devexit
rackmeter_remove(struct macio_dev
* mdev
)
527 struct rackmeter
*rm
= dev_get_drvdata(&mdev
->ofdev
.dev
);
529 /* Stop CPU sniffer timer & work queues */
530 rackmeter_stop_cpu_sniffer(rm
);
532 /* Clear reference to private data */
533 dev_set_drvdata(&mdev
->ofdev
.dev
, NULL
);
535 /* Stop/reset dbdma */
536 DBDMA_DO_RESET(rm
->dma_regs
);
538 /* Release the IRQ */
539 free_irq(rm
->irq
, rm
);
541 /* Unmap registers */
542 iounmap(rm
->dma_regs
);
543 iounmap(rm
->i2s_regs
);
546 dma_free_coherent(&macio_get_pci_dev(mdev
)->dev
,
547 sizeof(struct rackmeter_dma
),
548 rm
->dma_buf_v
, rm
->dma_buf_p
);
551 free_page((unsigned long)rm
->ubuf
);
554 /* Release resources */
555 macio_release_resources(mdev
);
564 static int rackmeter_shutdown(struct macio_dev
* mdev
)
566 struct rackmeter
*rm
= dev_get_drvdata(&mdev
->ofdev
.dev
);
571 /* Stop CPU sniffer timer & work queues */
572 rackmeter_stop_cpu_sniffer(rm
);
574 /* Stop/reset dbdma */
575 DBDMA_DO_RESET(rm
->dma_regs
);
580 static struct of_device_id rackmeter_match
[] = {
585 static struct macio_driver rackmeter_driver
= {
587 .owner
= THIS_MODULE
,
588 .match_table
= rackmeter_match
,
589 .probe
= rackmeter_probe
,
590 .remove
= __devexit_p(rackmeter_remove
),
591 .shutdown
= rackmeter_shutdown
,
595 static int __init
rackmeter_init(void)
597 pr_debug("rackmeter_init()\n");
599 return macio_register_driver(&rackmeter_driver
);
602 static void __exit
rackmeter_exit(void)
604 pr_debug("rackmeter_exit()\n");
606 macio_unregister_driver(&rackmeter_driver
);
609 module_init(rackmeter_init
);
610 module_exit(rackmeter_exit
);
613 MODULE_LICENSE("GPL");
614 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
615 MODULE_DESCRIPTION("RackMeter: Support vu-meter on XServe front panel");