beat_htab.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_NO_CACHE)
  102. hpte_r &= ~_PAGE_COHERENT;
  103. spin_lock(&beat_htab_lock);
  104. lpar_rc = beat_read_mask(hpte_group);
  105. if (lpar_rc == 0) {
  106. if (!(vflags & HPTE_V_BOLTED))
  107. DBG_LOW(" full\n");
  108. spin_unlock(&beat_htab_lock);
  109. return -1;
  110. }
  111. lpar_rc = beat_insert_htab_entry(0, hpte_group, lpar_rc << 48,
  112. hpte_v, hpte_r, &slot);
  113. spin_unlock(&beat_htab_lock);
  114. /*
  115. * Since we try and ioremap PHBs we don't own, the pte insert
  116. * will fail. However we must catch the failure in hash_page
  117. * or we will loop forever, so return -2 in this case.
  118. */
  119. if (unlikely(lpar_rc != 0)) {
  120. if (!(vflags & HPTE_V_BOLTED))
  121. DBG_LOW(" lpar err %lx\n", lpar_rc);
  122. return -2;
  123. }
  124. if (!(vflags & HPTE_V_BOLTED))
  125. DBG_LOW(" -> slot: %lx\n", slot);
  126. /* We have to pass down the secondary bucket bit here as well */
  127. return (slot ^ hpte_group) & 15;
  128. }
  129. static long beat_lpar_hpte_remove(unsigned long hpte_group)
  130. {
  131. DBG_LOW("hpte_remove(group=%lx)\n", hpte_group);
  132. return -1;
  133. }
  134. static unsigned long beat_lpar_hpte_getword0(unsigned long slot)
  135. {
  136. unsigned long dword0, dword[5];
  137. unsigned long lpar_rc;
  138. lpar_rc = beat_read_htab_entries(0, slot & ~3UL, dword);
  139. dword0 = dword[slot&3];
  140. BUG_ON(lpar_rc != 0);
  141. return dword0;
  142. }
  143. static void beat_lpar_hptab_clear(void)
  144. {
  145. unsigned long size_bytes = 1UL << ppc64_pft_size;
  146. unsigned long hpte_count = size_bytes >> 4;
  147. int i;
  148. unsigned long dummy0, dummy1;
  149. /* TODO: Use bulk call */
  150. for (i = 0; i < hpte_count; i++)
  151. beat_write_htab_entry(0, i, 0, 0, -1UL, -1UL, &dummy0, &dummy1);
  152. }
  153. /*
  154. * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
  155. * the low 3 bits of flags happen to line up. So no transform is needed.
  156. * We can probably optimize here and assume the high bits of newpp are
  157. * already zero. For now I am paranoid.
  158. */
  159. static long beat_lpar_hpte_updatepp(unsigned long slot,
  160. unsigned long newpp,
  161. unsigned long va,
  162. int psize, int ssize, int local)
  163. {
  164. unsigned long lpar_rc;
  165. unsigned long dummy0, dummy1, want_v;
  166. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  167. DBG_LOW(" update: "
  168. "avpnv=%016lx, slot=%016lx, psize: %d, newpp %016lx ... ",
  169. want_v & HPTE_V_AVPN, slot, psize, newpp);
  170. spin_lock(&beat_htab_lock);
  171. dummy0 = beat_lpar_hpte_getword0(slot);
  172. if ((dummy0 & ~0x7FUL) != (want_v & ~0x7FUL)) {
  173. DBG_LOW("not found !\n");
  174. spin_unlock(&beat_htab_lock);
  175. return -1;
  176. }
  177. lpar_rc = beat_write_htab_entry(0, slot, 0, newpp, 0, 7, &dummy0,
  178. &dummy1);
  179. spin_unlock(&beat_htab_lock);
  180. if (lpar_rc != 0 || dummy0 == 0) {
  181. DBG_LOW("not found !\n");
  182. return -1;
  183. }
  184. DBG_LOW("ok %lx %lx\n", dummy0, dummy1);
  185. BUG_ON(lpar_rc != 0);
  186. return 0;
  187. }
  188. static long beat_lpar_hpte_find(unsigned long va, int psize)
  189. {
  190. unsigned long hash;
  191. unsigned long i, j;
  192. long slot;
  193. unsigned long want_v, hpte_v;
  194. hash = hpt_hash(va, mmu_psize_defs[psize].shift, MMU_SEGSIZE_256M);
  195. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  196. for (j = 0; j < 2; j++) {
  197. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  198. for (i = 0; i < HPTES_PER_GROUP; i++) {
  199. hpte_v = beat_lpar_hpte_getword0(slot);
  200. if (HPTE_V_COMPARE(hpte_v, want_v)
  201. && (hpte_v & HPTE_V_VALID)
  202. && (!!(hpte_v & HPTE_V_SECONDARY) == j)) {
  203. /* HPTE matches */
  204. if (j)
  205. slot = -slot;
  206. return slot;
  207. }
  208. ++slot;
  209. }
  210. hash = ~hash;
  211. }
  212. return -1;
  213. }
  214. static void beat_lpar_hpte_updateboltedpp(unsigned long newpp,
  215. unsigned long ea,
  216. int psize, int ssize)
  217. {
  218. unsigned long lpar_rc, slot, vsid, va, dummy0, dummy1;
  219. vsid = get_kernel_vsid(ea, MMU_SEGSIZE_256M);
  220. va = (vsid << 28) | (ea & 0x0fffffff);
  221. spin_lock(&beat_htab_lock);
  222. slot = beat_lpar_hpte_find(va, psize);
  223. BUG_ON(slot == -1);
  224. lpar_rc = beat_write_htab_entry(0, slot, 0, newpp, 0, 7,
  225. &dummy0, &dummy1);
  226. spin_unlock(&beat_htab_lock);
  227. BUG_ON(lpar_rc != 0);
  228. }
  229. static void beat_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
  230. int psize, int ssize, int local)
  231. {
  232. unsigned long want_v;
  233. unsigned long lpar_rc;
  234. unsigned long dummy1, dummy2;
  235. unsigned long flags;
  236. DBG_LOW(" inval : slot=%lx, va=%016lx, psize: %d, local: %d\n",
  237. slot, va, psize, local);
  238. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  239. spin_lock_irqsave(&beat_htab_lock, flags);
  240. dummy1 = beat_lpar_hpte_getword0(slot);
  241. if ((dummy1 & ~0x7FUL) != (want_v & ~0x7FUL)) {
  242. DBG_LOW("not found !\n");
  243. spin_unlock_irqrestore(&beat_htab_lock, flags);
  244. return;
  245. }
  246. lpar_rc = beat_write_htab_entry(0, slot, 0, 0, HPTE_V_VALID, 0,
  247. &dummy1, &dummy2);
  248. spin_unlock_irqrestore(&beat_htab_lock, flags);
  249. BUG_ON(lpar_rc != 0);
  250. }
  251. void __init hpte_init_beat(void)
  252. {
  253. ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate;
  254. ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp;
  255. ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
  256. ppc_md.hpte_insert = beat_lpar_hpte_insert;
  257. ppc_md.hpte_remove = beat_lpar_hpte_remove;
  258. ppc_md.hpte_clear_all = beat_lpar_hptab_clear;
  259. }
  260. static long beat_lpar_hpte_insert_v3(unsigned long hpte_group,
  261. unsigned long va, unsigned long pa,
  262. unsigned long rflags, unsigned long vflags,
  263. int psize, int ssize)
  264. {
  265. unsigned long lpar_rc;
  266. unsigned long slot;
  267. unsigned long hpte_v, hpte_r;
  268. /* same as iseries */
  269. if (vflags & HPTE_V_SECONDARY)
  270. return -1;
  271. if (!(vflags & HPTE_V_BOLTED))
  272. DBG_LOW("hpte_insert(group=%lx, va=%016lx, pa=%016lx, "
  273. "rflags=%lx, vflags=%lx, psize=%d)\n",
  274. hpte_group, va, pa, rflags, vflags, psize);
  275. hpte_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M) |
  276. vflags | HPTE_V_VALID;
  277. hpte_r = hpte_encode_r(pa, psize) | rflags;
  278. if (!(vflags & HPTE_V_BOLTED))
  279. DBG_LOW(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
  280. if (rflags & _PAGE_NO_CACHE)
  281. hpte_r &= ~_PAGE_COHERENT;
  282. /* insert into not-volted entry */
  283. lpar_rc = beat_insert_htab_entry3(0, hpte_group, hpte_v, hpte_r,
  284. HPTE_V_BOLTED, 0, &slot);
  285. /*
  286. * Since we try and ioremap PHBs we don't own, the pte insert
  287. * will fail. However we must catch the failure in hash_page
  288. * or we will loop forever, so return -2 in this case.
  289. */
  290. if (unlikely(lpar_rc != 0)) {
  291. if (!(vflags & HPTE_V_BOLTED))
  292. DBG_LOW(" lpar err %lx\n", lpar_rc);
  293. return -2;
  294. }
  295. if (!(vflags & HPTE_V_BOLTED))
  296. DBG_LOW(" -> slot: %lx\n", slot);
  297. /* We have to pass down the secondary bucket bit here as well */
  298. return (slot ^ hpte_group) & 15;
  299. }
  300. /*
  301. * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
  302. * the low 3 bits of flags happen to line up. So no transform is needed.
  303. * We can probably optimize here and assume the high bits of newpp are
  304. * already zero. For now I am paranoid.
  305. */
  306. static long beat_lpar_hpte_updatepp_v3(unsigned long slot,
  307. unsigned long newpp,
  308. unsigned long va,
  309. int psize, int ssize, int local)
  310. {
  311. unsigned long lpar_rc;
  312. unsigned long want_v;
  313. unsigned long pss;
  314. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  315. pss = (psize == MMU_PAGE_4K) ? -1UL : mmu_psize_defs[psize].penc;
  316. DBG_LOW(" update: "
  317. "avpnv=%016lx, slot=%016lx, psize: %d, newpp %016lx ... ",
  318. want_v & HPTE_V_AVPN, slot, psize, newpp);
  319. lpar_rc = beat_update_htab_permission3(0, slot, want_v, pss, 7, newpp);
  320. if (lpar_rc == 0xfffffff7) {
  321. DBG_LOW("not found !\n");
  322. return -1;
  323. }
  324. DBG_LOW("ok\n");
  325. BUG_ON(lpar_rc != 0);
  326. return 0;
  327. }
  328. static void beat_lpar_hpte_invalidate_v3(unsigned long slot, unsigned long va,
  329. int psize, int ssize, int local)
  330. {
  331. unsigned long want_v;
  332. unsigned long lpar_rc;
  333. unsigned long pss;
  334. DBG_LOW(" inval : slot=%lx, va=%016lx, psize: %d, local: %d\n",
  335. slot, va, psize, local);
  336. want_v = hpte_encode_v(va, psize, MMU_SEGSIZE_256M);
  337. pss = (psize == MMU_PAGE_4K) ? -1UL : mmu_psize_defs[psize].penc;
  338. lpar_rc = beat_invalidate_htab_entry3(0, slot, want_v, pss);
  339. /* E_busy can be valid output: page may be already replaced */
  340. BUG_ON(lpar_rc != 0 && lpar_rc != 0xfffffff7);
  341. }
  342. static int64_t _beat_lpar_hptab_clear_v3(void)
  343. {
  344. return beat_clear_htab3(0);
  345. }
  346. static void beat_lpar_hptab_clear_v3(void)
  347. {
  348. _beat_lpar_hptab_clear_v3();
  349. }
  350. void __init hpte_init_beat_v3(void)
  351. {
  352. if (_beat_lpar_hptab_clear_v3() == 0) {
  353. ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate_v3;
  354. ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp_v3;
  355. ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
  356. ppc_md.hpte_insert = beat_lpar_hpte_insert_v3;
  357. ppc_md.hpte_remove = beat_lpar_hpte_remove;
  358. ppc_md.hpte_clear_all = beat_lpar_hptab_clear_v3;
  359. } else {
  360. ppc_md.hpte_invalidate = beat_lpar_hpte_invalidate;
  361. ppc_md.hpte_updatepp = beat_lpar_hpte_updatepp;
  362. ppc_md.hpte_updateboltedpp = beat_lpar_hpte_updateboltedpp;
  363. ppc_md.hpte_insert = beat_lpar_hpte_insert;
  364. ppc_md.hpte_remove = beat_lpar_hpte_remove;
  365. ppc_md.hpte_clear_all = beat_lpar_hptab_clear;
  366. }
  367. }