Skip to contentSkip to conversation

arm: Add NRF51 SOC non-volatile memory controller

RFC Superseded Steffen Görtz 4 participants 9 messages Jun 26, 2018 – Jul 5, 2018

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC v2 1/1 arm: Add NRF51 SOC non-volatile memory controller

Changes since V1:
- Code style changes

---
 hw/nvram/Makefile.objs        |   1 +
 hw/nvram/nrf51_nvmc.c         | 168 ++++++++++++++++++++++++++++++++++
 include/hw/nvram/nrf51_nvmc.h |  51 +++++++++++
 3 files changed, 220 insertions(+)
 create mode 100644 hw/nvram/nrf51_nvmc.c
 create mode 100644 include/hw/nvram/nrf51_nvmc.h
hw/nvram/Makefile.objs +1 -0
index a912d25391..9edd61e8af 100644
@@ -5,3 +5,4 @@ common-obj-y += fw_cfg.o
 common-obj-y += chrp_nvram.o
 common-obj-$(CONFIG_MAC_NVRAM) += mac_nvram.o
 obj-$(CONFIG_PSERIES) += spapr_nvram.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_nvmc.o
hw/nvram/nrf51_nvmc.c new file +168 -0

168 lines not on this page · raw message

include/hw/nvram/nrf51_nvmc.h new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines · Steffen Görtz

Changes since V1:
- Code style changes

Please put the changelog below '---'.

Quoted 6 lines
hw/nvram/nrf51_nvmc.c new file +0 -0
new file mode 100644
index 0000000000..5dde3088a8

hw/nvram/nrf51_nvmc.c line 8

Quoted 8 lines
hw/nvram/nrf51_nvmc.c new file +8 -0
@@ -0,0 +1,168 @@
+/*
+ * nrf51_nvmc.c
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ */

Please mention the full name of the peripheral so its purpose is clear and include a link to the datasheet.

It's convenient to have this information in the .c file, since that's where the majority of code changes happen.

Quoted 19 lines
include/hw/nvram/nrf51_nvmc.h new file +13 -0
new file mode 100644
index 0000000000..3a63b7e5ad
@@ -0,0 +1,51 @@
+/*
+ * nrf51_nvmc.h
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ *
+ * See Nrf51 reference manual 6 Non-Volatile Memory Controller (NVMC)
+ * See Nrf51 product sheet 8.22 NVMC specifications
+ *
+ * QEMU interface:
+ * + sysbus MMIO regions 0: Memory Region with registers

include/hw/nvram/nrf51_nvmc.h line 23

Quoted 10 lines
include/hw/nvram/nrf51_nvmc.h new file +10 -0
+ *   to be mapped to the peripherals instance address by the SOC.
+ * + page_size property to set the page size in bytes.
+ * + code_size property to set the code size in number of pages.
+ *
+ * Accuracy of the peripheral model:
+ * + The NVMC is always ready, all requested erase operations succeed
+ *   immediately.
+ * + CONFIG.WEN and CONFIG.EEN flags can be written and read back
+ *   but are not evaluated to check whether a requested write/erase operation
+ *   is legal.

Checking CONFIG.EEN would be easy, please do it.

Peter: CONFIG.WEN determines whether stores to the flash memory region are allowed. What is the best way to implement this protection?

include/hw/nvram/nrf51_nvmc.h line 42

Quoted 2 lines
include/hw/nvram/nrf51_nvmc.h new file +2 -0
+    MemoryRegion *mr;
+    AddressSpace as;

I remember you said you tried address_space_read/write(get_system_memory(), ...) but it didn't work. Did you figure out why?

include/hw/nvram/nrf51_nvmc.h line 46

Quoted 3 lines
include/hw/nvram/nrf51_nvmc.h new file +3 -0
+    struct {
+        uint32_t config:2;
+    } state;

I noticed you used bit-fields in recent patches. They are rarely-used in QEMU.

The representation of bit-fields is (compiler) implementation-defined, so they cannot be used for live-migration where values are serialized (marshaled).

For boolean flags just 'bool' is preferred because 'true'/'false' is clearer in code than '0'/'1' (the reader doesn't know whether it's a bool or used as an integer at some point unless they study all the code).

For this field I would use just uint32_t config.

Stefan

#2 lore raw
Quoted 59 lines · Stefan Hajnoczi
Quoted 2 lines · Steffen Görtz

Changes since V1:
- Code style changes

Please put the changelog below '---'.

Quoted 6 lines
hw/nvram/nrf51_nvmc.c new file +0 -0
new file mode 100644
index 0000000000..5dde3088a8

hw/nvram/nrf51_nvmc.c line 8

Quoted 8 lines
hw/nvram/nrf51_nvmc.c new file +8 -0
@@ -0,0 +1,168 @@
+/*
+ * nrf51_nvmc.c
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ */

Please mention the full name of the peripheral so its purpose is clear and include a link to the datasheet.

It's convenient to have this information in the .c file, since that's where the majority of code changes happen.

Quoted 19 lines
include/hw/nvram/nrf51_nvmc.h new file +13 -0
new file mode 100644
index 0000000000..3a63b7e5ad
@@ -0,0 +1,51 @@
+/*
+ * nrf51_nvmc.h
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ *
+ * See Nrf51 reference manual 6 Non-Volatile Memory Controller (NVMC)
+ * See Nrf51 product sheet 8.22 NVMC specifications
+ *
+ * QEMU interface:
+ * + sysbus MMIO regions 0: Memory Region with registers

include/hw/nvram/nrf51_nvmc.h line 23

Quoted 10 lines
include/hw/nvram/nrf51_nvmc.h new file +10 -0
+ *   to be mapped to the peripherals instance address by the SOC.
+ * + page_size property to set the page size in bytes.
+ * + code_size property to set the code size in number of pages.
+ *
+ * Accuracy of the peripheral model:
+ * + The NVMC is always ready, all requested erase operations succeed
+ *   immediately.
+ * + CONFIG.WEN and CONFIG.EEN flags can be written and read back
+ *   but are not evaluated to check whether a requested write/erase operation
+ *   is legal.

Checking CONFIG.EEN would be easy, please do it.

Quoted 2 lines

Peter: CONFIG.WEN determines whether stores to the flash memory region are allowed. What is the best way to implement this protection?

What accesses do they gate? If this is just accesses that go via the io_read() and io_write() functions then this is easy:
just put a check in those functions in the appropriate place. The harder case is if they affect accesses to a region that is implemented as an MMIO ram region which can be executed from; that can be done, but gets annoyingly complicated:
* unmap the RAM and map an MMIO region which handles the errors (only possible if the config bit gates the reads as well)
* use a ROM device memory region (only possible if reads are always ok but writes might not be
* use the IOMMU APIs (maximum flexibility, maximum pain)

A quick scan of the code suggests you're in the easy case where you can just have io_read() and io_write() handle the config switch checks, though.

Quoted 6 lines

include/hw/nvram/nrf51_nvmc.h line 42

Quoted 2 lines
include/hw/nvram/nrf51_nvmc.h new file +2 -0
+    MemoryRegion *mr;
+    AddressSpace as;

I remember you said you tried address_space_read/write(get_system_memory(), ...) but it didn't work. Did you figure out why?

Devices directly talking to get_system_memory() isn't a great design anyway.

It's not clear to me what this device is doing with its AddressSpace, though; comments that went into more detail about what the device is and what the "memory" property is for might help.

Quoted 6 lines

include/hw/nvram/nrf51_nvmc.h line 46

Quoted 3 lines
include/hw/nvram/nrf51_nvmc.h new file +3 -0
+    struct {
+        uint32_t config:2;
+    } state;

I noticed you used bit-fields in recent patches. They are rarely-used in QEMU.

Agreed that avoiding bitfields is preferable.

thanks
-- PMM

#3 lore raw
Quoted 75 lines · Peter Maydell
Quoted 59 lines · Stefan Hajnoczi
Quoted 2 lines · Steffen Görtz

Changes since V1:
- Code style changes

Please put the changelog below '---'.

Quoted 6 lines
hw/nvram/nrf51_nvmc.c new file +0 -0
new file mode 100644
index 0000000000..5dde3088a8

hw/nvram/nrf51_nvmc.c line 8

Quoted 8 lines
hw/nvram/nrf51_nvmc.c new file +8 -0
@@ -0,0 +1,168 @@
+/*
+ * nrf51_nvmc.c
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ */

Please mention the full name of the peripheral so its purpose is clear and include a link to the datasheet.

It's convenient to have this information in the .c file, since that's where the majority of code changes happen.

