sleep.S 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * This file contains sleep low-level functions for PowerBook G3.
  3. * Copyright (C) 1999 Benjamin Herrenschmidt (benh@kernel.crashing.org)
  4. * and Paul Mackerras (paulus@samba.org).
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <asm/processor.h>
  13. #include <asm/page.h>
  14. #include <asm/ppc_asm.h>
  15. #include <asm/cputable.h>
  16. #include <asm/cache.h>
  17. #include <asm/thread_info.h>
  18. #include <asm/asm-offsets.h>
  19. #include <asm/mmu.h>
  20. #include <asm/feature-fixups.h>
  21. #define MAGIC 0x4c617273 /* 'Lars' */
  22. /*
  23. * Structure for storing CPU registers on the stack.
  24. */
  25. #define SL_SP 0
  26. #define SL_PC 4
  27. #define SL_MSR 8
  28. #define SL_SDR1 0xc
  29. #define SL_SPRG0 0x10 /* 4 sprg's */
  30. #define SL_DBAT0 0x20
  31. #define SL_IBAT0 0x28
  32. #define SL_DBAT1 0x30
  33. #define SL_IBAT1 0x38
  34. #define SL_DBAT2 0x40
  35. #define SL_IBAT2 0x48
  36. #define SL_DBAT3 0x50
  37. #define SL_IBAT3 0x58
  38. #define SL_DBAT4 0x60
  39. #define SL_IBAT4 0x68
  40. #define SL_DBAT5 0x70
  41. #define SL_IBAT5 0x78
  42. #define SL_DBAT6 0x80
  43. #define SL_IBAT6 0x88
  44. #define SL_DBAT7 0x90
  45. #define SL_IBAT7 0x98
  46. #define SL_TB 0xa0
  47. #define SL_R2 0xa8
  48. #define SL_CR 0xac
  49. #define SL_R12 0xb0 /* r12 to r31 */
  50. #define SL_SIZE (SL_R12 + 80)
  51. .section .text
  52. .align 5
  53. #if defined(CONFIG_PM) || defined(CONFIG_CPU_FREQ_PMAC) || \
  54. (defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PPC32))
  55. /* This gets called by via-pmu.c late during the sleep process.
  56. * The PMU was already send the sleep command and will shut us down
  57. * soon. We need to save all that is needed and setup the wakeup
  58. * vector that will be called by the ROM on wakeup
  59. */
  60. _GLOBAL(low_sleep_handler)
  61. #ifndef CONFIG_6xx
  62. blr
  63. #else
  64. mflr r0
  65. stw r0,4(r1)
  66. stwu r1,-SL_SIZE(r1)
  67. mfcr r0
  68. stw r0,SL_CR(r1)
  69. stw r2,SL_R2(r1)
  70. stmw r12,SL_R12(r1)
  71. /* Save MSR & SDR1 */
  72. mfmsr r4
  73. stw r4,SL_MSR(r1)
  74. mfsdr1 r4
  75. stw r4,SL_SDR1(r1)
  76. /* Get a stable timebase and save it */
  77. 1: mftbu r4
  78. stw r4,SL_TB(r1)
  79. mftb r5
  80. stw r5,SL_TB+4(r1)
  81. mftbu r3
  82. cmpw r3,r4
  83. bne 1b
  84. /* Save SPRGs */
  85. mfsprg r4,0
  86. stw r4,SL_SPRG0(r1)
  87. mfsprg r4,1
  88. stw r4,SL_SPRG0+4(r1)
  89. mfsprg r4,2
  90. stw r4,SL_SPRG0+8(r1)
  91. mfsprg r4,3
  92. stw r4,SL_SPRG0+12(r1)
  93. /* Save BATs */
  94. mfdbatu r4,0
  95. stw r4,SL_DBAT0(r1)
  96. mfdbatl r4,0
  97. stw r4,SL_DBAT0+4(r1)
  98. mfdbatu r4,1
  99. stw r4,SL_DBAT1(r1)
  100. mfdbatl r4,1
  101. stw r4,SL_DBAT1+4(r1)
  102. mfdbatu r4,2
  103. stw r4,SL_DBAT2(r1)
  104. mfdbatl r4,2
  105. stw r4,SL_DBAT2+4(r1)
  106. mfdbatu r4,3
  107. stw r4,SL_DBAT3(r1)
  108. mfdbatl r4,3
  109. stw r4,SL_DBAT3+4(r1)
  110. mfibatu r4,0
  111. stw r4,SL_IBAT0(r1)
  112. mfibatl r4,0
  113. stw r4,SL_IBAT0+4(r1)
  114. mfibatu r4,1
  115. stw r4,SL_IBAT1(r1)
  116. mfibatl r4,1
  117. stw r4,SL_IBAT1+4(r1)
  118. mfibatu r4,2
  119. stw r4,SL_IBAT2(r1)
  120. mfibatl r4,2
  121. stw r4,SL_IBAT2+4(r1)
  122. mfibatu r4,3
  123. stw r4,SL_IBAT3(r1)
  124. mfibatl r4,3
  125. stw r4,SL_IBAT3+4(r1)
  126. BEGIN_MMU_FTR_SECTION
  127. mfspr r4,SPRN_DBAT4U
  128. stw r4,SL_DBAT4(r1)
  129. mfspr r4,SPRN_DBAT4L
  130. stw r4,SL_DBAT4+4(r1)
  131. mfspr r4,SPRN_DBAT5U
  132. stw r4,SL_DBAT5(r1)
  133. mfspr r4,SPRN_DBAT5L
  134. stw r4,SL_DBAT5+4(r1)
  135. mfspr r4,SPRN_DBAT6U
  136. stw r4,SL_DBAT6(r1)
  137. mfspr r4,SPRN_DBAT6L
  138. stw r4,SL_DBAT6+4(r1)
  139. mfspr r4,SPRN_DBAT7U
  140. stw r4,SL_DBAT7(r1)
  141. mfspr r4,SPRN_DBAT7L
  142. stw r4,SL_DBAT7+4(r1)
  143. mfspr r4,SPRN_IBAT4U
  144. stw r4,SL_IBAT4(r1)
  145. mfspr r4,SPRN_IBAT4L
  146. stw r4,SL_IBAT4+4(r1)
  147. mfspr r4,SPRN_IBAT5U
  148. stw r4,SL_IBAT5(r1)
  149. mfspr r4,SPRN_IBAT5L
  150. stw r4,SL_IBAT5+4(r1)
  151. mfspr r4,SPRN_IBAT6U
  152. stw r4,SL_IBAT6(r1)
  153. mfspr r4,SPRN_IBAT6L
  154. stw r4,SL_IBAT6+4(r1)
  155. mfspr r4,SPRN_IBAT7U
  156. stw r4,SL_IBAT7(r1)
  157. mfspr r4,SPRN_IBAT7L
  158. stw r4,SL_IBAT7+4(r1)
  159. END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS)
  160. /* Backup various CPU config stuffs */
  161. bl __save_cpu_setup
  162. /* The ROM can wake us up via 2 different vectors:
  163. * - On wallstreet & lombard, we must write a magic
  164. * value 'Lars' at address 4 and a pointer to a
  165. * memory location containing the PC to resume from
  166. * at address 0.
  167. * - On Core99, we must store the wakeup vector at
  168. * address 0x80 and eventually it's parameters
  169. * at address 0x84. I've have some trouble with those
  170. * parameters however and I no longer use them.
  171. */
  172. lis r5,grackle_wake_up@ha
  173. addi r5,r5,grackle_wake_up@l
  174. tophys(r5,r5)
  175. stw r5,SL_PC(r1)
  176. lis r4,KERNELBASE@h
  177. tophys(r5,r1)
  178. addi r5,r5,SL_PC
  179. lis r6,MAGIC@ha
  180. addi r6,r6,MAGIC@l
  181. stw r5,0(r4)
  182. stw r6,4(r4)
  183. /* Setup stuffs at 0x80-0x84 for Core99 */
  184. lis r3,core99_wake_up@ha
  185. addi r3,r3,core99_wake_up@l
  186. tophys(r3,r3)
  187. stw r3,0x80(r4)
  188. stw r5,0x84(r4)
  189. /* Store a pointer to our backup storage into
  190. * a kernel global
  191. */
  192. lis r3,sleep_storage@ha
  193. addi r3,r3,sleep_storage@l
  194. stw r5,0(r3)
  195. .globl low_cpu_die
  196. low_cpu_die:
  197. /* Flush & disable all caches */
  198. bl flush_disable_caches
  199. /* Turn off data relocation. */
  200. mfmsr r3 /* Save MSR in r7 */
  201. rlwinm r3,r3,0,28,26 /* Turn off DR bit */
  202. sync
  203. mtmsr r3
  204. isync
  205. BEGIN_FTR_SECTION
  206. /* Flush any pending L2 data prefetches to work around HW bug */
  207. sync
  208. lis r3,0xfff0
  209. lwz r0,0(r3) /* perform cache-inhibited load to ROM */
  210. sync /* (caches are disabled at this point) */
  211. END_FTR_SECTION_IFSET(CPU_FTR_SPEC7450)
  212. /*
  213. * Set the HID0 and MSR for sleep.
  214. */
  215. mfspr r2,SPRN_HID0
  216. rlwinm r2,r2,0,10,7 /* clear doze, nap */
  217. oris r2,r2,HID0_SLEEP@h
  218. sync
  219. isync
  220. mtspr SPRN_HID0,r2
  221. sync
  222. /* This loop puts us back to sleep in case we have a spurrious
  223. * wakeup so that the host bridge properly stays asleep. The
  224. * CPU will be turned off, either after a known time (about 1
  225. * second) on wallstreet & lombard, or as soon as the CPU enters
  226. * SLEEP mode on core99
  227. */
  228. mfmsr r2
  229. oris r2,r2,MSR_POW@h
  230. 1: sync
  231. mtmsr r2
  232. isync
  233. b 1b
  234. /*
  235. * Here is the resume code.
  236. */
  237. /*
  238. * Core99 machines resume here
  239. * r4 has the physical address of SL_PC(sp) (unused)
  240. */
  241. _GLOBAL(core99_wake_up)
  242. /* Make sure HID0 no longer contains any sleep bit and that data cache
  243. * is disabled
  244. */
  245. mfspr r3,SPRN_HID0
  246. rlwinm r3,r3,0,11,7 /* clear SLEEP, NAP, DOZE bits */
  247. rlwinm 3,r3,0,18,15 /* clear DCE, ICE */
  248. mtspr SPRN_HID0,r3
  249. sync
  250. isync
  251. /* sanitize MSR */
  252. mfmsr r3
  253. ori r3,r3,MSR_EE|MSR_IP
  254. xori r3,r3,MSR_EE|MSR_IP
  255. sync
  256. isync
  257. mtmsr r3
  258. sync
  259. isync
  260. /* Recover sleep storage */
  261. lis r3,sleep_storage@ha
  262. addi r3,r3,sleep_storage@l
  263. tophys(r3,r3)
  264. lwz r1,0(r3)
  265. /* Pass thru to older resume code ... */
  266. /*
  267. * Here is the resume code for older machines.
  268. * r1 has the physical address of SL_PC(sp).
  269. */
  270. grackle_wake_up:
  271. /* Restore the kernel's segment registers before
  272. * we do any r1 memory access as we are not sure they
  273. * are in a sane state above the first 256Mb region
  274. */
  275. li r0,16 /* load up segment register values */
  276. mtctr r0 /* for context 0 */
  277. lis r3,0x2000 /* Ku = 1, VSID = 0 */
  278. li r4,0
  279. 3: mtsrin r3,r4
  280. addi r3,r3,0x111 /* increment VSID */
  281. addis r4,r4,0x1000 /* address of next segment */
  282. bdnz 3b
  283. sync
  284. isync
  285. subi r1,r1,SL_PC
  286. /* Restore various CPU config stuffs */
  287. bl __restore_cpu_setup
  288. /* Make sure all FPRs have been initialized */
  289. bl reloc_offset
  290. bl __init_fpu_registers
  291. /* Invalidate & enable L1 cache, we don't care about
  292. * whatever the ROM may have tried to write to memory
  293. */
  294. bl __inval_enable_L1
  295. /* Restore the BATs, and SDR1. Then we can turn on the MMU. */
  296. lwz r4,SL_SDR1(r1)
  297. mtsdr1 r4
  298. lwz r4,SL_SPRG0(r1)
  299. mtsprg 0,r4
  300. lwz r4,SL_SPRG0+4(r1)
  301. mtsprg 1,r4
  302. lwz r4,SL_SPRG0+8(r1)
  303. mtsprg 2,r4
  304. lwz r4,SL_SPRG0+12(r1)
  305. mtsprg 3,r4
  306. lwz r4,SL_DBAT0(r1)
  307. mtdbatu 0,r4
  308. lwz r4,SL_DBAT0+4(r1)
  309. mtdbatl 0,r4
  310. lwz r4,SL_DBAT1(r1)
  311. mtdbatu 1,r4
  312. lwz r4,SL_DBAT1+4(r1)
  313. mtdbatl 1,r4
  314. lwz r4,SL_DBAT2(r1)
  315. mtdbatu 2,r4
  316. lwz r4,SL_DBAT2+4(r1)
  317. mtdbatl 2,r4
  318. lwz r4,SL_DBAT3(r1)
  319. mtdbatu 3,r4
  320. lwz r4,SL_DBAT3+4(r1)
  321. mtdbatl 3,r4
  322. lwz r4,SL_IBAT0(r1)
  323. mtibatu 0,r4
  324. lwz r4,SL_IBAT0+4(r1)
  325. mtibatl 0,r4
  326. lwz r4,SL_IBAT1(r1)
  327. mtibatu 1,r4
  328. lwz r4,SL_IBAT1+4(r1)
  329. mtibatl 1,r4
  330. lwz r4,SL_IBAT2(r1)
  331. mtibatu 2,r4
  332. lwz r4,SL_IBAT2+4(r1)
  333. mtibatl 2,r4
  334. lwz r4,SL_IBAT3(r1)
  335. mtibatu 3,r4
  336. lwz r4,SL_IBAT3+4(r1)
  337. mtibatl 3,r4
  338. BEGIN_MMU_FTR_SECTION
  339. lwz r4,SL_DBAT4(r1)
  340. mtspr SPRN_DBAT4U,r4
  341. lwz r4,SL_DBAT4+4(r1)
  342. mtspr SPRN_DBAT4L,r4
  343. lwz r4,SL_DBAT5(r1)
  344. mtspr SPRN_DBAT5U,r4
  345. lwz r4,SL_DBAT5+4(r1)
  346. mtspr SPRN_DBAT5L,r4
  347. lwz r4,SL_DBAT6(r1)
  348. mtspr SPRN_DBAT6U,r4
  349. lwz r4,SL_DBAT6+4(r1)
  350. mtspr SPRN_DBAT6L,r4
  351. lwz r4,SL_DBAT7(r1)
  352. mtspr SPRN_DBAT7U,r4
  353. lwz r4,SL_DBAT7+4(r1)
  354. mtspr SPRN_DBAT7L,r4
  355. lwz r4,SL_IBAT4(r1)
  356. mtspr SPRN_IBAT4U,r4
  357. lwz r4,SL_IBAT4+4(r1)
  358. mtspr SPRN_IBAT4L,r4
  359. lwz r4,SL_IBAT5(r1)
  360. mtspr SPRN_IBAT5U,r4
  361. lwz r4,SL_IBAT5+4(r1)
  362. mtspr SPRN_IBAT5L,r4
  363. lwz r4,SL_IBAT6(r1)
  364. mtspr SPRN_IBAT6U,r4
  365. lwz r4,SL_IBAT6+4(r1)
  366. mtspr SPRN_IBAT6L,r4
  367. lwz r4,SL_IBAT7(r1)
  368. mtspr SPRN_IBAT7U,r4
  369. lwz r4,SL_IBAT7+4(r1)
  370. mtspr SPRN_IBAT7L,r4
  371. END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS)
  372. /* Flush all TLBs */
  373. lis r4,0x1000
  374. 1: addic. r4,r4,-0x1000
  375. tlbie r4
  376. blt 1b
  377. sync
  378. /* restore the MSR and turn on the MMU */
  379. lwz r3,SL_MSR(r1)
  380. bl turn_on_mmu
  381. /* get back the stack pointer */
  382. tovirt(r1,r1)
  383. /* Restore TB */
  384. li r3,0
  385. mttbl r3
  386. lwz r3,SL_TB(r1)
  387. lwz r4,SL_TB+4(r1)
  388. mttbu r3
  389. mttbl r4
  390. /* Restore the callee-saved registers and return */
  391. lwz r0,SL_CR(r1)
  392. mtcr r0
  393. lwz r2,SL_R2(r1)
  394. lmw r12,SL_R12(r1)
  395. addi r1,r1,SL_SIZE
  396. lwz r0,4(r1)
  397. mtlr r0
  398. blr
  399. turn_on_mmu:
  400. mflr r4
  401. tovirt(r4,r4)
  402. mtsrr0 r4
  403. mtsrr1 r3
  404. sync
  405. isync
  406. rfi
  407. #endif /* defined(CONFIG_PM) || defined(CONFIG_CPU_FREQ) */
  408. .section .data
  409. .balign L1_CACHE_BYTES
  410. sleep_storage:
  411. .long 0
  412. .balign L1_CACHE_BYTES, 0
  413. #endif /* CONFIG_6xx */
  414. .section .text