ALSA: hda: track CIRB/CORB command/response states for each codec

Recently we hit a bug in our dev board, whose HDMI codec#3 may emit
redundant/spurious responses, which were then taken as responses to
command for another onboard Realtek codec#2, and mess up both codecs.

Extend the azx_rb.cmds and azx_rb.res to array and track each codec's
commands/responses separately. This helps keep good codec safe from
broken ones.

Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>

authored by Wu Fengguang and committed by Takashi Iwai deadff16 ce577e8c

+56 -24
+1 -1
sound/pci/hda/hda_codec.c
··· 174 174 mutex_lock(&bus->cmd_mutex); 175 175 err = bus->ops.command(bus, cmd); 176 176 if (!err && res) 177 - *res = bus->ops.get_response(bus); 177 + *res = bus->ops.get_response(bus, codec->addr); 178 178 mutex_unlock(&bus->cmd_mutex); 179 179 snd_hda_power_down(codec); 180 180 if (res && *res == -1 && bus->rirb_error) {
+1 -1
sound/pci/hda/hda_codec.h
··· 568 568 /* send a single command */ 569 569 int (*command)(struct hda_bus *bus, unsigned int cmd); 570 570 /* get a response from the last command */ 571 - unsigned int (*get_response)(struct hda_bus *bus); 571 + unsigned int (*get_response)(struct hda_bus *bus, unsigned int addr); 572 572 /* free the private data */ 573 573 void (*private_free)(struct hda_bus *); 574 574 /* attach a PCM stream */
+54 -22
sound/pci/hda/hda_intel.c
··· 253 253 254 254 /* STATESTS int mask: S3,SD2,SD1,SD0 */ 255 255 #define AZX_MAX_CODECS 4 256 - #define STATESTS_INT_MASK 0x0f 256 + #define STATESTS_INT_MASK ((1 << AZX_MAX_CODECS) - 1) 257 257 258 258 /* SD_CTL bits */ 259 259 #define SD_CTL_STREAM_RESET 0x01 /* stream reset bit */ ··· 361 361 dma_addr_t addr; /* physical address of CORB/RIRB buffer */ 362 362 /* for RIRB */ 363 363 unsigned short rp, wp; /* read/write pointers */ 364 - int cmds; /* number of pending requests */ 365 - u32 res; /* last read value */ 364 + int cmds[AZX_MAX_CODECS]; /* number of pending requests */ 365 + u32 res[AZX_MAX_CODECS]; /* last read value */ 366 366 }; 367 367 368 368 struct azx { ··· 531 531 /* RIRB set up */ 532 532 chip->rirb.addr = chip->rb.addr + 2048; 533 533 chip->rirb.buf = (u32 *)(chip->rb.area + 2048); 534 - chip->rirb.wp = chip->rirb.rp = chip->rirb.cmds = 0; 534 + chip->rirb.wp = chip->rirb.rp = 0; 535 + memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds)); 535 536 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr); 536 537 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr)); 537 538 ··· 553 552 azx_writeb(chip, CORBCTL, 0); 554 553 } 555 554 555 + static unsigned int azx_command_addr(u32 cmd) 556 + { 557 + unsigned int addr = cmd >> 28; 558 + 559 + if (addr >= AZX_MAX_CODECS) { 560 + snd_BUG(); 561 + addr = 0; 562 + } 563 + 564 + return addr; 565 + } 566 + 567 + static unsigned int azx_response_addr(u32 res) 568 + { 569 + unsigned int addr = res & 0xf; 570 + 571 + if (addr >= AZX_MAX_CODECS) { 572 + snd_BUG(); 573 + addr = 0; 574 + } 575 + 576 + return addr; 577 + } 578 + 556 579 /* send a command */ 557 580 static int azx_corb_send_cmd(struct hda_bus *bus, u32 val) 558 581 { 559 582 struct azx *chip = bus->private_data; 583 + unsigned int addr = azx_command_addr(val); 560 584 unsigned int wp; 561 585 562 586 /* add command to corb */ ··· 590 564 wp %= ICH6_MAX_CORB_ENTRIES; 591 565 592 566 spin_lock_irq(&chip->reg_lock); 593 - chip->rirb.cmds++; 567 + chip->rirb.cmds[addr]++; 594 568 chip->corb.buf[wp] = cpu_to_le32(val); 595 569 azx_writel(chip, CORBWP, wp); 596 570 spin_unlock_irq(&chip->reg_lock); ··· 604 578 static void azx_update_rirb(struct azx *chip) 605 579 { 606 580 unsigned int rp, wp; 581 + unsigned int addr; 607 582 u32 res, res_ex; 608 583 609 584 wp = azx_readb(chip, RIRBWP); 610 585 if (wp == chip->rirb.wp) 611 586 return; 612 587 chip->rirb.wp = wp; 613 - 588 + 614 589 while (chip->rirb.rp != wp) { 615 590 chip->rirb.rp++; 616 591 chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES; ··· 619 592 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */ 620 593 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]); 621 594 res = le32_to_cpu(chip->rirb.buf[rp]); 595 + addr = azx_response_addr(res_ex); 622 596 if (res_ex & ICH6_RIRB_EX_UNSOL_EV) 623 597 snd_hda_queue_unsol_event(chip->bus, res, res_ex); 624 - else if (chip->rirb.cmds) { 625 - chip->rirb.res = res; 598 + else if (chip->rirb.cmds[addr]) { 599 + chip->rirb.res[addr] = res; 626 600 smp_wmb(); 627 - chip->rirb.cmds--; 601 + chip->rirb.cmds[addr]--; 628 602 } 629 603 } 630 604 } 631 605 632 606 /* receive a response */ 633 - static unsigned int azx_rirb_get_response(struct hda_bus *bus) 607 + static unsigned int azx_rirb_get_response(struct hda_bus *bus, 608 + unsigned int addr) 634 609 { 635 610 struct azx *chip = bus->private_data; 636 611 unsigned long timeout; ··· 645 616 azx_update_rirb(chip); 646 617 spin_unlock_irq(&chip->reg_lock); 647 618 } 648 - if (!chip->rirb.cmds) { 619 + if (!chip->rirb.cmds[addr]) { 649 620 smp_rmb(); 650 621 bus->rirb_error = 0; 651 - return chip->rirb.res; /* the last value */ 622 + return chip->rirb.res[addr]; /* the last value */ 652 623 } 653 624 if (time_after(jiffies, timeout)) 654 625 break; ··· 721 692 */ 722 693 723 694 /* receive a response */ 724 - static int azx_single_wait_for_response(struct azx *chip) 695 + static int azx_single_wait_for_response(struct azx *chip, unsigned int addr) 725 696 { 726 697 int timeout = 50; 727 698 ··· 729 700 /* check IRV busy bit */ 730 701 if (azx_readw(chip, IRS) & ICH6_IRS_VALID) { 731 702 /* reuse rirb.res as the response return value */ 732 - chip->rirb.res = azx_readl(chip, IR); 703 + chip->rirb.res[addr] = azx_readl(chip, IR); 733 704 return 0; 734 705 } 735 706 udelay(1); ··· 737 708 if (printk_ratelimit()) 738 709 snd_printd(SFX "get_response timeout: IRS=0x%x\n", 739 710 azx_readw(chip, IRS)); 740 - chip->rirb.res = -1; 711 + chip->rirb.res[addr] = -1; 741 712 return -EIO; 742 713 } 743 714 ··· 745 716 static int azx_single_send_cmd(struct hda_bus *bus, u32 val) 746 717 { 747 718 struct azx *chip = bus->private_data; 719 + unsigned int addr = azx_command_addr(val); 748 720 int timeout = 50; 749 721 750 722 bus->rirb_error = 0; ··· 758 728 azx_writel(chip, IC, val); 759 729 azx_writew(chip, IRS, azx_readw(chip, IRS) | 760 730 ICH6_IRS_BUSY); 761 - return azx_single_wait_for_response(chip); 731 + return azx_single_wait_for_response(chip, addr); 762 732 } 763 733 udelay(1); 764 734 } ··· 769 739 } 770 740 771 741 /* receive a response */ 772 - static unsigned int azx_single_get_response(struct hda_bus *bus) 742 + static unsigned int azx_single_get_response(struct hda_bus *bus, 743 + unsigned int addr) 773 744 { 774 745 struct azx *chip = bus->private_data; 775 - return chip->rirb.res; 746 + return chip->rirb.res[addr]; 776 747 } 777 748 778 749 /* ··· 796 765 } 797 766 798 767 /* get a response */ 799 - static unsigned int azx_get_response(struct hda_bus *bus) 768 + static unsigned int azx_get_response(struct hda_bus *bus, 769 + unsigned int addr) 800 770 { 801 771 struct azx *chip = bus->private_data; 802 772 if (chip->single_cmd) 803 - return azx_single_get_response(bus); 773 + return azx_single_get_response(bus, addr); 804 774 else 805 - return azx_rirb_get_response(bus); 775 + return azx_rirb_get_response(bus, addr); 806 776 } 807 777 808 778 #ifdef CONFIG_SND_HDA_POWER_SAVE ··· 1277 1245 1278 1246 chip->probing = 1; 1279 1247 azx_send_cmd(chip->bus, cmd); 1280 - res = azx_get_response(chip->bus); 1248 + res = azx_get_response(chip->bus, addr); 1281 1249 chip->probing = 0; 1282 1250 if (res == -1) 1283 1251 return -EIO;