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 
22 #undef DEBUG_LOW
23 
24 #include <linux/kernel.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/console.h>
27 #include <asm/processor.h>
28 #include <asm/mmu.h>
29 #include <asm/page.h>
30 #include <asm/pgtable.h>
31 #include <asm/machdep.h>
32 #include <asm/abs_addr.h>
33 #include <asm/mmu_context.h>
34 #include <asm/iommu.h>
35 #include <asm/tlbflush.h>
36 #include <asm/tlb.h>
37 #include <asm/prom.h>
38 #include <asm/abs_addr.h>
39 #include <asm/cputable.h>
40 #include <asm/udbg.h>
41 #include <asm/smp.h>
42 
43 #include "plpar_wrappers.h"
44 
45 #ifdef DEBUG_LOW
46 #define DBG_LOW(fmt...) do { udbg_printf(fmt); } while(0)
47 #else
48 #define DBG_LOW(fmt...) do { } while(0)
49 #endif
50 
51 /* in hvCall.S */
52 EXPORT_SYMBOL(plpar_hcall);
53 EXPORT_SYMBOL(plpar_hcall9);
54 EXPORT_SYMBOL(plpar_hcall_norets);
55 
56 extern void pSeries_find_serial_port(void);
57 
58 
59 int vtermno;	/* virtual terminal# for udbg  */
60 
61 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
62 static void udbg_hvsi_putc(char c)
63 {
64 	/* packet's seqno isn't used anyways */
65 	uint8_t packet[] __ALIGNED__ = { 0xff, 5, 0, 0, c };
66 	int rc;
67 
68 	if (c == '\n')
69 		udbg_hvsi_putc('\r');
70 
71 	do {
72 		rc = plpar_put_term_char(vtermno, sizeof(packet), packet);
73 	} while (rc == H_BUSY);
74 }
75 
76 static long hvsi_udbg_buf_len;
77 static uint8_t hvsi_udbg_buf[256];
78 
79 static int udbg_hvsi_getc_poll(void)
80 {
81 	unsigned char ch;
82 	int rc, i;
83 
84 	if (hvsi_udbg_buf_len == 0) {
85 		rc = plpar_get_term_char(vtermno, &hvsi_udbg_buf_len, hvsi_udbg_buf);
86 		if (rc != H_SUCCESS || hvsi_udbg_buf[0] != 0xff) {
87 			/* bad read or non-data packet */
88 			hvsi_udbg_buf_len = 0;
89 		} else {
90 			/* remove the packet header */
91 			for (i = 4; i < hvsi_udbg_buf_len; i++)
92 				hvsi_udbg_buf[i-4] = hvsi_udbg_buf[i];
93 			hvsi_udbg_buf_len -= 4;
94 		}
95 	}
96 
97 	if (hvsi_udbg_buf_len <= 0 || hvsi_udbg_buf_len > 256) {
98 		/* no data ready */
99 		hvsi_udbg_buf_len = 0;
100 		return -1;
101 	}
102 
103 	ch = hvsi_udbg_buf[0];
104 	/* shift remaining data down */
105 	for (i = 1; i < hvsi_udbg_buf_len; i++) {
106 		hvsi_udbg_buf[i-1] = hvsi_udbg_buf[i];
107 	}
108 	hvsi_udbg_buf_len--;
109 
110 	return ch;
111 }
112 
113 static int udbg_hvsi_getc(void)
114 {
115 	int ch;
116 	for (;;) {
117 		ch = udbg_hvsi_getc_poll();
118 		if (ch == -1) {
119 			/* This shouldn't be needed...but... */
120 			volatile unsigned long delay;
121 			for (delay=0; delay < 2000000; delay++)
122 				;
123 		} else {
124 			return ch;
125 		}
126 	}
127 }
128 
129 static void udbg_putcLP(char c)
130 {
131 	char buf[16];
132 	unsigned long rc;
133 
134 	if (c == '\n')
135 		udbg_putcLP('\r');
136 
137 	buf[0] = c;
138 	do {
139 		rc = plpar_put_term_char(vtermno, 1, buf);
140 	} while(rc == H_BUSY);
141 }
142 
143 /* Buffered chars getc */
144 static long inbuflen;
145 static long inbuf[2];	/* must be 2 longs */
146 
147 static int udbg_getc_pollLP(void)
148 {
149 	/* The interface is tricky because it may return up to 16 chars.
150 	 * We save them statically for future calls to udbg_getc().
151 	 */
152 	char ch, *buf = (char *)inbuf;
153 	int i;
154 	long rc;
155 	if (inbuflen == 0) {
156 		/* get some more chars. */
157 		inbuflen = 0;
158 		rc = plpar_get_term_char(vtermno, &inbuflen, buf);
159 		if (rc != H_SUCCESS)
160 			inbuflen = 0;	/* otherwise inbuflen is garbage */
161 	}
162 	if (inbuflen <= 0 || inbuflen > 16) {
163 		/* Catch error case as well as other oddities (corruption) */
164 		inbuflen = 0;
165 		return -1;
166 	}
167 	ch = buf[0];
168 	for (i = 1; i < inbuflen; i++)	/* shuffle them down. */
169 		buf[i-1] = buf[i];
170 	inbuflen--;
171 	return ch;
172 }
173 
174 static int udbg_getcLP(void)
175 {
176 	int ch;
177 	for (;;) {
178 		ch = udbg_getc_pollLP();
179 		if (ch == -1) {
180 			/* This shouldn't be needed...but... */
181 			volatile unsigned long delay;
182 			for (delay=0; delay < 2000000; delay++)
183 				;
184 		} else {
185 			return ch;
186 		}
187 	}
188 }
189 
190 /* call this from early_init() for a working debug console on
191  * vterm capable LPAR machines
192  */
193 void __init udbg_init_debug_lpar(void)
194 {
195 	vtermno = 0;
196 	udbg_putc = udbg_putcLP;
197 	udbg_getc = udbg_getcLP;
198 	udbg_getc_poll = udbg_getc_pollLP;
199 }
200 
201 /* returns 0 if couldn't find or use /chosen/stdout as console */
202 void __init find_udbg_vterm(void)
203 {
204 	struct device_node *stdout_node;
205 	const u32 *termno;
206 	const char *name;
207 	int add_console;
208 
209 	/* find the boot console from /chosen/stdout */
210 	if (!of_chosen)
211 		return;
212 	name = of_get_property(of_chosen, "linux,stdout-path", NULL);
213 	if (name == NULL)
214 		return;
215 	stdout_node = of_find_node_by_path(name);
216 	if (!stdout_node)
217 		return;
218 	name = of_get_property(stdout_node, "name", NULL);
219 	if (!name) {
220 		printk(KERN_WARNING "stdout node missing 'name' property!\n");
221 		goto out;
222 	}
223 	/* The user has requested a console so this is already set up. */
224 	add_console = !strstr(cmd_line, "console=");
225 
226 	/* Check if it's a virtual terminal */
227 	if (strncmp(name, "vty", 3) != 0)
228 		goto out;
229 	termno = of_get_property(stdout_node, "reg", NULL);
230 	if (termno == NULL)
231 		goto out;
232 	vtermno = termno[0];
233 
234 	if (of_device_is_compatible(stdout_node, "hvterm1")) {
235 		udbg_putc = udbg_putcLP;
236 		udbg_getc = udbg_getcLP;
237 		udbg_getc_poll = udbg_getc_pollLP;
238 		if (add_console)
239 			add_preferred_console("hvc", termno[0] & 0xff, NULL);
240 	} else if (of_device_is_compatible(stdout_node, "hvterm-protocol")) {
241 		vtermno = termno[0];
242 		udbg_putc = udbg_hvsi_putc;
243 		udbg_getc = udbg_hvsi_getc;
244 		udbg_getc_poll = udbg_hvsi_getc_poll;
245 		if (add_console)
246 			add_preferred_console("hvsi", termno[0] & 0xff, NULL);
247 	}
248 out:
249 	of_node_put(stdout_node);
250 }
251 
252 void vpa_init(int cpu)
253 {
254 	int hwcpu = get_hard_smp_processor_id(cpu);
255 	unsigned long addr;
256 	long ret;
257 
258 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
259 		lppaca[cpu].vmxregs_in_use = 1;
260 
261 	addr = __pa(&lppaca[cpu]);
262 	ret = register_vpa(hwcpu, addr);
263 
264 	if (ret) {
265 		printk(KERN_ERR "WARNING: vpa_init: VPA registration for "
266 				"cpu %d (hw %d) of area %lx returns %ld\n",
267 				cpu, hwcpu, addr, ret);
268 		return;
269 	}
270 	/*
271 	 * PAPR says this feature is SLB-Buffer but firmware never
272 	 * reports that.  All SPLPAR support SLB shadow buffer.
273 	 */
274 	addr = __pa(&slb_shadow[cpu]);
275 	if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
276 		ret = register_slb_shadow(hwcpu, addr);
277 		if (ret)
278 			printk(KERN_ERR
279 			       "WARNING: vpa_init: SLB shadow buffer "
280 			       "registration for cpu %d (hw %d) of area %lx "
281 			       "returns %ld\n", cpu, hwcpu, addr, ret);
282 	}
283 }
284 
285 static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
286  			      unsigned long va, unsigned long pa,
287  			      unsigned long rflags, unsigned long vflags,
288  			      int psize)
289 {
290 	unsigned long lpar_rc;
291 	unsigned long flags;
292 	unsigned long slot;
293 	unsigned long hpte_v, hpte_r;
294 
295 	if (!(vflags & HPTE_V_BOLTED))
296 		DBG_LOW("hpte_insert(group=%lx, va=%016lx, pa=%016lx, "
297 			"rflags=%lx, vflags=%lx, psize=%d)\n",
298 		hpte_group, va, pa, rflags, vflags, psize);
299 
300  	hpte_v = hpte_encode_v(va, psize) | vflags | HPTE_V_VALID;
301 	hpte_r = hpte_encode_r(pa, psize) | rflags;
302 
303 	if (!(vflags & HPTE_V_BOLTED))
304 		DBG_LOW(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
305 
306 	/* Now fill in the actual HPTE */
307 	/* Set CEC cookie to 0         */
308 	/* Zero page = 0               */
309 	/* I-cache Invalidate = 0      */
310 	/* I-cache synchronize = 0     */
311 	/* Exact = 0                   */
312 	flags = 0;
313 
314 	/* Make pHyp happy */
315 	if (rflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
316 		hpte_r &= ~_PAGE_COHERENT;
317 
318 	lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
319 	if (unlikely(lpar_rc == H_PTEG_FULL)) {
320 		if (!(vflags & HPTE_V_BOLTED))
321 			DBG_LOW(" full\n");
322 		return -1;
323 	}
324 
325 	/*
326 	 * Since we try and ioremap PHBs we don't own, the pte insert
327 	 * will fail. However we must catch the failure in hash_page
328 	 * or we will loop forever, so return -2 in this case.
329 	 */
330 	if (unlikely(lpar_rc != H_SUCCESS)) {
331 		if (!(vflags & HPTE_V_BOLTED))
332 			DBG_LOW(" lpar err %d\n", lpar_rc);
333 		return -2;
334 	}
335 	if (!(vflags & HPTE_V_BOLTED))
336 		DBG_LOW(" -> slot: %d\n", slot & 7);
337 
338 	/* Because of iSeries, we have to pass down the secondary
339 	 * bucket bit here as well
340 	 */
341 	return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
342 }
343 
344 static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
345 
346 static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
347 {
348 	unsigned long slot_offset;
349 	unsigned long lpar_rc;
350 	int i;
351 	unsigned long dummy1, dummy2;
352 
353 	/* pick a random slot to start at */
354 	slot_offset = mftb() & 0x7;
355 
356 	for (i = 0; i < HPTES_PER_GROUP; i++) {
357 
358 		/* don't remove a bolted entry */
359 		lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
360 					   (0x1UL << 4), &dummy1, &dummy2);
361 		if (lpar_rc == H_SUCCESS)
362 			return i;
363 		BUG_ON(lpar_rc != H_NOT_FOUND);
364 
365 		slot_offset++;
366 		slot_offset &= 0x7;
367 	}
368 
369 	return -1;
370 }
371 
372 static void pSeries_lpar_hptab_clear(void)
373 {
374 	unsigned long size_bytes = 1UL << ppc64_pft_size;
375 	unsigned long hpte_count = size_bytes >> 4;
376 	unsigned long dummy1, dummy2;
377 	int i;
378 
379 	/* TODO: Use bulk call */
380 	for (i = 0; i < hpte_count; i++)
381 		plpar_pte_remove_raw(0, i, 0, &dummy1, &dummy2);
382 }
383 
384 /*
385  * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
386  * the low 3 bits of flags happen to line up.  So no transform is needed.
387  * We can probably optimize here and assume the high bits of newpp are
388  * already zero.  For now I am paranoid.
389  */
390 static long pSeries_lpar_hpte_updatepp(unsigned long slot,
391 				       unsigned long newpp,
392 				       unsigned long va,
393 				       int psize, int local)
394 {
395 	unsigned long lpar_rc;
396 	unsigned long flags = (newpp & 7) | H_AVPN;
397 	unsigned long want_v;
398 
399 	want_v = hpte_encode_v(va, psize);
400 
401 	DBG_LOW("    update: avpnv=%016lx, hash=%016lx, f=%x, psize: %d ... ",
402 		want_v & HPTE_V_AVPN, slot, flags, psize);
403 
404 	lpar_rc = plpar_pte_protect(flags, slot, want_v & HPTE_V_AVPN);
405 
406 	if (lpar_rc == H_NOT_FOUND) {
407 		DBG_LOW("not found !\n");
408 		return -1;
409 	}
410 
411 	DBG_LOW("ok\n");
412 
413 	BUG_ON(lpar_rc != H_SUCCESS);
414 
415 	return 0;
416 }
417 
418 static unsigned long pSeries_lpar_hpte_getword0(unsigned long slot)
419 {
420 	unsigned long dword0;
421 	unsigned long lpar_rc;
422 	unsigned long dummy_word1;
423 	unsigned long flags;
424 
425 	/* Read 1 pte at a time                        */
426 	/* Do not need RPN to logical page translation */
427 	/* No cross CEC PFT access                     */
428 	flags = 0;
429 
430 	lpar_rc = plpar_pte_read(flags, slot, &dword0, &dummy_word1);
431 
432 	BUG_ON(lpar_rc != H_SUCCESS);
433 
434 	return dword0;
435 }
436 
437 static long pSeries_lpar_hpte_find(unsigned long va, int psize)
438 {
439 	unsigned long hash;
440 	unsigned long i, j;
441 	long slot;
442 	unsigned long want_v, hpte_v;
443 
444 	hash = hpt_hash(va, mmu_psize_defs[psize].shift);
445 	want_v = hpte_encode_v(va, psize);
446 
447 	for (j = 0; j < 2; j++) {
448 		slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
449 		for (i = 0; i < HPTES_PER_GROUP; i++) {
450 			hpte_v = pSeries_lpar_hpte_getword0(slot);
451 
452 			if (HPTE_V_COMPARE(hpte_v, want_v)
453 			    && (hpte_v & HPTE_V_VALID)
454 			    && (!!(hpte_v & HPTE_V_SECONDARY) == j)) {
455 				/* HPTE matches */
456 				if (j)
457 					slot = -slot;
458 				return slot;
459 			}
460 			++slot;
461 		}
462 		hash = ~hash;
463 	}
464 
465 	return -1;
466 }
467 
468 static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
469 					     unsigned long ea,
470 					     int psize)
471 {
472 	unsigned long lpar_rc, slot, vsid, va, flags;
473 
474 	vsid = get_kernel_vsid(ea);
475 	va = (vsid << 28) | (ea & 0x0fffffff);
476 
477 	slot = pSeries_lpar_hpte_find(va, psize);
478 	BUG_ON(slot == -1);
479 
480 	flags = newpp & 7;
481 	lpar_rc = plpar_pte_protect(flags, slot, 0);
482 
483 	BUG_ON(lpar_rc != H_SUCCESS);
484 }
485 
486 static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
487 					 int psize, int local)
488 {
489 	unsigned long want_v;
490 	unsigned long lpar_rc;
491 	unsigned long dummy1, dummy2;
492 
493 	DBG_LOW("    inval : slot=%lx, va=%016lx, psize: %d, local: %d",
494 		slot, va, psize, local);
495 
496 	want_v = hpte_encode_v(va, psize);
497 	lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v & HPTE_V_AVPN,
498 				   &dummy1, &dummy2);
499 	if (lpar_rc == H_NOT_FOUND)
500 		return;
501 
502 	BUG_ON(lpar_rc != H_SUCCESS);
503 }
504 
505 /* Flag bits for H_BULK_REMOVE */
506 #define HBR_REQUEST	0x4000000000000000UL
507 #define HBR_RESPONSE	0x8000000000000000UL
508 #define HBR_END		0xc000000000000000UL
509 #define HBR_AVPN	0x0200000000000000UL
510 #define HBR_ANDCOND	0x0100000000000000UL
511 
512 /*
513  * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
514  * lock.
515  */
516 static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
517 {
518 	unsigned long i, pix, rc;
519 	unsigned long flags = 0;
520 	struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
521 	int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
522 	unsigned long param[9];
523 	unsigned long va;
524 	unsigned long hash, index, shift, hidx, slot;
525 	real_pte_t pte;
526 	int psize;
527 
528 	if (lock_tlbie)
529 		spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
530 
531 	psize = batch->psize;
532 	pix = 0;
533 	for (i = 0; i < number; i++) {
534 		va = batch->vaddr[i];
535 		pte = batch->pte[i];
536 		pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
537 			hash = hpt_hash(va, shift);
538 			hidx = __rpte_to_hidx(pte, index);
539 			if (hidx & _PTEIDX_SECONDARY)
540 				hash = ~hash;
541 			slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
542 			slot += hidx & _PTEIDX_GROUP_IX;
543 			if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
544 				pSeries_lpar_hpte_invalidate(slot, va, psize,
545 							     local);
546 			} else {
547 				param[pix] = HBR_REQUEST | HBR_AVPN | slot;
548 				param[pix+1] = hpte_encode_v(va, psize) &
549 					HPTE_V_AVPN;
550 				pix += 2;
551 				if (pix == 8) {
552 					rc = plpar_hcall9(H_BULK_REMOVE, param,
553 						param[0], param[1], param[2],
554 						param[3], param[4], param[5],
555 						param[6], param[7]);
556 					BUG_ON(rc != H_SUCCESS);
557 					pix = 0;
558 				}
559 			}
560 		} pte_iterate_hashed_end();
561 	}
562 	if (pix) {
563 		param[pix] = HBR_END;
564 		rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
565 				  param[2], param[3], param[4], param[5],
566 				  param[6], param[7]);
567 		BUG_ON(rc != H_SUCCESS);
568 	}
569 
570 	if (lock_tlbie)
571 		spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
572 }
573 
574 void __init hpte_init_lpar(void)
575 {
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;
582 	ppc_md.hpte_clear_all   = pSeries_lpar_hptab_clear;
583 }
584