1 FILE_LICENCE ( GPL2_OR_LATER );
7 #include <gpxe/timer.h>
9 #define K_RDWR 0x60 /* keyboard data & cmds (read/write) */
10 #define K_STATUS 0x64 /* keyboard status */
11 #define K_CMD 0x64 /* keybd ctlr command (write-only) */
13 #define K_OBUF_FUL 0x01 /* output buffer full */
14 #define K_IBUF_FUL 0x02 /* input buffer full */
16 #define KC_CMD_WIN 0xd0 /* read output port */
17 #define KC_CMD_WOUT 0xd1 /* write output port */
18 #define KC_CMD_NULL 0xff /* null command ("pulse nothing") */
19 #define KB_SET_A20 0xdf /* enable A20,
20 enable output buffer full interrupt
23 #define KB_UNSET_A20 0xdd /* enable A20,
24 enable output buffer full interrupt
28 #define SCP_A 0x92 /* System Control Port A */
30 enum { Disable_A20 = 0x2400, Enable_A20 = 0x2401, Query_A20_Status = 0x2402,
31 Query_A20_Support = 0x2403 };
40 #define A20_MAX_RETRIES 32
41 #define A20_INT15_RETRIES 32
42 #define A20_KBC_RETRIES (2^21)
43 #define A20_SCPA_RETRIES (2^21)
46 * Drain keyboard controller
48 static void empty_8042 ( void ) {
51 time = currticks() + TICKS_PER_SEC; /* max wait of 1 second */
52 while ( ( inb ( K_CMD ) & ( K_IBUF_FUL | K_OBUF_FUL ) ) &&
53 currticks() < time ) {
55 ( void ) inb_p ( K_RDWR );
61 * Fast test to see if gate A20 is already set
63 * @v retries Number of times to retry before giving up
64 * @ret set Gate A20 is set
66 static int gateA20_is_set ( int retries ) {
67 static uint32_t test_pattern = 0xdeadbeef;
68 physaddr_t test_pattern_phys = virt_to_phys ( &test_pattern );
69 physaddr_t verify_pattern_phys = ( test_pattern_phys ^ 0x100000 );
70 userptr_t verify_pattern_user = phys_to_user ( verify_pattern_phys );
71 uint32_t verify_pattern;
74 /* Check for difference */
75 copy_from_user ( &verify_pattern, verify_pattern_user, 0,
76 sizeof ( verify_pattern ) );
77 if ( verify_pattern != test_pattern )
80 /* Avoid false negatives */
85 /* Always retry at least once, to avoid false negatives */
86 } while ( retries-- >= 0 );
88 /* Pattern matched every time; gate A20 is not set */
93 * Gate A20 for high memory
95 * Note that this function gets called as part of the return path from
96 * librm's real_call, which is used to make the int15 call if librm is
97 * being used. To avoid an infinite recursion, we make gateA20_set
98 * return immediately if it is already part of the call stack.
100 void gateA20_set ( void ) {
101 static char reentry_guard = 0;
102 static int a20_method = A20_UNKNOWN;
103 unsigned int discard_a;
107 /* Avoid potential infinite recursion */
112 /* Fast check to see if gate A20 is already enabled */
113 if ( gateA20_is_set ( 0 ) )
116 for ( ; retries < A20_MAX_RETRIES ; retries++ ) {
117 switch ( a20_method ) {
120 /* Try INT 15 method */
121 __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
123 : "a" ( Enable_A20 ) );
124 if ( gateA20_is_set ( A20_INT15_RETRIES ) ) {
125 DBG ( "Enabled gate A20 using BIOS\n" );
126 a20_method = A20_INT15;
131 /* Try keyboard controller method */
133 outb ( KC_CMD_WOUT, K_CMD );
135 outb ( KB_SET_A20, K_RDWR );
137 outb ( KC_CMD_NULL, K_CMD );
139 if ( gateA20_is_set ( A20_KBC_RETRIES ) ) {
140 DBG ( "Enabled gate A20 using "
141 "keyboard controller\n" );
142 a20_method = A20_KBC;
147 /* Try "Fast gate A20" method */
148 scp_a = inb ( SCP_A );
149 scp_a &= ~0x01; /* Avoid triggering a reset */
150 scp_a |= 0x02; /* Enable A20 */
152 outb ( scp_a, SCP_A );
154 if ( gateA20_is_set ( A20_SCPA_RETRIES ) ) {
155 DBG ( "Enabled gate A20 using "
157 a20_method = A20_SCPA;
163 /* Better to die now than corrupt memory later */
164 printf ( "FATAL: Gate A20 stuck\n" );
169 DBG ( "%d attempts were required to enable A20\n",
174 void gateA20_unset ( void ) {
175 /* Not currently implemented */