[SeaBIOS] [PATCH] PIC code cleanups.

Kevin O'Connor kevin at koconnor.net
Sun Jul 14 21:13:01 CEST 2013


Preface PIC functions with a pic_ to provide a more consistent
naming.

Convert the irqmask code to a more consistent
pic_irqmask_read/write/mask form.

Move code from pic.h to pic.c.

Signed-off-by: Kevin O'Connor <kevin at koconnor.net>
---
 src/clock.c   |  6 ++---
 src/csm.c     |  7 +++---
 src/disk.c    |  4 +--
 src/floppy.c  |  4 +--
 src/misc.c    |  2 +-
 src/pic.c     | 79 +++++++++++++++++++++++++++++++++++++++++-----------------
 src/pic.h     | 80 +++++++++++------------------------------------------------
 src/ps2port.c |  6 ++---
 src/resume.c  |  4 +--
 src/system.c  |  4 +--
 10 files changed, 90 insertions(+), 106 deletions(-)

diff --git a/src/clock.c b/src/clock.c
index 5926a07..2eedab8 100644
--- a/src/clock.c
+++ b/src/clock.c
@@ -9,7 +9,7 @@
 #include "util.h" // debug_enter
 #include "disk.h" // floppy_tick
 #include "cmos.h" // inb_cmos
-#include "pic.h" // eoi_pic1
+#include "pic.h" // pic_eoi1
 #include "bregs.h" // struct bregs
 #include "biosvar.h" // GET_GLOBAL
 #include "usb-hid.h" // usb_check_event
@@ -583,7 +583,7 @@ handle_08(void)
     br.flags = F_IF;
     call16_int(0x1c, &br);
 
-    eoi_pic1();
+    pic_eoi1();
 }
 
 
@@ -746,5 +746,5 @@ handle_70(void)
     }
 
 done:
-    eoi_pic2();
+    pic_eoi2();
 }
diff --git a/src/csm.c b/src/csm.c
index 4336e16..2886bba 100644
--- a/src/csm.c
+++ b/src/csm.c
@@ -30,10 +30,11 @@ EFI_COMPATIBILITY16_TABLE csm_compat_table VARFSEG __aligned(16) = {
     .AcpiRsdPtrPointer = (u32)&csm_rsdp,
 };
 
-
 EFI_TO_COMPATIBILITY16_INIT_TABLE *csm_init_table;
 EFI_TO_COMPATIBILITY16_BOOT_TABLE *csm_boot_table;
 
+static u16 PICMask = PIC_IRQMASK_DEFAULT;
+
 extern void __csm_return(struct bregs *regs) __noreturn;
 
 static void
@@ -41,7 +42,7 @@ csm_return(struct bregs *regs)
 {
     dprintf(3, "handle_csm returning AX=%04x\n", regs->ax);
 
-    pic_save_mask();
+    PICMask = pic_irqmask_read();
     __csm_return(regs);
 }
 
@@ -271,7 +272,7 @@ handle_csm(struct bregs *regs)
 
     dprintf(3, "handle_csm regs %p AX=%04x\n", regs, regs->ax);
 
-    pic_restore_mask();
+    pic_irqmask_write(PICMask);
 
     switch(regs->ax) {
     case 0000: handle_csm_0000(regs); break;
diff --git a/src/disk.c b/src/disk.c
index 0291fe3..975d053 100644
--- a/src/disk.c
+++ b/src/disk.c
@@ -9,7 +9,7 @@
 #include "biosvar.h" // SET_BDA
 #include "config.h" // CONFIG_*
 #include "util.h" // debug_enter
-#include "pic.h" // eoi_pic2
+#include "pic.h" // pic_eoi2
 #include "bregs.h" // struct bregs
 #include "pci.h" // pci_bdf_to_bus
 #include "ata.h" // ATA_CB_DC
@@ -889,7 +889,7 @@ handle_76(void)
 {
     debug_isr(DEBUG_ISR_76);
     SET_BDA(disk_interrupt_flag, 0xff);
-    eoi_pic2();
+    pic_eoi2();
 }
 
 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
diff --git a/src/floppy.c b/src/floppy.c
index 62802ba..83dfaf8 100644
--- a/src/floppy.c
+++ b/src/floppy.c
@@ -11,7 +11,7 @@
 #include "biosvar.h" // SET_BDA
 #include "util.h" // dprintf
 #include "cmos.h" // inb_cmos
-#include "pic.h" // eoi_pic1
+#include "pic.h" // pic_eoi1
 #include "bregs.h" // struct bregs
 #include "boot.h" // boot_add_floppy
 #include "pci.h" // pci_to_bdf
@@ -679,7 +679,7 @@ handle_0e(void)
     u8 frs = GET_BDA(floppy_recalibration_status);
     SET_BDA(floppy_recalibration_status, frs | FRS_IRQ);
 
-    eoi_pic1();
+    pic_eoi1();
 }
 
 // Called from int08 handler.
