mpic_u3msi.c 5.2 KB

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