hyperv-iommu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "irq_remapping.h"
  21. #ifdef CONFIG_IRQ_REMAP
  22. /*
  23. * According 82093AA IO-APIC spec , IO APIC has a 24-entry Interrupt
  24. * Redirection Table. Hyper-V exposes one single IO-APIC and so define
  25. * 24 IO APIC remmapping entries.
  26. */
  27. #define IOAPIC_REMAPPING_ENTRY 24
  28. static cpumask_t ioapic_max_cpumask = { CPU_BITS_NONE };
  29. static struct irq_domain *ioapic_ir_domain;
  30. static int hyperv_ir_set_affinity(struct irq_data *data,
  31. const struct cpumask *mask, bool force)
  32. {
  33. struct irq_data *parent = data->parent_data;
  34. struct irq_cfg *cfg = irqd_cfg(data);
  35. struct IO_APIC_route_entry *entry;
  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. entry = data->chip_data;
  44. entry->dest = cfg->dest_apicid;
  45. entry->vector = cfg->vector;
  46. send_cleanup_vector(cfg);
  47. return 0;
  48. }
  49. static struct irq_chip hyperv_ir_chip = {
  50. .name = "HYPERV-IR",
  51. .irq_ack = apic_ack_irq,
  52. .irq_set_affinity = hyperv_ir_set_affinity,
  53. };
  54. static int hyperv_irq_remapping_alloc(struct irq_domain *domain,
  55. unsigned int virq, unsigned int nr_irqs,
  56. void *arg)
  57. {
  58. struct irq_alloc_info *info = arg;
  59. struct irq_data *irq_data;
  60. struct irq_desc *desc;
  61. int ret = 0;
  62. if (!info || info->type != X86_IRQ_ALLOC_TYPE_IOAPIC || nr_irqs > 1)
  63. return -EINVAL;
  64. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  65. if (ret < 0)
  66. return ret;
  67. irq_data = irq_domain_get_irq_data(domain, virq);
  68. if (!irq_data) {
  69. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  70. return -EINVAL;
  71. }
  72. irq_data->chip = &hyperv_ir_chip;
  73. /*
  74. * If there is interrupt remapping function of IOMMU, setting irq
  75. * affinity only needs to change IRTE of IOMMU. But Hyper-V doesn't
  76. * support interrupt remapping function, setting irq affinity of IO-APIC
  77. * interrupts still needs to change IO-APIC registers. But ioapic_
  78. * configure_entry() will ignore value of cfg->vector and cfg->
  79. * dest_apicid when IO-APIC's parent irq domain is not the vector
  80. * domain.(See ioapic_configure_entry()) In order to setting vector
  81. * and dest_apicid to IO-APIC register, IO-APIC entry pointer is saved
  82. * in the chip_data and hyperv_irq_remapping_activate()/hyperv_ir_set_
  83. * affinity() set vector and dest_apicid directly into IO-APIC entry.
  84. */
  85. irq_data->chip_data = info->ioapic.entry;
  86. /*
  87. * Hypver-V IO APIC irq affinity should be in the scope of
  88. * ioapic_max_cpumask because no irq remapping support.
  89. */
  90. desc = irq_data_to_desc(irq_data);
  91. cpumask_copy(desc->irq_common_data.affinity, &ioapic_max_cpumask);
  92. return 0;
  93. }
  94. static void hyperv_irq_remapping_free(struct irq_domain *domain,
  95. unsigned int virq, unsigned int nr_irqs)
  96. {
  97. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  98. }
  99. static int hyperv_irq_remapping_activate(struct irq_domain *domain,
  100. struct irq_data *irq_data, bool reserve)
  101. {
  102. struct irq_cfg *cfg = irqd_cfg(irq_data);
  103. struct IO_APIC_route_entry *entry = irq_data->chip_data;
  104. entry->dest = cfg->dest_apicid;
  105. entry->vector = cfg->vector;
  106. return 0;
  107. }
  108. static const struct irq_domain_ops hyperv_ir_domain_ops = {
  109. .alloc = hyperv_irq_remapping_alloc,
  110. .free = hyperv_irq_remapping_free,
  111. .activate = hyperv_irq_remapping_activate,
  112. };
  113. static int __init hyperv_prepare_irq_remapping(void)
  114. {
  115. struct fwnode_handle *fn;
  116. int i;
  117. if (!hypervisor_is_type(X86_HYPER_MS_HYPERV) ||
  118. !x2apic_supported())
  119. return -ENODEV;
  120. fn = irq_domain_alloc_named_id_fwnode("HYPERV-IR", 0);
  121. if (!fn)
  122. return -ENOMEM;
  123. ioapic_ir_domain =
  124. irq_domain_create_hierarchy(arch_get_ir_parent_domain(),
  125. 0, IOAPIC_REMAPPING_ENTRY, fn,
  126. &hyperv_ir_domain_ops, NULL);
  127. if (!ioapic_ir_domain) {
  128. irq_domain_free_fwnode(fn);
  129. return -ENOMEM;
  130. }
  131. /*
  132. * Hyper-V doesn't provide irq remapping function for
  133. * IO-APIC and so IO-APIC only accepts 8-bit APIC ID.
  134. * Cpu's APIC ID is read from ACPI MADT table and APIC IDs
  135. * in the MADT table on Hyper-v are sorted monotonic increasingly.
  136. * APIC ID reflects cpu topology. There maybe some APIC ID
  137. * gaps when cpu number in a socket is not power of two. Prepare
  138. * max cpu affinity for IOAPIC irqs. Scan cpu 0-255 and set cpu
  139. * into ioapic_max_cpumask if its APIC ID is less than 256.
  140. */
  141. for (i = min_t(unsigned int, num_possible_cpus() - 1, 255); i >= 0; i--)
  142. if (cpu_physical_id(i) < 256)
  143. cpumask_set_cpu(i, &ioapic_max_cpumask);
  144. return 0;
  145. }
  146. static int __init hyperv_enable_irq_remapping(void)
  147. {
  148. return IRQ_REMAP_X2APIC_MODE;
  149. }
  150. static struct irq_domain *hyperv_get_irq_domain(struct irq_alloc_info *info)
  151. {
  152. if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC_GET_PARENT)
  153. return ioapic_ir_domain;
  154. else
  155. return NULL;
  156. }
  157. struct irq_remap_ops hyperv_irq_remap_ops = {
  158. .prepare = hyperv_prepare_irq_remapping,
  159. .enable = hyperv_enable_irq_remapping,
  160. .get_irq_domain = hyperv_get_irq_domain,
  161. };
  162. #endif