msi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. pr_debug("pasemi_msi_teardown_msi_irqs, pdev %p\n", pdev);
  57. list_for_each_entry(entry, &pdev->msi_list, list) {
  58. if (entry->irq == NO_IRQ)
  59. continue;
  60. irq_set_msi_desc(entry->irq, NULL);
  61. msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap,
  62. virq_to_hw(entry->irq), ALLOC_CHUNK);
  63. irq_dispose_mapping(entry->irq);
  64. }
  65. return;
  66. }
  67. static int pasemi_msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  68. {
  69. unsigned int virq;
  70. struct msi_desc *entry;
  71. struct msi_msg msg;
  72. int hwirq;
  73. if (type == PCI_CAP_ID_MSIX)
  74. pr_debug("pasemi_msi: MSI-X untested, trying anyway\n");
  75. pr_debug("pasemi_msi_setup_msi_irqs, pdev %p nvec %d type %d\n",
  76. pdev, nvec, type);
  77. msg.address_hi = 0;
  78. msg.address_lo = PASEMI_MSI_ADDR;
  79. list_for_each_entry(entry, &pdev->msi_list, list) {
  80. /* Allocate 16 interrupts for now, since that's the grouping for
  81. * affinity. This can be changed later if it turns out 32 is too
  82. * few MSIs for someone, but restrictions will apply to how the
  83. * sources can be changed independently.
  84. */
  85. hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap,
  86. ALLOC_CHUNK);
  87. if (hwirq < 0) {
  88. pr_debug("pasemi_msi: failed allocating hwirq\n");
  89. return hwirq;
  90. }
  91. virq = irq_create_mapping(msi_mpic->irqhost, hwirq);
  92. if (virq == NO_IRQ) {
  93. pr_debug("pasemi_msi: failed mapping hwirq 0x%x\n",
  94. hwirq);
  95. msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq,
  96. ALLOC_CHUNK);
  97. return -ENOSPC;
  98. }
  99. /* Vector on MSI is really an offset, the hardware adds
  100. * it to the value written at the magic address. So set
  101. * it to 0 to remain sane.
  102. */
  103. mpic_set_vector(virq, 0);
  104. irq_set_msi_desc(virq, entry);
  105. irq_set_chip(virq, &mpic_pasemi_msi_chip);
  106. irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
  107. pr_debug("pasemi_msi: allocated virq 0x%x (hw 0x%x) " \
  108. "addr 0x%x\n", virq, hwirq, msg.address_lo);
  109. /* Likewise, the device writes [0...511] into the target
  110. * register to generate MSI [512...1023]
  111. */
  112. msg.data = hwirq-0x200;
  113. pci_write_msi_msg(virq, &msg);
  114. }
  115. return 0;
  116. }
  117. int mpic_pasemi_msi_init(struct mpic *mpic)
  118. {
  119. int rc;
  120. struct pci_controller *phb;
  121. if (!mpic->irqhost->of_node ||
  122. !of_device_is_compatible(mpic->irqhost->of_node,
  123. "pasemi,pwrficient-openpic"))
  124. return -ENODEV;
  125. rc = mpic_msi_init_allocator(mpic);
  126. if (rc) {
  127. pr_debug("pasemi_msi: Error allocating bitmap!\n");
  128. return rc;
  129. }
  130. pr_debug("pasemi_msi: Registering PA Semi MPIC MSI callbacks\n");
  131. msi_mpic = mpic;
  132. list_for_each_entry(phb, &hose_list, list_node) {
  133. WARN_ON(phb->controller_ops.setup_msi_irqs);
  134. phb->controller_ops.setup_msi_irqs = pasemi_msi_setup_msi_irqs;
  135. phb->controller_ops.teardown_msi_irqs = pasemi_msi_teardown_msi_irqs;
  136. }
  137. return 0;
  138. }