1 /* SPDX-License-Identifier: GPL-2.0 */
6 * This header file contains MSI data structures and functions which are
8 * - Interrupt core code
10 * - MSI interrupt domain implementations
11 * - IOMMU, low level VFIO, NTB and other justified exceptions
12 * dealing with low level MSI details.
14 * Regular device drivers have no business with any of these functions and
15 * especially storing MSI descriptor pointers in random code is considered
18 * Device driver relevant functions are available in <linux/msi_api.h>
21 #include <linux/irqdomain_defs.h>
22 #include <linux/cpumask.h>
23 #include <linux/msi_api.h>
24 #include <linux/xarray.h>
25 #include <linux/mutex.h>
26 #include <linux/list.h>
27 #include <linux/bits.h>
31 /* Dummy shadow structures if an architecture does not define them */
32 #ifndef arch_msi_msg_addr_lo
33 typedef struct arch_msi_msg_addr_lo {
35 } __attribute__ ((packed)) arch_msi_msg_addr_lo_t;
38 #ifndef arch_msi_msg_addr_hi
39 typedef struct arch_msi_msg_addr_hi {
41 } __attribute__ ((packed)) arch_msi_msg_addr_hi_t;
44 #ifndef arch_msi_msg_data
45 typedef struct arch_msi_msg_data {
47 } __attribute__ ((packed)) arch_msi_msg_data_t;
51 * msi_msg - Representation of a MSI message
52 * @address_lo: Low 32 bits of msi message address
53 * @arch_addrlo: Architecture specific shadow of @address_lo
54 * @address_hi: High 32 bits of msi message address
55 * (only used when device supports it)
56 * @arch_addrhi: Architecture specific shadow of @address_hi
57 * @data: MSI message data (usually 16 bits)
58 * @arch_data: Architecture specific shadow of @data
63 arch_msi_msg_addr_lo_t arch_addr_lo;
67 arch_msi_msg_addr_hi_t arch_addr_hi;
71 arch_msi_msg_data_t arch_data;
75 extern int pci_msi_ignore_mask;
76 /* Helper functions */
80 struct platform_msi_priv_data;
81 struct device_attribute;
84 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
85 #ifdef CONFIG_GENERIC_MSI_IRQ
86 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
88 static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg) { }
91 typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
95 * pci_msi_desc - PCI/MSI specific MSI descriptor data
97 * @msi_mask: [PCI MSI] MSI cached mask bits
98 * @msix_ctrl: [PCI MSI-X] MSI-X cached per vector control bits
99 * @is_msix: [PCI MSI/X] True if MSI-X
100 * @multiple: [PCI MSI/X] log2 num of messages allocated
101 * @multi_cap: [PCI MSI/X] log2 num of messages supported
102 * @can_mask: [PCI MSI/X] Masking supported?
103 * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit
104 * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq
105 * @mask_pos: [PCI MSI] Mask register position
106 * @mask_base: [PCI MSI-X] Mask register base address
108 struct pci_msi_desc {
120 unsigned default_irq;
124 void __iomem *mask_base;
128 #define MSI_MAX_INDEX ((unsigned int)USHRT_MAX)
131 * struct msi_desc - Descriptor structure for MSI based interrupts
132 * @irq: The base interrupt number
133 * @nvec_used: The number of vectors used
134 * @dev: Pointer to the device which uses this descriptor
135 * @msg: The last set MSI message cached for reuse
136 * @affinity: Optional pointer to a cpu affinity mask for this descriptor
137 * @sysfs_attr: Pointer to sysfs device attribute
139 * @write_msi_msg: Callback that may be called when the MSI message
140 * address or data changes
141 * @write_msi_msg_data: Data parameter for the callback.
143 * @msi_index: Index of the msi descriptor
144 * @pci: PCI specific msi descriptor data
147 /* Shared device/bus type independent data */
149 unsigned int nvec_used;
152 struct irq_affinity_desc *affinity;
153 #ifdef CONFIG_IRQ_MSI_IOMMU
154 const void *iommu_cookie;
157 struct device_attribute *sysfs_attrs;
160 void (*write_msi_msg)(struct msi_desc *entry, void *data);
161 void *write_msi_msg_data;
164 struct pci_msi_desc pci;
168 * Filter values for the MSI descriptor iterators and accessor functions.
170 enum msi_desc_filter {
171 /* All descriptors */
173 /* Descriptors which have no interrupt associated */
174 MSI_DESC_NOTASSOCIATED,
175 /* Descriptors which have an interrupt associated */
181 * struct msi_dev_domain - The internals of MSI domain info per device
182 * @store: Xarray for storing MSI descriptor pointers
183 * @irqdomain: Pointer to a per device interrupt domain
185 struct msi_dev_domain {
187 struct irq_domain *domain;
191 * msi_device_data - MSI per device data
192 * @properties: MSI properties which are interesting to drivers
193 * @platform_data: Platform-MSI specific data
194 * @mutex: Mutex protecting the MSI descriptor store
195 * @__domains: Internal data for per device MSI domains
196 * @__iter_idx: Index to search the next entry for iterators
198 struct msi_device_data {
199 unsigned long properties;
200 struct platform_msi_priv_data *platform_data;
202 struct msi_dev_domain __domains[MSI_MAX_DEVICE_IRQDOMAINS];
203 unsigned long __iter_idx;
206 int msi_setup_device_data(struct device *dev);
208 void msi_lock_descs(struct device *dev);
209 void msi_unlock_descs(struct device *dev);
211 struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
212 enum msi_desc_filter filter);
215 * msi_first_desc - Get the first MSI descriptor of the default irqdomain
216 * @dev: Device to operate on
217 * @filter: Descriptor state filter
219 * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
220 * must be invoked before the call.
222 * Return: Pointer to the first MSI descriptor matching the search
223 * criteria, NULL if none found.
225 static inline struct msi_desc *msi_first_desc(struct device *dev,
226 enum msi_desc_filter filter)
228 return msi_domain_first_desc(dev, MSI_DEFAULT_DOMAIN, filter);
231 struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid,
232 enum msi_desc_filter filter);
235 * msi_domain_for_each_desc - Iterate the MSI descriptors in a specific domain
237 * @desc: struct msi_desc pointer used as iterator
238 * @dev: struct device pointer - device to iterate
239 * @domid: The id of the interrupt domain which should be walked.
240 * @filter: Filter for descriptor selection
243 * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
245 * - It is safe to remove a retrieved MSI descriptor in the loop.
247 #define msi_domain_for_each_desc(desc, dev, domid, filter) \
248 for ((desc) = msi_domain_first_desc((dev), (domid), (filter)); (desc); \
249 (desc) = msi_next_desc((dev), (domid), (filter)))
252 * msi_for_each_desc - Iterate the MSI descriptors in the default irqdomain
254 * @desc: struct msi_desc pointer used as iterator
255 * @dev: struct device pointer - device to iterate
256 * @filter: Filter for descriptor selection
259 * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
261 * - It is safe to remove a retrieved MSI descriptor in the loop.
263 #define msi_for_each_desc(desc, dev, filter) \
264 msi_domain_for_each_desc((desc), (dev), MSI_DEFAULT_DOMAIN, (filter))
266 #define msi_desc_to_dev(desc) ((desc)->dev)
268 #ifdef CONFIG_IRQ_MSI_IOMMU
269 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
271 return desc->iommu_cookie;
274 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
275 const void *iommu_cookie)
277 desc->iommu_cookie = iommu_cookie;
280 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
285 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
286 const void *iommu_cookie)
291 int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
292 struct msi_desc *init_desc);
294 * msi_insert_msi_desc - Allocate and initialize a MSI descriptor in the
295 * default irqdomain and insert it at @init_desc->msi_index
296 * @dev: Pointer to the device for which the descriptor is allocated
297 * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor
299 * Return: 0 on success or an appropriate failure code.
301 static inline int msi_insert_msi_desc(struct device *dev, struct msi_desc *init_desc)
303 return msi_domain_insert_msi_desc(dev, MSI_DEFAULT_DOMAIN, init_desc);
306 void msi_domain_free_msi_descs_range(struct device *dev, unsigned int domid,
307 unsigned int first, unsigned int last);
310 * msi_free_msi_descs_range - Free a range of MSI descriptors of a device
311 * in the default irqdomain
313 * @dev: Device for which to free the descriptors
314 * @first: Index to start freeing from (inclusive)
315 * @last: Last index to be freed (inclusive)
317 static inline void msi_free_msi_descs_range(struct device *dev, unsigned int first,
320 msi_domain_free_msi_descs_range(dev, MSI_DEFAULT_DOMAIN, first, last);
324 * msi_free_msi_descs - Free all MSI descriptors of a device in the default irqdomain
325 * @dev: Device to free the descriptors
327 static inline void msi_free_msi_descs(struct device *dev)
329 msi_free_msi_descs_range(dev, 0, MSI_MAX_INDEX);
333 * The arch hooks to setup up msi irqs. Default functions are implemented
334 * as weak symbols so that they /can/ be overriden by architecture specific
335 * code if needed. These hooks can only be enabled by the architecture.
337 * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
338 * stubs with warnings.
340 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
341 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
342 void arch_teardown_msi_irq(unsigned int irq);
343 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
344 void arch_teardown_msi_irqs(struct pci_dev *dev);
346 int msi_device_populate_sysfs(struct device *dev);
347 void msi_device_destroy_sysfs(struct device *dev);
348 #else /* CONFIG_SYSFS */
349 static inline int msi_device_populate_sysfs(struct device *dev) { return 0; }
350 static inline void msi_device_destroy_sysfs(struct device *dev) { }
351 #endif /* !CONFIG_SYSFS */
352 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
355 * The restore hook is still available even for fully irq domain based
356 * setups. Courtesy to XEN/X86.
358 bool arch_restore_msi_irqs(struct pci_dev *dev);
360 #ifdef CONFIG_GENERIC_MSI_IRQ
362 #include <linux/irqhandler.h>
365 struct irq_domain_ops;
368 struct fwnode_handle;
369 struct msi_domain_info;
372 * struct msi_domain_ops - MSI interrupt domain callbacks
373 * @get_hwirq: Retrieve the resulting hw irq number
374 * @msi_init: Domain specific init function for MSI interrupts
375 * @msi_free: Domain specific function to free a MSI interrupts
376 * @msi_prepare: Prepare the allocation of the interrupts in the domain
377 * @set_desc: Set the msi descriptor for an interrupt
378 * @domain_alloc_irqs: Optional function to override the default allocation
380 * @domain_free_irqs: Optional function to override the default free
382 * @msi_post_free: Optional function which is invoked after freeing
385 * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying
388 * @msi_check, @msi_prepare and @set_desc are callbacks used by the
389 * msi_domain_alloc/free_irqs*() variants.
391 * @domain_alloc_irqs, @domain_free_irqs can be used to override the
392 * default allocation/free functions (__msi_domain_alloc/free_irqs). This
393 * is initially for a wrapper around XENs seperate MSI universe which can't
394 * be wrapped into the regular irq domains concepts by mere mortals. This
395 * allows to universally use msi_domain_alloc/free_irqs without having to
396 * special case XEN all over the place.
398 struct msi_domain_ops {
399 irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
400 msi_alloc_info_t *arg);
401 int (*msi_init)(struct irq_domain *domain,
402 struct msi_domain_info *info,
403 unsigned int virq, irq_hw_number_t hwirq,
404 msi_alloc_info_t *arg);
405 void (*msi_free)(struct irq_domain *domain,
406 struct msi_domain_info *info,
408 int (*msi_prepare)(struct irq_domain *domain,
409 struct device *dev, int nvec,
410 msi_alloc_info_t *arg);
411 void (*set_desc)(msi_alloc_info_t *arg,
412 struct msi_desc *desc);
413 int (*domain_alloc_irqs)(struct irq_domain *domain,
414 struct device *dev, int nvec);
415 void (*domain_free_irqs)(struct irq_domain *domain,
417 void (*msi_post_free)(struct irq_domain *domain,
422 * struct msi_domain_info - MSI interrupt domain data
423 * @flags: Flags to decribe features and capabilities
424 * @bus_token: The domain bus token
425 * @ops: The callback data structure
426 * @chip: Optional: associated interrupt chip
427 * @chip_data: Optional: associated interrupt chip data
428 * @handler: Optional: associated interrupt flow handler
429 * @handler_data: Optional: associated interrupt flow handler data
430 * @handler_name: Optional: associated interrupt flow handler name
431 * @data: Optional: domain specific data
433 struct msi_domain_info {
435 enum irq_domain_bus_token bus_token;
436 struct msi_domain_ops *ops;
437 struct irq_chip *chip;
439 irq_flow_handler_t handler;
441 const char *handler_name;
446 * Flags for msi_domain_info
448 * Bit 0-15: Generic MSI functionality which is not subject to restriction
451 * Bit 16-31: Functionality which depends on the underlying parent domain and
452 * can be masked out by msi_parent_ops::init_dev_msi_info() when
453 * a device MSI domain is initialized.
457 * Init non implemented ops callbacks with default MSI domain
460 MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0),
462 * Init non implemented chip callbacks with default MSI chip
465 MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1),
466 /* Needs early activate, required for PCI */
467 MSI_FLAG_ACTIVATE_EARLY = (1 << 2),
469 * Must reactivate when irq is started even when
470 * MSI_FLAG_ACTIVATE_EARLY has been set.
472 MSI_FLAG_MUST_REACTIVATE = (1 << 3),
473 /* Populate sysfs on alloc() and destroy it on free() */
474 MSI_FLAG_DEV_SYSFS = (1 << 4),
475 /* Allocate simple MSI descriptors */
476 MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS = (1 << 5),
477 /* Free MSI descriptors */
478 MSI_FLAG_FREE_MSI_DESCS = (1 << 6),
480 * Quirk to handle MSI implementations which do not provide
481 * masking. Currently known to affect x86, but has to be partially
482 * handled in the core MSI code.
484 MSI_FLAG_NOMASK_QUIRK = (1 << 7),
486 /* Mask for the generic functionality */
487 MSI_GENERIC_FLAGS_MASK = GENMASK(15, 0),
489 /* Mask for the domain specific functionality */
490 MSI_DOMAIN_FLAGS_MASK = GENMASK(31, 16),
492 /* Support multiple PCI MSI interrupts */
493 MSI_FLAG_MULTI_PCI_MSI = (1 << 16),
494 /* Support PCI MSIX interrupts */
495 MSI_FLAG_PCI_MSIX = (1 << 17),
496 /* Is level-triggered capable, using two messages */
497 MSI_FLAG_LEVEL_CAPABLE = (1 << 18),
498 /* MSI-X entries must be contiguous */
499 MSI_FLAG_MSIX_CONTIGUOUS = (1 << 19),
504 * struct msi_parent_ops - MSI parent domain callbacks and configuration info
506 * @supported_flags: Required: The supported MSI flags of the parent domain
507 * @prefix: Optional: Prefix for the domain and chip name
508 * @init_dev_msi_info: Required: Callback for MSI parent domains to setup parent
509 * domain specific domain flags, domain ops and interrupt chip
510 * callbacks when a per device domain is created.
512 struct msi_parent_ops {
515 bool (*init_dev_msi_info)(struct device *dev, struct irq_domain *domain,
516 struct irq_domain *msi_parent_domain,
517 struct msi_domain_info *msi_child_info);
520 bool msi_parent_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
521 struct irq_domain *msi_parent_domain,
522 struct msi_domain_info *msi_child_info);
524 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
527 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
528 struct msi_domain_info *info,
529 struct irq_domain *parent);
531 int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
532 unsigned int first, unsigned int last);
533 int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
534 unsigned int first, unsigned int last);
535 int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs);
538 void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
539 unsigned int first, unsigned int last);
540 void msi_domain_free_irqs_range(struct device *dev, unsigned int domid,
541 unsigned int first, unsigned int last);
542 void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid);
543 void msi_domain_free_irqs_all(struct device *dev, unsigned int domid);
545 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
547 struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
548 struct msi_domain_info *info,
549 struct irq_domain *parent);
550 int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
551 irq_write_msi_msg_t write_msi_msg);
552 void platform_msi_domain_free_irqs(struct device *dev);
554 /* When an MSI domain is used as an intermediate domain */
555 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
556 int nvec, msi_alloc_info_t *args);
557 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
558 int virq, int nvec, msi_alloc_info_t *args);
560 __platform_msi_create_device_domain(struct device *dev,
563 irq_write_msi_msg_t write_msi_msg,
564 const struct irq_domain_ops *ops,
567 #define platform_msi_create_device_domain(dev, nvec, write, ops, data) \
568 __platform_msi_create_device_domain(dev, nvec, false, write, ops, data)
569 #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \
570 __platform_msi_create_device_domain(dev, nvec, true, write, ops, data)
572 int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq,
573 unsigned int nr_irqs);
574 void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq,
576 void *platform_msi_get_host_data(struct irq_domain *domain);
577 #endif /* CONFIG_GENERIC_MSI_IRQ */
579 /* PCI specific interfaces */
580 #ifdef CONFIG_PCI_MSI
581 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
582 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
583 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
584 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
585 void pci_msi_mask_irq(struct irq_data *data);
586 void pci_msi_unmask_irq(struct irq_data *data);
587 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
588 struct msi_domain_info *info,
589 struct irq_domain *parent);
590 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
591 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
592 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev);
593 #else /* CONFIG_PCI_MSI */
594 static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
598 static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg) { }
599 #endif /* !CONFIG_PCI_MSI */
601 #endif /* LINUX_MSI_H */