xref: /netbsd-src/sys/arch/sparc64/dev/psycho.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: psycho.c,v 1.39 2001/10/07 20:30:41 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 
420 		/*
421 		 * Establish handlers for interesting interrupts....
422 		 *
423 		 * XXX We need to remember these and remove this to support
424 		 * hotplug on the UPA/FHC bus.
425 		 *
426 		 * XXX Not all controllers have these, but installing them
427 		 * is better than trying to sort through this mess.
428 		 */
429 		psycho_set_intr(sc, 15, psycho_ue,
430 			&sc->sc_regs->ue_int_map,
431 			&sc->sc_regs->ue_clr_int);
432 		psycho_set_intr(sc, 1, psycho_ce,
433 			&sc->sc_regs->ce_int_map,
434 			&sc->sc_regs->ce_clr_int);
435 		psycho_set_intr(sc, 15, psycho_bus_a,
436 			&sc->sc_regs->pciaerr_int_map,
437 			&sc->sc_regs->pciaerr_clr_int);
438 		psycho_set_intr(sc, 15, psycho_bus_b,
439 			&sc->sc_regs->pciberr_int_map,
440 			&sc->sc_regs->pciberr_clr_int);
441 		psycho_set_intr(sc, 15, psycho_powerfail,
442 			&sc->sc_regs->power_int_map,
443 			&sc->sc_regs->power_clr_int);
444 		psycho_set_intr(sc, 1, psycho_wakeup,
445 			&sc->sc_regs->pwrmgt_int_map,
446 			&sc->sc_regs->pwrmgt_clr_int);
447 
448 		/*
449 		 * Setup IOMMU and PCI configuration if we're the first
450 		 * of a pair of psycho's to arrive here.
451 		 *
452 		 * We should calculate a TSB size based on amount of RAM
453 		 * and number of bus controllers and number an type of
454 		 * child devices.
455 		 *
456 		 * For the moment, 32KB should be more than enough.
457 		 */
458 		sc->sc_is = malloc(sizeof(struct iommu_state),
459 			M_DEVBUF, M_NOWAIT);
460 		if (sc->sc_is == NULL)
461 			panic("psycho_attach: malloc iommu_state");
462 
463 
464 		sc->sc_is->is_sb[0] = 0;
465 		sc->sc_is->is_sb[1] = 0;
466 		if (PROM_getproplen(sc->sc_node, "no-streaming-cache") >= 0)
467 			sc->sc_is->is_sb[0] = &pci_ctl->pci_strbuf;
468 
469 		psycho_iommu_init(sc, 2);
470 
471 		sc->sc_configtag = psycho_alloc_config_tag(sc->sc_psycho_this);
472 		if (bus_space_map2(sc->sc_bustag,
473 				  PCI_CONFIG_BUS_SPACE,
474 				  sc->sc_basepaddr + 0x01000000,
475 				  0x0100000,
476 				  0,
477 				  0,
478 				  &bh))
479 			panic("could not map psycho PCI configuration space");
480 		sc->sc_configaddr = (off_t)bh;
481 	} else {
482 		/* Just copy IOMMU state, config tag and address */
483 		sc->sc_is = osc->sc_is;
484 		sc->sc_configtag = osc->sc_configtag;
485 		sc->sc_configaddr = osc->sc_configaddr;
486 
487 		if (PROM_getproplen(sc->sc_node, "no-streaming-cache") >= 0)
488 			sc->sc_is->is_sb[1] = &pci_ctl->pci_strbuf;
489 		iommu_reset(sc->sc_is);
490 	}
491 
492 	/*
493 	 * attach the pci.. note we pass PCI A tags, etc., for the sabre here.
494 	 */
495 	pba.pba_busname = "pci";
496 	pba.pba_flags = sc->sc_psycho_this->pp_flags;
497 	pba.pba_dmat = sc->sc_psycho_this->pp_dmat;
498 	pba.pba_iot = sc->sc_psycho_this->pp_iot;
499 	pba.pba_memt = sc->sc_psycho_this->pp_memt;
500 
501 	config_found(self, &pba, psycho_print);
502 }
503 
504 static	int
505 psycho_print(aux, p)
506 	void *aux;
507 	const char *p;
508 {
509 
510 	if (p == NULL)
511 		return (UNCONF);
512 	return (QUIET);
513 }
514 
515 static void
516 psycho_set_intr(sc, ipl, handler, mapper, clearer)
517 	struct psycho_softc *sc;
518 	int ipl;
519 	void *handler;
520 	u_int64_t *mapper;
521 	u_int64_t *clearer;
522 {
523 	struct intrhand *ih;
524 
525 	ih = (struct intrhand *)malloc(sizeof(struct intrhand),
526 		M_DEVBUF, M_NOWAIT);
527 	ih->ih_arg = sc;
528 	ih->ih_map = mapper;
529 	ih->ih_clr = clearer;
530 	ih->ih_fun = handler;
531 	ih->ih_pil = (1<<ipl);
532 	ih->ih_number = INTVEC(*(ih->ih_map));
533 	intr_establish(ipl, ih);
534 	*(ih->ih_map) |= INTMAP_V;
535 }
536 
537 /*
538  * PCI bus support
539  */
540 
541 /*
542  * allocate a PCI chipset tag and set it's cookie.
543  */
544 static pci_chipset_tag_t
545 psycho_alloc_chipset(pp, node, pc)
546 	struct psycho_pbm *pp;
547 	int node;
548 	pci_chipset_tag_t pc;
549 {
550 	pci_chipset_tag_t npc;
551 
552 	npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
553 	if (npc == NULL)
554 		panic("could not allocate pci_chipset_tag_t");
555 	memcpy(npc, pc, sizeof *pc);
556 	npc->cookie = pp;
557 	npc->rootnode = node;
558 	npc->curnode = node;
559 
560 	return (npc);
561 }
562 
563 /*
564  * grovel the OBP for various psycho properties
565  */
566 static void
567 psycho_get_bus_range(node, brp)
568 	int node;
569 	int *brp;
570 {
571 	int n;
572 
573 	if (PROM_getprop(node, "bus-range", sizeof(*brp), &n, (void **)&brp))
574 		panic("could not get psycho bus-range");
575 	if (n != 2)
576 		panic("broken psycho bus-range");
577 	DPRINTF(PDB_PROM, ("psycho debug: got `bus-range' for node %08x: %u - %u\n", node, brp[0], brp[1]));
578 }
579 
580 static void
581 psycho_get_ranges(node, rp, np)
582 	int node;
583 	struct psycho_ranges **rp;
584 	int *np;
585 {
586 
587 	if (PROM_getprop(node, "ranges", sizeof(**rp), np, (void **)rp))
588 		panic("could not get psycho ranges");
589 	DPRINTF(PDB_PROM, ("psycho debug: got `ranges' for node %08x: %d entries\n", node, *np));
590 }
591 
592 /*
593  * Interrupt handlers.
594  */
595 
596 static int
597 psycho_ue(arg)
598 	void *arg;
599 {
600 	struct psycho_softc *sc = (struct psycho_softc *)arg;
601 	struct psychoreg *regs = sc->sc_regs;
602 	long long afsr = regs->psy_ue_afsr;
603 	long long afar = regs->psy_ue_afar;
604 	char bits[128];
605 
606 	/*
607 	 * It's uncorrectable.  Dump the regs and panic.
608 	 */
609 	panic("%s: uncorrectable DMA error AFAR %llx pa %llx AFSR %llx:\n%s",
610 		sc->sc_dev.dv_xname, afar,
611 		(long long)iommu_extract(sc->sc_is, (vaddr_t)afar), afsr,
612 		bitmask_snprintf(afsr, PSYCHO_UE_AFSR_BITS,
613 			bits, sizeof(bits)));
614 	return (1);
615 }
616 static int
617 psycho_ce(arg)
618 	void *arg;
619 {
620 	struct psycho_softc *sc = (struct psycho_softc *)arg;
621 	struct psychoreg *regs = sc->sc_regs;
622 
623 	/*
624 	 * It's correctable.  Dump the regs and continue.
625 	 */
626 
627 	printf("%s: correctable DMA error AFAR %llx AFSR %llx\n",
628 		sc->sc_dev.dv_xname,
629 		(long long)regs->psy_ce_afar, (long long)regs->psy_ce_afsr);
630 	return (1);
631 }
632 static int
633 psycho_bus_a(arg)
634 	void *arg;
635 {
636 	struct psycho_softc *sc = (struct psycho_softc *)arg;
637 	struct psychoreg *regs = sc->sc_regs;
638 
639 	/*
640 	 * It's uncorrectable.  Dump the regs and panic.
641 	 */
642 
643 	panic("%s: PCI bus A error AFAR %llx AFSR %llx\n",
644 		sc->sc_dev.dv_xname,
645 		(long long)regs->psy_pcictl[0].pci_afar,
646 		(long long)regs->psy_pcictl[0].pci_afsr);
647 	return (1);
648 }
649 static int
650 psycho_bus_b(arg)
651 	void *arg;
652 {
653 	struct psycho_softc *sc = (struct psycho_softc *)arg;
654 	struct psychoreg *regs = sc->sc_regs;
655 
656 	/*
657 	 * It's uncorrectable.  Dump the regs and panic.
658 	 */
659 
660 	panic("%s: PCI bus B error AFAR %llx AFSR %llx\n",
661 		sc->sc_dev.dv_xname,
662 		(long long)regs->psy_pcictl[0].pci_afar,
663 		(long long)regs->psy_pcictl[0].pci_afsr);
664 	return (1);
665 }
666 static int
667 psycho_powerfail(arg)
668 	void *arg;
669 {
670 
671 	/*
672 	 * We lost power.  Try to shut down NOW.
673 	 */
674 	printf("Power Failure Detected: Shutting down NOW.\n");
675 	cpu_reboot(RB_POWERDOWN|RB_HALT, NULL);
676 	return (1);
677 }
678 static
679 int psycho_wakeup(arg)
680 	void *arg;
681 {
682 	struct psycho_softc *sc = (struct psycho_softc *)arg;
683 
684 	/*
685 	 * Gee, we don't really have a framework to deal with this
686 	 * properly.
687 	 */
688 	printf("%s: power management wakeup\n",	sc->sc_dev.dv_xname);
689 	return (1);
690 }
691 
692 
693 
694 /*
695  * initialise the IOMMU..
696  */
697 void
698 psycho_iommu_init(sc, tsbsize)
699 	struct psycho_softc *sc;
700 	int tsbsize;
701 {
702 	char *name;
703 	struct iommu_state *is = sc->sc_is;
704 	u_int32_t iobase = -1;
705 	int *vdma = NULL;
706 	int nitem;
707 
708 	/* punch in our copies */
709 	is->is_bustag = sc->sc_bustag;
710 	is->is_iommu = &sc->sc_regs->psy_iommu;
711 
712 	/*
713 	 * Separate the men from the boys.  Get the `virtual-dma'
714 	 * property for sabre and use that to make sure the damn
715 	 * iommu works.
716 	 *
717 	 * We could query the `#virtual-dma-size-cells' and
718 	 * `#virtual-dma-addr-cells' and DTRT, but I'm lazy.
719 	 */
720 	if (!PROM_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
721 		(void **)&vdma)) {
722 		/* Damn.  Gotta use these values. */
723 		iobase = vdma[0];
724 #define	TSBCASE(x)	case 1<<((x)+23): tsbsize = (x); break
725 		switch (vdma[1]) {
726 			TSBCASE(1); TSBCASE(2); TSBCASE(3);
727 			TSBCASE(4); TSBCASE(5); TSBCASE(6);
728 		default:
729 			printf("bogus tsb size %x, using 7\n", vdma[1]);
730 			TSBCASE(7);
731 		}
732 #undef TSBCASE
733 	}
734 
735 	/* give us a nice name.. */
736 	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
737 	if (name == 0)
738 		panic("couldn't malloc iommu name");
739 	snprintf(name, 32, "%s dvma", sc->sc_dev.dv_xname);
740 
741 	iommu_init(name, is, tsbsize, iobase);
742 }
743 
744 /*
745  * below here is bus space and bus dma support
746  */
747 bus_space_tag_t
748 psycho_alloc_bus_tag(pp, type)
749 	struct psycho_pbm *pp;
750 	int type;
751 {
752 	struct psycho_softc *sc = pp->pp_sc;
753 	bus_space_tag_t bt;
754 
755 	bt = (bus_space_tag_t)
756 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
757 	if (bt == NULL)
758 		panic("could not allocate psycho bus tag");
759 
760 	bzero(bt, sizeof *bt);
761 	bt->cookie = pp;
762 	bt->parent = sc->sc_bustag;
763 	bt->type = type;
764 	bt->sparc_bus_map = _psycho_bus_map;
765 	bt->sparc_bus_mmap = psycho_bus_mmap;
766 	bt->sparc_intr_establish = psycho_intr_establish;
767 	return (bt);
768 }
769 
770 bus_dma_tag_t
771 psycho_alloc_dma_tag(pp)
772 	struct psycho_pbm *pp;
773 {
774 	struct psycho_softc *sc = pp->pp_sc;
775 	bus_dma_tag_t dt, pdt = sc->sc_dmatag;
776 
777 	dt = (bus_dma_tag_t)
778 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
779 	if (dt == NULL)
780 		panic("could not allocate psycho dma tag");
781 
782 	bzero(dt, sizeof *dt);
783 	dt->_cookie = pp;
784 	dt->_parent = pdt;
785 #define PCOPY(x)	dt->x = pdt->x
786 	PCOPY(_dmamap_create);
787 	PCOPY(_dmamap_destroy);
788 	dt->_dmamap_load = psycho_dmamap_load;
789 	PCOPY(_dmamap_load_mbuf);
790 	PCOPY(_dmamap_load_uio);
791 	dt->_dmamap_load_raw = psycho_dmamap_load_raw;
792 	dt->_dmamap_unload = psycho_dmamap_unload;
793 	dt->_dmamap_sync = psycho_dmamap_sync;
794 	dt->_dmamem_alloc = psycho_dmamem_alloc;
795 	dt->_dmamem_free = psycho_dmamem_free;
796 	dt->_dmamem_map = psycho_dmamem_map;
797 	dt->_dmamem_unmap = psycho_dmamem_unmap;
798 	PCOPY(_dmamem_mmap);
799 #undef	PCOPY
800 	return (dt);
801 }
802 
803 /*
804  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion about
805  * PCI physical addresses.
806  */
807 
808 static int get_childspace __P((int));
809 
810 static int
811 get_childspace(type)
812 	int type;
813 {
814 	int ss;
815 
816 	switch (type) {
817 	case PCI_CONFIG_BUS_SPACE:
818 		ss = 0x00;
819 		break;
820 	case PCI_IO_BUS_SPACE:
821 		ss = 0x01;
822 		break;
823 	case PCI_MEMORY_BUS_SPACE:
824 		ss = 0x02;
825 		break;
826 #if 0
827 	/* we don't do 64 bit memory space */
828 	case PCI_MEMORY64_BUS_SPACE:
829 		ss = 0x03;
830 		break;
831 #endif
832 	default:
833 		panic("get_childspace: unknown bus type");
834 	}
835 
836 	return (ss);
837 }
838 
839 static int
840 _psycho_bus_map(t, btype, offset, size, flags, vaddr, hp)
841 	bus_space_tag_t t;
842 	bus_type_t btype;
843 	bus_addr_t offset;
844 	bus_size_t size;
845 	int	flags;
846 	vaddr_t vaddr;
847 	bus_space_handle_t *hp;
848 {
849 	struct psycho_pbm *pp = t->cookie;
850 	struct psycho_softc *sc = pp->pp_sc;
851 	int i, ss;
852 
853 	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,
854 	    (void *)vaddr));
855 
856 	ss = get_childspace(t->type);
857 	DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
858 
859 	for (i = 0; i < pp->pp_nrange; i++) {
860 		bus_addr_t paddr;
861 
862 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
863 			continue;
864 
865 		paddr = pp->pp_range[i].phys_lo + offset;
866 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
867 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_map: mapping paddr space %lx offset %lx paddr %qx\n",
868 			       (long)ss, (long)offset,
869 			       (unsigned long long)paddr));
870 		return (bus_space_map2(sc->sc_bustag, t->type, paddr,
871 					size, flags, vaddr, hp));
872 	}
873 	DPRINTF(PDB_BUSMAP, (" FAILED\n"));
874 	return (EINVAL);
875 }
876 
877 static paddr_t
878 psycho_bus_mmap(t, paddr, off, prot, flags)
879 	bus_space_tag_t t;
880 	bus_addr_t paddr;
881 	off_t off;
882 	int prot;
883 	int flags;
884 {
885 	bus_addr_t offset = paddr;
886 	struct psycho_pbm *pp = t->cookie;
887 	struct psycho_softc *sc = pp->pp_sc;
888 	int i, ss;
889 
890 	ss = get_childspace(t->type);
891 
892 	DPRINTF(PDB_BUSMAP, ("_psycho_bus_mmap: prot %x flags %d pa %qx\n",
893 		prot, flags, (unsigned long long)paddr));
894 
895 	for (i = 0; i < pp->pp_nrange; i++) {
896 		bus_addr_t paddr;
897 
898 		if (((pp->pp_range[i].cspace >> 24) & 0x03) != ss)
899 			continue;
900 
901 		paddr = pp->pp_range[i].phys_lo + offset;
902 		paddr |= ((bus_addr_t)pp->pp_range[i].phys_hi<<32);
903 		DPRINTF(PDB_BUSMAP, ("\n_psycho_bus_mmap: mapping paddr "
904 			"space %lx offset %lx paddr %qx\n",
905 			       (long)ss, (long)offset,
906 			       (unsigned long long)paddr));
907 		return (bus_space_mmap(sc->sc_bustag, paddr, off,
908 				       prot, flags));
909 	}
910 
911 	return (-1);
912 }
913 
914 
915 /*
916  * install an interrupt handler for a PCI device
917  */
918 void *
919 psycho_intr_establish(t, ihandle, level, flags, handler, arg)
920 	bus_space_tag_t t;
921 	int ihandle;
922 	int level;
923 	int flags;
924 	int (*handler) __P((void *));
925 	void *arg;
926 {
927 	struct psycho_pbm *pp = t->cookie;
928 	struct psycho_softc *sc = pp->pp_sc;
929 	struct intrhand *ih;
930 	volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
931 	int64_t intrmap = 0;
932 	int ino;
933 	long vec = INTVEC(ihandle);
934 
935 	ih = (struct intrhand *)
936 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
937 	if (ih == NULL)
938 		return (NULL);
939 
940 	/*
941 	 * Hunt through all the interrupt mapping regs to look for our
942 	 * interrupt vector.
943 	 *
944 	 * XXX We only compare INOs rather than IGNs since the firmware may
945 	 * not provide the IGN and the IGN is constant for all device on that
946 	 * PCI controller.  This could cause problems for the FFB/external
947 	 * interrupt which has a full vector that can be set arbitrarily.
948 	 */
949 
950 
951 	DPRINTF(PDB_INTR, ("\npsycho_intr_establish: ihandle %x vec %lx", ihandle, vec));
952 	ino = INTINO(vec);
953 	DPRINTF(PDB_INTR, (" ino %x", ino));
954 
955 	/* If the device didn't ask for an IPL, use the one encoded. */
956 	if (level == IPL_NONE) level = INTLEV(vec);
957 	/* If it still has no level, print a warning and assign IPL 2 */
958 	if (level == IPL_NONE) {
959 		printf("ERROR: no IPL, setting IPL 2.\n");
960 		level = 2;
961 	}
962 
963 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
964 
965 		DPRINTF(PDB_INTR, ("\npsycho: intr %lx: %p\nHunting for IRQ...\n",
966 		    (long)ino, intrlev[ino]));
967 
968 		/* Hunt thru obio first */
969 		for (intrmapptr = &sc->sc_regs->scsi_int_map,
970 			     intrclrptr = &sc->sc_regs->scsi_clr_int;
971 		     intrmapptr <= &sc->sc_regs->ffb1_int_map;
972 		     intrmapptr++, intrclrptr++) {
973 			if (INTINO(*intrmapptr) == ino)
974 				goto found;
975 		}
976 
977 		/* Now do PCI interrupts */
978 		for (intrmapptr = &sc->sc_regs->pcia_slot0_int,
979 			     intrclrptr = &sc->sc_regs->pcia0_clr_int[0];
980 		     intrmapptr <= &sc->sc_regs->pcib_slot3_int;
981 		     intrmapptr++, intrclrptr += 4) {
982 			if (((*intrmapptr ^ vec) & 0x3c) == 0) {
983 				intrclrptr += vec & 0x3;
984 				goto found;
985 			}
986 		}
987 		printf("Cannot find interrupt vector %lx\n", vec);
988 		return (NULL);
989 
990 	found:
991 		/* Register the map and clear intr registers */
992 		ih->ih_map = intrmapptr;
993 		ih->ih_clr = intrclrptr;
994 	}
995 #ifdef NOT_DEBUG
996 	if (psycho_debug & PDB_INTR) {
997 		long i;
998 
999 		for (i = 0; i < 500000000; i++)
1000 			continue;
1001 	}
1002 #endif
1003 
1004 	ih->ih_fun = handler;
1005 	ih->ih_arg = arg;
1006 	ih->ih_pil = level;
1007 	ih->ih_number = ino | sc->sc_ign;
1008 
1009 	DPRINTF(PDB_INTR, (
1010 	    "; installing handler %p arg %p with ino %u pil %u\n",
1011 	    handler, arg, (u_int)ino, (u_int)ih->ih_pil));
1012 
1013 	intr_establish(ih->ih_pil, ih);
1014 
1015 	/*
1016 	 * Enable the interrupt now we have the handler installed.
1017 	 * Read the current value as we can't change it besides the
1018 	 * valid bit so so make sure only this bit is changed.
1019 	 *
1020 	 * XXXX --- we really should use bus_space for this.
1021 	 */
1022 	if (intrmapptr) {
1023 		intrmap = *intrmapptr;
1024 		DPRINTF(PDB_INTR, ("; read intrmap = %016qx",
1025 			(unsigned long long)intrmap));
1026 
1027 		/* Enable the interrupt */
1028 		intrmap |= INTMAP_V;
1029 		DPRINTF(PDB_INTR, ("; addr of intrmapptr = %p", intrmapptr));
1030 		DPRINTF(PDB_INTR, ("; writing intrmap = %016qx\n",
1031 			(unsigned long long)intrmap));
1032 		*intrmapptr = intrmap;
1033 		DPRINTF(PDB_INTR, ("; reread intrmap = %016qx",
1034 			(unsigned long long)(intrmap = *intrmapptr)));
1035 	}
1036 	return (ih);
1037 }
1038 
1039 /*
1040  * hooks into the iommu dvma calls.
1041  */
1042 int
1043 psycho_dmamap_load(t, map, buf, buflen, p, flags)
1044 	bus_dma_tag_t t;
1045 	bus_dmamap_t map;
1046 	void *buf;
1047 	bus_size_t buflen;
1048 	struct proc *p;
1049 	int flags;
1050 {
1051 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1052 	struct psycho_softc *sc = pp->pp_sc;
1053 
1054 	return (iommu_dvmamap_load(t, sc->sc_is, map, buf, buflen, p, flags));
1055 }
1056 
1057 void
1058 psycho_dmamap_unload(t, map)
1059 	bus_dma_tag_t t;
1060 	bus_dmamap_t map;
1061 {
1062 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1063 	struct psycho_softc *sc = pp->pp_sc;
1064 
1065 	iommu_dvmamap_unload(t, sc->sc_is, map);
1066 }
1067 
1068 int
1069 psycho_dmamap_load_raw(t, map, segs, nsegs, size, flags)
1070 	bus_dma_tag_t t;
1071 	bus_dmamap_t map;
1072 	bus_dma_segment_t *segs;
1073 	int nsegs;
1074 	bus_size_t size;
1075 	int flags;
1076 {
1077 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1078 	struct psycho_softc *sc = pp->pp_sc;
1079 
1080 	return (iommu_dvmamap_load_raw(t, sc->sc_is, map, segs, nsegs, flags, size));
1081 }
1082 
1083 void
1084 psycho_dmamap_sync(t, map, offset, len, ops)
1085 	bus_dma_tag_t t;
1086 	bus_dmamap_t map;
1087 	bus_addr_t offset;
1088 	bus_size_t len;
1089 	int ops;
1090 {
1091 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1092 	struct psycho_softc *sc = pp->pp_sc;
1093 
1094 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) {
1095 		/* Flush the CPU then the IOMMU */
1096 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
1097 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1098 	}
1099 	if (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) {
1100 		/* Flush the IOMMU then the CPU */
1101 		iommu_dvmamap_sync(t, sc->sc_is, map, offset, len, ops);
1102 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
1103 	}
1104 
1105 }
1106 
1107 int
1108 psycho_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
1109 	bus_dma_tag_t t;
1110 	bus_size_t size;
1111 	bus_size_t alignment;
1112 	bus_size_t boundary;
1113 	bus_dma_segment_t *segs;
1114 	int nsegs;
1115 	int *rsegs;
1116 	int flags;
1117 {
1118 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1119 	struct psycho_softc *sc = pp->pp_sc;
1120 
1121 	return (iommu_dvmamem_alloc(t, sc->sc_is, size, alignment, boundary,
1122 	    segs, nsegs, rsegs, flags));
1123 }
1124 
1125 void
1126 psycho_dmamem_free(t, segs, nsegs)
1127 	bus_dma_tag_t t;
1128 	bus_dma_segment_t *segs;
1129 	int nsegs;
1130 {
1131 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1132 	struct psycho_softc *sc = pp->pp_sc;
1133 
1134 	iommu_dvmamem_free(t, sc->sc_is, segs, nsegs);
1135 }
1136 
1137 int
1138 psycho_dmamem_map(t, segs, nsegs, size, kvap, flags)
1139 	bus_dma_tag_t t;
1140 	bus_dma_segment_t *segs;
1141 	int nsegs;
1142 	size_t size;
1143 	caddr_t *kvap;
1144 	int flags;
1145 {
1146 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1147 	struct psycho_softc *sc = pp->pp_sc;
1148 
1149 	return (iommu_dvmamem_map(t, sc->sc_is, segs, nsegs, size, kvap, flags));
1150 }
1151 
1152 void
1153 psycho_dmamem_unmap(t, kva, size)
1154 	bus_dma_tag_t t;
1155 	caddr_t kva;
1156 	size_t size;
1157 {
1158 	struct psycho_pbm *pp = (struct psycho_pbm *)t->_cookie;
1159 	struct psycho_softc *sc = pp->pp_sc;
1160 
1161 	iommu_dvmamem_unmap(t, sc->sc_is, kva, size);
1162 }
1163