mpic_u3msi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1);
  93. }
  94. }
  95. static int u3msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  96. {
  97. unsigned int virq;
  98. struct msi_desc *entry;
  99. struct msi_msg msg;
  100. u64 addr;
  101. int hwirq;
  102. if (type == PCI_CAP_ID_MSIX)
  103. pr_debug("u3msi: MSI-X untested, trying anyway.\n");
  104. /* If we can't find a magic address then MSI ain't gonna work */
  105. if (find_ht_magic_addr(pdev, 0) == 0 &&
  106. find_u4_magic_addr(pdev, 0) == 0) {
  107. pr_debug("u3msi: no magic address found for %s\n",
  108. pci_name(pdev));
  109. return -ENXIO;
  110. }
  111. msi_for_each_desc(entry, &pdev->dev, MSI_DESC_NOTASSOCIATED) {
  112. hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap, 1);
  113. if (hwirq < 0) {
  114. pr_debug("u3msi: failed allocating hwirq\n");
  115. return hwirq;
  116. }
  117. addr = find_ht_magic_addr(pdev, hwirq);
  118. if (addr == 0)
  119. addr = find_u4_magic_addr(pdev, hwirq);
  120. msg.address_lo = addr & 0xFFFFFFFF;
  121. msg.address_hi = addr >> 32;
  122. virq = irq_create_mapping(msi_mpic->irqhost, hwirq);
  123. if (!virq) {
  124. pr_debug("u3msi: failed mapping hwirq 0x%x\n", hwirq);
  125. msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1);
  126. return -ENOSPC;
  127. }
  128. irq_set_msi_desc(virq, entry);
  129. irq_set_chip(virq, &mpic_u3msi_chip);
  130. irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
  131. pr_debug("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",
  132. virq, hwirq, (unsigned long)addr);
  133. printk("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",
  134. virq, hwirq, (unsigned long)addr);
  135. msg.data = hwirq;
  136. pci_write_msi_msg(virq, &msg);
  137. hwirq++;
  138. }
  139. return 0;
  140. }
  141. int __init mpic_u3msi_init(struct mpic *mpic)
  142. {
  143. int rc;
  144. struct pci_controller *phb;
  145. rc = mpic_msi_init_allocator(mpic);
  146. if (rc) {
  147. pr_debug("u3msi: Error allocating bitmap!\n");
  148. return rc;
  149. }
  150. pr_debug("u3msi: Registering MPIC U3 MSI callbacks.\n");
  151. BUG_ON(msi_mpic);
  152. msi_mpic = mpic;
  153. list_for_each_entry(phb, &hose_list, list_node) {
  154. WARN_ON(phb->controller_ops.setup_msi_irqs);
  155. phb->controller_ops.setup_msi_irqs = u3msi_setup_msi_irqs;
  156. phb->controller_ops.teardown_msi_irqs = u3msi_teardown_msi_irqs;
  157. }
  158. return 0;
  159. }