data-driven device registers
v5 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.
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 (7):
bitops: Add MAKE_64BIT_MASK macro
register: Add Register API
register: Add Memory API glue
register: Add support for decoding information
dma: Add Xilinx Zynq devcfg device model
register: Add GPIO API
xlnx-zynqmp: Connect the ZynqMP IOU SLCR
Peter Crosthwaite (8):
register: Define REG and FIELD macros
register: QOMify
register: Add block initialise helper
xilinx_zynq: Connect devcfg to the Zynq machine model
qdev: Define qdev_get_gpio_out
qdev: Add qdev_pass_all_gpios API
irq: Add opaque setter routine
misc: Introduce ZynqMP IOU SLCR
default-configs/arm-softmmu.mak | 1 + hw/arm/xilinx_zynq.c | 8 + hw/arm/xlnx-zynqmp.c | 13 ++ hw/core/Makefile.objs | 1 + hw/core/irq.c | 5 + hw/core/qdev.c | 21 ++ hw/core/register.c | 376 +++++++++++++++++++++++++++++++ hw/dma/Makefile.objs | 1 + hw/dma/xlnx-zynq-devcfg.c | 394 +++++++++++++++++++++++++++++++++ hw/misc/Makefile.objs | 1 + hw/misc/xlnx-zynqmp-iou-slcr.c | 115 ++++++++++ include/hw/arm/xlnx-zynqmp.h | 2 + include/hw/dma/xlnx-zynq-devcfg.h | 62 ++++++ include/hw/irq.h | 2 + include/hw/misc/xlnx-zynqmp-iou-slcr.h | 47 ++++ include/hw/qdev-core.h | 3 + include/hw/register.h | 263 ++++++++++++++++++++++ include/qemu/bitops.h | 3 + 18 files changed, 1318 insertions(+) create mode 100644 hw/core/register.c create mode 100644 hw/dma/xlnx-zynq-devcfg.c create mode 100644 hw/misc/xlnx-zynqmp-iou-slcr.c create mode 100644 include/hw/dma/xlnx-zynq-devcfg.h create mode 100644 include/hw/misc/xlnx-zynqmp-iou-slcr.h create mode 100644 include/hw/register.h
Signature · 1 line
2.5.0
PATCH v5 1/15 bitops: Add MAKE_64BIT_MASK macro
Add a macro that creates a 64bit value which has length number of ones shifted acrros by the value of shift.
---
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) \ + (((1ull << (length)) - 1) << shift) +/** * set_bit - Set a bit in memory * @nr: the bit to set
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v5 01/15] bitops: Add MAKE_64BIT_MASK macro
Quoted 4 lines · Alistair Francis
Add a macro that creates a 64bit value which has length number of ones shifted acrros by the value of shift.
Signed-off-by Alistair Francis <[email protected]>
Quoted 13 lines
---
V5:
- Re-write to a 64-bit mask instead ofONES()
- Re-order this patch in the seriesinclude/qemu/bitops.h | 3 +++ 1 file changed, 3 insertions(+)include/qemu/bitops.h +0
-0
include/qemu/bitops.h line 32
Quoted 9 lines
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) \ + (((1ull << (length)) - 1) << shift) +/** * set_bit - Set a bit in memory * @nr: the bit to set
--
Alex Bennée
PATCH v5 2/15 register: Add Register API
In reply to Alistair FrancisThis API provides some encapsulation of registers and factors our 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 allow 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.
---
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 | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 108 ++++++++++++++++++++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 hw/core/register.c create mode 100644 include/hw/register.h
hw/core/Makefile.objs +1 -0
@@ -14,4 +14,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 +148 -0
148 lines not on this page · raw message
include/hw/register.h new file +108 -0
108 lines not on this page · raw message
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v5 02/15] register: Add Register API
Quoted 315 lines · Alistair Francis
This API provides some encapsulation of registers and factors our 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 allow 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 asread_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/disabledUseful 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.
Signed-off-by Peter Crosthwaite <[email protected]>Signed-off-by Alistair Francis <[email protected]>---
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
- Removeread_liteandwrite_lite
- General fixes to asserts and log printing
V3:
- Address some comments from Fredhw/core/Makefile.objs | 1 + hw/core/register.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 108 ++++++++++++++++++++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 hw/core/register.c create mode 100644 include/hw/register.hhw/core/Makefile.objs +1
-0@@ -14,4 +14,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.ohw/core/register.c new file +148
-0@@ -0,0 +1,148 @@ +/* +* Register Definition API +* +* Copyright (c) 2016 Xilinx Inc. +* Copyright (c) 2013 Peter Crosthwaite <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "hw/register.h" +#include "hw/qdev.h" +#include "qemu/log.h" + +static inline void register_write_val (RegisterInfo *reg,uint64_t val) +{ +g_assert (reg->data); + +switch (reg->data_size) { +case 1 : + *(uint8_t *)reg->data = val; +break ; +case 2 : + *(uint16_t *)reg->data = val; +break ; +case 4 : + *(uint32_t *)reg->data = val; +break ; +case 8 : + *(uint64_t *)reg->data = val; +break ; +default : +g_assert_not_reached (); + } +} + +static inline uint64_t register_read_val (RegisterInfo *reg) +{ +switch (reg->data_size) { +case 1 : +return *(uint8_t *)reg->data; +case 2 : +return *(uint16_t *)reg->data; +case 4 : +return *(uint32_t *)reg->data; +case 8 : +return *(uint64_t *)reg->data; +default : +g_assert_not_reached (); + } +return 0 ;/* unreachable */ +} + +void register_write (RegisterInfo *reg,uint64_t val,uint64_t we) +{ +uint64_t old_val, new_val, test, no_w_mask; +const RegisterAccessInfo *ac; + +assert (reg); + + ac = reg->access; + +if (!ac || !ac->name) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: write to undefined device state " +"(written value: %#" PRIx64")\n" , reg->prefix, val); +return ; + } + + old_val = reg->data ?register_read_val (reg) : ac->reset; + + test = (old_val ^ val) & ac->rsvd; +if (test) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: change of value in reserved bit" +"fields: %#" PRIx64")\n" , reg->prefix, test); + } + + test = val & ac->unimp; +if (test) { +qemu_log_mask (LOG_UNIMP , +"%s:%s writing %#" PRIx64" to unimplemented bits:" \ +" %#" PRIx64"" , + reg->prefix, reg->access->name, val, ac->unimp); + } + +/* Create the no write mask based on the read only, write to clear and +* reserved bit masks. +*/ + no_w_mask = ac->ro | ac->w1c | ac->rsvd | ~we; + new_val = (val & ~no_w_mask) | (old_val & no_w_mask); + new_val &= ~(val & ac->w1c); + +if (ac->pre_write) { + new_val = ac->pre_write (reg, new_val); + } + +if (reg->debug) { +qemu_log ("%s:%s: write of value %#" PRIx64"\n" , reg->prefix, ac->name, + new_val); + } + +register_write_val (reg, new_val); + +if (ac->post_write) { + ac->post_write (reg, new_val); + } +} + +uint64_t register_read (RegisterInfo *reg) +{ +uint64_t ret; +const RegisterAccessInfo *ac; + +assert (reg); + + ac = reg->access; +if (!ac || !ac->name) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: read from undefined device state\n" , + reg->prefix); +return 0 ; + } + + ret = reg->data ?register_read_val (reg) : ac->reset; + +register_write_val (reg, ret & ~ac->cor); + +if (ac->post_read) { + ret = ac->post_read (reg, ret); + } + +if (reg->debug) { +qemu_log ("%s:%s: read of value %#" PRIx64"\n" , reg->prefix, + ac->name, ret); + } + +return ret; +} + +void register_reset (RegisterInfo *reg) +{ +g_assert (reg); + +if (!reg->data || !reg->access) { +return ; + } + +register_write_val (reg, reg->access->reset); +}include/hw/register.h new file +98
-0@@ -0,0 +1,108 @@ +/* +* Register Definition API +* +* Copyright (c) 2016 Xilinx Inc. +* Copyright (c) 2013 Peter Crosthwaite <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#ifndef REGISTER_H +#define REGISTER_H + +#include "exec/memory.h" + +typedef struct RegisterInfo RegisterInfo ; +typedef struct RegisterAccessInfo RegisterAccessInfo ; + +/** +* Access description for a register that is part of guest accessible device +* state. +* +* @name: String name of the register +* @ro: whether or not the bit is read-only +* @w1c: bits with the common write 1 to clear semantic. +* @reset: reset value. +* @cor: Bits that are clear on read +* @rsvd: Bits that are reserved and should not be changed +* +* @pre_write: Pre write callback. Passed the value that's to be written, +* immediately before the actual write. The returned value is what is written, +* giving the handler a chance to modify the written value. +* @post_write: Post write callback. Passed the written value. Most write side +* effects should be implemented here. +* +* @post_read: Post read callback. Passes the value that is about to be returned +* for a read. The return value from this function is what is ultimately read, +* allowing this function to modify the value before return to the client. +*/ + +struct RegisterAccessInfo { +const char *name; +uint64_t ro; +uint64_t w1c; +uint64_t reset; +uint64_t cor; +uint64_t rsvd; +uint64_t unimp; + +uint64_t (*pre_write)(RegisterInfo *reg,uint64_t val); +void (*post_write)(RegisterInfo *reg,uint64_t val); + +uint64_t (*post_read)(RegisterInfo *reg,uint64_t val); +}; + +/** +* A register that is part of guest accessible state +* @data: pointer to the register data. Will be cast +* to the relevant uint type depending on data_size. +* @data_size: Size of the register in bytes. Must be +* 1, 2, 4 or 8 +* +* @access: Access description of this register +* +* @debug: Whether or not verbose debug is enabled +* @prefix: String prefix for log and debug messages +* +* @opaque: Opaque data for the register +*/ + +struct RegisterInfo { +/* <public> */ +void *data; +int data_size; + +const RegisterAccessInfo *access; + +bool debug; +const char *prefix; + +void *opaque; +}; + +/** +* write a value to a register, subject to its restrictions +* @reg: register to write to +* @val: value to write +* @we: write enable mask +*/ + +void register_write (RegisterInfo *reg,uint64_t val,uint64_t we); + +/** +* read a value from a register, subject to its restrictions +* @reg: register to read from +* returns: value read +*/ +
include/hw/register.h line 108
Quoted 10 lines
include/hw/register.h new file +10
-0+uint64_t register_read (RegisterInfo *reg); + +/** +* reset a register +* @reg: register to reset +*/ + +void register_reset (RegisterInfo *reg); + +#endif
--
Alex Bennée
PATCH v5 3/15 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.
---
V5:
- Convert to using only one memory region
hw/core/register.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+)
hw/core/register.c +70 -0
@@ -146,3 +146,73 @@ void register_reset(RegisterInfo *reg)register_write_val (reg, reg->access->reset); } + +static inline void register_write_memory (void *opaque,hwaddr addr, +uint64_t value,unsigned size,bool be) +{ +RegisterInfoArray *reg_array = opaque; +RegisterInfo *reg =NULL ; +uint64_t we = ~0 ; +int i, shift =0 ; + +for (i =0 ; i < reg_array->num_elements; i++) { +if (reg_array->r[i]->access->decode.addr == addr) { + reg = reg_array->r[i]; +break ; + } + } +assert (reg); + +/* Generate appropriate write enable mask and shift values */ +if (reg->data_size < size) { + we =MAKE_64BIT_MASK (0 , reg->data_size *8 ); + shift =8 * (be ? reg->data_size - size :0 ); + }else if (reg->data_size >= size) { + we =MAKE_64BIT_MASK (0 , size *8 ); + } + +register_write (reg, value << shift, we << shift); +} + +void register_write_memory_be (void *opaque,hwaddr addr,uint64_t value, +unsigned size) +{ +register_write_memory (opaque, addr, value, size, true); +} + + +void register_write_memory_le (void *opaque,hwaddr addr,uint64_t value, +unsigned size) +{ +register_write_memory (opaque, addr, value, size, false); +} + +static inline uint64_t register_read_memory (void *opaque,hwaddr addr, +unsigned size,bool be) +{ +RegisterInfoArray *reg_array = opaque; +RegisterInfo *reg =NULL ; +int i, shift; + +for (i =0 ; i < reg_array->num_elements; i++) { +if (reg_array->r[i]->access->decode.addr == addr) { + reg = reg_array->r[i]; +break ; + } + } +assert (reg); + + shift =8 * (be ? reg->data_size - size :0 ); + +return (register_read (reg) >> shift) &MAKE_64BIT_MASK (0 , size *8 ); +} + +uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size) +{ +return register_read_memory (opaque, addr, size, true); +} + +uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size) +{ +return register_read_memory (opaque, addr, size, false); +}
include/hw/register.h +51 -0
73 lines not on this page · raw message
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v5 03/15] register: Add Memory API glue
Quoted 126 lines · Alistair Francis
Add 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.
Signed-off-by Peter Crosthwaite <[email protected]>Signed-off-by Alistair Francis <[email protected]>---
V5:
- Convert to using only one memory regionhw/core/register.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+)hw/core/register.c +70
-0@@ -146,3 +146,73 @@ void register_reset(RegisterInfo *reg)register_write_val (reg, reg->access->reset); } + +static inline void register_write_memory (void *opaque,hwaddr addr, +uint64_t value,unsigned size,bool be) +{ +RegisterInfoArray *reg_array = opaque; +RegisterInfo *reg =NULL ; +uint64_t we = ~0 ; +int i, shift =0 ; + +for (i =0 ; i < reg_array->num_elements; i++) { +if (reg_array->r[i]->access->decode.addr == addr) { + reg = reg_array->r[i]; +break ; + } + } +assert (reg); + +/* Generate appropriate write enable mask and shift values */ +if (reg->data_size < size) { + we =MAKE_64BIT_MASK (0 , reg->data_size *8 ); + shift =8 * (be ? reg->data_size - size :0 ); + }else if (reg->data_size >= size) { + we =MAKE_64BIT_MASK (0 , size *8 ); + } + +register_write (reg, value << shift, we << shift); +} + +void register_write_memory_be (void *opaque,hwaddr addr,uint64_t value, +unsigned size) +{ +register_write_memory (opaque, addr, value, size, true); +} + + +void register_write_memory_le (void *opaque,hwaddr addr,uint64_t value, +unsigned size) +{ +register_write_memory (opaque, addr, value, size, false); +} + +static inline uint64_t register_read_memory (void *opaque,hwaddr addr, +unsigned size,bool be) +{ +RegisterInfoArray *reg_array = opaque; +RegisterInfo *reg =NULL ; +int i, shift; + +for (i =0 ; i < reg_array->num_elements; i++) { +if (reg_array->r[i]->access->decode.addr == addr) { + reg = reg_array->r[i]; +break ; + } + } +assert (reg); + + shift =8 * (be ? reg->data_size - size :0 ); + +return (register_read (reg) >> shift) &MAKE_64BIT_MASK (0 , size *8 ); +} + +uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size) +{ +return register_read_memory (opaque, addr, size, true); +} + +uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size) +{ +return register_read_memory (opaque, addr, size, false); +}include/hw/register.h +8
-0@@ -15,6 +15,7 @@typedef struct RegisterInfo RegisterInfo ;typedef struct RegisterAccessInfo RegisterAccessInfo ; +typedef struct RegisterInfoArray RegisterInfoArray ;/** * Access description for a register that is part of guest accessible device @@ -51,6 +52,10 @@ struct RegisterAccessInfo {void (*post_write)(RegisterInfo *reg,uint64_t val);uint64_t (*post_read)(RegisterInfo *reg,uint64_t val); + +struct { +hwaddr addr; + } decode; };/** @@ -82,6 +87,26 @@ struct RegisterInfo { };/** +* This structure is used to group all of the individual registers which are +* modeled using the RegisterInfo strucutre. +*
include/hw/register.h struct RegisterInfo { · line 102
Quoted 10 lines
include/hw/register.h +10
-0+* @r is an aray containing of all the relevent RegisterInfo structures. +* +* @num_elements is the number of elements in the array r +* +* @mem: optional Memory region for the register +*/ + +struct RegisterInfoArray { +/* <private> */ +MemoryRegion mem;
I never see this used. I thought with the other changes the memory region enclosed the group of registers. Wouldn't a private mem violate that?
include/hw/register.h struct RegisterInfo { · line 107
Quoted 5 lines
include/hw/register.h +5
-0+ +/* <public> */ +int num_elements; +RegisterInfo **r; +};
Without the extra bits why re-invent GArray?
Quoted 26 lines
include/hw/register.h +19
-0+ +/** * write a value to a register, subject to its restrictions * @reg: register to write to * @val: value to write @@ -105,4 +130,30 @@ uint64_t register_read(RegisterInfo *reg);void register_reset (RegisterInfo *reg); +/** +* Memory API MMIO write handler that will write to a Register API register. +* _be for big endian variant and _le for little endian. +* @opaque: RegisterInfo to write to +* @addr: Address to write +* @value: Value to write +* @size: Number of bytes to write +*/ + +void register_write_memory_be (void *opaque,hwaddr addr,uint64_t value, +unsigned size); +void register_write_memory_le (void *opaque,hwaddr addr,uint64_t value, +unsigned size); + +/** +* Memory API MMIO read handler that will read from a Register API register. +* _be for big endian variant and _le for little endian.
include/hw/register.h uint64_t register_read(RegisterInfo *reg); · line 159
Quoted 10 lines
include/hw/register.h +9
-0+* @opaque: RegisterInfo to read from +* @addr: Address to read +* @size: Number of bytes to read +* returns: Value read from register +*/ + +uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size); +uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size); +#endif
--
Alex Bennée
Re: [Qemu-devel] [PATCH v5 03/15] register: Add Memory API glue
Quoted 125 lines · Alex Bennée
Quoted 122 lines · Alistair Francis
Add 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.
Signed-off-by Peter Crosthwaite <[email protected]>Signed-off-by Alistair Francis <[email protected]>---
V5:
- Convert to using only one memory regionhw/core/register.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+)hw/core/register.c +70
-0@@ -146,3 +146,73 @@ void register_reset(RegisterInfo *reg)register_write_val (reg, reg->access->reset); } + +static inline void register_write_memory (void *opaque,hwaddr addr, +uint64_t value,unsigned size,bool be) +{ +RegisterInfoArray *reg_array = opaque; +RegisterInfo *reg =NULL ; +uint64_t we = ~0 ; +int i, shift =0 ; + +for (i =0 ; i < reg_array->num_elements; i++) { +if (reg_array->r[i]->access->decode.addr == addr) { + reg = reg_array->r[i]; +break ; + } + } +assert (reg); + +/* Generate appropriate write enable mask and shift values */ +if (reg->data_size < size) { + we =MAKE_64BIT_MASK (0 , reg->data_size *8 ); + shift =8 * (be ? reg->data_size - size :0 ); + }else if (reg->data_size >= size) { + we =MAKE_64BIT_MASK (0 , size *8 ); + } + +register_write (reg, value << shift, we << shift); +} + +void register_write_memory_be (void *opaque,hwaddr addr,uint64_t value, +unsigned size) +{ +register_write_memory (opaque, addr, value, size, true); +} + + +void register_write_memory_le (void *opaque,hwaddr addr,uint64_t value, +unsigned size) +{ +register_write_memory (opaque, addr, value, size, false); +} + +static inline uint64_t register_read_memory (void *opaque,hwaddr addr, +unsigned size,bool be) +{ +RegisterInfoArray *reg_array = opaque; +RegisterInfo *reg =NULL ; +int i, shift; + +for (i =0 ; i < reg_array->num_elements; i++) { +if (reg_array->r[i]->access->decode.addr == addr) { + reg = reg_array->r[i]; +break ; + } + } +assert (reg); + + shift =8 * (be ? reg->data_size - size :0 ); + +return (register_read (reg) >> shift) &MAKE_64BIT_MASK (0 , size *8 ); +} + +uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size) +{ +return register_read_memory (opaque, addr, size, true); +} + +uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size) +{ +return register_read_memory (opaque, addr, size, false); +}include/hw/register.h +5
-0@@ -15,6 +15,7 @@typedef struct RegisterInfo RegisterInfo ;typedef struct RegisterAccessInfo RegisterAccessInfo ; +typedef struct RegisterInfoArray RegisterInfoArray ;/** * Access description for a register that is part of guest accessible device @@ -51,6 +52,10 @@ struct RegisterAccessInfo {void (*post_write)(RegisterInfo *reg,uint64_t val);uint64_t (*post_read)(RegisterInfo *reg,uint64_t val); + +struct { +hwaddr addr; + } decode; };/** @@ -82,6 +87,26 @@ struct RegisterInfo { };
Quoted 16 lines
Quoted 4 lines
include/hw/register.h +3
-0/** +* This structure is used to group all of the individual registers which are +* modeled using the RegisterInfo strucutre. +* include/hw/register.h struct RegisterInfo { · line 102
Quoted 10 lines
include/hw/register.h +10
-0+* @r is an aray containing of all the relevent RegisterInfo structures. +* +* @num_elements is the number of elements in the array r +* +* @mem: optional Memory region for the register +*/ + +struct RegisterInfoArray { +/* <private> */ +MemoryRegion mem;I never see this used. I thought with the other changes the memory
It isn't used here, it is used later on in the series when the register_init_block32() function is added.
I can move it back to the patch if you wish.
Quoted 1 line
region enclosed the group of registers. Wouldn't a private mem violate that?
Yes, you are right. I'll remove the private comment.
Quoted 8 lines
include/hw/register.h struct RegisterInfo { · line 107
Quoted 6 lines
include/hw/register.h +5
-0+ +/* <public> */ +int num_elements; +RegisterInfo **r; +};Without the extra bits why re-invent GArray?
What do you mean?
Thanks,
Alistair
Quoted 42 lines
Quoted 27 lines
include/hw/register.h +19
-0+ +/** * write a value to a register, subject to its restrictions * @reg: register to write to * @val: value to write @@ -105,4 +130,30 @@ uint64_t register_read(RegisterInfo *reg);void register_reset (RegisterInfo *reg); +/** +* Memory API MMIO write handler that will write to a Register API register. +* _be for big endian variant and _le for little endian. +* @opaque: RegisterInfo to write to +* @addr: Address to write +* @value: Value to write +* @size: Number of bytes to write +*/ + +void register_write_memory_be (void *opaque,hwaddr addr,uint64_t value, +unsigned size); +void register_write_memory_le (void *opaque,hwaddr addr,uint64_t value, +unsigned size); + +/** +* Memory API MMIO read handler that will read from a Register API register. +* _be for big endian variant and _le for little endian. include/hw/register.h uint64_t register_read(RegisterInfo *reg); · line 159
Quoted 10 lines
include/hw/register.h +9
-0+* @opaque: RegisterInfo to read from +* @addr: Address to read +* @size: Number of bytes to read +* returns: Value read from register +*/ + +uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size); +uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size); +#endif --
Alex Bennée
PATCH v5 4/15 register: Add support for decoding information
In reply to Alistair FrancisAllow defining of optional address decoding information in register definitions. This is useful for clients that want to associate registers with specific addresses.
---
V3:
- Remove unused flags option
include/hw/register.h | 5 +++++ 1 file changed, 5 insertions(+)
include/hw/register.h +5 -0
@@ -39,6 +39,11 @@ typedef struct RegisterInfoArray RegisterInfoArray;* allowing this function to modify the value before return to the client. */ +#define REG_DECODE_READ (1 << 0) +#define REG_DECODE_WRITE (1 << 1) +#define REG_DECODE_EXECUTE (1 << 2) +#define REG_DECODE_RW (REG_DECODE_READ | REG_DECODE_WRITE) +struct RegisterAccessInfo {const char *name;uint64_t ro;
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v5 04/15] register: Add support for decoding information
Quoted 19 lines · Alistair Francis
Allow defining of optional address decoding information in register definitions. This is useful for clients that want to associate registers with specific addresses.
Signed-off-by Peter Crosthwaite <[email protected]>Signed-off-by Alistair Francis <[email protected]>---
V3:
- Remove unused flags optioninclude/hw/register.h | 5 +++++ 1 file changed, 5 insertions(+)include/hw/register.h +0
-0@@ -39,6 +39,11 @@ typedef struct RegisterInfoArray RegisterInfoArray;* allowing this function to modify the value before return to the client.
include/hw/register.h typedef struct RegisterInfoArray RegisterInfoArray; · line 49
Quoted 10 lines
include/hw/register.h +5
-0*/ +#define REG_DECODE_READ (1 << 0) +#define REG_DECODE_WRITE (1 << 1) +#define REG_DECODE_EXECUTE (1 << 2) +#define REG_DECODE_RW (REG_DECODE_READ | REG_DECODE_WRITE) +struct RegisterAccessInfo {const char *name;uint64_t ro;
Without any other context I'm unsure of how these defines are going to be used. Are these just bits at the bottom of an address?
Nothing in this patch series uses them so I suggest you drop this patch for now.
--
Alex Bennée
Re: [Qemu-devel] [PATCH v5 04/15] register: Add support for decoding information
Quoted 36 lines · Alex Bennée
Quoted 19 lines · Alistair Francis
Allow defining of optional address decoding information in register definitions. This is useful for clients that want to associate registers with specific addresses.
Signed-off-by Peter Crosthwaite <[email protected]>Signed-off-by Alistair Francis <[email protected]>---
V3:
- Remove unused flags optioninclude/hw/register.h | 5 +++++ 1 file changed, 5 insertions(+)include/hw/register.h +0
-0@@ -39,6 +39,11 @@ typedef struct RegisterInfoArray RegisterInfoArray;* allowing this function to modify the value before return to the client. include/hw/register.h typedef struct RegisterInfoArray RegisterInfoArray; · line 49
Quoted 10 lines
include/hw/register.h +5
-0*/ +#define REG_DECODE_READ (1 << 0) +#define REG_DECODE_WRITE (1 << 1) +#define REG_DECODE_EXECUTE (1 << 2) +#define REG_DECODE_RW (REG_DECODE_READ | REG_DECODE_WRITE) +struct RegisterAccessInfo {const char *name;uint64_t ro;Without any other context I'm unsure of how these defines are going to be used. Are these just bits at the bottom of an address?
Quoted 2 lines
Nothing in this patch series uses them so I suggest you drop this patch for now.
Agreed. I will drop this patch.
Thanks,
Alistair
Quoted 4 lines
--
Alex Bennée
PATCH v5 5/15 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 F_EX32, AF_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 = AF_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 "F_" variants and still save 2 name stems:
uint32_t foobar_val = F_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. */AF_DP32(s->regs, XYZ1, TRC, 5);
/* Incorrectly set XYZ1.TRC = 16. */AF_DP32(s->regs, XYZ1, TRC, 16);
The latter assignment results in:
warning: large integer implicitly truncated to unsigned type [-Woverflow]
include/hw/register.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
include/hw/register.h +38 -0
@@ -161,4 +161,42 @@ void register_write_memory_le(void *opaque, hwaddr addr, uint64_t value,uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size);uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size); +/* Define constants for a 32 bit register */ +#define REG32 (reg, addr) \ + enum { A_ ## reg = (addr) }; \ + enum { R_ ## reg = (addr) / 4 }; + +/* Define SHIFT, LEGTH and MASK constants for a field within a register */ +#define FIELD (reg, field, shift, length) \ + enum { R_ ## reg ## _ ## field ## _SHIFT = (shift)}; \ + enum { R_ ## reg ## _ ## field ## _LENGTH = (length)}; \ + enum { R_ ## reg ## _ ## field ## _MASK = (((1ULL << (length)) - 1) \ + << (shift)) }; + +/* Extract a field from a register */ + +#define F_EX32 (storage, reg, field) \ + extract32((storage), R_ ## reg ## _ ## field ## _SHIFT, \ + R_ ## reg ## _ ## field ## _LENGTH) + +/* Extract a field from an array of registers */ + +#define AF_EX32 (regs, reg, field) \ + F_EX32((regs)[R_ ## reg], reg, field) + +/* Deposit a register field. */ + +#define F_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 AF_DP32 (regs, reg, field, val) \ + (regs)[R_ ## reg] = F_DP32((regs)[R_ ## reg], reg, field, val);#endif
Signature · 1 line
2.5.0
PATCH v5 6/15 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.
---
V5:
- Convert to using only one memory region
hw/core/register.c | 23 +++++++++++++++++++++++ include/hw/register.h | 15 +++++++++++++++ 2 files changed, 38 insertions(+)
hw/core/register.c +23 -0
@@ -147,6 +147,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 ); +} +static inline void register_write_memory (void *opaque,hwaddr addr,uint64_t value,unsigned size,bool be) {
@@ -216,3 +227,15 @@ uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size) {return register_read_memory (opaque, addr, size, false); } + +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 +15 -0
@@ -11,6 +11,7 @@#ifndef REGISTER_H #define REGISTER_H +#include "hw/qdev-core.h" #include "exec/memory.h" typedef struct RegisterInfo RegisterInfo ;
@@ -79,6 +80,9 @@ struct RegisterAccessInfo {*/ struct RegisterInfo { +/* <private> */ +DeviceState parent_obj; +/* <public> */ void *data;int data_size;
@@ -91,6 +95,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 strucutre.
@@ -136,6 +143,14 @@ uint64_t register_read(RegisterInfo *reg);void register_reset (RegisterInfo *reg);/** +* Initialize a register. GPIO's are setup as IOs to the specified device. +* Fast paths for eligible registers are enabled. +* @reg: Register to initialize +*/ + +void register_init (RegisterInfo *reg); + +/** * Memory API MMIO write handler that will write to a Register API register. * _be for big endian variant and _le for little endian. * @opaque: RegisterInfo to write to
Signature · 1 line
2.5.0
PATCH v5 7/15 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.
---
V3:
- Fix typo
V2:
- Use memory_region_add_subregion_no_print()
hw/core/register.c | 39 +++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 20 ++++++++++++++++++++ 2 files changed, 59 insertions(+)
hw/core/register.c +39 -0
@@ -228,6 +228,45 @@ uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size)return register_read_memory (opaque, addr, size, false); } +void register_init_block32 (DeviceState *owner,const RegisterAccessInfo *rae, +int num,RegisterInfo *ri,uint32_t *data, +MemoryRegion *container,const MemoryRegionOps *ops, +bool debug_enabled,uint64_t memory_size) +{ +const char *device_prefix =object_get_typename (OBJECT (owner)); +RegisterInfoArray *r_array =g_malloc (sizeof (RegisterInfoArray)); +int i; + + r_array->num_elements =0 ; + r_array->r =g_malloc_n (num,sizeof (RegisterInfo *)); + +for (i =0 ; i < num; i++) { +int index = rae[i].decode.addr /4 ; +RegisterInfo *r = &ri[index]; + + *r = (RegisterInfo ) { + .data = &data[index], + .data_size =sizeof (uint32_t ), + .access = &rae[i], + .debug = debug_enabled, + .prefix = device_prefix, + .opaque = owner, + }; +register_init (r); + + r_array->r[r_array->num_elements] = r; + r_array->num_elements++; + } + + r_array->num_elements--; + +memory_region_init_io (&r_array->mem,OBJECT (owner), ops, r_array, + device_prefix, memory_size); +memory_region_add_subregion (container, + r_array->r[0 ]->access->decode.addr, + &r_array->mem); +} +static const TypeInfo register_info = { .name =TYPE_REGISTER , .parent =TYPE_DEVICE ,
include/hw/register.h +20 -0
@@ -176,6 +176,26 @@ void register_write_memory_le(void *opaque, hwaddr addr, uint64_t value,uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size);uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size); +/** +* Init a block of consecutive 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 +* @data: Array to use for register data +* @container: Memory region to contain new registers +* @ops: Memory region ops to access registers. +* @debug enabled: turn on/off verbose debug information +*/ + +void register_init_block32 (DeviceState *owner,const RegisterAccessInfo *rae, +int num,RegisterInfo *ri,uint32_t *data, +MemoryRegion *container,const MemoryRegionOps *ops, +bool debug_enabled,uint64_t memory_size); +/* Define constants for a 32 bit register */ #define REG32 (reg, addr) \ enum { A_ ## reg = (addr) }; \
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH v5 07/15] register: Add block initialise helper
Quoted 26 lines · Alistair Francis
From: Peter Crosthwaite <[email protected]>
Add a helper that will scan a static RegisterAccessInfo Array and populate a container MemoryRegion with registers as defined.
Signed-off-by Peter Crosthwaite <[email protected]>Signed-off-by Alistair Francis <[email protected]>---
V3:
- Fix typo
V2:
- Usememory_region_add_subregion_no_print()hw/core/register.c | 39 +++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 20 ++++++++++++++++++++ 2 files changed, 59 insertions(+)hw/core/register.c +1
-0@@ -228,6 +228,45 @@ uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size)return register_read_memory (opaque, addr, size, false); } +void register_init_block32(DeviceState *owner, const RegisterAccessInfo *rae,
hw/core/register.c uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size) · line 241
Quoted 10 lines
hw/core/register.c +10
-0+ int num, RegisterInfo *ri, uint32_t *data, + MemoryRegion *container, const MemoryRegionOps *ops, + bool debug_enabled, uint64_t memory_size) +{ + const char *device_prefix = object_get_typename(OBJECT(owner)); +RegisterInfoArray *r_array =g_malloc (sizeof (RegisterInfoArray)); +int i; + + r_array->num_elements =0 ; + r_array->r =g_malloc_n (num,sizeof (RegisterInfo *));
Don't re-invent the wheel if you don't have to. glib already has an array type which means your boilerplate becomes:
GArray *r_array = g_array_sized_new(true, true, sizeof(RegisterInfo *),num);
for (...) {
...create r...
g_array_append_val(r_array, r);
}Quoted 10 lines
hw/core/register.c +10
-0+ +for (i =0 ; i < num; i++) { +int index = rae[i].decode.addr /4 ; +RegisterInfo *r = &ri[index]; + + *r = (RegisterInfo ) { + .data = &data[index], + .data_size =sizeof (uint32_t ), + .access = &rae[i], + .debug = debug_enabled,
hw/core/register.c uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size) · line 261
Quoted 10 lines
hw/core/register.c +10
-0+ .prefix = device_prefix, + .opaque = owner, + }; +register_init (r); + + r_array->r[r_array->num_elements] = r; + r_array->num_elements++; + } + + r_array->num_elements--;
That's just plain confusing.
Quoted 32 lines
hw/core/register.c +8
-0+ +memory_region_init_io (&r_array->mem,OBJECT (owner), ops, r_array, + device_prefix, memory_size); +memory_region_add_subregion (container, + r_array->r[0 ]->access->decode.addr, + &r_array->mem); +} +static const TypeInfo register_info = { .name =TYPE_REGISTER , .parent =TYPE_DEVICE ,include/hw/register.h +13
-0@@ -176,6 +176,26 @@ void register_write_memory_le(void *opaque, hwaddr addr, uint64_t value,uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size);uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size); +/** +* Init a block of consecutive 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 +* @data: Array to use for register data +* @container: Memory region to contain new registers +* @ops: Memory region ops to access registers. +* @debug enabled: turn on/off verbose debug information
include/hw/register.h void register_write_memory_le(void *opaque, hwaddr addr, uint64_t value, · line 201
Quoted 10 lines
include/hw/register.h +7
-0+*/ + +void register_init_block32 (DeviceState *owner,const RegisterAccessInfo *rae, +int num,RegisterInfo *ri,uint32_t *data, +MemoryRegion *container,const MemoryRegionOps *ops, +bool debug_enabled,uint64_t memory_size); +/* Define constants for a 32 bit register */ #define REG32 (reg, addr) \ enum { A_ ## reg = (addr) }; \
--
Alex Bennée
Re: [Qemu-devel] [PATCH v5 07/15] register: Add block initialise helper
Quoted 43 lines · Alex Bennée
Quoted 26 lines · Alistair Francis
From: Peter Crosthwaite <[email protected]>
Add a helper that will scan a static RegisterAccessInfo Array and populate a container MemoryRegion with registers as defined.
Signed-off-by Peter Crosthwaite <[email protected]>Signed-off-by Alistair Francis <[email protected]>---
V3:
- Fix typo
V2:
- Usememory_region_add_subregion_no_print()hw/core/register.c | 39 +++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 20 ++++++++++++++++++++ 2 files changed, 59 insertions(+)hw/core/register.c +1
-0@@ -228,6 +228,45 @@ uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size)return register_read_memory (opaque, addr, size, false); } +void register_init_block32(DeviceState *owner, const RegisterAccessInfo *rae,hw/core/register.c uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size) · line 241
Quoted 10 lines
hw/core/register.c +10
-0+ int num, RegisterInfo *ri, uint32_t *data, + MemoryRegion *container, const MemoryRegionOps *ops, + bool debug_enabled, uint64_t memory_size) +{ + const char *device_prefix = object_get_typename(OBJECT(owner)); +RegisterInfoArray *r_array =g_malloc (sizeof (RegisterInfoArray)); +int i; + + r_array->num_elements =0 ; + r_array->r =g_malloc_n (num,sizeof (RegisterInfo *));Don't re-invent the wheel if you don't have to. glib already has an array type which means your boilerplate becomes:
Quoted 5 lines
GArray *r_array = g_array_sized_new(true, true, sizeof(RegisterInfo *),num);
for (...) {
...create r...
g_array_append_val(r_array, r);
}The problem with that is where do I put the memory region for the array?
Quoted 16 lines
Quoted 16 lines
hw/core/register.c +14
-0+ +for (i =0 ; i < num; i++) { +int index = rae[i].decode.addr /4 ; +RegisterInfo *r = &ri[index]; + + *r = (RegisterInfo ) { + .data = &data[index], + .data_size =sizeof (uint32_t ), + .access = &rae[i], + .debug = debug_enabled, + .prefix = device_prefix, + .opaque = owner, + }; +register_init (r);
Quoted 8 lines
hw/core/register.c uint64_t register_read_memory_le(void *opaque, hwaddr addr, unsigned size) · line 261
Quoted 6 lines
hw/core/register.c +6
-0+ + r_array->r[r_array->num_elements] = r; + r_array->num_elements++; + } + + r_array->num_elements--;That's just plain confusing.
Yeah, I'll fix this.
Thanks,
Alistair
Quoted 48 lines
Quoted 33 lines
hw/core/register.c +8
-0+ +memory_region_init_io (&r_array->mem,OBJECT (owner), ops, r_array, + device_prefix, memory_size); +memory_region_add_subregion (container, + r_array->r[0 ]->access->decode.addr, + &r_array->mem); +} +static const TypeInfo register_info = { .name =TYPE_REGISTER , .parent =TYPE_DEVICE ,include/hw/register.h +13
-0@@ -176,6 +176,26 @@ void register_write_memory_le(void *opaque, hwaddr addr, uint64_t value,uint64_t register_read_memory_be (void *opaque,hwaddr addr,unsigned size);uint64_t register_read_memory_le (void *opaque,hwaddr addr,unsigned size); +/** +* Init a block of consecutive 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 +* @data: Array to use for register data +* @container: Memory region to contain new registers +* @ops: Memory region ops to access registers. +* @debug enabled: turn on/off verbose debug information include/hw/register.h void register_write_memory_le(void *opaque, hwaddr addr, uint64_t value, · line 201
Quoted 10 lines
include/hw/register.h +7
-0+*/ + +void register_init_block32 (DeviceState *owner,const RegisterAccessInfo *rae, +int num,RegisterInfo *ri,uint32_t *data, +MemoryRegion *container,const MemoryRegionOps *ops, +bool debug_enabled,uint64_t memory_size); +/* Define constants for a 32 bit register */ #define REG32 (reg, addr) \ enum { A_ ## reg = (addr) }; \--
Alex Bennée
PATCH v5 8/15 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.
---
V5:
- Corrections to the device model logic
default-configs/arm-softmmu.mak | 1 + hw/dma/Makefile.objs | 1 + hw/dma/xlnx-zynq-devcfg.c | 394 ++++++++++++++++++++++++++++++++++++++ include/hw/dma/xlnx-zynq-devcfg.h | 62 ++++++ 4 files changed, 458 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 +394 -0
394 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.5.0
PATCH v5 9/15 xilinx_zynq: Connect devcfg to the Zynq machine model
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
---
V4:
- Small corrections to the device model logic
hw/arm/xilinx_zynq.c | 8 ++++++++ 1 file changed, 8 insertions(+)
hw/arm/xilinx_zynq.c +8 -0
@@ -290,6 +290,14 @@ 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" ); +object_property_add_child (qdev_get_machine (),"xlnx-devcfg" ,OBJECT (dev), +NULL ); +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.5.0
PATCH v5 10/15 qdev: Define qdev_get_gpio_out
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
An API similar to the existing qdev_get_gpio_in() except gets outputs. Useful for:
1: Implementing lightweight devices that don't want to keep pointers to their own GPIOs. They can get their GPIO pointers at runtime from QOM using this API.
2: testing or debugging code which may wish to override the hardware generated value of of a GPIO with a user specified value (E.G. interrupt injection).
---
hw/core/qdev.c | 12 ++++++++++++ include/hw/qdev-core.h | 2 ++ 2 files changed, 14 insertions(+)
hw/core/qdev.c +12 -0
@@ -489,6 +489,18 @@ qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)return qdev_get_gpio_in_named (dev,NULL , n); } +qemu_irq qdev_get_gpio_out_named (DeviceState *dev,const char *name,int n) +{ +char *propname =g_strdup_printf ("%s[%d]" , + name ? name :"unnamed-gpio-out" , n); +return (qemu_irq )object_property_get_link (OBJECT (dev), propname,NULL ); +} + +qemu_irq qdev_get_gpio_out (DeviceState *dev,int n) +{ +return qdev_get_gpio_out_named (dev,NULL , n); +} +void qdev_connect_gpio_out_named (DeviceState *dev,const char *name,int n,qemu_irq pin) {
include/hw/qdev-core.h +2 -0
@@ -286,6 +286,8 @@ bool qdev_machine_modified(void);qemu_irq qdev_get_gpio_in (DeviceState *dev,int n);qemu_irq qdev_get_gpio_in_named (DeviceState *dev,const char *name,int n); +qemu_irq qdev_get_gpio_out (DeviceState *dev,int n); +qemu_irq qdev_get_gpio_out_named (DeviceState *dev,const char *name,int n);void qdev_connect_gpio_out (DeviceState *dev,int n,qemu_irq pin); void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
Signature · 1 line
2.5.0
PATCH v5 11/15 qdev: Add qdev_pass_all_gpios API
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
For passing all GPIOs of all names from a contained device to a container.
---
hw/core/qdev.c | 9 +++++++++ include/hw/qdev-core.h | 1 + 2 files changed, 10 insertions(+)
hw/core/qdev.c +9 -0
@@ -589,6 +589,15 @@ void qdev_pass_gpios(DeviceState *dev, DeviceState *container,QLIST_INSERT_HEAD (&container->gpios, ngl, node); } +void qdev_pass_all_gpios (DeviceState *dev,DeviceState *container) +{ +NamedGPIOList *ngl; + + QLIST_FOREACH(ngl, &dev->gpios, node) { +qdev_pass_gpios (dev, container, ngl->name); + } +} +BusState *qdev_get_child_bus (DeviceState *dev,const char *name) {BusState *bus;
include/hw/qdev-core.h +1 -0
@@ -311,6 +311,7 @@ void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,void qdev_pass_gpios (DeviceState *dev,DeviceState *container,const char *name); +void qdev_pass_all_gpios (DeviceState *dev,DeviceState *container);BusState *qdev_get_parent_bus (DeviceState *dev);
Signature · 1 line
2.5.0
PATCH v5 12/15 irq: Add opaque setter routine
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
Add a routine to set or override the opaque data of an IRQ.
Qdev currently always initialises IRQ opaque as the device itself. This allows you to override to a custom opaque in the case where there is extra or different data needed.
---
hw/core/irq.c | 5 +++++ include/hw/irq.h | 2 ++ 2 files changed, 7 insertions(+)
hw/core/irq.c +5 -0
@@ -77,6 +77,11 @@ qemu_irq qemu_allocate_irq(qemu_irq_handler handler, void *opaque, int n)return irq; } +void qemu_irq_set_opaque (qemu_irq irq,void *opaque) +{ + irq->opaque = opaque; +} +void qemu_free_irqs (qemu_irq *s,int n) {int i;
include/hw/irq.h +2 -0
@@ -44,6 +44,8 @@ qemu_irq qemu_allocate_irq(qemu_irq_handler handler, void *opaque, int n);qemu_irq *qemu_extend_irqs (qemu_irq *old,int n_old,qemu_irq_handler handler,void *opaque,int n); +void qemu_irq_set_opaque (qemu_irq irq,void *opaque); +void qemu_free_irqs (qemu_irq *s,int n);void qemu_free_irq (qemu_irq irq);
Signature · 1 line
2.5.0
PATCH v5 13/15 register: Add GPIO API
In reply to Alistair FrancisAdd GPIO functionality to the register API. This allows association and automatic connection of GPIOs to bits in registers. GPIO inputs will attach to handlers that automatically set read-only bits in registers. GPIO outputs will be updated to reflect their field value when their respective registers are written (or reset). Supports active low GPIOs.
This is particularly effective for implementing system level controllers, where heterogenous collections of control signals are placed is a SoC specific peripheral then propagated all over the system.
[ EI Changes:
* register: Add a polarity field to GPIO connections Makes it possible to directly connect active low signals to generic interrupt pins.
]
---
V5
- Remove RegisterAccessError struct
hw/core/register.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/register.h | 26 ++++++++++++++ 2 files changed, 122 insertions(+)
hw/core/register.c +96 -0
@@ -100,6 +100,7 @@ void register_write(RegisterInfo *reg, uint64_t val, uint64_t we) }register_write_val (reg, new_val); +register_refresh_gpios (reg, old_val);if (ac->post_write) { ac->post_write (reg, new_val);
@@ -139,23 +140,117 @@ uint64_t register_read(RegisterInfo *reg)void register_reset (RegisterInfo *reg) {g_assert (reg); +uint64_t old_val;if (!reg->data || !reg->access) {return ; } + old_val =register_read_val (reg); +register_write_val (reg, reg->access->reset); +register_refresh_gpios (reg, old_val); +} + +void register_refresh_gpios (RegisterInfo *reg,uint64_t old_value) +{ +const RegisterAccessInfo *ac; +const RegisterGPIOMapping *gpio; + + ac = reg->access; +for (gpio = ac->gpios; gpio && gpio->name; gpio++) { +int i; + +if (gpio->input) { +continue ; + } + +for (i =0 ; i < gpio->num; ++i) { +uint64_t gpio_value, gpio_value_old; + +qemu_irq gpo =qdev_get_gpio_out_named (DEVICE (reg), gpio->name, i); + gpio_value_old =extract64 (old_value, + gpio->bit_pos + i * gpio->width, + gpio->width) ^ gpio->polarity; + gpio_value =extract64 (register_read_val (reg), + gpio->bit_pos + i * gpio->width, + gpio->width) ^ gpio->polarity; +if (!(gpio_value_old ^ gpio_value)) { +continue ; + } +if (reg->debug && gpo) { +qemu_log ("refreshing gpio out %s to %" PRIx64"\n" , + gpio->name, gpio_value); + } +qemu_set_irq (gpo, gpio_value); + } + } +} + +typedef struct DeviceNamedGPIOHandlerOpaque { +DeviceState *dev; +const char *name; +}DeviceNamedGPIOHandlerOpaque ; + +static void register_gpio_handler (void *opaque,int n,int level) +{ +DeviceNamedGPIOHandlerOpaque *gho = opaque; +RegisterInfo *reg =REGISTER (gho->dev); + +const RegisterAccessInfo *ac; +const RegisterGPIOMapping *gpio; + + ac = reg->access; +for (gpio = ac->gpios; gpio && gpio->name; gpio++) { +if (gpio->input && !strcmp (gho->name, gpio->name)) { +register_write_val (reg,deposit64 (register_read_val (reg), + gpio->bit_pos + n * gpio->width, + gpio->width, + level ^ gpio->polarity)); +return ; + } + } + +abort (); }void register_init (RegisterInfo *reg) {assert (reg); +const RegisterAccessInfo *ac; +const RegisterGPIOMapping *gpio;if (!reg->data || !reg->access) {return ; }object_initialize ((void *)reg,sizeof (*reg),TYPE_REGISTER ); + + ac = reg->access; +for (gpio = ac->gpios; gpio && gpio->name; gpio++) { +if (!gpio->num) { + ((RegisterGPIOMapping *)gpio)->num =1 ; + } +if (!gpio->width) { + ((RegisterGPIOMapping *)gpio)->width =1 ; + } +if (gpio->input) { +DeviceNamedGPIOHandlerOpaque gho = { + .name = gpio->name, + .dev =DEVICE (reg), + }; +qemu_irq irq; + +qdev_init_gpio_in_named (DEVICE (reg), register_gpio_handler, + gpio->name, gpio->num); + irq =qdev_get_gpio_in_named (DEVICE (reg), gpio->name, gpio->num); +qemu_irq_set_opaque (irq,g_memdup (&gho,sizeof (gho))); + }else { +qemu_irq *gpos =g_new0 (qemu_irq, gpio->num); + +qdev_init_gpio_out_named (DEVICE (reg), gpos, gpio->name, gpio->num); + } + } } static inline void register_write_memory(void *opaque, hwaddr addr,
@@ -253,6 +348,7 @@ void register_init_block32(DeviceState *owner, const RegisterAccessInfo *rae, .opaque = owner, };register_init (r); +qdev_pass_all_gpios (DEVICE (r), owner); r_array->r[r_array->num_elements] = r; r_array->num_elements++;
include/hw/register.h +26 -0
@@ -13,11 +13,24 @@#include "hw/qdev-core.h" #include "exec/memory.h" +#include "hw/irq.h" typedef struct RegisterInfo RegisterInfo ;typedef struct RegisterAccessInfo RegisterAccessInfo ;typedef struct RegisterInfoArray RegisterInfoArray ; +#define REG_GPIO_POL_HIGH 0 +#define REG_GPIO_POL_LOW 1 + +typedef struct RegisterGPIOMapping { +const char *name; +uint8_t bit_pos; +bool input; +bool polarity; +uint8_t num; +uint8_t width; +}RegisterGPIOMapping ; +/** * Access description for a register that is part of guest accessible device * state.
@@ -59,6 +72,8 @@ struct RegisterAccessInfo {uint64_t (*post_read)(RegisterInfo *reg,uint64_t val); +const RegisterGPIOMapping *gpios; +struct {hwaddr addr; } decode;
@@ -151,6 +166,17 @@ void register_reset(RegisterInfo *reg);void register_init (RegisterInfo *reg);/** +* Refresh GPIO outputs based on diff between old value register current value. +* GPIOs are refreshed for fields where the old value differs to the current +* value. +* +* @reg: Register to refresh GPIO outs +* @old_value: previous value of register +*/ + +void register_refresh_gpios (RegisterInfo *reg,uint64_t old_value); + +/** * Memory API MMIO write handler that will write to a Register API register. * _be for big endian variant and _le for little endian. * @opaque: RegisterInfo to write to
Signature · 1 line
2.5.0
PATCH v5 14/15 misc: Introduce ZynqMP IOU SLCR
In reply to Alistair FrancisFrom: Peter Crosthwaite <[email protected]>
IOU = I/O Unit
SLCR = System Level Control Registers
This IP is a misc collections of control registers that switch various properties of system IPs. Currently the only thing implemented is the SD_SLOTTYPE control (implemented as a GPIO output).
---
hw/misc/Makefile.objs | 1 + hw/misc/xlnx-zynqmp-iou-slcr.c | 115 +++++++++++++++++++++++++++++++++ include/hw/misc/xlnx-zynqmp-iou-slcr.h | 47 ++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 hw/misc/xlnx-zynqmp-iou-slcr.c create mode 100644 include/hw/misc/xlnx-zynqmp-iou-slcr.h
hw/misc/Makefile.objs +1 -0
@@ -41,6 +41,7 @@ obj-$(CONFIG_RASPI) += bcm2835_property.o obj-$(CONFIG_SLAVIO) += slavio_misc.o obj-$(CONFIG_ZYNQ) += zynq_slcr.o obj-$(CONFIG_ZYNQ) += zynq-xadc.o +obj-$(CONFIG_ZYNQ) += xlnx-zynqmp-iou-slcr.o obj-$(CONFIG_STM32F2XX_SYSCFG) += stm32f2xx_syscfg.o obj-$(CONFIG_PVPANIC) += pvpanic.o
hw/misc/xlnx-zynqmp-iou-slcr.c new file +115 -0
115 lines not on this page · raw message
include/hw/misc/xlnx-zynqmp-iou-slcr.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.5.0
Quoted 9 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.
V5:
- Only create a single memory region instead of a memory region for each register
- General tidyups based on Alex's comments
OK I'm done for now, not much point going over the actual device definitions while still nailing the API down. I think we are almost there though, hopefully one more iteration for the core API.
--
Alex Bennée
Quoted 8 lines · Alex Bennée
Quoted 5 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.
Quoted 8 lines
Quoted 4 lines
V5:
- Only create a single memory region instead of a memory region for each register
- General tidyups based on Alex's commentsOK I'm done for now, not much point going over the actual device definitions while still nailing the API down. I think we are almost there though, hopefully one more iteration for the core API.
Thanks for the review. Besides the g_array_sized_new() I have made all of the changes.
Thanks,
Alistair
Quoted 4 lines
--
Alex Bennée