blob: 4fd1eed46e672185aba3d410cc5832d6f881362b [file] [log] [blame]
Felix Held3f3eca92020-01-23 17:12:32 +01001/* SPDX-License-Identifier: GPL-2.0-or-later */
Patrick Rudolphc1621312019-05-28 11:29:29 +02002
3#include <superio/common/ssdt.h>
4
5#include <device/device.h>
6#include <device/pnp.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07007#include <acpi/acpigen.h>
8#include <acpi/acpi.h>
Patrick Rudolphc1621312019-05-28 11:29:29 +02009#include <console/console.h>
10#include <types.h>
Joel Kitchinga1b15172020-03-12 18:15:34 +080011#include <string.h>
Patrick Rudolphc1621312019-05-28 11:29:29 +020012
13struct superio_dev {
14 const char *acpi_hid;
15 u16 io_base[4];
16 u8 irq[2];
17};
18
19static const struct superio_dev superio_devs[] = {
20 {ACPI_HID_FDC, {0x3f0, 0x3f2, 0x3f7}, {6, } },
21 {ACPI_HID_KEYBOARD, {60, 64, }, {1, } },
22 {ACPI_HID_MOUSE, {60, 64, }, {12, } },
23 {ACPI_HID_COM, {0x3f8, 0x2f8, 0x3e8, 0x2e8}, {4, 3} },
24 {ACPI_HID_LPT, {0x378, }, {7, } },
25};
26
27static const u8 io_idx[] = {PNP_IDX_IO0, PNP_IDX_IO1, PNP_IDX_IO2, PNP_IDX_IO3};
28static const u8 irq_idx[] = {PNP_IDX_IRQ0, PNP_IDX_IRQ1};
29
Furquan Shaikh7536a392020-04-24 21:59:21 -070030static const struct superio_dev *superio_guess_function(const struct device *dev)
Patrick Rudolphc1621312019-05-28 11:29:29 +020031{
32 for (size_t i = 0; i < ARRAY_SIZE(io_idx); i++) {
33 struct resource *res = probe_resource(dev, io_idx[i]);
34 if (!res || !res->base)
35 continue;
36
37 for (size_t j = 0; j < ARRAY_SIZE(superio_devs); j++) {
38 for (size_t k = 0; k < 4; k++) {
39 if (!superio_devs[j].io_base[k])
40 continue;
41 if (superio_devs[j].io_base[k] == res->base)
42 return &superio_devs[j];
43 }
44 }
45 }
46 for (size_t i = 0; i < ARRAY_SIZE(irq_idx); i++) {
47 struct resource *res = probe_resource(dev, irq_idx[i]);
48 if (!res || !res->size)
49 continue;
50 for (size_t j = 0; j < ARRAY_SIZE(superio_devs); j++) {
51 for (size_t k = 0; k < 2; k++) {
52 if (!superio_devs[j].irq[k])
53 continue;
54 if (superio_devs[j].irq[k] == res->base)
55 return &superio_devs[j];
56 }
57 }
58 }
59 return NULL;
60}
61
62/* Return true if there are resources to report */
Furquan Shaikh7536a392020-04-24 21:59:21 -070063static bool has_resources(const struct device *dev)
Patrick Rudolphc1621312019-05-28 11:29:29 +020064{
65 for (size_t i = 0; i < ARRAY_SIZE(io_idx); i++) {
66 struct resource *res = probe_resource(dev, io_idx[i]);
67 if (!res || !res->base || !res->size)
68 continue;
69 return 1;
70 }
71 for (size_t i = 0; i < ARRAY_SIZE(irq_idx); i++) {
72 struct resource *res = probe_resource(dev, irq_idx[i]);
73 if (!res || !res->size || res->base > 16)
74 continue;
75 return 1;
76 }
77 return 0;
78}
79
80/* Add IO and IRQ resources for _CRS or _PRS */
Furquan Shaikh7536a392020-04-24 21:59:21 -070081static void ldn_gen_resources(const struct device *dev)
Patrick Rudolphc1621312019-05-28 11:29:29 +020082{
83 uint16_t irq = 0;
84 for (size_t i = 0; i < ARRAY_SIZE(io_idx); i++) {
85 struct resource *res = probe_resource(dev, io_idx[i]);
86 if (!res || !res->base)
87 continue;
88 resource_t base = res->base;
89 resource_t size = res->size;
90 while (size > 0) {
91 resource_t sz = size > 255 ? 255 : size;
92 /* TODO: Needs test with regions >= 256 bytes */
93 acpigen_write_io16(base, base, 1, sz, 1);
94 size -= sz;
95 base += sz;
96 }
97 }
98 for (size_t i = 0; i < ARRAY_SIZE(irq_idx); i++) {
99 struct resource *res = probe_resource(dev, irq_idx[i]);
100 if (!res || !res->size || res->base >= 16)
101 continue;
102 irq |= 1 << res->base;
103 }
104 if (irq)
105 acpigen_write_irq(irq);
Patrick Rudolphc1621312019-05-28 11:29:29 +0200106}
107
108/* Add resource base and size for additional SuperIO code */
Furquan Shaikh7536a392020-04-24 21:59:21 -0700109static void ldn_gen_resources_use(const struct device *dev)
Patrick Rudolphc1621312019-05-28 11:29:29 +0200110{
111 char name[5];
112 for (size_t i = 0; i < ARRAY_SIZE(io_idx); i++) {
113 struct resource *res = probe_resource(dev, io_idx[i]);
114 if (!res || !res->base || !res->size)
115 continue;
116
Patrick Georgi28cbab32019-09-16 12:17:59 +0200117 snprintf(name, sizeof(name), "IO%zXB", i);
Patrick Rudolphc1621312019-05-28 11:29:29 +0200118 name[4] = '\0';
119 acpigen_write_name_integer(name, res->base);
120
Patrick Georgi28cbab32019-09-16 12:17:59 +0200121 snprintf(name, sizeof(name), "IO%zXS", i);
Patrick Rudolphc1621312019-05-28 11:29:29 +0200122 name[4] = '\0';
123 acpigen_write_name_integer(name, res->size);
124 }
125}
126
127const char *superio_common_ldn_acpi_name(const struct device *dev)
128{
129 u8 ldn = dev->path.pnp.device & 0xff;
130 u8 vldn = (dev->path.pnp.device >> 8) & 0x7;
131 static char name[5];
132
133 snprintf(name, sizeof(name), "L%02X%01X", ldn, vldn);
134
135 name[4] = '\0';
136
137 return name;
138}
139
140static const char *name_from_hid(const char *hid)
141{
142 static const struct {
143 const char *hid;
144 const char *name;
145 } lookup[] = {
146 {ACPI_HID_FDC, "FDC" },
147 {ACPI_HID_KEYBOARD, "PS2 Keyboard" },
148 {ACPI_HID_MOUSE, "PS2 Mouse"},
149 {ACPI_HID_COM, "COM port" },
150 {ACPI_HID_LPT, "LPT" },
151 {ACPI_HID_PNP, "Generic PNP device" },
152 };
153
154 for (size_t i = 0; hid && i < ARRAY_SIZE(lookup); i++) {
155 if (strcmp(hid, lookup[i].hid) == 0)
156 return lookup[i].name;
157 }
158 return "Generic device";
159}
160
Furquan Shaikh7536a392020-04-24 21:59:21 -0700161void superio_common_fill_ssdt_generator(const struct device *dev)
Patrick Rudolphc1621312019-05-28 11:29:29 +0200162{
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200163 if (!dev || !dev->upstream || !dev->upstream->dev) {
Patrick Rudolph1ac2cc22020-02-13 13:00:41 +0100164 printk(BIOS_CRIT, "BUG: Invalid argument in %s!\n", __func__);
165 return;
166 }
167
Patrick Rudolphc1621312019-05-28 11:29:29 +0200168 const char *scope = acpi_device_scope(dev);
169 const char *name = acpi_device_name(dev);
170 const u8 ldn = dev->path.pnp.device & 0xff;
171 const u8 vldn = (dev->path.pnp.device >> 8) & 0x7;
172 const char *hid;
173
Patrick Rudolph1ac2cc22020-02-13 13:00:41 +0100174 /* Validate devicetree settings */
175 bool bug = false;
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200176 if (dev->upstream->dev->path.type != DEVICE_PATH_PNP) {
Patrick Rudolph1ac2cc22020-02-13 13:00:41 +0100177 bug = true;
178 printk(BIOS_CRIT, "BUG: Parent of device %s is not a PNP device\n",
179 dev_path(dev));
Arthur Heymans7fcd4d52023-08-24 15:12:19 +0200180 } else if (dev->upstream->dev->path.pnp.port != dev->path.pnp.port) {
Patrick Rudolph1ac2cc22020-02-13 13:00:41 +0100181 bug = true;
182 printk(BIOS_CRIT, "BUG: Parent of device %s has wrong I/O port\n",
183 dev_path(dev));
184 }
185 if (bug) {
186 printk(BIOS_CRIT, "BUG: Check your devicetree!\n");
187 return;
188 }
189
Patrick Rudolphc1621312019-05-28 11:29:29 +0200190 if (!scope || !name) {
191 printk(BIOS_ERR, "%s: Missing ACPI path/scope\n", dev_path(dev));
192 return;
193 }
194 if (vldn) {
195 printk(BIOS_DEBUG, "%s: Ignoring virtual LDN\n", dev_path(dev));
196 return;
197 }
198
199 printk(BIOS_DEBUG, "%s.%s: %s\n", scope, name, dev_path(dev));
200
201 /* Scope */
202 acpigen_write_scope(scope);
203
204 /* Device */
205 acpigen_write_device(name);
206
Patrick Rudolphc83bab62019-12-13 12:16:06 +0100207 acpi_device_write_uid(dev);
208
Patrick Rudolphc1621312019-05-28 11:29:29 +0200209 acpigen_write_name_byte("LDN", ldn);
210 acpigen_write_name_byte("VLDN", vldn);
211
Patrick Rudolph95bff2e2019-12-10 14:12:03 +0100212 acpigen_write_method("_STA", 0);
213 {
214 acpigen_write_store();
215 acpigen_emit_namestring("^^QLDN");
216 acpigen_write_integer(ldn);
217 acpigen_emit_byte(LOCAL0_OP);
218
219 /* Multiply (Local0, 0xf, Local0) */
220 acpigen_emit_byte(MULTIPLY_OP);
221 acpigen_emit_byte(LOCAL0_OP);
222 acpigen_write_integer(0xf);
223 acpigen_emit_byte(LOCAL0_OP);
224
225 acpigen_emit_byte(RETURN_OP);
226 acpigen_emit_byte(LOCAL0_OP);
Patrick Rudolph95bff2e2019-12-10 14:12:03 +0100227 }
228 acpigen_pop_len(); /* Method */
Patrick Rudolphc1621312019-05-28 11:29:29 +0200229
Patrick Rudolph5c8ff792019-12-13 11:12:33 +0100230 /*
231 * The ACPI6.2 spec Chapter 6.1.5 requires to set a _HID if no _ADR
232 * is present. Tests on Windows 10 showed that this is also true for
233 * disabled (_STA = 0) devices, otherwise it BSODs.
234 */
Patrick Rudolphc1621312019-05-28 11:29:29 +0200235
236 hid = acpi_device_hid(dev);
237 if (!hid) {
238 printk(BIOS_ERR, "%s: SuperIO driver doesn't provide a _HID\n", dev_path(dev));
239 /* Try to guess it... */
240 const struct superio_dev *sdev = superio_guess_function(dev);
241 if (sdev && sdev->acpi_hid) {
242 hid = sdev->acpi_hid;
243 printk(BIOS_WARNING, "%s: Guessed _HID is '%s'\n", dev_path(dev), hid);
244 } else {
245 hid = ACPI_HID_PNP;
246 printk(BIOS_ERR, "%s: Failed to guessed _HID\n", dev_path(dev));
247 }
248 }
249
250 acpigen_write_name_string("_HID", hid);
251 acpigen_write_name_string("_DDN", name_from_hid(hid));
252
Patrick Rudolph95bff2e2019-12-10 14:12:03 +0100253 acpigen_write_method("_DIS", 0);
254 {
255 acpigen_emit_namestring("^^DLDN");
256 acpigen_write_integer(ldn);
257 }
258 acpigen_pop_len(); /* Method */
259
Patrick Rudolph5c8ff792019-12-13 11:12:33 +0100260 if (dev->enabled && has_resources(dev)) {
261 /* Resources - _CRS */
262 acpigen_write_name("_CRS");
263 acpigen_write_resourcetemplate_header();
264 ldn_gen_resources(dev);
265 acpigen_write_resourcetemplate_footer();
266
267 /* Resources - _PRS */
268 acpigen_write_name("_PRS");
269 acpigen_write_resourcetemplate_header();
270 ldn_gen_resources(dev);
271 acpigen_write_resourcetemplate_footer();
272
273 /* Resources base and size for 3rd party ACPI code */
274 ldn_gen_resources_use(dev);
275 }
276
Patrick Rudolphc1621312019-05-28 11:29:29 +0200277 acpigen_pop_len(); /* Device */
278 acpigen_pop_len(); /* Scope */
279}