1 // SPDX-License-Identifier: GPL-2.0-only
3 * imr.c -- Intel Isolated Memory Region driver
5 * Copyright(c) 2013 Intel Corporation.
6 * Copyright(c) 2015 Bryan O'Donoghue <pure.logic@nexus-software.ie>
8 * IMR registers define an isolated region of memory that can
9 * be masked to prohibit certain system agents from accessing memory.
10 * When a device behind a masked port performs an access - snooped or
11 * not, an IMR may optionally prevent that transaction from changing
12 * the state of memory or from getting correct data in response to the
15 * Write data will be dropped and reads will return 0xFFFFFFFF, the
16 * system will reset and system BIOS will print out an error message to
17 * inform the user that an IMR has been violated.
19 * This code is based on the Linux MTRR code and reference code from
20 * Intel's Quark BSP EFI, Linux and grub code.
22 * See quark-x1000-datasheet.pdf for register definitions.
23 * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/quark-x1000-datasheet.pdf
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <asm-generic/sections.h>
29 #include <asm/cpu_device_id.h>
31 #include <asm/iosf_mbi.h>
34 #include <linux/debugfs.h>
35 #include <linux/init.h>
37 #include <linux/types.h>
46 static struct imr_device imr_dev;
49 * IMR read/write mask control registers.
50 * See quark-x1000-datasheet.pdf sections 12.7.4.5 and 12.7.4.6 for
56 * 23:2 1 KiB aligned lo address
61 * 23:2 1 KiB aligned hi address
64 #define IMR_LOCK BIT(31)
73 #define IMR_NUM_REGS (sizeof(struct imr_regs)/sizeof(u32))
75 #define imr_to_phys(x) ((x) << IMR_SHIFT)
76 #define phys_to_imr(x) ((x) >> IMR_SHIFT)
79 * imr_is_enabled - true if an IMR is enabled false otherwise.
81 * Determines if an IMR is enabled based on address range and read/write
82 * mask. An IMR set with an address range set to zero and a read/write
83 * access mask set to all is considered to be disabled. An IMR in any
84 * other state - for example set to zero but without read/write access
85 * all is considered to be enabled. This definition of disabled is how
86 * firmware switches off an IMR and is maintained in kernel for
89 * @imr: pointer to IMR descriptor.
90 * @return: true if IMR enabled false if disabled.
92 static inline int imr_is_enabled(struct imr_regs *imr)
94 return !(imr->rmask == IMR_READ_ACCESS_ALL &&
95 imr->wmask == IMR_WRITE_ACCESS_ALL &&
96 imr_to_phys(imr->addr_lo) == 0 &&
97 imr_to_phys(imr->addr_hi) == 0);
101 * imr_read - read an IMR at a given index.
103 * Requires caller to hold imr mutex.
105 * @idev: pointer to imr_device structure.
106 * @imr_id: IMR entry to read.
107 * @imr: IMR structure representing address and access masks.
108 * @return: 0 on success or error code passed from mbi_iosf on failure.
110 static int imr_read(struct imr_device *idev, u32 imr_id, struct imr_regs *imr)
112 u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base;
115 ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->addr_lo);
119 ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->addr_hi);
123 ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->rmask);
127 return iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->wmask);
131 * imr_write - write an IMR at a given index.
133 * Requires caller to hold imr mutex.
134 * Note lock bits need to be written independently of address bits.
136 * @idev: pointer to imr_device structure.
137 * @imr_id: IMR entry to write.
138 * @imr: IMR structure representing address and access masks.
139 * @return: 0 on success or error code passed from mbi_iosf on failure.
141 static int imr_write(struct imr_device *idev, u32 imr_id, struct imr_regs *imr)
144 u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base;
147 local_irq_save(flags);
149 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->addr_lo);
153 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->addr_hi);
157 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->rmask);
161 ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->wmask);
165 local_irq_restore(flags);
169 * If writing to the IOSF failed then we're in an unknown state,
170 * likely a very bad state. An IMR in an invalid state will almost
171 * certainly lead to a memory access violation.
173 local_irq_restore(flags);
174 WARN(ret, "IOSF-MBI write fail range 0x%08x-0x%08x unreliable\n",
175 imr_to_phys(imr->addr_lo), imr_to_phys(imr->addr_hi) + IMR_MASK);
181 * imr_dbgfs_state_show - print state of IMR registers.
183 * @s: pointer to seq_file for output.
184 * @unused: unused parameter.
185 * @return: 0 on success or error code passed from mbi_iosf on failure.
187 static int imr_dbgfs_state_show(struct seq_file *s, void *unused)
192 struct imr_device *idev = s->private;
197 mutex_lock(&idev->lock);
199 for (i = 0; i < idev->max_imr; i++) {
201 ret = imr_read(idev, i, &imr);
206 * Remember to add IMR_ALIGN bytes to size to indicate the
207 * inherent IMR_ALIGN size bytes contained in the masked away
210 if (imr_is_enabled(&imr)) {
211 base = imr_to_phys(imr.addr_lo);
212 end = imr_to_phys(imr.addr_hi) + IMR_MASK;
213 size = end - base + 1;
219 seq_printf(s, "imr%02i: base=%pa, end=%pa, size=0x%08zx "
220 "rmask=0x%08x, wmask=0x%08x, %s, %s\n", i,
221 &base, &end, size, imr.rmask, imr.wmask,
222 imr_is_enabled(&imr) ? "enabled " : "disabled",
223 imr.addr_lo & IMR_LOCK ? "locked" : "unlocked");
226 mutex_unlock(&idev->lock);
229 DEFINE_SHOW_ATTRIBUTE(imr_dbgfs_state);
232 * imr_debugfs_register - register debugfs hooks.
234 * @idev: pointer to imr_device structure.
236 static void imr_debugfs_register(struct imr_device *idev)
238 debugfs_create_file("imr_state", 0444, NULL, idev,
239 &imr_dbgfs_state_fops);
243 * imr_check_params - check passed address range IMR alignment and non-zero size
245 * @base: base address of intended IMR.
246 * @size: size of intended IMR.
247 * @return: zero on valid range -EINVAL on unaligned base/size.
249 static int imr_check_params(phys_addr_t base, size_t size)
251 if ((base & IMR_MASK) || (size & IMR_MASK)) {
252 pr_err("base %pa size 0x%08zx must align to 1KiB\n",
263 * imr_raw_size - account for the IMR_ALIGN bytes that addr_hi appends.
265 * IMR addr_hi has a built in offset of plus IMR_ALIGN (0x400) bytes from the
266 * value in the register. We need to subtract IMR_ALIGN bytes from input sizes
269 * @size: input size bytes.
270 * @return: reduced size.
272 static inline size_t imr_raw_size(size_t size)
274 return size - IMR_ALIGN;
278 * imr_address_overlap - detects an address overlap.
280 * @addr: address to check against an existing IMR.
281 * @imr: imr being checked.
282 * @return: true for overlap false for no overlap.
284 static inline int imr_address_overlap(phys_addr_t addr, struct imr_regs *imr)
286 return addr >= imr_to_phys(imr->addr_lo) && addr <= imr_to_phys(imr->addr_hi);
290 * imr_add_range - add an Isolated Memory Region.
292 * @base: physical base address of region aligned to 1KiB.
293 * @size: physical size of region in bytes must be aligned to 1KiB.
294 * @read_mask: read access mask.
295 * @write_mask: write access mask.
296 * @return: zero on success or negative value indicating error.
298 int imr_add_range(phys_addr_t base, size_t size,
299 unsigned int rmask, unsigned int wmask)
303 struct imr_device *idev = &imr_dev;
309 if (WARN_ONCE(idev->init == false, "driver not initialized"))
312 ret = imr_check_params(base, size);
316 /* Tweak the size value. */
317 raw_size = imr_raw_size(size);
318 end = base + raw_size;
321 * Check for reserved IMR value common to firmware, kernel and grub
322 * indicating a disabled IMR.
324 imr.addr_lo = phys_to_imr(base);
325 imr.addr_hi = phys_to_imr(end);
328 if (!imr_is_enabled(&imr))
331 mutex_lock(&idev->lock);
334 * Find a free IMR while checking for an existing overlapping range.
335 * Note there's no restriction in silicon to prevent IMR overlaps.
336 * For the sake of simplicity and ease in defining/debugging an IMR
337 * memory map we exclude IMR overlaps.
340 for (i = 0; i < idev->max_imr; i++) {
341 ret = imr_read(idev, i, &imr);
345 /* Find overlap @ base or end of requested range. */
347 if (imr_is_enabled(&imr)) {
348 if (imr_address_overlap(base, &imr))
350 if (imr_address_overlap(end, &imr))
357 /* Error out if we have no free IMR entries. */
363 pr_debug("add %d phys %pa-%pa size %zx mask 0x%08x wmask 0x%08x\n",
364 reg, &base, &end, raw_size, rmask, wmask);
366 /* Enable IMR at specified range and access mask. */
367 imr.addr_lo = phys_to_imr(base);
368 imr.addr_hi = phys_to_imr(end);
372 ret = imr_write(idev, reg, &imr);
375 * In the highly unlikely event iosf_mbi_write failed
376 * attempt to rollback the IMR setup skipping the trapping
377 * of further IOSF write failures.
381 imr.rmask = IMR_READ_ACCESS_ALL;
382 imr.wmask = IMR_WRITE_ACCESS_ALL;
383 imr_write(idev, reg, &imr);
386 mutex_unlock(&idev->lock);
389 EXPORT_SYMBOL_GPL(imr_add_range);
392 * __imr_remove_range - delete an Isolated Memory Region.
394 * This function allows you to delete an IMR by its index specified by reg or
395 * by address range specified by base and size respectively. If you specify an
396 * index on its own the base and size parameters are ignored.
397 * imr_remove_range(0, base, size); delete IMR at index 0 base/size ignored.
398 * imr_remove_range(-1, base, size); delete IMR from base to base+size.
400 * @reg: imr index to remove.
401 * @base: physical base address of region aligned to 1 KiB.
402 * @size: physical size of region in bytes aligned to 1 KiB.
403 * @return: -EINVAL on invalid range or out or range id
404 * -ENODEV if reg is valid but no IMR exists or is locked
407 static int __imr_remove_range(int reg, phys_addr_t base, size_t size)
412 struct imr_device *idev = &imr_dev;
417 if (WARN_ONCE(idev->init == false, "driver not initialized"))
421 * Validate address range if deleting by address, else we are
422 * deleting by index where base and size will be ignored.
425 ret = imr_check_params(base, size);
430 /* Tweak the size value. */
431 raw_size = imr_raw_size(size);
432 end = base + raw_size;
434 mutex_lock(&idev->lock);
437 /* If a specific IMR is given try to use it. */
438 ret = imr_read(idev, reg, &imr);
442 if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK) {
448 /* Search for match based on address range. */
449 for (i = 0; i < idev->max_imr; i++) {
450 ret = imr_read(idev, i, &imr);
454 if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK)
457 if ((imr_to_phys(imr.addr_lo) == base) &&
458 (imr_to_phys(imr.addr_hi) == end)) {
471 pr_debug("remove %d phys %pa-%pa size %zx\n", reg, &base, &end, raw_size);
473 /* Tear down the IMR. */
476 imr.rmask = IMR_READ_ACCESS_ALL;
477 imr.wmask = IMR_WRITE_ACCESS_ALL;
479 ret = imr_write(idev, reg, &imr);
482 mutex_unlock(&idev->lock);
487 * imr_remove_range - delete an Isolated Memory Region by address
489 * This function allows you to delete an IMR by an address range specified
490 * by base and size respectively.
491 * imr_remove_range(base, size); delete IMR from base to base+size.
493 * @base: physical base address of region aligned to 1 KiB.
494 * @size: physical size of region in bytes aligned to 1 KiB.
495 * @return: -EINVAL on invalid range or out or range id
496 * -ENODEV if reg is valid but no IMR exists or is locked
499 int imr_remove_range(phys_addr_t base, size_t size)
501 return __imr_remove_range(-1, base, size);
503 EXPORT_SYMBOL_GPL(imr_remove_range);
506 * imr_clear - delete an Isolated Memory Region by index
508 * This function allows you to delete an IMR by an address range specified
509 * by the index of the IMR. Useful for initial sanitization of the IMR
511 * imr_ge(base, size); delete IMR from base to base+size.
513 * @reg: imr index to remove.
514 * @return: -EINVAL on invalid range or out or range id
515 * -ENODEV if reg is valid but no IMR exists or is locked
518 static inline int imr_clear(int reg)
520 return __imr_remove_range(reg, 0, 0);
524 * imr_fixup_memmap - Tear down IMRs used during bootup.
526 * BIOS and Grub both setup IMRs around compressed kernel, initrd memory
527 * that need to be removed before the kernel hands out one of the IMR
528 * encased addresses to a downstream DMA agent such as the SD or Ethernet.
529 * IMRs on Galileo are setup to immediately reset the system on violation.
530 * As a result if you're running a root filesystem from SD - you'll need
531 * the boot-time IMRs torn down or you'll find seemingly random resets when
532 * using your filesystem.
534 * @idev: pointer to imr_device structure.
537 static void __init imr_fixup_memmap(struct imr_device *idev)
539 phys_addr_t base = virt_to_phys(&_text);
540 size_t size = virt_to_phys(&__end_rodata) - base;
541 unsigned long start, end;
545 /* Tear down all existing unlocked IMRs. */
546 for (i = 0; i < idev->max_imr; i++)
549 start = (unsigned long)_text;
550 end = (unsigned long)__end_rodata - 1;
553 * Setup an unlocked IMR around the physical extent of the kernel
554 * from the beginning of the .text section to the end of the
555 * .rodata section as one physically contiguous block.
557 * We don't round up @size since it is already PAGE_SIZE aligned.
558 * See vmlinux.lds.S for details.
560 ret = imr_add_range(base, size, IMR_CPU, IMR_CPU);
562 pr_err("unable to setup IMR for kernel: %zu KiB (%lx - %lx)\n",
563 size / 1024, start, end);
565 pr_info("protecting kernel .text - .rodata: %zu KiB (%lx - %lx)\n",
566 size / 1024, start, end);
571 static const struct x86_cpu_id imr_ids[] __initconst = {
572 X86_MATCH_VENDOR_FAM_MODEL(INTEL, 5, INTEL_FAM5_QUARK_X1000, NULL),
577 * imr_init - entry point for IMR driver.
579 * return: -ENODEV for no IMR support 0 if good to go.
581 static int __init imr_init(void)
583 struct imr_device *idev = &imr_dev;
585 if (!x86_match_cpu(imr_ids) || !iosf_mbi_available())
588 idev->max_imr = QUARK_X1000_IMR_MAX;
589 idev->reg_base = QUARK_X1000_IMR_REGBASE;
592 mutex_init(&idev->lock);
593 imr_debugfs_register(idev);
594 imr_fixup_memmap(idev);
597 device_initcall(imr_init);