1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef LINUX_MSI_H 3 #define LINUX_MSI_H 4 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 16 * abuse. The only function which is relevant for drivers is msi_get_virq(). 17 */ 18 19 #include <linux/irqdomain_defs.h> 20 #include <linux/cpumask.h> 21 #include <linux/xarray.h> 22 #include <linux/mutex.h> 23 #include <linux/list.h> 24 #include <asm/msi.h> 25 26 /* Dummy shadow structures if an architecture does not define them */ 27 #ifndef arch_msi_msg_addr_lo 28 typedef struct arch_msi_msg_addr_lo { 29 u32 address_lo; 30 } __attribute__ ((packed)) arch_msi_msg_addr_lo_t; 31 #endif 32 33 #ifndef arch_msi_msg_addr_hi 34 typedef struct arch_msi_msg_addr_hi { 35 u32 address_hi; 36 } __attribute__ ((packed)) arch_msi_msg_addr_hi_t; 37 #endif 38 39 #ifndef arch_msi_msg_data 40 typedef struct arch_msi_msg_data { 41 u32 data; 42 } __attribute__ ((packed)) arch_msi_msg_data_t; 43 #endif 44 45 /** 46 * msi_msg - Representation of a MSI message 47 * @address_lo: Low 32 bits of msi message address 48 * @arch_addrlo: Architecture specific shadow of @address_lo 49 * @address_hi: High 32 bits of msi message address 50 * (only used when device supports it) 51 * @arch_addrhi: Architecture specific shadow of @address_hi 52 * @data: MSI message data (usually 16 bits) 53 * @arch_data: Architecture specific shadow of @data 54 */ 55 struct msi_msg { 56 union { 57 u32 address_lo; 58 arch_msi_msg_addr_lo_t arch_addr_lo; 59 }; 60 union { 61 u32 address_hi; 62 arch_msi_msg_addr_hi_t arch_addr_hi; 63 }; 64 union { 65 u32 data; 66 arch_msi_msg_data_t arch_data; 67 }; 68 }; 69 70 extern int pci_msi_ignore_mask; 71 /* Helper functions */ 72 struct irq_data; 73 struct msi_desc; 74 struct pci_dev; 75 struct platform_msi_priv_data; 76 struct device_attribute; 77 78 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg); 79 #ifdef CONFIG_GENERIC_MSI_IRQ 80 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg); 81 #else 82 static inline void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg) 83 { 84 } 85 #endif 86 87 typedef void (*irq_write_msi_msg_t)(struct msi_desc *desc, 88 struct msi_msg *msg); 89 90 /** 91 * pci_msi_desc - PCI/MSI specific MSI descriptor data 92 * 93 * @msi_mask: [PCI MSI] MSI cached mask bits 94 * @msix_ctrl: [PCI MSI-X] MSI-X cached per vector control bits 95 * @is_msix: [PCI MSI/X] True if MSI-X 96 * @multiple: [PCI MSI/X] log2 num of messages allocated 97 * @multi_cap: [PCI MSI/X] log2 num of messages supported 98 * @can_mask: [PCI MSI/X] Masking supported? 99 * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit 100 * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq 101 * @mask_pos: [PCI MSI] Mask register position 102 * @mask_base: [PCI MSI-X] Mask register base address 103 */ 104 struct pci_msi_desc { 105 union { 106 u32 msi_mask; 107 u32 msix_ctrl; 108 }; 109 struct { 110 u8 is_msix : 1; 111 u8 multiple : 3; 112 u8 multi_cap : 3; 113 u8 can_mask : 1; 114 u8 is_64 : 1; 115 u8 is_virtual : 1; 116 unsigned default_irq; 117 } msi_attrib; 118 union { 119 u8 mask_pos; 120 void __iomem *mask_base; 121 }; 122 }; 123 124 #define MSI_MAX_INDEX ((unsigned int)USHRT_MAX) 125 126 /** 127 * struct msi_desc - Descriptor structure for MSI based interrupts 128 * @irq: The base interrupt number 129 * @nvec_used: The number of vectors used 130 * @dev: Pointer to the device which uses this descriptor 131 * @msg: The last set MSI message cached for reuse 132 * @affinity: Optional pointer to a cpu affinity mask for this descriptor 133 * @sysfs_attr: Pointer to sysfs device attribute 134 * 135 * @write_msi_msg: Callback that may be called when the MSI message 136 * address or data changes 137 * @write_msi_msg_data: Data parameter for the callback. 138 * 139 * @msi_index: Index of the msi descriptor 140 * @pci: PCI specific msi descriptor data 141 */ 142 struct msi_desc { 143 /* Shared device/bus type independent data */ 144 unsigned int irq; 145 unsigned int nvec_used; 146 struct device *dev; 147 struct msi_msg msg; 148 struct irq_affinity_desc *affinity; 149 #ifdef CONFIG_IRQ_MSI_IOMMU 150 const void *iommu_cookie; 151 #endif 152 #ifdef CONFIG_SYSFS 153 struct device_attribute *sysfs_attrs; 154 #endif 155 156 void (*write_msi_msg)(struct msi_desc *entry, void *data); 157 void *write_msi_msg_data; 158 159 u16 msi_index; 160 struct pci_msi_desc pci; 161 }; 162 163 /* 164 * Filter values for the MSI descriptor iterators and accessor functions. 165 */ 166 enum msi_desc_filter { 167 /* All descriptors */ 168 MSI_DESC_ALL, 169 /* Descriptors which have no interrupt associated */ 170 MSI_DESC_NOTASSOCIATED, 171 /* Descriptors which have an interrupt associated */ 172 MSI_DESC_ASSOCIATED, 173 }; 174 175 /** 176 * msi_device_data - MSI per device data 177 * @properties: MSI properties which are interesting to drivers 178 * @platform_data: Platform-MSI specific data 179 * @mutex: Mutex protecting the MSI descriptor store 180 * @__store: Xarray for storing MSI descriptor pointers 181 * @__iter_idx: Index to search the next entry for iterators 182 */ 183 struct msi_device_data { 184 unsigned long properties; 185 struct platform_msi_priv_data *platform_data; 186 struct mutex mutex; 187 struct xarray __store; 188 unsigned long __iter_idx; 189 }; 190 191 int msi_setup_device_data(struct device *dev); 192 193 unsigned int msi_get_virq(struct device *dev, unsigned int index); 194 void msi_lock_descs(struct device *dev); 195 void msi_unlock_descs(struct device *dev); 196 197 struct msi_desc *msi_first_desc(struct device *dev, enum msi_desc_filter filter); 198 struct msi_desc *msi_next_desc(struct device *dev, enum msi_desc_filter filter); 199 200 /** 201 * msi_for_each_desc - Iterate the MSI descriptors 202 * 203 * @desc: struct msi_desc pointer used as iterator 204 * @dev: struct device pointer - device to iterate 205 * @filter: Filter for descriptor selection 206 * 207 * Notes: 208 * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs() 209 * pair. 210 * - It is safe to remove a retrieved MSI descriptor in the loop. 211 */ 212 #define msi_for_each_desc(desc, dev, filter) \ 213 for ((desc) = msi_first_desc((dev), (filter)); (desc); \ 214 (desc) = msi_next_desc((dev), (filter))) 215 216 #define msi_desc_to_dev(desc) ((desc)->dev) 217 218 #ifdef CONFIG_IRQ_MSI_IOMMU 219 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc) 220 { 221 return desc->iommu_cookie; 222 } 223 224 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc, 225 const void *iommu_cookie) 226 { 227 desc->iommu_cookie = iommu_cookie; 228 } 229 #else 230 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc) 231 { 232 return NULL; 233 } 234 235 static inline void msi_desc_set_iommu_cookie(struct msi_desc *desc, 236 const void *iommu_cookie) 237 { 238 } 239 #endif 240 241 int msi_add_msi_desc(struct device *dev, struct msi_desc *init_desc); 242 void msi_free_msi_descs_range(struct device *dev, unsigned int first_index, unsigned int last_index); 243 244 /** 245 * msi_free_msi_descs - Free MSI descriptors of a device 246 * @dev: Device to free the descriptors 247 */ 248 static inline void msi_free_msi_descs(struct device *dev) 249 { 250 msi_free_msi_descs_range(dev, 0, MSI_MAX_INDEX); 251 } 252 253 /* 254 * The arch hooks to setup up msi irqs. Default functions are implemented 255 * as weak symbols so that they /can/ be overriden by architecture specific 256 * code if needed. These hooks can only be enabled by the architecture. 257 * 258 * If CONFIG_PCI_MSI_ARCH_FALLBACKS is not selected they are replaced by 259 * stubs with warnings. 260 */ 261 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS 262 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc); 263 void arch_teardown_msi_irq(unsigned int irq); 264 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type); 265 void arch_teardown_msi_irqs(struct pci_dev *dev); 266 #ifdef CONFIG_SYSFS 267 int msi_device_populate_sysfs(struct device *dev); 268 void msi_device_destroy_sysfs(struct device *dev); 269 #else /* CONFIG_SYSFS */ 270 static inline int msi_device_populate_sysfs(struct device *dev) { return 0; } 271 static inline void msi_device_destroy_sysfs(struct device *dev) { } 272 #endif /* !CONFIG_SYSFS */ 273 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */ 274 275 /* 276 * The restore hook is still available even for fully irq domain based 277 * setups. Courtesy to XEN/X86. 278 */ 279 bool arch_restore_msi_irqs(struct pci_dev *dev); 280 281 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 282 283 #include <linux/irqhandler.h> 284 285 struct irq_domain; 286 struct irq_domain_ops; 287 struct irq_chip; 288 struct device_node; 289 struct fwnode_handle; 290 struct msi_domain_info; 291 292 /** 293 * struct msi_domain_ops - MSI interrupt domain callbacks 294 * @get_hwirq: Retrieve the resulting hw irq number 295 * @msi_init: Domain specific init function for MSI interrupts 296 * @msi_free: Domain specific function to free a MSI interrupts 297 * @msi_check: Callback for verification of the domain/info/dev data 298 * @msi_prepare: Prepare the allocation of the interrupts in the domain 299 * @set_desc: Set the msi descriptor for an interrupt 300 * @domain_alloc_irqs: Optional function to override the default allocation 301 * function. 302 * @domain_free_irqs: Optional function to override the default free 303 * function. 304 * @msi_post_free: Optional function which is invoked after freeing 305 * all interrupts. 306 * 307 * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying 308 * irqdomain. 309 * 310 * @msi_check, @msi_prepare and @set_desc are callbacks used by 311 * msi_domain_alloc/free_irqs(). 312 * 313 * @domain_alloc_irqs, @domain_free_irqs can be used to override the 314 * default allocation/free functions (__msi_domain_alloc/free_irqs). This 315 * is initially for a wrapper around XENs seperate MSI universe which can't 316 * be wrapped into the regular irq domains concepts by mere mortals. This 317 * allows to universally use msi_domain_alloc/free_irqs without having to 318 * special case XEN all over the place. 319 * 320 * Contrary to other operations @domain_alloc_irqs and @domain_free_irqs 321 * are set to the default implementation if NULL and even when 322 * MSI_FLAG_USE_DEF_DOM_OPS is not set to avoid breaking existing users and 323 * because these callbacks are obviously mandatory. 324 */ 325 struct msi_domain_ops { 326 irq_hw_number_t (*get_hwirq)(struct msi_domain_info *info, 327 msi_alloc_info_t *arg); 328 int (*msi_init)(struct irq_domain *domain, 329 struct msi_domain_info *info, 330 unsigned int virq, irq_hw_number_t hwirq, 331 msi_alloc_info_t *arg); 332 void (*msi_free)(struct irq_domain *domain, 333 struct msi_domain_info *info, 334 unsigned int virq); 335 int (*msi_check)(struct irq_domain *domain, 336 struct msi_domain_info *info, 337 struct device *dev); 338 int (*msi_prepare)(struct irq_domain *domain, 339 struct device *dev, int nvec, 340 msi_alloc_info_t *arg); 341 void (*set_desc)(msi_alloc_info_t *arg, 342 struct msi_desc *desc); 343 int (*domain_alloc_irqs)(struct irq_domain *domain, 344 struct device *dev, int nvec); 345 void (*domain_free_irqs)(struct irq_domain *domain, 346 struct device *dev); 347 void (*msi_post_free)(struct irq_domain *domain, 348 struct device *dev); 349 }; 350 351 /** 352 * struct msi_domain_info - MSI interrupt domain data 353 * @flags: Flags to decribe features and capabilities 354 * @bus_token: The domain bus token 355 * @ops: The callback data structure 356 * @chip: Optional: associated interrupt chip 357 * @chip_data: Optional: associated interrupt chip data 358 * @handler: Optional: associated interrupt flow handler 359 * @handler_data: Optional: associated interrupt flow handler data 360 * @handler_name: Optional: associated interrupt flow handler name 361 * @data: Optional: domain specific data 362 */ 363 struct msi_domain_info { 364 u32 flags; 365 enum irq_domain_bus_token bus_token; 366 struct msi_domain_ops *ops; 367 struct irq_chip *chip; 368 void *chip_data; 369 irq_flow_handler_t handler; 370 void *handler_data; 371 const char *handler_name; 372 void *data; 373 }; 374 375 /* Flags for msi_domain_info */ 376 enum { 377 /* 378 * Init non implemented ops callbacks with default MSI domain 379 * callbacks. 380 */ 381 MSI_FLAG_USE_DEF_DOM_OPS = (1 << 0), 382 /* 383 * Init non implemented chip callbacks with default MSI chip 384 * callbacks. 385 */ 386 MSI_FLAG_USE_DEF_CHIP_OPS = (1 << 1), 387 /* Support multiple PCI MSI interrupts */ 388 MSI_FLAG_MULTI_PCI_MSI = (1 << 2), 389 /* Support PCI MSIX interrupts */ 390 MSI_FLAG_PCI_MSIX = (1 << 3), 391 /* Needs early activate, required for PCI */ 392 MSI_FLAG_ACTIVATE_EARLY = (1 << 4), 393 /* 394 * Must reactivate when irq is started even when 395 * MSI_FLAG_ACTIVATE_EARLY has been set. 396 */ 397 MSI_FLAG_MUST_REACTIVATE = (1 << 5), 398 /* Is level-triggered capable, using two messages */ 399 MSI_FLAG_LEVEL_CAPABLE = (1 << 6), 400 /* Populate sysfs on alloc() and destroy it on free() */ 401 MSI_FLAG_DEV_SYSFS = (1 << 7), 402 /* MSI-X entries must be contiguous */ 403 MSI_FLAG_MSIX_CONTIGUOUS = (1 << 8), 404 /* Allocate simple MSI descriptors */ 405 MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS = (1 << 9), 406 /* Free MSI descriptors */ 407 MSI_FLAG_FREE_MSI_DESCS = (1 << 10), 408 }; 409 410 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask, 411 bool force); 412 413 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode, 414 struct msi_domain_info *info, 415 struct irq_domain *parent); 416 int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev, 417 int nvec); 418 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, 419 int nvec); 420 void msi_domain_free_irqs_descs_locked(struct irq_domain *domain, struct device *dev); 421 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev); 422 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain); 423 424 struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode, 425 struct msi_domain_info *info, 426 struct irq_domain *parent); 427 int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec, 428 irq_write_msi_msg_t write_msi_msg); 429 void platform_msi_domain_free_irqs(struct device *dev); 430 431 /* When an MSI domain is used as an intermediate domain */ 432 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev, 433 int nvec, msi_alloc_info_t *args); 434 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev, 435 int virq, int nvec, msi_alloc_info_t *args); 436 struct irq_domain * 437 __platform_msi_create_device_domain(struct device *dev, 438 unsigned int nvec, 439 bool is_tree, 440 irq_write_msi_msg_t write_msi_msg, 441 const struct irq_domain_ops *ops, 442 void *host_data); 443 444 #define platform_msi_create_device_domain(dev, nvec, write, ops, data) \ 445 __platform_msi_create_device_domain(dev, nvec, false, write, ops, data) 446 #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \ 447 __platform_msi_create_device_domain(dev, nvec, true, write, ops, data) 448 449 int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq, 450 unsigned int nr_irqs); 451 void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq, 452 unsigned int nvec); 453 void *platform_msi_get_host_data(struct irq_domain *domain); 454 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */ 455 456 /* PCI specific interfaces */ 457 #ifdef CONFIG_PCI_MSI 458 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc); 459 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg); 460 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg); 461 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg); 462 void pci_msi_mask_irq(struct irq_data *data); 463 void pci_msi_unmask_irq(struct irq_data *data); 464 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode, 465 struct msi_domain_info *info, 466 struct irq_domain *parent); 467 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev); 468 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev); 469 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev); 470 #else /* CONFIG_PCI_MSI */ 471 static inline struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev) 472 { 473 return NULL; 474 } 475 static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg) { } 476 #endif /* !CONFIG_PCI_MSI */ 477 478 #endif /* LINUX_MSI_H */ 479