lpar.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * pSeries_lpar.c
  3. * Copyright (C) 2001 Todd Inglett, IBM Corporation
  4. *
  5. * pSeries LPAR support.
  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; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* Enables debugging of low-level hash table routines - careful! */
  22. #undef DEBUG
  23. #include <linux/kernel.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/console.h>
  26. #include <asm/processor.h>
  27. #include <asm/mmu.h>
  28. #include <asm/page.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/machdep.h>
  31. #include <asm/abs_addr.h>
  32. #include <asm/mmu_context.h>
  33. #include <asm/iommu.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/tlb.h>
  36. #include <asm/prom.h>
  37. #include <asm/cputable.h>
  38. #include <asm/udbg.h>
  39. #include <asm/smp.h>
  40. #include "plpar_wrappers.h"
  41. #include "pseries.h"
  42. /* in hvCall.S */
  43. EXPORT_SYMBOL(plpar_hcall);
  44. EXPORT_SYMBOL(plpar_hcall9);
  45. EXPORT_SYMBOL(plpar_hcall_norets);
  46. extern void pSeries_find_serial_port(void);
  47. static int vtermno; /* virtual terminal# for udbg */
  48. #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
  49. static void udbg_hvsi_putc(char c)
  50. {
  51. /* packet's seqno isn't used anyways */
  52. uint8_t packet[] __ALIGNED__ = { 0xff, 5, 0, 0, c };
  53. int rc;
  54. if (c == '\n')
  55. udbg_hvsi_putc('\r');
  56. do {
  57. rc = plpar_put_term_char(vtermno, sizeof(packet), packet);
  58. } while (rc == H_BUSY);
  59. }
  60. static long hvsi_udbg_buf_len;
  61. static uint8_t hvsi_udbg_buf[256];
  62. static int udbg_hvsi_getc_poll(void)
  63. {
  64. unsigned char ch;
  65. int rc, i;
  66. if (hvsi_udbg_buf_len == 0) {
  67. rc = plpar_get_term_char(vtermno, &hvsi_udbg_buf_len, hvsi_udbg_buf);
  68. if (rc != H_SUCCESS || hvsi_udbg_buf[0] != 0xff) {
  69. /* bad read or non-data packet */
  70. hvsi_udbg_buf_len = 0;
  71. } else {
  72. /* remove the packet header */
  73. for (i = 4; i < hvsi_udbg_buf_len; i++)
  74. hvsi_udbg_buf[i-4] = hvsi_udbg_buf[i];
  75. hvsi_udbg_buf_len -= 4;
  76. }
  77. }
  78. if (hvsi_udbg_buf_len <= 0 || hvsi_udbg_buf_len > 256) {
  79. /* no data ready */
  80. hvsi_udbg_buf_len = 0;
  81. return -1;
  82. }
  83. ch = hvsi_udbg_buf[0];
  84. /* shift remaining data down */
  85. for (i = 1; i < hvsi_udbg_buf_len; i++) {
  86. hvsi_udbg_buf[i-1] = hvsi_udbg_buf[i];
  87. }
  88. hvsi_udbg_buf_len--;
  89. return ch;
  90. }
  91. static int udbg_hvsi_getc(void)
  92. {
  93. int ch;
  94. for (;;) {
  95. ch = udbg_hvsi_getc_poll();
  96. if (ch == -1) {
  97. /* This shouldn't be needed...but... */
  98. volatile unsigned long delay;
  99. for (delay=0; delay < 2000000; delay++)
  100. ;
  101. } else {
  102. return ch;
  103. }
  104. }
  105. }
  106. static void udbg_putcLP(char c)
  107. {
  108. char buf[16];
  109. unsigned long rc;
  110. if (c == '\n')
  111. udbg_putcLP('\r');
  112. buf[0] = c;
  113. do {
  114. rc = plpar_put_term_char(vtermno, 1, buf);
  115. } while(rc == H_BUSY);
  116. }
  117. /* Buffered chars getc */
  118. static long inbuflen;
  119. static long inbuf[2]; /* must be 2 longs */
  120. static int udbg_getc_pollLP(void)
  121. {
  122. /* The interface is tricky because it may return up to 16 chars.
  123. * We save them statically for future calls to udbg_getc().
  124. */
  125. char ch, *buf = (char *)inbuf;
  126. int i;
  127. long rc;
  128. if (inbuflen == 0) {
  129. /* get some more chars. */
  130. inbuflen = 0;
  131. rc = plpar_get_term_char(vtermno, &inbuflen, buf);
  132. if (rc != H_SUCCESS)
  133. inbuflen = 0; /* otherwise inbuflen is garbage */
  134. }
  135. if (inbuflen <= 0 || inbuflen > 16) {
  136. /* Catch error case as well as other oddities (corruption) */
  137. inbuflen = 0;
  138. return -1;
  139. }
  140. ch = buf[0];
  141. for (i = 1; i < inbuflen; i++) /* shuffle them down. */
  142. buf[i-1] = buf[i];
  143. inbuflen--;
  144. return ch;
  145. }
  146. static int udbg_getcLP(void)
  147. {
  148. int ch;
  149. for (;;) {
  150. ch = udbg_getc_pollLP();
  151. if (ch == -1) {
  152. /* This shouldn't be needed...but... */
  153. volatile unsigned long delay;
  154. for (delay=0; delay < 2000000; delay++)
  155. ;
  156. } else {
  157. return ch;
  158. }
  159. }
  160. }
  161. /* call this from early_init() for a working debug console on
  162. * vterm capable LPAR machines
  163. */
  164. void __init udbg_init_debug_lpar(void)
  165. {
  166. vtermno = 0;
  167. udbg_putc = udbg_putcLP;
  168. udbg_getc = udbg_getcLP;
  169. udbg_getc_poll = udbg_getc_pollLP;
  170. register_early_udbg_console();
  171. }
  172. /* returns 0 if couldn't find or use /chosen/stdout as console */
  173. void __init find_udbg_vterm(void)
  174. {
  175. struct device_node *stdout_node;
  176. const u32 *termno;
  177. const char *name;
  178. /* find the boot console from /chosen/stdout */
  179. if (!of_chosen)
  180. return;
  181. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  182. if (name == NULL)
  183. return;
  184. stdout_node = of_find_node_by_path(name);
  185. if (!stdout_node)
  186. return;
  187. name = of_get_property(stdout_node, "name", NULL);
  188. if (!name) {
  189. printk(KERN_WARNING "stdout node missing 'name' property!\n");
  190. goto out;
  191. }
  192. /* Check if it's a virtual terminal */
  193. if (strncmp(name, "vty", 3) != 0)
  194. goto out;
  195. termno = of_get_property(stdout_node, "reg", NULL);
  196. if (termno == NULL)
  197. goto out;
  198. vtermno = termno[0];
  199. if (of_device_is_compatible(stdout_node, "hvterm1")) {
  200. udbg_putc = udbg_putcLP;
  201. udbg_getc = udbg_getcLP;
  202. udbg_getc_poll = udbg_getc_pollLP;
  203. add_preferred_console("hvc", termno[0] & 0xff, NULL);
  204. } else if (of_device_is_compatible(stdout_node, "hvterm-protocol")) {
  205. vtermno = termno[0];
  206. udbg_putc = udbg_hvsi_putc;
  207. udbg_getc = udbg_hvsi_getc;
  208. udbg_getc_poll = udbg_hvsi_getc_poll;
  209. add_preferred_console("hvsi", termno[0] & 0xff, NULL);
  210. }
  211. out:
  212. of_node_put(stdout_node);
  213. }
  214. void vpa_init(int cpu)
  215. {
  216. int hwcpu = get_hard_smp_processor_id(cpu);
  217. unsigned long addr;
  218. long ret;
  219. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  220. lppaca[cpu].vmxregs_in_use = 1;
  221. addr = __pa(&lppaca[cpu]);
  222. ret = register_vpa(hwcpu, addr);
  223. if (ret) {
  224. printk(KERN_ERR "WARNING: vpa_init: VPA registration for "
  225. "cpu %d (hw %d) of area %lx returns %ld\n",
  226. cpu, hwcpu, addr, ret);
  227. return;
  228. }
  229. /*
  230. * PAPR says this feature is SLB-Buffer but firmware never
  231. * reports that. All SPLPAR support SLB shadow buffer.
  232. */
  233. addr = __pa(&slb_shadow[cpu]);
  234. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  235. ret = register_slb_shadow(hwcpu, addr);
  236. if (ret)
  237. printk(KERN_ERR
  238. "WARNING: vpa_init: SLB shadow buffer "
  239. "registration for cpu %d (hw %d) of area %lx "
  240. "returns %ld\n", cpu, hwcpu, addr, ret);
  241. }
  242. }
  243. static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
  244. unsigned long va, unsigned long pa,
  245. unsigned long rflags, unsigned long vflags,
  246. int psize, int ssize)
  247. {
  248. unsigned long lpar_rc;
  249. unsigned long flags;
  250. unsigned long slot;
  251. unsigned long hpte_v, hpte_r;
  252. if (!(vflags & HPTE_V_BOLTED))
  253. pr_debug("hpte_insert(group=%lx, va=%016lx, pa=%016lx, "
  254. "rflags=%lx, vflags=%lx, psize=%d)\n",
  255. hpte_group, va, pa, rflags, vflags, psize);
  256. hpte_v = hpte_encode_v(va, psize, ssize) | vflags | HPTE_V_VALID;
  257. hpte_r = hpte_encode_r(pa, psize) | rflags;
  258. if (!(vflags & HPTE_V_BOLTED))
  259. pr_debug(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
  260. /* Now fill in the actual HPTE */
  261. /* Set CEC cookie to 0 */
  262. /* Zero page = 0 */
  263. /* I-cache Invalidate = 0 */
  264. /* I-cache synchronize = 0 */
  265. /* Exact = 0 */
  266. flags = 0;
  267. /* Make pHyp happy */
  268. if ((rflags & _PAGE_NO_CACHE) & !(rflags & _PAGE_WRITETHRU))
  269. hpte_r &= ~_PAGE_COHERENT;
  270. lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
  271. if (unlikely(lpar_rc == H_PTEG_FULL)) {
  272. if (!(vflags & HPTE_V_BOLTED))
  273. pr_debug(" full\n");
  274. return -1;
  275. }
  276. /*
  277. * Since we try and ioremap PHBs we don't own, the pte insert
  278. * will fail. However we must catch the failure in hash_page
  279. * or we will loop forever, so return -2 in this case.
  280. */
  281. if (unlikely(lpar_rc != H_SUCCESS)) {
  282. if (!(vflags & HPTE_V_BOLTED))
  283. pr_debug(" lpar err %lu\n", lpar_rc);
  284. return -2;
  285. }
  286. if (!(vflags & HPTE_V_BOLTED))
  287. pr_debug(" -> slot: %lu\n", slot & 7);
  288. /* Because of iSeries, we have to pass down the secondary
  289. * bucket bit here as well
  290. */
  291. return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
  292. }
  293. static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
  294. static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
  295. {
  296. unsigned long slot_offset;
  297. unsigned long lpar_rc;
  298. int i;
  299. unsigned long dummy1, dummy2;
  300. /* pick a random slot to start at */
  301. slot_offset = mftb() & 0x7;
  302. for (i = 0; i < HPTES_PER_GROUP; i++) {
  303. /* don't remove a bolted entry */
  304. lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
  305. (0x1UL << 4), &dummy1, &dummy2);
  306. if (lpar_rc == H_SUCCESS)
  307. return i;
  308. BUG_ON(lpar_rc != H_NOT_FOUND);
  309. slot_offset++;
  310. slot_offset &= 0x7;
  311. }
  312. return -1;
  313. }
  314. static void pSeries_lpar_hptab_clear(void)
  315. {
  316. unsigned long size_bytes = 1UL << ppc64_pft_size;
  317. unsigned long hpte_count = size_bytes >> 4;
  318. unsigned long dummy1, dummy2, dword0;
  319. long lpar_rc;
  320. int i;
  321. /* TODO: Use bulk call */
  322. for (i = 0; i < hpte_count; i++) {
  323. /* dont remove HPTEs with VRMA mappings */
  324. lpar_rc = plpar_pte_remove_raw(H_ANDCOND, i, HPTE_V_1TB_SEG,
  325. &dummy1, &dummy2);
  326. if (lpar_rc == H_NOT_FOUND) {
  327. lpar_rc = plpar_pte_read_raw(0, i, &dword0, &dummy1);
  328. if (!lpar_rc && ((dword0 & HPTE_V_VRMA_MASK)
  329. != HPTE_V_VRMA_MASK))
  330. /* Can be hpte for 1TB Seg. So remove it */
  331. plpar_pte_remove_raw(0, i, 0, &dummy1, &dummy2);
  332. }
  333. }
  334. }
  335. /*
  336. * This computes the AVPN and B fields of the first dword of a HPTE,
  337. * for use when we want to match an existing PTE. The bottom 7 bits
  338. * of the returned value are zero.
  339. */
  340. static inline unsigned long hpte_encode_avpn(unsigned long va, int psize,
  341. int ssize)
  342. {
  343. unsigned long v;
  344. v = (va >> 23) & ~(mmu_psize_defs[psize].avpnm);
  345. v <<= HPTE_V_AVPN_SHIFT;
  346. v |= ((unsigned long) ssize) << HPTE_V_SSIZE_SHIFT;
  347. return v;
  348. }
  349. /*
  350. * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
  351. * the low 3 bits of flags happen to line up. So no transform is needed.
  352. * We can probably optimize here and assume the high bits of newpp are
  353. * already zero. For now I am paranoid.
  354. */
  355. static long pSeries_lpar_hpte_updatepp(unsigned long slot,
  356. unsigned long newpp,
  357. unsigned long va,
  358. int psize, int ssize, int local)
  359. {
  360. unsigned long lpar_rc;
  361. unsigned long flags = (newpp & 7) | H_AVPN;
  362. unsigned long want_v;
  363. want_v = hpte_encode_avpn(va, psize, ssize);
  364. pr_debug(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
  365. want_v, slot, flags, psize);
  366. lpar_rc = plpar_pte_protect(flags, slot, want_v);
  367. if (lpar_rc == H_NOT_FOUND) {
  368. pr_debug("not found !\n");
  369. return -1;
  370. }
  371. pr_debug("ok\n");
  372. BUG_ON(lpar_rc != H_SUCCESS);
  373. return 0;
  374. }
  375. static unsigned long pSeries_lpar_hpte_getword0(unsigned long slot)
  376. {
  377. unsigned long dword0;
  378. unsigned long lpar_rc;
  379. unsigned long dummy_word1;
  380. unsigned long flags;
  381. /* Read 1 pte at a time */
  382. /* Do not need RPN to logical page translation */
  383. /* No cross CEC PFT access */
  384. flags = 0;
  385. lpar_rc = plpar_pte_read(flags, slot, &dword0, &dummy_word1);
  386. BUG_ON(lpar_rc != H_SUCCESS);
  387. return dword0;
  388. }
  389. static long pSeries_lpar_hpte_find(unsigned long va, int psize, int ssize)
  390. {
  391. unsigned long hash;
  392. unsigned long i;
  393. long slot;
  394. unsigned long want_v, hpte_v;
  395. hash = hpt_hash(va, mmu_psize_defs[psize].shift, ssize);
  396. want_v = hpte_encode_avpn(va, psize, ssize);
  397. /* Bolted entries are always in the primary group */
  398. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  399. for (i = 0; i < HPTES_PER_GROUP; i++) {
  400. hpte_v = pSeries_lpar_hpte_getword0(slot);
  401. if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
  402. /* HPTE matches */
  403. return slot;
  404. ++slot;
  405. }
  406. return -1;
  407. }
  408. static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
  409. unsigned long ea,
  410. int psize, int ssize)
  411. {
  412. unsigned long lpar_rc, slot, vsid, va, flags;
  413. vsid = get_kernel_vsid(ea, ssize);
  414. va = hpt_va(ea, vsid, ssize);
  415. slot = pSeries_lpar_hpte_find(va, psize, ssize);
  416. BUG_ON(slot == -1);
  417. flags = newpp & 7;
  418. lpar_rc = plpar_pte_protect(flags, slot, 0);
  419. BUG_ON(lpar_rc != H_SUCCESS);
  420. }
  421. static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
  422. int psize, int ssize, int local)
  423. {
  424. unsigned long want_v;
  425. unsigned long lpar_rc;
  426. unsigned long dummy1, dummy2;
  427. pr_debug(" inval : slot=%lx, va=%016lx, psize: %d, local: %d\n",
  428. slot, va, psize, local);
  429. want_v = hpte_encode_avpn(va, psize, ssize);
  430. lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
  431. if (lpar_rc == H_NOT_FOUND)
  432. return;
  433. BUG_ON(lpar_rc != H_SUCCESS);
  434. }
  435. static void pSeries_lpar_hpte_removebolted(unsigned long ea,
  436. int psize, int ssize)
  437. {
  438. unsigned long slot, vsid, va;
  439. vsid = get_kernel_vsid(ea, ssize);
  440. va = hpt_va(ea, vsid, ssize);
  441. slot = pSeries_lpar_hpte_find(va, psize, ssize);
  442. BUG_ON(slot == -1);
  443. pSeries_lpar_hpte_invalidate(slot, va, psize, ssize, 0);
  444. }
  445. /* Flag bits for H_BULK_REMOVE */
  446. #define HBR_REQUEST 0x4000000000000000UL
  447. #define HBR_RESPONSE 0x8000000000000000UL
  448. #define HBR_END 0xc000000000000000UL
  449. #define HBR_AVPN 0x0200000000000000UL
  450. #define HBR_ANDCOND 0x0100000000000000UL
  451. /*
  452. * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
  453. * lock.
  454. */
  455. static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
  456. {
  457. unsigned long i, pix, rc;
  458. unsigned long flags = 0;
  459. struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
  460. int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
  461. unsigned long param[9];
  462. unsigned long va;
  463. unsigned long hash, index, shift, hidx, slot;
  464. real_pte_t pte;
  465. int psize, ssize;
  466. if (lock_tlbie)
  467. spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
  468. psize = batch->psize;
  469. ssize = batch->ssize;
  470. pix = 0;
  471. for (i = 0; i < number; i++) {
  472. va = batch->vaddr[i];
  473. pte = batch->pte[i];
  474. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  475. hash = hpt_hash(va, shift, ssize);
  476. hidx = __rpte_to_hidx(pte, index);
  477. if (hidx & _PTEIDX_SECONDARY)
  478. hash = ~hash;
  479. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  480. slot += hidx & _PTEIDX_GROUP_IX;
  481. if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
  482. pSeries_lpar_hpte_invalidate(slot, va, psize,
  483. ssize, local);
  484. } else {
  485. param[pix] = HBR_REQUEST | HBR_AVPN | slot;
  486. param[pix+1] = hpte_encode_avpn(va, psize,
  487. ssize);
  488. pix += 2;
  489. if (pix == 8) {
  490. rc = plpar_hcall9(H_BULK_REMOVE, param,
  491. param[0], param[1], param[2],
  492. param[3], param[4], param[5],
  493. param[6], param[7]);
  494. BUG_ON(rc != H_SUCCESS);
  495. pix = 0;
  496. }
  497. }
  498. } pte_iterate_hashed_end();
  499. }
  500. if (pix) {
  501. param[pix] = HBR_END;
  502. rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
  503. param[2], param[3], param[4], param[5],
  504. param[6], param[7]);
  505. BUG_ON(rc != H_SUCCESS);
  506. }
  507. if (lock_tlbie)
  508. spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
  509. }
  510. void __init hpte_init_lpar(void)
  511. {
  512. ppc_md.hpte_invalidate = pSeries_lpar_hpte_invalidate;
  513. ppc_md.hpte_updatepp = pSeries_lpar_hpte_updatepp;
  514. ppc_md.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
  515. ppc_md.hpte_insert = pSeries_lpar_hpte_insert;
  516. ppc_md.hpte_remove = pSeries_lpar_hpte_remove;
  517. ppc_md.hpte_removebolted = pSeries_lpar_hpte_removebolted;
  518. ppc_md.flush_hash_range = pSeries_lpar_flush_hash_range;
  519. ppc_md.hpte_clear_all = pSeries_lpar_hptab_clear;
  520. }