qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] timer: Handle decrements of PIT counter
@ 2020-06-26 18:06 Roman Bolshakov
  2020-06-26 18:27 ` Kevin O'Connor
  0 siblings, 1 reply; 2+ messages in thread
From: Roman Bolshakov @ 2020-06-26 18:06 UTC (permalink / raw)
  To: seabios
  Cc: Roman Bolshakov, Kevin O'Connor, Philippe Mathieu-Daudé,
	qemu-devel

There's a fallback to PIT if TSC is not present but it doesn't work
properly. It prevents boot from floppy on isapc and 486 cpu [1][2].

SeaBIOS configures PIT in Mode 2. PIT counter is decremented in the mode
but timer_adjust_bits() thinks that the counter overflows and increases
32-bit tick counter on each detected "overflow". Invalid overflow
detection results in 55ms time advance (1 / 18.2Hz) on each read from
PIT counter. So all timers expire much faster and 5-second floppy
timeout expires in 83 real microseconds (or just a bit longer).

It can be fixed by making the counter recieved from PIT an increasing
value so it can be passed to timer_adjust_bits():
0, 1, 2 and up to 65535 and then the counter is re-loaded with 0.

1. https://bugs.launchpad.net/seabios/+bug/1840719
2. https://lists.gnu.org/archive/html/qemu-devel/2019-08/msg03924.html

Fixes: eac11944019 ("Unify pmtimer_read() and pittimer_read() code.")
Reported-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Roman Bolshakov <r.bolshakov@yadro.com>
---
Changes since v1:
- Simplified change of counter direction (Kevin)

 src/hw/timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/hw/timer.c b/src/hw/timer.c
index 56bb289..b6f102e 100644
--- a/src/hw/timer.c
+++ b/src/hw/timer.c
@@ -180,7 +180,7 @@ timer_read(void)
     // Read from PIT.
     outb(PM_SEL_READBACK | PM_READ_VALUE | PM_READ_COUNTER0, PORT_PIT_MODE);
     u16 v = inb(PORT_PIT_COUNTER0) | (inb(PORT_PIT_COUNTER0) << 8);
-    return timer_adjust_bits(v, 0xffff);
+    return timer_adjust_bits(-v, 0xffff);
 }
 
 // Return the TSC value that is 'msecs' time in the future.
-- 
2.26.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] timer: Handle decrements of PIT counter
  2020-06-26 18:06 [PATCH v2] timer: Handle decrements of PIT counter Roman Bolshakov
@ 2020-06-26 18:27 ` Kevin O'Connor
  0 siblings, 0 replies; 2+ messages in thread
From: Kevin O'Connor @ 2020-06-26 18:27 UTC (permalink / raw)
  To: Roman Bolshakov; +Cc: seabios, Philippe Mathieu-Daudé, qemu-devel

On Fri, Jun 26, 2020 at 09:06:58PM +0300, Roman Bolshakov wrote:
> There's a fallback to PIT if TSC is not present but it doesn't work
> properly. It prevents boot from floppy on isapc and 486 cpu [1][2].
> 
> SeaBIOS configures PIT in Mode 2. PIT counter is decremented in the mode
> but timer_adjust_bits() thinks that the counter overflows and increases
> 32-bit tick counter on each detected "overflow". Invalid overflow
> detection results in 55ms time advance (1 / 18.2Hz) on each read from
> PIT counter. So all timers expire much faster and 5-second floppy
> timeout expires in 83 real microseconds (or just a bit longer).
> 
> It can be fixed by making the counter recieved from PIT an increasing
> value so it can be passed to timer_adjust_bits():
> 0, 1, 2 and up to 65535 and then the counter is re-loaded with 0.
> 
> 1. https://bugs.launchpad.net/seabios/+bug/1840719
> 2. https://lists.gnu.org/archive/html/qemu-devel/2019-08/msg03924.html

Thanks.  I committed this change.

-Kevin


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-06-26 18:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-26 18:06 [PATCH v2] timer: Handle decrements of PIT counter Roman Bolshakov
2020-06-26 18:27 ` Kevin O'Connor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).