msi.c 9.6 KB

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