diff --git a/src/misc.c b/src/misc.c
index def31f8..c84cac8 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -71,7 +71,7 @@ handle_75(void)
     // clear irq13
     outb(0, PORT_MATH_CLEAR);
     // clear interrupt
-    eoi_pic2();
+    pic_eoi2();
     // legacy nmi call
     struct bregs br;
     memset(&br, 0, sizeof(br));
diff --git a/src/pic.c b/src/pic.c
index 3e96bb3..9a3ea0b 100644
--- a/src/pic.c
+++ b/src/pic.c
@@ -5,12 +5,34 @@
 //
 // This file may be distributed under the terms of the GNU LGPLv3 license.
 
-#include "pic.h" // get_pic1_isr
+#include "pic.h" // pic_*
 #include "util.h" // dprintf
 #include "config.h" // CONFIG_*
+#include "biosvar.h" // SET_IVT
+
+u16
+pic_irqmask_read(void)
+{
+    return inb(PORT_PIC1_DATA) | (inb(PORT_PIC2_DATA) << 8);
+}
+
+void
+pic_irqmask_write(u16 mask)
+{
+    outb(mask, PORT_PIC1_DATA);
+    outb(mask >> 8, PORT_PIC2_DATA);
+}
 
 void
-set_pics(u8 irq0, u8 irq8)
+pic_irqmask_mask(u16 off, u16 on)
+{
+    u8 pic1off = off, pic1on = on, pic2off = off>>8, pic2on = on>>8;
+    outb((inb(PORT_PIC1_DATA) & ~pic1off) | pic1on, PORT_PIC1_DATA);
+    outb((inb(PORT_PIC2_DATA) & ~pic2off) | pic2on, PORT_PIC2_DATA);
+}
+
+void
+pic_reset(u8 irq0, u8 irq8)
 {
     // Send ICW1 (select OCW1 + will send ICW4)
     outb(0x11, PORT_PIC1_CMD);
@@ -25,44 +47,55 @@ set_pics(u8 irq0, u8 irq8)
     outb(0x01, PORT_PIC1_DATA);
     outb(0x01, PORT_PIC2_DATA);
     // Mask all irqs (except cascaded PIC2 irq)
-    outb(~PIC1_IRQ2, PORT_PIC1_DATA);
-    outb(~0, PORT_PIC2_DATA);
+    pic_irqmask_write(PIC_IRQMASK_DEFAULT);
 }
 
 void
 pic_setup(void)
 {
     dprintf(3, "init pic\n");
-    set_pics(BIOS_HWIRQ0_VECTOR, BIOS_HWIRQ8_VECTOR);
+    pic_reset(BIOS_HWIRQ0_VECTOR, BIOS_HWIRQ8_VECTOR);
 }
 
-// Handler for otherwise unused hardware irqs.
-void VISIBLE16
-handle_hwpic1(struct bregs *regs)
+void
+enable_hwirq(int hwirq, struct segoff_s func)
 {
-    dprintf(DEBUG_ISR_hwpic1, "handle_hwpic1 irq=%x\n", get_pic1_isr());
-    eoi_pic1();
+    pic_irqmask_mask(1 << hwirq, 0);
+    int vector;
+    if (hwirq < 8)
+        vector = BIOS_HWIRQ0_VECTOR + hwirq;
+    else
+        vector = BIOS_HWIRQ8_VECTOR + hwirq - 8;
+    SET_IVT(vector, func);
 }
 
