amd_early.c 9.0 KB
Newer Older
1 2 3 4
/*
 * Copyright (C) 2013 Advanced Micro Devices, Inc.
 *
 * Author: Jacob Shin <jacob.shin@amd.com>
5
 * Fixes: Borislav Petkov <bp@suse.de>
6 7 8 9 10 11 12
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/earlycpio.h>
13
#include <linux/initrd.h>
14 15 16 17 18

#include <asm/cpu.h>
#include <asm/setup.h>
#include <asm/microcode_amd.h>

19 20 21 22 23 24 25
/*
 * This points to the current valid container of microcode patches which we will
 * save from the initrd before jettisoning its contents.
 */
static u8 *container;
static size_t container_size;

26
static u32 ucode_new_rev;
27 28 29
u8 amd_ucode_patch[PATCH_MAX_SIZE];
static u16 this_equiv_id;

30
static struct cpio_data ucode_cpio;
31 32 33 34 35

/*
 * Microcode patch container file is prepended to the initrd in cpio format.
 * See Documentation/x86/early-microcode.txt
 */
36
static __initdata char ucode_path[] = "kernel/x86/microcode/AuthenticAMD.bin";
37

38
static struct cpio_data __init find_ucode_in_initrd(void)
39 40
{
	long offset = 0;
41 42 43
	char *path;
	void *start;
	size_t size;
44 45

#ifdef CONFIG_X86_32
46 47
	struct boot_params *p;

48 49 50 51
	/*
	 * On 32-bit, early load occurs before paging is turned on so we need
	 * to use physical addresses.
	 */
52 53 54 55 56 57 58 59
	p       = (struct boot_params *)__pa_nodebug(&boot_params);
	path    = (char *)__pa_nodebug(ucode_path);
	start   = (void *)p->hdr.ramdisk_image;
	size    = p->hdr.ramdisk_size;
#else
	path    = ucode_path;
	start   = (void *)(boot_params.hdr.ramdisk_image + PAGE_OFFSET);
	size    = boot_params.hdr.ramdisk_size;
60
#endif
61

62 63
	return find_cpio_data(path, start, size, &offset);
}
64

65 66 67 68
static size_t compute_container_size(u8 *data, u32 total_size)
{
	size_t size = 0;
	u32 *header = (u32 *)data;
69

70 71 72 73
	if (header[0] != UCODE_MAGIC ||
	    header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
	    header[2] == 0)                            /* size */
		return size;
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	size = header[2] + CONTAINER_HDR_SZ;
	total_size -= size;
	data += size;

	while (total_size) {
		u16 patch_size;

		header = (u32 *)data;

		if (header[0] != UCODE_UCODE_TYPE)
			break;

		/*
		 * Sanity-check patch size.
		 */
		patch_size = header[1];
		if (patch_size > PATCH_MAX_SIZE)
			break;

		size	   += patch_size + SECTION_HDR_SIZE;
		data	   += patch_size + SECTION_HDR_SIZE;
		total_size -= patch_size + SECTION_HDR_SIZE;
	}

	return size;
100 101 102 103 104 105 106 107 108 109 110
}

/*
 * Early load occurs before we can vmalloc(). So we look for the microcode
 * patch container file in initrd, traverse equivalent cpu table, look for a
 * matching microcode patch, and update, all in initrd memory in place.
 * When vmalloc() is available for use later -- on 64-bit during first AP load,
 * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
 * load_microcode_amd() to save equivalent cpu table and microcode patches in
 * kernel heap memory.
 */
