]> Pileus Git - ~andy/linux/blob - arch/x86_64/kernel/acpi/wakeup.S
[PATCH] x86-64: wakeup.S misc cleanups
[~andy/linux] / arch / x86_64 / kernel / acpi / wakeup.S
1 .text
2 #include <linux/linkage.h>
3 #include <asm/segment.h>
4 #include <asm/page.h>
5 #include <asm/msr.h>
6
7 # Copyright 2003 Pavel Machek <pavel@suse.cz>, distribute under GPLv2
8 #
9 # wakeup_code runs in real mode, and at unknown address (determined at run-time).
10 # Therefore it must only use relative jumps/calls. 
11 #
12 # Do we need to deal with A20? It is okay: ACPI specs says A20 must be enabled
13 #
14 # If physical address of wakeup_code is 0x12345, BIOS should call us with
15 # cs = 0x1234, eip = 0x05
16 #
17
18
19 ALIGN
20         .align  16
21 ENTRY(wakeup_start)
22 wakeup_code:
23         wakeup_code_start = .
24         .code16
25
26 # Running in *copy* of this code, somewhere in low 1MB.
27
28         movb    $0xa1, %al      ;  outb %al, $0x80
29         cli
30         cld
31         # setup data segment
32         movw    %cs, %ax
33         movw    %ax, %ds                # Make ds:0 point to wakeup_start
34         movw    %ax, %ss
35                                         # Private stack is needed for ASUS board
36         mov     $(wakeup_stack - wakeup_code), %sp
37
38         pushl   $0                      # Kill any dangerous flags
39         popfl
40
41         movl    real_magic - wakeup_code, %eax
42         cmpl    $0x12345678, %eax
43         jne     bogus_real_magic
44
45         testl   $1, video_flags - wakeup_code
46         jz      1f
47         lcall   $0xc000,$3
48         movw    %cs, %ax
49         movw    %ax, %ds                # Bios might have played with that
50         movw    %ax, %ss
51 1:
52
53         testl   $2, video_flags - wakeup_code
54         jz      1f
55         mov     video_mode - wakeup_code, %ax
56         call    mode_seta
57 1:
58
59         movw    $0xb800, %ax
60         movw    %ax,%fs
61         movw    $0x0e00 + 'L', %fs:(0x10)
62
63         movb    $0xa2, %al      ;  outb %al, $0x80
64         
65         lidt    %ds:idt_48a - wakeup_code
66         xorl    %eax, %eax
67         movw    %ds, %ax                        # (Convert %ds:gdt to a linear ptr)
68         shll    $4, %eax
69         addl    $(gdta - wakeup_code), %eax
70         movl    %eax, gdt_48a +2 - wakeup_code
71         lgdtl   %ds:gdt_48a - wakeup_code       # load gdt with whatever is
72                                                 # appropriate
73
74         movl    $1, %eax                        # protected mode (PE) bit
75         lmsw    %ax                             # This is it!
76         jmp     1f
77 1:
78
79         ljmpl   *(wakeup_32_vector - wakeup_code)
80
81         .balign 4
82 wakeup_32_vector:
83         .long   wakeup_32 - __START_KERNEL_map
84         .word   __KERNEL32_CS, 0
85
86         .code32
87 wakeup_32:
88 # Running in this code, but at low address; paging is not yet turned on.
89         movb    $0xa5, %al      ;  outb %al, $0x80
90
91         /* Check if extended functions are implemented */               
92         movl    $0x80000000, %eax
93         cpuid
94         cmpl    $0x80000000, %eax
95         jbe     bogus_cpu
96         wbinvd
97         mov     $0x80000001, %eax
98         cpuid
99         btl     $29, %edx
100         jnc     bogus_cpu
101         movl    %edx,%edi
102         
103         movl    $__KERNEL_DS, %eax
104         movl    %eax, %ds
105
106         movl    saved_magic - __START_KERNEL_map, %eax
107         cmpl    $0x9abcdef0, %eax
108         jne     bogus_32_magic
109
110         movw    $0x0e00 + 'i', %ds:(0xb8012)
111         movb    $0xa8, %al      ;  outb %al, $0x80;
112
113         /*
114          * Prepare for entering 64bits mode
115          */
116
117         /* Enable PAE */
118         xorl    %eax, %eax
119         btsl    $5, %eax
120         movl    %eax, %cr4
121
122         /* Setup early boot stage 4 level pagetables */
123         movl    $(wakeup_level4_pgt - __START_KERNEL_map), %eax
124         movl    %eax, %cr3
125
126         /* Enable Long Mode */
127         xorl    %eax, %eax
128         btsl    $_EFER_LME, %eax
129
130         /* No Execute supported? */
131         btl     $20,%edi
132         jnc     1f
133         btsl    $_EFER_NX, %eax
134                                 
135         /* Make changes effective */
136 1:      movl    $MSR_EFER, %ecx
137         xorl    %edx, %edx
138         wrmsr
139
140         xorl    %eax, %eax
141         btsl    $31, %eax                       /* Enable paging and in turn activate Long Mode */
142         btsl    $0, %eax                        /* Enable protected mode */
143
144         /* Make changes effective */
145         movl    %eax, %cr0
146
147         /* At this point:
148                 CR4.PAE must be 1
149                 CS.L must be 0
150                 CR3 must point to PML4
151                 Next instruction must be a branch
152                 This must be on identity-mapped page
153         */
154         /*
155          * At this point we're in long mode but in 32bit compatibility mode
156          * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
157          * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we load
158          * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
159          */
160
161         /* Finally jump in 64bit mode */
162         ljmp    *(wakeup_long64_vector - __START_KERNEL_map)
163
164         .balign 4
165 wakeup_long64_vector:
166         .long   wakeup_long64 - __START_KERNEL_map
167         .word   __KERNEL_CS, 0
168
169 .code64
170
171         /* Hooray, we are in Long 64-bit mode (but still running in
172          * low memory)
173          */
174 wakeup_long64:
175         /*
176          * We must switch to a new descriptor in kernel space for the GDT
177          * because soon the kernel won't have access anymore to the userspace
178          * addresses where we're currently running on. We have to do that here
179          * because in 32bit we couldn't load a 64bit linear address.
180          */
181         lgdt    cpu_gdt_descr - __START_KERNEL_map
182
183         movw    $0x0e00 + 'n', %ds:(0xb8014)
184         movb    $0xa9, %al      ;  outb %al, $0x80
185
186         movw    $0x0e00 + 'u', %ds:(0xb8016)
187         
188         nop
189         nop
190         movw    $__KERNEL_DS, %ax
191         movw    %ax, %ss        
192         movw    %ax, %ds
193         movw    %ax, %es
194         movw    %ax, %fs
195         movw    %ax, %gs
196         movq    saved_rsp, %rsp
197
198         movw    $0x0e00 + 'x', %ds:(0xb8018)
199         movq    saved_rbx, %rbx
200         movq    saved_rdi, %rdi
201         movq    saved_rsi, %rsi
202         movq    saved_rbp, %rbp
203
204         movw    $0x0e00 + '!', %ds:(0xb801a)
205         movq    saved_rip, %rax
206         jmp     *%rax
207
208 .code32
209
210         .align  64      
211 gdta:
212         /* Its good to keep gdt in sync with one in trampoline.S */
213         .word   0, 0, 0, 0                      # dummy
214         /* ??? Why I need the accessed bit set in order for this to work? */
215         .quad   0x00cf9b000000ffff              # __KERNEL32_CS
216         .quad   0x00af9b000000ffff              # __KERNEL_CS
217         .quad   0x00cf93000000ffff              # __KERNEL_DS
218
219 idt_48a:
220         .word   0                               # idt limit = 0
221         .word   0, 0                            # idt base = 0L
222
223 gdt_48a:
224         .word   0x800                           # gdt limit=2048,
225                                                 #  256 GDT entries
226         .word   0, 0                            # gdt base (filled in later)
227         
228         
229 real_magic:     .quad 0
230 video_mode:     .quad 0
231 video_flags:    .quad 0
232
233 bogus_real_magic:
234         movb    $0xba,%al       ;  outb %al,$0x80
235         jmp bogus_real_magic
236
237 bogus_32_magic:
238         movb    $0xb3,%al       ;  outb %al,$0x80
239         jmp bogus_32_magic
240
241 bogus_cpu:
242         movb    $0xbc,%al       ;  outb %al,$0x80
243         jmp bogus_cpu
244
245         
246 /* This code uses an extended set of video mode numbers. These include:
247  * Aliases for standard modes
248  *      NORMAL_VGA (-1)
249  *      EXTENDED_VGA (-2)
250  *      ASK_VGA (-3)
251  * Video modes numbered by menu position -- NOT RECOMMENDED because of lack
252  * of compatibility when extending the table. These are between 0x00 and 0xff.
253  */
254 #define VIDEO_FIRST_MENU 0x0000
255
256 /* Standard BIOS video modes (BIOS number + 0x0100) */
257 #define VIDEO_FIRST_BIOS 0x0100
258
259 /* VESA BIOS video modes (VESA number + 0x0200) */
260 #define VIDEO_FIRST_VESA 0x0200
261
262 /* Video7 special modes (BIOS number + 0x0900) */
263 #define VIDEO_FIRST_V7 0x0900
264
265 # Setting of user mode (AX=mode ID) => CF=success
266 mode_seta:
267         movw    %ax, %bx
268 #if 0
269         cmpb    $0xff, %ah
270         jz      setalias
271
272         testb   $VIDEO_RECALC>>8, %ah
273         jnz     _setrec
274
275         cmpb    $VIDEO_FIRST_RESOLUTION>>8, %ah
276         jnc     setres
277         
278         cmpb    $VIDEO_FIRST_SPECIAL>>8, %ah
279         jz      setspc
280
281         cmpb    $VIDEO_FIRST_V7>>8, %ah
282         jz      setv7
283 #endif
284         
285         cmpb    $VIDEO_FIRST_VESA>>8, %ah
286         jnc     check_vesaa
287 #if 0   
288         orb     %ah, %ah
289         jz      setmenu
290 #endif
291         
292         decb    %ah
293 #       jz      setbios                           Add bios modes later
294
295 setbada:        clc
296         ret
297
298 check_vesaa:
299         subb    $VIDEO_FIRST_VESA>>8, %bh
300         orw     $0x4000, %bx                    # Use linear frame buffer
301         movw    $0x4f02, %ax                    # VESA BIOS mode set call
302         int     $0x10
303         cmpw    $0x004f, %ax                    # AL=4f if implemented
304         jnz     _setbada                                # AH=0 if OK
305
306         stc
307         ret
308
309 _setbada: jmp setbada
310
311 wakeup_stack_begin:     # Stack grows down
312
313 .org    0xff0
314 wakeup_stack:           # Just below end of page
315
316 ENTRY(wakeup_end)
317         
318 ##
319 # acpi_copy_wakeup_routine
320 #
321 # Copy the above routine to low memory.
322 #
323 # Parameters:
324 # %rdi: place to copy wakeup routine to
325 #
326 # Returned address is location of code in low memory (past data and stack)
327 #
328         .code64
329 ENTRY(acpi_copy_wakeup_routine)
330         pushq   %rax
331         pushq   %rdx
332
333         movl    saved_video_mode, %edx
334         movl    %edx, video_mode - wakeup_start (,%rdi)
335         movl    acpi_video_flags, %edx
336         movl    %edx, video_flags - wakeup_start (,%rdi)
337         movq    $0x12345678, real_magic - wakeup_start (,%rdi)
338         movq    $0x123456789abcdef0, %rdx
339         movq    %rdx, saved_magic
340
341         movl    saved_magic - __START_KERNEL_map, %eax
342         cmpl    $0x9abcdef0, %eax
343         jne     bogus_32_magic
344
345         # restore the regs we used
346         popq    %rdx
347         popq    %rax
348 ENTRY(do_suspend_lowlevel_s4bios)
349         ret
350
351         .align 2
352         .p2align 4,,15
353 .globl do_suspend_lowlevel
354         .type   do_suspend_lowlevel,@function
355 do_suspend_lowlevel:
356 .LFB5:
357         subq    $8, %rsp
358         xorl    %eax, %eax
359         call    save_processor_state
360
361         movq %rsp, saved_context_esp(%rip)
362         movq %rax, saved_context_eax(%rip)
363         movq %rbx, saved_context_ebx(%rip)
364         movq %rcx, saved_context_ecx(%rip)
365         movq %rdx, saved_context_edx(%rip)
366         movq %rbp, saved_context_ebp(%rip)
367         movq %rsi, saved_context_esi(%rip)
368         movq %rdi, saved_context_edi(%rip)
369         movq %r8,  saved_context_r08(%rip)
370         movq %r9,  saved_context_r09(%rip)
371         movq %r10, saved_context_r10(%rip)
372         movq %r11, saved_context_r11(%rip)
373         movq %r12, saved_context_r12(%rip)
374         movq %r13, saved_context_r13(%rip)
375         movq %r14, saved_context_r14(%rip)
376         movq %r15, saved_context_r15(%rip)
377         pushfq ; popq saved_context_eflags(%rip)
378
379         movq    $.L97, saved_rip(%rip)
380
381         movq %rsp,saved_rsp
382         movq %rbp,saved_rbp
383         movq %rbx,saved_rbx
384         movq %rdi,saved_rdi
385         movq %rsi,saved_rsi
386
387         addq    $8, %rsp
388         movl    $3, %edi
389         xorl    %eax, %eax
390         jmp     acpi_enter_sleep_state
391 .L97:
392         .p2align 4,,7
393 .L99:
394         .align 4
395         movl    $24, %eax
396         movw %ax, %ds
397         movq    saved_context+58(%rip), %rax
398         movq %rax, %cr4
399         movq    saved_context+50(%rip), %rax
400         movq %rax, %cr3
401         movq    saved_context+42(%rip), %rax
402         movq %rax, %cr2
403         movq    saved_context+34(%rip), %rax
404         movq %rax, %cr0
405         pushq saved_context_eflags(%rip) ; popfq
406         movq saved_context_esp(%rip), %rsp
407         movq saved_context_ebp(%rip), %rbp
408         movq saved_context_eax(%rip), %rax
409         movq saved_context_ebx(%rip), %rbx
410         movq saved_context_ecx(%rip), %rcx
411         movq saved_context_edx(%rip), %rdx
412         movq saved_context_esi(%rip), %rsi
413         movq saved_context_edi(%rip), %rdi
414         movq saved_context_r08(%rip), %r8
415         movq saved_context_r09(%rip), %r9
416         movq saved_context_r10(%rip), %r10
417         movq saved_context_r11(%rip), %r11
418         movq saved_context_r12(%rip), %r12
419         movq saved_context_r13(%rip), %r13
420         movq saved_context_r14(%rip), %r14
421         movq saved_context_r15(%rip), %r15
422
423         xorl    %eax, %eax
424         addq    $8, %rsp
425         jmp     restore_processor_state
426 .LFE5:
427 .Lfe5:
428         .size   do_suspend_lowlevel,.Lfe5-do_suspend_lowlevel
429         
430 .data
431 ALIGN
432 ENTRY(saved_rbp)        .quad   0
433 ENTRY(saved_rsi)        .quad   0
434 ENTRY(saved_rdi)        .quad   0
435 ENTRY(saved_rbx)        .quad   0
436
437 ENTRY(saved_rip)        .quad   0
438 ENTRY(saved_rsp)        .quad   0
439
440 ENTRY(saved_magic)      .quad   0