htab.c 7.2 KB

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