19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/device.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
31 #include <asm/machdep.h>
38 #define SAMPLE_COUNT 256
41 #define CPU_SAMPLING_RATE 250
74 static int rackmeter_ignore_nice;
77 #define RACKMETER_MAGIC_GPIO 0x78
89 if (rackmeter_ignore_nice)
95 static void rackmeter_setup_i2s(
struct rackmeter *rm)
97 struct macio_chip *macio = rm->
mdev->bus->chip;
106 pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, rm->
i2s, 0, 1);
112 MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_ENABLE);
113 MACIO_BIC(KEYLARGO_FCR1, KL1_I2S0_CLK_ENABLE_BIT);
114 (
void)MACIO_IN32(KEYLARGO_FCR1);
126 MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_CELL_ENABLE |
127 KL1_I2S0_CLK_ENABLE_BIT);
128 (
void)MACIO_IN32(KEYLARGO_FCR1);
132 static void rackmeter_set_default_pattern(
struct rackmeter *rm)
136 for (i = 0; i < 16; i++) {
138 rm->
ubuf[
i] = (i & 1) * 255;
140 rm->
ubuf[
i] = ((~i) & 1) * 255;
148 pr_debug(
"rackmeter: %s\n", pause ?
"paused" :
"started");
166 static void rackmeter_setup_dbdma(
struct rackmeter *rm)
169 struct dbdma_cmd *
cmd = db->cmd;
174 pr_debug(
"rackmeter: mark offset=0x%zx\n",
176 pr_debug(
"rackmeter: buf1 offset=0x%zx\n",
178 pr_debug(
"rackmeter: buf2 offset=0x%zx\n",
182 memset(cmd, 0, 4 *
sizeof(
struct dbdma_cmd));
183 st_le16(&cmd->req_count, 4);
184 st_le16(&cmd->command, STORE_WORD |
INTR_ALWAYS | KEY_SYSTEM);
187 st_le32(&cmd->cmd_dep, 0x02000000);
191 st_le16(&cmd->command, OUTPUT_MORE);
196 st_le16(&cmd->req_count, 4);
197 st_le16(&cmd->command, STORE_WORD |
INTR_ALWAYS | KEY_SYSTEM);
200 st_le32(&cmd->cmd_dep, 0x01000000);
204 st_le16(&cmd->command, OUTPUT_MORE | BR_ALWAYS);
209 rackmeter_do_pause(rm, 0);
219 unsigned int total_ticks, idle_ticks;
223 total_ticks = (
unsigned int) (cur_jiffies - rcpu->
prev_wall);
226 total_idle_ticks = get_cpu_idle_time(cpu);
227 idle_ticks = (
unsigned int) (total_idle_ticks - rcpu->
prev_idle);
233 load = (9 * (total_ticks - idle_ticks)) / total_ticks;
237 for (i = 0; i < 8; i++) {
238 u8 ub = (load >
i) ? 0xff : 0;
242 rcpu->
zero = (cumm == 0);
245 pause = (rm->
cpu[0].zero && rm->
cpu[1].zero);
246 if (pause != rm->
paused) {
248 pause = (rm->
cpu[0].zero && rm->
cpu[1].zero);
249 rackmeter_do_pause(rm, pause);
277 rcpu->
prev_idle = get_cpu_idle_time(cpu);
284 static void rackmeter_stop_cpu_sniffer(
struct rackmeter *rm)
292 pr_debug(
"rackmeter: setting up i2s..\n");
293 rackmeter_setup_i2s(rm);
295 pr_debug(
"rackmeter: setting up default pattern..\n");
296 rackmeter_set_default_pattern(rm);
298 pr_debug(
"rackmeter: setting up dbdma..\n");
299 rackmeter_setup_dbdma(rm);
301 pr_debug(
"rackmeter: start CPU measurements..\n");
302 rackmeter_init_cpu_sniffer(rm);
315 for (led = 0; led < 16; led++) {
317 sample |= ((rm->
ubuf[
led] >= 0x80) << 15);
319 return (sample << 17) | (sample >> 15);
326 unsigned int mark,
i;
340 if (mark != 1 && mark != 2) {
353 buf = mark == 1 ? db->buf1 : db->buf2;
359 buf[i] = rackmeter_calc_sample(rm, i);
365 static int __devinit rackmeter_probe(
struct macio_dev* mdev,
380 pr_debug(
" i2s-a child not found\n");
385 if (
strcmp(np->name,
"lightshow") == 0)
387 if ((
strcmp(np->name,
"sound") == 0) &&
392 pr_debug(
" lightshow or sound+virtual child not found\n");
409 if (macio_resource_count(mdev) < 2 || macio_irq_count(mdev) < 2) {
411 "rackmeter: found match but lacks resources: %s"
412 " (%d resources, %d interrupts)\n",
413 mdev->ofdev.node->full_name);
419 "rackmeter: failed to request resources: %s\n",
420 mdev->ofdev.node->full_name);
424 rm->
irq = macio_irq(mdev, 1);
431 "rackmeter: found match but lacks resources: %s",
432 mdev->ofdev.dev.of_node->full_name);
438 pr_debug(
" i2s @0x%08x\n", (
unsigned int)ri2s.start);
439 pr_debug(
" dma @0x%08x\n", (
unsigned int)rdma.start);
445 "rackmeter: failed to allocate samples page !\n");
455 "rackmeter: failed to allocate dma buffer !\n");
457 goto bail_free_samples;
466 "rackmeter: failed to map i2s registers !\n");
477 "rackmeter: failed to map dma registers !\n");
482 rc = rackmeter_setup(rm);
485 "rackmeter: failed to initialize !\n");
493 "rackmeter: failed to request interrupt !\n");
524 static int __devexit rackmeter_remove(
struct macio_dev* mdev)
529 rackmeter_stop_cpu_sniffer(rm);
563 static int rackmeter_shutdown(
struct macio_dev* mdev)
571 rackmeter_stop_cpu_sniffer(rm);
584 static struct macio_driver rackmeter_driver = {
588 .of_match_table = rackmeter_match,
590 .probe = rackmeter_probe,
592 .shutdown = rackmeter_shutdown,
596 static int __init rackmeter_init(
void)
603 static void __exit rackmeter_exit(
void)