xref: /linux/arch/x86/platform/intel-quark/imr.c (revision 03e075b38e6cd25267c8d6e2797fa4537ca3348d)
1 /**
2  * imr.c
3  *
4  * Copyright(c) 2013 Intel Corporation.
5  * Copyright(c) 2015 Bryan O'Donoghue <pure.logic@nexus-software.ie>
6  *
7  * IMR registers define an isolated region of memory that can
8  * be masked to prohibit certain system agents from accessing memory.
9  * When a device behind a masked port performs an access - snooped or
10  * not, an IMR may optionally prevent that transaction from changing
11  * the state of memory or from getting correct data in response to the
12  * operation.
13  *
14  * Write data will be dropped and reads will return 0xFFFFFFFF, the
15  * system will reset and system BIOS will print out an error message to
16  * inform the user that an IMR has been violated.
17  *
18  * This code is based on the Linux MTRR code and reference code from
19  * Intel's Quark BSP EFI, Linux and grub code.
20  *
21  * See quark-x1000-datasheet.pdf for register definitions.
22  * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/quark-x1000-datasheet.pdf
23  */
24 
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 
27 #include <asm-generic/sections.h>
28 #include <asm/cpu_device_id.h>
29 #include <asm/imr.h>
30 #include <asm/iosf_mbi.h>
31 #include <linux/debugfs.h>
32 #include <linux/init.h>
33 #include <linux/mm.h>
34 #include <linux/module.h>
35 #include <linux/types.h>
36 
37 struct imr_device {
38 	struct dentry	*file;
39 	bool		init;
40 	struct mutex	lock;
41 	int		max_imr;
42 	int		reg_base;
43 };
44 
45 static struct imr_device imr_dev;
46 
47 /*
48  * IMR read/write mask control registers.
49  * See quark-x1000-datasheet.pdf sections 12.7.4.5 and 12.7.4.6 for
50  * bit definitions.
51  *
52  * addr_hi
53  * 31		Lock bit
54  * 30:24	Reserved
55  * 23:2		1 KiB aligned lo address
56  * 1:0		Reserved
57  *
58  * addr_hi
59  * 31:24	Reserved
60  * 23:2		1 KiB aligned hi address
61  * 1:0		Reserved
62  */
63 #define IMR_LOCK	BIT(31)
64 
65 struct imr_regs {
66 	u32 addr_lo;
67 	u32 addr_hi;
68 	u32 rmask;
69 	u32 wmask;
70 };
71 
72 #define IMR_NUM_REGS	(sizeof(struct imr_regs)/sizeof(u32))
73 #define IMR_SHIFT	8
74 #define imr_to_phys(x)	((x) << IMR_SHIFT)
75 #define phys_to_imr(x)	((x) >> IMR_SHIFT)
76 
77 /**
78  * imr_is_enabled - true if an IMR is enabled false otherwise.
79  *
80  * Determines if an IMR is enabled based on address range and read/write
81  * mask. An IMR set with an address range set to zero and a read/write
82  * access mask set to all is considered to be disabled. An IMR in any
83  * other state - for example set to zero but without read/write access
84  * all is considered to be enabled. This definition of disabled is how
85  * firmware switches off an IMR and is maintained in kernel for
86  * consistency.
87  *
88  * @imr:	pointer to IMR descriptor.
89  * @return:	true if IMR enabled false if disabled.
90  */
91 static inline int imr_is_enabled(struct imr_regs *imr)
92 {
93 	return !(imr->rmask == IMR_READ_ACCESS_ALL &&
94 		 imr->wmask == IMR_WRITE_ACCESS_ALL &&
95 		 imr_to_phys(imr->addr_lo) == 0 &&
96 		 imr_to_phys(imr->addr_hi) == 0);
97 }
98 
99 /**
100  * imr_read - read an IMR at a given index.
101  *
102  * Requires caller to hold imr mutex.
103  *
104  * @idev:	pointer to imr_device structure.
105  * @imr_id:	IMR entry to read.
106  * @imr:	IMR structure representing address and access masks.
107  * @return:	0 on success or error code passed from mbi_iosf on failure.
108  */
109 static int imr_read(struct imr_device *idev, u32 imr_id, struct imr_regs *imr)
110 {
111 	u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base;
112 	int ret;
113 
114 	ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->addr_lo);
115 	if (ret)
116 		return ret;
117 
118 	ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->addr_hi);
119 	if (ret)
120 		return ret;
121 
122 	ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->rmask);
123 	if (ret)
124 		return ret;
125 
126 	return iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->wmask);
127 }
128 
129 /**
130  * imr_write - write an IMR at a given index.
131  *
132  * Requires caller to hold imr mutex.
133  * Note lock bits need to be written independently of address bits.
134  *
135  * @idev:	pointer to imr_device structure.
136  * @imr_id:	IMR entry to write.
137  * @imr:	IMR structure representing address and access masks.
138  * @lock:	indicates if the IMR lock bit should be applied.
139  * @return:	0 on success or error code passed from mbi_iosf on failure.
140  */
141 static int imr_write(struct imr_device *idev, u32 imr_id,
142 		     struct imr_regs *imr, bool lock)
143 {
144 	unsigned long flags;
145 	u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base;
146 	int ret;
147 
148 	local_irq_save(flags);
149 
150 	ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->addr_lo);
151 	if (ret)
152 		goto failed;
153 
154 	ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->addr_hi);
155 	if (ret)
156 		goto failed;
157 
158 	ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->rmask);
159 	if (ret)
160 		goto failed;
161 
162 	ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->wmask);
163 	if (ret)
164 		goto failed;
165 
166 	/* Lock bit must be set separately to addr_lo address bits. */
167 	if (lock) {
168 		imr->addr_lo |= IMR_LOCK;
169 		ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE,
170 				     reg - IMR_NUM_REGS, imr->addr_lo);
171 		if (ret)
172 			goto failed;
173 	}
174 
175 	local_irq_restore(flags);
176 	return 0;
177 failed:
178 	/*
179 	 * If writing to the IOSF failed then we're in an unknown state,
180 	 * likely a very bad state. An IMR in an invalid state will almost
181 	 * certainly lead to a memory access violation.
182 	 */
183 	local_irq_restore(flags);
184 	WARN(ret, "IOSF-MBI write fail range 0x%08x-0x%08x unreliable\n",
185 	     imr_to_phys(imr->addr_lo), imr_to_phys(imr->addr_hi) + IMR_MASK);
186 
187 	return ret;
188 }
189 
190 /**
191  * imr_dbgfs_state_show - print state of IMR registers.
192  *
193  * @s:		pointer to seq_file for output.
194  * @unused:	unused parameter.
195  * @return:	0 on success or error code passed from mbi_iosf on failure.
196  */
197 static int imr_dbgfs_state_show(struct seq_file *s, void *unused)
198 {
199 	phys_addr_t base;
200 	phys_addr_t end;
201 	int i;
202 	struct imr_device *idev = s->private;
203 	struct imr_regs imr;
204 	size_t size;
205 	int ret = -ENODEV;
206 
207 	mutex_lock(&idev->lock);
208 
209 	for (i = 0; i < idev->max_imr; i++) {
210 
211 		ret = imr_read(idev, i, &imr);
212 		if (ret)
213 			break;
214 
215 		/*
216 		 * Remember to add IMR_ALIGN bytes to size to indicate the
217 		 * inherent IMR_ALIGN size bytes contained in the masked away
218 		 * lower ten bits.
219 		 */
220 		if (imr_is_enabled(&imr)) {
221 			base = imr_to_phys(imr.addr_lo);
222 			end = imr_to_phys(imr.addr_hi) + IMR_MASK;
223 			size = end - base + 1;
224 		} else {
225 			base = 0;
226 			end = 0;
227 			size = 0;
228 		}
229 		seq_printf(s, "imr%02i: base=%pa, end=%pa, size=0x%08zx "
230 			   "rmask=0x%08x, wmask=0x%08x, %s, %s\n", i,
231 			   &base, &end, size, imr.rmask, imr.wmask,
232 			   imr_is_enabled(&imr) ? "enabled " : "disabled",
233 			   imr.addr_lo & IMR_LOCK ? "locked" : "unlocked");
234 	}
235 
236 	mutex_unlock(&idev->lock);
237 	return ret;
238 }
239 
240 /**
241  * imr_state_open - debugfs open callback.
242  *
243  * @inode:	pointer to struct inode.
244  * @file:	pointer to struct file.
245  * @return:	result of single open.
246  */
247 static int imr_state_open(struct inode *inode, struct file *file)
248 {
249 	return single_open(file, imr_dbgfs_state_show, inode->i_private);
250 }
251 
252 static const struct file_operations imr_state_ops = {
253 	.open		= imr_state_open,
254 	.read		= seq_read,
255 	.llseek		= seq_lseek,
256 	.release	= single_release,
257 };
258 
259 /**
260  * imr_debugfs_register - register debugfs hooks.
261  *
262  * @idev:	pointer to imr_device structure.
263  * @return:	0 on success - errno on failure.
264  */
265 static int imr_debugfs_register(struct imr_device *idev)
266 {
267 	idev->file = debugfs_create_file("imr_state", S_IFREG | S_IRUGO, NULL,
268 					 idev, &imr_state_ops);
269 	return PTR_ERR_OR_ZERO(idev->file);
270 }
271 
272 /**
273  * imr_debugfs_unregister - unregister debugfs hooks.
274  *
275  * @idev:	pointer to imr_device structure.
276  * @return:
277  */
278 static void imr_debugfs_unregister(struct imr_device *idev)
279 {
280 	debugfs_remove(idev->file);
281 }
282 
283 /**
284  * imr_check_params - check passed address range IMR alignment and non-zero size
285  *
286  * @base:	base address of intended IMR.
287  * @size:	size of intended IMR.
288  * @return:	zero on valid range -EINVAL on unaligned base/size.
289  */
290 static int imr_check_params(phys_addr_t base, size_t size)
291 {
292 	if ((base & IMR_MASK) || (size & IMR_MASK)) {
293 		pr_err("base %pa size 0x%08zx must align to 1KiB\n",
294 			&base, size);
295 		return -EINVAL;
296 	}
297 	if (size == 0)
298 		return -EINVAL;
299 
300 	return 0;
301 }
302 
303 /**
304  * imr_raw_size - account for the IMR_ALIGN bytes that addr_hi appends.
305  *
306  * IMR addr_hi has a built in offset of plus IMR_ALIGN (0x400) bytes from the
307  * value in the register. We need to subtract IMR_ALIGN bytes from input sizes
308  * as a result.
309  *
310  * @size:	input size bytes.
311  * @return:	reduced size.
312  */
313 static inline size_t imr_raw_size(size_t size)
314 {
315 	return size - IMR_ALIGN;
316 }
317 
318 /**
319  * imr_address_overlap - detects an address overlap.
320  *
321  * @addr:	address to check against an existing IMR.
322  * @imr:	imr being checked.
323  * @return:	true for overlap false for no overlap.
324  */
325 static inline int imr_address_overlap(phys_addr_t addr, struct imr_regs *imr)
326 {
327 	return addr >= imr_to_phys(imr->addr_lo) && addr <= imr_to_phys(imr->addr_hi);
328 }
329 
330 /**
331  * imr_add_range - add an Isolated Memory Region.
332  *
333  * @base:	physical base address of region aligned to 1KiB.
334  * @size:	physical size of region in bytes must be aligned to 1KiB.
335  * @read_mask:	read access mask.
336  * @write_mask:	write access mask.
337  * @lock:	indicates whether or not to permanently lock this region.
338  * @return:	zero on success or negative value indicating error.
339  */
340 int imr_add_range(phys_addr_t base, size_t size,
341 		  unsigned int rmask, unsigned int wmask, bool lock)
342 {
343 	phys_addr_t end;
344 	unsigned int i;
345 	struct imr_device *idev = &imr_dev;
346 	struct imr_regs imr;
347 	size_t raw_size;
348 	int reg;
349 	int ret;
350 
351 	if (WARN_ONCE(idev->init == false, "driver not initialized"))
352 		return -ENODEV;
353 
354 	ret = imr_check_params(base, size);
355 	if (ret)
356 		return ret;
357 
358 	/* Tweak the size value. */
359 	raw_size = imr_raw_size(size);
360 	end = base + raw_size;
361 
362 	/*
363 	 * Check for reserved IMR value common to firmware, kernel and grub
364 	 * indicating a disabled IMR.
365 	 */
366 	imr.addr_lo = phys_to_imr(base);
367 	imr.addr_hi = phys_to_imr(end);
368 	imr.rmask = rmask;
369 	imr.wmask = wmask;
370 	if (!imr_is_enabled(&imr))
371 		return -ENOTSUPP;
372 
373 	mutex_lock(&idev->lock);
374 
375 	/*
376 	 * Find a free IMR while checking for an existing overlapping range.
377 	 * Note there's no restriction in silicon to prevent IMR overlaps.
378 	 * For the sake of simplicity and ease in defining/debugging an IMR
379 	 * memory map we exclude IMR overlaps.
380 	 */
381 	reg = -1;
382 	for (i = 0; i < idev->max_imr; i++) {
383 		ret = imr_read(idev, i, &imr);
384 		if (ret)
385 			goto failed;
386 
387 		/* Find overlap @ base or end of requested range. */
388 		ret = -EINVAL;
389 		if (imr_is_enabled(&imr)) {
390 			if (imr_address_overlap(base, &imr))
391 				goto failed;
392 			if (imr_address_overlap(end, &imr))
393 				goto failed;
394 		} else {
395 			reg = i;
396 		}
397 	}
398 
399 	/* Error out if we have no free IMR entries. */
400 	if (reg == -1) {
401 		ret = -ENOMEM;
402 		goto failed;
403 	}
404 
405 	pr_debug("add %d phys %pa-%pa size %zx mask 0x%08x wmask 0x%08x\n",
406 		 reg, &base, &end, raw_size, rmask, wmask);
407 
408 	/* Enable IMR at specified range and access mask. */
409 	imr.addr_lo = phys_to_imr(base);
410 	imr.addr_hi = phys_to_imr(end);
411 	imr.rmask = rmask;
412 	imr.wmask = wmask;
413 
414 	ret = imr_write(idev, reg, &imr, lock);
415 	if (ret < 0) {
416 		/*
417 		 * In the highly unlikely event iosf_mbi_write failed
418 		 * attempt to rollback the IMR setup skipping the trapping
419 		 * of further IOSF write failures.
420 		 */
421 		imr.addr_lo = 0;
422 		imr.addr_hi = 0;
423 		imr.rmask = IMR_READ_ACCESS_ALL;
424 		imr.wmask = IMR_WRITE_ACCESS_ALL;
425 		imr_write(idev, reg, &imr, false);
426 	}
427 failed:
428 	mutex_unlock(&idev->lock);
429 	return ret;
430 }
431 EXPORT_SYMBOL_GPL(imr_add_range);
432 
433 /**
434  * __imr_remove_range - delete an Isolated Memory Region.
435  *
436  * This function allows you to delete an IMR by its index specified by reg or
437  * by address range specified by base and size respectively. If you specify an
438  * index on its own the base and size parameters are ignored.
439  * imr_remove_range(0, base, size); delete IMR at index 0 base/size ignored.
440  * imr_remove_range(-1, base, size); delete IMR from base to base+size.
441  *
442  * @reg:	imr index to remove.
443  * @base:	physical base address of region aligned to 1 KiB.
444  * @size:	physical size of region in bytes aligned to 1 KiB.
445  * @return:	-EINVAL on invalid range or out or range id
446  *		-ENODEV if reg is valid but no IMR exists or is locked
447  *		0 on success.
448  */
449 static int __imr_remove_range(int reg, phys_addr_t base, size_t size)
450 {
451 	phys_addr_t end;
452 	bool found = false;
453 	unsigned int i;
454 	struct imr_device *idev = &imr_dev;
455 	struct imr_regs imr;
456 	size_t raw_size;
457 	int ret = 0;
458 
459 	if (WARN_ONCE(idev->init == false, "driver not initialized"))
460 		return -ENODEV;
461 
462 	/*
463 	 * Validate address range if deleting by address, else we are
464 	 * deleting by index where base and size will be ignored.
465 	 */
466 	if (reg == -1) {
467 		ret = imr_check_params(base, size);
468 		if (ret)
469 			return ret;
470 	}
471 
472 	/* Tweak the size value. */
473 	raw_size = imr_raw_size(size);
474 	end = base + raw_size;
475 
476 	mutex_lock(&idev->lock);
477 
478 	if (reg >= 0) {
479 		/* If a specific IMR is given try to use it. */
480 		ret = imr_read(idev, reg, &imr);
481 		if (ret)
482 			goto failed;
483 
484 		if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK) {
485 			ret = -ENODEV;
486 			goto failed;
487 		}
488 		found = true;
489 	} else {
490 		/* Search for match based on address range. */
491 		for (i = 0; i < idev->max_imr; i++) {
492 			ret = imr_read(idev, i, &imr);
493 			if (ret)
494 				goto failed;
495 
496 			if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK)
497 				continue;
498 
499 			if ((imr_to_phys(imr.addr_lo) == base) &&
500 			    (imr_to_phys(imr.addr_hi) == end)) {
501 				found = true;
502 				reg = i;
503 				break;
504 			}
505 		}
506 	}
507 
508 	if (!found) {
509 		ret = -ENODEV;
510 		goto failed;
511 	}
512 
513 	pr_debug("remove %d phys %pa-%pa size %zx\n", reg, &base, &end, raw_size);
514 
515 	/* Tear down the IMR. */
516 	imr.addr_lo = 0;
517 	imr.addr_hi = 0;
518 	imr.rmask = IMR_READ_ACCESS_ALL;
519 	imr.wmask = IMR_WRITE_ACCESS_ALL;
520 
521 	ret = imr_write(idev, reg, &imr, false);
522 
523 failed:
524 	mutex_unlock(&idev->lock);
525 	return ret;
526 }
527 
528 /**
529  * imr_remove_range - delete an Isolated Memory Region by address
530  *
531  * This function allows you to delete an IMR by an address range specified
532  * by base and size respectively.
533  * imr_remove_range(base, size); delete IMR from base to base+size.
534  *
535  * @base:	physical base address of region aligned to 1 KiB.
536  * @size:	physical size of region in bytes aligned to 1 KiB.
537  * @return:	-EINVAL on invalid range or out or range id
538  *		-ENODEV if reg is valid but no IMR exists or is locked
539  *		0 on success.
540  */
541 int imr_remove_range(phys_addr_t base, size_t size)
542 {
543 	return __imr_remove_range(-1, base, size);
544 }
545 EXPORT_SYMBOL_GPL(imr_remove_range);
546 
547 /**
548  * imr_clear - delete an Isolated Memory Region by index
549  *
550  * This function allows you to delete an IMR by an address range specified
551  * by the index of the IMR. Useful for initial sanitization of the IMR
552  * address map.
553  * imr_ge(base, size); delete IMR from base to base+size.
554  *
555  * @reg:	imr index to remove.
556  * @return:	-EINVAL on invalid range or out or range id
557  *		-ENODEV if reg is valid but no IMR exists or is locked
558  *		0 on success.
559  */
560 static inline int imr_clear(int reg)
561 {
562 	return __imr_remove_range(reg, 0, 0);
563 }
564 
565 /**
566  * imr_fixup_memmap - Tear down IMRs used during bootup.
567  *
568  * BIOS and Grub both setup IMRs around compressed kernel, initrd memory
569  * that need to be removed before the kernel hands out one of the IMR
570  * encased addresses to a downstream DMA agent such as the SD or Ethernet.
571  * IMRs on Galileo are setup to immediately reset the system on violation.
572  * As a result if you're running a root filesystem from SD - you'll need
573  * the boot-time IMRs torn down or you'll find seemingly random resets when
574  * using your filesystem.
575  *
576  * @idev:	pointer to imr_device structure.
577  * @return:
578  */
579 static void __init imr_fixup_memmap(struct imr_device *idev)
580 {
581 	phys_addr_t base = virt_to_phys(&_text);
582 	size_t size = virt_to_phys(&__end_rodata) - base;
583 	unsigned long start, end;
584 	int i;
585 	int ret;
586 
587 	/* Tear down all existing unlocked IMRs. */
588 	for (i = 0; i < idev->max_imr; i++)
589 		imr_clear(i);
590 
591 	start = (unsigned long)_text;
592 	end = (unsigned long)__end_rodata - 1;
593 
594 	/*
595 	 * Setup a locked IMR around the physical extent of the kernel
596 	 * from the beginning of the .text secton to the end of the
597 	 * .rodata section as one physically contiguous block.
598 	 *
599 	 * We don't round up @size since it is already PAGE_SIZE aligned.
600 	 * See vmlinux.lds.S for details.
601 	 */
602 	ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, true);
603 	if (ret < 0) {
604 		pr_err("unable to setup IMR for kernel: %zu KiB (%lx - %lx)\n",
605 			size / 1024, start, end);
606 	} else {
607 		pr_info("protecting kernel .text - .rodata: %zu KiB (%lx - %lx)\n",
608 			size / 1024, start, end);
609 	}
610 
611 }
612 
613 static const struct x86_cpu_id imr_ids[] __initconst = {
614 	{ X86_VENDOR_INTEL, 5, 9 },	/* Intel Quark SoC X1000. */
615 	{}
616 };
617 MODULE_DEVICE_TABLE(x86cpu, imr_ids);
618 
619 /**
620  * imr_init - entry point for IMR driver.
621  *
622  * return: -ENODEV for no IMR support 0 if good to go.
623  */
624 static int __init imr_init(void)
625 {
626 	struct imr_device *idev = &imr_dev;
627 	int ret;
628 
629 	if (!x86_match_cpu(imr_ids) || !iosf_mbi_available())
630 		return -ENODEV;
631 
632 	idev->max_imr = QUARK_X1000_IMR_MAX;
633 	idev->reg_base = QUARK_X1000_IMR_REGBASE;
634 	idev->init = true;
635 
636 	mutex_init(&idev->lock);
637 	ret = imr_debugfs_register(idev);
638 	if (ret != 0)
639 		pr_warn("debugfs register failed!\n");
640 	imr_fixup_memmap(idev);
641 	return 0;
642 }
643 
644 /**
645  * imr_exit - exit point for IMR code.
646  *
647  * Deregisters debugfs, leave IMR state as-is.
648  *
649  * return:
650  */
651 static void __exit imr_exit(void)
652 {
653 	imr_debugfs_unregister(&imr_dev);
654 }
655 
656 module_init(imr_init);
657 module_exit(imr_exit);
658 
659 MODULE_AUTHOR("Bryan O'Donoghue <pure.logic@nexus-software.ie>");
660 MODULE_DESCRIPTION("Intel Isolated Memory Region driver");
661 MODULE_LICENSE("Dual BSD/GPL");
662