mpic_u3msi.c 5.3 KB

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