On Tue, 15 Feb 2022 at 10:32, Damien Hedde <damien.he...@greensocs.com> wrote: > I'm wondering if there are rules or convention about what we put in the > instance_init() vs realize() for simple devices ? (For complex ones we > generally have no choice to put everything in realize()) > > For example we can declare irqs and mmios in instance_init() or > realize() if they do not depend on some property.
We don't, unfortunately, have a clear set of conventions for this. We really ideally ought to write some up, because the question keeps coming up. There are a few absolute rules: * things that can fail must be done in realize * things that depend on property values must be done in realize * things that affect the simulation must be done in realize * if you do something that needs a corresponding manual deinit step in instance_init then you must provide an instance_finalize even if the device would otherwise be "create once, lasts for entire simulation" as many of our devices are But in many cases actions can be done in either method, and we end up with devices being inconsistent and people wondering whether there's a reason for it. I vaguely think it would be good to get into the habit of writing all our devices to have the full lifecycle code including supporting init-realize-unrealize-finalize, but on the other hand that implies a bunch of code (unrealize) which is never executed or tested... I also suspect we have a bunch of buggy code in realize methods which isn't correctly undoing things it has done already in the error-exit-from-realize case. > This is not a big deal, but given how works the help message generation > in the monitor, there are difference if the device is user-creatable. > > If we leave irqs and mmios declaration in the instance_init(). They > appear in the help message. > > (qemu) device_add ibex-timer,help > > ibex-timer options: > > ibex-timer[0]=<child<memory-region>> > > sysbus-irq[0]=<link<irq>> > > timebase-freq=<uint32> - (default: 10000) > > If we delay the declaration in realize(), we only have the declared > qdev-properties (which is maybe more what we expect at this point): > > > (qemu) device_add ibex-timer,help > > ibex-timer options: > > timebase-freq=<uint32> - (default: 10000) This seems to me to be a problem with the help message generation. IRQ and MMIO properties shouldn't be being presented to the user whether we set them up in instance-init or realize: there is nothing the user can usefully do with them. thanks -- PMM