lpar.c 15 KB

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