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 #include <linux/config.h>
23 #include <linux/kernel.h>
24 #include <linux/fs.h>
25 #include <asm/processor.h>
26 #include <asm/semaphore.h>
27 #include <asm/mmu.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
30 #include <asm/machdep.h>
31 #include <asm/abs_addr.h>
32 #include <asm/mmu_context.h>
33 #include <asm/ppcdebug.h>
34 #include <asm/pci_dma.h>
35 #include <linux/pci.h>
36 #include <asm/naca.h>
37 #include <asm/hvcall.h>
poll_pending(void)38 long poll_pending(void)
39 {
40 	unsigned long dummy;
41 	return plpar_hcall(H_POLL_PENDING, 0, 0, 0, 0,
42 			   &dummy, &dummy, &dummy);
43 }
44 
prod_processor(void)45 long prod_processor(void)
46 {
47 	plpar_hcall_norets(H_PROD);
48 	return(0);
49 }
50 
cede_processor(void)51 long cede_processor(void)
52 {
53 	plpar_hcall_norets(H_CEDE);
54 	return(0);
55 }
56 
register_vpa(unsigned long flags,unsigned long proc,unsigned long vpa)57 long register_vpa(unsigned long flags, unsigned long proc, unsigned long vpa)
58 {
59 	plpar_hcall_norets(H_REGISTER_VPA, flags, proc, vpa);
60 	return(0);
61 }
62 
vpa_init(int cpu)63 void vpa_init(int cpu)
64 {
65 	unsigned long flags;
66 
67 	/* Register the Virtual Processor Area (VPA) */
68 	printk(KERN_INFO "register_vpa: cpu 0x%x\n", cpu);
69 	flags = 1UL << (63 - 18);
70 	paca[cpu].xLpPaca.xSLBCount = 64; /* SLB restore highwater mark */
71 	register_vpa(flags, cpu, __pa((unsigned long)&(paca[cpu].xLpPaca)));
72 }
73 
plpar_tce_get(unsigned long liobn,unsigned long ioba,unsigned long * tce_ret)74 long plpar_tce_get(unsigned long liobn,
75 		   unsigned long ioba,
76 		   unsigned long *tce_ret)
77 {
78 	unsigned long dummy;
79 	return plpar_hcall(H_GET_TCE, liobn, ioba, 0, 0,
80 			   tce_ret, &dummy, &dummy);
81 }
82 
83 
plpar_tce_put(unsigned long liobn,unsigned long ioba,unsigned long tceval)84 long plpar_tce_put(unsigned long liobn,
85 		   unsigned long ioba,
86 		   unsigned long tceval)
87 {
88 	return plpar_hcall_norets(H_PUT_TCE, liobn, ioba, tceval);
89 }
90 
plpar_get_term_char(unsigned long termno,unsigned long * len_ret,char * buf_ret)91 long plpar_get_term_char(unsigned long termno,
92 			 unsigned long *len_ret,
93 			 char *buf_ret)
94 {
95 	unsigned long *lbuf = (unsigned long *)buf_ret;  /* ToDo: alignment? */
96 	return plpar_hcall(H_GET_TERM_CHAR, termno, 0, 0, 0,
97 			   len_ret, lbuf+0, lbuf+1);
98 }
99 
plpar_put_term_char(unsigned long termno,unsigned long len,const char * buffer)100 long plpar_put_term_char(unsigned long termno,
101 			 unsigned long len,
102 			 const char *buffer)
103 {
104 	unsigned long dummy;
105 	unsigned long *lbuf = (unsigned long *)buffer;  /* ToDo: alignment? */
106 	return plpar_hcall(H_PUT_TERM_CHAR, termno, len,
107 			   lbuf[0], lbuf[1], &dummy, &dummy, &dummy);
108 }
109 
plpar_eoi(unsigned long xirr)110 long plpar_eoi(unsigned long xirr)
111 {
112 	return plpar_hcall_norets(H_EOI, xirr);
113 }
114 
plpar_cppr(unsigned long cppr)115 long plpar_cppr(unsigned long cppr)
116 {
117 	return plpar_hcall_norets(H_CPPR, cppr);
118 }
119 
plpar_ipi(unsigned long servernum,unsigned long mfrr)120 long plpar_ipi(unsigned long servernum,
121 	       unsigned long mfrr)
122 {
123 	return plpar_hcall_norets(H_IPI, servernum, mfrr);
124 }
125 
plpar_xirr(unsigned long * xirr_ret)126 long plpar_xirr(unsigned long *xirr_ret)
127 {
128 	unsigned long dummy;
129 	return plpar_hcall(H_XIRR, 0, 0, 0, 0,
130 			   xirr_ret, &dummy, &dummy);
131 }
132 
plpar_ipoll(unsigned long servernum,unsigned long * xirr_ret,unsigned long * mfrr_ret)133 long plpar_ipoll(unsigned long servernum, unsigned long* xirr_ret, unsigned long* mfrr_ret)
134 {
135 	unsigned long dummy;
136 	return plpar_hcall(H_IPOLL, servernum, 0, 0, 0,
137 			   xirr_ret, mfrr_ret, &dummy);
138 }
139 
140 
tce_build_pSeriesLP(struct TceTable * tbl,long tcenum,unsigned long uaddr,int direction)141 static void tce_build_pSeriesLP(struct TceTable *tbl, long tcenum,
142 				unsigned long uaddr, int direction )
143 {
144 	u64 set_tce_rc;
145 	union Tce tce;
146 
147 	PPCDBG(PPCDBG_TCE, "build_tce: uaddr = 0x%lx\n", uaddr);
148 	PPCDBG(PPCDBG_TCE, "\ttcenum = 0x%lx, tbl = 0x%lx, index=%lx\n",
149 	       tcenum, tbl, tbl->index);
150 
151 	tce.wholeTce = 0;
152 	tce.tceBits.rpn = (virt_to_absolute(uaddr)) >> PAGE_SHIFT;
153 
154 	tce.tceBits.readWrite = 1;
155 	if ( direction != PCI_DMA_TODEVICE ) tce.tceBits.pciWrite = 1;
156 
157 	set_tce_rc = plpar_tce_put((u64)tbl->index,
158 				 (u64)tcenum << 12,
159 				 tce.wholeTce );
160 
161 	if(set_tce_rc) {
162 		printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", set_tce_rc);
163 		printk("\tindex   = 0x%lx\n", (u64)tbl->index);
164 		printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
165 		printk("\ttce val = 0x%lx\n", tce.wholeTce );
166 	}
167 }
168 
tce_free_one_pSeriesLP(struct TceTable * tbl,long tcenum)169 static void tce_free_one_pSeriesLP(struct TceTable *tbl, long tcenum)
170 {
171 	u64 set_tce_rc;
172 	union Tce tce;
173 
174 	tce.wholeTce = 0;
175 	set_tce_rc = plpar_tce_put((u64)tbl->index,
176 				 (u64)tcenum << 12,
177 				 tce.wholeTce );
178 	if ( set_tce_rc ) {
179 		printk("tce_free_one_pSeriesLP: plpar_tce_put failed\n");
180 		printk("\trc      = %ld\n", set_tce_rc);
181 		printk("\tindex   = 0x%lx\n", (u64)tbl->index);
182 		printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
183 		printk("\ttce val = 0x%lx\n", tce.wholeTce );
184 	}
185 
186 }
187 
188 /* PowerPC Interrupts for lpar. */
189 /* NOTE: this typedef is duplicated (for now) from xics.c! */
190 typedef struct {
191 	int (*xirr_info_get)(int cpu);
192 	void (*xirr_info_set)(int cpu, int val);
193 	void (*cppr_info)(int cpu, u8 val);
194 	void (*qirr_info)(int cpu, u8 val);
195 } xics_ops;
pSeriesLP_xirr_info_get(int n_cpu)196 static int pSeriesLP_xirr_info_get(int n_cpu)
197 {
198 	unsigned long lpar_rc;
199 	unsigned long return_value;
200 
201 	lpar_rc = plpar_xirr(&return_value);
202 	if (lpar_rc != H_Success) {
203 		panic(" bad return code xirr - rc = %lx \n", lpar_rc);
204 	}
205 	return ((int)(return_value));
206 }
207 
pSeriesLP_xirr_info_set(int n_cpu,int value)208 static void pSeriesLP_xirr_info_set(int n_cpu, int value)
209 {
210 	unsigned long lpar_rc;
211 	unsigned long val64 = value & 0xffffffff;
212 
213 	lpar_rc = plpar_eoi(val64);
214 	if (lpar_rc != H_Success) {
215 		panic(" bad return code EOI - rc = %ld, value=%lx \n", lpar_rc, val64);
216 	}
217 }
218 
pSeriesLP_cppr_info(int n_cpu,u8 value)219 static void pSeriesLP_cppr_info(int n_cpu, u8 value)
220 {
221 	unsigned long lpar_rc;
222 
223 	lpar_rc = plpar_cppr(value);
224 	if (lpar_rc != H_Success) {
225 		panic(" bad return code cppr - rc = %lx \n", lpar_rc);
226 	}
227 }
228 
pSeriesLP_qirr_info(int n_cpu,u8 value)229 static void pSeriesLP_qirr_info(int n_cpu , u8 value)
230 {
231 	unsigned long lpar_rc;
232 
233 	lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu),value);
234 	if (lpar_rc != H_Success) {
235     udbg_printf("pSeriesLP_qirr_info - plpar_ipi failed!!!!!!!! \n");
236 		panic(" bad return code qirr -ipi  - rc = %lx \n", lpar_rc);
237 	}
238 }
239 
240 xics_ops pSeriesLP_ops = {
241 	pSeriesLP_xirr_info_get,
242 	pSeriesLP_xirr_info_set,
243 	pSeriesLP_cppr_info,
244 	pSeriesLP_qirr_info
245 };
246 /* end TAI-LPAR */
247 
248 
249 int vtermno;	/* virtual terminal# for udbg  */
250 
udbg_putcLP(unsigned char c)251 static void udbg_putcLP(unsigned char c)
252 {
253 	char buf[16];
254 	unsigned long rc;
255 
256 	if (c == '\n')
257 		udbg_putcLP('\r');
258 
259 	buf[0] = c;
260 	do {
261 		rc = plpar_put_term_char(vtermno, 1, buf);
262 	} while(rc == H_Busy);
263 }
264 
265 /* Buffered chars getc */
266 static long inbuflen;
267 static long inbuf[2];	/* must be 2 longs */
268 
udbg_getc_pollLP(void)269 static int udbg_getc_pollLP(void)
270 {
271 	/* The interface is tricky because it may return up to 16 chars.
272 	 * We save them statically for future calls to udbg_getc().
273 	 */
274 	char ch, *buf = (char *)inbuf;
275 	int i;
276 	long rc;
277 	if (inbuflen == 0) {
278 		/* get some more chars. */
279 		inbuflen = 0;
280 		rc = plpar_get_term_char(vtermno, &inbuflen, buf);
281 		if (rc != H_Success)
282 			inbuflen = 0;	/* otherwise inbuflen is garbage */
283 	}
284 	if (inbuflen <= 0 || inbuflen > 16) {
285 		/* Catch error case as well as other oddities (corruption) */
286 		inbuflen = 0;
287 		return -1;
288 	}
289 	ch = buf[0];
290 	for (i = 1; i < inbuflen; i++)	/* shuffle them down. */
291 		buf[i-1] = buf[i];
292 	inbuflen--;
293 	return ch;
294 }
295 
udbg_getcLP(void)296 static unsigned char udbg_getcLP(void)
297 {
298 	int ch;
299 	for (;;) {
300 		ch = udbg_getc_pollLP();
301 		if (ch == -1) {
302 			/* This shouldn't be needed...but... */
303 			volatile unsigned long delay;
304 			for (delay=0; delay < 2000000; delay++)
305 				;
306 		} else {
307 			return ch;
308 		}
309 	}
310 }
311 
312 
313 
314 
hvc_get_chars(int index,char * buf,int count)315 int hvc_get_chars(int index, char *buf, int count)
316 {
317 	unsigned long got;
318 
319 	if (plpar_hcall(H_GET_TERM_CHAR, index, 0, 0, 0, &got,
320 		(unsigned long *)buf, (unsigned long *)buf+1) == H_Success) {
321 		/*
322 		 * Work around a HV bug where it gives us a null
323 		 * after every \r.  -- paulus
324 		 */
325 		if (got > 0) {
326 			int i;
327 			for (i = 1; i < got; ++i) {
328 				if (buf[i] == 0 && buf[i-1] == '\r') {
329 					--got;
330 					if (i < got)
331 						memmove(&buf[i], &buf[i+1],
332 							got - i);
333 				}
334 			}
335 		}
336 		return got;
337 	}
338 	return 0;
339 }
340 
hvc_put_chars(int index,const char * buf,int count)341 int hvc_put_chars(int index, const char *buf, int count)
342 {
343 	unsigned long dummy;
344 	unsigned long *lbuf = (unsigned long *) buf;
345 	long ret;
346 
347 	ret = plpar_hcall(H_PUT_TERM_CHAR, index, count, lbuf[0], lbuf[1],
348 			  &dummy, &dummy, &dummy);
349 	if (ret == H_Success)
350 		return count;
351 	if (ret == H_Busy)
352 		return 0;
353 	return -1;
354 }
355 
356 /* return the number of client vterms present */
357 /* XXX this requires an interface change to handle multiple discontiguous
358  * vterms */
hvc_count(int * start_termno)359 int hvc_count(int *start_termno)
360 {
361 	u32 *termno;
362 	struct device_node *rtas;
363 	struct device_node *vtys;
364 
365 	/* consider only the first vty node.
366 	 * we should _always_ be able to find one. however, it may not be compatible
367 	 * with hvterm1, in which case hvc_console can't use it. */
368 	vtys = find_devices("vty");
369 	if (vtys && device_is_compatible(vtys, "hvterm1")) {
370 		termno = (u32 *)get_property(vtys, "reg", 0);
371 		if (start_termno && termno)
372 			*start_termno = *termno;
373 		return 1; /* we can't support >1 with this interface */
374 	}
375 
376 	/* no vty nodes; use the /rtas/ibm,termno property */
377 	printk(KERN_ERR "%s: couldn't find a 'vty' node\n", __FUNCTION__);
378 	if ((rtas = find_path_device("/rtas")) != NULL) {
379 		if ((termno = (u32 *)get_property(rtas, "ibm,termno", 0)) != NULL) {
380 			if (start_termno)
381 				*start_termno = termno[0];
382 			return termno[1];
383 		}
384 	}
385 
386 	/* couldn't find any vterms */
387 	return 0;
388 }
389 
390 #ifndef CONFIG_PPC_ISERIES
391 void pSeries_lpar_mm_init(void);
392 
393 /* returns 0 if couldn't find or use /chosen/stdout as console */
find_udbg_vterm(void)394 static int find_udbg_vterm(void)
395 {
396 	struct device_node *stdout_node;
397 	u32 *termno;
398 	char *name;
399 	int found = 0;
400 
401 	/* find the boot console from /chosen/stdout */
402 	if (!of_stdout_device) {
403 		printk(KERN_WARNING "couldn't get path from /chosen/stdout!\n");
404 		return found;
405 	}
406 	stdout_node = find_path_device(of_stdout_device);
407 	if (!stdout_node) {
408 		printk(KERN_WARNING "couldn't find node from /chosen/stdout\n");
409 		return found;
410 	}
411 
412 	/* now we have the stdout node; figure out what type of device it is. */
413 	name = (char *)get_property(stdout_node, "name", 0);
414 	if (!name) {
415 		printk(KERN_WARNING "stdout node missing 'name' property!\n");
416 		return found;
417 	}
418 
419 	if (strncmp(name, "vty", 3) == 0) {
420 		char *compatible;
421 		compatible = (char *)get_property(stdout_node, "compatible", 0);
422 		if (compatible && (strncmp(compatible, "hvterm1", 7) == 0)) {
423 			termno = (u32 *)get_property(stdout_node, "reg", 0);
424 			if (termno) {
425 				vtermno = termno[0];
426 				ppc_md.udbg_putc = udbg_putcLP;
427 				ppc_md.udbg_getc = udbg_getcLP;
428 				ppc_md.udbg_getc_poll = udbg_getc_pollLP;
429 				found = 1;
430 			}
431 		} else {
432 			/* XXX implement udbg_putcLP_vtty for hvterm-protocol1 case */
433 			printk(KERN_WARNING "%s doesn't speak hvterm1; "
434 				"can't print udbg messages\n", of_stdout_device);
435 		}
436 	} else if (strncmp(name, "rtas", 4)) {
437 		/* according to firmware, this should never happen. to be removed */
438 		printk(KERN_ERR "ATTENTION: /chosen/stdout should be /vdevice/vty@0!\n"
439 			"Please report this to linuxppc64-dev@lists.linuxppc.org\n");
440 
441 		/* "ibm,termno" property is a pair of numbers. The first is the
442 		 * starting termno (the one we use) and the second is the number
443 		 * of terminals. */
444 		termno = (u32 *)get_property(stdout_node, "ibm,termno", 0);
445 		if (termno) {
446 			vtermno = termno[0];
447 			ppc_md.udbg_putc = udbg_putcLP;
448 			ppc_md.udbg_getc = udbg_getcLP;
449 			ppc_md.udbg_getc_poll = udbg_getc_pollLP;
450 			found = 1;
451 		}
452 	} else if (strncmp(name, "serial", 6)) {
453 		/* XXX fix ISA serial console */
454 		printk(KERN_WARNING "serial stdout on LPAR ('%s')! "
455 			"can't print udbg messages\n", of_stdout_device);
456 	} else {
457 		printk(KERN_WARNING "don't know how to print to stdout '%s'\n",
458 			of_stdout_device);
459 	}
460 
461 	return found;
462 }
463 
464 /* This is called early in setup.c.
465  * Use it to setup page table ppc_md stuff as well as udbg.
466  */
pSeriesLP_init_early(void)467 void pSeriesLP_init_early(void)
468 {
469 	pSeries_lpar_mm_init();
470 
471 	ppc_md.tce_build	 = tce_build_pSeriesLP;
472 	ppc_md.tce_free_one	 = tce_free_one_pSeriesLP;
473 
474 #ifdef CONFIG_SMP
475 	smp_init_pSeries();
476 #endif
477 	pSeries_pcibios_init_early();
478 
479 	/* The keyboard is not useful in the LPAR environment.
480 	 * Leave all ppc_md keyboard interfaces NULL.
481 	 */
482 
483 	if (0 == find_udbg_vterm()) {
484 		printk(KERN_WARNING
485 				"can't use stdout; can't print early debug messages.\n");
486 	}
487 }
488 #endif
489