Merge branch 'master' of ssh://master.kernel.org/pub/scm/linux/kernel/git/rusty/linux...
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / tboot.c
index 263591afd29eaa12c8f191bedab150656f47ef1c..86c9f91b48aea8ef47f0e77372fcf2d5f822d77e 100644 (file)
 #include <linux/dma_remapping.h>
 #include <linux/init_task.h>
 #include <linux/spinlock.h>
+#include <linux/delay.h>
 #include <linux/sched.h>
 #include <linux/init.h>
 #include <linux/dmar.h>
+#include <linux/cpu.h>
 #include <linux/pfn.h>
 #include <linux/mm.h>
+#include <linux/tboot.h>
 
 #include <asm/trampoline.h>
 #include <asm/processor.h>
 #include <asm/bootparam.h>
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
+#include <asm/fixmap.h>
+#include <asm/proto.h>
 #include <asm/setup.h>
-#include <asm/tboot.h>
 #include <asm/e820.h>
 #include <asm/io.h>
 
@@ -152,36 +156,63 @@ static int map_tboot_pages(unsigned long vaddr, unsigned long start_pfn,
        return 0;
 }
 
-void tboot_create_trampoline(void)
+static void tboot_create_trampoline(void)
 {
        u32 map_base, map_size;
 
-       if (!tboot_enabled())
-               return;
-
        /* Create identity map for tboot shutdown code. */
        map_base = PFN_DOWN(tboot->tboot_base);
        map_size = PFN_UP(tboot->tboot_size);
        if (map_tboot_pages(map_base << PAGE_SHIFT, map_base, map_size))
-               panic("tboot: Error mapping tboot pages (mfns) @ 0x%x, 0x%x\n", map_base, map_size);
+               panic("tboot: Error mapping tboot pages (mfns) @ 0x%x, 0x%x\n",
+                     map_base, map_size);
 }
 
-static void set_mac_regions(void)
+#ifdef CONFIG_ACPI_SLEEP
+
+static void add_mac_region(phys_addr_t start, unsigned long size)
 {
-       tboot->num_mac_regions = 3;
+       struct tboot_mac_region *mr;
+       phys_addr_t end = start + size;
+
+       if (start && size) {
+               mr = &tboot->mac_regions[tboot->num_mac_regions++];
+               mr->start = round_down(start, PAGE_SIZE);
+               mr->size  = round_up(end, PAGE_SIZE) - mr->start;
+       }
+}
+
+static int tboot_setup_sleep(void)
+{
+       tboot->num_mac_regions = 0;
+
        /* S3 resume code */
-       tboot->mac_regions[0].start = PFN_PHYS(PFN_DOWN(acpi_wakeup_address));
-       tboot->mac_regions[0].size = PFN_UP(WAKEUP_SIZE) << PAGE_SHIFT;
+       add_mac_region(acpi_wakeup_address, WAKEUP_SIZE);
+
+#ifdef CONFIG_X86_TRAMPOLINE
        /* AP trampoline code */
-       tboot->mac_regions[1].start =
-                       PFN_PHYS(PFN_DOWN(virt_to_phys(trampoline_base)));
-       tboot->mac_regions[1].size = PFN_UP(TRAMPOLINE_SIZE) << PAGE_SHIFT;
+       add_mac_region(virt_to_phys(trampoline_base), TRAMPOLINE_SIZE);
+#endif
+
        /* kernel code + data + bss */
-       tboot->mac_regions[2].start = PFN_PHYS(PFN_DOWN(virt_to_phys(&_text)));
-       tboot->mac_regions[2].size = PFN_PHYS(PFN_UP(virt_to_phys(&_end))) -
-                                    PFN_PHYS(PFN_DOWN(virt_to_phys(&_text)));
+       add_mac_region(virt_to_phys(_text), _end - _text);
+
+       tboot->acpi_sinfo.kernel_s3_resume_vector = acpi_wakeup_address;
+
+       return 0;
+}
+
+#else /* no CONFIG_ACPI_SLEEP */
+
+static int tboot_setup_sleep(void)
+{
+       /* S3 shutdown requested, but S3 not supported by the kernel... */
+       BUG();
+       return -1;
 }
 
+#endif
+
 void tboot_shutdown(u32 shutdown_type)
 {
        void (*shutdown)(void);
@@ -199,7 +230,8 @@ void tboot_shutdown(u32 shutdown_type)
 
        /* if this is S3 then set regions to MAC */
        if (shutdown_type == TB_SHUTDOWN_S3)
-               set_mac_regions();
+               if (tboot_setup_sleep())
+                       return;
 
        tboot->shutdown_type = shutdown_type;
 
@@ -252,7 +284,6 @@ void tboot_sleep(u8 sleep_state, u32 pm1a_control, u32 pm1b_control)
        tboot->acpi_sinfo.pm1b_cnt_val = pm1b_control;
        /* we always use the 32b wakeup vector */
        tboot->acpi_sinfo.vector_width = 32;
-       tboot->acpi_sinfo.kernel_s3_resume_vector = acpi_wakeup_address;
 
        if (sleep_state >= ACPI_S_STATE_COUNT ||
            acpi_shutdown_map[sleep_state] == -1) {
@@ -263,21 +294,58 @@ void tboot_sleep(u8 sleep_state, u32 pm1a_control, u32 pm1b_control)
        tboot_shutdown(acpi_shutdown_map[sleep_state]);
 }
 
-int tboot_wait_for_aps(int num_aps)
+static atomic_t ap_wfs_count;
+
+static int tboot_wait_for_aps(int num_aps)
 {
        unsigned long timeout;
 
+       timeout = AP_WAIT_TIMEOUT*HZ;
+       while (atomic_read((atomic_t *)&tboot->num_in_wfs) != num_aps &&
+              timeout) {
+               mdelay(1);
+               timeout--;
+       }
+
+       if (timeout)
+               pr_warning("tboot wait for APs timeout\n");
+
+       return !(atomic_read((atomic_t *)&tboot->num_in_wfs) == num_aps);
+}
+
+static int __cpuinit tboot_cpu_callback(struct notifier_block *nfb,
+                       unsigned long action, void *hcpu)
+{
+       switch (action) {
+       case CPU_DYING:
+               atomic_inc(&ap_wfs_count);
+               if (num_online_cpus() == 1)
+                       if (tboot_wait_for_aps(atomic_read(&ap_wfs_count)))
+                               return NOTIFY_BAD;
+               break;
+       }
+       return NOTIFY_OK;
+}
+
+static struct notifier_block tboot_cpu_notifier __cpuinitdata =
+{
+       .notifier_call = tboot_cpu_callback,
+};
+
+static __init int tboot_late_init(void)
+{
        if (!tboot_enabled())
                return 0;
 
-       timeout = jiffies + AP_WAIT_TIMEOUT*HZ;
-       while (atomic_read((atomic_t *)&tboot->num_in_wfs) != num_aps &&
-              time_before(jiffies, timeout))
-               cpu_relax();
+       tboot_create_trampoline();
 
-       return time_before(jiffies, timeout) ? 0 : 1;
+       atomic_set(&ap_wfs_count, 0);
+       register_hotcpu_notifier(&tboot_cpu_notifier);
+       return 0;
 }
 
+late_initcall(tboot_late_init);
+
 /*
  * TXT configuration registers (offsets from TXT_{PUB, PRIV}_CONFIG_REGS_BASE)
  */