PCI/MSI: Provide MSI_FLAG_MSIX_CONTIGUOUS
[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/list.h>
7 #include <asm/msi.h>
8
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 {
12         u32     address_lo;
13 } __attribute__ ((packed)) arch_msi_msg_addr_lo_t;
14 #endif
15
16 #ifndef arch_msi_msg_addr_hi
17 typedef struct arch_msi_msg_addr_hi {
18         u32     address_hi;
19 } __attribute__ ((packed)) arch_msi_msg_addr_hi_t;
20 #endif
21
22 #ifndef arch_msi_msg_data
23 typedef struct arch_msi_msg_data {
24         u32     data;
25 } __attribute__ ((packed)) arch_msi_msg_data_t;
26 #endif
27
28 /**
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
37  */
38 struct msi_msg {
39         union {
40                 u32                     address_lo;
41                 arch_msi_msg_addr_lo_t  arch_addr_lo;
42         };
43         union {
44                 u32                     address_hi;
45                 arch_msi_msg_addr_hi_t  arch_addr_hi;
46         };
47         union {
48                 u32                     data;
49                 arch_msi_msg_data_t     arch_data;
50         };
51 };
52
53 extern int pci_msi_ignore_mask;
54 /* Helper functions */
55 struct irq_data;
56 struct msi_desc;
57 struct pci_dev;
58 struct platform_msi_priv_data;
59 struct attribute_group;
60
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);
64 #else
65 static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
66 {
67 }
68 #endif
69
70 typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc,
71                                     struct msi_msg *msg);
72
73 /**
74  * pci_msi_desc - PCI/MSI specific MSI descriptor data
75  *
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  * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq
84  * @mask_pos:   [PCI MSI]   Mask register position
85  * @mask_base:  [PCI MSI-X] Mask register base address
86  */
87 struct pci_msi_desc {
88         union {
89                 u32 msi_mask;
90                 u32 msix_ctrl;
91         };
92         struct {
93                 u8      is_msix         : 1;
94                 u8      multiple        : 3;
95                 u8      multi_cap       : 3;
96                 u8      can_mask        : 1;
97                 u8      is_64           : 1;
98                 u8      is_virtual      : 1;
99                 unsigned default_irq;
100         } msi_attrib;
101         union {
102                 u8      mask_pos;
103                 void __iomem *mask_base;
104         };
105 };
106
107 /**
108  * struct msi_desc - Descriptor structure for MSI based interrupts
109  * @list:       List head for management
110  * @irq:        The base interrupt number
111  * @nvec_used:  The number of vectors used
112  * @dev:        Pointer to the device which uses this descriptor
113  * @msg:        The last set MSI message cached for reuse
114  * @affinity:   Optional pointer to a cpu affinity mask for this descriptor
115  *
116  * @write_msi_msg:      Callback that may be called when the MSI message
117  *                      address or data changes
118  * @write_msi_msg_data: Data parameter for the callback.
119  *
120  * @msi_index:  Index of the msi descriptor
121  * @pci:        PCI specific msi descriptor data
122  */
123 struct msi_desc {
124         /* Shared device/bus type independent data */
125         struct list_head                list;
126         unsigned int                    irq;
127         unsigned int                    nvec_used;
128         struct device                   *dev;
129         struct msi_msg                  msg;
130         struct irq_affinity_desc        *affinity;
131 #ifdef CONFIG_IRQ_MSI_IOMMU
132         const void                      *iommu_cookie;
133 #endif
134
135         void (*write_msi_msg)(struct msi_desc *entry, void *data);
136         void *write_msi_msg_data;
137
138         u16                             msi_index;
139         struct pci_msi_desc             pci;
140 };
141
142 /**
143  * msi_device_data - MSI per device data
144  * @properties:         MSI properties which are interesting to drivers
145  * @attrs:              Pointer to the sysfs attribute group
146  * @platform_data:      Platform-MSI specific data
147  */
148 struct msi_device_data {
149         unsigned long                   properties;
150         const struct attribute_group    **attrs;
151         struct platform_msi_priv_data   *platform_data;
152 };
153
154 int msi_setup_device_data(struct device *dev);
155
156 /* Helpers to hide struct msi_desc implementation details */
157 #define msi_desc_to_dev(desc)           ((desc)->dev)
158 #define dev_to_msi_list(dev)            (&(dev)->msi_list)
159 #define first_msi_entry(dev)            \
160         list_first_entry(dev_to_msi_list((dev)), struct msi_desc, list)
161 #define for_each_msi_entry(desc, dev)   \
162         list_for_each_entry((desc), dev_to_msi_list((dev)), list)
163 #define for_each_msi_entry_safe(desc, tmp, dev) \
164         list_for_each_entry_safe((desc), (tmp), dev_to_msi_list((dev)), list)
165 #define for_each_msi_vector(desc, __irq, dev)                           \
166         for_each_msi_entry((desc), (dev))                               \
167                 if ((desc)->irq)                                        \
168                         for (__irq = (desc)->irq;                       \
169                              __irq < ((desc)->irq + (desc)->nvec_used); \
170                              __irq++)
171
172 #ifdef CONFIG_IRQ_MSI_IOMMU
173 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
174 {
175         return desc->iommu_cookie;
176 }
177
178 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
179                                              const void *iommu_cookie)
180 {
181         desc->iommu_cookie = iommu_cookie;
182 }
183 #else
184 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc)
185 {
186         return NULL;
187 }
188
189 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc,
190                                              const void *iommu_cookie)
191 {
192 }
193 #endif
194
195 #ifdef CONFIG_PCI_MSI
196 #define first_pci_msi_entry(pdev)       first_msi_entry(&(pdev)->dev)
197 #define for_each_pci_msi_entry(desc, pdev)      \
198         for_each_msi_entry((desc), &(pdev)->dev)
199
200 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc);
201 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg);
202 #else /* CONFIG_PCI_MSI */
203 static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
204 {
205 }
206 #endif /* CONFIG_PCI_MSI */
207
208 struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
209                                  const struct irq_affinity_desc *affinity);
210 void free_msi_entry(struct msi_desc *entry);
211 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
212 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
213
214 void pci_msi_mask_irq(struct irq_data *data);
215 void pci_msi_unmask_irq(struct irq_data *data);
216
217 #ifdef CONFIG_SYSFS
218 int msi_device_populate_sysfs(struct device *dev);
219 void msi_device_destroy_sysfs(struct device *dev);
220 #else /* CONFIG_SYSFS */
221 static inline int msi_device_populate_sysfs(struct device *dev) { return 0; }
222 static inline void msi_device_destroy_sysfs(struct device *dev) { }
223 #endif /* !CONFIG_SYSFS */
224
225 /*
226  * The arch hooks to setup up msi irqs. Default functions are implemented
227  * as weak symbols so that they /can/ be overriden by architecture specific
228  * code if needed. These hooks can only be enabled by the architecture.
229  *
230  * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by
231  * stubs with warnings.
232  */
233 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
234 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc);
235 void arch_teardown_msi_irq(unsigned int irq);
236 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type);
237 void arch_teardown_msi_irqs(struct pci_dev *dev);
238 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
239
240 /*
241  * The restore hook is still available even for fully irq domain based
242  * setups. Courtesy to XEN/X86.
243  */
244 bool arch_restore_msi_irqs(struct pci_dev *dev);
245
246 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
247
248 #include <linux/irqhandler.h>
249
250 struct irq_domain;
251 struct irq_domain_ops;
252 struct irq_chip;
253 struct device_node;
254 struct fwnode_handle;
255 struct msi_domain_info;
256
257 /**
258  * struct msi_domain_ops - MSI interrupt domain callbacks
259  * @get_hwirq:          Retrieve the resulting hw irq number
260  * @msi_init:           Domain specific init function for MSI interrupts
261  * @msi_free:           Domain specific function to free a MSI interrupts
262  * @msi_check:          Callback for verification of the domain/info/dev data
263  * @msi_prepare:        Prepare the allocation of the interrupts in the domain
264  * @set_desc:           Set the msi descriptor for an interrupt
265  * @domain_alloc_irqs:  Optional function to override the default allocation
266  *                      function.
267  * @domain_free_irqs:   Optional function to override the default free
268  *                      function.
269  *
270  * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying
271  * irqdomain.
272  *
273  * @msi_check, @msi_prepare and @set_desc are callbacks used by
274  * msi_domain_alloc/free_irqs().
275  *
276  * @domain_alloc_irqs, @domain_free_irqs can be used to override the
277  * default allocation/free functions (__msi_domain_alloc/free_irqs). This
278  * is initially for a wrapper around XENs seperate MSI universe which can't
279  * be wrapped into the regular irq domains concepts by mere mortals.  This
280  * allows to universally use msi_domain_alloc/free_irqs without having to
281  * special case XEN all over the place.
282  *
283  * Contrary to other operations @domain_alloc_irqs and @domain_free_irqs
284  * are set to the default implementation if NULL and even when
285  * MSI_FLAG_USE_DEF_DOM_OPS is not set to avoid breaking existing users and
286  * because these callbacks are obviously mandatory.
287  *
288  * This is NOT meant to be abused, but it can be useful to build wrappers
289  * for specialized MSI irq domains which need extra work before and after
290  * calling __msi_domain_alloc_irqs()/__msi_domain_free_irqs().
291  */
292 struct msi_domain_ops {
293         irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info,
294                                      msi_alloc_info_t *arg);
295         int             (*msi_init)(struct irq_domain *domain,
296                                     struct msi_domain_info *info,
297                                     unsigned int virq, irq_hw_number_t hwirq,
298                                     msi_alloc_info_t *arg);
299         void            (*msi_free)(struct irq_domain *domain,
300                                     struct msi_domain_info *info,
301                                     unsigned int virq);
302         int             (*msi_check)(struct irq_domain *domain,
303                                      struct msi_domain_info *info,
304                                      struct device *dev);
305         int             (*msi_prepare)(struct irq_domain *domain,
306                                        struct device *dev, int nvec,
307                                        msi_alloc_info_t *arg);
308         void            (*set_desc)(msi_alloc_info_t *arg,
309                                     struct msi_desc *desc);
310         int             (*domain_alloc_irqs)(struct irq_domain *domain,
311                                              struct device *dev, int nvec);
312         void            (*domain_free_irqs)(struct irq_domain *domain,
313                                             struct device *dev);
314 };
315
316 /**
317  * struct msi_domain_info - MSI interrupt domain data
318  * @flags:              Flags to decribe features and capabilities
319  * @ops:                The callback data structure
320  * @chip:               Optional: associated interrupt chip
321  * @chip_data:          Optional: associated interrupt chip data
322  * @handler:            Optional: associated interrupt flow handler
323  * @handler_data:       Optional: associated interrupt flow handler data
324  * @handler_name:       Optional: associated interrupt flow handler name
325  * @data:               Optional: domain specific data
326  */
327 struct msi_domain_info {
328         u32                     flags;
329         struct msi_domain_ops   *ops;
330         struct irq_chip         *chip;
331         void                    *chip_data;
332         irq_flow_handler_t      handler;
333         void                    *handler_data;
334         const char              *handler_name;
335         void                    *data;
336 };
337
338 /* Flags for msi_domain_info */
339 enum {
340         /*
341          * Init non implemented ops callbacks with default MSI domain
342          * callbacks.
343          */
344         MSI_FLAG_USE_DEF_DOM_OPS        = (1 << 0),
345         /*
346          * Init non implemented chip callbacks with default MSI chip
347          * callbacks.
348          */
349         MSI_FLAG_USE_DEF_CHIP_OPS       = (1 << 1),
350         /* Support multiple PCI MSI interrupts */
351         MSI_FLAG_MULTI_PCI_MSI          = (1 << 2),
352         /* Support PCI MSIX interrupts */
353         MSI_FLAG_PCI_MSIX               = (1 << 3),
354         /* Needs early activate, required for PCI */
355         MSI_FLAG_ACTIVATE_EARLY         = (1 << 4),
356         /*
357          * Must reactivate when irq is started even when
358          * MSI_FLAG_ACTIVATE_EARLY has been set.
359          */
360         MSI_FLAG_MUST_REACTIVATE        = (1 << 5),
361         /* Is level-triggered capable, using two messages */
362         MSI_FLAG_LEVEL_CAPABLE          = (1 << 6),
363         /* Populate sysfs on alloc() and destroy it on free() */
364         MSI_FLAG_DEV_SYSFS              = (1 << 7),
365         /* MSI-X entries must be contiguous */
366         MSI_FLAG_MSIX_CONTIGUOUS        = (1 << 8),
367 };
368
369 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask,
370                             bool force);
371
372 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
373                                          struct msi_domain_info *info,
374                                          struct irq_domain *parent);
375 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
376                             int nvec);
377 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
378                           int nvec);
379 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
380 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev);
381 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain);
382
383 struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
384                                                   struct msi_domain_info *info,
385                                                   struct irq_domain *parent);
386 int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
387                                    irq_write_msi_msg_t write_msi_msg);
388 void platform_msi_domain_free_irqs(struct device *dev);
389
390 /* When an MSI domain is used as an intermediate domain */
391 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
392                             int nvec, msi_alloc_info_t *args);
393 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
394                              int virq, int nvec, msi_alloc_info_t *args);
395 struct irq_domain *
396 __platform_msi_create_device_domain(struct device *dev,
397                                     unsigned int nvec,
398                                     bool is_tree,
399                                     irq_write_msi_msg_t write_msi_msg,
400                                     const struct irq_domain_ops *ops,
401                                     void *host_data);
402
403 #define platform_msi_create_device_domain(dev, nvec, write, ops, data)  \
404         __platform_msi_create_device_domain(dev, nvec, false, write, ops, data)
405 #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \
406         __platform_msi_create_device_domain(dev, nvec, true, write, ops, data)
407
408 int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq,
409                                      unsigned int nr_irqs);
410 void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq,
411                                      unsigned int nvec);
412 void *platform_msi_get_host_data(struct irq_domain *domain);
413 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
414
415 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
416 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
417                                              struct msi_domain_info *info,
418                                              struct irq_domain *parent);
419 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev);
420 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev);
421 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev);
422 #else
423 static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
424 {
425         return NULL;
426 }
427 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
428
429 #endif /* LINUX_MSI_H */