[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: When is SysBusDevice's num_mmios determined? and does SYS_BUS_DEVICE

From: ckim
Subject: RE: When is SysBusDevice's num_mmios determined? and does SYS_BUS_DEVICE macro allocate memory?
Date: Sun, 6 Jun 2021 00:09:20 +0900

Hi,Peter Maydell,

Thanks again for the explanations.
I see, so the device type smc91c111_state is registered at the beginning of 
main() and when  it(the struct smc91c111 data type) is registered the struct 
already contains the SysBusDevice  which again contains DeviceState which 
contains Object. So we can do just type casting between different sized 
embedded structs. I also looked into how smc91c111 is instantiated and realized 
and attached to the system bus when the board like arm versatilepb is 

I have a questions for my clarification.
In function sysbut_init_mmio, I see these two lines,
     assert(dev->num_mmio < QDEV_MAX_MMIO);
     n = dev->num_mmio++;
I gathered this QDEV_MAX_MMIO means (currently defined to 32): maximum number 
of instances a same device can be attached to the system bus. Is my 
understanding correct?

And I have an opinion about the comment in code for OBJECT_CHECK :
By the way, when I see the comment on OBJECT_CHECK, it says
 * @type: The C type to use for the return value.
 * @obj: A derivative of @type to cast.
 * @name: The QOM typename of @type
#define OBJECT_CHECK(type, obj, name) \
    ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
                                        __FILE__, __LINE__, __func__))

Many times, this OBJECT_CHECK is used like 

#define TYPE_SMC91C111 "smc91c111"
#define SMC91C111(obj) OBJECT_CHECK(smc91c111_state, (obj), TYPE_SMC91C111)

typedef struct {
    SysBusDevice parent_obj;

    NICState *nic;
    NICConf conf;
    uint8_t data[NUM_PACKETS][2048];
    uint8_t int_level;
    uint8_t int_mask;
    MemoryRegion mmio;
} smc91c111_state;

As you know, C program in effect implements a derived class by adding more data 
to the base struct.
For example in smc91c111.c, smc91c111_state is a derived class of SysBusDevice 
which is a derived class of DeviceState which is a derived class of Object.
And many times, the macro OBJECT_CHECK is used like in this code below.

static void smc91c111_reset(DeviceState *dev)
    smc91c111_state *s = SMC91C111(dev);

Here in macro OBJECT_CHECK is used to cast DeviceState to its derivative 
So in the comment above, shouldn't "@obj: A derivative of @type to cast". be 
modified to "@obj: A derivative or a base of @type to case"?
I understand this casting is possible from base to derivative, or from 
derivative to base.

Thanks and regards,
Chan Kim

> -----Original Message-----
> From: Peter Maydell <peter.maydell@linaro.org>
> Sent: Friday, June 4, 2021 6:03 PM
> To: Chan Kim <ckim@etri.re.kr>
> Cc: qemu-discuss <qemu-discuss@nongnu.org>
> Subject: Re: When is SysBusDevice's num_mmios determined? and does
> SYS_BUS_DEVICE macro allocate memory?
> On Fri, 4 Jun 2021 at 09:47, <ckim@etri.re.kr> wrote:
> > The sysbus_mmio_map function checks if the given mmio range number (here
> 0) is between 0 and num_mmios of the SyBusDevice.
> >
> > But when is the num_mmios value inside the struct SysBusDevice
> determined? I couldn’t follow it up.
> num_mmios starts at zero, and every time the device calls
> sysbus_init_mmio() it is incremented. The assertion in
> sysbus_mmio_map() is checking "did the caller ask for an MMIO region which
> the device didn't create?".
> > And SysBusDevice includes DeviceState, then does the s =
> > SYS_BUS_DEVICE(dev) statement allocates the SysBusDevice on memory?
> No. This is a typechecked cast of a pointer. It's equivalent to "s =
> (SysBusDevice *)dev;" except that it will assert if "dev" is not actually
> a pointer to an instance of SysBusDevice.
> thanks
> -- PMM

reply via email to

[Prev in Thread] Current Thread [Next in Thread]