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