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