htab.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * PS3 pagetable management routines.
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006, 2007 Sony Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <asm/machdep.h>
  22. #include <asm/lmb.h>
  23. #include <asm/udbg.h>
  24. #include <asm/lv1call.h>
  25. #include <asm/ps3fb.h>
  26. #include "platform.h"
  27. #if defined(DEBUG)
  28. #define DBG udbg_printf
  29. #else
  30. #define DBG pr_debug
  31. #endif
  32. static struct hash_pte *htab;
  33. static unsigned long htab_addr;
  34. static unsigned char *bolttab;
  35. static unsigned char *inusetab;
  36. static DEFINE_SPINLOCK(ps3_bolttab_lock);
  37. #define debug_dump_hpte(_a, _b, _c, _d, _e, _f, _g) \
  38. _debug_dump_hpte(_a, _b, _c, _d, _e, _f, _g, __func__, __LINE__)
  39. static void _debug_dump_hpte(unsigned long pa, unsigned long va,
  40. unsigned long group, unsigned long bitmap, struct hash_pte lhpte,
  41. int psize, unsigned long slot, const char* func, int line)
  42. {
  43. DBG("%s:%d: pa = %lxh\n", func, line, pa);
  44. DBG("%s:%d: lpar = %lxh\n", func, line,
  45. ps3_mm_phys_to_lpar(pa));
  46. DBG("%s:%d: va = %lxh\n", func, line, va);
  47. DBG("%s:%d: group = %lxh\n", func, line, group);
  48. DBG("%s:%d: bitmap = %lxh\n", func, line, bitmap);
  49. DBG("%s:%d: hpte.v = %lxh\n", func, line, lhpte.v);
  50. DBG("%s:%d: hpte.r = %lxh\n", func, line, lhpte.r);
  51. DBG("%s:%d: psize = %xh\n", func, line, psize);
  52. DBG("%s:%d: slot = %lxh\n", func, line, slot);
  53. }
  54. static long ps3_hpte_insert(unsigned long hpte_group, unsigned long va,
  55. unsigned long pa, unsigned long rflags, unsigned long vflags,
  56. int psize, int ssize)
  57. {
  58. unsigned long slot;
  59. struct hash_pte lhpte;
  60. int secondary = 0;
  61. unsigned long result;
  62. unsigned long bitmap;
  63. unsigned long flags;
  64. unsigned long p_pteg, s_pteg, b_index, b_mask, cb, ci;
  65. vflags &= ~HPTE_V_SECONDARY; /* this bit is ignored */
  66. lhpte.v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M) |
  67. vflags | HPTE_V_VALID;
  68. lhpte.r = hpte_encode_r(ps3_mm_phys_to_lpar(pa), psize) | rflags;
  69. p_pteg = hpte_group / HPTES_PER_GROUP;
  70. s_pteg = ~p_pteg & htab_hash_mask;
  71. spin_lock_irqsave(&ps3_bolttab_lock, flags);
  72. BUG_ON(bolttab[p_pteg] == 0xff && bolttab[s_pteg] == 0xff);
  73. bitmap = (inusetab[p_pteg] << 8) | inusetab[s_pteg];
  74. if (bitmap == 0xffff) {
  75. /*
  76. * PTEG is full. Search for victim.
  77. */
  78. bitmap &= ~((bolttab[p_pteg] << 8) | bolttab[s_pteg]);
  79. do {
  80. ci = mftb() & 15;
  81. cb = 0x8000UL >> ci;
  82. } while ((cb & bitmap) == 0);
  83. } else {
  84. /*
  85. * search free slot in hardware order
  86. * [primary] 0, 2, 4, 6, 1, 3, 5, 7
  87. * [secondary] 0, 2, 4, 6, 1, 3, 5, 7
  88. */
  89. for (ci = 0; ci < HPTES_PER_GROUP; ci += 2) {
  90. cb = 0x8000UL >> ci;
  91. if ((cb & bitmap) == 0)
  92. goto found;
  93. }
  94. for (ci = 1; ci < HPTES_PER_GROUP; ci += 2) {
  95. cb = 0x8000UL >> ci;
  96. if ((cb & bitmap) == 0)
  97. goto found;
  98. }
  99. for (ci = HPTES_PER_GROUP; ci < HPTES_PER_GROUP*2; ci += 2) {
  100. cb = 0x8000UL >> ci;
  101. if ((cb & bitmap) == 0)
  102. goto found;
  103. }
  104. for (ci = HPTES_PER_GROUP+1; ci < HPTES_PER_GROUP*2; ci += 2) {
  105. cb = 0x8000UL >> ci;
  106. if ((cb & bitmap) == 0)
  107. goto found;
  108. }
  109. }
  110. found:
  111. if (ci < HPTES_PER_GROUP) {
  112. slot = p_pteg * HPTES_PER_GROUP + ci;
  113. } else {
  114. slot = s_pteg * HPTES_PER_GROUP + (ci & 7);
  115. /* lhpte.dw0.dw0.h = 1; */
  116. vflags |= HPTE_V_SECONDARY;
  117. lhpte.v |= HPTE_V_SECONDARY;
  118. }
  119. result = lv1_write_htab_entry(0, slot, lhpte.v, lhpte.r);
  120. if (result) {
  121. debug_dump_hpte(pa, va, hpte_group, bitmap, lhpte, psize, slot);
  122. BUG();
  123. }
  124. /*
  125. * If used slot is not in primary HPTE group,
  126. * the slot should be in secondary HPTE group.
  127. */
  128. if ((hpte_group ^ slot) & ~(HPTES_PER_GROUP - 1)) {
  129. secondary = 1;
  130. b_index = s_pteg;
  131. } else {
  132. secondary = 0;
  133. b_index = p_pteg;
  134. }
  135. b_mask = (lhpte.v & HPTE_V_BOLTED) ? 1 << 7 : 0 << 7;
  136. bolttab[b_index] |= b_mask >> (slot & 7);
  137. b_mask = 1 << 7;
  138. inusetab[b_index] |= b_mask >> (slot & 7);
  139. spin_unlock_irqrestore(&ps3_bolttab_lock, flags);
  140. return (slot & 7) | (secondary << 3);
  141. }
  142. static long ps3_hpte_remove(unsigned long hpte_group)
  143. {
  144. panic("ps3_hpte_remove() not implemented");
  145. return 0;
  146. }
  147. static long ps3_hpte_updatepp(unsigned long slot, unsigned long newpp,
  148. unsigned long va, int psize, int ssize, int local)
  149. {
  150. unsigned long flags;
  151. unsigned long result;
  152. unsigned long pteg, bit;
  153. unsigned long hpte_v, want_v;
  154. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  155. spin_lock_irqsave(&ps3_bolttab_lock, flags);
  156. hpte_v = htab[slot].v;
  157. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
  158. spin_unlock_irqrestore(&ps3_bolttab_lock, flags);
  159. /* ps3_hpte_insert() will be used to update PTE */
  160. return -1;
  161. }
  162. result = lv1_write_htab_entry(0, slot, 0, 0);
  163. if (result) {
  164. DBG("%s: va=%lx slot=%lx psize=%d result = %ld (0x%lx)\n",
  165. __func__, va, slot, psize, result, result);
  166. BUG();
  167. }
  168. pteg = slot / HPTES_PER_GROUP;
  169. bit = slot % HPTES_PER_GROUP;
  170. inusetab[pteg] &= ~(0x80 >> bit);
  171. spin_unlock_irqrestore(&ps3_bolttab_lock, flags);
  172. /* ps3_hpte_insert() will be used to update PTE */
  173. return -1;
  174. }
  175. static void ps3_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
  176. int psize, int ssize)
  177. {
  178. panic("ps3_hpte_updateboltedpp() not implemented");
  179. }
  180. static void ps3_hpte_invalidate(unsigned long slot, unsigned long va,
  181. int psize, int ssize, int local)
  182. {
  183. unsigned long flags;
  184. unsigned long result;
  185. unsigned long pteg, bit;
  186. spin_lock_irqsave(&ps3_bolttab_lock, flags);
  187. result = lv1_write_htab_entry(0, slot, 0, 0);
  188. if (result) {
  189. DBG("%s: va=%lx slot=%lx psize=%d result = %ld (0x%lx)\n",
  190. __func__, va, slot, psize, result, result);
  191. BUG();
  192. }
  193. pteg = slot / HPTES_PER_GROUP;
  194. bit = slot % HPTES_PER_GROUP;
  195. inusetab[pteg] &= ~(0x80 >> bit);
  196. spin_unlock_irqrestore(&ps3_bolttab_lock, flags);
  197. }
  198. static void ps3_hpte_clear(void)
  199. {
  200. int result;
  201. DBG(" -> %s:%d\n", __func__, __LINE__);
  202. result = lv1_unmap_htab(htab_addr);
  203. BUG_ON(result);
  204. ps3_mm_shutdown();
  205. ps3_mm_vas_destroy();
  206. DBG(" <- %s:%d\n", __func__, __LINE__);
  207. }
  208. void __init ps3_hpte_init(unsigned long htab_size)
  209. {
  210. long bitmap_size;
  211. DBG(" -> %s:%d\n", __func__, __LINE__);
  212. ppc_md.hpte_invalidate = ps3_hpte_invalidate;
  213. ppc_md.hpte_updatepp = ps3_hpte_updatepp;
  214. ppc_md.hpte_updateboltedpp = ps3_hpte_updateboltedpp;
  215. ppc_md.hpte_insert = ps3_hpte_insert;
  216. ppc_md.hpte_remove = ps3_hpte_remove;
  217. ppc_md.hpte_clear_all = ps3_hpte_clear;
  218. ppc64_pft_size = __ilog2(htab_size);
  219. bitmap_size = htab_size / sizeof(struct hash_pte) / 8;
  220. bolttab = __va(lmb_alloc(bitmap_size, 1));
  221. inusetab = __va(lmb_alloc(bitmap_size, 1));
  222. memset(bolttab, 0, bitmap_size);
  223. memset(inusetab, 0, bitmap_size);
  224. DBG(" <- %s:%d\n", __func__, __LINE__);
  225. }
  226. void __init ps3_map_htab(void)
  227. {
  228. long result;
  229. unsigned long htab_size = (1UL << ppc64_pft_size);
  230. result = lv1_map_htab(0, &htab_addr);
  231. htab = (__force struct hash_pte *)ioremap_flags(htab_addr, htab_size,
  232. pgprot_val(PAGE_READONLY_X));
  233. DBG("%s:%d: lpar %016lxh, virt %016lxh\n", __func__, __LINE__,
  234. htab_addr, (unsigned long)htab);
  235. }