162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * imr.c -- Intel Isolated Memory Region driver 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright(c) 2013 Intel Corporation. 662306a36Sopenharmony_ci * Copyright(c) 2015 Bryan O'Donoghue <pure.logic@nexus-software.ie> 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * IMR registers define an isolated region of memory that can 962306a36Sopenharmony_ci * be masked to prohibit certain system agents from accessing memory. 1062306a36Sopenharmony_ci * When a device behind a masked port performs an access - snooped or 1162306a36Sopenharmony_ci * not, an IMR may optionally prevent that transaction from changing 1262306a36Sopenharmony_ci * the state of memory or from getting correct data in response to the 1362306a36Sopenharmony_ci * operation. 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * Write data will be dropped and reads will return 0xFFFFFFFF, the 1662306a36Sopenharmony_ci * system will reset and system BIOS will print out an error message to 1762306a36Sopenharmony_ci * inform the user that an IMR has been violated. 1862306a36Sopenharmony_ci * 1962306a36Sopenharmony_ci * This code is based on the Linux MTRR code and reference code from 2062306a36Sopenharmony_ci * Intel's Quark BSP EFI, Linux and grub code. 2162306a36Sopenharmony_ci * 2262306a36Sopenharmony_ci * See quark-x1000-datasheet.pdf for register definitions. 2362306a36Sopenharmony_ci * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/quark-x1000-datasheet.pdf 2462306a36Sopenharmony_ci */ 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci#include <asm-generic/sections.h> 2962306a36Sopenharmony_ci#include <asm/cpu_device_id.h> 3062306a36Sopenharmony_ci#include <asm/imr.h> 3162306a36Sopenharmony_ci#include <asm/iosf_mbi.h> 3262306a36Sopenharmony_ci#include <asm/io.h> 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#include <linux/debugfs.h> 3562306a36Sopenharmony_ci#include <linux/init.h> 3662306a36Sopenharmony_ci#include <linux/mm.h> 3762306a36Sopenharmony_ci#include <linux/types.h> 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_cistruct imr_device { 4062306a36Sopenharmony_ci bool init; 4162306a36Sopenharmony_ci struct mutex lock; 4262306a36Sopenharmony_ci int max_imr; 4362306a36Sopenharmony_ci int reg_base; 4462306a36Sopenharmony_ci}; 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistatic struct imr_device imr_dev; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci/* 4962306a36Sopenharmony_ci * IMR read/write mask control registers. 5062306a36Sopenharmony_ci * See quark-x1000-datasheet.pdf sections 12.7.4.5 and 12.7.4.6 for 5162306a36Sopenharmony_ci * bit definitions. 5262306a36Sopenharmony_ci * 5362306a36Sopenharmony_ci * addr_hi 5462306a36Sopenharmony_ci * 31 Lock bit 5562306a36Sopenharmony_ci * 30:24 Reserved 5662306a36Sopenharmony_ci * 23:2 1 KiB aligned lo address 5762306a36Sopenharmony_ci * 1:0 Reserved 5862306a36Sopenharmony_ci * 5962306a36Sopenharmony_ci * addr_hi 6062306a36Sopenharmony_ci * 31:24 Reserved 6162306a36Sopenharmony_ci * 23:2 1 KiB aligned hi address 6262306a36Sopenharmony_ci * 1:0 Reserved 6362306a36Sopenharmony_ci */ 6462306a36Sopenharmony_ci#define IMR_LOCK BIT(31) 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistruct imr_regs { 6762306a36Sopenharmony_ci u32 addr_lo; 6862306a36Sopenharmony_ci u32 addr_hi; 6962306a36Sopenharmony_ci u32 rmask; 7062306a36Sopenharmony_ci u32 wmask; 7162306a36Sopenharmony_ci}; 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci#define IMR_NUM_REGS (sizeof(struct imr_regs)/sizeof(u32)) 7462306a36Sopenharmony_ci#define IMR_SHIFT 8 7562306a36Sopenharmony_ci#define imr_to_phys(x) ((x) << IMR_SHIFT) 7662306a36Sopenharmony_ci#define phys_to_imr(x) ((x) >> IMR_SHIFT) 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci/** 7962306a36Sopenharmony_ci * imr_is_enabled - true if an IMR is enabled false otherwise. 8062306a36Sopenharmony_ci * 8162306a36Sopenharmony_ci * Determines if an IMR is enabled based on address range and read/write 8262306a36Sopenharmony_ci * mask. An IMR set with an address range set to zero and a read/write 8362306a36Sopenharmony_ci * access mask set to all is considered to be disabled. An IMR in any 8462306a36Sopenharmony_ci * other state - for example set to zero but without read/write access 8562306a36Sopenharmony_ci * all is considered to be enabled. This definition of disabled is how 8662306a36Sopenharmony_ci * firmware switches off an IMR and is maintained in kernel for 8762306a36Sopenharmony_ci * consistency. 8862306a36Sopenharmony_ci * 8962306a36Sopenharmony_ci * @imr: pointer to IMR descriptor. 9062306a36Sopenharmony_ci * @return: true if IMR enabled false if disabled. 9162306a36Sopenharmony_ci */ 9262306a36Sopenharmony_cistatic inline int imr_is_enabled(struct imr_regs *imr) 9362306a36Sopenharmony_ci{ 9462306a36Sopenharmony_ci return !(imr->rmask == IMR_READ_ACCESS_ALL && 9562306a36Sopenharmony_ci imr->wmask == IMR_WRITE_ACCESS_ALL && 9662306a36Sopenharmony_ci imr_to_phys(imr->addr_lo) == 0 && 9762306a36Sopenharmony_ci imr_to_phys(imr->addr_hi) == 0); 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci/** 10162306a36Sopenharmony_ci * imr_read - read an IMR at a given index. 10262306a36Sopenharmony_ci * 10362306a36Sopenharmony_ci * Requires caller to hold imr mutex. 10462306a36Sopenharmony_ci * 10562306a36Sopenharmony_ci * @idev: pointer to imr_device structure. 10662306a36Sopenharmony_ci * @imr_id: IMR entry to read. 10762306a36Sopenharmony_ci * @imr: IMR structure representing address and access masks. 10862306a36Sopenharmony_ci * @return: 0 on success or error code passed from mbi_iosf on failure. 10962306a36Sopenharmony_ci */ 11062306a36Sopenharmony_cistatic int imr_read(struct imr_device *idev, u32 imr_id, struct imr_regs *imr) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base; 11362306a36Sopenharmony_ci int ret; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->addr_lo); 11662306a36Sopenharmony_ci if (ret) 11762306a36Sopenharmony_ci return ret; 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->addr_hi); 12062306a36Sopenharmony_ci if (ret) 12162306a36Sopenharmony_ci return ret; 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci ret = iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->rmask); 12462306a36Sopenharmony_ci if (ret) 12562306a36Sopenharmony_ci return ret; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci return iosf_mbi_read(QRK_MBI_UNIT_MM, MBI_REG_READ, reg++, &imr->wmask); 12862306a36Sopenharmony_ci} 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci/** 13162306a36Sopenharmony_ci * imr_write - write an IMR at a given index. 13262306a36Sopenharmony_ci * 13362306a36Sopenharmony_ci * Requires caller to hold imr mutex. 13462306a36Sopenharmony_ci * Note lock bits need to be written independently of address bits. 13562306a36Sopenharmony_ci * 13662306a36Sopenharmony_ci * @idev: pointer to imr_device structure. 13762306a36Sopenharmony_ci * @imr_id: IMR entry to write. 13862306a36Sopenharmony_ci * @imr: IMR structure representing address and access masks. 13962306a36Sopenharmony_ci * @return: 0 on success or error code passed from mbi_iosf on failure. 14062306a36Sopenharmony_ci */ 14162306a36Sopenharmony_cistatic int imr_write(struct imr_device *idev, u32 imr_id, struct imr_regs *imr) 14262306a36Sopenharmony_ci{ 14362306a36Sopenharmony_ci unsigned long flags; 14462306a36Sopenharmony_ci u32 reg = imr_id * IMR_NUM_REGS + idev->reg_base; 14562306a36Sopenharmony_ci int ret; 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci local_irq_save(flags); 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->addr_lo); 15062306a36Sopenharmony_ci if (ret) 15162306a36Sopenharmony_ci goto failed; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->addr_hi); 15462306a36Sopenharmony_ci if (ret) 15562306a36Sopenharmony_ci goto failed; 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->rmask); 15862306a36Sopenharmony_ci if (ret) 15962306a36Sopenharmony_ci goto failed; 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci ret = iosf_mbi_write(QRK_MBI_UNIT_MM, MBI_REG_WRITE, reg++, imr->wmask); 16262306a36Sopenharmony_ci if (ret) 16362306a36Sopenharmony_ci goto failed; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci local_irq_restore(flags); 16662306a36Sopenharmony_ci return 0; 16762306a36Sopenharmony_cifailed: 16862306a36Sopenharmony_ci /* 16962306a36Sopenharmony_ci * If writing to the IOSF failed then we're in an unknown state, 17062306a36Sopenharmony_ci * likely a very bad state. An IMR in an invalid state will almost 17162306a36Sopenharmony_ci * certainly lead to a memory access violation. 17262306a36Sopenharmony_ci */ 17362306a36Sopenharmony_ci local_irq_restore(flags); 17462306a36Sopenharmony_ci WARN(ret, "IOSF-MBI write fail range 0x%08x-0x%08x unreliable\n", 17562306a36Sopenharmony_ci imr_to_phys(imr->addr_lo), imr_to_phys(imr->addr_hi) + IMR_MASK); 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci return ret; 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci/** 18162306a36Sopenharmony_ci * imr_dbgfs_state_show - print state of IMR registers. 18262306a36Sopenharmony_ci * 18362306a36Sopenharmony_ci * @s: pointer to seq_file for output. 18462306a36Sopenharmony_ci * @unused: unused parameter. 18562306a36Sopenharmony_ci * @return: 0 on success or error code passed from mbi_iosf on failure. 18662306a36Sopenharmony_ci */ 18762306a36Sopenharmony_cistatic int imr_dbgfs_state_show(struct seq_file *s, void *unused) 18862306a36Sopenharmony_ci{ 18962306a36Sopenharmony_ci phys_addr_t base; 19062306a36Sopenharmony_ci phys_addr_t end; 19162306a36Sopenharmony_ci int i; 19262306a36Sopenharmony_ci struct imr_device *idev = s->private; 19362306a36Sopenharmony_ci struct imr_regs imr; 19462306a36Sopenharmony_ci size_t size; 19562306a36Sopenharmony_ci int ret = -ENODEV; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci mutex_lock(&idev->lock); 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci for (i = 0; i < idev->max_imr; i++) { 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci ret = imr_read(idev, i, &imr); 20262306a36Sopenharmony_ci if (ret) 20362306a36Sopenharmony_ci break; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci /* 20662306a36Sopenharmony_ci * Remember to add IMR_ALIGN bytes to size to indicate the 20762306a36Sopenharmony_ci * inherent IMR_ALIGN size bytes contained in the masked away 20862306a36Sopenharmony_ci * lower ten bits. 20962306a36Sopenharmony_ci */ 21062306a36Sopenharmony_ci if (imr_is_enabled(&imr)) { 21162306a36Sopenharmony_ci base = imr_to_phys(imr.addr_lo); 21262306a36Sopenharmony_ci end = imr_to_phys(imr.addr_hi) + IMR_MASK; 21362306a36Sopenharmony_ci size = end - base + 1; 21462306a36Sopenharmony_ci } else { 21562306a36Sopenharmony_ci base = 0; 21662306a36Sopenharmony_ci end = 0; 21762306a36Sopenharmony_ci size = 0; 21862306a36Sopenharmony_ci } 21962306a36Sopenharmony_ci seq_printf(s, "imr%02i: base=%pa, end=%pa, size=0x%08zx " 22062306a36Sopenharmony_ci "rmask=0x%08x, wmask=0x%08x, %s, %s\n", i, 22162306a36Sopenharmony_ci &base, &end, size, imr.rmask, imr.wmask, 22262306a36Sopenharmony_ci imr_is_enabled(&imr) ? "enabled " : "disabled", 22362306a36Sopenharmony_ci imr.addr_lo & IMR_LOCK ? "locked" : "unlocked"); 22462306a36Sopenharmony_ci } 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci mutex_unlock(&idev->lock); 22762306a36Sopenharmony_ci return ret; 22862306a36Sopenharmony_ci} 22962306a36Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(imr_dbgfs_state); 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci/** 23262306a36Sopenharmony_ci * imr_debugfs_register - register debugfs hooks. 23362306a36Sopenharmony_ci * 23462306a36Sopenharmony_ci * @idev: pointer to imr_device structure. 23562306a36Sopenharmony_ci */ 23662306a36Sopenharmony_cistatic void imr_debugfs_register(struct imr_device *idev) 23762306a36Sopenharmony_ci{ 23862306a36Sopenharmony_ci debugfs_create_file("imr_state", 0444, NULL, idev, 23962306a36Sopenharmony_ci &imr_dbgfs_state_fops); 24062306a36Sopenharmony_ci} 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci/** 24362306a36Sopenharmony_ci * imr_check_params - check passed address range IMR alignment and non-zero size 24462306a36Sopenharmony_ci * 24562306a36Sopenharmony_ci * @base: base address of intended IMR. 24662306a36Sopenharmony_ci * @size: size of intended IMR. 24762306a36Sopenharmony_ci * @return: zero on valid range -EINVAL on unaligned base/size. 24862306a36Sopenharmony_ci */ 24962306a36Sopenharmony_cistatic int imr_check_params(phys_addr_t base, size_t size) 25062306a36Sopenharmony_ci{ 25162306a36Sopenharmony_ci if ((base & IMR_MASK) || (size & IMR_MASK)) { 25262306a36Sopenharmony_ci pr_err("base %pa size 0x%08zx must align to 1KiB\n", 25362306a36Sopenharmony_ci &base, size); 25462306a36Sopenharmony_ci return -EINVAL; 25562306a36Sopenharmony_ci } 25662306a36Sopenharmony_ci if (size == 0) 25762306a36Sopenharmony_ci return -EINVAL; 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci return 0; 26062306a36Sopenharmony_ci} 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci/** 26362306a36Sopenharmony_ci * imr_raw_size - account for the IMR_ALIGN bytes that addr_hi appends. 26462306a36Sopenharmony_ci * 26562306a36Sopenharmony_ci * IMR addr_hi has a built in offset of plus IMR_ALIGN (0x400) bytes from the 26662306a36Sopenharmony_ci * value in the register. We need to subtract IMR_ALIGN bytes from input sizes 26762306a36Sopenharmony_ci * as a result. 26862306a36Sopenharmony_ci * 26962306a36Sopenharmony_ci * @size: input size bytes. 27062306a36Sopenharmony_ci * @return: reduced size. 27162306a36Sopenharmony_ci */ 27262306a36Sopenharmony_cistatic inline size_t imr_raw_size(size_t size) 27362306a36Sopenharmony_ci{ 27462306a36Sopenharmony_ci return size - IMR_ALIGN; 27562306a36Sopenharmony_ci} 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ci/** 27862306a36Sopenharmony_ci * imr_address_overlap - detects an address overlap. 27962306a36Sopenharmony_ci * 28062306a36Sopenharmony_ci * @addr: address to check against an existing IMR. 28162306a36Sopenharmony_ci * @imr: imr being checked. 28262306a36Sopenharmony_ci * @return: true for overlap false for no overlap. 28362306a36Sopenharmony_ci */ 28462306a36Sopenharmony_cistatic inline int imr_address_overlap(phys_addr_t addr, struct imr_regs *imr) 28562306a36Sopenharmony_ci{ 28662306a36Sopenharmony_ci return addr >= imr_to_phys(imr->addr_lo) && addr <= imr_to_phys(imr->addr_hi); 28762306a36Sopenharmony_ci} 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci/** 29062306a36Sopenharmony_ci * imr_add_range - add an Isolated Memory Region. 29162306a36Sopenharmony_ci * 29262306a36Sopenharmony_ci * @base: physical base address of region aligned to 1KiB. 29362306a36Sopenharmony_ci * @size: physical size of region in bytes must be aligned to 1KiB. 29462306a36Sopenharmony_ci * @read_mask: read access mask. 29562306a36Sopenharmony_ci * @write_mask: write access mask. 29662306a36Sopenharmony_ci * @return: zero on success or negative value indicating error. 29762306a36Sopenharmony_ci */ 29862306a36Sopenharmony_ciint imr_add_range(phys_addr_t base, size_t size, 29962306a36Sopenharmony_ci unsigned int rmask, unsigned int wmask) 30062306a36Sopenharmony_ci{ 30162306a36Sopenharmony_ci phys_addr_t end; 30262306a36Sopenharmony_ci unsigned int i; 30362306a36Sopenharmony_ci struct imr_device *idev = &imr_dev; 30462306a36Sopenharmony_ci struct imr_regs imr; 30562306a36Sopenharmony_ci size_t raw_size; 30662306a36Sopenharmony_ci int reg; 30762306a36Sopenharmony_ci int ret; 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ci if (WARN_ONCE(idev->init == false, "driver not initialized")) 31062306a36Sopenharmony_ci return -ENODEV; 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci ret = imr_check_params(base, size); 31362306a36Sopenharmony_ci if (ret) 31462306a36Sopenharmony_ci return ret; 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ci /* Tweak the size value. */ 31762306a36Sopenharmony_ci raw_size = imr_raw_size(size); 31862306a36Sopenharmony_ci end = base + raw_size; 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci /* 32162306a36Sopenharmony_ci * Check for reserved IMR value common to firmware, kernel and grub 32262306a36Sopenharmony_ci * indicating a disabled IMR. 32362306a36Sopenharmony_ci */ 32462306a36Sopenharmony_ci imr.addr_lo = phys_to_imr(base); 32562306a36Sopenharmony_ci imr.addr_hi = phys_to_imr(end); 32662306a36Sopenharmony_ci imr.rmask = rmask; 32762306a36Sopenharmony_ci imr.wmask = wmask; 32862306a36Sopenharmony_ci if (!imr_is_enabled(&imr)) 32962306a36Sopenharmony_ci return -ENOTSUPP; 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci mutex_lock(&idev->lock); 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci /* 33462306a36Sopenharmony_ci * Find a free IMR while checking for an existing overlapping range. 33562306a36Sopenharmony_ci * Note there's no restriction in silicon to prevent IMR overlaps. 33662306a36Sopenharmony_ci * For the sake of simplicity and ease in defining/debugging an IMR 33762306a36Sopenharmony_ci * memory map we exclude IMR overlaps. 33862306a36Sopenharmony_ci */ 33962306a36Sopenharmony_ci reg = -1; 34062306a36Sopenharmony_ci for (i = 0; i < idev->max_imr; i++) { 34162306a36Sopenharmony_ci ret = imr_read(idev, i, &imr); 34262306a36Sopenharmony_ci if (ret) 34362306a36Sopenharmony_ci goto failed; 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci /* Find overlap @ base or end of requested range. */ 34662306a36Sopenharmony_ci ret = -EINVAL; 34762306a36Sopenharmony_ci if (imr_is_enabled(&imr)) { 34862306a36Sopenharmony_ci if (imr_address_overlap(base, &imr)) 34962306a36Sopenharmony_ci goto failed; 35062306a36Sopenharmony_ci if (imr_address_overlap(end, &imr)) 35162306a36Sopenharmony_ci goto failed; 35262306a36Sopenharmony_ci } else { 35362306a36Sopenharmony_ci reg = i; 35462306a36Sopenharmony_ci } 35562306a36Sopenharmony_ci } 35662306a36Sopenharmony_ci 35762306a36Sopenharmony_ci /* Error out if we have no free IMR entries. */ 35862306a36Sopenharmony_ci if (reg == -1) { 35962306a36Sopenharmony_ci ret = -ENOMEM; 36062306a36Sopenharmony_ci goto failed; 36162306a36Sopenharmony_ci } 36262306a36Sopenharmony_ci 36362306a36Sopenharmony_ci pr_debug("add %d phys %pa-%pa size %zx mask 0x%08x wmask 0x%08x\n", 36462306a36Sopenharmony_ci reg, &base, &end, raw_size, rmask, wmask); 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ci /* Enable IMR at specified range and access mask. */ 36762306a36Sopenharmony_ci imr.addr_lo = phys_to_imr(base); 36862306a36Sopenharmony_ci imr.addr_hi = phys_to_imr(end); 36962306a36Sopenharmony_ci imr.rmask = rmask; 37062306a36Sopenharmony_ci imr.wmask = wmask; 37162306a36Sopenharmony_ci 37262306a36Sopenharmony_ci ret = imr_write(idev, reg, &imr); 37362306a36Sopenharmony_ci if (ret < 0) { 37462306a36Sopenharmony_ci /* 37562306a36Sopenharmony_ci * In the highly unlikely event iosf_mbi_write failed 37662306a36Sopenharmony_ci * attempt to rollback the IMR setup skipping the trapping 37762306a36Sopenharmony_ci * of further IOSF write failures. 37862306a36Sopenharmony_ci */ 37962306a36Sopenharmony_ci imr.addr_lo = 0; 38062306a36Sopenharmony_ci imr.addr_hi = 0; 38162306a36Sopenharmony_ci imr.rmask = IMR_READ_ACCESS_ALL; 38262306a36Sopenharmony_ci imr.wmask = IMR_WRITE_ACCESS_ALL; 38362306a36Sopenharmony_ci imr_write(idev, reg, &imr); 38462306a36Sopenharmony_ci } 38562306a36Sopenharmony_cifailed: 38662306a36Sopenharmony_ci mutex_unlock(&idev->lock); 38762306a36Sopenharmony_ci return ret; 38862306a36Sopenharmony_ci} 38962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(imr_add_range); 39062306a36Sopenharmony_ci 39162306a36Sopenharmony_ci/** 39262306a36Sopenharmony_ci * __imr_remove_range - delete an Isolated Memory Region. 39362306a36Sopenharmony_ci * 39462306a36Sopenharmony_ci * This function allows you to delete an IMR by its index specified by reg or 39562306a36Sopenharmony_ci * by address range specified by base and size respectively. If you specify an 39662306a36Sopenharmony_ci * index on its own the base and size parameters are ignored. 39762306a36Sopenharmony_ci * imr_remove_range(0, base, size); delete IMR at index 0 base/size ignored. 39862306a36Sopenharmony_ci * imr_remove_range(-1, base, size); delete IMR from base to base+size. 39962306a36Sopenharmony_ci * 40062306a36Sopenharmony_ci * @reg: imr index to remove. 40162306a36Sopenharmony_ci * @base: physical base address of region aligned to 1 KiB. 40262306a36Sopenharmony_ci * @size: physical size of region in bytes aligned to 1 KiB. 40362306a36Sopenharmony_ci * @return: -EINVAL on invalid range or out or range id 40462306a36Sopenharmony_ci * -ENODEV if reg is valid but no IMR exists or is locked 40562306a36Sopenharmony_ci * 0 on success. 40662306a36Sopenharmony_ci */ 40762306a36Sopenharmony_cistatic int __imr_remove_range(int reg, phys_addr_t base, size_t size) 40862306a36Sopenharmony_ci{ 40962306a36Sopenharmony_ci phys_addr_t end; 41062306a36Sopenharmony_ci bool found = false; 41162306a36Sopenharmony_ci unsigned int i; 41262306a36Sopenharmony_ci struct imr_device *idev = &imr_dev; 41362306a36Sopenharmony_ci struct imr_regs imr; 41462306a36Sopenharmony_ci size_t raw_size; 41562306a36Sopenharmony_ci int ret = 0; 41662306a36Sopenharmony_ci 41762306a36Sopenharmony_ci if (WARN_ONCE(idev->init == false, "driver not initialized")) 41862306a36Sopenharmony_ci return -ENODEV; 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci /* 42162306a36Sopenharmony_ci * Validate address range if deleting by address, else we are 42262306a36Sopenharmony_ci * deleting by index where base and size will be ignored. 42362306a36Sopenharmony_ci */ 42462306a36Sopenharmony_ci if (reg == -1) { 42562306a36Sopenharmony_ci ret = imr_check_params(base, size); 42662306a36Sopenharmony_ci if (ret) 42762306a36Sopenharmony_ci return ret; 42862306a36Sopenharmony_ci } 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci /* Tweak the size value. */ 43162306a36Sopenharmony_ci raw_size = imr_raw_size(size); 43262306a36Sopenharmony_ci end = base + raw_size; 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci mutex_lock(&idev->lock); 43562306a36Sopenharmony_ci 43662306a36Sopenharmony_ci if (reg >= 0) { 43762306a36Sopenharmony_ci /* If a specific IMR is given try to use it. */ 43862306a36Sopenharmony_ci ret = imr_read(idev, reg, &imr); 43962306a36Sopenharmony_ci if (ret) 44062306a36Sopenharmony_ci goto failed; 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK) { 44362306a36Sopenharmony_ci ret = -ENODEV; 44462306a36Sopenharmony_ci goto failed; 44562306a36Sopenharmony_ci } 44662306a36Sopenharmony_ci found = true; 44762306a36Sopenharmony_ci } else { 44862306a36Sopenharmony_ci /* Search for match based on address range. */ 44962306a36Sopenharmony_ci for (i = 0; i < idev->max_imr; i++) { 45062306a36Sopenharmony_ci ret = imr_read(idev, i, &imr); 45162306a36Sopenharmony_ci if (ret) 45262306a36Sopenharmony_ci goto failed; 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_ci if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK) 45562306a36Sopenharmony_ci continue; 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci if ((imr_to_phys(imr.addr_lo) == base) && 45862306a36Sopenharmony_ci (imr_to_phys(imr.addr_hi) == end)) { 45962306a36Sopenharmony_ci found = true; 46062306a36Sopenharmony_ci reg = i; 46162306a36Sopenharmony_ci break; 46262306a36Sopenharmony_ci } 46362306a36Sopenharmony_ci } 46462306a36Sopenharmony_ci } 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci if (!found) { 46762306a36Sopenharmony_ci ret = -ENODEV; 46862306a36Sopenharmony_ci goto failed; 46962306a36Sopenharmony_ci } 47062306a36Sopenharmony_ci 47162306a36Sopenharmony_ci pr_debug("remove %d phys %pa-%pa size %zx\n", reg, &base, &end, raw_size); 47262306a36Sopenharmony_ci 47362306a36Sopenharmony_ci /* Tear down the IMR. */ 47462306a36Sopenharmony_ci imr.addr_lo = 0; 47562306a36Sopenharmony_ci imr.addr_hi = 0; 47662306a36Sopenharmony_ci imr.rmask = IMR_READ_ACCESS_ALL; 47762306a36Sopenharmony_ci imr.wmask = IMR_WRITE_ACCESS_ALL; 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_ci ret = imr_write(idev, reg, &imr); 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_cifailed: 48262306a36Sopenharmony_ci mutex_unlock(&idev->lock); 48362306a36Sopenharmony_ci return ret; 48462306a36Sopenharmony_ci} 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_ci/** 48762306a36Sopenharmony_ci * imr_remove_range - delete an Isolated Memory Region by address 48862306a36Sopenharmony_ci * 48962306a36Sopenharmony_ci * This function allows you to delete an IMR by an address range specified 49062306a36Sopenharmony_ci * by base and size respectively. 49162306a36Sopenharmony_ci * imr_remove_range(base, size); delete IMR from base to base+size. 49262306a36Sopenharmony_ci * 49362306a36Sopenharmony_ci * @base: physical base address of region aligned to 1 KiB. 49462306a36Sopenharmony_ci * @size: physical size of region in bytes aligned to 1 KiB. 49562306a36Sopenharmony_ci * @return: -EINVAL on invalid range or out or range id 49662306a36Sopenharmony_ci * -ENODEV if reg is valid but no IMR exists or is locked 49762306a36Sopenharmony_ci * 0 on success. 49862306a36Sopenharmony_ci */ 49962306a36Sopenharmony_ciint imr_remove_range(phys_addr_t base, size_t size) 50062306a36Sopenharmony_ci{ 50162306a36Sopenharmony_ci return __imr_remove_range(-1, base, size); 50262306a36Sopenharmony_ci} 50362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(imr_remove_range); 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci/** 50662306a36Sopenharmony_ci * imr_clear - delete an Isolated Memory Region by index 50762306a36Sopenharmony_ci * 50862306a36Sopenharmony_ci * This function allows you to delete an IMR by an address range specified 50962306a36Sopenharmony_ci * by the index of the IMR. Useful for initial sanitization of the IMR 51062306a36Sopenharmony_ci * address map. 51162306a36Sopenharmony_ci * imr_ge(base, size); delete IMR from base to base+size. 51262306a36Sopenharmony_ci * 51362306a36Sopenharmony_ci * @reg: imr index to remove. 51462306a36Sopenharmony_ci * @return: -EINVAL on invalid range or out or range id 51562306a36Sopenharmony_ci * -ENODEV if reg is valid but no IMR exists or is locked 51662306a36Sopenharmony_ci * 0 on success. 51762306a36Sopenharmony_ci */ 51862306a36Sopenharmony_cistatic inline int imr_clear(int reg) 51962306a36Sopenharmony_ci{ 52062306a36Sopenharmony_ci return __imr_remove_range(reg, 0, 0); 52162306a36Sopenharmony_ci} 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ci/** 52462306a36Sopenharmony_ci * imr_fixup_memmap - Tear down IMRs used during bootup. 52562306a36Sopenharmony_ci * 52662306a36Sopenharmony_ci * BIOS and Grub both setup IMRs around compressed kernel, initrd memory 52762306a36Sopenharmony_ci * that need to be removed before the kernel hands out one of the IMR 52862306a36Sopenharmony_ci * encased addresses to a downstream DMA agent such as the SD or Ethernet. 52962306a36Sopenharmony_ci * IMRs on Galileo are setup to immediately reset the system on violation. 53062306a36Sopenharmony_ci * As a result if you're running a root filesystem from SD - you'll need 53162306a36Sopenharmony_ci * the boot-time IMRs torn down or you'll find seemingly random resets when 53262306a36Sopenharmony_ci * using your filesystem. 53362306a36Sopenharmony_ci * 53462306a36Sopenharmony_ci * @idev: pointer to imr_device structure. 53562306a36Sopenharmony_ci * @return: 53662306a36Sopenharmony_ci */ 53762306a36Sopenharmony_cistatic void __init imr_fixup_memmap(struct imr_device *idev) 53862306a36Sopenharmony_ci{ 53962306a36Sopenharmony_ci phys_addr_t base = virt_to_phys(&_text); 54062306a36Sopenharmony_ci size_t size = virt_to_phys(&__end_rodata) - base; 54162306a36Sopenharmony_ci unsigned long start, end; 54262306a36Sopenharmony_ci int i; 54362306a36Sopenharmony_ci int ret; 54462306a36Sopenharmony_ci 54562306a36Sopenharmony_ci /* Tear down all existing unlocked IMRs. */ 54662306a36Sopenharmony_ci for (i = 0; i < idev->max_imr; i++) 54762306a36Sopenharmony_ci imr_clear(i); 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci start = (unsigned long)_text; 55062306a36Sopenharmony_ci end = (unsigned long)__end_rodata - 1; 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ci /* 55362306a36Sopenharmony_ci * Setup an unlocked IMR around the physical extent of the kernel 55462306a36Sopenharmony_ci * from the beginning of the .text section to the end of the 55562306a36Sopenharmony_ci * .rodata section as one physically contiguous block. 55662306a36Sopenharmony_ci * 55762306a36Sopenharmony_ci * We don't round up @size since it is already PAGE_SIZE aligned. 55862306a36Sopenharmony_ci * See vmlinux.lds.S for details. 55962306a36Sopenharmony_ci */ 56062306a36Sopenharmony_ci ret = imr_add_range(base, size, IMR_CPU, IMR_CPU); 56162306a36Sopenharmony_ci if (ret < 0) { 56262306a36Sopenharmony_ci pr_err("unable to setup IMR for kernel: %zu KiB (%lx - %lx)\n", 56362306a36Sopenharmony_ci size / 1024, start, end); 56462306a36Sopenharmony_ci } else { 56562306a36Sopenharmony_ci pr_info("protecting kernel .text - .rodata: %zu KiB (%lx - %lx)\n", 56662306a36Sopenharmony_ci size / 1024, start, end); 56762306a36Sopenharmony_ci } 56862306a36Sopenharmony_ci 56962306a36Sopenharmony_ci} 57062306a36Sopenharmony_ci 57162306a36Sopenharmony_cistatic const struct x86_cpu_id imr_ids[] __initconst = { 57262306a36Sopenharmony_ci X86_MATCH_VENDOR_FAM_MODEL(INTEL, 5, INTEL_FAM5_QUARK_X1000, NULL), 57362306a36Sopenharmony_ci {} 57462306a36Sopenharmony_ci}; 57562306a36Sopenharmony_ci 57662306a36Sopenharmony_ci/** 57762306a36Sopenharmony_ci * imr_init - entry point for IMR driver. 57862306a36Sopenharmony_ci * 57962306a36Sopenharmony_ci * return: -ENODEV for no IMR support 0 if good to go. 58062306a36Sopenharmony_ci */ 58162306a36Sopenharmony_cistatic int __init imr_init(void) 58262306a36Sopenharmony_ci{ 58362306a36Sopenharmony_ci struct imr_device *idev = &imr_dev; 58462306a36Sopenharmony_ci 58562306a36Sopenharmony_ci if (!x86_match_cpu(imr_ids) || !iosf_mbi_available()) 58662306a36Sopenharmony_ci return -ENODEV; 58762306a36Sopenharmony_ci 58862306a36Sopenharmony_ci idev->max_imr = QUARK_X1000_IMR_MAX; 58962306a36Sopenharmony_ci idev->reg_base = QUARK_X1000_IMR_REGBASE; 59062306a36Sopenharmony_ci idev->init = true; 59162306a36Sopenharmony_ci 59262306a36Sopenharmony_ci mutex_init(&idev->lock); 59362306a36Sopenharmony_ci imr_debugfs_register(idev); 59462306a36Sopenharmony_ci imr_fixup_memmap(idev); 59562306a36Sopenharmony_ci return 0; 59662306a36Sopenharmony_ci} 59762306a36Sopenharmony_cidevice_initcall(imr_init); 598