lpar.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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/config.h>
  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/abs_addr.h>
  38. #include <asm/cputable.h>
  39. #include <asm/udbg.h>
  40. #include <asm/smp.h>
  41. #include "plpar_wrappers.h"
  42. #ifdef DEBUG_LOW
  43. #define DBG_LOW(fmt...) do { udbg_printf(fmt); } while(0)
  44. #else
  45. #define DBG_LOW(fmt...) do { } while(0)
  46. #endif
  47. /* in pSeries_hvCall.S */
  48. EXPORT_SYMBOL(plpar_hcall);
  49. EXPORT_SYMBOL(plpar_hcall_4out);
  50. EXPORT_SYMBOL(plpar_hcall_norets);
  51. EXPORT_SYMBOL(plpar_hcall_8arg_2ret);
  52. extern void pSeries_find_serial_port(void);
  53. int vtermno; /* virtual terminal# for udbg */
  54. #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
  55. static void udbg_hvsi_putc(char c)
  56. {
  57. /* packet's seqno isn't used anyways */
  58. uint8_t packet[] __ALIGNED__ = { 0xff, 5, 0, 0, c };
  59. int rc;
  60. if (c == '\n')
  61. udbg_hvsi_putc('\r');
  62. do {
  63. rc = plpar_put_term_char(vtermno, sizeof(packet), packet);
  64. } while (rc == H_Busy);
  65. }
  66. static long hvsi_udbg_buf_len;
  67. static uint8_t hvsi_udbg_buf[256];
  68. static int udbg_hvsi_getc_poll(void)
  69. {
  70. unsigned char ch;
  71. int rc, i;
  72. if (hvsi_udbg_buf_len == 0) {
  73. rc = plpar_get_term_char(vtermno, &hvsi_udbg_buf_len, hvsi_udbg_buf);
  74. if (rc != H_Success || hvsi_udbg_buf[0] != 0xff) {
  75. /* bad read or non-data packet */
  76. hvsi_udbg_buf_len = 0;
  77. } else {
  78. /* remove the packet header */
  79. for (i = 4; i < hvsi_udbg_buf_len; i++)
  80. hvsi_udbg_buf[i-4] = hvsi_udbg_buf[i];
  81. hvsi_udbg_buf_len -= 4;
  82. }
  83. }
  84. if (hvsi_udbg_buf_len <= 0 || hvsi_udbg_buf_len > 256) {
  85. /* no data ready */
  86. hvsi_udbg_buf_len = 0;
  87. return -1;
  88. }
  89. ch = hvsi_udbg_buf[0];
  90. /* shift remaining data down */
  91. for (i = 1; i < hvsi_udbg_buf_len; i++) {
  92. hvsi_udbg_buf[i-1] = hvsi_udbg_buf[i];
  93. }
  94. hvsi_udbg_buf_len--;
  95. return ch;
  96. }
  97. static int udbg_hvsi_getc(void)
  98. {
  99. int ch;
  100. for (;;) {
  101. ch = udbg_hvsi_getc_poll();
  102. if (ch == -1) {
  103. /* This shouldn't be needed...but... */
  104. volatile unsigned long delay;
  105. for (delay=0; delay < 2000000; delay++)
  106. ;
  107. } else {
  108. return ch;
  109. }
  110. }
  111. }
  112. static void udbg_putcLP(char c)
  113. {
  114. char buf[16];
  115. unsigned long rc;
  116. if (c == '\n')
  117. udbg_putcLP('\r');
  118. buf[0] = c;
  119. do {
  120. rc = plpar_put_term_char(vtermno, 1, buf);
  121. } while(rc == H_Busy);
  122. }
  123. /* Buffered chars getc */
  124. static long inbuflen;
  125. static long inbuf[2]; /* must be 2 longs */
  126. static int udbg_getc_pollLP(void)
  127. {
  128. /* The interface is tricky because it may return up to 16 chars.
  129. * We save them statically for future calls to udbg_getc().
  130. */
  131. char ch, *buf = (char *)inbuf;
  132. int i;
  133. long rc;
  134. if (inbuflen == 0) {
  135. /* get some more chars. */
  136. inbuflen = 0;
  137. rc = plpar_get_term_char(vtermno, &inbuflen, buf);
  138. if (rc != H_Success)
  139. inbuflen = 0; /* otherwise inbuflen is garbage */
  140. }
  141. if (inbuflen <= 0 || inbuflen > 16) {
  142. /* Catch error case as well as other oddities (corruption) */
  143. inbuflen = 0;
  144. return -1;
  145. }
  146. ch = buf[0];
  147. for (i = 1; i < inbuflen; i++) /* shuffle them down. */
  148. buf[i-1] = buf[i];
  149. inbuflen--;
  150. return ch;
  151. }
  152. static int udbg_getcLP(void)
  153. {
  154. int ch;
  155. for (;;) {
  156. ch = udbg_getc_pollLP();
  157. if (ch == -1) {
  158. /* This shouldn't be needed...but... */
  159. volatile unsigned long delay;
  160. for (delay=0; delay < 2000000; delay++)
  161. ;
  162. } else {
  163. return ch;
  164. }
  165. }
  166. }
  167. /* call this from early_init() for a working debug console on
  168. * vterm capable LPAR machines
  169. */
  170. void __init udbg_init_debug_lpar(void)
  171. {
  172. vtermno = 0;
  173. udbg_putc = udbg_putcLP;
  174. udbg_getc = udbg_getcLP;
  175. udbg_getc_poll = udbg_getc_pollLP;
  176. }
  177. /* returns 0 if couldn't find or use /chosen/stdout as console */
  178. void __init find_udbg_vterm(void)
  179. {
  180. struct device_node *stdout_node;
  181. u32 *termno;
  182. char *name;
  183. int add_console;
  184. /* find the boot console from /chosen/stdout */
  185. if (!of_chosen)
  186. return;
  187. name = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
  188. if (name == NULL)
  189. return;
  190. stdout_node = of_find_node_by_path(name);
  191. if (!stdout_node)
  192. return;
  193. name = (char *)get_property(stdout_node, "name", NULL);
  194. if (!name) {
  195. printk(KERN_WARNING "stdout node missing 'name' property!\n");
  196. goto out;
  197. }
  198. /* The user has requested a console so this is already set up. */
  199. add_console = !strstr(cmd_line, "console=");
  200. /* Check if it's a virtual terminal */
  201. if (strncmp(name, "vty", 3) != 0)
  202. goto out;
  203. termno = (u32 *)get_property(stdout_node, "reg", NULL);
  204. if (termno == NULL)
  205. goto out;
  206. vtermno = termno[0];
  207. if (device_is_compatible(stdout_node, "hvterm1")) {
  208. udbg_putc = udbg_putcLP;
  209. udbg_getc = udbg_getcLP;
  210. udbg_getc_poll = udbg_getc_pollLP;
  211. if (add_console)
  212. add_preferred_console("hvc", termno[0] & 0xff, NULL);
  213. } else if (device_is_compatible(stdout_node, "hvterm-protocol")) {
  214. vtermno = termno[0];
  215. udbg_putc = udbg_hvsi_putc;
  216. udbg_getc = udbg_hvsi_getc;
  217. udbg_getc_poll = udbg_hvsi_getc_poll;
  218. if (add_console)
  219. add_preferred_console("hvsi", termno[0] & 0xff, NULL);
  220. }
  221. out:
  222. of_node_put(stdout_node);
  223. }
  224. void vpa_init(int cpu)
  225. {
  226. int hwcpu = get_hard_smp_processor_id(cpu);
  227. unsigned long vpa = __pa(&lppaca[cpu]);
  228. long ret;
  229. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  230. lppaca[cpu].vmxregs_in_use = 1;
  231. ret = register_vpa(hwcpu, vpa);
  232. if (ret)
  233. printk(KERN_ERR "WARNING: vpa_init: VPA registration for "
  234. "cpu %d (hw %d) of area %lx returns %ld\n",
  235. cpu, hwcpu, vpa, ret);
  236. }
  237. long pSeries_lpar_hpte_insert(unsigned long hpte_group,
  238. unsigned long va, unsigned long pa,
  239. unsigned long rflags, unsigned long vflags,
  240. int psize)
  241. {
  242. unsigned long lpar_rc;
  243. unsigned long flags;
  244. unsigned long slot;
  245. unsigned long hpte_v, hpte_r;
  246. unsigned long dummy0, dummy1;
  247. if (!(vflags & HPTE_V_BOLTED))
  248. DBG_LOW("hpte_insert(group=%lx, va=%016lx, pa=%016lx, "
  249. "rflags=%lx, vflags=%lx, psize=%d)\n",
  250. hpte_group, va, pa, rflags, vflags, psize);
  251. hpte_v = hpte_encode_v(va, psize) | vflags | HPTE_V_VALID;
  252. hpte_r = hpte_encode_r(pa, psize) | rflags;
  253. if (!(vflags & HPTE_V_BOLTED))
  254. DBG_LOW(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
  255. /* Now fill in the actual HPTE */
  256. /* Set CEC cookie to 0 */
  257. /* Zero page = 0 */
  258. /* I-cache Invalidate = 0 */
  259. /* I-cache synchronize = 0 */
  260. /* Exact = 0 */
  261. flags = 0;
  262. /* Make pHyp happy */
  263. if (rflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
  264. hpte_r &= ~_PAGE_COHERENT;
  265. lpar_rc = plpar_hcall(H_ENTER, flags, hpte_group, hpte_v,
  266. hpte_r, &slot, &dummy0, &dummy1);
  267. if (unlikely(lpar_rc == H_PTEG_Full)) {
  268. if (!(vflags & HPTE_V_BOLTED))
  269. DBG_LOW(" full\n");
  270. return -1;
  271. }
  272. /*
  273. * Since we try and ioremap PHBs we don't own, the pte insert
  274. * will fail. However we must catch the failure in hash_page
  275. * or we will loop forever, so return -2 in this case.
  276. */
  277. if (unlikely(lpar_rc != H_Success)) {
  278. if (!(vflags & HPTE_V_BOLTED))
  279. DBG_LOW(" lpar err %d\n", lpar_rc);
  280. return -2;
  281. }
  282. if (!(vflags & HPTE_V_BOLTED))
  283. DBG_LOW(" -> slot: %d\n", slot & 7);
  284. /* Because of iSeries, we have to pass down the secondary
  285. * bucket bit here as well
  286. */
  287. return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
  288. }
  289. static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
  290. static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
  291. {
  292. unsigned long slot_offset;
  293. unsigned long lpar_rc;
  294. int i;
  295. unsigned long dummy1, dummy2;
  296. /* pick a random slot to start at */
  297. slot_offset = mftb() & 0x7;
  298. for (i = 0; i < HPTES_PER_GROUP; i++) {
  299. /* don't remove a bolted entry */
  300. lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
  301. (0x1UL << 4), &dummy1, &dummy2);
  302. if (lpar_rc == H_Success)
  303. return i;
  304. BUG_ON(lpar_rc != H_Not_Found);
  305. slot_offset++;
  306. slot_offset &= 0x7;
  307. }
  308. return -1;
  309. }
  310. static void pSeries_lpar_hptab_clear(void)
  311. {
  312. unsigned long size_bytes = 1UL << ppc64_pft_size;
  313. unsigned long hpte_count = size_bytes >> 4;
  314. unsigned long dummy1, dummy2;
  315. int i;
  316. /* TODO: Use bulk call */
  317. for (i = 0; i < hpte_count; i++)
  318. plpar_pte_remove(0, i, 0, &dummy1, &dummy2);
  319. }
  320. /*
  321. * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
  322. * the low 3 bits of flags happen to line up. So no transform is needed.
  323. * We can probably optimize here and assume the high bits of newpp are
  324. * already zero. For now I am paranoid.
  325. */
  326. static long pSeries_lpar_hpte_updatepp(unsigned long slot,
  327. unsigned long newpp,
  328. unsigned long va,
  329. int psize, int local)
  330. {
  331. unsigned long lpar_rc;
  332. unsigned long flags = (newpp & 7) | H_AVPN;
  333. unsigned long want_v;
  334. want_v = hpte_encode_v(va, psize);
  335. DBG_LOW(" update: avpnv=%016lx, hash=%016lx, f=%x, psize: %d ... ",
  336. want_v & HPTE_V_AVPN, slot, flags, psize);
  337. lpar_rc = plpar_pte_protect(flags, slot, want_v & HPTE_V_AVPN);
  338. if (lpar_rc == H_Not_Found) {
  339. DBG_LOW("not found !\n");
  340. return -1;
  341. }
  342. DBG_LOW("ok\n");
  343. BUG_ON(lpar_rc != H_Success);
  344. return 0;
  345. }
  346. static unsigned long pSeries_lpar_hpte_getword0(unsigned long slot)
  347. {
  348. unsigned long dword0;
  349. unsigned long lpar_rc;
  350. unsigned long dummy_word1;
  351. unsigned long flags;
  352. /* Read 1 pte at a time */
  353. /* Do not need RPN to logical page translation */
  354. /* No cross CEC PFT access */
  355. flags = 0;
  356. lpar_rc = plpar_pte_read(flags, slot, &dword0, &dummy_word1);
  357. BUG_ON(lpar_rc != H_Success);
  358. return dword0;
  359. }
  360. static long pSeries_lpar_hpte_find(unsigned long va, int psize)
  361. {
  362. unsigned long hash;
  363. unsigned long i, j;
  364. long slot;
  365. unsigned long want_v, hpte_v;
  366. hash = hpt_hash(va, mmu_psize_defs[psize].shift);
  367. want_v = hpte_encode_v(va, psize);
  368. for (j = 0; j < 2; j++) {
  369. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  370. for (i = 0; i < HPTES_PER_GROUP; i++) {
  371. hpte_v = pSeries_lpar_hpte_getword0(slot);
  372. if (HPTE_V_COMPARE(hpte_v, want_v)
  373. && (hpte_v & HPTE_V_VALID)
  374. && (!!(hpte_v & HPTE_V_SECONDARY) == j)) {
  375. /* HPTE matches */
  376. if (j)
  377. slot = -slot;
  378. return slot;
  379. }
  380. ++slot;
  381. }
  382. hash = ~hash;
  383. }
  384. return -1;
  385. }
  386. static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
  387. unsigned long ea,
  388. int psize)
  389. {
  390. unsigned long lpar_rc, slot, vsid, va, flags;
  391. vsid = get_kernel_vsid(ea);
  392. va = (vsid << 28) | (ea & 0x0fffffff);
  393. slot = pSeries_lpar_hpte_find(va, psize);
  394. BUG_ON(slot == -1);
  395. flags = newpp & 7;
  396. lpar_rc = plpar_pte_protect(flags, slot, 0);
  397. BUG_ON(lpar_rc != H_Success);
  398. }
  399. static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
  400. int psize, int local)
  401. {
  402. unsigned long want_v;
  403. unsigned long lpar_rc;
  404. unsigned long dummy1, dummy2;
  405. DBG_LOW(" inval : slot=%lx, va=%016lx, psize: %d, local: %d",
  406. slot, va, psize, local);
  407. want_v = hpte_encode_v(va, psize);
  408. lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v & HPTE_V_AVPN,
  409. &dummy1, &dummy2);
  410. if (lpar_rc == H_Not_Found)
  411. return;
  412. BUG_ON(lpar_rc != H_Success);
  413. }
  414. /*
  415. * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
  416. * lock.
  417. */
  418. void pSeries_lpar_flush_hash_range(unsigned long number, int local)
  419. {
  420. int i;
  421. unsigned long flags = 0;
  422. struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
  423. int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
  424. if (lock_tlbie)
  425. spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
  426. for (i = 0; i < number; i++)
  427. flush_hash_page(batch->vaddr[i], batch->pte[i],
  428. batch->psize, local);
  429. if (lock_tlbie)
  430. spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
  431. }
  432. void hpte_init_lpar(void)
  433. {
  434. ppc_md.hpte_invalidate = pSeries_lpar_hpte_invalidate;
  435. ppc_md.hpte_updatepp = pSeries_lpar_hpte_updatepp;
  436. ppc_md.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
  437. ppc_md.hpte_insert = pSeries_lpar_hpte_insert;
  438. ppc_md.hpte_remove = pSeries_lpar_hpte_remove;
  439. ppc_md.flush_hash_range = pSeries_lpar_flush_hash_range;
  440. ppc_md.hpte_clear_all = pSeries_lpar_hptab_clear;
  441. htab_finish_init();
  442. }