msi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2007, Olof Johansson, PA Semi
  3. *
  4. * Based on arch/powerpc/sysdev/mpic_u3msi.c:
  5. *
  6. * Copyright 2006, Segher Boessenkool, IBM Corporation.
  7. * Copyright 2006-2007, Michael Ellerman, IBM Corporation.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2 of the
  12. * License.
  13. *
  14. */
  15. #include <linux/irq.h>
  16. #include <linux/msi.h>
  17. #include <asm/mpic.h>
  18. #include <asm/prom.h>
  19. #include <asm/hw_irq.h>
  20. #include <asm/ppc-pci.h>
  21. #include <asm/msi_bitmap.h>
  22. #include <sysdev/mpic.h>
  23. /* Allocate 16 interrupts per device, to give an alignment of 16,
  24. * since that's the size of the grouping w.r.t. affinity. If someone
  25. * needs more than 32 MSI's down the road we'll have to rethink this,
  26. * but it should be OK for now.
  27. */
  28. #define ALLOC_CHUNK 16
  29. #define PASEMI_MSI_ADDR 0xfc080000
  30. /* A bit ugly, can we get this from the pci_dev somehow? */
  31. static struct mpic *msi_mpic;
  32. static void mpic_pasemi_msi_mask_irq(struct irq_data *data)
  33. {
  34. pr_debug("mpic_pasemi_msi_mask_irq %d\n", data->irq);
  35. pci_msi_mask_irq(data);
  36. mpic_mask_irq(data);
  37. }
  38. static void mpic_pasemi_msi_unmask_irq(struct irq_data *data)
  39. {
  40. pr_debug("mpic_pasemi_msi_unmask_irq %d\n", data->irq);
  41. mpic_unmask_irq(data);
  42. pci_msi_unmask_irq(data);
  43. }
  44. static struct irq_chip mpic_pasemi_msi_chip = {
  45. .irq_shutdown = mpic_pasemi_msi_mask_irq,
  46. .irq_mask = mpic_pasemi_msi_mask_irq,
  47. .irq_unmask = mpic_pasemi_msi_unmask_irq,
  48. .irq_eoi = mpic_end_irq,
  49. .irq_set_type = mpic_set_irq_type,
  50. .irq_set_affinity = mpic_set_affinity,
  51. .name = "PASEMI-MSI",
  52. };
  53. static void pasemi_msi_teardown_msi_irqs(struct pci_dev *pdev)
  54. {
  55. struct msi_desc *entry;
  56. irq_hw_number_t hwirq;
  57. pr_debug("pasemi_msi_teardown_msi_irqs, pdev %p\n", pdev);
  58. for_each_pci_msi_entry(entry, pdev) {
  59. if (!entry->irq)
  60. continue;
  61. hwirq = virq_to_hw(entry->irq);
  62. irq_set_msi_desc(entry->irq, NULL);
  63. irq_dispose_mapping(entry->irq);
  64. msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, ALLOC_CHUNK);
  65. }
  66. return;
  67. }
  68. static int pasemi_msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  69. {
  70. unsigned int virq;
  71. struct msi_desc *entry;
  72. struct msi_msg msg;
  73. int hwirq;
  74. if (type == PCI_CAP_ID_MSIX)
  75. pr_debug("pasemi_msi: MSI-X untested, trying anyway\n");
  76. pr_debug("pasemi_msi_setup_msi_irqs, pdev %p nvec %d type %d\n",
  77. pdev, nvec, type);
  78. msg.address_hi = 0;
  79. msg.address_lo = PASEMI_MSI_ADDR;
  80. for_each_pci_msi_entry(entry, pdev) {
  81. /* Allocate 16 interrupts for now, since that's the grouping for
  82. * affinity. This can be changed later if it turns out 32 is too
  83. * few MSIs for someone, but restrictions will apply to how the
  84. * sources can be changed independently.
  85. */
  86. hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap,
  87. ALLOC_CHUNK);
  88. if (hwirq < 0) {
  89. pr_debug("pasemi_msi: failed allocating hwirq\n");
  90. return hwirq;
  91. }
  92. virq = irq_create_mapping(msi_mpic->irqhost, hwirq);
  93. if (!virq) {
  94. pr_debug("pasemi_msi: failed mapping hwirq 0x%x\n",
  95. hwirq);
  96. msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq,
  97. ALLOC_CHUNK);
  98. return -ENOSPC;
  99. }
  100. /* Vector on MSI is really an offset, the hardware adds
  101. * it to the value written at the magic address. So set
  102. * it to 0 to remain sane.
  103. */
  104. mpic_set_vector(virq, 0);
  105. irq_set_msi_desc(virq, entry);
  106. irq_set_chip(virq, &mpic_pasemi_msi_chip);
  107. irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
  108. pr_debug("pasemi_msi: allocated virq 0x%x (hw 0x%x) " \
  109. "addr 0x%x\n", virq, hwirq, msg.address_lo);
  110. /* Likewise, the device writes [0...511] into the target
  111. * register to generate MSI [512...1023]
  112. */
  113. msg.data = hwirq-0x200;
  114. pci_write_msi_msg(virq, &msg);
  115. }
  116. return 0;
  117. }
  118. int mpic_pasemi_msi_init(struct mpic *mpic)
  119. {
  120. int rc;
  121. struct pci_controller *phb;
  122. struct device_node *of_node;
  123. of_node = irq_domain_get_of_node(mpic->irqhost);
  124. if (!of_node ||
  125. !of_device_is_compatible(of_node,
  126. "pasemi,pwrficient-openpic"))
  127. return -ENODEV;
  128. rc = mpic_msi_init_allocator(mpic);
  129. if (rc) {
  130. pr_debug("pasemi_msi: Error allocating bitmap!\n");
  131. return rc;
  132. }
  133. pr_debug("pasemi_msi: Registering PA Semi MPIC MSI callbacks\n");
  134. msi_mpic = mpic;
  135. list_for_each_entry(phb, &hose_list, list_node) {
  136. WARN_ON(phb->controller_ops.setup_msi_irqs);
  137. phb->controller_ops.setup_msi_irqs = pasemi_msi_setup_msi_irqs;
  138. phb->controller_ops.teardown_msi_irqs = pasemi_msi_teardown_msi_irqs;
  139. }
  140. return 0;
  141. }