Skip to contentSkip to conversation

x86: Add IMR support to Quark/Galileo

PATCH Superseded Bryan O'Donoghue 5 participants 16 messages Jan 29, 2015

Subsystems (2)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover x86: Add IMR support to Quark/Galileo

This patchset adds support for Isolated Memory Regions to the kernel.

Quark SoC X1000 contains a set of registers called Isolated Memory Regions. IMRs provide fine grained memory access control to various system agents within the SoC such as CPU SMM/non-SMM mode, PCIe virtual channels, CPU snoop cycles, eSRAM flush cycles and the RMU. In simple terms, IMRs provide a mechanism to protect memory regions from unwarranted access by system agents that should not have access to that memory.

IMRs support a lock bit. Once a lock bit is set for an individual IMR it is not possible to tear down that IMR without performing a cold boot of the system. IMRs support reporting of violations. The SoC system can be configured to reboot immediately when an IMR violation has taken place. Immediate reboot of the system on IMR violation is recommended and is currently how Quark BIOS configures the system.

An example of IMRs in use is given with Arduino compatiable Galileo boards which ship with an IMR around the ACPI runtime services memory. If a DMA read/write cycle were to occur to this region of memory this would trigger the IMR violation mechansim.

As part of the IMR init code all unlocked IMRs are removed to ensure the EFI memory map and IMR memory map are consistent. This is necessary since at various stages during the boot of Quark systems firmware and second stage bootloader will place unlocked IMRs around various assets in memory, with the expectation that subsequent phases of boot will tear-down unlocked/stale IMRs before proceeding. The kernel needs to tear-down unlocked IMRs placed around the boot params structure and compressed kernel in memory. Without doing so DMA addresses given out by the kernel to DMA capable hardware runs the risk of triggering an IMR fault when DMA happens to those addresses. As a result any unlocked IMR must be torn down by the kernel early in the boot process to sanitize the memory map.

As an additional protection to the run-time kernel from unwarranted memory transactions an IMR is placed around the kernel's .text and .rodata sections.

Changes since v5:
- Full stops
Added to all comments - including one-liners.
Andy Shevchenko
- %zu and %zx
Used to print size_t variables
Andy Shevchenko
- imr_selftest
Split into seperate module
Andy Shevchenko
Changes since v4:
- Quark added as a platform in a separate patch to IMRs
Andy Shevchenko
- imr_read
Add extra reg++ to end of read for consistency
Andy Shevchenko
- ret
Declarations of return variable moved to end of each declaration block Andy Shevchenko
- Parameters passed to debugfs hooks
Andy Shevchenko
- struct imr_dev *idev = &imr_dev;
Used globally for entry point functions
Andy Shevchenko/Bryan O'Donoghue
- struct imr_dev * passed to static functions
Andy Shevchenko/Bryan O'Donoghue
- Comment in imr_fixup_memmap
Paried down and made more readable
Andy Shevchenko
- imr_fixup_memmap/imr_selftest
phys_addr_t used for base
Andy Shevchenko
- phys_addr_t/size_t
Supplemented globally in place of unsigned long where appropriate Bryan O'Donoghue
- print routines to take %pa globally
In place of pr_info("0x%08lx\n", (unsigned long)some_phs_addr_t_variable); Andy Shevchenko
- debugfs hook moved to after init = true
Andy Shevchenko

Changes since v3:
- Remove reference to imr.o in arch/x86/kernel/Makefile Bryan O'Donoghue

Changes since v2:
- Move IMR code to arch/x86/platform/intel-quark/imr.c
Thomas Gleixner/Darren Hart
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Andy Shevchenko
- ret = iosf_mbi_read()
Style made consistent in imr_write
Andy Shevchenko
- reg++/IMR_NUM_REGS
Offset used for lock bit in imr_write
Andy Shevchenko
- debugfs s->private pointer used
Andy Shevchenko
- debugfs
Conditional compilation defines removed
Andy Shevchenko
- debugfs
Failure to hook debugfs treated as non-fatal
Andy Shevchenko
- phys_addr_t
Updated API to use phys_addr_t in place of unsigned long for base param Andy Shevchenko
- printk
"KiB" instead of "k"
Andy Shevchenko
- imr_enabled
-> static inline imr_is_enabled
Ong, Boon Leong/Andy Shevchenko
- imr_write
trap final ret from iosf_mbi_write for lock bit in imr_write - bugfix Ong, Boon Leong
- imr_fixup_size
-> static inline imr_fixup_size
Ong, Boon Leong
- imr_address_overlap
-> imr_address_overlap
Ong, Boon Leong
- imr_add_range
End address in imr_add_range calculated after imr_fixup_size() Ong, Boon Leong
- imr_del_range
Pass i in place of reg in imr_del_range() - bugfix
Ong, Boon Leong
- Add test case
imr_del_range(-1, addr, size)
Ong, Boon Leong/Andy Shevchenko
- Added text "aligned to 1 KiB" removed reference to "4 k"
Ong, Boon Leong
- imr_is_enabled
Definition of enabled updated to be negation of disabled
Bryan O'Donoghue
- imr_add_range
Add check for adding an IMR in the disabled state
Bryan O'Donoghue
- Add test case IMR @ invalid address, @0 with rmask/wmask=CPU, @ 0 size 0x800 Bryan O'Donoghue
- Add WARN() to failed IMR test in print routine
Bryan O'Donoghue
- Update license to Dual BSD/GPL
Reflect licensing in Intel reference code
Bryan O'Donoghue

