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