1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include <superio/common/ssdt.h>
5 #include <device/device.h>
6 #include <device/pnp.h>
7 #include <acpi/acpigen.h>
9 #include <console/console.h>
19 static 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, } },
27 static const u8 io_idx
[] = {PNP_IDX_IO0
, PNP_IDX_IO1
, PNP_IDX_IO2
, PNP_IDX_IO3
};
28 static const u8 irq_idx
[] = {PNP_IDX_IRQ0
, PNP_IDX_IRQ1
};
30 static const struct superio_dev
*superio_guess_function(const struct device
*dev
)
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
)
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
])
41 if (superio_devs
[j
].io_base
[k
] == res
->base
)
42 return &superio_devs
[j
];
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
)
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
])
54 if (superio_devs
[j
].irq
[k
] == res
->base
)
55 return &superio_devs
[j
];
62 /* Return true if there are resources to report */
63 static bool has_resources(const struct device
*dev
)
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
)
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)
80 /* Add IO and IRQ resources for _CRS or _PRS */
81 static void ldn_gen_resources(const struct device
*dev
)
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
)
88 resource_t base
= res
->base
;
89 resource_t size
= res
->size
;
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);
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)
102 irq
|= 1 << res
->base
;
105 acpigen_write_irq(irq
);
109 /* Add resource base and size for additional SuperIO code */
110 static void ldn_gen_resources_use(const struct device
*dev
)
113 for (size_t i
= 0; i
< ARRAY_SIZE(io_idx
); i
++) {
114 struct resource
*res
= probe_resource(dev
, io_idx
[i
]);
115 if (!res
|| !res
->base
|| !res
->size
)
118 snprintf(name
, sizeof(name
), "IO%zXB", i
);
120 acpigen_write_name_integer(name
, res
->base
);
122 snprintf(name
, sizeof(name
), "IO%zXS", i
);
124 acpigen_write_name_integer(name
, res
->size
);
128 const char *superio_common_ldn_acpi_name(const struct device
*dev
)
130 u8 ldn
= dev
->path
.pnp
.device
& 0xff;
131 u8 vldn
= (dev
->path
.pnp
.device
>> 8) & 0x7;
134 snprintf(name
, sizeof(name
), "L%02X%01X", ldn
, vldn
);
141 static const char *name_from_hid(const char *hid
)
143 static const struct {
147 {ACPI_HID_FDC
, "FDC" },
148 {ACPI_HID_KEYBOARD
, "PS2 Keyboard" },
149 {ACPI_HID_MOUSE
, "PS2 Mouse"},
150 {ACPI_HID_COM
, "COM port" },
151 {ACPI_HID_LPT
, "LPT" },
152 {ACPI_HID_PNP
, "Generic PNP device" },
155 for (size_t i
= 0; hid
&& i
< ARRAY_SIZE(lookup
); i
++) {
156 if (strcmp(hid
, lookup
[i
].hid
) == 0)
157 return lookup
[i
].name
;
159 return "Generic device";
162 void superio_common_fill_ssdt_generator(const struct device
*dev
)
164 if (!dev
|| !dev
->upstream
|| !dev
->upstream
->dev
) {
165 printk(BIOS_CRIT
, "BUG: Invalid argument in %s!\n", __func__
);
169 const char *scope
= acpi_device_scope(dev
);
170 const char *name
= acpi_device_name(dev
);
171 const u8 ldn
= dev
->path
.pnp
.device
& 0xff;
172 const u8 vldn
= (dev
->path
.pnp
.device
>> 8) & 0x7;
175 /* Validate devicetree settings */
177 if (dev
->upstream
->dev
->path
.type
!= DEVICE_PATH_PNP
) {
179 printk(BIOS_CRIT
, "BUG: Parent of device %s is not a PNP device\n",
181 } else if (dev
->upstream
->dev
->path
.pnp
.port
!= dev
->path
.pnp
.port
) {
183 printk(BIOS_CRIT
, "BUG: Parent of device %s has wrong I/O port\n",
187 printk(BIOS_CRIT
, "BUG: Check your devicetree!\n");
191 if (!scope
|| !name
) {
192 printk(BIOS_ERR
, "%s: Missing ACPI path/scope\n", dev_path(dev
));
196 printk(BIOS_DEBUG
, "%s: Ignoring virtual LDN\n", dev_path(dev
));
200 printk(BIOS_DEBUG
, "%s.%s: %s\n", scope
, name
, dev_path(dev
));
203 acpigen_write_scope(scope
);
206 acpigen_write_device(name
);
208 acpi_device_write_uid(dev
);
210 acpigen_write_name_byte("LDN", ldn
);
211 acpigen_write_name_byte("VLDN", vldn
);
213 acpigen_write_method("_STA", 0);
215 acpigen_write_store();
216 acpigen_emit_namestring("^^QLDN");
217 acpigen_write_integer(ldn
);
218 acpigen_emit_byte(LOCAL0_OP
);
220 /* Multiply (Local0, 0xf, Local0) */
221 acpigen_emit_byte(MULTIPLY_OP
);
222 acpigen_emit_byte(LOCAL0_OP
);
223 acpigen_write_integer(0xf);
224 acpigen_emit_byte(LOCAL0_OP
);
226 acpigen_emit_byte(RETURN_OP
);
227 acpigen_emit_byte(LOCAL0_OP
);
230 acpigen_pop_len(); /* Method */
233 * The ACPI6.2 spec Chapter 6.1.5 requires to set a _HID if no _ADR
234 * is present. Tests on Windows 10 showed that this is also true for
235 * disabled (_STA = 0) devices, otherwise it BSODs.
238 hid
= acpi_device_hid(dev
);
240 printk(BIOS_ERR
, "%s: SuperIO driver doesn't provide a _HID\n", dev_path(dev
));
241 /* Try to guess it... */
242 const struct superio_dev
*sdev
= superio_guess_function(dev
);
243 if (sdev
&& sdev
->acpi_hid
) {
244 hid
= sdev
->acpi_hid
;
245 printk(BIOS_WARNING
, "%s: Guessed _HID is '%s'\n", dev_path(dev
), hid
);
248 printk(BIOS_ERR
, "%s: Failed to guessed _HID\n", dev_path(dev
));
252 acpigen_write_name_string("_HID", hid
);
253 acpigen_write_name_string("_DDN", name_from_hid(hid
));
255 acpigen_write_method("_DIS", 0);
257 acpigen_emit_namestring("^^DLDN");
258 acpigen_write_integer(ldn
);
260 acpigen_pop_len(); /* Method */
262 if (dev
->enabled
&& has_resources(dev
)) {
263 /* Resources - _CRS */
264 acpigen_write_name("_CRS");
265 acpigen_write_resourcetemplate_header();
266 ldn_gen_resources(dev
);
267 acpigen_write_resourcetemplate_footer();
269 /* Resources - _PRS */
270 acpigen_write_name("_PRS");
271 acpigen_write_resourcetemplate_header();
272 ldn_gen_resources(dev
);
273 acpigen_write_resourcetemplate_footer();
275 /* Resources base and size for 3rd party ACPI code */
276 ldn_gen_resources_use(dev
);
279 acpigen_pop_len(); /* Device */
280 acpigen_pop_len(); /* Scope */