lpar.c 13 KB

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