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