msi.c 9.6 KB

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