msi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LINUX_MSI_H
  3. #define LINUX_MSI_H
  4. /*
  5. * This header file contains MSI data structures and functions which are
  6. * only relevant for:
  7. * - Interrupt core code
  8. * - PCI/MSI core code
  9. * - MSI interrupt domain implementations
  10. * - IOMMU, low level VFIO, NTB and other justified exceptions
  11. * dealing with low level MSI details.
  12. *
  13. * Regular device drivers have no business with any of these functions and
  14. * especially storing MSI descriptor pointers in random code is considered
  15. * abuse. The only function which is relevant for drivers is msi_get_virq().
  16. */
  17. #include <linux/cpumask.h>
  18. #include <linux/xarray.h>
  19. #include <linux/mutex.h>
  20. #include <linux/list.h>
  21. #include <asm/msi.h>
  22. /* Dummy shadow structures if an architecture does not define them */
  23. #ifndef arch_msi_msg_addr_lo
  24. typedef struct arch_msi_msg_addr_lo {
  25. u32 address_lo;
  26. } __attribute__ ((packed)) arch_msi_msg_addr_lo_t;
  27. #endif
  28. #ifndef arch_msi_msg_addr_hi
  29. typedef struct arch_msi_msg_addr_hi {
  30. u32 address_hi;
  31. } __attribute__ ((packed)) arch_msi_msg_addr_hi_t;
  32. #endif
  33. #ifndef arch_msi_msg_data
  34. typedef struct arch_msi_msg_data {
  35. u32 data;
  36. } __attribute__ ((packed)) arch_msi_msg_data_t;
  37. #endif
  38. /**
  39. * msi_msg - Representation of a MSI message
  40. * @address_lo: Low 32 bits of msi message address
  41. * @arch_addrlo: Architecture specific shadow of @address_lo
  42. * @address_hi: High 32 bits of msi message address
  43. * (only used when device supports it)
  44. * @arch_addrhi: Architecture specific shadow of @address_hi
  45. * @data: MSI message data (usually 16 bits)
  46. * @arch_data: Architecture specific shadow of @data
  47. */
  48. struct msi_msg {
  49. union {
  50. u32 address_lo;
  51. arch_msi_msg_addr_lo_t arch_addr_lo;
  52. };
  53. union {
  54. u32 address_hi;
  55. arch_msi_msg_addr_hi_t arch_addr_hi;
  56. };
  57. union {
  58. u32 data;
  59. arch_msi_msg_data_t arch_data;
  60. };
  61. };
  62. extern int pci_msi_ignore_mask;
  63. /* Helper functions */
  64. struct irq_data;
  65. struct msi_desc;
  66. struct pci_dev;
  67. struct platform_msi_priv_data;
  68. struct device_attribute;
  69. void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
  70. #ifdef CONFIG_GENERIC_MSI_IRQ
  71. void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
  72. #else
  73. static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
  74. {
  75. }
  76. #endif
  77. typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
  78. struct msi_msg *msg);
  79. /**
  80. * pci_msi_desc - PCI/MSI specific MSI descriptor data
  81. *
  82. * @msi_mask: [PCI MSI] MSI cached mask bits
  83. * @msix_ctrl: [PCI MSI-X] MSI-X cached per vector control bits
  84. * @is_msix: [PCI MSI/X] True if MSI-X
  85. * @multiple: [PCI MSI/X] log2 num of messages allocated
  86. * @multi_cap: [PCI MSI/X] log2 num of messages supported
  87. * @can_mask: [PCI MSI/X] Masking supported?
  88. * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit
  89. * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq
  90. * @mask_pos: [PCI MSI] Mask register position
  91. * @mask_base: [PCI MSI-X] Mask register base address
  92. */
  93. struct pci_msi_desc {
  94. union {
  95. u32 msi_mask;
  96. u32 msix_ctrl;
  97. };
  98. struct {
  99. u8 is_msix : 1;
  100. u8 multiple : 3;
  101. u8 multi_cap : 3;
  102. u8 can_mask : 1;
  103. u8 is_64 : 1;
  104. u8 is_virtual : 1;
  105. unsigned default_irq;
  106. } msi_attrib;
  107. union {
  108. u8 mask_pos;
  109. void __iomem *mask_base;
  110. };
  111. };
  112. #define MSI_MAX_INDEX ((unsigned int)USHRT_MAX)
  113. /**
  114. * struct msi_desc - Descriptor structure for MSI based interrupts
  115. * @irq: The base interrupt number
  116. * @nvec_used: The number of vectors used
  117. * @dev: Pointer to the device which uses this descriptor
  118. * @msg: The last set MSI message cached for reuse
  119. * @affinity: Optional pointer to a cpu affinity mask for this descriptor
  120. * @sysfs_attr: Pointer to sysfs device attribute
  121. *
  122. * @write_msi_msg: Callback that may be called when the MSI message
  123. * address or data changes
  124. * @write_msi_msg_data: Data parameter for the callback.
  125. *
  126. * @msi_index: Index of the msi descriptor
  127. * @pci: PCI specific msi descriptor data
  128. */
  129. struct msi_desc {
  130. /* Shared device/bus type independent data */
  131. unsigned int irq;
  132. unsigned int nvec_used;
  133. struct device *dev;
  134. struct msi_msg msg;
  135. struct irq_affinity_desc *affinity;
  136. #ifdef CONFIG_IRQ_MSI_IOMMU
  137. const void *iommu_cookie;
  138. #endif
  139. #ifdef CONFIG_SYSFS
  140. struct device_attribute *sysfs_attrs;
  141. #endif
  142. void (*write_msi_msg)(struct msi_desc *entry, void *data);
  143. void *write_msi_msg_data;
  144. u16 msi_index;
  145. struct pci_msi_desc pci;
  146. };
  147. /*
  148. * Filter values for the MSI descriptor iterators and accessor functions.
  149. */
  150. enum msi_desc_filter {
  151. /* All descriptors */
  152. MSI_DESC_ALL,
  153. /* Descriptors which have no interrupt associated */
  154. MSI_DESC_NOTASSOCIATED,
  155. /* Descriptors which have an interrupt associated */
  156. MSI_DESC_ASSOCIATED,
  157. };
  158. /**
  159. * msi_device_data - MSI per device data
  160. * @properties: MSI properties which are interesting to drivers
  161. * @platform_data: Platform-MSI specific data
  162. * @mutex: Mutex protecting the MSI descriptor store
  163. * @__store: Xarray for storing MSI descriptor pointers
  164. * @__iter_idx: Index to search the next entry for iterators
  165. */
  166. struct msi_device_data {
  167. unsigned long properties;
  168. struct platform_msi_priv_data *platform_data;
  169. struct mutex mutex;
  170. struct xarray __store;
  171. unsigned long __iter_idx;
  172. };
  173. int msi_setup_device_data(struct device *dev);
  174. unsigned int msi_get_virq(struct device *dev, unsigned int index);
  175. void msi_lock_descs(struct device *dev);
  176. void msi_unlock_descs(struct device *dev);
  177. struct msi_desc *msi_first_desc(struct device *dev, enum msi_desc_filter filter);
  178. struct msi_desc *msi_next_desc(struct device *dev, enum msi_desc_filter filter);
  179. /**
  180. * msi_for_each_desc - Iterate the MSI descriptors
  181. *
  182. * @desc: struct msi_desc pointer used as iterator
  183. * @dev: struct device pointer - device to iterate
  184. * @filter: Filter for descriptor selection
  185. *
  186. * Notes:
  187. * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
  188. * pair.
  189. * - It is safe to remove a retrieved MSI descriptor in the loop.
  190. */
  191. #define msi_for_each_desc(desc, dev, filter) \
  192. for ((desc) = msi_first_desc((dev), (filter)); (desc); \
  193. (desc) = msi_next_desc((dev), (filter)))
  194. #define msi_desc_to_dev(desc) ((desc)->dev)
  195. #ifdef CONFIG_IRQ_MSI_IOMMU
  196. static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
  197. {
  198. return desc->iommu_cookie;
  199. }
  200. static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
  201. const void *iommu_cookie)
  202. {
  203. desc->iommu_cookie = iommu_cookie;
  204. }
  205. #else
  206. static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
  207. {
  208. return NULL;
  209. }
  210. static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
  211. const void *iommu_cookie)
  212. {
  213. }
  214. #endif
  215. #ifdef CONFIG_PCI_MSI
  216. struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
  217. void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
  218. #else /* CONFIG_PCI_MSI */
  219. static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
  220. {
  221. }
  222. #endif /* CONFIG_PCI_MSI */
  223. int msi_add_msi_desc(struct device *dev, struct msi_desc *init_desc);
  224. void msi_free_msi_descs_range(struct device *dev, enum msi_desc_filter filter,
  225. unsigned int first_index, unsigned int last_index);
  226. /**
  227. * msi_free_msi_descs - Free MSI descriptors of a device
  228. * @dev: Device to free the descriptors
  229. */
  230. static inline void msi_free_msi_descs(struct device *dev)
  231. {
  232. msi_free_msi_descs_range(dev, MSI_DESC_ALL, 0, MSI_MAX_INDEX);
  233. }
  234. void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
  235. void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
  236. void pci_msi_mask_irq(struct irq_data *data);
  237. void pci_msi_unmask_irq(struct irq_data *data);
  238. /*
  239. * The arch hooks to setup up msi irqs. Default functions are implemented
  240. * as weak symbols so that they /can/ be overriden by architecture specific
  241. * code if needed. These hooks can only be enabled by the architecture.
  242. *
  243. * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
  244. * stubs with warnings.
  245. */
  246. #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
  247. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
  248. void arch_teardown_msi_irq(unsigned int irq);
  249. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
  250. void arch_teardown_msi_irqs(struct pci_dev *dev);
  251. #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
  252. /*
  253. * Xen uses non-default msi_domain_ops and hence needs a way to populate sysfs
  254. * entries of MSI IRQs.
  255. */
  256. #if defined(CONFIG_PCI_XEN) || defined(CONFIG_PCI_MSI_ARCH_FALLBACKS)
  257. #ifdef CONFIG_SYSFS
  258. int msi_device_populate_sysfs(struct device *dev);
  259. void msi_device_destroy_sysfs(struct device *dev);
  260. #else /* CONFIG_SYSFS */
  261. static inline int msi_device_populate_sysfs(struct device *dev) { return 0; }
  262. static inline void msi_device_destroy_sysfs(struct device *dev) { }
  263. #endif /* !CONFIG_SYSFS */
  264. #endif /* CONFIG_PCI_XEN || CONFIG_PCI_MSI_ARCH_FALLBACKS */
  265. /*
  266. * The restore hook is still available even for fully irq domain based
  267. * setups. Courtesy to XEN/X86.
  268. */
  269. bool arch_restore_msi_irqs(struct pci_dev *dev);
  270. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  271. #include <linux/irqhandler.h>
  272. struct irq_domain;
  273. struct irq_domain_ops;
  274. struct irq_chip;
  275. struct device_node;
  276. struct fwnode_handle;
  277. struct msi_domain_info;
  278. /**
  279. * struct msi_domain_ops - MSI interrupt domain callbacks
  280. * @get_hwirq: Retrieve the resulting hw irq number
  281. * @msi_init: Domain specific init function for MSI interrupts
  282. * @msi_free: Domain specific function to free a MSI interrupts
  283. * @msi_check: Callback for verification of the domain/info/dev data
  284. * @msi_prepare: Prepare the allocation of the interrupts in the domain
  285. * @set_desc: Set the msi descriptor for an interrupt
  286. * @domain_alloc_irqs: Optional function to override the default allocation
  287. * function.
  288. * @domain_free_irqs: Optional function to override the default free
  289. * function.
  290. *
  291. * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying
  292. * irqdomain.
  293. *
  294. * @msi_check, @msi_prepare and @set_desc are callbacks used by
  295. * msi_domain_alloc/free_irqs().
  296. *
  297. * @domain_alloc_irqs, @domain_free_irqs can be used to override the
  298. * default allocation/free functions (__msi_domain_alloc/free_irqs). This
  299. * is initially for a wrapper around XENs seperate MSI universe which can't
  300. * be wrapped into the regular irq domains concepts by mere mortals. This
  301. * allows to universally use msi_domain_alloc/free_irqs without having to
  302. * special case XEN all over the place.
  303. *
  304. * Contrary to other operations @domain_alloc_irqs and @domain_free_irqs
  305. * are set to the default implementation if NULL and even when
  306. * MSI_FLAG_USE_DEF_DOM_OPS is not set to avoid breaking existing users and
  307. * because these callbacks are obviously mandatory.
  308. *
  309. * This is NOT meant to be abused, but it can be useful to build wrappers
  310. * for specialized MSI irq domains which need extra work before and after
  311. * calling __msi_domain_alloc_irqs()/__msi_domain_free_irqs().
  312. */
  313. struct msi_domain_ops {
  314. irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
  315. msi_alloc_info_t *arg);
  316. int (*msi_init)(struct irq_domain *domain,
  317. struct msi_domain_info *info,
  318. unsigned int virq, irq_hw_number_t hwirq,
  319. msi_alloc_info_t *arg);
  320. void (*msi_free)(struct irq_domain *domain,
  321. struct msi_domain_info *info,
  322. unsigned int virq);
  323. int (*msi_check)(struct irq_domain *domain,
  324. struct msi_domain_info *info,
  325. struct device *dev);
  326. int (*msi_prepare)(struct irq_domain *domain,
  327. struct device *dev, int nvec,
  328. msi_alloc_info_t *arg);
  329. void (*set_desc)(msi_alloc_info_t *arg,
  330. struct msi_desc *desc);
  331. int (*domain_alloc_irqs)(struct irq_domain *domain,
  332. struct device *dev, int nvec);
  333. void (*domain_free_irqs)(struct irq_domain *domain,
  334. struct device *dev);
  335. };
  336. /**
  337. * struct msi_domain_info - MSI interrupt domain data
  338. * @flags: Flags to decribe features and capabilities
  339. * @ops: The callback data structure
  340. * @chip: Optional: associated interrupt chip
  341. * @chip_data: Optional: associated interrupt chip data
  342. * @handler: Optional: associated interrupt flow handler
  343. * @handler_data: Optional: associated interrupt flow handler data
  344. * @handler_name: Optional: associated interrupt flow handler name
  345. * @data: Optional: domain specific data
  346. */
  347. struct msi_domain_info {
  348. u32 flags;
  349. struct msi_domain_ops *ops;
  350. struct irq_chip *chip;
  351. void *chip_data;
  352. irq_flow_handler_t handler;
  353. void *handler_data;
  354. const char *handler_name;
  355. void *data;
  356. };
  357. /* Flags for msi_domain_info */
  358. enum {
  359. /*
  360. * Init non implemented ops callbacks with default MSI domain
  361. * callbacks.
  362. */
  363. MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0),
  364. /*
  365. * Init non implemented chip callbacks with default MSI chip
  366. * callbacks.
  367. */
  368. MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1),
  369. /* Support multiple PCI MSI interrupts */
  370. MSI_FLAG_MULTI_PCI_MSI = (1 << 2),
  371. /* Support PCI MSIX interrupts */
  372. MSI_FLAG_PCI_MSIX = (1 << 3),
  373. /* Needs early activate, required for PCI */
  374. MSI_FLAG_ACTIVATE_EARLY = (1 << 4),
  375. /*
  376. * Must reactivate when irq is started even when
  377. * MSI_FLAG_ACTIVATE_EARLY has been set.
  378. */
  379. MSI_FLAG_MUST_REACTIVATE = (1 << 5),
  380. /* Is level-triggered capable, using two messages */
  381. MSI_FLAG_LEVEL_CAPABLE = (1 << 6),
  382. /* Populate sysfs on alloc() and destroy it on free() */
  383. MSI_FLAG_DEV_SYSFS = (1 << 7),
  384. /* MSI-X entries must be contiguous */
  385. MSI_FLAG_MSIX_CONTIGUOUS = (1 << 8),
  386. /* Allocate simple MSI descriptors */
  387. MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS = (1 << 9),
  388. /* Free MSI descriptors */
  389. MSI_FLAG_FREE_MSI_DESCS = (1 << 10),
  390. };
  391. int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
  392. bool force);
  393. struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
  394. struct msi_domain_info *info,
  395. struct irq_domain *parent);
  396. int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
  397. int nvec);
  398. int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev,
  399. int nvec);
  400. int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
  401. int nvec);
  402. void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
  403. void msi_domain_free_irqs_descs_locked(struct irq_domain *domain, struct device *dev);
  404. void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
  405. struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
  406. struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
  407. struct msi_domain_info *info,
  408. struct irq_domain *parent);
  409. int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
  410. irq_write_msi_msg_t write_msi_msg);
  411. void platform_msi_domain_free_irqs(struct device *dev);
  412. /* When an MSI domain is used as an intermediate domain */
  413. int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
  414. int nvec, msi_alloc_info_t *args);
  415. int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
  416. int virq, int nvec, msi_alloc_info_t *args);
  417. struct irq_domain *
  418. __platform_msi_create_device_domain(struct device *dev,
  419. unsigned int nvec,
  420. bool is_tree,
  421. irq_write_msi_msg_t write_msi_msg,
  422. const struct irq_domain_ops *ops,
  423. void *host_data);
  424. #define platform_msi_create_device_domain(dev, nvec, write, ops, data) \
  425. __platform_msi_create_device_domain(dev, nvec, false, write, ops, data)
  426. #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \
  427. __platform_msi_create_device_domain(dev, nvec, true, write, ops, data)
  428. int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq,
  429. unsigned int nr_irqs);
  430. void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq,
  431. unsigned int nvec);
  432. void *platform_msi_get_host_data(struct irq_domain *domain);
  433. #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
  434. #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
  435. struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
  436. struct msi_domain_info *info,
  437. struct irq_domain *parent);
  438. u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
  439. struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
  440. bool pci_dev_has_special_msi_domain(struct pci_dev *pdev);
  441. #else
  442. static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
  443. {
  444. return NULL;
  445. }
  446. #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
  447. #endif /* LINUX_MSI_H */