1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Scott Long
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "opt_acpi.h"
30 #include "opt_thunderbolt.h"
31
32 /* ACPI identified PCIe bridge for Thunderbolt */
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/malloc.h>
41 #include <sys/sysctl.h>
42 #include <sys/lock.h>
43 #include <sys/param.h>
44 #include <sys/endian.h>
45
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <machine/stdarg.h>
49 #include <sys/rman.h>
50
51 #include <machine/pci_cfgreg.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcib_private.h>
55 #include <dev/pci/pci_private.h>
56
57 #include <contrib/dev/acpica/include/acpi.h>
58 #include <contrib/dev/acpica/include/accommon.h>
59 #include <dev/acpica/acpivar.h>
60 #include <dev/acpica/acpi_pcibvar.h>
61 #include <machine/md_var.h>
62
63 #include <dev/thunderbolt/tb_reg.h>
64 #include <dev/thunderbolt/tb_pcib.h>
65 #include <dev/thunderbolt/nhi_var.h>
66 #include <dev/thunderbolt/nhi_reg.h>
67 #include <dev/thunderbolt/tbcfg_reg.h>
68 #include <dev/thunderbolt/tb_debug.h>
69
70 static int tb_acpi_pcib_probe(device_t);
71 static int tb_acpi_pcib_attach(device_t);
72 static int tb_acpi_pcib_detach(device_t);
73
74 /* ACPI attachment for Thudnerbolt Bridges */
75
76 static int
tb_acpi_pcib_probe(device_t dev)77 tb_acpi_pcib_probe(device_t dev)
78 {
79 char desc[TB_DESC_MAX], desc1[TB_DESC_MAX];
80 int val;
81
82 if (pci_get_class(dev) != PCIC_BRIDGE ||
83 pci_get_subclass(dev) != PCIS_BRIDGE_PCI ||
84 acpi_disabled("pci"))
85 return (ENXIO);
86 if (acpi_get_handle(dev) == NULL)
87 return (ENXIO);
88 if (pci_cfgregopen() == 0)
89 return (ENXIO);
90
91 /*
92 * Success? Specify a higher probe priority than the conventional
93 * Thunderbolt PCIb driver
94 */
95 if ((val = tb_pcib_probe_common(dev, desc)) < 0) {
96 val++;
97 snprintf(desc1, TB_DESC_MAX, "ACPI %s", desc);
98 device_set_desc_copy(dev, desc1);
99 }
100
101 return (val);
102 }
103
104 static int
tb_acpi_pcib_attach(device_t dev)105 tb_acpi_pcib_attach(device_t dev)
106 {
107 struct tb_pcib_softc *sc;
108 int error;
109
110 error = tb_pcib_attach_common(dev);
111 if (error)
112 return (error);
113
114 sc = device_get_softc(dev);
115 sc->ap_handle = acpi_get_handle(dev);
116 KASSERT(sc->ap_handle != NULL, ("ACPI handle cannot be NULL\n"));
117
118 /* Execute OSUP in case the BIOS didn't */
119 if (TB_IS_ROOT(sc)) {
120 ACPI_OBJECT_LIST list;
121 ACPI_OBJECT arg;
122 ACPI_BUFFER buf;
123 ACPI_STATUS s;
124
125 tb_debug(sc, DBG_BRIDGE, "Executing OSUP\n");
126
127 list.Pointer = &arg;
128 list.Count = 1;
129 arg.Integer.Value = 0;
130 arg.Type = ACPI_TYPE_INTEGER;
131 buf.Length = ACPI_ALLOCATE_BUFFER;
132 buf.Pointer = NULL;
133
134 s = AcpiEvaluateObject(sc->ap_handle, "\\_GPE.OSUP", &list,
135 &buf);
136 tb_debug(sc, DBG_BRIDGE|DBG_FULL,
137 "ACPI returned %d, buf= %p\n", s, buf.Pointer);
138 if (buf.Pointer != NULL)
139 tb_debug(sc, DBG_BRIDGE|DBG_FULL, "buffer= 0x%x\n",
140 *(uint32_t *)buf.Pointer);
141
142 AcpiOsFree(buf.Pointer);
143 }
144
145 pcib_attach_common(dev);
146 acpi_pcib_fetch_prt(dev, &sc->ap_prt);
147
148 return (pcib_attach_child(dev));
149 }
150
151 static int
tb_acpi_pcib_detach(device_t dev)152 tb_acpi_pcib_detach(device_t dev)
153 {
154 struct tb_pcib_softc *sc;
155 int error;
156
157 sc = device_get_softc(dev);
158 tb_debug(sc, DBG_BRIDGE|DBG_ROUTER|DBG_EXTRA, "tb_acpi_pcib_detach\n");
159
160 error = pcib_detach(dev);
161 if (error == 0)
162 AcpiOsFree(sc->ap_prt.Pointer);
163 return (error);
164 }
165
166 static device_method_t tb_acpi_pcib_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_probe, tb_acpi_pcib_probe),
169 DEVMETHOD(device_attach, tb_acpi_pcib_attach),
170 DEVMETHOD(device_detach, tb_acpi_pcib_detach),
171
172 /* Thunderbolt interface is inherited */
173
174 DEVMETHOD_END
175 };
176
177 DEFINE_CLASS_2(tbolt, tb_acpi_pcib_driver, tb_acpi_pcib_methods,
178 sizeof(struct tb_pcib_softc), pcib_driver, tb_pcib_driver);
179 DRIVER_MODULE_ORDERED(tb_acpi_pcib, pci, tb_acpi_pcib_driver,
180 NULL, NULL, SI_ORDER_MIDDLE);
181 MODULE_DEPEND(tb_acpi_pcib, acpi, 1, 1, 1);
182