3 * Copyright (C) 2001 Todd Inglett, IBM Corporation
5 * pSeries LPAR support.
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.
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.
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
22 #include <linux/config.h>
23 #include <linux/kernel.h>
24 #include <linux/dma-mapping.h>
25 #include <asm/processor.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>
35 #include <asm/tlbflush.h>
37 #include <asm/hvcall.h>
39 #include <asm/abs_addr.h>
40 #include <asm/cputable.h>
42 /* in pSeries_hvCall.S */
43 EXPORT_SYMBOL(plpar_hcall);
44 EXPORT_SYMBOL(plpar_hcall_4out);
45 EXPORT_SYMBOL(plpar_hcall_norets);
46 EXPORT_SYMBOL(plpar_hcall_8arg_2ret);
48 long poll_pending(void)
51 return plpar_hcall(H_POLL_PENDING, 0, 0, 0, 0,
52 &dummy, &dummy, &dummy);
55 long prod_processor(void)
57 plpar_hcall_norets(H_PROD);
61 long cede_processor(void)
63 plpar_hcall_norets(H_CEDE);
67 long register_vpa(unsigned long flags, unsigned long proc, unsigned long vpa)
69 plpar_hcall_norets(H_REGISTER_VPA, flags, proc, vpa);
73 long plpar_pte_remove(unsigned long flags,
76 unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
79 return plpar_hcall(H_REMOVE, flags, ptex, avpn, 0,
80 old_pteh_ret, old_ptel_ret, &dummy);
83 long plpar_pte_read(unsigned long flags,
85 unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
88 return plpar_hcall(H_READ, flags, ptex, 0, 0,
89 old_pteh_ret, old_ptel_ret, &dummy);
92 long plpar_pte_protect(unsigned long flags,
96 return plpar_hcall_norets(H_PROTECT, flags, ptex, avpn);
99 long plpar_tce_get(unsigned long liobn,
101 unsigned long *tce_ret)
104 return plpar_hcall(H_GET_TCE, liobn, ioba, 0, 0,
105 tce_ret, &dummy, &dummy);
108 long plpar_tce_put(unsigned long liobn,
110 unsigned long tceval)
112 return plpar_hcall_norets(H_PUT_TCE, liobn, ioba, tceval);
115 long plpar_get_term_char(unsigned long termno,
116 unsigned long *len_ret,
119 unsigned long *lbuf = (unsigned long *)buf_ret; /* ToDo: alignment? */
120 return plpar_hcall(H_GET_TERM_CHAR, termno, 0, 0, 0,
121 len_ret, lbuf+0, lbuf+1);
124 long plpar_put_term_char(unsigned long termno,
128 unsigned long *lbuf = (unsigned long *)buffer; /* ToDo: alignment? */
129 return plpar_hcall_norets(H_PUT_TERM_CHAR, termno, len, lbuf[0],
133 static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
134 long npages, unsigned long uaddr,
135 enum dma_data_direction direction)
141 tce.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
143 if (direction != DMA_TO_DEVICE)
147 rc = plpar_tce_put((u64)tbl->it_index,
151 if (rc && printk_ratelimit()) {
152 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
153 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
154 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
155 printk("\ttce val = 0x%lx\n", tce.te_word );
156 show_stack(current, (unsigned long *)__get_SP());
164 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
172 rc = plpar_tce_put((u64)tbl->it_index,
176 if (rc && printk_ratelimit()) {
177 printk("tce_free_pSeriesLP: plpar_tce_put failed\n");
178 printk("\trc = %ld\n", rc);
179 printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
180 printk("\ttcenum = 0x%lx\n", (u64)tcenum);
181 printk("\ttce val = 0x%lx\n", tce.te_word );
182 show_stack(current, (unsigned long *)__get_SP());
189 int vtermno; /* virtual terminal# for udbg */
191 static void udbg_putcLP(unsigned char c)
201 rc = plpar_put_term_char(vtermno, 1, buf);
202 } while(rc == H_Busy);
205 /* Buffered chars getc */
206 static long inbuflen;
207 static long inbuf[2]; /* must be 2 longs */
209 static int udbg_getc_pollLP(void)
211 /* The interface is tricky because it may return up to 16 chars.
212 * We save them statically for future calls to udbg_getc().
214 char ch, *buf = (char *)inbuf;
218 /* get some more chars. */
220 rc = plpar_get_term_char(vtermno, &inbuflen, buf);
222 inbuflen = 0; /* otherwise inbuflen is garbage */
224 if (inbuflen <= 0 || inbuflen > 16) {
225 /* Catch error case as well as other oddities (corruption) */
230 for (i = 1; i < inbuflen; i++) /* shuffle them down. */
236 static unsigned char udbg_getcLP(void)
240 ch = udbg_getc_pollLP();
242 /* This shouldn't be needed...but... */
243 volatile unsigned long delay;
244 for (delay=0; delay < 2000000; delay++)
252 /* returns 0 if couldn't find or use /chosen/stdout as console */
253 static int find_udbg_vterm(void)
255 struct device_node *stdout_node;
260 /* find the boot console from /chosen/stdout */
261 if (!of_stdout_device) {
262 printk(KERN_WARNING "couldn't get path from /chosen/stdout!\n");
265 stdout_node = of_find_node_by_path(of_stdout_device);
267 printk(KERN_WARNING "couldn't find node from /chosen/stdout\n");
271 /* now we have the stdout node; figure out what type of device it is. */
272 name = (char *)get_property(stdout_node, "name", 0);
274 printk(KERN_WARNING "stdout node missing 'name' property!\n");
278 if (strncmp(name, "vty", 3) == 0) {
279 if (device_is_compatible(stdout_node, "hvterm1")) {
280 termno = (u32 *)get_property(stdout_node, "reg", 0);
283 ppc_md.udbg_putc = udbg_putcLP;
284 ppc_md.udbg_getc = udbg_getcLP;
285 ppc_md.udbg_getc_poll = udbg_getc_pollLP;
289 /* XXX implement udbg_putcLP_vtty for hvterm-protocol1 case */
290 printk(KERN_WARNING "%s doesn't speak hvterm1; "
291 "can't print udbg messages\n", of_stdout_device);
293 } else if (strncmp(name, "serial", 6)) {
294 /* XXX fix ISA serial console */
295 printk(KERN_WARNING "serial stdout on LPAR ('%s')! "
296 "can't print udbg messages\n", of_stdout_device);
298 printk(KERN_WARNING "don't know how to print to stdout '%s'\n",
303 of_node_put(stdout_node);
307 void pSeries_lpar_mm_init(void);
309 /* This is called early in setup.c.
310 * Use it to setup page table ppc_md stuff as well as udbg.
312 void pSeriesLP_init_early(void)
314 pSeries_lpar_mm_init();
318 ppc_md.tce_build = tce_build_pSeriesLP;
319 ppc_md.tce_free = tce_free_pSeriesLP;
327 /* The keyboard is not useful in the LPAR environment.
328 * Leave all the ppc_md keyboard interfaces NULL.
331 if (0 == find_udbg_vterm()) {
333 "can't use stdout; can't print early debug messages.\n");
337 long pSeries_lpar_hpte_insert(unsigned long hpte_group,
338 unsigned long va, unsigned long prpn,
339 int secondary, unsigned long hpteflags,
340 int bolted, int large)
342 unsigned long arpn = physRpn_to_absRpn(prpn);
343 unsigned long lpar_rc;
347 unsigned long dummy0, dummy1;
349 /* Fill in the local HPTE with absolute rpn, avpn and flags */
350 lhpte.dw1.dword1 = 0;
351 lhpte.dw1.dw1.rpn = arpn;
352 lhpte.dw1.flags.flags = hpteflags;
354 lhpte.dw0.dword0 = 0;
355 lhpte.dw0.dw0.avpn = va >> 23;
356 lhpte.dw0.dw0.h = secondary;
357 lhpte.dw0.dw0.bolted = bolted;
362 lhpte.dw0.dw0.avpn &= ~0x1UL;
365 /* Now fill in the actual HPTE */
366 /* Set CEC cookie to 0 */
368 /* I-cache Invalidate = 0 */
369 /* I-cache synchronize = 0 */
373 /* XXX why is this here? - Anton */
374 if (hpteflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
375 lhpte.dw1.flags.flags &= ~_PAGE_COHERENT;
377 lpar_rc = plpar_hcall(H_ENTER, flags, hpte_group, lhpte.dw0.dword0,
378 lhpte.dw1.dword1, &slot, &dummy0, &dummy1);
380 if (lpar_rc == H_PTEG_Full)
384 * Since we try and ioremap PHBs we don't own, the pte insert
385 * will fail. However we must catch the failure in hash_page
386 * or we will loop forever, so return -2 in this case.
388 if (lpar_rc != H_Success)
391 /* Because of iSeries, we have to pass down the secondary
392 * bucket bit here as well
394 return (slot & 7) | (secondary << 3);
397 static spinlock_t pSeries_lpar_tlbie_lock = SPIN_LOCK_UNLOCKED;
399 static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
401 unsigned long slot_offset;
402 unsigned long lpar_rc;
404 unsigned long dummy1, dummy2;
406 /* pick a random slot to start at */
407 slot_offset = mftb() & 0x7;
409 for (i = 0; i < HPTES_PER_GROUP; i++) {
411 /* don't remove a bolted entry */
412 lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
413 (0x1UL << 4), &dummy1, &dummy2);
415 if (lpar_rc == H_Success)
418 if (lpar_rc != H_Not_Found)
419 panic("Bad return code from pte remove rc = %lx\n",
430 * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
431 * the low 3 bits of flags happen to line up. So no transform is needed.
432 * We can probably optimize here and assume the high bits of newpp are
433 * already zero. For now I am paranoid.
435 static long pSeries_lpar_hpte_updatepp(unsigned long slot, unsigned long newpp,
436 unsigned long va, int large, int local)
438 unsigned long lpar_rc;
439 unsigned long flags = (newpp & 7) | H_AVPN;
440 unsigned long avpn = va >> 23;
445 lpar_rc = plpar_pte_protect(flags, slot, (avpn << 7));
447 if (lpar_rc == H_Not_Found)
450 if (lpar_rc != H_Success)
451 panic("bad return code from pte protect rc = %lx\n", lpar_rc);
456 static unsigned long pSeries_lpar_hpte_getword0(unsigned long slot)
458 unsigned long dword0;
459 unsigned long lpar_rc;
460 unsigned long dummy_word1;
463 /* Read 1 pte at a time */
464 /* Do not need RPN to logical page translation */
465 /* No cross CEC PFT access */
468 lpar_rc = plpar_pte_read(flags, slot, &dword0, &dummy_word1);
470 if (lpar_rc != H_Success)
471 panic("Error on pte read in get_hpte0 rc = %lx\n", lpar_rc);
476 static long pSeries_lpar_hpte_find(unsigned long vpn)
482 unsigned long dword0;
487 hash = hpt_hash(vpn, 0);
489 for (j = 0; j < 2; j++) {
490 slot = (hash & htab_data.htab_hash_mask) * HPTES_PER_GROUP;
491 for (i = 0; i < HPTES_PER_GROUP; i++) {
492 hpte_dw0.dword0 = pSeries_lpar_hpte_getword0(slot);
495 if ((dw0.avpn == (vpn >> 11)) && dw0.v &&
510 static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
513 unsigned long lpar_rc;
514 unsigned long vsid, va, vpn, flags;
517 vsid = get_kernel_vsid(ea);
518 va = (vsid << 28) | (ea & 0x0fffffff);
519 vpn = va >> PAGE_SHIFT;
521 slot = pSeries_lpar_hpte_find(vpn);
523 panic("updateboltedpp: Could not find page to bolt\n");
526 lpar_rc = plpar_pte_protect(flags, slot, 0);
528 if (lpar_rc != H_Success)
529 panic("Bad return code from pte bolted protect rc = %lx\n",
533 static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
534 int large, int local)
536 unsigned long avpn = va >> 23;
537 unsigned long lpar_rc;
538 unsigned long dummy1, dummy2;
543 lpar_rc = plpar_pte_remove(H_AVPN, slot, (avpn << 7), &dummy1,
546 if (lpar_rc == H_Not_Found)
549 if (lpar_rc != H_Success)
550 panic("Bad return code from invalidate rc = %lx\n", lpar_rc);
554 * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
557 void pSeries_lpar_flush_hash_range(unsigned long context, unsigned long number,
562 struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
564 if (!(cur_cpu_spec->cpu_features & CPU_FTR_LOCKLESS_TLBIE))
565 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
567 for (i = 0; i < number; i++)
568 flush_hash_page(context, batch->addr[i], batch->pte[i], local);
570 if (!(cur_cpu_spec->cpu_features & CPU_FTR_LOCKLESS_TLBIE))
571 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
574 void pSeries_lpar_mm_init(void)
576 ppc_md.hpte_invalidate = pSeries_lpar_hpte_invalidate;
577 ppc_md.hpte_updatepp = pSeries_lpar_hpte_updatepp;
578 ppc_md.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
579 ppc_md.hpte_insert = pSeries_lpar_hpte_insert;
580 ppc_md.hpte_remove = pSeries_lpar_hpte_remove;
581 ppc_md.flush_hash_range = pSeries_lpar_flush_hash_range;