api.c 13 KB

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