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