misc_32.S 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  2. * This file contains miscellaneous low-level functions.
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
  6. * and Paul Mackerras.
  7. *
  8. * kexec bits:
  9. * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com>
  10. * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
  11. * PPC44x port. Copyright (C) 2011, IBM Corporation
  12. * Author: Suzuki Poulose <suzuki@in.ibm.com>
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/sys.h>
  21. #include <asm/unistd.h>
  22. #include <asm/errno.h>
  23. #include <asm/reg.h>
  24. #include <asm/page.h>
  25. #include <asm/cache.h>
  26. #include <asm/cputable.h>
  27. #include <asm/mmu.h>
  28. #include <asm/ppc_asm.h>
  29. #include <asm/thread_info.h>
  30. #include <asm/asm-offsets.h>
  31. #include <asm/processor.h>
  32. #include <asm/kexec.h>
  33. #include <asm/bug.h>
  34. #include <asm/ptrace.h>
  35. .text
  36. /*
  37. * We store the saved ksp_limit in the unused part
  38. * of the STACK_FRAME_OVERHEAD
  39. */
  40. _GLOBAL(call_do_softirq)
  41. mflr r0
  42. stw r0,4(r1)
  43. lwz r10,THREAD+KSP_LIMIT(r2)
  44. addi r11,r3,THREAD_INFO_GAP
  45. stwu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
  46. mr r1,r3
  47. stw r10,8(r1)
  48. stw r11,THREAD+KSP_LIMIT(r2)
  49. bl __do_softirq
  50. lwz r10,8(r1)
  51. lwz r1,0(r1)
  52. lwz r0,4(r1)
  53. stw r10,THREAD+KSP_LIMIT(r2)
  54. mtlr r0
  55. blr
  56. /*
  57. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  58. */
  59. _GLOBAL(call_do_irq)
  60. mflr r0
  61. stw r0,4(r1)
  62. lwz r10,THREAD+KSP_LIMIT(r2)
  63. addi r11,r4,THREAD_INFO_GAP
  64. stwu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r4)
  65. mr r1,r4
  66. stw r10,8(r1)
  67. stw r11,THREAD+KSP_LIMIT(r2)
  68. bl __do_irq
  69. lwz r10,8(r1)
  70. lwz r1,0(r1)
  71. lwz r0,4(r1)
  72. stw r10,THREAD+KSP_LIMIT(r2)
  73. mtlr r0
  74. blr
  75. /*
  76. * This returns the high 64 bits of the product of two 64-bit numbers.
  77. */
  78. _GLOBAL(mulhdu)
  79. cmpwi r6,0
  80. cmpwi cr1,r3,0
  81. mr r10,r4
  82. mulhwu r4,r4,r5
  83. beq 1f
  84. mulhwu r0,r10,r6
  85. mullw r7,r10,r5
  86. addc r7,r0,r7
  87. addze r4,r4
  88. 1: beqlr cr1 /* all done if high part of A is 0 */
  89. mr r10,r3
  90. mullw r9,r3,r5
  91. mulhwu r3,r3,r5
  92. beq 2f
  93. mullw r0,r10,r6
  94. mulhwu r8,r10,r6
  95. addc r7,r0,r7
  96. adde r4,r4,r8
  97. addze r3,r3
  98. 2: addc r4,r4,r9
  99. addze r3,r3
  100. blr
  101. /*
  102. * sub_reloc_offset(x) returns x - reloc_offset().
  103. */
  104. _GLOBAL(sub_reloc_offset)
  105. mflr r0
  106. bl 1f
  107. 1: mflr r5
  108. lis r4,1b@ha
  109. addi r4,r4,1b@l
  110. subf r5,r4,r5
  111. subf r3,r5,r3
  112. mtlr r0
  113. blr
  114. /*
  115. * reloc_got2 runs through the .got2 section adding an offset
  116. * to each entry.
  117. */
  118. _GLOBAL(reloc_got2)
  119. mflr r11
  120. lis r7,__got2_start@ha
  121. addi r7,r7,__got2_start@l
  122. lis r8,__got2_end@ha
  123. addi r8,r8,__got2_end@l
  124. subf r8,r7,r8
  125. srwi. r8,r8,2
  126. beqlr
  127. mtctr r8
  128. bl 1f
  129. 1: mflr r0
  130. lis r4,1b@ha
  131. addi r4,r4,1b@l
  132. subf r0,r4,r0
  133. add r7,r0,r7
  134. 2: lwz r0,0(r7)
  135. add r0,r0,r3
  136. stw r0,0(r7)
  137. addi r7,r7,4
  138. bdnz 2b
  139. mtlr r11
  140. blr
  141. /*
  142. * call_setup_cpu - call the setup_cpu function for this cpu
  143. * r3 = data offset, r24 = cpu number
  144. *
  145. * Setup function is called with:
  146. * r3 = data offset
  147. * r4 = ptr to CPU spec (relocated)
  148. */
  149. _GLOBAL(call_setup_cpu)
  150. addis r4,r3,cur_cpu_spec@ha
  151. addi r4,r4,cur_cpu_spec@l
  152. lwz r4,0(r4)
  153. add r4,r4,r3
  154. lwz r5,CPU_SPEC_SETUP(r4)
  155. cmpwi 0,r5,0
  156. add r5,r5,r3
  157. beqlr
  158. mtctr r5
  159. bctr
  160. #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
  161. /* This gets called by via-pmu.c to switch the PLL selection
  162. * on 750fx CPU. This function should really be moved to some
  163. * other place (as most of the cpufreq code in via-pmu
  164. */
  165. _GLOBAL(low_choose_750fx_pll)
  166. /* Clear MSR:EE */
  167. mfmsr r7
  168. rlwinm r0,r7,0,17,15
  169. mtmsr r0
  170. /* If switching to PLL1, disable HID0:BTIC */
  171. cmplwi cr0,r3,0
  172. beq 1f
  173. mfspr r5,SPRN_HID0
  174. rlwinm r5,r5,0,27,25
  175. sync
  176. mtspr SPRN_HID0,r5
  177. isync
  178. sync
  179. 1:
  180. /* Calc new HID1 value */
  181. mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
  182. rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
  183. rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
  184. or r4,r4,r5
  185. mtspr SPRN_HID1,r4
  186. /* Store new HID1 image */
  187. CURRENT_THREAD_INFO(r6, r1)
  188. lwz r6,TI_CPU(r6)
  189. slwi r6,r6,2
  190. addis r6,r6,nap_save_hid1@ha
  191. stw r4,nap_save_hid1@l(r6)
  192. /* If switching to PLL0, enable HID0:BTIC */
  193. cmplwi cr0,r3,0
  194. bne 1f
  195. mfspr r5,SPRN_HID0
  196. ori r5,r5,HID0_BTIC
  197. sync
  198. mtspr SPRN_HID0,r5
  199. isync
  200. sync
  201. 1:
  202. /* Return */
  203. mtmsr r7
  204. blr
  205. _GLOBAL(low_choose_7447a_dfs)
  206. /* Clear MSR:EE */
  207. mfmsr r7
  208. rlwinm r0,r7,0,17,15
  209. mtmsr r0
  210. /* Calc new HID1 value */
  211. mfspr r4,SPRN_HID1
  212. insrwi r4,r3,1,9 /* insert parameter into bit 9 */
  213. sync
  214. mtspr SPRN_HID1,r4
  215. sync
  216. isync
  217. /* Return */
  218. mtmsr r7
  219. blr
  220. #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
  221. /*
  222. * complement mask on the msr then "or" some values on.
  223. * _nmask_and_or_msr(nmask, value_to_or)
  224. */
  225. _GLOBAL(_nmask_and_or_msr)
  226. mfmsr r0 /* Get current msr */
  227. andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
  228. or r0,r0,r4 /* Or on the bits in r4 (second parm) */
  229. SYNC /* Some chip revs have problems here... */
  230. mtmsr r0 /* Update machine state */
  231. isync
  232. blr /* Done */
  233. #ifdef CONFIG_40x
  234. /*
  235. * Do an IO access in real mode
  236. */
  237. _GLOBAL(real_readb)
  238. mfmsr r7
  239. ori r0,r7,MSR_DR
  240. xori r0,r0,MSR_DR
  241. sync
  242. mtmsr r0
  243. sync
  244. isync
  245. lbz r3,0(r3)
  246. sync
  247. mtmsr r7
  248. sync
  249. isync
  250. blr
  251. /*
  252. * Do an IO access in real mode
  253. */
  254. _GLOBAL(real_writeb)
  255. mfmsr r7
  256. ori r0,r7,MSR_DR
  257. xori r0,r0,MSR_DR
  258. sync
  259. mtmsr r0
  260. sync
  261. isync
  262. stb r3,0(r4)
  263. sync
  264. mtmsr r7
  265. sync
  266. isync
  267. blr
  268. #endif /* CONFIG_40x */
  269. /*
  270. * Flush instruction cache.
  271. * This is a no-op on the 601.
  272. */
  273. _GLOBAL(flush_instruction_cache)
  274. #if defined(CONFIG_8xx)
  275. isync
  276. lis r5, IDC_INVALL@h
  277. mtspr SPRN_IC_CST, r5
  278. #elif defined(CONFIG_4xx)
  279. #ifdef CONFIG_403GCX
  280. li r3, 512
  281. mtctr r3
  282. lis r4, KERNELBASE@h
  283. 1: iccci 0, r4
  284. addi r4, r4, 16
  285. bdnz 1b
  286. #else
  287. lis r3, KERNELBASE@h
  288. iccci 0,r3
  289. #endif
  290. #elif CONFIG_FSL_BOOKE
  291. BEGIN_FTR_SECTION
  292. mfspr r3,SPRN_L1CSR0
  293. ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
  294. /* msync; isync recommended here */
  295. mtspr SPRN_L1CSR0,r3
  296. isync
  297. blr
  298. END_FTR_SECTION_IFSET(CPU_FTR_UNIFIED_ID_CACHE)
  299. mfspr r3,SPRN_L1CSR1
  300. ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
  301. mtspr SPRN_L1CSR1,r3
  302. #else
  303. mfspr r3,SPRN_PVR
  304. rlwinm r3,r3,16,16,31
  305. cmpwi 0,r3,1
  306. beqlr /* for 601, do nothing */
  307. /* 603/604 processor - use invalidate-all bit in HID0 */
  308. mfspr r3,SPRN_HID0
  309. ori r3,r3,HID0_ICFI
  310. mtspr SPRN_HID0,r3
  311. #endif /* CONFIG_8xx/4xx */
  312. isync
  313. blr
  314. /*
  315. * Write any modified data cache blocks out to memory
  316. * and invalidate the corresponding instruction cache blocks.
  317. * This is a no-op on the 601.
  318. *
  319. * flush_icache_range(unsigned long start, unsigned long stop)
  320. */
  321. _KPROBE(flush_icache_range)
  322. BEGIN_FTR_SECTION
  323. PURGE_PREFETCHED_INS
  324. blr /* for 601, do nothing */
  325. END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
  326. li r5,L1_CACHE_BYTES-1
  327. andc r3,r3,r5
  328. subf r4,r3,r4
  329. add r4,r4,r5
  330. srwi. r4,r4,L1_CACHE_SHIFT
  331. beqlr
  332. mtctr r4
  333. mr r6,r3
  334. 1: dcbst 0,r3
  335. addi r3,r3,L1_CACHE_BYTES
  336. bdnz 1b
  337. sync /* wait for dcbst's to get to ram */
  338. #ifndef CONFIG_44x
  339. mtctr r4
  340. 2: icbi 0,r6
  341. addi r6,r6,L1_CACHE_BYTES
  342. bdnz 2b
  343. #else
  344. /* Flash invalidate on 44x because we are passed kmapped addresses and
  345. this doesn't work for userspace pages due to the virtually tagged
  346. icache. Sigh. */
  347. iccci 0, r0
  348. #endif
  349. sync /* additional sync needed on g4 */
  350. isync
  351. blr
  352. /*
  353. * Write any modified data cache blocks out to memory.
  354. * Does not invalidate the corresponding cache lines (especially for
  355. * any corresponding instruction cache).
  356. *
  357. * clean_dcache_range(unsigned long start, unsigned long stop)
  358. */
  359. _GLOBAL(clean_dcache_range)
  360. li r5,L1_CACHE_BYTES-1
  361. andc r3,r3,r5
  362. subf r4,r3,r4
  363. add r4,r4,r5
  364. srwi. r4,r4,L1_CACHE_SHIFT
  365. beqlr
  366. mtctr r4
  367. 1: dcbst 0,r3
  368. addi r3,r3,L1_CACHE_BYTES
  369. bdnz 1b
  370. sync /* wait for dcbst's to get to ram */
  371. blr
  372. /*
  373. * Write any modified data cache blocks out to memory and invalidate them.
  374. * Does not invalidate the corresponding instruction cache blocks.
  375. *
  376. * flush_dcache_range(unsigned long start, unsigned long stop)
  377. */
  378. _GLOBAL(flush_dcache_range)
  379. li r5,L1_CACHE_BYTES-1
  380. andc r3,r3,r5
  381. subf r4,r3,r4
  382. add r4,r4,r5
  383. srwi. r4,r4,L1_CACHE_SHIFT
  384. beqlr
  385. mtctr r4
  386. 1: dcbf 0,r3
  387. addi r3,r3,L1_CACHE_BYTES
  388. bdnz 1b
  389. sync /* wait for dcbst's to get to ram */
  390. blr
  391. /*
  392. * Like above, but invalidate the D-cache. This is used by the 8xx
  393. * to invalidate the cache so the PPC core doesn't get stale data
  394. * from the CPM (no cache snooping here :-).
  395. *
  396. * invalidate_dcache_range(unsigned long start, unsigned long stop)
  397. */
  398. _GLOBAL(invalidate_dcache_range)
  399. li r5,L1_CACHE_BYTES-1
  400. andc r3,r3,r5
  401. subf r4,r3,r4
  402. add r4,r4,r5
  403. srwi. r4,r4,L1_CACHE_SHIFT
  404. beqlr
  405. mtctr r4
  406. 1: dcbi 0,r3
  407. addi r3,r3,L1_CACHE_BYTES
  408. bdnz 1b
  409. sync /* wait for dcbi's to get to ram */
  410. blr
  411. /*
  412. * Flush a particular page from the data cache to RAM.
  413. * Note: this is necessary because the instruction cache does *not*
  414. * snoop from the data cache.
  415. * This is a no-op on the 601 which has a unified cache.
  416. *
  417. * void __flush_dcache_icache(void *page)
  418. */
  419. _GLOBAL(__flush_dcache_icache)
  420. BEGIN_FTR_SECTION
  421. PURGE_PREFETCHED_INS
  422. blr
  423. END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
  424. rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
  425. li r4,PAGE_SIZE/L1_CACHE_BYTES /* Number of lines in a page */
  426. mtctr r4
  427. mr r6,r3
  428. 0: dcbst 0,r3 /* Write line to ram */
  429. addi r3,r3,L1_CACHE_BYTES
  430. bdnz 0b
  431. sync
  432. #ifdef CONFIG_44x
  433. /* We don't flush the icache on 44x. Those have a virtual icache
  434. * and we don't have access to the virtual address here (it's
  435. * not the page vaddr but where it's mapped in user space). The
  436. * flushing of the icache on these is handled elsewhere, when
  437. * a change in the address space occurs, before returning to
  438. * user space
  439. */
  440. BEGIN_MMU_FTR_SECTION
  441. blr
  442. END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x)
  443. #endif /* CONFIG_44x */
  444. mtctr r4
  445. 1: icbi 0,r6
  446. addi r6,r6,L1_CACHE_BYTES
  447. bdnz 1b
  448. sync
  449. isync
  450. blr
  451. #ifndef CONFIG_BOOKE
  452. /*
  453. * Flush a particular page from the data cache to RAM, identified
  454. * by its physical address. We turn off the MMU so we can just use
  455. * the physical address (this may be a highmem page without a kernel
  456. * mapping).
  457. *
  458. * void __flush_dcache_icache_phys(unsigned long physaddr)
  459. */
  460. _GLOBAL(__flush_dcache_icache_phys)
  461. BEGIN_FTR_SECTION
  462. PURGE_PREFETCHED_INS
  463. blr /* for 601, do nothing */
  464. END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
  465. mfmsr r10
  466. rlwinm r0,r10,0,28,26 /* clear DR */
  467. mtmsr r0
  468. isync
  469. rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
  470. li r4,PAGE_SIZE/L1_CACHE_BYTES /* Number of lines in a page */
  471. mtctr r4
  472. mr r6,r3
  473. 0: dcbst 0,r3 /* Write line to ram */
  474. addi r3,r3,L1_CACHE_BYTES
  475. bdnz 0b
  476. sync
  477. mtctr r4
  478. 1: icbi 0,r6
  479. addi r6,r6,L1_CACHE_BYTES
  480. bdnz 1b
  481. sync
  482. mtmsr r10 /* restore DR */
  483. isync
  484. blr
  485. #endif /* CONFIG_BOOKE */
  486. /*
  487. * Clear pages using the dcbz instruction, which doesn't cause any
  488. * memory traffic (except to write out any cache lines which get
  489. * displaced). This only works on cacheable memory.
  490. *
  491. * void clear_pages(void *page, int order) ;
  492. */
  493. _GLOBAL(clear_pages)
  494. li r0,PAGE_SIZE/L1_CACHE_BYTES
  495. slw r0,r0,r4
  496. mtctr r0
  497. 1: dcbz 0,r3
  498. addi r3,r3,L1_CACHE_BYTES
  499. bdnz 1b
  500. blr
  501. /*
  502. * Copy a whole page. We use the dcbz instruction on the destination
  503. * to reduce memory traffic (it eliminates the unnecessary reads of
  504. * the destination into cache). This requires that the destination
  505. * is cacheable.
  506. */
  507. #define COPY_16_BYTES \
  508. lwz r6,4(r4); \
  509. lwz r7,8(r4); \
  510. lwz r8,12(r4); \
  511. lwzu r9,16(r4); \
  512. stw r6,4(r3); \
  513. stw r7,8(r3); \
  514. stw r8,12(r3); \
  515. stwu r9,16(r3)
  516. _GLOBAL(copy_page)
  517. addi r3,r3,-4
  518. addi r4,r4,-4
  519. li r5,4
  520. #if MAX_COPY_PREFETCH > 1
  521. li r0,MAX_COPY_PREFETCH
  522. li r11,4
  523. mtctr r0
  524. 11: dcbt r11,r4
  525. addi r11,r11,L1_CACHE_BYTES
  526. bdnz 11b
  527. #else /* MAX_COPY_PREFETCH == 1 */
  528. dcbt r5,r4
  529. li r11,L1_CACHE_BYTES+4
  530. #endif /* MAX_COPY_PREFETCH */
  531. li r0,PAGE_SIZE/L1_CACHE_BYTES - MAX_COPY_PREFETCH
  532. crclr 4*cr0+eq
  533. 2:
  534. mtctr r0
  535. 1:
  536. dcbt r11,r4
  537. dcbz r5,r3
  538. COPY_16_BYTES
  539. #if L1_CACHE_BYTES >= 32
  540. COPY_16_BYTES
  541. #if L1_CACHE_BYTES >= 64
  542. COPY_16_BYTES
  543. COPY_16_BYTES
  544. #if L1_CACHE_BYTES >= 128
  545. COPY_16_BYTES
  546. COPY_16_BYTES
  547. COPY_16_BYTES
  548. COPY_16_BYTES
  549. #endif
  550. #endif
  551. #endif
  552. bdnz 1b
  553. beqlr
  554. crnot 4*cr0+eq,4*cr0+eq
  555. li r0,MAX_COPY_PREFETCH
  556. li r11,4
  557. b 2b
  558. /*
  559. * Extended precision shifts.
  560. *
  561. * Updated to be valid for shift counts from 0 to 63 inclusive.
  562. * -- Gabriel
  563. *
  564. * R3/R4 has 64 bit value
  565. * R5 has shift count
  566. * result in R3/R4
  567. *
  568. * ashrdi3: arithmetic right shift (sign propagation)
  569. * lshrdi3: logical right shift
  570. * ashldi3: left shift
  571. */
  572. _GLOBAL(__ashrdi3)
  573. subfic r6,r5,32
  574. srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
  575. addi r7,r5,32 # could be xori, or addi with -32
  576. slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
  577. rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
  578. sraw r7,r3,r7 # t2 = MSW >> (count-32)
  579. or r4,r4,r6 # LSW |= t1
  580. slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
  581. sraw r3,r3,r5 # MSW = MSW >> count
  582. or r4,r4,r7 # LSW |= t2
  583. blr
  584. _GLOBAL(__ashldi3)
  585. subfic r6,r5,32
  586. slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
  587. addi r7,r5,32 # could be xori, or addi with -32
  588. srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
  589. slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
  590. or r3,r3,r6 # MSW |= t1
  591. slw r4,r4,r5 # LSW = LSW << count
  592. or r3,r3,r7 # MSW |= t2
  593. blr
  594. _GLOBAL(__lshrdi3)
  595. subfic r6,r5,32
  596. srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
  597. addi r7,r5,32 # could be xori, or addi with -32
  598. slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
  599. srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
  600. or r4,r4,r6 # LSW |= t1
  601. srw r3,r3,r5 # MSW = MSW >> count
  602. or r4,r4,r7 # LSW |= t2
  603. blr
  604. /*
  605. * 64-bit comparison: __cmpdi2(s64 a, s64 b)
  606. * Returns 0 if a < b, 1 if a == b, 2 if a > b.
  607. */
  608. _GLOBAL(__cmpdi2)
  609. cmpw r3,r5
  610. li r3,1
  611. bne 1f
  612. cmplw r4,r6
  613. beqlr
  614. 1: li r3,0
  615. bltlr
  616. li r3,2
  617. blr
  618. /*
  619. * 64-bit comparison: __ucmpdi2(u64 a, u64 b)
  620. * Returns 0 if a < b, 1 if a == b, 2 if a > b.
  621. */
  622. _GLOBAL(__ucmpdi2)
  623. cmplw r3,r5
  624. li r3,1
  625. bne 1f
  626. cmplw r4,r6
  627. beqlr
  628. 1: li r3,0
  629. bltlr
  630. li r3,2
  631. blr
  632. _GLOBAL(__bswapdi2)
  633. rotlwi r9,r4,8
  634. rotlwi r10,r3,8
  635. rlwimi r9,r4,24,0,7
  636. rlwimi r10,r3,24,0,7
  637. rlwimi r9,r4,24,16,23
  638. rlwimi r10,r3,24,16,23
  639. mr r3,r9
  640. mr r4,r10
  641. blr
  642. _GLOBAL(abs)
  643. srawi r4,r3,31
  644. xor r3,r3,r4
  645. sub r3,r3,r4
  646. blr
  647. #ifdef CONFIG_SMP
  648. _GLOBAL(start_secondary_resume)
  649. /* Reset stack */
  650. CURRENT_THREAD_INFO(r1, r1)
  651. addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
  652. li r3,0
  653. stw r3,0(r1) /* Zero the stack frame pointer */
  654. bl start_secondary
  655. b .
  656. #endif /* CONFIG_SMP */
  657. /*
  658. * This routine is just here to keep GCC happy - sigh...
  659. */
  660. _GLOBAL(__main)
  661. blr
  662. #ifdef CONFIG_KEXEC
  663. /*
  664. * Must be relocatable PIC code callable as a C function.
  665. */
  666. .globl relocate_new_kernel
  667. relocate_new_kernel:
  668. /* r3 = page_list */
  669. /* r4 = reboot_code_buffer */
  670. /* r5 = start_address */
  671. #ifdef CONFIG_FSL_BOOKE
  672. mr r29, r3
  673. mr r30, r4
  674. mr r31, r5
  675. #define ENTRY_MAPPING_KEXEC_SETUP
  676. #include "fsl_booke_entry_mapping.S"
  677. #undef ENTRY_MAPPING_KEXEC_SETUP
  678. mr r3, r29
  679. mr r4, r30
  680. mr r5, r31
  681. li r0, 0
  682. #elif defined(CONFIG_44x)
  683. /* Save our parameters */
  684. mr r29, r3
  685. mr r30, r4
  686. mr r31, r5
  687. #ifdef CONFIG_PPC_47x
  688. /* Check for 47x cores */
  689. mfspr r3,SPRN_PVR
  690. srwi r3,r3,16
  691. cmplwi cr0,r3,PVR_476FPE@h
  692. beq setup_map_47x
  693. cmplwi cr0,r3,PVR_476@h
  694. beq setup_map_47x
  695. cmplwi cr0,r3,PVR_476_ISS@h
  696. beq setup_map_47x
  697. #endif /* CONFIG_PPC_47x */
  698. /*
  699. * Code for setting up 1:1 mapping for PPC440x for KEXEC
  700. *
  701. * We cannot switch off the MMU on PPC44x.
  702. * So we:
  703. * 1) Invalidate all the mappings except the one we are running from.
  704. * 2) Create a tmp mapping for our code in the other address space(TS) and
  705. * jump to it. Invalidate the entry we started in.
  706. * 3) Create a 1:1 mapping for 0-2GiB in chunks of 256M in original TS.
  707. * 4) Jump to the 1:1 mapping in original TS.
  708. * 5) Invalidate the tmp mapping.
  709. *
  710. * - Based on the kexec support code for FSL BookE
  711. *
  712. */
  713. /*
  714. * Load the PID with kernel PID (0).
  715. * Also load our MSR_IS and TID to MMUCR for TLB search.
  716. */
  717. li r3, 0
  718. mtspr SPRN_PID, r3
  719. mfmsr r4
  720. andi. r4,r4,MSR_IS@l
  721. beq wmmucr
  722. oris r3,r3,PPC44x_MMUCR_STS@h
  723. wmmucr:
  724. mtspr SPRN_MMUCR,r3
  725. sync
  726. /*
  727. * Invalidate all the TLB entries except the current entry
  728. * where we are running from
  729. */
  730. bl 0f /* Find our address */
  731. 0: mflr r5 /* Make it accessible */
  732. tlbsx r23,0,r5 /* Find entry we are in */
  733. li r4,0 /* Start at TLB entry 0 */
  734. li r3,0 /* Set PAGEID inval value */
  735. 1: cmpw r23,r4 /* Is this our entry? */
  736. beq skip /* If so, skip the inval */
  737. tlbwe r3,r4,PPC44x_TLB_PAGEID /* If not, inval the entry */
  738. skip:
  739. addi r4,r4,1 /* Increment */
  740. cmpwi r4,64 /* Are we done? */
  741. bne 1b /* If not, repeat */
  742. isync
  743. /* Create a temp mapping and jump to it */
  744. andi. r6, r23, 1 /* Find the index to use */
  745. addi r24, r6, 1 /* r24 will contain 1 or 2 */
  746. mfmsr r9 /* get the MSR */
  747. rlwinm r5, r9, 27, 31, 31 /* Extract the MSR[IS] */
  748. xori r7, r5, 1 /* Use the other address space */
  749. /* Read the current mapping entries */
  750. tlbre r3, r23, PPC44x_TLB_PAGEID
  751. tlbre r4, r23, PPC44x_TLB_XLAT
  752. tlbre r5, r23, PPC44x_TLB_ATTRIB
  753. /* Save our current XLAT entry */
  754. mr r25, r4
  755. /* Extract the TLB PageSize */
  756. li r10, 1 /* r10 will hold PageSize */
  757. rlwinm r11, r3, 0, 24, 27 /* bits 24-27 */
  758. /* XXX: As of now we use 256M, 4K pages */
  759. cmpwi r11, PPC44x_TLB_256M
  760. bne tlb_4k
  761. rotlwi r10, r10, 28 /* r10 = 256M */
  762. b write_out
  763. tlb_4k:
  764. cmpwi r11, PPC44x_TLB_4K
  765. bne default
  766. rotlwi r10, r10, 12 /* r10 = 4K */
  767. b write_out
  768. default:
  769. rotlwi r10, r10, 10 /* r10 = 1K */
  770. write_out:
  771. /*
  772. * Write out the tmp 1:1 mapping for this code in other address space
  773. * Fixup EPN = RPN , TS=other address space
  774. */
  775. insrwi r3, r7, 1, 23 /* Bit 23 is TS for PAGEID field */
  776. /* Write out the tmp mapping entries */
  777. tlbwe r3, r24, PPC44x_TLB_PAGEID
  778. tlbwe r4, r24, PPC44x_TLB_XLAT
  779. tlbwe r5, r24, PPC44x_TLB_ATTRIB
  780. subi r11, r10, 1 /* PageOffset Mask = PageSize - 1 */
  781. not r10, r11 /* Mask for PageNum */
  782. /* Switch to other address space in MSR */
  783. insrwi r9, r7, 1, 26 /* Set MSR[IS] = r7 */
  784. bl 1f
  785. 1: mflr r8
  786. addi r8, r8, (2f-1b) /* Find the target offset */
  787. /* Jump to the tmp mapping */
  788. mtspr SPRN_SRR0, r8
  789. mtspr SPRN_SRR1, r9
  790. rfi
  791. 2:
  792. /* Invalidate the entry we were executing from */
  793. li r3, 0
  794. tlbwe r3, r23, PPC44x_TLB_PAGEID
  795. /* attribute fields. rwx for SUPERVISOR mode */
  796. li r5, 0
  797. ori r5, r5, (PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G)
  798. /* Create 1:1 mapping in 256M pages */
  799. xori r7, r7, 1 /* Revert back to Original TS */
  800. li r8, 0 /* PageNumber */
  801. li r6, 3 /* TLB Index, start at 3 */
  802. next_tlb:
  803. rotlwi r3, r8, 28 /* Create EPN (bits 0-3) */
  804. mr r4, r3 /* RPN = EPN */
  805. ori r3, r3, (PPC44x_TLB_VALID | PPC44x_TLB_256M) /* SIZE = 256M, Valid */
  806. insrwi r3, r7, 1, 23 /* Set TS from r7 */
  807. tlbwe r3, r6, PPC44x_TLB_PAGEID /* PageID field : EPN, V, SIZE */
  808. tlbwe r4, r6, PPC44x_TLB_XLAT /* Address translation : RPN */
  809. tlbwe r5, r6, PPC44x_TLB_ATTRIB /* Attributes */
  810. addi r8, r8, 1 /* Increment PN */
  811. addi r6, r6, 1 /* Increment TLB Index */
  812. cmpwi r8, 8 /* Are we done ? */
  813. bne next_tlb
  814. isync
  815. /* Jump to the new mapping 1:1 */
  816. li r9,0
  817. insrwi r9, r7, 1, 26 /* Set MSR[IS] = r7 */
  818. bl 1f
  819. 1: mflr r8
  820. and r8, r8, r11 /* Get our offset within page */
  821. addi r8, r8, (2f-1b)
  822. and r5, r25, r10 /* Get our target PageNum */
  823. or r8, r8, r5 /* Target jump address */
  824. mtspr SPRN_SRR0, r8
  825. mtspr SPRN_SRR1, r9
  826. rfi
  827. 2:
  828. /* Invalidate the tmp entry we used */
  829. li r3, 0
  830. tlbwe r3, r24, PPC44x_TLB_PAGEID
  831. sync
  832. b ppc44x_map_done
  833. #ifdef CONFIG_PPC_47x
  834. /* 1:1 mapping for 47x */
  835. setup_map_47x:
  836. /*
  837. * Load the kernel pid (0) to PID and also to MMUCR[TID].
  838. * Also set the MSR IS->MMUCR STS
  839. */
  840. li r3, 0
  841. mtspr SPRN_PID, r3 /* Set PID */
  842. mfmsr r4 /* Get MSR */
  843. andi. r4, r4, MSR_IS@l /* TS=1? */
  844. beq 1f /* If not, leave STS=0 */
  845. oris r3, r3, PPC47x_MMUCR_STS@h /* Set STS=1 */
  846. 1: mtspr SPRN_MMUCR, r3 /* Put MMUCR */
  847. sync
  848. /* Find the entry we are running from */
  849. bl 2f
  850. 2: mflr r23
  851. tlbsx r23, 0, r23
  852. tlbre r24, r23, 0 /* TLB Word 0 */
  853. tlbre r25, r23, 1 /* TLB Word 1 */
  854. tlbre r26, r23, 2 /* TLB Word 2 */
  855. /*
  856. * Invalidates all the tlb entries by writing to 256 RPNs(r4)
  857. * of 4k page size in all 4 ways (0-3 in r3).
  858. * This would invalidate the entire UTLB including the one we are
  859. * running from. However the shadow TLB entries would help us
  860. * to continue the execution, until we flush them (rfi/isync).
  861. */
  862. addis r3, 0, 0x8000 /* specify the way */
  863. addi r4, 0, 0 /* TLB Word0 = (EPN=0, VALID = 0) */
  864. addi r5, 0, 0
  865. b clear_utlb_entry
  866. /* Align the loop to speed things up. from head_44x.S */
  867. .align 6
  868. clear_utlb_entry:
  869. tlbwe r4, r3, 0
  870. tlbwe r5, r3, 1
  871. tlbwe r5, r3, 2
  872. addis r3, r3, 0x2000 /* Increment the way */
  873. cmpwi r3, 0
  874. bne clear_utlb_entry
  875. addis r3, 0, 0x8000
  876. addis r4, r4, 0x100 /* Increment the EPN */
  877. cmpwi r4, 0
  878. bne clear_utlb_entry
  879. /* Create the entries in the other address space */
  880. mfmsr r5
  881. rlwinm r7, r5, 27, 31, 31 /* Get the TS (Bit 26) from MSR */
  882. xori r7, r7, 1 /* r7 = !TS */
  883. insrwi r24, r7, 1, 21 /* Change the TS in the saved TLB word 0 */
  884. /*
  885. * write out the TLB entries for the tmp mapping
  886. * Use way '0' so that we could easily invalidate it later.
  887. */
  888. lis r3, 0x8000 /* Way '0' */
  889. tlbwe r24, r3, 0
  890. tlbwe r25, r3, 1
  891. tlbwe r26, r3, 2
  892. /* Update the msr to the new TS */
  893. insrwi r5, r7, 1, 26
  894. bl 1f
  895. 1: mflr r6
  896. addi r6, r6, (2f-1b)
  897. mtspr SPRN_SRR0, r6
  898. mtspr SPRN_SRR1, r5
  899. rfi
  900. /*
  901. * Now we are in the tmp address space.
  902. * Create a 1:1 mapping for 0-2GiB in the original TS.
  903. */
  904. 2:
  905. li r3, 0
  906. li r4, 0 /* TLB Word 0 */
  907. li r5, 0 /* TLB Word 1 */
  908. li r6, 0
  909. ori r6, r6, PPC47x_TLB2_S_RWX /* TLB word 2 */
  910. li r8, 0 /* PageIndex */
  911. xori r7, r7, 1 /* revert back to original TS */
  912. write_utlb:
  913. rotlwi r5, r8, 28 /* RPN = PageIndex * 256M */
  914. /* ERPN = 0 as we don't use memory above 2G */
  915. mr r4, r5 /* EPN = RPN */
  916. ori r4, r4, (PPC47x_TLB0_VALID | PPC47x_TLB0_256M)
  917. insrwi r4, r7, 1, 21 /* Insert the TS to Word 0 */
  918. tlbwe r4, r3, 0 /* Write out the entries */
  919. tlbwe r5, r3, 1
  920. tlbwe r6, r3, 2
  921. addi r8, r8, 1
  922. cmpwi r8, 8 /* Have we completed ? */
  923. bne write_utlb
  924. /* make sure we complete the TLB write up */
  925. isync
  926. /*
  927. * Prepare to jump to the 1:1 mapping.
  928. * 1) Extract page size of the tmp mapping
  929. * DSIZ = TLB_Word0[22:27]
  930. * 2) Calculate the physical address of the address
  931. * to jump to.
  932. */
  933. rlwinm r10, r24, 0, 22, 27
  934. cmpwi r10, PPC47x_TLB0_4K
  935. bne 0f
  936. li r10, 0x1000 /* r10 = 4k */
  937. bl 1f
  938. 0:
  939. /* Defaults to 256M */
  940. lis r10, 0x1000
  941. bl 1f
  942. 1: mflr r4
  943. addi r4, r4, (2f-1b) /* virtual address of 2f */
  944. subi r11, r10, 1 /* offsetmask = Pagesize - 1 */
  945. not r10, r11 /* Pagemask = ~(offsetmask) */
  946. and r5, r25, r10 /* Physical page */
  947. and r6, r4, r11 /* offset within the current page */
  948. or r5, r5, r6 /* Physical address for 2f */
  949. /* Switch the TS in MSR to the original one */
  950. mfmsr r8
  951. insrwi r8, r7, 1, 26
  952. mtspr SPRN_SRR1, r8
  953. mtspr SPRN_SRR0, r5
  954. rfi
  955. 2:
  956. /* Invalidate the tmp mapping */
  957. lis r3, 0x8000 /* Way '0' */
  958. clrrwi r24, r24, 12 /* Clear the valid bit */
  959. tlbwe r24, r3, 0
  960. tlbwe r25, r3, 1
  961. tlbwe r26, r3, 2
  962. /* Make sure we complete the TLB write and flush the shadow TLB */
  963. isync
  964. #endif
  965. ppc44x_map_done:
  966. /* Restore the parameters */
  967. mr r3, r29
  968. mr r4, r30
  969. mr r5, r31
  970. li r0, 0
  971. #else
  972. li r0, 0
  973. /*
  974. * Set Machine Status Register to a known status,
  975. * switch the MMU off and jump to 1: in a single step.
  976. */
  977. mr r8, r0
  978. ori r8, r8, MSR_RI|MSR_ME
  979. mtspr SPRN_SRR1, r8
  980. addi r8, r4, 1f - relocate_new_kernel
  981. mtspr SPRN_SRR0, r8
  982. sync
  983. rfi
  984. 1:
  985. #endif
  986. /* from this point address translation is turned off */
  987. /* and interrupts are disabled */
  988. /* set a new stack at the bottom of our page... */
  989. /* (not really needed now) */
  990. addi r1, r4, KEXEC_CONTROL_PAGE_SIZE - 8 /* for LR Save+Back Chain */
  991. stw r0, 0(r1)
  992. /* Do the copies */
  993. li r6, 0 /* checksum */
  994. mr r0, r3
  995. b 1f
  996. 0: /* top, read another word for the indirection page */
  997. lwzu r0, 4(r3)
  998. 1:
  999. /* is it a destination page? (r8) */
  1000. rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
  1001. beq 2f
  1002. rlwinm r8, r0, 0, 0, 19 /* clear kexec flags, page align */
  1003. b 0b
  1004. 2: /* is it an indirection page? (r3) */
  1005. rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
  1006. beq 2f
  1007. rlwinm r3, r0, 0, 0, 19 /* clear kexec flags, page align */
  1008. subi r3, r3, 4
  1009. b 0b
  1010. 2: /* are we done? */
  1011. rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
  1012. beq 2f
  1013. b 3f
  1014. 2: /* is it a source page? (r9) */
  1015. rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
  1016. beq 0b
  1017. rlwinm r9, r0, 0, 0, 19 /* clear kexec flags, page align */
  1018. li r7, PAGE_SIZE / 4
  1019. mtctr r7
  1020. subi r9, r9, 4
  1021. subi r8, r8, 4
  1022. 9:
  1023. lwzu r0, 4(r9) /* do the copy */
  1024. xor r6, r6, r0
  1025. stwu r0, 4(r8)
  1026. dcbst 0, r8
  1027. sync
  1028. icbi 0, r8
  1029. bdnz 9b
  1030. addi r9, r9, 4
  1031. addi r8, r8, 4
  1032. b 0b
  1033. 3:
  1034. /* To be certain of avoiding problems with self-modifying code
  1035. * execute a serializing instruction here.
  1036. */
  1037. isync
  1038. sync
  1039. mfspr r3, SPRN_PIR /* current core we are running on */
  1040. mr r4, r5 /* load physical address of chunk called */
  1041. /* jump to the entry point, usually the setup routine */
  1042. mtlr r5
  1043. blrl
  1044. 1: b 1b
  1045. relocate_new_kernel_end:
  1046. .globl relocate_new_kernel_size
  1047. relocate_new_kernel_size:
  1048. .long relocate_new_kernel_end - relocate_new_kernel
  1049. #endif