4 #include <gpxe/timer.h>
6 #define K_RDWR 0x60 /* keyboard data & cmds (read/write) */
7 #define K_STATUS 0x64 /* keyboard status */
8 #define K_CMD 0x64 /* keybd ctlr command (write-only) */
10 #define K_OBUF_FUL 0x01 /* output buffer full */
11 #define K_IBUF_FUL 0x02 /* input buffer full */
13 #define KC_CMD_WIN 0xd0 /* read output port */
14 #define KC_CMD_WOUT 0xd1 /* write output port */
15 #define KC_CMD_NULL 0xff /* null command ("pulse nothing") */
16 #define KB_SET_A20 0xdf /* enable A20,
17 enable output buffer full interrupt
20 #define KB_UNSET_A20 0xdd /* enable A20,
21 enable output buffer full interrupt
25 #define SCP_A 0x92 /* System Control Port A */
27 enum { Disable_A20 = 0x2400, Enable_A20 = 0x2401, Query_A20_Status = 0x2402,
28 Query_A20_Support = 0x2403 };
37 #define A20_MAX_RETRIES 32
38 #define A20_INT15_RETRIES 32
39 #define A20_KBC_RETRIES (2^21)
40 #define A20_SCPA_RETRIES (2^21)
43 * Drain keyboard controller
45 static void empty_8042 ( void ) {
48 time = currticks() + TICKS_PER_SEC; /* max wait of 1 second */
49 while ( ( inb ( K_CMD ) & ( K_IBUF_FUL | K_OBUF_FUL ) ) &&
50 currticks() < time ) {
52 ( void ) inb ( K_RDWR );
58 * Fast test to see if gate A20 is already set
60 * @v retries Number of times to retry before giving up
61 * @ret set Gate A20 is set
63 static int gateA20_is_set ( int retries ) {
64 static uint32_t test_pattern = 0xdeadbeef;
65 physaddr_t test_pattern_phys = virt_to_phys ( &test_pattern );
66 physaddr_t verify_pattern_phys = ( test_pattern_phys ^ 0x100000 );
67 userptr_t verify_pattern_user = phys_to_user ( verify_pattern_phys );
68 uint32_t verify_pattern;
71 /* Check for difference */
72 copy_from_user ( &verify_pattern, verify_pattern_user, 0,
73 sizeof ( verify_pattern ) );
74 if ( verify_pattern != test_pattern )
77 /* Avoid false negatives */
82 /* Always retry at least once, to avoid false negatives */
83 } while ( retries-- >= 0 );
85 /* Pattern matched every time; gate A20 is not set */
90 * Gate A20 for high memory
92 * Note that this function gets called as part of the return path from
93 * librm's real_call, which is used to make the int15 call if librm is
94 * being used. To avoid an infinite recursion, we make gateA20_set
95 * return immediately if it is already part of the call stack.
97 void gateA20_set ( void ) {
98 static char reentry_guard = 0;
99 static int a20_method = A20_UNKNOWN;
100 unsigned int discard_a;
104 /* Avoid potential infinite recursion */
109 /* Fast check to see if gate A20 is already enabled */
110 if ( gateA20_is_set ( 0 ) )
113 for ( ; retries < A20_MAX_RETRIES ; retries++ ) {
114 switch ( a20_method ) {
117 /* Try INT 15 method */
118 __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
120 : "a" ( Enable_A20 ) );
121 if ( gateA20_is_set ( A20_INT15_RETRIES ) ) {
122 DBG ( "Enabled gate A20 using BIOS\n" );
123 a20_method = A20_INT15;
128 /* Try keyboard controller method */
130 outb ( KC_CMD_WOUT, K_CMD );
132 outb ( KB_SET_A20, K_RDWR );
134 outb ( KC_CMD_NULL, K_CMD );
136 if ( gateA20_is_set ( A20_KBC_RETRIES ) ) {
137 DBG ( "Enabled gate A20 using "
138 "keyboard controller\n" );
139 a20_method = A20_KBC;
144 /* Try "Fast gate A20" method */
145 scp_a = inb ( SCP_A );
146 scp_a &= ~0x01; /* Avoid triggering a reset */
147 scp_a |= 0x02; /* Enable A20 */
149 outb ( scp_a, SCP_A );
151 if ( gateA20_is_set ( A20_SCPA_RETRIES ) ) {
152 DBG ( "Enabled gate A20 using "
154 a20_method = A20_SCPA;
160 /* Better to die now than corrupt memory later */
161 printf ( "FATAL: Gate A20 stuck\n" );
166 DBG ( "%d attempts were required to enable A20\n",
171 void gateA20_unset ( void ) {
172 /* Not currently implemented */