pcihost_fdt.c revision 1.2.2.2 1 1.2.2.2 pgoyette /* $NetBSD: pcihost_fdt.c,v 1.2.2.2 2018/09/30 01:45:38 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /*-
4 1.2.2.2 pgoyette * Copyright (c) 2018 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.2.2.2 pgoyette * All rights reserved.
6 1.2.2.2 pgoyette *
7 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.2.2.2 pgoyette * are met:
10 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.2.2.2 pgoyette *
16 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.2.2.2 pgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.2.2.2 pgoyette * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.2.2.2 pgoyette * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.2.2.2 pgoyette * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.2.2.2 pgoyette * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.2.2.2 pgoyette * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.2.2.2 pgoyette * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.2.2.2 pgoyette * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 pgoyette * SUCH DAMAGE.
27 1.2.2.2 pgoyette */
28 1.2.2.2 pgoyette
29 1.2.2.2 pgoyette #include <sys/cdefs.h>
30 1.2.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: pcihost_fdt.c,v 1.2.2.2 2018/09/30 01:45:38 pgoyette Exp $");
31 1.2.2.2 pgoyette
32 1.2.2.2 pgoyette #include <sys/param.h>
33 1.2.2.2 pgoyette #include <sys/bus.h>
34 1.2.2.2 pgoyette #include <sys/device.h>
35 1.2.2.2 pgoyette #include <sys/intr.h>
36 1.2.2.2 pgoyette #include <sys/systm.h>
37 1.2.2.2 pgoyette #include <sys/kernel.h>
38 1.2.2.2 pgoyette #include <sys/extent.h>
39 1.2.2.2 pgoyette #include <sys/queue.h>
40 1.2.2.2 pgoyette #include <sys/mutex.h>
41 1.2.2.2 pgoyette #include <sys/kmem.h>
42 1.2.2.2 pgoyette
43 1.2.2.2 pgoyette #include <machine/cpu.h>
44 1.2.2.2 pgoyette
45 1.2.2.2 pgoyette #include <arm/cpufunc.h>
46 1.2.2.2 pgoyette
47 1.2.2.2 pgoyette #include <dev/pci/pcireg.h>
48 1.2.2.2 pgoyette #include <dev/pci/pcivar.h>
49 1.2.2.2 pgoyette #include <dev/pci/pciconf.h>
50 1.2.2.2 pgoyette
51 1.2.2.2 pgoyette #include <dev/fdt/fdtvar.h>
52 1.2.2.2 pgoyette
53 1.2.2.2 pgoyette #define IH_INDEX_MASK 0x0000ffff
54 1.2.2.2 pgoyette #define IH_MPSAFE 0x80000000
55 1.2.2.2 pgoyette
56 1.2.2.2 pgoyette #define PCIHOST_DEFAULT_BUS_MIN 0
57 1.2.2.2 pgoyette #define PCIHOST_DEFAULT_BUS_MAX 255
58 1.2.2.2 pgoyette
59 1.2.2.2 pgoyette #define PCIHOST_CACHELINE_SIZE arm_dcache_align
60 1.2.2.2 pgoyette
61 1.2.2.2 pgoyette /* Physical address format bit definitions */
62 1.2.2.2 pgoyette #define PHYS_HI_RELO __BIT(31)
63 1.2.2.2 pgoyette #define PHYS_HI_PREFETCH __BIT(30)
64 1.2.2.2 pgoyette #define PHYS_HI_ALIASED __BIT(29)
65 1.2.2.2 pgoyette #define PHYS_HI_SPACE __BITS(25,24)
66 1.2.2.2 pgoyette #define PHYS_HI_SPACE_CFG 0
67 1.2.2.2 pgoyette #define PHYS_HI_SPACE_IO 1
68 1.2.2.2 pgoyette #define PHYS_HI_SPACE_MEM32 2
69 1.2.2.2 pgoyette #define PHYS_HI_SPACE_MEM64 3
70 1.2.2.2 pgoyette #define PHYS_HI_BUS __BITS(23,16)
71 1.2.2.2 pgoyette #define PHYS_HI_DEVICE __BITS(15,11)
72 1.2.2.2 pgoyette #define PHYS_HI_FUNCTION __BITS(10,8)
73 1.2.2.2 pgoyette #define PHYS_HI_REGISTER __BITS(7,0)
74 1.2.2.2 pgoyette
75 1.2.2.2 pgoyette enum pcihost_type {
76 1.2.2.2 pgoyette PCIHOST_CAM = 1,
77 1.2.2.2 pgoyette PCIHOST_ECAM,
78 1.2.2.2 pgoyette };
79 1.2.2.2 pgoyette
80 1.2.2.2 pgoyette struct pcihost_softc {
81 1.2.2.2 pgoyette device_t sc_dev;
82 1.2.2.2 pgoyette bus_dma_tag_t sc_dmat;
83 1.2.2.2 pgoyette bus_space_tag_t sc_bst;
84 1.2.2.2 pgoyette bus_space_handle_t sc_bsh;
85 1.2.2.2 pgoyette int sc_phandle;
86 1.2.2.2 pgoyette
87 1.2.2.2 pgoyette enum pcihost_type sc_type;
88 1.2.2.2 pgoyette
89 1.2.2.2 pgoyette u_int sc_bus_min;
90 1.2.2.2 pgoyette u_int sc_bus_max;
91 1.2.2.2 pgoyette
92 1.2.2.2 pgoyette struct arm32_pci_chipset sc_pc;
93 1.2.2.2 pgoyette };
94 1.2.2.2 pgoyette
95 1.2.2.2 pgoyette static int pcihost_match(device_t, cfdata_t, void *);
96 1.2.2.2 pgoyette static void pcihost_attach(device_t, device_t, void *);
97 1.2.2.2 pgoyette
98 1.2.2.2 pgoyette static void pcihost_init(pci_chipset_tag_t, void *);
99 1.2.2.2 pgoyette static int pcihost_config(struct pcihost_softc *);
100 1.2.2.2 pgoyette
101 1.2.2.2 pgoyette static void pcihost_attach_hook(device_t, device_t,
102 1.2.2.2 pgoyette struct pcibus_attach_args *);
103 1.2.2.2 pgoyette static int pcihost_bus_maxdevs(void *, int);
104 1.2.2.2 pgoyette static pcitag_t pcihost_make_tag(void *, int, int, int);
105 1.2.2.2 pgoyette static void pcihost_decompose_tag(void *, pcitag_t, int *, int *, int *);
106 1.2.2.2 pgoyette static pcireg_t pcihost_conf_read(void *, pcitag_t, int);
107 1.2.2.2 pgoyette static void pcihost_conf_write(void *, pcitag_t, int, pcireg_t);
108 1.2.2.2 pgoyette static int pcihost_conf_hook(void *, int, int, int, pcireg_t);
109 1.2.2.2 pgoyette static void pcihost_conf_interrupt(void *, int, int, int, int, int *);
110 1.2.2.2 pgoyette
111 1.2.2.2 pgoyette static int pcihost_intr_map(const struct pci_attach_args *,
112 1.2.2.2 pgoyette pci_intr_handle_t *);
113 1.2.2.2 pgoyette static const char *pcihost_intr_string(void *, pci_intr_handle_t,
114 1.2.2.2 pgoyette char *, size_t);
115 1.2.2.2 pgoyette const struct evcnt *pcihost_intr_evcnt(void *, pci_intr_handle_t);
116 1.2.2.2 pgoyette static int pcihost_intr_setattr(void *, pci_intr_handle_t *, int,
117 1.2.2.2 pgoyette uint64_t);
118 1.2.2.2 pgoyette static void * pcihost_intr_establish(void *, pci_intr_handle_t,
119 1.2.2.2 pgoyette int, int (*)(void *), void *);
120 1.2.2.2 pgoyette static void pcihost_intr_disestablish(void *, void *);
121 1.2.2.2 pgoyette
122 1.2.2.2 pgoyette CFATTACH_DECL_NEW(pcihost_fdt, sizeof(struct pcihost_softc),
123 1.2.2.2 pgoyette pcihost_match, pcihost_attach, NULL, NULL);
124 1.2.2.2 pgoyette
125 1.2.2.2 pgoyette static const struct of_compat_data compat_data[] = {
126 1.2.2.2 pgoyette { "pci-host-cam-generic", PCIHOST_CAM },
127 1.2.2.2 pgoyette { "pci-host-ecam-generic", PCIHOST_ECAM },
128 1.2.2.2 pgoyette { NULL, 0 }
129 1.2.2.2 pgoyette };
130 1.2.2.2 pgoyette
131 1.2.2.2 pgoyette static int
132 1.2.2.2 pgoyette pcihost_match(device_t parent, cfdata_t cf, void *aux)
133 1.2.2.2 pgoyette {
134 1.2.2.2 pgoyette struct fdt_attach_args * const faa = aux;
135 1.2.2.2 pgoyette
136 1.2.2.2 pgoyette return of_match_compat_data(faa->faa_phandle, compat_data);
137 1.2.2.2 pgoyette }
138 1.2.2.2 pgoyette
139 1.2.2.2 pgoyette static void
140 1.2.2.2 pgoyette pcihost_attach(device_t parent, device_t self, void *aux)
141 1.2.2.2 pgoyette {
142 1.2.2.2 pgoyette struct pcihost_softc * const sc = device_private(self);
143 1.2.2.2 pgoyette struct fdt_attach_args * const faa = aux;
144 1.2.2.2 pgoyette struct pcibus_attach_args pba;
145 1.2.2.2 pgoyette bus_addr_t cs_addr;
146 1.2.2.2 pgoyette bus_size_t cs_size;
147 1.2.2.2 pgoyette const u_int *data;
148 1.2.2.2 pgoyette int error, len;
149 1.2.2.2 pgoyette
150 1.2.2.2 pgoyette if (fdtbus_get_reg(faa->faa_phandle, 0, &cs_addr, &cs_size) != 0) {
151 1.2.2.2 pgoyette aprint_error(": couldn't get registers\n");
152 1.2.2.2 pgoyette return;
153 1.2.2.2 pgoyette }
154 1.2.2.2 pgoyette
155 1.2.2.2 pgoyette sc->sc_dev = self;
156 1.2.2.2 pgoyette sc->sc_dmat = faa->faa_dmat;
157 1.2.2.2 pgoyette sc->sc_bst = faa->faa_bst;
158 1.2.2.2 pgoyette sc->sc_phandle = faa->faa_phandle;
159 1.2.2.2 pgoyette error = bus_space_map(sc->sc_bst, cs_addr, cs_size, 0, &sc->sc_bsh);
160 1.2.2.2 pgoyette if (error) {
161 1.2.2.2 pgoyette aprint_error(": couldn't map registers: %d\n", error);
162 1.2.2.2 pgoyette return;
163 1.2.2.2 pgoyette }
164 1.2.2.2 pgoyette sc->sc_type = of_search_compatible(sc->sc_phandle, compat_data)->data;
165 1.2.2.2 pgoyette
166 1.2.2.2 pgoyette aprint_naive("\n");
167 1.2.2.2 pgoyette aprint_normal(": Generic PCI host controller\n");
168 1.2.2.2 pgoyette
169 1.2.2.2 pgoyette if ((data = fdtbus_get_prop(sc->sc_phandle, "bus-range", &len)) != NULL) {
170 1.2.2.2 pgoyette if (len != 8) {
171 1.2.2.2 pgoyette aprint_error_dev(self, "malformed 'bus-range' property\n");
172 1.2.2.2 pgoyette return;
173 1.2.2.2 pgoyette }
174 1.2.2.2 pgoyette sc->sc_bus_min = be32toh(data[0]);
175 1.2.2.2 pgoyette sc->sc_bus_max = be32toh(data[1]);
176 1.2.2.2 pgoyette } else {
177 1.2.2.2 pgoyette sc->sc_bus_min = PCIHOST_DEFAULT_BUS_MIN;
178 1.2.2.2 pgoyette sc->sc_bus_max = PCIHOST_DEFAULT_BUS_MAX;
179 1.2.2.2 pgoyette }
180 1.2.2.2 pgoyette
181 1.2.2.2 pgoyette pcihost_init(&sc->sc_pc, sc);
182 1.2.2.2 pgoyette
183 1.2.2.2 pgoyette if (pcihost_config(sc) != 0)
184 1.2.2.2 pgoyette return;
185 1.2.2.2 pgoyette
186 1.2.2.2 pgoyette memset(&pba, 0, sizeof(pba));
187 1.2.2.2 pgoyette pba.pba_flags = PCI_FLAGS_MRL_OKAY |
188 1.2.2.2 pgoyette PCI_FLAGS_MRM_OKAY |
189 1.2.2.2 pgoyette PCI_FLAGS_MWI_OKAY |
190 1.2.2.2 pgoyette PCI_FLAGS_MEM_OKAY |
191 1.2.2.2 pgoyette PCI_FLAGS_IO_OKAY;
192 1.2.2.2 pgoyette pba.pba_iot = sc->sc_bst;
193 1.2.2.2 pgoyette pba.pba_memt = sc->sc_bst;
194 1.2.2.2 pgoyette pba.pba_dmat = sc->sc_dmat;
195 1.2.2.2 pgoyette #ifdef _PCI_HAVE_DMA64
196 1.2.2.2 pgoyette pba.pba_dmat64 = sc->sc_dmat;
197 1.2.2.2 pgoyette #endif
198 1.2.2.2 pgoyette pba.pba_pc = &sc->sc_pc;
199 1.2.2.2 pgoyette pba.pba_bus = 0;
200 1.2.2.2 pgoyette
201 1.2.2.2 pgoyette config_found_ia(self, "pcibus", &pba, pcibusprint);
202 1.2.2.2 pgoyette }
203 1.2.2.2 pgoyette
204 1.2.2.2 pgoyette static void
205 1.2.2.2 pgoyette pcihost_init(pci_chipset_tag_t pc, void *priv)
206 1.2.2.2 pgoyette {
207 1.2.2.2 pgoyette pc->pc_conf_v = priv;
208 1.2.2.2 pgoyette pc->pc_attach_hook = pcihost_attach_hook;
209 1.2.2.2 pgoyette pc->pc_bus_maxdevs = pcihost_bus_maxdevs;
210 1.2.2.2 pgoyette pc->pc_make_tag = pcihost_make_tag;
211 1.2.2.2 pgoyette pc->pc_decompose_tag = pcihost_decompose_tag;
212 1.2.2.2 pgoyette pc->pc_conf_read = pcihost_conf_read;
213 1.2.2.2 pgoyette pc->pc_conf_write = pcihost_conf_write;
214 1.2.2.2 pgoyette pc->pc_conf_hook = pcihost_conf_hook;
215 1.2.2.2 pgoyette pc->pc_conf_interrupt = pcihost_conf_interrupt;
216 1.2.2.2 pgoyette
217 1.2.2.2 pgoyette pc->pc_intr_v = priv;
218 1.2.2.2 pgoyette pc->pc_intr_map = pcihost_intr_map;
219 1.2.2.2 pgoyette pc->pc_intr_string = pcihost_intr_string;
220 1.2.2.2 pgoyette pc->pc_intr_evcnt = pcihost_intr_evcnt;
221 1.2.2.2 pgoyette pc->pc_intr_setattr = pcihost_intr_setattr;
222 1.2.2.2 pgoyette pc->pc_intr_establish = pcihost_intr_establish;
223 1.2.2.2 pgoyette pc->pc_intr_disestablish = pcihost_intr_disestablish;
224 1.2.2.2 pgoyette }
225 1.2.2.2 pgoyette
226 1.2.2.2 pgoyette static int
227 1.2.2.2 pgoyette pcihost_config(struct pcihost_softc *sc)
228 1.2.2.2 pgoyette {
229 1.2.2.2 pgoyette struct extent *ioext = NULL, *memext = NULL, *pmemext = NULL;
230 1.2.2.2 pgoyette const u_int *ranges;
231 1.2.2.2 pgoyette int error, len;
232 1.2.2.2 pgoyette
233 1.2.2.2 pgoyette ranges = fdtbus_get_prop(sc->sc_phandle, "ranges", &len);
234 1.2.2.2 pgoyette if (ranges == NULL) {
235 1.2.2.2 pgoyette aprint_error_dev(sc->sc_dev, "missing 'ranges' property\n");
236 1.2.2.2 pgoyette return EINVAL;
237 1.2.2.2 pgoyette }
238 1.2.2.2 pgoyette
239 1.2.2.2 pgoyette /*
240 1.2.2.2 pgoyette * Each entry in the ranges table contains:
241 1.2.2.2 pgoyette * - bus address (3 cells)
242 1.2.2.2 pgoyette * - cpu physical address (2 cells)
243 1.2.2.2 pgoyette * - size (2 cells)
244 1.2.2.2 pgoyette * Total size for each entry is 28 bytes (7 cells).
245 1.2.2.2 pgoyette */
246 1.2.2.2 pgoyette while (len >= 28) {
247 1.2.2.2 pgoyette const uint32_t phys_hi = be32dec(&ranges[0]);
248 1.2.2.2 pgoyette const uint64_t cpu_phys = be64dec(&ranges[3]);
249 1.2.2.2 pgoyette const uint64_t size = be64dec(&ranges[5]);
250 1.2.2.2 pgoyette
251 1.2.2.2 pgoyette switch (__SHIFTOUT(phys_hi, PHYS_HI_SPACE)) {
252 1.2.2.2 pgoyette case PHYS_HI_SPACE_IO:
253 1.2.2.2 pgoyette if (ioext != NULL) {
254 1.2.2.2 pgoyette aprint_error_dev(sc->sc_dev, "ignoring duplicate IO space range\n");
255 1.2.2.2 pgoyette continue;
256 1.2.2.2 pgoyette }
257 1.2.2.2 pgoyette ioext = extent_create("pciio", cpu_phys, cpu_phys + size - 1, NULL, 0, EX_NOWAIT);
258 1.2.2.2 pgoyette aprint_verbose_dev(sc->sc_dev,
259 1.2.2.2 pgoyette "I/O memory @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
260 1.2.2.2 pgoyette cpu_phys, size);
261 1.2.2.2 pgoyette break;
262 1.2.2.2 pgoyette case PHYS_HI_SPACE_MEM32:
263 1.2.2.2 pgoyette if ((phys_hi & PHYS_HI_PREFETCH) != 0) {
264 1.2.2.2 pgoyette if (pmemext != NULL) {
265 1.2.2.2 pgoyette aprint_error_dev(sc->sc_dev, "ignoring duplicate mem (prefetchable) range\n");
266 1.2.2.2 pgoyette continue;
267 1.2.2.2 pgoyette }
268 1.2.2.2 pgoyette pmemext = extent_create("pcipmem", cpu_phys, cpu_phys + size - 1, NULL, 0, EX_NOWAIT);
269 1.2.2.2 pgoyette aprint_verbose_dev(sc->sc_dev,
270 1.2.2.2 pgoyette "32-bit MMIO (prefetchable) @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
271 1.2.2.2 pgoyette cpu_phys, size);
272 1.2.2.2 pgoyette } else {
273 1.2.2.2 pgoyette if (memext != NULL) {
274 1.2.2.2 pgoyette aprint_error_dev(sc->sc_dev, "ignoring duplicate mem (non-prefetchable) range\n");
275 1.2.2.2 pgoyette continue;
276 1.2.2.2 pgoyette }
277 1.2.2.2 pgoyette memext = extent_create("pcimem", cpu_phys, cpu_phys + size - 1, NULL, 0, EX_NOWAIT);
278 1.2.2.2 pgoyette aprint_verbose_dev(sc->sc_dev,
279 1.2.2.2 pgoyette "32-bit MMIO (non-prefetchable) @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
280 1.2.2.2 pgoyette cpu_phys, size);
281 1.2.2.2 pgoyette }
282 1.2.2.2 pgoyette break;
283 1.2.2.2 pgoyette default:
284 1.2.2.2 pgoyette break;
285 1.2.2.2 pgoyette }
286 1.2.2.2 pgoyette
287 1.2.2.2 pgoyette len -= 28;
288 1.2.2.2 pgoyette ranges += 7;
289 1.2.2.2 pgoyette }
290 1.2.2.2 pgoyette
291 1.2.2.2 pgoyette error = pci_configure_bus(&sc->sc_pc, ioext, memext, pmemext, sc->sc_bus_min, PCIHOST_CACHELINE_SIZE);
292 1.2.2.2 pgoyette
293 1.2.2.2 pgoyette if (ioext)
294 1.2.2.2 pgoyette extent_destroy(ioext);
295 1.2.2.2 pgoyette if (memext)
296 1.2.2.2 pgoyette extent_destroy(memext);
297 1.2.2.2 pgoyette if (pmemext)
298 1.2.2.2 pgoyette extent_destroy(pmemext);
299 1.2.2.2 pgoyette
300 1.2.2.2 pgoyette if (error) {
301 1.2.2.2 pgoyette aprint_error_dev(sc->sc_dev, "configuration failed: %d\n", error);
302 1.2.2.2 pgoyette return error;
303 1.2.2.2 pgoyette }
304 1.2.2.2 pgoyette
305 1.2.2.2 pgoyette return 0;
306 1.2.2.2 pgoyette }
307 1.2.2.2 pgoyette
308 1.2.2.2 pgoyette static void
309 1.2.2.2 pgoyette pcihost_attach_hook(device_t parent, device_t self,
310 1.2.2.2 pgoyette struct pcibus_attach_args *pba)
311 1.2.2.2 pgoyette {
312 1.2.2.2 pgoyette }
313 1.2.2.2 pgoyette
314 1.2.2.2 pgoyette static int
315 1.2.2.2 pgoyette pcihost_bus_maxdevs(void *v, int busno)
316 1.2.2.2 pgoyette {
317 1.2.2.2 pgoyette return 32;
318 1.2.2.2 pgoyette }
319 1.2.2.2 pgoyette
320 1.2.2.2 pgoyette static pcitag_t
321 1.2.2.2 pgoyette pcihost_make_tag(void *v, int b, int d, int f)
322 1.2.2.2 pgoyette {
323 1.2.2.2 pgoyette return (b << 16) | (d << 11) | (f << 8);
324 1.2.2.2 pgoyette }
325 1.2.2.2 pgoyette
326 1.2.2.2 pgoyette static void
327 1.2.2.2 pgoyette pcihost_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
328 1.2.2.2 pgoyette {
329 1.2.2.2 pgoyette if (bp)
330 1.2.2.2 pgoyette *bp = (tag >> 16) & 0xff;
331 1.2.2.2 pgoyette if (dp)
332 1.2.2.2 pgoyette *dp = (tag >> 11) & 0x1f;
333 1.2.2.2 pgoyette if (fp)
334 1.2.2.2 pgoyette *fp = (tag >> 8) & 0x7;
335 1.2.2.2 pgoyette }
336 1.2.2.2 pgoyette
337 1.2.2.2 pgoyette static pcireg_t
338 1.2.2.2 pgoyette pcihost_conf_read(void *v, pcitag_t tag, int offset)
339 1.2.2.2 pgoyette {
340 1.2.2.2 pgoyette struct pcihost_softc *sc = v;
341 1.2.2.2 pgoyette int b, d, f;
342 1.2.2.2 pgoyette u_int reg;
343 1.2.2.2 pgoyette
344 1.2.2.2 pgoyette pcihost_decompose_tag(v, tag, &b, &d, &f);
345 1.2.2.2 pgoyette
346 1.2.2.2 pgoyette if (b < sc->sc_bus_min || b > sc->sc_bus_max)
347 1.2.2.2 pgoyette return (pcireg_t) -1;
348 1.2.2.2 pgoyette
349 1.2.2.2 pgoyette if (sc->sc_type == PCIHOST_CAM) {
350 1.2.2.2 pgoyette if (offset & ~0xff)
351 1.2.2.2 pgoyette return (pcireg_t) -1;
352 1.2.2.2 pgoyette reg = (b << 16) | (d << 11) | (f << 8) | offset;
353 1.2.2.2 pgoyette } else if (sc->sc_type == PCIHOST_ECAM) {
354 1.2.2.2 pgoyette if (offset & ~0xfff)
355 1.2.2.2 pgoyette return (pcireg_t) -1;
356 1.2.2.2 pgoyette reg = (b << 20) | (d << 15) | (f << 12) | offset;
357 1.2.2.2 pgoyette } else {
358 1.2.2.2 pgoyette return (pcireg_t) -1;
359 1.2.2.2 pgoyette }
360 1.2.2.2 pgoyette
361 1.2.2.2 pgoyette return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
362 1.2.2.2 pgoyette }
363 1.2.2.2 pgoyette
364 1.2.2.2 pgoyette static void
365 1.2.2.2 pgoyette pcihost_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
366 1.2.2.2 pgoyette {
367 1.2.2.2 pgoyette struct pcihost_softc *sc = v;
368 1.2.2.2 pgoyette int b, d, f;
369 1.2.2.2 pgoyette u_int reg;
370 1.2.2.2 pgoyette
371 1.2.2.2 pgoyette pcihost_decompose_tag(v, tag, &b, &d, &f);
372 1.2.2.2 pgoyette
373 1.2.2.2 pgoyette if (b < sc->sc_bus_min || b > sc->sc_bus_max)
374 1.2.2.2 pgoyette return;
375 1.2.2.2 pgoyette
376 1.2.2.2 pgoyette if (sc->sc_type == PCIHOST_CAM) {
377 1.2.2.2 pgoyette if (offset & ~0xff)
378 1.2.2.2 pgoyette return;
379 1.2.2.2 pgoyette reg = (b << 16) | (d << 11) | (f << 8) | offset;
380 1.2.2.2 pgoyette } else if (sc->sc_type == PCIHOST_ECAM) {
381 1.2.2.2 pgoyette if (offset & ~0xfff)
382 1.2.2.2 pgoyette return;
383 1.2.2.2 pgoyette reg = (b << 20) | (d << 15) | (f << 12) | offset;
384 1.2.2.2 pgoyette } else {
385 1.2.2.2 pgoyette return;
386 1.2.2.2 pgoyette }
387 1.2.2.2 pgoyette
388 1.2.2.2 pgoyette bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val);
389 1.2.2.2 pgoyette }
390 1.2.2.2 pgoyette
391 1.2.2.2 pgoyette static int
392 1.2.2.2 pgoyette pcihost_conf_hook(void *v, int b, int d, int f, pcireg_t id)
393 1.2.2.2 pgoyette {
394 1.2.2.2 pgoyette return PCI_CONF_DEFAULT;
395 1.2.2.2 pgoyette }
396 1.2.2.2 pgoyette
397 1.2.2.2 pgoyette static void
398 1.2.2.2 pgoyette pcihost_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
399 1.2.2.2 pgoyette {
400 1.2.2.2 pgoyette }
401 1.2.2.2 pgoyette
402 1.2.2.2 pgoyette static int
403 1.2.2.2 pgoyette pcihost_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
404 1.2.2.2 pgoyette {
405 1.2.2.2 pgoyette struct pcihost_softc *sc = pa->pa_pc->pc_intr_v;
406 1.2.2.2 pgoyette u_int addr_cells, interrupt_cells;
407 1.2.2.2 pgoyette const u_int *imap, *imask;
408 1.2.2.2 pgoyette int imaplen, imasklen;
409 1.2.2.2 pgoyette u_int match[4];
410 1.2.2.2 pgoyette int index;
411 1.2.2.2 pgoyette
412 1.2.2.2 pgoyette if (pa->pa_intrpin == 0)
413 1.2.2.2 pgoyette return EINVAL;
414 1.2.2.2 pgoyette
415 1.2.2.2 pgoyette imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
416 1.2.2.2 pgoyette imask = fdtbus_get_prop(sc->sc_phandle, "interrupt-map-mask", &imasklen);
417 1.2.2.2 pgoyette if (imap == NULL || imask == NULL || imasklen != 16)
418 1.2.2.2 pgoyette return EINVAL;
419 1.2.2.2 pgoyette
420 1.2.2.2 pgoyette /* Convert attach args to specifier */
421 1.2.2.2 pgoyette match[0] = htobe32(
422 1.2.2.2 pgoyette __SHIFTIN(pa->pa_bus, PHYS_HI_BUS) |
423 1.2.2.2 pgoyette __SHIFTIN(pa->pa_device, PHYS_HI_DEVICE) |
424 1.2.2.2 pgoyette __SHIFTIN(pa->pa_function, PHYS_HI_FUNCTION)
425 1.2.2.2 pgoyette ) & imask[0];
426 1.2.2.2 pgoyette match[1] = htobe32(0) & imask[1];
427 1.2.2.2 pgoyette match[2] = htobe32(0) & imask[2];
428 1.2.2.2 pgoyette match[3] = htobe32(pa->pa_intrpin) & imask[3];
429 1.2.2.2 pgoyette
430 1.2.2.2 pgoyette index = 0;
431 1.2.2.2 pgoyette while (imaplen >= 20) {
432 1.2.2.2 pgoyette const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
433 1.2.2.2 pgoyette if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
434 1.2.2.2 pgoyette addr_cells = 2;
435 1.2.2.2 pgoyette if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
436 1.2.2.2 pgoyette interrupt_cells = 0;
437 1.2.2.2 pgoyette if (imaplen < (addr_cells + interrupt_cells) * 4)
438 1.2.2.2 pgoyette return ENXIO;
439 1.2.2.2 pgoyette
440 1.2.2.2 pgoyette if ((imap[0] & imask[0]) == match[0] &&
441 1.2.2.2 pgoyette (imap[1] & imask[1]) == match[1] &&
442 1.2.2.2 pgoyette (imap[2] & imask[2]) == match[2] &&
443 1.2.2.2 pgoyette (imap[3] & imask[3]) == match[3]) {
444 1.2.2.2 pgoyette *ih = index;
445 1.2.2.2 pgoyette return 0;
446 1.2.2.2 pgoyette }
447 1.2.2.2 pgoyette
448 1.2.2.2 pgoyette imap += (5 + addr_cells + interrupt_cells);
449 1.2.2.2 pgoyette imaplen -= (5 + addr_cells + interrupt_cells) * 4;
450 1.2.2.2 pgoyette index++;
451 1.2.2.2 pgoyette }
452 1.2.2.2 pgoyette
453 1.2.2.2 pgoyette return EINVAL;
454 1.2.2.2 pgoyette }
455 1.2.2.2 pgoyette
456 1.2.2.2 pgoyette static const u_int *
457 1.2.2.2 pgoyette pcihost_find_intr(struct pcihost_softc *sc, pci_intr_handle_t ih, int *pihandle)
458 1.2.2.2 pgoyette {
459 1.2.2.2 pgoyette u_int addr_cells, interrupt_cells;
460 1.2.2.2 pgoyette int imaplen, index;
461 1.2.2.2 pgoyette const u_int *imap;
462 1.2.2.2 pgoyette
463 1.2.2.2 pgoyette imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
464 1.2.2.2 pgoyette KASSERT(imap != NULL);
465 1.2.2.2 pgoyette
466 1.2.2.2 pgoyette index = 0;
467 1.2.2.2 pgoyette while (imaplen >= 20) {
468 1.2.2.2 pgoyette const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
469 1.2.2.2 pgoyette if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
470 1.2.2.2 pgoyette addr_cells = 2;
471 1.2.2.2 pgoyette if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
472 1.2.2.2 pgoyette interrupt_cells = 0;
473 1.2.2.2 pgoyette if (imaplen < (addr_cells + interrupt_cells) * 4)
474 1.2.2.2 pgoyette return NULL;
475 1.2.2.2 pgoyette
476 1.2.2.2 pgoyette if (index == ih) {
477 1.2.2.2 pgoyette *pihandle = map_ihandle;
478 1.2.2.2 pgoyette return imap + 5 + addr_cells;
479 1.2.2.2 pgoyette }
480 1.2.2.2 pgoyette
481 1.2.2.2 pgoyette imap += (5 + addr_cells + interrupt_cells);
482 1.2.2.2 pgoyette imaplen -= (5 + addr_cells + interrupt_cells) * 4;
483 1.2.2.2 pgoyette index++;
484 1.2.2.2 pgoyette }
485 1.2.2.2 pgoyette
486 1.2.2.2 pgoyette return NULL;
487 1.2.2.2 pgoyette }
488 1.2.2.2 pgoyette
489 1.2.2.2 pgoyette static const char *
490 1.2.2.2 pgoyette pcihost_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
491 1.2.2.2 pgoyette {
492 1.2.2.2 pgoyette struct pcihost_softc *sc = v;
493 1.2.2.2 pgoyette const u_int *specifier;
494 1.2.2.2 pgoyette int ihandle;
495 1.2.2.2 pgoyette
496 1.2.2.2 pgoyette specifier = pcihost_find_intr(sc, ih & IH_INDEX_MASK, &ihandle);
497 1.2.2.2 pgoyette if (specifier == NULL)
498 1.2.2.2 pgoyette return NULL;
499 1.2.2.2 pgoyette
500 1.2.2.2 pgoyette if (!fdtbus_intr_str_raw(ihandle, specifier, buf, len))
501 1.2.2.2 pgoyette return NULL;
502 1.2.2.2 pgoyette
503 1.2.2.2 pgoyette return buf;
504 1.2.2.2 pgoyette }
505 1.2.2.2 pgoyette
506 1.2.2.2 pgoyette const struct evcnt *
507 1.2.2.2 pgoyette pcihost_intr_evcnt(void *v, pci_intr_handle_t ih)
508 1.2.2.2 pgoyette {
509 1.2.2.2 pgoyette return NULL;
510 1.2.2.2 pgoyette }
511 1.2.2.2 pgoyette
512 1.2.2.2 pgoyette static int
513 1.2.2.2 pgoyette pcihost_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
514 1.2.2.2 pgoyette {
515 1.2.2.2 pgoyette switch (attr) {
516 1.2.2.2 pgoyette case PCI_INTR_MPSAFE:
517 1.2.2.2 pgoyette if (data)
518 1.2.2.2 pgoyette *ih |= IH_MPSAFE;
519 1.2.2.2 pgoyette else
520 1.2.2.2 pgoyette *ih &= ~IH_MPSAFE;
521 1.2.2.2 pgoyette return 0;
522 1.2.2.2 pgoyette default:
523 1.2.2.2 pgoyette return ENODEV;
524 1.2.2.2 pgoyette }
525 1.2.2.2 pgoyette }
526 1.2.2.2 pgoyette
527 1.2.2.2 pgoyette static void *
528 1.2.2.2 pgoyette pcihost_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
529 1.2.2.2 pgoyette int (*callback)(void *), void *arg)
530 1.2.2.2 pgoyette {
531 1.2.2.2 pgoyette struct pcihost_softc *sc = v;
532 1.2.2.2 pgoyette const int flags = (ih & IH_MPSAFE) ? FDT_INTR_MPSAFE : 0;
533 1.2.2.2 pgoyette const u_int *specifier;
534 1.2.2.2 pgoyette int ihandle;
535 1.2.2.2 pgoyette
536 1.2.2.2 pgoyette specifier = pcihost_find_intr(sc, ih & IH_INDEX_MASK, &ihandle);
537 1.2.2.2 pgoyette if (specifier == NULL)
538 1.2.2.2 pgoyette return NULL;
539 1.2.2.2 pgoyette
540 1.2.2.2 pgoyette return fdtbus_intr_establish_raw(ihandle, specifier, ipl, flags, callback, arg);
541 1.2.2.2 pgoyette }
542 1.2.2.2 pgoyette
543 1.2.2.2 pgoyette static void
544 1.2.2.2 pgoyette pcihost_intr_disestablish(void *v, void *vih)
545 1.2.2.2 pgoyette {
546 1.2.2.2 pgoyette struct pcihost_softc *sc = v;
547 1.2.2.2 pgoyette
548 1.2.2.2 pgoyette fdtbus_intr_disestablish(sc->sc_phandle, vih);
549 1.2.2.2 pgoyette }
550