Received: from eggs.gnu.org ([2001:4830:134:3::10]:51020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZD4oN-0006np-6u for qemu-devel@nongnu.org; Thu, 09 Jul 2015 01:55:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZD4oJ-00029z-UN for qemu-devel@nongnu.org; Thu, 09 Jul 2015 01:55:55 -0400 Received: from zose-mta03.web4all.fr ([185.49.20.44]:54987) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZD4oJ-00028X-LC for qemu-devel@nongnu.org; Thu, 09 Jul 2015 01:55:51 -0400 Message-ID: <559E0CE3.1050706@tribudubois.net> Date: Thu, 09 Jul 2015 07:55:47 +0200 From: Jean-Christophe DUBOIS MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v11 02/19] i.MX: Move serial initialization to init/realize of DeviceClass. List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Crosthwaite Cc: "qemu-devel@nongnu.org Developers" Le 08/07/2015 22:49, Peter Crosthwaite a =C3=A9crit : > On Wed, Jul 8, 2015 at 11:42 AM, Jean-Christophe Dubois > wrote: >> Move constructor to DeviceClass methods >> * imx_serial_init >> * imx_serial_realize >> >> imx32_serial_properties is renamed to imx_serial_properties. >> >> Rework of Qdev construction helper function. >> >> Signed-off-by: Jean-Christophe Dubois >> --- >> >> Changes since v1: >> * not present on v1 >> >> Changes since v2: >> * not present on v2 >> >> Changes since v3: >> * not present on v3 >> >> Changes since v4: >> * not present on v4 >> >> Changes since v5: >> * not present on v5 >> >> Changes since v6: >> * not present on v6 >> >> Changes since v7: >> * not present on v7 >> >> Changes since v8: >> * Remove Qdev construction helper >> >> Changes since v9: >> * Qdev construction helper is reintegrated and moved to a header = file >> as an inline function. >> >> Changes since v10: >> * Qdev construction helper is put back in the main file. >> * Qdev construction helper is reworked >> * We don't use qemu_char_get_next_serial() anymore but the charde= v >> property instead. >> * Fix code to work with an unitialized (null) chardev property >> >> hw/char/imx_serial.c | 98 +++++++++++++++++++++++++++++-------------= ---------- >> 1 file changed, 54 insertions(+), 44 deletions(-) >> >> diff --git a/hw/char/imx_serial.c b/hw/char/imx_serial.c >> index 1dcb325..950c740 100644 >> --- a/hw/char/imx_serial.c >> +++ b/hw/char/imx_serial.c >> @@ -38,13 +38,13 @@ do { printf("imx_serial: " fmt , ##args); } while = (0) >> //#define DEBUG_IMPLEMENTATION 1 >> #ifdef DEBUG_IMPLEMENTATION >> # define IPRINTF(fmt, args...) \ >> - do { fprintf(stderr, "imx_serial: " fmt, ##args); } while (0) >> + do { fprintf(stderr, "%s: " fmt, TYPE_IMX_SERIAL, ##args); } whi= le (0) >> #else >> # define IPRINTF(fmt, args...) do {} while (0) >> #endif >> >> static const VMStateDescription vmstate_imx_serial =3D { >> - .name =3D "imx-serial", >> + .name =3D TYPE_IMX_SERIAL, >> .version_id =3D 1, >> .minimum_version_id =3D 1, >> .fields =3D (VMStateField[]) { >> @@ -125,7 +125,9 @@ static uint64_t imx_serial_read(void *opaque, hwad= dr offset, >> s->usr2 &=3D ~USR2_RDR; >> s->uts1 |=3D UTS1_RXEMPTY; >> imx_update(s); >> - qemu_chr_accept_input(s->chr); >> + if (s->chr) { >> + qemu_chr_accept_input(s->chr); >> + } >> } >> return c; >> >> @@ -212,7 +214,9 @@ static void imx_serial_write(void *opaque, hwaddr = offset, >> } >> if (value & UCR2_RXEN) { >> if (!(s->ucr2 & UCR2_RXEN)) { >> - qemu_chr_accept_input(s->chr); >> + if (s->chr) { >> + qemu_chr_accept_input(s->chr); >> + } > Looking around, imx serial is trying to be NULL safe except for these > two cases. This and the hunk above is a full-on bugfix that should > probably be split off and go to 2.4. How do you provide a patch specifically for 2.4 (beside including it in=20 my series)? > >> } >> } >> s->ucr2 =3D value & 0xffff; >> @@ -299,23 +303,16 @@ static void imx_event(void *opaque, int event) >> } >> } >> >> - > Out of scope style change. > >> static const struct MemoryRegionOps imx_serial_ops =3D { >> .read =3D imx_serial_read, >> .write =3D imx_serial_write, >> .endianness =3D DEVICE_NATIVE_ENDIAN, >> }; >> >> -static int imx_serial_init(SysBusDevice *dev) >> +static void imx_serial_realize(DeviceState *dev, Error **errp) >> { >> IMXSerialState *s =3D IMX_SERIAL(dev); >> >> - >> - memory_region_init_io(&s->iomem, OBJECT(s), &imx_serial_ops, s, >> - "imx-serial", 0x1000); >> - sysbus_init_mmio(dev, &s->iomem); >> - sysbus_init_irq(dev, &s->irq); >> - >> if (s->chr) { >> qemu_chr_add_handlers(s->chr, imx_can_receive, imx_receive, >> imx_event, s); >> @@ -323,45 +320,58 @@ static int imx_serial_init(SysBusDevice *dev) >> DPRINTF("No char dev for uart at 0x%lx\n", >> (unsigned long)s->iomem.ram_addr); >> } >> +} >> + >> +static void imx_serial_init(Object *obj) >> +{ >> + SysBusDevice *sbd =3D SYS_BUS_DEVICE(obj); >> + IMXSerialState *s =3D IMX_SERIAL(obj); >> >> - return 0; >> + memory_region_init_io(&s->iomem, obj, &imx_serial_ops, s, >> + TYPE_IMX_SERIAL, 0x1000); >> + sysbus_init_mmio(sbd, &s->iomem); >> + sysbus_init_irq(sbd, &s->irq); >> } >> >> void imx_serial_create(int uart, const hwaddr addr, qemu_irq irq) >> { >> DeviceState *dev; >> - SysBusDevice *bus; >> - CharDriverState *chr; >> - const char chr_name[] =3D "serial"; >> - char label[ARRAY_SIZE(chr_name) + 1]; >> >> dev =3D qdev_create(NULL, TYPE_IMX_SERIAL); >> >> - if (uart >=3D MAX_SERIAL_PORTS) { >> - hw_error("Cannot assign uart %d: QEMU supports only %d ports\= n", >> - uart, MAX_SERIAL_PORTS); >> - } >> - chr =3D serial_hds[uart]; >> - if (!chr) { >> - snprintf(label, ARRAY_SIZE(label), "%s%d", chr_name, uart); >> - chr =3D qemu_chr_new(label, "null", NULL); >> - if (!(chr)) { >> - hw_error("Can't assign serial port to imx-uart%d.\n", uar= t); >> + if (dev) { > If dev is NULL I think you have big problems. If there is a valid > reason why this can be NULL in a non-error case then it should just > short return. It is obviously an error case (out of resource?) and therefore we should=20 not ignore it (as it was previously the case). So I test for it here. Do=20 you feel there should be another behavior (hw_error or something)? > > if (!dev) { > return; > } > >> + SysBusDevice *bus; >> + >> + if (uart < MAX_SERIAL_PORTS) { >> + CharDriverState *chr; >> + >> + chr =3D serial_hds[uart]; >> + >> + if (!chr) { >> + char label[20]; >> + snprintf(label, sizeof(label), "imx.uart%d", uart); >> + chr =3D qemu_chr_new(label, "null", NULL); >> + if (!(chr)) { >> + hw_error("Can't assign serial port to %s.\n", lab= el); >> + } >> + } >> + >> + qdev_prop_set_chr(dev, "chardev", chr); >> } >> - } >> >> - qdev_prop_set_chr(dev, "chardev", chr); >> - bus =3D SYS_BUS_DEVICE(dev); >> - qdev_init_nofail(dev); >> - if (addr !=3D (hwaddr)-1) { >> - sysbus_mmio_map(bus, 0, addr); >> - } >> - sysbus_connect_irq(bus, 0, irq); >> + bus =3D SYS_BUS_DEVICE(dev); >> >> -} >> + qdev_init_nofail(dev); >> + >> + if (addr !=3D (hwaddr)-1) { >> + sysbus_mmio_map(bus, 0, addr); >> + } >> >> + sysbus_connect_irq(bus, 0, irq); >> + } >> +} > So the change to _create looks correct, but it is still out of scope > of the patch which by commit message is an init/realize conversion. Is > there a reason the old _create implementation wont work with the new > init/realize and allow a split? > > Aside from the minor comments the code is good, but I would split this > to 3 patches. > > 1: chr NULL guards, and send that as a single for 2.4. > 2: realize and init conversion > 3: _create rewrite. OK, I'll do it even if I am spending quite some time on something that=20 is going to be wiped out in a following patch. > > Regards, > Peter > >> -static Property imx32_serial_properties[] =3D { >> +static Property imx_serial_properties[] =3D { >> DEFINE_PROP_CHR("chardev", IMXSerialState, chr), >> DEFINE_PROP_END_OF_LIST(), >> }; >> @@ -369,21 +379,21 @@ static Property imx32_serial_properties[] =3D { >> static void imx_serial_class_init(ObjectClass *klass, void *data) >> { >> DeviceClass *dc =3D DEVICE_CLASS(klass); >> - SysBusDeviceClass *k =3D SYS_BUS_DEVICE_CLASS(klass); >> >> - k->init =3D imx_serial_init; >> + dc->realize =3D imx_serial_realize; >> dc->vmsd =3D &vmstate_imx_serial; >> dc->reset =3D imx_serial_reset_at_boot; >> set_bit(DEVICE_CATEGORY_INPUT, dc->categories); >> dc->desc =3D "i.MX series UART"; >> - dc->props =3D imx32_serial_properties; >> + dc->props =3D imx_serial_properties; >> } >> >> static const TypeInfo imx_serial_info =3D { >> - .name =3D TYPE_IMX_SERIAL, >> - .parent =3D TYPE_SYS_BUS_DEVICE, >> - .instance_size =3D sizeof(IMXSerialState), >> - .class_init =3D imx_serial_class_init, >> + .name =3D TYPE_IMX_SERIAL, >> + .parent =3D TYPE_SYS_BUS_DEVICE, >> + .instance_size =3D sizeof(IMXSerialState), >> + .instance_init =3D imx_serial_init, >> + .class_init =3D imx_serial_class_init, >> }; >> >> static void imx_serial_register_types(void) >> -- >> 2.1.4 >> >>