Changes since v1:
- Galileo platform code
Removed completely. Policy to tear-down unlocked IMRs and setup IMR around kernel .text and .rodata as part of IMR init code. Darren Hart/Ong, Boon Leong
- imr_add/imr_del
Renamed to imr_add_range and imr_del_range respectively. Andy Shevchenko
- x86_match_cpu
Used in place of DMI strings specific to Galileo.
Andy Shevchenko/Ong, Boon Leong
- Expanded git log definitions of IMRs
Addition of more descriptive text to deliniate between different IMR types.
Ong, Boon Leong
- struct imr
Renamed to struct imr_regs
Andy Shevchenko/Darren Hart
- imr_read/imr_write
Flow reworked flow of register indexing
Andy Shevchenko
- debugfs hooks changed
Andy Shevchenko
- imr_enabled
Definition of an enabled IMR updated to include read/write mask values present in IMR. Address @ zero and read/write mask in conjunction will be the definition of a disabled IMR on X1000 to be consistent with firmware both old and current which also defines a disabled IMR this way.
Darren Hart/Ong, Boon Leong
- Overlapping
Comment added to code to explain the design decision not to allow IMR overlaps.
Darren Hart/Ong, Boon Leong
- CONFIG_DEBUG_IMR_SELFTEST
Automated IMR self test moved from removed Galileo platform code and added to IMR init code. Option exists in the kernel hacking section. Darren Hart
- IMR self test
Expanded to over more scenarios
Bryan O'Donoghue
- Remove reference to IMR_ENABLE bit
Undocumented bit with respect to Quark X1000
Ong, Boon Leong
- Expanded kernel IMR to encompass .text and .rodata
IMR protecting both .text and .rodata as in the same way as .text and .rodata are marked read-only in the relevant page-table entries. Bryan O'Donoghue
- Overlap bounds checking
Moved range checking of overlap into a function
Andy Shevchenko

Bryan O'Donoghue (2):
x86: Add Isolated Memory Regions for Quark X1000 x86, quark: Add Intel Quark platform support

 arch/x86/Kconfig                             |  16 +
 arch/x86/Kconfig.debug                       |  12 +
 arch/x86/include/asm/imr.h                   |  60 +++
 arch/x86/platform/Makefile                   |   1 +
 arch/x86/platform/intel-quark/Makefile       |   2 +
 arch/x86/platform/intel-quark/imr.c          | 616 +++++++++++++++++++++++++++
 arch/x86/platform/intel-quark/imr_selftest.c | 135 ++++++
 drivers/platform/x86/Kconfig                 |  25 ++
 8 files changed, 867 insertions(+)
 create mode 100644 arch/x86/include/asm/imr.h
 create mode 100644 arch/x86/platform/intel-quark/Makefile
 create mode 100644 arch/x86/platform/intel-quark/imr.c
 create mode 100644 arch/x86/platform/intel-quark/imr_selftest.c
Signature · 1 line
1.9.1
#1 lore raw

PATCH v6 1/2 x86: Add Isolated Memory Regions for Quark X1000

Intel's Quark X1000 SoC contains a set of registers called Isolated Memory Regions. IMRs are accessed over the IOSF mailbox interface. IMRs are areas carved out of memory that define read/write access rights to the various system agents within the Quark system. For a given agent in the system it is possible to specify if that agent may read or write an area of memory defined by an IMR with a granularity of 1 KiB.

Quark_SecureBootPRM_330234_001.pdf section 4.5 details the concept of IMRs quark-x1000-datasheet.pdf section 12.7.4 details the implementation of IMRs in silicon.

eSRAM flush, CPU Snoop write-only, CPU SMM Mode, CPU non-SMM mode, RMU and PCIe Virtual Channels (VC0 and VC1) can have individual read/write access masks applied to them for a given memory region in Quark X1000. This enables IMRs to treat each memory transaction type listed above on an individual basis and to filter appropriately based on the IMR access mask for the memory region. Quark supports eight IMRs.

Since all of the DMA capable SoC components in the X1000 are mapped to VC0 it is possible to define sections of memory as invalid for DMA write operations originating from Ethernet, USB, SD and any other DMA capable south-cluster component on VC0. Similarly it is possible to mark kernel memory as non-SMM mode read/write only or to mark BIOS runtime memory as SMM mode accessible only depending on the particular memory footprint on a given system.

On an IMR violation Quark SoC X1000 systems are configured to reset the system, so ensuring that the IMR memory map is consistent with the EFI provided memory map is critical to ensure no IMR violations reset the system.

The API for accessing IMRs is based on MTRR code but doesn't provide a /proc or /sys interface to manipulate IMRs. Defining the size and extent of IMRs is exclusively the domain of in-kernel code.

Quark firmware sets up a series of locked IMRs around pieces of memory that firmware owns such as ACPI runtime data. During boot a series of unlocked IMRs are placed around items in memory to guarantee no DMA modification of those items can take place. Grub also places an unlocked IMR around the kernel boot params data structure and compressed kernel image. It is necessary for the kernel to tear down all unlocked IMRs in order to ensure that the kernel's view of memory passed via the EFI memory map is consistent with the IMR memory map. Without tearing down all unlocked IMRs on boot transitory IMRs such as those used to protect the compressed kernel image will cause IMR violations and system reboots.

The IMR init code tears down all unlocked IMRs and sets a protective IMR around the kernel .text and .rodata as one contiguous block. This sanitizes the IMR memory map with respect to the EFI memory map and protects the read-only portions of the kernel from unwarranted DMA access.

