Message ID | 102a10b5ede9a4c8dc06d2f4f81c57c4e2a71a1c.1457470980.git.alistair.francis@xilinx.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Alistair Francis <alistair.francis@xilinx.com> writes: > From: Peter Crosthwaite <peter.crosthwaite@xilinx.com> > > Add a helper that will scan a static RegisterAccessInfo Array > and populate a container MemoryRegion with registers as defined. > > Signed-off-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com> > Signed-off-by: Alistair Francis <alistair.francis@xilinx.com> > --- > 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(+) > > diff --git a/hw/core/register.c b/hw/core/register.c > index 28f3776..5db8f62 100644 > --- a/hw/core/register.c > +++ b/hw/core/register.c > @@ -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 *)); 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); } > + > + 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--; That's just plain confusing. > + > + 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, > diff --git a/include/hw/register.h b/include/hw/register.h > index d732f55..00df7d5 100644 > --- a/include/hw/register.h > +++ b/include/hw/register.h > @@ -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) }; \ -- Alex Bennée
On Tue, Mar 22, 2016 at 10:11 AM, Alex Bennée <alex.bennee@linaro.org> wrote: > > Alistair Francis <alistair.francis@xilinx.com> writes: > >> From: Peter Crosthwaite <peter.crosthwaite@xilinx.com> >> >> Add a helper that will scan a static RegisterAccessInfo Array >> and populate a container MemoryRegion with registers as defined. >> >> Signed-off-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com> >> Signed-off-by: Alistair Francis <alistair.francis@xilinx.com> >> --- >> 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(+) >> >> diff --git a/hw/core/register.c b/hw/core/register.c >> index 28f3776..5db8f62 100644 >> --- a/hw/core/register.c >> +++ b/hw/core/register.c >> @@ -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 *)); > > 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); > } The problem with that is where do I put the memory region for the array? > > >> + >> + 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--; > > That's just plain confusing. Yeah, I'll fix this. Thanks, Alistair > >> + >> + 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, >> diff --git a/include/hw/register.h b/include/hw/register.h >> index d732f55..00df7d5 100644 >> --- a/include/hw/register.h >> +++ b/include/hw/register.h >> @@ -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) }; \ > > > -- > Alex Bennée >
diff --git a/hw/core/register.c b/hw/core/register.c index 28f3776..5db8f62 100644 --- a/hw/core/register.c +++ b/hw/core/register.c @@ -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, diff --git a/include/hw/register.h b/include/hw/register.h index d732f55..00df7d5 100644 --- a/include/hw/register.h +++ b/include/hw/register.h @@ -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) }; \