mpic_u3msi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2006, Segher Boessenkool, IBM Corporation.
  4. * Copyright 2006-2007, Michael Ellerman, IBM Corporation.
  5. */
  6. #include <linux/irq.h>
  7. #include <linux/irqdomain.h>
  8. #include <linux/msi.h>
  9. #include <asm/mpic.h>
  10. #include <asm/hw_irq.h>
  11. #include <asm/ppc-pci.h>
  12. #include <asm/msi_bitmap.h>
  13. #include "mpic.h"
  14. /* A bit ugly, can we get this from the pci_dev somehow? */
  15. static struct mpic *msi_mpic;
  16. static void mpic_u3msi_mask_irq(struct irq_data *data)
  17. {
  18. pci_msi_mask_irq(data);
  19. mpic_mask_irq(data);
  20. }
  21. static void mpic_u3msi_unmask_irq(struct irq_data *data)
  22. {
  23. mpic_unmask_irq(data);
  24. pci_msi_unmask_irq(data);
  25. }
  26. static struct irq_chip mpic_u3msi_chip = {
  27. .irq_shutdown = mpic_u3msi_mask_irq,
  28. .irq_mask = mpic_u3msi_mask_irq,
  29. .irq_unmask = mpic_u3msi_unmask_irq,
  30. .irq_eoi = mpic_end_irq,
  31. .irq_set_type = mpic_set_irq_type,
  32. .irq_set_affinity = mpic_set_affinity,
  33. .name = "MPIC-U3MSI",
  34. };
  35. static u64 read_ht_magic_addr(struct pci_dev *pdev, unsigned int pos)
  36. {
  37. u8 flags;
  38. u32 tmp;
  39. u64 addr;
  40. pci_read_config_byte(pdev, pos + HT_MSI_FLAGS, &flags);
  41. if (flags & HT_MSI_FLAGS_FIXED)
  42. return HT_MSI_FIXED_ADDR;
  43. pci_read_config_dword(pdev, pos + HT_MSI_ADDR_LO, &tmp);
  44. addr = tmp & HT_MSI_ADDR_LO_MASK;
  45. pci_read_config_dword(pdev, pos + HT_MSI_ADDR_HI, &tmp);
  46. addr = addr | ((u64)tmp << 32);
  47. return addr;
  48. }
  49. static u64 find_ht_magic_addr(struct pci_dev *pdev, unsigned int hwirq)
  50. {
  51. struct pci_bus *bus;
  52. unsigned int pos;
  53. for (bus = pdev->bus; bus && bus->self; bus = bus->parent) {
  54. pos = pci_find_ht_capability(bus->self, HT_CAPTYPE_MSI_MAPPING);
  55. if (pos)
  56. return read_ht_magic_addr(bus->self, pos);
  57. }
  58. return 0;
  59. }
  60. static u64 find_u4_magic_addr(struct pci_dev *pdev, unsigned int hwirq)
  61. {
  62. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  63. /* U4 PCIe MSIs need to write to the special register in
  64. * the bridge that generates interrupts. There should be
  65. * theoretically a register at 0xf8005000 where you just write
  66. * the MSI number and that triggers the right interrupt, but
  67. * unfortunately, this is busted in HW, the bridge endian swaps
  68. * the value and hits the wrong nibble in the register.
  69. *
  70. * So instead we use another register set which is used normally
  71. * for converting HT interrupts to MPIC interrupts, which decodes
  72. * the interrupt number as part of the low address bits
  73. *
  74. * This will not work if we ever use more than one legacy MSI in
  75. * a block but we never do. For one MSI or multiple MSI-X where
  76. * each interrupt address can be specified separately, it works
  77. * just fine.
  78. */
  79. if (of_device_is_compatible(hose->dn, "u4-pcie") ||
  80. of_device_is_compatible(hose->dn, "U4-pcie"))
  81. return 0xf8004000 | (hwirq << 4);
  82. return 0;
  83. }
  84. static void u3msi_teardown_msi_irqs(struct pci_dev *pdev)
  85. {
  86. struct msi_desc *entry;
  87. irq_hw_number_t hwirq;
  88. msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) {
  89. hwirq = virq_to_hw(entry->irq);
  90. irq_set_msi_desc(entry->irq, NULL);
  91. irq_dispose_mapping(entry->irq);
  92. entry->irq = 0;
  93. msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1);
  94. }
  95. }
  96. static int u3msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  97. {
  98. unsigned int virq;
  99. struct msi_desc *entry;
  100. struct msi_msg msg;
  101. u64 addr;
  102. int hwirq;
  103. if (type == PCI_CAP_ID_MSIX)
  104. pr_debug("u3msi: MSI-X untested, trying anyway.\n");
  105. /* If we can't find a magic address then MSI ain't gonna work */
  106. if (find_ht_magic_addr(pdev, 0) == 0 &&
  107. find_u4_magic_addr(pdev, 0) == 0) {
  108. pr_debug("u3msi: no magic address found for %s\n",
  109. pci_name(pdev));
  110. return -ENXIO;
  111. }
  112. msi_for_each_desc(entry, &pdev->dev, MSI_DESC_NOTASSOCIATED) {
  113. hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap, 1);
  114. if (hwirq < 0) {
  115. pr_debug("u3msi: failed allocating hwirq\n");
  116. return hwirq;
  117. }
  118. addr = find_ht_magic_addr(pdev, hwirq);
  119. if (addr == 0)
  120. addr = find_u4_magic_addr(pdev, hwirq);
  121. msg.address_lo = addr & 0xFFFFFFFF;
  122. msg.address_hi = addr >> 32;
  123. virq = irq_create_mapping(msi_mpic->irqhost, hwirq);
  124. if (!virq) {
  125. pr_debug("u3msi: failed mapping hwirq 0x%x\n", hwirq);
  126. msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1);
  127. return -ENOSPC;
  128. }
  129. irq_set_msi_desc(virq, entry);
  130. irq_set_chip(virq, &mpic_u3msi_chip);
  131. irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
  132. pr_debug("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",
  133. virq, hwirq, (unsigned long)addr);
  134. printk("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",
  135. virq, hwirq, (unsigned long)addr);
  136. msg.data = hwirq;
  137. pci_write_msi_msg(virq, &msg);
  138. hwirq++;
  139. }
  140. return 0;
  141. }
  142. int __init mpic_u3msi_init(struct mpic *mpic)
  143. {
  144. int rc;
  145. struct pci_controller *phb;
  146. rc = mpic_msi_init_allocator(mpic);
  147. if (rc) {
  148. pr_debug("u3msi: Error allocating bitmap!\n");
  149. return rc;
  150. }
  151. pr_debug("u3msi: Registering MPIC U3 MSI callbacks.\n");
  152. BUG_ON(msi_mpic);
  153. msi_mpic = mpic;
  154. list_for_each_entry(phb, &hose_list, list_node) {
  155. WARN_ON(phb->controller_ops.setup_msi_irqs);
  156. phb->controller_ops.setup_msi_irqs = u3msi_setup_msi_irqs;
  157. phb->controller_ops.teardown_msi_irqs = u3msi_teardown_msi_irqs;
  158. }
  159. return 0;
  160. }