htab.c 7.3 KB

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