Received: from eggs.gnu.org ([208.118.235.92]:59282) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US4hY-0007U9-31 for qemu-devel@nongnu.org; Tue, 16 Apr 2013 08:09:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1US4hP-0003Aw-D0 for qemu-devel@nongnu.org; Tue, 16 Apr 2013 08:09:31 -0400 Received: from smtp1-g21.free.fr ([2a01:e0c:1:1599::10]:48050) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US4hO-0003AA-JB for qemu-devel@nongnu.org; Tue, 16 Apr 2013 08:09:23 -0400 Received: from mail.thom.fr.eu.org (unknown [IPv6:2a01:e35:2f1f:e7b0:215:17ff:fec0:7e1f]) by smtp1-g21.free.fr (Postfix) with ESMTP id B8C32940245 for ; Tue, 16 Apr 2013 14:09:15 +0200 (CEST) Received: from mail.thom.fr.eu.org (localhost [127.0.0.1]) by mail.thom.fr.eu.org (8.14.3/8.14.3/Debian-9.4) with ESMTP id r3GC9EoQ021621 for ; Tue, 16 Apr 2013 14:09:14 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_e02875ddf5d151efe2178bfcbc66d09e" Date: Tue, 16 Apr 2013 14:09:14 +0200 From: =?UTF-8?Q?Fran=C3=A7ois_Legal?= In-Reply-To: References: <3e0e268a897603d5ae067f6e01fad68e@thom.fr.eu.org> Message-ID: <0487933dd01712ab479f234975b8ea2c@thom.fr.eu.org> Subject: Re: [Qemu-devel] [PATCH] ARM Cortex A9 Global Timer List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org --=_e02875ddf5d151efe2178bfcbc66d09e Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable =20 Le 16-04-2013 11:50, Peter Maydell a =C3=A9crit :=20 > On 15 April 2013 16:41, Fran=C3=A7ois Legal wrote: >=20 >> I made up this patch to implement the Cortex A9 global timer in Qemu. My patch is based on the= Qemu branch maintained by Xilinx for the Zynq. >=20 > Hi Fran=C3=A7ois; thanks for this patch. Some comments on the code below. >=20 > Firstly, if you could send future versions of this patch in the > standard QEMU format that would be helpful: > * text/plain mail, not multipart with an HTML part > * commit message at the top describing the patch, with comments > below a '---' line > * Signed-off-by: line in the commit message itself > * please submit patches based on git.qemu.org's master branch, > not on other trees >=20 > http://wiki.qemu.org/Contribute/SubmitAPatch [1]has some other helpful > suggestions. This is correct. I made my best to follow the guidelines, but failed for some of them. My point here was that globaltimer was required = for my application based on the Xilinx tree, and the comments on that tree tend = to let me believe they were quite close to the official tree. Sorry about the format I did not pay attention to my mail reader. This is my very first post to th= is list. > Onto the code:=20 >=20 >> diff -urN qemu-master/hw/cpu/a9mpcore.c qemu-master.new/hw/cpu/a9mpcore.c --- qemu-master/hw/cpu/a9mpcore.c 2013-= 04-08 20:12:33.000000000 +0200 +++ qemu-master.new/hw/cpu/a9mpcore.c 2013-04-15 12:54:06.000000000 +0200 @@ -15,6 +15,7 @@ uint32_t num_cpu; MemoryRegion container; DeviceState *mptimer; + DeviceState *mpgtimer; DeviceState *wd= t; DeviceState *gic; DeviceState *scu; @@ -31,6 +32,7 @@ { A9MPPrivState *s = =3D FROM_SYSBUS(A9MPPrivState, dev); SysBusDevice *timerbusdev, *wdtbusdev, *gicbusdev, *scubusdev; + SysBusDevice *gtimerbusdev; int i; s->gic =3D qdev_create(NULL, "arm_gic"); @@ -50,6 +52,11 @@ qdev_init_nofail(s->scu)= ; scubusdev =3D SYS_BUS_DEVICE(s->scu); + s->mpgtimer =3D qdev_create(NULL, "arm_mp_globaltimer"); >=20 > I think a better name for the device would be "a9-globaltimer". > This fits our convention of preferring '-' rather than '_' > in new device names, and makes it clear that the global > timer is only used for the A9. (The private timers are used > also by the 11MPCore.) Alright. I'll fix this is the next version of patch + qdev_prop_set_uint32(s->mpgtimer, "num-cpu", s->num_cpu); + qdev_init_nofail(s->mpgtimer); + gtimerbusdev =3D SYS_BUS_DEVICE(s->mpgtimer > um_cpu); qdev_init_nofail(s->mptimer); @@ -68,8 +75,6 @@ * 0x0600-0x06ff -- private timers and watchdogs * 0x0700-0x0fff = -- nothing * 0x1000-0x1fff -- GIC Distributor - * - * We should implement th= e global timer but don't currently do so. */ memory_region_init(&s->contain= er, "a9mp-priv-container", 0x2000); memory_region_add_subregion(&s->container= , 0, @@ -80,6 +85,8 @@ /* Note that the A9 exposes only the "timer/watchdog for t= his core" * memory region, not the "timer/watchdog for core X" ones 11MPcore = has. */ + memory_region_add_subregion(&s->container, 0x200, + sysbus_mmio_get_region(gtimerbusdev, 0)); memory_region_add_subregion(&s->container, 0x600, sysbus_mmio_get_region(timerbusdev, 0)); memory_region_add_subregion(&s->container, 0x620, @@ -90,10 +97,13 @@ sysbus_init_mmio(dev, &s->container); /* Wire up the interrupt from each watchdog and timer. - * For each core the timer is PPI 29 and the watchdo= g PPI 30. + * For each core the global timer is PPI 27, the private + * timer i= s PPI 29 and the watchdog PPI 30. */ for (i =3D 0; i < s->num_cpu; i++) { int p= pibase =3D (s->num_irq - 32) + i * 32; + sysbus_connect_irq(gtimerbusdev, i, + qdev_get_gpio_in(s->gic, ppibase + 27)); sysbus_connect_irq(timerbusdev, = i, qdev_get_gpio_in(s->gic, ppibase + 29)); sysbus_connect_irq(wdtbusdev, i,= diff -urN qemu-master/hw/timer/arm_mpgtimer.c qemu-master.new/hw/timer/arm_mpg= timer.c --- qemu-master/hw/timer/arm_mpgtimer.c 1970-01-01 01:00:00.000000000 +01= 00 +++ qemu-master.new/hw/timer/arm_mpgtimer.c 2013-04-15 13:56:23.000000000 +02= 00=20 > > The file should also be renamed: 'hw/timer/a9gtimer.c' >=20 > Alright. I'll fix this is the next version of patch >=20 > @@ -0,0 +1,359 @@ +/* + * Global peripheral timer block Core and A9MP=20 This isn't used in 11MPCore. Alright. I'll fix this is the next version of the patch + int i; + for (i =3D 0; i < ARRAY_SIZE(s->gtimer); i++) { + gtimer_reset(&s->gtimer[i]); + } +} + +st= atic int arm_mpgtimer_init(SysBusDevice *dev) +{ + ARMMPGTimerState *s =3D FROM_SYSBUS(ARMMPGTimerState, dev); + int i; + if (s->num_cpu < 1 || s->n= um_cpu > MAX_CPUS) { + hw_error("%s: num-cpu must be between 1 and %dn", __func_= _, MAX_CPUS);=20 If you make this a DeviceClass realize method (see below) > br />Alright. I'll fix this is the next version of patch >=20 >>> + } + + /* We implement one timer block per CPU, and expose multiple MMIO regions: + * = * region 0 is "timer for this core" + * * region 1 is "timer for core 0" + = * * region 2 is "timer for core 1" + * and so on. >>=20 >> Why implement multiple memory regions? There is only one >> for the global timer. >=20 > Sorry. Copy/Paste... >=20 > + * The outgoing interrupt lines are + * * timer for core 0 + * * timer for core 1 + * and so on. + */ + memory_region_init_io(&s->iome= m, &arm_thisgtimer_ops, s, + "arm_mptimer_gtimer", 0x20); + sysbus_init_mmio= (dev, &s->iomem); + for (i =3D 0; i < s->num_cpu; i++) { + gTimerBlock *gtb =3D &s->gtimer[i]; + gtb->gtimer_counter =3D &s->gtimer_counter; imer_control =3D &s->gtimer_control; + gtb->timer =3D qemu_new_timer_ns(vm_clock, gtimerbl= ock_tick, gtb); + sysbus_init_irq(dev, >b->irq); + memory_region_init_io(>b->io= mem, >imerblock_ops, gtb, + "arm_mptimer_gtimerblock", 0x20); + sysbus_init_mmio(dev, >b->iomem); + } + + return 0; +} + +static const VMStateDescription vmstate_gtimerblock =3D { + .name =3D "arm_mptimer_gti= merblock", + .version_id =3D 2, + .minimum_version_id =3D 2,=20 Why vers> dding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%"> + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(control, gTimerBlock), + VMSTATE_UINT32(status, gTimerBlock), + VMSTATE_UINT64(c rBlock), + VMSTATE_INT64(tick, gTimerBlock), + VMSTATE_END_OF_LIST() + } +}; + +stat= ic const VMStateDescription vmstate_arm_mpgtimer =3D { + .name =3D "arm_mp_globaltimer", + .version_id =3D 2, + .minimum_version_id =3D 2, += .fields =3D (VMStateField[]) { + VMSTATE_STRUCT_VARRAY_UINT32(gtimer, ARMMPGTimerStat= e, num_cpu, + 1, vmstate_gtimerblock, gTimerBlock), + VMSTATE_END_OF_LIST() = + } +}; + +static Property arm_mpgtimer_properties[] =3D { + DEFINE_PROP_UINT32("= num-cpu", ARMMPGTimerState, num_cpu, 0), + DEFINE_PROP_END_OF_LIST() +}; + +static = void arm_mpgtimer_class_init(ObjectClass *klass, void *d > sbc->init =3D arm_mpgtimer_init;=20 >=20 > Can you use the DeviceClass realize method rather than > SysBusDeviceClass init, please? (see eg a9scu.c for an > example.) >=20 > Alright. I'll fix this is the next version of patch >=20 > + dc->vmsd =3D &vmstate_arm_mpgtimer; + dc->reset =3D arm_mpgtimer_reset; + dc->no_user = =3D 1; + dc->props =3D arm_mpgtimer_properties; +} + t TypeInfo arm_mpgtimer_info =3D { + .name =3D "arm_mp_globaltimer", + .parent =3D TYPE_SYS_BUS_DEVICE, + .ins= tance_size =3D sizeof(ARMMPGTimerState), + .class_init =3D arm_mpgtimer_class_init, = +}; + +static void arm_mpgtimer_register_types(void) +{ + type_register_static(&arm_mpgtimer_info);=20 > w/timer/arm_mptimer.c --- qemu-master/hw/timer/arm_mptimer.c 2013-04-08 20:12:33.000000000 +0200 ++= + qemu-master.new/hw/timer/arm_mptimer.c 2013-04-15 13:44:33.000000000 +020= 0 @@ -49,13 +49,19 @@ static inline int get_current_cpu(ARMMPTimerState *s) { = - CPUState *cpu_single_cpu =3D ENV_GET_CPU(cpu_single_env); + CPUState *cpu_single_cpu; - if (cpu_single_cpu->cpu_index >=3D s->num_cpu) { - hw_error("arm_mptimer: num-cpu pu is %d!n", - s->num_cpu, cpu_single_cpu->cpu_index); + if (cpu_single_env !=3D NULL) { + cpu_singl= e_cpu =3D ENV_GET_CPU(cpu_single_env); + + if (cpu_single_cpu->cpu_index >=3D s->nu= m_cpu) { + hw_error("arm_mptimer: num-cpu %d but this cpu is %d!n", + s->num_cpu, cpu_single_cpu->cpu_index); + } + return cpu_single_cpu->cpu_index; + } e= lse { + return 0; } - return cpu_single_cpu > ockquote>=20 >=20 > This is for the same reason I modified the get_current_cpu for global timer. >=20 > static inline void timerblock_update_irq(TimerBlock *tb) diff -urN qemu-master/hw/timer/Makefile.objs qemu-master.new/hw/timer/Makefile.objs= --- qemu-master/hw/timer/Makefile.objs 2013-04-08 20:12:33.000000000 +0200 ++= + qemu-master.new/hw/timer/Makefile.objs 2013-04-15 12:54:06.000000000 +020= 0 @@ -24,5 +24,5 @@ obj-$(CONFIG_SH4) +=3D sh_timer.o obj-$(CONFIG_TUSB6010) += =3D tusb6010.o -obj-$(CONFIG_ARM_MPTIMER) +=3D arm_mptimer.o +obj-$(CONFIG_ARM_MPTIMER) +=3D arm_mptimer.o arm_mpgtimer.o obj-$(CONFIG_MC146818RTC) +=3D mc146818rtc.o Signed-off-by: Fran=C3=A7ois= LEGAL =20 >=20 > thanks > -- PMM >=20 >=20 >=20 Links: ------ [1] http://wiki.qemu.org/Contribute/SubmitAPatch --=_e02875ddf5d151efe2178bfcbc66d09e Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=UTF-8

 

Le 16-04-2013 11:50, Peter Maydell a écrit :

On 15 April 2013 16:41, François Legal <francois.legal@thom.fr.eu.org> wrote:
I made up this patch to implement the= Cortex A9 global timer in Qemu. My patch is based on the Qemu branch maint= ained by Xilinx for the Zynq.
Hi François; thanks for this patch. Some comments on the code b=
elow.

Firstly, if you could send future versions of this patch in the
standard QEMU format that would be helpful:
 * text/plain mail, not multipart with an HTML part
 * commit message at the top describing the patch, with comments
   below a '---' line
 * Signed-off-by: line in the commit message itself
 * please submit patches based on git.qemu.org's master branch,
   not on other trees

http://wiki.qemu=
=2Eorg/Contribute/SubmitAPatchhas some other helpful
suggestions.


This is correct. I made my best to follow the guidelines, but fail= ed for some of them. My point here was that globaltimer was required for my= application based on the Xilinx tree, and the comments on that tree tend t= o let me believe they were quite close to the official tree.
Sorry abo= ut the format I did not pay attention to my mail reader. This is my very fi= rst post to this list.

Onto the code:
diff -urN qemu-master/hw/cpu/a9mpcore= =2Ec qemu-master.new/hw/cpu/a9mpcore.c --- qemu-master/hw/cpu/a9mpcore.c 20= 13-04-08 20:12:33.000000000 +0200 +++ qemu-master.new/hw/cpu/a9mpcore.c 201= 3-04-15 12:54:06.000000000 +0200 @@ -15,6 +15,7 @@ uint32_t num_cpu; Memory= Region container; DeviceState *mptimer; + DeviceState *mpgtimer; DeviceStat= e *wdt; DeviceState *gic; DeviceState *scu; @@ -31,6 +32,7 @@ { A9MPPrivSta= te *s =3D FROM_SYSBUS(A9MPPrivState, dev); SysBusDevice *timerbusdev, *wdtb= usdev, *gicbusdev, *scubusdev; + SysBusDevice *gtimerbusdev; int i; s->g= ic =3D qdev_create(NULL, "arm_gic"); @@ -50,6 +52,11 @@ qdev_init_nofail(s-= >scu); scubusdev =3D SYS_BUS_DEVICE(s->scu); + s->mpgtimer =3D qde= v_create(NULL, "arm_mp_globaltimer");
I think a better name for the device would be "a9-globaltimer".
This fits our convention of preferring '-' rather than '_'
in new device names, and makes it clear that the global
timer is only used for the A9. (The private timers are used
also by the 11MPCore.)


Alright. I'll fix this is the next version of patch

+ qdev_prop_set_uint32(s->mpgtimer= , "num-cpu", s->num_cpu); + qdev_init_nofail(s->mpgtimer); + gtimerbu= sdev =3D SYS_BUS_DEVICE(s->mpgtimer); + s->mptimer =3D qdev_create(NU= LL, "arm_mptimer"); qdev_prop_set_uint32(s->mptimer, "num-cpu", s->nu= m_cpu); qdev_init_nofail(s->mptimer); @@ -68,8 +75,6 @@ * 0x0600-0x06ff = -- private timers and watchdogs * 0x0700-0x0fff -- nothing * 0x1000-0x1fff = -- GIC Distributor - * - * We should implement the global timer but don't c= urrently do so. */ memory_region_init(&s->container, "a9mp-priv-cont= ainer", 0x2000); memory_region_add_subregion(&s->container, 0, @@ -8= 0,6 +85,8 @@ /* Note that the A9 exposes only the "timer/watchdog for this = core" * memory region, not the "timer/watchdog for core X" ones 11MPcore ha= s. */ + memory_region_add_subregion(&s->container, 0x200, + sysbus_m= mio_get_region(gtimerbusdev, 0)); memory_region_add_subregion(&s->co= ntainer, 0x600, sysbus_mmio_get_region(timerbusdev, 0)); memory_region_add_= subregion(&s->container, 0x620, @@ -90,10 +97,13 @@ sysbus_init_mmio= (dev, &s->container); /* Wire up the interrupt from each watchdog an= d timer. - * For each core the timer is PPI 29 and the watchdog PPI 30. + *= For each core the global timer is PPI 27, the private + * timer is PPI 29 = and the watchdog PPI 30. */ for (i =3D 0; i < s->num_cpu; i++) { int = ppibase =3D (s->num_irq - 32) + i * 32; + sysbus_connect_irq(gtimerbusde= v, i, + qdev_get_gpio_in(s->gic, ppibase + 27)); sysbus_connect_irq(time= rbusdev, i, qdev_get_gpio_in(s->gic, ppibase + 29)); sysbus_connect_irq(= wdtbusdev, i, diff -urN qemu-master/hw/timer/arm_mpgtimer.c qemu-master.new= /hw/timer/arm_mpgtimer.c --- qemu-master/hw/timer/arm_mpgtimer.c 1970-01-01= 01:00:00.000000000 +0100 +++ qemu-master.new/hw/timer/arm_mpgtimer.c 2013-= 04-15 13:56:23.000000000 +0200
The file should also be renamed: 'hw/timer/a9gtimer.c'


Alright. I'll fix this is the next version of patch

@@ -0,0 +1,359 @@ +/* + * Global peri= pheral timer block for ARM 11MPCore and A9MP
This isn't used in 11MPCore.


Alright. I'll fix this is the next version of the patch

+ * + * Written by François LE= GAL + * + * This program is free software; you can redistribute it and/or += * modify it under the terms of the GNU General Public License + * as publi= shed by the Free Software Foundation; either version + * 2 of the License, = or (at your option) any later version. + * + * This program is distributed = in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without e= ven the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR= PURPOSE. See the + * GNU General Public License for more details. + * + * = You should have received a copy of the GNU General Public License along + *= with this program; if not, see <http://www.gnu.org/licenses/>. + */ + +#include "hw/sysbus.h" +#in= clude "qemu/timer.h" + +/* This device implements the per-cpu private timer= and watchdog block + * which is used in both the ARM11MPCore and Cortex-A9= MP. + */
This comment looks like an incorrect cut-n-paste from the private
timer implementation.


Alright. I'll fix this is the next version of patch

+ +#define MAX_CPUS 4 + +/* State of = a single gtimer or block */ +typedef struct { + uint32_t control; + uint64_= t compare; + uint32_t inc; + uint32_t status; + int64_t tick; + + int64_t d= elta; + uint64_t *gtimer_counter; + uint32_t *gtimer_control; + + QEMUTimer= *timer; + MemoryRegion iomem; + qemu_irq irq; +} gTimerBlock;
This isn't right -- the global timer should have a single
QEMUTimer shared between all CPUs, not one per gTimerBlock.

You might just want to roll all the banked registers into
the top level device struct, so
 uint64_t comparator[MAX_CPUS];
and so on (the global timer is more of a single block
of logic with some banked registers, unlike the private
timers which really are separate blocks of logic per
core).


I do not really agree here (though I don't really know the interna= ls of QEMU regarding timers and multicore). The GT could generate separate = different interrupts at different rate on each of the core given their bank= ed Comparator register.

+ +typedef struct { + SysBusDevice bu= sdev; + uint32_t num_cpu; + uint64_t gtimer_counter; + uint32_t gtimer_cont= rol; + gTimerBlock gtimer[MAX_CPUS]; + MemoryRegion iomem; +} ARMMPGTimerSt= ate; + +static inline int get_current_cpu(ARMMPGTimerState *s) +{ + CPUStat= e *cpu_single_cpu; + + if (cpu_single_env !=3D NULL) { + cpu_single_cpu =3D= ENV_GET_CPU(cpu_single_env); + + if (cpu_single_cpu->cpu_index >=3D = s->num_cpu) { + hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n"= , + s->num_cpu, cpu_single_cpu->cpu_index); + } + return cpu_single_c= pu->cpu_index; + } else { + return 0; + } +}
This is more complicated than get_current_cpu() in arm_mptimer.c:
why does it need to be different? I don't think this function
should ever be called with cpu_single_env NULL.


It is maint to ! I actually found a strange behaviour while using = QEMU with GDB to debug so system code. While trying to display any address = within private timer or global timer range, I got QEMU crashing because whe= n attempting readouts using GDB (print 0xF8F00200 for instance) the cpu_sin= gle_env variable is NULL. I don't know if it's a problem on the Xilinx tree= or a more general one, but this code did the job.

+ +static inline void gtimerblock_upd= ate_irq(gTimerBlock *gtb) +{ + qemu_set_irq(gtb->irq, gtb->status); += } + +/* Return conversion factor from mpcore timer ticks to qemu timer tick= s. */ +static inline uint32_t gtimerblock_scale(gTimerBlock *gtb) +{ + retu= rn ((((*gtb->gtimer_control) >> 8) & 0xff) + 1) * 10; +} + +st= atic void gtimerblock_reload(gTimerBlock *gtb, int restart) +{ + if (restar= t) { + gtb->tick =3D qemu_get_clock_ns(vm_clock); + gtb->tick +=3D (i= nt64_t)(((gtb->compare - *gtb->gtimer_counter) + + gtb->delta) * g= timerblock_scale(gtb)); + } else { + gtb->tick +=3D (int64_t)(gtb->in= c * gtimerblock_scale(gtb)); + } + qemu_mod_timer(gtb->timer, gtb->ti= ck); +} + +static void gtimerblock_tick(void *opaque) +{ + gTimerBlock *gtb= =3D (gTimerBlock *)opaque; + *gtb->gtimer_counter =3D gtb->compare; = + if ((gtb->control | *gtb->gtimer_control) & 0x9) {
Please abstract out 'read the control register value as this
core sees it' into a function, rather than doing
"(gtb->control | *gtb->gtimer_control)" everywhere.


Alright. I'll fix this is the next version of patch

+ gtb->compare +=3D gtb->inc; += gtimerblock_reload(gtb, 0); + } + if (((gtb->control | *gtb->gtimer_= control) & 0x7) && + ((gtb->status & 1) =3D=3D 0)) { + g= tb->status =3D 1; + gtimerblock_update_irq(gtb); + } +} + +static uint64= _t gtimer_read(void *opaque, hwaddr addr, + unsigned size) +{ + gTimerBlock= *gtb =3D (gTimerBlock *)opaque; + int64_t val =3D 0; + switch (addr) { + c= ase 0: /* Counter LSB */ + if (*gtb->gtimer_control & 1) { + val =3D= gtb->tick - qemu_get_clock_ns(vm_clock); + val /=3D gtimerblock_scale(g= tb); + } + return val < 0 ? val : 0 & 0xFFFFFFFF; + case 4: /* Count= er MSB. */ + if (*gtb->gtimer_control & 1) { + val =3D gtb->tick = - qemu_get_clock_ns(vm_clock); + val /=3D gtimerblock_scale(gtb); + } + ret= urn val < 0 ? (val >> 32) : 0; + case 8: /* Control. */ + return (= gtb->control & 0x0000000E) | + ((*gtb->gtimer_control) & 0x00= 00FF01); + case 12: /* Interrupt status. */ + return gtb->status; + case= 16: /* Compare LSB */ + return gtb->compare & 0xFFFFFFFF; + case 20= : /* Counter MSB */ + return gtb->compare >> 32; + case 24: /* Aut= oincrement */ + return gtb->inc; + default: + return 0; + } +} + +static= void gtimer_write(void *opaque, hwaddr addr, + uint64_t value, unsigned si= ze) +{ + gTimerBlock *gtb =3D (gTimerBlock *)opaque; + int64_t old; + switc= h (addr) { + case 0: /* Counter LSB */ + old =3D (*gtb->gtimer_counter);= + old &=3D 0xFFFFFFFF; + gtb->delta =3D old - (value & 0xFFFFFF= FF); + old |=3D value & 0xFFFFFFFF; + *gtb->gtimer_counter =3D old; = + /* Cancel the previous timer. */ + if (((gtb->control | *gtb->gtime= r_control) & 7) =3D=3D 7) { + gtimerblock_reload(gtb, 1); + } + break; = + case 4: /* Counter MSB. */ + old =3D (*gtb->gtimer_counter); + old &am= p;=3D 0xFFFFFFFF00000000; + gtb->delta =3D old - (value << 32); + = old |=3D value << 32; + *gtb->gtimer_counter =3D old; + if (((gtb-= >control | *gtb->gtimer_control) & 7) =3D=3D 7) { + gtimerblock_r= eload(gtb, 1); + } + break; + case 8: /* Control. */ + old =3D *gtb->gti= mer_control; + gtb->control =3D value & 0x0000000E; + *gtb->gtime= r_control =3D value & 0x0000FF01; + if (((old & 1) =3D=3D 0) &&= amp; ((value & 7) =3D=3D 7)) { + gtb->delta =3D 0; + gtimerblock_rel= oad(gtb, 1); + } + break; + case 12: /* Interrupt status. */ + gtb->stat= us ^=3D value & 1;
This doesn't look right -- this bit is write-one-to-clear,
not write-one-to-invert.


Correct.

+ gtimerblock_update_irq(gtb); + brea= k; + case 16: /* Compare LSB */ + old =3D gtb->compare; + old &=3D 0= xFFFFFFFF; + gtb->delta =3D (value & 0xFFFFFFFF) - old; + old |=3D v= alue & 0xFFFFFFFF; + gtb->compare =3D old; + if (((gtb->control |= *gtb->gtimer_control) & 7) =3D=3D 7) { + gtimerblock_reload(gtb, 1)= ; + } + break; + case 20: /* Compare MSB. */ + old =3D gtb->compare; + o= ld &=3D 0xFFFFFFFF00000000; + gtb->delta =3D (value << 32) - o= ld; + old |=3D value << 32; + gtb->compare =3D old; + if (((gtb-&g= t;control | *gtb->gtimer_control) & 7) =3D=3D 7) { + gtimerblock_rel= oad(gtb, 1); + } + break; + case 24: /* Autoincrement */ + gtb->inc =3D = value; + break; + default: + break; + } +} + +/* Wrapper functions to imple= ment the "read timer/watchdog for
"watchdog" doesn't apply here.


Correct. Copy/Paste...

+ * the current CPU" memory regions= =2E + */ +static uint64_t arm_thisgtimer_read(void *opaque, hwaddr addr, + = unsigned size) +{ + ARMMPGTimerState *s =3D (ARMMPGTimerState *)opaque; + i= nt id =3D get_current_cpu(s); + return gtimer_read(&s->gtimer[id], a= ddr, size); +} + +static void arm_thisgtimer_write(void *opaque, hwaddr add= r, + uint64_t value, unsigned size) +{ + ARMMPGTimerState *s =3D (ARMMPGTim= erState *)opaque; + int id =3D get_current_cpu(s); + gtimer_write(&s-&g= t;gtimer[id], addr, value, size); +} + +static const MemoryRegionOps arm_th= isgtimer_ops =3D { + .read =3D arm_thisgtimer_read, + .write =3D arm_thisgt= imer_write, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D= 4, + }, + .endianness =3D DEVICE_NATIVE_ENDIAN, +}; + +static const Memory= RegionOps gtimerblock_ops =3D { + .read =3D gtimer_read, + .write =3D gtime= r_write, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 4,= + }, + .endianness =3D DEVICE_NATIVE_ENDIAN, +}; + +static void gtimer_res= et(gTimerBlock *gtb) +{ + gtb->control =3D 0; + gtb->status =3D 0; + = gtb->compare =3D 0; + gtb->inc =3D 0; + gtb->tick =3D 0;
Reset should delete the qemu timer too.


Correct. I'll fix this is the next version of patch

+} + +static void arm_mpgtimer_reset(= DeviceState *dev) +{ + ARMMPGTimerState *s =3D + FROM_SYSBUS(ARMMPGTimerSta= te, SYS_BUS_DEVICE(dev));
Please don't use FROM_SYSBUS in new code.


Alright. I'll fix this is the next version of patch

+ int i; + for (i =3D 0; i < ARRAY= _SIZE(s->gtimer); i++) { + gtimer_reset(&s->gtimer[i]); + } +} + = +static int arm_mpgtimer_init(SysBusDevice *dev) +{ + ARMMPGTimerState *s = =3D FROM_SYSBUS(ARMMPGTimerState, dev); + int i; + if (s->num_cpu < 1= || s->num_cpu > MAX_CPUS) { + hw_error("%s: num-cpu must be between = 1 and %d\n", __func__, MAX_CPUS);
If you make this a DeviceClass realize method (see below) then
you can use error_setg rather than hw_error here. (pl330.c has
an example.)


Alright. I'll fix this is the next version of patch

+ } + + /* We implement one timer blo= ck per CPU, and expose multiple MMIO regions: + * * region 0 is "timer for = this core" + * * region 1 is "timer for core 0" + * * region 2 is "timer fo= r core 1" + * and so on.
Why implement multiple memory regions? There is only one
for the global timer.


Sorry. Copy/Paste...

+ * The outgoing interrupt lines are = + * * timer for core 0 + * * timer for core 1 + * and so on. + */ + memory_= region_init_io(&s->iomem, &arm_thisgtimer_ops, s, + "arm_mptimer= _gtimer", 0x20); + sysbus_init_mmio(dev, &s->iomem); + for (i =3D 0;= i < s->num_cpu; i++) { + gTimerBlock *gtb =3D &s->gtimer[i]; = + gtb->gtimer_counter =3D &s->gtimer_counter; + gtb->gtimer_co= ntrol =3D &s->gtimer_control; + gtb->timer =3D qemu_new_timer_ns(= vm_clock, gtimerblock_tick, gtb); + sysbus_init_irq(dev, &gtb->irq);= + memory_region_init_io(&gtb->iomem, &gtimerblock_ops, gtb, + "= arm_mptimer_gtimerblock", 0x20); + sysbus_init_mmio(dev, &gtb->iomem= ); + } + + return 0; +} + +static const VMStateDescription vmstate_gtimerbl= ock =3D { + .name =3D "arm_mptimer_gtimerblock", + .version_id =3D 2, + .mi= nimum_version_id =3D 2,
Why version 2?


Sorry. Copy/Paste...

+ .fields =3D (VMStateField[]) { + VM= STATE_UINT32(control, gTimerBlock), + VMSTATE_UINT32(status, gTimerBlock), = + VMSTATE_UINT64(compare, gTimerBlock), + VMSTATE_INT64(tick, gTimerBlock),= + VMSTATE_END_OF_LIST() + } +}; + +static const VMStateDescription vmstate= _arm_mpgtimer =3D { + .name =3D "arm_mp_globaltimer", + .version_id =3D 2, = + .minimum_version_id =3D 2, + .fields =3D (VMStateField[]) { + VMSTATE_STR= UCT_VARRAY_UINT32(gtimer, ARMMPGTimerState, num_cpu, + 1, vmstate_gtimerblo= ck, gTimerBlock), + VMSTATE_END_OF_LIST() + } +}; + +static Property arm_mp= gtimer_properties[] =3D { + DEFINE_PROP_UINT32("num-cpu", ARMMPGTimerState,= num_cpu, 0), + DEFINE_PROP_END_OF_LIST() +}; + +static void arm_mpgtimer_c= lass_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_C= LASS(klass); + SysBusDeviceClass *sbc =3D SYS_BUS_DEVICE_CLASS(klass); + + = sbc->init =3D arm_mpgtimer_init;
Can you use the DeviceClass realize method rather than
SysBusDeviceClass init, please? (see eg a9scu.c for an
example.)


Alright. I'll fix this is the next version of patch

+ dc->vmsd =3D &vmstate_arm_mp= gtimer; + dc->reset =3D arm_mpgtimer_reset; + dc->no_user =3D 1; + dc= ->props =3D arm_mpgtimer_properties; +} + +static const TypeInfo arm_mpg= timer_info =3D { + .name =3D "arm_mp_globaltimer", + .parent =3D TYPE_SYS_B= US_DEVICE, + .instance_size =3D sizeof(ARMMPGTimerState), + .class_init =3D= arm_mpgtimer_class_init, +}; + +static void arm_mpgtimer_register_types(vo= id) +{ + type_register_static(&arm_mpgtimer_info); +} + +type_init(arm_= mpgtimer_register_types) + diff -urN qemu-master/hw/timer/arm_mptimer.c qem= u-master.new/hw/timer/arm_mptimer.c --- qemu-master/hw/timer/arm_mptimer.c = 2013-04-08 20:12:33.000000000 +0200 +++ qemu-master.new/hw/timer/arm_mptime= r.c 2013-04-15 13:44:33.000000000 +0200 @@ -49,13 +49,19 @@ static inline i= nt get_current_cpu(ARMMPTimerState *s) { - CPUState *cpu_single_cpu =3D ENV= _GET_CPU(cpu_single_env); + CPUState *cpu_single_cpu; - if (cpu_single_cpu-= >cpu_index >=3D s->num_cpu) { - hw_error("arm_mptimer: num-cpu %d = but this cpu is %d!\n", - s->num_cpu, cpu_single_cpu->cpu_index); + i= f (cpu_single_env !=3D NULL) { + cpu_single_cpu =3D ENV_GET_CPU(cpu_single_= env); + + if (cpu_single_cpu->cpu_index >=3D s->num_cpu) { + hw_er= ror("arm_mptimer: num-cpu %d but this cpu is %d!\n", + s->num_cpu, cpu_s= ingle_cpu->cpu_index); + } + return cpu_single_cpu->cpu_index; + } el= se { + return 0; } - return cpu_single_cpu->cpu_index; }
Why is this patch changing this code in an unrelated device?


This is for the same reason I modified the get_current_cpu for glo= bal timer.

static inline void timerblock_update_= irq(TimerBlock *tb) diff -urN qemu-master/hw/timer/Makefile.objs qemu-maste= r.new/hw/timer/Makefile.objs --- qemu-master/hw/timer/Makefile.objs 2013-04= -08 20:12:33.000000000 +0200 +++ qemu-master.new/hw/timer/Makefile.objs 201= 3-04-15 12:54:06.000000000 +0200 @@ -24,5 +24,5 @@ obj-$(CONFIG_SH4) +=3D s= h_timer.o obj-$(CONFIG_TUSB6010) +=3D tusb6010.o -obj-$(CONFIG_ARM_MPTIMER)= +=3D arm_mptimer.o +obj-$(CONFIG_ARM_MPTIMER) +=3D arm_mptimer.o arm_mpgti= mer.o obj-$(CONFIG_MC146818RTC) +=3D mc146818rtc.o Signed-off-by: Fran&cced= il;ois LEGAL <devel@thom.fr.eu= =2Eorg>
thanks
-- PMM
 
--=_e02875ddf5d151efe2178bfcbc66d09e--