Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / linux / msi.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
3b7d1921
EB
2#ifndef LINUX_MSI_H
3#define LINUX_MSI_H
4
ef3350c5
TG
5/*
6 * This header file contains MSI data structures and functions which are
7 * only relevant for:
8 * - Interrupt core code
9 * - PCI/MSI core code
10 * - MSI interrupt domain implementations
11 * - IOMMU, low level VFIO, NTB and other justified exceptions
12 * dealing with low level MSI details.
13 *
14 * Regular device drivers have no business with any of these functions and
15 * especially storing MSI descriptor pointers in random code is considered
6b6941f6
TG
16 * abuse.
17 *
18 * Device driver relevant functions are available in <linux/msi_api.h>
ef3350c5
TG
19 */
20
22db089a 21#include <linux/irqdomain_defs.h>
e1b6705b 22#include <linux/cpumask_types.h>
6b6941f6 23#include <linux/msi_api.h>
ebca4396 24#include <linux/irq.h>
2d958b02 25
8073c1ac
TG
26#include <asm/msi.h>
27
28/* Dummy shadow structures if an architecture does not define them */
29#ifndef arch_msi_msg_addr_lo
30typedef struct arch_msi_msg_addr_lo {
31 u32 address_lo;
32} __attribute__ ((packed)) arch_msi_msg_addr_lo_t;
33#endif
34
35#ifndef arch_msi_msg_addr_hi
36typedef struct arch_msi_msg_addr_hi {
37 u32 address_hi;
38} __attribute__ ((packed)) arch_msi_msg_addr_hi_t;
39#endif
40
41#ifndef arch_msi_msg_data
42typedef struct arch_msi_msg_data {
43 u32 data;
44} __attribute__ ((packed)) arch_msi_msg_data_t;
45#endif
4aa9bc95 46
bf210f79
JG
47#ifndef arch_is_isolated_msi
48#define arch_is_isolated_msi() false
49#endif
50
8073c1ac
TG
51/**
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
60 */
3b7d1921 61struct msi_msg {
8073c1ac
TG
62 union {
63 u32 address_lo;
64 arch_msi_msg_addr_lo_t arch_addr_lo;
65 };
66 union {
67 u32 address_hi;
68 arch_msi_msg_addr_hi_t arch_addr_hi;
69 };
70 union {
71 u32 data;
72 arch_msi_msg_data_t arch_data;
73 };
3b7d1921
EB
74};
75
c54c1879 76/* Helper functions */
39431acb 77struct msi_desc;
25a98bd4 78struct pci_dev;
bf5e758f 79struct device_attribute;
64258eaa 80struct irq_domain;
3d393b21 81struct irq_affinity_desc;
bf6e054e 82
112e43e9 83void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
2f44e29c 84#ifdef CONFIG_GENERIC_MSI_IRQ
2366d06e 85void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
2f44e29c 86#else
13e7accb 87static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg) { }
2f44e29c 88#endif
891d4a48 89
c09fcc4b
MZ
90typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
91 struct msi_msg *msg);
92
e58f2259
TG
93/**
94 * pci_msi_desc - PCI/MSI specific MSI descriptor data
95 *
96 * @msi_mask: [PCI MSI] MSI cached mask bits
97 * @msix_ctrl: [PCI MSI-X] MSI-X cached per vector control bits
98 * @is_msix: [PCI MSI/X] True if MSI-X
99 * @multiple: [PCI MSI/X] log2 num of messages allocated
100 * @multi_cap: [PCI MSI/X] log2 num of messages supported
101 * @can_mask: [PCI MSI/X] Masking supported?
102 * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit
e58f2259
TG
103 * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq
104 * @mask_pos: [PCI MSI] Mask register position
105 * @mask_base: [PCI MSI-X] Mask register base address
106 */
107struct pci_msi_desc {
108 union {
109 u32 msi_mask;
110 u32 msix_ctrl;
111 };
112 struct {
113 u8 is_msix : 1;
114 u8 multiple : 3;
115 u8 multi_cap : 3;
116 u8 can_mask : 1;
117 u8 is_64 : 1;
118 u8 is_virtual : 1;
e58f2259
TG
119 unsigned default_irq;
120 } msi_attrib;
121 union {
122 u8 mask_pos;
123 void __iomem *mask_base;
124 };
125};
126
efd42049
TG
127/**
128 * union msi_domain_cookie - Opaque MSI domain specific data
129 * @value: u64 value store
130 * @ptr: Pointer to domain specific data
131 * @iobase: Domain specific IOmem pointer
132 *
133 * The content of this data is implementation defined and used by the MSI
134 * domain to store domain specific information which is requried for
135 * interrupt chip callbacks.
136 */
137union msi_domain_cookie {
138 u64 value;
139 void *ptr;
140 void __iomem *iobase;
141};
142
143/**
144 * struct msi_desc_data - Generic MSI descriptor data
145 * @dcookie: Cookie for MSI domain specific data which is required
146 * for irq_chip callbacks
147 * @icookie: Cookie for the MSI interrupt instance provided by
148 * the usage site to the allocation function
149 *
150 * The content of this data is implementation defined, e.g. PCI/IMS
151 * implementations define the meaning of the data. The MSI core ignores
152 * this data completely.
153 */
154struct msi_desc_data {
155 union msi_domain_cookie dcookie;
156 union msi_instance_cookie icookie;
157};
158
645474e2
TG
159#define MSI_MAX_INDEX ((unsigned int)USHRT_MAX)
160
fc88419c
JL
161/**
162 * struct msi_desc - Descriptor structure for MSI based interrupts
fc88419c
JL
163 * @irq: The base interrupt number
164 * @nvec_used: The number of vectors used
165 * @dev: Pointer to the device which uses this descriptor
166 * @msg: The last set MSI message cached for reuse
0972fa57 167 * @affinity: Optional pointer to a cpu affinity mask for this descriptor
1f7df3a6
JG
168 * @iommu_msi_iova: Optional shifted IOVA from the IOMMU to override the msi_addr.
169 * Only used if iommu_msi_shift != 0
170 * @iommu_msi_shift: Indicates how many bits of the original address should be
171 * preserved when using iommu_msi_iova.
bf5e758f 172 * @sysfs_attr: Pointer to sysfs device attribute
fc88419c 173 *
d7cc609f
LG
174 * @write_msi_msg: Callback that may be called when the MSI message
175 * address or data changes
176 * @write_msi_msg_data: Data parameter for the callback.
177 *
20c6d424 178 * @msi_index: Index of the msi descriptor
0f180958 179 * @pci: PCI specific msi descriptor data
efd42049 180 * @data: Generic MSI descriptor data
fc88419c 181 */
3b7d1921 182struct msi_desc {
fc88419c 183 /* Shared device/bus type independent data */
fc88419c
JL
184 unsigned int irq;
185 unsigned int nvec_used;
186 struct device *dev;
187 struct msi_msg msg;
bec04037 188 struct irq_affinity_desc *affinity;
aaebdf8d 189#ifdef CONFIG_IRQ_MSI_IOMMU
1f7df3a6
JG
190 u64 iommu_msi_iova : 58;
191 u64 iommu_msi_shift : 6;
aaebdf8d 192#endif
bf5e758f
TG
193#ifdef CONFIG_SYSFS
194 struct device_attribute *sysfs_attrs;
195#endif
3b7d1921 196
d7cc609f
LG
197 void (*write_msi_msg)(struct msi_desc *entry, void *data);
198 void *write_msi_msg_data;
199
20c6d424 200 u16 msi_index;
efd42049
TG
201 union {
202 struct pci_msi_desc pci;
203 struct msi_desc_data data;
204 };
3b7d1921
EB
205};
206
1046f71d
TG
207/*
208 * Filter values for the MSI descriptor iterators and accessor functions.
209 */
210enum msi_desc_filter {
211 /* All descriptors */
212 MSI_DESC_ALL,
213 /* Descriptors which have no interrupt associated */
214 MSI_DESC_NOTASSOCIATED,
215 /* Descriptors which have an interrupt associated */
216 MSI_DESC_ASSOCIATED,
217};
218
f1139f90
TG
219
220/**
221 * struct msi_dev_domain - The internals of MSI domain info per device
222 * @store: Xarray for storing MSI descriptor pointers
64258eaa 223 * @irqdomain: Pointer to a per device interrupt domain
f1139f90
TG
224 */
225struct msi_dev_domain {
226 struct xarray store;
64258eaa 227 struct irq_domain *domain;
f1139f90
TG
228};
229
013bd8e5
TG
230int msi_setup_device_data(struct device *dev);
231
9357e329
TG
232void __msi_lock_descs(struct device *dev);
233void __msi_unlock_descs(struct device *dev);
5c99e022 234
9357e329
TG
235DEFINE_LOCK_GUARD_1(msi_descs_lock, struct device, __msi_lock_descs(_T->lock),
236 __msi_unlock_descs(_T->lock));
0dac2b09 237
94ff94cf
TG
238struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
239 enum msi_desc_filter filter);
1046f71d
TG
240
241/**
94ff94cf
TG
242 * msi_first_desc - Get the first MSI descriptor of the default irqdomain
243 * @dev: Device to operate on
244 * @filter: Descriptor state filter
245 *
246 * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
247 * must be invoked before the call.
248 *
249 * Return: Pointer to the first MSI descriptor matching the search
250 * criteria, NULL if none found.
251 */
252static inline struct msi_desc *msi_first_desc(struct device *dev,
253 enum msi_desc_filter filter)
254{
255 return msi_domain_first_desc(dev, MSI_DEFAULT_DOMAIN, filter);
256}
257
258struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid,
259 enum msi_desc_filter filter);
260
261/**
262 * msi_domain_for_each_desc - Iterate the MSI descriptors in a specific domain
263 *
264 * @desc: struct msi_desc pointer used as iterator
265 * @dev: struct device pointer - device to iterate
266 * @domid: The id of the interrupt domain which should be walked.
267 * @filter: Filter for descriptor selection
268 *
269 * Notes:
270 * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
271 * pair.
272 * - It is safe to remove a retrieved MSI descriptor in the loop.
273 */
274#define msi_domain_for_each_desc(desc, dev, domid, filter) \
275 for ((desc) = msi_domain_first_desc((dev), (domid), (filter)); (desc); \
276 (desc) = msi_next_desc((dev), (domid), (filter)))
277
278/**
279 * msi_for_each_desc - Iterate the MSI descriptors in the default irqdomain
1046f71d
TG
280 *
281 * @desc: struct msi_desc pointer used as iterator
282 * @dev: struct device pointer - device to iterate
283 * @filter: Filter for descriptor selection
284 *
285 * Notes:
286 * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
287 * pair.
288 * - It is safe to remove a retrieved MSI descriptor in the loop.
289 */
94ff94cf
TG
290#define msi_for_each_desc(desc, dev, filter) \
291 msi_domain_for_each_desc((desc), (dev), MSI_DEFAULT_DOMAIN, (filter))
1046f71d 292
25a98bd4 293#define msi_desc_to_dev(desc) ((desc)->dev)
d31eb342 294
1f7df3a6
JG
295static inline void msi_desc_set_iommu_msi_iova(struct msi_desc *desc, u64 msi_iova,
296 unsigned int msi_shift)
aaebdf8d 297{
1f7df3a6
JG
298#ifdef CONFIG_IRQ_MSI_IOMMU
299 desc->iommu_msi_iova = msi_iova >> msi_shift;
300 desc->iommu_msi_shift = msi_shift;
aaebdf8d 301#endif
aaebdf8d
JG
302}
303
9349887e
JG
304/**
305 * msi_msg_set_addr() - Set MSI address in an MSI message
306 *
307 * @desc: MSI descriptor that may carry an IOVA base address for MSI via @iommu_msi_iova/shift
308 * @msg: Target MSI message to set its address_hi and address_lo
309 * @msi_addr: Physical address to set the MSI message
310 *
311 * Notes:
312 * - Override @msi_addr using the IOVA base address in the @desc if @iommu_msi_shift is set
313 * - Otherwise, simply set @msi_addr to @msg
314 */
315static inline void msi_msg_set_addr(struct msi_desc *desc, struct msi_msg *msg,
316 phys_addr_t msi_addr)
aaebdf8d 317{
9349887e
JG
318#ifdef CONFIG_IRQ_MSI_IOMMU
319 if (desc->iommu_msi_shift) {
320 u64 msi_iova = desc->iommu_msi_iova << desc->iommu_msi_shift;
321
322 msg->address_hi = upper_32_bits(msi_iova);
323 msg->address_lo = lower_32_bits(msi_iova) |
324 (msi_addr & ((1 << desc->iommu_msi_shift) - 1));
325 return;
326 }
aaebdf8d 327#endif
9349887e
JG
328 msg->address_hi = upper_32_bits(msi_addr);
329 msg->address_lo = lower_32_bits(msi_addr);
330}
aaebdf8d 331
fc8ab388
TG
332int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
333 struct msi_desc *init_desc);
334/**
335 * msi_insert_msi_desc - Allocate and initialize a MSI descriptor in the
336 * default irqdomain and insert it at @init_desc->msi_index
337 * @dev: Pointer to the device for which the descriptor is allocated
338 * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor
339 *
340 * Return: 0 on success or an appropriate failure code.
341 */
342static inline int msi_insert_msi_desc(struct device *dev, struct msi_desc *init_desc)
343{
344 return msi_domain_insert_msi_desc(dev, MSI_DEFAULT_DOMAIN, init_desc);
345}
346
377712c5
TG
347void msi_domain_free_msi_descs_range(struct device *dev, unsigned int domid,
348 unsigned int first, unsigned int last);
645474e2
TG
349
350/**
377712c5
TG
351 * msi_free_msi_descs_range - Free a range of MSI descriptors of a device
352 * in the default irqdomain
353 *
354 * @dev: Device for which to free the descriptors
355 * @first: Index to start freeing from (inclusive)
356 * @last: Last index to be freed (inclusive)
357 */
358static inline void msi_free_msi_descs_range(struct device *dev, unsigned int first,
359 unsigned int last)
360{
361 msi_domain_free_msi_descs_range(dev, MSI_DEFAULT_DOMAIN, first, last);
362}
363
364/**
365 * msi_free_msi_descs - Free all MSI descriptors of a device in the default irqdomain
645474e2
TG
366 * @dev: Device to free the descriptors
367 */
368static inline void msi_free_msi_descs(struct device *dev)
369{
2f2940d1 370 msi_free_msi_descs_range(dev, 0, MSI_MAX_INDEX);
645474e2 371}
60290525 372
3b7d1921 373/*
077ee78e
TG
374 * The arch hooks to setup up msi irqs. Default functions are implemented
375 * as weak symbols so that they /can/ be overriden by architecture specific
b227be0d 376 * code if needed. These hooks can only be enabled by the architecture.
077ee78e
TG
377 *
378 * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
379 * stubs with warnings.
3b7d1921 380 */
077ee78e 381#ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
f7feaca7 382int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
3b7d1921 383void arch_teardown_msi_irq(unsigned int irq);
2366d06e
BH
384int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
385void arch_teardown_msi_irqs(struct pci_dev *dev);
335b4223
MH
386#endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
387
388/*
389 * Xen uses non-default msi_domain_ops and hence needs a way to populate sysfs
390 * entries of MSI IRQs.
391 */
392#if defined(CONFIG_PCI_XEN) || defined(CONFIG_PCI_MSI_ARCH_FALLBACKS)
bf5e758f
TG
393#ifdef CONFIG_SYSFS
394int msi_device_populate_sysfs(struct device *dev);
395void msi_device_destroy_sysfs(struct device *dev);
396#else /* CONFIG_SYSFS */
397static inline int msi_device_populate_sysfs(struct device *dev) { return 0; }
398static inline void msi_device_destroy_sysfs(struct device *dev) { }
399#endif /* !CONFIG_SYSFS */
335b4223 400#endif /* CONFIG_PCI_XEN || CONFIG_PCI_MSI_ARCH_FALLBACKS */
077ee78e
TG
401
402/*
ae72f315
TG
403 * The restore hook is still available even for fully irq domain based
404 * setups. Courtesy to XEN/X86.
077ee78e 405 */
ae72f315 406bool arch_restore_msi_irqs(struct pci_dev *dev);
3b7d1921 407
13e7accb 408#ifdef CONFIG_GENERIC_MSI_IRQ
d9109698 409
aeeb5965 410#include <linux/irqhandler.h>
d9109698 411
f3cf8bb0 412struct irq_domain;
552c494a 413struct irq_domain_ops;
f3cf8bb0 414struct irq_chip;
9c78c1a8 415struct irq_fwspec;
f3cf8bb0 416struct device_node;
be5436c8 417struct fwnode_handle;
f3cf8bb0
JL
418struct msi_domain_info;
419
420/**
421 * struct msi_domain_ops - MSI interrupt domain callbacks
422 * @get_hwirq: Retrieve the resulting hw irq number
423 * @msi_init: Domain specific init function for MSI interrupts
424 * @msi_free: Domain specific function to free a MSI interrupts
d9109698 425 * @msi_prepare: Prepare the allocation of the interrupts in the domain
28026cf2 426 * @msi_teardown: Reverse the effects of @msi_prepare
8f986fd7
TG
427 * @prepare_desc: Optional function to prepare the allocated MSI descriptor
428 * in the domain
d9109698 429 * @set_desc: Set the msi descriptor for an interrupt
43e9e705
TG
430 * @domain_alloc_irqs: Optional function to override the default allocation
431 * function.
432 * @domain_free_irqs: Optional function to override the default free
433 * function.
f6d3486a
TG
434 * @msi_post_free: Optional function which is invoked after freeing
435 * all interrupts.
9c78c1a8
TG
436 * @msi_translate: Optional translate callback to support the odd wire to
437 * MSI bridges, e.g. MBIGEN
d9109698 438 *
1dd2c6a0
TG
439 * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying
440 * irqdomain.
d9109698 441 *
28026cf2
MZ
442 * @msi_check, @msi_prepare, @msi_teardown, @prepare_desc and
443 * @set_desc are callbacks used by the msi_domain_alloc/free_irqs*()
444 * variants.
43e9e705
TG
445 *
446 * @domain_alloc_irqs, @domain_free_irqs can be used to override the
447 * default allocation/free functions (__msi_domain_alloc/free_irqs). This
448 * is initially for a wrapper around XENs seperate MSI universe which can't
449 * be wrapped into the regular irq domains concepts by mere mortals. This
450 * allows to universally use msi_domain_alloc/free_irqs without having to
451 * special case XEN all over the place.
f3cf8bb0
JL
452 */
453struct msi_domain_ops {
aeeb5965
JL
454 irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
455 msi_alloc_info_t *arg);
f3cf8bb0
JL
456 int (*msi_init)(struct irq_domain *domain,
457 struct msi_domain_info *info,
458 unsigned int virq, irq_hw_number_t hwirq,
aeeb5965 459 msi_alloc_info_t *arg);
f3cf8bb0
JL
460 void (*msi_free)(struct irq_domain *domain,
461 struct msi_domain_info *info,
462 unsigned int virq);
d9109698
JL
463 int (*msi_prepare)(struct irq_domain *domain,
464 struct device *dev, int nvec,
465 msi_alloc_info_t *arg);
28026cf2
MZ
466 void (*msi_teardown)(struct irq_domain *domain,
467 msi_alloc_info_t *arg);
8f986fd7
TG
468 void (*prepare_desc)(struct irq_domain *domain, msi_alloc_info_t *arg,
469 struct msi_desc *desc);
d9109698
JL
470 void (*set_desc)(msi_alloc_info_t *arg,
471 struct msi_desc *desc);
43e9e705
TG
472 int (*domain_alloc_irqs)(struct irq_domain *domain,
473 struct device *dev, int nvec);
474 void (*domain_free_irqs)(struct irq_domain *domain,
475 struct device *dev);
f6d3486a
TG
476 void (*msi_post_free)(struct irq_domain *domain,
477 struct device *dev);
9c78c1a8
TG
478 int (*msi_translate)(struct irq_domain *domain, struct irq_fwspec *fwspec,
479 irq_hw_number_t *hwirq, unsigned int *type);
f3cf8bb0
JL
480};
481
482/**
483 * struct msi_domain_info - MSI interrupt domain data
aeeb5965 484 * @flags: Flags to decribe features and capabilities
22db089a 485 * @bus_token: The domain bus token
61bf992f
TG
486 * @hwsize: The hardware table size or the software index limit.
487 * If 0 then the size is considered unlimited and
488 * gets initialized to the maximum software index limit
489 * by the domain creation code.
aeeb5965
JL
490 * @ops: The callback data structure
491 * @chip: Optional: associated interrupt chip
492 * @chip_data: Optional: associated interrupt chip data
493 * @handler: Optional: associated interrupt flow handler
494 * @handler_data: Optional: associated interrupt flow handler data
495 * @handler_name: Optional: associated interrupt flow handler name
28026cf2 496 * @alloc_data: Optional: associated interrupt allocation data
aeeb5965 497 * @data: Optional: domain specific data
f3cf8bb0
JL
498 */
499struct msi_domain_info {
22db089a
AD
500 u32 flags;
501 enum irq_domain_bus_token bus_token;
61bf992f 502 unsigned int hwsize;
22db089a
AD
503 struct msi_domain_ops *ops;
504 struct irq_chip *chip;
505 void *chip_data;
506 irq_flow_handler_t handler;
507 void *handler_data;
508 const char *handler_name;
28026cf2 509 msi_alloc_info_t *alloc_data;
22db089a 510 void *data;
f3cf8bb0
JL
511};
512
ebca4396
TG
513/**
514 * struct msi_domain_template - Template for MSI device domains
515 * @name: Storage for the resulting name. Filled in by the core.
516 * @chip: Interrupt chip for this domain
517 * @ops: MSI domain ops
518 * @info: MSI domain info data
1396e89e 519 * @alloc_info: MSI domain allocation data (architecture specific)
ebca4396
TG
520 */
521struct msi_domain_template {
522 char name[48];
523 struct irq_chip chip;
524 struct msi_domain_ops ops;
525 struct msi_domain_info info;
1396e89e 526 msi_alloc_info_t alloc_info;
ebca4396
TG
527};
528
2d958b02
TG
529/*
530 * Flags for msi_domain_info
531 *
532 * Bit 0-15: Generic MSI functionality which is not subject to restriction
533 * by parent domains
534 *
535 * Bit 16-31: Functionality which depends on the underlying parent domain and
536 * can be masked out by msi_parent_ops::init_dev_msi_info() when
537 * a device MSI domain is initialized.
538 */
aeeb5965
JL
539enum {
540 /*
541 * Init non implemented ops callbacks with default MSI domain
542 * callbacks.
543 */
544 MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0),
545 /*
546 * Init non implemented chip callbacks with default MSI chip
547 * callbacks.
548 */
549 MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1),
f3b0946d 550 /* Needs early activate, required for PCI */
2d958b02 551 MSI_FLAG_ACTIVATE_EARLY = (1 << 2),
22d0b12f
TG
552 /*
553 * Must reactivate when irq is started even when
554 * MSI_FLAG_ACTIVATE_EARLY has been set.
555 */
2d958b02 556 MSI_FLAG_MUST_REACTIVATE = (1 << 3),
013bd8e5 557 /* Populate sysfs on alloc() and destroy it on free() */
2d958b02 558 MSI_FLAG_DEV_SYSFS = (1 << 4),
645474e2 559 /* Allocate simple MSI descriptors */
2d958b02 560 MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS = (1 << 5),
645474e2 561 /* Free MSI descriptors */
2d958b02 562 MSI_FLAG_FREE_MSI_DESCS = (1 << 6),
9d1c58c8
TG
563 /* Use dev->fwnode for MSI device domain creation */
564 MSI_FLAG_USE_DEV_FWNODE = (1 << 7),
9bbe13a5
TG
565 /* Set parent->dev into domain->pm_dev on device domain creation */
566 MSI_FLAG_PARENT_PM_DEV = (1 << 8),
7d189c77
SS
567 /* Support for parent mask/unmask */
568 MSI_FLAG_PCI_MSI_MASK_PARENT = (1 << 9),
2d958b02
TG
569
570 /* Mask for the generic functionality */
571 MSI_GENERIC_FLAGS_MASK = GENMASK(15, 0),
572
573 /* Mask for the domain specific functionality */
574 MSI_DOMAIN_FLAGS_MASK = GENMASK(31, 16),
575
576 /* Support multiple PCI MSI interrupts */
577 MSI_FLAG_MULTI_PCI_MSI = (1 << 16),
578 /* Support PCI MSIX interrupts */
579 MSI_FLAG_PCI_MSIX = (1 << 17),
580 /* Is level-triggered capable, using two messages */
581 MSI_FLAG_LEVEL_CAPABLE = (1 << 18),
582 /* MSI-X entries must be contiguous */
583 MSI_FLAG_MSIX_CONTIGUOUS = (1 << 19),
b834e3c0
TG
584 /* PCI/MSI-X vectors can be dynamically allocated/freed post MSI-X enable */
585 MSI_FLAG_PCI_MSIX_ALLOC_DYN = (1 << 20),
5297bba5
MV
586 /* PCI MSIs cannot be steered separately to CPU cores */
587 MSI_FLAG_NO_AFFINITY = (1 << 21),
c3164d2e
RPM
588 /* Inhibit usage of entry masking */
589 MSI_FLAG_NO_MASK = (1 << 22),
aeeb5965
JL
590};
591
1c000dca
TG
592/*
593 * Flags for msi_parent_ops::chip_flags
594 */
595enum {
596 MSI_CHIP_FLAG_SET_EOI = (1 << 0),
597 MSI_CHIP_FLAG_SET_ACK = (1 << 1),
598};
599
b78780d9
TG
600/**
601 * struct msi_parent_ops - MSI parent domain callbacks and configuration info
602 *
603 * @supported_flags: Required: The supported MSI flags of the parent domain
ac81e94a 604 * @required_flags: Optional: The required MSI flags of the parent MSI domain
1c000dca
TG
605 * @chip_flags: Optional: Select MSI chip callbacks to update with defaults
606 * in msi_lib_init_dev_msi_info().
ac81e94a
TG
607 * @bus_select_token: Optional: The bus token of the real parent domain for
608 * irq_domain::select()
609 * @bus_select_mask: Optional: A mask of supported BUS_DOMAINs for
610 * irq_domain::select()
b78780d9
TG
611 * @prefix: Optional: Prefix for the domain and chip name
612 * @init_dev_msi_info: Required: Callback for MSI parent domains to setup parent
613 * domain specific domain flags, domain ops and interrupt chip
614 * callbacks when a per device domain is created.
615 */
616struct msi_parent_ops {
617 u32 supported_flags;
ac81e94a 618 u32 required_flags;
1c000dca 619 u32 chip_flags;
ac81e94a
TG
620 u32 bus_select_token;
621 u32 bus_select_mask;
b78780d9
TG
622 const char *prefix;
623 bool (*init_dev_msi_info)(struct device *dev, struct irq_domain *domain,
624 struct irq_domain *msi_parent_domain,
625 struct msi_domain_info *msi_child_info);
626};
627
628bool msi_parent_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
629 struct irq_domain *msi_parent_domain,
630 struct msi_domain_info *msi_child_info);
631
f3cf8bb0
JL
632int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
633 bool force);
634
be5436c8 635struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
f3cf8bb0
JL
636 struct msi_domain_info *info,
637 struct irq_domain *parent);
4cd5f440 638
e4d001b5
MZ
639struct irq_domain_info;
640struct irq_domain *msi_create_parent_irq_domain(struct irq_domain_info *info,
641 const struct msi_parent_ops *msi_parent_ops);
642
27a6dea3
TG
643bool msi_create_device_irq_domain(struct device *dev, unsigned int domid,
644 const struct msi_domain_template *template,
645 unsigned int hwsize, void *domain_data,
646 void *chip_data);
647void msi_remove_device_irq_domain(struct device *dev, unsigned int domid);
648
26e91b75
TG
649bool msi_match_device_irq_domain(struct device *dev, unsigned int domid,
650 enum irq_domain_bus_token bus_token);
651
112e43e9
LT
652int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
653 unsigned int first, unsigned int last);
f2480e7d
TG
654int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
655 unsigned int first, unsigned int last);
656int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs);
657
3d393b21
TG
658struct msi_map msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, unsigned int index,
659 const struct irq_affinity_desc *affdesc,
660 union msi_instance_cookie *cookie);
f2480e7d 661
112e43e9
LT
662void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
663 unsigned int first, unsigned int last);
4cd5f440
TG
664void msi_domain_free_irqs_range(struct device *dev, unsigned int domid,
665 unsigned int first, unsigned int last);
4cd5f440
TG
666void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid);
667void msi_domain_free_irqs_all(struct device *dev, unsigned int domid);
668
f3cf8bb0
JL
669struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
670
c88f9110
TG
671/* Per device platform MSI */
672int platform_device_msi_init_and_alloc_irqs(struct device *dev, unsigned int nvec,
673 irq_write_msi_msg_t write_msi_msg);
674void platform_device_msi_free_irqs_all(struct device *dev);
17cde5e6
JG
675
676bool msi_device_has_isolated_msi(struct device *dev);
06fe8fd6
NG
677
678static inline int msi_domain_alloc_irqs(struct device *dev, unsigned int domid, int nirqs)
679{
680 return msi_domain_alloc_irqs_range(dev, domid, 0, nirqs - 1);
681}
682
17cde5e6
JG
683#else /* CONFIG_GENERIC_MSI_IRQ */
684static inline bool msi_device_has_isolated_msi(struct device *dev)
685{
686 /*
687 * Arguably if the platform does not enable MSI support then it has
688 * "isolated MSI", as an interrupt controller that cannot receive MSIs
bf210f79
JG
689 * is inherently isolated by our definition. The default definition for
690 * arch_is_isolated_msi() is conservative and returns false anyhow.
17cde5e6 691 */
bf210f79 692 return arch_is_isolated_msi();
17cde5e6 693}
13e7accb 694#endif /* CONFIG_GENERIC_MSI_IRQ */
f3cf8bb0 695
a474d3fb
TG
696/* PCI specific interfaces */
697#ifdef CONFIG_PCI_MSI
698struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
699void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
700void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
701void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
702void pci_msi_mask_irq(struct irq_data *data);
703void pci_msi_unmask_irq(struct irq_data *data);
be5436c8 704struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
3878eaef
JL
705 struct msi_domain_info *info,
706 struct irq_domain *parent);
b6eec9b7 707u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
54fa97ee 708struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
a474d3fb 709#else /* CONFIG_PCI_MSI */
54fa97ee
MZ
710static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
711{
712 return NULL;
713}
a474d3fb
TG
714static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg) { }
715#endif /* !CONFIG_PCI_MSI */
3878eaef 716
3b7d1921 717#endif /* LINUX_MSI_H */