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