apple_pcie.c revision 1.1 1 1.1 jmcneill /* $NetBSD: apple_pcie.c,v 1.1 2021/08/30 23:26:26 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2021 Jared 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: apple_pcie.c,v 1.1 2021/08/30 23:26:26 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/device.h>
34 1.1 jmcneill #include <sys/kernel.h>
35 1.1 jmcneill #include <sys/systm.h>
36 1.1 jmcneill #include <sys/bus.h>
37 1.1 jmcneill #include <sys/kmem.h>
38 1.1 jmcneill #include <sys/bitops.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/pci/pcireg.h>
41 1.1 jmcneill #include <dev/pci/pcivar.h>
42 1.1 jmcneill #include <dev/pci/pciconf.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <dev/fdt/fdtvar.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <arm/pci/pci_msi_machdep.h>
47 1.1 jmcneill #include <arm/fdt/pcihost_fdtvar.h>
48 1.1 jmcneill
49 1.1 jmcneill #define PCIE_MSI_CTRL 0x0124
50 1.1 jmcneill #define PCIE_MSI_CTRL_EN (1U << 0)
51 1.1 jmcneill #define PCIE_MSI_CTRL_32 (5U << 4)
52 1.1 jmcneill #define PCIE_MSI_REMAP 0x0128
53 1.1 jmcneill #define PCIE_MSI_DOORBELL 0x0168
54 1.1 jmcneill
55 1.1 jmcneill /* XXX apple_dart.c */
56 1.1 jmcneill extern bus_dma_tag_t apple_dart_iommu_lookup(int);
57 1.1 jmcneill
58 1.1 jmcneill struct apple_pcie_softc {
59 1.1 jmcneill struct pcihost_softc sc_pcihost;
60 1.1 jmcneill
61 1.1 jmcneill int sc_phandle;
62 1.1 jmcneill struct arm_pci_msi sc_msi;
63 1.1 jmcneill u_int sc_msi_start;
64 1.1 jmcneill u_int sc_nmsi;
65 1.1 jmcneill struct pci_attach_args **sc_msi_pa;
66 1.1 jmcneill void **sc_msi_ih;
67 1.1 jmcneill uint64_t sc_msi_addr;
68 1.1 jmcneill };
69 1.1 jmcneill
70 1.1 jmcneill static int apple_pcie_match(device_t, cfdata_t, void *);
71 1.1 jmcneill static void apple_pcie_attach(device_t, device_t, void *);
72 1.1 jmcneill
73 1.1 jmcneill static void apple_pcie_attach_hook(device_t, device_t,
74 1.1 jmcneill struct pcibus_attach_args *);
75 1.1 jmcneill static int apple_pcie_msi_init(struct apple_pcie_softc *);
76 1.1 jmcneill
77 1.1 jmcneill CFATTACH_DECL_NEW(apple_pcie, sizeof(struct apple_pcie_softc),
78 1.1 jmcneill apple_pcie_match, apple_pcie_attach, NULL, NULL);
79 1.1 jmcneill
80 1.1 jmcneill static const struct device_compatible_entry compat_data[] = {
81 1.1 jmcneill { .compat = "apple,pcie" },
82 1.1 jmcneill DEVICE_COMPAT_EOL
83 1.1 jmcneill };
84 1.1 jmcneill
85 1.1 jmcneill static int
86 1.1 jmcneill apple_pcie_match(device_t parent, cfdata_t cf, void *aux)
87 1.1 jmcneill {
88 1.1 jmcneill struct fdt_attach_args * const faa = aux;
89 1.1 jmcneill
90 1.1 jmcneill return of_compatible_match(faa->faa_phandle, compat_data);
91 1.1 jmcneill }
92 1.1 jmcneill
93 1.1 jmcneill static void
94 1.1 jmcneill apple_pcie_attach(device_t parent, device_t self, void *aux)
95 1.1 jmcneill {
96 1.1 jmcneill struct apple_pcie_softc * const asc = device_private(self);
97 1.1 jmcneill struct pcihost_softc * const sc = &asc->sc_pcihost;
98 1.1 jmcneill struct fdt_attach_args * const faa = aux;
99 1.1 jmcneill const int phandle = faa->faa_phandle;
100 1.1 jmcneill bus_addr_t cs_addr;
101 1.1 jmcneill bus_size_t cs_size;
102 1.1 jmcneill int error;
103 1.1 jmcneill
104 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &cs_addr, &cs_size) != 0) {
105 1.1 jmcneill aprint_error(": couldn't get registers\n");
106 1.1 jmcneill return;
107 1.1 jmcneill }
108 1.1 jmcneill
109 1.1 jmcneill sc->sc_dev = self;
110 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
111 1.1 jmcneill sc->sc_bst = faa->faa_bst;
112 1.1 jmcneill sc->sc_phandle = phandle;
113 1.1 jmcneill error = bus_space_map(sc->sc_bst, cs_addr, cs_size,
114 1.1 jmcneill _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &sc->sc_bsh);
115 1.1 jmcneill if (error) {
116 1.1 jmcneill aprint_error(": couldn't map registers: %d\n", error);
117 1.1 jmcneill return;
118 1.1 jmcneill }
119 1.1 jmcneill sc->sc_type = PCIHOST_ECAM;
120 1.1 jmcneill
121 1.1 jmcneill if (apple_pcie_msi_init(asc) == 0) {
122 1.1 jmcneill sc->sc_pci_flags |= PCI_FLAGS_MSI_OKAY;
123 1.1 jmcneill #if notyet
124 1.1 jmcneill sc->sc_pci_flags |= PCI_FLAGS_MSIX_OKAY;
125 1.1 jmcneill #endif
126 1.1 jmcneill }
127 1.1 jmcneill
128 1.1 jmcneill aprint_naive("\n");
129 1.1 jmcneill aprint_normal(": Apple PCIe host controller\n");
130 1.1 jmcneill
131 1.1 jmcneill pcihost_init(&sc->sc_pc, sc);
132 1.1 jmcneill sc->sc_pc.pc_attach_hook = apple_pcie_attach_hook;
133 1.1 jmcneill pcihost_init2(sc);
134 1.1 jmcneill }
135 1.1 jmcneill
136 1.1 jmcneill static void
137 1.1 jmcneill apple_pcie_setup_port(struct apple_pcie_softc *sc, u_int portno)
138 1.1 jmcneill {
139 1.1 jmcneill const int phandle = sc->sc_pcihost.sc_phandle;
140 1.1 jmcneill bus_space_tag_t bst = sc->sc_pcihost.sc_bst;
141 1.1 jmcneill char regname[sizeof("portX")];
142 1.1 jmcneill bus_space_handle_t bsh;
143 1.1 jmcneill bus_addr_t addr;
144 1.1 jmcneill bus_size_t size;
145 1.1 jmcneill int error;
146 1.1 jmcneill
147 1.1 jmcneill snprintf(regname, sizeof(regname), "port%u", portno);
148 1.1 jmcneill if (fdtbus_get_reg_byname(phandle, regname, &addr, &size) != 0) {
149 1.1 jmcneill aprint_error(": couldn't get %s regs\n", regname);
150 1.1 jmcneill return;
151 1.1 jmcneill }
152 1.1 jmcneill error = bus_space_map(bst, addr, size,
153 1.1 jmcneill _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &bsh);
154 1.1 jmcneill if (error != 0) {
155 1.1 jmcneill aprint_error(": couldn't map %s regs\n", regname);
156 1.1 jmcneill return;
157 1.1 jmcneill }
158 1.1 jmcneill
159 1.1 jmcneill /* Doorbell address must be below 4GB */
160 1.1 jmcneill KASSERT((sc->sc_msi_addr & ~0xffffffffUL) == 0);
161 1.1 jmcneill
162 1.1 jmcneill bus_space_write_4(bst, bsh, PCIE_MSI_CTRL,
163 1.1 jmcneill PCIE_MSI_CTRL_32 | PCIE_MSI_CTRL_EN);
164 1.1 jmcneill bus_space_write_4(bst, bsh, PCIE_MSI_REMAP, 0);
165 1.1 jmcneill bus_space_write_4(bst, bsh, PCIE_MSI_DOORBELL,
166 1.1 jmcneill (uint32_t)sc->sc_msi_addr);
167 1.1 jmcneill
168 1.1 jmcneill bus_space_unmap(bst, bsh, size);
169 1.1 jmcneill }
170 1.1 jmcneill
171 1.1 jmcneill static void
172 1.1 jmcneill apple_pcie_attach_hook(device_t parent, device_t self,
173 1.1 jmcneill struct pcibus_attach_args *pba)
174 1.1 jmcneill {
175 1.1 jmcneill struct apple_pcie_softc *sc = pba->pba_pc->pc_conf_v;
176 1.1 jmcneill const int phandle = sc->sc_pcihost.sc_phandle;
177 1.1 jmcneill const u_int *iommu_map;
178 1.1 jmcneill int len;
179 1.1 jmcneill
180 1.1 jmcneill KASSERT(device_is_a(sc->sc_pcihost.sc_dev, "applepcie"));
181 1.1 jmcneill
182 1.1 jmcneill iommu_map = fdtbus_get_prop(phandle, "iommu-map", &len);
183 1.1 jmcneill if (iommu_map == NULL) {
184 1.1 jmcneill panic("%s: no iommu-map?!",
185 1.1 jmcneill device_xname(sc->sc_pcihost.sc_dev));
186 1.1 jmcneill return;
187 1.1 jmcneill }
188 1.1 jmcneill
189 1.1 jmcneill while (len >= 16) {
190 1.1 jmcneill const u_int ridbase = be32toh(iommu_map[0]);
191 1.1 jmcneill const u_int xref = fdtbus_get_phandle_from_native(
192 1.1 jmcneill be32toh(iommu_map[1]));
193 1.1 jmcneill
194 1.1 jmcneill const int bus = (ridbase >> 8) & 0xff;
195 1.1 jmcneill if (bus == pba->pba_bus) {
196 1.1 jmcneill pba->pba_dmat = apple_dart_iommu_lookup(xref);
197 1.1 jmcneill pba->pba_dmat64 = pba->pba_dmat;
198 1.1 jmcneill return;
199 1.1 jmcneill }
200 1.1 jmcneill
201 1.1 jmcneill iommu_map += 4;
202 1.1 jmcneill len -= 16;
203 1.1 jmcneill }
204 1.1 jmcneill
205 1.1 jmcneill //panic("no iommu for bus %d\n", pba->pba_bus);
206 1.1 jmcneill pba->pba_dmat = pba->pba_dmat64 = sc->sc_pcihost.sc_dmat;
207 1.1 jmcneill }
208 1.1 jmcneill
209 1.1 jmcneill static int
210 1.1 jmcneill apple_pcie_msi_alloc_msi(struct apple_pcie_softc *sc, int count,
211 1.1 jmcneill const struct pci_attach_args *pa)
212 1.1 jmcneill {
213 1.1 jmcneill struct pci_attach_args *new_pa;
214 1.1 jmcneill int msi, n;
215 1.1 jmcneill
216 1.1 jmcneill for (msi = 0; msi < sc->sc_nmsi; msi += count) {
217 1.1 jmcneill if (sc->sc_msi_pa[msi] == NULL) {
218 1.1 jmcneill for (n = 1; n < count; n++) {
219 1.1 jmcneill if (msi + n < sc->sc_nmsi &&
220 1.1 jmcneill sc->sc_msi_pa[msi + n] != NULL) {
221 1.1 jmcneill continue;
222 1.1 jmcneill }
223 1.1 jmcneill }
224 1.1 jmcneill
225 1.1 jmcneill for (n = 0; n < count; n++) {
226 1.1 jmcneill new_pa = kmem_alloc(sizeof(*new_pa), KM_SLEEP);
227 1.1 jmcneill memcpy(new_pa, pa, sizeof(*new_pa));
228 1.1 jmcneill sc->sc_msi_pa[msi + n] = new_pa;
229 1.1 jmcneill }
230 1.1 jmcneill
231 1.1 jmcneill return msi;
232 1.1 jmcneill }
233 1.1 jmcneill }
234 1.1 jmcneill
235 1.1 jmcneill return -1;
236 1.1 jmcneill }
237 1.1 jmcneill
238 1.1 jmcneill static void
239 1.1 jmcneill apple_pcie_msi_free_msi(struct apple_pcie_softc *sc, int msi)
240 1.1 jmcneill {
241 1.1 jmcneill struct pci_attach_args *pa;
242 1.1 jmcneill
243 1.1 jmcneill pa = sc->sc_msi_pa[msi];
244 1.1 jmcneill sc->sc_msi_pa[msi] = NULL;
245 1.1 jmcneill
246 1.1 jmcneill if (pa != NULL) {
247 1.1 jmcneill kmem_free(pa, sizeof(*pa));
248 1.1 jmcneill }
249 1.1 jmcneill }
250 1.1 jmcneill
251 1.1 jmcneill static int
252 1.1 jmcneill apple_pcie_msi_available_msi(struct apple_pcie_softc *sc)
253 1.1 jmcneill {
254 1.1 jmcneill int msi, n;
255 1.1 jmcneill
256 1.1 jmcneill for (n = 0, msi = 0; msi < sc->sc_nmsi; msi++) {
257 1.1 jmcneill if (sc->sc_msi_pa[msi] == NULL) {
258 1.1 jmcneill n++;
259 1.1 jmcneill }
260 1.1 jmcneill }
261 1.1 jmcneill
262 1.1 jmcneill return n;
263 1.1 jmcneill }
264 1.1 jmcneill
265 1.1 jmcneill static void
266 1.1 jmcneill apple_pcie_msi_msi_enable(struct apple_pcie_softc *sc, int msi, int count)
267 1.1 jmcneill {
268 1.1 jmcneill const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
269 1.1 jmcneill pci_chipset_tag_t pc = pa->pa_pc;
270 1.1 jmcneill pcitag_t tag = pa->pa_tag;
271 1.1 jmcneill pcireg_t ctl;
272 1.1 jmcneill int off;
273 1.1 jmcneill
274 1.1 jmcneill if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
275 1.1 jmcneill panic("apple_pcie_msi_msi_enable: device is not MSI-capable");
276 1.1 jmcneill
277 1.1 jmcneill ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
278 1.1 jmcneill ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
279 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
280 1.1 jmcneill
281 1.1 jmcneill ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
282 1.1 jmcneill ctl &= ~PCI_MSI_CTL_MME_MASK;
283 1.1 jmcneill ctl |= __SHIFTIN(ilog2(count), PCI_MSI_CTL_MME_MASK);
284 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
285 1.1 jmcneill
286 1.1 jmcneill const uint64_t addr = sc->sc_msi_addr;
287 1.1 jmcneill const uint32_t data = msi;
288 1.1 jmcneill
289 1.1 jmcneill ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
290 1.1 jmcneill if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
291 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
292 1.1 jmcneill addr & 0xffffffff);
293 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
294 1.1 jmcneill (addr >> 32) & 0xffffffff);
295 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, data);
296 1.1 jmcneill } else {
297 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
298 1.1 jmcneill addr & 0xffffffff);
299 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_MDATA, data);
300 1.1 jmcneill }
301 1.1 jmcneill ctl |= PCI_MSI_CTL_MSI_ENABLE;
302 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
303 1.1 jmcneill }
304 1.1 jmcneill
305 1.1 jmcneill static void
306 1.1 jmcneill apple_pcie_msi_msi_disable(struct apple_pcie_softc *sc, int msi)
307 1.1 jmcneill {
308 1.1 jmcneill const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
309 1.1 jmcneill pci_chipset_tag_t pc = pa->pa_pc;
310 1.1 jmcneill pcitag_t tag = pa->pa_tag;
311 1.1 jmcneill pcireg_t ctl;
312 1.1 jmcneill int off;
313 1.1 jmcneill
314 1.1 jmcneill if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
315 1.1 jmcneill panic("apple_pcie_msi_msi_disable: device is not MSI-capable");
316 1.1 jmcneill
317 1.1 jmcneill ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
318 1.1 jmcneill ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
319 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
320 1.1 jmcneill }
321 1.1 jmcneill
322 1.1 jmcneill static void
323 1.1 jmcneill apple_pcie_msi_msix_enable(struct apple_pcie_softc *sc, int msi, int msix_vec,
324 1.1 jmcneill bus_space_tag_t bst, bus_space_handle_t bsh)
325 1.1 jmcneill {
326 1.1 jmcneill const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
327 1.1 jmcneill pci_chipset_tag_t pc = pa->pa_pc;
328 1.1 jmcneill pcitag_t tag = pa->pa_tag;
329 1.1 jmcneill pcireg_t ctl;
330 1.1 jmcneill uint32_t val;
331 1.1 jmcneill int off;
332 1.1 jmcneill
333 1.1 jmcneill if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
334 1.1 jmcneill panic("apple_pcie_msi_msix_enable: device is not MSI-X-capable");
335 1.1 jmcneill
336 1.1 jmcneill ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
337 1.1 jmcneill ctl &= ~PCI_MSIX_CTL_ENABLE;
338 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
339 1.1 jmcneill
340 1.1 jmcneill const uint64_t addr = sc->sc_msi_addr;
341 1.1 jmcneill const uint32_t data = msi;
342 1.1 jmcneill const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
343 1.1 jmcneill bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO,
344 1.1 jmcneill (uint32_t)addr);
345 1.1 jmcneill bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI,
346 1.1 jmcneill (uint32_t)(addr >> 32));
347 1.1 jmcneill bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA,
348 1.1 jmcneill data);
349 1.1 jmcneill val = bus_space_read_4(bst, bsh,
350 1.1 jmcneill entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL);
351 1.1 jmcneill val &= ~PCI_MSIX_VECTCTL_MASK;
352 1.1 jmcneill bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL,
353 1.1 jmcneill val);
354 1.1 jmcneill
355 1.1 jmcneill ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
356 1.1 jmcneill ctl |= PCI_MSIX_CTL_ENABLE;
357 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
358 1.1 jmcneill }
359 1.1 jmcneill
360 1.1 jmcneill static void
361 1.1 jmcneill apple_pcie_msi_msix_disable(struct apple_pcie_softc *sc, int msi)
362 1.1 jmcneill {
363 1.1 jmcneill const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
364 1.1 jmcneill pci_chipset_tag_t pc = pa->pa_pc;
365 1.1 jmcneill pcitag_t tag = pa->pa_tag;
366 1.1 jmcneill pcireg_t ctl;
367 1.1 jmcneill int off;
368 1.1 jmcneill
369 1.1 jmcneill if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
370 1.1 jmcneill panic("apple_pcie_msi_msix_disable: device is not MSI-X-capable");
371 1.1 jmcneill
372 1.1 jmcneill ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
373 1.1 jmcneill ctl &= ~PCI_MSIX_CTL_ENABLE;
374 1.1 jmcneill pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
375 1.1 jmcneill }
376 1.1 jmcneill
377 1.1 jmcneill static pci_intr_handle_t *
378 1.1 jmcneill apple_pcie_msi_msi_alloc(struct arm_pci_msi *msi, int *count,
379 1.1 jmcneill const struct pci_attach_args *pa, bool exact)
380 1.1 jmcneill {
381 1.1 jmcneill struct apple_pcie_softc * const sc = msi->msi_priv;
382 1.1 jmcneill pci_intr_handle_t *vectors;
383 1.1 jmcneill int n, off;
384 1.1 jmcneill
385 1.1 jmcneill if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
386 1.1 jmcneill return NULL;
387 1.1 jmcneill
388 1.1 jmcneill const int avail = apple_pcie_msi_available_msi(sc);
389 1.1 jmcneill if (exact && *count > avail)
390 1.1 jmcneill return NULL;
391 1.1 jmcneill
392 1.1 jmcneill while (*count > avail) {
393 1.1 jmcneill if (avail < *count)
394 1.1 jmcneill (*count) >>= 1;
395 1.1 jmcneill }
396 1.1 jmcneill if (*count == 0)
397 1.1 jmcneill return NULL;
398 1.1 jmcneill
399 1.1 jmcneill const int msi_base = apple_pcie_msi_alloc_msi(sc, *count, pa);
400 1.1 jmcneill if (msi_base == -1)
401 1.1 jmcneill return NULL;
402 1.1 jmcneill
403 1.1 jmcneill vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
404 1.1 jmcneill for (n = 0; n < *count; n++) {
405 1.1 jmcneill const int msino = msi_base + n;
406 1.1 jmcneill vectors[n] = ARM_PCI_INTR_MSI |
407 1.1 jmcneill __SHIFTIN(msino, ARM_PCI_INTR_IRQ) |
408 1.1 jmcneill __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
409 1.1 jmcneill __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
410 1.1 jmcneill }
411 1.1 jmcneill
412 1.1 jmcneill apple_pcie_msi_msi_enable(sc, msi_base, *count);
413 1.1 jmcneill
414 1.1 jmcneill return vectors;
415 1.1 jmcneill }
416 1.1 jmcneill
417 1.1 jmcneill static pci_intr_handle_t *
418 1.1 jmcneill apple_pcie_msi_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes,
419 1.1 jmcneill int *count, const struct pci_attach_args *pa, bool exact)
420 1.1 jmcneill {
421 1.1 jmcneill struct apple_pcie_softc * const sc = msi->msi_priv;
422 1.1 jmcneill pci_intr_handle_t *vectors;
423 1.1 jmcneill bus_space_tag_t bst;
424 1.1 jmcneill bus_space_handle_t bsh;
425 1.1 jmcneill bus_size_t bsz;
426 1.1 jmcneill uint32_t table_offset, table_size;
427 1.1 jmcneill int n, off, bar, error;
428 1.1 jmcneill pcireg_t tbl;
429 1.1 jmcneill
430 1.1 jmcneill if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
431 1.1 jmcneill return NULL;
432 1.1 jmcneill
433 1.1 jmcneill const int avail = apple_pcie_msi_available_msi(sc);
434 1.1 jmcneill if (exact && *count > avail)
435 1.1 jmcneill return NULL;
436 1.1 jmcneill
437 1.1 jmcneill while (*count > avail) {
438 1.1 jmcneill if (avail < *count)
439 1.1 jmcneill (*count) >>= 1;
440 1.1 jmcneill }
441 1.1 jmcneill if (*count == 0)
442 1.1 jmcneill return NULL;
443 1.1 jmcneill
444 1.1 jmcneill tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
445 1.1 jmcneill bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_TBLBIR_MASK));
446 1.1 jmcneill table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
447 1.1 jmcneill table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
448 1.1 jmcneill if (table_size == 0)
449 1.1 jmcneill return NULL;
450 1.1 jmcneill
451 1.1 jmcneill error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
452 1.1 jmcneill BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
453 1.1 jmcneill &bst, &bsh, NULL, &bsz);
454 1.1 jmcneill if (error)
455 1.1 jmcneill return NULL;
456 1.1 jmcneill
457 1.1 jmcneill const int msi_base = apple_pcie_msi_alloc_msi(sc, *count, pa);
458 1.1 jmcneill if (msi_base == -1) {
459 1.1 jmcneill bus_space_unmap(bst, bsh, bsz);
460 1.1 jmcneill return NULL;
461 1.1 jmcneill }
462 1.1 jmcneill
463 1.1 jmcneill vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
464 1.1 jmcneill for (n = 0; n < *count; n++) {
465 1.1 jmcneill const int msino = msi_base + n;
466 1.1 jmcneill const int msix_vec = table_indexes ? table_indexes[n] : n;
467 1.1 jmcneill vectors[msix_vec] = ARM_PCI_INTR_MSIX |
468 1.1 jmcneill __SHIFTIN(msino, ARM_PCI_INTR_IRQ) |
469 1.1 jmcneill __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
470 1.1 jmcneill __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
471 1.1 jmcneill
472 1.1 jmcneill apple_pcie_msi_msix_enable(sc, msino, msix_vec, bst, bsh);
473 1.1 jmcneill }
474 1.1 jmcneill
475 1.1 jmcneill bus_space_unmap(bst, bsh, bsz);
476 1.1 jmcneill
477 1.1 jmcneill return vectors;
478 1.1 jmcneill }
479 1.1 jmcneill
480 1.1 jmcneill static void *
481 1.1 jmcneill apple_pcie_msi_intr_establish(struct arm_pci_msi *msi,
482 1.1 jmcneill pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
483 1.1 jmcneill {
484 1.1 jmcneill struct apple_pcie_softc * const sc = msi->msi_priv;
485 1.1 jmcneill
486 1.1 jmcneill const int msino = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
487 1.1 jmcneill const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? FDT_INTR_MPSAFE : 0;
488 1.1 jmcneill
489 1.1 jmcneill KASSERT(sc->sc_msi_ih[msino] == NULL);
490 1.1 jmcneill sc->sc_msi_ih[msino] = intr_establish_xname(sc->sc_msi_start + msino,
491 1.1 jmcneill ipl, IST_LEVEL | (mpsafe ? IST_MPSAFE : 0), func, arg, xname);
492 1.1 jmcneill
493 1.1 jmcneill return sc->sc_msi_ih[msino];
494 1.1 jmcneill }
495 1.1 jmcneill
496 1.1 jmcneill static void
497 1.1 jmcneill apple_pcie_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
498 1.1 jmcneill int count)
499 1.1 jmcneill {
500 1.1 jmcneill struct apple_pcie_softc * const sc = msi->msi_priv;
501 1.1 jmcneill int n;
502 1.1 jmcneill
503 1.1 jmcneill for (n = 0; n < count; n++) {
504 1.1 jmcneill const int msino = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
505 1.1 jmcneill if (pih[n] & ARM_PCI_INTR_MSIX)
506 1.1 jmcneill apple_pcie_msi_msix_disable(sc, msino);
507 1.1 jmcneill if (pih[n] & ARM_PCI_INTR_MSI)
508 1.1 jmcneill apple_pcie_msi_msi_disable(sc, msino);
509 1.1 jmcneill apple_pcie_msi_free_msi(sc, msino);
510 1.1 jmcneill if (sc->sc_msi_ih[msino] != NULL) {
511 1.1 jmcneill intr_disestablish(sc->sc_msi_ih[msino]);
512 1.1 jmcneill sc->sc_msi_ih[msino] = NULL;
513 1.1 jmcneill }
514 1.1 jmcneill }
515 1.1 jmcneill }
516 1.1 jmcneill
517 1.1 jmcneill static int
518 1.1 jmcneill apple_pcie_msi_init(struct apple_pcie_softc *sc)
519 1.1 jmcneill {
520 1.1 jmcneill struct arm_pci_msi *msi = &sc->sc_msi;
521 1.1 jmcneill const int phandle = sc->sc_pcihost.sc_phandle;
522 1.1 jmcneill u_int portno;
523 1.1 jmcneill int len;
524 1.1 jmcneill
525 1.1 jmcneill const u_int *data = fdtbus_get_prop(phandle, "msi-ranges", &len);
526 1.1 jmcneill if (len != 8) {
527 1.1 jmcneill aprint_error_dev(sc->sc_pcihost.sc_dev,
528 1.1 jmcneill "WARNING: bad msi-ranges property, MSI not enabled!\n");
529 1.1 jmcneill return ENXIO;
530 1.1 jmcneill }
531 1.1 jmcneill sc->sc_msi_start = be32toh(data[0]);
532 1.1 jmcneill sc->sc_nmsi = be32toh(data[1]);
533 1.1 jmcneill sc->sc_msi_pa = kmem_zalloc(sizeof(*sc->sc_msi_pa) * sc->sc_nmsi,
534 1.1 jmcneill KM_SLEEP);
535 1.1 jmcneill sc->sc_msi_ih = kmem_zalloc(sizeof(*sc->sc_msi_ih) * sc->sc_nmsi,
536 1.1 jmcneill KM_SLEEP);
537 1.1 jmcneill
538 1.1 jmcneill if (of_getprop_uint64(phandle, "msi-doorbell", &sc->sc_msi_addr)) {
539 1.1 jmcneill sc->sc_msi_addr = 0xffff000ULL;
540 1.1 jmcneill }
541 1.1 jmcneill
542 1.1 jmcneill for (portno = 0; portno < 3; portno++) {
543 1.1 jmcneill apple_pcie_setup_port(sc, portno);
544 1.1 jmcneill }
545 1.1 jmcneill
546 1.1 jmcneill msi->msi_dev = sc->sc_pcihost.sc_dev;
547 1.1 jmcneill msi->msi_priv = sc;
548 1.1 jmcneill msi->msi_alloc = apple_pcie_msi_msi_alloc;
549 1.1 jmcneill msi->msix_alloc = apple_pcie_msi_msix_alloc;
550 1.1 jmcneill msi->msi_intr_establish = apple_pcie_msi_intr_establish;
551 1.1 jmcneill msi->msi_intr_release = apple_pcie_msi_intr_release;
552 1.1 jmcneill
553 1.1 jmcneill return arm_pci_msi_add(msi);
554 1.1 jmcneill }
555