pyro.c revision 1.4.2.3 1 1.4.2.2 rmind /* $NetBSD: pyro.c,v 1.4.2.3 2011/05/31 03:04:18 rmind Exp $ */
2 1.4.2.2 rmind /* from: $OpenBSD: pyro.c,v 1.20 2010/12/05 15:15:14 kettenis Exp $ */
3 1.4.2.2 rmind
4 1.4.2.2 rmind /*
5 1.4.2.2 rmind * Copyright (c) 2002 Jason L. Wright (jason (at) thought.net)
6 1.4.2.2 rmind * Copyright (c) 2003 Henric Jungheim
7 1.4.2.2 rmind * Copyright (c) 2007 Mark Kettenis
8 1.4.2.2 rmind * Copyright (c) 2011 Matthew R. Green
9 1.4.2.2 rmind * All rights reserved.
10 1.4.2.2 rmind *
11 1.4.2.2 rmind * Redistribution and use in source and binary forms, with or without
12 1.4.2.2 rmind * modification, are permitted provided that the following conditions
13 1.4.2.2 rmind * are met:
14 1.4.2.2 rmind * 1. Redistributions of source code must retain the above copyright
15 1.4.2.2 rmind * notice, this list of conditions and the following disclaimer.
16 1.4.2.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
17 1.4.2.2 rmind * notice, this list of conditions and the following disclaimer in the
18 1.4.2.2 rmind * documentation and/or other materials provided with the distribution.
19 1.4.2.2 rmind *
20 1.4.2.2 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.4.2.2 rmind * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 1.4.2.2 rmind * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 1.4.2.2 rmind * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 1.4.2.2 rmind * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.4.2.2 rmind * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 1.4.2.2 rmind * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.4.2.2 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 1.4.2.2 rmind * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 1.4.2.2 rmind * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.4.2.2 rmind * POSSIBILITY OF SUCH DAMAGE.
31 1.4.2.2 rmind */
32 1.4.2.2 rmind
33 1.4.2.2 rmind #include <sys/param.h>
34 1.4.2.2 rmind #include <sys/device.h>
35 1.4.2.2 rmind #include <sys/errno.h>
36 1.4.2.2 rmind #include <sys/malloc.h>
37 1.4.2.2 rmind #include <sys/systm.h>
38 1.4.2.2 rmind
39 1.4.2.2 rmind #define _SPARC_BUS_DMA_PRIVATE
40 1.4.2.2 rmind #include <machine/bus.h>
41 1.4.2.2 rmind #include <machine/autoconf.h>
42 1.4.2.2 rmind
43 1.4.2.2 rmind #ifdef DDB
44 1.4.2.2 rmind #include <machine/db_machdep.h>
45 1.4.2.2 rmind #endif
46 1.4.2.2 rmind
47 1.4.2.2 rmind #include <dev/pci/pcivar.h>
48 1.4.2.2 rmind #include <dev/pci/pcireg.h>
49 1.4.2.2 rmind
50 1.4.2.2 rmind #include <sparc64/dev/iommureg.h>
51 1.4.2.2 rmind #include <sparc64/dev/iommuvar.h>
52 1.4.2.2 rmind #include <sparc64/dev/pyrovar.h>
53 1.4.2.2 rmind
54 1.4.2.2 rmind #ifdef DEBUG
55 1.4.2.2 rmind #define PDB_PROM 0x01
56 1.4.2.2 rmind #define PDB_BUSMAP 0x02
57 1.4.2.2 rmind #define PDB_INTR 0x04
58 1.4.2.2 rmind #define PDB_CONF 0x08
59 1.4.2.2 rmind int pyro_debug = 0x4;
60 1.4.2.2 rmind #define DPRINTF(l, s) do { if (pyro_debug & l) printf s; } while (0)
61 1.4.2.2 rmind #else
62 1.4.2.2 rmind #define DPRINTF(l, s)
63 1.4.2.2 rmind #endif
64 1.4.2.2 rmind
65 1.4.2.2 rmind #define FIRE_RESET_GEN 0x7010
66 1.4.2.2 rmind
67 1.4.2.2 rmind #define FIRE_RESET_GEN_XIR 0x0000000000000002L
68 1.4.2.2 rmind
69 1.4.2.2 rmind #define FIRE_INTRMAP_INT_CNTRL_NUM_MASK 0x000003c0
70 1.4.2.2 rmind #define FIRE_INTRMAP_INT_CNTRL_NUM0 0x00000040
71 1.4.2.2 rmind #define FIRE_INTRMAP_INT_CNTRL_NUM1 0x00000080
72 1.4.2.2 rmind #define FIRE_INTRMAP_INT_CNTRL_NUM2 0x00000100
73 1.4.2.2 rmind #define FIRE_INTRMAP_INT_CNTRL_NUM3 0x00000200
74 1.4.2.2 rmind #define FIRE_INTRMAP_T_JPID_SHIFT 26
75 1.4.2.2 rmind #define FIRE_INTRMAP_T_JPID_MASK 0x7c000000
76 1.4.2.2 rmind
77 1.4.2.2 rmind #define OBERON_INTRMAP_T_DESTID_SHIFT 21
78 1.4.2.2 rmind #define OBERON_INTRMAP_T_DESTID_MASK 0x7fe00000
79 1.4.2.2 rmind
80 1.4.2.2 rmind extern struct sparc_pci_chipset _sparc_pci_chipset;
81 1.4.2.2 rmind
82 1.4.2.2 rmind int pyro_match(struct device *, struct cfdata *, void *);
83 1.4.2.2 rmind void pyro_attach(struct device *, struct device *, void *);
84 1.4.2.2 rmind int pyro_print(void *, const char *);
85 1.4.2.2 rmind
86 1.4.2.2 rmind CFATTACH_DECL(pyro, sizeof(struct pyro_softc),
87 1.4.2.2 rmind pyro_match, pyro_attach, NULL, NULL);
88 1.4.2.2 rmind
89 1.4.2.2 rmind void pyro_init(struct pyro_softc *, int);
90 1.4.2.2 rmind void pyro_init_iommu(struct pyro_softc *, struct pyro_pbm *);
91 1.4.2.2 rmind
92 1.4.2.2 rmind pci_chipset_tag_t pyro_alloc_chipset(struct pyro_pbm *, int,
93 1.4.2.2 rmind pci_chipset_tag_t);
94 1.4.2.2 rmind bus_space_tag_t pyro_alloc_mem_tag(struct pyro_pbm *);
95 1.4.2.2 rmind bus_space_tag_t pyro_alloc_io_tag(struct pyro_pbm *);
96 1.4.2.2 rmind bus_space_tag_t pyro_alloc_config_tag(struct pyro_pbm *);
97 1.4.2.2 rmind bus_space_tag_t pyro_alloc_bus_tag(struct pyro_pbm *, const char *, int);
98 1.4.2.2 rmind bus_dma_tag_t pyro_alloc_dma_tag(struct pyro_pbm *);
99 1.4.2.2 rmind
100 1.4.2.2 rmind #if 0
101 1.4.2.2 rmind int pyro_conf_size(pci_chipset_tag_t, pcitag_t);
102 1.4.2.2 rmind #endif
103 1.4.2.2 rmind pcireg_t pyro_conf_read(pci_chipset_tag_t, pcitag_t, int);
104 1.4.2.2 rmind void pyro_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
105 1.4.2.2 rmind
106 1.4.2.2 rmind static void * pyro_pci_intr_establish(pci_chipset_tag_t pc,
107 1.4.2.2 rmind pci_intr_handle_t ih, int level,
108 1.4.2.2 rmind int (*func)(void *), void *arg);
109 1.4.2.2 rmind
110 1.4.2.2 rmind int pyro_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
111 1.4.2.2 rmind int pyro_bus_map(bus_space_tag_t, bus_addr_t,
112 1.4.2.2 rmind bus_size_t, int, vaddr_t, bus_space_handle_t *);
113 1.4.2.2 rmind paddr_t pyro_bus_mmap(bus_space_tag_t, bus_addr_t, off_t,
114 1.4.2.2 rmind int, int);
115 1.4.2.2 rmind void *pyro_intr_establish(bus_space_tag_t, int, int,
116 1.4.2.2 rmind int (*)(void *), void *, void (*)(void));
117 1.4.2.2 rmind
118 1.4.2.2 rmind int pyro_dmamap_create(bus_dma_tag_t, bus_size_t, int,
119 1.4.2.2 rmind bus_size_t, bus_size_t, int, bus_dmamap_t *);
120 1.4.2.2 rmind
121 1.4.2.2 rmind int
122 1.4.2.2 rmind pyro_match(struct device *parent, struct cfdata *match, void *aux)
123 1.4.2.2 rmind {
124 1.4.2.2 rmind struct mainbus_attach_args *ma = aux;
125 1.4.2.2 rmind char *str;
126 1.4.2.2 rmind
127 1.4.2.2 rmind if (strcmp(ma->ma_name, "pci") != 0)
128 1.4.2.2 rmind return (0);
129 1.4.2.2 rmind
130 1.4.2.2 rmind str = prom_getpropstring(ma->ma_node, "compatible");
131 1.4.2.2 rmind if (strcmp(str, "pciex108e,80f0") == 0 ||
132 1.4.2.2 rmind strcmp(str, "pciex108e,80f8") == 0)
133 1.4.2.2 rmind return (1);
134 1.4.2.2 rmind
135 1.4.2.2 rmind return (0);
136 1.4.2.2 rmind }
137 1.4.2.2 rmind
138 1.4.2.2 rmind void
139 1.4.2.2 rmind pyro_attach(struct device *parent, struct device *self, void *aux)
140 1.4.2.2 rmind {
141 1.4.2.2 rmind struct pyro_softc *sc = (struct pyro_softc *)self;
142 1.4.2.2 rmind struct mainbus_attach_args *ma = aux;
143 1.4.2.2 rmind char *str;
144 1.4.2.2 rmind int busa;
145 1.4.2.2 rmind
146 1.4.2.2 rmind sc->sc_node = ma->ma_node;
147 1.4.2.2 rmind sc->sc_dmat = ma->ma_dmatag;
148 1.4.2.2 rmind sc->sc_bustag = ma->ma_bustag;
149 1.4.2.2 rmind sc->sc_csr = ma->ma_reg[0].ur_paddr;
150 1.4.2.2 rmind sc->sc_xbc = ma->ma_reg[1].ur_paddr;
151 1.4.2.2 rmind sc->sc_ign = INTIGN(ma->ma_upaid << INTMAP_IGN_SHIFT);
152 1.4.2.2 rmind
153 1.4.2.2 rmind if ((ma->ma_reg[0].ur_paddr & 0x00700000) == 0x00600000)
154 1.4.2.2 rmind busa = 1;
155 1.4.2.2 rmind else
156 1.4.2.2 rmind busa = 0;
157 1.4.2.2 rmind
158 1.4.2.2 rmind if (bus_space_map(sc->sc_bustag, sc->sc_csr,
159 1.4.2.2 rmind ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR, &sc->sc_csrh)) {
160 1.4.2.2 rmind printf(": failed to map csr registers\n");
161 1.4.2.2 rmind return;
162 1.4.2.2 rmind }
163 1.4.2.2 rmind
164 1.4.2.2 rmind if (bus_space_map(sc->sc_bustag, sc->sc_xbc,
165 1.4.2.2 rmind ma->ma_reg[1].ur_len, 0, &sc->sc_xbch)) {
166 1.4.2.2 rmind printf(": failed to map xbc registers\n");
167 1.4.2.2 rmind return;
168 1.4.2.2 rmind }
169 1.4.2.2 rmind
170 1.4.2.2 rmind str = prom_getpropstring(ma->ma_node, "compatible");
171 1.4.2.2 rmind if (strcmp(str, "pciex108e,80f8") == 0)
172 1.4.2.2 rmind sc->sc_oberon = 1;
173 1.4.2.2 rmind
174 1.4.2.2 rmind pyro_init(sc, busa);
175 1.4.2.2 rmind }
176 1.4.2.2 rmind
177 1.4.2.2 rmind void
178 1.4.2.2 rmind pyro_init(struct pyro_softc *sc, int busa)
179 1.4.2.2 rmind {
180 1.4.2.2 rmind struct pyro_pbm *pbm;
181 1.4.2.2 rmind struct pcibus_attach_args pba;
182 1.4.2.2 rmind int *busranges = NULL, nranges;
183 1.4.2.2 rmind
184 1.4.2.2 rmind pbm = malloc(sizeof(*pbm), M_DEVBUF, M_NOWAIT | M_ZERO);
185 1.4.2.2 rmind if (pbm == NULL)
186 1.4.2.2 rmind panic("pyro: can't alloc pyro pbm");
187 1.4.2.2 rmind
188 1.4.2.2 rmind pbm->pp_sc = sc;
189 1.4.2.2 rmind pbm->pp_bus_a = busa;
190 1.4.2.2 rmind
191 1.4.2.2 rmind if (prom_getprop(sc->sc_node, "ranges", sizeof(struct pyro_range),
192 1.4.2.2 rmind &pbm->pp_nrange, (void **)&pbm->pp_range))
193 1.4.2.2 rmind panic("pyro: can't get ranges");
194 1.4.2.2 rmind
195 1.4.2.2 rmind if (prom_getprop(sc->sc_node, "bus-range", sizeof(int), &nranges,
196 1.4.2.2 rmind (void **)&busranges))
197 1.4.2.2 rmind panic("pyro: can't get bus-range");
198 1.4.2.2 rmind
199 1.4.2.2 rmind printf(": \"%s\", rev %d, ign %x, bus %c %d to %d\n",
200 1.4.2.2 rmind sc->sc_oberon ? "Oberon" : "Fire",
201 1.4.2.2 rmind prom_getpropint(sc->sc_node, "module-revision#", 0), sc->sc_ign,
202 1.4.2.2 rmind busa ? 'A' : 'B', busranges[0], busranges[1]);
203 1.4.2.2 rmind
204 1.4.2.2 rmind printf("%s: ", sc->sc_dv.dv_xname);
205 1.4.2.2 rmind pyro_init_iommu(sc, pbm);
206 1.4.2.2 rmind
207 1.4.2.2 rmind pbm->pp_memt = pyro_alloc_mem_tag(pbm);
208 1.4.2.2 rmind pbm->pp_iot = pyro_alloc_io_tag(pbm);
209 1.4.2.2 rmind pbm->pp_cfgt = pyro_alloc_config_tag(pbm);
210 1.4.2.2 rmind pbm->pp_dmat = pyro_alloc_dma_tag(pbm);
211 1.4.2.3 rmind pbm->pp_flags = (pbm->pp_memt ? PCI_FLAGS_MEM_OKAY : 0) |
212 1.4.2.3 rmind (pbm->pp_iot ? PCI_FLAGS_IO_OKAY : 0);
213 1.4.2.2 rmind
214 1.4.2.2 rmind if (bus_space_map(pbm->pp_cfgt, 0, 0x10000000, 0, &pbm->pp_cfgh))
215 1.4.2.2 rmind panic("pyro: can't map config space");
216 1.4.2.2 rmind
217 1.4.2.2 rmind pbm->pp_pc = pyro_alloc_chipset(pbm, sc->sc_node, &_sparc_pci_chipset);
218 1.4.2.2 rmind pbm->pp_pc->spc_busmax = busranges[1];
219 1.4.2.2 rmind pbm->pp_pc->spc_busnode = malloc(sizeof(*pbm->pp_pc->spc_busnode),
220 1.4.2.2 rmind M_DEVBUF, M_NOWAIT | M_ZERO);
221 1.4.2.2 rmind if (pbm->pp_pc->spc_busnode == NULL)
222 1.4.2.2 rmind panic("schizo: malloc busnode");
223 1.4.2.2 rmind
224 1.4.2.2 rmind #if 0
225 1.4.2.2 rmind pbm->pp_pc->bustag = pbm->pp_cfgt;
226 1.4.2.2 rmind pbm->pp_pc->bushandle = pbm->pp_cfgh;
227 1.4.2.2 rmind #endif
228 1.4.2.2 rmind
229 1.4.2.2 rmind bzero(&pba, sizeof(pba));
230 1.4.2.2 rmind pba.pba_bus = busranges[0];
231 1.4.2.2 rmind pba.pba_pc = pbm->pp_pc;
232 1.4.2.2 rmind pba.pba_flags = pbm->pp_flags;
233 1.4.2.2 rmind pba.pba_dmat = pbm->pp_dmat;
234 1.4.2.2 rmind pba.pba_dmat64 = NULL; /* XXX */
235 1.4.2.2 rmind pba.pba_memt = pbm->pp_memt;
236 1.4.2.2 rmind pba.pba_iot = pbm->pp_iot;
237 1.4.2.2 rmind
238 1.4.2.2 rmind free(busranges, M_DEVBUF);
239 1.4.2.2 rmind
240 1.4.2.2 rmind config_found(&sc->sc_dv, &pba, pyro_print);
241 1.4.2.2 rmind }
242 1.4.2.2 rmind
243 1.4.2.2 rmind void
244 1.4.2.2 rmind pyro_init_iommu(struct pyro_softc *sc, struct pyro_pbm *pbm)
245 1.4.2.2 rmind {
246 1.4.2.2 rmind struct iommu_state *is = &pbm->pp_is;
247 1.4.2.2 rmind int tsbsize = 7;
248 1.4.2.2 rmind u_int32_t iobase = -1;
249 1.4.2.2 rmind char *name;
250 1.4.2.2 rmind
251 1.4.2.2 rmind pbm->pp_sb.sb_is = is;
252 1.4.2.2 rmind is->is_bustag = sc->sc_bustag;
253 1.4.2.2 rmind
254 1.4.2.2 rmind if (bus_space_subregion(is->is_bustag, sc->sc_csrh,
255 1.4.2.2 rmind 0x40000, 0x100, &is->is_iommu)) {
256 1.4.2.2 rmind panic("pyro: unable to create iommu handle");
257 1.4.2.2 rmind }
258 1.4.2.2 rmind
259 1.4.2.2 rmind /* We have no STC. */
260 1.4.2.2 rmind is->is_sb[0] = NULL;
261 1.4.2.2 rmind
262 1.4.2.2 rmind name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
263 1.4.2.2 rmind if (name == NULL)
264 1.4.2.2 rmind panic("couldn't malloc iommu name");
265 1.4.2.2 rmind snprintf(name, 32, "%s dvma", sc->sc_dv.dv_xname);
266 1.4.2.2 rmind
267 1.4.2.2 rmind /* Tell iommu how to set the TSB size. */
268 1.4.2.2 rmind is->is_flags = IOMMU_TSBSIZE_IN_PTSB;
269 1.4.2.2 rmind
270 1.4.2.2 rmind /* On Oberon, we need to flush the cache. */
271 1.4.2.2 rmind if (sc->sc_oberon)
272 1.4.2.2 rmind is->is_flags |= IOMMU_FLUSH_CACHE;
273 1.4.2.2 rmind
274 1.4.2.2 rmind iommu_init(name, is, tsbsize, iobase);
275 1.4.2.2 rmind }
276 1.4.2.2 rmind
277 1.4.2.2 rmind int
278 1.4.2.2 rmind pyro_print(void *aux, const char *p)
279 1.4.2.2 rmind {
280 1.4.2.2 rmind if (p == NULL)
281 1.4.2.2 rmind return (UNCONF);
282 1.4.2.2 rmind return (QUIET);
283 1.4.2.2 rmind }
284 1.4.2.2 rmind
285 1.4.2.2 rmind pcireg_t
286 1.4.2.2 rmind pyro_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
287 1.4.2.2 rmind {
288 1.4.2.2 rmind struct pyro_pbm *pp = pc->cookie;
289 1.4.2.2 rmind pcireg_t val = (pcireg_t)~0;
290 1.4.2.2 rmind
291 1.4.2.2 rmind DPRINTF(PDB_CONF, ("%s: tag %lx reg %x ", __func__, (long)tag, reg));
292 1.4.2.2 rmind if (PCITAG_NODE(tag) != -1)
293 1.4.2.2 rmind val = bus_space_read_4(pp->pp_cfgt, pp->pp_cfgh,
294 1.4.2.2 rmind (PCITAG_OFFSET(tag) << 4) + reg);
295 1.4.2.2 rmind DPRINTF(PDB_CONF, (" returning %08x\n", (u_int)val));
296 1.4.2.2 rmind return (val);
297 1.4.2.2 rmind }
298 1.4.2.2 rmind
299 1.4.2.2 rmind void
300 1.4.2.2 rmind pyro_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
301 1.4.2.2 rmind {
302 1.4.2.2 rmind struct pyro_pbm *pp = pc->cookie;
303 1.4.2.2 rmind
304 1.4.2.2 rmind DPRINTF(PDB_CONF, ("%s: tag %lx; reg %x; data %x", __func__,
305 1.4.2.2 rmind (long)tag, reg, (int)data));
306 1.4.2.2 rmind
307 1.4.2.2 rmind /* If we don't know it, just punt it. */
308 1.4.2.2 rmind if (PCITAG_NODE(tag) == -1) {
309 1.4.2.2 rmind DPRINTF(PDB_CONF, (" .. bad addr\n"));
310 1.4.2.2 rmind return;
311 1.4.2.2 rmind }
312 1.4.2.2 rmind
313 1.4.2.2 rmind bus_space_write_4(pp->pp_cfgt, pp->pp_cfgh,
314 1.4.2.2 rmind (PCITAG_OFFSET(tag) << 4) + reg, data);
315 1.4.2.2 rmind DPRINTF(PDB_CONF, (" .. done\n"));
316 1.4.2.2 rmind }
317 1.4.2.2 rmind
318 1.4.2.2 rmind /*
319 1.4.2.2 rmind * Bus-specific interrupt mapping
320 1.4.2.2 rmind */
321 1.4.2.2 rmind int
322 1.4.2.2 rmind pyro_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
323 1.4.2.2 rmind {
324 1.4.2.2 rmind struct pyro_pbm *pp = pa->pa_pc->cookie;
325 1.4.2.2 rmind struct pyro_softc *sc = pp->pp_sc;
326 1.4.2.2 rmind u_int dev;
327 1.4.2.2 rmind
328 1.4.2.2 rmind if (*ihp != (pci_intr_handle_t)-1) {
329 1.4.2.2 rmind *ihp |= sc->sc_ign;
330 1.4.2.2 rmind DPRINTF(PDB_INTR, ("%s: not -1 -> ih %lx\n", __func__, (u_long)*ihp));
331 1.4.2.2 rmind return (0);
332 1.4.2.2 rmind }
333 1.4.2.2 rmind
334 1.4.2.2 rmind /*
335 1.4.2.2 rmind * We didn't find a PROM mapping for this interrupt. Try to
336 1.4.2.2 rmind * construct one ourselves based on the swizzled interrupt pin
337 1.4.2.2 rmind * and the interrupt mapping for PCI slots documented in the
338 1.4.2.2 rmind * UltraSPARC-IIi User's Manual.
339 1.4.2.2 rmind */
340 1.4.2.2 rmind
341 1.4.2.2 rmind if (pa->pa_intrpin == 0) {
342 1.4.2.2 rmind DPRINTF(PDB_INTR, ("%s: no intrpen\n", __func__));
343 1.4.2.2 rmind return (-1);
344 1.4.2.2 rmind }
345 1.4.2.2 rmind
346 1.4.2.2 rmind /*
347 1.4.2.2 rmind * This deserves some documentation. Should anyone
348 1.4.2.2 rmind * have anything official looking, please speak up.
349 1.4.2.2 rmind */
350 1.4.2.2 rmind dev = pa->pa_device - 1;
351 1.4.2.2 rmind
352 1.4.2.2 rmind *ihp = (pa->pa_intrpin - 1) & INTMAP_PCIINT;
353 1.4.2.2 rmind *ihp |= (dev << 2) & INTMAP_PCISLOT;
354 1.4.2.2 rmind *ihp |= sc->sc_ign;
355 1.4.2.2 rmind
356 1.4.2.2 rmind DPRINTF(PDB_INTR, ("%s: weird hack -> ih %lx\n", __func__, (u_long)*ihp));
357 1.4.2.2 rmind return (0);
358 1.4.2.2 rmind }
359 1.4.2.2 rmind
360 1.4.2.2 rmind bus_space_tag_t
361 1.4.2.2 rmind pyro_alloc_mem_tag(struct pyro_pbm *pp)
362 1.4.2.2 rmind {
363 1.4.2.2 rmind return (pyro_alloc_bus_tag(pp, "mem", PCI_MEMORY_BUS_SPACE));
364 1.4.2.2 rmind }
365 1.4.2.2 rmind
366 1.4.2.2 rmind bus_space_tag_t
367 1.4.2.2 rmind pyro_alloc_io_tag(struct pyro_pbm *pp)
368 1.4.2.2 rmind {
369 1.4.2.2 rmind return (pyro_alloc_bus_tag(pp, "io", PCI_IO_BUS_SPACE));
370 1.4.2.2 rmind }
371 1.4.2.2 rmind
372 1.4.2.2 rmind bus_space_tag_t
373 1.4.2.2 rmind pyro_alloc_config_tag(struct pyro_pbm *pp)
374 1.4.2.2 rmind {
375 1.4.2.2 rmind return (pyro_alloc_bus_tag(pp, "cfg", PCI_CONFIG_BUS_SPACE));
376 1.4.2.2 rmind }
377 1.4.2.2 rmind
378 1.4.2.2 rmind bus_space_tag_t
379 1.4.2.2 rmind pyro_alloc_bus_tag(struct pyro_pbm *pbm, const char *name, int type)
380 1.4.2.2 rmind {
381 1.4.2.2 rmind struct pyro_softc *sc = pbm->pp_sc;
382 1.4.2.2 rmind struct sparc_bus_space_tag *bt;
383 1.4.2.2 rmind
384 1.4.2.2 rmind bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
385 1.4.2.2 rmind if (bt == NULL)
386 1.4.2.2 rmind panic("pyro: could not allocate bus tag");
387 1.4.2.2 rmind
388 1.4.2.2 rmind #if 0
389 1.4.2.2 rmind snprintf(bt->name, sizeof(bt->name), "%s-pbm_%s(%d/%2.2x)",
390 1.4.2.2 rmind sc->sc_dv.dv_xname, name, ss, asi);
391 1.4.2.2 rmind #endif
392 1.4.2.2 rmind
393 1.4.2.2 rmind bt->cookie = pbm;
394 1.4.2.2 rmind bt->parent = sc->sc_bustag;
395 1.4.2.2 rmind bt->type = type;
396 1.4.2.2 rmind bt->sparc_bus_map = pyro_bus_map;
397 1.4.2.2 rmind bt->sparc_bus_mmap = pyro_bus_mmap;
398 1.4.2.2 rmind bt->sparc_intr_establish = pyro_intr_establish;
399 1.4.2.2 rmind return (bt);
400 1.4.2.2 rmind }
401 1.4.2.2 rmind
402 1.4.2.2 rmind bus_dma_tag_t
403 1.4.2.2 rmind pyro_alloc_dma_tag(struct pyro_pbm *pbm)
404 1.4.2.2 rmind {
405 1.4.2.2 rmind struct pyro_softc *sc = pbm->pp_sc;
406 1.4.2.2 rmind bus_dma_tag_t dt, pdt = sc->sc_dmat;
407 1.4.2.2 rmind
408 1.4.2.2 rmind dt = malloc(sizeof(*dt), M_DEVBUF, M_NOWAIT | M_ZERO);
409 1.4.2.2 rmind if (dt == NULL)
410 1.4.2.2 rmind panic("pyro: could not alloc dma tag");
411 1.4.2.2 rmind
412 1.4.2.2 rmind dt->_cookie = pbm;
413 1.4.2.2 rmind dt->_parent = pdt;
414 1.4.2.2 rmind #define PCOPY(x) dt->x = pdt->x
415 1.4.2.2 rmind dt->_dmamap_create = pyro_dmamap_create;
416 1.4.2.2 rmind PCOPY(_dmamap_destroy);
417 1.4.2.2 rmind dt->_dmamap_load = iommu_dvmamap_load;
418 1.4.2.2 rmind PCOPY(_dmamap_load_mbuf);
419 1.4.2.2 rmind PCOPY(_dmamap_load_uio);
420 1.4.2.2 rmind dt->_dmamap_load_raw = iommu_dvmamap_load_raw;
421 1.4.2.2 rmind dt->_dmamap_unload = iommu_dvmamap_unload;
422 1.4.2.2 rmind dt->_dmamap_sync = iommu_dvmamap_sync;
423 1.4.2.2 rmind dt->_dmamem_alloc = iommu_dvmamem_alloc;
424 1.4.2.2 rmind dt->_dmamem_free = iommu_dvmamem_free;
425 1.4.2.2 rmind dt->_dmamem_map = iommu_dvmamem_map;
426 1.4.2.2 rmind dt->_dmamem_unmap = iommu_dvmamem_unmap;
427 1.4.2.2 rmind PCOPY(_dmamem_mmap);
428 1.4.2.2 rmind #undef PCOPY
429 1.4.2.2 rmind return (dt);
430 1.4.2.2 rmind }
431 1.4.2.2 rmind
432 1.4.2.2 rmind pci_chipset_tag_t
433 1.4.2.2 rmind pyro_alloc_chipset(struct pyro_pbm *pbm, int node, pci_chipset_tag_t pc)
434 1.4.2.2 rmind {
435 1.4.2.2 rmind pci_chipset_tag_t npc;
436 1.4.2.2 rmind
437 1.4.2.2 rmind npc = malloc(sizeof *npc, M_DEVBUF, M_NOWAIT);
438 1.4.2.2 rmind if (npc == NULL)
439 1.4.2.2 rmind panic("pyro: could not allocate pci_chipset_tag_t");
440 1.4.2.2 rmind memcpy(npc, pc, sizeof *pc);
441 1.4.2.2 rmind npc->cookie = pbm;
442 1.4.2.2 rmind npc->rootnode = node;
443 1.4.2.2 rmind npc->spc_conf_read = pyro_conf_read;
444 1.4.2.2 rmind npc->spc_conf_write = pyro_conf_write;
445 1.4.2.2 rmind npc->spc_intr_map = pyro_intr_map;
446 1.4.2.2 rmind npc->spc_intr_establish = pyro_pci_intr_establish;
447 1.4.2.2 rmind npc->spc_find_ino = NULL;
448 1.4.2.2 rmind return (npc);
449 1.4.2.2 rmind }
450 1.4.2.2 rmind
451 1.4.2.2 rmind int
452 1.4.2.2 rmind pyro_dmamap_create(bus_dma_tag_t t, bus_size_t size,
453 1.4.2.2 rmind int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags,
454 1.4.2.2 rmind bus_dmamap_t *dmamp)
455 1.4.2.2 rmind {
456 1.4.2.2 rmind struct pyro_pbm *pbm = t->_cookie;
457 1.4.2.2 rmind int error;
458 1.4.2.2 rmind
459 1.4.2.2 rmind error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
460 1.4.2.2 rmind boundary, flags, dmamp);
461 1.4.2.2 rmind if (error == 0)
462 1.4.2.2 rmind (*dmamp)->_dm_cookie = &pbm->pp_sb;
463 1.4.2.2 rmind return error;
464 1.4.2.2 rmind }
465 1.4.2.2 rmind
466 1.4.2.2 rmind int
467 1.4.2.2 rmind pyro_bus_map(bus_space_tag_t t, bus_addr_t offset,
468 1.4.2.2 rmind bus_size_t size, int flags, vaddr_t unused, bus_space_handle_t *hp)
469 1.4.2.2 rmind {
470 1.4.2.2 rmind struct pyro_pbm *pbm = t->cookie;
471 1.4.2.2 rmind struct pyro_softc *sc = pbm->pp_sc;
472 1.4.2.2 rmind int i, ss;
473 1.4.2.2 rmind
474 1.4.2.2 rmind DPRINTF(PDB_BUSMAP, ("pyro_bus_map: type %d off %qx sz %qx flags %d",
475 1.4.2.2 rmind t->type,
476 1.4.2.2 rmind (unsigned long long)offset,
477 1.4.2.2 rmind (unsigned long long)size,
478 1.4.2.2 rmind flags));
479 1.4.2.2 rmind
480 1.4.2.2 rmind ss = sparc_pci_childspace(t->type);
481 1.4.2.2 rmind DPRINTF(PDB_BUSMAP, (" cspace %d", ss));
482 1.4.2.2 rmind
483 1.4.2.2 rmind if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
484 1.4.2.2 rmind printf("\n_pyro_bus_map: invalid parent");
485 1.4.2.2 rmind return (EINVAL);
486 1.4.2.2 rmind }
487 1.4.2.2 rmind
488 1.4.2.2 rmind for (i = 0; i < pbm->pp_nrange; i++) {
489 1.4.2.2 rmind bus_addr_t paddr;
490 1.4.2.2 rmind struct pyro_range *pr = &pbm->pp_range[i];
491 1.4.2.2 rmind
492 1.4.2.2 rmind if (((pr->cspace >> 24) & 0x03) != ss)
493 1.4.2.2 rmind continue;
494 1.4.2.2 rmind
495 1.4.2.2 rmind paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
496 1.4.2.2 rmind return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
497 1.4.2.2 rmind flags, 0, hp));
498 1.4.2.2 rmind }
499 1.4.2.2 rmind
500 1.4.2.2 rmind return (EINVAL);
501 1.4.2.2 rmind }
502 1.4.2.2 rmind
503 1.4.2.2 rmind paddr_t
504 1.4.2.2 rmind pyro_bus_mmap(bus_space_tag_t t, bus_addr_t paddr,
505 1.4.2.2 rmind off_t off, int prot, int flags)
506 1.4.2.2 rmind {
507 1.4.2.2 rmind bus_addr_t offset = paddr;
508 1.4.2.2 rmind struct pyro_pbm *pbm = t->cookie;
509 1.4.2.2 rmind struct pyro_softc *sc = pbm->pp_sc;
510 1.4.2.2 rmind int i, ss;
511 1.4.2.2 rmind
512 1.4.2.2 rmind ss = sparc_pci_childspace(t->type);
513 1.4.2.2 rmind
514 1.4.2.2 rmind DPRINTF(PDB_BUSMAP, ("pyro_bus_mmap: prot %d flags %d pa %qx\n",
515 1.4.2.2 rmind prot, flags, (unsigned long long)paddr));
516 1.4.2.2 rmind
517 1.4.2.2 rmind if (t->parent == 0 || t->parent->sparc_bus_mmap == 0) {
518 1.4.2.2 rmind printf("\n_pyro_bus_mmap: invalid parent");
519 1.4.2.2 rmind return (-1);
520 1.4.2.2 rmind }
521 1.4.2.2 rmind
522 1.4.2.2 rmind for (i = 0; i < pbm->pp_nrange; i++) {
523 1.4.2.2 rmind struct pyro_range *pr = &pbm->pp_range[i];
524 1.4.2.2 rmind
525 1.4.2.2 rmind if (((pr->cspace >> 24) & 0x03) != ss)
526 1.4.2.2 rmind continue;
527 1.4.2.2 rmind
528 1.4.2.2 rmind paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
529 1.4.2.2 rmind return (bus_space_mmap(sc->sc_bustag, paddr, off,
530 1.4.2.2 rmind prot, flags));
531 1.4.2.2 rmind }
532 1.4.2.2 rmind
533 1.4.2.2 rmind return (-1);
534 1.4.2.2 rmind }
535 1.4.2.2 rmind
536 1.4.2.2 rmind void *
537 1.4.2.2 rmind pyro_intr_establish(bus_space_tag_t t, int ihandle, int level,
538 1.4.2.2 rmind int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
539 1.4.2.2 rmind {
540 1.4.2.2 rmind struct pyro_pbm *pbm = t->cookie;
541 1.4.2.2 rmind struct pyro_softc *sc = pbm->pp_sc;
542 1.4.2.2 rmind struct intrhand *ih = NULL;
543 1.4.2.2 rmind volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
544 1.4.2.2 rmind u_int64_t *imapbase, *iclrbase;
545 1.4.2.2 rmind int ino;
546 1.4.2.2 rmind
547 1.4.2.2 rmind ino = INTINO(ihandle);
548 1.4.2.2 rmind DPRINTF(PDB_INTR, ("%s: ih %lx; level %d ino %d", __func__, (u_long)ih, level, ino));
549 1.4.2.2 rmind
550 1.4.2.2 rmind if (level == IPL_NONE)
551 1.4.2.2 rmind level = INTLEV(ihandle);
552 1.4.2.2 rmind if (level == IPL_NONE) {
553 1.4.2.2 rmind printf(": no IPL, setting IPL 2.\n");
554 1.4.2.2 rmind level = 2;
555 1.4.2.2 rmind }
556 1.4.2.2 rmind
557 1.4.2.2 rmind imapbase = (uint64_t *)((uintptr_t)bus_space_vaddr(sc->sc_bustag, sc->sc_csrh) + 0x1000);
558 1.4.2.2 rmind iclrbase = (uint64_t *)((uintptr_t)bus_space_vaddr(sc->sc_bustag, sc->sc_csrh) + 0x1400);
559 1.4.2.2 rmind intrmapptr = &imapbase[ino];
560 1.4.2.2 rmind intrclrptr = &iclrbase[ino];
561 1.4.2.2 rmind DPRINTF(PDB_INTR, (" imapbase %p iclrbase %p mapptr %p clrptr %p\n", imapbase, iclrbase, intrmapptr, intrclrptr));
562 1.4.2.2 rmind ino |= INTVEC(ihandle);
563 1.4.2.2 rmind
564 1.4.2.2 rmind ih = malloc(sizeof *ih, M_DEVBUF, M_NOWAIT);
565 1.4.2.2 rmind if (ih == NULL)
566 1.4.2.2 rmind return (NULL);
567 1.4.2.2 rmind
568 1.4.2.2 rmind /* Register the map and clear intr registers */
569 1.4.2.2 rmind ih->ih_map = intrmapptr;
570 1.4.2.2 rmind ih->ih_clr = intrclrptr;
571 1.4.2.2 rmind
572 1.4.2.2 rmind ih->ih_fun = handler;
573 1.4.2.2 rmind ih->ih_arg = arg;
574 1.4.2.2 rmind ih->ih_pil = level;
575 1.4.2.2 rmind ih->ih_number = ino;
576 1.4.2.2 rmind
577 1.4.2.2 rmind intr_establish(ih->ih_pil, level != IPL_VM, ih);
578 1.4.2.2 rmind
579 1.4.2.2 rmind if (intrmapptr != NULL) {
580 1.4.2.2 rmind u_int64_t imap;
581 1.4.2.2 rmind
582 1.4.2.2 rmind imap = *intrmapptr;
583 1.4.2.2 rmind DPRINTF(PDB_INTR, ("%s: read intrmap = %016qx", __func__,
584 1.4.2.2 rmind (unsigned long long)imap));
585 1.4.2.2 rmind imap &= ~FIRE_INTRMAP_INT_CNTRL_NUM_MASK;
586 1.4.2.2 rmind imap |= FIRE_INTRMAP_INT_CNTRL_NUM0;
587 1.4.2.2 rmind DPRINTF(PDB_INTR, ("; set intr group intrmap = %016qx",
588 1.4.2.2 rmind (unsigned long long)imap));
589 1.4.2.2 rmind if (sc->sc_oberon) {
590 1.4.2.2 rmind imap &= ~OBERON_INTRMAP_T_DESTID_MASK;
591 1.4.2.2 rmind imap |= CPU_JUPITERID <<
592 1.4.2.2 rmind OBERON_INTRMAP_T_DESTID_SHIFT;
593 1.4.2.2 rmind } else {
594 1.4.2.2 rmind imap &= ~FIRE_INTRMAP_T_JPID_MASK;
595 1.4.2.2 rmind imap |= CPU_UPAID << FIRE_INTRMAP_T_JPID_SHIFT;
596 1.4.2.2 rmind }
597 1.4.2.2 rmind DPRINTF(PDB_INTR, ("; set cpuid num intrmap = %016qx",
598 1.4.2.2 rmind (unsigned long long)imap));
599 1.4.2.2 rmind imap |= INTMAP_V;
600 1.4.2.2 rmind *intrmapptr = imap;
601 1.4.2.2 rmind DPRINTF(PDB_INTR, ("; writing intrmap = %016qx",
602 1.4.2.2 rmind (unsigned long long)imap));
603 1.4.2.2 rmind imap = *intrmapptr;
604 1.4.2.2 rmind DPRINTF(PDB_INTR, ("; reread intrmap = %016qx\n",
605 1.4.2.2 rmind (unsigned long long)imap));
606 1.4.2.2 rmind ih->ih_number |= imap & INTMAP_INR;
607 1.4.2.2 rmind }
608 1.4.2.2 rmind if (intrclrptr) {
609 1.4.2.2 rmind /* set state to IDLE */
610 1.4.2.2 rmind *intrclrptr = 0;
611 1.4.2.2 rmind }
612 1.4.2.2 rmind
613 1.4.2.2 rmind return (ih);
614 1.4.2.2 rmind }
615 1.4.2.2 rmind
616 1.4.2.2 rmind static void *
617 1.4.2.2 rmind pyro_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
618 1.4.2.2 rmind int (*func)(void *), void *arg)
619 1.4.2.2 rmind {
620 1.4.2.2 rmind void *cookie;
621 1.4.2.2 rmind struct pyro_pbm *pbm = (struct pyro_pbm *)pc->cookie;
622 1.4.2.2 rmind
623 1.4.2.2 rmind DPRINTF(PDB_INTR, ("%s: ih %lx; level %d\n", __func__, (u_long)ih, level));
624 1.4.2.2 rmind cookie = bus_intr_establish(pbm->pp_memt, ih, level, func, arg);
625 1.4.2.2 rmind
626 1.4.2.2 rmind DPRINTF(PDB_INTR, ("%s: returning handle %p\n", __func__, cookie));
627 1.4.2.2 rmind return (cookie);
628 1.4.2.2 rmind }
629