-void VISIBLE16
-handle_hwpic2(struct bregs *regs)
+static u8
+pic_isr1_read(void)
 {
-    dprintf(DEBUG_ISR_hwpic2, "handle_hwpic2 irq=%x\n", get_pic2_isr());
-    eoi_pic2();
+    // 0x0b == select OCW1 + read ISR
+    outb(0x0b, PORT_PIC1_CMD);
+    return inb(PORT_PIC1_CMD);
 }
 
-u8 saved_pic_mask[2] = { ~PIC1_IRQ2, ~0 };
+static u8
+pic_isr2_read(void)
+{
+    // 0x0b == select OCW1 + read ISR
+    outb(0x0b, PORT_PIC2_CMD);
+    return inb(PORT_PIC2_CMD);
+}
 
-void
-pic_save_mask(void)
+// Handler for otherwise unused hardware irqs.
+void VISIBLE16
+handle_hwpic1(struct bregs *regs)
 {
-    saved_pic_mask[0] = inb(PORT_PIC1_DATA);
-    saved_pic_mask[1] = inb(PORT_PIC2_DATA);
+    dprintf(DEBUG_ISR_hwpic1, "handle_hwpic1 irq=%x\n", pic_isr1_read());
+    pic_eoi1();
 }
 
-void
-pic_restore_mask(void)
+void VISIBLE16
+handle_hwpic2(struct bregs *regs)
 {
-    outb(saved_pic_mask[0], PORT_PIC1_DATA);
-    outb(saved_pic_mask[1], PORT_PIC2_DATA);
+    dprintf(DEBUG_ISR_hwpic2, "handle_hwpic2 irq=%x\n", pic_isr2_read());
+    pic_eoi2();
 }
diff --git a/src/pic.h b/src/pic.h
index 92cb870..19aecba 100644
--- a/src/pic.h
+++ b/src/pic.h
@@ -8,7 +8,7 @@
 #define __PIC_H
 
 #include "ioport.h" // PORT_PIC*
-#include "biosvar.h" // SET_IVT
+#include "farptr.h" // struct segoff_s
 
 // PORT_PIC1 bitdefs
 #define PIC1_IRQ0  (1<<0)
@@ -17,86 +17,36 @@
 #define PIC1_IRQ5  (1<<5)
 #define PIC1_IRQ6  (1<<6)
 // PORT_PIC2 bitdefs
-#define PIC2_IRQ8  (1<<0)
-#define PIC2_IRQ12 (1<<4)
-#define PIC2_IRQ13 (1<<5)
-#define PIC2_IRQ14 (1<<6)
+#define PIC2_IRQ8  (1<<8)
+#define PIC2_IRQ12 (1<<12)
+#define PIC2_IRQ13 (1<<13)
+#define PIC2_IRQ14 (1<<14)
+
+#define PIC_IRQMASK_DEFAULT ((u16)~PIC1_IRQ2)
 
 #define BIOS_HWIRQ0_VECTOR 0x08
 #define BIOS_HWIRQ8_VECTOR 0x70
 
 static inline void
-eoi_pic1(void)
+pic_eoi1(void)
 {
     // Send eoi (select OCW2 + eoi)
     outb(0x20, PORT_PIC1_CMD);
 }
 
 static inline void
