1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/acpi/resource.c - ACPI device resources interpretation.
4 *
5 * Copyright (C) 2012, Intel Corp.
6 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 */
12
13 #include <linux/acpi.h>
14 #include <linux/device.h>
15 #include <linux/export.h>
16 #include <linux/ioport.h>
17 #include <linux/slab.h>
18 #include <linux/irq.h>
19 #include <linux/dmi.h>
20 #include <linux/string_choices.h>
21
22 #ifdef CONFIG_X86
23 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
acpi_iospace_resource_valid(struct resource * res)24 static inline bool acpi_iospace_resource_valid(struct resource *res)
25 {
26 /* On X86 IO space is limited to the [0 - 64K] IO port range */
27 return res->end < 0x10003;
28 }
29 #else
30 #define valid_IRQ(i) (true)
31 /*
32 * ACPI IO descriptors on arches other than X86 contain MMIO CPU physical
33 * addresses mapping IO space in CPU physical address space, IO space
34 * resources can be placed anywhere in the 64-bit physical address space.
35 */
36 static inline bool
acpi_iospace_resource_valid(struct resource * res)37 acpi_iospace_resource_valid(struct resource *res) { return true; }
38 #endif
39
40 #if IS_ENABLED(CONFIG_ACPI_GENERIC_GSI)
is_gsi(struct acpi_resource_extended_irq * ext_irq)41 static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
42 {
43 return ext_irq->resource_source.string_length == 0 &&
44 ext_irq->producer_consumer == ACPI_CONSUMER;
45 }
46 #else
is_gsi(struct acpi_resource_extended_irq * ext_irq)47 static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
48 {
49 return true;
50 }
51 #endif
52
acpi_dev_resource_len_valid(u64 start,u64 end,u64 len,bool io)53 static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
54 {
55 u64 reslen = end - start + 1;
56
57 /*
58 * CHECKME: len might be required to check versus a minimum
59 * length as well. 1 for io is fine, but for memory it does
60 * not make any sense at all.
61 * Note: some BIOSes report incorrect length for ACPI address space
62 * descriptor, so remove check of 'reslen == len' to avoid regression.
63 */
64 if (len && reslen && start <= end)
65 return true;
66
67 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
68 io ? "io" : "mem", start, end, len);
69
70 return false;
71 }
72
acpi_dev_memresource_flags(struct resource * res,u64 len,u8 write_protect)73 static void acpi_dev_memresource_flags(struct resource *res, u64 len,
74 u8 write_protect)
75 {
76 res->flags = IORESOURCE_MEM;
77
78 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
79 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
80
81 if (write_protect == ACPI_READ_WRITE_MEMORY)
82 res->flags |= IORESOURCE_MEM_WRITEABLE;
83 }
84
acpi_dev_get_memresource(struct resource * res,u64 start,u64 len,u8 write_protect)85 static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
86 u8 write_protect)
87 {
88 res->start = start;
89 res->end = start + len - 1;
90 acpi_dev_memresource_flags(res, len, write_protect);
91 }
92
93 /**
94 * acpi_dev_resource_memory - Extract ACPI memory resource information.
95 * @ares: Input ACPI resource object.
96 * @res: Output generic resource object.
97 *
98 * Check if the given ACPI resource object represents a memory resource and
99 * if that's the case, use the information in it to populate the generic
100 * resource object pointed to by @res.
101 *
102 * Return:
103 * 1) false with res->flags setting to zero: not the expected resource type
104 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
105 * 3) true: valid assigned resource
106 */
acpi_dev_resource_memory(struct acpi_resource * ares,struct resource * res)107 bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
108 {
109 struct acpi_resource_memory24 *memory24;
110 struct acpi_resource_memory32 *memory32;
111 struct acpi_resource_fixed_memory32 *fixed_memory32;
112
113 switch (ares->type) {
114 case ACPI_RESOURCE_TYPE_MEMORY24:
115 memory24 = &ares->data.memory24;
116 acpi_dev_get_memresource(res, memory24->minimum << 8,
117 memory24->address_length << 8,
118 memory24->write_protect);
119 break;
120 case ACPI_RESOURCE_TYPE_MEMORY32:
121 memory32 = &ares->data.memory32;
122 acpi_dev_get_memresource(res, memory32->minimum,
123 memory32->address_length,
124 memory32->write_protect);
125 break;
126 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
127 fixed_memory32 = &ares->data.fixed_memory32;
128 acpi_dev_get_memresource(res, fixed_memory32->address,
129 fixed_memory32->address_length,
130 fixed_memory32->write_protect);
131 break;
132 default:
133 res->flags = 0;
134 return false;
135 }
136
137 return !(res->flags & IORESOURCE_DISABLED);
138 }
139 EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
140
acpi_dev_ioresource_flags(struct resource * res,u64 len,u8 io_decode,u8 translation_type)141 static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
142 u8 io_decode, u8 translation_type)
143 {
144 res->flags = IORESOURCE_IO;
145
146 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
147 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
148
149 if (!acpi_iospace_resource_valid(res))
150 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
151
152 if (io_decode == ACPI_DECODE_16)
153 res->flags |= IORESOURCE_IO_16BIT_ADDR;
154 if (translation_type == ACPI_SPARSE_TRANSLATION)
155 res->flags |= IORESOURCE_IO_SPARSE;
156 }
157
acpi_dev_get_ioresource(struct resource * res,u64 start,u64 len,u8 io_decode)158 static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
159 u8 io_decode)
160 {
161 res->start = start;
162 res->end = start + len - 1;
163 acpi_dev_ioresource_flags(res, len, io_decode, 0);
164 }
165
166 /**
167 * acpi_dev_resource_io - Extract ACPI I/O resource information.
168 * @ares: Input ACPI resource object.
169 * @res: Output generic resource object.
170 *
171 * Check if the given ACPI resource object represents an I/O resource and
172 * if that's the case, use the information in it to populate the generic
173 * resource object pointed to by @res.
174 *
175 * Return:
176 * 1) false with res->flags setting to zero: not the expected resource type
177 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
178 * 3) true: valid assigned resource
179 */
acpi_dev_resource_io(struct acpi_resource * ares,struct resource * res)180 bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
181 {
182 struct acpi_resource_io *io;
183 struct acpi_resource_fixed_io *fixed_io;
184
185 switch (ares->type) {
186 case ACPI_RESOURCE_TYPE_IO:
187 io = &ares->data.io;
188 acpi_dev_get_ioresource(res, io->minimum,
189 io->address_length,
190 io->io_decode);
191 break;
192 case ACPI_RESOURCE_TYPE_FIXED_IO:
193 fixed_io = &ares->data.fixed_io;
194 acpi_dev_get_ioresource(res, fixed_io->address,
195 fixed_io->address_length,
196 ACPI_DECODE_10);
197 break;
198 default:
199 res->flags = 0;
200 return false;
201 }
202
203 return !(res->flags & IORESOURCE_DISABLED);
204 }
205 EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
206
acpi_decode_space(struct resource_win * win,struct acpi_resource_address * addr,struct acpi_address64_attribute * attr)207 static bool acpi_decode_space(struct resource_win *win,
208 struct acpi_resource_address *addr,
209 struct acpi_address64_attribute *attr)
210 {
211 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
212 bool wp = addr->info.mem.write_protect;
213 u64 len = attr->address_length;
214 u64 start, end, offset = 0;
215 struct resource *res = &win->res;
216
217 /*
218 * Filter out invalid descriptor according to ACPI Spec 5.0, section
219 * 6.4.3.5 Address Space Resource Descriptors.
220 */
221 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
222 (addr->min_address_fixed && addr->max_address_fixed && !len))
223 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
224 addr->min_address_fixed, addr->max_address_fixed, len);
225
226 /*
227 * For bridges that translate addresses across the bridge,
228 * translation_offset is the offset that must be added to the
229 * address on the secondary side to obtain the address on the
230 * primary side. Non-bridge devices must list 0 for all Address
231 * Translation offset bits.
232 */
233 if (addr->producer_consumer == ACPI_PRODUCER)
234 offset = attr->translation_offset;
235 else if (attr->translation_offset)
236 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
237 attr->translation_offset);
238 start = attr->minimum + offset;
239 end = attr->maximum + offset;
240
241 win->offset = offset;
242 res->start = start;
243 res->end = end;
244 if (sizeof(resource_size_t) < sizeof(u64) &&
245 (offset != win->offset || start != res->start || end != res->end)) {
246 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
247 attr->minimum, attr->maximum);
248 return false;
249 }
250
251 switch (addr->resource_type) {
252 case ACPI_MEMORY_RANGE:
253 acpi_dev_memresource_flags(res, len, wp);
254
255 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
256 res->flags |= IORESOURCE_PREFETCH;
257 break;
258 case ACPI_IO_RANGE:
259 acpi_dev_ioresource_flags(res, len, iodec,
260 addr->info.io.translation_type);
261 break;
262 case ACPI_BUS_NUMBER_RANGE:
263 res->flags = IORESOURCE_BUS;
264 break;
265 default:
266 return false;
267 }
268
269 if (addr->producer_consumer == ACPI_PRODUCER)
270 res->flags |= IORESOURCE_WINDOW;
271
272 return !(res->flags & IORESOURCE_DISABLED);
273 }
274
275 /**
276 * acpi_dev_resource_address_space - Extract ACPI address space information.
277 * @ares: Input ACPI resource object.
278 * @win: Output generic resource object.
279 *
280 * Check if the given ACPI resource object represents an address space resource
281 * and if that's the case, use the information in it to populate the generic
282 * resource object pointed to by @win.
283 *
284 * Return:
285 * 1) false with win->res.flags setting to zero: not the expected resource type
286 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
287 * resource
288 * 3) true: valid assigned resource
289 */
acpi_dev_resource_address_space(struct acpi_resource * ares,struct resource_win * win)290 bool acpi_dev_resource_address_space(struct acpi_resource *ares,
291 struct resource_win *win)
292 {
293 struct acpi_resource_address64 addr;
294
295 win->res.flags = 0;
296 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
297 return false;
298
299 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
300 &addr.address);
301 }
302 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
303
304 /**
305 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
306 * @ares: Input ACPI resource object.
307 * @win: Output generic resource object.
308 *
309 * Check if the given ACPI resource object represents an extended address space
310 * resource and if that's the case, use the information in it to populate the
311 * generic resource object pointed to by @win.
312 *
313 * Return:
314 * 1) false with win->res.flags setting to zero: not the expected resource type
315 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
316 * resource
317 * 3) true: valid assigned resource
318 */
acpi_dev_resource_ext_address_space(struct acpi_resource * ares,struct resource_win * win)319 bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
320 struct resource_win *win)
321 {
322 struct acpi_resource_extended_address64 *ext_addr;
323
324 win->res.flags = 0;
325 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
326 return false;
327
328 ext_addr = &ares->data.ext_address64;
329
330 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
331 &ext_addr->address);
332 }
333 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
334
335 /**
336 * acpi_dev_irq_flags - Determine IRQ resource flags.
337 * @triggering: Triggering type as provided by ACPI.
338 * @polarity: Interrupt polarity as provided by ACPI.
339 * @shareable: Whether or not the interrupt is shareable.
340 * @wake_capable: Wake capability as provided by ACPI.
341 */
acpi_dev_irq_flags(u8 triggering,u8 polarity,u8 shareable,u8 wake_capable)342 unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable, u8 wake_capable)
343 {
344 unsigned long flags;
345
346 if (triggering == ACPI_LEVEL_SENSITIVE)
347 flags = polarity == ACPI_ACTIVE_LOW ?
348 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
349 else
350 flags = polarity == ACPI_ACTIVE_LOW ?
351 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
352
353 if (shareable == ACPI_SHARED)
354 flags |= IORESOURCE_IRQ_SHAREABLE;
355
356 if (wake_capable == ACPI_WAKE_CAPABLE)
357 flags |= IORESOURCE_IRQ_WAKECAPABLE;
358
359 return flags | IORESOURCE_IRQ;
360 }
361 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
362
363 /**
364 * acpi_dev_get_irq_type - Determine irq type.
365 * @triggering: Triggering type as provided by ACPI.
366 * @polarity: Interrupt polarity as provided by ACPI.
367 */
acpi_dev_get_irq_type(int triggering,int polarity)368 unsigned int acpi_dev_get_irq_type(int triggering, int polarity)
369 {
370 switch (polarity) {
371 case ACPI_ACTIVE_LOW:
372 return triggering == ACPI_EDGE_SENSITIVE ?
373 IRQ_TYPE_EDGE_FALLING :
374 IRQ_TYPE_LEVEL_LOW;
375 case ACPI_ACTIVE_HIGH:
376 return triggering == ACPI_EDGE_SENSITIVE ?
377 IRQ_TYPE_EDGE_RISING :
378 IRQ_TYPE_LEVEL_HIGH;
379 case ACPI_ACTIVE_BOTH:
380 if (triggering == ACPI_EDGE_SENSITIVE)
381 return IRQ_TYPE_EDGE_BOTH;
382 fallthrough;
383 default:
384 return IRQ_TYPE_NONE;
385 }
386 }
387 EXPORT_SYMBOL_GPL(acpi_dev_get_irq_type);
388
389 /*
390 * DMI matches for boards where the DSDT specifies the kbd IRQ as
391 * level active-low and using the override changes this to rising edge,
392 * stopping the keyboard from working.
393 */
394 static const struct dmi_system_id irq1_level_low_skip_override[] = {
395 {
396 /* MEDION P15651 */
397 .matches = {
398 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
399 DMI_MATCH(DMI_BOARD_NAME, "M15T"),
400 },
401 },
402 {
403 /* MEDION S17405 */
404 .matches = {
405 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
406 DMI_MATCH(DMI_BOARD_NAME, "M17T"),
407 },
408 },
409 {
410 /* MEDION S17413 */
411 .matches = {
412 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
413 DMI_MATCH(DMI_BOARD_NAME, "M1xA"),
414 },
415 },
416 {
417 /* Asus Vivobook K3402ZA */
418 .matches = {
419 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
420 DMI_MATCH(DMI_BOARD_NAME, "K3402ZA"),
421 },
422 },
423 {
424 /* Asus Vivobook K3502ZA */
425 .matches = {
426 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
427 DMI_MATCH(DMI_BOARD_NAME, "K3502ZA"),
428 },
429 },
430 {
431 /* Asus Vivobook S5402ZA */
432 .matches = {
433 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
434 DMI_MATCH(DMI_BOARD_NAME, "S5402ZA"),
435 },
436 },
437 {
438 /* Asus Vivobook S5602ZA */
439 .matches = {
440 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
441 DMI_MATCH(DMI_BOARD_NAME, "S5602ZA"),
442 },
443 },
444 {
445 /* Asus Vivobook X1404VAP */
446 .matches = {
447 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
448 DMI_MATCH(DMI_BOARD_NAME, "X1404VAP"),
449 },
450 },
451 {
452 /* Asus Vivobook X1504VAP */
453 .matches = {
454 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
455 DMI_MATCH(DMI_BOARD_NAME, "X1504VAP"),
456 },
457 },
458 {
459 /* Asus Vivobook X1704VAP */
460 .matches = {
461 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
462 DMI_MATCH(DMI_BOARD_NAME, "X1704VAP"),
463 },
464 },
465 {
466 /* Asus ExpertBook B1402C* */
467 .matches = {
468 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
469 DMI_MATCH(DMI_BOARD_NAME, "B1402C"),
470 },
471 },
472 {
473 /* Asus ExpertBook B1502C* */
474 .matches = {
475 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
476 DMI_MATCH(DMI_BOARD_NAME, "B1502C"),
477 },
478 },
479 {
480 /* Asus ExpertBook B2402 (B2402CBA / B2402FBA / B2402CVA / B2402FVA) */
481 .matches = {
482 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
483 DMI_MATCH(DMI_BOARD_NAME, "B2402"),
484 },
485 },
486 {
487 /* Asus ExpertBook B2502 (B2502CBA / B2502FBA / B2502CVA / B2502FVA) */
488 .matches = {
489 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
490 DMI_MATCH(DMI_BOARD_NAME, "B2502"),
491 },
492 },
493 {
494 /* Asus Vivobook Go E1404GA* */
495 .matches = {
496 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
497 DMI_MATCH(DMI_BOARD_NAME, "E1404GA"),
498 },
499 },
500 {
501 /* Asus Vivobook E1504GA* */
502 .matches = {
503 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
504 DMI_MATCH(DMI_BOARD_NAME, "E1504GA"),
505 },
506 },
507 {
508 /* Asus Vivobook Pro N6506M* */
509 .matches = {
510 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
511 DMI_MATCH(DMI_BOARD_NAME, "N6506M"),
512 },
513 },
514 {
515 /* Asus Vivobook Pro N6506CU* */
516 .matches = {
517 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
518 DMI_MATCH(DMI_BOARD_NAME, "N6506CU"),
519 },
520 },
521 {
522 /* LG Electronics 17U70P */
523 .matches = {
524 DMI_MATCH(DMI_SYS_VENDOR, "LG Electronics"),
525 DMI_MATCH(DMI_BOARD_NAME, "17U70P"),
526 },
527 },
528 {
529 /* LG Electronics 16T90SP */
530 .matches = {
531 DMI_MATCH(DMI_SYS_VENDOR, "LG Electronics"),
532 DMI_MATCH(DMI_BOARD_NAME, "16T90SP"),
533 },
534 },
535 {
536 /* JWIPC JVC9100 */
537 .matches = {
538 DMI_MATCH(DMI_BOARD_NAME, "JVC9100"),
539 },
540 },
541 { }
542 };
543
544 /*
545 * DMI matches for AMD Zen boards where the DSDT specifies the kbd IRQ
546 * as falling edge and this must be overridden to rising edge,
547 * to have a working keyboard.
548 */
549 static const struct dmi_system_id irq1_edge_low_force_override[] = {
550 {
551 /* MECHREVO Jiaolong17KS Series GM7XG0M */
552 .matches = {
553 DMI_MATCH(DMI_BOARD_NAME, "GM7XG0M"),
554 },
555 },
556 {
557 /* XMG APEX 17 (M23) */
558 .matches = {
559 DMI_MATCH(DMI_BOARD_NAME, "GMxBGxx"),
560 },
561 },
562 {
563 /* TongFang GMxRGxx/XMG CORE 15 (M22)/TUXEDO Stellaris 15 Gen4 AMD */
564 .matches = {
565 DMI_MATCH(DMI_BOARD_NAME, "GMxRGxx"),
566 },
567 },
568 {
569 /* TongFang GMxXGxx/TUXEDO Polaris 15 Gen5 AMD */
570 .matches = {
571 DMI_MATCH(DMI_BOARD_NAME, "GMxXGxx"),
572 },
573 },
574 {
575 /* TongFang GMxXGxX/TUXEDO Polaris 15 Gen5 AMD */
576 .matches = {
577 DMI_MATCH(DMI_BOARD_NAME, "GMxXGxX"),
578 },
579 },
580 {
581 /* TongFang GMxXGxx sold as Eluktronics Inc. RP-15 */
582 .matches = {
583 DMI_MATCH(DMI_SYS_VENDOR, "Eluktronics Inc."),
584 DMI_MATCH(DMI_BOARD_NAME, "RP-15"),
585 },
586 },
587 {
588 .matches = {
589 DMI_MATCH(DMI_SYS_VENDOR, "Eluktronics Inc."),
590 DMI_MATCH(DMI_BOARD_NAME, "MECH-17"),
591 },
592 },
593 {
594 /* TongFang GM6XGxX/TUXEDO Stellaris 16 Gen5 AMD */
595 .matches = {
596 DMI_MATCH(DMI_BOARD_NAME, "GM6XGxX"),
597 },
598 },
599 {
600 /* MAINGEAR Vector Pro 2 15 */
601 .matches = {
602 DMI_MATCH(DMI_SYS_VENDOR, "Micro Electronics Inc"),
603 DMI_MATCH(DMI_PRODUCT_NAME, "MG-VCP2-15A3070T"),
604 }
605 },
606 {
607 /* MAINGEAR Vector Pro 2 17 */
608 .matches = {
609 DMI_MATCH(DMI_SYS_VENDOR, "Micro Electronics Inc"),
610 DMI_MATCH(DMI_PRODUCT_NAME, "MG-VCP2-17A3070T"),
611 },
612 },
613 {
614 /* TongFang GM6BGEQ / PCSpecialist Elimina Pro 16 M, RTX 3050 */
615 .matches = {
616 DMI_MATCH(DMI_BOARD_NAME, "GM6BGEQ"),
617 },
618 },
619 {
620 /* TongFang GM6BG5Q, RTX 4050 */
621 .matches = {
622 DMI_MATCH(DMI_BOARD_NAME, "GM6BG5Q"),
623 },
624 },
625 {
626 /* TongFang GM6BG0Q / PCSpecialist Elimina Pro 16 M, RTX 4060 */
627 .matches = {
628 DMI_MATCH(DMI_BOARD_NAME, "GM6BG0Q"),
629 },
630 },
631 {
632 /* Infinity E15-5A165-BM */
633 .matches = {
634 DMI_MATCH(DMI_BOARD_NAME, "GM5RG1E0009COM"),
635 },
636 },
637 {
638 /* Infinity E15-5A305-1M */
639 .matches = {
640 DMI_MATCH(DMI_BOARD_NAME, "GM5RGEE0016COM"),
641 },
642 },
643 {
644 /* Lunnen Ground 15 / AMD Ryzen 5 5500U */
645 .matches = {
646 DMI_MATCH(DMI_SYS_VENDOR, "Lunnen"),
647 DMI_MATCH(DMI_BOARD_NAME, "LLL5DAW"),
648 },
649 },
650 {
651 /* Lunnen Ground 16 / AMD Ryzen 7 5800U */
652 .matches = {
653 DMI_MATCH(DMI_SYS_VENDOR, "Lunnen"),
654 DMI_MATCH(DMI_BOARD_NAME, "LL6FA"),
655 },
656 },
657 {
658 /* MAIBENBEN X577 */
659 .matches = {
660 DMI_MATCH(DMI_SYS_VENDOR, "MAIBENBEN"),
661 DMI_MATCH(DMI_BOARD_NAME, "X577"),
662 },
663 },
664 {
665 /* Maibenben X565 */
666 .matches = {
667 DMI_MATCH(DMI_SYS_VENDOR, "MAIBENBEN"),
668 DMI_MATCH(DMI_BOARD_NAME, "X565"),
669 },
670 },
671 {
672 /* TongFang GXxHRXx/TUXEDO InfinityBook Pro Gen9 AMD */
673 .matches = {
674 DMI_MATCH(DMI_BOARD_NAME, "GXxHRXx"),
675 },
676 },
677 {
678 /* TongFang GMxHGxx/TUXEDO Stellaris Slim Gen1 AMD */
679 .matches = {
680 DMI_MATCH(DMI_BOARD_NAME, "GMxHGxx"),
681 },
682 },
683 {
684 /* MACHENIKE L16P/L16P */
685 .matches = {
686 DMI_MATCH(DMI_SYS_VENDOR, "MACHENIKE"),
687 DMI_MATCH(DMI_BOARD_NAME, "L16P"),
688 },
689 },
690 {
691 /*
692 * TongFang GM5HG0A in case of the SKIKK Vanaheim relabel the
693 * board-name is changed, so check OEM strings instead. Note
694 * OEM string matches are always exact matches.
695 * https://bugzilla.kernel.org/show_bug.cgi?id=219614
696 */
697 .matches = {
698 DMI_EXACT_MATCH(DMI_OEM_STRING, "GM5HG0A"),
699 },
700 },
701 { }
702 };
703
704 struct irq_override_cmp {
705 const struct dmi_system_id *system;
706 unsigned char irq;
707 unsigned char triggering;
708 unsigned char polarity;
709 unsigned char shareable;
710 bool override;
711 };
712
713 static const struct irq_override_cmp override_table[] = {
714 { irq1_level_low_skip_override, 1, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 0, false },
715 { irq1_level_low_skip_override, 10, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 1, false },
716 { irq1_level_low_skip_override, 11, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 1, false },
717 { irq1_edge_low_force_override, 1, ACPI_EDGE_SENSITIVE, ACPI_ACTIVE_LOW, 1, true },
718 };
719
acpi_dev_irq_override(u32 gsi,u8 triggering,u8 polarity,u8 shareable)720 static bool acpi_dev_irq_override(u32 gsi, u8 triggering, u8 polarity,
721 u8 shareable)
722 {
723 int i;
724
725 for (i = 0; i < ARRAY_SIZE(override_table); i++) {
726 const struct irq_override_cmp *entry = &override_table[i];
727
728 if (entry->irq == gsi &&
729 entry->triggering == triggering &&
730 entry->polarity == polarity &&
731 entry->shareable == shareable &&
732 dmi_check_system(entry->system))
733 return entry->override;
734 }
735
736 #ifdef CONFIG_X86
737 /*
738 * Always use the MADT override info, except for the i8042 PS/2 ctrl
739 * IRQs (1 and 12). For these the DSDT IRQ settings should sometimes
740 * be used otherwise PS/2 keyboards / mice will not work.
741 */
742 if (gsi != 1 && gsi != 12)
743 return true;
744
745 /* If the override comes from an INT_SRC_OVR MADT entry, honor it. */
746 if (acpi_int_src_ovr[gsi])
747 return true;
748
749 /*
750 * IRQ override isn't needed on modern AMD Zen systems and
751 * this override breaks active low IRQs on AMD Ryzen 6000 and
752 * newer systems. Skip it.
753 */
754 if (boot_cpu_has(X86_FEATURE_ZEN))
755 return false;
756 #endif
757
758 return true;
759 }
760
acpi_dev_get_irqresource(struct resource * res,u32 gsi,u8 triggering,u8 polarity,u8 shareable,u8 wake_capable,bool check_override)761 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
762 u8 triggering, u8 polarity, u8 shareable,
763 u8 wake_capable, bool check_override)
764 {
765 int irq, p, t;
766
767 if (!valid_IRQ(gsi)) {
768 irqresource_disabled(res, gsi);
769 return;
770 }
771
772 /*
773 * In IO-APIC mode, use overridden attribute. Two reasons:
774 * 1. BIOS bug in DSDT
775 * 2. BIOS uses IO-APIC mode Interrupt Source Override
776 *
777 * We do this only if we are dealing with IRQ() or IRQNoFlags()
778 * resource (the legacy ISA resources). With modern ACPI 5 devices
779 * using extended IRQ descriptors we take the IRQ configuration
780 * from _CRS directly.
781 */
782 if (check_override &&
783 acpi_dev_irq_override(gsi, triggering, polarity, shareable) &&
784 !acpi_get_override_irq(gsi, &t, &p)) {
785 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
786 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
787
788 if (triggering != trig || polarity != pol) {
789 pr_warn("ACPI: IRQ %d override to %s%s, %s%s\n", gsi,
790 t ? "level" : "edge",
791 trig == triggering ? "" : "(!)",
792 str_low_high(p),
793 pol == polarity ? "" : "(!)");
794 triggering = trig;
795 polarity = pol;
796 }
797 }
798
799 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable, wake_capable);
800 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
801 if (irq >= 0) {
802 res->start = irq;
803 res->end = irq;
804 } else {
805 irqresource_disabled(res, gsi);
806 }
807 }
808
809 /**
810 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
811 * @ares: Input ACPI resource object.
812 * @index: Index into the array of GSIs represented by the resource.
813 * @res: Output generic resource object.
814 *
815 * Check if the given ACPI resource object represents an interrupt resource
816 * and @index does not exceed the resource's interrupt count (true is returned
817 * in that case regardless of the results of the other checks)). If that's the
818 * case, register the GSI corresponding to @index from the array of interrupts
819 * represented by the resource and populate the generic resource object pointed
820 * to by @res accordingly. If the registration of the GSI is not successful,
821 * IORESOURCE_DISABLED will be set it that object's flags.
822 *
823 * Return:
824 * 1) false with res->flags setting to zero: not the expected resource type
825 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
826 * 3) true: valid assigned resource
827 */
acpi_dev_resource_interrupt(struct acpi_resource * ares,int index,struct resource * res)828 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
829 struct resource *res)
830 {
831 struct acpi_resource_irq *irq;
832 struct acpi_resource_extended_irq *ext_irq;
833
834 switch (ares->type) {
835 case ACPI_RESOURCE_TYPE_IRQ:
836 /*
837 * Per spec, only one interrupt per descriptor is allowed in
838 * _CRS, but some firmware violates this, so parse them all.
839 */
840 irq = &ares->data.irq;
841 if (index >= irq->interrupt_count) {
842 irqresource_disabled(res, 0);
843 return false;
844 }
845 acpi_dev_get_irqresource(res, irq->interrupts[index],
846 irq->triggering, irq->polarity,
847 irq->shareable, irq->wake_capable,
848 true);
849 break;
850 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
851 ext_irq = &ares->data.extended_irq;
852 if (index >= ext_irq->interrupt_count) {
853 irqresource_disabled(res, 0);
854 return false;
855 }
856 if (is_gsi(ext_irq))
857 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
858 ext_irq->triggering, ext_irq->polarity,
859 ext_irq->shareable, ext_irq->wake_capable,
860 false);
861 else
862 irqresource_disabled(res, 0);
863 break;
864 default:
865 res->flags = 0;
866 return false;
867 }
868
869 return true;
870 }
871 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
872
873 /**
874 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
875 * @list: The head of the resource list to free.
876 */
acpi_dev_free_resource_list(struct list_head * list)877 void acpi_dev_free_resource_list(struct list_head *list)
878 {
879 resource_list_free(list);
880 }
881 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
882
883 struct res_proc_context {
884 struct list_head *list;
885 int (*preproc)(struct acpi_resource *, void *);
886 void *preproc_data;
887 int count;
888 int error;
889 };
890
acpi_dev_new_resource_entry(struct resource_win * win,struct res_proc_context * c)891 static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
892 struct res_proc_context *c)
893 {
894 struct resource_entry *rentry;
895
896 rentry = resource_list_create_entry(NULL, 0);
897 if (!rentry) {
898 c->error = -ENOMEM;
899 return AE_NO_MEMORY;
900 }
901 *rentry->res = win->res;
902 rentry->offset = win->offset;
903 resource_list_add_tail(rentry, c->list);
904 c->count++;
905 return AE_OK;
906 }
907
acpi_dev_process_resource(struct acpi_resource * ares,void * context)908 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
909 void *context)
910 {
911 struct res_proc_context *c = context;
912 struct resource_win win;
913 struct resource *res = &win.res;
914 int i;
915
916 if (c->preproc) {
917 int ret;
918
919 ret = c->preproc(ares, c->preproc_data);
920 if (ret < 0) {
921 c->error = ret;
922 return AE_ABORT_METHOD;
923 } else if (ret > 0) {
924 return AE_OK;
925 }
926 }
927
928 memset(&win, 0, sizeof(win));
929
930 if (acpi_dev_resource_memory(ares, res)
931 || acpi_dev_resource_io(ares, res)
932 || acpi_dev_resource_address_space(ares, &win)
933 || acpi_dev_resource_ext_address_space(ares, &win))
934 return acpi_dev_new_resource_entry(&win, c);
935
936 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
937 acpi_status status;
938
939 status = acpi_dev_new_resource_entry(&win, c);
940 if (ACPI_FAILURE(status))
941 return status;
942 }
943
944 return AE_OK;
945 }
946
__acpi_dev_get_resources(struct acpi_device * adev,struct list_head * list,int (* preproc)(struct acpi_resource *,void *),void * preproc_data,char * method)947 static int __acpi_dev_get_resources(struct acpi_device *adev,
948 struct list_head *list,
949 int (*preproc)(struct acpi_resource *, void *),
950 void *preproc_data, char *method)
951 {
952 struct res_proc_context c;
953 acpi_status status;
954
955 if (!adev || !adev->handle || !list_empty(list))
956 return -EINVAL;
957
958 if (!acpi_has_method(adev->handle, method))
959 return 0;
960
961 c.list = list;
962 c.preproc = preproc;
963 c.preproc_data = preproc_data;
964 c.count = 0;
965 c.error = 0;
966 status = acpi_walk_resources(adev->handle, method,
967 acpi_dev_process_resource, &c);
968 if (ACPI_FAILURE(status)) {
969 acpi_dev_free_resource_list(list);
970 return c.error ? c.error : -EIO;
971 }
972
973 return c.count;
974 }
975
976 /**
977 * acpi_dev_get_resources - Get current resources of a device.
978 * @adev: ACPI device node to get the resources for.
979 * @list: Head of the resultant list of resources (must be empty).
980 * @preproc: The caller's preprocessing routine.
981 * @preproc_data: Pointer passed to the caller's preprocessing routine.
982 *
983 * Evaluate the _CRS method for the given device node and process its output by
984 * (1) executing the @preproc() routine provided by the caller, passing the
985 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
986 * returned and (2) converting all of the returned ACPI resources into struct
987 * resource objects if possible. If the return value of @preproc() in step (1)
988 * is different from 0, step (2) is not applied to the given ACPI resource and
989 * if that value is negative, the whole processing is aborted and that value is
990 * returned as the final error code.
991 *
992 * The resultant struct resource objects are put on the list pointed to by
993 * @list, that must be empty initially, as members of struct resource_entry
994 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
995 * free that list.
996 *
997 * The number of resources in the output list is returned on success, an error
998 * code reflecting the error condition is returned otherwise.
999 */
acpi_dev_get_resources(struct acpi_device * adev,struct list_head * list,int (* preproc)(struct acpi_resource *,void *),void * preproc_data)1000 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
1001 int (*preproc)(struct acpi_resource *, void *),
1002 void *preproc_data)
1003 {
1004 return __acpi_dev_get_resources(adev, list, preproc, preproc_data,
1005 METHOD_NAME__CRS);
1006 }
1007 EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
1008
is_memory(struct acpi_resource * ares,void * not_used)1009 static int is_memory(struct acpi_resource *ares, void *not_used)
1010 {
1011 struct resource_win win;
1012 struct resource *res = &win.res;
1013
1014 memset(&win, 0, sizeof(win));
1015
1016 if (acpi_dev_filter_resource_type(ares, IORESOURCE_MEM))
1017 return 1;
1018
1019 return !(acpi_dev_resource_memory(ares, res)
1020 || acpi_dev_resource_address_space(ares, &win)
1021 || acpi_dev_resource_ext_address_space(ares, &win));
1022 }
1023
1024 /**
1025 * acpi_dev_get_dma_resources - Get current DMA resources of a device.
1026 * @adev: ACPI device node to get the resources for.
1027 * @list: Head of the resultant list of resources (must be empty).
1028 *
1029 * Evaluate the _DMA method for the given device node and process its
1030 * output.
1031 *
1032 * The resultant struct resource objects are put on the list pointed to
1033 * by @list, that must be empty initially, as members of struct
1034 * resource_entry objects. Callers of this routine should use
1035 * %acpi_dev_free_resource_list() to free that list.
1036 *
1037 * The number of resources in the output list is returned on success,
1038 * an error code reflecting the error condition is returned otherwise.
1039 */
acpi_dev_get_dma_resources(struct acpi_device * adev,struct list_head * list)1040 int acpi_dev_get_dma_resources(struct acpi_device *adev, struct list_head *list)
1041 {
1042 return __acpi_dev_get_resources(adev, list, is_memory, NULL,
1043 METHOD_NAME__DMA);
1044 }
1045 EXPORT_SYMBOL_GPL(acpi_dev_get_dma_resources);
1046
1047 /**
1048 * acpi_dev_get_memory_resources - Get current memory resources of a device.
1049 * @adev: ACPI device node to get the resources for.
1050 * @list: Head of the resultant list of resources (must be empty).
1051 *
1052 * This is a helper function that locates all memory type resources of @adev
1053 * with acpi_dev_get_resources().
1054 *
1055 * The number of resources in the output list is returned on success, an error
1056 * code reflecting the error condition is returned otherwise.
1057 */
acpi_dev_get_memory_resources(struct acpi_device * adev,struct list_head * list)1058 int acpi_dev_get_memory_resources(struct acpi_device *adev, struct list_head *list)
1059 {
1060 return acpi_dev_get_resources(adev, list, is_memory, NULL);
1061 }
1062 EXPORT_SYMBOL_GPL(acpi_dev_get_memory_resources);
1063
1064 /**
1065 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
1066 * types
1067 * @ares: Input ACPI resource object.
1068 * @types: Valid resource types of IORESOURCE_XXX
1069 *
1070 * This is a helper function to support acpi_dev_get_resources(), which filters
1071 * ACPI resource objects according to resource types.
1072 */
acpi_dev_filter_resource_type(struct acpi_resource * ares,unsigned long types)1073 int acpi_dev_filter_resource_type(struct acpi_resource *ares,
1074 unsigned long types)
1075 {
1076 unsigned long type = 0;
1077
1078 switch (ares->type) {
1079 case ACPI_RESOURCE_TYPE_MEMORY24:
1080 case ACPI_RESOURCE_TYPE_MEMORY32:
1081 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1082 type = IORESOURCE_MEM;
1083 break;
1084 case ACPI_RESOURCE_TYPE_IO:
1085 case ACPI_RESOURCE_TYPE_FIXED_IO:
1086 type = IORESOURCE_IO;
1087 break;
1088 case ACPI_RESOURCE_TYPE_IRQ:
1089 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1090 type = IORESOURCE_IRQ;
1091 break;
1092 case ACPI_RESOURCE_TYPE_DMA:
1093 case ACPI_RESOURCE_TYPE_FIXED_DMA:
1094 type = IORESOURCE_DMA;
1095 break;
1096 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1097 type = IORESOURCE_REG;
1098 break;
1099 case ACPI_RESOURCE_TYPE_ADDRESS16:
1100 case ACPI_RESOURCE_TYPE_ADDRESS32:
1101 case ACPI_RESOURCE_TYPE_ADDRESS64:
1102 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1103 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
1104 type = IORESOURCE_MEM;
1105 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
1106 type = IORESOURCE_IO;
1107 else if (ares->data.address.resource_type ==
1108 ACPI_BUS_NUMBER_RANGE)
1109 type = IORESOURCE_BUS;
1110 break;
1111 default:
1112 break;
1113 }
1114
1115 return (type & types) ? 0 : 1;
1116 }
1117 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);
1118
acpi_dev_consumes_res(struct acpi_device * adev,struct resource * res)1119 static int acpi_dev_consumes_res(struct acpi_device *adev, struct resource *res)
1120 {
1121 struct list_head resource_list;
1122 struct resource_entry *rentry;
1123 int ret, found = 0;
1124
1125 INIT_LIST_HEAD(&resource_list);
1126 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
1127 if (ret < 0)
1128 return 0;
1129
1130 list_for_each_entry(rentry, &resource_list, node) {
1131 if (resource_contains(rentry->res, res)) {
1132 found = 1;
1133 break;
1134 }
1135
1136 }
1137
1138 acpi_dev_free_resource_list(&resource_list);
1139 return found;
1140 }
1141
acpi_res_consumer_cb(acpi_handle handle,u32 depth,void * context,void ** ret)1142 static acpi_status acpi_res_consumer_cb(acpi_handle handle, u32 depth,
1143 void *context, void **ret)
1144 {
1145 struct resource *res = context;
1146 struct acpi_device **consumer = (struct acpi_device **) ret;
1147 struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
1148
1149 if (!adev)
1150 return AE_OK;
1151
1152 if (acpi_dev_consumes_res(adev, res)) {
1153 *consumer = adev;
1154 return AE_CTRL_TERMINATE;
1155 }
1156
1157 return AE_OK;
1158 }
1159
1160 /**
1161 * acpi_resource_consumer - Find the ACPI device that consumes @res.
1162 * @res: Resource to search for.
1163 *
1164 * Search the current resource settings (_CRS) of every ACPI device node
1165 * for @res. If we find an ACPI device whose _CRS includes @res, return
1166 * it. Otherwise, return NULL.
1167 */
acpi_resource_consumer(struct resource * res)1168 struct acpi_device *acpi_resource_consumer(struct resource *res)
1169 {
1170 struct acpi_device *consumer = NULL;
1171
1172 acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer);
1173 return consumer;
1174 }
1175