d4f460f962ee35d5202894223cc36732524f051f
[~shefty/rdma-dev.git] / arch / x86 / kernel / tboot.c
1 /*
2  * tboot.c: main implementation of helper functions used by kernel for
3  *          runtime support of Intel(R) Trusted Execution Technology
4  *
5  * Copyright (c) 2006-2009, Intel Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22 #include <linux/dma_remapping.h>
23 #include <linux/init_task.h>
24 #include <linux/spinlock.h>
25 #include <linux/export.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/init.h>
29 #include <linux/dmar.h>
30 #include <linux/cpu.h>
31 #include <linux/pfn.h>
32 #include <linux/mm.h>
33 #include <linux/tboot.h>
34
35 #include <asm/realmode.h>
36 #include <asm/processor.h>
37 #include <asm/bootparam.h>
38 #include <asm/pgtable.h>
39 #include <asm/pgalloc.h>
40 #include <asm/swiotlb.h>
41 #include <asm/fixmap.h>
42 #include <asm/proto.h>
43 #include <asm/setup.h>
44 #include <asm/e820.h>
45 #include <asm/io.h>
46
47 #include "../realmode/rm/wakeup.h"
48
49 /* Global pointer to shared data; NULL means no measured launch. */
50 struct tboot *tboot __read_mostly;
51 EXPORT_SYMBOL(tboot);
52
53 /* timeout for APs (in secs) to enter wait-for-SIPI state during shutdown */
54 #define AP_WAIT_TIMEOUT         1
55
56 #undef pr_fmt
57 #define pr_fmt(fmt)     "tboot: " fmt
58
59 static u8 tboot_uuid[16] __initdata = TBOOT_UUID;
60
61 void __init tboot_probe(void)
62 {
63         /* Look for valid page-aligned address for shared page. */
64         if (!boot_params.tboot_addr)
65                 return;
66         /*
67          * also verify that it is mapped as we expect it before calling
68          * set_fixmap(), to reduce chance of garbage value causing crash
69          */
70         if (!e820_any_mapped(boot_params.tboot_addr,
71                              boot_params.tboot_addr, E820_RESERVED)) {
72                 pr_warning("non-0 tboot_addr but it is not of type E820_RESERVED\n");
73                 return;
74         }
75
76         /* only a natively booted kernel should be using TXT */
77         if (paravirt_enabled()) {
78                 pr_warning("non-0 tboot_addr but pv_ops is enabled\n");
79                 return;
80         }
81
82         /* Map and check for tboot UUID. */
83         set_fixmap(FIX_TBOOT_BASE, boot_params.tboot_addr);
84         tboot = (struct tboot *)fix_to_virt(FIX_TBOOT_BASE);
85         if (memcmp(&tboot_uuid, &tboot->uuid, sizeof(tboot->uuid))) {
86                 pr_warning("tboot at 0x%llx is invalid\n",
87                            boot_params.tboot_addr);
88                 tboot = NULL;
89                 return;
90         }
91         if (tboot->version < 5) {
92                 pr_warning("tboot version is invalid: %u\n", tboot->version);
93                 tboot = NULL;
94                 return;
95         }
96
97         pr_info("found shared page at phys addr 0x%llx:\n",
98                 boot_params.tboot_addr);
99         pr_debug("version: %d\n", tboot->version);
100         pr_debug("log_addr: 0x%08x\n", tboot->log_addr);
101         pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
102         pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
103         pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
104 }
105
106 static inline void switch_to_tboot_pt(void)
107 {
108 #ifdef CONFIG_X86_32
109         load_cr3(initial_page_table);
110 #else
111         write_cr3(real_mode_header->trampoline_pgd);
112 #endif
113 }
114
115 #ifdef CONFIG_ACPI_SLEEP
116
117 static void add_mac_region(phys_addr_t start, unsigned long size)
118 {
119         struct tboot_mac_region *mr;
120         phys_addr_t end = start + size;
121
122         if (tboot->num_mac_regions >= MAX_TB_MAC_REGIONS)
123                 panic("tboot: Too many MAC regions\n");
124
125         if (start && size) {
126                 mr = &tboot->mac_regions[tboot->num_mac_regions++];
127                 mr->start = round_down(start, PAGE_SIZE);
128                 mr->size  = round_up(end, PAGE_SIZE) - mr->start;
129         }
130 }
131
132 static int tboot_setup_sleep(void)
133 {
134         int i;
135
136         tboot->num_mac_regions = 0;
137
138         for (i = 0; i < e820.nr_map; i++) {
139                 if ((e820.map[i].type != E820_RAM)
140                  && (e820.map[i].type != E820_RESERVED_KERN))
141                         continue;
142
143                 add_mac_region(e820.map[i].addr, e820.map[i].size);
144         }
145
146         tboot->acpi_sinfo.kernel_s3_resume_vector =
147                 real_mode_header->wakeup_start;
148
149         return 0;
150 }
151
152 #else /* no CONFIG_ACPI_SLEEP */
153
154 static int tboot_setup_sleep(void)
155 {
156         /* S3 shutdown requested, but S3 not supported by the kernel... */
157         BUG();
158         return -1;
159 }
160
161 #endif
162
163 void tboot_shutdown(u32 shutdown_type)
164 {
165         void (*shutdown)(void);
166
167         if (!tboot_enabled())
168                 return;
169
170         /* if this is S3 then set regions to MAC */
171         if (shutdown_type == TB_SHUTDOWN_S3)
172                 if (tboot_setup_sleep())
173                         return;
174
175         tboot->shutdown_type = shutdown_type;
176
177         switch_to_tboot_pt();
178
179         shutdown = (void(*)(void))(unsigned long)tboot->shutdown_entry;
180         shutdown();
181
182         /* should not reach here */
183         while (1)
184                 halt();
185 }
186
187 static void tboot_copy_fadt(const struct acpi_table_fadt *fadt)
188 {
189 #define TB_COPY_GAS(tbg, g)                     \
190         tbg.space_id     = g.space_id;          \
191         tbg.bit_width    = g.bit_width;         \
192         tbg.bit_offset   = g.bit_offset;        \
193         tbg.access_width = g.access_width;      \
194         tbg.address      = g.address;
195
196         TB_COPY_GAS(tboot->acpi_sinfo.pm1a_cnt_blk, fadt->xpm1a_control_block);
197         TB_COPY_GAS(tboot->acpi_sinfo.pm1b_cnt_blk, fadt->xpm1b_control_block);
198         TB_COPY_GAS(tboot->acpi_sinfo.pm1a_evt_blk, fadt->xpm1a_event_block);
199         TB_COPY_GAS(tboot->acpi_sinfo.pm1b_evt_blk, fadt->xpm1b_event_block);
200
201         /*
202          * We need phys addr of waking vector, but can't use virt_to_phys() on
203          * &acpi_gbl_FACS because it is ioremap'ed, so calc from FACS phys
204          * addr.
205          */
206         tboot->acpi_sinfo.wakeup_vector = fadt->facs +
207                 offsetof(struct acpi_table_facs, firmware_waking_vector);
208 }
209
210 static int tboot_sleep(u8 sleep_state, u32 pm1a_control, u32 pm1b_control)
211 {
212         static u32 acpi_shutdown_map[ACPI_S_STATE_COUNT] = {
213                 /* S0,1,2: */ -1, -1, -1,
214                 /* S3: */ TB_SHUTDOWN_S3,
215                 /* S4: */ TB_SHUTDOWN_S4,
216                 /* S5: */ TB_SHUTDOWN_S5 };
217
218         if (!tboot_enabled())
219                 return 0;
220
221         tboot_copy_fadt(&acpi_gbl_FADT);
222         tboot->acpi_sinfo.pm1a_cnt_val = pm1a_control;
223         tboot->acpi_sinfo.pm1b_cnt_val = pm1b_control;
224         /* we always use the 32b wakeup vector */
225         tboot->acpi_sinfo.vector_width = 32;
226
227         if (sleep_state >= ACPI_S_STATE_COUNT ||
228             acpi_shutdown_map[sleep_state] == -1) {
229                 pr_warning("unsupported sleep state 0x%x\n", sleep_state);
230                 return -1;
231         }
232
233         tboot_shutdown(acpi_shutdown_map[sleep_state]);
234         return 0;
235 }
236
237 static atomic_t ap_wfs_count;
238
239 static int tboot_wait_for_aps(int num_aps)
240 {
241         unsigned long timeout;
242
243         timeout = AP_WAIT_TIMEOUT*HZ;
244         while (atomic_read((atomic_t *)&tboot->num_in_wfs) != num_aps &&
245                timeout) {
246                 mdelay(1);
247                 timeout--;
248         }
249
250         if (timeout)
251                 pr_warning("tboot wait for APs timeout\n");
252
253         return !(atomic_read((atomic_t *)&tboot->num_in_wfs) == num_aps);
254 }
255
256 static int __cpuinit tboot_cpu_callback(struct notifier_block *nfb,
257                         unsigned long action, void *hcpu)
258 {
259         switch (action) {
260         case CPU_DYING:
261                 atomic_inc(&ap_wfs_count);
262                 if (num_online_cpus() == 1)
263                         if (tboot_wait_for_aps(atomic_read(&ap_wfs_count)))
264                                 return NOTIFY_BAD;
265                 break;
266         }
267         return NOTIFY_OK;
268 }
269
270 static struct notifier_block tboot_cpu_notifier __cpuinitdata =
271 {
272         .notifier_call = tboot_cpu_callback,
273 };
274
275 static __init int tboot_late_init(void)
276 {
277         if (!tboot_enabled())
278                 return 0;
279
280         atomic_set(&ap_wfs_count, 0);
281         register_hotcpu_notifier(&tboot_cpu_notifier);
282
283         acpi_os_set_prepare_sleep(&tboot_sleep);
284         return 0;
285 }
286
287 late_initcall(tboot_late_init);
288
289 /*
290  * TXT configuration registers (offsets from TXT_{PUB, PRIV}_CONFIG_REGS_BASE)
291  */
292
293 #define TXT_PUB_CONFIG_REGS_BASE       0xfed30000
294 #define TXT_PRIV_CONFIG_REGS_BASE      0xfed20000
295
296 /* # pages for each config regs space - used by fixmap */
297 #define NR_TXT_CONFIG_PAGES     ((TXT_PUB_CONFIG_REGS_BASE -                \
298                                   TXT_PRIV_CONFIG_REGS_BASE) >> PAGE_SHIFT)
299
300 /* offsets from pub/priv config space */
301 #define TXTCR_HEAP_BASE             0x0300
302 #define TXTCR_HEAP_SIZE             0x0308
303
304 #define SHA1_SIZE      20
305
306 struct sha1_hash {
307         u8 hash[SHA1_SIZE];
308 };
309
310 struct sinit_mle_data {
311         u32               version;             /* currently 6 */
312         struct sha1_hash  bios_acm_id;
313         u32               edx_senter_flags;
314         u64               mseg_valid;
315         struct sha1_hash  sinit_hash;
316         struct sha1_hash  mle_hash;
317         struct sha1_hash  stm_hash;
318         struct sha1_hash  lcp_policy_hash;
319         u32               lcp_policy_control;
320         u32               rlp_wakeup_addr;
321         u32               reserved;
322         u32               num_mdrs;
323         u32               mdrs_off;
324         u32               num_vtd_dmars;
325         u32               vtd_dmars_off;
326 } __packed;
327
328 struct acpi_table_header *tboot_get_dmar_table(struct acpi_table_header *dmar_tbl)
329 {
330         void *heap_base, *heap_ptr, *config;
331
332         if (!tboot_enabled())
333                 return dmar_tbl;
334
335         /*
336          * ACPI tables may not be DMA protected by tboot, so use DMAR copy
337          * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
338          */
339
340         /* map config space in order to get heap addr */
341         config = ioremap(TXT_PUB_CONFIG_REGS_BASE, NR_TXT_CONFIG_PAGES *
342                          PAGE_SIZE);
343         if (!config)
344                 return NULL;
345
346         /* now map TXT heap */
347         heap_base = ioremap(*(u64 *)(config + TXTCR_HEAP_BASE),
348                             *(u64 *)(config + TXTCR_HEAP_SIZE));
349         iounmap(config);
350         if (!heap_base)
351                 return NULL;
352
353         /* walk heap to SinitMleData */
354         /* skip BiosData */
355         heap_ptr = heap_base + *(u64 *)heap_base;
356         /* skip OsMleData */
357         heap_ptr += *(u64 *)heap_ptr;
358         /* skip OsSinitData */
359         heap_ptr += *(u64 *)heap_ptr;
360         /* now points to SinitMleDataSize; set to SinitMleData */
361         heap_ptr += sizeof(u64);
362         /* get addr of DMAR table */
363         dmar_tbl = (struct acpi_table_header *)(heap_ptr +
364                    ((struct sinit_mle_data *)heap_ptr)->vtd_dmars_off -
365                    sizeof(u64));
366
367         /* don't unmap heap because dmar.c needs access to this */
368
369         return dmar_tbl;
370 }
371
372 int tboot_force_iommu(void)
373 {
374         if (!tboot_enabled())
375                 return 0;
376
377         if (no_iommu || swiotlb || dmar_disabled)
378                 pr_warning("Forcing Intel-IOMMU to enabled\n");
379
380         dmar_disabled = 0;
381 #ifdef CONFIG_SWIOTLB
382         swiotlb = 0;
383 #endif
384         no_iommu = 0;
385
386         return 1;
387 }