pSeries_lpar.c 12 KB

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