2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Andrew Turner
5 * Copyright (c) 2019 Ruslan Bukin <br@bsdpad.com>
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory (Department of Computer Science and
9 * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
10 * DARPA SSITH research programme.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/endian.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
47 #include <vm/vm_extern.h>
48 #include <vm/vm_page.h>
50 #include <contrib/dev/acpica/include/acpi.h>
51 #include <contrib/dev/acpica/include/accommon.h>
53 #include <dev/acpica/acpivar.h>
54 #include <dev/acpica/acpi_pcibvar.h>
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcib_private.h>
59 #include <dev/pci/pci_host_generic.h>
60 #include <dev/pci/pci_host_generic_acpi.h>
64 #define AP_NS_SHARED_MEM_BASE 0x06000000
65 #define N1SDP_MAX_SEGMENTS 2 /* Two PCIe root complex devices. */
66 #define BDF_TABLE_SIZE (16 * 1024)
67 #define PCI_CFG_SPACE_SIZE 0x1000
69 struct pcie_discovery_data {
70 uint32_t rc_base_addr;
72 uint32_t valid_bdfs[0];
75 struct generic_pcie_n1sdp_softc {
76 struct generic_pcie_acpi_softc acpi;
77 struct pcie_discovery_data *n1_discovery_data;
78 bus_space_handle_t n1_bsh;
82 n1sdp_init(struct generic_pcie_n1sdp_softc *sc)
84 struct pcie_discovery_data *shared_data;
92 paddr = AP_NS_SHARED_MEM_BASE + sc->acpi.segment * BDF_TABLE_SIZE;
93 vaddr = kva_alloc((vm_size_t)BDF_TABLE_SIZE);
95 printf("%s: Can't allocate KVA memory.", __func__);
98 pmap_kenter(vaddr, (vm_size_t)BDF_TABLE_SIZE, paddr,
99 VM_MEMATTR_UNCACHEABLE);
101 shared_data = (struct pcie_discovery_data *)vaddr;
102 bdfs_size = sizeof(struct pcie_discovery_data) +
103 sizeof(uint32_t) * shared_data->nr_bdfs;
104 sc->n1_discovery_data = malloc(bdfs_size, M_DEVBUF, M_WAITOK | M_ZERO);
105 memcpy(sc->n1_discovery_data, shared_data, bdfs_size);
107 paddr_rc = (vm_offset_t)shared_data->rc_base_addr;
108 error = bus_space_map(sc->acpi.base.bst, paddr_rc, PCI_CFG_SPACE_SIZE,
114 table_count = sc->n1_discovery_data->nr_bdfs;
115 for (i = 0; i < table_count; i++)
116 printf("valid bdf %x\n",
117 sc->n1_discovery_data->valid_bdfs[i]);
121 kva_free(vaddr, (vm_size_t)BDF_TABLE_SIZE);
127 n1sdp_check_bdf(struct generic_pcie_n1sdp_softc *sc,
128 u_int bus, u_int slot, u_int func)
134 bdf = PCIE_ADDR_OFFSET(bus, slot, func, 0);
138 table_count = sc->n1_discovery_data->nr_bdfs;
140 for (i = 0; i < table_count; i++)
141 if (bdf == sc->n1_discovery_data->valid_bdfs[i])
148 n1sdp_pcie_acpi_probe(device_t dev)
150 ACPI_DEVICE_INFO *devinfo;
151 ACPI_TABLE_HEADER *hdr;
156 if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
157 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
160 root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
165 /* TODO: Move this to an ACPI quirk? */
166 status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr);
167 if (ACPI_FAILURE(status))
170 if (memcmp(hdr->OemId, "ARMLTD", ACPI_OEM_ID_SIZE) != 0 ||
171 memcmp(hdr->OemTableId, "ARMN1SDP", ACPI_OEM_TABLE_ID_SIZE) != 0 ||
172 hdr->OemRevision != 0x20181101)
175 device_set_desc(dev, "ARM N1SDP PCI host controller");
176 return (BUS_PROBE_DEFAULT);
180 n1sdp_pcie_acpi_attach(device_t dev)
182 struct generic_pcie_n1sdp_softc *sc;
187 err = pci_host_generic_acpi_init(dev);
191 sc = device_get_softc(dev);
192 handle = acpi_get_handle(dev);
194 /* Get PCI Segment (domain) needed for IOMMU space remap. */
195 status = acpi_GetInteger(handle, "_SEG", &sc->acpi.segment);
196 if (ACPI_FAILURE(status)) {
197 device_printf(dev, "No _SEG for PCI Bus\n");
201 if (sc->acpi.segment >= N1SDP_MAX_SEGMENTS) {
202 device_printf(dev, "Unknown PCI Bus segment (domain) %d\n",
207 err = n1sdp_init(sc);
211 device_add_child(dev, "pci", -1);
212 return (bus_generic_attach(dev));
216 n1sdp_get_bus_space(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
217 bus_space_tag_t *bst, bus_space_handle_t *bsh, bus_size_t *offset)
219 struct generic_pcie_n1sdp_softc *sc;
221 sc = device_get_softc(dev);
223 if (n1sdp_check_bdf(sc, bus, slot, func) == 0)
226 if (bus == sc->acpi.base.bus_start) {
227 if (slot != 0 || func != 0)
231 *bsh = sc->acpi.base.bsh;
234 *bst = sc->acpi.base.bst;
235 *offset = PCIE_ADDR_OFFSET(bus - sc->acpi.base.bus_start, slot, func,
242 n1sdp_pcie_read_config(device_t dev, u_int bus, u_int slot,
243 u_int func, u_int reg, int bytes)
245 struct generic_pcie_n1sdp_softc *sc_n1sdp;
246 struct generic_pcie_acpi_softc *sc_acpi;
247 struct generic_pcie_core_softc *sc;
248 bus_space_handle_t h;
253 sc_n1sdp = device_get_softc(dev);
254 sc_acpi = &sc_n1sdp->acpi;
257 if ((bus < sc->bus_start) || (bus > sc->bus_end))
259 if ((slot > PCI_SLOTMAX) || (func > PCI_FUNCMAX) ||
263 if (n1sdp_get_bus_space(dev, bus, slot, func, reg, &t, &h, &offset) !=0)
266 data = bus_space_read_4(t, h, offset & ~3);
270 data >>= (offset & 3) * 8;
274 data >>= (offset & 3) * 8;
275 data = le16toh(data);
278 data = le32toh(data);
288 n1sdp_pcie_write_config(device_t dev, u_int bus, u_int slot,
289 u_int func, u_int reg, uint32_t val, int bytes)
291 struct generic_pcie_n1sdp_softc *sc_n1sdp;
292 struct generic_pcie_acpi_softc *sc_acpi;
293 struct generic_pcie_core_softc *sc;
294 bus_space_handle_t h;
299 sc_n1sdp = device_get_softc(dev);
300 sc_acpi = &sc_n1sdp->acpi;
303 if ((bus < sc->bus_start) || (bus > sc->bus_end))
305 if ((slot > PCI_SLOTMAX) || (func > PCI_FUNCMAX) ||
309 if (n1sdp_get_bus_space(dev, bus, slot, func, reg, &t, &h, &offset) !=0)
312 data = bus_space_read_4(t, h, offset & ~3);
316 data &= ~(0xff << ((offset & 3) * 8));
317 data |= (val & 0xff) << ((offset & 3) * 8);
320 data &= ~(0xffff << ((offset & 3) * 8));
321 data |= (val & 0xffff) << ((offset & 3) * 8);
330 bus_space_write_4(t, h, offset & ~3, data);
333 static device_method_t n1sdp_pcie_acpi_methods[] = {
334 DEVMETHOD(device_probe, n1sdp_pcie_acpi_probe),
335 DEVMETHOD(device_attach, n1sdp_pcie_acpi_attach),
338 DEVMETHOD(pcib_read_config, n1sdp_pcie_read_config),
339 DEVMETHOD(pcib_write_config, n1sdp_pcie_write_config),
344 DEFINE_CLASS_1(pcib, n1sdp_pcie_acpi_driver, n1sdp_pcie_acpi_methods,
345 sizeof(struct generic_pcie_n1sdp_softc), generic_pcie_acpi_driver);
347 static devclass_t n1sdp_pcie_acpi_devclass;
349 DRIVER_MODULE(n1sdp_pcib, acpi, n1sdp_pcie_acpi_driver,
350 n1sdp_pcie_acpi_devclass, 0, 0);