htab.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * "Cell Reference Set" HTAB support.
  3. *
  4. * (C) Copyright 2006-2007 TOSHIBA CORPORATION
  5. *
  6. * This code is based on arch/powerpc/platforms/pseries/lpar.c:
  7. * Copyright (C) 2001 Todd Inglett, IBM Corporation
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. */
  23. #undef DEBUG_LOW
  24. #include <linux/kernel.h>
  25. #include <linux/spinlock.h>
  26. #include <asm/mmu.h>
  27. #include <asm/page.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/machdep.h>
  30. #include <asm/udbg.h>
  31. #include "beat_wrapper.h"
  32. #ifdef DEBUG_LOW
  33. #define DBG_LOW(fmt...) do { udbg_printf(fmt); } while(0)
  34. #else
  35. #define DBG_LOW(fmt...) do { } while(0)
  36. #endif
  37. static DEFINE_SPINLOCK(beat_htab_lock);
  38. static inline unsigned int beat_read_mask(unsigned hpte_group)
  39. {
  40. unsigned long hpte_v[5];
  41. unsigned long rmask = 0;
  42. beat_read_htab_entries(0, hpte_group + 0, hpte_v);
  43. if (!(hpte_v[0] & HPTE_V_BOLTED))
  44. rmask |= 0x8000;
  45. if (!(hpte_v[1] & HPTE_V_BOLTED))
  46. rmask |= 0x4000;
  47. if (!(hpte_v[2] & HPTE_V_BOLTED))
  48. rmask |= 0x2000;
  49. if (!(hpte_v[3] & HPTE_V_BOLTED))
  50. rmask |= 0x1000;
  51. beat_read_htab_entries(0, hpte_group + 4, hpte_v);
  52. if (!(hpte_v[0] & HPTE_V_BOLTED))
  53. rmask |= 0x0800;
  54. if (!(hpte_v[1] & HPTE_V_BOLTED))
  55. rmask |= 0x0400;
  56. if (!(hpte_v[2] & HPTE_V_BOLTED))
  57. rmask |= 0x0200;
  58. if (!(hpte_v[3] & HPTE_V_BOLTED))
  59. rmask |= 0x0100;
  60. hpte_group = ~hpte_group & (htab_hash_mask * HPTES_PER_GROUP);
  61. beat_read_htab_entries(0, hpte_group + 0, hpte_v);
  62. if (!(hpte_v[0] & HPTE_V_BOLTED))
  63. rmask |= 0x80;
  64. if (!(hpte_v[1] & HPTE_V_BOLTED))
  65. rmask |= 0x40;
  66. if (!(hpte_v[2] & HPTE_V_BOLTED))
  67. rmask |= 0x20;
  68. if (!(hpte_v[3] & HPTE_V_BOLTED))
  69. rmask |= 0x10;
  70. beat_read_htab_entries(0, hpte_group + 4, hpte_v);
  71. if (!(hpte_v[0] & HPTE_V_BOLTED))
  72. rmask |= 0x08;
  73. if (!(hpte_v[1] & HPTE_V_BOLTED))
  74. rmask |= 0x04;
  75. if (!(hpte_v[2] & HPTE_V_BOLTED))
  76. rmask |= 0x02;
  77. if (!(hpte_v[3] & HPTE_V_BOLTED))
  78. rmask |= 0x01;
  79. return rmask;
  80. }
  81. static long beat_lpar_hpte_insert(unsigned long hpte_group,
  82. unsigned long va, unsigned long pa,
  83. unsigned long rflags, unsigned long vflags,
  84. int psize, int ssize)
  85. {
  86. unsigned long lpar_rc;
  87. unsigned long slot;
  88. unsigned long hpte_v, hpte_r;
  89. /* same as iseries */
  90. if (vflags & HPTE_V_SECONDARY)
  91. return -1;
  92. if (!(vflags & HPTE_V_BOLTED))
  93. DBG_LOW("hpte_insert(group=%lx, va=%016lx, pa=%016lx, "
  94. "rflags=%lx, vflags=%lx, psize=%d)\n",
  95. hpte_group, va, pa, rflags, vflags, psize);
  96. hpte_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M) |
  97. vflags | HPTE_V_VALID;
  98. hpte_r = hpte_encode_r(pa, psize) | rflags;
  99. if (!(vflags & HPTE_V_BOLTED))
  100. DBG_LOW(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
  101. if (rflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
  102. hpte_r &= ~_PAGE_COHERENT;
  103. spin_lock(&beat_htab_lock);
  104. if ((lpar_rc = beat_read_mask(hpte_group)) == 0) {
  105. if (!(vflags & HPTE_V_BOLTED))
  106. DBG_LOW(" full\n");
  107. spin_unlock(&beat_htab_lock);
  108. return -1;
  109. }
  110. lpar_rc = beat_insert_htab_entry(0, hpte_group, lpar_rc << 48,
  111. hpte_v, hpte_r, &slot);
  112. spin_unlock(&beat_htab_lock);
  113. /*
  114. * Since we try and ioremap PHBs we don't own, the pte insert
  115. * will fail. However we must catch the failure in hash_page
  116. * or we will loop forever, so return -2 in this case.
  117. */
  118. if (unlikely(lpar_rc != 0)) {
  119. if (!(vflags & HPTE_V_BOLTED))
  120. DBG_LOW(" lpar err %lx\n", lpar_rc);
  121. return -2;
  122. }
  123. if (!(vflags & HPTE_V_BOLTED))
  124. DBG_LOW(" -> slot: %lx\n", slot);
  125. /* We have to pass down the secondary bucket bit here as well */
  126. return (slot ^ hpte_group) & 15;
  127. }
  128. static long beat_lpar_hpte_remove(unsigned long hpte_group)
  129. {
  130. DBG_LOW("hpte_remove(group=%lx)\n", hpte_group);
  131. return -1;
  132. }
  133. static unsigned long beat_lpar_hpte_getword0(unsigned long slot)
  134. {
  135. unsigned long dword0, dword[5];
  136. unsigned long lpar_rc;
  137. lpar_rc = beat_read_htab_entries(0, slot & ~3UL, dword);
  138. dword0 = dword[slot&3];
  139. BUG_ON(lpar_rc != 0);
  140. return dword0;
  141. }
  142. static void beat_lpar_hptab_clear(void)
  143. {
  144. unsigned long size_bytes = 1UL << ppc64_pft_size;
  145. unsigned long hpte_count = size_bytes >> 4;
  146. int i;
  147. unsigned long dummy0, dummy1;
  148. /* TODO: Use bulk call */
  149. for (i = 0; i < hpte_count; i++)
  150. beat_write_htab_entry(0, i, 0, 0, -1UL, -1UL, &dummy0, &dummy1);
  151. }
  152. /*
  153. * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
  154. * the low 3 bits of flags happen to line up. So no transform is needed.
  155. * We can probably optimize here and assume the high bits of newpp are
  156. * already zero. For now I am paranoid.
  157. */
  158. static long beat_lpar_hpte_updatepp(unsigned long slot,
  159. unsigned long newpp,
  160. unsigned long va,
  161. int psize, int ssize, int local)
  162. {
  163. unsigned long lpar_rc;
  164. unsigned long dummy0, dummy1, want_v;
  165. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  166. DBG_LOW(" update: "
  167. "avpnv=%016lx, slot=%016lx, psize: %d, newpp %016lx ... ",
  168. want_v & HPTE_V_AVPN, slot, psize, newpp);
  169. spin_lock(&beat_htab_lock);
  170. dummy0 = beat_lpar_hpte_getword0(slot);
  171. if ((dummy0 & ~0x7FUL) != (want_v & ~0x7FUL)) {
  172. DBG_LOW("not found !\n");
  173. spin_unlock(&beat_htab_lock);
  174. return -1;
  175. }
  176. lpar_rc = beat_write_htab_entry(0, slot, 0, newpp, 0, 7, &dummy0,
  177. &dummy1);
  178. spin_unlock(&beat_htab_lock);
  179. if (lpar_rc != 0 || dummy0 == 0) {
  180. DBG_LOW("not found !\n");
  181. return -1;
  182. }
  183. DBG_LOW("ok %lx %lx\n", dummy0, dummy1);
  184. BUG_ON(lpar_rc != 0);
  185. return 0;
  186. }
  187. static long beat_lpar_hpte_find(unsigned long va, int psize)
  188. {
  189. unsigned long hash;
  190. unsigned long i, j;
  191. long slot;
  192. unsigned long want_v, hpte_v;
  193. hash = hpt_hash(va, mmu_psize_defs[psize].shift, MMU_SEGSIZE_256M);
  194. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  195. for (j = 0; j < 2; j++) {
  196. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  197. for (i = 0; i < HPTES_PER_GROUP; i++) {
  198. hpte_v = beat_lpar_hpte_getword0(slot);
  199. if (HPTE_V_COMPARE(hpte_v, want_v)
  200. && (hpte_v & HPTE_V_VALID)
  201. && (!!(hpte_v & HPTE_V_SECONDARY) == j)) {
  202. /* HPTE matches */
  203. if (j)
  204. slot = -slot;
  205. return slot;
  206. }
  207. ++slot;
  208. }
  209. hash = ~hash;
  210. }
  211. return -1;
  212. }
  213. static void beat_lpar_hpte_updateboltedpp(unsigned long newpp,
  214. unsigned long ea,
  215. int psize, int ssize)
  216. {
  217. unsigned long lpar_rc, slot, vsid, va, dummy0, dummy1;
  218. vsid = get_kernel_vsid(ea, MMU_SEGSIZE_256M);
  219. va = (vsid << 28) | (ea & 0x0fffffff);
  220. spin_lock(&beat_htab_lock);
  221. slot = beat_lpar_hpte_find(va, psize);
  222. BUG_ON(slot == -1);
  223. lpar_rc = beat_write_htab_entry(0, slot, 0, newpp, 0, 7,
  224. &dummy0, &dummy1);
  225. spin_unlock(&beat_htab_lock);
  226. BUG_ON(lpar_rc != 0);
  227. }
  228. static void beat_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
  229. int psize, int ssize, int local)
  230. {
  231. unsigned long want_v;
  232. unsigned long lpar_rc;
  233. unsigned long dummy1, dummy2;
  234. unsigned long flags;
  235. DBG_LOW(" inval : slot=%lx, va=%016lx, psize: %d, local: %d\n",
  236. slot, va, psize, local);
  237. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  238. spin_lock_irqsave(&beat_htab_lock, flags);
  239. dummy1 = beat_lpar_hpte_getword0(slot);
  240. if ((dummy1 & ~0x7FUL) != (want_v & ~0x7FUL)) {
  241. DBG_LOW("not found !\n");
  242. spin_unlock_irqrestore(&beat_htab_lock, flags);
  243. return;
  244. }
  245. lpar_rc = beat_write_htab_entry(0, slot, 0, 0, HPTE_V_VALID, 0,
  246. &dummy1, &dummy2);
  247. spin_unlock_irqrestore(&beat_htab_lock, flags);
  248. BUG_ON(lpar_rc != 0);
  249. }
  250. void __init hpte_init_beat(void)
  251. {
  252. ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate;
  253. ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp;
  254. ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
  255. ppc_md.hpte_insert = beat_lpar_hpte_insert;
  256. ppc_md.hpte_remove = beat_lpar_hpte_remove;
  257. ppc_md.hpte_clear_all = beat_lpar_hptab_clear;
  258. }
  259. static long beat_lpar_hpte_insert_v3(unsigned long hpte_group,
  260. unsigned long va, unsigned long pa,
  261. unsigned long rflags, unsigned long vflags,
  262. int psize, int ssize)
  263. {
  264. unsigned long lpar_rc;
  265. unsigned long slot;
  266. unsigned long hpte_v, hpte_r;
  267. /* same as iseries */
  268. if (vflags & HPTE_V_SECONDARY)
  269. return -1;
  270. if (!(vflags & HPTE_V_BOLTED))
  271. DBG_LOW("hpte_insert(group=%lx, va=%016lx, pa=%016lx, "
  272. "rflags=%lx, vflags=%lx, psize=%d)\n",
  273. hpte_group, va, pa, rflags, vflags, psize);
  274. hpte_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M) |
  275. vflags | HPTE_V_VALID;
  276. hpte_r = hpte_encode_r(pa, psize) | rflags;
  277. if (!(vflags & HPTE_V_BOLTED))
  278. DBG_LOW(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
  279. if (rflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
  280. hpte_r &= ~_PAGE_COHERENT;
  281. /* insert into not-volted entry */
  282. lpar_rc = beat_insert_htab_entry3(0, hpte_group, hpte_v, hpte_r,
  283. HPTE_V_BOLTED, 0, &slot);
  284. /*
  285. * Since we try and ioremap PHBs we don't own, the pte insert
  286. * will fail. However we must catch the failure in hash_page
  287. * or we will loop forever, so return -2 in this case.
  288. */
  289. if (unlikely(lpar_rc != 0)) {
  290. if (!(vflags & HPTE_V_BOLTED))
  291. DBG_LOW(" lpar err %lx\n", lpar_rc);
  292. return -2;
  293. }
  294. if (!(vflags & HPTE_V_BOLTED))
  295. DBG_LOW(" -> slot: %lx\n", slot);
  296. /* We have to pass down the secondary bucket bit here as well */
  297. return (slot ^ hpte_group) & 15;
  298. }
  299. /*
  300. * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
  301. * the low 3 bits of flags happen to line up. So no transform is needed.
  302. * We can probably optimize here and assume the high bits of newpp are
  303. * already zero. For now I am paranoid.
  304. */
  305. static long beat_lpar_hpte_updatepp_v3(unsigned long slot,
  306. unsigned long newpp,
  307. unsigned long va,
  308. int psize, int ssize, int local)
  309. {
  310. unsigned long lpar_rc;
  311. unsigned long want_v;
  312. unsigned long pss;
  313. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  314. pss = (psize == MMU_PAGE_4K) ? -1UL : mmu_psize_defs[psize].penc;
  315. DBG_LOW(" update: "
  316. "avpnv=%016lx, slot=%016lx, psize: %d, newpp %016lx ... ",
  317. want_v & HPTE_V_AVPN, slot, psize, newpp);
  318. lpar_rc = beat_update_htab_permission3(0, slot, want_v, pss, 7, newpp);
  319. if (lpar_rc == 0xfffffff7) {
  320. DBG_LOW("not found !\n");
  321. return -1;
  322. }
  323. DBG_LOW("ok\n");
  324. BUG_ON(lpar_rc != 0);
  325. return 0;
  326. }
  327. static void beat_lpar_hpte_invalidate_v3(unsigned long slot, unsigned long va,
  328. int psize, int ssize, int local)
  329. {
  330. unsigned long want_v;
  331. unsigned long lpar_rc;
  332. unsigned long pss;
  333. DBG_LOW(" inval : slot=%lx, va=%016lx, psize: %d, local: %d\n",
  334. slot, va, psize, local);
  335. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  336. pss = (psize == MMU_PAGE_4K) ? -1UL : mmu_psize_defs[psize].penc;
  337. lpar_rc = beat_invalidate_htab_entry3(0, slot, want_v, pss);
  338. /* E_busy can be valid output: page may be already replaced */
  339. BUG_ON(lpar_rc != 0 && lpar_rc != 0xfffffff7);
  340. }
  341. static int64_t _beat_lpar_hptab_clear_v3(void)
  342. {
  343. return beat_clear_htab3(0);
  344. }
  345. static void beat_lpar_hptab_clear_v3(void)
  346. {
  347. _beat_lpar_hptab_clear_v3();
  348. }
  349. void __init hpte_init_beat_v3(void)
  350. {
  351. if (_beat_lpar_hptab_clear_v3() == 0) {
  352. ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate_v3;
  353. ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp_v3;
  354. ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
  355. ppc_md.hpte_insert = beat_lpar_hpte_insert_v3;
  356. ppc_md.hpte_remove = beat_lpar_hpte_remove;
  357. ppc_md.hpte_clear_all = beat_lpar_hptab_clear_v3;
  358. } else {
  359. ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate;
  360. ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp;
  361. ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
  362. ppc_md.hpte_insert = beat_lpar_hpte_insert;
  363. ppc_md.hpte_remove = beat_lpar_hpte_remove;
  364. ppc_md.hpte_clear_all = beat_lpar_hptab_clear;
  365. }
  366. }