jailhouse.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Jailhouse paravirt_ops implementation
  4. *
  5. * Copyright (c) Siemens AG, 2015-2017
  6. *
  7. * Authors:
  8. * Jan Kiszka <jan.kiszka@siemens.com>
  9. */
  10. #include <linux/acpi_pmtmr.h>
  11. #include <linux/kernel.h>
  12. #include <linux/reboot.h>
  13. #include <linux/serial_8250.h>
  14. #include <linux/acpi.h>
  15. #include <asm/apic.h>
  16. #include <asm/io_apic.h>
  17. #include <asm/acpi.h>
  18. #include <asm/cpu.h>
  19. #include <asm/hypervisor.h>
  20. #include <asm/i8259.h>
  21. #include <asm/irqdomain.h>
  22. #include <asm/pci_x86.h>
  23. #include <asm/reboot.h>
  24. #include <asm/setup.h>
  25. #include <asm/jailhouse_para.h>
  26. static struct jailhouse_setup_data setup_data;
  27. #define SETUP_DATA_V1_LEN (sizeof(setup_data.hdr) + sizeof(setup_data.v1))
  28. #define SETUP_DATA_V2_LEN (SETUP_DATA_V1_LEN + sizeof(setup_data.v2))
  29. static unsigned int precalibrated_tsc_khz;
  30. static void jailhouse_setup_irq(unsigned int irq)
  31. {
  32. struct mpc_intsrc mp_irq = {
  33. .type = MP_INTSRC,
  34. .irqtype = mp_INT,
  35. .irqflag = MP_IRQPOL_ACTIVE_HIGH | MP_IRQTRIG_EDGE,
  36. .srcbusirq = irq,
  37. .dstirq = irq,
  38. };
  39. mp_save_irq(&mp_irq);
  40. }
  41. static uint32_t jailhouse_cpuid_base(void)
  42. {
  43. if (boot_cpu_data.cpuid_level < 0 ||
  44. !boot_cpu_has(X86_FEATURE_HYPERVISOR))
  45. return 0;
  46. return hypervisor_cpuid_base("Jailhouse\0\0\0", 0);
  47. }
  48. static uint32_t __init jailhouse_detect(void)
  49. {
  50. return jailhouse_cpuid_base();
  51. }
  52. static void jailhouse_get_wallclock(struct timespec64 *now)
  53. {
  54. memset(now, 0, sizeof(*now));
  55. }
  56. static void __init jailhouse_timer_init(void)
  57. {
  58. lapic_timer_period = setup_data.v1.apic_khz * (1000 / HZ);
  59. }
  60. static unsigned long jailhouse_get_tsc(void)
  61. {
  62. return precalibrated_tsc_khz;
  63. }
  64. static void __init jailhouse_x2apic_init(void)
  65. {
  66. #ifdef CONFIG_X86_X2APIC
  67. if (!x2apic_enabled())
  68. return;
  69. /*
  70. * We do not have access to IR inside Jailhouse non-root cells. So
  71. * we have to run in physical mode.
  72. */
  73. x2apic_phys = 1;
  74. /*
  75. * This will trigger the switch to apic_x2apic_phys. Empty OEM IDs
  76. * ensure that only this APIC driver picks up the call.
  77. */
  78. default_acpi_madt_oem_check("", "");
  79. #endif
  80. }
  81. static void __init jailhouse_parse_smp_config(void)
  82. {
  83. struct ioapic_domain_cfg ioapic_cfg = {
  84. .type = IOAPIC_DOMAIN_STRICT,
  85. .ops = &mp_ioapic_irqdomain_ops,
  86. };
  87. unsigned int cpu;
  88. jailhouse_x2apic_init();
  89. register_lapic_address(0xfee00000);
  90. for (cpu = 0; cpu < setup_data.v1.num_cpus; cpu++)
  91. topology_register_apic(setup_data.v1.cpu_ids[cpu], CPU_ACPIID_INVALID, true);
  92. smp_found_config = 1;
  93. if (setup_data.v1.standard_ioapic) {
  94. mp_register_ioapic(0, 0xfec00000, gsi_top, &ioapic_cfg);
  95. if (IS_ENABLED(CONFIG_SERIAL_8250) &&
  96. setup_data.hdr.version < 2) {
  97. /* Register 1:1 mapping for legacy UART IRQs 3 and 4 */
  98. jailhouse_setup_irq(3);
  99. jailhouse_setup_irq(4);
  100. }
  101. }
  102. }
  103. static void jailhouse_no_restart(void)
  104. {
  105. pr_notice("Jailhouse: Restart not supported, halting\n");
  106. machine_halt();
  107. }
  108. static int __init jailhouse_pci_arch_init(void)
  109. {
  110. pci_direct_init(1);
  111. /*
  112. * There are no bridges on the virtual PCI root bus under Jailhouse,
  113. * thus no other way to discover all devices than a full scan.
  114. * Respect any overrides via the command line, though.
  115. */
  116. if (pcibios_last_bus < 0)
  117. pcibios_last_bus = 0xff;
  118. #ifdef CONFIG_PCI_MMCONFIG
  119. if (setup_data.v1.pci_mmconfig_base) {
  120. pci_mmconfig_add(0, 0, pcibios_last_bus,
  121. setup_data.v1.pci_mmconfig_base);
  122. pci_mmcfg_arch_init();
  123. }
  124. #endif
  125. return 0;
  126. }
  127. #ifdef CONFIG_SERIAL_8250
  128. static inline bool jailhouse_uart_enabled(unsigned int uart_nr)
  129. {
  130. return setup_data.v2.flags & BIT(uart_nr);
  131. }
  132. static void jailhouse_serial_fixup(int port, struct uart_port *up,
  133. u32 *capabilities)
  134. {
  135. static const u16 pcuart_base[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8};
  136. unsigned int n;
  137. for (n = 0; n < ARRAY_SIZE(pcuart_base); n++) {
  138. if (pcuart_base[n] != up->iobase)
  139. continue;
  140. if (jailhouse_uart_enabled(n)) {
  141. pr_info("Enabling UART%u (port 0x%lx)\n", n,
  142. up->iobase);
  143. jailhouse_setup_irq(up->irq);
  144. } else {
  145. /* Deactivate UART if access isn't allowed */
  146. up->iobase = 0;
  147. }
  148. break;
  149. }
  150. }
  151. static void __init jailhouse_serial_workaround(void)
  152. {
  153. /*
  154. * There are flags inside setup_data that indicate availability of
  155. * platform UARTs since setup data version 2.
  156. *
  157. * In case of version 1, we don't know which UARTs belong Linux. In
  158. * this case, unconditionally register 1:1 mapping for legacy UART IRQs
  159. * 3 and 4.
  160. */
  161. if (setup_data.hdr.version > 1)
  162. serial8250_set_isa_configurator(jailhouse_serial_fixup);
  163. }
  164. #else /* !CONFIG_SERIAL_8250 */
  165. static inline void jailhouse_serial_workaround(void)
  166. {
  167. }
  168. #endif /* CONFIG_SERIAL_8250 */
  169. static void __init jailhouse_init_platform(void)
  170. {
  171. u64 pa_data = boot_params.hdr.setup_data;
  172. unsigned long setup_data_len;
  173. struct setup_data header;
  174. void *mapping;
  175. x86_init.irqs.pre_vector_init = x86_init_noop;
  176. x86_init.timers.timer_init = jailhouse_timer_init;
  177. x86_init.mpparse.find_mptable = x86_init_noop;
  178. x86_init.mpparse.early_parse_smp_cfg = x86_init_noop;
  179. x86_init.mpparse.parse_smp_cfg = jailhouse_parse_smp_config;
  180. x86_init.pci.arch_init = jailhouse_pci_arch_init;
  181. x86_platform.calibrate_cpu = jailhouse_get_tsc;
  182. x86_platform.calibrate_tsc = jailhouse_get_tsc;
  183. x86_platform.get_wallclock = jailhouse_get_wallclock;
  184. x86_platform.legacy.rtc = 0;
  185. x86_platform.legacy.warm_reset = 0;
  186. x86_platform.legacy.i8042 = X86_LEGACY_I8042_PLATFORM_ABSENT;
  187. legacy_pic = &null_legacy_pic;
  188. machine_ops.emergency_restart = jailhouse_no_restart;
  189. while (pa_data) {
  190. mapping = early_memremap(pa_data, sizeof(header));
  191. memcpy(&header, mapping, sizeof(header));
  192. early_memunmap(mapping, sizeof(header));
  193. if (header.type == SETUP_JAILHOUSE)
  194. break;
  195. pa_data = header.next;
  196. }
  197. if (!pa_data)
  198. panic("Jailhouse: No valid setup data found");
  199. /* setup data must at least contain the header */
  200. if (header.len < sizeof(setup_data.hdr))
  201. goto unsupported;
  202. pa_data += offsetof(struct setup_data, data);
  203. setup_data_len = min_t(unsigned long, sizeof(setup_data),
  204. (unsigned long)header.len);
  205. mapping = early_memremap(pa_data, setup_data_len);
  206. memcpy(&setup_data, mapping, setup_data_len);
  207. early_memunmap(mapping, setup_data_len);
  208. if (setup_data.hdr.version == 0 ||
  209. setup_data.hdr.compatible_version !=
  210. JAILHOUSE_SETUP_REQUIRED_VERSION ||
  211. (setup_data.hdr.version == 1 && header.len < SETUP_DATA_V1_LEN) ||
  212. (setup_data.hdr.version >= 2 && header.len < SETUP_DATA_V2_LEN))
  213. goto unsupported;
  214. pmtmr_ioport = setup_data.v1.pm_timer_address;
  215. pr_debug("Jailhouse: PM-Timer IO Port: %#x\n", pmtmr_ioport);
  216. precalibrated_tsc_khz = setup_data.v1.tsc_khz;
  217. setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
  218. pci_probe = 0;
  219. /*
  220. * Avoid that the kernel complains about missing ACPI tables - there
  221. * are none in a non-root cell.
  222. */
  223. disable_acpi();
  224. jailhouse_serial_workaround();
  225. return;
  226. unsupported:
  227. panic("Jailhouse: Unsupported setup data structure");
  228. }
  229. bool jailhouse_paravirt(void)
  230. {
  231. return jailhouse_cpuid_base() != 0;
  232. }
  233. static bool __init jailhouse_x2apic_available(void)
  234. {
  235. /*
  236. * The x2APIC is only available if the root cell enabled it. Jailhouse
  237. * does not support switching between xAPIC and x2APIC.
  238. */
  239. return x2apic_enabled();
  240. }
  241. const struct hypervisor_x86 x86_hyper_jailhouse __refconst = {
  242. .name = "Jailhouse",
  243. .detect = jailhouse_detect,
  244. .init.init_platform = jailhouse_init_platform,
  245. .init.x2apic_available = jailhouse_x2apic_available,
  246. .ignore_nopv = true,
  247. };