api.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI MSI/MSI-X — Exported APIs for device drivers
  4. *
  5. * Copyright (C) 2003-2004 Intel
  6. * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
  7. * Copyright (C) 2016 Christoph Hellwig.
  8. * Copyright (C) 2022 Linutronix GmbH
  9. */
  10. #include <linux/export.h>
  11. #include <linux/irq.h>
  12. #include "msi.h"
  13. /**
  14. * pci_enable_msi() - Enable MSI interrupt mode on device
  15. * @dev: the PCI device to operate on
  16. *
  17. * Legacy device driver API to enable MSI interrupts mode on device and
  18. * allocate a single interrupt vector. On success, the allocated vector
  19. * Linux IRQ will be saved at @dev->irq. The driver must invoke
  20. * pci_disable_msi() on cleanup.
  21. *
  22. * NOTE: The newer pci_alloc_irq_vectors() / pci_free_irq_vectors() API
  23. * pair should, in general, be used instead.
  24. *
  25. * Return: 0 on success, errno otherwise
  26. */
  27. int pci_enable_msi(struct pci_dev *dev)
  28. {
  29. int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
  30. if (rc < 0)
  31. return rc;
  32. return 0;
  33. }
  34. EXPORT_SYMBOL(pci_enable_msi);
  35. /**
  36. * pci_disable_msi() - Disable MSI interrupt mode on device
  37. * @dev: the PCI device to operate on
  38. *
  39. * Legacy device driver API to disable MSI interrupt mode on device,
  40. * free earlier allocated interrupt vectors, and restore INTx emulation.
  41. * The PCI device Linux IRQ (@dev->irq) is restored to its default
  42. * pin-assertion IRQ. This is the cleanup pair of pci_enable_msi().
  43. *
  44. * NOTE: The newer pci_alloc_irq_vectors() / pci_free_irq_vectors() API
  45. * pair should, in general, be used instead.
  46. */
  47. void pci_disable_msi(struct pci_dev *dev)
  48. {
  49. if (!pci_msi_enabled() || !dev || !dev->msi_enabled)
  50. return;
  51. guard(msi_descs_lock)(&dev->dev);
  52. pci_msi_shutdown(dev);
  53. pci_free_msi_irqs(dev);
  54. }
  55. EXPORT_SYMBOL(pci_disable_msi);
  56. /**
  57. * pci_msix_vec_count() - Get number of MSI-X interrupt vectors on device
  58. * @dev: the PCI device to operate on
  59. *
  60. * Return: number of MSI-X interrupt vectors available on this device
  61. * (i.e., the device's MSI-X capability structure "table size"), -EINVAL
  62. * if the device is not MSI-X capable, other errnos otherwise.
  63. */
  64. int pci_msix_vec_count(struct pci_dev *dev)
  65. {
  66. u16 control;
  67. if (!dev->msix_cap)
  68. return -EINVAL;
  69. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  70. return msix_table_size(control);
  71. }
  72. EXPORT_SYMBOL(pci_msix_vec_count);
  73. /**
  74. * pci_enable_msix_range() - Enable MSI-X interrupt mode on device
  75. * @dev: the PCI device to operate on
  76. * @entries: input/output parameter, array of MSI-X configuration entries
  77. * @minvec: minimum required number of MSI-X vectors
  78. * @maxvec: maximum desired number of MSI-X vectors
  79. *
  80. * Legacy device driver API to enable MSI-X interrupt mode on device and
  81. * configure its MSI-X capability structure as appropriate. The passed
  82. * @entries array must have each of its members "entry" field set to a
  83. * desired (valid) MSI-X vector number, where the range of valid MSI-X
  84. * vector numbers can be queried through pci_msix_vec_count(). If
  85. * successful, the driver must invoke pci_disable_msix() on cleanup.
  86. *
  87. * NOTE: The newer pci_alloc_irq_vectors() / pci_free_irq_vectors() API
  88. * pair should, in general, be used instead.
  89. *
  90. * Return: number of MSI-X vectors allocated (which might be smaller
  91. * than @maxvecs), where Linux IRQ numbers for such allocated vectors
  92. * are saved back in the @entries array elements' "vector" field. Return
  93. * -ENOSPC if less than @minvecs interrupt vectors are available.
  94. * Return -EINVAL if one of the passed @entries members "entry" field
  95. * was invalid or a duplicate, or if plain MSI interrupts mode was
  96. * earlier enabled on device. Return other errnos otherwise.
  97. */
  98. int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
  99. int minvec, int maxvec)
  100. {
  101. return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
  102. }
  103. EXPORT_SYMBOL(pci_enable_msix_range);
  104. /**
  105. * pci_msix_can_alloc_dyn - Query whether dynamic allocation after enabling
  106. * MSI-X is supported
  107. *
  108. * @dev: PCI device to operate on
  109. *
  110. * Return: True if supported, false otherwise
  111. */
  112. bool pci_msix_can_alloc_dyn(struct pci_dev *dev)
  113. {
  114. if (!dev->msix_cap)
  115. return false;
  116. return pci_msi_domain_supports(dev, MSI_FLAG_PCI_MSIX_ALLOC_DYN, DENY_LEGACY);
  117. }
  118. EXPORT_SYMBOL_GPL(pci_msix_can_alloc_dyn);
  119. /**
  120. * pci_msix_alloc_irq_at - Allocate an MSI-X interrupt after enabling MSI-X
  121. * at a given MSI-X vector index or any free vector index
  122. *
  123. * @dev: PCI device to operate on
  124. * @index: Index to allocate. If @index == MSI_ANY_INDEX this allocates
  125. * the next free index in the MSI-X table
  126. * @affdesc: Optional pointer to an affinity descriptor structure. NULL otherwise
  127. *
  128. * Return: A struct msi_map
  129. *
  130. * On success msi_map::index contains the allocated index (>= 0) and
  131. * msi_map::virq contains the allocated Linux interrupt number (> 0).
  132. *
  133. * On fail msi_map::index contains the error code and msi_map::virq
  134. * is set to 0.
  135. */
  136. struct msi_map pci_msix_alloc_irq_at(struct pci_dev *dev, unsigned int index,
  137. const struct irq_affinity_desc *affdesc)
  138. {
  139. struct msi_map map = { .index = -ENOTSUPP };
  140. if (!dev->msix_enabled)
  141. return map;
  142. if (!pci_msix_can_alloc_dyn(dev))
  143. return map;
  144. return msi_domain_alloc_irq_at(&dev->dev, MSI_DEFAULT_DOMAIN, index, affdesc, NULL);
  145. }
  146. EXPORT_SYMBOL_GPL(pci_msix_alloc_irq_at);
  147. /**
  148. * pci_msix_free_irq - Free an interrupt on a PCI/MSI-X interrupt domain
  149. *
  150. * @dev: The PCI device to operate on
  151. * @map: A struct msi_map describing the interrupt to free
  152. *
  153. * Undo an interrupt vector allocation. Does not disable MSI-X.
  154. */
  155. void pci_msix_free_irq(struct pci_dev *dev, struct msi_map map)
  156. {
  157. if (WARN_ON_ONCE(map.index < 0 || map.virq <= 0))
  158. return;
  159. if (WARN_ON_ONCE(!pci_msix_can_alloc_dyn(dev)))
  160. return;
  161. msi_domain_free_irqs_range(&dev->dev, MSI_DEFAULT_DOMAIN, map.index, map.index);
  162. }
  163. EXPORT_SYMBOL_GPL(pci_msix_free_irq);
  164. /**
  165. * pci_disable_msix() - Disable MSI-X interrupt mode on device
  166. * @dev: the PCI device to operate on
  167. *
  168. * Legacy device driver API to disable MSI-X interrupt mode on device,
  169. * free earlier-allocated interrupt vectors, and restore INTx.
  170. * The PCI device Linux IRQ (@dev->irq) is restored to its default pin
  171. * assertion IRQ. This is the cleanup pair of pci_enable_msix_range().
  172. *
  173. * NOTE: The newer pci_alloc_irq_vectors() / pci_free_irq_vectors() API
  174. * pair should, in general, be used instead.
  175. */
  176. void pci_disable_msix(struct pci_dev *dev)
  177. {
  178. if (!pci_msi_enabled() || !dev || !dev->msix_enabled)
  179. return;
  180. guard(msi_descs_lock)(&dev->dev);
  181. pci_msix_shutdown(dev);
  182. pci_free_msi_irqs(dev);
  183. }
  184. EXPORT_SYMBOL(pci_disable_msix);
  185. /**
  186. * pci_alloc_irq_vectors() - Allocate multiple device interrupt vectors
  187. * @dev: the PCI device to operate on
  188. * @min_vecs: minimum required number of vectors (must be >= 1)
  189. * @max_vecs: maximum desired number of vectors
  190. * @flags: One or more of:
  191. *
  192. * * %PCI_IRQ_MSIX Allow trying MSI-X vector allocations
  193. * * %PCI_IRQ_MSI Allow trying MSI vector allocations
  194. *
  195. * * %PCI_IRQ_INTX Allow trying INTx interrupts, if and
  196. * only if @min_vecs == 1
  197. *
  198. * * %PCI_IRQ_AFFINITY Auto-manage IRQs affinity by spreading
  199. * the vectors around available CPUs
  200. *
  201. * Allocate up to @max_vecs interrupt vectors on device. MSI-X irq
  202. * vector allocation has a higher precedence over plain MSI, which has a
  203. * higher precedence over legacy INTx emulation.
  204. *
  205. * Upon a successful allocation, the caller should use pci_irq_vector()
  206. * to get the Linux IRQ number to be passed to request_threaded_irq().
  207. * The driver must call pci_free_irq_vectors() on cleanup.
  208. *
  209. * Return: number of allocated vectors (which might be smaller than
  210. * @max_vecs), -ENOSPC if less than @min_vecs interrupt vectors are
  211. * available, other errnos otherwise.
  212. */
  213. int pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
  214. unsigned int max_vecs, unsigned int flags)
  215. {
  216. return pci_alloc_irq_vectors_affinity(dev, min_vecs, max_vecs,
  217. flags, NULL);
  218. }
  219. EXPORT_SYMBOL(pci_alloc_irq_vectors);
  220. /**
  221. * pci_alloc_irq_vectors_affinity() - Allocate multiple device interrupt
  222. * vectors with affinity requirements
  223. * @dev: the PCI device to operate on
  224. * @min_vecs: minimum required number of vectors (must be >= 1)
  225. * @max_vecs: maximum desired number of vectors
  226. * @flags: allocation flags, as in pci_alloc_irq_vectors()
  227. * @affd: affinity requirements (can be %NULL).
  228. *
  229. * Same as pci_alloc_irq_vectors(), but with the extra @affd parameter.
  230. * Check that function docs, and &struct irq_affinity, for more details.
  231. */
  232. int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
  233. unsigned int max_vecs, unsigned int flags,
  234. struct irq_affinity *affd)
  235. {
  236. struct irq_affinity msi_default_affd = {0};
  237. int nvecs = -ENOSPC;
  238. if (flags & PCI_IRQ_AFFINITY) {
  239. if (!affd)
  240. affd = &msi_default_affd;
  241. } else {
  242. if (WARN_ON(affd))
  243. affd = NULL;
  244. }
  245. if (flags & PCI_IRQ_MSIX) {
  246. nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
  247. affd, flags);
  248. if (nvecs > 0)
  249. return nvecs;
  250. }
  251. if (flags & PCI_IRQ_MSI) {
  252. nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
  253. if (nvecs > 0)
  254. return nvecs;
  255. }
  256. /* use INTx IRQ if allowed */
  257. if (flags & PCI_IRQ_INTX) {
  258. if (min_vecs == 1 && dev->irq) {
  259. /*
  260. * Invoke the affinity spreading logic to ensure that
  261. * the device driver can adjust queue configuration
  262. * for the single interrupt case.
  263. */
  264. if (affd)
  265. irq_create_affinity_masks(1, affd);
  266. pci_intx(dev, 1);
  267. return 1;
  268. }
  269. }
  270. return nvecs;
  271. }
  272. EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
  273. /**
  274. * pci_irq_vector() - Get Linux IRQ number of a device interrupt vector
  275. * @dev: the PCI device to operate on
  276. * @nr: device-relative interrupt vector index (0-based); has different
  277. * meanings, depending on interrupt mode:
  278. *
  279. * * MSI-X the index in the MSI-X vector table
  280. * * MSI the index of the enabled MSI vectors
  281. * * INTx must be 0
  282. *
  283. * Return: the Linux IRQ number, or -EINVAL if @nr is out of range
  284. */
  285. int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
  286. {
  287. unsigned int irq;
  288. if (!dev->msi_enabled && !dev->msix_enabled)
  289. return !nr ? dev->irq : -EINVAL;
  290. irq = msi_get_virq(&dev->dev, nr);
  291. return irq ? irq : -EINVAL;
  292. }
  293. EXPORT_SYMBOL(pci_irq_vector);
  294. /**
  295. * pci_irq_get_affinity() - Get a device interrupt vector affinity
  296. * @dev: the PCI device to operate on
  297. * @nr: device-relative interrupt vector index (0-based); has different
  298. * meanings, depending on interrupt mode:
  299. *
  300. * * MSI-X the index in the MSI-X vector table
  301. * * MSI the index of the enabled MSI vectors
  302. * * INTx must be 0
  303. *
  304. * Return: MSI/MSI-X vector affinity, NULL if @nr is out of range or if
  305. * the MSI(-X) vector was allocated without explicit affinity
  306. * requirements (e.g., by pci_enable_msi(), pci_enable_msix_range(), or
  307. * pci_alloc_irq_vectors() without the %PCI_IRQ_AFFINITY flag). Return a
  308. * generic set of CPU IDs representing all possible CPUs available
  309. * during system boot if the device is in legacy INTx mode.
  310. */
  311. const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
  312. {
  313. int idx, irq = pci_irq_vector(dev, nr);
  314. struct msi_desc *desc;
  315. if (WARN_ON_ONCE(irq <= 0))
  316. return NULL;
  317. desc = irq_get_msi_desc(irq);
  318. /* Non-MSI does not have the information handy */
  319. if (!desc)
  320. return cpu_possible_mask;
  321. /* MSI[X] interrupts can be allocated without affinity descriptor */
  322. if (!desc->affinity)
  323. return NULL;
  324. /*
  325. * MSI has a mask array in the descriptor.
  326. * MSI-X has a single mask.
  327. */
  328. idx = dev->msi_enabled ? nr : 0;
  329. return &desc->affinity[idx].mask;
  330. }
  331. EXPORT_SYMBOL(pci_irq_get_affinity);
  332. /**
  333. * pci_free_irq_vectors() - Free previously allocated IRQs for a device
  334. * @dev: the PCI device to operate on
  335. *
  336. * Undo the interrupt vector allocations and possible device MSI/MSI-X
  337. * enablement earlier done through pci_alloc_irq_vectors_affinity() or
  338. * pci_alloc_irq_vectors().
  339. */
  340. void pci_free_irq_vectors(struct pci_dev *dev)
  341. {
  342. pci_disable_msix(dev);
  343. pci_disable_msi(dev);
  344. }
  345. EXPORT_SYMBOL(pci_free_irq_vectors);
  346. /**
  347. * pci_restore_msi_state() - Restore cached MSI(-X) state on device
  348. * @dev: the PCI device to operate on
  349. *
  350. * Write the Linux-cached MSI(-X) state back on device. This is
  351. * typically useful upon system resume, or after an error-recovery PCI
  352. * adapter reset.
  353. */
  354. void pci_restore_msi_state(struct pci_dev *dev)
  355. {
  356. __pci_restore_msi_state(dev);
  357. __pci_restore_msix_state(dev);
  358. }
  359. EXPORT_SYMBOL_GPL(pci_restore_msi_state);
  360. /**
  361. * pci_msi_enabled() - Are MSI(-X) interrupts enabled system-wide?
  362. *
  363. * Return: true if MSI has not been globally disabled through ACPI FADT,
  364. * PCI bridge quirks, or the "pci=nomsi" kernel command-line option.
  365. */
  366. bool pci_msi_enabled(void)
  367. {
  368. return pci_msi_enable;
  369. }
  370. EXPORT_SYMBOL(pci_msi_enabled);