-eoi_pic2(void)
+pic_eoi2(void)
 {
     // Send eoi (select OCW2 + eoi)
     outb(0x20, PORT_PIC2_CMD);
-    eoi_pic1();
-}
-
-static inline void
-unmask_pic1(u8 irq)
-{
-    outb(inb(PORT_PIC1_DATA) & ~irq, PORT_PIC1_DATA);
-}
-
-static inline void
-unmask_pic2(u8 irq)
-{
-    outb(inb(PORT_PIC2_DATA) & ~irq, PORT_PIC2_DATA);
-}
-
-static inline void
-mask_pic1(u8 irq)
-{
-    outb(inb(PORT_PIC1_DATA) | irq, PORT_PIC1_DATA);
-}
-
-static inline void
-mask_pic2(u8 irq)
-{
-    outb(inb(PORT_PIC2_DATA) | irq, PORT_PIC2_DATA);
-}
-
-static inline u8
-get_pic1_isr(void)
-{
-    // 0x0b == select OCW1 + read ISR
-    outb(0x0b, PORT_PIC1_CMD);
-    return inb(PORT_PIC1_CMD);
-}
-
-static inline u8
-get_pic2_isr(void)
-{
-    // 0x0b == select OCW1 + read ISR
-    outb(0x0b, PORT_PIC2_CMD);
-    return inb(PORT_PIC2_CMD);
-}
-
-static inline void
-enable_hwirq(int hwirq, struct segoff_s func)
-{
-    int vector;
-    if (hwirq < 8) {
-        unmask_pic1(1 << hwirq);
-        vector = BIOS_HWIRQ0_VECTOR + hwirq;
-    } else {
-        unmask_pic2(1 << (hwirq - 8));
-        vector = BIOS_HWIRQ8_VECTOR + hwirq - 8;
-    }
-    SET_IVT(vector, func);
+    pic_eoi1();
 }
 
-void set_pics(u8 irq0, u8 irq8);
+u16 pic_irqmask_read(void);
+void pic_irqmask_write(u16 mask);
+void pic_irqmask_mask(u16 off, u16 on);
+void pic_reset(u8 irq0, u8 irq8);
 void pic_setup(void);
-void pic_save_mask(void);
-void pic_restore_mask(void);
+void enable_hwirq(int hwirq, struct segoff_s func);
 
 #endif // pic.h
diff --git a/src/ps2port.c b/src/ps2port.c
index 9b760fd..cfcd5eb 100644
--- a/src/ps2port.c
+++ b/src/ps2port.c
@@ -9,7 +9,7 @@
 #include "util.h" // dprintf
 #include "biosvar.h" // GET_LOW
 #include "ps2port.h" // ps2_kbd_command
-#include "pic.h" // eoi_pic1
+#include "pic.h" // pic_eoi1
 
 
 /****************************************************************
@@ -378,7 +378,7 @@ handle_74(void)
     process_mouse(v);
 
 done:
-    eoi_pic2();
+    pic_eoi2();
 }
 
 // INT09h : Keyboard Hardware Service Entry Point
@@ -408,7 +408,7 @@ handle_09(void)
     i8042_command(I8042_CMD_KBD_ENABLE, NULL);
 
 done:
-    eoi_pic1();
+    pic_eoi1();
 }
 
 
diff --git a/src/resume.c b/src/resume.c
index 9466480..48e1bd8 100644
--- a/src/resume.c
+++ b/src/resume.c
@@ -6,7 +6,7 @@
 
 #include "util.h" // dprintf
 #include "ioport.h" // outb
-#include "pic.h" // eoi_pic2
+#include "pic.h" // pic_eoi2
 #include "biosvar.h" // struct bios_data_area_s
 #include "bregs.h" // struct bregs
 #include "acpi.h" // find_resume_vector
@@ -49,7 +49,7 @@ handle_resume(void)
 
     case 0x05:
         // flush keyboard (issue EOI) and jump via 40h:0067h
-        eoi_pic2();
+        pic_eoi2();
         // NO BREAK
     case 0x0a:
 #define BDA_JUMP (((struct bios_data_area_s *)0)->jump)
diff --git a/src/system.c b/src/system.c
index 7b481a8..3c815b3 100644
--- a/src/system.c
+++ b/src/system.c
@@ -9,7 +9,7 @@
 #include "biosvar.h" // BIOS_CONFIG_TABLE
 #include "ioport.h" // inb
 #include "memmap.h" // E820_RAM
-#include "pic.h" // eoi_pic2
+#include "pic.h" // pic_reset
 #include "bregs.h" // struct bregs
 
 // Use PS2 System Control port A to set A20 enable
@@ -191,7 +191,7 @@ handle_1589(struct bregs *regs)
 {
     set_a20(1);
 
-    set_pics(regs->bl, regs->bh);
+    pic_reset(regs->bl, regs->bh);
 
     u64 *gdt_far = (void*)(regs->si + 0);
     u16 gdt_seg = regs->es;
-- 
1.7.11.7




More information about the SeaBIOS mailing list