msi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/pci.h>
  16. #include <linux/dmar.h>
  17. #include <linux/hpet.h>
  18. #include <linux/msi.h>
  19. #include <asm/irqdomain.h>
  20. #include <asm/msidef.h>
  21. #include <asm/hpet.h>
  22. #include <asm/hw_irq.h>
  23. #include <asm/apic.h>
  24. #include <asm/irq_remapping.h>
  25. static struct irq_domain *msi_default_domain;
  26. static void irq_msi_compose_msg(struct irq_data *data, struct msi_msg *msg)
  27. {
  28. struct irq_cfg *cfg = irqd_cfg(data);
  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. ((apic->irq_delivery_mode != dest_LowestPrio) ?
  38. MSI_ADDR_REDIRECTION_CPU :
  39. MSI_ADDR_REDIRECTION_LOWPRI) |
  40. MSI_ADDR_DEST_ID(cfg->dest_apicid);
  41. msg->data =
  42. MSI_DATA_TRIGGER_EDGE |
  43. MSI_DATA_LEVEL_ASSERT |
  44. ((apic->irq_delivery_mode != dest_LowestPrio) ?
  45. MSI_DATA_DELIVERY_FIXED :
  46. MSI_DATA_DELIVERY_LOWPRI) |
  47. MSI_DATA_VECTOR(cfg->vector);
  48. }
  49. /*
  50. * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
  51. * which implement the MSI or MSI-X Capability Structure.
  52. */
  53. static struct irq_chip pci_msi_controller = {
  54. .name = "PCI-MSI",
  55. .irq_unmask = pci_msi_unmask_irq,
  56. .irq_mask = pci_msi_mask_irq,
  57. .irq_ack = irq_chip_ack_parent,
  58. .irq_retrigger = irq_chip_retrigger_hierarchy,
  59. .irq_compose_msi_msg = irq_msi_compose_msg,
  60. .flags = IRQCHIP_SKIP_SET_WAKE,
  61. };
  62. int native_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  63. {
  64. struct irq_domain *domain;
  65. struct irq_alloc_info info;
  66. init_irq_alloc_info(&info, NULL);
  67. info.type = X86_IRQ_ALLOC_TYPE_MSI;
  68. info.msi_dev = dev;
  69. domain = irq_remapping_get_irq_domain(&info);
  70. if (domain == NULL)
  71. domain = msi_default_domain;
  72. if (domain == NULL)
  73. return -ENOSYS;
  74. return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
  75. }
  76. void native_teardown_msi_irq(unsigned int irq)
  77. {
  78. irq_domain_free_irqs(irq, 1);
  79. }
  80. static irq_hw_number_t pci_msi_get_hwirq(struct msi_domain_info *info,
  81. msi_alloc_info_t *arg)
  82. {
  83. return arg->msi_hwirq;
  84. }
  85. int pci_msi_prepare(struct irq_domain *domain, struct device *dev, int nvec,
  86. msi_alloc_info_t *arg)
  87. {
  88. struct pci_dev *pdev = to_pci_dev(dev);
  89. struct msi_desc *desc = first_pci_msi_entry(pdev);
  90. init_irq_alloc_info(arg, NULL);
  91. arg->msi_dev = pdev;
  92. if (desc->msi_attrib.is_msix) {
  93. arg->type = X86_IRQ_ALLOC_TYPE_MSIX;
  94. } else {
  95. arg->type = X86_IRQ_ALLOC_TYPE_MSI;
  96. arg->flags |= X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  97. }
  98. return 0;
  99. }
  100. EXPORT_SYMBOL_GPL(pci_msi_prepare);
  101. void pci_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  102. {
  103. arg->msi_hwirq = pci_msi_domain_calc_hwirq(arg->msi_dev, desc);
  104. }
  105. EXPORT_SYMBOL_GPL(pci_msi_set_desc);
  106. static struct msi_domain_ops pci_msi_domain_ops = {
  107. .get_hwirq = pci_msi_get_hwirq,
  108. .msi_prepare = pci_msi_prepare,
  109. .set_desc = pci_msi_set_desc,
  110. };
  111. static struct msi_domain_info pci_msi_domain_info = {
  112. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  113. MSI_FLAG_PCI_MSIX,
  114. .ops = &pci_msi_domain_ops,
  115. .chip = &pci_msi_controller,
  116. .handler = handle_edge_irq,
  117. .handler_name = "edge",
  118. };
  119. void __init arch_init_msi_domain(struct irq_domain *parent)
  120. {
  121. struct fwnode_handle *fn;
  122. if (disable_apic)
  123. return;
  124. fn = irq_domain_alloc_named_fwnode("PCI-MSI");
  125. if (fn) {
  126. msi_default_domain =
  127. pci_msi_create_irq_domain(fn, &pci_msi_domain_info,
  128. parent);
  129. irq_domain_free_fwnode(fn);
  130. }
  131. if (!msi_default_domain)
  132. pr_warn("failed to initialize irqdomain for MSI/MSI-x.\n");
  133. }
  134. #ifdef CONFIG_IRQ_REMAP
  135. static struct irq_chip pci_msi_ir_controller = {
  136. .name = "IR-PCI-MSI",
  137. .irq_unmask = pci_msi_unmask_irq,
  138. .irq_mask = pci_msi_mask_irq,
  139. .irq_ack = irq_chip_ack_parent,
  140. .irq_retrigger = irq_chip_retrigger_hierarchy,
  141. .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
  142. .flags = IRQCHIP_SKIP_SET_WAKE,
  143. };
  144. static struct msi_domain_info pci_msi_ir_domain_info = {
  145. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  146. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX,
  147. .ops = &pci_msi_domain_ops,
  148. .chip = &pci_msi_ir_controller,
  149. .handler = handle_edge_irq,
  150. .handler_name = "edge",
  151. };
  152. struct irq_domain *arch_create_remap_msi_irq_domain(struct irq_domain *parent,
  153. const char *name, int id)
  154. {
  155. struct fwnode_handle *fn;
  156. struct irq_domain *d;
  157. fn = irq_domain_alloc_named_id_fwnode(name, id);
  158. if (!fn)
  159. return NULL;
  160. d = pci_msi_create_irq_domain(fn, &pci_msi_ir_domain_info, parent);
  161. irq_domain_free_fwnode(fn);
  162. return d;
  163. }
  164. #endif
  165. #ifdef CONFIG_DMAR_TABLE
  166. static void dmar_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  167. {
  168. dmar_msi_write(data->irq, msg);
  169. }
  170. static struct irq_chip dmar_msi_controller = {
  171. .name = "DMAR-MSI",
  172. .irq_unmask = dmar_msi_unmask,
  173. .irq_mask = dmar_msi_mask,
  174. .irq_ack = irq_chip_ack_parent,
  175. .irq_set_affinity = msi_domain_set_affinity,
  176. .irq_retrigger = irq_chip_retrigger_hierarchy,
  177. .irq_compose_msi_msg = irq_msi_compose_msg,
  178. .irq_write_msi_msg = dmar_msi_write_msg,
  179. .flags = IRQCHIP_SKIP_SET_WAKE,
  180. };
  181. static irq_hw_number_t dmar_msi_get_hwirq(struct msi_domain_info *info,
  182. msi_alloc_info_t *arg)
  183. {
  184. return arg->dmar_id;
  185. }
  186. static int dmar_msi_init(struct irq_domain *domain,
  187. struct msi_domain_info *info, unsigned int virq,
  188. irq_hw_number_t hwirq, msi_alloc_info_t *arg)
  189. {
  190. irq_domain_set_info(domain, virq, arg->dmar_id, info->chip, NULL,
  191. handle_edge_irq, arg->dmar_data, "edge");
  192. return 0;
  193. }
  194. static struct msi_domain_ops dmar_msi_domain_ops = {
  195. .get_hwirq = dmar_msi_get_hwirq,
  196. .msi_init = dmar_msi_init,
  197. };
  198. static struct msi_domain_info dmar_msi_domain_info = {
  199. .ops = &dmar_msi_domain_ops,
  200. .chip = &dmar_msi_controller,
  201. };
  202. static struct irq_domain *dmar_get_irq_domain(void)
  203. {
  204. static struct irq_domain *dmar_domain;
  205. static DEFINE_MUTEX(dmar_lock);
  206. struct fwnode_handle *fn;
  207. mutex_lock(&dmar_lock);
  208. if (dmar_domain)
  209. goto out;
  210. fn = irq_domain_alloc_named_fwnode("DMAR-MSI");
  211. if (fn) {
  212. dmar_domain = msi_create_irq_domain(fn, &dmar_msi_domain_info,
  213. x86_vector_domain);
  214. irq_domain_free_fwnode(fn);
  215. }
  216. out:
  217. mutex_unlock(&dmar_lock);
  218. return dmar_domain;
  219. }
  220. int dmar_alloc_hwirq(int id, int node, void *arg)
  221. {
  222. struct irq_domain *domain = dmar_get_irq_domain();
  223. struct irq_alloc_info info;
  224. if (!domain)
  225. return -1;
  226. init_irq_alloc_info(&info, NULL);
  227. info.type = X86_IRQ_ALLOC_TYPE_DMAR;
  228. info.dmar_id = id;
  229. info.dmar_data = arg;
  230. return irq_domain_alloc_irqs(domain, 1, node, &info);
  231. }
  232. void dmar_free_hwirq(int irq)
  233. {
  234. irq_domain_free_irqs(irq, 1);
  235. }
  236. #endif
  237. /*
  238. * MSI message composition
  239. */
  240. #ifdef CONFIG_HPET_TIMER
  241. static inline int hpet_dev_id(struct irq_domain *domain)
  242. {
  243. struct msi_domain_info *info = msi_get_domain_info(domain);
  244. return (int)(long)info->data;
  245. }
  246. static void hpet_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  247. {
  248. hpet_msi_write(irq_data_get_irq_handler_data(data), msg);
  249. }
  250. static struct irq_chip hpet_msi_controller __ro_after_init = {
  251. .name = "HPET-MSI",
  252. .irq_unmask = hpet_msi_unmask,
  253. .irq_mask = hpet_msi_mask,
  254. .irq_ack = irq_chip_ack_parent,
  255. .irq_set_affinity = msi_domain_set_affinity,
  256. .irq_retrigger = irq_chip_retrigger_hierarchy,
  257. .irq_compose_msi_msg = irq_msi_compose_msg,
  258. .irq_write_msi_msg = hpet_msi_write_msg,
  259. .flags = IRQCHIP_SKIP_SET_WAKE,
  260. };
  261. static irq_hw_number_t hpet_msi_get_hwirq(struct msi_domain_info *info,
  262. msi_alloc_info_t *arg)
  263. {
  264. return arg->hpet_index;
  265. }
  266. static int hpet_msi_init(struct irq_domain *domain,
  267. struct msi_domain_info *info, unsigned int virq,
  268. irq_hw_number_t hwirq, msi_alloc_info_t *arg)
  269. {
  270. irq_set_status_flags(virq, IRQ_MOVE_PCNTXT);
  271. irq_domain_set_info(domain, virq, arg->hpet_index, info->chip, NULL,
  272. handle_edge_irq, arg->hpet_data, "edge");
  273. return 0;
  274. }
  275. static void hpet_msi_free(struct irq_domain *domain,
  276. struct msi_domain_info *info, unsigned int virq)
  277. {
  278. irq_clear_status_flags(virq, IRQ_MOVE_PCNTXT);
  279. }
  280. static struct msi_domain_ops hpet_msi_domain_ops = {
  281. .get_hwirq = hpet_msi_get_hwirq,
  282. .msi_init = hpet_msi_init,
  283. .msi_free = hpet_msi_free,
  284. };
  285. static struct msi_domain_info hpet_msi_domain_info = {
  286. .ops = &hpet_msi_domain_ops,
  287. .chip = &hpet_msi_controller,
  288. };
  289. struct irq_domain *hpet_create_irq_domain(int hpet_id)
  290. {
  291. struct msi_domain_info *domain_info;
  292. struct irq_domain *parent, *d;
  293. struct irq_alloc_info info;
  294. struct fwnode_handle *fn;
  295. if (x86_vector_domain == NULL)
  296. return NULL;
  297. domain_info = kzalloc(sizeof(*domain_info), GFP_KERNEL);
  298. if (!domain_info)
  299. return NULL;
  300. *domain_info = hpet_msi_domain_info;
  301. domain_info->data = (void *)(long)hpet_id;
  302. init_irq_alloc_info(&info, NULL);
  303. info.type = X86_IRQ_ALLOC_TYPE_HPET;
  304. info.hpet_id = hpet_id;
  305. parent = irq_remapping_get_ir_irq_domain(&info);
  306. if (parent == NULL)
  307. parent = x86_vector_domain;
  308. else
  309. hpet_msi_controller.name = "IR-HPET-MSI";
  310. fn = irq_domain_alloc_named_id_fwnode(hpet_msi_controller.name,
  311. hpet_id);
  312. if (!fn) {
  313. kfree(domain_info);
  314. return NULL;
  315. }
  316. d = msi_create_irq_domain(fn, domain_info, parent);
  317. irq_domain_free_fwnode(fn);
  318. return d;
  319. }
  320. int hpet_assign_irq(struct irq_domain *domain, struct hpet_dev *dev,
  321. int dev_num)
  322. {
  323. struct irq_alloc_info info;
  324. init_irq_alloc_info(&info, NULL);
  325. info.type = X86_IRQ_ALLOC_TYPE_HPET;
  326. info.hpet_data = dev;
  327. info.hpet_id = hpet_dev_id(domain);
  328. info.hpet_index = dev_num;
  329. return irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info);
  330. }
  331. #endif