hyperv-iommu.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <Tianyu.Lan@microsoft.com>
  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. vector_schedule_cleanup(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. /*
  101. * For a Hyper-V root partition, ms_hyperv_msi_ext_dest_id()
  102. * will always return false.
  103. */
  104. if (!hypervisor_is_type(X86_HYPER_MS_HYPERV) ||
  105. x86_init.hyper.msi_ext_dest_id())
  106. return -ENODEV;
  107. if (hv_root_partition) {
  108. name = "HYPERV-ROOT-IR";
  109. ops = &hyperv_root_ir_domain_ops;
  110. } else {
  111. name = "HYPERV-IR";
  112. ops = &hyperv_ir_domain_ops;
  113. }
  114. fn = irq_domain_alloc_named_id_fwnode(name, 0);
  115. if (!fn)
  116. return -ENOMEM;
  117. ioapic_ir_domain =
  118. irq_domain_create_hierarchy(arch_get_ir_parent_domain(),
  119. 0, IOAPIC_REMAPPING_ENTRY, fn, ops, NULL);
  120. if (!ioapic_ir_domain) {
  121. irq_domain_free_fwnode(fn);
  122. return -ENOMEM;
  123. }
  124. if (hv_root_partition)
  125. return 0; /* The rest is only relevant to guests */
  126. /*
  127. * Hyper-V doesn't provide irq remapping function for
  128. * IO-APIC and so IO-APIC only accepts 8-bit APIC ID.
  129. * Cpu's APIC ID is read from ACPI MADT table and APIC IDs
  130. * in the MADT table on Hyper-v are sorted monotonic increasingly.
  131. * APIC ID reflects cpu topology. There maybe some APIC ID
  132. * gaps when cpu number in a socket is not power of two. Prepare
  133. * max cpu affinity for IOAPIC irqs. Scan cpu 0-255 and set cpu
  134. * into ioapic_max_cpumask if its APIC ID is less than 256.
  135. */
  136. for (i = min_t(unsigned int, num_possible_cpus() - 1, 255); i >= 0; i--)
  137. if (cpu_physical_id(i) < 256)
  138. cpumask_set_cpu(i, &ioapic_max_cpumask);
  139. return 0;
  140. }
  141. static int __init hyperv_enable_irq_remapping(void)
  142. {
  143. if (x2apic_supported())
  144. return IRQ_REMAP_X2APIC_MODE;
  145. return IRQ_REMAP_XAPIC_MODE;
  146. }
  147. struct irq_remap_ops hyperv_irq_remap_ops = {
  148. .prepare = hyperv_prepare_irq_remapping,
  149. .enable = hyperv_enable_irq_remapping,
  150. };
  151. /* IRQ remapping domain when Linux runs as the root partition */
  152. struct hyperv_root_ir_data {
  153. u8 ioapic_id;
  154. bool is_level;
  155. struct hv_interrupt_entry entry;
  156. };
  157. static void
  158. hyperv_root_ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
  159. {
  160. u64 status;
  161. u32 vector;
  162. struct irq_cfg *cfg;
  163. int ioapic_id;
  164. const struct cpumask *affinity;
  165. int cpu;
  166. struct hv_interrupt_entry entry;
  167. struct hyperv_root_ir_data *data = irq_data->chip_data;
  168. struct IO_APIC_route_entry e;
  169. cfg = irqd_cfg(irq_data);
  170. affinity = irq_data_get_effective_affinity_mask(irq_data);
  171. cpu = cpumask_first_and(affinity, cpu_online_mask);
  172. vector = cfg->vector;
  173. ioapic_id = data->ioapic_id;
  174. if (data->entry.source == HV_DEVICE_TYPE_IOAPIC
  175. && data->entry.ioapic_rte.as_uint64) {
  176. entry = data->entry;
  177. status = hv_unmap_ioapic_interrupt(ioapic_id, &entry);
  178. if (status != HV_STATUS_SUCCESS)
  179. pr_debug("%s: unexpected unmap status %lld\n", __func__, status);
  180. data->entry.ioapic_rte.as_uint64 = 0;
  181. data->entry.source = 0; /* Invalid source */
  182. }
  183. status = hv_map_ioapic_interrupt(ioapic_id, data->is_level, cpu,
  184. vector, &entry);
  185. if (status != HV_STATUS_SUCCESS) {
  186. pr_err("%s: map hypercall failed, status %lld\n", __func__, status);
  187. return;
  188. }
  189. data->entry = entry;
  190. /* Turn it into an IO_APIC_route_entry, and generate MSI MSG. */
  191. e.w1 = entry.ioapic_rte.low_uint32;
  192. e.w2 = entry.ioapic_rte.high_uint32;
  193. memset(msg, 0, sizeof(*msg));
  194. msg->arch_data.vector = e.vector;
  195. msg->arch_data.delivery_mode = e.delivery_mode;
  196. msg->arch_addr_lo.dest_mode_logical = e.dest_mode_logical;
  197. msg->arch_addr_lo.dmar_format = e.ir_format;
  198. msg->arch_addr_lo.dmar_index_0_14 = e.ir_index_0_14;
  199. }
  200. static int hyperv_root_ir_set_affinity(struct irq_data *data,
  201. const struct cpumask *mask, bool force)
  202. {
  203. struct irq_data *parent = data->parent_data;
  204. struct irq_cfg *cfg = irqd_cfg(data);
  205. int ret;
  206. ret = parent->chip->irq_set_affinity(parent, mask, force);
  207. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  208. return ret;
  209. vector_schedule_cleanup(cfg);
  210. return 0;
  211. }
  212. static struct irq_chip hyperv_root_ir_chip = {
  213. .name = "HYPERV-ROOT-IR",
  214. .irq_ack = apic_ack_irq,
  215. .irq_set_affinity = hyperv_root_ir_set_affinity,
  216. .irq_compose_msi_msg = hyperv_root_ir_compose_msi_msg,
  217. };
  218. static int hyperv_root_irq_remapping_alloc(struct irq_domain *domain,
  219. unsigned int virq, unsigned int nr_irqs,
  220. void *arg)
  221. {
  222. struct irq_alloc_info *info = arg;
  223. struct irq_data *irq_data;
  224. struct hyperv_root_ir_data *data;
  225. int ret = 0;
  226. if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
  227. return -EINVAL;
  228. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  229. if (ret < 0)
  230. return ret;
  231. data = kzalloc(sizeof(*data), GFP_KERNEL);
  232. if (!data) {
  233. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  234. return -ENOMEM;
  235. }
  236. irq_data = irq_domain_get_irq_data(domain, virq);
  237. if (!irq_data) {
  238. kfree(data);
  239. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  240. return -EINVAL;
  241. }
  242. data->ioapic_id = info->devid;
  243. data->is_level = info->ioapic.is_level;
  244. irq_data->chip = &hyperv_root_ir_chip;
  245. irq_data->chip_data = data;
  246. return 0;
  247. }
  248. static void hyperv_root_irq_remapping_free(struct irq_domain *domain,
  249. unsigned int virq, unsigned int nr_irqs)
  250. {
  251. struct irq_data *irq_data;
  252. struct hyperv_root_ir_data *data;
  253. struct hv_interrupt_entry *e;
  254. int i;
  255. for (i = 0; i < nr_irqs; i++) {
  256. irq_data = irq_domain_get_irq_data(domain, virq + i);
  257. if (irq_data && irq_data->chip_data) {
  258. data = irq_data->chip_data;
  259. e = &data->entry;
  260. if (e->source == HV_DEVICE_TYPE_IOAPIC
  261. && e->ioapic_rte.as_uint64)
  262. hv_unmap_ioapic_interrupt(data->ioapic_id,
  263. &data->entry);
  264. kfree(data);
  265. }
  266. }
  267. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  268. }
  269. static const struct irq_domain_ops hyperv_root_ir_domain_ops = {
  270. .select = hyperv_irq_remapping_select,
  271. .alloc = hyperv_root_irq_remapping_alloc,
  272. .free = hyperv_root_irq_remapping_free,
  273. };
  274. #endif