xref: /linux/drivers/acpi/resource.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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))
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
37 acpi_iospace_resource_valid(struct resource *res) { return true; }
38 #endif
39 
40 #if IS_ENABLED(CONFIG_ACPI_GENERIC_GSI)
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
47 static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
48 {
49 	return true;
50 }
51 #endif
52 
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 
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 
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  */
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 
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 
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  */
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 
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  */
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  */
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  */
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  */
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 };
537 
538 /*
539  * DMI matches for AMD Zen boards where the DSDT specifies the kbd IRQ
540  * as falling edge and this must be overridden to rising edge,
541  * to have a working keyboard.
542  */
543 static const struct dmi_system_id irq1_edge_low_force_override[] = {
544 	{
545 		/* MECHREVO Jiaolong17KS Series GM7XG0M */
546 		.matches = {
547 			DMI_MATCH(DMI_BOARD_NAME, "GM7XG0M"),
548 		},
549 	},
550 	{
551 		/* XMG APEX 17 (M23) */
552 		.matches = {
553 			DMI_MATCH(DMI_BOARD_NAME, "GMxBGxx"),
554 		},
555 	},
556 	{
557 		/* TongFang GMxRGxx/XMG CORE 15 (M22)/TUXEDO Stellaris 15 Gen4 AMD */
558 		.matches = {
559 			DMI_MATCH(DMI_BOARD_NAME, "GMxRGxx"),
560 		},
561 	},
562 	{
563 		/* TongFang GMxXGxx/TUXEDO Polaris 15 Gen5 AMD */
564 		.matches = {
565 			DMI_MATCH(DMI_BOARD_NAME, "GMxXGxx"),
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 sold as Eluktronics Inc. RP-15 */
576 		.matches = {
577 			DMI_MATCH(DMI_SYS_VENDOR, "Eluktronics Inc."),
578 			DMI_MATCH(DMI_BOARD_NAME, "RP-15"),
579 		},
580 	},
581 	{
582 		.matches = {
583 			DMI_MATCH(DMI_SYS_VENDOR, "Eluktronics Inc."),
584 			DMI_MATCH(DMI_BOARD_NAME, "MECH-17"),
585 		},
586 	},
587 	{
588 		/* TongFang GM6XGxX/TUXEDO Stellaris 16 Gen5 AMD */
589 		.matches = {
590 			DMI_MATCH(DMI_BOARD_NAME, "GM6XGxX"),
591 		},
592 	},
593 	{
594 		/* MAINGEAR Vector Pro 2 15 */
595 		.matches = {
596 			DMI_MATCH(DMI_SYS_VENDOR, "Micro Electronics Inc"),
597 			DMI_MATCH(DMI_PRODUCT_NAME, "MG-VCP2-15A3070T"),
598 		}
599 	},
600 	{
601 		/* MAINGEAR Vector Pro 2 17 */
602 		.matches = {
603 			DMI_MATCH(DMI_SYS_VENDOR, "Micro Electronics Inc"),
604 			DMI_MATCH(DMI_PRODUCT_NAME, "MG-VCP2-17A3070T"),
605 		},
606 	},
607 	{
608 		/* TongFang GM6BGEQ / PCSpecialist Elimina Pro 16 M, RTX 3050 */
609 		.matches = {
610 			DMI_MATCH(DMI_BOARD_NAME, "GM6BGEQ"),
611 		},
612 	},
613 	{
614 		/* TongFang GM6BG5Q, RTX 4050 */
615 		.matches = {
616 			DMI_MATCH(DMI_BOARD_NAME, "GM6BG5Q"),
617 		},
618 	},
619 	{
620 		/* TongFang GM6BG0Q / PCSpecialist Elimina Pro 16 M, RTX 4060 */
621 		.matches = {
622 			DMI_MATCH(DMI_BOARD_NAME, "GM6BG0Q"),
623 		},
624 	},
625 	{
626 		/* Infinity E15-5A165-BM */
627 		.matches = {
628 			DMI_MATCH(DMI_BOARD_NAME, "GM5RG1E0009COM"),
629 		},
630 	},
631 	{
632 		/* Infinity E15-5A305-1M */
633 		.matches = {
634 			DMI_MATCH(DMI_BOARD_NAME, "GM5RGEE0016COM"),
635 		},
636 	},
637 	{
638 		/* Lunnen Ground 15 / AMD Ryzen 5 5500U */
639 		.matches = {
640 			DMI_MATCH(DMI_SYS_VENDOR, "Lunnen"),
641 			DMI_MATCH(DMI_BOARD_NAME, "LLL5DAW"),
642 		},
643 	},
644 	{
645 		/* Lunnen Ground 16 / AMD Ryzen 7 5800U */
646 		.matches = {
647 			DMI_MATCH(DMI_SYS_VENDOR, "Lunnen"),
648 			DMI_MATCH(DMI_BOARD_NAME, "LL6FA"),
649 		},
650 	},
651 	{
652 		/* MAIBENBEN X577 */
653 		.matches = {
654 			DMI_MATCH(DMI_SYS_VENDOR, "MAIBENBEN"),
655 			DMI_MATCH(DMI_BOARD_NAME, "X577"),
656 		},
657 	},
658 	{
659 		/* Maibenben X565 */
660 		.matches = {
661 			DMI_MATCH(DMI_SYS_VENDOR, "MAIBENBEN"),
662 			DMI_MATCH(DMI_BOARD_NAME, "X565"),
663 		},
664 	},
665 	{
666 		/* TongFang GXxHRXx/TUXEDO InfinityBook Pro Gen9 AMD */
667 		.matches = {
668 			DMI_MATCH(DMI_BOARD_NAME, "GXxHRXx"),
669 		},
670 	},
671 	{
672 		/* TongFang GMxHGxx/TUXEDO Stellaris Slim Gen1 AMD */
673 		.matches = {
674 			DMI_MATCH(DMI_BOARD_NAME, "GMxHGxx"),
675 		},
676 	},
677 	{
678 		/* MACHENIKE L16P/L16P */
679 		.matches = {
680 			DMI_MATCH(DMI_SYS_VENDOR, "MACHENIKE"),
681 			DMI_MATCH(DMI_BOARD_NAME, "L16P"),
682 		},
683 	},
684 	{
685 		/*
686 		 * TongFang GM5HG0A in case of the SKIKK Vanaheim relabel the
687 		 * board-name is changed, so check OEM strings instead. Note
688 		 * OEM string matches are always exact matches.
689 		 * https://bugzilla.kernel.org/show_bug.cgi?id=219614
690 		 */
691 		.matches = {
692 			DMI_EXACT_MATCH(DMI_OEM_STRING, "GM5HG0A"),
693 		},
694 	},
695 	{ }
696 };
697 
698 struct irq_override_cmp {
699 	const struct dmi_system_id *system;
700 	unsigned char irq;
701 	unsigned char triggering;
702 	unsigned char polarity;
703 	unsigned char shareable;
704 	bool override;
705 };
706 
707 static const struct irq_override_cmp override_table[] = {
708 	{ irq1_level_low_skip_override, 1, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 0, false },
709 	{ irq1_edge_low_force_override, 1, ACPI_EDGE_SENSITIVE, ACPI_ACTIVE_LOW, 1, true },
710 };
711 
712 static bool acpi_dev_irq_override(u32 gsi, u8 triggering, u8 polarity,
713 				  u8 shareable)
714 {
715 	int i;
716 
717 	for (i = 0; i < ARRAY_SIZE(override_table); i++) {
718 		const struct irq_override_cmp *entry = &override_table[i];
719 
720 		if (entry->irq == gsi &&
721 		    entry->triggering == triggering &&
722 		    entry->polarity == polarity &&
723 		    entry->shareable == shareable &&
724 		    dmi_check_system(entry->system))
725 			return entry->override;
726 	}
727 
728 #ifdef CONFIG_X86
729 	/*
730 	 * Always use the MADT override info, except for the i8042 PS/2 ctrl
731 	 * IRQs (1 and 12). For these the DSDT IRQ settings should sometimes
732 	 * be used otherwise PS/2 keyboards / mice will not work.
733 	 */
734 	if (gsi != 1 && gsi != 12)
735 		return true;
736 
737 	/* If the override comes from an INT_SRC_OVR MADT entry, honor it. */
738 	if (acpi_int_src_ovr[gsi])
739 		return true;
740 
741 	/*
742 	 * IRQ override isn't needed on modern AMD Zen systems and
743 	 * this override breaks active low IRQs on AMD Ryzen 6000 and
744 	 * newer systems. Skip it.
745 	 */
746 	if (boot_cpu_has(X86_FEATURE_ZEN))
747 		return false;
748 #endif
749 
750 	return true;
751 }
752 
753 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
754 				     u8 triggering, u8 polarity, u8 shareable,
755 				     u8 wake_capable, bool check_override)
756 {
757 	int irq, p, t;
758 
759 	if (!valid_IRQ(gsi)) {
760 		irqresource_disabled(res, gsi);
761 		return;
762 	}
763 
764 	/*
765 	 * In IO-APIC mode, use overridden attribute. Two reasons:
766 	 * 1. BIOS bug in DSDT
767 	 * 2. BIOS uses IO-APIC mode Interrupt Source Override
768 	 *
769 	 * We do this only if we are dealing with IRQ() or IRQNoFlags()
770 	 * resource (the legacy ISA resources). With modern ACPI 5 devices
771 	 * using extended IRQ descriptors we take the IRQ configuration
772 	 * from _CRS directly.
773 	 */
774 	if (check_override &&
775 	    acpi_dev_irq_override(gsi, triggering, polarity, shareable) &&
776 	    !acpi_get_override_irq(gsi, &t, &p)) {
777 		u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
778 		u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
779 
780 		if (triggering != trig || polarity != pol) {
781 			pr_warn("ACPI: IRQ %d override to %s%s, %s%s\n", gsi,
782 				t ? "level" : "edge",
783 				trig == triggering ? "" : "(!)",
784 				str_low_high(p),
785 				pol == polarity ? "" : "(!)");
786 			triggering = trig;
787 			polarity = pol;
788 		}
789 	}
790 
791 	res->flags = acpi_dev_irq_flags(triggering, polarity, shareable, wake_capable);
792 	irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
793 	if (irq >= 0) {
794 		res->start = irq;
795 		res->end = irq;
796 	} else {
797 		irqresource_disabled(res, gsi);
798 	}
799 }
800 
801 /**
802  * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
803  * @ares: Input ACPI resource object.
804  * @index: Index into the array of GSIs represented by the resource.
805  * @res: Output generic resource object.
806  *
807  * Check if the given ACPI resource object represents an interrupt resource
808  * and @index does not exceed the resource's interrupt count (true is returned
809  * in that case regardless of the results of the other checks)).  If that's the
810  * case, register the GSI corresponding to @index from the array of interrupts
811  * represented by the resource and populate the generic resource object pointed
812  * to by @res accordingly.  If the registration of the GSI is not successful,
813  * IORESOURCE_DISABLED will be set it that object's flags.
814  *
815  * Return:
816  * 1) false with res->flags setting to zero: not the expected resource type
817  * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
818  * 3) true: valid assigned resource
819  */
820 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
821 				 struct resource *res)
822 {
823 	struct acpi_resource_irq *irq;
824 	struct acpi_resource_extended_irq *ext_irq;
825 
826 	switch (ares->type) {
827 	case ACPI_RESOURCE_TYPE_IRQ:
828 		/*
829 		 * Per spec, only one interrupt per descriptor is allowed in
830 		 * _CRS, but some firmware violates this, so parse them all.
831 		 */
832 		irq = &ares->data.irq;
833 		if (index >= irq->interrupt_count) {
834 			irqresource_disabled(res, 0);
835 			return false;
836 		}
837 		acpi_dev_get_irqresource(res, irq->interrupts[index],
838 					 irq->triggering, irq->polarity,
839 					 irq->shareable, irq->wake_capable,
840 					 true);
841 		break;
842 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
843 		ext_irq = &ares->data.extended_irq;
844 		if (index >= ext_irq->interrupt_count) {
845 			irqresource_disabled(res, 0);
846 			return false;
847 		}
848 		if (is_gsi(ext_irq))
849 			acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
850 					 ext_irq->triggering, ext_irq->polarity,
851 					 ext_irq->shareable, ext_irq->wake_capable,
852 					 false);
853 		else
854 			irqresource_disabled(res, 0);
855 		break;
856 	default:
857 		res->flags = 0;
858 		return false;
859 	}
860 
861 	return true;
862 }
863 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
864 
865 /**
866  * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
867  * @list: The head of the resource list to free.
868  */
869 void acpi_dev_free_resource_list(struct list_head *list)
870 {
871 	resource_list_free(list);
872 }
873 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
874 
875 struct res_proc_context {
876 	struct list_head *list;
877 	int (*preproc)(struct acpi_resource *, void *);
878 	void *preproc_data;
879 	int count;
880 	int error;
881 };
882 
883 static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
884 					       struct res_proc_context *c)
885 {
886 	struct resource_entry *rentry;
887 
888 	rentry = resource_list_create_entry(NULL, 0);
889 	if (!rentry) {
890 		c->error = -ENOMEM;
891 		return AE_NO_MEMORY;
892 	}
893 	*rentry->res = win->res;
894 	rentry->offset = win->offset;
895 	resource_list_add_tail(rentry, c->list);
896 	c->count++;
897 	return AE_OK;
898 }
899 
900 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
901 					     void *context)
902 {
903 	struct res_proc_context *c = context;
904 	struct resource_win win;
905 	struct resource *res = &win.res;
906 	int i;
907 
908 	if (c->preproc) {
909 		int ret;
910 
911 		ret = c->preproc(ares, c->preproc_data);
912 		if (ret < 0) {
913 			c->error = ret;
914 			return AE_ABORT_METHOD;
915 		} else if (ret > 0) {
916 			return AE_OK;
917 		}
918 	}
919 
920 	memset(&win, 0, sizeof(win));
921 
922 	if (acpi_dev_resource_memory(ares, res)
923 	    || acpi_dev_resource_io(ares, res)
924 	    || acpi_dev_resource_address_space(ares, &win)
925 	    || acpi_dev_resource_ext_address_space(ares, &win))
926 		return acpi_dev_new_resource_entry(&win, c);
927 
928 	for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
929 		acpi_status status;
930 
931 		status = acpi_dev_new_resource_entry(&win, c);
932 		if (ACPI_FAILURE(status))
933 			return status;
934 	}
935 
936 	return AE_OK;
937 }
938 
939 static int __acpi_dev_get_resources(struct acpi_device *adev,
940 				    struct list_head *list,
941 				    int (*preproc)(struct acpi_resource *, void *),
942 				    void *preproc_data, char *method)
943 {
944 	struct res_proc_context c;
945 	acpi_status status;
946 
947 	if (!adev || !adev->handle || !list_empty(list))
948 		return -EINVAL;
949 
950 	if (!acpi_has_method(adev->handle, method))
951 		return 0;
952 
953 	c.list = list;
954 	c.preproc = preproc;
955 	c.preproc_data = preproc_data;
956 	c.count = 0;
957 	c.error = 0;
958 	status = acpi_walk_resources(adev->handle, method,
959 				     acpi_dev_process_resource, &c);
960 	if (ACPI_FAILURE(status)) {
961 		acpi_dev_free_resource_list(list);
962 		return c.error ? c.error : -EIO;
963 	}
964 
965 	return c.count;
966 }
967 
968 /**
969  * acpi_dev_get_resources - Get current resources of a device.
970  * @adev: ACPI device node to get the resources for.
971  * @list: Head of the resultant list of resources (must be empty).
972  * @preproc: The caller's preprocessing routine.
973  * @preproc_data: Pointer passed to the caller's preprocessing routine.
974  *
975  * Evaluate the _CRS method for the given device node and process its output by
976  * (1) executing the @preproc() routine provided by the caller, passing the
977  * resource pointer and @preproc_data to it as arguments, for each ACPI resource
978  * returned and (2) converting all of the returned ACPI resources into struct
979  * resource objects if possible.  If the return value of @preproc() in step (1)
980  * is different from 0, step (2) is not applied to the given ACPI resource and
981  * if that value is negative, the whole processing is aborted and that value is
982  * returned as the final error code.
983  *
984  * The resultant struct resource objects are put on the list pointed to by
985  * @list, that must be empty initially, as members of struct resource_entry
986  * objects.  Callers of this routine should use %acpi_dev_free_resource_list() to
987  * free that list.
988  *
989  * The number of resources in the output list is returned on success, an error
990  * code reflecting the error condition is returned otherwise.
991  */
992 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
993 			   int (*preproc)(struct acpi_resource *, void *),
994 			   void *preproc_data)
995 {
996 	return __acpi_dev_get_resources(adev, list, preproc, preproc_data,
997 					METHOD_NAME__CRS);
998 }
999 EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
1000 
1001 static int is_memory(struct acpi_resource *ares, void *not_used)
1002 {
1003 	struct resource_win win;
1004 	struct resource *res = &win.res;
1005 
1006 	memset(&win, 0, sizeof(win));
1007 
1008 	if (acpi_dev_filter_resource_type(ares, IORESOURCE_MEM))
1009 		return 1;
1010 
1011 	return !(acpi_dev_resource_memory(ares, res)
1012 	       || acpi_dev_resource_address_space(ares, &win)
1013 	       || acpi_dev_resource_ext_address_space(ares, &win));
1014 }
1015 
1016 /**
1017  * acpi_dev_get_dma_resources - Get current DMA resources of a device.
1018  * @adev: ACPI device node to get the resources for.
1019  * @list: Head of the resultant list of resources (must be empty).
1020  *
1021  * Evaluate the _DMA method for the given device node and process its
1022  * output.
1023  *
1024  * The resultant struct resource objects are put on the list pointed to
1025  * by @list, that must be empty initially, as members of struct
1026  * resource_entry objects.  Callers of this routine should use
1027  * %acpi_dev_free_resource_list() to free that list.
1028  *
1029  * The number of resources in the output list is returned on success,
1030  * an error code reflecting the error condition is returned otherwise.
1031  */
1032 int acpi_dev_get_dma_resources(struct acpi_device *adev, struct list_head *list)
1033 {
1034 	return __acpi_dev_get_resources(adev, list, is_memory, NULL,
1035 					METHOD_NAME__DMA);
1036 }
1037 EXPORT_SYMBOL_GPL(acpi_dev_get_dma_resources);
1038 
1039 /**
1040  * acpi_dev_get_memory_resources - Get current memory resources of a device.
1041  * @adev: ACPI device node to get the resources for.
1042  * @list: Head of the resultant list of resources (must be empty).
1043  *
1044  * This is a helper function that locates all memory type resources of @adev
1045  * with acpi_dev_get_resources().
1046  *
1047  * The number of resources in the output list is returned on success, an error
1048  * code reflecting the error condition is returned otherwise.
1049  */
1050 int acpi_dev_get_memory_resources(struct acpi_device *adev, struct list_head *list)
1051 {
1052 	return acpi_dev_get_resources(adev, list, is_memory, NULL);
1053 }
1054 EXPORT_SYMBOL_GPL(acpi_dev_get_memory_resources);
1055 
1056 /**
1057  * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
1058  *				   types
1059  * @ares: Input ACPI resource object.
1060  * @types: Valid resource types of IORESOURCE_XXX
1061  *
1062  * This is a helper function to support acpi_dev_get_resources(), which filters
1063  * ACPI resource objects according to resource types.
1064  */
1065 int acpi_dev_filter_resource_type(struct acpi_resource *ares,
1066 				  unsigned long types)
1067 {
1068 	unsigned long type = 0;
1069 
1070 	switch (ares->type) {
1071 	case ACPI_RESOURCE_TYPE_MEMORY24:
1072 	case ACPI_RESOURCE_TYPE_MEMORY32:
1073 	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1074 		type = IORESOURCE_MEM;
1075 		break;
1076 	case ACPI_RESOURCE_TYPE_IO:
1077 	case ACPI_RESOURCE_TYPE_FIXED_IO:
1078 		type = IORESOURCE_IO;
1079 		break;
1080 	case ACPI_RESOURCE_TYPE_IRQ:
1081 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1082 		type = IORESOURCE_IRQ;
1083 		break;
1084 	case ACPI_RESOURCE_TYPE_DMA:
1085 	case ACPI_RESOURCE_TYPE_FIXED_DMA:
1086 		type = IORESOURCE_DMA;
1087 		break;
1088 	case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1089 		type = IORESOURCE_REG;
1090 		break;
1091 	case ACPI_RESOURCE_TYPE_ADDRESS16:
1092 	case ACPI_RESOURCE_TYPE_ADDRESS32:
1093 	case ACPI_RESOURCE_TYPE_ADDRESS64:
1094 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1095 		if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
1096 			type = IORESOURCE_MEM;
1097 		else if (ares->data.address.resource_type == ACPI_IO_RANGE)
1098 			type = IORESOURCE_IO;
1099 		else if (ares->data.address.resource_type ==
1100 			 ACPI_BUS_NUMBER_RANGE)
1101 			type = IORESOURCE_BUS;
1102 		break;
1103 	default:
1104 		break;
1105 	}
1106 
1107 	return (type & types) ? 0 : 1;
1108 }
1109 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);
1110 
1111 static int acpi_dev_consumes_res(struct acpi_device *adev, struct resource *res)
1112 {
1113 	struct list_head resource_list;
1114 	struct resource_entry *rentry;
1115 	int ret, found = 0;
1116 
1117 	INIT_LIST_HEAD(&resource_list);
1118 	ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
1119 	if (ret < 0)
1120 		return 0;
1121 
1122 	list_for_each_entry(rentry, &resource_list, node) {
1123 		if (resource_contains(rentry->res, res)) {
1124 			found = 1;
1125 			break;
1126 		}
1127 
1128 	}
1129 
1130 	acpi_dev_free_resource_list(&resource_list);
1131 	return found;
1132 }
1133 
1134 static acpi_status acpi_res_consumer_cb(acpi_handle handle, u32 depth,
1135 					 void *context, void **ret)
1136 {
1137 	struct resource *res = context;
1138 	struct acpi_device **consumer = (struct acpi_device **) ret;
1139 	struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
1140 
1141 	if (!adev)
1142 		return AE_OK;
1143 
1144 	if (acpi_dev_consumes_res(adev, res)) {
1145 		*consumer = adev;
1146 		return AE_CTRL_TERMINATE;
1147 	}
1148 
1149 	return AE_OK;
1150 }
1151 
1152 /**
1153  * acpi_resource_consumer - Find the ACPI device that consumes @res.
1154  * @res: Resource to search for.
1155  *
1156  * Search the current resource settings (_CRS) of every ACPI device node
1157  * for @res.  If we find an ACPI device whose _CRS includes @res, return
1158  * it.  Otherwise, return NULL.
1159  */
1160 struct acpi_device *acpi_resource_consumer(struct resource *res)
1161 {
1162 	struct acpi_device *consumer = NULL;
1163 
1164 	acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer);
1165 	return consumer;
1166 }
1167