genirq/msi: Provide domain flags to allocate/free MSI descriptors automatically
[linux-2.6-microblaze.git] / include / linux / msi.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef LINUX_MSI_H
3 #define LINUX_MSI_H
4
5 #include <linux/cpumask.h>
6 #include <linux/mutex.h>
7 #include <linux/list.h>
8 #include <asm/msi.h>
9
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 {
13         u32     address_lo;
14 } __attribute__ ((packed)) arch_msi_msg_addr_lo_t;
15 #endif
16
17 #ifndef arch_msi_msg_addr_hi
18 typedef struct arch_msi_msg_addr_hi {
19         u32     address_hi;
20 } __attribute__ ((packed)) arch_msi_msg_addr_hi_t;
21 #endif
22
23 #ifndef arch_msi_msg_data
24 typedef struct arch_msi_msg_data {
25         u32     data;
26 } __attribute__ ((packed)) arch_msi_msg_data_t;
27 #endif
28
29 /**
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
38  */
39 struct msi_msg {
40         union {
41                 u32                     address_lo;
42                 arch_msi_msg_addr_lo_t  arch_addr_lo;
43         };
44         union {
45                 u32                     address_hi;
46                 arch_msi_msg_addr_hi_t  arch_addr_hi;
47         };
48         union {
49                 u32                     data;
50                 arch_msi_msg_data_t     arch_data;
51         };
52 };
53
54 extern int pci_msi_ignore_mask;
55 /* Helper functions */
56 struct irq_data;
57 struct msi_desc;
58 struct pci_dev;
59 struct platform_msi_priv_data;
60 struct attribute_group;
61
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);
65 #else
66 static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
67 {
68 }
69 #endif
70
71 typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
72                                     struct msi_msg *msg);
73
74 /**
75  * pci_msi_desc - PCI/MSI specific MSI descriptor data
76  *
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
87  */
88 struct pci_msi_desc {
89         union {
90                 u32 msi_mask;
91                 u32 msix_ctrl;
92         };
93         struct {
94                 u8      is_msix         : 1;
95                 u8      multiple        : 3;
96                 u8      multi_cap       : 3;
97                 u8      can_mask        : 1;
98                 u8      is_64           : 1;
99                 u8      is_virtual      : 1;
100                 unsigned default_irq;
101         } msi_attrib;
102         union {
103                 u8      mask_pos;
104                 void __iomem *mask_base;
105         };
106 };
107
108 #define MSI_MAX_INDEX           ((unsigned int)USHRT_MAX)
109
110 /**
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
118  *
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.
122  *
123  * @msi_index:  Index of the msi descriptor
124  * @pci:        PCI specific msi descriptor data
125  */
126 struct msi_desc {
127         /* Shared device/bus type independent data */
128         struct list_head                list;
129         unsigned int                    irq;
130         unsigned int                    nvec_used;
131         struct device                   *dev;
132         struct msi_msg                  msg;
133         struct irq_affinity_desc        *affinity;
134 #ifdef CONFIG_IRQ_MSI_IOMMU
135         const void                      *iommu_cookie;
136 #endif
137
138         void (*write_msi_msg)(struct msi_desc *entry, void *data);
139         void *write_msi_msg_data;
140
141         u16                             msi_index;
142         struct pci_msi_desc             pci;
143 };
144
145 /*
146  * Filter values for the MSI descriptor iterators and accessor functions.
147  */
148 enum msi_desc_filter {
149         /* All descriptors */
150         MSI_DESC_ALL,
151         /* Descriptors which have no interrupt associated */
152         MSI_DESC_NOTASSOCIATED,
153         /* Descriptors which have an interrupt associated */
154         MSI_DESC_ASSOCIATED,
155 };
156
157 /**
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
165  */
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;
171         struct mutex                    mutex;
172         struct msi_desc                 *__next;
173 };
174
175 int msi_setup_device_data(struct device *dev);
176
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);
180
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);
183
184 /**
185  * msi_for_each_desc - Iterate the MSI descriptors
186  *
187  * @desc:       struct msi_desc pointer used as iterator
188  * @dev:        struct device pointer - device to iterate
189  * @filter:     Filter for descriptor selection
190  *
191  * Notes:
192  *  - The loop must be protected with a msi_lock_descs()/msi_unlock_descs()
193  *    pair.
194  *  - It is safe to remove a retrieved MSI descriptor in the loop.
195  */
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)))
199
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))                               \
211                 if ((desc)->irq)                                        \
212                         for (__irq = (desc)->irq;                       \
213                              __irq < ((desc)->irq + (desc)->nvec_used); \
214                              __irq++)
215
216 #ifdef CONFIG_IRQ_MSI_IOMMU
217 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
218 {
219         return desc->iommu_cookie;
220 }
221
222 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
223                                              const void *iommu_cookie)
224 {
225         desc->iommu_cookie = iommu_cookie;
226 }
227 #else
228 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
229 {
230         return NULL;
231 }
232
233 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
234                                              const void *iommu_cookie)
235 {
236 }
237 #endif
238
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)
243
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)
248 {
249 }
250 #endif /* CONFIG_PCI_MSI */
251
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);
255
256 /**
257  * msi_free_msi_descs - Free MSI descriptors of a device
258  * @dev:        Device to free the descriptors
259  */
260 static inline void msi_free_msi_descs(struct device *dev)
261 {
262         msi_free_msi_descs_range(dev, MSI_DESC_ALL, 0, MSI_MAX_INDEX);
263 }
264
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);
270
271 void pci_msi_mask_irq(struct irq_data *data);
272 void pci_msi_unmask_irq(struct irq_data *data);
273
274 #ifdef CONFIG_SYSFS
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 */
281
282 /*
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.
286  *
287  * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
288  * stubs with warnings.
289  */
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 */
296
297 /*
298  * The restore hook is still available even for fully irq domain based
299  * setups. Courtesy to XEN/X86.
300  */
301 bool arch_restore_msi_irqs(struct pci_dev *dev);
302
303 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
304
305 #include <linux/irqhandler.h>
306
307 struct irq_domain;
308 struct irq_domain_ops;
309 struct irq_chip;
310 struct device_node;
311 struct fwnode_handle;
312 struct msi_domain_info;
313
314 /**
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
323  *                      function.
324  * @domain_free_irqs:   Optional function to override the default free
325  *                      function.
326  *
327  * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying
328  * irqdomain.
329  *
330  * @msi_check, @msi_prepare and @set_desc are callbacks used by
331  * msi_domain_alloc/free_irqs().
332  *
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.
339  *
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.
344  *
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().
348  */
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,
358                                     unsigned int virq);
359         int             (*msi_check)(struct irq_domain *domain,
360                                      struct msi_domain_info *info,
361                                      struct device *dev);
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,
370                                             struct device *dev);
371 };
372
373 /**
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
383  */
384 struct msi_domain_info {
385         u32                     flags;
386         struct msi_domain_ops   *ops;
387         struct irq_chip         *chip;
388         void                    *chip_data;
389         irq_flow_handler_t      handler;
390         void                    *handler_data;
391         const char              *handler_name;
392         void                    *data;
393 };
394
395 /* Flags for msi_domain_info */
396 enum {
397         /*
398          * Init non implemented ops callbacks with default MSI domain
399          * callbacks.
400          */
401         MSI_FLAG_USE_DEF_DOM_OPS        = (1 << 0),
402         /*
403          * Init non implemented chip callbacks with default MSI chip
404          * callbacks.
405          */
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),
413         /*
414          * Must reactivate when irq is started even when
415          * MSI_FLAG_ACTIVATE_EARLY has been set.
416          */
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),
428 };
429
430 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
431                             bool force);
432
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,
437                             int nvec);
438 int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev,
439                                        int nvec);
440 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
441                           int nvec);
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);
446
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);
453
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);
459 struct irq_domain *
460 __platform_msi_create_device_domain(struct device *dev,
461                                     unsigned int nvec,
462                                     bool is_tree,
463                                     irq_write_msi_msg_t write_msi_msg,
464                                     const struct irq_domain_ops *ops,
465                                     void *host_data);
466
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)
471
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,
475                                      unsigned int nvec);
476 void *platform_msi_get_host_data(struct irq_domain *domain);
477 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
478
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);
486 #else
487 static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
488 {
489         return NULL;
490 }
491 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
492
493 #endif /* LINUX_MSI_H */