---
 arch/x86/Kconfig.debug                       |  12 +
 arch/x86/include/asm/imr.h                   |  60 +++
 arch/x86/platform/intel-quark/Makefile       |   2 +
 arch/x86/platform/intel-quark/imr.c          | 616 +++++++++++++++++++++++++++
 arch/x86/platform/intel-quark/imr_selftest.c | 135 ++++++
 drivers/platform/x86/Kconfig                 |  25 ++
 6 files changed, 850 insertions(+)
 create mode 100644 arch/x86/include/asm/imr.h
 create mode 100644 arch/x86/platform/intel-quark/Makefile
 create mode 100644 arch/x86/platform/intel-quark/imr.c
 create mode 100644 arch/x86/platform/intel-quark/imr_selftest.c
arch/x86/Kconfig.debug +12 -0
index 61bd2ad..fcf5701 100644
@@ -313,6 +313,18 @@ config DEBUG_NMI_SELFTEST
 
 	  If unsure, say N.
 
+config DEBUG_IMR_SELFTEST
+	bool "Isolated Memory Region self test"
+	default n
+	depends on INTEL_IMR
+	---help---
+	  This option enables automated sanity testing of the IMR code.
+	  Some simple tests are run to verify IMR bounds checking, alignment
+	  and overlapping. This option is really only useful if you are
+	  debugging an IMR memory map or are modifying the IMR code and want to
+	  test your changes.
+
+	  If unsure say N here.
 config X86_DEBUG_STATIC_CPU_HAS
 	bool "Debug alternatives"
 	depends on DEBUG_KERNEL
arch/x86/include/asm/imr.h new file +60 -0

60 lines not on this page · raw message

arch/x86/platform/intel-quark/Makefile new file +2 -0
new file mode 100644
index 0000000..9cc57ed
@@ -0,0 +1,2 @@
+obj-$(CONFIG_INTEL_IMR) += imr.o
+obj-$(CONFIG_DEBUG_IMR_SELFTEST) += imr_selftest.o
arch/x86/platform/intel-quark/imr.c new file +616 -0

616 lines not on this page · raw message

arch/x86/platform/intel-quark/imr_selftest.c new file +135 -0

135 lines not on this page · raw message

drivers/platform/x86/Kconfig +25 -0
index 638e7970..9752761 100644
@@ -735,6 +735,31 @@ config INTEL_IPS
 	  functionality.  If in doubt, say Y here; it will only load on
 	  supported platforms.
 
+config INTEL_IMR
+	bool "Intel Isolated Memory Region support"
+	default n
+	depends on X86_INTEL_QUARK && IOSF_MBI
+	---help---
+	  This option provides a means to manipulate Isolated Memory Regions.
+	  IMRs are a set of registers that define read and write access masks
+	  to prohibit certain system agents from accessing memory with 1 KiB
+	  granularity.
+
+	  IMRs make it possible to control read/write access to an address
+	  by hardware agents inside the SoC. Read and write masks can be
+	  defined for:
+		- eSRAM flush
+		- Dirty CPU snoop (write only)
+		- RMU access
+		- PCI Virtual Channel 0/Virtual Channel 1
+		- SMM mode
+		- Non SMM mode
+
+	  Quark contains a set of eight IMR registers and makes use of those
+	  registers during its bootup process.
+
+	  If you are running on a Galileo/Quark say Y here.
+
 config IBM_RTL
 	tristate "Device driver to enable PRTL support"
 	depends on X86 && PCI
Signature · 1 line
1.9.1
Reviewed-by on v6 1/2
#2 lore raw

Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 46 lines · Bryan O'Donoghue

Intel's Quark X1000 SoC contains a set of registers called Isolated Memory Regions. IMRs are accessed over the IOSF mailbox interface. IMRs are areas carved out of memory that define read/write access rights to the various system agents within the Quark system. For a given agent in the system it is possible to specify if that agent may read or write an area of memory defined by an IMR with a granularity of 1 KiB.

Quark_SecureBootPRM_330234_001.pdf section 4.5 details the concept of IMRs quark-x1000-datasheet.pdf section 12.7.4 details the implementation of IMRs in silicon.

eSRAM flush, CPU Snoop write-only, CPU SMM Mode, CPU non-SMM mode, RMU and PCIe Virtual Channels (VC0 and VC1) can have individual read/write access masks applied to them for a given memory region in Quark X1000. This enables IMRs to treat each memory transaction type listed above on an individual basis and to filter appropriately based on the IMR access mask for the memory region. Quark supports eight IMRs.

Since all of the DMA capable SoC components in the X1000 are mapped to VC0 it is possible to define sections of memory as invalid for DMA write operations originating from Ethernet, USB, SD and any other DMA capable south-cluster component on VC0. Similarly it is possible to mark kernel memory as non-SMM mode read/write only or to mark BIOS runtime memory as SMM mode accessible only depending on the particular memory footprint on a given system.

On an IMR violation Quark SoC X1000 systems are configured to reset the system, so ensuring that the IMR memory map is consistent with the EFI provided memory map is critical to ensure no IMR violations reset the system.

The API for accessing IMRs is based on MTRR code but doesn't provide a /proc or /sys interface to manipulate IMRs. Defining the size and extent of IMRs is exclusively the domain of in-kernel code.

Quark firmware sets up a series of locked IMRs around pieces of memory that firmware owns such as ACPI runtime data. During boot a series of unlocked IMRs are placed around items in memory to guarantee no DMA modification of those items can take place. Grub also places an unlocked IMR around the kernel boot params data structure and compressed kernel image. It is necessary for the kernel to tear down all unlocked IMRs in order to ensure that the kernel's view of memory passed via the EFI memory map is consistent with the IMR memory map. Without tearing down all unlocked IMRs on boot transitory IMRs such as those used to protect the compressed kernel image will cause IMR violations and system reboots.

Quoted 6 lines

The IMR init code tears down all unlocked IMRs and sets a protective IMR around the kernel .text and .rodata as one contiguous block. This sanitizes the IMR memory map with respect to the EFI memory map and protects the read-only portions of the kernel from unwarranted DMA access.

