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 KB_SET_A20 0xdf /* enable A20,
16 enable output buffer full interrupt
19 #define KB_UNSET_A20 0xdd /* enable A20,
20 enable output buffer full interrupt
24 #define SCP_A 0x92 /* System Control Port A */
26 enum { Disable_A20 = 0x2400, Enable_A20 = 0x2401, Query_A20_Status = 0x2402,
27 Query_A20_Support = 0x2403 };
36 #define A20_MAX_RETRIES 32
37 #define A20_INT15_RETRIES 32
38 #define A20_KBC_RETRIES (2^21)
39 #define A20_SCPA_RETRIES (2^21)
42 * Drain keyboard controller
44 static void empty_8042 ( void ) {
47 time = currticks() + TICKS_PER_SEC; /* max wait of 1 second */
48 while ( ( inb ( K_CMD ) & ( K_IBUF_FUL | K_OBUF_FUL ) ) &&
49 currticks() < time ) {
51 ( void ) inb ( K_RDWR );
57 * Fast test to see if gate A20 is already set
59 * @v retries Number of times to retry before giving up
60 * @ret set Gate A20 is set
62 static int gateA20_is_set ( int retries ) {
63 static uint32_t test_pattern = 0xdeadbeef;
64 physaddr_t test_pattern_phys = virt_to_phys ( &test_pattern );
65 physaddr_t verify_pattern_phys = ( test_pattern_phys ^ 0x100000 );
66 userptr_t verify_pattern_user = phys_to_user ( verify_pattern_phys );
67 uint32_t verify_pattern;
70 /* Check for difference */
71 copy_from_user ( &verify_pattern, verify_pattern_user, 0,
72 sizeof ( verify_pattern ) );
73 if ( verify_pattern != test_pattern )
76 /* Avoid false negatives */
81 /* Always retry at least once, to avoid false negatives */
82 } while ( retries-- >= 0 );
84 /* Pattern matched every time; gate A20 is not set */
89 * Gate A20 for high memory
91 * Note that this function gets called as part of the return path from
92 * librm's real_call, which is used to make the int15 call if librm is
93 * being used. To avoid an infinite recursion, we make gateA20_set
94 * return immediately if it is already part of the call stack.
96 void gateA20_set ( void ) {
97 static char reentry_guard = 0;
98 static int a20_method = A20_UNKNOWN;
99 unsigned int discard_a;
103 /* Avoid potential infinite recursion */
108 /* Fast check to see if gate A20 is already enabled */
109 if ( gateA20_is_set ( 0 ) )
112 for ( ; retries < A20_MAX_RETRIES ; retries++ ) {
113 switch ( a20_method ) {
116 /* Try INT 15 method */
117 __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
119 : "a" ( Enable_A20 ) );
120 if ( gateA20_is_set ( A20_INT15_RETRIES ) ) {
121 DBG ( "Enabled gate A20 using BIOS\n" );
122 a20_method = A20_INT15;
127 /* Try keyboard controller method */
129 outb ( KC_CMD_WOUT, K_CMD );
131 outb ( KB_SET_A20, K_RDWR );
133 if ( gateA20_is_set ( A20_KBC_RETRIES ) ) {
134 DBG ( "Enabled gate A20 using "
135 "keyboard controller\n" );
136 a20_method = A20_KBC;
141 /* Try "Fast gate A20" method */
142 scp_a = inb ( SCP_A );
143 scp_a &= ~0x01; /* Avoid triggering a reset */
144 scp_a |= 0x02; /* Enable A20 */
146 outb ( scp_a, SCP_A );
148 if ( gateA20_is_set ( A20_SCPA_RETRIES ) ) {
149 DBG ( "Enabled gate A20 using "
151 a20_method = A20_SCPA;
157 /* Better to die now than corrupt memory later */
158 printf ( "FATAL: Gate A20 stuck\n" );
163 DBG ( "%d attempts were required to enable A20\n",
168 void gateA20_unset ( void ) {
169 /* Not currently implemented */