Introduce cpu_relax() and use it in busy loops.
authorKevin O'Connor <kevin@koconnor.net>
Sat, 5 Apr 2008 23:30:02 +0000 (19:30 -0400)
committerKevin O'Connor <kevin@koconnor.net>
Sat, 5 Apr 2008 23:30:02 +0000 (19:30 -0400)
src/floppy.c
src/kbd.c
src/util.h

index 0954720e565912df2edb3e4dfd6261edd5981fa5..c27ee87a5c4435f8a21a40541819393eafa36b49 100644 (file)
@@ -117,9 +117,12 @@ floppy_prepare_controller(u8 drive)
     if (prev_reset == 0) {
         irq_enable();
         u8 v;
-        do {
+        for (;;) {
             v = GET_BDA(floppy_recalibration_status);
-        } while ((v & FRS_TIMEOUT) == 0);
+            if (v & FRS_TIMEOUT)
+                break;
+            cpu_relax();
+        }
         irq_disable();
 
         v &= ~FRS_TIMEOUT;
@@ -139,14 +142,17 @@ floppy_pio(u8 *cmd, u8 cmdlen)
 
     irq_enable();
     u8 v;
-    do {
+    for (;;) {
         if (!GET_BDA(floppy_motor_counter)) {
             irq_disable();
             floppy_reset_controller();
             return DISK_RET_ETIMEOUT;
         }
         v = GET_BDA(floppy_recalibration_status);
-    } while (!(v & FRS_TIMEOUT));
+        if (v & FRS_TIMEOUT)
+            break;
+        cpu_relax();
+    }
     irq_disable();
 
     v &= ~FRS_TIMEOUT;
index 0e0dd5b7a2f6b1e7f46ea7fb1310d67e64c05178..3770305b213897a7840f374a7929294922468737 100644 (file)
--- a/src/kbd.c
+++ b/src/kbd.c
@@ -59,8 +59,8 @@ keyboard_init()
         if (inb(PORT_PS2_STATUS) & 0x01) {
             inb(PORT_PS2_DATA);
             max = 0x2000;
-            }
         }
+    }
 
     // Due to timer issues, and if the IPS setting is > 15000000,
     // the incoming keys might not be flushed here. That will
@@ -193,7 +193,7 @@ dequeue_key(u8 *scan_code, u8 *ascii_code, u8 incr)
             break;
         if (!incr)
             return 0;
-        nop();
+        cpu_relax();
     }
 
     *ascii_code = GET_FARVAR(SEG_BDA, *(u8*)(buffer_head+0x400+0));
index dff1a1166733de3a8defe2a9f95b2fb714cd0ac2..344bcbc19ef166113c55b747ab5d05f11b4ddac2 100644 (file)
@@ -32,6 +32,11 @@ static inline void irq_restore(unsigned long flags)
     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
 }
 
+static inline void cpu_relax(void)
+{
+    asm volatile("rep ; nop": : :"memory");
+}
+
 static inline void nop(void)
 {
     asm volatile("nop");