Most of my concerns were addressed by V3 or so, but I've followed along and concur with the subsequent improvements.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#3 lore raw

Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

In reply to Bryan O'Donoghue
Quoted 7 lines · Bryan O'Donoghue

Intel's Quark X1000 SoC contains a set of registers called Isolated Memory Regions. IMRs are accessed over the IOSF mailbox interface. IMRs are areas carved out of memory that define read/write access rights to the various system agents within the Quark system. For a given agent in the system it is possible to specify if that agent may read or write an area of memory defined by an IMR with a granularity of 1 KiB.

A couple of minor details:

Quoted 16 lines
arch/x86/platform/intel-quark/imr.c new file +16 -0
+	if (ret)
+		goto done;
+
+	/* Lock bit must be set separately to addr_lo address bits. */
+	if (lock) {
+		imr->addr_lo |= IMR_LOCK;
+		ret = iosf_mbi_write(QRK_MBI_UNIT_MM, QRK_MBI_MM_WRITE,
+					reg - IMR_NUM_REGS, imr->addr_lo);
+		if (ret)
+			goto done;
+	}
+
+	local_irq_restore(flags);
+	return 0;
+done:
+	/*

arch/x86/platform/intel-quark/imr.c line 200

Quoted 10 lines
arch/x86/platform/intel-quark/imr.c new file +10 -0
+	 * If writing to the IOSF failed then we're in an unknown state,
+	 * likely a very bad state. An IMR in an invalid state will almost
+	 * certainly lead to a memory access violation.
+	 */
+	local_irq_restore(flags);
+	WARN(ret, "IOSF-MBI write fail range 0x%08x-0x%08x unreliable\n",
+	     imr_to_phys(imr->addr_lo), imr_to_phys(imr->addr_hi) + IMR_MASK);
+
+	return ret;
+}

looks like the 'done' label really wants to be 'error' or 'failed'?

Also, if this message ever triggers in practice, if the IMR is in a likely bad state, we might as well attempt to turn it off, so that we have a chance to get the log out and all that?

