lpar.c 13 KB

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