jailhouse.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <asm/apic.h>
  14. #include <asm/cpu.h>
  15. #include <asm/hypervisor.h>
  16. #include <asm/i8259.h>
  17. #include <asm/irqdomain.h>
  18. #include <asm/pci_x86.h>
  19. #include <asm/reboot.h>
  20. #include <asm/setup.h>
  21. static __initdata struct jailhouse_setup_data setup_data;
  22. static unsigned int precalibrated_tsc_khz;
  23. static uint32_t jailhouse_cpuid_base(void)
  24. {
  25. if (boot_cpu_data.cpuid_level < 0 ||
  26. !boot_cpu_has(X86_FEATURE_HYPERVISOR))
  27. return 0;
  28. return hypervisor_cpuid_base("Jailhouse\0\0\0", 0);
  29. }
  30. static uint32_t __init jailhouse_detect(void)
  31. {
  32. return jailhouse_cpuid_base();
  33. }
  34. static void jailhouse_get_wallclock(struct timespec64 *now)
  35. {
  36. memset(now, 0, sizeof(*now));
  37. }
  38. static void __init jailhouse_timer_init(void)
  39. {
  40. lapic_timer_frequency = setup_data.apic_khz * (1000 / HZ);
  41. }
  42. static unsigned long jailhouse_get_tsc(void)
  43. {
  44. return precalibrated_tsc_khz;
  45. }
  46. static void __init jailhouse_x2apic_init(void)
  47. {
  48. #ifdef CONFIG_X86_X2APIC
  49. if (!x2apic_enabled())
  50. return;
  51. /*
  52. * We do not have access to IR inside Jailhouse non-root cells. So
  53. * we have to run in physical mode.
  54. */
  55. x2apic_phys = 1;
  56. /*
  57. * This will trigger the switch to apic_x2apic_phys. Empty OEM IDs
  58. * ensure that only this APIC driver picks up the call.
  59. */
  60. default_acpi_madt_oem_check("", "");
  61. #endif
  62. }
  63. static void __init jailhouse_get_smp_config(unsigned int early)
  64. {
  65. struct ioapic_domain_cfg ioapic_cfg = {
  66. .type = IOAPIC_DOMAIN_STRICT,
  67. .ops = &mp_ioapic_irqdomain_ops,
  68. };
  69. struct mpc_intsrc mp_irq = {
  70. .type = MP_INTSRC,
  71. .irqtype = mp_INT,
  72. .irqflag = MP_IRQPOL_ACTIVE_HIGH | MP_IRQTRIG_EDGE,
  73. };
  74. unsigned int cpu;
  75. jailhouse_x2apic_init();
  76. register_lapic_address(0xfee00000);
  77. for (cpu = 0; cpu < setup_data.num_cpus; cpu++) {
  78. generic_processor_info(setup_data.cpu_ids[cpu],
  79. boot_cpu_apic_version);
  80. }
  81. smp_found_config = 1;
  82. if (setup_data.standard_ioapic) {
  83. mp_register_ioapic(0, 0xfec00000, gsi_top, &ioapic_cfg);
  84. /* Register 1:1 mapping for legacy UART IRQs 3 and 4 */
  85. mp_irq.srcbusirq = mp_irq.dstirq = 3;
  86. mp_save_irq(&mp_irq);
  87. mp_irq.srcbusirq = mp_irq.dstirq = 4;
  88. mp_save_irq(&mp_irq);
  89. }
  90. }
  91. static void jailhouse_no_restart(void)
  92. {
  93. pr_notice("Jailhouse: Restart not supported, halting\n");
  94. machine_halt();
  95. }
  96. static int __init jailhouse_pci_arch_init(void)
  97. {
  98. pci_direct_init(1);
  99. /*
  100. * There are no bridges on the virtual PCI root bus under Jailhouse,
  101. * thus no other way to discover all devices than a full scan.
  102. * Respect any overrides via the command line, though.
  103. */
  104. if (pcibios_last_bus < 0)
  105. pcibios_last_bus = 0xff;
  106. #ifdef CONFIG_PCI_MMCONFIG
  107. if (setup_data.pci_mmconfig_base) {
  108. pci_mmconfig_add(0, 0, pcibios_last_bus,
  109. setup_data.pci_mmconfig_base);
  110. pci_mmcfg_arch_init();
  111. }
  112. #endif
  113. return 0;
  114. }
  115. static void __init jailhouse_init_platform(void)
  116. {
  117. u64 pa_data = boot_params.hdr.setup_data;
  118. struct setup_data header;
  119. void *mapping;
  120. x86_init.irqs.pre_vector_init = x86_init_noop;
  121. x86_init.timers.timer_init = jailhouse_timer_init;
  122. x86_init.mpparse.get_smp_config = jailhouse_get_smp_config;
  123. x86_init.pci.arch_init = jailhouse_pci_arch_init;
  124. x86_platform.calibrate_cpu = jailhouse_get_tsc;
  125. x86_platform.calibrate_tsc = jailhouse_get_tsc;
  126. x86_platform.get_wallclock = jailhouse_get_wallclock;
  127. x86_platform.legacy.rtc = 0;
  128. x86_platform.legacy.warm_reset = 0;
  129. x86_platform.legacy.i8042 = X86_LEGACY_I8042_PLATFORM_ABSENT;
  130. legacy_pic = &null_legacy_pic;
  131. machine_ops.emergency_restart = jailhouse_no_restart;
  132. while (pa_data) {
  133. mapping = early_memremap(pa_data, sizeof(header));
  134. memcpy(&header, mapping, sizeof(header));
  135. early_memunmap(mapping, sizeof(header));
  136. if (header.type == SETUP_JAILHOUSE &&
  137. header.len >= sizeof(setup_data)) {
  138. pa_data += offsetof(struct setup_data, data);
  139. mapping = early_memremap(pa_data, sizeof(setup_data));
  140. memcpy(&setup_data, mapping, sizeof(setup_data));
  141. early_memunmap(mapping, sizeof(setup_data));
  142. break;
  143. }
  144. pa_data = header.next;
  145. }
  146. if (!pa_data)
  147. panic("Jailhouse: No valid setup data found");
  148. if (setup_data.compatible_version > JAILHOUSE_SETUP_REQUIRED_VERSION)
  149. panic("Jailhouse: Unsupported setup data structure");
  150. pmtmr_ioport = setup_data.pm_timer_address;
  151. pr_debug("Jailhouse: PM-Timer IO Port: %#x\n", pmtmr_ioport);
  152. precalibrated_tsc_khz = setup_data.tsc_khz;
  153. setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
  154. pci_probe = 0;
  155. /*
  156. * Avoid that the kernel complains about missing ACPI tables - there
  157. * are none in a non-root cell.
  158. */
  159. disable_acpi();
  160. }
  161. bool jailhouse_paravirt(void)
  162. {
  163. return jailhouse_cpuid_base() != 0;
  164. }
  165. static bool jailhouse_x2apic_available(void)
  166. {
  167. /*
  168. * The x2APIC is only available if the root cell enabled it. Jailhouse
  169. * does not support switching between xAPIC and x2APIC.
  170. */
  171. return x2apic_enabled();
  172. }
  173. const struct hypervisor_x86 x86_hyper_jailhouse __refconst = {
  174. .name = "Jailhouse",
  175. .detect = jailhouse_detect,
  176. .init.init_platform = jailhouse_init_platform,
  177. .init.x2apic_available = jailhouse_x2apic_available,
  178. };