Quoted 24 lines
arch/x86/platform/intel-quark/imr.c new file +23 -0
+
+/**
+ * imr_dbgfs_state_show - print state of IMR registers.
+ *
+ * @s:		pointer to seq_file for output.
+ * @unused:	unused parameter.
+ * @return:	0 on success or error code passed from mbi_iosf on failure.
+ */
+static int imr_dbgfs_state_show(struct seq_file *s, void *unused)
+{
+	phys_addr_t base;
+	phys_addr_t end;
+	int i;
+	struct imr_device *idev = s->private;
+	struct imr_regs imr;
+	size_t size;
+	int ret = -ENODEV;
+
+	mutex_lock(&idev->lock);
+
+	for (i = 0; i < idev->max_imr; i++) {
 
+int imr_add_range(phys_addr_t base, size_t size,
+		  unsigned int rmask, unsigned int wmask, bool lock)

arch/x86/platform/intel-quark/imr.c

Quoted 10 lines
arch/x86/platform/intel-quark/imr.c new file +10 -0
+{
+	phys_addr_t end;
+	unsigned int i;
+	struct imr_device *idev = &imr_dev;
+	struct imr_regs imr;
+	int reg;
+	int ret;
+
+	if (idev->init == false)
+		return -ENODEV;

You might want to add a WARN_ONCE() here? Someone tried to add an IMR but has done it too early. Return values get ignored so often, kernel messages not so much.

(Just like imr_check_range() does emit warnings.)

arch/x86/platform/intel-quark/imr.c line 370

Quoted 6 lines
arch/x86/platform/intel-quark/imr.c new file +6 -0
+	ret = imr_check_range(base, size);
+	if (ret)
+		return ret;
+
+	if (size == 0)
+		return -EINVAL;

Looks like the zero-size check could be added to imr_check_range(), and be renamed to imr_check_params() or so?

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/* Tweak the size value. */
+	size = imr_fixup_size(size);

So why not add a 'raw_size' (or so) variable and use that, to not confuse user provided size with the offset-corrected size?

Then imr_fixup_size() could be renamed to imr_raw_size() as well, making it slightly easier on the eyes as well.

Quoted 42 lines
arch/x86/platform/intel-quark/imr.c new file +42 -0
+	end = base + size;
+
+	/*
+	 * Check for reserved IMR value common to firmware, kernel and grub
+	 * indicating a disabled IMR.
+	 */
+	imr.addr_lo = phys_to_imr(base);
+	imr.addr_hi = phys_to_imr(end);
+	imr.rmask = rmask;
+	imr.wmask = wmask;
+	if (!imr_is_enabled(&imr))
+		return -EINVAL;
+
+	mutex_lock(&idev->lock);
+
+	/*
+	 * Find a free IMR while checking for an existing overlapping range.
+	 * Note there's no restriction in silicon to prevent IMR overlaps.
+	 * For the sake of simplicity and ease in defining/debugging an IMR
+	 * memory map we exclude IMR overlaps.
+	 */
+	reg = -1;
+	for (i = 0; i < idev->max_imr; i++) {
+		ret = imr_read(idev, i, &imr);
+		if (ret)
+			goto done;
+
+		/* Find overlap @ base or end of requested range. */
+		if (imr_is_enabled(&imr)) {
+			if (imr_address_overlap(base, &imr)) {
+				ret = -EINVAL;
+				goto done;
+			}
+			if (imr_address_overlap(end, &imr)) {
+				ret = -EINVAL;
+				goto done;
+			}
+		} else {
+			reg = i;
+		}
+	}
+

arch/x86/platform/intel-quark/imr.c line 425

Quoted 10 lines
arch/x86/platform/intel-quark/imr.c new file +10 -0
+	/* Error out if we have no free IMR entries. */
+	if (reg == -1) {
+		ret = -ENODEV;
+		goto done;
+	}
+
+	pr_debug("IMR %d phys %pa-%pa rmask 0x%08x wmask 0x%08x\n",
+		reg, &base, &end, rmask, wmask);
+
+	/* Allocate IMR. */

Technically we don't 'allocate' the IMR here, we write to an existing, currently disabled IMR, right?

arch/x86/platform/intel-quark/imr.c line 433

Quoted 8 lines
arch/x86/platform/intel-quark/imr.c new file +8 -0
+	imr.addr_lo = phys_to_imr(base);
+	imr.addr_hi = phys_to_imr(end);
+	imr.rmask = rmask;
+	imr.wmask = wmask;
+
+	ret = imr_write(idev, reg, &imr, lock);
+
+done:

So 'done:' should really be named 'fail:'.

arch/x86/platform/intel-quark/imr.c line 435

Quoted 2 lines
arch/x86/platform/intel-quark/imr.c new file +2 -0
+	mutex_unlock(&idev->lock);
+	return ret == 0 ? reg : ret;

So we have a variable named 'ret', but it turns out to not be the return value after all?

Why not just 'ret'? It's not like users of this API are supposed to know about the internals of the IMR code: in which register it was stored and such. (See my comments below about the removal API.)

If this is simply an MTRR leftover then good riddance!

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL(imr_add_range);

That should really be EXPORT_SYMBOL_GPL().

Quoted 7 lines
arch/x86/platform/intel-quark/imr.c new file +7 -0
+/**
+ * imr_remove_range - delete an Isolated Memory Region.
+ *
+ * This function allows you to delete an IMR by its index specified by reg or
+ * by address range specified by base and size respectively. If you specify an
+ * index on its own the base and size parameters are ignored.
+ * imr_remove_range(0, size, base); delete IMR at index 0 base/size ignored.

arch/x86/platform/intel-quark/imr.c line 455

Quoted 10 lines
arch/x86/platform/intel-quark/imr.c new file +10 -0
+ * imr_remove_range(-1, base, size); delete IMR from base to base+size.
+ *
+ * @reg:	imr index to remove.
+ * @base:	physical base address of region aligned to 1 KiB.
+ * @size:	physical size of region in bytes aligned to 1 KiB.
+ * @return:	-EINVAL on invalid range or out or range id
+ *		-ENODEV if reg is valid but no IMR exists or is locked
+ *		0 on success.
+ */
+int imr_remove_range(int reg, phys_addr_t base, size_t size)

I think this API should lose the MTRR-ism as well: instead of allowing this weird deletion by index and by address as well, just split it in two variants, and only allow external users to delete by address.

That will simplify things and will allow future enhancements as
well: like transparent merging and even reshuffling of IMRs, should the need arise.

Quoted 24 lines
arch/x86/platform/intel-quark/imr.c new file +24 -0
+{
+	phys_addr_t end;
+	bool found = false;
+	unsigned int i;
+	struct imr_device *idev = &imr_dev;
+	struct imr_regs imr;
+	int ret = 0;
+
+	if (idev->init == false)
+		return -ENODEV;
+
+	if (imr_check_range(base, size))
+		return -EINVAL;
+
+	if (reg >= idev->max_imr)
+		return -EINVAL;
+
+	/* Tweak the size value. */
+	size = imr_fixup_size(size);
+	end = base + size;
+
+	mutex_lock(&idev->lock);
+
+	if (reg >= 0) {

arch/x86/platform/intel-quark/imr.c line 489

Quoted 10 lines
arch/x86/platform/intel-quark/imr.c new file +10 -0
+		/* If a specific IMR is given try to use it. */
+		ret = imr_read(idev, reg, &imr);
+		if (ret)
+			goto done;
+
+		if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK) {
+			ret = -ENODEV;
+			goto done;
+		}
+		found = true;

By my simplification suggestions above this branch would go away from the external API.

Quoted 10 lines
arch/x86/platform/intel-quark/imr.c new file +10 -0
+	} else {
+		/* Search for match based on address range. */
+		for (i = 0; i < idev->max_imr; i++) {
+			ret = imr_read(idev, i, &imr);
+			if (ret)
+				goto done;
+
+			if (!imr_is_enabled(&imr) || imr.addr_lo & IMR_LOCK)
+				continue;
+

arch/x86/platform/intel-quark/imr.c line 509

Quoted 10 lines
arch/x86/platform/intel-quark/imr.c new file +10 -0
+			if ((imr_to_phys(imr.addr_lo) == base) &&
+			    (imr_to_phys(imr.addr_hi) == end)) {
+				found = true;
+				reg = i;
+				break;
+			}
+		}
+	}
+
+	if (found == false) {

So writing this as:

	if (!found) {

is a lot more readable, right?

arch/x86/platform/intel-quark/imr.c line 520

Quoted 11 lines
arch/x86/platform/intel-quark/imr.c new file +11 -0
+		ret = -ENODEV;
+		goto done;
+	}
+
+	/* Tear down the IMR. */
+	imr.addr_lo = 0;
+	imr.addr_hi = 0;
+	imr.rmask = IMR_READ_ACCESS_ALL;
+	imr.wmask = IMR_WRITE_ACCESS_ALL;
+
+	ret = imr_write(idev, reg, &imr, false);

So 'ret' here gets mixed with other potential failure modes.

If imr_write() fails here then that's a highly anomalous internal failure - so it might be better to add a WARN_ON(ret) or so.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+done:

s/done/failed/, or so.

arch/x86/platform/intel-quark/imr.c line 526

Quoted 4 lines
arch/x86/platform/intel-quark/imr.c new file +4 -0
+	mutex_unlock(&idev->lock);
+	return ret;
+}
+EXPORT_SYMBOL(imr_remove_range);

Please make all new kernel internal symbol exports _GPL.

Quoted 15 lines
arch/x86/platform/intel-quark/imr.c new file +15 -0
+
+/**
+ * imr_fixup_memmap - Tear down IMRs used during bootup.
+ *
+ * BIOS and Grub both setup IMRs around compressed kernel, initrd memory
+ * that need to be removed before the kernel hands out one of the IMR
+ * encased addresses to a downstream DMA agent such as the SD or Ethernet.
+ * IMRs on Galileo are setup to immediately reset the system on violation.
+ * As a result if you're running a root filesystem from SD - you'll need
+ * the boot-time IMRs torn down or you'll find seemingly random resets when
+ * using your filesystem.
+ *
+ * @idev:	pointer to imr_device structure.
+ * @return:
+ */

arch/x86/platform/intel-quark/imr.c line 551

Quoted 10 lines
arch/x86/platform/intel-quark/imr.c new file +10 -0
+static void __init imr_fixup_memmap(struct imr_device *idev)
+{
+	phys_addr_t base = virt_to_phys(&_text);
+	size_t size = virt_to_phys(&__end_rodata) - base;
+	int i;
+	int ret;
+
+	/* Tear down all existing unlocked IMRs. */
+	for (i = 0; i < idev->max_imr; i++)
+		imr_remove_range(i, 0, 0);

This would use a simpler variant of imr_remove_range() which is basically just an imr_write(). It might even be written as an imr_clear() helper.

arch/x86/platform/intel-quark/imr.c line 564

Quoted 13 lines
arch/x86/platform/intel-quark/imr.c new file +13 -0
+
+	/*
+	 * Setup a locked IMR around the physical extent of the kernel
+	 * from the beginning of the .text secton to the end of the
+	 * .rodata section as one physically contiguous block.
+	 */
+	ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, true);
+	if (ret < 0)
+		pr_err("unable to setup IMR for kernel: (%p - %p)\n",
+			&_text, &__end_rodata);
+	else
+		pr_info("protecting kernel .text - .rodata: %zu KiB (%p - %p)\n",
+			size / 1024, &_text, &__end_rodata);

Curly braces around multi-line statements and all that.

Quoted 4 lines
arch/x86/platform/intel-quark/imr_selftest.c new file +1 -0
@@ -0,0 +1,135 @@
+/**

arch/x86/platform/intel-quark/imr_selftest.c line 11

Quoted 10 lines
arch/x86/platform/intel-quark/imr_selftest.c new file +10 -0
+ * imr_selftest.c
+ *
+ * Copyright(c) 2013 Intel Corporation.
+ * Copyright(c) 2015 Bryan O'Donoghue <[email protected]>
+ *
+ * IMR self test. The purpose of this module is to run a set of tests on the
+ * IMR API to validate it's sanity. We check for overlapping, reserved
+ * addresses and setup/teardown sanity.
+ *
+ */

Looks like we could shrink the kernel source code by 3 bytes there!

In any case, I don't see any major problems with this code, so if it's fixed it could go into v3.20.

Thanks,

Ingo

#4 lore raw

Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 1 line · Ingo Molnar

* Bryan O'Donoghue <[email protected]> wrote:

[]

Quoted 2 lines

In any case, I don't see any major problems with this code, so if it's fixed it could go into v3.20.

Brian, it would be really nice to have it in 3.20 since we have several drivers already in upstream or be ready for 3.20. It would be then first upstream kernel with minimal Quark support.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Ingo Molnar

* Bryan O'Donoghue <[email protected]> wrote:

[]

Quoted 2 lines

In any case, I don't see any major problems with this code, so if it's fixed it could go into v3.20.

Brian, it would be really nice to have it in 3.20 since we have several drivers already in upstream or be ready for 3.20. It would be then first upstream kernel with minimal Quark support.

NP lads - I'll get a patch out today

#6 lore raw

RE: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Bryan,

Once you have the next revision ready, I would like to test it on my end across both Galileo Gen v1 & v2.

Cheers,
BL

Quoted 22 lines

-----Original Message-----
From: Bryan O'Donoghue [mailto:[email protected]]
Sent: Thursday, January 29, 2015 6:12 PM
To: Andy Shevchenko; Ingo Molnar
Cc: Thomas Gleixner; Ingo Molnar; H. Peter Anvin; [email protected]; [email protected]; Ong, Boon Leong; [email protected]
Subject: Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Ingo Molnar

* Bryan O'Donoghue <[email protected]> wrote:

[]

Quoted 2 lines

In any case, I don't see any major problems with this code, so if it's fixed it could go into v3.20.

Brian, it would be really nice to have it in 3.20 since we have several drivers already in upstream or be ready for 3.20. It would be then first upstream kernel with minimal Quark support.

NP lads - I'll get a patch out today

#7 lore raw

Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 6 lines · Boon Leong

Bryan,

Once you have the next revision ready, I would like to test it on my end across both Galileo Gen v1 & v2.

Cheers,
BL

Andy/BL - thanks for taking the time to test.

I may end up dropping the imr_del_range() tests based on index as a result of changing the external interface as suggested by Ingo.

If you guys have a clear preference to keep those tests shout now and I'll re-merge the imr_selftest code into the main chunk of IMR code.

--
BOD

#8 lore raw

RE: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 17 lines

-----Original Message-----
From: Bryan O'Donoghue [mailto:[email protected]]
Sent: Thursday, January 29, 2015 11:22 PM
To: Ong, Boon Leong
Cc: Thomas Gleixner; Ingo Molnar; H. Peter Anvin; [email protected]; [email protected]; [email protected]; Andy Shevchenko; Ingo Molnar
Subject: Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 3 lines · Boon Leong

Bryan,

Once you have the next revision ready, I would like to test it on my end across

both Galileo Gen v1 & v2.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Cheers,
BL

Andy/BL - thanks for taking the time to test.

I may end up dropping the imr_del_range() tests based on index as a result of changing the external interface as suggested by Ingo.

It would be nice to have two variants (1) index based & (2) address based.

Quoted 3 lines

If you guys have a clear preference to keep those tests shout now and I'll re- merge the imr_selftest code into the main chunk of IMR code.

Ok, once you have that ready, I will re-run the test on my end.

Quoted 3 lines

--
BOD

#9 lore raw

Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 17 lines · Boon Leong
Quoted 17 lines

-----Original Message-----
From: Bryan O'Donoghue [mailto:[email protected]]
Sent: Thursday, January 29, 2015 11:22 PM
To: Ong, Boon Leong
Cc: Thomas Gleixner; Ingo Molnar; H. Peter Anvin; [email protected]; [email protected]; [email protected]; Andy Shevchenko; Ingo Molnar
Subject: Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 3 lines · Boon Leong

Bryan,

Once you have the next revision ready, I would like to test it on my end across

both Galileo Gen v1 & v2.

Quoted 1 line
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Cheers,
BL

Andy/BL - thanks for taking the time to test.

I may end up dropping the imr_del_range() tests based on index as a result of changing the external interface as suggested by Ingo.

It would be nice to have two variants (1) index based & (2) address based.

Understood. The direction from Ingo was to have address based external interface imr_del_range() and support an index based internal imr_clear() - internally.

So - in order to get test coverage - I'll move the self-test code back into the main IMR code

Not as pretty that way - but better coverage :)

#10 lore raw

Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 11 lines · Bryan O'Donoghue
Quoted 2 lines

It would be nice to have two variants (1) index based & (2) address based.

Understood. The direction from Ingo was to have address based external interface imr_del_range() and support an index based internal imr_clear() - internally.

So - in order to get test coverage - I'll move the self-test code back into the main IMR code

Not as pretty that way - but better coverage :)

Talking to myself in public...

Second (third) thought - there's no advantage to moving the test code back in - since imr_add_range() won't return the index anymore...

#11 lore raw

RE: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 11 lines · Bryan O'Donoghue
Quoted 9 lines
Quoted 2 lines

It would be nice to have two variants (1) index based & (2) address based.

Understood. The direction from Ingo was to have address based external interface imr_del_range() and support an index based internal imr_clear() - internally.

So - in order to get test coverage - I'll move the self-test code back into the main IMR code

It will be nice to have the separation of test code. However, that also means the index-based variant needs to be external function.
Ingo, what is your preference?

Quoted 7 lines
Quoted 2 lines

Not as pretty that way - but better coverage :)

