Handle tsc rollover.
Handle case where timetamp counter overflows while waiting.
diff --git a/src/ata.c b/src/ata.c
index 2312f7d..050269e 100644
--- a/src/ata.c
+++ b/src/ata.c
@@ -36,7 +36,7 @@
u8 status = inb(base+ATA_CB_STAT);
if ((status & mask) == flags)
return status;
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "IDE time out\n");
return -1;
}
@@ -107,7 +107,7 @@
if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
break;
// Change drive request failed to take effect - retry.
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "ata_reset slave time out\n");
goto done;
}
@@ -649,7 +649,7 @@
dprintf(1, "powerup IDE floating\n");
return orstatus;
}
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "powerup IDE time out\n");
return -1;
}
diff --git a/src/cdrom.c b/src/cdrom.c
index ba533d0..8d1ec9a 100644
--- a/src/cdrom.c
+++ b/src/cdrom.c
@@ -228,7 +228,7 @@
int in_progress = 0;
u64 end = calc_future_tsc(5000);
for (;;) {
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "read capacity failed\n");
return -1;
}
diff --git a/src/clock.c b/src/clock.c
index 0592a4e..7735c70 100644
--- a/src/clock.c
+++ b/src/clock.c
@@ -93,11 +93,11 @@
}
static void
-tscsleep(u64 diff)
+tscdelay(u64 diff)
{
u64 start = rdtscll();
u64 end = start + diff;
- while (rdtscll() <= end)
+ while (!check_time(end))
cpu_relax();
}
@@ -105,19 +105,19 @@
ndelay(u32 count)
{
u32 khz = GET_GLOBAL(cpu_khz);
- tscsleep(count * khz / 1000000);
+ tscdelay(count * khz / 1000000);
}
void
udelay(u32 count)
{
u32 khz = GET_GLOBAL(cpu_khz);
- tscsleep(count * khz / 1000);
+ tscdelay(count * khz / 1000);
}
void
mdelay(u32 count)
{
u32 khz = GET_GLOBAL(cpu_khz);
- tscsleep(count * khz);
+ tscdelay(count * khz);
}
// Return the TSC value that is 'msecs' time in the future.
@@ -156,7 +156,7 @@
do {
if ((inb_cmos(CMOS_STATUS_A) & RTC_A_UIP) == 0)
return 0;
- } while (rdtscll() <= end);
+ } while (!check_time(end));
// update-in-progress never transitioned to 0
return -1;
diff --git a/src/ps2port.c b/src/ps2port.c
index 01e8b3d..25d4544 100644
--- a/src/ps2port.c
+++ b/src/ps2port.c
@@ -152,7 +152,7 @@
{
u64 end = calc_future_tsc(timeout);
for (;;) {
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "ps2_recvbyte timeout\n");
return -1;
}
diff --git a/src/usb-ohci.c b/src/usb-ohci.c
index f1ca559..71202f8 100644
--- a/src/usb-ohci.c
+++ b/src/usb-ohci.c
@@ -34,7 +34,7 @@
u32 status = readl(&cntl->ohci.regs->cmdstatus);
if (! status & OHCI_HCR)
break;
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "Timeout on ohci software reset\n");
return -1;
}
@@ -181,7 +181,7 @@
for (;;) {
if (ed->hwHeadP == ed->hwTailP)
return 0;
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "Timeout on wait_ed %p\n", ed);
return -1;
}
diff --git a/src/usb-uhci.c b/src/usb-uhci.c
index 5829069..14a5300 100644
--- a/src/usb-uhci.c
+++ b/src/usb-uhci.c
@@ -160,7 +160,7 @@
for (;;) {
if (qh->element & UHCI_PTR_TERM)
return 0;
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "Timeout on wait_qh %p\n", qh);
return -1;
}
diff --git a/src/util.h b/src/util.h
index 6cf27ac..f95cdb5 100644
--- a/src/util.h
+++ b/src/util.h
@@ -214,6 +214,9 @@
void lpt_setup();
// clock.c
+static inline int check_time(u64 end) {
+ return (s64)(rdtscll() - end) > 0;
+}
void timer_setup();
void ndelay(u32 count);
void udelay(u32 count);