irqdomain.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Message Signaled Interrupt (MSI) - irqdomain support
  4. */
  5. #include <linux/acpi_iort.h>
  6. #include <linux/irqdomain.h>
  7. #include <linux/of_irq.h>
  8. #include "msi.h"
  9. int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  10. {
  11. struct irq_domain *domain;
  12. domain = dev_get_msi_domain(&dev->dev);
  13. if (domain && irq_domain_is_hierarchy(domain))
  14. return msi_domain_alloc_irqs_descs_locked(domain, &dev->dev, nvec);
  15. return pci_msi_legacy_setup_msi_irqs(dev, nvec, type);
  16. }
  17. void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
  18. {
  19. struct irq_domain *domain;
  20. domain = dev_get_msi_domain(&dev->dev);
  21. if (domain && irq_domain_is_hierarchy(domain))
  22. msi_domain_free_irqs_descs_locked(domain, &dev->dev);
  23. else
  24. pci_msi_legacy_teardown_msi_irqs(dev);
  25. msi_free_msi_descs(&dev->dev);
  26. }
  27. /**
  28. * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
  29. * @irq_data: Pointer to interrupt data of the MSI interrupt
  30. * @msg: Pointer to the message
  31. */
  32. static void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
  33. {
  34. struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
  35. /*
  36. * For MSI-X desc->irq is always equal to irq_data->irq. For
  37. * MSI only the first interrupt of MULTI MSI passes the test.
  38. */
  39. if (desc->irq == irq_data->irq)
  40. __pci_write_msi_msg(desc, msg);
  41. }
  42. /**
  43. * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
  44. * @desc: Pointer to the MSI descriptor
  45. *
  46. * The ID number is only used within the irqdomain.
  47. */
  48. static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
  49. {
  50. struct pci_dev *dev = msi_desc_to_pci_dev(desc);
  51. return (irq_hw_number_t)desc->msi_index |
  52. pci_dev_id(dev) << 11 |
  53. (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
  54. }
  55. static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
  56. {
  57. return !desc->pci.msi_attrib.is_msix && desc->nvec_used > 1;
  58. }
  59. /**
  60. * pci_msi_domain_check_cap - Verify that @domain supports the capabilities
  61. * for @dev
  62. * @domain: The interrupt domain to check
  63. * @info: The domain info for verification
  64. * @dev: The device to check
  65. *
  66. * Returns:
  67. * 0 if the functionality is supported
  68. * 1 if Multi MSI is requested, but the domain does not support it
  69. * -ENOTSUPP otherwise
  70. */
  71. static int pci_msi_domain_check_cap(struct irq_domain *domain,
  72. struct msi_domain_info *info,
  73. struct device *dev)
  74. {
  75. struct msi_desc *desc = msi_first_desc(dev, MSI_DESC_ALL);
  76. /* Special handling to support __pci_enable_msi_range() */
  77. if (pci_msi_desc_is_multi_msi(desc) &&
  78. !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
  79. return 1;
  80. if (desc->pci.msi_attrib.is_msix) {
  81. if (!(info->flags & MSI_FLAG_PCI_MSIX))
  82. return -ENOTSUPP;
  83. if (info->flags & MSI_FLAG_MSIX_CONTIGUOUS) {
  84. unsigned int idx = 0;
  85. /* Check for gaps in the entry indices */
  86. msi_for_each_desc(desc, dev, MSI_DESC_ALL) {
  87. if (desc->msi_index != idx++)
  88. return -ENOTSUPP;
  89. }
  90. }
  91. }
  92. return 0;
  93. }
  94. static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
  95. struct msi_desc *desc)
  96. {
  97. arg->desc = desc;
  98. arg->hwirq = pci_msi_domain_calc_hwirq(desc);
  99. }
  100. static struct msi_domain_ops pci_msi_domain_ops_default = {
  101. .set_desc = pci_msi_domain_set_desc,
  102. .msi_check = pci_msi_domain_check_cap,
  103. };
  104. static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
  105. {
  106. struct msi_domain_ops *ops = info->ops;
  107. if (ops == NULL) {
  108. info->ops = &pci_msi_domain_ops_default;
  109. } else {
  110. if (ops->set_desc == NULL)
  111. ops->set_desc = pci_msi_domain_set_desc;
  112. if (ops->msi_check == NULL)
  113. ops->msi_check = pci_msi_domain_check_cap;
  114. }
  115. }
  116. static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
  117. {
  118. struct irq_chip *chip = info->chip;
  119. BUG_ON(!chip);
  120. if (!chip->irq_write_msi_msg)
  121. chip->irq_write_msi_msg = pci_msi_domain_write_msg;
  122. if (!chip->irq_mask)
  123. chip->irq_mask = pci_msi_mask_irq;
  124. if (!chip->irq_unmask)
  125. chip->irq_unmask = pci_msi_unmask_irq;
  126. }
  127. /**
  128. * pci_msi_create_irq_domain - Create a MSI interrupt domain
  129. * @fwnode: Optional fwnode of the interrupt controller
  130. * @info: MSI domain info
  131. * @parent: Parent irq domain
  132. *
  133. * Updates the domain and chip ops and creates a MSI interrupt domain.
  134. *
  135. * Returns:
  136. * A domain pointer or NULL in case of failure.
  137. */
  138. struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
  139. struct msi_domain_info *info,
  140. struct irq_domain *parent)
  141. {
  142. struct irq_domain *domain;
  143. if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
  144. info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
  145. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  146. pci_msi_domain_update_dom_ops(info);
  147. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  148. pci_msi_domain_update_chip_ops(info);
  149. info->flags |= MSI_FLAG_ACTIVATE_EARLY | MSI_FLAG_DEV_SYSFS;
  150. if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
  151. info->flags |= MSI_FLAG_MUST_REACTIVATE;
  152. /* PCI-MSI is oneshot-safe */
  153. info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
  154. domain = msi_create_irq_domain(fwnode, info, parent);
  155. if (!domain)
  156. return NULL;
  157. irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
  158. return domain;
  159. }
  160. EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
  161. /*
  162. * Users of the generic MSI infrastructure expect a device to have a single ID,
  163. * so with DMA aliases we have to pick the least-worst compromise. Devices with
  164. * DMA phantom functions tend to still emit MSIs from the real function number,
  165. * so we ignore those and only consider topological aliases where either the
  166. * alias device or RID appears on a different bus number. We also make the
  167. * reasonable assumption that bridges are walked in an upstream direction (so
  168. * the last one seen wins), and the much braver assumption that the most likely
  169. * case is that of PCI->PCIe so we should always use the alias RID. This echoes
  170. * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
  171. * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
  172. * for taking ownership all we can really do is close our eyes and hope...
  173. */
  174. static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
  175. {
  176. u32 *pa = data;
  177. u8 bus = PCI_BUS_NUM(*pa);
  178. if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
  179. *pa = alias;
  180. return 0;
  181. }
  182. /**
  183. * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
  184. * @domain: The interrupt domain
  185. * @pdev: The PCI device.
  186. *
  187. * The RID for a device is formed from the alias, with a firmware
  188. * supplied mapping applied
  189. *
  190. * Returns: The RID.
  191. */
  192. u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
  193. {
  194. struct device_node *of_node;
  195. u32 rid = pci_dev_id(pdev);
  196. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  197. of_node = irq_domain_get_of_node(domain);
  198. rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
  199. iort_msi_map_id(&pdev->dev, rid);
  200. return rid;
  201. }
  202. /**
  203. * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
  204. * @pdev: The PCI device
  205. *
  206. * Use the firmware data to find a device-specific MSI domain
  207. * (i.e. not one that is set as a default).
  208. *
  209. * Returns: The corresponding MSI domain or NULL if none has been found.
  210. */
  211. struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
  212. {
  213. struct irq_domain *dom;
  214. u32 rid = pci_dev_id(pdev);
  215. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  216. dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
  217. if (!dom)
  218. dom = iort_get_device_domain(&pdev->dev, rid,
  219. DOMAIN_BUS_PCI_MSI);
  220. return dom;
  221. }
  222. /**
  223. * pci_dev_has_special_msi_domain - Check whether the device is handled by
  224. * a non-standard PCI-MSI domain
  225. * @pdev: The PCI device to check.
  226. *
  227. * Returns: True if the device irqdomain or the bus irqdomain is
  228. * non-standard PCI/MSI.
  229. */
  230. bool pci_dev_has_special_msi_domain(struct pci_dev *pdev)
  231. {
  232. struct irq_domain *dom = dev_get_msi_domain(&pdev->dev);
  233. if (!dom)
  234. dom = dev_get_msi_domain(&pdev->bus->dev);
  235. if (!dom)
  236. return true;
  237. return dom->bus_token != DOMAIN_BUS_PCI_MSI;
  238. }