hyperv-iommu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hyper-V stub IOMMU driver.
  4. *
  5. * Copyright (C) 2019, Microsoft, Inc.
  6. *
  7. * Author : Lan Tianyu <[email protected]>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/iommu.h>
  13. #include <linux/module.h>
  14. #include <asm/apic.h>
  15. #include <asm/cpu.h>
  16. #include <asm/hw_irq.h>
  17. #include <asm/io_apic.h>
  18. #include <asm/irq_remapping.h>
  19. #include <asm/hypervisor.h>
  20. #include <asm/mshyperv.h>
  21. #include "irq_remapping.h"
  22. #ifdef CONFIG_IRQ_REMAP
  23. /*
  24. * According 82093AA IO-APIC spec , IO APIC has a 24-entry Interrupt
  25. * Redirection Table. Hyper-V exposes one single IO-APIC and so define
  26. * 24 IO APIC remmapping entries.
  27. */
  28. #define IOAPIC_REMAPPING_ENTRY 24
  29. static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
  30. static struct irq_domain *ioapic_ir_domain;
  31. static int hyperv_ir_set_affinity(struct irq_data *data,
  32. const struct cpumask *mask, bool force)
  33. {
  34. struct irq_data *parent = data->parent_data;
  35. struct irq_cfg *cfg = irqd_cfg(data);
  36. int ret;
  37. /* Return error If new irq affinity is out of ioapic_max_cpumask. */
  38. if (!cpumask_subset(mask, &ioapic_max_cpumask))
  39. return -EINVAL;
  40. ret = parent->chip->irq_set_affinity(parent, mask, force);
  41. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  42. return ret;
  43. send_cleanup_vector(cfg);
  44. return 0;
  45. }
  46. static struct irq_chip hyperv_ir_chip = {
  47. .name = "HYPERV-IR",
  48. .irq_ack = apic_ack_irq,
  49. .irq_set_affinity = hyperv_ir_set_affinity,
  50. };
  51. static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
  52. unsigned int virq, unsigned int nr_irqs,
  53. void *arg)
  54. {
  55. struct irq_alloc_info *info = arg;
  56. struct irq_data *irq_data;
  57. int ret = 0;
  58. if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
  59. return -EINVAL;
  60. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  61. if (ret < 0)
  62. return ret;
  63. irq_data = irq_domain_get_irq_data(domain, virq);
  64. if (!irq_data) {
  65. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  66. return -EINVAL;
  67. }
  68. irq_data->chip = &hyperv_ir_chip;
  69. /*
  70. * Hypver-V IO APIC irq affinity should be in the scope of
  71. * ioapic_max_cpumask because no irq remapping support.
  72. */
  73. irq_data_update_affinity(irq_data, &ioapic_max_cpumask);
  74. return 0;
  75. }
  76. static void hyperv_irq_remapping_free(struct irq_domain *domain,
  77. unsigned int virq, unsigned int nr_irqs)
  78. {
  79. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  80. }
  81. static int hyperv_irq_remapping_select(struct irq_domain *d,
  82. struct irq_fwspec *fwspec,
  83. enum irq_domain_bus_token bus_token)
  84. {
  85. /* Claim the only I/O APIC emulated by Hyper-V */
  86. return x86_fwspec_is_ioapic(fwspec);
  87. }
  88. static const struct irq_domain_ops hyperv_ir_domain_ops = {
  89. .select = hyperv_irq_remapping_select,
  90. .alloc = hyperv_irq_remapping_alloc,
  91. .free = hyperv_irq_remapping_free,
  92. };
  93. static const struct irq_domain_ops hyperv_root_ir_domain_ops;
  94. static int __init hyperv_prepare_irq_remapping(void)
  95. {
  96. struct fwnode_handle *fn;
  97. int i;
  98. const char *name;
  99. const struct irq_domain_ops *ops;
  100. if (!hypervisor_is_type(X86_HYPER_MS_HYPERV) ||
  101. x86_init.hyper.msi_ext_dest_id() ||
  102. !x2apic_supported())
  103. return -ENODEV;
  104. if (hv_root_partition) {
  105. name = "HYPERV-ROOT-IR";
  106. ops = &hyperv_root_ir_domain_ops;
  107. } else {
  108. name = "HYPERV-IR";
  109. ops = &hyperv_ir_domain_ops;
  110. }
  111. fn = irq_domain_alloc_named_id_fwnode(name, 0);
  112. if (!fn)
  113. return -ENOMEM;
  114. ioapic_ir_domain =
  115. irq_domain_create_hierarchy(arch_get_ir_parent_domain(),
  116. 0, IOAPIC_REMAPPING_ENTRY, fn, ops, NULL);
  117. if (!ioapic_ir_domain) {
  118. irq_domain_free_fwnode(fn);
  119. return -ENOMEM;
  120. }
  121. if (hv_root_partition)
  122. return 0; /* The rest is only relevant to guests */
  123. /*
  124. * Hyper-V doesn't provide irq remapping function for
  125. * IO-APIC and so IO-APIC only accepts 8-bit APIC ID.
  126. * Cpu's APIC ID is read from ACPI MADT table and APIC IDs
  127. * in the MADT table on Hyper-v are sorted monotonic increasingly.
  128. * APIC ID reflects cpu topology. There maybe some APIC ID
  129. * gaps when cpu number in a socket is not power of two. Prepare
  130. * max cpu affinity for IOAPIC irqs. Scan cpu 0-255 and set cpu
  131. * into ioapic_max_cpumask if its APIC ID is less than 256.
  132. */
  133. for (i = min_t(unsigned int, num_possible_cpus() - 1, 255); i >= 0; i--)
  134. if (cpu_physical_id(i) < 256)
  135. cpumask_set_cpu(i, &ioapic_max_cpumask);
  136. return 0;
  137. }
  138. static int __init hyperv_enable_irq_remapping(void)
  139. {
  140. return IRQ_REMAP_X2APIC_MODE;
  141. }
  142. struct irq_remap_ops hyperv_irq_remap_ops = {
  143. .prepare = hyperv_prepare_irq_remapping,
  144. .enable = hyperv_enable_irq_remapping,
  145. };
  146. /* IRQ remapping domain when Linux runs as the root partition */
  147. struct hyperv_root_ir_data {
  148. u8 ioapic_id;
  149. bool is_level;
  150. struct hv_interrupt_entry entry;
  151. };
  152. static void
  153. hyperv_root_ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
  154. {
  155. u64 status;
  156. u32 vector;
  157. struct irq_cfg *cfg;
  158. int ioapic_id;
  159. const struct cpumask *affinity;
  160. int cpu;
  161. struct hv_interrupt_entry entry;
  162. struct hyperv_root_ir_data *data = irq_data->chip_data;
  163. struct IO_APIC_route_entry e;
  164. cfg = irqd_cfg(irq_data);
  165. affinity = irq_data_get_effective_affinity_mask(irq_data);
  166. cpu = cpumask_first_and(affinity, cpu_online_mask);
  167. vector = cfg->vector;
  168. ioapic_id = data->ioapic_id;
  169. if (data->entry.source == HV_DEVICE_TYPE_IOAPIC
  170. && data->entry.ioapic_rte.as_uint64) {
  171. entry = data->entry;
  172. status = hv_unmap_ioapic_interrupt(ioapic_id, &entry);
  173. if (status != HV_STATUS_SUCCESS)
  174. pr_debug("%s: unexpected unmap status %lld\n", __func__, status);
  175. data->entry.ioapic_rte.as_uint64 = 0;
  176. data->entry.source = 0; /* Invalid source */
  177. }
  178. status = hv_map_ioapic_interrupt(ioapic_id, data->is_level, cpu,
  179. vector, &entry);
  180. if (status != HV_STATUS_SUCCESS) {
  181. pr_err("%s: map hypercall failed, status %lld\n", __func__, status);
  182. return;
  183. }
  184. data->entry = entry;
  185. /* Turn it into an IO_APIC_route_entry, and generate MSI MSG. */
  186. e.w1 = entry.ioapic_rte.low_uint32;
  187. e.w2 = entry.ioapic_rte.high_uint32;
  188. memset(msg, 0, sizeof(*msg));
  189. msg->arch_data.vector = e.vector;
  190. msg->arch_data.delivery_mode = e.delivery_mode;
  191. msg->arch_addr_lo.dest_mode_logical = e.dest_mode_logical;
  192. msg->arch_addr_lo.dmar_format = e.ir_format;
  193. msg->arch_addr_lo.dmar_index_0_14 = e.ir_index_0_14;
  194. }
  195. static int hyperv_root_ir_set_affinity(struct irq_data *data,
  196. const struct cpumask *mask, bool force)
  197. {
  198. struct irq_data *parent = data->parent_data;
  199. struct irq_cfg *cfg = irqd_cfg(data);
  200. int ret;
  201. ret = parent->chip->irq_set_affinity(parent, mask, force);
  202. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  203. return ret;
  204. send_cleanup_vector(cfg);
  205. return 0;
  206. }
  207. static struct irq_chip hyperv_root_ir_chip = {
  208. .name = "HYPERV-ROOT-IR",
  209. .irq_ack = apic_ack_irq,
  210. .irq_set_affinity = hyperv_root_ir_set_affinity,
  211. .irq_compose_msi_msg = hyperv_root_ir_compose_msi_msg,
  212. };
  213. static int hyperv_root_irq_remapping_alloc(struct irq_domain *domain,
  214. unsigned int virq, unsigned int nr_irqs,
  215. void *arg)
  216. {
  217. struct irq_alloc_info *info = arg;
  218. struct irq_data *irq_data;
  219. struct hyperv_root_ir_data *data;
  220. int ret = 0;
  221. if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
  222. return -EINVAL;
  223. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  224. if (ret < 0)
  225. return ret;
  226. data = kzalloc(sizeof(*data), GFP_KERNEL);
  227. if (!data) {
  228. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  229. return -ENOMEM;
  230. }
  231. irq_data = irq_domain_get_irq_data(domain, virq);
  232. if (!irq_data) {
  233. kfree(data);
  234. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  235. return -EINVAL;
  236. }
  237. data->ioapic_id = info->devid;
  238. data->is_level = info->ioapic.is_level;
  239. irq_data->chip = &hyperv_root_ir_chip;
  240. irq_data->chip_data = data;
  241. return 0;
  242. }
  243. static void hyperv_root_irq_remapping_free(struct irq_domain *domain,
  244. unsigned int virq, unsigned int nr_irqs)
  245. {
  246. struct irq_data *irq_data;
  247. struct hyperv_root_ir_data *data;
  248. struct hv_interrupt_entry *e;
  249. int i;
  250. for (i = 0; i < nr_irqs; i++) {
  251. irq_data = irq_domain_get_irq_data(domain, virq + i);
  252. if (irq_data && irq_data->chip_data) {
  253. data = irq_data->chip_data;
  254. e = &data->entry;
  255. if (e->source == HV_DEVICE_TYPE_IOAPIC
  256. && e->ioapic_rte.as_uint64)
  257. hv_unmap_ioapic_interrupt(data->ioapic_id,
  258. &data->entry);
  259. kfree(data);
  260. }
  261. }
  262. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  263. }
  264. static const struct irq_domain_ops hyperv_root_ir_domain_ops = {
  265. .select = hyperv_irq_remapping_select,
  266. .alloc = hyperv_root_irq_remapping_alloc,
  267. .free = hyperv_root_irq_remapping_free,
  268. };
  269. #endif