1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/cpumask.h>
6 #include <linux/mutex.h>
7 #include <linux/list.h>
10 /* Dummy shadow structures if an architecture does not define them */
11 #ifndef arch_msi_msg_addr_lo
12 typedef struct arch_msi_msg_addr_lo {
14 } __attribute__ ((packed)) arch_msi_msg_addr_lo_t;
17 #ifndef arch_msi_msg_addr_hi
18 typedef struct arch_msi_msg_addr_hi {
20 } __attribute__ ((packed)) arch_msi_msg_addr_hi_t;
23 #ifndef arch_msi_msg_data
24 typedef struct arch_msi_msg_data {
26 } __attribute__ ((packed)) arch_msi_msg_data_t;
30 * msi_msg - Representation of a MSI message
31 * @address_lo: Low 32 bits of msi message address
32 * @arch_addrlo: Architecture specific shadow of @address_lo
33 * @address_hi: High 32 bits of msi message address
34 * (only used when device supports it)
35 * @arch_addrhi: Architecture specific shadow of @address_hi
36 * @data: MSI message data (usually 16 bits)
37 * @arch_data: Architecture specific shadow of @data
42 arch_msi_msg_addr_lo_t arch_addr_lo;
46 arch_msi_msg_addr_hi_t arch_addr_hi;
50 arch_msi_msg_data_t arch_data;
54 extern int pci_msi_ignore_mask;
55 /* Helper functions */
59 struct platform_msi_priv_data;
60 struct attribute_group;
62 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
63 #ifdef CONFIG_GENERIC_MSI_IRQ
64 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
66 static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
71 typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
75 * pci_msi_desc - PCI/MSI specific MSI descriptor data
77 * @msi_mask: [PCI MSI] MSI cached mask bits
78 * @msix_ctrl: [PCI MSI-X] MSI-X cached per vector control bits
79 * @is_msix: [PCI MSI/X] True if MSI-X
80 * @multiple: [PCI MSI/X] log2 num of messages allocated
81 * @multi_cap: [PCI MSI/X] log2 num of messages supported
82 * @can_mask: [PCI MSI/X] Masking supported?
83 * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit
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
100 unsigned default_irq;
104 void __iomem *mask_base;
108 #define MSI_MAX_INDEX ((unsigned int)USHRT_MAX)
111 * struct msi_desc - Descriptor structure for MSI based interrupts
112 * @list: List head for management
113 * @irq: The base interrupt number
114 * @nvec_used: The number of vectors used
115 * @dev: Pointer to the device which uses this descriptor
116 * @msg: The last set MSI message cached for reuse
117 * @affinity: Optional pointer to a cpu affinity mask for this descriptor
119 * @write_msi_msg: Callback that may be called when the MSI message
120 * address or data changes
121 * @write_msi_msg_data: Data parameter for the callback.
123 * @msi_index: Index of the msi descriptor
124 * @pci: PCI specific msi descriptor data
127 /* Shared device/bus type independent data */
128 struct list_head list;
130 unsigned int nvec_used;
133 struct irq_affinity_desc *affinity;
134 #ifdef CONFIG_IRQ_MSI_IOMMU
135 const void *iommu_cookie;
138 void (*write_msi_msg)(struct msi_desc *entry, void *data);
139 void *write_msi_msg_data;
142 struct pci_msi_desc pci;
146 * Filter values for the MSI descriptor iterators and accessor functions.
148 enum msi_desc_filter {
149 /* All descriptors */
151 /* Descriptors which have no interrupt associated */
152 MSI_DESC_NOTASSOCIATED,
153 /* Descriptors which have an interrupt associated */
158 * msi_device_data - MSI per device data
159 * @properties: MSI properties which are interesting to drivers
160 * @attrs: Pointer to the sysfs attribute group
161 * @platform_data: Platform-MSI specific data
162 * @list: List of MSI descriptors associated to the device
163 * @mutex: Mutex protecting the MSI list
164 * @__next: Cached pointer to the next entry for iterators
166 struct msi_device_data {
167 unsigned long properties;
168 const struct attribute_group **attrs;
169 struct platform_msi_priv_data *platform_data;
170 struct list_head list;
172 struct msi_desc *__next;
175 int msi_setup_device_data(struct device *dev);
177 unsigned int msi_get_virq(struct device *dev, unsigned int index);
178 void msi_lock_descs(struct device *dev);
179 void msi_unlock_descs(struct device *dev);
181 struct msi_desc *msi_first_desc(struct device *dev, enum msi_desc_filter filter);
182 struct msi_desc *msi_next_desc(struct device *dev, enum msi_desc_filter filter);
185 * msi_for_each_desc - Iterate the MSI descriptors
187 * @desc: struct msi_desc pointer used as iterator
188 * @dev: struct device pointer - device to iterate
189 * @filter: Filter for descriptor selection
192 * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
194 * - It is safe to remove a retrieved MSI descriptor in the loop.
196 #define msi_for_each_desc(desc, dev, filter) \
197 for ((desc) = msi_first_desc((dev), (filter)); (desc); \
198 (desc) = msi_next_desc((dev), (filter)))
200 /* Helpers to hide struct msi_desc implementation details */
201 #define msi_desc_to_dev(desc) ((desc)->dev)
202 #define dev_to_msi_list(dev) (&(dev)->msi.data->list)
203 #define first_msi_entry(dev) \
204 list_first_entry(dev_to_msi_list((dev)), struct msi_desc, list)
205 #define for_each_msi_entry(desc, dev) \
206 list_for_each_entry((desc), dev_to_msi_list((dev)), list)
207 #define for_each_msi_entry_safe(desc, tmp, dev) \
208 list_for_each_entry_safe((desc), (tmp), dev_to_msi_list((dev)), list)
209 #define for_each_msi_vector(desc, __irq, dev) \
210 for_each_msi_entry((desc), (dev)) \
212 for (__irq = (desc)->irq; \
213 __irq < ((desc)->irq + (desc)->nvec_used); \
216 #ifdef CONFIG_IRQ_MSI_IOMMU
217 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
219 return desc->iommu_cookie;
222 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
223 const void *iommu_cookie)
225 desc->iommu_cookie = iommu_cookie;
228 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
233 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
234 const void *iommu_cookie)
239 #ifdef CONFIG_PCI_MSI
240 #define first_pci_msi_entry(pdev) first_msi_entry(&(pdev)->dev)
241 #define for_each_pci_msi_entry(desc, pdev) \
242 for_each_msi_entry((desc), &(pdev)->dev)
244 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
245 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
246 #else /* CONFIG_PCI_MSI */
247 static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
250 #endif /* CONFIG_PCI_MSI */
252 int msi_add_msi_desc(struct device *dev, struct msi_desc *init_desc);
253 void msi_free_msi_descs_range(struct device *dev, enum msi_desc_filter filter,
254 unsigned int first_index, unsigned int last_index);
257 * msi_free_msi_descs - Free MSI descriptors of a device
258 * @dev: Device to free the descriptors
260 static inline void msi_free_msi_descs(struct device *dev)
262 msi_free_msi_descs_range(dev, MSI_DESC_ALL, 0, MSI_MAX_INDEX);
265 struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
266 const struct irq_affinity_desc *affinity);
267 void free_msi_entry(struct msi_desc *entry);
268 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
269 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
271 void pci_msi_mask_irq(struct irq_data *data);
272 void pci_msi_unmask_irq(struct irq_data *data);
275 int msi_device_populate_sysfs(struct device *dev);
276 void msi_device_destroy_sysfs(struct device *dev);
277 #else /* CONFIG_SYSFS */
278 static inline int msi_device_populate_sysfs(struct device *dev) { return 0; }
279 static inline void msi_device_destroy_sysfs(struct device *dev) { }
280 #endif /* !CONFIG_SYSFS */
283 * The arch hooks to setup up msi irqs. Default functions are implemented
284 * as weak symbols so that they /can/ be overriden by architecture specific
285 * code if needed. These hooks can only be enabled by the architecture.
287 * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
288 * stubs with warnings.
290 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
291 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
292 void arch_teardown_msi_irq(unsigned int irq);
293 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
294 void arch_teardown_msi_irqs(struct pci_dev *dev);
295 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
298 * The restore hook is still available even for fully irq domain based
299 * setups. Courtesy to XEN/X86.
301 bool arch_restore_msi_irqs(struct pci_dev *dev);
303 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
305 #include <linux/irqhandler.h>
308 struct irq_domain_ops;
311 struct fwnode_handle;
312 struct msi_domain_info;
315 * struct msi_domain_ops - MSI interrupt domain callbacks
316 * @get_hwirq: Retrieve the resulting hw irq number
317 * @msi_init: Domain specific init function for MSI interrupts
318 * @msi_free: Domain specific function to free a MSI interrupts
319 * @msi_check: Callback for verification of the domain/info/dev data
320 * @msi_prepare: Prepare the allocation of the interrupts in the domain
321 * @set_desc: Set the msi descriptor for an interrupt
322 * @domain_alloc_irqs: Optional function to override the default allocation
324 * @domain_free_irqs: Optional function to override the default free
327 * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying
330 * @msi_check, @msi_prepare and @set_desc are callbacks used by
331 * msi_domain_alloc/free_irqs().
333 * @domain_alloc_irqs, @domain_free_irqs can be used to override the
334 * default allocation/free functions (__msi_domain_alloc/free_irqs). This
335 * is initially for a wrapper around XENs seperate MSI universe which can't
336 * be wrapped into the regular irq domains concepts by mere mortals. This
337 * allows to universally use msi_domain_alloc/free_irqs without having to
338 * special case XEN all over the place.
340 * Contrary to other operations @domain_alloc_irqs and @domain_free_irqs
341 * are set to the default implementation if NULL and even when
342 * MSI_FLAG_USE_DEF_DOM_OPS is not set to avoid breaking existing users and
343 * because these callbacks are obviously mandatory.
345 * This is NOT meant to be abused, but it can be useful to build wrappers
346 * for specialized MSI irq domains which need extra work before and after
347 * calling __msi_domain_alloc_irqs()/__msi_domain_free_irqs().
349 struct msi_domain_ops {
350 irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
351 msi_alloc_info_t *arg);
352 int (*msi_init)(struct irq_domain *domain,
353 struct msi_domain_info *info,
354 unsigned int virq, irq_hw_number_t hwirq,
355 msi_alloc_info_t *arg);
356 void (*msi_free)(struct irq_domain *domain,
357 struct msi_domain_info *info,
359 int (*msi_check)(struct irq_domain *domain,
360 struct msi_domain_info *info,
362 int (*msi_prepare)(struct irq_domain *domain,
363 struct device *dev, int nvec,
364 msi_alloc_info_t *arg);
365 void (*set_desc)(msi_alloc_info_t *arg,
366 struct msi_desc *desc);
367 int (*domain_alloc_irqs)(struct irq_domain *domain,
368 struct device *dev, int nvec);
369 void (*domain_free_irqs)(struct irq_domain *domain,
374 * struct msi_domain_info - MSI interrupt domain data
375 * @flags: Flags to decribe features and capabilities
376 * @ops: The callback data structure
377 * @chip: Optional: associated interrupt chip
378 * @chip_data: Optional: associated interrupt chip data
379 * @handler: Optional: associated interrupt flow handler
380 * @handler_data: Optional: associated interrupt flow handler data
381 * @handler_name: Optional: associated interrupt flow handler name
382 * @data: Optional: domain specific data
384 struct msi_domain_info {
386 struct msi_domain_ops *ops;
387 struct irq_chip *chip;
389 irq_flow_handler_t handler;
391 const char *handler_name;
395 /* Flags for msi_domain_info */
398 * Init non implemented ops callbacks with default MSI domain
401 MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0),
403 * Init non implemented chip callbacks with default MSI chip
406 MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1),
407 /* Support multiple PCI MSI interrupts */
408 MSI_FLAG_MULTI_PCI_MSI = (1 << 2),
409 /* Support PCI MSIX interrupts */
410 MSI_FLAG_PCI_MSIX = (1 << 3),
411 /* Needs early activate, required for PCI */
412 MSI_FLAG_ACTIVATE_EARLY = (1 << 4),
414 * Must reactivate when irq is started even when
415 * MSI_FLAG_ACTIVATE_EARLY has been set.
417 MSI_FLAG_MUST_REACTIVATE = (1 << 5),
418 /* Is level-triggered capable, using two messages */
419 MSI_FLAG_LEVEL_CAPABLE = (1 << 6),
420 /* Populate sysfs on alloc() and destroy it on free() */
421 MSI_FLAG_DEV_SYSFS = (1 << 7),
422 /* MSI-X entries must be contiguous */
423 MSI_FLAG_MSIX_CONTIGUOUS = (1 << 8),
424 /* Allocate simple MSI descriptors */
425 MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS = (1 << 9),
426 /* Free MSI descriptors */
427 MSI_FLAG_FREE_MSI_DESCS = (1 << 10),
430 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
433 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
434 struct msi_domain_info *info,
435 struct irq_domain *parent);
436 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
438 int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev,
440 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
442 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
443 void msi_domain_free_irqs_descs_locked(struct irq_domain *domain, struct device *dev);
444 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
445 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
447 struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
448 struct msi_domain_info *info,
449 struct irq_domain *parent);
450 int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
451 irq_write_msi_msg_t write_msi_msg);
452 void platform_msi_domain_free_irqs(struct device *dev);
454 /* When an MSI domain is used as an intermediate domain */
455 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
456 int nvec, msi_alloc_info_t *args);
457 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
458 int virq, int nvec, msi_alloc_info_t *args);
460 __platform_msi_create_device_domain(struct device *dev,
463 irq_write_msi_msg_t write_msi_msg,
464 const struct irq_domain_ops *ops,
467 #define platform_msi_create_device_domain(dev, nvec, write, ops, data) \
468 __platform_msi_create_device_domain(dev, nvec, false, write, ops, data)
469 #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \
470 __platform_msi_create_device_domain(dev, nvec, true, write, ops, data)
472 int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq,
473 unsigned int nr_irqs);
474 void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq,
476 void *platform_msi_get_host_data(struct irq_domain *domain);
477 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
479 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
480 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
481 struct msi_domain_info *info,
482 struct irq_domain *parent);
483 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
484 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
485 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev);
487 static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
491 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
493 #endif /* LINUX_MSI_H */