rack-meter.c revision 648616343cdbe904c585a6c12e323d3b3c72e46f
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> 21#include <linux/slab.h> 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> 28 29#include <asm/io.h> 30#include <asm/prom.h> 31#include <asm/machdep.h> 32#include <asm/pmac_feature.h> 33#include <asm/dbdma.h> 34#include <asm/macio.h> 35#include <asm/keylargo.h> 36 37/* Number of samples in a sample buffer */ 38#define SAMPLE_COUNT 256 39 40/* CPU meter sampling rate in ms */ 41#define CPU_SAMPLING_RATE 250 42 43struct rackmeter_dma { 44 struct dbdma_cmd cmd[4] ____cacheline_aligned; 45 u32 mark ____cacheline_aligned; 46 u32 buf1[SAMPLE_COUNT] ____cacheline_aligned; 47 u32 buf2[SAMPLE_COUNT] ____cacheline_aligned; 48} ____cacheline_aligned; 49 50struct rackmeter_cpu { 51 struct delayed_work sniffer; 52 struct rackmeter *rm; 53 cputime64_t prev_wall; 54 cputime64_t prev_idle; 55 int zero; 56} ____cacheline_aligned; 57 58struct rackmeter { 59 struct macio_dev *mdev; 60 unsigned int irq; 61 struct device_node *i2s; 62 u8 *ubuf; 63 struct dbdma_regs __iomem *dma_regs; 64 void __iomem *i2s_regs; 65 dma_addr_t dma_buf_p; 66 struct rackmeter_dma *dma_buf_v; 67 int stale_irq; 68 struct rackmeter_cpu cpu[2]; 69 int paused; 70 struct mutex sem; 71}; 72 73/* To be set as a tunable */ 74static int rackmeter_ignore_nice; 75 76/* This GPIO is whacked by the OS X driver when initializing */ 77#define RACKMETER_MAGIC_GPIO 0x78 78 79/* This is copied from cpufreq_ondemand, maybe we should put it in 80 * a common header somewhere 81 */ 82static inline cputime64_t get_cpu_idle_time(unsigned int cpu) 83{ 84 cputime64_t retval; 85 86 retval = kstat_cpu(cpu).cpustat.idle + kstat_cpu(cpu).cpustat.iowait; 87 88 if (rackmeter_ignore_nice) 89 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 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)); 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 211static void rackmeter_do_timer(struct work_struct *work) 212{ 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; 220 221 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64()); 222 total_ticks = (unsigned int) (cur_jiffies - rcpu->prev_wall); 223 rcpu->prev_wall = cur_jiffies; 224 225 total_idle_ticks = get_cpu_idle_time(cpu); 226 idle_ticks = (unsigned int) (total_idle_ticks - rcpu->prev_idle); 227 rcpu->prev_idle = total_idle_ticks; 228 229 /* We do a very dumb calculation to update the LEDs for now, 230 * we'll do better once we have actual PWM implemented 231 */ 232 load = (9 * (total_ticks - idle_ticks)) / total_ticks; 233 234 offset = cpu << 3; 235 cumm = 0; 236 for (i = 0; i < 8; i++) { 237 u8 ub = (load > i) ? 0xff : 0; 238 rm->ubuf[i + offset] = ub; 239 cumm |= ub; 240 } 241 rcpu->zero = (cumm == 0); 242 243 /* Now check if LEDs are all 0, we can stop DMA */ 244 pause = (rm->cpu[0].zero && rm->cpu[1].zero); 245 if (pause != rm->paused) { 246 mutex_lock(&rm->sem); 247 pause = (rm->cpu[0].zero && rm->cpu[1].zero); 248 rackmeter_do_pause(rm, pause); 249 mutex_unlock(&rm->sem); 250 } 251 schedule_delayed_work_on(cpu, &rcpu->sniffer, 252 msecs_to_jiffies(CPU_SAMPLING_RATE)); 253} 254 255static void __devinit rackmeter_init_cpu_sniffer(struct rackmeter *rm) 256{ 257 unsigned int cpu; 258 259 /* This driver works only with 1 or 2 CPUs numbered 0 and 1, 260 * but that's really all we have on Apple Xserve. It doesn't 261 * play very nice with CPU hotplug neither but we don't do that 262 * on those machines yet 263 */ 264 265 rm->cpu[0].rm = rm; 266 INIT_DELAYED_WORK(&rm->cpu[0].sniffer, rackmeter_do_timer); 267 rm->cpu[1].rm = rm; 268 INIT_DELAYED_WORK(&rm->cpu[1].sniffer, rackmeter_do_timer); 269 270 for_each_online_cpu(cpu) { 271 struct rackmeter_cpu *rcpu; 272 273 if (cpu > 1) 274 continue; 275 rcpu = &rm->cpu[cpu]; 276 rcpu->prev_idle = get_cpu_idle_time(cpu); 277 rcpu->prev_wall = jiffies64_to_cputime64(get_jiffies_64()); 278 schedule_delayed_work_on(cpu, &rm->cpu[cpu].sniffer, 279 msecs_to_jiffies(CPU_SAMPLING_RATE)); 280 } 281} 282 283static void rackmeter_stop_cpu_sniffer(struct rackmeter *rm) 284{ 285 cancel_delayed_work_sync(&rm->cpu[0].sniffer); 286 cancel_delayed_work_sync(&rm->cpu[1].sniffer); 287} 288 289static int __devinit rackmeter_setup(struct rackmeter *rm) 290{ 291 pr_debug("rackmeter: setting up i2s..\n"); 292 rackmeter_setup_i2s(rm); 293 294 pr_debug("rackmeter: setting up default pattern..\n"); 295 rackmeter_set_default_pattern(rm); 296 297 pr_debug("rackmeter: setting up dbdma..\n"); 298 rackmeter_setup_dbdma(rm); 299 300 pr_debug("rackmeter: start CPU measurements..\n"); 301 rackmeter_init_cpu_sniffer(rm); 302 303 printk(KERN_INFO "RackMeter initialized\n"); 304 305 return 0; 306} 307 308/* XXX FIXME: No PWM yet, this is 0/1 */ 309static u32 rackmeter_calc_sample(struct rackmeter *rm, unsigned int index) 310{ 311 int led; 312 u32 sample = 0; 313 314 for (led = 0; led < 16; led++) { 315 sample >>= 1; 316 sample |= ((rm->ubuf[led] >= 0x80) << 15); 317 } 318 return (sample << 17) | (sample >> 15); 319} 320 321static irqreturn_t rackmeter_irq(int irq, void *arg) 322{ 323 struct rackmeter *rm = arg; 324 struct rackmeter_dma *db = rm->dma_buf_v; 325 unsigned int mark, i; 326 u32 *buf; 327 328 /* Flush PCI buffers with an MMIO read. Maybe we could actually 329 * check the status one day ... in case things go wrong, though 330 * this never happened to me 331 */ 332 (void)in_le32(&rm->dma_regs->status); 333 334 /* Make sure the CPU gets us in order */ 335 rmb(); 336 337 /* Read mark */ 338 mark = db->mark; 339 if (mark != 1 && mark != 2) { 340 printk(KERN_WARNING "rackmeter: Incorrect DMA mark 0x%08x\n", 341 mark); 342 /* We allow for 3 errors like that (stale DBDMA irqs) */ 343 if (++rm->stale_irq > 3) { 344 printk(KERN_ERR "rackmeter: Too many errors," 345 " stopping DMA\n"); 346 DBDMA_DO_RESET(rm->dma_regs); 347 } 348 return IRQ_HANDLED; 349 } 350 351 /* Next buffer we need to fill is mark value */ 352 buf = mark == 1 ? db->buf1 : db->buf2; 353 354 /* Fill it now. This routine converts the 8 bits depth sample array 355 * into the PWM bitmap for each LED. 356 */ 357 for (i = 0; i < SAMPLE_COUNT; i++) 358 buf[i] = rackmeter_calc_sample(rm, i); 359 360 361 return IRQ_HANDLED; 362} 363 364static int __devinit rackmeter_probe(struct macio_dev* mdev, 365 const struct of_device_id *match) 366{ 367 struct device_node *i2s = NULL, *np = NULL; 368 struct rackmeter *rm = NULL; 369 struct resource ri2s, rdma; 370 int rc = -ENODEV; 371 372 pr_debug("rackmeter_probe()\n"); 373 374 /* Get i2s-a node */ 375 while ((i2s = of_get_next_child(mdev->ofdev.dev.of_node, i2s)) != NULL) 376 if (strcmp(i2s->name, "i2s-a") == 0) 377 break; 378 if (i2s == NULL) { 379 pr_debug(" i2s-a child not found\n"); 380 goto bail; 381 } 382 /* Get lightshow or virtual sound */ 383 while ((np = of_get_next_child(i2s, np)) != NULL) { 384 if (strcmp(np->name, "lightshow") == 0) 385 break; 386 if ((strcmp(np->name, "sound") == 0) && 387 of_get_property(np, "virtual", NULL) != NULL) 388 break; 389 } 390 if (np == NULL) { 391 pr_debug(" lightshow or sound+virtual child not found\n"); 392 goto bail; 393 } 394 395 /* Create and initialize our instance data */ 396 rm = kzalloc(sizeof(struct rackmeter), GFP_KERNEL); 397 if (rm == NULL) { 398 printk(KERN_ERR "rackmeter: failed to allocate memory !\n"); 399 rc = -ENOMEM; 400 goto bail_release; 401 } 402 rm->mdev = mdev; 403 rm->i2s = i2s; 404 mutex_init(&rm->sem); 405 dev_set_drvdata(&mdev->ofdev.dev, rm); 406 /* Check resources availability. We need at least resource 0 and 1 */ 407#if 0 /* Use that when i2s-a is finally an mdev per-se */ 408 if (macio_resource_count(mdev) < 2 || macio_irq_count(mdev) < 2) { 409 printk(KERN_ERR 410 "rackmeter: found match but lacks resources: %s" 411 " (%d resources, %d interrupts)\n", 412 mdev->ofdev.node->full_name); 413 rc = -ENXIO; 414 goto bail_free; 415 } 416 if (macio_request_resources(mdev, "rackmeter")) { 417 printk(KERN_ERR 418 "rackmeter: failed to request resources: %s\n", 419 mdev->ofdev.node->full_name); 420 rc = -EBUSY; 421 goto bail_free; 422 } 423 rm->irq = macio_irq(mdev, 1); 424#else 425 rm->irq = irq_of_parse_and_map(i2s, 1); 426 if (rm->irq == NO_IRQ || 427 of_address_to_resource(i2s, 0, &ri2s) || 428 of_address_to_resource(i2s, 1, &rdma)) { 429 printk(KERN_ERR 430 "rackmeter: found match but lacks resources: %s", 431 mdev->ofdev.dev.of_node->full_name); 432 rc = -ENXIO; 433 goto bail_free; 434 } 435#endif 436 437 pr_debug(" i2s @0x%08x\n", (unsigned int)ri2s.start); 438 pr_debug(" dma @0x%08x\n", (unsigned int)rdma.start); 439 pr_debug(" irq %d\n", rm->irq); 440 441 rm->ubuf = (u8 *)__get_free_page(GFP_KERNEL); 442 if (rm->ubuf == NULL) { 443 printk(KERN_ERR 444 "rackmeter: failed to allocate samples page !\n"); 445 rc = -ENOMEM; 446 goto bail_release; 447 } 448 449 rm->dma_buf_v = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev, 450 sizeof(struct rackmeter_dma), 451 &rm->dma_buf_p, GFP_KERNEL); 452 if (rm->dma_buf_v == NULL) { 453 printk(KERN_ERR 454 "rackmeter: failed to allocate dma buffer !\n"); 455 rc = -ENOMEM; 456 goto bail_free_samples; 457 } 458#if 0 459 rm->i2s_regs = ioremap(macio_resource_start(mdev, 0), 0x1000); 460#else 461 rm->i2s_regs = ioremap(ri2s.start, 0x1000); 462#endif 463 if (rm->i2s_regs == NULL) { 464 printk(KERN_ERR 465 "rackmeter: failed to map i2s registers !\n"); 466 rc = -ENXIO; 467 goto bail_free_dma; 468 } 469#if 0 470 rm->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x100); 471#else 472 rm->dma_regs = ioremap(rdma.start, 0x100); 473#endif 474 if (rm->dma_regs == NULL) { 475 printk(KERN_ERR 476 "rackmeter: failed to map dma registers !\n"); 477 rc = -ENXIO; 478 goto bail_unmap_i2s; 479 } 480 481 rc = rackmeter_setup(rm); 482 if (rc) { 483 printk(KERN_ERR 484 "rackmeter: failed to initialize !\n"); 485 rc = -ENXIO; 486 goto bail_unmap_dma; 487 } 488 489 rc = request_irq(rm->irq, rackmeter_irq, 0, "rackmeter", rm); 490 if (rc != 0) { 491 printk(KERN_ERR 492 "rackmeter: failed to request interrupt !\n"); 493 goto bail_stop_dma; 494 } 495 of_node_put(np); 496 return 0; 497 498 bail_stop_dma: 499 DBDMA_DO_RESET(rm->dma_regs); 500 bail_unmap_dma: 501 iounmap(rm->dma_regs); 502 bail_unmap_i2s: 503 iounmap(rm->i2s_regs); 504 bail_free_dma: 505 dma_free_coherent(&macio_get_pci_dev(mdev)->dev, 506 sizeof(struct rackmeter_dma), 507 rm->dma_buf_v, rm->dma_buf_p); 508 bail_free_samples: 509 free_page((unsigned long)rm->ubuf); 510 bail_release: 511#if 0 512 macio_release_resources(mdev); 513#endif 514 bail_free: 515 kfree(rm); 516 bail: 517 of_node_put(i2s); 518 of_node_put(np); 519 dev_set_drvdata(&mdev->ofdev.dev, NULL); 520 return rc; 521} 522 523static int __devexit rackmeter_remove(struct macio_dev* mdev) 524{ 525 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev); 526 527 /* Stop CPU sniffer timer & work queues */ 528 rackmeter_stop_cpu_sniffer(rm); 529 530 /* Clear reference to private data */ 531 dev_set_drvdata(&mdev->ofdev.dev, NULL); 532 533 /* Stop/reset dbdma */ 534 DBDMA_DO_RESET(rm->dma_regs); 535 536 /* Release the IRQ */ 537 free_irq(rm->irq, rm); 538 539 /* Unmap registers */ 540 iounmap(rm->dma_regs); 541 iounmap(rm->i2s_regs); 542 543 /* Free DMA */ 544 dma_free_coherent(&macio_get_pci_dev(mdev)->dev, 545 sizeof(struct rackmeter_dma), 546 rm->dma_buf_v, rm->dma_buf_p); 547 548 /* Free samples */ 549 free_page((unsigned long)rm->ubuf); 550 551#if 0 552 /* Release resources */ 553 macio_release_resources(mdev); 554#endif 555 556 /* Get rid of me */ 557 kfree(rm); 558 559 return 0; 560} 561 562static int rackmeter_shutdown(struct macio_dev* mdev) 563{ 564 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev); 565 566 if (rm == NULL) 567 return -ENODEV; 568 569 /* Stop CPU sniffer timer & work queues */ 570 rackmeter_stop_cpu_sniffer(rm); 571 572 /* Stop/reset dbdma */ 573 DBDMA_DO_RESET(rm->dma_regs); 574 575 return 0; 576} 577 578static struct of_device_id rackmeter_match[] = { 579 { .name = "i2s" }, 580 { } 581}; 582 583static struct macio_driver rackmeter_driver = { 584 .driver = { 585 .name = "rackmeter", 586 .owner = THIS_MODULE, 587 .of_match_table = rackmeter_match, 588 }, 589 .probe = rackmeter_probe, 590 .remove = __devexit_p(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_driver); 600} 601 602static void __exit rackmeter_exit(void) 603{ 604 pr_debug("rackmeter_exit()\n"); 605 606 macio_unregister_driver(&rackmeter_driver); 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"); 616