1*b9411a12SAndroid Build Coastguard Worker _____ ____ _____ ______ ____ ____ ____ _______ 2*b9411a12SAndroid Build Coastguard Worker / ____/ __ \| __ \| ____| _ \ / __ \ / __ \__ __| 3*b9411a12SAndroid Build Coastguard Worker | | | | | | |__) | |__ | |_) | | | | | | | | | 4*b9411a12SAndroid Build Coastguard Worker | | | | | | _ /| __| | _ <| | | | | | | | | 5*b9411a12SAndroid Build Coastguard Worker | |___| |__| | | \ \| |____| |_) | |__| | |__| | | | 6*b9411a12SAndroid Build Coastguard Worker \_____\____/|_| \_\______|____/ \____/ \____/ |_| 7*b9411a12SAndroid Build Coastguard Worker 8*b9411a12SAndroid Build Coastguard Worker __ __ _____ _____ ____ 9*b9411a12SAndroid Build Coastguard Worker /\ | \/ | __ \ / ____| |___ \ 10*b9411a12SAndroid Build Coastguard Worker / \ | \ / | | | | | (___ __) | 11*b9411a12SAndroid Build Coastguard Worker / /\ \ | |\/| | | | | \___ \ |__ < 12*b9411a12SAndroid Build Coastguard Worker / ____ \| | | | |__| | ____) | ___) | 13*b9411a12SAndroid Build Coastguard Worker /_/ \_\_| |_|_____/ |_____/ |____/ 14*b9411a12SAndroid Build Coastguard Worker 15*b9411a12SAndroid Build Coastguard Worker 16*b9411a12SAndroid Build Coastguard Worker S3 in coreboot (V 1.2) 17*b9411a12SAndroid Build Coastguard Worker---------------------------------------- 18*b9411a12SAndroid Build Coastguard Worker Zheng Bao 19*b9411a12SAndroid Build Coastguard Worker <[email protected]> 20*b9411a12SAndroid Build Coastguard Worker <[email protected]> 21*b9411a12SAndroid Build Coastguard Worker 22*b9411a12SAndroid Build Coastguard WorkerIntroduction 23*b9411a12SAndroid Build Coastguard Worker============ 24*b9411a12SAndroid Build Coastguard WorkerThis document is about how the feature S3 is implemented on coreboot, 25*b9411a12SAndroid Build Coastguard Workerspecifically on AMD platform. This topic deals with ACPI spec, hardware, 26*b9411a12SAndroid Build Coastguard WorkerBIOS, OS. We try to help coreboot users to realize their own S3. 27*b9411a12SAndroid Build Coastguard Worker 28*b9411a12SAndroid Build Coastguard WorkerS3 in a nutshell 29*b9411a12SAndroid Build Coastguard Worker================ 30*b9411a12SAndroid Build Coastguard WorkerThe S3 sleeping state is a low wake latency sleeping state where all 31*b9411a12SAndroid Build Coastguard Workersystem context is lost except system memory. [1]. S3 is a ACPI 32*b9411a12SAndroid Build Coastguard Workerdefinition. 33*b9411a12SAndroid Build Coastguard WorkerTo enter S3, write 3 in SLP_TYPx and set the SLP_EN bit (See ACPI 34*b9411a12SAndroid Build Coastguard Workerregisters). But if you do that, board can not resume at where it 35*b9411a12SAndroid Build Coastguard Workersleeps, because you don't save the context. More often than not, we 36*b9411a12SAndroid Build Coastguard Workermake the board go into S3 by the tools which OSes provide. For 37*b9411a12SAndroid Build Coastguard Workerwindows, click Start->sleep. For linux, some distribution provide a 38*b9411a12SAndroid Build Coastguard Workertools called pm-suspend, which can make the system goto S3. If 39*b9411a12SAndroid Build Coastguard Workerpm-suspend is not available, we can run "echo mem > /sys/power/state", 40*b9411a12SAndroid Build Coastguard Workerbut this way may not save all the needed context. 41*b9411a12SAndroid Build Coastguard WorkerIn S3 state, the power is off. So when the power button is pressed, 42*b9411a12SAndroid Build Coastguard WorkerBIOS runs as it does in cold boot. If BIOS didn't detect whether 43*b9411a12SAndroid Build Coastguard Workerboard boots or resumes, it would go the same way as boot. It is not 44*b9411a12SAndroid Build Coastguard Workerwhat we expect. BIOS detects the SLP_TYPx. If it is 3, it means BIOS 45*b9411a12SAndroid Build Coastguard Workerare waking up. 46*b9411a12SAndroid Build Coastguard WorkerBIOS is responsible for restore the machine state as it is before 47*b9411a12SAndroid Build Coastguard Workersleep. It needs restore the memory controller, not overwriting memory 48*b9411a12SAndroid Build Coastguard Workerwhich is not marked as reserved. For the peripheral which loses its 49*b9411a12SAndroid Build Coastguard Workerregisters, BIOS needs to write the original value. 50*b9411a12SAndroid Build Coastguard WorkerWhen everything is done, BIOS needs to find out the wakeup vector 51*b9411a12SAndroid Build Coastguard Workerprovided by OSes and jump there. OSes also have work to do. We can go 52*b9411a12SAndroid Build Coastguard Workerto linux kernel or some other open source projects to find out how they 53*b9411a12SAndroid Build Coastguard Workerhandle S3 resume. 54*b9411a12SAndroid Build Coastguard Worker 55*b9411a12SAndroid Build Coastguard WorkerACPI registers 56*b9411a12SAndroid Build Coastguard Worker============== 57*b9411a12SAndroid Build Coastguard WorkerACPI specification defines a group of registers. OSes handle all these 58*b9411a12SAndroid Build Coastguard Workerregisters to read and write status to all the platform. 59*b9411a12SAndroid Build Coastguard WorkerOn AMD platform, these registers are provided by southbridge. For 60*b9411a12SAndroid Build Coastguard Workerexample, Hudson uses PMIO 60:6F to define ACPI registers. 61*b9411a12SAndroid Build Coastguard WorkerOSes don't have any specific driver to know where these registers 62*b9411a12SAndroid Build Coastguard Workerare. BIOS has the responsibility to allocated the IO resources and 63*b9411a12SAndroid Build Coastguard Workerwrite all these address to FADT, a ACPI defined table. 64*b9411a12SAndroid Build Coastguard Worker 65*b9411a12SAndroid Build Coastguard WorkerMemory Layout 66*b9411a12SAndroid Build Coastguard Worker============= 67*b9411a12SAndroid Build Coastguard WorkerRestoring memory is the most important job done by BIOS. When the 68*b9411a12SAndroid Build Coastguard Workerpower is off, the memory is maintained by standby power. BIOS need to 69*b9411a12SAndroid Build Coastguard Workermake sure that when flow goes to OS, everything in memory should be 70*b9411a12SAndroid Build Coastguard Workerthe same as it was. 71*b9411a12SAndroid Build Coastguard Worker 72*b9411a12SAndroid Build Coastguard WorkerThe chip vendor will provide a way, or code, to wake up the memory 73*b9411a12SAndroid Build Coastguard Workerfrom sleeping. In AGESA 2008 arch, it is called AmdInitResume. 74*b9411a12SAndroid Build Coastguard Worker 75*b9411a12SAndroid Build Coastguard WorkerThe BIOS itself needs some memory to run. Either, BIOS marks the erea 76*b9411a12SAndroid Build Coastguard Workeras reserved in e820, or BIOS saves the content into reserved space. 77*b9411a12SAndroid Build Coastguard Worker 78*b9411a12SAndroid Build Coastguard WorkerHere is the address Map for S3 Resume. Assumingly the total memory is 1GB. 79*b9411a12SAndroid Build Coastguard Worker00000000 --- 00100000 BIOS Reserved area. 80*b9411a12SAndroid Build Coastguard Worker00100000 --- 00200000 Free 81*b9411a12SAndroid Build Coastguard Worker00200000 --- 01000000 coreboot ramstage area. 82*b9411a12SAndroid Build Coastguard Worker01000000 --- 2e160000 Free 83*b9411a12SAndroid Build Coastguard Worker2e160000 --- 2e170000 ACPI table 84*b9411a12SAndroid Build Coastguard Worker2e170000 --- 2ef70000 OSRAM 85*b9411a12SAndroid Build Coastguard Worker2ef70000 --- 2efe0000 Stack in highmem 86*b9411a12SAndroid Build Coastguard Worker2efe0000 --- 2f000000 heap in highmem 87*b9411a12SAndroid Build Coastguard Worker2f000000 TOM 88*b9411a12SAndroid Build Coastguard Worker 89*b9411a12SAndroid Build Coastguard WorkerAMD requirements in S3 90*b9411a12SAndroid Build Coastguard Worker====================== 91*b9411a12SAndroid Build Coastguard WorkerChip vendor like AMD will provide bunch of routines to restore the 92*b9411a12SAndroid Build Coastguard Workerboard.[2] 93*b9411a12SAndroid Build Coastguard Worker * AmdS3Save: It is called in cold boot, save required register into 94*b9411a12SAndroid Build Coastguard Worker non-volatile storage. Currently, we use SPI flash to store the data. 95*b9411a12SAndroid Build Coastguard Worker * AmdInitResume: Restore the memory controller. 96*b9411a12SAndroid Build Coastguard Worker * AmdS3LateRestore: Called after AmdInitResume, restore other 97*b9411a12SAndroid Build Coastguard Worker register that memory. 98*b9411a12SAndroid Build Coastguard Worker * (SouthBridge)InitS3EarlyRestore, (SouthBridge)InitS3LateRestore: 99*b9411a12SAndroid Build Coastguard Worker Provided by Southbridge vendor code. Early is called before PCI 100*b9411a12SAndroid Build Coastguard Worker enumeration, and Late is called after that. 101*b9411a12SAndroid Build Coastguard Worker 102*b9411a12SAndroid Build Coastguard WorkerLifecycle of booting, sleeping and waking coreboot and Ubuntu 103*b9411a12SAndroid Build Coastguard Worker============================================================= 104*b9411a12SAndroid Build Coastguard Worker1. Cold boot. 105*b9411a12SAndroid Build Coastguard WorkerFor a system with S3 feature, the BIOS needs to save some data to 106*b9411a12SAndroid Build Coastguard Workernon-volatile storage at cold boot stage. What data need to be save are 107*b9411a12SAndroid Build Coastguard Workerprovided by AmdS3Save. After the wrapper calls the AmdS3Save, it gets 108*b9411a12SAndroid Build Coastguard Workerthe VolatileStorage and NvStorage, which are where the data are 109*b9411a12SAndroid Build Coastguard Workerlocated. It is the wrappers's responsibility to save the data.[3][4] 110*b9411a12SAndroid Build Coastguard WorkerCurrently, the wrappers allocate a CBFS modules in BIOS image. To do 111*b9411a12SAndroid Build Coastguard Workerthat, the wrapper needs to have the ability to write flash chips. It 112*b9411a12SAndroid Build Coastguard Workeris not as comprehensive as flashrom. But for the SST chip on Parmer, 113*b9411a12SAndroid Build Coastguard WorkerMX chip on Thather, coreboot works well.[5] 114*b9411a12SAndroid Build Coastguard Worker 115*b9411a12SAndroid Build Coastguard Worker2. OS goes in S3. 116*b9411a12SAndroid Build Coastguard WorkerFor Linux, besides the kernel needs to do some saving, most distributions 117*b9411a12SAndroid Build Coastguard Workerrun some scripts. For Ubuntu, scripts are located at /usr/lib/pm-utils/sleep.d. 118*b9411a12SAndroid Build Coastguard Worker # ls /usr/lib/pm-utils/sleep.d 119*b9411a12SAndroid Build Coastguard Worker 000kernel-change 49bluetooth 90clock 95led 120*b9411a12SAndroid Build Coastguard Worker 00logging 55NetworkManager 94cpufreq 98video-quirk-db-handler 121*b9411a12SAndroid Build Coastguard Worker 00powersave 60_wpa_supplicant 95anacron 99video 122*b9411a12SAndroid Build Coastguard Worker 01PulseAudio 75modules 95hdparm-apm 123*b9411a12SAndroid Build Coastguard WorkerThe script with lower prefix runs before the one with higher prefix. 124*b9411a12SAndroid Build Coastguard Worker99video is the last one. 125*b9411a12SAndroid Build Coastguard WorkerThose scripts have hooks called hibernate, suspend, thaw, resume. For 126*b9411a12SAndroid Build Coastguard Workereach script, suspend is called when system sleeps and wakeup is called 127*b9411a12SAndroid Build Coastguard Workerwhen system wakeups. 128*b9411a12SAndroid Build Coastguard Worker 129*b9411a12SAndroid Build Coastguard Worker3. Firmware detects S3 wakeup 130*b9411a12SAndroid Build Coastguard WorkerAs we mentioned, Firmware detects the SLP_TYPx to find out if the board 131*b9411a12SAndroid Build Coastguard Workerwakes up. In romstage.c, AmdInitReset and AmdInitEarly are called 132*b9411a12SAndroid Build Coastguard Workeras they are during cold boot. AmdInitResume and AmdS3LateRestore are 133*b9411a12SAndroid Build Coastguard Workercalled only during resume. For whole ramstage, coreboot goes through 134*b9411a12SAndroid Build Coastguard Workeralmost the same way as cold boot, other than not calling the AmdInitMid, 135*b9411a12SAndroid Build Coastguard WorkerAmdInitLate and AmdS3Save, and restoring all the MTRRs. 136*b9411a12SAndroid Build Coastguard WorkerAt last step of coreboot stage, coreboot finds out the wakeup vector in FADT, 137*b9411a12SAndroid Build Coastguard Workerwritten by OS, and jump. 138*b9411a12SAndroid Build Coastguard Worker 139*b9411a12SAndroid Build Coastguard Worker4. OS resumes. 140*b9411a12SAndroid Build Coastguard WorkerWhen Linux resumes, all the sleeping scripts call their resume 141*b9411a12SAndroid Build Coastguard Workerhooks. If we are more lucky, all the scripts can go through. More 142*b9411a12SAndroid Build Coastguard Workerchances that the 99video hangs or fails to get the display 143*b9411a12SAndroid Build Coastguard Workerback. Sometimes it can fixed if CONFIG_S3_VGA_ROM_RUN is unset in 144*b9411a12SAndroid Build Coastguard Workercoreboot/Kconfig. That needs more troubleshooting. 145*b9411a12SAndroid Build Coastguard Worker 146*b9411a12SAndroid Build Coastguard Worker 147*b9411a12SAndroid Build Coastguard WorkerReference 148*b9411a12SAndroid Build Coastguard Worker========= 149*b9411a12SAndroid Build Coastguard Worker[1] ACPI40a, http://www.acpi.info/spec40a.htm 150*b9411a12SAndroid Build Coastguard Worker[2] coreboot Vendorcode, {top}/src/vendorcode/amd/agesa/{family}/Proc/Common/ 151*b9411a12SAndroid Build Coastguard Worker[3] coreboot AGESA wrapper, {top}/src/mainboard/amd/parmer/agesawrapper.c 152*b9411a12SAndroid Build Coastguard Worker[4] coreboot AGESA wrapper, {top}/src/cpu/amd/agesa/s3_resume.c 153*b9411a12SAndroid Build Coastguard Worker[5] coreboot Southbridge, {top}/src/southbridge/amd/agesa/hudson/spi.c 154