htab.c 7.5 KB

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