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