17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fea9cb91Slq  * Common Development and Distribution License (the "License").
6fea9cb91Slq  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fea9cb91Slq 
227c478bd9Sstevel@tonic-gate /*
23*e20e22c9STrevor Thompson  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/promif.h>
277c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  *  Returns 0 on error. Otherwise returns a handle.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate int
prom_open(char * path)337c478bd9Sstevel@tonic-gate prom_open(char *path)
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate 	cell_t ci[5];
367c478bd9Sstevel@tonic-gate 	promif_owrap_t *ow;
377c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
387c478bd9Sstevel@tonic-gate 	char *opath = NULL;
397c478bd9Sstevel@tonic-gate 	size_t len;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	if ((uintptr_t)path > (uint32_t)-1) {
427c478bd9Sstevel@tonic-gate 		opath = path;
437c478bd9Sstevel@tonic-gate 		len = prom_strlen(opath) + 1; /* include terminating NUL */
447c478bd9Sstevel@tonic-gate 		path = promplat_alloc(len);
457c478bd9Sstevel@tonic-gate 		if (path == NULL)
467c478bd9Sstevel@tonic-gate 			return (0);
477c478bd9Sstevel@tonic-gate 		(void) prom_strcpy(path, opath);
487c478bd9Sstevel@tonic-gate 	}
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	ow = promif_preout();
527c478bd9Sstevel@tonic-gate 	promif_preprom();
537c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("open");		/* Service name */
547c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)1;			/* #argument cells */
557c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
567c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell(path);		/* Arg1: Pathname */
577c478bd9Sstevel@tonic-gate 	ci[4] = (cell_t)0;			/* Res1: Prime result */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	promif_postprom();
627c478bd9Sstevel@tonic-gate 	promif_postout(ow);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
657c478bd9Sstevel@tonic-gate 	if (opath != NULL)
667c478bd9Sstevel@tonic-gate 		promplat_free(path, len);
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	return (p1275_cell2int(ci[4]));		/* Res1: ihandle */
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate int
prom_seek(int fd,unsigned long long offset)747c478bd9Sstevel@tonic-gate prom_seek(int fd, unsigned long long offset)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	cell_t ci[7];
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("seek");		/* Service name */
797c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)3;			/* #argument cells */
807c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
817c478bd9Sstevel@tonic-gate 	ci[3] = p1275_uint2cell((uint_t)fd);	/* Arg1: ihandle */
827c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ull2cell_high(offset);	/* Arg2: pos.hi */
837c478bd9Sstevel@tonic-gate 	ci[5] = p1275_ull2cell_low(offset);	/* Arg3: pos.lo */
847c478bd9Sstevel@tonic-gate 	ci[6] = (cell_t)-1;			/* Res1: Prime result */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	promif_preprom();
877c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
887c478bd9Sstevel@tonic-gate 	promif_postprom();
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (p1275_cell2int(ci[6]));		/* Res1: actual */
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
947c478bd9Sstevel@tonic-gate ssize_t
prom_read(ihandle_t fd,caddr_t buf,size_t len,uint_t startblk,char devtype)957c478bd9Sstevel@tonic-gate prom_read(ihandle_t fd, caddr_t buf, size_t len, uint_t startblk, char devtype)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	cell_t ci[7];
987c478bd9Sstevel@tonic-gate 	promif_owrap_t *ow;
997c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
1007c478bd9Sstevel@tonic-gate 	caddr_t obuf = NULL;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if ((uintptr_t)buf > (uint32_t)-1) {
1037c478bd9Sstevel@tonic-gate 		obuf = buf;
1047c478bd9Sstevel@tonic-gate 		buf = promplat_alloc(len);
1057c478bd9Sstevel@tonic-gate 		if (buf == NULL)
1067c478bd9Sstevel@tonic-gate 			return (-1);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate #endif
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	ow = promif_preout();
1117c478bd9Sstevel@tonic-gate 	promif_preprom();
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("read");		/* Service name */
1147c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)3;			/* #argument cells */
1157c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
1167c478bd9Sstevel@tonic-gate 	ci[3] = p1275_size2cell((uint_t)fd);	/* Arg1: ihandle */
1177c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ptr2cell(buf);		/* Arg2: buffer address */
1187c478bd9Sstevel@tonic-gate 	ci[5] = p1275_uint2cell(len);		/* Arg3: buffer length */
1197c478bd9Sstevel@tonic-gate 	ci[6] = (cell_t)-1;			/* Res1: Prime result */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	promif_postprom();
1247c478bd9Sstevel@tonic-gate 	promif_postout(ow);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
1277c478bd9Sstevel@tonic-gate 	if (obuf != NULL) {
1287c478bd9Sstevel@tonic-gate 		promplat_bcopy(buf, obuf, len);
1297c478bd9Sstevel@tonic-gate 		promplat_free(buf, len);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (p1275_cell2size(ci[6]));	/* Res1: actual length */
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
136fea9cb91Slq /*
137fea9cb91Slq  * prom_write is the only prom_*() function we have to intercept
138fea9cb91Slq  * because all the other prom_*() io interfaces eventually call
139fea9cb91Slq  * into prom_write().
140fea9cb91Slq  */
1417c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
1427c478bd9Sstevel@tonic-gate ssize_t
prom_write(ihandle_t fd,caddr_t buf,size_t len,uint_t startblk,char devtype)1437c478bd9Sstevel@tonic-gate prom_write(ihandle_t fd, caddr_t buf, size_t len, uint_t startblk, char devtype)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	cell_t ci[7];
1467c478bd9Sstevel@tonic-gate 	promif_owrap_t *ow;
147fea9cb91Slq 	ssize_t rlen;
148fea9cb91Slq 
1497c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
1507c478bd9Sstevel@tonic-gate 	caddr_t obuf = NULL;
1517c478bd9Sstevel@tonic-gate 	static char smallbuf[256];
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	ASSERT(buf);
154*e20e22c9STrevor Thompson #endif
155*e20e22c9STrevor Thompson 
156*e20e22c9STrevor Thompson 	/*
157*e20e22c9STrevor Thompson 	 * If the callback address is set, attempt to redirect
158*e20e22c9STrevor Thompson 	 * console output back into kernel terminal emulator.
159*e20e22c9STrevor Thompson 	 */
160*e20e22c9STrevor Thompson 	if (promif_redirect != NULL && fd == prom_stdout_ihandle()) {
161*e20e22c9STrevor Thompson 		ow = promif_preout();
162*e20e22c9STrevor Thompson 		rlen = promif_redirect(promif_redirect_arg, (uchar_t *)buf,
163*e20e22c9STrevor Thompson 		    len);
164*e20e22c9STrevor Thompson 		promif_postout(ow);
165*e20e22c9STrevor Thompson 		return (rlen);
166*e20e22c9STrevor Thompson 	}
1677c478bd9Sstevel@tonic-gate 
168*e20e22c9STrevor Thompson #ifdef PROM_32BIT_ADDRS
1697c478bd9Sstevel@tonic-gate 	if ((uintptr_t)buf > (uint32_t)-1) {
1707c478bd9Sstevel@tonic-gate 		/*
1717c478bd9Sstevel@tonic-gate 		 * This is a hack for kernel message output.
1727c478bd9Sstevel@tonic-gate 		 * By avoiding calls to promplat_alloc (and
1737c478bd9Sstevel@tonic-gate 		 * using smallbuf instead) when memory is low
1747c478bd9Sstevel@tonic-gate 		 * we can print shortish kernel messages without
1757c478bd9Sstevel@tonic-gate 		 * deadlocking. smallbuf should be at least as
1767c478bd9Sstevel@tonic-gate 		 * large as the automatic buffer in
1777c478bd9Sstevel@tonic-gate 		 * prom_printf.c:_doprint()'s stack frame.
1787c478bd9Sstevel@tonic-gate 		 * promplat_alloc() can block on a mutex and so
1797c478bd9Sstevel@tonic-gate 		 * is called here before calling promif_preprom().
1807c478bd9Sstevel@tonic-gate 		 */
1817c478bd9Sstevel@tonic-gate 		if (len > sizeof (smallbuf)) {
1827c478bd9Sstevel@tonic-gate 			obuf = buf;
1837c478bd9Sstevel@tonic-gate 			buf = promplat_alloc(len);
1847c478bd9Sstevel@tonic-gate 			if (buf == NULL) {
1857c478bd9Sstevel@tonic-gate 				return (-1);
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 			promplat_bcopy(obuf, buf, len);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate #endif
1917c478bd9Sstevel@tonic-gate 
192*e20e22c9STrevor Thompson 	/*
193*e20e22c9STrevor Thompson 	 * Normally we'd call promif_preprom() just before
194*e20e22c9STrevor Thompson 	 * calling into the prom (to enforce single-threaded
195*e20e22c9STrevor Thompson 	 * access) but here we need to call it before accessing
196*e20e22c9STrevor Thompson 	 * smallbuf, since smallbuf is statically allocated and
197*e20e22c9STrevor Thompson 	 * hence can only be accessed by one thread at a time.
198*e20e22c9STrevor Thompson 	 */
1997c478bd9Sstevel@tonic-gate 	ow = promif_preout();
2007c478bd9Sstevel@tonic-gate 	promif_preprom();
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
2037c478bd9Sstevel@tonic-gate 	if ((uintptr_t)buf > (uint32_t)-1) {
2047c478bd9Sstevel@tonic-gate 		/*
2057c478bd9Sstevel@tonic-gate 		 * If buf is small enough, use smallbuf
2067c478bd9Sstevel@tonic-gate 		 * instead of promplat_alloc() (see above)
2077c478bd9Sstevel@tonic-gate 		 * smallbuf is static, so single thread
2087c478bd9Sstevel@tonic-gate 		 * access to it by using it only after
2097c478bd9Sstevel@tonic-gate 		 * promif_preprom()
2107c478bd9Sstevel@tonic-gate 		 */
2117c478bd9Sstevel@tonic-gate 		if (len <= sizeof (smallbuf)) {
2127c478bd9Sstevel@tonic-gate 			promplat_bcopy(buf, smallbuf, len);
2137c478bd9Sstevel@tonic-gate 			buf = smallbuf;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate #endif
217*e20e22c9STrevor Thompson 
218*e20e22c9STrevor Thompson 	ci[0] = p1275_ptr2cell("write");	/* Service name */
219*e20e22c9STrevor Thompson 	ci[1] = (cell_t)3;			/* #argument cells */
220*e20e22c9STrevor Thompson 	ci[2] = (cell_t)1;			/* #result cells */
221*e20e22c9STrevor Thompson 	ci[3] = p1275_uint2cell((uint_t)fd);	/* Arg1: ihandle */
222*e20e22c9STrevor Thompson 	ci[4] = p1275_ptr2cell(buf);		/* Arg2: buffer addr */
223*e20e22c9STrevor Thompson 	ci[5] = p1275_size2cell(len);		/* Arg3: buffer len */
224*e20e22c9STrevor Thompson 	ci[6] = (cell_t)-1;			/* Res1: Prime result */
225*e20e22c9STrevor Thompson 
226*e20e22c9STrevor Thompson 	(void) p1275_cif_handler(&ci);
227*e20e22c9STrevor Thompson 	rlen = p1275_cell2size(ci[6]);		/* Res1: actual len */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	promif_postprom();
2307c478bd9Sstevel@tonic-gate 	promif_postout(ow);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
2337c478bd9Sstevel@tonic-gate 	if (obuf != NULL)
2347c478bd9Sstevel@tonic-gate 		promplat_free(buf, len);
2357c478bd9Sstevel@tonic-gate #endif
2367c478bd9Sstevel@tonic-gate 
237fea9cb91Slq 	return (rlen);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate int
prom_close(int fd)2417c478bd9Sstevel@tonic-gate prom_close(int fd)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	cell_t ci[4];
2447c478bd9Sstevel@tonic-gate 	promif_owrap_t *ow;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("close");	/* Service name */
2477c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)1;			/* #argument cells */
2487c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)0;			/* #result cells */
2497c478bd9Sstevel@tonic-gate 	ci[3] = p1275_uint2cell((uint_t)fd);	/* Arg1: ihandle */
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	ow = promif_preout();
2527c478bd9Sstevel@tonic-gate 	promif_preprom();
2537c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
2547c478bd9Sstevel@tonic-gate 	promif_postprom();
2557c478bd9Sstevel@tonic-gate 	promif_postout(ow);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return (0);
2587c478bd9Sstevel@tonic-gate }
259