msi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support of MSI, HPET and DMAR interrupts.
  4. *
  5. * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
  6. * Moved from arch/x86/kernel/apic/io_apic.c.
  7. * Jiang Liu <jiang.liu@linux.intel.com>
  8. * Convert to hierarchical irqdomain
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/pci.h>
  14. #include <linux/dmar.h>
  15. #include <linux/hpet.h>
  16. #include <linux/msi.h>
  17. #include <asm/irqdomain.h>
  18. #include <asm/hpet.h>
  19. #include <asm/hw_irq.h>
  20. #include <asm/apic.h>
  21. #include <asm/irq_remapping.h>
  22. #include <asm/xen/hypervisor.h>
  23. struct irq_domain *x86_pci_msi_default_domain __ro_after_init;
  24. static void irq_msi_update_msg(struct irq_data *irqd, struct irq_cfg *cfg)
  25. {
  26. struct msi_msg msg[2] = { [1] = { }, };
  27. __irq_msi_compose_msg(cfg, msg, false);
  28. irq_data_get_irq_chip(irqd)->irq_write_msi_msg(irqd, msg);
  29. }
  30. static int
  31. msi_set_affinity(struct irq_data *irqd, const struct cpumask *mask, bool force)
  32. {
  33. struct irq_cfg old_cfg, *cfg = irqd_cfg(irqd);
  34. struct irq_data *parent = irqd->parent_data;
  35. unsigned int cpu;
  36. int ret;
  37. /* Save the current configuration */
  38. cpu = cpumask_first(irq_data_get_effective_affinity_mask(irqd));
  39. old_cfg = *cfg;
  40. /* Allocate a new target vector */
  41. ret = parent->chip->irq_set_affinity(parent, mask, force);
  42. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  43. return ret;
  44. /*
  45. * For non-maskable and non-remapped MSI interrupts the migration
  46. * to a different destination CPU and a different vector has to be
  47. * done careful to handle the possible stray interrupt which can be
  48. * caused by the non-atomic update of the address/data pair.
  49. *
  50. * Direct update is possible when:
  51. * - The MSI is maskable (remapped MSI does not use this code path).
  52. * The reservation mode bit is set in this case.
  53. * - The new vector is the same as the old vector
  54. * - The old vector is MANAGED_IRQ_SHUTDOWN_VECTOR (interrupt starts up)
  55. * - The interrupt is not yet started up
  56. * - The new destination CPU is the same as the old destination CPU
  57. */
  58. if (!irqd_can_reserve(irqd) ||
  59. cfg->vector == old_cfg.vector ||
  60. old_cfg.vector == MANAGED_IRQ_SHUTDOWN_VECTOR ||
  61. !irqd_is_started(irqd) ||
  62. cfg->dest_apicid == old_cfg.dest_apicid) {
  63. irq_msi_update_msg(irqd, cfg);
  64. return ret;
  65. }
  66. /*
  67. * Paranoia: Validate that the interrupt target is the local
  68. * CPU.
  69. */
  70. if (WARN_ON_ONCE(cpu != smp_processor_id())) {
  71. irq_msi_update_msg(irqd, cfg);
  72. return ret;
  73. }
  74. /*
  75. * Redirect the interrupt to the new vector on the current CPU
  76. * first. This might cause a spurious interrupt on this vector if
  77. * the device raises an interrupt right between this update and the
  78. * update to the final destination CPU.
  79. *
  80. * If the vector is in use then the installed device handler will
  81. * denote it as spurious which is no harm as this is a rare event
  82. * and interrupt handlers have to cope with spurious interrupts
  83. * anyway. If the vector is unused, then it is marked so it won't
  84. * trigger the 'No irq handler for vector' warning in
  85. * common_interrupt().
  86. *
  87. * This requires to hold vector lock to prevent concurrent updates to
  88. * the affected vector.
  89. */
  90. lock_vector_lock();
  91. /*
  92. * Mark the new target vector on the local CPU if it is currently
  93. * unused. Reuse the VECTOR_RETRIGGERED state which is also used in
  94. * the CPU hotplug path for a similar purpose. This cannot be
  95. * undone here as the current CPU has interrupts disabled and
  96. * cannot handle the interrupt before the whole set_affinity()
  97. * section is done. In the CPU unplug case, the current CPU is
  98. * about to vanish and will not handle any interrupts anymore. The
  99. * vector is cleaned up when the CPU comes online again.
  100. */
  101. if (IS_ERR_OR_NULL(this_cpu_read(vector_irq[cfg->vector])))
  102. this_cpu_write(vector_irq[cfg->vector], VECTOR_RETRIGGERED);
  103. /* Redirect it to the new vector on the local CPU temporarily */
  104. old_cfg.vector = cfg->vector;
  105. irq_msi_update_msg(irqd, &old_cfg);
  106. /* Now transition it to the target CPU */
  107. irq_msi_update_msg(irqd, cfg);
  108. /*
  109. * All interrupts after this point are now targeted at the new
  110. * vector/CPU.
  111. *
  112. * Drop vector lock before testing whether the temporary assignment
  113. * to the local CPU was hit by an interrupt raised in the device,
  114. * because the retrigger function acquires vector lock again.
  115. */
  116. unlock_vector_lock();
  117. /*
  118. * Check whether the transition raced with a device interrupt and
  119. * is pending in the local APICs IRR. It is safe to do this outside
  120. * of vector lock as the irq_desc::lock of this interrupt is still
  121. * held and interrupts are disabled: The check is not accessing the
  122. * underlying vector store. It's just checking the local APIC's
  123. * IRR.
  124. */
  125. if (lapic_vector_set_in_irr(cfg->vector))
  126. irq_data_get_irq_chip(irqd)->irq_retrigger(irqd);
  127. return ret;
  128. }
  129. /**
  130. * pci_dev_has_default_msi_parent_domain - Check whether the device has the default
  131. * MSI parent domain associated
  132. * @dev: Pointer to the PCI device
  133. */
  134. bool pci_dev_has_default_msi_parent_domain(struct pci_dev *dev)
  135. {
  136. struct irq_domain *domain = dev_get_msi_domain(&dev->dev);
  137. if (!domain)
  138. domain = dev_get_msi_domain(&dev->bus->dev);
  139. if (!domain)
  140. return false;
  141. return domain == x86_vector_domain;
  142. }
  143. /**
  144. * x86_msi_prepare - Setup of msi_alloc_info_t for allocations
  145. * @domain: The domain for which this setup happens
  146. * @dev: The device for which interrupts are allocated
  147. * @nvec: The number of vectors to allocate
  148. * @alloc: The allocation info structure to initialize
  149. *
  150. * This function is to be used for all types of MSI domains above the x86
  151. * vector domain and any intermediates. It is always invoked from the
  152. * top level interrupt domain. The domain specific allocation
  153. * functionality is determined via the @domain's bus token which allows to
  154. * map the X86 specific allocation type.
  155. */
  156. static int x86_msi_prepare(struct irq_domain *domain, struct device *dev,
  157. int nvec, msi_alloc_info_t *alloc)
  158. {
  159. struct msi_domain_info *info = domain->host_data;
  160. init_irq_alloc_info(alloc, NULL);
  161. switch (info->bus_token) {
  162. case DOMAIN_BUS_PCI_DEVICE_MSI:
  163. alloc->type = X86_IRQ_ALLOC_TYPE_PCI_MSI;
  164. return 0;
  165. case DOMAIN_BUS_PCI_DEVICE_MSIX:
  166. alloc->type = X86_IRQ_ALLOC_TYPE_PCI_MSIX;
  167. return 0;
  168. default:
  169. return -EINVAL;
  170. }
  171. }
  172. /**
  173. * x86_init_dev_msi_info - Domain info setup for MSI domains
  174. * @dev: The device for which the domain should be created
  175. * @domain: The (root) domain providing this callback
  176. * @real_parent: The real parent domain of the to initialize domain
  177. * @info: The domain info for the to initialize domain
  178. *
  179. * This function is to be used for all types of MSI domains above the x86
  180. * vector domain and any intermediates. The domain specific functionality
  181. * is determined via the @real_parent.
  182. */
  183. static bool x86_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
  184. struct irq_domain *real_parent, struct msi_domain_info *info)
  185. {
  186. const struct msi_parent_ops *pops = real_parent->msi_parent_ops;
  187. /* MSI parent domain specific settings */
  188. switch (real_parent->bus_token) {
  189. case DOMAIN_BUS_ANY:
  190. /* Only the vector domain can have the ANY token */
  191. if (WARN_ON_ONCE(domain != real_parent))
  192. return false;
  193. info->chip->irq_set_affinity = msi_set_affinity;
  194. break;
  195. case DOMAIN_BUS_DMAR:
  196. case DOMAIN_BUS_AMDVI:
  197. break;
  198. default:
  199. WARN_ON_ONCE(1);
  200. return false;
  201. }
  202. /* Is the target supported? */
  203. switch(info->bus_token) {
  204. case DOMAIN_BUS_PCI_DEVICE_MSI:
  205. case DOMAIN_BUS_PCI_DEVICE_MSIX:
  206. break;
  207. default:
  208. WARN_ON_ONCE(1);
  209. return false;
  210. }
  211. /*
  212. * Mask out the domain specific MSI feature flags which are not
  213. * supported by the real parent.
  214. */
  215. info->flags &= pops->supported_flags;
  216. /* Enforce the required flags */
  217. info->flags |= X86_VECTOR_MSI_FLAGS_REQUIRED;
  218. /* This is always invoked from the top level MSI domain! */
  219. info->ops->msi_prepare = x86_msi_prepare;
  220. info->chip->irq_ack = irq_chip_ack_parent;
  221. info->chip->irq_retrigger = irq_chip_retrigger_hierarchy;
  222. info->chip->flags |= IRQCHIP_SKIP_SET_WAKE |
  223. IRQCHIP_AFFINITY_PRE_STARTUP;
  224. info->handler = handle_edge_irq;
  225. info->handler_name = "edge";
  226. return true;
  227. }
  228. static const struct msi_parent_ops x86_vector_msi_parent_ops = {
  229. .supported_flags = X86_VECTOR_MSI_FLAGS_SUPPORTED,
  230. .init_dev_msi_info = x86_init_dev_msi_info,
  231. };
  232. struct irq_domain * __init native_create_pci_msi_domain(void)
  233. {
  234. if (apic_is_disabled)
  235. return NULL;
  236. x86_vector_domain->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT;
  237. x86_vector_domain->msi_parent_ops = &x86_vector_msi_parent_ops;
  238. return x86_vector_domain;
  239. }
  240. void __init x86_create_pci_msi_domain(void)
  241. {
  242. x86_pci_msi_default_domain = x86_init.irqs.create_pci_msi_domain();
  243. }
  244. /* Keep around for hyperV */
  245. int pci_msi_prepare(struct irq_domain *domain, struct device *dev, int nvec,
  246. msi_alloc_info_t *arg)
  247. {
  248. init_irq_alloc_info(arg, NULL);
  249. if (to_pci_dev(dev)->msix_enabled)
  250. arg->type = X86_IRQ_ALLOC_TYPE_PCI_MSIX;
  251. else
  252. arg->type = X86_IRQ_ALLOC_TYPE_PCI_MSI;
  253. return 0;
  254. }
  255. EXPORT_SYMBOL_GPL(pci_msi_prepare);
  256. #ifdef CONFIG_DMAR_TABLE
  257. /*
  258. * The Intel IOMMU (ab)uses the high bits of the MSI address to contain the
  259. * high bits of the destination APIC ID. This can't be done in the general
  260. * case for MSIs as it would be targeting real memory above 4GiB not the
  261. * APIC.
  262. */
  263. static void dmar_msi_compose_msg(struct irq_data *data, struct msi_msg *msg)
  264. {
  265. __irq_msi_compose_msg(irqd_cfg(data), msg, true);
  266. }
  267. static void dmar_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  268. {
  269. dmar_msi_write(data->irq, msg);
  270. }
  271. static struct irq_chip dmar_msi_controller = {
  272. .name = "DMAR-MSI",
  273. .irq_unmask = dmar_msi_unmask,
  274. .irq_mask = dmar_msi_mask,
  275. .irq_ack = irq_chip_ack_parent,
  276. .irq_set_affinity = msi_domain_set_affinity,
  277. .irq_retrigger = irq_chip_retrigger_hierarchy,
  278. .irq_compose_msi_msg = dmar_msi_compose_msg,
  279. .irq_write_msi_msg = dmar_msi_write_msg,
  280. .flags = IRQCHIP_SKIP_SET_WAKE |
  281. IRQCHIP_AFFINITY_PRE_STARTUP,
  282. };
  283. static int dmar_msi_init(struct irq_domain *domain,
  284. struct msi_domain_info *info, unsigned int virq,
  285. irq_hw_number_t hwirq, msi_alloc_info_t *arg)
  286. {
  287. irq_domain_set_info(domain, virq, arg->devid, info->chip, NULL,
  288. handle_edge_irq, arg->data, "edge");
  289. return 0;
  290. }
  291. static struct msi_domain_ops dmar_msi_domain_ops = {
  292. .msi_init = dmar_msi_init,
  293. };
  294. static struct msi_domain_info dmar_msi_domain_info = {
  295. .ops = &dmar_msi_domain_ops,
  296. .chip = &dmar_msi_controller,
  297. .flags = MSI_FLAG_USE_DEF_DOM_OPS,
  298. };
  299. static struct irq_domain *dmar_get_irq_domain(void)
  300. {
  301. static struct irq_domain *dmar_domain;
  302. static DEFINE_MUTEX(dmar_lock);
  303. struct fwnode_handle *fn;
  304. mutex_lock(&dmar_lock);
  305. if (dmar_domain)
  306. goto out;
  307. fn = irq_domain_alloc_named_fwnode("DMAR-MSI");
  308. if (fn) {
  309. dmar_domain = msi_create_irq_domain(fn, &dmar_msi_domain_info,
  310. x86_vector_domain);
  311. if (!dmar_domain)
  312. irq_domain_free_fwnode(fn);
  313. }
  314. out:
  315. mutex_unlock(&dmar_lock);
  316. return dmar_domain;
  317. }
  318. int dmar_alloc_hwirq(int id, int node, void *arg)
  319. {
  320. struct irq_domain *domain = dmar_get_irq_domain();
  321. struct irq_alloc_info info;
  322. if (!domain)
  323. return -1;
  324. init_irq_alloc_info(&info, NULL);
  325. info.type = X86_IRQ_ALLOC_TYPE_DMAR;
  326. info.devid = id;
  327. info.hwirq = id;
  328. info.data = arg;
  329. return irq_domain_alloc_irqs(domain, 1, node, &info);
  330. }
  331. void dmar_free_hwirq(int irq)
  332. {
  333. irq_domain_free_irqs(irq, 1);
  334. }
  335. #endif
  336. bool arch_restore_msi_irqs(struct pci_dev *dev)
  337. {
  338. return xen_initdom_restore_msi(dev);
  339. }