msi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Support of MSI, HPET and DMAR interrupts.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
  5. * Moved from arch/x86/kernel/apic/io_apic.c.
  6. * Jiang Liu <jiang.liu@linux.intel.com>
  7. * Convert to hierarchical irqdomain
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/mm.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/pci.h>
  17. #include <linux/dmar.h>
  18. #include <linux/hpet.h>
  19. #include <linux/msi.h>
  20. #include <asm/irqdomain.h>
  21. #include <asm/msidef.h>
  22. #include <asm/hpet.h>
  23. #include <asm/hw_irq.h>
  24. #include <asm/apic.h>
  25. #include <asm/irq_remapping.h>
  26. static struct irq_domain *msi_default_domain;
  27. static void __irq_msi_compose_msg(struct irq_cfg *cfg, struct msi_msg *msg)
  28. {
  29. msg->address_hi = MSI_ADDR_BASE_HI;
  30. if (x2apic_enabled())
  31. msg->address_hi |= MSI_ADDR_EXT_DEST_ID(cfg->dest_apicid);
  32. msg->address_lo =
  33. MSI_ADDR_BASE_LO |
  34. ((apic->irq_dest_mode == 0) ?
  35. MSI_ADDR_DEST_MODE_PHYSICAL :
  36. MSI_ADDR_DEST_MODE_LOGICAL) |
  37. MSI_ADDR_REDIRECTION_CPU |
  38. MSI_ADDR_DEST_ID(cfg->dest_apicid);
  39. msg->data =
  40. MSI_DATA_TRIGGER_EDGE |
  41. MSI_DATA_LEVEL_ASSERT |
  42. MSI_DATA_DELIVERY_FIXED |
  43. MSI_DATA_VECTOR(cfg->vector);
  44. }
  45. static void irq_msi_compose_msg(struct irq_data *data, struct msi_msg *msg)
  46. {
  47. __irq_msi_compose_msg(irqd_cfg(data), msg);
  48. }
  49. static void irq_msi_update_msg(struct irq_data *irqd, struct irq_cfg *cfg)
  50. {
  51. struct msi_msg msg[2] = { [1] = { }, };
  52. __irq_msi_compose_msg(cfg, msg);
  53. irq_data_get_irq_chip(irqd)->irq_write_msi_msg(irqd, msg);
  54. }
  55. static int
  56. msi_set_affinity(struct irq_data *irqd, const struct cpumask *mask, bool force)
  57. {
  58. struct irq_cfg old_cfg, *cfg = irqd_cfg(irqd);
  59. struct irq_data *parent = irqd->parent_data;
  60. unsigned int cpu;
  61. int ret;
  62. /* Save the current configuration */
  63. cpu = cpumask_first(irq_data_get_effective_affinity_mask(irqd));
  64. old_cfg = *cfg;
  65. /* Allocate a new target vector */
  66. ret = parent->chip->irq_set_affinity(parent, mask, force);
  67. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  68. return ret;
  69. /*
  70. * For non-maskable and non-remapped MSI interrupts the migration
  71. * to a different destination CPU and a different vector has to be
  72. * done careful to handle the possible stray interrupt which can be
  73. * caused by the non-atomic update of the address/data pair.
  74. *
  75. * Direct update is possible when:
  76. * - The MSI is maskable (remapped MSI does not use this code path)).
  77. * The quirk bit is not set in this case.
  78. * - The new vector is the same as the old vector
  79. * - The old vector is MANAGED_IRQ_SHUTDOWN_VECTOR (interrupt starts up)
  80. * - The new destination CPU is the same as the old destination CPU
  81. */
  82. if (!irqd_msi_nomask_quirk(irqd) ||
  83. cfg->vector == old_cfg.vector ||
  84. old_cfg.vector == MANAGED_IRQ_SHUTDOWN_VECTOR ||
  85. cfg->dest_apicid == old_cfg.dest_apicid) {
  86. irq_msi_update_msg(irqd, cfg);
  87. return ret;
  88. }
  89. /*
  90. * Paranoia: Validate that the interrupt target is the local
  91. * CPU.
  92. */
  93. if (WARN_ON_ONCE(cpu != smp_processor_id())) {
  94. irq_msi_update_msg(irqd, cfg);
  95. return ret;
  96. }
  97. /*
  98. * Redirect the interrupt to the new vector on the current CPU
  99. * first. This might cause a spurious interrupt on this vector if
  100. * the device raises an interrupt right between this update and the
  101. * update to the final destination CPU.
  102. *
  103. * If the vector is in use then the installed device handler will
  104. * denote it as spurious which is no harm as this is a rare event
  105. * and interrupt handlers have to cope with spurious interrupts
  106. * anyway. If the vector is unused, then it is marked so it won't
  107. * trigger the 'No irq handler for vector' warning in do_IRQ().
  108. *
  109. * This requires to hold vector lock to prevent concurrent updates to
  110. * the affected vector.
  111. */
  112. lock_vector_lock();
  113. /*
  114. * Mark the new target vector on the local CPU if it is currently
  115. * unused. Reuse the VECTOR_RETRIGGERED state which is also used in
  116. * the CPU hotplug path for a similar purpose. This cannot be
  117. * undone here as the current CPU has interrupts disabled and
  118. * cannot handle the interrupt before the whole set_affinity()
  119. * section is done. In the CPU unplug case, the current CPU is
  120. * about to vanish and will not handle any interrupts anymore. The
  121. * vector is cleaned up when the CPU comes online again.
  122. */
  123. if (IS_ERR_OR_NULL(this_cpu_read(vector_irq[cfg->vector])))
  124. this_cpu_write(vector_irq[cfg->vector], VECTOR_RETRIGGERED);
  125. /* Redirect it to the new vector on the local CPU temporarily */
  126. old_cfg.vector = cfg->vector;
  127. irq_msi_update_msg(irqd, &old_cfg);
  128. /* Now transition it to the target CPU */
  129. irq_msi_update_msg(irqd, cfg);
  130. /*
  131. * All interrupts after this point are now targeted at the new
  132. * vector/CPU.
  133. *
  134. * Drop vector lock before testing whether the temporary assignment
  135. * to the local CPU was hit by an interrupt raised in the device,
  136. * because the retrigger function acquires vector lock again.
  137. */
  138. unlock_vector_lock();
  139. /*
  140. * Check whether the transition raced with a device interrupt and
  141. * is pending in the local APICs IRR. It is safe to do this outside
  142. * of vector lock as the irq_desc::lock of this interrupt is still
  143. * held and interrupts are disabled: The check is not accessing the
  144. * underlying vector store. It's just checking the local APIC's
  145. * IRR.
  146. */
  147. if (lapic_vector_set_in_irr(cfg->vector))
  148. irq_data_get_irq_chip(irqd)->irq_retrigger(irqd);
  149. return ret;
  150. }
  151. /*
  152. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  153. * which implement the MSI or MSI-X Capability Structure.
  154. */
  155. static struct irq_chip pci_msi_controller = {
  156. .name = "PCI-MSI",
  157. .irq_unmask = pci_msi_unmask_irq,
  158. .irq_mask = pci_msi_mask_irq,
  159. .irq_ack = irq_chip_ack_parent,
  160. .irq_retrigger = irq_chip_retrigger_hierarchy,
  161. .irq_compose_msi_msg = irq_msi_compose_msg,
  162. .irq_set_affinity = msi_set_affinity,
  163. .flags = IRQCHIP_SKIP_SET_WAKE,
  164. };
  165. int native_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  166. {
  167. struct irq_domain *domain;
  168. struct irq_alloc_info info;
  169. init_irq_alloc_info(&info, NULL);
  170. info.type = X86_IRQ_ALLOC_TYPE_MSI;
  171. info.msi_dev = dev;
  172. domain = irq_remapping_get_irq_domain(&info);
  173. if (domain == NULL)
  174. domain = msi_default_domain;
  175. if (domain == NULL)
  176. return -ENOSYS;
  177. return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
  178. }
  179. void native_teardown_msi_irq(unsigned int irq)
  180. {
  181. irq_domain_free_irqs(irq, 1);
  182. }
  183. static irq_hw_number_t pci_msi_get_hwirq(struct msi_domain_info *info,
  184. msi_alloc_info_t *arg)
  185. {
  186. return arg->msi_hwirq;
  187. }
  188. int pci_msi_prepare(struct irq_domain *domain, struct device *dev, int nvec,
  189. msi_alloc_info_t *arg)
  190. {
  191. struct pci_dev *pdev = to_pci_dev(dev);
  192. struct msi_desc *desc = first_pci_msi_entry(pdev);
  193. init_irq_alloc_info(arg, NULL);
  194. arg->msi_dev = pdev;
  195. if (desc->msi_attrib.is_msix) {
  196. arg->type = X86_IRQ_ALLOC_TYPE_MSIX;
  197. } else {
  198. arg->type = X86_IRQ_ALLOC_TYPE_MSI;
  199. arg->flags |= X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  200. }
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(pci_msi_prepare);
  204. void pci_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  205. {
  206. arg->msi_hwirq = pci_msi_domain_calc_hwirq(arg->msi_dev, desc);
  207. }
  208. EXPORT_SYMBOL_GPL(pci_msi_set_desc);
  209. static struct msi_domain_ops pci_msi_domain_ops = {
  210. .get_hwirq = pci_msi_get_hwirq,
  211. .msi_prepare = pci_msi_prepare,
  212. .set_desc = pci_msi_set_desc,
  213. };
  214. static struct msi_domain_info pci_msi_domain_info = {
  215. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  216. MSI_FLAG_PCI_MSIX,
  217. .ops = &pci_msi_domain_ops,
  218. .chip = &pci_msi_controller,
  219. .handler = handle_edge_irq,
  220. .handler_name = "edge",
  221. };
  222. void __init arch_init_msi_domain(struct irq_domain *parent)
  223. {
  224. struct fwnode_handle *fn;
  225. if (disable_apic)
  226. return;
  227. fn = irq_domain_alloc_named_fwnode("PCI-MSI");
  228. if (fn) {
  229. msi_default_domain =
  230. pci_msi_create_irq_domain(fn, &pci_msi_domain_info,
  231. parent);
  232. }
  233. if (!msi_default_domain) {
  234. irq_domain_free_fwnode(fn);
  235. pr_warn("failed to initialize irqdomain for MSI/MSI-x.\n");
  236. } else {
  237. msi_default_domain->flags |= IRQ_DOMAIN_MSI_NOMASK_QUIRK;
  238. }
  239. }
  240. #ifdef CONFIG_IRQ_REMAP
  241. static struct irq_chip pci_msi_ir_controller = {
  242. .name = "IR-PCI-MSI",
  243. .irq_unmask = pci_msi_unmask_irq,
  244. .irq_mask = pci_msi_mask_irq,
  245. .irq_ack = irq_chip_ack_parent,
  246. .irq_retrigger = irq_chip_retrigger_hierarchy,
  247. .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
  248. .flags = IRQCHIP_SKIP_SET_WAKE,
  249. };
  250. static struct msi_domain_info pci_msi_ir_domain_info = {
  251. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  252. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX,
  253. .ops = &pci_msi_domain_ops,
  254. .chip = &pci_msi_ir_controller,
  255. .handler = handle_edge_irq,
  256. .handler_name = "edge",
  257. };
  258. struct irq_domain *arch_create_remap_msi_irq_domain(struct irq_domain *parent,
  259. const char *name, int id)
  260. {
  261. struct fwnode_handle *fn;
  262. struct irq_domain *d;
  263. fn = irq_domain_alloc_named_id_fwnode(name, id);
  264. if (!fn)
  265. return NULL;
  266. d = pci_msi_create_irq_domain(fn, &pci_msi_ir_domain_info, parent);
  267. if (!d)
  268. irq_domain_free_fwnode(fn);
  269. return d;
  270. }
  271. #endif
  272. #ifdef CONFIG_DMAR_TABLE
  273. static void dmar_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  274. {
  275. dmar_msi_write(data->irq, msg);
  276. }
  277. static struct irq_chip dmar_msi_controller = {
  278. .name = "DMAR-MSI",
  279. .irq_unmask = dmar_msi_unmask,
  280. .irq_mask = dmar_msi_mask,
  281. .irq_ack = irq_chip_ack_parent,
  282. .irq_set_affinity = msi_domain_set_affinity,
  283. .irq_retrigger = irq_chip_retrigger_hierarchy,
  284. .irq_compose_msi_msg = irq_msi_compose_msg,
  285. .irq_write_msi_msg = dmar_msi_write_msg,
  286. .flags = IRQCHIP_SKIP_SET_WAKE,
  287. };
  288. static irq_hw_number_t dmar_msi_get_hwirq(struct msi_domain_info *info,
  289. msi_alloc_info_t *arg)
  290. {
  291. return arg->dmar_id;
  292. }
  293. static int dmar_msi_init(struct irq_domain *domain,
  294. struct msi_domain_info *info, unsigned int virq,
  295. irq_hw_number_t hwirq, msi_alloc_info_t *arg)
  296. {
  297. irq_domain_set_info(domain, virq, arg->dmar_id, info->chip, NULL,
  298. handle_edge_irq, arg->dmar_data, "edge");
  299. return 0;
  300. }
  301. static struct msi_domain_ops dmar_msi_domain_ops = {
  302. .get_hwirq = dmar_msi_get_hwirq,
  303. .msi_init = dmar_msi_init,
  304. };
  305. static struct msi_domain_info dmar_msi_domain_info = {
  306. .ops = &dmar_msi_domain_ops,
  307. .chip = &dmar_msi_controller,
  308. };
  309. static struct irq_domain *dmar_get_irq_domain(void)
  310. {
  311. static struct irq_domain *dmar_domain;
  312. static DEFINE_MUTEX(dmar_lock);
  313. struct fwnode_handle *fn;
  314. mutex_lock(&dmar_lock);
  315. if (dmar_domain)
  316. goto out;
  317. fn = irq_domain_alloc_named_fwnode("DMAR-MSI");
  318. if (fn) {
  319. dmar_domain = msi_create_irq_domain(fn, &dmar_msi_domain_info,
  320. x86_vector_domain);
  321. if (!dmar_domain)
  322. irq_domain_free_fwnode(fn);
  323. }
  324. out:
  325. mutex_unlock(&dmar_lock);
  326. return dmar_domain;
  327. }
  328. int dmar_alloc_hwirq(int id, int node, void *arg)
  329. {
  330. struct irq_domain *domain = dmar_get_irq_domain();
  331. struct irq_alloc_info info;
  332. if (!domain)
  333. return -1;
  334. init_irq_alloc_info(&info, NULL);
  335. info.type = X86_IRQ_ALLOC_TYPE_DMAR;
  336. info.dmar_id = id;
  337. info.dmar_data = arg;
  338. return irq_domain_alloc_irqs(domain, 1, node, &info);
  339. }
  340. void dmar_free_hwirq(int irq)
  341. {
  342. irq_domain_free_irqs(irq, 1);
  343. }
  344. #endif
  345. /*
  346. * MSI message composition
  347. */
  348. #ifdef CONFIG_HPET_TIMER
  349. static inline int hpet_dev_id(struct irq_domain *domain)
  350. {
  351. struct msi_domain_info *info = msi_get_domain_info(domain);
  352. return (int)(long)info->data;
  353. }
  354. static void hpet_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  355. {
  356. hpet_msi_write(irq_data_get_irq_handler_data(data), msg);
  357. }
  358. static struct irq_chip hpet_msi_controller __ro_after_init = {
  359. .name = "HPET-MSI",
  360. .irq_unmask = hpet_msi_unmask,
  361. .irq_mask = hpet_msi_mask,
  362. .irq_ack = irq_chip_ack_parent,
  363. .irq_set_affinity = msi_domain_set_affinity,
  364. .irq_retrigger = irq_chip_retrigger_hierarchy,
  365. .irq_compose_msi_msg = irq_msi_compose_msg,
  366. .irq_write_msi_msg = hpet_msi_write_msg,
  367. .flags = IRQCHIP_SKIP_SET_WAKE,
  368. };
  369. static irq_hw_number_t hpet_msi_get_hwirq(struct msi_domain_info *info,
  370. msi_alloc_info_t *arg)
  371. {
  372. return arg->hpet_index;
  373. }
  374. static int hpet_msi_init(struct irq_domain *domain,
  375. struct msi_domain_info *info, unsigned int virq,
  376. irq_hw_number_t hwirq, msi_alloc_info_t *arg)
  377. {
  378. irq_set_status_flags(virq, IRQ_MOVE_PCNTXT);
  379. irq_domain_set_info(domain, virq, arg->hpet_index, info->chip, NULL,
  380. handle_edge_irq, arg->hpet_data, "edge");
  381. return 0;
  382. }
  383. static void hpet_msi_free(struct irq_domain *domain,
  384. struct msi_domain_info *info, unsigned int virq)
  385. {
  386. irq_clear_status_flags(virq, IRQ_MOVE_PCNTXT);
  387. }
  388. static struct msi_domain_ops hpet_msi_domain_ops = {
  389. .get_hwirq = hpet_msi_get_hwirq,
  390. .msi_init = hpet_msi_init,
  391. .msi_free = hpet_msi_free,
  392. };
  393. static struct msi_domain_info hpet_msi_domain_info = {
  394. .ops = &hpet_msi_domain_ops,
  395. .chip = &hpet_msi_controller,
  396. };
  397. struct irq_domain *hpet_create_irq_domain(int hpet_id)
  398. {
  399. struct msi_domain_info *domain_info;
  400. struct irq_domain *parent, *d;
  401. struct irq_alloc_info info;
  402. struct fwnode_handle *fn;
  403. if (x86_vector_domain == NULL)
  404. return NULL;
  405. domain_info = kzalloc(sizeof(*domain_info), GFP_KERNEL);
  406. if (!domain_info)
  407. return NULL;
  408. *domain_info = hpet_msi_domain_info;
  409. domain_info->data = (void *)(long)hpet_id;
  410. init_irq_alloc_info(&info, NULL);
  411. info.type = X86_IRQ_ALLOC_TYPE_HPET;
  412. info.hpet_id = hpet_id;
  413. parent = irq_remapping_get_ir_irq_domain(&info);
  414. if (parent == NULL)
  415. parent = x86_vector_domain;
  416. else
  417. hpet_msi_controller.name = "IR-HPET-MSI";
  418. fn = irq_domain_alloc_named_id_fwnode(hpet_msi_controller.name,
  419. hpet_id);
  420. if (!fn) {
  421. kfree(domain_info);
  422. return NULL;
  423. }
  424. d = msi_create_irq_domain(fn, domain_info, parent);
  425. if (!d) {
  426. irq_domain_free_fwnode(fn);
  427. kfree(domain_info);
  428. }
  429. return d;
  430. }
  431. int hpet_assign_irq(struct irq_domain *domain, struct hpet_dev *dev,
  432. int dev_num)
  433. {
  434. struct irq_alloc_info info;
  435. init_irq_alloc_info(&info, NULL);
  436. info.type = X86_IRQ_ALLOC_TYPE_HPET;
  437. info.hpet_data = dev;
  438. info.hpet_id = hpet_dev_id(domain);
  439. info.hpet_index = dev_num;
  440. return irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info);
  441. }
  442. #endif