mpic_u3msi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "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(unsigned int irq)
  22. {
  23. mask_msi_irq(irq);
  24. mpic_mask_irq(irq);
  25. }
  26. static void mpic_u3msi_unmask_irq(unsigned int irq)
  27. {
  28. mpic_unmask_irq(irq);
  29. unmask_msi_irq(irq);
  30. }
  31. static struct irq_chip mpic_u3msi_chip = {
  32. .shutdown = mpic_u3msi_mask_irq,
  33. .mask = mpic_u3msi_mask_irq,
  34. .unmask = mpic_u3msi_unmask_irq,
  35. .eoi = mpic_end_irq,
  36. .set_type = mpic_set_irq_type,
  37. .typename = "MPIC-U3MSI",
  38. };
  39. static u64 read_ht_magic_addr(struct pci_dev *pdev, unsigned int pos)
  40. {
  41. u8 flags;
  42. u32 tmp;
  43. u64 addr;
  44. pci_read_config_byte(pdev, pos + HT_MSI_FLAGS, &flags);
  45. if (flags & HT_MSI_FLAGS_FIXED)
  46. return HT_MSI_FIXED_ADDR;
  47. pci_read_config_dword(pdev, pos + HT_MSI_ADDR_LO, &tmp);
  48. addr = tmp & HT_MSI_ADDR_LO_MASK;
  49. pci_read_config_dword(pdev, pos + HT_MSI_ADDR_HI, &tmp);
  50. addr = addr | ((u64)tmp << 32);
  51. return addr;
  52. }
  53. static u64 find_ht_magic_addr(struct pci_dev *pdev)
  54. {
  55. struct pci_bus *bus;
  56. unsigned int pos;
  57. for (bus = pdev->bus; bus; bus = bus->parent) {
  58. pos = pci_find_ht_capability(bus->self, HT_CAPTYPE_MSI_MAPPING);
  59. if (pos)
  60. return read_ht_magic_addr(bus->self, pos);
  61. }
  62. return 0;
  63. }
  64. static int u3msi_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  65. {
  66. if (type == PCI_CAP_ID_MSIX)
  67. pr_debug("u3msi: MSI-X untested, trying anyway.\n");
  68. /* If we can't find a magic address then MSI ain't gonna work */
  69. if (find_ht_magic_addr(pdev) == 0) {
  70. pr_debug("u3msi: no magic address found for %s\n",
  71. pci_name(pdev));
  72. return -ENXIO;
  73. }
  74. return 0;
  75. }
  76. static void u3msi_teardown_msi_irqs(struct pci_dev *pdev)
  77. {
  78. struct msi_desc *entry;
  79. list_for_each_entry(entry, &pdev->msi_list, list) {
  80. if (entry->irq == NO_IRQ)
  81. continue;
  82. set_irq_msi(entry->irq, NULL);
  83. mpic_msi_free_hwirqs(msi_mpic, virq_to_hw(entry->irq), 1);
  84. irq_dispose_mapping(entry->irq);
  85. }
  86. return;
  87. }
  88. static void u3msi_compose_msi_msg(struct pci_dev *pdev, int virq,
  89. struct msi_msg *msg)
  90. {
  91. u64 addr;
  92. addr = find_ht_magic_addr(pdev);
  93. msg->address_lo = addr & 0xFFFFFFFF;
  94. msg->address_hi = addr >> 32;
  95. msg->data = virq_to_hw(virq);
  96. pr_debug("u3msi: allocated virq 0x%x (hw 0x%lx) at address 0x%lx\n",
  97. virq, virq_to_hw(virq), addr);
  98. }
  99. static int u3msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  100. {
  101. irq_hw_number_t hwirq;
  102. int rc;
  103. unsigned int virq;
  104. struct msi_desc *entry;
  105. struct msi_msg msg;
  106. list_for_each_entry(entry, &pdev->msi_list, list) {
  107. hwirq = mpic_msi_alloc_hwirqs(msi_mpic, 1);
  108. if (hwirq < 0) {
  109. rc = hwirq;
  110. pr_debug("u3msi: failed allocating hwirq\n");
  111. goto out_free;
  112. }
  113. virq = irq_create_mapping(msi_mpic->irqhost, hwirq);
  114. if (virq == NO_IRQ) {
  115. pr_debug("u3msi: failed mapping hwirq 0x%lx\n", hwirq);
  116. mpic_msi_free_hwirqs(msi_mpic, hwirq, 1);
  117. rc = -ENOSPC;
  118. goto out_free;
  119. }
  120. set_irq_msi(virq, entry);
  121. set_irq_chip(virq, &mpic_u3msi_chip);
  122. set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
  123. u3msi_compose_msi_msg(pdev, virq, &msg);
  124. write_msi_msg(virq, &msg);
  125. hwirq++;
  126. }
  127. return 0;
  128. out_free:
  129. u3msi_teardown_msi_irqs(pdev);
  130. return rc;
  131. }
  132. int mpic_u3msi_init(struct mpic *mpic)
  133. {
  134. int rc;
  135. rc = mpic_msi_init_allocator(mpic);
  136. if (rc) {
  137. pr_debug("u3msi: Error allocating bitmap!\n");
  138. return rc;
  139. }
  140. pr_debug("u3msi: Registering MPIC U3 MSI callbacks.\n");
  141. BUG_ON(msi_mpic);
  142. msi_mpic = mpic;
  143. WARN_ON(ppc_md.setup_msi_irqs);
  144. ppc_md.setup_msi_irqs = u3msi_setup_msi_irqs;
  145. ppc_md.teardown_msi_irqs = u3msi_teardown_msi_irqs;
  146. ppc_md.msi_check_device = u3msi_msi_check_device;
  147. return 0;
  148. }