Talking to myself in public...

Second (third) thought - there's no advantage to moving the test code back in - since imr_add_range() won't return the index anymore...

It does. See below from v6:

+	ret = imr_write(idev, reg, &imr, lock);
			          ^^ =========== IMR ID
+
+done:
+	mutex_unlock(&idev->lock);
+	return ret == 0 ? reg : ret;
			 ^^  ============ IMR ID

+}
+EXPORT_SYMBOL(imr_add_range);

#12 lore raw

RE: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

In reply to Bryan O'Donoghue

Hi Bryan,

These two patches requires another patch "x86: Re-enable IO-APIC for non-SMP X86_32" to be included to make sure we have the right build for Quark.

I tested v6 on Galileo Gen v2 just now. I am happy with it. Many thanks.

Below are the logs:

root@quark:/proc# dmesg | grep imr
[    3.727884] imr: protecting kernel .text - .rodata: 9240 KiB (c1000000 - c1906000)
[    3.736421] imr_selftest: self_test pass zero sized IMR
[    3.742413] imr_selftest: self_test pass overlapped IMR @ (0xc1000000 - 0xc1906000)
[    3.751164] imr_selftest: self_test pass overlapped IMR @ (0xc1905c00 - 0xc220bc00)
[    3.759916] imr_selftest: self_test pass overlapped IMR @ (0xc0fff400 - 0xc1905400)
[    3.768566] imr_selftest: self_test pass 1KiB IMR @ 0x00000000 - access-all
[    3.776542] imr_selftest: self_test pass 1KiB IMR @ 0x00000000 - cpu access
[    3.784442] imr_selftest: self_test pass index-teardown - cpu access
[    3.791690] imr_selftest: self_test pass 2KiB IMR @ 0x00000000
[    3.798396] imr_selftest: self_test pass index-teardown 2KiB
[    3.804957] imr_selftest: self_test pass 2KiB IMR @ 0x00000000
[    3.811616] imr_selftest: self_test pass addr-teardown 2KiB
root@quark:/proc# cat /sys/kernel/debug/imr_state
imr00: base=0x00000000, end=0x00000000, size=0x00000000 rmask=0xbfffffff, wmask=0xffffffff, disabled, unlocked
imr01: base=0x00000000, end=0x00000000, size=0x00000000 rmask=0xbfffffff, wmask=0xffffffff, disabled, unlocked
imr02: base=0x00000000, end=0x00000000, size=0x00000000 rmask=0xbfffffff, wmask=0xffffffff, disabled, unlocked
imr03: base=0x00000000, end=0x00000000, size=0x00000000 rmask=0xbfffffff, wmask=0xffffffff, disabled, unlocked
imr04: base=0x00000000, end=0x00000000, size=0x00000000 rmask=0xbfffffff, wmask=0xffffffff, disabled, unlocked
imr05: base=0x00000000, end=0x00000000, size=0x00000000 rmask=0xbfffffff, wmask=0xffffffff, disabled, unlocked
imr06: base=0x00000000, end=0x00000000, size=0x00000000 rmask=0xbfffffff, wmask=0xffffffff, disabled, unlocked
imr07: base=0x01000000, end=0x01905fff, size=0x00905fff rmask=0x00000001, wmask=0x00000001, enabled , locked
Quoted 40 lines

