1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/cpumask.h>
6 #include <linux/list.h>
9 /* Dummy shadow structures if an architecture does not define them */
10 #ifndef arch_msi_msg_addr_lo
11 typedef struct arch_msi_msg_addr_lo {
13 } __attribute__ ((packed)) arch_msi_msg_addr_lo_t;
16 #ifndef arch_msi_msg_addr_hi
17 typedef struct arch_msi_msg_addr_hi {
19 } __attribute__ ((packed)) arch_msi_msg_addr_hi_t;
22 #ifndef arch_msi_msg_data
23 typedef struct arch_msi_msg_data {
25 } __attribute__ ((packed)) arch_msi_msg_data_t;
29 * msi_msg - Representation of a MSI message
30 * @address_lo: Low 32 bits of msi message address
31 * @arch_addrlo: Architecture specific shadow of @address_lo
32 * @address_hi: High 32 bits of msi message address
33 * (only used when device supports it)
34 * @arch_addrhi: Architecture specific shadow of @address_hi
35 * @data: MSI message data (usually 16 bits)
36 * @arch_data: Architecture specific shadow of @data
41 arch_msi_msg_addr_lo_t arch_addr_lo;
45 arch_msi_msg_addr_hi_t arch_addr_hi;
49 arch_msi_msg_data_t arch_data;
53 extern int pci_msi_ignore_mask;
54 /* Helper functions */
58 struct platform_msi_priv_data;
59 struct attribute_group;
61 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
62 #ifdef CONFIG_GENERIC_MSI_IRQ
63 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
65 static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
70 typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
74 * pci_msi_desc - PCI/MSI specific MSI descriptor data
76 * @msi_mask: [PCI MSI] MSI cached mask bits
77 * @msix_ctrl: [PCI MSI-X] MSI-X cached per vector control bits
78 * @is_msix: [PCI MSI/X] True if MSI-X
79 * @multiple: [PCI MSI/X] log2 num of messages allocated
80 * @multi_cap: [PCI MSI/X] log2 num of messages supported
81 * @can_mask: [PCI MSI/X] Masking supported?
82 * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit
83 * @entry_nr: [PCI MSI/X] Entry which is described by this descriptor
84 * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq
85 * @mask_pos: [PCI MSI] Mask register position
86 * @mask_base: [PCI MSI-X] Mask register base address
101 unsigned default_irq;
105 void __iomem *mask_base;
110 * platform_msi_desc - Platform device specific msi descriptor data
111 * @msi_priv_data: Pointer to platform private data
112 * @msi_index: The index of the MSI descriptor for multi MSI
114 struct platform_msi_desc {
115 struct platform_msi_priv_data *msi_priv_data;
120 * fsl_mc_msi_desc - FSL-MC device specific msi descriptor data
121 * @msi_index: The index of the MSI descriptor
123 struct fsl_mc_msi_desc {
128 * ti_sci_inta_msi_desc - TISCI based INTA specific msi descriptor data
129 * @dev_index: TISCI device index
131 struct ti_sci_inta_msi_desc {
136 * struct msi_desc - Descriptor structure for MSI based interrupts
137 * @list: List head for management
138 * @irq: The base interrupt number
139 * @nvec_used: The number of vectors used
140 * @dev: Pointer to the device which uses this descriptor
141 * @msg: The last set MSI message cached for reuse
142 * @affinity: Optional pointer to a cpu affinity mask for this descriptor
144 * @write_msi_msg: Callback that may be called when the MSI message
145 * address or data changes
146 * @write_msi_msg_data: Data parameter for the callback.
148 * @pci: [PCI] PCI speficic msi descriptor data
149 * @platform: [platform] Platform device specific msi descriptor data
150 * @fsl_mc: [fsl-mc] FSL MC device specific msi descriptor data
151 * @inta: [INTA] TISCI based INTA specific msi descriptor data
154 /* Shared device/bus type independent data */
155 struct list_head list;
157 unsigned int nvec_used;
160 struct irq_affinity_desc *affinity;
161 #ifdef CONFIG_IRQ_MSI_IOMMU
162 const void *iommu_cookie;
165 void (*write_msi_msg)(struct msi_desc *entry, void *data);
166 void *write_msi_msg_data;
169 struct pci_msi_desc pci;
170 struct platform_msi_desc platform;
171 struct fsl_mc_msi_desc fsl_mc;
172 struct ti_sci_inta_msi_desc inta;
177 * msi_device_data - MSI per device data
178 * @properties: MSI properties which are interesting to drivers
179 * @attrs: Pointer to the sysfs attribute group
181 struct msi_device_data {
182 unsigned long properties;
183 const struct attribute_group **attrs;
186 int msi_setup_device_data(struct device *dev);
188 /* Helpers to hide struct msi_desc implementation details */
189 #define msi_desc_to_dev(desc) ((desc)->dev)
190 #define dev_to_msi_list(dev) (&(dev)->msi_list)
191 #define first_msi_entry(dev) \
192 list_first_entry(dev_to_msi_list((dev)), struct msi_desc, list)
193 #define for_each_msi_entry(desc, dev) \
194 list_for_each_entry((desc), dev_to_msi_list((dev)), list)
195 #define for_each_msi_entry_safe(desc, tmp, dev) \
196 list_for_each_entry_safe((desc), (tmp), dev_to_msi_list((dev)), list)
197 #define for_each_msi_vector(desc, __irq, dev) \
198 for_each_msi_entry((desc), (dev)) \
200 for (__irq = (desc)->irq; \
201 __irq < ((desc)->irq + (desc)->nvec_used); \
204 #ifdef CONFIG_IRQ_MSI_IOMMU
205 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
207 return desc->iommu_cookie;
210 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
211 const void *iommu_cookie)
213 desc->iommu_cookie = iommu_cookie;
216 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
221 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
222 const void *iommu_cookie)
227 #ifdef CONFIG_PCI_MSI
228 #define first_pci_msi_entry(pdev) first_msi_entry(&(pdev)->dev)
229 #define for_each_pci_msi_entry(desc, pdev) \
230 for_each_msi_entry((desc), &(pdev)->dev)
232 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
233 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
234 #else /* CONFIG_PCI_MSI */
235 static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
238 #endif /* CONFIG_PCI_MSI */
240 struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
241 const struct irq_affinity_desc *affinity);
242 void free_msi_entry(struct msi_desc *entry);
243 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
244 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
246 void pci_msi_mask_irq(struct irq_data *data);
247 void pci_msi_unmask_irq(struct irq_data *data);
250 int msi_device_populate_sysfs(struct device *dev);
251 void msi_device_destroy_sysfs(struct device *dev);
253 const struct attribute_group **msi_populate_sysfs(struct device *dev);
254 void msi_destroy_sysfs(struct device *dev,
255 const struct attribute_group **msi_irq_groups);
257 static inline int msi_device_populate_sysfs(struct device *dev) { return 0; }
258 static inline void msi_device_destroy_sysfs(struct device *dev) { }
260 static inline const struct attribute_group **msi_populate_sysfs(struct device *dev)
264 static inline void msi_destroy_sysfs(struct device *dev, const struct attribute_group **msi_irq_groups)
270 * The arch hooks to setup up msi irqs. Default functions are implemented
271 * as weak symbols so that they /can/ be overriden by architecture specific
272 * code if needed. These hooks can only be enabled by the architecture.
274 * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
275 * stubs with warnings.
277 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
278 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
279 void arch_teardown_msi_irq(unsigned int irq);
280 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
281 void arch_teardown_msi_irqs(struct pci_dev *dev);
285 * The restore hook is still available even for fully irq domain based
286 * setups. Courtesy to XEN/X86.
288 bool arch_restore_msi_irqs(struct pci_dev *dev);
290 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
292 #include <linux/irqhandler.h>
295 struct irq_domain_ops;
298 struct fwnode_handle;
299 struct msi_domain_info;
302 * struct msi_domain_ops - MSI interrupt domain callbacks
303 * @get_hwirq: Retrieve the resulting hw irq number
304 * @msi_init: Domain specific init function for MSI interrupts
305 * @msi_free: Domain specific function to free a MSI interrupts
306 * @msi_check: Callback for verification of the domain/info/dev data
307 * @msi_prepare: Prepare the allocation of the interrupts in the domain
308 * @set_desc: Set the msi descriptor for an interrupt
309 * @domain_alloc_irqs: Optional function to override the default allocation
311 * @domain_free_irqs: Optional function to override the default free
314 * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying
317 * @msi_check, @msi_prepare and @set_desc are callbacks used by
318 * msi_domain_alloc/free_irqs().
320 * @domain_alloc_irqs, @domain_free_irqs can be used to override the
321 * default allocation/free functions (__msi_domain_alloc/free_irqs). This
322 * is initially for a wrapper around XENs seperate MSI universe which can't
323 * be wrapped into the regular irq domains concepts by mere mortals. This
324 * allows to universally use msi_domain_alloc/free_irqs without having to
325 * special case XEN all over the place.
327 * Contrary to other operations @domain_alloc_irqs and @domain_free_irqs
328 * are set to the default implementation if NULL and even when
329 * MSI_FLAG_USE_DEF_DOM_OPS is not set to avoid breaking existing users and
330 * because these callbacks are obviously mandatory.
332 * This is NOT meant to be abused, but it can be useful to build wrappers
333 * for specialized MSI irq domains which need extra work before and after
334 * calling __msi_domain_alloc_irqs()/__msi_domain_free_irqs().
336 struct msi_domain_ops {
337 irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
338 msi_alloc_info_t *arg);
339 int (*msi_init)(struct irq_domain *domain,
340 struct msi_domain_info *info,
341 unsigned int virq, irq_hw_number_t hwirq,
342 msi_alloc_info_t *arg);
343 void (*msi_free)(struct irq_domain *domain,
344 struct msi_domain_info *info,
346 int (*msi_check)(struct irq_domain *domain,
347 struct msi_domain_info *info,
349 int (*msi_prepare)(struct irq_domain *domain,
350 struct device *dev, int nvec,
351 msi_alloc_info_t *arg);
352 void (*set_desc)(msi_alloc_info_t *arg,
353 struct msi_desc *desc);
354 int (*domain_alloc_irqs)(struct irq_domain *domain,
355 struct device *dev, int nvec);
356 void (*domain_free_irqs)(struct irq_domain *domain,
361 * struct msi_domain_info - MSI interrupt domain data
362 * @flags: Flags to decribe features and capabilities
363 * @ops: The callback data structure
364 * @chip: Optional: associated interrupt chip
365 * @chip_data: Optional: associated interrupt chip data
366 * @handler: Optional: associated interrupt flow handler
367 * @handler_data: Optional: associated interrupt flow handler data
368 * @handler_name: Optional: associated interrupt flow handler name
369 * @data: Optional: domain specific data
371 struct msi_domain_info {
373 struct msi_domain_ops *ops;
374 struct irq_chip *chip;
376 irq_flow_handler_t handler;
378 const char *handler_name;
382 /* Flags for msi_domain_info */
385 * Init non implemented ops callbacks with default MSI domain
388 MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0),
390 * Init non implemented chip callbacks with default MSI chip
393 MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1),
394 /* Support multiple PCI MSI interrupts */
395 MSI_FLAG_MULTI_PCI_MSI = (1 << 2),
396 /* Support PCI MSIX interrupts */
397 MSI_FLAG_PCI_MSIX = (1 << 3),
398 /* Needs early activate, required for PCI */
399 MSI_FLAG_ACTIVATE_EARLY = (1 << 4),
401 * Must reactivate when irq is started even when
402 * MSI_FLAG_ACTIVATE_EARLY has been set.
404 MSI_FLAG_MUST_REACTIVATE = (1 << 5),
405 /* Is level-triggered capable, using two messages */
406 MSI_FLAG_LEVEL_CAPABLE = (1 << 6),
407 /* Populate sysfs on alloc() and destroy it on free() */
408 MSI_FLAG_DEV_SYSFS = (1 << 7),
411 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
414 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
415 struct msi_domain_info *info,
416 struct irq_domain *parent);
417 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
419 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
421 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
422 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
423 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
425 struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
426 struct msi_domain_info *info,
427 struct irq_domain *parent);
428 int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
429 irq_write_msi_msg_t write_msi_msg);
430 void platform_msi_domain_free_irqs(struct device *dev);
432 /* When an MSI domain is used as an intermediate domain */
433 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
434 int nvec, msi_alloc_info_t *args);
435 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
436 int virq, int nvec, msi_alloc_info_t *args);
438 __platform_msi_create_device_domain(struct device *dev,
441 irq_write_msi_msg_t write_msi_msg,
442 const struct irq_domain_ops *ops,
445 #define platform_msi_create_device_domain(dev, nvec, write, ops, data) \
446 __platform_msi_create_device_domain(dev, nvec, false, write, ops, data)
447 #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \
448 __platform_msi_create_device_domain(dev, nvec, true, write, ops, data)
450 int platform_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
451 unsigned int nr_irqs);
452 void platform_msi_domain_free(struct irq_domain *domain, unsigned int virq,
454 void *platform_msi_get_host_data(struct irq_domain *domain);
455 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
457 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
458 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
459 struct msi_domain_info *info,
460 struct irq_domain *parent);
461 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
462 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
463 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev);
465 static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
469 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
471 #endif /* LINUX_MSI_H */