msi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LINUX_MSI_H
  3. #define LINUX_MSI_H
  4. #include <linux/kobject.h>
  5. #include <linux/list.h>
  6. struct msi_msg {
  7. u32 address_lo; /* low 32 bits of msi message address */
  8. u32 address_hi; /* high 32 bits of msi message address */
  9. u32 data; /* 16 bits of msi message data */
  10. };
  11. extern int pci_msi_ignore_mask;
  12. /* Helper functions */
  13. struct irq_data;
  14. struct msi_desc;
  15. struct pci_dev;
  16. struct platform_msi_priv_data;
  17. void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
  18. #ifdef CONFIG_GENERIC_MSI_IRQ
  19. void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
  20. #else
  21. static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
  22. {
  23. }
  24. #endif
  25. typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
  26. struct msi_msg *msg);
  27. /**
  28. * platform_msi_desc - Platform device specific msi descriptor data
  29. * @msi_priv_data: Pointer to platform private data
  30. * @msi_index: The index of the MSI descriptor for multi MSI
  31. */
  32. struct platform_msi_desc {
  33. struct platform_msi_priv_data *msi_priv_data;
  34. u16 msi_index;
  35. };
  36. /**
  37. * fsl_mc_msi_desc - FSL-MC device specific msi descriptor data
  38. * @msi_index: The index of the MSI descriptor
  39. */
  40. struct fsl_mc_msi_desc {
  41. u16 msi_index;
  42. };
  43. /**
  44. * struct msi_desc - Descriptor structure for MSI based interrupts
  45. * @list: List head for management
  46. * @irq: The base interrupt number
  47. * @nvec_used: The number of vectors used
  48. * @dev: Pointer to the device which uses this descriptor
  49. * @msg: The last set MSI message cached for reuse
  50. * @affinity: Optional pointer to a cpu affinity mask for this descriptor
  51. *
  52. * @masked: [PCI MSI/X] Mask bits
  53. * @is_msix: [PCI MSI/X] True if MSI-X
  54. * @multiple: [PCI MSI/X] log2 num of messages allocated
  55. * @multi_cap: [PCI MSI/X] log2 num of messages supported
  56. * @maskbit: [PCI MSI/X] Mask-Pending bit supported?
  57. * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit
  58. * @entry_nr: [PCI MSI/X] Entry which is described by this descriptor
  59. * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq
  60. * @mask_pos: [PCI MSI] Mask register position
  61. * @mask_base: [PCI MSI-X] Mask register base address
  62. * @platform: [platform] Platform device specific msi descriptor data
  63. * @fsl_mc: [fsl-mc] FSL MC device specific msi descriptor data
  64. */
  65. struct msi_desc {
  66. /* Shared device/bus type independent data */
  67. struct list_head list;
  68. unsigned int irq;
  69. unsigned int nvec_used;
  70. struct device *dev;
  71. struct msi_msg msg;
  72. struct cpumask *affinity;
  73. union {
  74. /* PCI MSI/X specific data */
  75. struct {
  76. u32 masked;
  77. struct {
  78. __u8 is_msix : 1;
  79. __u8 multiple : 3;
  80. __u8 multi_cap : 3;
  81. __u8 maskbit : 1;
  82. __u8 is_64 : 1;
  83. __u16 entry_nr;
  84. unsigned default_irq;
  85. } msi_attrib;
  86. union {
  87. u8 mask_pos;
  88. void __iomem *mask_base;
  89. };
  90. };
  91. /*
  92. * Non PCI variants add their data structure here. New
  93. * entries need to use a named structure. We want
  94. * proper name spaces for this. The PCI part is
  95. * anonymous for now as it would require an immediate
  96. * tree wide cleanup.
  97. */
  98. struct platform_msi_desc platform;
  99. struct fsl_mc_msi_desc fsl_mc;
  100. };
  101. };
  102. /* Helpers to hide struct msi_desc implementation details */
  103. #define msi_desc_to_dev(desc) ((desc)->dev)
  104. #define dev_to_msi_list(dev) (&(dev)->msi_list)
  105. #define first_msi_entry(dev) \
  106. list_first_entry(dev_to_msi_list((dev)), struct msi_desc, list)
  107. #define for_each_msi_entry(desc, dev) \
  108. list_for_each_entry((desc), dev_to_msi_list((dev)), list)
  109. #ifdef CONFIG_PCI_MSI
  110. #define first_pci_msi_entry(pdev) first_msi_entry(&(pdev)->dev)
  111. #define for_each_pci_msi_entry(desc, pdev) \
  112. for_each_msi_entry((desc), &(pdev)->dev)
  113. struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
  114. void *msi_desc_to_pci_sysdata(struct msi_desc *desc);
  115. void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
  116. #else /* CONFIG_PCI_MSI */
  117. static inline void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
  118. {
  119. return NULL;
  120. }
  121. static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
  122. {
  123. }
  124. #endif /* CONFIG_PCI_MSI */
  125. struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
  126. const struct cpumask *affinity);
  127. void free_msi_entry(struct msi_desc *entry);
  128. void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
  129. void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
  130. u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag);
  131. u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag);
  132. void pci_msi_mask_irq(struct irq_data *data);
  133. void pci_msi_unmask_irq(struct irq_data *data);
  134. /* Conversion helpers. Should be removed after merging */
  135. static inline void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  136. {
  137. __pci_write_msi_msg(entry, msg);
  138. }
  139. static inline void write_msi_msg(int irq, struct msi_msg *msg)
  140. {
  141. pci_write_msi_msg(irq, msg);
  142. }
  143. static inline void mask_msi_irq(struct irq_data *data)
  144. {
  145. pci_msi_mask_irq(data);
  146. }
  147. static inline void unmask_msi_irq(struct irq_data *data)
  148. {
  149. pci_msi_unmask_irq(data);
  150. }
  151. /*
  152. * The arch hooks to setup up msi irqs. Those functions are
  153. * implemented as weak symbols so that they /can/ be overriden by
  154. * architecture specific code if needed.
  155. */
  156. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
  157. void arch_teardown_msi_irq(unsigned int irq);
  158. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
  159. void arch_teardown_msi_irqs(struct pci_dev *dev);
  160. void arch_restore_msi_irqs(struct pci_dev *dev);
  161. void default_teardown_msi_irqs(struct pci_dev *dev);
  162. void default_restore_msi_irqs(struct pci_dev *dev);
  163. struct msi_controller {
  164. struct module *owner;
  165. struct device *dev;
  166. struct device_node *of_node;
  167. struct list_head list;
  168. int (*setup_irq)(struct msi_controller *chip, struct pci_dev *dev,
  169. struct msi_desc *desc);
  170. int (*setup_irqs)(struct msi_controller *chip, struct pci_dev *dev,
  171. int nvec, int type);
  172. void (*teardown_irq)(struct msi_controller *chip, unsigned int irq);
  173. };
  174. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  175. #include <linux/irqhandler.h>
  176. #include <asm/msi.h>
  177. struct irq_domain;
  178. struct irq_domain_ops;
  179. struct irq_chip;
  180. struct device_node;
  181. struct fwnode_handle;
  182. struct msi_domain_info;
  183. /**
  184. * struct msi_domain_ops - MSI interrupt domain callbacks
  185. * @get_hwirq: Retrieve the resulting hw irq number
  186. * @msi_init: Domain specific init function for MSI interrupts
  187. * @msi_free: Domain specific function to free a MSI interrupts
  188. * @msi_check: Callback for verification of the domain/info/dev data
  189. * @msi_prepare: Prepare the allocation of the interrupts in the domain
  190. * @msi_finish: Optional callback to finalize the allocation
  191. * @set_desc: Set the msi descriptor for an interrupt
  192. * @handle_error: Optional error handler if the allocation fails
  193. *
  194. * @get_hwirq, @msi_init and @msi_free are callbacks used by
  195. * msi_create_irq_domain() and related interfaces
  196. *
  197. * @msi_check, @msi_prepare, @msi_finish, @set_desc and @handle_error
  198. * are callbacks used by msi_domain_alloc_irqs() and related
  199. * interfaces which are based on msi_desc.
  200. */
  201. struct msi_domain_ops {
  202. irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
  203. msi_alloc_info_t *arg);
  204. int (*msi_init)(struct irq_domain *domain,
  205. struct msi_domain_info *info,
  206. unsigned int virq, irq_hw_number_t hwirq,
  207. msi_alloc_info_t *arg);
  208. void (*msi_free)(struct irq_domain *domain,
  209. struct msi_domain_info *info,
  210. unsigned int virq);
  211. int (*msi_check)(struct irq_domain *domain,
  212. struct msi_domain_info *info,
  213. struct device *dev);
  214. int (*msi_prepare)(struct irq_domain *domain,
  215. struct device *dev, int nvec,
  216. msi_alloc_info_t *arg);
  217. void (*msi_finish)(msi_alloc_info_t *arg, int retval);
  218. void (*set_desc)(msi_alloc_info_t *arg,
  219. struct msi_desc *desc);
  220. int (*handle_error)(struct irq_domain *domain,
  221. struct msi_desc *desc, int error);
  222. };
  223. /**
  224. * struct msi_domain_info - MSI interrupt domain data
  225. * @flags: Flags to decribe features and capabilities
  226. * @ops: The callback data structure
  227. * @chip: Optional: associated interrupt chip
  228. * @chip_data: Optional: associated interrupt chip data
  229. * @handler: Optional: associated interrupt flow handler
  230. * @handler_data: Optional: associated interrupt flow handler data
  231. * @handler_name: Optional: associated interrupt flow handler name
  232. * @data: Optional: domain specific data
  233. */
  234. struct msi_domain_info {
  235. u32 flags;
  236. struct msi_domain_ops *ops;
  237. struct irq_chip *chip;
  238. void *chip_data;
  239. irq_flow_handler_t handler;
  240. void *handler_data;
  241. const char *handler_name;
  242. void *data;
  243. };
  244. /* Flags for msi_domain_info */
  245. enum {
  246. /*
  247. * Init non implemented ops callbacks with default MSI domain
  248. * callbacks.
  249. */
  250. MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0),
  251. /*
  252. * Init non implemented chip callbacks with default MSI chip
  253. * callbacks.
  254. */
  255. MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1),
  256. /* Support multiple PCI MSI interrupts */
  257. MSI_FLAG_MULTI_PCI_MSI = (1 << 2),
  258. /* Support PCI MSIX interrupts */
  259. MSI_FLAG_PCI_MSIX = (1 << 3),
  260. /* Needs early activate, required for PCI */
  261. MSI_FLAG_ACTIVATE_EARLY = (1 << 4),
  262. /*
  263. * Must reactivate when irq is started even when
  264. * MSI_FLAG_ACTIVATE_EARLY has been set.
  265. */
  266. MSI_FLAG_MUST_REACTIVATE = (1 << 5),
  267. /* Is level-triggered capable, using two messages */
  268. MSI_FLAG_LEVEL_CAPABLE = (1 << 6),
  269. };
  270. int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
  271. bool force);
  272. struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
  273. struct msi_domain_info *info,
  274. struct irq_domain *parent);
  275. int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
  276. int nvec);
  277. void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
  278. struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
  279. struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
  280. struct msi_domain_info *info,
  281. struct irq_domain *parent);
  282. int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
  283. irq_write_msi_msg_t write_msi_msg);
  284. void platform_msi_domain_free_irqs(struct device *dev);
  285. /* When an MSI domain is used as an intermediate domain */
  286. int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
  287. int nvec, msi_alloc_info_t *args);
  288. int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
  289. int virq, int nvec, msi_alloc_info_t *args);
  290. struct irq_domain *
  291. platform_msi_create_device_domain(struct device *dev,
  292. unsigned int nvec,
  293. irq_write_msi_msg_t write_msi_msg,
  294. const struct irq_domain_ops *ops,
  295. void *host_data);
  296. int platform_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
  297. unsigned int nr_irqs);
  298. void platform_msi_domain_free(struct irq_domain *domain, unsigned int virq,
  299. unsigned int nvec);
  300. void *platform_msi_get_host_data(struct irq_domain *domain);
  301. #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
  302. #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
  303. void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg);
  304. struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
  305. struct msi_domain_info *info,
  306. struct irq_domain *parent);
  307. irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
  308. struct msi_desc *desc);
  309. int pci_msi_domain_check_cap(struct irq_domain *domain,
  310. struct msi_domain_info *info, struct device *dev);
  311. u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
  312. struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
  313. #else
  314. static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
  315. {
  316. return NULL;
  317. }
  318. #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
  319. #endif /* LINUX_MSI_H */