Received: from eggs.gnu.org ([2001:4830:134:3::10]:50774) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZDeAc-0003sZ-Sx for qemu-devel@nongnu.org; Fri, 10 Jul 2015 15:41:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZDeAZ-0006wC-ID for qemu-devel@nongnu.org; Fri, 10 Jul 2015 15:41:14 -0400 Received: from zose-mta03.web4all.fr ([185.49.20.44]:45293) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZDeAZ-0006vw-9K for qemu-devel@nongnu.org; Fri, 10 Jul 2015 15:41:11 -0400 Message-ID: <55A01FD2.1000706@tribudubois.net> Date: Fri, 10 Jul 2015 21:41:06 +0200 From: Jean-Christophe DUBOIS MIME-Version: 1.0 References: <559E0CE3.1050706@tribudubois.net> In-Reply-To: Content-Type: text/plain; charset=windows-1252; 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 09/07/2015 08:41, Peter Crosthwaite a =E9crit : > sorry accidental send, comments inline below. > > On Wed, Jul 8, 2015 at 11:36 PM, Peter Crosthwaite > wrote: >> On Wed, Jul 8, 2015 at 10:55 PM, Jean-Christophe DUBOIS >> wrote: >>> Le 08/07/2015 22:49, Peter Crosthwaite a =E9crit : >>>> 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 hea= der >>>>> 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 ch= ardev >>>>> property instead. >>>>> * Fix code to work with an unitialized (null) chardev propert= y >>>>> >>>>> 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); } whi= le (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); } = while >>>>> (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, h= waddr >>>>> 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, hwad= dr >>>>> 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 thes= e >>>> 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 my >>> series)? >>> > Split it off as a single and send it alone, --subject-prefix=3D"PATCH > for-2.4" argument to git send-email. > >>>>> } >>>>> } >>>>> 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_recei= ve, >>>>> 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 por= ts\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", = uart); >>>>> + 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 sho= uld not >>> ignore it (as it was previously the case). So I test for it here. Do = 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", >>>>> label); >>>>> + } >>>>> + } >>>>> + >>>>> + 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 th= is >>>> 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 tha= t is >>> going to be wiped out in a following patch. >>> > Ok but why? Does an unchanged _create work? Note existing bugs are ok, > you don't faciliate any of your new functionalities or fix existing > issues (e.g. becoming NULL safe or deal with missing error paths) you > just need to not cause a regression half way through the series. So if > the existing _create just works with your realize change (which AFAICT > it does) just leave it unchanged and nuke it later in the series. What > am I missing that requires the change to _create at all? It sure does work in the non error case. So I'll remove my "fix" and let it as is until it is nuked when I use=20 the i.MX31 SOC implementation. > > Regards, > Peter > >>>> 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 >>>>> >>>>> >>>