hash_low_32.S 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
  5. * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
  6. * Adapted for Power Macintosh by Paul Mackerras.
  7. * Low-level exception handlers and MMU support
  8. * rewritten by Paul Mackerras.
  9. * Copyright (C) 1996 Paul Mackerras.
  10. *
  11. * This file contains low-level assembler routines for managing
  12. * the PowerPC MMU hash table. (PPC 8xx processors don't use a
  13. * hash table, so this file is not used on them.)
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. */
  21. #include <asm/reg.h>
  22. #include <asm/page.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/cputable.h>
  25. #include <asm/ppc_asm.h>
  26. #include <asm/thread_info.h>
  27. #include <asm/asm-offsets.h>
  28. #include <asm/export.h>
  29. #include <asm/feature-fixups.h>
  30. #ifdef CONFIG_SMP
  31. .section .bss
  32. .align 2
  33. mmu_hash_lock:
  34. .space 4
  35. #endif /* CONFIG_SMP */
  36. /*
  37. * Load a PTE into the hash table, if possible.
  38. * The address is in r4, and r3 contains an access flag:
  39. * _PAGE_RW (0x400) if a write.
  40. * r9 contains the SRR1 value, from which we use the MSR_PR bit.
  41. * SPRG_THREAD contains the physical address of the current task's thread.
  42. *
  43. * Returns to the caller if the access is illegal or there is no
  44. * mapping for the address. Otherwise it places an appropriate PTE
  45. * in the hash table and returns from the exception.
  46. * Uses r0, r3 - r8, r10, ctr, lr.
  47. */
  48. .text
  49. _GLOBAL(hash_page)
  50. tophys(r7,0) /* gets -KERNELBASE into r7 */
  51. #ifdef CONFIG_SMP
  52. addis r8,r7,mmu_hash_lock@h
  53. ori r8,r8,mmu_hash_lock@l
  54. lis r0,0x0fff
  55. b 10f
  56. 11: lwz r6,0(r8)
  57. cmpwi 0,r6,0
  58. bne 11b
  59. 10: lwarx r6,0,r8
  60. cmpwi 0,r6,0
  61. bne- 11b
  62. stwcx. r0,0,r8
  63. bne- 10b
  64. isync
  65. #endif
  66. /* Get PTE (linux-style) and check access */
  67. lis r0,KERNELBASE@h /* check if kernel address */
  68. cmplw 0,r4,r0
  69. mfspr r8,SPRN_SPRG_THREAD /* current task's THREAD (phys) */
  70. ori r3,r3,_PAGE_USER|_PAGE_PRESENT /* test low addresses as user */
  71. lwz r5,PGDIR(r8) /* virt page-table root */
  72. blt+ 112f /* assume user more likely */
  73. lis r5,swapper_pg_dir@ha /* if kernel address, use */
  74. addi r5,r5,swapper_pg_dir@l /* kernel page table */
  75. rlwimi r3,r9,32-12,29,29 /* MSR_PR -> _PAGE_USER */
  76. 112: add r5,r5,r7 /* convert to phys addr */
  77. #ifndef CONFIG_PTE_64BIT
  78. rlwimi r5,r4,12,20,29 /* insert top 10 bits of address */
  79. lwz r8,0(r5) /* get pmd entry */
  80. rlwinm. r8,r8,0,0,19 /* extract address of pte page */
  81. #else
  82. rlwinm r8,r4,13,19,29 /* Compute pgdir/pmd offset */
  83. lwzx r8,r8,r5 /* Get L1 entry */
  84. rlwinm. r8,r8,0,0,20 /* extract pt base address */
  85. #endif
  86. #ifdef CONFIG_SMP
  87. beq- hash_page_out /* return if no mapping */
  88. #else
  89. /* XXX it seems like the 601 will give a machine fault on the
  90. rfi if its alignment is wrong (bottom 4 bits of address are
  91. 8 or 0xc) and we have had a not-taken conditional branch
  92. to the address following the rfi. */
  93. beqlr-
  94. #endif
  95. #ifndef CONFIG_PTE_64BIT
  96. rlwimi r8,r4,22,20,29 /* insert next 10 bits of address */
  97. #else
  98. rlwimi r8,r4,23,20,28 /* compute pte address */
  99. #endif
  100. rlwinm r0,r3,32-3,24,24 /* _PAGE_RW access -> _PAGE_DIRTY */
  101. ori r0,r0,_PAGE_ACCESSED|_PAGE_HASHPTE
  102. /*
  103. * Update the linux PTE atomically. We do the lwarx up-front
  104. * because almost always, there won't be a permission violation
  105. * and there won't already be an HPTE, and thus we will have
  106. * to update the PTE to set _PAGE_HASHPTE. -- paulus.
  107. *
  108. * If PTE_64BIT is set, the low word is the flags word; use that
  109. * word for locking since it contains all the interesting bits.
  110. */
  111. #if (PTE_FLAGS_OFFSET != 0)
  112. addi r8,r8,PTE_FLAGS_OFFSET
  113. #endif
  114. retry:
  115. lwarx r6,0,r8 /* get linux-style pte, flag word */
  116. andc. r5,r3,r6 /* check access & ~permission */
  117. #ifdef CONFIG_SMP
  118. bne- hash_page_out /* return if access not permitted */
  119. #else
  120. bnelr-
  121. #endif
  122. or r5,r0,r6 /* set accessed/dirty bits */
  123. #ifdef CONFIG_PTE_64BIT
  124. #ifdef CONFIG_SMP
  125. subf r10,r6,r8 /* create false data dependency */
  126. subi r10,r10,PTE_FLAGS_OFFSET
  127. lwzx r10,r6,r10 /* Get upper PTE word */
  128. #else
  129. lwz r10,-PTE_FLAGS_OFFSET(r8)
  130. #endif /* CONFIG_SMP */
  131. #endif /* CONFIG_PTE_64BIT */
  132. stwcx. r5,0,r8 /* attempt to update PTE */
  133. bne- retry /* retry if someone got there first */
  134. mfsrin r3,r4 /* get segment reg for segment */
  135. mfctr r0
  136. stw r0,_CTR(r11)
  137. bl create_hpte /* add the hash table entry */
  138. #ifdef CONFIG_SMP
  139. eieio
  140. addis r8,r7,mmu_hash_lock@ha
  141. li r0,0
  142. stw r0,mmu_hash_lock@l(r8)
  143. #endif
  144. /* Return from the exception */
  145. lwz r5,_CTR(r11)
  146. mtctr r5
  147. lwz r0,GPR0(r11)
  148. lwz r7,GPR7(r11)
  149. lwz r8,GPR8(r11)
  150. b fast_exception_return
  151. #ifdef CONFIG_SMP
  152. hash_page_out:
  153. eieio
  154. addis r8,r7,mmu_hash_lock@ha
  155. li r0,0
  156. stw r0,mmu_hash_lock@l(r8)
  157. blr
  158. #endif /* CONFIG_SMP */
  159. /*
  160. * Add an entry for a particular page to the hash table.
  161. *
  162. * add_hash_page(unsigned context, unsigned long va, unsigned long pmdval)
  163. *
  164. * We assume any necessary modifications to the pte (e.g. setting
  165. * the accessed bit) have already been done and that there is actually
  166. * a hash table in use (i.e. we're not on a 603).
  167. */
  168. _GLOBAL(add_hash_page)
  169. mflr r0
  170. stw r0,4(r1)
  171. /* Convert context and va to VSID */
  172. mulli r3,r3,897*16 /* multiply context by context skew */
  173. rlwinm r0,r4,4,28,31 /* get ESID (top 4 bits of va) */
  174. mulli r0,r0,0x111 /* multiply by ESID skew */
  175. add r3,r3,r0 /* note create_hpte trims to 24 bits */
  176. #ifdef CONFIG_SMP
  177. CURRENT_THREAD_INFO(r8, r1) /* use cpu number to make tag */
  178. lwz r8,TI_CPU(r8) /* to go in mmu_hash_lock */
  179. oris r8,r8,12
  180. #endif /* CONFIG_SMP */
  181. /*
  182. * We disable interrupts here, even on UP, because we don't
  183. * want to race with hash_page, and because we want the
  184. * _PAGE_HASHPTE bit to be a reliable indication of whether
  185. * the HPTE exists (or at least whether one did once).
  186. * We also turn off the MMU for data accesses so that we
  187. * we can't take a hash table miss (assuming the code is
  188. * covered by a BAT). -- paulus
  189. */
  190. mfmsr r9
  191. SYNC
  192. rlwinm r0,r9,0,17,15 /* clear bit 16 (MSR_EE) */
  193. rlwinm r0,r0,0,28,26 /* clear MSR_DR */
  194. mtmsr r0
  195. SYNC_601
  196. isync
  197. tophys(r7,0)
  198. #ifdef CONFIG_SMP
  199. addis r6,r7,mmu_hash_lock@ha
  200. addi r6,r6,mmu_hash_lock@l
  201. 10: lwarx r0,0,r6 /* take the mmu_hash_lock */
  202. cmpi 0,r0,0
  203. bne- 11f
  204. stwcx. r8,0,r6
  205. beq+ 12f
  206. 11: lwz r0,0(r6)
  207. cmpi 0,r0,0
  208. beq 10b
  209. b 11b
  210. 12: isync
  211. #endif
  212. /*
  213. * Fetch the linux pte and test and set _PAGE_HASHPTE atomically.
  214. * If _PAGE_HASHPTE was already set, we don't replace the existing
  215. * HPTE, so we just unlock and return.
  216. */
  217. mr r8,r5
  218. #ifndef CONFIG_PTE_64BIT
  219. rlwimi r8,r4,22,20,29
  220. #else
  221. rlwimi r8,r4,23,20,28
  222. addi r8,r8,PTE_FLAGS_OFFSET
  223. #endif
  224. 1: lwarx r6,0,r8
  225. andi. r0,r6,_PAGE_HASHPTE
  226. bne 9f /* if HASHPTE already set, done */
  227. #ifdef CONFIG_PTE_64BIT
  228. #ifdef CONFIG_SMP
  229. subf r10,r6,r8 /* create false data dependency */
  230. subi r10,r10,PTE_FLAGS_OFFSET
  231. lwzx r10,r6,r10 /* Get upper PTE word */
  232. #else
  233. lwz r10,-PTE_FLAGS_OFFSET(r8)
  234. #endif /* CONFIG_SMP */
  235. #endif /* CONFIG_PTE_64BIT */
  236. ori r5,r6,_PAGE_HASHPTE
  237. stwcx. r5,0,r8
  238. bne- 1b
  239. bl create_hpte
  240. 9:
  241. #ifdef CONFIG_SMP
  242. addis r6,r7,mmu_hash_lock@ha
  243. addi r6,r6,mmu_hash_lock@l
  244. eieio
  245. li r0,0
  246. stw r0,0(r6) /* clear mmu_hash_lock */
  247. #endif
  248. /* reenable interrupts and DR */
  249. mtmsr r9
  250. SYNC_601
  251. isync
  252. lwz r0,4(r1)
  253. mtlr r0
  254. blr
  255. /*
  256. * This routine adds a hardware PTE to the hash table.
  257. * It is designed to be called with the MMU either on or off.
  258. * r3 contains the VSID, r4 contains the virtual address,
  259. * r5 contains the linux PTE, r6 contains the old value of the
  260. * linux PTE (before setting _PAGE_HASHPTE) and r7 contains the
  261. * offset to be added to addresses (0 if the MMU is on,
  262. * -KERNELBASE if it is off). r10 contains the upper half of
  263. * the PTE if CONFIG_PTE_64BIT.
  264. * On SMP, the caller should have the mmu_hash_lock held.
  265. * We assume that the caller has (or will) set the _PAGE_HASHPTE
  266. * bit in the linux PTE in memory. The value passed in r6 should
  267. * be the old linux PTE value; if it doesn't have _PAGE_HASHPTE set
  268. * this routine will skip the search for an existing HPTE.
  269. * This procedure modifies r0, r3 - r6, r8, cr0.
  270. * -- paulus.
  271. *
  272. * For speed, 4 of the instructions get patched once the size and
  273. * physical address of the hash table are known. These definitions
  274. * of Hash_base and Hash_bits below are just an example.
  275. */
  276. Hash_base = 0xc0180000
  277. Hash_bits = 12 /* e.g. 256kB hash table */
  278. Hash_msk = (((1 << Hash_bits) - 1) * 64)
  279. /* defines for the PTE format for 32-bit PPCs */
  280. #define HPTE_SIZE 8
  281. #define PTEG_SIZE 64
  282. #define LG_PTEG_SIZE 6
  283. #define LDPTEu lwzu
  284. #define LDPTE lwz
  285. #define STPTE stw
  286. #define CMPPTE cmpw
  287. #define PTE_H 0x40
  288. #define PTE_V 0x80000000
  289. #define TST_V(r) rlwinm. r,r,0,0,0
  290. #define SET_V(r) oris r,r,PTE_V@h
  291. #define CLR_V(r,t) rlwinm r,r,0,1,31
  292. #define HASH_LEFT 31-(LG_PTEG_SIZE+Hash_bits-1)
  293. #define HASH_RIGHT 31-LG_PTEG_SIZE
  294. _GLOBAL(create_hpte)
  295. /* Convert linux-style PTE (r5) to low word of PPC-style PTE (r8) */
  296. rlwinm r8,r5,32-10,31,31 /* _PAGE_RW -> PP lsb */
  297. rlwinm r0,r5,32-7,31,31 /* _PAGE_DIRTY -> PP lsb */
  298. and r8,r8,r0 /* writable if _RW & _DIRTY */
  299. rlwimi r5,r5,32-1,30,30 /* _PAGE_USER -> PP msb */
  300. rlwimi r5,r5,32-2,31,31 /* _PAGE_USER -> PP lsb */
  301. ori r8,r8,0xe04 /* clear out reserved bits */
  302. andc r8,r5,r8 /* PP = user? (rw&dirty? 2: 3): 0 */
  303. BEGIN_FTR_SECTION
  304. rlwinm r8,r8,0,~_PAGE_COHERENT /* clear M (coherence not required) */
  305. END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
  306. #ifdef CONFIG_PTE_64BIT
  307. /* Put the XPN bits into the PTE */
  308. rlwimi r8,r10,8,20,22
  309. rlwimi r8,r10,2,29,29
  310. #endif
  311. /* Construct the high word of the PPC-style PTE (r5) */
  312. rlwinm r5,r3,7,1,24 /* put VSID in 0x7fffff80 bits */
  313. rlwimi r5,r4,10,26,31 /* put in API (abbrev page index) */
  314. SET_V(r5) /* set V (valid) bit */
  315. /* Get the address of the primary PTE group in the hash table (r3) */
  316. _GLOBAL(hash_page_patch_A)
  317. addis r0,r7,Hash_base@h /* base address of hash table */
  318. rlwimi r0,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* VSID -> hash */
  319. rlwinm r3,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */
  320. xor r3,r3,r0 /* make primary hash */
  321. li r0,8 /* PTEs/group */
  322. /*
  323. * Test the _PAGE_HASHPTE bit in the old linux PTE, and skip the search
  324. * if it is clear, meaning that the HPTE isn't there already...
  325. */
  326. andi. r6,r6,_PAGE_HASHPTE
  327. beq+ 10f /* no PTE: go look for an empty slot */
  328. tlbie r4
  329. addis r4,r7,htab_hash_searches@ha
  330. lwz r6,htab_hash_searches@l(r4)
  331. addi r6,r6,1 /* count how many searches we do */
  332. stw r6,htab_hash_searches@l(r4)
  333. /* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */
  334. mtctr r0
  335. addi r4,r3,-HPTE_SIZE
  336. 1: LDPTEu r6,HPTE_SIZE(r4) /* get next PTE */
  337. CMPPTE 0,r6,r5
  338. bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */
  339. beq+ found_slot
  340. /* Search the secondary PTEG for a matching PTE */
  341. ori r5,r5,PTE_H /* set H (secondary hash) bit */
  342. _GLOBAL(hash_page_patch_B)
  343. xoris r4,r3,Hash_msk>>16 /* compute secondary hash */
  344. xori r4,r4,(-PTEG_SIZE & 0xffff)
  345. addi r4,r4,-HPTE_SIZE
  346. mtctr r0
  347. 2: LDPTEu r6,HPTE_SIZE(r4)
  348. CMPPTE 0,r6,r5
  349. bdnzf 2,2b
  350. beq+ found_slot
  351. xori r5,r5,PTE_H /* clear H bit again */
  352. /* Search the primary PTEG for an empty slot */
  353. 10: mtctr r0
  354. addi r4,r3,-HPTE_SIZE /* search primary PTEG */
  355. 1: LDPTEu r6,HPTE_SIZE(r4) /* get next PTE */
  356. TST_V(r6) /* test valid bit */
  357. bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */
  358. beq+ found_empty
  359. /* update counter of times that the primary PTEG is full */
  360. addis r4,r7,primary_pteg_full@ha
  361. lwz r6,primary_pteg_full@l(r4)
  362. addi r6,r6,1
  363. stw r6,primary_pteg_full@l(r4)
  364. /* Search the secondary PTEG for an empty slot */
  365. ori r5,r5,PTE_H /* set H (secondary hash) bit */
  366. _GLOBAL(hash_page_patch_C)
  367. xoris r4,r3,Hash_msk>>16 /* compute secondary hash */
  368. xori r4,r4,(-PTEG_SIZE & 0xffff)
  369. addi r4,r4,-HPTE_SIZE
  370. mtctr r0
  371. 2: LDPTEu r6,HPTE_SIZE(r4)
  372. TST_V(r6)
  373. bdnzf 2,2b
  374. beq+ found_empty
  375. xori r5,r5,PTE_H /* clear H bit again */
  376. /*
  377. * Choose an arbitrary slot in the primary PTEG to overwrite.
  378. * Since both the primary and secondary PTEGs are full, and we
  379. * have no information that the PTEs in the primary PTEG are
  380. * more important or useful than those in the secondary PTEG,
  381. * and we know there is a definite (although small) speed
  382. * advantage to putting the PTE in the primary PTEG, we always
  383. * put the PTE in the primary PTEG.
  384. *
  385. * In addition, we skip any slot that is mapping kernel text in
  386. * order to avoid a deadlock when not using BAT mappings if
  387. * trying to hash in the kernel hash code itself after it has
  388. * already taken the hash table lock. This works in conjunction
  389. * with pre-faulting of the kernel text.
  390. *
  391. * If the hash table bucket is full of kernel text entries, we'll
  392. * lockup here but that shouldn't happen
  393. */
  394. 1: addis r4,r7,next_slot@ha /* get next evict slot */
  395. lwz r6,next_slot@l(r4)
  396. addi r6,r6,HPTE_SIZE /* search for candidate */
  397. andi. r6,r6,7*HPTE_SIZE
  398. stw r6,next_slot@l(r4)
  399. add r4,r3,r6
  400. LDPTE r0,HPTE_SIZE/2(r4) /* get PTE second word */
  401. clrrwi r0,r0,12
  402. lis r6,etext@h
  403. ori r6,r6,etext@l /* get etext */
  404. tophys(r6,r6)
  405. cmpl cr0,r0,r6 /* compare and try again */
  406. blt 1b
  407. #ifndef CONFIG_SMP
  408. /* Store PTE in PTEG */
  409. found_empty:
  410. STPTE r5,0(r4)
  411. found_slot:
  412. STPTE r8,HPTE_SIZE/2(r4)
  413. #else /* CONFIG_SMP */
  414. /*
  415. * Between the tlbie above and updating the hash table entry below,
  416. * another CPU could read the hash table entry and put it in its TLB.
  417. * There are 3 cases:
  418. * 1. using an empty slot
  419. * 2. updating an earlier entry to change permissions (i.e. enable write)
  420. * 3. taking over the PTE for an unrelated address
  421. *
  422. * In each case it doesn't really matter if the other CPUs have the old
  423. * PTE in their TLB. So we don't need to bother with another tlbie here,
  424. * which is convenient as we've overwritten the register that had the
  425. * address. :-) The tlbie above is mainly to make sure that this CPU comes
  426. * and gets the new PTE from the hash table.
  427. *
  428. * We do however have to make sure that the PTE is never in an invalid
  429. * state with the V bit set.
  430. */
  431. found_empty:
  432. found_slot:
  433. CLR_V(r5,r0) /* clear V (valid) bit in PTE */
  434. STPTE r5,0(r4)
  435. sync
  436. TLBSYNC
  437. STPTE r8,HPTE_SIZE/2(r4) /* put in correct RPN, WIMG, PP bits */
  438. sync
  439. SET_V(r5)
  440. STPTE r5,0(r4) /* finally set V bit in PTE */
  441. #endif /* CONFIG_SMP */
  442. sync /* make sure pte updates get to memory */
  443. blr
  444. .section .bss
  445. .align 2
  446. next_slot:
  447. .space 4
  448. primary_pteg_full:
  449. .space 4
  450. htab_hash_searches:
  451. .space 4
  452. .previous
  453. /*
  454. * Flush the entry for a particular page from the hash table.
  455. *
  456. * flush_hash_pages(unsigned context, unsigned long va, unsigned long pmdval,
  457. * int count)
  458. *
  459. * We assume that there is a hash table in use (Hash != 0).
  460. */
  461. _GLOBAL(flush_hash_pages)
  462. tophys(r7,0)
  463. /*
  464. * We disable interrupts here, even on UP, because we want
  465. * the _PAGE_HASHPTE bit to be a reliable indication of
  466. * whether the HPTE exists (or at least whether one did once).
  467. * We also turn off the MMU for data accesses so that we
  468. * we can't take a hash table miss (assuming the code is
  469. * covered by a BAT). -- paulus
  470. */
  471. mfmsr r10
  472. SYNC
  473. rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
  474. rlwinm r0,r0,0,28,26 /* clear MSR_DR */
  475. mtmsr r0
  476. SYNC_601
  477. isync
  478. /* First find a PTE in the range that has _PAGE_HASHPTE set */
  479. #ifndef CONFIG_PTE_64BIT
  480. rlwimi r5,r4,22,20,29
  481. #else
  482. rlwimi r5,r4,23,20,28
  483. #endif
  484. 1: lwz r0,PTE_FLAGS_OFFSET(r5)
  485. cmpwi cr1,r6,1
  486. andi. r0,r0,_PAGE_HASHPTE
  487. bne 2f
  488. ble cr1,19f
  489. addi r4,r4,0x1000
  490. addi r5,r5,PTE_SIZE
  491. addi r6,r6,-1
  492. b 1b
  493. /* Convert context and va to VSID */
  494. 2: mulli r3,r3,897*16 /* multiply context by context skew */
  495. rlwinm r0,r4,4,28,31 /* get ESID (top 4 bits of va) */
  496. mulli r0,r0,0x111 /* multiply by ESID skew */
  497. add r3,r3,r0 /* note code below trims to 24 bits */
  498. /* Construct the high word of the PPC-style PTE (r11) */
  499. rlwinm r11,r3,7,1,24 /* put VSID in 0x7fffff80 bits */
  500. rlwimi r11,r4,10,26,31 /* put in API (abbrev page index) */
  501. SET_V(r11) /* set V (valid) bit */
  502. #ifdef CONFIG_SMP
  503. addis r9,r7,mmu_hash_lock@ha
  504. addi r9,r9,mmu_hash_lock@l
  505. CURRENT_THREAD_INFO(r8, r1)
  506. add r8,r8,r7
  507. lwz r8,TI_CPU(r8)
  508. oris r8,r8,9
  509. 10: lwarx r0,0,r9
  510. cmpi 0,r0,0
  511. bne- 11f
  512. stwcx. r8,0,r9
  513. beq+ 12f
  514. 11: lwz r0,0(r9)
  515. cmpi 0,r0,0
  516. beq 10b
  517. b 11b
  518. 12: isync
  519. #endif
  520. /*
  521. * Check the _PAGE_HASHPTE bit in the linux PTE. If it is
  522. * already clear, we're done (for this pte). If not,
  523. * clear it (atomically) and proceed. -- paulus.
  524. */
  525. #if (PTE_FLAGS_OFFSET != 0)
  526. addi r5,r5,PTE_FLAGS_OFFSET
  527. #endif
  528. 33: lwarx r8,0,r5 /* fetch the pte flags word */
  529. andi. r0,r8,_PAGE_HASHPTE
  530. beq 8f /* done if HASHPTE is already clear */
  531. rlwinm r8,r8,0,31,29 /* clear HASHPTE bit */
  532. stwcx. r8,0,r5 /* update the pte */
  533. bne- 33b
  534. /* Get the address of the primary PTE group in the hash table (r3) */
  535. _GLOBAL(flush_hash_patch_A)
  536. addis r8,r7,Hash_base@h /* base address of hash table */
  537. rlwimi r8,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* VSID -> hash */
  538. rlwinm r0,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */
  539. xor r8,r0,r8 /* make primary hash */
  540. /* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */
  541. li r0,8 /* PTEs/group */
  542. mtctr r0
  543. addi r12,r8,-HPTE_SIZE
  544. 1: LDPTEu r0,HPTE_SIZE(r12) /* get next PTE */
  545. CMPPTE 0,r0,r11
  546. bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */
  547. beq+ 3f
  548. /* Search the secondary PTEG for a matching PTE */
  549. ori r11,r11,PTE_H /* set H (secondary hash) bit */
  550. li r0,8 /* PTEs/group */
  551. _GLOBAL(flush_hash_patch_B)
  552. xoris r12,r8,Hash_msk>>16 /* compute secondary hash */
  553. xori r12,r12,(-PTEG_SIZE & 0xffff)
  554. addi r12,r12,-HPTE_SIZE
  555. mtctr r0
  556. 2: LDPTEu r0,HPTE_SIZE(r12)
  557. CMPPTE 0,r0,r11
  558. bdnzf 2,2b
  559. xori r11,r11,PTE_H /* clear H again */
  560. bne- 4f /* should rarely fail to find it */
  561. 3: li r0,0
  562. STPTE r0,0(r12) /* invalidate entry */
  563. 4: sync
  564. tlbie r4 /* in hw tlb too */
  565. sync
  566. 8: ble cr1,9f /* if all ptes checked */
  567. 81: addi r6,r6,-1
  568. addi r5,r5,PTE_SIZE
  569. addi r4,r4,0x1000
  570. lwz r0,0(r5) /* check next pte */
  571. cmpwi cr1,r6,1
  572. andi. r0,r0,_PAGE_HASHPTE
  573. bne 33b
  574. bgt cr1,81b
  575. 9:
  576. #ifdef CONFIG_SMP
  577. TLBSYNC
  578. li r0,0
  579. stw r0,0(r9) /* clear mmu_hash_lock */
  580. #endif
  581. 19: mtmsr r10
  582. SYNC_601
  583. isync
  584. blr
  585. EXPORT_SYMBOL(flush_hash_pages)
  586. /*
  587. * Flush an entry from the TLB
  588. */
  589. _GLOBAL(_tlbie)
  590. #ifdef CONFIG_SMP
  591. CURRENT_THREAD_INFO(r8, r1)
  592. lwz r8,TI_CPU(r8)
  593. oris r8,r8,11
  594. mfmsr r10
  595. SYNC
  596. rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
  597. rlwinm r0,r0,0,28,26 /* clear DR */
  598. mtmsr r0
  599. SYNC_601
  600. isync
  601. lis r9,mmu_hash_lock@h
  602. ori r9,r9,mmu_hash_lock@l
  603. tophys(r9,r9)
  604. 10: lwarx r7,0,r9
  605. cmpwi 0,r7,0
  606. bne- 10b
  607. stwcx. r8,0,r9
  608. bne- 10b
  609. eieio
  610. tlbie r3
  611. sync
  612. TLBSYNC
  613. li r0,0
  614. stw r0,0(r9) /* clear mmu_hash_lock */
  615. mtmsr r10
  616. SYNC_601
  617. isync
  618. #else /* CONFIG_SMP */
  619. tlbie r3
  620. sync
  621. #endif /* CONFIG_SMP */
  622. blr
  623. /*
  624. * Flush the entire TLB. 603/603e only
  625. */
  626. _GLOBAL(_tlbia)
  627. #if defined(CONFIG_SMP)
  628. CURRENT_THREAD_INFO(r8, r1)
  629. lwz r8,TI_CPU(r8)
  630. oris r8,r8,10
  631. mfmsr r10
  632. SYNC
  633. rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
  634. rlwinm r0,r0,0,28,26 /* clear DR */
  635. mtmsr r0
  636. SYNC_601
  637. isync
  638. lis r9,mmu_hash_lock@h
  639. ori r9,r9,mmu_hash_lock@l
  640. tophys(r9,r9)
  641. 10: lwarx r7,0,r9
  642. cmpwi 0,r7,0
  643. bne- 10b
  644. stwcx. r8,0,r9
  645. bne- 10b
  646. sync
  647. tlbia
  648. sync
  649. TLBSYNC
  650. li r0,0
  651. stw r0,0(r9) /* clear mmu_hash_lock */
  652. mtmsr r10
  653. SYNC_601
  654. isync
  655. #else /* CONFIG_SMP */
  656. sync
  657. tlbia
  658. sync
  659. #endif /* CONFIG_SMP */
  660. blr