111
static void apply_ucode_in_initrd(void *ucode, size_t size)
112 113
{
	struct equiv_cpu_entry *eq;
114
	size_t *cont_sz;
115
	u32 *header;
116
	u8  *data, **cont;
117
	u16 eq_id = 0;
118
	int offset, left;
119
	u32 rev, eax, ebx, ecx, edx;
120 121 122 123
	u32 *new_rev;

#ifdef CONFIG_X86_32
	new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
124 125
	cont_sz = (size_t *)__pa_nodebug(&container_size);
	cont	= (u8 **)__pa_nodebug(&container);
126 127
#else
	new_rev = &ucode_new_rev;
128 129
	cont_sz = &container_size;
	cont	= &container;
130 131
#endif

132 133
	data   = ucode;
	left   = size;
134 135 136
	header = (u32 *)data;

	/* find equiv cpu table */
137 138
	if (header[0] != UCODE_MAGIC ||
	    header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
139 140 141
	    header[2] == 0)                            /* size */
		return;

142 143 144
	eax = 0x00000001;
	ecx = 0;
	native_cpuid(&eax, &ebx, &ecx, &edx);
145 146 147 148

	while (left > 0) {
		eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);

149 150 151
		*cont = data;

		/* Advance past the container header */
152 153 154 155 156
		offset = header[2] + CONTAINER_HDR_SZ;
		data  += offset;
		left  -= offset;

		eq_id = find_equiv_id(eq, eax);
157 158 159 160 161 162 163 164 165
		if (eq_id) {
			this_equiv_id = eq_id;
			*cont_sz = compute_container_size(*cont, left + offset);

			/*
			 * truncate how much we need to iterate over in the
			 * ucode update loop below
			 */
			left = *cont_sz - offset;
166
			break;
167
		}
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

		/*
		 * support multiple container files appended together. if this
		 * one does not have a matching equivalent cpu entry, we fast
		 * forward to the next container file.
		 */
		while (left > 0) {
			header = (u32 *)data;
			if (header[0] == UCODE_MAGIC &&
			    header[1] == UCODE_EQUIV_CPU_TABLE_TYPE)
				break;

			offset = header[1] + SECTION_HDR_SIZE;
			data  += offset;
			left  -= offset;
		}

185 186 187
		/* mark where the next microcode container file starts */
		offset    = data - (u8 *)ucode;
		ucode     = data;
188
	}
189

190
	if (!eq_id) {
191 192
		*cont = NULL;
		*cont_sz = 0;
193
		return;
194
	}
195 196 197

	/* find ucode and update if needed */

198
	native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
199 200 201 202 203 204 205 206 207 208

	while (left > 0) {
		struct microcode_amd *mc;

		header = (u32 *)data;
		if (header[0] != UCODE_UCODE_TYPE || /* type */
		    header[1] == 0)                  /* size */
			break;

		mc = (struct microcode_amd *)(data + SECTION_HDR_SIZE);
209 210 211 212

		if (eq_id == mc->hdr.processor_rev_id && rev < mc->hdr.patch_id) {

			if (!__apply_microcode_amd(mc)) {
213 214
				rev = mc->hdr.patch_id;
				*new_rev = rev;
215 216 217 218

				/* save ucode patch */
				memcpy(amd_ucode_patch, mc,
				       min_t(u32, header[1], PATCH_MAX_SIZE));
219
			}
220
		}
221 222 223 224 225 226 227 228 229

		offset  = header[1] + SECTION_HDR_SIZE;
		data   += offset;
		left   -= offset;
	}
}

void __init load_ucode_amd_bsp(void)
{
230 231 232 233 234 235 236 237 238 239 240 241 242 243
	struct cpio_data cp;
	void **data;
	size_t *size;

#ifdef CONFIG_X86_32
	data =  (void **)__pa_nodebug(&ucode_cpio.data);
	size = (size_t *)__pa_nodebug(&ucode_cpio.size);
#else
	data = &ucode_cpio.data;
	size = &ucode_cpio.size;
#endif

	cp = find_ucode_in_initrd();
	if (!cp.data)
244 245
		return;

246 247 248 249
	*data = cp.data;
	*size = cp.size;

	apply_ucode_in_initrd(cp.data, cp.size);
250 251 252 253 254 255 256
}

#ifdef CONFIG_X86_32
/*
 * On 32-bit, since AP's early load occurs before paging is turned on, we
 * cannot traverse cpu_equiv_table and pcache in kernel heap memory. So during
 * cold boot, AP will apply_ucode_in_initrd() just like the BSP. During
257 258
 * save_microcode_in_initrd_amd() BSP's patch is copied to amd_ucode_patch,
 * which is used upon resume from suspend.
259
 */
