irqdomain.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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_all_locked(&dev->dev, MSI_DEFAULT_DOMAIN, 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_all_locked(&dev->dev, MSI_DEFAULT_DOMAIN);
  23. } else {
  24. pci_msi_legacy_teardown_msi_irqs(dev);
  25. msi_free_msi_descs(&dev->dev);
  26. }
  27. }
  28. /**
  29. * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
  30. * @irq_data: Pointer to interrupt data of the MSI interrupt
  31. * @msg: Pointer to the message
  32. */
  33. static void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
  34. {
  35. struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
  36. /*
  37. * For MSI-X desc->irq is always equal to irq_data->irq. For
  38. * MSI only the first interrupt of MULTI MSI passes the test.
  39. */
  40. if (desc->irq == irq_data->irq)
  41. __pci_write_msi_msg(desc, msg);
  42. }
  43. /**
  44. * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
  45. * @desc: Pointer to the MSI descriptor
  46. *
  47. * The ID number is only used within the irqdomain.
  48. */
  49. static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
  50. {
  51. struct pci_dev *dev = msi_desc_to_pci_dev(desc);
  52. return (irq_hw_number_t)desc->msi_index |
  53. pci_dev_id(dev) << 11 |
  54. ((irq_hw_number_t)(pci_domain_nr(dev->bus) & 0xFFFFFFFF)) << 27;
  55. }
  56. static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
  57. struct msi_desc *desc)
  58. {
  59. arg->desc = desc;
  60. arg->hwirq = pci_msi_domain_calc_hwirq(desc);
  61. }
  62. static struct msi_domain_ops pci_msi_domain_ops_default = {
  63. .set_desc = pci_msi_domain_set_desc,
  64. };
  65. static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
  66. {
  67. struct msi_domain_ops *ops = info->ops;
  68. if (ops == NULL) {
  69. info->ops = &pci_msi_domain_ops_default;
  70. } else {
  71. if (ops->set_desc == NULL)
  72. ops->set_desc = pci_msi_domain_set_desc;
  73. }
  74. }
  75. static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
  76. {
  77. struct irq_chip *chip = info->chip;
  78. BUG_ON(!chip);
  79. if (!chip->irq_write_msi_msg)
  80. chip->irq_write_msi_msg = pci_msi_domain_write_msg;
  81. if (!chip->irq_mask)
  82. chip->irq_mask = pci_msi_mask_irq;
  83. if (!chip->irq_unmask)
  84. chip->irq_unmask = pci_msi_unmask_irq;
  85. }
  86. /**
  87. * pci_msi_create_irq_domain - Create a MSI interrupt domain
  88. * @fwnode: Optional fwnode of the interrupt controller
  89. * @info: MSI domain info
  90. * @parent: Parent irq domain
  91. *
  92. * Updates the domain and chip ops and creates a MSI interrupt domain.
  93. *
  94. * Returns:
  95. * A domain pointer or NULL in case of failure.
  96. */
  97. struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
  98. struct msi_domain_info *info,
  99. struct irq_domain *parent)
  100. {
  101. if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
  102. info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
  103. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  104. pci_msi_domain_update_dom_ops(info);
  105. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  106. pci_msi_domain_update_chip_ops(info);
  107. /* Let the core code free MSI descriptors when freeing interrupts */
  108. info->flags |= MSI_FLAG_FREE_MSI_DESCS;
  109. info->flags |= MSI_FLAG_ACTIVATE_EARLY | MSI_FLAG_DEV_SYSFS;
  110. if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
  111. info->flags |= MSI_FLAG_MUST_REACTIVATE;
  112. /* PCI-MSI is oneshot-safe */
  113. info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
  114. /* Let the core update the bus token */
  115. info->bus_token = DOMAIN_BUS_PCI_MSI;
  116. return msi_create_irq_domain(fwnode, info, parent);
  117. }
  118. EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
  119. /*
  120. * Per device MSI[-X] domain functionality
  121. */
  122. static void pci_device_domain_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  123. {
  124. arg->desc = desc;
  125. arg->hwirq = desc->msi_index;
  126. }
  127. static __always_inline void cond_mask_parent(struct irq_data *data)
  128. {
  129. struct msi_domain_info *info = data->domain->host_data;
  130. if (unlikely(info->flags & MSI_FLAG_PCI_MSI_MASK_PARENT))
  131. irq_chip_mask_parent(data);
  132. }
  133. static __always_inline void cond_unmask_parent(struct irq_data *data)
  134. {
  135. struct msi_domain_info *info = data->domain->host_data;
  136. if (unlikely(info->flags & MSI_FLAG_PCI_MSI_MASK_PARENT))
  137. irq_chip_unmask_parent(data);
  138. }
  139. static void pci_irq_mask_msi(struct irq_data *data)
  140. {
  141. struct msi_desc *desc = irq_data_get_msi_desc(data);
  142. pci_msi_mask(desc, BIT(data->irq - desc->irq));
  143. cond_mask_parent(data);
  144. }
  145. static void pci_irq_unmask_msi(struct irq_data *data)
  146. {
  147. struct msi_desc *desc = irq_data_get_msi_desc(data);
  148. cond_unmask_parent(data);
  149. pci_msi_unmask(desc, BIT(data->irq - desc->irq));
  150. }
  151. #ifdef CONFIG_GENERIC_IRQ_RESERVATION_MODE
  152. # define MSI_REACTIVATE MSI_FLAG_MUST_REACTIVATE
  153. #else
  154. # define MSI_REACTIVATE 0
  155. #endif
  156. #define MSI_COMMON_FLAGS (MSI_FLAG_FREE_MSI_DESCS | \
  157. MSI_FLAG_ACTIVATE_EARLY | \
  158. MSI_FLAG_DEV_SYSFS | \
  159. MSI_REACTIVATE)
  160. static const struct msi_domain_template pci_msi_template = {
  161. .chip = {
  162. .name = "PCI-MSI",
  163. .irq_mask = pci_irq_mask_msi,
  164. .irq_unmask = pci_irq_unmask_msi,
  165. .irq_write_msi_msg = pci_msi_domain_write_msg,
  166. .flags = IRQCHIP_ONESHOT_SAFE,
  167. },
  168. .ops = {
  169. .set_desc = pci_device_domain_set_desc,
  170. },
  171. .info = {
  172. .flags = MSI_COMMON_FLAGS | MSI_FLAG_MULTI_PCI_MSI,
  173. .bus_token = DOMAIN_BUS_PCI_DEVICE_MSI,
  174. },
  175. };
  176. static void pci_irq_mask_msix(struct irq_data *data)
  177. {
  178. pci_msix_mask(irq_data_get_msi_desc(data));
  179. cond_mask_parent(data);
  180. }
  181. static void pci_irq_unmask_msix(struct irq_data *data)
  182. {
  183. cond_unmask_parent(data);
  184. pci_msix_unmask(irq_data_get_msi_desc(data));
  185. }
  186. static void pci_msix_prepare_desc(struct irq_domain *domain, msi_alloc_info_t *arg,
  187. struct msi_desc *desc)
  188. {
  189. /* Don't fiddle with preallocated MSI descriptors */
  190. if (!desc->pci.mask_base)
  191. msix_prepare_msi_desc(to_pci_dev(desc->dev), desc);
  192. }
  193. static const struct msi_domain_template pci_msix_template = {
  194. .chip = {
  195. .name = "PCI-MSIX",
  196. .irq_mask = pci_irq_mask_msix,
  197. .irq_unmask = pci_irq_unmask_msix,
  198. .irq_write_msi_msg = pci_msi_domain_write_msg,
  199. .flags = IRQCHIP_ONESHOT_SAFE,
  200. },
  201. .ops = {
  202. .prepare_desc = pci_msix_prepare_desc,
  203. .set_desc = pci_device_domain_set_desc,
  204. },
  205. .info = {
  206. .flags = MSI_COMMON_FLAGS | MSI_FLAG_PCI_MSIX |
  207. MSI_FLAG_PCI_MSIX_ALLOC_DYN,
  208. .bus_token = DOMAIN_BUS_PCI_DEVICE_MSIX,
  209. },
  210. };
  211. static bool pci_match_device_domain(struct pci_dev *pdev, enum irq_domain_bus_token bus_token)
  212. {
  213. return msi_match_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN, bus_token);
  214. }
  215. static bool pci_create_device_domain(struct pci_dev *pdev, const struct msi_domain_template *tmpl,
  216. unsigned int hwsize)
  217. {
  218. struct irq_domain *domain = dev_get_msi_domain(&pdev->dev);
  219. if (!domain || !irq_domain_is_msi_parent(domain))
  220. return true;
  221. return msi_create_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN, tmpl,
  222. hwsize, NULL, NULL);
  223. }
  224. /**
  225. * pci_setup_msi_device_domain - Setup a device MSI interrupt domain
  226. * @pdev: The PCI device to create the domain on
  227. *
  228. * Return:
  229. * True when:
  230. * - The device does not have a MSI parent irq domain associated,
  231. * which keeps the legacy architecture specific and the global
  232. * PCI/MSI domain models working
  233. * - The MSI domain exists already
  234. * - The MSI domain was successfully allocated
  235. * False when:
  236. * - MSI-X is enabled
  237. * - The domain creation fails.
  238. *
  239. * The created MSI domain is preserved until:
  240. * - The device is removed
  241. * - MSI is disabled and a MSI-X domain is created
  242. */
  243. bool pci_setup_msi_device_domain(struct pci_dev *pdev)
  244. {
  245. if (WARN_ON_ONCE(pdev->msix_enabled))
  246. return false;
  247. if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSI))
  248. return true;
  249. if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSIX))
  250. msi_remove_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN);
  251. return pci_create_device_domain(pdev, &pci_msi_template, 1);
  252. }
  253. /**
  254. * pci_setup_msix_device_domain - Setup a device MSI-X interrupt domain
  255. * @pdev: The PCI device to create the domain on
  256. * @hwsize: The size of the MSI-X vector table
  257. *
  258. * Return:
  259. * True when:
  260. * - The device does not have a MSI parent irq domain associated,
  261. * which keeps the legacy architecture specific and the global
  262. * PCI/MSI domain models working
  263. * - The MSI-X domain exists already
  264. * - The MSI-X domain was successfully allocated
  265. * False when:
  266. * - MSI is enabled
  267. * - The domain creation fails.
  268. *
  269. * The created MSI-X domain is preserved until:
  270. * - The device is removed
  271. * - MSI-X is disabled and a MSI domain is created
  272. */
  273. bool pci_setup_msix_device_domain(struct pci_dev *pdev, unsigned int hwsize)
  274. {
  275. if (WARN_ON_ONCE(pdev->msi_enabled))
  276. return false;
  277. if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSIX))
  278. return true;
  279. if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSI))
  280. msi_remove_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN);
  281. return pci_create_device_domain(pdev, &pci_msix_template, hwsize);
  282. }
  283. /**
  284. * pci_msi_domain_supports - Check for support of a particular feature flag
  285. * @pdev: The PCI device to operate on
  286. * @feature_mask: The feature mask to check for (full match)
  287. * @mode: If ALLOW_LEGACY this grants the feature when there is no irq domain
  288. * associated to the device. If DENY_LEGACY the lack of an irq domain
  289. * makes the feature unsupported
  290. */
  291. bool pci_msi_domain_supports(struct pci_dev *pdev, unsigned int feature_mask,
  292. enum support_mode mode)
  293. {
  294. struct msi_domain_info *info;
  295. struct irq_domain *domain;
  296. unsigned int supported;
  297. domain = dev_get_msi_domain(&pdev->dev);
  298. if (!domain || !irq_domain_is_hierarchy(domain)) {
  299. if (IS_ENABLED(CONFIG_PCI_MSI_ARCH_FALLBACKS))
  300. return mode == ALLOW_LEGACY;
  301. return false;
  302. }
  303. if (!irq_domain_is_msi_parent(domain)) {
  304. /*
  305. * For "global" PCI/MSI interrupt domains the associated
  306. * msi_domain_info::flags is the authoritative source of
  307. * information.
  308. */
  309. info = domain->host_data;
  310. supported = info->flags;
  311. } else {
  312. /*
  313. * For MSI parent domains the supported feature set
  314. * is available in the parent ops. This makes checks
  315. * possible before actually instantiating the
  316. * per device domain because the parent is never
  317. * expanding the PCI/MSI functionality.
  318. */
  319. supported = domain->msi_parent_ops->supported_flags;
  320. }
  321. return (supported & feature_mask) == feature_mask;
  322. }
  323. /*
  324. * Users of the generic MSI infrastructure expect a device to have a single ID,
  325. * so with DMA aliases we have to pick the least-worst compromise. Devices with
  326. * DMA phantom functions tend to still emit MSIs from the real function number,
  327. * so we ignore those and only consider topological aliases where either the
  328. * alias device or RID appears on a different bus number. We also make the
  329. * reasonable assumption that bridges are walked in an upstream direction (so
  330. * the last one seen wins), and the much braver assumption that the most likely
  331. * case is that of PCI->PCIe so we should always use the alias RID. This echoes
  332. * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
  333. * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
  334. * for taking ownership all we can really do is close our eyes and hope...
  335. */
  336. static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
  337. {
  338. u32 *pa = data;
  339. u8 bus = PCI_BUS_NUM(*pa);
  340. if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
  341. *pa = alias;
  342. return 0;
  343. }
  344. /**
  345. * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
  346. * @domain: The interrupt domain
  347. * @pdev: The PCI device.
  348. *
  349. * The RID for a device is formed from the alias, with a firmware
  350. * supplied mapping applied
  351. *
  352. * Returns: The RID.
  353. */
  354. u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
  355. {
  356. struct device_node *of_node;
  357. u32 rid = pci_dev_id(pdev);
  358. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  359. of_node = irq_domain_get_of_node(domain);
  360. rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
  361. iort_msi_map_id(&pdev->dev, rid);
  362. return rid;
  363. }
  364. /**
  365. * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
  366. * @pdev: The PCI device
  367. *
  368. * Use the firmware data to find a device-specific MSI domain
  369. * (i.e. not one that is set as a default).
  370. *
  371. * Returns: The corresponding MSI domain or NULL if none has been found.
  372. */
  373. struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
  374. {
  375. struct irq_domain *dom;
  376. u32 rid = pci_dev_id(pdev);
  377. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  378. dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
  379. if (!dom)
  380. dom = iort_get_device_domain(&pdev->dev, rid,
  381. DOMAIN_BUS_PCI_MSI);
  382. return dom;
  383. }