-----Original Message-----
From: Ong, Boon Leong
Sent: Thursday, January 29, 2015 9:47 PM
To: 'Bryan O'Donoghue'
Cc: Thomas Gleixner; Ingo Molnar; H. Peter Anvin; [email protected]; [email protected]; [email protected]; Andy Shevchenko; Ingo Molnar
Subject: RE: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Bryan,

Once you have the next revision ready, I would like to test it on my end across both Galileo Gen v1 & v2.

Cheers,
BL

Quoted 23 lines

-----Original Message-----
From: Bryan O'Donoghue [mailto:[email protected]]
Sent: Thursday, January 29, 2015 6:12 PM
To: Andy Shevchenko; Ingo Molnar
Cc: Thomas Gleixner; Ingo Molnar; H. Peter Anvin; [email protected]; [email protected]; Ong, Boon Leong; [email protected]
Subject: Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Ingo Molnar

* Bryan O'Donoghue <[email protected]> wrote:

[]

Quoted 2 lines

In any case, I don't see any major problems with this code, so if it's fixed it could go into v3.20.

Brian, it would be really nice to have it in 3.20 since we have several drivers already in upstream or be ready for 3.20. It would be then first upstream kernel with minimal Quark support.

NP lads - I'll get a patch out today

#13 lore raw

