xref: /netbsd-src/sys/arch/riscv/fdt/pcihost_fdt.c (revision 05276bf3c24743a2bb3b218fc21708914b5da491)
1*05276bf3Sskrll /* $NetBSD: pcihost_fdt.c,v 1.1 2025/01/01 17:53:07 skrll Exp $ */
2*05276bf3Sskrll 
3*05276bf3Sskrll /*-
4*05276bf3Sskrll  * Copyright (c) 2018 Jared D. McNeill <jmcneill@invisible.ca>
5*05276bf3Sskrll  * All rights reserved.
6*05276bf3Sskrll  *
7*05276bf3Sskrll  * Redistribution and use in source and binary forms, with or without
8*05276bf3Sskrll  * modification, are permitted provided that the following conditions
9*05276bf3Sskrll  * are met:
10*05276bf3Sskrll  * 1. Redistributions of source code must retain the above copyright
11*05276bf3Sskrll  *    notice, this list of conditions and the following disclaimer.
12*05276bf3Sskrll  * 2. Redistributions in binary form must reproduce the above copyright
13*05276bf3Sskrll  *    notice, this list of conditions and the following disclaimer in the
14*05276bf3Sskrll  *    documentation and/or other materials provided with the distribution.
15*05276bf3Sskrll  *
16*05276bf3Sskrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*05276bf3Sskrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*05276bf3Sskrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*05276bf3Sskrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*05276bf3Sskrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*05276bf3Sskrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*05276bf3Sskrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*05276bf3Sskrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*05276bf3Sskrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*05276bf3Sskrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*05276bf3Sskrll  * SUCH DAMAGE.
27*05276bf3Sskrll  */
28*05276bf3Sskrll 
29*05276bf3Sskrll #include <sys/cdefs.h>
30*05276bf3Sskrll __KERNEL_RCSID(0, "$NetBSD: pcihost_fdt.c,v 1.1 2025/01/01 17:53:07 skrll Exp $");
31*05276bf3Sskrll 
32*05276bf3Sskrll #include <sys/param.h>
33*05276bf3Sskrll 
34*05276bf3Sskrll #include <sys/bus.h>
35*05276bf3Sskrll #include <sys/device.h>
36*05276bf3Sskrll #include <sys/intr.h>
37*05276bf3Sskrll #include <sys/kernel.h>
38*05276bf3Sskrll #include <sys/kmem.h>
39*05276bf3Sskrll #include <sys/lwp.h>
40*05276bf3Sskrll #include <sys/mutex.h>
41*05276bf3Sskrll #include <sys/queue.h>
42*05276bf3Sskrll #include <sys/systm.h>
43*05276bf3Sskrll 
44*05276bf3Sskrll #include <machine/cpu.h>
45*05276bf3Sskrll 
46*05276bf3Sskrll #include <dev/pci/pcireg.h>
47*05276bf3Sskrll #include <dev/pci/pcivar.h>
48*05276bf3Sskrll #include <dev/pci/pciconf.h>
49*05276bf3Sskrll 
50*05276bf3Sskrll #include <dev/fdt/fdtvar.h>
51*05276bf3Sskrll 
52*05276bf3Sskrll #include <riscv/fdt/pcihost_fdtvar.h>
53*05276bf3Sskrll 
54*05276bf3Sskrll #define	PCIHOST_DEFAULT_BUS_MIN		0
55*05276bf3Sskrll #define	PCIHOST_DEFAULT_BUS_MAX		255
56*05276bf3Sskrll 
57*05276bf3Sskrll #define	PCIHOST_CACHELINE_SIZE		64 /* riscv_dcache_align */
58*05276bf3Sskrll 
59*05276bf3Sskrll int pcihost_segment = 0;
60*05276bf3Sskrll 
61*05276bf3Sskrll static int	pcihost_match(device_t, cfdata_t, void *);
62*05276bf3Sskrll static void	pcihost_attach(device_t, device_t, void *);
63*05276bf3Sskrll 
64*05276bf3Sskrll static int	pcihost_config(struct pcihost_softc *);
65*05276bf3Sskrll 
66*05276bf3Sskrll static void	pcihost_attach_hook(device_t, device_t,
67*05276bf3Sskrll 				       struct pcibus_attach_args *);
68*05276bf3Sskrll static int	pcihost_bus_maxdevs(void *, int);
69*05276bf3Sskrll static pcitag_t	pcihost_make_tag(void *, int, int, int);
70*05276bf3Sskrll static void	pcihost_decompose_tag(void *, pcitag_t, int *, int *, int *);
71*05276bf3Sskrll static u_int	pcihost_get_segment(void *);
72*05276bf3Sskrll static pcireg_t	pcihost_conf_read(void *, pcitag_t, int);
73*05276bf3Sskrll static void	pcihost_conf_write(void *, pcitag_t, int, pcireg_t);
74*05276bf3Sskrll static int	pcihost_conf_hook(void *, int, int, int, pcireg_t);
75*05276bf3Sskrll static void	pcihost_conf_interrupt(void *, int, int, int, int, int *);
76*05276bf3Sskrll 
77*05276bf3Sskrll static int	pcihost_intr_map(const struct pci_attach_args *,
78*05276bf3Sskrll 				    pci_intr_handle_t *);
79*05276bf3Sskrll static const char *pcihost_intr_string(void *, pci_intr_handle_t,
80*05276bf3Sskrll 					  char *, size_t);
81*05276bf3Sskrll static const struct evcnt *pcihost_intr_evcnt(void *, pci_intr_handle_t);
82*05276bf3Sskrll static int	pcihost_intr_setattr(void *, pci_intr_handle_t *, int,
83*05276bf3Sskrll 					uint64_t);
84*05276bf3Sskrll static void *	pcihost_intr_establish(void *, pci_intr_handle_t,
85*05276bf3Sskrll 					 int, int (*)(void *), void *,
86*05276bf3Sskrll 					 const char *);
87*05276bf3Sskrll static void	pcihost_intr_disestablish(void *, void *);
88*05276bf3Sskrll 
89*05276bf3Sskrll static int	pcihost_bus_space_map(void *, bus_addr_t, bus_size_t,
90*05276bf3Sskrll 		int, bus_space_handle_t *);
91*05276bf3Sskrll 
92*05276bf3Sskrll CFATTACH_DECL_NEW(pcihost_fdt, sizeof(struct pcihost_softc),
93*05276bf3Sskrll 	pcihost_match, pcihost_attach, NULL, NULL);
94*05276bf3Sskrll 
95*05276bf3Sskrll static const struct device_compatible_entry compat_data[] = {
96*05276bf3Sskrll 	{ .compat = "pci-host-cam-generic",	.value = PCIHOST_CAM },
97*05276bf3Sskrll 	{ .compat = "pci-host-ecam-generic",	.value = PCIHOST_ECAM },
98*05276bf3Sskrll 	DEVICE_COMPAT_EOL
99*05276bf3Sskrll };
100*05276bf3Sskrll 
101*05276bf3Sskrll static int
102*05276bf3Sskrll pcihost_match(device_t parent, cfdata_t cf, void *aux)
103*05276bf3Sskrll {
104*05276bf3Sskrll 	struct fdt_attach_args * const faa = aux;
105*05276bf3Sskrll 
106*05276bf3Sskrll 	return of_compatible_match(faa->faa_phandle, compat_data);
107*05276bf3Sskrll }
108*05276bf3Sskrll 
109*05276bf3Sskrll static void
110*05276bf3Sskrll pcihost_attach(device_t parent, device_t self, void *aux)
111*05276bf3Sskrll {
112*05276bf3Sskrll 	struct pcihost_softc * const sc = device_private(self);
113*05276bf3Sskrll 	struct fdt_attach_args * const faa = aux;
114*05276bf3Sskrll 	bus_addr_t cs_addr;
115*05276bf3Sskrll 	bus_size_t cs_size;
116*05276bf3Sskrll 	int error;
117*05276bf3Sskrll 
118*05276bf3Sskrll 	if (fdtbus_get_reg(faa->faa_phandle, 0, &cs_addr, &cs_size) != 0) {
119*05276bf3Sskrll 		aprint_error(": couldn't get registers\n");
120*05276bf3Sskrll 		return;
121*05276bf3Sskrll 	}
122*05276bf3Sskrll 
123*05276bf3Sskrll 	sc->sc_dev = self;
124*05276bf3Sskrll 	sc->sc_dmat = faa->faa_dmat;
125*05276bf3Sskrll 	sc->sc_bst = faa->faa_bst;
126*05276bf3Sskrll 	sc->sc_pci_bst = faa->faa_bst;
127*05276bf3Sskrll 	sc->sc_phandle = faa->faa_phandle;
128*05276bf3Sskrll 	error = bus_space_map(sc->sc_bst, cs_addr, cs_size,
129*05276bf3Sskrll 	    0, &sc->sc_bsh);
130*05276bf3Sskrll 	if (error) {
131*05276bf3Sskrll 		aprint_error(": couldn't map registers: %d\n", error);
132*05276bf3Sskrll 		return;
133*05276bf3Sskrll 	}
134*05276bf3Sskrll 	sc->sc_type = of_compatible_lookup(sc->sc_phandle, compat_data)->value;
135*05276bf3Sskrll 
136*05276bf3Sskrll 	aprint_naive("\n");
137*05276bf3Sskrll 	aprint_normal(": Generic PCI host controller\n");
138*05276bf3Sskrll 
139*05276bf3Sskrll 	pcihost_init(&sc->sc_pc, sc);
140*05276bf3Sskrll 	pcihost_init2(sc);
141*05276bf3Sskrll }
142*05276bf3Sskrll 
143*05276bf3Sskrll void
144*05276bf3Sskrll pcihost_init2(struct pcihost_softc *sc)
145*05276bf3Sskrll {
146*05276bf3Sskrll 	struct pcibus_attach_args pba;
147*05276bf3Sskrll 	const u_int *data;
148*05276bf3Sskrll 	int len;
149*05276bf3Sskrll 
150*05276bf3Sskrll 	if ((data = fdtbus_get_prop(sc->sc_phandle, "bus-range", &len)) != NULL) {
151*05276bf3Sskrll 		if (len != 8) {
152*05276bf3Sskrll 			aprint_error_dev(sc->sc_dev, "malformed 'bus-range' property\n");
153*05276bf3Sskrll 			return;
154*05276bf3Sskrll 		}
155*05276bf3Sskrll 		sc->sc_bus_min = be32toh(data[0]);
156*05276bf3Sskrll 		sc->sc_bus_max = be32toh(data[1]);
157*05276bf3Sskrll 	} else {
158*05276bf3Sskrll 		sc->sc_bus_min = PCIHOST_DEFAULT_BUS_MIN;
159*05276bf3Sskrll 		sc->sc_bus_max = PCIHOST_DEFAULT_BUS_MAX;
160*05276bf3Sskrll 	}
161*05276bf3Sskrll 
162*05276bf3Sskrll 	/*
163*05276bf3Sskrll 	 * Assign a fixed PCI segment ("domain") number. If the property is not
164*05276bf3Sskrll 	 * present, assign one. The binding spec says if this property is used to
165*05276bf3Sskrll 	 * assign static segment numbers, all host bridges should have segments
166*05276bf3Sskrll 	 * astatic assigned to prevent overlaps.
167*05276bf3Sskrll 	 */
168*05276bf3Sskrll 	if (of_getprop_uint32(sc->sc_phandle, "linux,pci-domain", &sc->sc_seg))
169*05276bf3Sskrll 		sc->sc_seg = pcihost_segment++;
170*05276bf3Sskrll 
171*05276bf3Sskrll 	if (pcihost_config(sc) != 0)
172*05276bf3Sskrll 		return;
173*05276bf3Sskrll 
174*05276bf3Sskrll 	memset(&pba, 0, sizeof(pba));
175*05276bf3Sskrll 	pba.pba_flags = PCI_FLAGS_MRL_OKAY |
176*05276bf3Sskrll 			PCI_FLAGS_MRM_OKAY |
177*05276bf3Sskrll 			PCI_FLAGS_MWI_OKAY |
178*05276bf3Sskrll 			sc->sc_pci_flags;
179*05276bf3Sskrll 	pba.pba_iot = &sc->sc_io.bst;
180*05276bf3Sskrll 	pba.pba_memt = &sc->sc_mem.bst;
181*05276bf3Sskrll 	pba.pba_dmat = sc->sc_dmat;
182*05276bf3Sskrll #ifdef _PCI_HAVE_DMA64
183*05276bf3Sskrll 	pba.pba_dmat64 = sc->sc_dmat;
184*05276bf3Sskrll #endif
185*05276bf3Sskrll 	pba.pba_pc = &sc->sc_pc;
186*05276bf3Sskrll 	pba.pba_bus = sc->sc_bus_min;
187*05276bf3Sskrll 
188*05276bf3Sskrll 	config_found(sc->sc_dev, &pba, pcibusprint,
189*05276bf3Sskrll 	    CFARGS(.devhandle = device_handle(sc->sc_dev)));
190*05276bf3Sskrll }
191*05276bf3Sskrll 
192*05276bf3Sskrll void
193*05276bf3Sskrll pcihost_init(pci_chipset_tag_t pc, void *priv)
194*05276bf3Sskrll {
195*05276bf3Sskrll 	pc->pc_conf_v = priv;
196*05276bf3Sskrll 	pc->pc_attach_hook = pcihost_attach_hook;
197*05276bf3Sskrll 	pc->pc_bus_maxdevs = pcihost_bus_maxdevs;
198*05276bf3Sskrll 	pc->pc_make_tag = pcihost_make_tag;
199*05276bf3Sskrll 	pc->pc_decompose_tag = pcihost_decompose_tag;
200*05276bf3Sskrll 	pc->pc_get_segment = pcihost_get_segment;
201*05276bf3Sskrll 	pc->pc_conf_read = pcihost_conf_read;
202*05276bf3Sskrll 	pc->pc_conf_write = pcihost_conf_write;
203*05276bf3Sskrll 	pc->pc_conf_hook = pcihost_conf_hook;
204*05276bf3Sskrll 	pc->pc_conf_interrupt = pcihost_conf_interrupt;
205*05276bf3Sskrll 
206*05276bf3Sskrll 	pc->pc_intr_v = priv;
207*05276bf3Sskrll 	pc->pc_intr_map = pcihost_intr_map;
208*05276bf3Sskrll 	pc->pc_intr_string = pcihost_intr_string;
209*05276bf3Sskrll 	pc->pc_intr_evcnt = pcihost_intr_evcnt;
210*05276bf3Sskrll 	pc->pc_intr_setattr = pcihost_intr_setattr;
211*05276bf3Sskrll 	pc->pc_intr_establish = pcihost_intr_establish;
212*05276bf3Sskrll 	pc->pc_intr_disestablish = pcihost_intr_disestablish;
213*05276bf3Sskrll }
214*05276bf3Sskrll 
215*05276bf3Sskrll static int
216*05276bf3Sskrll pcihost_config(struct pcihost_softc *sc)
217*05276bf3Sskrll {
218*05276bf3Sskrll 	const u_int *ranges;
219*05276bf3Sskrll 	u_int probe_only;
220*05276bf3Sskrll 	int error, len, type;
221*05276bf3Sskrll 	bool swap;
222*05276bf3Sskrll 
223*05276bf3Sskrll 	struct pcih_bus_space * const pibs = &sc->sc_io;
224*05276bf3Sskrll 	pibs->bst = *sc->sc_pci_bst;
225*05276bf3Sskrll 	pibs->bst.bs_cookie = pibs;
226*05276bf3Sskrll 	pibs->map = pibs->bst.bs_map;
227*05276bf3Sskrll 	pibs->flags = PCI_FLAGS_IO_OKAY;
228*05276bf3Sskrll 	pibs->bst.bs_map = pcihost_bus_space_map;
229*05276bf3Sskrll 
230*05276bf3Sskrll 	struct pcih_bus_space * const pmbs = &sc->sc_mem;
231*05276bf3Sskrll 	pmbs->bst = *sc->sc_pci_bst;
232*05276bf3Sskrll 	pmbs->bst.bs_cookie = pmbs;
233*05276bf3Sskrll 	pmbs->map = pmbs->bst.bs_map;
234*05276bf3Sskrll 	pmbs->flags = PCI_FLAGS_MEM_OKAY;
235*05276bf3Sskrll 	pmbs->bst.bs_map = pcihost_bus_space_map;
236*05276bf3Sskrll 
237*05276bf3Sskrll 	/*
238*05276bf3Sskrll 	 * If this flag is set, skip configuration of the PCI bus and use
239*05276bf3Sskrll 	 * existing config.
240*05276bf3Sskrll 	 */
241*05276bf3Sskrll 	const int chosen = OF_finddevice("/chosen");
242*05276bf3Sskrll 	if (chosen <= 0 || of_getprop_uint32(chosen, "linux,pci-probe-only", &probe_only))
243*05276bf3Sskrll 		probe_only = 0;
244*05276bf3Sskrll 
245*05276bf3Sskrll 	if (sc->sc_pci_ranges != NULL) {
246*05276bf3Sskrll 		ranges = sc->sc_pci_ranges;
247*05276bf3Sskrll 		len = sc->sc_pci_ranges_cells * 4;
248*05276bf3Sskrll 		swap = false;
249*05276bf3Sskrll 	} else {
250*05276bf3Sskrll 		ranges = fdtbus_get_prop(sc->sc_phandle, "ranges", &len);
251*05276bf3Sskrll 		if (ranges == NULL) {
252*05276bf3Sskrll 			aprint_error_dev(sc->sc_dev, "missing 'ranges' property\n");
253*05276bf3Sskrll 			return EINVAL;
254*05276bf3Sskrll 		}
255*05276bf3Sskrll 		swap = true;
256*05276bf3Sskrll 	}
257*05276bf3Sskrll 	struct pciconf_resources *pcires = pciconf_resource_init();
258*05276bf3Sskrll 
259*05276bf3Sskrll 	/*
260*05276bf3Sskrll 	 * Each entry in the ranges table contains:
261*05276bf3Sskrll 	 *  - bus address (3 cells)
262*05276bf3Sskrll 	 *  - cpu physical address (2 cells)
263*05276bf3Sskrll 	 *  - size (2 cells)
264*05276bf3Sskrll 	 * Total size for each entry is 28 bytes (7 cells).
265*05276bf3Sskrll 	 */
266*05276bf3Sskrll 	while (len >= 28) {
267*05276bf3Sskrll #define	DECODE32(x,o)	(swap ? be32dec(&(x)[o]) : (x)[o])
268*05276bf3Sskrll #define	DECODE64(x,o)	(swap ? be64dec(&(x)[o]) : (((uint64_t)((x)[(o)+0]) << 32) + (x)[(o)+1]))
269*05276bf3Sskrll 		const uint32_t phys_hi = DECODE32(ranges, 0);
270*05276bf3Sskrll 		      uint64_t bus_phys = DECODE64(ranges, 1);
271*05276bf3Sskrll 		const uint64_t cpu_phys = DECODE64(ranges, 3);
272*05276bf3Sskrll 		      uint64_t size = DECODE64(ranges, 5);
273*05276bf3Sskrll #undef	DECODE32
274*05276bf3Sskrll #undef	DECODE64
275*05276bf3Sskrll 
276*05276bf3Sskrll 		len -= 28;
277*05276bf3Sskrll 		ranges += 7;
278*05276bf3Sskrll 
279*05276bf3Sskrll 		const bool is64 = (__SHIFTOUT(phys_hi, PHYS_HI_SPACE) ==
280*05276bf3Sskrll 		    PHYS_HI_SPACE_MEM64) ? true : false;
281*05276bf3Sskrll 		switch (__SHIFTOUT(phys_hi, PHYS_HI_SPACE)) {
282*05276bf3Sskrll 		case PHYS_HI_SPACE_IO:
283*05276bf3Sskrll 			if (pibs->nranges + 1 >= __arraycount(pibs->ranges)) {
284*05276bf3Sskrll 				aprint_error_dev(sc->sc_dev, "too many IO ranges\n");
285*05276bf3Sskrll 				continue;
286*05276bf3Sskrll 			}
287*05276bf3Sskrll 			pibs->ranges[pibs->nranges].bpci = bus_phys;
288*05276bf3Sskrll 			pibs->ranges[pibs->nranges].bbus = cpu_phys;
289*05276bf3Sskrll 			pibs->ranges[pibs->nranges].size = size;
290*05276bf3Sskrll 			++pibs->nranges;
291*05276bf3Sskrll 			aprint_verbose_dev(sc->sc_dev,
292*05276bf3Sskrll 			    "IO: %#018" PRIx64 " + %#018" PRIx64 " @ %#018" PRIx64 "\n",
293*05276bf3Sskrll 			    bus_phys, size, cpu_phys);
294*05276bf3Sskrll 			/*
295*05276bf3Sskrll 			 * Reserve a PC-like legacy IO ports range, perhaps
296*05276bf3Sskrll 			 * for access to VGA registers.
297*05276bf3Sskrll 			 */
298*05276bf3Sskrll 			if (bus_phys == 0 && size >= 0x10000) {
299*05276bf3Sskrll 				bus_phys += 0x1000;
300*05276bf3Sskrll 				size -= 0x1000;
301*05276bf3Sskrll 			}
302*05276bf3Sskrll 			error = pciconf_resource_add(pcires,
303*05276bf3Sskrll 			    PCICONF_RESOURCE_IO, bus_phys, size);
304*05276bf3Sskrll 			if (error == 0)
305*05276bf3Sskrll 				sc->sc_pci_flags |= PCI_FLAGS_IO_OKAY;
306*05276bf3Sskrll 			break;
307*05276bf3Sskrll 		case PHYS_HI_SPACE_MEM64:
308*05276bf3Sskrll 			/* FALLTHROUGH */
309*05276bf3Sskrll 		case PHYS_HI_SPACE_MEM32:
310*05276bf3Sskrll 			if (pmbs->nranges + 1 >= __arraycount(pmbs->ranges)) {
311*05276bf3Sskrll 				aprint_error_dev(sc->sc_dev, "too many mem ranges\n");
312*05276bf3Sskrll 				continue;
313*05276bf3Sskrll 			}
314*05276bf3Sskrll 			/* both pmem and mem spaces are in the same tag */
315*05276bf3Sskrll 			pmbs->ranges[pmbs->nranges].bpci = bus_phys;
316*05276bf3Sskrll 			pmbs->ranges[pmbs->nranges].bbus = cpu_phys;
317*05276bf3Sskrll 			pmbs->ranges[pmbs->nranges].size = size;
318*05276bf3Sskrll 			++pmbs->nranges;
319*05276bf3Sskrll 			if ((phys_hi & PHYS_HI_PREFETCH) != 0 ||
320*05276bf3Sskrll 			    __SHIFTOUT(phys_hi, PHYS_HI_SPACE) == PHYS_HI_SPACE_MEM64) {
321*05276bf3Sskrll 				type = PCICONF_RESOURCE_PREFETCHABLE_MEM;
322*05276bf3Sskrll 				aprint_verbose_dev(sc->sc_dev,
323*05276bf3Sskrll 				    "MMIO (%d-bit prefetchable)    : %#018" PRIx64 " + %#018" PRIx64 " @ %#018" PRIx64 "\n",
324*05276bf3Sskrll 				    is64 ? 64 : 32, bus_phys, size, cpu_phys);
325*05276bf3Sskrll 			} else {
326*05276bf3Sskrll 				type = PCICONF_RESOURCE_MEM;
327*05276bf3Sskrll 				aprint_verbose_dev(sc->sc_dev,
328*05276bf3Sskrll 				    "MMIO (%d-bit non-prefetchable): %#018" PRIx64 " + %#018" PRIx64 " @ %#018" PRIx64 "\n",
329*05276bf3Sskrll 				    is64 ? 64 : 32, bus_phys, size, cpu_phys);
330*05276bf3Sskrll 			}
331*05276bf3Sskrll 			error = pciconf_resource_add(pcires, type, bus_phys,
332*05276bf3Sskrll 			    size);
333*05276bf3Sskrll 			if (error == 0)
334*05276bf3Sskrll 				sc->sc_pci_flags |= PCI_FLAGS_MEM_OKAY;
335*05276bf3Sskrll 			break;
336*05276bf3Sskrll 		default:
337*05276bf3Sskrll 			break;
338*05276bf3Sskrll 		}
339*05276bf3Sskrll 	}
340*05276bf3Sskrll 
341*05276bf3Sskrll 	if (probe_only) {
342*05276bf3Sskrll 		error = 0;
343*05276bf3Sskrll 	} else {
344*05276bf3Sskrll 		error = pci_configure_bus(&sc->sc_pc, pcires, sc->sc_bus_min,
345*05276bf3Sskrll 		    PCIHOST_CACHELINE_SIZE);
346*05276bf3Sskrll 	}
347*05276bf3Sskrll 
348*05276bf3Sskrll 	pciconf_resource_fini(pcires);
349*05276bf3Sskrll 	if (error) {
350*05276bf3Sskrll 		aprint_error_dev(sc->sc_dev, "configuration failed: %d\n", error);
351*05276bf3Sskrll 		return error;
352*05276bf3Sskrll 	}
353*05276bf3Sskrll 
354*05276bf3Sskrll 	return 0;
355*05276bf3Sskrll }
356*05276bf3Sskrll 
357*05276bf3Sskrll static void
358*05276bf3Sskrll pcihost_attach_hook(device_t parent, device_t self,
359*05276bf3Sskrll     struct pcibus_attach_args *pba)
360*05276bf3Sskrll {
361*05276bf3Sskrll }
362*05276bf3Sskrll 
363*05276bf3Sskrll static int
364*05276bf3Sskrll pcihost_bus_maxdevs(void *v, int busno)
365*05276bf3Sskrll {
366*05276bf3Sskrll 	return 32;
367*05276bf3Sskrll }
368*05276bf3Sskrll 
369*05276bf3Sskrll static pcitag_t
370*05276bf3Sskrll pcihost_make_tag(void *v, int b, int d, int f)
371*05276bf3Sskrll {
372*05276bf3Sskrll 	return (b << 16) | (d << 11) | (f << 8);
373*05276bf3Sskrll }
374*05276bf3Sskrll 
375*05276bf3Sskrll static void
376*05276bf3Sskrll pcihost_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
377*05276bf3Sskrll {
378*05276bf3Sskrll 	if (bp)
379*05276bf3Sskrll 		*bp = (tag >> 16) & 0xff;
380*05276bf3Sskrll 	if (dp)
381*05276bf3Sskrll 		*dp = (tag >> 11) & 0x1f;
382*05276bf3Sskrll 	if (fp)
383*05276bf3Sskrll 		*fp = (tag >> 8) & 0x7;
384*05276bf3Sskrll }
385*05276bf3Sskrll 
386*05276bf3Sskrll static u_int
387*05276bf3Sskrll pcihost_get_segment(void *v)
388*05276bf3Sskrll {
389*05276bf3Sskrll 	struct pcihost_softc *sc = v;
390*05276bf3Sskrll 
391*05276bf3Sskrll 	return sc->sc_seg;
392*05276bf3Sskrll }
393*05276bf3Sskrll 
394*05276bf3Sskrll static pcireg_t
395*05276bf3Sskrll pcihost_conf_read(void *v, pcitag_t tag, int offset)
396*05276bf3Sskrll {
397*05276bf3Sskrll 	struct pcihost_softc *sc = v;
398*05276bf3Sskrll 	int b, d, f;
399*05276bf3Sskrll 	u_int reg;
400*05276bf3Sskrll 
401*05276bf3Sskrll 	pcihost_decompose_tag(v, tag, &b, &d, &f);
402*05276bf3Sskrll 
403*05276bf3Sskrll 	if (b < sc->sc_bus_min || b > sc->sc_bus_max)
404*05276bf3Sskrll 		return (pcireg_t) -1;
405*05276bf3Sskrll 
406*05276bf3Sskrll 	if (sc->sc_type == PCIHOST_CAM) {
407*05276bf3Sskrll 		if (offset & ~0xff)
408*05276bf3Sskrll 			return (pcireg_t) -1;
409*05276bf3Sskrll 		reg = (b << 16) | (d << 11) | (f << 8) | offset;
410*05276bf3Sskrll 	} else if (sc->sc_type == PCIHOST_ECAM) {
411*05276bf3Sskrll 		if (offset & ~0xfff)
412*05276bf3Sskrll 			return (pcireg_t) -1;
413*05276bf3Sskrll 		reg = (b << 20) | (d << 15) | (f << 12) | offset;
414*05276bf3Sskrll 	} else {
415*05276bf3Sskrll 		return (pcireg_t) -1;
416*05276bf3Sskrll 	}
417*05276bf3Sskrll 
418*05276bf3Sskrll 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
419*05276bf3Sskrll }
420*05276bf3Sskrll 
421*05276bf3Sskrll static void
422*05276bf3Sskrll pcihost_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
423*05276bf3Sskrll {
424*05276bf3Sskrll 	struct pcihost_softc *sc = v;
425*05276bf3Sskrll 	int b, d, f;
426*05276bf3Sskrll 	u_int reg;
427*05276bf3Sskrll 
428*05276bf3Sskrll 	pcihost_decompose_tag(v, tag, &b, &d, &f);
429*05276bf3Sskrll 
430*05276bf3Sskrll 	if (b < sc->sc_bus_min || b > sc->sc_bus_max)
431*05276bf3Sskrll 		return;
432*05276bf3Sskrll 
433*05276bf3Sskrll 	if (sc->sc_type == PCIHOST_CAM) {
434*05276bf3Sskrll 		if (offset & ~0xff)
435*05276bf3Sskrll 			return;
436*05276bf3Sskrll 		reg = (b << 16) | (d << 11) | (f << 8) | offset;
437*05276bf3Sskrll 	} else if (sc->sc_type == PCIHOST_ECAM) {
438*05276bf3Sskrll 		if (offset & ~0xfff)
439*05276bf3Sskrll 			return;
440*05276bf3Sskrll 		reg = (b << 20) | (d << 15) | (f << 12) | offset;
441*05276bf3Sskrll 	} else {
442*05276bf3Sskrll 		return;
443*05276bf3Sskrll 	}
444*05276bf3Sskrll 
445*05276bf3Sskrll 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val);
446*05276bf3Sskrll }
447*05276bf3Sskrll 
448*05276bf3Sskrll static int
449*05276bf3Sskrll pcihost_conf_hook(void *v, int b, int d, int f, pcireg_t id)
450*05276bf3Sskrll {
451*05276bf3Sskrll 	return PCI_CONF_DEFAULT;
452*05276bf3Sskrll }
453*05276bf3Sskrll 
454*05276bf3Sskrll static void
455*05276bf3Sskrll pcihost_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
456*05276bf3Sskrll {
457*05276bf3Sskrll }
458*05276bf3Sskrll 
459*05276bf3Sskrll static int
460*05276bf3Sskrll pcihost_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
461*05276bf3Sskrll {
462*05276bf3Sskrll 	struct pcihost_softc *sc = pa->pa_pc->pc_intr_v;
463*05276bf3Sskrll 	u_int addr_cells, interrupt_cells;
464*05276bf3Sskrll 	const u_int *imap, *imask;
465*05276bf3Sskrll 	int imaplen, imasklen;
466*05276bf3Sskrll 	u_int match[4];
467*05276bf3Sskrll 	int index;
468*05276bf3Sskrll 
469*05276bf3Sskrll 	if (pa->pa_intrpin == 0)
470*05276bf3Sskrll 		return EINVAL;
471*05276bf3Sskrll 
472*05276bf3Sskrll 	imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
473*05276bf3Sskrll 	imask = fdtbus_get_prop(sc->sc_phandle, "interrupt-map-mask", &imasklen);
474*05276bf3Sskrll 	if (imap == NULL || imask == NULL || imasklen != 16)
475*05276bf3Sskrll 		return EINVAL;
476*05276bf3Sskrll 
477*05276bf3Sskrll 	/* Convert attach args to specifier */
478*05276bf3Sskrll 	match[0] = htobe32(
479*05276bf3Sskrll 			__SHIFTIN(pa->pa_bus, PHYS_HI_BUS) |
480*05276bf3Sskrll 			__SHIFTIN(pa->pa_device, PHYS_HI_DEVICE) |
481*05276bf3Sskrll 			__SHIFTIN(pa->pa_function, PHYS_HI_FUNCTION)
482*05276bf3Sskrll 		   ) & imask[0];
483*05276bf3Sskrll 	match[1] = htobe32(0) & imask[1];
484*05276bf3Sskrll 	match[2] = htobe32(0) & imask[2];
485*05276bf3Sskrll 	match[3] = htobe32(pa->pa_intrpin) & imask[3];
486*05276bf3Sskrll 
487*05276bf3Sskrll 	index = 0;
488*05276bf3Sskrll 	while (imaplen >= 20) {
489*05276bf3Sskrll 		const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
490*05276bf3Sskrll 		if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
491*05276bf3Sskrll 			addr_cells = 2;
492*05276bf3Sskrll 		if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
493*05276bf3Sskrll 			interrupt_cells = 0;
494*05276bf3Sskrll 		if (imaplen < (addr_cells + interrupt_cells) * 4)
495*05276bf3Sskrll 			return ENXIO;
496*05276bf3Sskrll 
497*05276bf3Sskrll 		if ((imap[0] & imask[0]) == match[0] &&
498*05276bf3Sskrll 		    (imap[1] & imask[1]) == match[1] &&
499*05276bf3Sskrll 		    (imap[2] & imask[2]) == match[2] &&
500*05276bf3Sskrll 		    (imap[3] & imask[3]) == match[3]) {
501*05276bf3Sskrll 			*ih = index;
502*05276bf3Sskrll 			return 0;
503*05276bf3Sskrll 		}
504*05276bf3Sskrll 
505*05276bf3Sskrll 		imap += (5 + addr_cells + interrupt_cells);
506*05276bf3Sskrll 		imaplen -= (5 + addr_cells + interrupt_cells) * 4;
507*05276bf3Sskrll 		index++;
508*05276bf3Sskrll 	}
509*05276bf3Sskrll 
510*05276bf3Sskrll 	return EINVAL;
511*05276bf3Sskrll }
512*05276bf3Sskrll 
513*05276bf3Sskrll static const u_int *
514*05276bf3Sskrll pcihost_find_intr(struct pcihost_softc *sc, pci_intr_handle_t ih, int *pihandle)
515*05276bf3Sskrll {
516*05276bf3Sskrll 	u_int addr_cells, interrupt_cells;
517*05276bf3Sskrll 	int imaplen, index;
518*05276bf3Sskrll 	const u_int *imap;
519*05276bf3Sskrll 
520*05276bf3Sskrll 	imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
521*05276bf3Sskrll 	KASSERT(imap != NULL);
522*05276bf3Sskrll 
523*05276bf3Sskrll 	index = 0;
524*05276bf3Sskrll 	while (imaplen >= 20) {
525*05276bf3Sskrll 		const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
526*05276bf3Sskrll 		if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
527*05276bf3Sskrll 			addr_cells = 2;
528*05276bf3Sskrll 		if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
529*05276bf3Sskrll 			interrupt_cells = 0;
530*05276bf3Sskrll 		if (imaplen < (addr_cells + interrupt_cells) * 4)
531*05276bf3Sskrll 			return NULL;
532*05276bf3Sskrll 
533*05276bf3Sskrll 		if (index == ih) {
534*05276bf3Sskrll 			*pihandle = map_ihandle;
535*05276bf3Sskrll 			return imap + 5 + addr_cells;
536*05276bf3Sskrll 		}
537*05276bf3Sskrll 
538*05276bf3Sskrll 		imap += (5 + addr_cells + interrupt_cells);
539*05276bf3Sskrll 		imaplen -= (5 + addr_cells + interrupt_cells) * 4;
540*05276bf3Sskrll 		index++;
541*05276bf3Sskrll 	}
542*05276bf3Sskrll 
543*05276bf3Sskrll 	return NULL;
544*05276bf3Sskrll }
545*05276bf3Sskrll 
546*05276bf3Sskrll static const char *
547*05276bf3Sskrll pcihost_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
548*05276bf3Sskrll {
549*05276bf3Sskrll 	const int irq = __SHIFTOUT(ih, RISCV_PCI_INTR_IRQ);
550*05276bf3Sskrll 	const int vec = __SHIFTOUT(ih, RISCV_PCI_INTR_MSI_VEC);
551*05276bf3Sskrll 	struct pcihost_softc *sc = v;
552*05276bf3Sskrll 	const u_int *specifier;
553*05276bf3Sskrll 	int ihandle;
554*05276bf3Sskrll 
555*05276bf3Sskrll 	if (ih & RISCV_PCI_INTR_MSIX) {
556*05276bf3Sskrll 		snprintf(buf, len, "irq %d (MSI-X vec %d)", irq, vec);
557*05276bf3Sskrll 	} else if (ih & RISCV_PCI_INTR_MSI) {
558*05276bf3Sskrll 		snprintf(buf, len, "irq %d (MSI vec %d)", irq, vec);
559*05276bf3Sskrll 	} else {
560*05276bf3Sskrll 		specifier = pcihost_find_intr(sc, ih & RISCV_PCI_INTR_IRQ, &ihandle);
561*05276bf3Sskrll 		if (specifier == NULL)
562*05276bf3Sskrll 			return NULL;
563*05276bf3Sskrll 
564*05276bf3Sskrll 		if (!fdtbus_intr_str_raw(ihandle, specifier, buf, len))
565*05276bf3Sskrll 			return NULL;
566*05276bf3Sskrll 	}
567*05276bf3Sskrll 
568*05276bf3Sskrll 	return buf;
569*05276bf3Sskrll }
570*05276bf3Sskrll 
571*05276bf3Sskrll const struct evcnt *
572*05276bf3Sskrll pcihost_intr_evcnt(void *v, pci_intr_handle_t ih)
573*05276bf3Sskrll {
574*05276bf3Sskrll 	return NULL;
575*05276bf3Sskrll }
576*05276bf3Sskrll 
577*05276bf3Sskrll static int
578*05276bf3Sskrll pcihost_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
579*05276bf3Sskrll {
580*05276bf3Sskrll 	switch (attr) {
581*05276bf3Sskrll 	case PCI_INTR_MPSAFE:
582*05276bf3Sskrll 		if (data)
583*05276bf3Sskrll 			*ih |= RISCV_PCI_INTR_MPSAFE;
584*05276bf3Sskrll 		else
585*05276bf3Sskrll 			*ih &= ~RISCV_PCI_INTR_MPSAFE;
586*05276bf3Sskrll 		return 0;
587*05276bf3Sskrll 	default:
588*05276bf3Sskrll 		return ENODEV;
589*05276bf3Sskrll 	}
590*05276bf3Sskrll }
591*05276bf3Sskrll 
592*05276bf3Sskrll static void *
593*05276bf3Sskrll pcihost_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
594*05276bf3Sskrll     int (*callback)(void *), void *arg, const char *xname)
595*05276bf3Sskrll {
596*05276bf3Sskrll 	struct pcihost_softc *sc = v;
597*05276bf3Sskrll 	const int flags = (pih & RISCV_PCI_INTR_MPSAFE) ? FDT_INTR_MPSAFE : 0;
598*05276bf3Sskrll 	const u_int *specifier;
599*05276bf3Sskrll 	int ihandle;
600*05276bf3Sskrll 
601*05276bf3Sskrll 	specifier = pcihost_find_intr(sc, pih & RISCV_PCI_INTR_IRQ, &ihandle);
602*05276bf3Sskrll 
603*05276bf3Sskrll 	if (specifier == NULL)
604*05276bf3Sskrll 		return NULL;
605*05276bf3Sskrll 
606*05276bf3Sskrll 	return fdtbus_intr_establish_raw(ihandle, specifier, ipl, flags,
607*05276bf3Sskrll 	    callback, arg, xname);
608*05276bf3Sskrll }
609*05276bf3Sskrll 
610*05276bf3Sskrll static void
611*05276bf3Sskrll pcihost_intr_disestablish(void *v, void *vih)
612*05276bf3Sskrll {
613*05276bf3Sskrll 	struct pcihost_softc *sc = v;
614*05276bf3Sskrll 
615*05276bf3Sskrll 	fdtbus_intr_disestablish(sc->sc_phandle, vih);
616*05276bf3Sskrll }
617*05276bf3Sskrll 
618*05276bf3Sskrll static int
619*05276bf3Sskrll pcihost_bus_space_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
620*05276bf3Sskrll     bus_space_handle_t *bshp)
621*05276bf3Sskrll {
622*05276bf3Sskrll 	struct pcih_bus_space * const pbs = t;
623*05276bf3Sskrll 
624*05276bf3Sskrll 	for (size_t i = 0; i < pbs->nranges; i++) {
625*05276bf3Sskrll 		const bus_addr_t rmin = pbs->ranges[i].bpci;
626*05276bf3Sskrll 		const bus_addr_t rmax = pbs->ranges[i].bpci - 1 + pbs->ranges[i].size;
627*05276bf3Sskrll 		if ((bpa >= rmin) && ((bpa - 1 + size) <= rmax)) {
628*05276bf3Sskrll 			return pbs->map(t, bpa - pbs->ranges[i].bpci + pbs->ranges[i].bbus, size, flag, bshp);
629*05276bf3Sskrll 		}
630*05276bf3Sskrll 	}
631*05276bf3Sskrll 
632*05276bf3Sskrll 	return ERANGE;
633*05276bf3Sskrll }
634