260
void load_ucode_amd_ap(void)
261 262
{
	struct microcode_amd *mc;
263
	size_t *usize;
264
	void **ucode;
265

266
	mc = (struct microcode_amd *)__pa(amd_ucode_patch);
267
	if (mc->hdr.patch_id && mc->hdr.processor_rev_id) {
268
		__apply_microcode_amd(mc);
269 270 271
		return;
	}

272 273
	ucode = (void *)__pa_nodebug(&container);
	usize = (size_t *)__pa_nodebug(&container_size);
274

275
	if (!*ucode || !*usize)
276 277
		return;

278
	apply_ucode_in_initrd(*ucode, *usize);
279 280 281 282 283 284
}

static void __init collect_cpu_sig_on_bsp(void *arg)
{
	unsigned int cpu = smp_processor_id();
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
285

286 287
	uci->cpu_sig.sig = cpuid_eax(0x00000001);
}
288 289 290 291 292 293 294 295 296

static void __init get_bsp_sig(void)
{
	unsigned int bsp = boot_cpu_data.cpu_index;
	struct ucode_cpu_info *uci = ucode_cpu_info + bsp;

	if (!uci->cpu_sig.sig)
		smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
}
297
#else
298
void load_ucode_amd_ap(void)
299
{
300 301
	unsigned int cpu = smp_processor_id();
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
302 303
	struct equiv_cpu_entry *eq;
	struct microcode_amd *mc;
304
	u32 rev, eax;
305 306 307 308 309 310 311 312
	u16 eq_id;

	/* Exit if called on the BSP. */
	if (!cpu)
		return;

	if (!container)
		return;
313 314 315 316

	rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);

	uci->cpu_sig.rev = rev;
317
	uci->cpu_sig.sig = eax;
318

319 320
	eax = cpuid_eax(0x00000001);
	eq  = (struct equiv_cpu_entry *)(container + CONTAINER_HDR_SZ);
321

322 323 324 325 326 327
	eq_id = find_equiv_id(eq, eax);
	if (!eq_id)
		return;

	if (eq_id == this_equiv_id) {
		mc = (struct microcode_amd *)amd_ucode_patch;
328

329 330 331 332 333 334 335
		if (mc && rev < mc->hdr.patch_id) {
			if (!__apply_microcode_amd(mc))
				ucode_new_rev = mc->hdr.patch_id;
		}

	} else {
		if (!ucode_cpio.data)
336
			return;
337

338 339 340 341 342
		/*
		 * AP has a different equivalence ID than BSP, looks like
		 * mixed-steppings silicon so go through the ucode blob anew.
		 */
		apply_ucode_in_initrd(ucode_cpio.data, ucode_cpio.size);
343 344 345 346 347 348
	}
}
#endif

int __init save_microcode_in_initrd_amd(void)
{
349
	unsigned long cont;
350
	enum ucode_state ret;
351 352
	u32 eax;

353 354
	if (!container)
		return -EINVAL;
355

356 357 358 359
#ifdef CONFIG_X86_32
	get_bsp_sig();
	cont = (unsigned long)container;
#else
360
	/*
361 362
	 * We need the physical address of the container for both bitness since
	 * boot_params.hdr.ramdisk_image is a physical address.
363
	 */
364
	cont = __pa(container);
365
#endif
366 367 368 369 370 371 372 373 374 375

	/*
	 * Take into account the fact that the ramdisk might get relocated and
	 * therefore we need to recompute the container's position in virtual
	 * memory space.
	 */
	if (relocated_ramdisk)
		container = (u8 *)(__va(relocated_ramdisk) +
			     (cont - boot_params.hdr.ramdisk_image));

376 377 378 379
	if (ucode_new_rev)
		pr_info("microcode: updated early to new patch_level=0x%08x\n",
			ucode_new_rev);

380 381 382
	eax   = cpuid_eax(0x00000001);
	eax   = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);

383
	ret = load_microcode_amd(eax, container, container_size);
384 385 386
	if (ret != UCODE_OK)
		return -EINVAL;

387 388 389 390 391 392 393
	/*
	 * This will be freed any msec now, stash patches for the current
	 * family and switch to patch cache for cpu hotplug, etc later.
	 */
	container = NULL;
	container_size = 0;

394 395
	return 0;
}