slb_low.S 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Low-level SLB routines
  3. *
  4. * Copyright (C) 2004 David Gibson <dwg@au.ibm.com>, IBM
  5. *
  6. * Based on earlier C version:
  7. * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
  8. * Copyright (c) 2001 Dave Engebretsen
  9. * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <asm/processor.h>
  17. #include <asm/ppc_asm.h>
  18. #include <asm/asm-offsets.h>
  19. #include <asm/cputable.h>
  20. #include <asm/page.h>
  21. #include <asm/mmu.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/firmware.h>
  24. /*
  25. * This macro generates asm code to compute the VSID scramble
  26. * function. Used in slb_allocate() and do_stab_bolted. The function
  27. * computed is: (protovsid*VSID_MULTIPLIER) % VSID_MODULUS
  28. *
  29. * rt = register containing the proto-VSID and into which the
  30. * VSID will be stored
  31. * rx = scratch register (clobbered)
  32. * rf = flags
  33. *
  34. * - rt and rx must be different registers
  35. * - The answer will end up in the low VSID_BITS bits of rt. The higher
  36. * bits may contain other garbage, so you may need to mask the
  37. * result.
  38. */
  39. #define ASM_VSID_SCRAMBLE(rt, rx, rf, size) \
  40. lis rx,VSID_MULTIPLIER_##size@h; \
  41. ori rx,rx,VSID_MULTIPLIER_##size@l; \
  42. mulld rt,rt,rx; /* rt = rt * MULTIPLIER */ \
  43. /* \
  44. * powermac get slb fault before feature fixup, so make 65 bit part \
  45. * the default part of feature fixup \
  46. */ \
  47. BEGIN_MMU_FTR_SECTION \
  48. srdi rx,rt,VSID_BITS_65_##size; \
  49. clrldi rt,rt,(64-VSID_BITS_65_##size); \
  50. add rt,rt,rx; \
  51. addi rx,rt,1; \
  52. srdi rx,rx,VSID_BITS_65_##size; \
  53. add rt,rt,rx; \
  54. rldimi rf,rt,SLB_VSID_SHIFT_##size,(64 - (SLB_VSID_SHIFT_##size + VSID_BITS_65_##size)); \
  55. MMU_FTR_SECTION_ELSE \
  56. srdi rx,rt,VSID_BITS_##size; \
  57. clrldi rt,rt,(64-VSID_BITS_##size); \
  58. add rt,rt,rx; /* add high and low bits */ \
  59. addi rx,rt,1; \
  60. srdi rx,rx,VSID_BITS_##size; /* extract 2^VSID_BITS bit */ \
  61. add rt,rt,rx; \
  62. rldimi rf,rt,SLB_VSID_SHIFT_##size,(64 - (SLB_VSID_SHIFT_##size + VSID_BITS_##size)); \
  63. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_68_BIT_VA)
  64. /* void slb_allocate(unsigned long ea);
  65. *
  66. * Create an SLB entry for the given EA (user or kernel).
  67. * r3 = faulting address, r13 = PACA
  68. * r9, r10, r11 are clobbered by this function
  69. * r3 is preserved.
  70. * No other registers are examined or changed.
  71. */
  72. _GLOBAL(slb_allocate)
  73. /*
  74. * Check if the address falls within the range of the first context, or
  75. * if we may need to handle multi context. For the first context we
  76. * allocate the slb entry via the fast path below. For large address we
  77. * branch out to C-code and see if additional contexts have been
  78. * allocated.
  79. * The test here is:
  80. * (ea & ~REGION_MASK) >= (1ull << MAX_EA_BITS_PER_CONTEXT)
  81. */
  82. rldicr. r9,r3,4,(63 - MAX_EA_BITS_PER_CONTEXT - 4)
  83. bne- 8f
  84. srdi r9,r3,60 /* get region */
  85. srdi r10,r3,SID_SHIFT /* get esid */
  86. cmpldi cr7,r9,0xc /* cmp PAGE_OFFSET for later use */
  87. /* r3 = address, r10 = esid, cr7 = <> PAGE_OFFSET */
  88. blt cr7,0f /* user or kernel? */
  89. /* Check if hitting the linear mapping or some other kernel space
  90. */
  91. bne cr7,1f
  92. /* Linear mapping encoding bits, the "li" instruction below will
  93. * be patched by the kernel at boot
  94. */
  95. .globl slb_miss_kernel_load_linear
  96. slb_miss_kernel_load_linear:
  97. li r11,0
  98. /*
  99. * context = (ea >> 60) - (0xc - 1)
  100. * r9 = region id.
  101. */
  102. subi r9,r9,KERNEL_REGION_CONTEXT_OFFSET
  103. BEGIN_FTR_SECTION
  104. b .Lslb_finish_load
  105. END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
  106. b .Lslb_finish_load_1T
  107. 1:
  108. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  109. cmpldi cr0,r9,0xf
  110. bne 1f
  111. /* Check virtual memmap region. To be patched at kernel boot */
  112. .globl slb_miss_kernel_load_vmemmap
  113. slb_miss_kernel_load_vmemmap:
  114. li r11,0
  115. b 6f
  116. 1:
  117. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  118. /*
  119. * r10 contains the ESID, which is the original faulting EA shifted
  120. * right by 28 bits. We need to compare that with (H_VMALLOC_END >> 28)
  121. * which is 0xd00038000. That can't be used as an immediate, even if we
  122. * ignored the 0xd, so we have to load it into a register, and we only
  123. * have one register free. So we must load all of (H_VMALLOC_END >> 28)
  124. * into a register and compare ESID against that.
  125. */
  126. lis r11,(H_VMALLOC_END >> 32)@h // r11 = 0xffffffffd0000000
  127. ori r11,r11,(H_VMALLOC_END >> 32)@l // r11 = 0xffffffffd0003800
  128. // Rotate left 4, then mask with 0xffffffff0
  129. rldic r11,r11,4,28 // r11 = 0xd00038000
  130. cmpld r10,r11 // if r10 >= r11
  131. bge 5f // goto io_mapping
  132. /*
  133. * vmalloc mapping gets the encoding from the PACA as the mapping
  134. * can be demoted from 64K -> 4K dynamically on some machines.
  135. */
  136. lhz r11,PACAVMALLOCSLLP(r13)
  137. b 6f
  138. 5:
  139. /* IO mapping */
  140. .globl slb_miss_kernel_load_io
  141. slb_miss_kernel_load_io:
  142. li r11,0
  143. 6:
  144. /*
  145. * context = (ea >> 60) - (0xc - 1)
  146. * r9 = region id.
  147. */
  148. subi r9,r9,KERNEL_REGION_CONTEXT_OFFSET
  149. BEGIN_FTR_SECTION
  150. b .Lslb_finish_load
  151. END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
  152. b .Lslb_finish_load_1T
  153. 0: /*
  154. * For userspace addresses, make sure this is region 0.
  155. */
  156. cmpdi r9, 0
  157. bne- 8f
  158. /*
  159. * user space make sure we are within the allowed limit
  160. */
  161. ld r11,PACA_SLB_ADDR_LIMIT(r13)
  162. cmpld r3,r11
  163. bge- 8f
  164. /* when using slices, we extract the psize off the slice bitmaps
  165. * and then we need to get the sllp encoding off the mmu_psize_defs
  166. * array.
  167. *
  168. * XXX This is a bit inefficient especially for the normal case,
  169. * so we should try to implement a fast path for the standard page
  170. * size using the old sllp value so we avoid the array. We cannot
  171. * really do dynamic patching unfortunately as processes might flip
  172. * between 4k and 64k standard page size
  173. */
  174. #ifdef CONFIG_PPC_MM_SLICES
  175. /* r10 have esid */
  176. cmpldi r10,16
  177. /* below SLICE_LOW_TOP */
  178. blt 5f
  179. /*
  180. * Handle hpsizes,
  181. * r9 is get_paca()->context.high_slices_psize[index], r11 is mask_index
  182. */
  183. srdi r11,r10,(SLICE_HIGH_SHIFT - SLICE_LOW_SHIFT + 1) /* index */
  184. addi r9,r11,PACAHIGHSLICEPSIZE
  185. lbzx r9,r13,r9 /* r9 is hpsizes[r11] */
  186. /* r11 = (r10 >> (SLICE_HIGH_SHIFT - SLICE_LOW_SHIFT)) & 0x1 */
  187. rldicl r11,r10,(64 - (SLICE_HIGH_SHIFT - SLICE_LOW_SHIFT)),63
  188. b 6f
  189. 5:
  190. /*
  191. * Handle lpsizes
  192. * r9 is get_paca()->context.low_slices_psize[index], r11 is mask_index
  193. */
  194. srdi r11,r10,1 /* index */
  195. addi r9,r11,PACALOWSLICESPSIZE
  196. lbzx r9,r13,r9 /* r9 is lpsizes[r11] */
  197. rldicl r11,r10,0,63 /* r11 = r10 & 0x1 */
  198. 6:
  199. sldi r11,r11,2 /* index * 4 */
  200. /* Extract the psize and multiply to get an array offset */
  201. srd r9,r9,r11
  202. andi. r9,r9,0xf
  203. mulli r9,r9,MMUPSIZEDEFSIZE
  204. /* Now get to the array and obtain the sllp
  205. */
  206. ld r11,PACATOC(r13)
  207. ld r11,mmu_psize_defs@got(r11)
  208. add r11,r11,r9
  209. ld r11,MMUPSIZESLLP(r11)
  210. ori r11,r11,SLB_VSID_USER
  211. #else
  212. /* paca context sllp already contains the SLB_VSID_USER bits */
  213. lhz r11,PACACONTEXTSLLP(r13)
  214. #endif /* CONFIG_PPC_MM_SLICES */
  215. ld r9,PACACONTEXTID(r13)
  216. BEGIN_FTR_SECTION
  217. cmpldi r10,0x1000
  218. bge .Lslb_finish_load_1T
  219. END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  220. b .Lslb_finish_load
  221. 8: /* invalid EA - return an error indication */
  222. crset 4*cr0+eq /* indicate failure */
  223. blr
  224. /*
  225. * Finish loading of an SLB entry and return
  226. *
  227. * r3 = EA, r9 = context, r10 = ESID, r11 = flags, clobbers r9, cr7 = <> PAGE_OFFSET
  228. */
  229. .Lslb_finish_load:
  230. rldimi r10,r9,ESID_BITS,0
  231. ASM_VSID_SCRAMBLE(r10,r9,r11,256M)
  232. /* r3 = EA, r11 = VSID data */
  233. /*
  234. * Find a slot, round robin. Previously we tried to find a
  235. * free slot first but that took too long. Unfortunately we
  236. * dont have any LRU information to help us choose a slot.
  237. */
  238. mr r9,r3
  239. /* slb_finish_load_1T continues here. r9=EA with non-ESID bits clear */
  240. 7: ld r10,PACASTABRR(r13)
  241. addi r10,r10,1
  242. /* This gets soft patched on boot. */
  243. .globl slb_compare_rr_to_size
  244. slb_compare_rr_to_size:
  245. cmpldi r10,0
  246. blt+ 4f
  247. li r10,SLB_NUM_BOLTED
  248. 4:
  249. std r10,PACASTABRR(r13)
  250. 3:
  251. rldimi r9,r10,0,36 /* r9 = EA[0:35] | entry */
  252. oris r10,r9,SLB_ESID_V@h /* r10 = r9 | SLB_ESID_V */
  253. /* r9 = ESID data, r11 = VSID data */
  254. /*
  255. * No need for an isync before or after this slbmte. The exception
  256. * we enter with and the rfid we exit with are context synchronizing.
  257. */
  258. slbmte r11,r10
  259. /* we're done for kernel addresses */
  260. crclr 4*cr0+eq /* set result to "success" */
  261. bgelr cr7
  262. /* Update the slb cache */
  263. lhz r9,PACASLBCACHEPTR(r13) /* offset = paca->slb_cache_ptr */
  264. cmpldi r9,SLB_CACHE_ENTRIES
  265. bge 1f
  266. /* still room in the slb cache */
  267. sldi r11,r9,2 /* r11 = offset * sizeof(u32) */
  268. srdi r10,r10,28 /* get the 36 bits of the ESID */
  269. add r11,r11,r13 /* r11 = (u32 *)paca + offset */
  270. stw r10,PACASLBCACHE(r11) /* paca->slb_cache[offset] = esid */
  271. addi r9,r9,1 /* offset++ */
  272. b 2f
  273. 1: /* offset >= SLB_CACHE_ENTRIES */
  274. li r9,SLB_CACHE_ENTRIES+1
  275. 2:
  276. sth r9,PACASLBCACHEPTR(r13) /* paca->slb_cache_ptr = offset */
  277. crclr 4*cr0+eq /* set result to "success" */
  278. blr
  279. /*
  280. * Finish loading of a 1T SLB entry (for the kernel linear mapping) and return.
  281. *
  282. * r3 = EA, r9 = context, r10 = ESID(256MB), r11 = flags, clobbers r9
  283. */
  284. .Lslb_finish_load_1T:
  285. srdi r10,r10,(SID_SHIFT_1T - SID_SHIFT) /* get 1T ESID */
  286. rldimi r10,r9,ESID_BITS_1T,0
  287. ASM_VSID_SCRAMBLE(r10,r9,r11,1T)
  288. li r10,MMU_SEGSIZE_1T
  289. rldimi r11,r10,SLB_VSID_SSIZE_SHIFT,0 /* insert segment size */
  290. /* r3 = EA, r11 = VSID data */
  291. clrrdi r9,r3,SID_SHIFT_1T /* clear out non-ESID bits */
  292. b 7b
  293. _ASM_NOKPROBE_SYMBOL(slb_allocate)
  294. _ASM_NOKPROBE_SYMBOL(slb_miss_kernel_load_linear)
  295. _ASM_NOKPROBE_SYMBOL(slb_miss_kernel_load_io)
  296. _ASM_NOKPROBE_SYMBOL(slb_compare_rr_to_size)
  297. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  298. _ASM_NOKPROBE_SYMBOL(slb_miss_kernel_load_vmemmap)
  299. #endif