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/irq.h>
28 #include <linux/bits.h>
32 /* Dummy shadow structures if an architecture does not define them */
33 #ifndef arch_msi_msg_addr_lo
34 typedef struct arch_msi_msg_addr_lo {
36 } __attribute__ ((packed)) arch_msi_msg_addr_lo_t;
39 #ifndef arch_msi_msg_addr_hi
40 typedef struct arch_msi_msg_addr_hi {
42 } __attribute__ ((packed)) arch_msi_msg_addr_hi_t;
45 #ifndef arch_msi_msg_data
46 typedef struct arch_msi_msg_data {
48 } __attribute__ ((packed)) arch_msi_msg_data_t;
52 * msi_msg - Representation of a MSI message
53 * @address_lo: Low 32 bits of msi message address
54 * @arch_addrlo: Architecture specific shadow of @address_lo
55 * @address_hi: High 32 bits of msi message address
56 * (only used when device supports it)
57 * @arch_addrhi: Architecture specific shadow of @address_hi
58 * @data: MSI message data (usually 16 bits)
59 * @arch_data: Architecture specific shadow of @data
64 arch_msi_msg_addr_lo_t arch_addr_lo;
68 arch_msi_msg_addr_hi_t arch_addr_hi;
72 arch_msi_msg_data_t arch_data;
76 extern int pci_msi_ignore_mask;
77 /* Helper functions */
80 struct platform_msi_priv_data;
81 struct device_attribute;
83 struct irq_affinity_desc;
85 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
86 #ifdef CONFIG_GENERIC_MSI_IRQ
87 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
89 static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg) { }
92 typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
96 * pci_msi_desc - PCI/MSI specific MSI descriptor data
98 * @msi_mask: [PCI MSI] MSI cached mask bits
99 * @msix_ctrl: [PCI MSI-X] MSI-X cached per vector control bits
100 * @is_msix: [PCI MSI/X] True if MSI-X
101 * @multiple: [PCI MSI/X] log2 num of messages allocated
102 * @multi_cap: [PCI MSI/X] log2 num of messages supported
103 * @can_mask: [PCI MSI/X] Masking supported?
104 * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit
105 * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq
106 * @mask_pos: [PCI MSI] Mask register position
107 * @mask_base: [PCI MSI-X] Mask register base address
109 struct pci_msi_desc {
121 unsigned default_irq;
125 void __iomem *mask_base;
130 * union msi_domain_cookie - Opaque MSI domain specific data
131 * @value: u64 value store
132 * @ptr: Pointer to domain specific data
133 * @iobase: Domain specific IOmem pointer
135 * The content of this data is implementation defined and used by the MSI
136 * domain to store domain specific information which is requried for
137 * interrupt chip callbacks.
139 union msi_domain_cookie {
142 void __iomem *iobase;
146 * struct msi_desc_data - Generic MSI descriptor data
147 * @dcookie: Cookie for MSI domain specific data which is required
148 * for irq_chip callbacks
149 * @icookie: Cookie for the MSI interrupt instance provided by
150 * the usage site to the allocation function
152 * The content of this data is implementation defined, e.g. PCI/IMS
153 * implementations define the meaning of the data. The MSI core ignores
154 * this data completely.
156 struct msi_desc_data {
157 union msi_domain_cookie dcookie;
158 union msi_instance_cookie icookie;
161 #define MSI_MAX_INDEX ((unsigned int)USHRT_MAX)
164 * struct msi_desc - Descriptor structure for MSI based interrupts
165 * @irq: The base interrupt number
166 * @nvec_used: The number of vectors used
167 * @dev: Pointer to the device which uses this descriptor
168 * @msg: The last set MSI message cached for reuse
169 * @affinity: Optional pointer to a cpu affinity mask for this descriptor
170 * @sysfs_attr: Pointer to sysfs device attribute
172 * @write_msi_msg: Callback that may be called when the MSI message
173 * address or data changes
174 * @write_msi_msg_data: Data parameter for the callback.
176 * @msi_index: Index of the msi descriptor
177 * @pci: PCI specific msi descriptor data
178 * @data: Generic MSI descriptor data
181 /* Shared device/bus type independent data */
183 unsigned int nvec_used;
186 struct irq_affinity_desc *affinity;
187 #ifdef CONFIG_IRQ_MSI_IOMMU
188 const void *iommu_cookie;
191 struct device_attribute *sysfs_attrs;
194 void (*write_msi_msg)(struct msi_desc *entry, void *data);
195 void *write_msi_msg_data;
199 struct pci_msi_desc pci;
200 struct msi_desc_data data;
205 * Filter values for the MSI descriptor iterators and accessor functions.
207 enum msi_desc_filter {
208 /* All descriptors */
210 /* Descriptors which have no interrupt associated */
211 MSI_DESC_NOTASSOCIATED,
212 /* Descriptors which have an interrupt associated */
218 * struct msi_dev_domain - The internals of MSI domain info per device
219 * @store: Xarray for storing MSI descriptor pointers
220 * @irqdomain: Pointer to a per device interrupt domain
222 struct msi_dev_domain {
224 struct irq_domain *domain;
228 * msi_device_data - MSI per device data
229 * @properties: MSI properties which are interesting to drivers
230 * @platform_data: Platform-MSI specific data
231 * @mutex: Mutex protecting the MSI descriptor store
232 * @__domains: Internal data for per device MSI domains
233 * @__iter_idx: Index to search the next entry for iterators
235 struct msi_device_data {
236 unsigned long properties;
237 struct platform_msi_priv_data *platform_data;
239 struct msi_dev_domain __domains[MSI_MAX_DEVICE_IRQDOMAINS];
240 unsigned long __iter_idx;
243 int msi_setup_device_data(struct device *dev);
245 void msi_lock_descs(struct device *dev);
246 void msi_unlock_descs(struct device *dev);
248 struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
249 enum msi_desc_filter filter);
252 * msi_first_desc - Get the first MSI descriptor of the default irqdomain
253 * @dev: Device to operate on
254 * @filter: Descriptor state filter
256 * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
257 * must be invoked before the call.
259 * Return: Pointer to the first MSI descriptor matching the search
260 * criteria, NULL if none found.
262 static inline struct msi_desc *msi_first_desc(struct device *dev,
263 enum msi_desc_filter filter)
265 return msi_domain_first_desc(dev, MSI_DEFAULT_DOMAIN, filter);
268 struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid,
269 enum msi_desc_filter filter);
272 * msi_domain_for_each_desc - Iterate the MSI descriptors in a specific domain
274 * @desc: struct msi_desc pointer used as iterator
275 * @dev: struct device pointer - device to iterate
276 * @domid: The id of the interrupt domain which should be walked.
277 * @filter: Filter for descriptor selection
280 * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
282 * - It is safe to remove a retrieved MSI descriptor in the loop.
284 #define msi_domain_for_each_desc(desc, dev, domid, filter) \
285 for ((desc) = msi_domain_first_desc((dev), (domid), (filter)); (desc); \
286 (desc) = msi_next_desc((dev), (domid), (filter)))
289 * msi_for_each_desc - Iterate the MSI descriptors in the default irqdomain
291 * @desc: struct msi_desc pointer used as iterator
292 * @dev: struct device pointer - device to iterate
293 * @filter: Filter for descriptor selection
296 * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
298 * - It is safe to remove a retrieved MSI descriptor in the loop.
300 #define msi_for_each_desc(desc, dev, filter) \
301 msi_domain_for_each_desc((desc), (dev), MSI_DEFAULT_DOMAIN, (filter))
303 #define msi_desc_to_dev(desc) ((desc)->dev)
305 #ifdef CONFIG_IRQ_MSI_IOMMU
306 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
308 return desc->iommu_cookie;
311 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
312 const void *iommu_cookie)
314 desc->iommu_cookie = iommu_cookie;
317 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
322 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
323 const void *iommu_cookie)
328 int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
329 struct msi_desc *init_desc);
331 * msi_insert_msi_desc - Allocate and initialize a MSI descriptor in the
332 * default irqdomain and insert it at @init_desc->msi_index
333 * @dev: Pointer to the device for which the descriptor is allocated
334 * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor
336 * Return: 0 on success or an appropriate failure code.
338 static inline int msi_insert_msi_desc(struct device *dev, struct msi_desc *init_desc)
340 return msi_domain_insert_msi_desc(dev, MSI_DEFAULT_DOMAIN, init_desc);
343 void msi_domain_free_msi_descs_range(struct device *dev, unsigned int domid,
344 unsigned int first, unsigned int last);
347 * msi_free_msi_descs_range - Free a range of MSI descriptors of a device
348 * in the default irqdomain
350 * @dev: Device for which to free the descriptors
351 * @first: Index to start freeing from (inclusive)
352 * @last: Last index to be freed (inclusive)
354 static inline void msi_free_msi_descs_range(struct device *dev, unsigned int first,
357 msi_domain_free_msi_descs_range(dev, MSI_DEFAULT_DOMAIN, first, last);
361 * msi_free_msi_descs - Free all MSI descriptors of a device in the default irqdomain
362 * @dev: Device to free the descriptors
364 static inline void msi_free_msi_descs(struct device *dev)
366 msi_free_msi_descs_range(dev, 0, MSI_MAX_INDEX);
370 * The arch hooks to setup up msi irqs. Default functions are implemented
371 * as weak symbols so that they /can/ be overriden by architecture specific
372 * code if needed. These hooks can only be enabled by the architecture.
374 * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
375 * stubs with warnings.
377 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
378 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
379 void arch_teardown_msi_irq(unsigned int irq);
380 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
381 void arch_teardown_msi_irqs(struct pci_dev *dev);
383 int msi_device_populate_sysfs(struct device *dev);
384 void msi_device_destroy_sysfs(struct device *dev);
385 #else /* CONFIG_SYSFS */
386 static inline int msi_device_populate_sysfs(struct device *dev) { return 0; }
387 static inline void msi_device_destroy_sysfs(struct device *dev) { }
388 #endif /* !CONFIG_SYSFS */
389 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
392 * The restore hook is still available even for fully irq domain based
393 * setups. Courtesy to XEN/X86.
395 bool arch_restore_msi_irqs(struct pci_dev *dev);
397 #ifdef CONFIG_GENERIC_MSI_IRQ
399 #include <linux/irqhandler.h>
402 struct irq_domain_ops;
405 struct fwnode_handle;
406 struct msi_domain_info;
409 * struct msi_domain_ops - MSI interrupt domain callbacks
410 * @get_hwirq: Retrieve the resulting hw irq number
411 * @msi_init: Domain specific init function for MSI interrupts
412 * @msi_free: Domain specific function to free a MSI interrupts
413 * @msi_prepare: Prepare the allocation of the interrupts in the domain
414 * @prepare_desc: Optional function to prepare the allocated MSI descriptor
416 * @set_desc: Set the msi descriptor for an interrupt
417 * @domain_alloc_irqs: Optional function to override the default allocation
419 * @domain_free_irqs: Optional function to override the default free
421 * @msi_post_free: Optional function which is invoked after freeing
424 * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying
427 * @msi_check, @msi_prepare, @prepare_desc and @set_desc are callbacks used by the
428 * msi_domain_alloc/free_irqs*() variants.
430 * @domain_alloc_irqs, @domain_free_irqs can be used to override the
431 * default allocation/free functions (__msi_domain_alloc/free_irqs). This
432 * is initially for a wrapper around XENs seperate MSI universe which can't
433 * be wrapped into the regular irq domains concepts by mere mortals. This
434 * allows to universally use msi_domain_alloc/free_irqs without having to
435 * special case XEN all over the place.
437 struct msi_domain_ops {
438 irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
439 msi_alloc_info_t *arg);
440 int (*msi_init)(struct irq_domain *domain,
441 struct msi_domain_info *info,
442 unsigned int virq, irq_hw_number_t hwirq,
443 msi_alloc_info_t *arg);
444 void (*msi_free)(struct irq_domain *domain,
445 struct msi_domain_info *info,
447 int (*msi_prepare)(struct irq_domain *domain,
448 struct device *dev, int nvec,
449 msi_alloc_info_t *arg);
450 void (*prepare_desc)(struct irq_domain *domain, msi_alloc_info_t *arg,
451 struct msi_desc *desc);
452 void (*set_desc)(msi_alloc_info_t *arg,
453 struct msi_desc *desc);
454 int (*domain_alloc_irqs)(struct irq_domain *domain,
455 struct device *dev, int nvec);
456 void (*domain_free_irqs)(struct irq_domain *domain,
458 void (*msi_post_free)(struct irq_domain *domain,
463 * struct msi_domain_info - MSI interrupt domain data
464 * @flags: Flags to decribe features and capabilities
465 * @bus_token: The domain bus token
466 * @hwsize: The hardware table size or the software index limit.
467 * If 0 then the size is considered unlimited and
468 * gets initialized to the maximum software index limit
469 * by the domain creation code.
470 * @ops: The callback data structure
471 * @chip: Optional: associated interrupt chip
472 * @chip_data: Optional: associated interrupt chip data
473 * @handler: Optional: associated interrupt flow handler
474 * @handler_data: Optional: associated interrupt flow handler data
475 * @handler_name: Optional: associated interrupt flow handler name
476 * @data: Optional: domain specific data
478 struct msi_domain_info {
480 enum irq_domain_bus_token bus_token;
482 struct msi_domain_ops *ops;
483 struct irq_chip *chip;
485 irq_flow_handler_t handler;
487 const char *handler_name;
492 * struct msi_domain_template - Template for MSI device domains
493 * @name: Storage for the resulting name. Filled in by the core.
494 * @chip: Interrupt chip for this domain
495 * @ops: MSI domain ops
496 * @info: MSI domain info data
498 struct msi_domain_template {
500 struct irq_chip chip;
501 struct msi_domain_ops ops;
502 struct msi_domain_info info;
506 * Flags for msi_domain_info
508 * Bit 0-15: Generic MSI functionality which is not subject to restriction
511 * Bit 16-31: Functionality which depends on the underlying parent domain and
512 * can be masked out by msi_parent_ops::init_dev_msi_info() when
513 * a device MSI domain is initialized.
517 * Init non implemented ops callbacks with default MSI domain
520 MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0),
522 * Init non implemented chip callbacks with default MSI chip
525 MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1),
526 /* Needs early activate, required for PCI */
527 MSI_FLAG_ACTIVATE_EARLY = (1 << 2),
529 * Must reactivate when irq is started even when
530 * MSI_FLAG_ACTIVATE_EARLY has been set.
532 MSI_FLAG_MUST_REACTIVATE = (1 << 3),
533 /* Populate sysfs on alloc() and destroy it on free() */
534 MSI_FLAG_DEV_SYSFS = (1 << 4),
535 /* Allocate simple MSI descriptors */
536 MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS = (1 << 5),
537 /* Free MSI descriptors */
538 MSI_FLAG_FREE_MSI_DESCS = (1 << 6),
540 * Quirk to handle MSI implementations which do not provide
541 * masking. Currently known to affect x86, but has to be partially
542 * handled in the core MSI code.
544 MSI_FLAG_NOMASK_QUIRK = (1 << 7),
546 /* Mask for the generic functionality */
547 MSI_GENERIC_FLAGS_MASK = GENMASK(15, 0),
549 /* Mask for the domain specific functionality */
550 MSI_DOMAIN_FLAGS_MASK = GENMASK(31, 16),
552 /* Support multiple PCI MSI interrupts */
553 MSI_FLAG_MULTI_PCI_MSI = (1 << 16),
554 /* Support PCI MSIX interrupts */
555 MSI_FLAG_PCI_MSIX = (1 << 17),
556 /* Is level-triggered capable, using two messages */
557 MSI_FLAG_LEVEL_CAPABLE = (1 << 18),
558 /* MSI-X entries must be contiguous */
559 MSI_FLAG_MSIX_CONTIGUOUS = (1 << 19),
560 /* PCI/MSI-X vectors can be dynamically allocated/freed post MSI-X enable */
561 MSI_FLAG_PCI_MSIX_ALLOC_DYN = (1 << 20),
562 /* Support for PCI/IMS */
563 MSI_FLAG_PCI_IMS = (1 << 21),
567 * struct msi_parent_ops - MSI parent domain callbacks and configuration info
569 * @supported_flags: Required: The supported MSI flags of the parent domain
570 * @prefix: Optional: Prefix for the domain and chip name
571 * @init_dev_msi_info: Required: Callback for MSI parent domains to setup parent
572 * domain specific domain flags, domain ops and interrupt chip
573 * callbacks when a per device domain is created.
575 struct msi_parent_ops {
578 bool (*init_dev_msi_info)(struct device *dev, struct irq_domain *domain,
579 struct irq_domain *msi_parent_domain,
580 struct msi_domain_info *msi_child_info);
583 bool msi_parent_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
584 struct irq_domain *msi_parent_domain,
585 struct msi_domain_info *msi_child_info);
587 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
590 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
591 struct msi_domain_info *info,
592 struct irq_domain *parent);
594 bool msi_create_device_irq_domain(struct device *dev, unsigned int domid,
595 const struct msi_domain_template *template,
596 unsigned int hwsize, void *domain_data,
598 void msi_remove_device_irq_domain(struct device *dev, unsigned int domid);
600 bool msi_match_device_irq_domain(struct device *dev, unsigned int domid,
601 enum irq_domain_bus_token bus_token);
603 int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
604 unsigned int first, unsigned int last);
605 int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
606 unsigned int first, unsigned int last);
607 int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs);
609 struct msi_map msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, unsigned int index,
610 const struct irq_affinity_desc *affdesc,
611 union msi_instance_cookie *cookie);
613 void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
614 unsigned int first, unsigned int last);
615 void msi_domain_free_irqs_range(struct device *dev, unsigned int domid,
616 unsigned int first, unsigned int last);
617 void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid);
618 void msi_domain_free_irqs_all(struct device *dev, unsigned int domid);
620 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
622 struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
623 struct msi_domain_info *info,
624 struct irq_domain *parent);
625 int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
626 irq_write_msi_msg_t write_msi_msg);
627 void platform_msi_domain_free_irqs(struct device *dev);
629 /* When an MSI domain is used as an intermediate domain */
630 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
631 int nvec, msi_alloc_info_t *args);
632 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
633 int virq, int nvec, msi_alloc_info_t *args);
635 __platform_msi_create_device_domain(struct device *dev,
638 irq_write_msi_msg_t write_msi_msg,
639 const struct irq_domain_ops *ops,
642 #define platform_msi_create_device_domain(dev, nvec, write, ops, data) \
643 __platform_msi_create_device_domain(dev, nvec, false, write, ops, data)
644 #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \
645 __platform_msi_create_device_domain(dev, nvec, true, write, ops, data)
647 int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq,
648 unsigned int nr_irqs);
649 void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq,
651 void *platform_msi_get_host_data(struct irq_domain *domain);
652 #endif /* CONFIG_GENERIC_MSI_IRQ */
654 /* PCI specific interfaces */
655 #ifdef CONFIG_PCI_MSI
656 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
657 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
658 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
659 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
660 void pci_msi_mask_irq(struct irq_data *data);
661 void pci_msi_unmask_irq(struct irq_data *data);
662 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
663 struct msi_domain_info *info,
664 struct irq_domain *parent);
665 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
666 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
667 #else /* CONFIG_PCI_MSI */
668 static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
672 static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg) { }
673 #endif /* !CONFIG_PCI_MSI */
675 #endif /* LINUX_MSI_H */