Re: powerpc, macppc: switch to clockintr(9)

2022-11-21 Thread Scott Cheloha
On Sun, Nov 20, 2022 at 03:15:55PM -0500, George Koehler wrote:
> On Sun, 20 Nov 2022 06:55:23 -0600
> Scott Cheloha  wrote:
> 
> > Build completed, upgrade from resulting bsd.rd completed.  I think
> > this is ready to commit.
> 
> I'm happy to hear that your dual G4 macppc is running and your diff
> works.  Your clock diff doesn't conflict with my incomplete suspend
> diff, because we are editing different files.  (My PowerBook G4 can
> suspend but not resume, so my diff isn't ready to commit.)
> 
> I want to mention one little detail,
> 
> > @@ -54,7 +58,14 @@ u_int tb_get_timecount(struct timecounte
> >   */
> >  u_int32_t ticks_per_sec = 3125000;
> >  u_int32_t ns_per_tick = 320;
> > -static int32_t ticks_per_intr;
> > +uint64_t dec_nsec_cycle_ratio;
> > +uint64_t dec_nsec_max;
> > +static int initialized;
> > +
> > +const struct intrclock dec_intrclock = {
> > +   .ic_rearm = dec_rearm,
> > +   .ic_trigger = dec_trigger
> > +};
> >  
> >  static struct timecounter tb_timecounter = {
> > .tc_get_timecount = tb_get_timecount,
> 
> The old "static int32_t ticks_per_intr;" was visible in ddb,
> 
> ddb> x/ld ticks_per_intr,1
> ticks_per_intr: 184320
> 
> I don't like "static int initialized;" because its name is too
> generic, so "x/ld initialized,1" might examine the wrong file's
> "initialized".
> 
> I prefer a less generic name, perhaps
> "int clock_initialized;"
> 
> If it isn't "static", the linker might check that it doesn't have
> the same name as another variable.  I don't need you to try another
> "make build" if you rename this variable.

Alright, I changed it to a non-static "clock_initialized".  Kernel
compiles, boots, etc.

Will go ahead with the attached patch shortly.

Index: sys/arch/macppc/macppc/clock.c
===
RCS file: /cvs/src/sys/arch/macppc/macppc/clock.c,v
retrieving revision 1.50
diff -u -p -r1.50 clock.c
--- sys/arch/macppc/macppc/clock.c  8 Sep 2022 03:06:33 -   1.50
+++ sys/arch/macppc/macppc/clock.c  21 Nov 2022 20:30:41 -
@@ -35,7 +35,9 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
 #include 
 
 #include 
@@ -46,6 +48,8 @@
 #include 
 #include 
 
+void dec_rearm(void *, uint64_t);
+void dec_trigger(void *);
 void decr_intr(struct clockframe *frame);
 u_int tb_get_timecount(struct timecounter *);
 
@@ -54,7 +58,14 @@ u_int tb_get_timecount(struct timecounte
  */
 u_int32_t ticks_per_sec = 3125000;
 u_int32_t ns_per_tick = 320;
-static int32_t ticks_per_intr;
+uint64_t dec_nsec_cycle_ratio;
+uint64_t dec_nsec_max;
+int clock_initialized;
+
+const struct intrclock dec_intrclock = {
+   .ic_rearm = dec_rearm,
+   .ic_trigger = dec_trigger
+};
 
 static struct timecounter tb_timecounter = {
.tc_get_timecount = tb_get_timecount,
@@ -75,15 +86,8 @@ static const char *calibrate_tc_models[]
 time_read_t  *time_read;
 time_write_t *time_write;
 
-/* vars for stats */
-int statint;
-u_int32_t statvar;
-u_int32_t statmin;
-
 static struct evcount clk_count;
-static struct evcount stat_count;
 static int clk_irq = PPC_CLK_IRQ;
-static int stat_irq = PPC_STAT_IRQ;
 
 extern todr_chip_handle_t todr_handle;
 struct todr_chip_handle rtc_todr;
@@ -115,18 +119,16 @@ rtc_settime(struct todr_chip_handle *han
 void
 decr_intr(struct clockframe *frame)
 {
-   u_int64_t tb;
-   u_int64_t nextevent;
struct cpu_info *ci = curcpu();
-   int nstats;
int s;
 
-   /*
-* Check whether we are initialized.
-*/
-   if (!ticks_per_intr)
+   if (!clock_initialized)
return;
 
+   clk_count.ec_count++;   /* XXX not atomic */
+
+   ppc_mtdec(UINT32_MAX >> 1); /* clear DEC exception */
+
/*
 * We can't actually mask DEC interrupts at or above IPL_CLOCK
 * without masking other essential interrupts.  To simulate
@@ -135,73 +137,15 @@ decr_intr(struct clockframe *frame)
 */
if (ci->ci_cpl >= IPL_CLOCK) {
ci->ci_dec_deferred = 1;
-   ppc_mtdec(UINT32_MAX >> 1); /* clear DEC exception */
return;
}
ci->ci_dec_deferred = 0;
 
-   /*
-* Based on the actual time delay since the last decrementer reload,
-* we arrange for earlier interrupt next time.
-*/
-
-   tb = ppc_mftb();
-   while (ci->ci_nexttimerevent <= tb)
-   ci->ci_nexttimerevent += ticks_per_intr;
-
-   ci->ci_prevtb = ci->ci_nexttimerevent - ticks_per_intr;
-
-   for (nstats = 0; ci->ci_nextstatevent <= tb; nstats++) {
-   int r;
-   do {
-   r = random() & (statvar -1);
-   } while (r == 0); /* random == 0 not allowed */
-   ci->ci_nextstatevent += statmin + r;
-   }
-
-   /* only count timer ticks for CLK_IRQ */
-   stat_count.ec_count += nstats;
-
-   if (ci->ci_nexttimerevent < ci->ci_nextstatevent)
-  

Re: powerpc, macppc: switch to clockintr(9)

2022-11-20 Thread George Koehler
On Sun, 20 Nov 2022 06:55:23 -0600
Scott Cheloha  wrote:

> Build completed, upgrade from resulting bsd.rd completed.  I think
> this is ready to commit.

I'm happy to hear that your dual G4 macppc is running and your diff
works.  Your clock diff doesn't conflict with my incomplete suspend
diff, because we are editing different files.  (My PowerBook G4 can
suspend but not resume, so my diff isn't ready to commit.)

I want to mention one little detail,

> @@ -54,7 +58,14 @@ u_int tb_get_timecount(struct timecounte
>   */
>  u_int32_t ticks_per_sec = 3125000;
>  u_int32_t ns_per_tick = 320;
> -static int32_t ticks_per_intr;
> +uint64_t dec_nsec_cycle_ratio;
> +uint64_t dec_nsec_max;
> +static int initialized;
> +
> +const struct intrclock dec_intrclock = {
> + .ic_rearm = dec_rearm,
> + .ic_trigger = dec_trigger
> +};
>  
>  static struct timecounter tb_timecounter = {
>   .tc_get_timecount = tb_get_timecount,

The old "static int32_t ticks_per_intr;" was visible in ddb,

ddb> x/ld ticks_per_intr,1
ticks_per_intr: 184320

I don't like "static int initialized;" because its name is too
generic, so "x/ld initialized,1" might examine the wrong file's
"initialized".

I prefer a less generic name, perhaps
"int clock_initialized;"

If it isn't "static", the linker might check that it doesn't have
the same name as another variable.  I don't need you to try another
"make build" if you rename this variable.

--George



Re: powerpc, macppc: switch to clockintr(9)

2022-11-20 Thread Scott Cheloha
On Sat, Nov 19, 2022 at 09:46:03AM -0600, Scott Cheloha wrote:
> On Sun, Nov 06, 2022 at 07:48:31PM +, Scott Cheloha wrote:
> > This patch switches macppc to clockintr(9).
> > 
> > It has survived two or three parallel release builds on my G4 MDD
> > (PowerMac7,3) and upgrades from the resulting bsd.rd.  The machine
> > does not have a working IDE drive at the moment, so I booted and built
> > over USB 1.1 on an external drive.
> > 
> > It needs testing on a beefier Mac.  It should probably also be tested
> > on a Mac with a working graphics card.
> > 
> > I am aware of gkoehler@'s work on suspend/resume.  We'll need to
> > coordinate this to avoid breaking things.
> > 
> > Notes:
> > 
> > - There are no longer separate counters for "clock" and "stat".
> 
> I bought a SATA/PATA adapter and, to my surprise, it worked!  I had an
> older Kingston SATA SDD lying around and lo, the G4 now boots from
> that over the PATA ribbon.  What a time to be alive!
> 
> The mac is doing a parallel build (`make -j2 build`) atop a kernel
> with the attached patch right now.  Still up after two days.
> Accelerated graphics do not work on this machine, but I think the
> radeon card is just semi-fried...  pretty sure it has nothing to do
> with the patch.
> 
> Assuming the build completes and the install from the resulting bsd.rd
> succeeds, can I commit this?
> 
> Trying to avoid trampling on whatever gkoehler@ is doing.
> 
> ok?

Build completed, upgrade from resulting bsd.rd completed.  I think
this is ready to commit.

Looking for an ok.

Index: sys/arch/macppc/macppc/clock.c
===
RCS file: /cvs/src/sys/arch/macppc/macppc/clock.c,v
retrieving revision 1.50
diff -u -p -r1.50 clock.c
--- sys/arch/macppc/macppc/clock.c  8 Sep 2022 03:06:33 -   1.50
+++ sys/arch/macppc/macppc/clock.c  19 Nov 2022 15:44:22 -
@@ -35,7 +35,9 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
 #include 
 
 #include 
@@ -46,6 +48,8 @@
 #include 
 #include 
 
+void dec_rearm(void *, uint64_t);
+void dec_trigger(void *);
 void decr_intr(struct clockframe *frame);
 u_int tb_get_timecount(struct timecounter *);
 
@@ -54,7 +58,14 @@ u_int tb_get_timecount(struct timecounte
  */
 u_int32_t ticks_per_sec = 3125000;
 u_int32_t ns_per_tick = 320;
-static int32_t ticks_per_intr;
+uint64_t dec_nsec_cycle_ratio;
+uint64_t dec_nsec_max;
+static int initialized;
+
+const struct intrclock dec_intrclock = {
+   .ic_rearm = dec_rearm,
+   .ic_trigger = dec_trigger
+};
 
 static struct timecounter tb_timecounter = {
.tc_get_timecount = tb_get_timecount,
@@ -75,15 +86,8 @@ static const char *calibrate_tc_models[]
 time_read_t  *time_read;
 time_write_t *time_write;
 
-/* vars for stats */
-int statint;
-u_int32_t statvar;
-u_int32_t statmin;
-
 static struct evcount clk_count;
-static struct evcount stat_count;
 static int clk_irq = PPC_CLK_IRQ;
-static int stat_irq = PPC_STAT_IRQ;
 
 extern todr_chip_handle_t todr_handle;
 struct todr_chip_handle rtc_todr;
@@ -115,18 +119,16 @@ rtc_settime(struct todr_chip_handle *han
 void
 decr_intr(struct clockframe *frame)
 {
-   u_int64_t tb;
-   u_int64_t nextevent;
struct cpu_info *ci = curcpu();
-   int nstats;
int s;
 
-   /*
-* Check whether we are initialized.
-*/
-   if (!ticks_per_intr)
+   if (!initialized)
return;
 
+   clk_count.ec_count++;   /* XXX not atomic */
+
+   ppc_mtdec(UINT32_MAX >> 1); /* clear DEC exception */
+
/*
 * We can't actually mask DEC interrupts at or above IPL_CLOCK
 * without masking other essential interrupts.  To simulate
@@ -135,73 +137,15 @@ decr_intr(struct clockframe *frame)
 */
if (ci->ci_cpl >= IPL_CLOCK) {
ci->ci_dec_deferred = 1;
-   ppc_mtdec(UINT32_MAX >> 1); /* clear DEC exception */
return;
}
ci->ci_dec_deferred = 0;
 
-   /*
-* Based on the actual time delay since the last decrementer reload,
-* we arrange for earlier interrupt next time.
-*/
-
-   tb = ppc_mftb();
-   while (ci->ci_nexttimerevent <= tb)
-   ci->ci_nexttimerevent += ticks_per_intr;
-
-   ci->ci_prevtb = ci->ci_nexttimerevent - ticks_per_intr;
-
-   for (nstats = 0; ci->ci_nextstatevent <= tb; nstats++) {
-   int r;
-   do {
-   r = random() & (statvar -1);
-   } while (r == 0); /* random == 0 not allowed */
-   ci->ci_nextstatevent += statmin + r;
-   }
-
-   /* only count timer ticks for CLK_IRQ */
-   stat_count.ec_count += nstats;
-
-   if (ci->ci_nexttimerevent < ci->ci_nextstatevent)
-   nextevent = ci->ci_nexttimerevent;
-   else
-   nextevent = ci->ci_nextstatevent;
-
-   /*
-* Need to work about the near constant 

Re: powerpc, macppc: switch to clockintr(9)

2022-11-19 Thread Scott Cheloha
On Sun, Nov 06, 2022 at 07:48:31PM +, Scott Cheloha wrote:
> This patch switches macppc to clockintr(9).
> 
> It has survived two or three parallel release builds on my G4 MDD
> (PowerMac7,3) and upgrades from the resulting bsd.rd.  The machine
> does not have a working IDE drive at the moment, so I booted and built
> over USB 1.1 on an external drive.
> 
> It needs testing on a beefier Mac.  It should probably also be tested
> on a Mac with a working graphics card.
> 
> I am aware of gkoehler@'s work on suspend/resume.  We'll need to
> coordinate this to avoid breaking things.
> 
> Notes:
> 
> - There are no longer separate counters for "clock" and "stat".

I bought a SATA/PATA adapter and, to my surprise, it worked!  I had an
older Kingston SATA SDD lying around and lo, the G4 now boots from
that over the PATA ribbon.  What a time to be alive!

The mac is doing a parallel build (`make -j2 build`) atop a kernel
with the attached patch right now.  Still up after two days.
Accelerated graphics do not work on this machine, but I think the
radeon card is just semi-fried...  pretty sure it has nothing to do
with the patch.

Assuming the build completes and the install from the resulting bsd.rd
succeeds, can I commit this?

Trying to avoid trampling on whatever gkoehler@ is doing.

ok?

Index: sys/arch/macppc/macppc/clock.c
===
RCS file: /cvs/src/sys/arch/macppc/macppc/clock.c,v
retrieving revision 1.50
diff -u -p -r1.50 clock.c
--- sys/arch/macppc/macppc/clock.c  8 Sep 2022 03:06:33 -   1.50
+++ sys/arch/macppc/macppc/clock.c  19 Nov 2022 15:44:22 -
@@ -35,7 +35,9 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
 #include 
 
 #include 
@@ -46,6 +48,8 @@
 #include 
 #include 
 
+void dec_rearm(void *, uint64_t);
+void dec_trigger(void *);
 void decr_intr(struct clockframe *frame);
 u_int tb_get_timecount(struct timecounter *);
 
@@ -54,7 +58,14 @@ u_int tb_get_timecount(struct timecounte
  */
 u_int32_t ticks_per_sec = 3125000;
 u_int32_t ns_per_tick = 320;
-static int32_t ticks_per_intr;
+uint64_t dec_nsec_cycle_ratio;
+uint64_t dec_nsec_max;
+static int initialized;
+
+const struct intrclock dec_intrclock = {
+   .ic_rearm = dec_rearm,
+   .ic_trigger = dec_trigger
+};
 
 static struct timecounter tb_timecounter = {
.tc_get_timecount = tb_get_timecount,
@@ -75,15 +86,8 @@ static const char *calibrate_tc_models[]
 time_read_t  *time_read;
 time_write_t *time_write;
 
-/* vars for stats */
-int statint;
-u_int32_t statvar;
-u_int32_t statmin;
-
 static struct evcount clk_count;
-static struct evcount stat_count;
 static int clk_irq = PPC_CLK_IRQ;
-static int stat_irq = PPC_STAT_IRQ;
 
 extern todr_chip_handle_t todr_handle;
 struct todr_chip_handle rtc_todr;
@@ -115,18 +119,16 @@ rtc_settime(struct todr_chip_handle *han
 void
 decr_intr(struct clockframe *frame)
 {
-   u_int64_t tb;
-   u_int64_t nextevent;
struct cpu_info *ci = curcpu();
-   int nstats;
int s;
 
-   /*
-* Check whether we are initialized.
-*/
-   if (!ticks_per_intr)
+   if (!initialized)
return;
 
+   clk_count.ec_count++;   /* XXX not atomic */
+
+   ppc_mtdec(UINT32_MAX >> 1); /* clear DEC exception */
+
/*
 * We can't actually mask DEC interrupts at or above IPL_CLOCK
 * without masking other essential interrupts.  To simulate
@@ -135,73 +137,15 @@ decr_intr(struct clockframe *frame)
 */
if (ci->ci_cpl >= IPL_CLOCK) {
ci->ci_dec_deferred = 1;
-   ppc_mtdec(UINT32_MAX >> 1); /* clear DEC exception */
return;
}
ci->ci_dec_deferred = 0;
 
-   /*
-* Based on the actual time delay since the last decrementer reload,
-* we arrange for earlier interrupt next time.
-*/
-
-   tb = ppc_mftb();
-   while (ci->ci_nexttimerevent <= tb)
-   ci->ci_nexttimerevent += ticks_per_intr;
-
-   ci->ci_prevtb = ci->ci_nexttimerevent - ticks_per_intr;
-
-   for (nstats = 0; ci->ci_nextstatevent <= tb; nstats++) {
-   int r;
-   do {
-   r = random() & (statvar -1);
-   } while (r == 0); /* random == 0 not allowed */
-   ci->ci_nextstatevent += statmin + r;
-   }
-
-   /* only count timer ticks for CLK_IRQ */
-   stat_count.ec_count += nstats;
-
-   if (ci->ci_nexttimerevent < ci->ci_nextstatevent)
-   nextevent = ci->ci_nexttimerevent;
-   else
-   nextevent = ci->ci_nextstatevent;
-
-   /*
-* Need to work about the near constant skew this introduces???
-* reloading tb here could cause a missed tick.
-*/
-   ppc_mtdec(nextevent - tb);
-
-   nstats += ci->ci_statspending;
-   ci->ci_statspending = 0;
-
s = splclock();
-
-   /*
-