data-driven device registers
v9 cover data-driven device registers
This patch series is based on Peter C's original register API. His original cover letter is below.
Future work: Allow support for memory attributes.
V9:
- Fixup the finalise function
V8:
- Remove GPIO infrastructure from patch set
V7:
- Rebase
- Fixup the DecCFG model
- Fixes and corrections to the register API model and functions
V6:
- Small changes to the API based on Alex's comments
- Remove 'register: Add support for decoding information' patch
- Move prefix and debug into the RegisterInfoArray as it is the same for every register.
V5:
- Only create a single memory region instead of a memory region for each register
- General tidyups based on Alex's comments
V4:
- Rebase and fix build issue
- Simplify the register write logic
- Other small fixes suggested by Alex Bennee
V3:
- Small changes reported by Fred
V2:
- Rebase
- Fix up IOU SLCR connections
- Add the memory_region_add_subregion_no_print() function and use it for the registers
Changes since RFC:
- Connect the ZynqMP IOU SLCR device
- Rebase
Original cover letter From Peter:
Hi All. This is a new scheme I've come up with handling device registers in a data driven way. My motivation for this is to factor out a lot of the access checking that seems to be replicated in every device. See P1 commit message for further discussion.
P1 is the main patch, adds the register definition functionality P2-3,6 add helpers that glue the register API to the Memory API P4 Defines a set of macros that minimise register and field definitions P5 is QOMfication
P7 is a trivial
P10-13 Work up to GPIO support
P8,9,14 add new devices (the Xilinx Zynq devcfg & ZynqMP SLCR) that use this
scheme.
P15: Connect the ZynqMP SLCR device
This Zynq devcfg device was particularly finnicky with per-bit restrictions. I'm also looking for a higher-than-usual modelling fidelity
on the register space, with semantics defined for random reserved bits in-between otherwise consistent fields.
Here's an example of the qemu_log output for the devcfg device. This is produced by now generic sharable code:
/machine/unattached/device[44]:Addr 0x000008:CFG: write of value 00000508 /machine/unattached/device[44]:Addr 0x000080:MCTRL: write of value 00800010 /machine/unattached/device[44]:Addr 0x000010:INT_MASK: write of value ffffffff /machine/unattached/device[44]:Addr 00000000:CTRL: write of value 0c00607f
And an example of a rogue guest banging on a bad bit:
/machine/unattached/device[44]:Addr 0x000014:STATUS bits 0x000001 may not be \
written to 1
A future feature I am interested in is implementing TCG optimisation of side-effectless registers. The register API allows clear definition of what registers have txn side effects and which ones don't. You could even go a step further and translate such side-effectless accesses based on the data pointer for the register.
Alistair Francis (4):
bitops: Add MAKE_64BIT_MASK macro
register: Add Register API
register: Add Memory API glue
dma: Add Xilinx Zynq devcfg device model
Peter Crosthwaite (4):
register: Define REG and FIELD macros
register: QOMify
register: Add block initialise helper
xilinx_zynq: Connect devcfg to the Zynq machine model
default-configs/arm-softmmu.mak | 1 + hw/arm/xilinx_zynq.c | 6 + hw/core/Makefile.objs | 1 + hw/core/register.c | 287 +++++++++++++++++++++++++++ hw/dma/Makefile.objs | 1 + hw/dma/xlnx-zynq-devcfg.c | 400 ++++++++++++++++++++++++++++++++++++++ include/hw/dma/xlnx-zynq-devcfg.h | 62 ++++++ include/hw/register.h | 254 ++++++++++++++++++++++++ include/qemu/bitops.h | 3 + 9 files changed, 1015 insertions(+) create mode 100644 hw/core/register.c create mode 100644 hw/dma/xlnx-zynq-devcfg.c create mode 100644 include/hw/dma/xlnx-zynq-devcfg.h create mode 100644 include/hw/register.h
Signature · 1 line
2.7.4
PATCH v9 1/8 bitops: Add MAKE_64BIT_MASK macro
Add a macro that creates a 64bit value which has length number of ones shifted across by the value of shift.
---
V7:
- Fixup commit typo
- Use the method from deposit64()
V5:
- Re-write to a 64-bit mask instead of ONES()
- Re-order this patch in the series
include/qemu/bitops.h | 3 +++ 1 file changed, 3 insertions(+)
include/qemu/bitops.h +3 -0
@@ -24,6 +24,9 @@#define BIT_WORD (nr) ((nr) / BITS_PER_LONG)#define BITS_TO_LONGS (nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) +#define MAKE_64BIT_MASK (shift, length) \ + (((~0ULL) >> (64 - (length))) << (shift)) +/** * set_bit - Set a bit in memory * @nr: the bit to set
Signature · 1 line
2.7.4
PATCH v9 2/8 register: Add Register API
In reply to Alistair FrancisThis API provides some encapsulation of registers and factors out some common functionality to common code. Bits of device state (usually MMIO registers) often have all sorts of access restrictions and semantics associated with them. This API allows you to define what those restrictions are on a bit-by-bit basis.
Helper functions are then used to access the register which observe the semantics defined by the RegisterAccessInfo struct.
Some features:
Bits can be marked as read_only (ro field)
Bits can be marked as write-1-clear (w1c field)
Bits can be marked as reserved (rsvd field)
Reset values can be defined (reset)
Bits can be marked clear on read (cor)
Pre and post action callbacks can be added to read and write ops Verbose debugging info can be enabled/disabled
Useful for defining device register spaces in a data driven way. Cuts down on a lot of the verbosity and repetition in the switch-case blocks in the standard foo_mmio_read/write functions.
Also useful for automated generation of device models from hardware design sources.
---
V9:
- Fixup read enable mask logic
V8:
- Add read enable mask
V5:
- Convert to using only one memory region
V4:
- Rebase
- Remove the guest error masking
- Simplify the unimplemented masking
- Use the reserved value in the write calculations
- Remove read_lite and write_lite
- General fixes to asserts and log printing
V3:
- Address some comments from Fred
hw/core/Makefile.objs | 1 + hw/core/register.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 112 +++++++++++++++++++++++++++++++++++ 3 files changed, 273 insertions(+) create mode 100644 hw/core/register.c create mode 100644 include/hw/register.h
hw/core/Makefile.objs +1 -0
@@ -15,4 +15,5 @@ common-obj-$(CONFIG_SOFTMMU) += machine.o common-obj-$(CONFIG_SOFTMMU) += null-machine.o common-obj-$(CONFIG_SOFTMMU) += loader.o common-obj-$(CONFIG_SOFTMMU) += qdev-properties-system.o +common-obj-$(CONFIG_SOFTMMU) += register.o common-obj-$(CONFIG_PLATFORM_BUS) += platform-bus.o
hw/core/register.c new file +160 -0
160 lines not on this page · raw message
include/hw/register.h new file +112 -0
112 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v9 3/8 register: Add Memory API glue
In reply to Alistair FrancisAdd memory io handlers that glue the register API to the memory API. Just translation functions at this stage. Although it does allow for devices to be created without all-in-one mmio r/w handlers.
This patch also adds the RegisterInfoArray struct, which allows all of the individual RegisterInfo structs to be grouped into a single memory region.
---
V8:
- Mask reads based on size as well as writes
V7:
- Remove endianess handling
- Remove assert() and log missing registers
V6:
- Add the memory region later
V5:
- Convert to using only one memory region
hw/core/register.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+)
hw/core/register.c +59 -0
@@ -158,3 +158,62 @@ void register_reset(RegisterInfo *reg)register_write_val (reg, reg->access->reset); } + +void register_write_memory (void *opaque,hwaddr addr, +uint64_t value,unsigned size) +{ +RegisterInfoArray *reg_array = opaque; +RegisterInfo *reg =NULL ; +uint64_t we; +int i; + +for (i =0 ; i < reg_array->num_elements; i++) { +if (reg_array->r[i]->access->addr == addr) { + reg = reg_array->r[i]; +break ; + } + } + +if (!reg) { +qemu_log_mask (LOG_GUEST_ERROR ,"Write to unimplemented register at " \ +"address: %#" PRIx64"\n" , addr); +return ; + } + +/* Generate appropriate write enable mask */ +if (reg->data_size < size) { + we =MAKE_64BIT_MASK (0 , reg->data_size *8 ); + }else { + we =MAKE_64BIT_MASK (0 , size *8 ); + } + +register_write (reg, value, we, reg_array->prefix, + reg_array->debug); +} + +uint64_t register_read_memory (void *opaque,hwaddr addr, +unsigned size) +{ +RegisterInfoArray *reg_array = opaque; +RegisterInfo *reg =NULL ; +uint64_t read_val; +int i; + +for (i =0 ; i < reg_array->num_elements; i++) { +if (reg_array->r[i]->access->addr == addr) { + reg = reg_array->r[i]; +break ; + } + } + +if (!reg) { +qemu_log_mask (LOG_GUEST_ERROR ,"Read to unimplemented register at " \ +"address: %#" PRIx64"\n" , addr); +return 0 ; + } + + read_val =register_read (reg, size *8 , reg_array->prefix, + reg_array->debug); + +return extract64 (read_val,0 , size *8 ); +}
include/hw/register.h +43 -0
65 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v9 4/8 register: Define REG and FIELD macros
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
Define some macros that can be used for defining registers and fields.
The REG32 macro will define A_FOO, for the byte address of a register as well as R_FOO for the uint32_t[] register number (A_FOO / 4).
The FIELD macro will define FOO_BAR_MASK, FOO_BAR_SHIFT and FOO_BAR_LENGTH constants for field BAR in register FOO.
Finally, there are some shorthand helpers for extracting/depositing fields from registers based on these naming schemes.
Usage can greatly reduce the verbosity of device code.
The deposit and extract macros (eg FIELD_EX32, FIELD_DP32 etc.) can be used to generate extract and deposits without any repetition of the name stems.
[ EI Changes:
* Add Deposit macros
]
---
E.g. Currently you have to define something like:
\#define R_FOOREG (0x84/4)
\#define R_FOOREG_BARFIELD_SHIFT 10
\#define R_FOOREG_BARFIELD_LENGTH 5
uint32_t foobar_val = extract32(s->regs[R_FOOREG],
R_FOOREG_BARFIELD_SHIFT,
R_FOOREG_BARFIELD_LENGTH);Which has:
2 macro definitions per field
3 register names ("FOOREG") per extract
2 field names ("BARFIELD") per extract
With these macros this becomes:
REG32(FOOREG, 0x84)FIELD(FOOREG, BARFIELD, 10, 5)
uint32_t foobar_val = ARRAY_FIELD_EX32(s->regs, FOOREG, BARFIELD)
Which has:
1 macro definition per field
1 register name per extract
1 field name per extract
If you are not using arrays for the register data you can just use the non-array "FIELD_" variants and still save 2 name stems:
uint32_t foobar_val = FIELD_EX32(s->fooreg, FOOREG, BARFIELD)
Deposit is similar for depositing values. Deposit has compile-time overflow checking for literals.
For example:
REG32(XYZ1, 0x84)FIELD(XYZ1, TRC, 0, 4)
/* Correctly set XYZ1.TRC = 5. */ARRAY_FIELD_DP32(s->regs, XYZ1, TRC, 5);
/* Incorrectly set XYZ1.TRC = 16. */ARRAY_FIELD_DP32(s->regs, XYZ1, TRC, 16);
The latter assignment results in:
warning: large integer implicitly truncated to unsigned type [-Woverflow]
include/hw/register.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
include/hw/register.h +46 -0
@@ -152,4 +152,50 @@ void register_write_memory(void *opaque, hwaddr addr, uint64_t value,uint64_t register_read_memory (void *opaque,hwaddr addr,unsigned size); +/* Define constants for a 32 bit register */ + +/* This macro will define A_FOO, for the byte address of a register +* as well as R_FOO for the uint32_t[] register number (A_FOO / 4). +*/ +#define REG32 (reg, addr) \ + enum { A_ ## reg = (addr) }; \ + enum { R_ ## reg = (addr) / 4 }; + +/* Define SHIFT, LENGTH and MASK constants for a field within a register */ + +/* This macro will define FOO_BAR_MASK, FOO_BAR_SHIFT and FOO_BAR_LENGTH +* constants for field BAR in register FOO. +*/ +#define FIELD (reg, field, shift, length) \ + enum { R_ ## reg ## _ ## field ## _SHIFT = (shift)}; \ + enum { R_ ## reg ## _ ## field ## _LENGTH = (length)}; \ + enum { R_ ## reg ## _ ## field ## _MASK = \ + MAKE_64BIT_MASK(shift, length)}; + +/* Extract a field from a register */ +#define FIELD_EX32 (storage, reg, field) \ + extract32((storage), R_ ## reg ## _ ## field ## _SHIFT, \ + R_ ## reg ## _ ## field ## _LENGTH) + +/* Extract a field from an array of registers */ +#define ARRAY_FIELD_EX32 (regs, reg, field) \ + FIELD_EX32((regs)[R_ ## reg], reg, field) + +/* Deposit a register field. +* Assigning values larger then the target field will result in +* compilation warnings. +*/ +#define FIELD_DP32 (storage, reg, field, val) ({ \ + struct { \ + unsigned int v:R_ ## reg ## _ ## field ## _LENGTH; \ + } v = { .v = val }; \ + uint32_t d; \ + d = deposit32((storage), R_ ## reg ## _ ## field ## _SHIFT, \ + R_ ## reg ## _ ## field ## _LENGTH, v.v); \ + d; }) + +/* Deposit a field to array of registers. */ +#define ARRAY_FIELD_DP32 (regs, reg, field, val) \ + (regs)[R_ ## reg] = FIELD_DP32((regs)[R_ ## reg], reg, field, val); +#endif
Signature · 1 line
2.7.4
PATCH v9 5/8 register: QOMify
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
QOMify registers as a child of TYPE_DEVICE. This allows registers to define GPIOs.
Define an init helper that will do QOM initialisation.
---
V7:
- Remove uneeded struct
- Fixup documentation
V5:
- Convert to using only one memory region
hw/core/register.c | 23 +++++++++++++++++++++++ include/hw/register.h | 14 ++++++++++++++ 2 files changed, 37 insertions(+)
hw/core/register.c +23 -0
@@ -159,6 +159,17 @@ void register_reset(RegisterInfo *reg)register_write_val (reg, reg->access->reset); } +void register_init (RegisterInfo *reg) +{ +assert (reg); + +if (!reg->data || !reg->access) { +return ; + } + +object_initialize ((void *)reg,sizeof (*reg),TYPE_REGISTER ); +} +void register_write_memory (void *opaque,hwaddr addr,uint64_t value,unsigned size) {
@@ -217,3 +228,15 @@ uint64_t register_read_memory(void *opaque, hwaddr addr,return extract64 (read_val,0 , size *8 ); } + +static const TypeInfo register_info = { + .name =TYPE_REGISTER , + .parent =TYPE_DEVICE , +}; + +static void register_register_types (void ) +{ +type_register_static (®ister_info); +} + +type_init (register_register_types)
include/hw/register.h +14 -0
@@ -11,6 +11,7 @@#ifndef REGISTER_H #define REGISTER_H +#include "hw/qdev-core.h" #include "exec/memory.h" typedef struct RegisterInfo RegisterInfo ;
@@ -72,6 +73,9 @@ struct RegisterAccessInfo {*/ struct RegisterInfo { +/* <private> */ +DeviceState parent_obj; +/* <public> */ void *data;int data_size;
@@ -81,6 +85,9 @@ struct RegisterInfo {void *opaque; }; +#define TYPE_REGISTER "qemu,register" +#define REGISTER (obj) OBJECT_CHECK(RegisterInfo, (obj), TYPE_REGISTER) +/** * This structure is used to group all of the individual registers which are * modeled using the RegisterInfo structure.
@@ -132,6 +139,13 @@ uint64_t register_read(RegisterInfo *reg, uint64_t re, const char* prefix,void register_reset (RegisterInfo *reg);/** +* Initialize a register. +* @reg: Register to initialize +*/ + +void register_init (RegisterInfo *reg); + +/** * Memory API MMIO write handler that will write to a Register API register. * @opaque: RegisterInfo to write to * @addr: Address to write
Signature · 1 line
2.7.4
PATCH v9 6/8 register: Add block initialise helper
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
Add a helper that will scan a static RegisterAccessInfo Array and populate a container MemoryRegion with registers as defined.
---
The reason that I'm not using GArray is because the array needs to store the memory region that covers all of the registers.
V9:
- Improve the finalise function
V8:
- Add a finalise function used to free the allocated memory
V7:
- Return the memory region instead of adding it as a subregion
V6:
- Fixup the loop logic
V5:
- Convert to only using one memory region
V3:
- Fix typo
V2:
- Use memory_region_add_subregion_no_print()
hw/core/register.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)
hw/core/register.c +45 -0
@@ -229,6 +229,51 @@ uint64_t register_read_memory(void *opaque, hwaddr addr,return extract64 (read_val,0 , size *8 ); } +RegisterInfoArray *register_init_block32 (DeviceState *owner, +const RegisterAccessInfo *rae, +int num,RegisterInfo *ri, +uint32_t *data, +const MemoryRegionOps *ops, +bool debug_enabled, +uint64_t memory_size) +{ +const char *device_prefix =object_get_typename (OBJECT (owner)); +RegisterInfoArray *r_array =g_new0 (RegisterInfoArray,1 ); +int i; + + r_array->r = g_new0(RegisterInfo *, num); + r_array->num_elements = num; + r_array->debug = debug_enabled; + r_array->prefix = device_prefix; + +for (i =0 ; i < num; i++) { +int index = rae[i].addr /4 ; +RegisterInfo *r = &ri[index]; + + *r = (RegisterInfo ) { + .data = &data[index], + .data_size =sizeof (uint32_t ), + .access = &rae[i], + .opaque = owner, + }; +register_init (r); + + r_array->r[i] = r; + } + +memory_region_init_io (&r_array->mem,OBJECT (owner), ops, r_array, + device_prefix, memory_size); + +return r_array; +} + +void register_finlize_block (RegisterInfoArray *r_array) +{ +object_unparent (OBJECT (&r_array->mem)); +g_free (r_array->r); +g_free (r_array); +} +static const TypeInfo register_info = { .name =TYPE_REGISTER , .parent =TYPE_DEVICE ,
include/hw/register.h +39 -0
@@ -100,6 +100,8 @@ struct RegisterInfo {*/ struct RegisterInfoArray { +MemoryRegion mem; +int num_elements;RegisterInfo **r;
@@ -166,6 +168,43 @@ void register_write_memory(void *opaque, hwaddr addr, uint64_t value,uint64_t register_read_memory (void *opaque,hwaddr addr,unsigned size); +/** +* Init a block of registers into a container MemoryRegion. A +* number of constant register definitions are parsed to create a corresponding +* array of RegisterInfo's. +* +* @owner: device owning the registers +* @rae: Register definitions to init +* @num: number of registers to init (length of @rae) +* @ri: Register array to init, must already be allocated +* @data: Array to use for register data, must already be allocated +* @ops: Memory region ops to access registers. +* @debug enabled: turn on/off verbose debug information +* returns: An structure containing all of the registers and an initialized +memory region (r_array->mem) the caller should add to a container. +*/ + +RegisterInfoArray *register_init_block32 (DeviceState *owner, +const RegisterAccessInfo *rae, +int num,RegisterInfo *ri, +uint32_t *data, +const MemoryRegionOps *ops, +bool debug_enabled, +uint64_t memory_size); + +/** +* This function should be called to cleanup the registers that were initialized +* when calling register_init_block32(). This function should only be called +* from the devices instance_finalize function. +* +* Any memory operations that the device performed that require cleanup (such +* as creating subregions) need to be called before calling this function. +* +* @r_array: An structure containing all of the registers. +*/ + +void register_finlize_block (RegisterInfoArray *r_array); +/* Define constants for a 32 bit register */ /* This macro will define A_FOO, for the byte address of a register
Signature · 1 line
2.7.4
PATCH v9 7/8 dma: Add Xilinx Zynq devcfg device model
In reply to Alistair FrancisAdd a minimal model for the devcfg device which is part of Zynq. This model supports DMA capabilities and interrupt generation.
---
V9:
- Don't indent #defines
V7:
- Corrections to the internal device
V5:
- Corrections to the device model logic
default-configs/arm-softmmu.mak | 1 + hw/dma/Makefile.objs | 1 + hw/dma/xlnx-zynq-devcfg.c | 400 ++++++++++++++++++++++++++++++++++++++ include/hw/dma/xlnx-zynq-devcfg.h | 62 ++++++ 4 files changed, 464 insertions(+) create mode 100644 hw/dma/xlnx-zynq-devcfg.c create mode 100644 include/hw/dma/xlnx-zynq-devcfg.h
default-configs/arm-softmmu.mak +1 -0
@@ -66,6 +66,7 @@ CONFIG_PXA2XX=y CONFIG_BITBANG_I2C=y CONFIG_FRAMEBUFFER=y CONFIG_XILINX_SPIPS=y +CONFIG_ZYNQ_DEVCFG=y CONFIG_ARM11SCU=y CONFIG_A9SCU=y
hw/dma/Makefile.objs +1 -0
@@ -5,6 +5,7 @@ common-obj-$(CONFIG_PL330) += pl330.o common-obj-$(CONFIG_I82374) += i82374.o common-obj-$(CONFIG_I8257) += i8257.o common-obj-$(CONFIG_XILINX_AXI) += xilinx_axidma.o +common-obj-$(CONFIG_ZYNQ_DEVCFG) += xlnx-zynq-devcfg.o common-obj-$(CONFIG_ETRAXFS) += etraxfs_dma.o common-obj-$(CONFIG_STP2000) += sparc32_dma.o common-obj-$(CONFIG_SUN4M) += sun4m_iommu.o
hw/dma/xlnx-zynq-devcfg.c new file +400 -0
400 lines not on this page · raw message
include/hw/dma/xlnx-zynq-devcfg.h new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v9 8/8 xilinx_zynq: Connect devcfg to the Zynq machine model
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
---
V7:
- Remove unused property add
V4:
- Small corrections to the device model logic
hw/arm/xilinx_zynq.c | 6 ++++++ 1 file changed, 6 insertions(+)
hw/arm/xilinx_zynq.c +6 -0
@@ -294,6 +294,12 @@ static void zynq_init(MachineState *machine)sysbus_connect_irq (busdev, n +1 , pic[dma_irqs[n] -IRQ_OFFSET ]); } + dev =qdev_create (NULL ,"xlnx.ps7-dev-cfg" ); +qdev_init_nofail (dev); + busdev =SYS_BUS_DEVICE (dev); +sysbus_connect_irq (busdev,0 , pic[40 -IRQ_OFFSET ]); +sysbus_mmio_map (busdev,0 ,0xF8007000 ); + zynq_binfo.ram_size = ram_size; zynq_binfo.kernel_filename = kernel_filename; zynq_binfo.kernel_cmdline = kernel_cmdline;
Signature · 1 line
2.7.4
Peter Maydell
AppliedQuoted 4 lines · Alistair Francis
This patch series is based on Peter C's original register API. His original cover letter is below.
Future work: Allow support for memory attributes.
Thanks, applied to target-arm.next. (You still had a typo in your finalize function name but I have fixed it.)
thanks
-- PMM
Quoted 8 lines · Peter Maydell
Quoted 4 lines · Alistair Francis
This patch series is based on Peter C's original register API. His original cover letter is below.
Future work: Allow support for memory attributes.
Thanks, applied to target-arm.next. (You still had a typo in your finalize function name but I have fixed it.)
Great! Thanks so much everyone who helped review this.
Thanks,
Alistair
Quoted 4 lines
thanks
-- PMM