msi.c 9.0 KB

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