xref: /netbsd-src/sys/arch/sparc64/dev/psycho.c (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1 /*	$NetBSD: psycho.c,v 1.40 2002/01/14 16:30:31 eeh Exp $	*/
2 
3 /*
4  * Copyright (c) 1999, 2000 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
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 BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include "opt_ddb.h"
32 
33 /*
34  * Support for `psycho' and `psycho+' UPA to PCI bridge and
35  * UltraSPARC IIi and IIe `sabre' PCI controllers.
36  */
37 
38 #undef DEBUG
39 #define DEBUG
40 
41 #ifdef DEBUG
42 #define PDB_PROM	0x01
43 #define PDB_BUSMAP	0x02
44 #define PDB_INTR	0x04
45 int psycho_debug = 0x0;
46 #define DPRINTF(l, s)   do { if (psycho_debug & l) printf s; } while (0)
47 #else
48 #define DPRINTF(l, s)
49 #endif
50 
51 #include <sys/param.h>
52 #include <sys/device.h>
53 #include <sys/errno.h>
54 #include <sys/extent.h>
55 #include <sys/malloc.h>
56 #include <sys/systm.h>
57 #include <sys/time.h>
58 #include <sys/reboot.h>
59 
60 #define _SPARC_BUS_DMA_PRIVATE
61 #include <machine/bus.h>
62 #include <machine/autoconf.h>
63 #include <machine/psl.h>
64 
65 #include <dev/pci/pcivar.h>
66 #include <dev/pci/pcireg.h>
67 
68 #include <sparc64/dev/iommureg.h>
69 #include <sparc64/dev/iommuvar.h>
70 #include <sparc64/dev/psychoreg.h>
71 #include <sparc64/dev/psychovar.h>
72 #include <sparc64/sparc64/cache.h>
73 
74 #include "ioconf.h"
75 
76 static pci_chipset_tag_t psycho_alloc_chipset __P((struct psycho_pbm *, int,
77 						   pci_chipset_tag_t));
78 static void psycho_get_bus_range __P((int, int *));
79 static void psycho_get_ranges __P((int, struct psycho_ranges **, int *));
80 static void psycho_set_intr __P((struct psycho_softc *, int, void *,
81 	u_int64_t *, u_int64_t *));
82 
83 /* Interrupt handlers */
84 static int psycho_ue __P((void *));
85 static int psycho_ce __P((void *));
86 static int psycho_bus_a __P((void *));
87 static int psycho_bus_b __P((void *));
88 static int psycho_powerfail __P((void *));
89 static int psycho_wakeup __P((void *));
90 
91 
92 /* IOMMU support */
93 static void psycho_iommu_init __P((struct psycho_softc *, int));
94 
95 /*
96  * bus space and bus dma support for UltraSPARC `psycho'.  note that most
97  * of the bus dma support is provided by the iommu dvma controller.
98  */
99 static paddr_t psycho_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t, int, int));
100 static int _psycho_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
101 				bus_size_t, int, vaddr_t,
102 				bus_space_handle_t *));
103 static void *psycho_intr_establish __P((bus_space_tag_t, int, int, int,
104 				int (*) __P((void *)), void *));
105 
106 static int psycho_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
107 				   bus_size_t, struct proc *, int));
108 static void psycho_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
109 static int psycho_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
110 		    bus_dma_segment_t *, int, bus_size_t, int));
111 static void psycho_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
112 				    bus_size_t, int));
113 int psycho_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
114 			     bus_dma_segment_t *, int, int *, int));
115 void psycho_dmamem_free __P((bus_dma_tag_t, bus_dma_segment_t *, int));
116 int psycho_dmamem_map __P((bus_dma_tag_t, bus_dma_segment_t *, int, size_t,
117 			   caddr_t *, int));
118 void psycho_dmamem_unmap __P((bus_dma_tag_t, caddr_t, size_t));
119 
120 /* base pci_chipset */
121 extern struct sparc_pci_chipset _sparc_pci_chipset;
122 
123 /*
124  * autoconfiguration
125  */
126 static	int	psycho_match __P((struct device *, struct cfdata *, void *));
127 static	void	psycho_attach __P((struct device *, struct device *, void *));
128 static	int	psycho_print __P((void *aux, const char *p));
129 
130 struct cfattach psycho_ca = {
131         sizeof(struct psycho_softc), psycho_match, psycho_attach
132 };
133 
134 /*
135  * "sabre" is the UltraSPARC IIi onboard UPA to PCI bridge.  It manages a
136  * single PCI bus and does not have a streaming buffer.  It often has an APB
137  * (advanced PCI bridge) connected to it, which was designed specifically for
138  * the IIi.  The APB let's the IIi handle two independednt PCI buses, and
139  * appears as two "simba"'s underneath the sabre.
140  *
141  * "psycho" and "psycho+" is a dual UPA to PCI bridge.  It sits on the UPA bus
142  * and manages two PCI buses.  "psycho" has two 64-bit 33MHz buses, while
143  * "psycho+" controls both a 64-bit 33Mhz and a 64-bit 66Mhz PCI bus.  You
144  * will usually find a "psycho+" since I don't think the original "psycho"
145  * ever shipped, and if it did it would be in the U30.
146  *
147  * Each "psycho" PCI bus appears as a separate OFW node, but since they are
148  * both part of the same IC, they only have a single register space.  As such,
149  * they need to be configured together, even though the autoconfiguration will
150  * attach them separately.
151  *
152  * On UltraIIi machines, "sabre" itself usually takes pci0, with "simba" often
153  * as pci1 and pci2, although they have been implemented with other PCI bus
154  * numbers on some machines.
155  *
156  * On UltraII machines, there can be any number of "psycho+" ICs, each
157  * providing two PCI buses.
158  *
159  *
160  * XXXX The psycho/sabre node has an `interrupts' attribute.  They contain
161  * the values of the following interrupts in this order:
162  *
163  * PCI Bus Error	(30)
164  * DMA UE		(2e)
165  * DMA CE		(2f)
166  * Power Fail		(25)
167  *
168  * We really should attach handlers for each.
169  *
170  */
171 
172 #define	ROM_PCI_NAME		"pci"
173 
174 struct psycho_names {
175 	char *p_name;
176 	int p_type;
177 } psycho_names[] = {
178 	{ "SUNW,psycho",        PSYCHO_MODE_PSYCHO      },
179 	{ "pci108e,8000",       PSYCHO_MODE_PSYCHO      },
180 	{ "SUNW,sabre",         PSYCHO_MODE_SABRE       },
181 	{ "pci108e,a000",       PSYCHO_MODE_SABRE       },
182 	{ "pci108e,a001",       PSYCHO_MODE_SABRE       },
183 	{ NULL, 0 }
184 };
185 
186 static	int
187 psycho_match(parent, match, aux)
188 	struct device	*parent;
189 	struct cfdata	*match;
190 	void		*aux;
191 {
192 	struct mainbus_attach_args *ma = aux;
193 	char *model = PROM_getpropstring(ma->ma_node, "model");
194 	int i;
195 
196 	/* match on a name of "pci" and a sabre or a psycho */
197 	if (strcmp(ma->ma_name, ROM_PCI_NAME) == 0) {
198 		for (i=0; psycho_names[i].p_name; i++)
199 			if (strcmp(model, psycho_names[i].p_name) == 0)
200 				return (1);
201 
202 		model = PROM_getpropstring(ma->ma_node, "compatible");
203 		for (i=0; psycho_names[i].p_name; i++)
204 			if (strcmp(model, psycho_names[i].p_name) == 0)
205 				return (1);
206 	}
207 	return (0);
208 }
209 
210 /*
211  * SUNW,psycho initialisation ..
212  *	- find the per-psycho registers
213  *	- figure out the IGN.
214  *	- find our partner psycho
215  *	- configure ourselves
216  *	- bus range, bus,
217  *	- get interrupt-map and interrupt-map-mask
218  *	- setup the chipsets.
219  *	- if we're the first of the pair, initialise the IOMMU, otherwise
220  *	  just copy it's tags and addresses.
221  */
222 static	void
223 psycho_attach(parent, self, aux)
224 	struct device *parent, *self;
225 	void *aux;
226 {
227 	struct psycho_softc *sc = (struct psycho_softc *)self;
228 	struct psycho_softc *osc = NULL;
229 	struct psycho_pbm *pp;
230 	struct pcibus_attach_args pba;
231 	struct mainbus_attach_args *ma = aux;
232 	bus_space_handle_t bh;
233 	u_int64_t csr;
234 	int psycho_br[2], n, i;
235 	struct pci_ctl *pci_ctl;
236 	char *model = PROM_getpropstring(ma->ma_node, "model");
237 
238 	printf("\n");
239 
240 	sc->sc_node = ma->ma_node;
241 	sc->sc_bustag = ma->ma_bustag;
242 	sc->sc_dmatag = ma->ma_dmatag;
243 
244 	/*
245 	 * call the model-specific initialisation routine.
246 	 */
247 	for (i=0; psycho_names[i].p_name; i++)
248 		if (strcmp(model, psycho_names[i].p_name) == 0) {
249 			sc->sc_mode = psycho_names[i].p_type;
250 			goto found;
251 		}
252 
253 	model = PROM_getpropstring(ma->ma_node, "compatible");
254 	for (i=0; psycho_names[i].p_name; i++)
255 		if (strcmp(model, psycho_names[i].p_name) == 0) {
256 			sc->sc_mode = psycho_names[i].p_type;
257 			goto found;
258 		}
259 
260 	panic("unknown psycho model %s", model);
261 found:
262 
263 	/*
264 	 * The psycho gets three register banks:
265 	 * (0) per-PBM configuration and status registers
266 	 * (1) per-PBM PCI configuration space, containing only the
267 	 *     PBM 256-byte PCI header
268 	 * (2) the shared psycho configuration registers (struct psychoreg)
269 	 *
270 	 * XXX use the prom address for the psycho registers?  we do so far.
271 	 */
272 
273 	/* Register layouts are different.  stuupid. */
274 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO) {
275 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[2].ur_paddr;
276 
277 		if (ma->ma_naddress > 2) {
278 			sc->sc_regs = (struct psychoreg *)
279 				(u_long)ma->ma_address[2];
280 			pci_ctl = (struct pci_ctl *)
281 				(u_long)ma->ma_address[0];
282 		} else if (ma->ma_nreg > 2) {
283 			bus_space_handle_t handle;
284 
285 			/* We need to map this in ourselves. */
286 			if (bus_space_map2(sc->sc_bustag, 0,
287 				ma->ma_reg[2].ur_paddr,
288 				ma->ma_reg[2].ur_len, 0, NULL, &handle))
289 				panic("psycho_attach: cannot map regs");
290 			sc->sc_regs = (struct psychoreg *)(u_long)handle;
291 
292 			if (bus_space_map2(sc->sc_bustag, 0,
293 				ma->ma_reg[0].ur_paddr,
294 				ma->ma_reg[0].ur_len, 0, NULL, &handle))
295 				panic("psycho_attach: cannot map ctl");
296 /* XXX -- this is lost but never unmapped */
297 			pci_ctl = (struct pci_ctl *)(u_long)handle;
298 
299 		} else
300 			panic("psycho_attach: %d not enough registers",
301 				ma->ma_nreg);
302 	} else {
303 		sc->sc_basepaddr = (paddr_t)ma->ma_reg[0].ur_paddr;
304 
305 		if (ma->ma_naddress) {
306 			sc->sc_regs = (struct psychoreg *)
307 				(u_long)ma->ma_address[0];
308 			pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
309 		} else if (ma->ma_nreg) {
310 			bus_space_handle_t handle;
311 
312 			/* We need to map this in ourselves. */
313 			if (bus_space_map2(sc->sc_bustag, 0,
314 				ma->ma_reg[0].ur_paddr,
315 				ma->ma_reg[0].ur_len, 0, NULL, &handle))
316 				panic("psycho_attach: cannot map regs");
317 			sc->sc_regs = (struct psychoreg *)(u_long)handle;
318 			pci_ctl = (struct pci_ctl *)&sc->sc_regs->psy_pcictl[0];
319 		} else
320 			panic("psycho_attach: %d not enough registers",
321 				ma->ma_nreg);
322 	}
323 
324 	csr = sc->sc_regs->psy_csr;
325 	sc->sc_ign = 0x7c0; /* APB IGN is always 0x7c */
326 	if (sc->sc_mode == PSYCHO_MODE_PSYCHO)
327 		sc->sc_ign = PSYCHO_GCSR_IGN(csr) << 6;
328 
329 	printf("%s: impl %d, version %d: ign %x ",
330 		model, PSYCHO_GCSR_IMPL(csr), PSYCHO_GCSR_VERS(csr),
331 		sc->sc_ign);
332 	/*
333 	 * Match other psycho's that are already configured against
334 	 * the base physical address. This will be the same for a
335 	 * pair of devices that share register space.
336 	 */
337 	for (n = 0; n < psycho_cd.cd_ndevs; n++) {
338 
339 		struct psycho_softc *asc =
340 			(struct psycho_softc *)psycho_cd.cd_devs[n];
341 
342 		if (asc == NULL || asc == sc)
343 			/* This entry is not there or it is me */
344 			continue;
345 
346 		if (asc->sc_basepaddr != sc->sc_basepaddr)
347 			/* This is an unrelated psycho */
348 			continue;
349 
350 		/* Found partner */
351 		osc = asc;
352 		break;
353 	}
354 
355 
356 	/* Oh, dear.  OK, lets get started */
357 
358 	/*
359 	 * Setup the PCI control register
360 	 */
361 	csr = bus_space_read_8(sc->sc_bustag,
362 			(bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0);
363 	csr |= PCICTL_MRLM |
364 	       PCICTL_ARB_PARK |
365 	       PCICTL_ERRINTEN |
366 	       PCICTL_4ENABLE;
367 	csr &= ~(PCICTL_SERR |
368 		 PCICTL_CPU_PRIO |
369 		 PCICTL_ARB_PRIO |
370 		 PCICTL_RTRYWAIT);
371 	bus_space_write_8(sc->sc_bustag,
372 			(bus_space_handle_t)(u_long)&pci_ctl->pci_csr, 0, csr);
373 
374 
375 	/*
376 	 * Allocate our psycho_pbm
377 	 */
378 	pp = sc->sc_psycho_this = malloc(sizeof *pp, M_DEVBUF, M_NOWAIT);
379 	if (pp == NULL)
380 		panic("could not allocate psycho pbm");
381 
382 	memset(pp, 0, sizeof *pp);
383 
384 	pp->pp_sc = sc;
385 
386 	/* grab the psycho ranges */
387 	psycho_get_ranges(sc->sc_node, &pp->pp_range, &pp->pp_nrange);
388 
389 	/* get the bus-range for the psycho */
390 	psycho_get_bus_range(sc->sc_node, psycho_br);
391 
392 	pba.pba_bus = psycho_br[0];
393 
394 	printf("bus range %u to %u", psycho_br[0], psycho_br[1]);
395 	printf("; PCI bus %d", psycho_br[0]);
396 
397 	pp->pp_pcictl = &sc->sc_regs->psy_pcictl[0];
398 
399 	/* allocate our tags */
400 	pp->pp_memt = psycho_alloc_mem_tag(pp);
401 	pp->pp_iot = psycho_alloc_io_tag(pp);
402 	pp->pp_dmat = psycho_alloc_dma_tag(pp);
403 	pp->pp_flags = (pp->pp_memt ? PCI_FLAGS_MEM_ENABLED : 0) |
404 		       (pp->pp_iot ? PCI_FLAGS_IO_ENABLED : 0);
405 
406 	/* allocate a chipset for this */
407 	pp->pp_pc = psycho_alloc_chipset(pp, sc->sc_node, &_sparc_pci_chipset);
408 
409 	/* setup the rest of the psycho pbm */
410 	pba.pba_pc = psycho_alloc_chipset(pp, sc->sc_node, pp->pp_pc);
411 
412 	printf("\n");
413 
414 	/*
415 	 * And finally, if we're a sabre or the first of a pair of psycho's to
416 	 * arrive here, start up the IOMMU and get a config space tag.
417 	 */
418 	if (osc == NULL) {
419 		uint64_t timeo;
420 
421 		/*
422 		 * Establish handlers for interesting interrupts....
423 		 *
424 		 * XXX We need to remember these and remove this to support
425 		 * hotplug on the UPA/FHC bus.
426 		 *
427 		 * XXX Not all controllers have these, but installing them
428 		 * is better than trying to sort through this mess.
429 		 */
430 		psycho_set_intr(sc, 15, psycho_ue,
431 			&sc->sc_regs->ue_int_map,
432 			&sc->sc_regs->ue_clr_int);
433 		psycho_set_intr(sc, 1, psycho_ce,
434 			&sc->sc_regs->ce_int_map,
435 			&sc->sc_regs->ce_clr_int);
436 		psycho_set_intr(sc, 15, psycho_bus_a,
437 			&sc->sc_regs->pciaerr_int_map,
438 			&sc->sc_regs->pciaerr_clr_int);
439 		psycho_set_intr(sc, 15, psycho_bus_b,
440 			&sc->sc_regs->pciberr_int_map,
441 			&sc->sc_regs->pciberr_clr_int);
442 		psycho_set_intr(sc, 15, psycho_powerfail,
443 			&sc->sc_regs->power_int_map,
444 			&sc->sc_regs->power_clr_int);
445 		psycho_set_intr(sc, 1, psycho_wakeup,
446 			&sc->sc_regs->pwrmgt_int_map,
447 			&sc->sc_regs->pwrmgt_clr_int);
448 
449 
450 		/*
451 		 * Apparently a number of machines with psycho and psycho+
452 		 * controllers have interrupt latency issues.  We'll try
453 		 * setting the interrupt retry timeout to 0xff which gives us
454 		 * a retry of 3-6 usec (which is what sysio is set to) for the
455 		 * moment, which seems to help alleviate this problem.
456 		 */
457 		timeo = bus_space_read_8(sc->sc_bustag,
458 			(bus_space_handle_t)
459 			(u_long)&sc->sc_regs->intr_retry_timer, 0);
460 		if (timeo > 0xfff) {
461 #ifdef DEBUG
462 			printf("decreasing interrupt retry timeout "
463 				"from %lx to 0xff\n", (long)timeo);
464 #endif
465 			bus_space_write_8(sc->sc_bustag,
466 				(bus_space_handle_t)
467 				(u_long)&sc->sc_regs->intr_retry_timer, 0,
468 				0xff);
469 		}
470 
471 		/*
472 		 * Setup IOMMU and PCI configuration if we're the first
473 		 * of a pair of psycho's to arrive here.
474 		 *
475 		 * We should calculate a TSB size based on amount of RAM
476 		 * and number of bus controllers and number an type of
477 		 * child devices.
478 		 *
479 		 * For the moment, 32KB should be more than enough.
480 		 */
481 		sc->sc_is = malloc(sizeof(struct iommu_state),
482 			M_DEVBUF, M_NOWAIT);
483 		if (sc->sc_is == NULL)
484 			panic("psycho_attach: malloc iommu_state");
485 
486 
487 		sc->sc_is->is_sb[0] = 0;
488 		sc->sc_is->is_sb[1] = 0;
489 		if (PROM_getproplen(sc->sc_node, "no-streaming-cache") >= 0)
490 			sc->sc_is->is_sb[0] = &pci_ctl->pci_strbuf;
491 
492 		psycho_iommu_init(sc, 2);
493 
494 		sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
495 		if (bus_space_map2(sc->sc_bustag,
496 				  PCI_CONFIG_BUS_SPACE,
497 				  sc->sc_basepaddr + 0x01000000,
498 				  0x0100000,
499 				  0,
500 				  0,
501 				  &bh))
502 			panic("could not map psycho PCI configuration space");
503 		sc->sc_configaddr = (off_t)bh;
504 	} else {
505 		/* Just copy IOMMU state, config tag and address */
506 		sc->sc_is = osc->sc_is;
507 		sc->sc_configtag = osc->sc_configtag;
508 		sc->sc_configaddr = osc->sc_configaddr;
509 
510 		if (PROM_getproplen(sc->sc_node, "no-streaming-cache") >= 0)
511 			sc->sc_is->is_sb[1] = &pci_ctl->pci_strbuf;
512 		iommu_reset(sc->sc_is);
513 	}
514 
515 	/*
516 	 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
517 	 */
518 	pba.pba_busname = "pci";
519 	pba.pba_flags = sc->sc_psycho_this->pp_flags;
520 	pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
521 	pba.pba_iot = sc->sc_psycho_this->pp_iot;
522 	pba.pba_memt = sc->sc_psycho_this->pp_memt;
523 
524 	config_found(self, &pba, psycho_print);
525 }
526 
527 static	int
528 psycho_print(aux, p)
529 	void *aux;
530 	const char *p;
531 {
532 
533 	if (p == NULL)
534 		return (UNCONF);
535 	return (QUIET);
536 }
537 
538 static void
539 psycho_set_intr(sc, ipl, handler, mapper, clearer)
540 	struct psycho_softc *sc;
541 	int ipl;
542 	void *handler;
543 	u_int64_t *mapper;
544 	u_int64_t *clearer;
545 {
546 	struct intrhand *ih;
547 
548 	ih = (struct intrhand *)malloc(sizeof(struct intrhand),
549 		M_DEVBUF, M_NOWAIT);
550 	ih->ih_arg = sc;
551 	ih->ih_map = mapper;
552 	ih->ih_clr = clearer;
553 	ih->ih_fun = handler;
554 	ih->ih_pil = (1<<ipl);
555 	ih->ih_number = INTVEC(*(ih->ih_map));
556 	intr_establish(ipl, ih);
557 	*(ih->ih_map) |= INTMAP_V;
558 }
559 
560 /*
561  * PCI bus support
562  */
563 
564 /*
565  * allocate a PCI chipset tag and set it's cookie.
566  */
567 static pci_chipset_tag_t
568 psycho_alloc_chipset(pp, node, pc)
569 	struct psycho_pbm *pp;
570 	int node;
571 	pci_chipset_tag_t pc;
572 {
573 	pci_chipset_tag_t npc;
574 
575 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
576 	if (npc == NULL)
577 		panic("could not allocate pci_chipset_tag_t");
578 	memcpy(npc, pc, sizeof *pc);
579 	npc->cookie = pp;
580 	npc->rootnode = node;
581 	npc->curnode = node;
582 
583 	return (npc);
584 }
585 
586 /*
587  * grovel the OBP for various psycho properties
588  */
589 static void
590 psycho_get_bus_range(node, brp)
591 	int node;
592 	int *brp;
593 {
594 	int n;
595 
596 	if (PROM_getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
597 		panic("could not get psycho bus-range");
598 	if (n != 2)
599 		panic("broken psycho bus-range");
600 	DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
601 }
602 
603 static void
604 psycho_get_ranges(node, rp, np)
605 	int node;
606 	struct psycho_ranges **rp;
607 	int *np;
608 {
609 
610 	if (PROM_getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
611 		panic("could not get psycho ranges");
612 	DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
613 }
614 
615 /*
616  * Interrupt handlers.
617  */
618 
619 static int
620 psycho_ue(arg)
621 	void *arg;
622 {
623 	struct psycho_softc *sc = (struct psycho_softc *)arg;
624 	struct psychoreg *regs = sc->sc_regs;
625 	long long afsr = regs->psy_ue_afsr;
626 	long long afar = regs->psy_ue_afar;
627 	char bits[128];
628 
629 	/*
630 	 * It's uncorrectable.  Dump the regs and panic.
631 	 */
632 	panic("%s: uncorrectable DMA error AFAR %llx pa %llx AFSR %llx:\n%s",
633 		sc->sc_dev.dv_xname, afar,
634 		(long long)iommu_extract(sc->sc_is, (vaddr_t)afar), afsr,
635 		bitmask_snprintf(afsr, PSYCHO_UE_AFSR_BITS,
636 			bits, sizeof(bits)));
637 	return (1);
638 }
639 static int
640 psycho_ce(arg)
641 	void *arg;
642 {
643 	struct psycho_softc *sc = (struct psycho_softc *)arg;
644 	struct psychoreg *regs = sc->sc_regs;
645 
646 	/*
647 	 * It's correctable.  Dump the regs and continue.
648 	 */
649 
650 	printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
651 		sc->sc_dev.dv_xname,
652 		(long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
653 	return (1);
654 }
655 static int
656 psycho_bus_a(arg)
657 	void *arg;
658 {
659 	struct psycho_softc *sc = (struct psycho_softc *)arg;
660 	struct psychoreg *regs = sc->sc_regs;
661 
662 	/*
663 	 * It's uncorrectable.  Dump the regs and panic.
664 	 */
665 
666 	panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
667 		sc->sc_dev.dv_xname,
668 		(long long)regs->psy_pcictl[0].pci_afar,
669 		(long long)regs->psy_pcictl[0].pci_afsr);
670 	return (1);
671 }
672 static int
673 psycho_bus_b(arg)
674 	void *arg;
675 {
676 	struct psycho_softc *sc = (struct psycho_softc *)arg;
677 	struct psychoreg *regs = sc->sc_regs;
678 
679 	/*
680 	 * It's uncorrectable.  Dump the regs and panic.
681 	 */
682 
683 	panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
684 		sc->sc_dev.dv_xname,
685 		(long long)regs->psy_pcictl[0].pci_afar,
686 		(long long)regs->psy_pcictl[0].pci_afsr);
687 	return (1);
688 }
689 static int
690 psycho_powerfail(arg)
691 	void *arg;
692 {
693 
694 	/*
695 	 * We lost power.  Try to shut down NOW.
696 	 */
697 	printf("Power Failure Detected: Shutting down NOW.\n");
698 	cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
699 	return (1);
700 }
701 static
702 int psycho_wakeup(arg)
703 	void *arg;
704 {
705 	struct psycho_softc *sc = (struct psycho_softc *)arg;
706 
707 	/*
708 	 * Gee, we don't really have a framework to deal with this
709 	 * properly.
710 	 */
711 	printf("%s: power management wakeup\n",	sc->sc_dev.dv_xname);
712 	return (1);
713 }
714 
715 
716 
717 /*
718  * initialise the IOMMU..
719  */
720 void
721 psycho_iommu_init(sc, tsbsize)
722 	struct psycho_softc *sc;
723 	int tsbsize;
724 {
725 	char *name;
726 	struct iommu_state *is = sc->sc_is;
727 	u_int32_t iobase = -1;
728 	int *vdma = NULL;
729 	int nitem;
730 
731 	/* punch in our copies */
732 	is->is_bustag = sc->sc_bustag;
733 	is->is_iommu = &sc->sc_regs->psy_iommu;
734 
735 	/*
736 	 * Separate the men from the boys.  Get the `virtual-dma'
737 	 * property for sabre and use that to make sure the damn
738 	 * iommu works.
739 	 *
740 	 * We could query the `#virtual-dma-size-cells' and
741 	 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
742 	 */
743 	if (!PROM_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
744 		(void **)&vdma)) {
745 		/* Damn.  Gotta use these values. */
746 		iobase = vdma[0];
747 #define	TSBCASE(x)	case 1<<((x)+23): tsbsize = (x); break
748 		switch (vdma[1]) {
749 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
750 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
751 		default:
752 			printf("bogus tsb size %x, using 7\n", vdma[1]);
753 			TSBCASE(7);
754 		}
755 #undef TSBCASE
756 	}
757 
758 	/* give us a nice name.. */
759 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
760 	if (name == 0)
761 		panic("couldn't malloc iommu name");
762 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
763 
764 	iommu_init(name, is, tsbsize, iobase);
765 }
766 
767 /*
768  * below here is bus space and bus dma support
769  */
770 bus_space_tag_t
771 psycho_alloc_bus_tag(pp, type)
772 	struct psycho_pbm *pp;
773 	int type;
774 {
775 	struct psycho_softc *sc = pp->pp_sc;
776 	bus_space_tag_t bt;
777 
778 	bt = (bus_space_tag_t)
779 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
780 	if (bt == NULL)
781 		panic("could not allocate psycho bus tag");
782 
783 	bzero(bt, sizeof *bt);
784 	bt->cookie = pp;
785 	bt->parent = sc->sc_bustag;
786 	bt->type = type;
787 	bt->sparc_bus_map = _psycho_bus_map;
788 	bt->sparc_bus_mmap = psycho_bus_mmap;
789 	bt->sparc_intr_establish = psycho_intr_establish;
790 	return (bt);
791 }
792 
793 bus_dma_tag_t
794 psycho_alloc_dma_tag(pp)
795 	struct psycho_pbm *pp;
796 {
797 	struct psycho_softc *sc = pp->pp_sc;
798 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
799 
800 	dt = (bus_dma_tag_t)
801 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
802 	if (dt == NULL)
803 		panic("could not allocate psycho dma tag");
804 
805 	bzero(dt, sizeof *dt);
806 	dt->_cookie = pp;
807 	dt->_parent = pdt;
808 #define PCOPY(x)	dt->x = pdt->x
809 	PCOPY(_dmamap_create);
810 	PCOPY(_dmamap_destroy);
811 	dt->_dmamap_load = psycho_dmamap_load;
812 	PCOPY(_dmamap_load_mbuf);
813 	PCOPY(_dmamap_load_uio);
814 	dt->_dmamap_load_raw = psycho_dmamap_load_raw;
815 	dt->_dmamap_unload = psycho_dmamap_unload;
816 	dt->_dmamap_sync = psycho_dmamap_sync;
817 	dt->_dmamem_alloc = psycho_dmamem_alloc;
818 	dt->_dmamem_free = psycho_dmamem_free;
819 	dt->_dmamem_map = psycho_dmamem_map;
820 	dt->_dmamem_unmap = psycho_dmamem_unmap;
821 	PCOPY(_dmamem_mmap);
822 #undef	PCOPY
823 	return (dt);
824 }
825 
826 /*
827  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
828  * PCI physical addresses.
829  */
830 
831 static int get_childspace __P((int));
832 
833 static int
834 get_childspace(type)
835 	int type;
836 {
837 	int ss;
838 
839 	switch (type) {
840 	case PCI_CONFIG_BUS_SPACE:
841 		ss = 0x00;
842 		break;
843 	case PCI_IO_BUS_SPACE:
844 		ss = 0x01;
845 		break;
846 	case PCI_MEMORY_BUS_SPACE:
847 		ss = 0x02;
848 		break;
849 #if 0
850 	/* we don't do 64 bit memory space */
851 	case PCI_MEMORY64_BUS_SPACE:
852 		ss = 0x03;
853 		break;
854 #endif
855 	default:
856 		panic("get_childspace: unknown bus type");
857 	}
858 
859 	return (ss);
860 }
861 
862 static int
863 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
864 	bus_space_tag_t t;
865 	bus_type_t btype;
866 	bus_addr_t offset;
867 	bus_size_t size;
868 	int	flags;
869 	vaddr_t vaddr;
870 	bus_space_handle_t *hp;
871 {
872 	struct psycho_pbm *pp = t->cookie;
873 	struct psycho_softc *sc = pp->pp_sc;
874 	int i, ss;
875 
876 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_map: type %d off %qx sz %qx flags %d va %p", t->type, (unsigned long long)offset, (unsigned long long)size, flags,
877 	    (void *)vaddr));
878 
879 	ss = get_childspace(t->type);
880 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
881 
882 	for (i = 0; i < pp->pp_nrange; i++) {
883 		bus_addr_t paddr;
884 
885 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
886 			continue;
887 
888 		paddr = pp->pp_range[i].phys_lo + offset;
889 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
890 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
891 			       (long)ss, (long)offset,
892 			       (unsigned long long)paddr));
893 		return (bus_space_map2(sc->sc_bustag, t->type, paddr,
894 					size, flags, vaddr, hp));
895 	}
896 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
897 	return (EINVAL);
898 }
899 
900 static paddr_t
901 psycho_bus_mmap(t, paddr, off, prot, flags)
902 	bus_space_tag_t t;
903 	bus_addr_t paddr;
904 	off_t off;
905 	int prot;
906 	int flags;
907 {
908 	bus_addr_t offset = paddr;
909 	struct psycho_pbm *pp = t->cookie;
910 	struct psycho_softc *sc = pp->pp_sc;
911 	int i, ss;
912 
913 	ss = get_childspace(t->type);
914 
915 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n",
916 		prot, flags, (unsigned long long)paddr));
917 
918 	for (i = 0; i < pp->pp_nrange; i++) {
919 		bus_addr_t paddr;
920 
921 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
922 			continue;
923 
924 		paddr = pp->pp_range[i].phys_lo + offset;
925 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
926 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr "
927 			"space %lx offset %lx paddr %qx\n",
928 			       (long)ss, (long)offset,
929 			       (unsigned long long)paddr));
930 		return (bus_space_mmap(sc->sc_bustag, paddr, off,
931 				       prot, flags));
932 	}
933 
934 	return (-1);
935 }
936 
937 
938 /*
939  * install an interrupt handler for a PCI device
940  */
941 void *
942 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
943 	bus_space_tag_t t;
944 	int ihandle;
945 	int level;
946 	int flags;
947 	int (*handler) __P((void *));
948 	void *arg;
949 {
950 	struct psycho_pbm *pp = t->cookie;
951 	struct psycho_softc *sc = pp->pp_sc;
952 	struct intrhand *ih;
953 	volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
954 	int64_t intrmap = 0;
955 	int ino;
956 	long vec = INTVEC(ihandle);
957 
958 	ih = (struct intrhand *)
959 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
960 	if (ih == NULL)
961 		return (NULL);
962 
963 	/*
964 	 * Hunt through all the interrupt mapping regs to look for our
965 	 * interrupt vector.
966 	 *
967 	 * XXX We only compare INOs rather than IGNs since the firmware may
968 	 * not provide the IGN and the IGN is constant for all device on that
969 	 * PCI controller.  This could cause problems for the FFB/external
970 	 * interrupt which has a full vector that can be set arbitrarily.
971 	 */
972 
973 
974 	DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
975 	ino = INTINO(vec);
976 	DPRINTF(PDB_INTR, (" ino %x", ino));
977 
978 	/* If the device didn't ask for an IPL, use the one encoded. */
979 	if (level == IPL_NONE) level = INTLEV(vec);
980 	/* If it still has no level, print a warning and assign IPL 2 */
981 	if (level == IPL_NONE) {
982 		printf("ERROR: no IPL, setting IPL 2.\n");
983 		level = 2;
984 	}
985 
986 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
987 
988 		DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
989 		    (long)ino, intrlev[ino]));
990 
991 		/* Hunt thru obio first */
992 		for (intrmapptr = &sc->sc_regs->scsi_int_map,
993 			     intrclrptr = &sc->sc_regs->scsi_clr_int;
994 		     intrmapptr <= &sc->sc_regs->ffb1_int_map;
995 		     intrmapptr++, intrclrptr++) {
996 			if (INTINO(*intrmapptr) == ino)
997 				goto found;
998 		}
999 
1000 		/* Now do PCI interrupts */
1001 		for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
1002 			     intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
1003 		     intrmapptr <= &sc->sc_regs->pcib_slot3_int;
1004 		     intrmapptr++, intrclrptr += 4) {
1005 			if (((*intrmapptr ^ vec) & 0x3c) == 0) {
1006 				intrclrptr += vec & 0x3;
1007 				goto found;
1008 			}
1009 		}
1010 		printf("Cannot find interrupt vector %lx\n", vec);
1011 		return (NULL);
1012 
1013 	found:
1014 		/* Register the map and clear intr registers */
1015 		ih->ih_map = intrmapptr;
1016 		ih->ih_clr = intrclrptr;
1017 	}
1018 #ifdef NOT_DEBUG
1019 	if (psycho_debug & PDB_INTR) {
1020 		long i;
1021 
1022 		for (i = 0; i < 500000000; i++)
1023 			continue;
1024 	}
1025 #endif
1026 
1027 	ih->ih_fun = handler;
1028 	ih->ih_arg = arg;
1029 	ih->ih_pil = level;
1030 	ih->ih_number = ino | sc->sc_ign;
1031 
1032 	DPRINTF(PDB_INTR, (
1033 	    "; installing handler %p arg %p with ino %u pil %u\n",
1034 	    handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1035 
1036 	intr_establish(ih->ih_pil, ih);
1037 
1038 	/*
1039 	 * Enable the interrupt now we have the handler installed.
1040 	 * Read the current value as we can't change it besides the
1041 	 * valid bit so so make sure only this bit is changed.
1042 	 *
1043 	 * XXXX --- we really should use bus_space for this.
1044 	 */
1045 	if (intrmapptr) {
1046 		intrmap = *intrmapptr;
1047 		DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1048 			(unsigned long long)intrmap));
1049 
1050 		/* Enable the interrupt */
1051 		intrmap |= INTMAP_V;
1052 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1053 		DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1054 			(unsigned long long)intrmap));
1055 		*intrmapptr = intrmap;
1056 		DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1057 			(unsigned long long)(intrmap = *intrmapptr)));
1058 	}
1059 	return (ih);
1060 }
1061 
1062 /*
1063  * hooks into the iommu dvma calls.
1064  */
1065 int
1066 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1067 	bus_dma_tag_t t;
1068 	bus_dmamap_t map;
1069 	void *buf;
1070 	bus_size_t buflen;
1071 	struct proc *p;
1072 	int flags;
1073 {
1074 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1075 	struct psycho_softc *sc = pp->pp_sc;
1076 
1077 	return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1078 }
1079 
1080 void
1081 psycho_dmamap_unload(t, map)
1082 	bus_dma_tag_t t;
1083 	bus_dmamap_t map;
1084 {
1085 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1086 	struct psycho_softc *sc = pp->pp_sc;
1087 
1088 	iommu_dvmamap_unload(t, sc->sc_is, map);
1089 }
1090 
1091 int
1092 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1093 	bus_dma_tag_t t;
1094 	bus_dmamap_t map;
1095 	bus_dma_segment_t *segs;
1096 	int nsegs;
1097 	bus_size_t size;
1098 	int flags;
1099 {
1100 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1101 	struct psycho_softc *sc = pp->pp_sc;
1102 
1103 	return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1104 }
1105 
1106 void
1107 psycho_dmamap_sync(t, map, offset, len, ops)
1108 	bus_dma_tag_t t;
1109 	bus_dmamap_t map;
1110 	bus_addr_t offset;
1111 	bus_size_t len;
1112 	int ops;
1113 {
1114 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1115 	struct psycho_softc *sc = pp->pp_sc;
1116 
1117 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1118 		/* Flush the CPU then the IOMMU */
1119 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
1120 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1121 	}
1122 	if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1123 		/* Flush the IOMMU then the CPU */
1124 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1125 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
1126 	}
1127 
1128 }
1129 
1130 int
1131 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1132 	bus_dma_tag_t t;
1133 	bus_size_t size;
1134 	bus_size_t alignment;
1135 	bus_size_t boundary;
1136 	bus_dma_segment_t *segs;
1137 	int nsegs;
1138 	int *rsegs;
1139 	int flags;
1140 {
1141 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1142 	struct psycho_softc *sc = pp->pp_sc;
1143 
1144 	return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1145 	    segs, nsegs, rsegs, flags));
1146 }
1147 
1148 void
1149 psycho_dmamem_free(t, segs, nsegs)
1150 	bus_dma_tag_t t;
1151 	bus_dma_segment_t *segs;
1152 	int nsegs;
1153 {
1154 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1155 	struct psycho_softc *sc = pp->pp_sc;
1156 
1157 	iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1158 }
1159 
1160 int
1161 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1162 	bus_dma_tag_t t;
1163 	bus_dma_segment_t *segs;
1164 	int nsegs;
1165 	size_t size;
1166 	caddr_t *kvap;
1167 	int flags;
1168 {
1169 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1170 	struct psycho_softc *sc = pp->pp_sc;
1171 
1172 	return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1173 }
1174 
1175 void
1176 psycho_dmamem_unmap(t, kva, size)
1177 	bus_dma_tag_t t;
1178 	caddr_t kva;
1179 	size_t size;
1180 {
1181 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1182 	struct psycho_softc *sc = pp->pp_sc;
1183 
1184 	iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1185 }
1186