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