1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/kobject.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 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
60 #ifdef CONFIG_GENERIC_MSI_IRQ
61 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
63 static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
68 typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
72 * platform_msi_desc - Platform device specific msi descriptor data
73 * @msi_priv_data: Pointer to platform private data
74 * @msi_index: The index of the MSI descriptor for multi MSI
76 struct platform_msi_desc {
77 struct platform_msi_priv_data *msi_priv_data;
82 * fsl_mc_msi_desc - FSL-MC device specific msi descriptor data
83 * @msi_index: The index of the MSI descriptor
85 struct fsl_mc_msi_desc {
90 * ti_sci_inta_msi_desc - TISCI based INTA specific msi descriptor data
91 * @dev_index: TISCI device index
93 struct ti_sci_inta_msi_desc {
98 * struct msi_desc - Descriptor structure for MSI based interrupts
99 * @list: List head for management
100 * @irq: The base interrupt number
101 * @nvec_used: The number of vectors used
102 * @dev: Pointer to the device which uses this descriptor
103 * @msg: The last set MSI message cached for reuse
104 * @affinity: Optional pointer to a cpu affinity mask for this descriptor
106 * @write_msi_msg: Callback that may be called when the MSI message
107 * address or data changes
108 * @write_msi_msg_data: Data parameter for the callback.
110 * @masked: [PCI MSI/X] Mask bits
111 * @is_msix: [PCI MSI/X] True if MSI-X
112 * @multiple: [PCI MSI/X] log2 num of messages allocated
113 * @multi_cap: [PCI MSI/X] log2 num of messages supported
114 * @maskbit: [PCI MSI/X] Mask-Pending bit supported?
115 * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit
116 * @entry_nr: [PCI MSI/X] Entry which is described by this descriptor
117 * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq
118 * @mask_pos: [PCI MSI] Mask register position
119 * @mask_base: [PCI MSI-X] Mask register base address
120 * @platform: [platform] Platform device specific msi descriptor data
121 * @fsl_mc: [fsl-mc] FSL MC device specific msi descriptor data
122 * @inta: [INTA] TISCI based INTA specific msi descriptor data
125 /* Shared device/bus type independent data */
126 struct list_head list;
128 unsigned int nvec_used;
131 struct irq_affinity_desc *affinity;
132 #ifdef CONFIG_IRQ_MSI_IOMMU
133 const void *iommu_cookie;
136 void (*write_msi_msg)(struct msi_desc *entry, void *data);
137 void *write_msi_msg_data;
140 /* PCI MSI/X specific data */
151 unsigned default_irq;
155 void __iomem *mask_base;
160 * Non PCI variants add their data structure here. New
161 * entries need to use a named structure. We want
162 * proper name spaces for this. The PCI part is
163 * anonymous for now as it would require an immediate
166 struct platform_msi_desc platform;
167 struct fsl_mc_msi_desc fsl_mc;
168 struct ti_sci_inta_msi_desc inta;
172 /* Helpers to hide struct msi_desc implementation details */
173 #define msi_desc_to_dev(desc) ((desc)->dev)
174 #define dev_to_msi_list(dev) (&(dev)->msi_list)
175 #define first_msi_entry(dev) \
176 list_first_entry(dev_to_msi_list((dev)), struct msi_desc, list)
177 #define for_each_msi_entry(desc, dev) \
178 list_for_each_entry((desc), dev_to_msi_list((dev)), list)
179 #define for_each_msi_entry_safe(desc, tmp, dev) \
180 list_for_each_entry_safe((desc), (tmp), dev_to_msi_list((dev)), list)
182 #ifdef CONFIG_IRQ_MSI_IOMMU
183 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
185 return desc->iommu_cookie;
188 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
189 const void *iommu_cookie)
191 desc->iommu_cookie = iommu_cookie;
194 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
199 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
200 const void *iommu_cookie)
205 #ifdef CONFIG_PCI_MSI
206 #define first_pci_msi_entry(pdev) first_msi_entry(&(pdev)->dev)
207 #define for_each_pci_msi_entry(desc, pdev) \
208 for_each_msi_entry((desc), &(pdev)->dev)
210 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
211 void *msi_desc_to_pci_sysdata(struct msi_desc *desc);
212 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
213 #else /* CONFIG_PCI_MSI */
214 static inline void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
218 static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
221 #endif /* CONFIG_PCI_MSI */
223 struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
224 const struct irq_affinity_desc *affinity);
225 void free_msi_entry(struct msi_desc *entry);
226 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
227 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
229 u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag);
230 u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag);
231 void pci_msi_mask_irq(struct irq_data *data);
232 void pci_msi_unmask_irq(struct irq_data *data);
235 * The arch hooks to setup up msi irqs. Default functions are implemented
236 * as weak symbols so that they /can/ be overriden by architecture specific
237 * code if needed. These hooks must be enabled by the architecture or by
238 * drivers which depend on them via msi_controller based MSI handling.
240 * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
241 * stubs with warnings.
243 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
244 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
245 void arch_teardown_msi_irq(unsigned int irq);
246 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
247 void arch_teardown_msi_irqs(struct pci_dev *dev);
248 void default_teardown_msi_irqs(struct pci_dev *dev);
250 static inline int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
256 static inline void arch_teardown_msi_irqs(struct pci_dev *dev)
263 * The restore hooks are still available as they are useful even
264 * for fully irq domain based setups. Courtesy to XEN/X86.
266 void arch_restore_msi_irqs(struct pci_dev *dev);
267 void default_restore_msi_irqs(struct pci_dev *dev);
269 struct msi_controller {
270 struct module *owner;
272 struct device_node *of_node;
273 struct list_head list;
275 int (*setup_irq)(struct msi_controller *chip, struct pci_dev *dev,
276 struct msi_desc *desc);
277 int (*setup_irqs)(struct msi_controller *chip, struct pci_dev *dev,
279 void (*teardown_irq)(struct msi_controller *chip, unsigned int irq);
282 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
284 #include <linux/irqhandler.h>
287 struct irq_domain_ops;
290 struct fwnode_handle;
291 struct msi_domain_info;
294 * struct msi_domain_ops - MSI interrupt domain callbacks
295 * @get_hwirq: Retrieve the resulting hw irq number
296 * @msi_init: Domain specific init function for MSI interrupts
297 * @msi_free: Domain specific function to free a MSI interrupts
298 * @msi_check: Callback for verification of the domain/info/dev data
299 * @msi_prepare: Prepare the allocation of the interrupts in the domain
300 * @msi_finish: Optional callback to finalize the allocation
301 * @set_desc: Set the msi descriptor for an interrupt
302 * @handle_error: Optional error handler if the allocation fails
303 * @domain_alloc_irqs: Optional function to override the default allocation
305 * @domain_free_irqs: Optional function to override the default free
308 * @get_hwirq, @msi_init and @msi_free are callbacks used by
309 * msi_create_irq_domain() and related interfaces
311 * @msi_check, @msi_prepare, @msi_finish, @set_desc and @handle_error
312 * are callbacks used by msi_domain_alloc_irqs() and related
313 * interfaces which are based on msi_desc.
315 * @domain_alloc_irqs, @domain_free_irqs can be used to override the
316 * default allocation/free functions (__msi_domain_alloc/free_irqs). This
317 * is initially for a wrapper around XENs seperate MSI universe which can't
318 * be wrapped into the regular irq domains concepts by mere mortals. This
319 * allows to universally use msi_domain_alloc/free_irqs without having to
320 * special case XEN all over the place.
322 * Contrary to other operations @domain_alloc_irqs and @domain_free_irqs
323 * are set to the default implementation if NULL and even when
324 * MSI_FLAG_USE_DEF_DOM_OPS is not set to avoid breaking existing users and
325 * because these callbacks are obviously mandatory.
327 * This is NOT meant to be abused, but it can be useful to build wrappers
328 * for specialized MSI irq domains which need extra work before and after
329 * calling __msi_domain_alloc_irqs()/__msi_domain_free_irqs().
331 struct msi_domain_ops {
332 irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
333 msi_alloc_info_t *arg);
334 int (*msi_init)(struct irq_domain *domain,
335 struct msi_domain_info *info,
336 unsigned int virq, irq_hw_number_t hwirq,
337 msi_alloc_info_t *arg);
338 void (*msi_free)(struct irq_domain *domain,
339 struct msi_domain_info *info,
341 int (*msi_check)(struct irq_domain *domain,
342 struct msi_domain_info *info,
344 int (*msi_prepare)(struct irq_domain *domain,
345 struct device *dev, int nvec,
346 msi_alloc_info_t *arg);
347 void (*msi_finish)(msi_alloc_info_t *arg, int retval);
348 void (*set_desc)(msi_alloc_info_t *arg,
349 struct msi_desc *desc);
350 int (*handle_error)(struct irq_domain *domain,
351 struct msi_desc *desc, int error);
352 int (*domain_alloc_irqs)(struct irq_domain *domain,
353 struct device *dev, int nvec);
354 void (*domain_free_irqs)(struct irq_domain *domain,
359 * struct msi_domain_info - MSI interrupt domain data
360 * @flags: Flags to decribe features and capabilities
361 * @ops: The callback data structure
362 * @chip: Optional: associated interrupt chip
363 * @chip_data: Optional: associated interrupt chip data
364 * @handler: Optional: associated interrupt flow handler
365 * @handler_data: Optional: associated interrupt flow handler data
366 * @handler_name: Optional: associated interrupt flow handler name
367 * @data: Optional: domain specific data
369 struct msi_domain_info {
371 struct msi_domain_ops *ops;
372 struct irq_chip *chip;
374 irq_flow_handler_t handler;
376 const char *handler_name;
380 /* Flags for msi_domain_info */
383 * Init non implemented ops callbacks with default MSI domain
386 MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0),
388 * Init non implemented chip callbacks with default MSI chip
391 MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1),
392 /* Support multiple PCI MSI interrupts */
393 MSI_FLAG_MULTI_PCI_MSI = (1 << 2),
394 /* Support PCI MSIX interrupts */
395 MSI_FLAG_PCI_MSIX = (1 << 3),
396 /* Needs early activate, required for PCI */
397 MSI_FLAG_ACTIVATE_EARLY = (1 << 4),
399 * Must reactivate when irq is started even when
400 * MSI_FLAG_ACTIVATE_EARLY has been set.
402 MSI_FLAG_MUST_REACTIVATE = (1 << 5),
403 /* Is level-triggered capable, using two messages */
404 MSI_FLAG_LEVEL_CAPABLE = (1 << 6),
407 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
410 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
411 struct msi_domain_info *info,
412 struct irq_domain *parent);
413 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
415 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
417 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
418 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
419 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
421 struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
422 struct msi_domain_info *info,
423 struct irq_domain *parent);
424 int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
425 irq_write_msi_msg_t write_msi_msg);
426 void platform_msi_domain_free_irqs(struct device *dev);
428 /* When an MSI domain is used as an intermediate domain */
429 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
430 int nvec, msi_alloc_info_t *args);
431 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
432 int virq, int nvec, msi_alloc_info_t *args);
434 __platform_msi_create_device_domain(struct device *dev,
437 irq_write_msi_msg_t write_msi_msg,
438 const struct irq_domain_ops *ops,
441 #define platform_msi_create_device_domain(dev, nvec, write, ops, data) \
442 __platform_msi_create_device_domain(dev, nvec, false, write, ops, data)
443 #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \
444 __platform_msi_create_device_domain(dev, nvec, true, write, ops, data)
446 int platform_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
447 unsigned int nr_irqs);
448 void platform_msi_domain_free(struct irq_domain *domain, unsigned int virq,
450 void *platform_msi_get_host_data(struct irq_domain *domain);
451 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
453 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
454 void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg);
455 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
456 struct msi_domain_info *info,
457 struct irq_domain *parent);
458 int pci_msi_domain_check_cap(struct irq_domain *domain,
459 struct msi_domain_info *info, struct device *dev);
460 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
461 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
462 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev);
464 static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
468 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
470 #endif /* LINUX_MSI_H */