xref: /netbsd-src/sys/arch/sparc64/dev/vpci.c (revision 90d7b449b611b9e411624cf344e9ff5914fd701e)
1*90d7b449Sthorpej /*	$NetBSD: vpci.c,v 1.13 2022/01/21 19:14:14 thorpej Exp $	*/
2ea65b081Spalle /*
3ea65b081Spalle  * Copyright (c) 2015 Palle Lyckegaard
4ea65b081Spalle  * All rights reserved.
5ea65b081Spalle  *
6ea65b081Spalle  * Driver for virtual PCIe host bridge on sun4v systems.
7ea65b081Spalle  *
8ea65b081Spalle  * Based on NetBSD pyro and OpenBSD vpci drivers.
9ea65b081Spalle  *
10ea65b081Spalle  * Redistribution and use in source and binary forms, with or without
11ea65b081Spalle  * modification, are permitted provided that the following conditions
12ea65b081Spalle  * are met:
13ea65b081Spalle  * 1. Redistributions of source code must retain the above copyright
14ea65b081Spalle  *    notice, this list of conditions and the following disclaimer.
15ea65b081Spalle  * 2. Redistributions in binary form must reproduce the above copyright
16ea65b081Spalle  *    notice, this list of conditions and the following disclaimer in the
17ea65b081Spalle  *    documentation and/or other materials provided with the distribution.
18ea65b081Spalle  *
19ea65b081Spalle  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20ea65b081Spalle  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21ea65b081Spalle  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22ea65b081Spalle  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23ea65b081Spalle  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24ea65b081Spalle  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25ea65b081Spalle  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ea65b081Spalle  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27ea65b081Spalle  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28ea65b081Spalle  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ea65b081Spalle  * POSSIBILITY OF SUCH DAMAGE.
30ea65b081Spalle  */
31ea65b081Spalle 
32ea65b081Spalle #include <sys/cdefs.h>
33*90d7b449Sthorpej __KERNEL_RCSID(0, "$NetBSD: vpci.c,v 1.13 2022/01/21 19:14:14 thorpej Exp $");
34ea65b081Spalle 
35ea65b081Spalle #include <sys/param.h>
36ea65b081Spalle #include <sys/device.h>
37ea65b081Spalle #include <sys/errno.h>
38ea65b081Spalle #include <sys/malloc.h>
39fc256c4aSthorpej #include <sys/kmem.h>
40ea65b081Spalle #include <sys/systm.h>
41ea65b081Spalle 
42ea65b081Spalle #define _SPARC_BUS_DMA_PRIVATE
43ea65b081Spalle #include <sys/bus.h>
44ea65b081Spalle #include <machine/autoconf.h>
45ea65b081Spalle 
46ea65b081Spalle #ifdef DDB
47ea65b081Spalle #include <machine/db_machdep.h>
48ea65b081Spalle #endif
49ea65b081Spalle 
50ea65b081Spalle #include <dev/pci/pcivar.h>
51ea65b081Spalle #include <dev/pci/pcireg.h>
52ea65b081Spalle 
53ea65b081Spalle #include <sparc64/dev/iommureg.h>
54ea65b081Spalle #include <sparc64/dev/iommuvar.h>
55ea65b081Spalle #include <sparc64/dev/vpcivar.h>
56ea65b081Spalle 
57f4df0c24Snakayama #include <machine/hypervisor.h>
58ea65b081Spalle 
59ea65b081Spalle #ifdef DEBUG
60ea65b081Spalle #define VDB_PROM             0x01
61ea65b081Spalle #define VDB_BUSMAP           0x02
62ea65b081Spalle #define VDB_INTR             0x04
63688f2bd1Spalle #define VDB_CONF_READ        0x08
64688f2bd1Spalle #define VDB_CONF_WRITE       0x10
65688f2bd1Spalle #define VDB_CONF             VDB_CONF_READ|VDB_CONF_WRITE
66688f2bd1Spalle int vpci_debug = 0x00;
67ea65b081Spalle #define DPRINTF(l, s)   do { if (vpci_debug & l) printf s; } while (0)
68ea65b081Spalle #else
69ea65b081Spalle #define DPRINTF(l, s)
70ea65b081Spalle #endif
71ea65b081Spalle 
72ea65b081Spalle #if 0
73ea65b081Spalle FIXME
74ea65b081Spalle #define FIRE_RESET_GEN			0x7010
75ea65b081Spalle 
76ea65b081Spalle #define FIRE_RESET_GEN_XIR		0x0000000000000002L
77ea65b081Spalle 
78ea65b081Spalle #define FIRE_INTRMAP_INT_CNTRL_NUM_MASK	0x000003c0
79ea65b081Spalle #define FIRE_INTRMAP_INT_CNTRL_NUM0	0x00000040
80ea65b081Spalle #define FIRE_INTRMAP_INT_CNTRL_NUM1	0x00000080
81ea65b081Spalle #define FIRE_INTRMAP_INT_CNTRL_NUM2	0x00000100
82ea65b081Spalle #define FIRE_INTRMAP_INT_CNTRL_NUM3	0x00000200
83ea65b081Spalle #define FIRE_INTRMAP_T_JPID_SHIFT	26
84ea65b081Spalle #define FIRE_INTRMAP_T_JPID_MASK	0x7c000000
85ea65b081Spalle 
86ea65b081Spalle #define OBERON_INTRMAP_T_DESTID_SHIFT	21
87ea65b081Spalle #define OBERON_INTRMAP_T_DESTID_MASK	0x7fe00000
88ea65b081Spalle #endif
89ea65b081Spalle 
90ea65b081Spalle extern struct sparc_pci_chipset _sparc_pci_chipset;
91ea65b081Spalle 
92ea65b081Spalle int vpci_match(device_t, cfdata_t, void *);
93ea65b081Spalle void vpci_attach(device_t, device_t, void *);
94ea65b081Spalle int vpci_print(void *, const char *);
95ea65b081Spalle 
96ea65b081Spalle CFATTACH_DECL_NEW(vpci, sizeof(struct vpci_softc),
97ea65b081Spalle     vpci_match, vpci_attach, NULL, NULL);
98ea65b081Spalle 
99ea65b081Spalle void vpci_init(struct vpci_softc */*FIXME, int*/, struct mainbus_attach_args *);
100ea65b081Spalle void vpci_init_iommu(struct vpci_softc *, struct vpci_pbm *);
101ea65b081Spalle pci_chipset_tag_t vpci_alloc_chipset(struct vpci_pbm *, int,
102ea65b081Spalle     pci_chipset_tag_t);
103ea65b081Spalle bus_space_tag_t vpci_alloc_mem_tag(struct vpci_pbm *);
104ea65b081Spalle bus_space_tag_t vpci_alloc_io_tag(struct vpci_pbm *);
105ea65b081Spalle bus_space_tag_t vpci_alloc_config_tag(struct vpci_pbm *);
106ea65b081Spalle bus_space_tag_t vpci_alloc_bus_tag(struct vpci_pbm *, const char *, int);
107ea65b081Spalle bus_dma_tag_t vpci_alloc_dma_tag(struct vpci_pbm *);
108ea65b081Spalle 
109ea65b081Spalle #if 0
110ea65b081Spalle int vpci_conf_size(pci_chipset_tag_t, pcitag_t);
111ea65b081Spalle #endif
112ea65b081Spalle pcireg_t vpci_conf_read(pci_chipset_tag_t, pcitag_t, int);
113ea65b081Spalle void vpci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
114ea65b081Spalle 
115ea65b081Spalle static void * vpci_pci_intr_establish(pci_chipset_tag_t pc,
116ea65b081Spalle 				      pci_intr_handle_t ih, int level,
117ea65b081Spalle 				      int (*func)(void *), void *arg);
1187820dc41Spalle void vpci_intr_ack(struct intrhand *);
119ea65b081Spalle int vpci_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
120ea65b081Spalle int vpci_bus_map(bus_space_tag_t, bus_addr_t,
121ea65b081Spalle     bus_size_t, int, vaddr_t, bus_space_handle_t *);
122ea65b081Spalle paddr_t vpci_bus_mmap(bus_space_tag_t, bus_addr_t, off_t,
123ea65b081Spalle     int, int);
124ea65b081Spalle void *vpci_intr_establish(bus_space_tag_t, int, int,
125ea65b081Spalle     int (*)(void *), void *, void (*)(void));
126ea65b081Spalle 
127ea65b081Spalle int vpci_dmamap_create(bus_dma_tag_t, bus_size_t, int,
128ea65b081Spalle     bus_size_t, bus_size_t, int, bus_dmamap_t *);
129ea65b081Spalle 
130ea65b081Spalle int
vpci_match(device_t parent,cfdata_t match,void * aux)131ea65b081Spalle vpci_match(device_t parent, cfdata_t match, void *aux)
132ea65b081Spalle {
133ea65b081Spalle 	struct mainbus_attach_args *ma = aux;
134ea65b081Spalle 	char compat[32];
135ea65b081Spalle 
136ea65b081Spalle 	if (strcmp(ma->ma_name, "pci") != 0)
137ea65b081Spalle 		return (0);
138ea65b081Spalle 
139ea65b081Spalle 	if (OF_getprop(ma->ma_node, "compatible", compat, sizeof(compat)) == -1)
140ea65b081Spalle 		return (0);
141ea65b081Spalle 
142ea65b081Spalle 	if (strcmp(compat, "SUNW,sun4v-pci") == 0)
143ea65b081Spalle 		return (1);
144ea65b081Spalle 
145ea65b081Spalle 	return (0);
146ea65b081Spalle }
147ea65b081Spalle 
148ea65b081Spalle void
vpci_attach(device_t parent,device_t self,void * aux)149ea65b081Spalle vpci_attach(device_t parent, device_t self, void *aux)
150ea65b081Spalle {
151ea65b081Spalle 	struct vpci_softc *sc = device_private(self);
152ea65b081Spalle 	struct mainbus_attach_args *ma = aux;
153ea65b081Spalle #if 0
154ea65b081Spalle FIXME
155ea65b081Spalle 	char *str;
156ea65b081Spalle 	int busa;
157ea65b081Spalle #endif
158ea65b081Spalle 	sc->sc_dev = self;
159ea65b081Spalle 	sc->sc_node = ma->ma_node;
160ea65b081Spalle 	sc->sc_dmat = ma->ma_dmatag;
161ea65b081Spalle 	sc->sc_bustag = ma->ma_bustag;
162ea65b081Spalle 	sc->sc_csr = ma->ma_reg[0].ur_paddr;
163ea65b081Spalle #if 0
164ea65b081Spalle FIXME
165ea65b081Spalle 	sc->sc_xbc = ma->ma_reg[1].ur_paddr;
166ea65b081Spalle 	sc->sc_ign = INTIGN(ma->ma_upaid << INTMAP_IGN_SHIFT);
167ea65b081Spalle 
168ea65b081Spalle 	if ((ma->ma_reg[0].ur_paddr & 0x00700000) == 0x00600000)
169ea65b081Spalle 		busa = 1;
170ea65b081Spalle 	else
171ea65b081Spalle 		busa = 0;
172ea65b081Spalle #endif
173ea65b081Spalle #if 0
174ea65b081Spalle FIXME
175ea65b081Spalle 	if (bus_space_map(sc->sc_bustag, sc->sc_csr,
176ea65b081Spalle 	    ma->ma_reg[0].ur_len, BUS_SPACE_MAP_LINEAR, &sc->sc_csrh)) {
177ea65b081Spalle 		printf(": failed to map csr registers\n");
178ea65b081Spalle 		return;
179ea65b081Spalle 	}
180ea65b081Spalle #endif
181ea65b081Spalle #if 0
182ea65b081Spalle FIXME
183ea65b081Spalle 	if (bus_space_map(sc->sc_bustag, sc->sc_xbc,
184ea65b081Spalle 	    ma->ma_reg[1].ur_len, 0, &sc->sc_xbch)) {
185ea65b081Spalle 		printf(": failed to map xbc registers\n");
186ea65b081Spalle 		return;
187ea65b081Spalle 	}
188ea65b081Spalle 
189ea65b081Spalle 	str = prom_getpropstring(ma->ma_node, "compatible");
190ea65b081Spalle 	if (strcmp(str, "pciex108e,80f8") == 0)
191ea65b081Spalle 		sc->sc_oberon = 1;
192ea65b081Spalle 
193ea65b081Spalle #endif
194ea65b081Spalle 	vpci_init(sc/*FIXME, busa*/, ma);
195ea65b081Spalle }
196ea65b081Spalle 
197ea65b081Spalle void
vpci_init(struct vpci_softc * sc,struct mainbus_attach_args * ma)198ea65b081Spalle vpci_init(struct vpci_softc *sc/*FIXME, int busa*/, struct mainbus_attach_args *ma)
199ea65b081Spalle {
200ea65b081Spalle 	struct vpci_pbm *pbm;
201ea65b081Spalle 	struct pcibus_attach_args pba;
202ea65b081Spalle 	int *busranges = NULL, nranges;
203ea65b081Spalle 
204fc256c4aSthorpej 	pbm = kmem_zalloc(sizeof(*pbm), KM_SLEEP);
205ea65b081Spalle 	pbm->vp_sc = sc;
206ea65b081Spalle 	pbm->vp_devhandle = (ma->ma_reg[0].ur_paddr >> 32) & 0x0fffffff;
207ea65b081Spalle #if 0
208ea65b081Spalle FiXME
209ea65b081Spalle 	pbm->vp_bus_a = busa;
210ea65b081Spalle #endif
211ea65b081Spalle 
212ea65b081Spalle 	if (prom_getprop(sc->sc_node, "ranges", sizeof(struct vpci_range),
213ea65b081Spalle 	    &pbm->vp_nrange, (void **)&pbm->vp_range))
214ea65b081Spalle 		panic("vpci: can't get ranges");
215ea65b081Spalle 	for (int range = 0; range < pbm->vp_nrange; range++)
216ea65b081Spalle 		DPRINTF(VDB_PROM,
217ea65b081Spalle 			("\nvpci_attach: range %d  cspace %08x  "
218ea65b081Spalle 			"child_hi %08x  child_lo %08x  phys_hi %08x  phys_lo %08x  "
219ea65b081Spalle 			"size_hi %08x  size_lo %08x", range,
220ea65b081Spalle 			pbm->vp_range[range].cspace,
221ea65b081Spalle 			pbm->vp_range[range].child_hi,
222ea65b081Spalle 			pbm->vp_range[range].child_lo,
223ea65b081Spalle 			pbm->vp_range[range].phys_hi,
224ea65b081Spalle 			pbm->vp_range[range].phys_lo,
225ea65b081Spalle 			pbm->vp_range[range].size_hi,
226ea65b081Spalle 			pbm->vp_range[range].size_lo));
227ea65b081Spalle 
228ea65b081Spalle 	if (prom_getprop(sc->sc_node, "bus-range", sizeof(int), &nranges,
229ea65b081Spalle 	    (void **)&busranges))
230ea65b081Spalle 		panic("vpci: can't get bus-range");
231ea65b081Spalle 	for (int range = 0; range < nranges; range++)
232ea65b081Spalle 		DPRINTF(VDB_PROM, ("\nvpci_attach: bus-range %d %08x", range, busranges[range]));
233688f2bd1Spalle 
234df6d76d7Spalle  	aprint_normal(": bus %d to %d", busranges[0], busranges[1]);
235df6d76d7Spalle 
236ea65b081Spalle 	vpci_init_iommu(sc, pbm);
237688f2bd1Spalle 
238ea65b081Spalle 	pbm->vp_memt = vpci_alloc_mem_tag(pbm);
239ea65b081Spalle 	pbm->vp_iot = vpci_alloc_io_tag(pbm);
240ea65b081Spalle 	pbm->vp_cfgt = vpci_alloc_config_tag(pbm);
241ea65b081Spalle 	pbm->vp_dmat = vpci_alloc_dma_tag(pbm);
242ea65b081Spalle 	pbm->vp_flags = (pbm->vp_memt ? PCI_FLAGS_MEM_OKAY : 0) |
243ea65b081Spalle 		        (pbm->vp_iot ? PCI_FLAGS_IO_OKAY : 0);
244ea65b081Spalle #if 0
245ea65b081Spalle FIXME
246ea65b081Spalle 	if (bus_space_map(pbm->vp_cfgt, 0, 0x10000000, 0, &pbm->vp_cfgh))
247ea65b081Spalle 		panic("vpci: can't map config space");
248ea65b081Spalle #endif
249ea65b081Spalle 	pbm->vp_pc = vpci_alloc_chipset(pbm, sc->sc_node, &_sparc_pci_chipset);
250ea65b081Spalle 	pbm->vp_pc->spc_busmax = busranges[1];
251fc256c4aSthorpej 	pbm->vp_pc->spc_busnode = kmem_zalloc(sizeof(*pbm->vp_pc->spc_busnode),
252fc256c4aSthorpej 	    KM_SLEEP);
253ea65b081Spalle 
254ea65b081Spalle #if 0
255ea65b081Spalle 	pbm->vp_pc->bustag = pbm->vp_cfgt;
256ea65b081Spalle 	pbm->vp_pc->bushandle = pbm->vp_cfgh;
257ea65b081Spalle #endif
258ea65b081Spalle 
259ea65b081Spalle 	bzero(&pba, sizeof(pba));
260ea65b081Spalle 	pba.pba_bus = busranges[0];
261ea65b081Spalle 	pba.pba_pc = pbm->vp_pc;
262ea65b081Spalle 	pba.pba_flags = pbm->vp_flags;
263ea65b081Spalle 	pba.pba_dmat = pbm->vp_dmat;
264ea65b081Spalle 	pba.pba_dmat64 = NULL;	/* XXX */
265ea65b081Spalle 	pba.pba_memt = pbm->vp_memt;
266ea65b081Spalle 	pba.pba_iot = pbm->vp_iot;
267ea65b081Spalle 
268ea65b081Spalle 	free(busranges, M_DEVBUF);
269ea65b081Spalle 
27065c738d1Sthorpej 	config_found(sc->sc_dev, &pba, vpci_print,
271*90d7b449Sthorpej 	    CFARGS(.devhandle = device_handle(sc->sc_dev)));
272ea65b081Spalle }
273ea65b081Spalle 
274ea65b081Spalle void
vpci_init_iommu(struct vpci_softc * sc,struct vpci_pbm * pbm)275ea65b081Spalle vpci_init_iommu(struct vpci_softc *sc, struct vpci_pbm *pbm)
276ea65b081Spalle {
277ea65b081Spalle 	struct iommu_state *is = &pbm->vp_is;
278688f2bd1Spalle 	int *vdma = NULL;
279688f2bd1Spalle 	int nitem;
280688f2bd1Spalle 	int tsbsize = 0;
281ea65b081Spalle 	u_int32_t iobase = -1;
282688f2bd1Spalle 	u_int32_t iolen = 0;
283ea65b081Spalle 	char *name;
284ea65b081Spalle 
285ea65b081Spalle 	pbm->vp_sb.sb_is = is;
286ea65b081Spalle 	is->is_bustag = sc->sc_bustag;
287ea65b081Spalle 
288ea65b081Spalle 	if (bus_space_subregion(is->is_bustag, sc->sc_csrh,
289ea65b081Spalle 	    0x40000, 0x100, &is->is_iommu)) {
290ea65b081Spalle 		panic("vpci: unable to create iommu handle");
291ea65b081Spalle 	}
292ea65b081Spalle 
293688f2bd1Spalle 	/* Construct tsbsize */
294688f2bd1Spalle 	if (!prom_getprop(sc->sc_node, "virtual-dma", sizeof(vdma), &nitem,
295688f2bd1Spalle 	    (void **)&vdma)) {
296688f2bd1Spalle 		DPRINTF(VDB_BUSMAP, ("vpci_init_iommu: vdma[0]=0x%x  vdma[1]=0x%x\n",
297688f2bd1Spalle 		    vdma[0], vdma[1]));
298688f2bd1Spalle 		iobase = vdma[0];
299688f2bd1Spalle 		iolen = vdma[1];
3007a3084cdSjoerg 		for (tsbsize = 8; (1 << (tsbsize+23)) > iolen;)
3017a3084cdSjoerg 			tsbsize--;
302688f2bd1Spalle 		DPRINTF(VDB_BUSMAP, ("vpci_init_iommu: iobase=0x%x  iolen = 0x%x  tsbsize=0x%x\n",
303688f2bd1Spalle 		    iobase, iolen, tsbsize));
304688f2bd1Spalle 		free(vdma, M_DEVBUF);
305688f2bd1Spalle 	} else
306688f2bd1Spalle 		panic("vpci_init_iommu: getprop virtual-dma failed");
307688f2bd1Spalle 
308df6d76d7Spalle 	aprint_normal(" vdma %x length %x\n", iobase, iolen);
3092ae392feSpalle 	aprint_naive("\n");
310df6d76d7Spalle 
311ea65b081Spalle 	/* We have no STC.  */
312ea65b081Spalle 	is->is_sb[0] = NULL;
313ea65b081Spalle 
314fc256c4aSthorpej 	name = kmem_asprintf("%s dvma", device_xname(sc->sc_dev));
315ea65b081Spalle 
316ea65b081Spalle 	/* Tell iommu how to set the TSB size.  */
317ea65b081Spalle 	is->is_flags = IOMMU_TSBSIZE_IN_PTSB;
318ea65b081Spalle 
319688f2bd1Spalle 	is->is_devhandle = pbm->vp_devhandle;
320ea65b081Spalle 	iommu_init(name, is, tsbsize, iobase);
321ea65b081Spalle }
322688f2bd1Spalle 
323ea65b081Spalle 
324ea65b081Spalle int
vpci_print(void * aux,const char * p)325ea65b081Spalle vpci_print(void *aux, const char *p)
326ea65b081Spalle {
327ea65b081Spalle 	if (p == NULL)
328ea65b081Spalle 		return (UNCONF);
329ea65b081Spalle 	return (QUIET);
330ea65b081Spalle }
331ea65b081Spalle 
332ea65b081Spalle pcireg_t
vpci_conf_read(pci_chipset_tag_t pc,pcitag_t tag,int reg)333ea65b081Spalle vpci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
334ea65b081Spalle {
335ea65b081Spalle 	struct vpci_pbm *pbm = pc->cookie;
336ea65b081Spalle 	uint64_t error_flag, data;
337ea65b081Spalle 
338ea65b081Spalle 	int64_t hv_rc;
339688f2bd1Spalle 	DPRINTF(VDB_CONF_READ, ("%s: tag %lx reg %x ", __func__, (long)tag, reg));
340ea65b081Spalle 	hv_rc = hv_pci_config_get(pbm->vp_devhandle, PCITAG_OFFSET(tag), reg, 4,
341ea65b081Spalle 	    &error_flag, &data);
342ea65b081Spalle 	if (hv_rc != H_EOK)
343ea65b081Spalle 		panic("hv_pci_config_get() failed - rc = %" PRId64 "\n",
344ea65b081Spalle 		    hv_rc);
345ea65b081Spalle 
346ea65b081Spalle 	pcireg_t val = error_flag ? (pcireg_t)~0 : data;
347688f2bd1Spalle 	DPRINTF(VDB_CONF_READ, (" returning %08x\n", (u_int)val));
348ea65b081Spalle 	return val;
349ea65b081Spalle }
350ea65b081Spalle 
351ea65b081Spalle void
vpci_conf_write(pci_chipset_tag_t pc,pcitag_t tag,int reg,pcireg_t data)352ea65b081Spalle vpci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
353ea65b081Spalle {
354ea65b081Spalle 
355ea65b081Spalle 	struct vpci_pbm *pbm = pc->cookie;
356ea65b081Spalle 	uint64_t error_flag;
357ea65b081Spalle 	int64_t hv_rc;
358688f2bd1Spalle 	DPRINTF(VDB_CONF_WRITE, ("%s: tag %lx; reg %x; data %x", __func__,
359ea65b081Spalle 		(long)tag, reg, (int)data));
360ea65b081Spalle 	hv_rc = hv_pci_config_put(pbm->vp_devhandle, PCITAG_OFFSET(tag), reg, 4,
361ea65b081Spalle             data, &error_flag);
362ea65b081Spalle 	if (hv_rc != H_EOK)
363ea65b081Spalle 		panic("hv_pci_config_put() failed - rc = %" PRId64 "\n",
364ea65b081Spalle 		    hv_rc);
365688f2bd1Spalle 	DPRINTF(VDB_CONF_WRITE, (" .. done\n"));
366ea65b081Spalle }
367ea65b081Spalle 
368ea65b081Spalle /*
369ea65b081Spalle  * Bus-specific interrupt mapping
370ea65b081Spalle  */
371ea65b081Spalle int
vpci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)372ea65b081Spalle vpci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
373ea65b081Spalle {
374ea65b081Spalle 	struct vpci_pbm *pbm = pa->pa_pc->cookie;
375ea65b081Spalle 	uint64_t devhandle = pbm->vp_devhandle;
376ea65b081Spalle 	uint64_t devino = INTINO(*ihp);
377ea65b081Spalle 	DPRINTF(VDB_INTR, ("vpci_intr_map(): devino 0x%lx\n", devino));
378ea65b081Spalle 	uint64_t sysino;
379ea65b081Spalle 	int err;
380ea65b081Spalle 
381ea65b081Spalle 	if (*ihp != (pci_intr_handle_t)-1) {
382ea65b081Spalle 		err = hv_intr_devino_to_sysino(devhandle, devino, &sysino);
383ea65b081Spalle 		if (err != H_EOK)
384ea65b081Spalle 			return (-1);
385ea65b081Spalle 
386ea65b081Spalle 		KASSERT(sysino == INTVEC(sysino));
387ea65b081Spalle 		*ihp = sysino;
388ea65b081Spalle 		DPRINTF(VDB_INTR, ("vpci_intr_map(): sysino 0x%lx\n", sysino));
389ea65b081Spalle 		return (0);
390ea65b081Spalle 	}
391ea65b081Spalle 
392ea65b081Spalle 	return (-1);
393ea65b081Spalle }
394ea65b081Spalle 
395ea65b081Spalle bus_space_tag_t
vpci_alloc_mem_tag(struct vpci_pbm * vp)396ea65b081Spalle vpci_alloc_mem_tag(struct vpci_pbm *vp)
397ea65b081Spalle {
398ea65b081Spalle 	return (vpci_alloc_bus_tag(vp, "mem", PCI_MEMORY_BUS_SPACE));
399ea65b081Spalle }
400ea65b081Spalle 
401ea65b081Spalle bus_space_tag_t
vpci_alloc_io_tag(struct vpci_pbm * vp)402ea65b081Spalle vpci_alloc_io_tag(struct vpci_pbm *vp)
403ea65b081Spalle {
404ea65b081Spalle 	return (vpci_alloc_bus_tag(vp, "io", PCI_IO_BUS_SPACE));
405ea65b081Spalle }
406ea65b081Spalle 
407ea65b081Spalle bus_space_tag_t
vpci_alloc_config_tag(struct vpci_pbm * vp)408ea65b081Spalle vpci_alloc_config_tag(struct vpci_pbm *vp)
409ea65b081Spalle {
410ea65b081Spalle 	return (vpci_alloc_bus_tag(vp, "cfg", PCI_CONFIG_BUS_SPACE));
411ea65b081Spalle }
412ea65b081Spalle 
413ea65b081Spalle bus_space_tag_t
vpci_alloc_bus_tag(struct vpci_pbm * pbm,const char * name,int type)414ea65b081Spalle vpci_alloc_bus_tag(struct vpci_pbm *pbm, const char *name, int type)
415ea65b081Spalle {
416ea65b081Spalle 	struct vpci_softc *sc = pbm->vp_sc;
417ea65b081Spalle 	struct sparc_bus_space_tag *bt;
418ea65b081Spalle 
419fc256c4aSthorpej 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
420ea65b081Spalle 
421ea65b081Spalle #if 0
422ea65b081Spalle 	snprintf(bt->name, sizeof(bt->name), "%s-pbm_%s(%d/%2.2x)",
423ea65b081Spalle 	    device_xname(sc->sc_dev), name, ss, asi);
424ea65b081Spalle #endif
425ea65b081Spalle 
426ea65b081Spalle 	bt->cookie = pbm;
427ea65b081Spalle 	bt->parent = sc->sc_bustag;
428ea65b081Spalle 	bt->type = type;
429ea65b081Spalle 	bt->sparc_bus_map = vpci_bus_map;
430ea65b081Spalle 	bt->sparc_bus_mmap = vpci_bus_mmap;
431ea65b081Spalle 	bt->sparc_intr_establish = vpci_intr_establish;
432ea65b081Spalle 	return (bt);
433ea65b081Spalle }
434ea65b081Spalle 
435ea65b081Spalle bus_dma_tag_t
vpci_alloc_dma_tag(struct vpci_pbm * pbm)436ea65b081Spalle vpci_alloc_dma_tag(struct vpci_pbm *pbm)
437ea65b081Spalle {
438ea65b081Spalle 	struct vpci_softc *sc = pbm->vp_sc;
439ea65b081Spalle 	bus_dma_tag_t dt, pdt = sc->sc_dmat;
440ea65b081Spalle 
441fc256c4aSthorpej 	dt = kmem_zalloc(sizeof(*dt), KM_SLEEP);
442ea65b081Spalle 	dt->_cookie = pbm;
443ea65b081Spalle 	dt->_parent = pdt;
444ea65b081Spalle #define PCOPY(x)	dt->x = pdt->x
445ea65b081Spalle 	dt->_dmamap_create	= vpci_dmamap_create;
446ea65b081Spalle 	PCOPY(_dmamap_destroy);
447ea65b081Spalle 	dt->_dmamap_load	= iommu_dvmamap_load;
448ea65b081Spalle 	PCOPY(_dmamap_load_mbuf);
449ea65b081Spalle 	PCOPY(_dmamap_load_uio);
450ea65b081Spalle 	dt->_dmamap_load_raw	= iommu_dvmamap_load_raw;
451ea65b081Spalle 	dt->_dmamap_unload	= iommu_dvmamap_unload;
452ea65b081Spalle 	dt->_dmamap_sync	= iommu_dvmamap_sync;
453ea65b081Spalle 	dt->_dmamem_alloc	= iommu_dvmamem_alloc;
454ea65b081Spalle 	dt->_dmamem_free	= iommu_dvmamem_free;
455ea65b081Spalle 	dt->_dmamem_map         = iommu_dvmamem_map;
456ea65b081Spalle 	dt->_dmamem_unmap       = iommu_dvmamem_unmap;
457ea65b081Spalle 	PCOPY(_dmamem_mmap);
458ea65b081Spalle #undef	PCOPY
459ea65b081Spalle 	return (dt);
460ea65b081Spalle }
461ea65b081Spalle 
462ea65b081Spalle pci_chipset_tag_t
vpci_alloc_chipset(struct vpci_pbm * pbm,int node,pci_chipset_tag_t pc)463ea65b081Spalle vpci_alloc_chipset(struct vpci_pbm *pbm, int node, pci_chipset_tag_t pc)
464ea65b081Spalle {
465ea65b081Spalle 	pci_chipset_tag_t npc;
466ea65b081Spalle 
467fc256c4aSthorpej 	npc = kmem_alloc(sizeof *npc, KM_SLEEP);
468ea65b081Spalle 	memcpy(npc, pc, sizeof *pc);
469ea65b081Spalle 	npc->cookie = pbm;
470ea65b081Spalle 	npc->rootnode = node;
471ea65b081Spalle 	npc->spc_conf_read = vpci_conf_read;
472ea65b081Spalle 	npc->spc_conf_write = vpci_conf_write;
473ea65b081Spalle 	npc->spc_intr_map = vpci_intr_map;
474ea65b081Spalle 	npc->spc_intr_establish = vpci_pci_intr_establish;
475ea65b081Spalle 	npc->spc_find_ino = NULL;
476ea65b081Spalle 	return (npc);
477ea65b081Spalle }
478ea65b081Spalle 
479ea65b081Spalle int
vpci_dmamap_create(bus_dma_tag_t t,bus_size_t size,int nsegments,bus_size_t maxsegsz,bus_size_t boundary,int flags,bus_dmamap_t * dmamp)480ea65b081Spalle vpci_dmamap_create(bus_dma_tag_t t, bus_size_t size,
481ea65b081Spalle     int nsegments, bus_size_t maxsegsz, bus_size_t boundary, int flags,
482ea65b081Spalle     bus_dmamap_t *dmamp)
483ea65b081Spalle {
484ea65b081Spalle 	struct vpci_pbm *pbm = t->_cookie;
485ea65b081Spalle 	int error;
486ea65b081Spalle 
487ea65b081Spalle 	error = bus_dmamap_create(t->_parent, size, nsegments, maxsegsz,
488ea65b081Spalle 				  boundary, flags, dmamp);
489ea65b081Spalle 	if (error == 0)
490ea65b081Spalle 		(*dmamp)->_dm_cookie = &pbm->vp_sb;
491ea65b081Spalle 	return error;
492ea65b081Spalle }
493ea65b081Spalle 
494ea65b081Spalle int
vpci_bus_map(bus_space_tag_t t,bus_addr_t offset,bus_size_t size,int flags,vaddr_t unused,bus_space_handle_t * hp)495ea65b081Spalle vpci_bus_map(bus_space_tag_t t, bus_addr_t offset,
496ea65b081Spalle     bus_size_t size, int flags, vaddr_t unused, bus_space_handle_t *hp)
497ea65b081Spalle {
498ea65b081Spalle 	struct vpci_pbm *pbm = t->cookie;
499ea65b081Spalle 	struct vpci_softc *sc = pbm->vp_sc;
500ea65b081Spalle 	int i, ss;
501ea65b081Spalle 
502688f2bd1Spalle 	DPRINTF(VDB_BUSMAP, ("vpci_bus_map: type %d off %qx sz %qx flags %d",
503ea65b081Spalle 	    t->type,
504ea65b081Spalle 	    (unsigned long long)offset,
505ea65b081Spalle 	    (unsigned long long)size,
506ea65b081Spalle 	    flags));
507ea65b081Spalle 
508ea65b081Spalle 	ss = sparc_pci_childspace(t->type);
509688f2bd1Spalle 	DPRINTF(VDB_BUSMAP, (" cspace %d\n", ss));
510ea65b081Spalle 
511ea65b081Spalle 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
512ea65b081Spalle 		printf("\n_vpci_bus_map: invalid parent");
513ea65b081Spalle 		return (EINVAL);
514ea65b081Spalle 	}
515ea65b081Spalle 
516ea65b081Spalle 	for (i = 0; i < pbm->vp_nrange; i++) {
517ea65b081Spalle 		bus_addr_t paddr;
518ea65b081Spalle 		struct vpci_range *pr = &pbm->vp_range[i];
519ea65b081Spalle 
520ea65b081Spalle 		if (((pr->cspace >> 24) & 0x03) != ss)
521ea65b081Spalle 			continue;
522ea65b081Spalle 
523ea65b081Spalle 		paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
524ea65b081Spalle 		return ((*sc->sc_bustag->sparc_bus_map)(t, paddr, size,
525ea65b081Spalle 			flags, 0, hp));
526ea65b081Spalle 	}
527ea65b081Spalle 
528ea65b081Spalle 	return (EINVAL);
529ea65b081Spalle }
530ea65b081Spalle 
531ea65b081Spalle paddr_t
vpci_bus_mmap(bus_space_tag_t t,bus_addr_t paddr,off_t off,int prot,int flags)532ea65b081Spalle vpci_bus_mmap(bus_space_tag_t t, bus_addr_t paddr,
533ea65b081Spalle     off_t off, int prot, int flags)
534ea65b081Spalle {
535ea65b081Spalle 	bus_addr_t offset = paddr;
536ea65b081Spalle 	struct vpci_pbm *pbm = t->cookie;
537ea65b081Spalle 	struct vpci_softc *sc = pbm->vp_sc;
538ea65b081Spalle 	int i, ss;
539ea65b081Spalle 
540ea65b081Spalle 	ss = sparc_pci_childspace(t->type);
541ea65b081Spalle 
542ea65b081Spalle 	DPRINTF(VDB_BUSMAP, ("vpci_bus_mmap: prot %d flags %d pa %qx\n",
543ea65b081Spalle 	    prot, flags, (unsigned long long)paddr));
544ea65b081Spalle 
545ea65b081Spalle 	if (t->parent == 0 || t->parent->sparc_bus_mmap == 0) {
546ea65b081Spalle 		printf("\n_vpci_bus_mmap: invalid parent");
547ea65b081Spalle 		return (-1);
548ea65b081Spalle 	}
549ea65b081Spalle 
550ea65b081Spalle 	for (i = 0; i < pbm->vp_nrange; i++) {
551ea65b081Spalle 		struct vpci_range *pr = &pbm->vp_range[i];
552ea65b081Spalle 
553ea65b081Spalle 		if (((pr->cspace >> 24) & 0x03) != ss)
554ea65b081Spalle 			continue;
555ea65b081Spalle 
556ea65b081Spalle 		paddr = BUS_ADDR(pr->phys_hi, pr->phys_lo + offset);
557ea65b081Spalle 		return (bus_space_mmap(sc->sc_bustag, paddr, off,
558ea65b081Spalle 				       prot, flags));
559ea65b081Spalle 	}
560ea65b081Spalle 
561ea65b081Spalle 	return (-1);
562ea65b081Spalle }
563ea65b081Spalle 
564ea65b081Spalle void *
vpci_intr_establish(bus_space_tag_t t,int ihandle,int level,int (* handler)(void *),void * arg,void (* fastvec)(void))565ea65b081Spalle vpci_intr_establish(bus_space_tag_t t, int ihandle, int level,
566ea65b081Spalle 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
567ea65b081Spalle {
568ea65b081Spalle 	struct intrhand *ih = NULL;
569ea65b081Spalle 	int ino;
570ea65b081Spalle 
571ea65b081Spalle 	ino = INTINO(ihandle);
572ea65b081Spalle 	DPRINTF(VDB_INTR, ("%s: ih %lx; level %d ino %#x\n", __func__, (u_long)ihandle, level, ino));
573ea65b081Spalle 
574ea65b081Spalle 	if (level == IPL_NONE) {
575ea65b081Spalle 		level = INTLEV(ihandle);
576ea65b081Spalle 		printf(": IPL_NONE, setting IPL %d.\n", level);
577ea65b081Spalle 	}
578ea65b081Spalle 	if (level == IPL_NONE) {
579ea65b081Spalle 		level = 2;
580ea65b081Spalle 		printf(": no IPL, setting IPL 2.\n");
581ea65b081Spalle 	}
582ea65b081Spalle 
583ea65b081Spalle 	ino |= INTVEC(ihandle);
584ea65b081Spalle 	DPRINTF(VDB_INTR, ("%s: ih %lx; level %d ino %#x\n", __func__, (u_long)ihandle, level, ino));
585ea65b081Spalle 
5867820dc41Spalle 	ih = intrhand_alloc();
587ea65b081Spalle 
588ea65b081Spalle 	ih->ih_ivec = ihandle;
589ea65b081Spalle 	ih->ih_fun = handler;
590ea65b081Spalle 	ih->ih_arg = arg;
591ea65b081Spalle 	ih->ih_pil = level;
592ea65b081Spalle 	ih->ih_number = ino;
593ea65b081Spalle 	ih->ih_pending = 0;
5947820dc41Spalle 	ih->ih_ack = vpci_intr_ack;
595ea65b081Spalle 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
596ea65b081Spalle 
5977820dc41Spalle 	uint64_t sysino = INTVEC(ihandle);
5987820dc41Spalle 	DPRINTF(VDB_INTR, ("vpci_intr_establish(): sysino 0x%lx\n", sysino));
5997820dc41Spalle 
6007820dc41Spalle 	int err;
6017820dc41Spalle 
6027820dc41Spalle 	err = hv_intr_settarget(sysino, cpus->ci_cpuid);
6037820dc41Spalle 	if (err != H_EOK)
6047820dc41Spalle 		printf("hv_intr_settarget(%lu, %u) failed - err = %d\n",
6057820dc41Spalle 		       (long unsigned int)sysino, cpus->ci_cpuid, err);
6067820dc41Spalle 
6077820dc41Spalle 	/* Clear pending interrupts. */
6087820dc41Spalle 	err = hv_intr_setstate(sysino, INTR_IDLE);
6097820dc41Spalle 	if (err != H_EOK)
6107820dc41Spalle 	  printf("hv_intr_setstate(%lu, INTR_IDLE) failed - err = %d\n",
6117820dc41Spalle 		(long unsigned int)sysino, err);
6127820dc41Spalle 
6137820dc41Spalle 	err = hv_intr_setenabled(sysino, INTR_ENABLED);
6147820dc41Spalle 	if (err != H_EOK)
6157820dc41Spalle 	  printf("hv_intr_setenabled(%lu) failed - err = %d\n",
6167820dc41Spalle 		(long unsigned int)sysino, err);
6177820dc41Spalle 
6187820dc41Spalle 	DPRINTF(VDB_INTR, ("%s() returning %p\n", __func__, ih));
619ea65b081Spalle 	return (ih);
620ea65b081Spalle }
621ea65b081Spalle 
6227820dc41Spalle void
vpci_intr_ack(struct intrhand * ih)6237820dc41Spalle vpci_intr_ack(struct intrhand *ih)
6247820dc41Spalle {
6257820dc41Spalle 	int err;
6267820dc41Spalle 	err = hv_intr_setstate(ih->ih_number, INTR_IDLE);
6277820dc41Spalle 	if (err != H_EOK)
6287820dc41Spalle 	  panic("%s(%u, INTR_IDLE) failed - err = %d\n",
6297820dc41Spalle 		__func__, ih->ih_number, err);
6307820dc41Spalle }
6317820dc41Spalle 
632ea65b081Spalle static void *
vpci_pci_intr_establish(pci_chipset_tag_t pc,pci_intr_handle_t ih,int level,int (* func)(void *),void * arg)633ea65b081Spalle vpci_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
634ea65b081Spalle 	int (*func)(void *), void *arg)
635ea65b081Spalle {
636ea65b081Spalle 	void *cookie;
637ea65b081Spalle 	struct vpci_pbm *pbm = (struct vpci_pbm *)pc->cookie;
638ea65b081Spalle 
639ea65b081Spalle 	DPRINTF(VDB_INTR, ("%s: ih %lx; level %d\n", __func__, (u_long)ih, level));
640ea65b081Spalle 	cookie = bus_intr_establish(pbm->vp_memt, ih, level, func, arg);
641ea65b081Spalle 
642ea65b081Spalle 	DPRINTF(VDB_INTR, ("%s: returning handle %p\n", __func__, cookie));
643ea65b081Spalle 	return (cookie);
644ea65b081Spalle }
645