Quoted 19 lines
include/hw/nvram/nrf51_nvmc.h new file +13 -0
new file mode 100644
index 0000000000..3a63b7e5ad
@@ -0,0 +1,51 @@
+/*
+ * nrf51_nvmc.h
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ *
+ * See Nrf51 reference manual 6 Non-Volatile Memory Controller (NVMC)
+ * See Nrf51 product sheet 8.22 NVMC specifications
+ *
+ * QEMU interface:
+ * + sysbus MMIO regions 0: Memory Region with registers

include/hw/nvram/nrf51_nvmc.h line 23

Quoted 10 lines
include/hw/nvram/nrf51_nvmc.h new file +10 -0
+ *   to be mapped to the peripherals instance address by the SOC.
+ * + page_size property to set the page size in bytes.
+ * + code_size property to set the code size in number of pages.
+ *
+ * Accuracy of the peripheral model:
+ * + The NVMC is always ready, all requested erase operations succeed
+ *   immediately.
+ * + CONFIG.WEN and CONFIG.EEN flags can be written and read back
+ *   but are not evaluated to check whether a requested write/erase operation
+ *   is legal.

Checking CONFIG.EEN would be easy, please do it.

Quoted 2 lines

Peter: CONFIG.WEN determines whether stores to the flash memory region are allowed. What is the best way to implement this protection?

What accesses do they gate? If this is just accesses that go via the io_read() and io_write() functions then this is easy:
just put a check in those functions in the appropriate place. The harder case is if they affect accesses to a region that is implemented as an MMIO ram region which can be executed from; that can be done, but gets annoyingly complicated:
* unmap the RAM and map an MMIO region which handles the errors (only possible if the config bit gates the reads as well)
* use a ROM device memory region (only possible if reads are always ok but writes might not be
* use the IOMMU APIs (maximum flexibility, maximum pain)

Quoted 3 lines

A quick scan of the code suggests you're in the easy case where you can just have io_read() and io_write() handle the config switch checks, though.

Thanks!

Quoted 7 lines
Quoted 6 lines

include/hw/nvram/nrf51_nvmc.h line 42

Quoted 2 lines
include/hw/nvram/nrf51_nvmc.h new file +2 -0
+    MemoryRegion *mr;
+    AddressSpace as;

I remember you said you tried address_space_read/write(get_system_memory(), ...) but it didn't work. Did you figure out why?

Quoted 7 lines

Devices directly talking to get_system_memory() isn't a great design anyway.

It's not clear to me what this device is doing with its AddressSpace, though; comments that went into more detail about what the device is and what the "memory" property is for might help.

I understand this issue now. It's the same as qtest.

This device is only visible from cpu->as, it's not visible from address_space_memory (system_memory).

The NVMC needs access to the SoC's flash memory region, and that's what mr/as achieve here. I agree that comments explaining the purpose of mr/as would be useful.

#4 lore raw
Quoted 6 lines · Stefan Hajnoczi
Quoted 4 lines · Peter Maydell

It's not clear to me what this device is doing with its AddressSpace, though; comments that went into more detail about what the device is and what the "memory" property is for might help.

Quoted 8 lines

I understand this issue now. It's the same as qtest.

This device is only visible from cpu->as, it's not visible from address_space_memory (system_memory).

The NVMC needs access to the SoC's flash memory region, and that's what mr/as achieve here. I agree that comments explaining the purpose of mr/as would be useful.

Is the flash memory region only accessible to the CPU via the NVMC, or can the CPU get at it both directly and via the NVMC? (That is, in hardware, does the flash sit "behind" the NVMC?)

thanks
-- PMM

#5 lore raw
Quoted 8 lines

Is the flash memory region only accessible to the CPU via the NVMC, or can the CPU get at it both directly and via the NVMC? (That is, in hardware, does the flash sit "behind" the NVMC?)

thanks
-- PMM

The latter is the case.

Steffen

#6 lore raw
In reply to Stefan Hajnoczi
Quoted 6 lines · Stefan Hajnoczi
Quoted 4 lines · Peter Maydell

It's not clear to me what this device is doing with its AddressSpace, though; comments that went into more detail about what the device is and what the "memory" property is for might help.

Quoted 8 lines

I understand this issue now. It's the same as qtest.

This device is only visible from cpu->as, it's not visible from address_space_memory (system_memory).

The NVMC needs access to the SoC's flash memory region, and that's what mr/as achieve here. I agree that comments explaining the purpose of mr/as would be useful.

I've fished out a copy of the NRF51 datasheet, which helps. Roughly, what we have is:

[ CPU ] -+- [ NVM, either FICR, UICR or CODE ]

          |      |
          \- [ NVMC ]

where the CPU can talk to either the NVMC or the NVMs, and the NVMC also has a control connection to the NVM. (See the block diagram in the nRF51 manual.)

The behaviour this allows is:

* the CPU can directly read and execute from the NVMs (which it can see in its address space at 0x00000000, 0x10000000, 0x10001000)
* the NVMC controls whether the CPU can write to the NVMs. If the NVMC CONFIG register permits writes then the CPU writes directly to the mapped NVMs, and gets the usual flash-memory behaviour that writes can turn 1s to 0s but not 0s to 1s.
* the NVMC also has support for performing erases of either pages or entire NVMs (which writes them to all-1s)

My suggestion for the best way to model this is:

* the NVMs are (sysbus) devices which expose a MemoryRegion which they have created with memory_region_init_rom_device(). MRs of this sort can be directly read (from a ram block), but writes always go via a MemoryRegionOps write function. This will let us do fast execution and reading, but catch writes so we can make them behave correctly (be forbidden, not allow writing of 0->1, etc).
* the NVMs should implement a QOM interface that defines how we model the "control connection" from the NVMC -- basically this will have some methods for "erase all", "erase this page", and "set the writes permitted flag".
* the NVMC should have QOM link properties for its NVMCs, and the SoC code sets those link properties to pointers to the NVMCs
* the NVMC implements an ordinary IO MemoryRegion for its register interface; guest writes to the registers may cause it to tell the NVMs what to do by calling methods on the NVMs (which it has pointers to via the link properties)
* the SoC code maps the NVMs and the NVMC's registers into the right places in its address space

There's nothing here that requires extra functionality from core QEMU code, but on the other hand it is quite complicated, so you might prefer to start by modelling the NVMs as plain ROM MRs (ie never-writable) and the NVMC as a register interface which just complains (LOG_UNIMP) if the guest tries to enable writes or erases, plus a comment giving a sketch of the design changes needed to add write/erase support later. (I would prefer that to the code in this patch which implements erases by writing to a single passed-in memory region which covers the whole address space.)

thanks
-- PMM

#7 lore raw

Hello Peter,

thank you for your very elaborate remarks!

Quoted 21 lines

[ CPU ] -+- [ NVM, either FICR, UICR or CODE ]

          |      |
          \- [ NVMC ]

where the CPU can talk to either the NVMC or the NVMs, and the NVMC also has a control connection to the NVM. (See the block diagram in the nRF51 manual.)

The behaviour this allows is:

* the CPU can directly read and execute from the NVMs (which it can see in its address space at 0x00000000, 0x10000000, 0x10001000)
* the NVMC controls whether the CPU can write to the NVMs. If the NVMC CONFIG register permits writes then the CPU writes directly to the mapped NVMs, and gets the usual flash-memory behaviour that writes can turn 1s to 0s but not 0s to 1s.
* the NVMC also has support for performing erases of either pages or entire NVMs (which writes them to all-1s)

My suggestion for the best way to model this is:

Quoted 7 lines

* the NVMs are (sysbus) devices which expose a MemoryRegion which they have created with memory_region_init_rom_device(). MRs of this sort can be directly read (from a ram block), but writes always go via a MemoryRegionOps write function. This will let us do fast execution and reading, but catch writes so we can make them behave correctly (be forbidden, not allow writing of 0->1, etc).

In the latest revision of my NVMC model i included the FICR/UICR as sysbus devices which expose a memory region using memory_region_init_device. I can change this using init_rom_device for performance. Furthermore i will add the CODE device.

I would like to provide all four devices (NVMC, CODE, FICR, UICR) from one module and rename nrf51_nvmc.[ch] to nrf51_nvm.[ch]. Is this acceptable or would you rather prefer three modules?

Quoted 5 lines

* the NVMs should implement a QOM interface that defines how we model the "control connection" from the NVMC -- basically this will have some methods for "erase all", "erase this page", and "set the writes permitted flag".
* the NVMC should have QOM link properties for its NVMCs, and

Quoted 8 lines

the SoC code sets those link properties to pointers to the NVMCs
* the NVMC implements an ordinary IO MemoryRegion for its register interface; guest writes to the registers may cause it to tell the NVMs what to do by calling methods on the NVMs (which it has pointers to via the link properties)
* the SoC code maps the NVMs and the NVMC's registers into the right places in its address space

Thank you for these remarks, i will follow them.

Steffen

#8 lore raw
Quoted 3 lines · Steffen Görtz

I would like to provide all four devices (NVMC, CODE, FICR, UICR) from one module and rename nrf51_nvmc.[ch] to nrf51_nvm.[ch]. Is this acceptable or would you rather prefer three modules?

Yeah, that would be ok too.

thanks
-- PMM