lpar.c 13 KB

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