Re: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

In reply to Ingo Molnar

On 29/01/15 07:44, Ingo Molnar wrote:

Hi Ingo.

I'll transmit those changes in with the following exception

Quoted 7 lines

arch/x86/platform/intel-quark/imr.c line 520

Quoted 1 line
arch/x86/platform/intel-quark/imr.c new file +1 -0
+	ret = imr_write(idev, reg, &imr, false);

So 'ret' here gets mixed with other potential failure modes.

If imr_write() fails here then that's a highly anomalous internal failure - so it might be better to add a WARN_ON(ret) or so.

imr_write() already does a WARN - so no need to do it again.

#14 lore raw

RE: [PATCH 1/2] x86: Add Isolated Memory Regions for Quark X1000

In reply to Bryan O'Donoghue

Bryan, a minor fix on the config below...

Quoted 18 lines
arch/x86/Kconfig.debug index renamed from arch/x86/Kconfig.debug +0 -0

61bd2ad..fcf5701 100644

arch/x86/Kconfig.debug +9 -0
@@ -313,6 +313,18 @@ config DEBUG_NMI_SELFTEST
 
 	  If unsure, say N.
 
+config DEBUG_IMR_SELFTEST
+	bool "Isolated Memory Region self test"
+	default n
+	depends on INTEL_IMR
+	---help---
+	  This option enables automated sanity testing of the IMR code.
+	  Some simple tests are run to verify IMR bounds checking, alignment
+	  and overlapping. This option is really only useful if you are
+	  debugging an IMR memory map or are modifying the IMR code and

want to

arch/x86/Kconfig.debug config DEBUG_NMI_SELFTEST · line 327

Quoted 3 lines
arch/x86/Kconfig.debug +3 -0
+	  test your changes.
+
+	  If unsure say N here.

Miss a newline here.

Quoted 3 lines

config X86_DEBUG_STATIC_CPU_HAS
bool "Debug alternatives"
depends on DEBUG_KERNEL

#15 lore raw

PATCH v6 2/2 x86, quark: Add Intel Quark platform support

In reply to Bryan O'Donoghue

Add Intel Quark platform support. Quark needs to pull down all unlocked IMRs to ensure agreement with the EFI memory map post boot.

This patch adds an entry in Kconfig for Quark as a platform and makes IMR support mandatory if selected.

---
 arch/x86/Kconfig           | 16 ++++++++++++++++
 arch/x86/platform/Makefile |  1 +
 2 files changed, 17 insertions(+)
arch/x86/Kconfig +16 -0
index 0dc9d01..36d50cd 100644
@@ -485,6 +485,22 @@ config X86_INTEL_MID
 	  Intel MID platforms are based on an Intel processor and chipset which
 	  consume less power than most of the x86 derivatives.
 
+config X86_INTEL_QUARK
+	bool "Intel Quark platform support"
+	depends on X86_32
+	depends on X86_EXTENDED_PLATFORM
+	depends on X86_PLATFORM_DEVICES
+	depends on X86_TSC
+	depends on PCI
+	depends on PCI_GOANY
+	depends on X86_IO_APIC
+	select IOSF_MBI
+	select INTEL_IMR
+	---help---
+	  Select to include support for Quark X1000 SoC.
+	  Say Y here if you have a Quark based system such as the Arduino
+	  compatible Intel Galileo.
+
 config X86_INTEL_LPSS
 	bool "Intel Low Power Subsystem Support"
 	depends on ACPI
arch/x86/platform/Makefile +1 -0
index 85afde1..a62e0be 100644
@@ -5,6 +5,7 @@ obj-y	+= geode/
 obj-y	+= goldfish/
 obj-y	+= iris/
 obj-y	+= intel-mid/
+obj-y	+= intel-quark/
 obj-y	+= olpc/
 obj-y	+= scx200/
 obj-y	+= sfi/
Signature · 1 line
1.9.1