Home | History | Annotate | Line # | Download | only in common
pdc.c revision 1.3
      1 /*	$NetBSD: pdc.c,v 1.3 2019/01/08 17:14:52 christos Exp $	*/
      2 
      3 /*	$OpenBSD: pdc.c,v 1.10 1999/05/06 02:27:44 mickey Exp $	*/
      4 
      5 /*
      6  * Copyright (c) 1998-2004 Michael Shalayeff
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     28  * THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 /*
     31  * Copyright 1996 1995 by Open Software Foundation, Inc.
     32  *              All Rights Reserved
     33  *
     34  * Permission to use, copy, modify, and distribute this software and
     35  * its documentation for any purpose and without fee is hereby granted,
     36  * provided that the above copyright notice appears in all copies and
     37  * that both the copyright notice and this permission notice appear in
     38  * supporting documentation.
     39  *
     40  * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
     41  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     42  * FOR A PARTICULAR PURPOSE.
     43  *
     44  * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
     45  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     46  * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
     47  * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
     48  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     49  *
     50  */
     51 /*
     52  * Copyright (c) 1990 mt Xinu, Inc.  All rights reserved.
     53  * Copyright (c) 1990 University of Utah.  All rights reserved.
     54  *
     55  * This file may be freely distributed in any form as long as
     56  * this copyright notice is included.
     57  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
     59  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     60  *
     61  *	Utah $Hdr: pdc.c 1.8 92/03/14$
     62  */
     63 
     64 #include <sys/param.h>
     65 #include <sys/time.h>
     66 #include "libsa.h"
     67 #include <sys/reboot.h>
     68 #include <sys/disklabel.h>
     69 
     70 #include <machine/trap.h>
     71 #include <machine/pdc.h>
     72 #include <machine/iomod.h>
     73 #include <machine/nvm.h>
     74 #include <machine/param.h>
     75 #include <machine/cpufunc.h>
     76 
     77 #include "dev_hppa.h"
     78 
     79 /*
     80  * Interface routines to initialize and access the PDC.
     81  */
     82 
     83 pdcio_t pdc;
     84 int	pdcbuf[64] PDC_ALIGNMENT;/* PDC return buffer */
     85 struct	stable_storage sstor;	/* contents of Stable Storage */
     86 int	sstorsiz;		/* size of Stable Storage */
     87 struct bootdata bd;
     88 int bdsize = sizeof(struct bootdata);
     89 
     90 /*
     91  * Initialize PDC and related variables.
     92  */
     93 void
     94 pdc_init(void)
     95 {
     96 	int err;
     97 
     98 	/*
     99 	 * Initialize important global variables (defined above).
    100 	 */
    101 	pdc = (pdcio_t)PAGE0->mem_pdc;
    102 
    103 	err = (*pdc)(PDC_STABLE, PDC_STABLE_SIZE, pdcbuf, 0, 0);
    104 	if (err >= 0) {
    105 		sstorsiz = uimin(pdcbuf[0],sizeof(sstor));
    106 		err = (*pdc)(PDC_STABLE, PDC_STABLE_READ, 0, &sstor, sstorsiz);
    107 	}
    108 
    109 	/*
    110 	 * Now that we (may) have an output device, if we encountered
    111 	 * an error reading Stable Storage (above), let them know.
    112 	 */
    113 #ifdef DEBUG
    114 	if (debug && err)
    115 		printf("Stable storage PDC_STABLE Read Ret'd %d\n", err);
    116 #endif
    117 
    118 	/*
    119 	 * Clear the FAULT light (so we know when we get a real one)
    120 	 */
    121 	(void) (*pdc)(PDC_CHASSIS, PDC_CHASSIS_DISP,
    122 		      PDC_OSTAT(PDC_OSTAT_BOOT) | 0xCEC0);
    123 }
    124 
    125 /*
    126  * Generic READ/WRITE through IODC.  Takes pointer to PDC device
    127  * information, returns (positive) number of bytes actually read or
    128  * the (negative) error condition, or zero if at "EOF".
    129  */
    130 int
    131 iodcstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
    132     size_t *rsize)
    133 {
    134 	struct hppa_dev *dp = devdata;
    135 	struct pz_device *pzdev = dp->pz_dev;
    136 	int offset, xfer, ret;
    137 
    138 #ifdef PDCDEBUG
    139 	if (debug)
    140 		printf("iodcstrategy(%p, %s, %u, %u, %p, %p)\n", devdata,
    141 		       rw==F_READ?"READ":"WRITE", (unsigned) blk, (unsigned) size, buf, rsize);
    142 
    143 	if (debug > 1)
    144 		PZDEV_PRINT(pzdev);
    145 #endif
    146 
    147 	blk <<= DEV_BSHIFT;
    148 	blk += dp->part_off;
    149 	if ((pzdev->pz_class & PCL_CLASS_MASK) == PCL_SEQU) {
    150 		/* rewind and re-read to seek */
    151 		if (blk < dp->last_blk) {
    152 #ifdef PDCDEBUG
    153 			if (debug)
    154 				printf("iodc: rewind ");
    155 #endif
    156 			twiddle();
    157 			if ((ret = (pzdev->pz_iodc_io)(pzdev->pz_hpa,
    158 				IODC_IO_READ, pzdev->pz_spa, pzdev->pz_layers,
    159 				pdcbuf, 0, dp->buf, 0, 0)) < 0) {
    160 #ifdef DEBUG
    161 				if (debug)
    162 					printf("IODC_IO: %d\n", ret);
    163 #endif
    164 				return (EIO);
    165 			} else {
    166 				dp->last_blk = 0;
    167 				dp->last_read = 0;
    168 			}
    169 		}
    170 
    171 #ifdef PDCDEBUG
    172 		if (debug)
    173 			printf("seek %d ", (int) dp->last_blk);
    174 #endif
    175 		for (; (dp->last_blk + dp->last_read) <= blk;
    176 		     dp->last_read = ret) {
    177 			twiddle();
    178 			dp->last_blk += dp->last_read;
    179 			if ((ret = (pzdev->pz_iodc_io)(pzdev->pz_hpa,
    180 				IODC_IO_READ, pzdev->pz_spa, pzdev->pz_layers,
    181 				pdcbuf, (unsigned) dp->last_blk, dp->buf, IODC_MAXIOSIZ,
    182 				IODC_MAXIOSIZ)) < 0) {
    183 #ifdef DEBUG
    184 				if (debug)
    185 					printf("IODC_IO: %d\n", ret);
    186 #endif
    187 				return (EIO);
    188 			}
    189 			if ((ret = pdcbuf[0]) == 0)
    190 				break;
    191 #ifdef PDCDEBUG
    192 			if (debug)
    193 				printf("-");
    194 #endif
    195 		}
    196 #ifdef PDCDEBUG
    197 		if (debug)
    198 			printf("> %d[%d]\n", (int)dp->last_blk, (int)dp->last_read);
    199 #endif
    200 	}
    201 
    202 	xfer = 0;
    203 	/* see if we can scratch anything from buffer */
    204 	if (dp->last_blk <= blk && (dp->last_blk + dp->last_read) > blk) {
    205 		twiddle();
    206 		offset = blk - dp->last_blk;
    207 		xfer = uimin(dp->last_read - offset, size);
    208 		size -= xfer;
    209 		blk += xfer;
    210 #ifdef PDCDEBUG
    211 		if (debug)
    212 			printf("off=%d,xfer=%d,size=%d,blk=%d\n",
    213 			       offset, xfer, (int)size, (int)blk);
    214 #endif
    215 		memcpy(buf, dp->buf + offset, xfer);
    216 		buf = (char *) buf + xfer;
    217 	}
    218 
    219 	/*
    220 	 * double buffer it all the time, to cache
    221 	 */
    222 	for (; size;
    223 	    size -= ret, buf = (char *) buf + ret, blk += ret, xfer += ret) {
    224 		twiddle();
    225 		offset = blk & IOPGOFSET;
    226 		if ((ret = (pzdev->pz_iodc_io)(pzdev->pz_hpa,
    227 				(rw == F_READ? IODC_IO_READ: IODC_IO_WRITE),
    228 				pzdev->pz_spa, pzdev->pz_layers, pdcbuf,
    229 				(int)blk - offset, dp->buf, IODC_MAXIOSIZ,
    230 				IODC_MAXIOSIZ)) < 0) {
    231 #ifdef DEBUG
    232 			if (debug)
    233 				printf("iodc_read(%d,%d): %d\n",
    234 					(int)blk - offset, IODC_MAXIOSIZ, ret);
    235 #endif
    236 			if (xfer)
    237 				break;
    238 			else
    239 				return (EIO);
    240 		}
    241 		if ((ret = pdcbuf[0]) <= 0)
    242 			break;
    243 		dp->last_blk = blk - offset;
    244 		dp->last_read = ret;
    245 		if ((ret -= offset) > size)
    246 			ret = size;
    247 		memcpy(buf, dp->buf + offset, ret);
    248 #ifdef PDCDEBUG
    249 		if (debug)
    250 			printf("read %d(%d,%d)@%x ", ret,
    251 			       (int)dp->last_blk, (int)dp->last_read, (u_int)buf);
    252 #endif
    253 	    }
    254 
    255 #ifdef PDCDEBUG
    256 	if (debug)
    257 		printf("\n");
    258 #endif
    259 
    260 	if (rsize)
    261 		*rsize = xfer;
    262 	return (0);
    263 }
    264 
    265 /*
    266  * Find a device with specified unit number
    267  * (any if unit == -1), and of specified class (PCL_*).
    268  */
    269 struct pz_device *
    270 pdc_findev(int unit, int class)
    271 {
    272 	static struct pz_device pz;
    273 	int layers[sizeof(pz.pz_layers)/sizeof(pz.pz_layers[0])];
    274 	iodcio_t iodc;
    275 	struct iomod *io;
    276 	int err = 0;
    277 
    278 #ifdef	PDCDEBUG
    279 	if (debug)
    280 		printf("pdc_finddev(%d, %x)\n", unit, class);
    281 #endif
    282 	iodc = (iodcio_t)(PAGE0->mem_free + IODC_MAXSIZE);
    283 	io = PAGE0->mem_boot.pz_hpa;
    284 
    285 	/* quick hack for boot device */
    286 	if (PAGE0->mem_boot.pz_class == class &&
    287 	    (unit == -1 || PAGE0->mem_boot.pz_layers[0] == unit)) {
    288 
    289 		bcopy (&PAGE0->mem_boot.pz_dp, &pz.pz_dp, sizeof(pz.pz_dp));
    290 		bcopy (pz.pz_layers, layers, sizeof(layers));
    291 		if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
    292 				  IODC_INIT, iodc, IODC_MAXSIZE)) < 0) {
    293 #ifdef DEBUG
    294 			if (debug)
    295 				printf("IODC_READ: %d\n", err);
    296 #endif
    297 			return NULL;
    298 		}
    299 	} else {
    300 		struct pdc_memmap memmap;
    301 		struct iodc_data mptr;
    302 		int i, stp;
    303 
    304 		for (i = 0; i < 0xf; i++) {
    305 			pz.pz_bc[0] = pz.pz_bc[1] =
    306 			pz.pz_bc[2] = pz.pz_bc[3] = -1;
    307 			pz.pz_bc[4] = 2;
    308 			pz.pz_bc[5] = 0;	/* core bus */
    309 			pz.pz_mod = i;
    310 			if ((pdc)(PDC_MEMMAP, PDC_MEMMAP_HPA, &memmap,
    311 				  &pz.pz_dp) < 0)
    312 				continue;
    313 #ifdef PDCDEBUG
    314 			if (debug)
    315 				printf("memap: %d.%d.%d, hpa=%x, mpgs=%x\n",
    316 				       pz.pz_bc[4], pz.pz_bc[5], pz.pz_mod,
    317 				       memmap.hpa, memmap.morepages);
    318 #endif
    319 			io = (struct iomod *) memmap.hpa;
    320 
    321 			if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, &pdcbuf, io,
    322 				   IODC_DATA, &mptr, sizeof(mptr))) < 0) {
    323 #ifdef DEBUG
    324 				if (debug)
    325 					printf("IODC_DATA: %d\n", err);
    326 #endif
    327 				continue;
    328 			}
    329 
    330 			if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
    331 					  IODC_INIT, iodc, IODC_MAXSIZE)) < 0) {
    332 #ifdef DEBUG
    333 				if (debug)
    334 					printf("IODC_READ: %d\n", err);
    335 #endif
    336 				continue;
    337 			}
    338 
    339 			stp = IODC_INIT_FIRST;
    340 			do {
    341 				if ((err = (iodc)(io, stp, io->io_spa, layers,
    342 						  pdcbuf, 0, 0, 0, 0)) < 0) {
    343 #ifdef DEBUG
    344 					if (debug && err != PDC_ERR_EOD)
    345 						printf("IODC_INIT_%s: %d\n",
    346 						       stp==IODC_INIT_FIRST?
    347 						       "FIRST":"NEXT", err);
    348 #endif
    349 					break;
    350 				}
    351 #ifdef PDCDEBUG
    352 				if (debug)
    353 					printf("[%x,%x,%x,%x,%x,%x], "
    354 					       "[%x,%x,%x,%x,%x,%x]\n",
    355 					       pdcbuf[0], pdcbuf[1], pdcbuf[2],
    356 					       pdcbuf[3], pdcbuf[4], pdcbuf[5],
    357 					       layers[0], layers[1], layers[2],
    358 					       layers[3], layers[4], layers[5]);
    359 #endif
    360 				stp = IODC_INIT_NEXT;
    361 
    362 			} while (pdcbuf[1] != class &&
    363 				 unit != -1 && unit != layers[0]);
    364 
    365 			if (err >= 0)
    366 				break;
    367 		}
    368 	}
    369 
    370 	if (err >= 0) {
    371 		/* init device */
    372 		if (0  && (err = (iodc)(io, IODC_INIT_DEV, io->io_spa,
    373 				  layers, pdcbuf, 0, 0, 0, 0)) < 0) {
    374 #ifdef DEBUG
    375 			if (debug)
    376 				printf("INIT_DEV: %d\n", err);
    377 #endif
    378 			return NULL;
    379 		}
    380 
    381 		/* read i/o entry code */
    382 		if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
    383 				IODC_IO, iodc, IODC_MAXSIZE)) < 0) {
    384 #ifdef DEBUG
    385 			if (debug)
    386 				printf("IODC_READ: %d\n", err);
    387 #endif
    388 			return NULL;
    389 		}
    390 
    391 		pz.pz_flags = 0;
    392 		memcpy(pz.pz_layers, layers, sizeof(pz.pz_layers));
    393 		pz.pz_hpa = io;
    394 /* XXX		pz.pz_spa = io->io_spa; */
    395 		pz.pz_iodc_io = iodc;
    396 		pz.pz_class = class;
    397 
    398 		return &pz;
    399 	}
    400 
    401 	return NULL;
    402 }
    403 
    404 static inline void
    405 fall(int c_base, int c_count, int c_loop, int c_stride, int data)
    406 {
    407 	int loop;
    408 
    409 	for (; c_count--; c_base += c_stride)
    410 		for (loop = c_loop; loop--; )
    411 			if (data)
    412 				fdce(0, c_base);
    413 			else
    414 				fice(0, c_base);
    415 
    416 }
    417 
    418 /*
    419  * fcacheall - Flush all caches.
    420  *
    421  * This routine is just a wrapper around the real cache flush routine.
    422  */
    423 struct pdc_cache pdc_cacheinfo PDC_ALIGNMENT;
    424 
    425 void
    426 fcacheall(void)
    427 {
    428 	int err;
    429 
    430 	if ((err = (*pdc)(PDC_CACHE, PDC_CACHE_DFLT, &pdc_cacheinfo)) < 0) {
    431 #ifdef DEBUG
    432 		if (debug)
    433 			printf("fcacheall: PDC_CACHE failed (%d).\n", err);
    434 #endif
    435 		return;
    436 	}
    437 #if PDCDEBUG
    438 	if (debug)
    439 		printf("pdc_cache:\nic={%u,%x,%x,%u,%u,%u}\n"
    440 		       "dc={%u,%x,%x,%u,%u,%u}\n",
    441 		       pdc_cacheinfo.ic_size, *(u_int*)&pdc_cacheinfo.ic_conf,
    442 		       pdc_cacheinfo.ic_base, pdc_cacheinfo.ic_stride,
    443 		       pdc_cacheinfo.ic_count, pdc_cacheinfo.ic_loop,
    444 		       pdc_cacheinfo.dc_size, *(u_int*)&pdc_cacheinfo.ic_conf,
    445 		       pdc_cacheinfo.dc_base, pdc_cacheinfo.dc_stride,
    446 		       pdc_cacheinfo.dc_count, pdc_cacheinfo.dc_loop);
    447 #endif
    448 	/*
    449 	 * Flush the instruction, then data cache.
    450 	 */
    451 	fall(pdc_cacheinfo.ic_base, pdc_cacheinfo.ic_count,
    452 	     pdc_cacheinfo.ic_loop, pdc_cacheinfo.ic_stride, 0);
    453 	sync_caches();
    454 	fall(pdc_cacheinfo.dc_base, pdc_cacheinfo.dc_count,
    455 	     pdc_cacheinfo.dc_loop, pdc_cacheinfo.dc_stride, 1);
    456 	sync_caches();
    457 }
    458