msi.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 pci_msi_domain_alloc_irqs(domain, dev, nvec, type);
  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 arch_init_msi_domain(struct irq_domain *parent)
  120. {
  121. if (disable_apic)
  122. return;
  123. msi_default_domain = pci_msi_create_irq_domain(NULL,
  124. &pci_msi_domain_info, parent);
  125. if (!msi_default_domain)
  126. pr_warn("failed to initialize irqdomain for MSI/MSI-x.\n");
  127. }
  128. #ifdef CONFIG_IRQ_REMAP
  129. static struct irq_chip pci_msi_ir_controller = {
  130. .name = "IR-PCI-MSI",
  131. .irq_unmask = pci_msi_unmask_irq,
  132. .irq_mask = pci_msi_mask_irq,
  133. .irq_ack = irq_chip_ack_parent,
  134. .irq_retrigger = irq_chip_retrigger_hierarchy,
  135. .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
  136. .flags = IRQCHIP_SKIP_SET_WAKE,
  137. };
  138. static struct msi_domain_info pci_msi_ir_domain_info = {
  139. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  140. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX,
  141. .ops = &pci_msi_domain_ops,
  142. .chip = &pci_msi_ir_controller,
  143. .handler = handle_edge_irq,
  144. .handler_name = "edge",
  145. };
  146. struct irq_domain *arch_create_msi_irq_domain(struct irq_domain *parent)
  147. {
  148. return pci_msi_create_irq_domain(NULL, &pci_msi_ir_domain_info, parent);
  149. }
  150. #endif
  151. #ifdef CONFIG_DMAR_TABLE
  152. static void dmar_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  153. {
  154. dmar_msi_write(data->irq, msg);
  155. }
  156. static struct irq_chip dmar_msi_controller = {
  157. .name = "DMAR-MSI",
  158. .irq_unmask = dmar_msi_unmask,
  159. .irq_mask = dmar_msi_mask,
  160. .irq_ack = irq_chip_ack_parent,
  161. .irq_set_affinity = msi_domain_set_affinity,
  162. .irq_retrigger = irq_chip_retrigger_hierarchy,
  163. .irq_compose_msi_msg = irq_msi_compose_msg,
  164. .irq_write_msi_msg = dmar_msi_write_msg,
  165. .flags = IRQCHIP_SKIP_SET_WAKE,
  166. };
  167. static irq_hw_number_t dmar_msi_get_hwirq(struct msi_domain_info *info,
  168. msi_alloc_info_t *arg)
  169. {
  170. return arg->dmar_id;
  171. }
  172. static int dmar_msi_init(struct irq_domain *domain,
  173. struct msi_domain_info *info, unsigned int virq,
  174. irq_hw_number_t hwirq, msi_alloc_info_t *arg)
  175. {
  176. irq_domain_set_info(domain, virq, arg->dmar_id, info->chip, NULL,
  177. handle_edge_irq, arg->dmar_data, "edge");
  178. return 0;
  179. }
  180. static struct msi_domain_ops dmar_msi_domain_ops = {
  181. .get_hwirq = dmar_msi_get_hwirq,
  182. .msi_init = dmar_msi_init,
  183. };
  184. static struct msi_domain_info dmar_msi_domain_info = {
  185. .ops = &dmar_msi_domain_ops,
  186. .chip = &dmar_msi_controller,
  187. };
  188. static struct irq_domain *dmar_get_irq_domain(void)
  189. {
  190. static struct irq_domain *dmar_domain;
  191. static DEFINE_MUTEX(dmar_lock);
  192. mutex_lock(&dmar_lock);
  193. if (dmar_domain == NULL)
  194. dmar_domain = msi_create_irq_domain(NULL, &dmar_msi_domain_info,
  195. x86_vector_domain);
  196. mutex_unlock(&dmar_lock);
  197. return dmar_domain;
  198. }
  199. int dmar_alloc_hwirq(int id, int node, void *arg)
  200. {
  201. struct irq_domain *domain = dmar_get_irq_domain();
  202. struct irq_alloc_info info;
  203. if (!domain)
  204. return -1;
  205. init_irq_alloc_info(&info, NULL);
  206. info.type = X86_IRQ_ALLOC_TYPE_DMAR;
  207. info.dmar_id = id;
  208. info.dmar_data = arg;
  209. return irq_domain_alloc_irqs(domain, 1, node, &info);
  210. }
  211. void dmar_free_hwirq(int irq)
  212. {
  213. irq_domain_free_irqs(irq, 1);
  214. }
  215. #endif
  216. /*
  217. * MSI message composition
  218. */
  219. #ifdef CONFIG_HPET_TIMER
  220. static inline int hpet_dev_id(struct irq_domain *domain)
  221. {
  222. struct msi_domain_info *info = msi_get_domain_info(domain);
  223. return (int)(long)info->data;
  224. }
  225. static void hpet_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
  226. {
  227. hpet_msi_write(irq_data_get_irq_handler_data(data), msg);
  228. }
  229. static struct irq_chip hpet_msi_controller __ro_after_init = {
  230. .name = "HPET-MSI",
  231. .irq_unmask = hpet_msi_unmask,
  232. .irq_mask = hpet_msi_mask,
  233. .irq_ack = irq_chip_ack_parent,
  234. .irq_set_affinity = msi_domain_set_affinity,
  235. .irq_retrigger = irq_chip_retrigger_hierarchy,
  236. .irq_compose_msi_msg = irq_msi_compose_msg,
  237. .irq_write_msi_msg = hpet_msi_write_msg,
  238. .flags = IRQCHIP_SKIP_SET_WAKE,
  239. };
  240. static irq_hw_number_t hpet_msi_get_hwirq(struct msi_domain_info *info,
  241. msi_alloc_info_t *arg)
  242. {
  243. return arg->hpet_index;
  244. }
  245. static int hpet_msi_init(struct irq_domain *domain,
  246. struct msi_domain_info *info, unsigned int virq,
  247. irq_hw_number_t hwirq, msi_alloc_info_t *arg)
  248. {
  249. irq_set_status_flags(virq, IRQ_MOVE_PCNTXT);
  250. irq_domain_set_info(domain, virq, arg->hpet_index, info->chip, NULL,
  251. handle_edge_irq, arg->hpet_data, "edge");
  252. return 0;
  253. }
  254. static void hpet_msi_free(struct irq_domain *domain,
  255. struct msi_domain_info *info, unsigned int virq)
  256. {
  257. irq_clear_status_flags(virq, IRQ_MOVE_PCNTXT);
  258. }
  259. static struct msi_domain_ops hpet_msi_domain_ops = {
  260. .get_hwirq = hpet_msi_get_hwirq,
  261. .msi_init = hpet_msi_init,
  262. .msi_free = hpet_msi_free,
  263. };
  264. static struct msi_domain_info hpet_msi_domain_info = {
  265. .ops = &hpet_msi_domain_ops,
  266. .chip = &hpet_msi_controller,
  267. };
  268. struct irq_domain *hpet_create_irq_domain(int hpet_id)
  269. {
  270. struct irq_domain *parent;
  271. struct irq_alloc_info info;
  272. struct msi_domain_info *domain_info;
  273. if (x86_vector_domain == NULL)
  274. return NULL;
  275. domain_info = kzalloc(sizeof(*domain_info), GFP_KERNEL);
  276. if (!domain_info)
  277. return NULL;
  278. *domain_info = hpet_msi_domain_info;
  279. domain_info->data = (void *)(long)hpet_id;
  280. init_irq_alloc_info(&info, NULL);
  281. info.type = X86_IRQ_ALLOC_TYPE_HPET;
  282. info.hpet_id = hpet_id;
  283. parent = irq_remapping_get_ir_irq_domain(&info);
  284. if (parent == NULL)
  285. parent = x86_vector_domain;
  286. else
  287. hpet_msi_controller.name = "IR-HPET-MSI";
  288. return msi_create_irq_domain(NULL, domain_info, parent);
  289. }
  290. int hpet_assign_irq(struct irq_domain *domain, struct hpet_dev *dev,
  291. int dev_num)
  292. {
  293. struct irq_alloc_info info;
  294. init_irq_alloc_info(&info, NULL);
  295. info.type = X86_IRQ_ALLOC_TYPE_HPET;
  296. info.hpet_data = dev;
  297. info.hpet_id = hpet_dev_id(domain);
  298. info.hpet_index = dev_num;
  299. return irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info);
  300. }
  301. #endif