resource.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 <[email protected]>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/device.h>
  14. #include <linux/export.h>
  15. #include <linux/ioport.h>
  16. #include <linux/slab.h>
  17. #include <linux/irq.h>
  18. #include <linux/dmi.h>
  19. #ifdef CONFIG_X86
  20. #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
  21. static inline bool acpi_iospace_resource_valid(struct resource *res)
  22. {
  23. /* On X86 IO space is limited to the [0 - 64K] IO port range */
  24. return res->end < 0x10003;
  25. }
  26. #else
  27. #define valid_IRQ(i) (true)
  28. /*
  29. * ACPI IO descriptors on arches other than X86 contain MMIO CPU physical
  30. * addresses mapping IO space in CPU physical address space, IO space
  31. * resources can be placed anywhere in the 64-bit physical address space.
  32. */
  33. static inline bool
  34. acpi_iospace_resource_valid(struct resource *res) { return true; }
  35. #endif
  36. #if IS_ENABLED(CONFIG_ACPI_GENERIC_GSI)
  37. static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
  38. {
  39. return ext_irq->resource_source.string_length == 0 &&
  40. ext_irq->producer_consumer == ACPI_CONSUMER;
  41. }
  42. #else
  43. static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
  44. {
  45. return true;
  46. }
  47. #endif
  48. static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
  49. {
  50. u64 reslen = end - start + 1;
  51. /*
  52. * CHECKME: len might be required to check versus a minimum
  53. * length as well. 1 for io is fine, but for memory it does
  54. * not make any sense at all.
  55. * Note: some BIOSes report incorrect length for ACPI address space
  56. * descriptor, so remove check of 'reslen == len' to avoid regression.
  57. */
  58. if (len && reslen && start <= end)
  59. return true;
  60. pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
  61. io ? "io" : "mem", start, end, len);
  62. return false;
  63. }
  64. static void acpi_dev_memresource_flags(struct resource *res, u64 len,
  65. u8 write_protect)
  66. {
  67. res->flags = IORESOURCE_MEM;
  68. if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
  69. res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
  70. if (write_protect == ACPI_READ_WRITE_MEMORY)
  71. res->flags |= IORESOURCE_MEM_WRITEABLE;
  72. }
  73. static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
  74. u8 write_protect)
  75. {
  76. res->start = start;
  77. res->end = start + len - 1;
  78. acpi_dev_memresource_flags(res, len, write_protect);
  79. }
  80. /**
  81. * acpi_dev_resource_memory - Extract ACPI memory resource information.
  82. * @ares: Input ACPI resource object.
  83. * @res: Output generic resource object.
  84. *
  85. * Check if the given ACPI resource object represents a memory resource and
  86. * if that's the case, use the information in it to populate the generic
  87. * resource object pointed to by @res.
  88. *
  89. * Return:
  90. * 1) false with res->flags setting to zero: not the expected resource type
  91. * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
  92. * 3) true: valid assigned resource
  93. */
  94. bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
  95. {
  96. struct acpi_resource_memory24 *memory24;
  97. struct acpi_resource_memory32 *memory32;
  98. struct acpi_resource_fixed_memory32 *fixed_memory32;
  99. switch (ares->type) {
  100. case ACPI_RESOURCE_TYPE_MEMORY24:
  101. memory24 = &ares->data.memory24;
  102. acpi_dev_get_memresource(res, memory24->minimum << 8,
  103. memory24->address_length << 8,
  104. memory24->write_protect);
  105. break;
  106. case ACPI_RESOURCE_TYPE_MEMORY32:
  107. memory32 = &ares->data.memory32;
  108. acpi_dev_get_memresource(res, memory32->minimum,
  109. memory32->address_length,
  110. memory32->write_protect);
  111. break;
  112. case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
  113. fixed_memory32 = &ares->data.fixed_memory32;
  114. acpi_dev_get_memresource(res, fixed_memory32->address,
  115. fixed_memory32->address_length,
  116. fixed_memory32->write_protect);
  117. break;
  118. default:
  119. res->flags = 0;
  120. return false;
  121. }
  122. return !(res->flags & IORESOURCE_DISABLED);
  123. }
  124. EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
  125. static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
  126. u8 io_decode, u8 translation_type)
  127. {
  128. res->flags = IORESOURCE_IO;
  129. if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
  130. res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
  131. if (!acpi_iospace_resource_valid(res))
  132. res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
  133. if (io_decode == ACPI_DECODE_16)
  134. res->flags |= IORESOURCE_IO_16BIT_ADDR;
  135. if (translation_type == ACPI_SPARSE_TRANSLATION)
  136. res->flags |= IORESOURCE_IO_SPARSE;
  137. }
  138. static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
  139. u8 io_decode)
  140. {
  141. res->start = start;
  142. res->end = start + len - 1;
  143. acpi_dev_ioresource_flags(res, len, io_decode, 0);
  144. }
  145. /**
  146. * acpi_dev_resource_io - Extract ACPI I/O resource information.
  147. * @ares: Input ACPI resource object.
  148. * @res: Output generic resource object.
  149. *
  150. * Check if the given ACPI resource object represents an I/O resource and
  151. * if that's the case, use the information in it to populate the generic
  152. * resource object pointed to by @res.
  153. *
  154. * Return:
  155. * 1) false with res->flags setting to zero: not the expected resource type
  156. * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
  157. * 3) true: valid assigned resource
  158. */
  159. bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
  160. {
  161. struct acpi_resource_io *io;
  162. struct acpi_resource_fixed_io *fixed_io;
  163. switch (ares->type) {
  164. case ACPI_RESOURCE_TYPE_IO:
  165. io = &ares->data.io;
  166. acpi_dev_get_ioresource(res, io->minimum,
  167. io->address_length,
  168. io->io_decode);
  169. break;
  170. case ACPI_RESOURCE_TYPE_FIXED_IO:
  171. fixed_io = &ares->data.fixed_io;
  172. acpi_dev_get_ioresource(res, fixed_io->address,
  173. fixed_io->address_length,
  174. ACPI_DECODE_10);
  175. break;
  176. default:
  177. res->flags = 0;
  178. return false;
  179. }
  180. return !(res->flags & IORESOURCE_DISABLED);
  181. }
  182. EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
  183. static bool acpi_decode_space(struct resource_win *win,
  184. struct acpi_resource_address *addr,
  185. struct acpi_address64_attribute *attr)
  186. {
  187. u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
  188. bool wp = addr->info.mem.write_protect;
  189. u64 len = attr->address_length;
  190. u64 start, end, offset = 0;
  191. struct resource *res = &win->res;
  192. /*
  193. * Filter out invalid descriptor according to ACPI Spec 5.0, section
  194. * 6.4.3.5 Address Space Resource Descriptors.
  195. */
  196. if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
  197. (addr->min_address_fixed && addr->max_address_fixed && !len))
  198. pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
  199. addr->min_address_fixed, addr->max_address_fixed, len);
  200. /*
  201. * For bridges that translate addresses across the bridge,
  202. * translation_offset is the offset that must be added to the
  203. * address on the secondary side to obtain the address on the
  204. * primary side. Non-bridge devices must list 0 for all Address
  205. * Translation offset bits.
  206. */
  207. if (addr->producer_consumer == ACPI_PRODUCER)
  208. offset = attr->translation_offset;
  209. else if (attr->translation_offset)
  210. pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
  211. attr->translation_offset);
  212. start = attr->minimum + offset;
  213. end = attr->maximum + offset;
  214. win->offset = offset;
  215. res->start = start;
  216. res->end = end;
  217. if (sizeof(resource_size_t) < sizeof(u64) &&
  218. (offset != win->offset || start != res->start || end != res->end)) {
  219. pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
  220. attr->minimum, attr->maximum);
  221. return false;
  222. }
  223. switch (addr->resource_type) {
  224. case ACPI_MEMORY_RANGE:
  225. acpi_dev_memresource_flags(res, len, wp);
  226. break;
  227. case ACPI_IO_RANGE:
  228. acpi_dev_ioresource_flags(res, len, iodec,
  229. addr->info.io.translation_type);
  230. break;
  231. case ACPI_BUS_NUMBER_RANGE:
  232. res->flags = IORESOURCE_BUS;
  233. break;
  234. default:
  235. return false;
  236. }
  237. if (addr->producer_consumer == ACPI_PRODUCER)
  238. res->flags |= IORESOURCE_WINDOW;
  239. if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  240. res->flags |= IORESOURCE_PREFETCH;
  241. return !(res->flags & IORESOURCE_DISABLED);
  242. }
  243. /**
  244. * acpi_dev_resource_address_space - Extract ACPI address space information.
  245. * @ares: Input ACPI resource object.
  246. * @win: Output generic resource object.
  247. *
  248. * Check if the given ACPI resource object represents an address space resource
  249. * and if that's the case, use the information in it to populate the generic
  250. * resource object pointed to by @win.
  251. *
  252. * Return:
  253. * 1) false with win->res.flags setting to zero: not the expected resource type
  254. * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
  255. * resource
  256. * 3) true: valid assigned resource
  257. */
  258. bool acpi_dev_resource_address_space(struct acpi_resource *ares,
  259. struct resource_win *win)
  260. {
  261. struct acpi_resource_address64 addr;
  262. win->res.flags = 0;
  263. if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
  264. return false;
  265. return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
  266. &addr.address);
  267. }
  268. EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
  269. /**
  270. * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
  271. * @ares: Input ACPI resource object.
  272. * @win: Output generic resource object.
  273. *
  274. * Check if the given ACPI resource object represents an extended address space
  275. * resource and if that's the case, use the information in it to populate the
  276. * generic resource object pointed to by @win.
  277. *
  278. * Return:
  279. * 1) false with win->res.flags setting to zero: not the expected resource type
  280. * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
  281. * resource
  282. * 3) true: valid assigned resource
  283. */
  284. bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
  285. struct resource_win *win)
  286. {
  287. struct acpi_resource_extended_address64 *ext_addr;
  288. win->res.flags = 0;
  289. if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
  290. return false;
  291. ext_addr = &ares->data.ext_address64;
  292. return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
  293. &ext_addr->address);
  294. }
  295. EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
  296. /**
  297. * acpi_dev_irq_flags - Determine IRQ resource flags.
  298. * @triggering: Triggering type as provided by ACPI.
  299. * @polarity: Interrupt polarity as provided by ACPI.
  300. * @shareable: Whether or not the interrupt is shareable.
  301. * @wake_capable: Wake capability as provided by ACPI.
  302. */
  303. unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable, u8 wake_capable)
  304. {
  305. unsigned long flags;
  306. if (triggering == ACPI_LEVEL_SENSITIVE)
  307. flags = polarity == ACPI_ACTIVE_LOW ?
  308. IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
  309. else
  310. flags = polarity == ACPI_ACTIVE_LOW ?
  311. IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
  312. if (shareable == ACPI_SHARED)
  313. flags |= IORESOURCE_IRQ_SHAREABLE;
  314. if (wake_capable == ACPI_WAKE_CAPABLE)
  315. flags |= IORESOURCE_IRQ_WAKECAPABLE;
  316. return flags | IORESOURCE_IRQ;
  317. }
  318. EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
  319. /**
  320. * acpi_dev_get_irq_type - Determine irq type.
  321. * @triggering: Triggering type as provided by ACPI.
  322. * @polarity: Interrupt polarity as provided by ACPI.
  323. */
  324. unsigned int acpi_dev_get_irq_type(int triggering, int polarity)
  325. {
  326. switch (polarity) {
  327. case ACPI_ACTIVE_LOW:
  328. return triggering == ACPI_EDGE_SENSITIVE ?
  329. IRQ_TYPE_EDGE_FALLING :
  330. IRQ_TYPE_LEVEL_LOW;
  331. case ACPI_ACTIVE_HIGH:
  332. return triggering == ACPI_EDGE_SENSITIVE ?
  333. IRQ_TYPE_EDGE_RISING :
  334. IRQ_TYPE_LEVEL_HIGH;
  335. case ACPI_ACTIVE_BOTH:
  336. if (triggering == ACPI_EDGE_SENSITIVE)
  337. return IRQ_TYPE_EDGE_BOTH;
  338. fallthrough;
  339. default:
  340. return IRQ_TYPE_NONE;
  341. }
  342. }
  343. EXPORT_SYMBOL_GPL(acpi_dev_get_irq_type);
  344. static const struct dmi_system_id medion_laptop[] = {
  345. {
  346. .ident = "MEDION P15651",
  347. .matches = {
  348. DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
  349. DMI_MATCH(DMI_BOARD_NAME, "M15T"),
  350. },
  351. },
  352. {
  353. .ident = "MEDION S17405",
  354. .matches = {
  355. DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
  356. DMI_MATCH(DMI_BOARD_NAME, "M17T"),
  357. },
  358. },
  359. {
  360. .ident = "MEDION S17413",
  361. .matches = {
  362. DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
  363. DMI_MATCH(DMI_BOARD_NAME, "M1xA"),
  364. },
  365. },
  366. { }
  367. };
  368. static const struct dmi_system_id asus_laptop[] = {
  369. {
  370. .ident = "Asus Vivobook K3402ZA",
  371. .matches = {
  372. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  373. DMI_MATCH(DMI_BOARD_NAME, "K3402ZA"),
  374. },
  375. },
  376. {
  377. .ident = "Asus Vivobook K3502ZA",
  378. .matches = {
  379. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  380. DMI_MATCH(DMI_BOARD_NAME, "K3502ZA"),
  381. },
  382. },
  383. {
  384. .ident = "Asus Vivobook S5402ZA",
  385. .matches = {
  386. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  387. DMI_MATCH(DMI_BOARD_NAME, "S5402ZA"),
  388. },
  389. },
  390. {
  391. .ident = "Asus Vivobook S5602ZA",
  392. .matches = {
  393. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  394. DMI_MATCH(DMI_BOARD_NAME, "S5602ZA"),
  395. },
  396. },
  397. {
  398. .ident = "Asus ExpertBook B1402CBA",
  399. .matches = {
  400. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  401. DMI_MATCH(DMI_BOARD_NAME, "B1402CBA"),
  402. },
  403. },
  404. {
  405. /* Asus ExpertBook B1402CVA */
  406. .matches = {
  407. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  408. DMI_MATCH(DMI_BOARD_NAME, "B1402CVA"),
  409. },
  410. },
  411. {
  412. .ident = "Asus ExpertBook B2402CBA",
  413. .matches = {
  414. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  415. DMI_MATCH(DMI_BOARD_NAME, "B2402CBA"),
  416. },
  417. },
  418. {
  419. .ident = "Asus ExpertBook B2502",
  420. .matches = {
  421. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  422. DMI_MATCH(DMI_BOARD_NAME, "B2502CBA"),
  423. },
  424. },
  425. { }
  426. };
  427. static const struct dmi_system_id lenovo_laptop[] = {
  428. {
  429. .ident = "LENOVO IdeaPad Flex 5 14ALC7",
  430. .matches = {
  431. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  432. DMI_MATCH(DMI_PRODUCT_NAME, "82R9"),
  433. },
  434. },
  435. {
  436. .ident = "LENOVO IdeaPad Flex 5 16ALC7",
  437. .matches = {
  438. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  439. DMI_MATCH(DMI_PRODUCT_NAME, "82RA"),
  440. },
  441. },
  442. { }
  443. };
  444. static const struct dmi_system_id tongfang_gm_rg[] = {
  445. {
  446. .ident = "TongFang GMxRGxx/XMG CORE 15 (M22)/TUXEDO Stellaris 15 Gen4 AMD",
  447. .matches = {
  448. DMI_MATCH(DMI_BOARD_NAME, "GMxRGxx"),
  449. },
  450. },
  451. { }
  452. };
  453. static const struct dmi_system_id maingear_laptop[] = {
  454. {
  455. .ident = "MAINGEAR Vector Pro 2 15",
  456. .matches = {
  457. DMI_MATCH(DMI_SYS_VENDOR, "Micro Electronics Inc"),
  458. DMI_MATCH(DMI_PRODUCT_NAME, "MG-VCP2-15A3070T"),
  459. }
  460. },
  461. {
  462. /* TongFang GMxXGxx/TUXEDO Polaris 15 Gen5 AMD */
  463. .matches = {
  464. DMI_MATCH(DMI_BOARD_NAME, "GMxXGxx"),
  465. },
  466. },
  467. {
  468. /* TongFang GM6XGxX/TUXEDO Stellaris 16 Gen5 AMD */
  469. .matches = {
  470. DMI_MATCH(DMI_BOARD_NAME, "GM6XGxX"),
  471. },
  472. },
  473. {
  474. .ident = "MAINGEAR Vector Pro 2 17",
  475. .matches = {
  476. DMI_MATCH(DMI_SYS_VENDOR, "Micro Electronics Inc"),
  477. DMI_MATCH(DMI_PRODUCT_NAME, "MG-VCP2-17A3070T"),
  478. },
  479. },
  480. { }
  481. };
  482. static const struct dmi_system_id lg_laptop[] = {
  483. {
  484. .ident = "LG Electronics 17U70P",
  485. .matches = {
  486. DMI_MATCH(DMI_SYS_VENDOR, "LG Electronics"),
  487. DMI_MATCH(DMI_BOARD_NAME, "17U70P"),
  488. },
  489. },
  490. { }
  491. };
  492. struct irq_override_cmp {
  493. const struct dmi_system_id *system;
  494. unsigned char irq;
  495. unsigned char triggering;
  496. unsigned char polarity;
  497. unsigned char shareable;
  498. bool override;
  499. };
  500. static const struct irq_override_cmp override_table[] = {
  501. { medion_laptop, 1, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 0, false },
  502. { asus_laptop, 1, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 0, false },
  503. { lenovo_laptop, 6, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 0, true },
  504. { lenovo_laptop, 10, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 0, true },
  505. { tongfang_gm_rg, 1, ACPI_EDGE_SENSITIVE, ACPI_ACTIVE_LOW, 1, true },
  506. { maingear_laptop, 1, ACPI_EDGE_SENSITIVE, ACPI_ACTIVE_LOW, 1, true },
  507. { lg_laptop, 1, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 0, false },
  508. };
  509. static bool acpi_dev_irq_override(u32 gsi, u8 triggering, u8 polarity,
  510. u8 shareable)
  511. {
  512. int i;
  513. for (i = 0; i < ARRAY_SIZE(override_table); i++) {
  514. const struct irq_override_cmp *entry = &override_table[i];
  515. if (dmi_check_system(entry->system) &&
  516. entry->irq == gsi &&
  517. entry->triggering == triggering &&
  518. entry->polarity == polarity &&
  519. entry->shareable == shareable)
  520. return entry->override;
  521. }
  522. #ifdef CONFIG_X86
  523. /*
  524. * IRQ override isn't needed on modern AMD Zen systems and
  525. * this override breaks active low IRQs on AMD Ryzen 6000 and
  526. * newer systems. Skip it.
  527. */
  528. if (boot_cpu_has(X86_FEATURE_ZEN))
  529. return false;
  530. #endif
  531. return true;
  532. }
  533. static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
  534. u8 triggering, u8 polarity, u8 shareable,
  535. u8 wake_capable, bool check_override)
  536. {
  537. int irq, p, t;
  538. if (!valid_IRQ(gsi)) {
  539. irqresource_disabled(res, gsi);
  540. return;
  541. }
  542. /*
  543. * In IO-APIC mode, use overridden attribute. Two reasons:
  544. * 1. BIOS bug in DSDT
  545. * 2. BIOS uses IO-APIC mode Interrupt Source Override
  546. *
  547. * We do this only if we are dealing with IRQ() or IRQNoFlags()
  548. * resource (the legacy ISA resources). With modern ACPI 5 devices
  549. * using extended IRQ descriptors we take the IRQ configuration
  550. * from _CRS directly.
  551. */
  552. if (check_override &&
  553. acpi_dev_irq_override(gsi, triggering, polarity, shareable) &&
  554. !acpi_get_override_irq(gsi, &t, &p)) {
  555. u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
  556. u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
  557. if (triggering != trig || polarity != pol) {
  558. pr_warn("ACPI: IRQ %d override to %s%s, %s%s\n", gsi,
  559. t ? "level" : "edge",
  560. trig == triggering ? "" : "(!)",
  561. p ? "low" : "high",
  562. pol == polarity ? "" : "(!)");
  563. triggering = trig;
  564. polarity = pol;
  565. }
  566. }
  567. res->flags = acpi_dev_irq_flags(triggering, polarity, shareable, wake_capable);
  568. irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
  569. if (irq >= 0) {
  570. res->start = irq;
  571. res->end = irq;
  572. } else {
  573. irqresource_disabled(res, gsi);
  574. }
  575. }
  576. /**
  577. * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
  578. * @ares: Input ACPI resource object.
  579. * @index: Index into the array of GSIs represented by the resource.
  580. * @res: Output generic resource object.
  581. *
  582. * Check if the given ACPI resource object represents an interrupt resource
  583. * and @index does not exceed the resource's interrupt count (true is returned
  584. * in that case regardless of the results of the other checks)). If that's the
  585. * case, register the GSI corresponding to @index from the array of interrupts
  586. * represented by the resource and populate the generic resource object pointed
  587. * to by @res accordingly. If the registration of the GSI is not successful,
  588. * IORESOURCE_DISABLED will be set it that object's flags.
  589. *
  590. * Return:
  591. * 1) false with res->flags setting to zero: not the expected resource type
  592. * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
  593. * 3) true: valid assigned resource
  594. */
  595. bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
  596. struct resource *res)
  597. {
  598. struct acpi_resource_irq *irq;
  599. struct acpi_resource_extended_irq *ext_irq;
  600. switch (ares->type) {
  601. case ACPI_RESOURCE_TYPE_IRQ:
  602. /*
  603. * Per spec, only one interrupt per descriptor is allowed in
  604. * _CRS, but some firmware violates this, so parse them all.
  605. */
  606. irq = &ares->data.irq;
  607. if (index >= irq->interrupt_count) {
  608. irqresource_disabled(res, 0);
  609. return false;
  610. }
  611. acpi_dev_get_irqresource(res, irq->interrupts[index],
  612. irq->triggering, irq->polarity,
  613. irq->shareable, irq->wake_capable,
  614. true);
  615. break;
  616. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  617. ext_irq = &ares->data.extended_irq;
  618. if (index >= ext_irq->interrupt_count) {
  619. irqresource_disabled(res, 0);
  620. return false;
  621. }
  622. if (is_gsi(ext_irq))
  623. acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
  624. ext_irq->triggering, ext_irq->polarity,
  625. ext_irq->shareable, ext_irq->wake_capable,
  626. false);
  627. else
  628. irqresource_disabled(res, 0);
  629. break;
  630. default:
  631. res->flags = 0;
  632. return false;
  633. }
  634. return true;
  635. }
  636. EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
  637. /**
  638. * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
  639. * @list: The head of the resource list to free.
  640. */
  641. void acpi_dev_free_resource_list(struct list_head *list)
  642. {
  643. resource_list_free(list);
  644. }
  645. EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
  646. struct res_proc_context {
  647. struct list_head *list;
  648. int (*preproc)(struct acpi_resource *, void *);
  649. void *preproc_data;
  650. int count;
  651. int error;
  652. };
  653. static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
  654. struct res_proc_context *c)
  655. {
  656. struct resource_entry *rentry;
  657. rentry = resource_list_create_entry(NULL, 0);
  658. if (!rentry) {
  659. c->error = -ENOMEM;
  660. return AE_NO_MEMORY;
  661. }
  662. *rentry->res = win->res;
  663. rentry->offset = win->offset;
  664. resource_list_add_tail(rentry, c->list);
  665. c->count++;
  666. return AE_OK;
  667. }
  668. static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
  669. void *context)
  670. {
  671. struct res_proc_context *c = context;
  672. struct resource_win win;
  673. struct resource *res = &win.res;
  674. int i;
  675. if (c->preproc) {
  676. int ret;
  677. ret = c->preproc(ares, c->preproc_data);
  678. if (ret < 0) {
  679. c->error = ret;
  680. return AE_ABORT_METHOD;
  681. } else if (ret > 0) {
  682. return AE_OK;
  683. }
  684. }
  685. memset(&win, 0, sizeof(win));
  686. if (acpi_dev_resource_memory(ares, res)
  687. || acpi_dev_resource_io(ares, res)
  688. || acpi_dev_resource_address_space(ares, &win)
  689. || acpi_dev_resource_ext_address_space(ares, &win))
  690. return acpi_dev_new_resource_entry(&win, c);
  691. for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
  692. acpi_status status;
  693. status = acpi_dev_new_resource_entry(&win, c);
  694. if (ACPI_FAILURE(status))
  695. return status;
  696. }
  697. return AE_OK;
  698. }
  699. static int __acpi_dev_get_resources(struct acpi_device *adev,
  700. struct list_head *list,
  701. int (*preproc)(struct acpi_resource *, void *),
  702. void *preproc_data, char *method)
  703. {
  704. struct res_proc_context c;
  705. acpi_status status;
  706. if (!adev || !adev->handle || !list_empty(list))
  707. return -EINVAL;
  708. if (!acpi_has_method(adev->handle, method))
  709. return 0;
  710. c.list = list;
  711. c.preproc = preproc;
  712. c.preproc_data = preproc_data;
  713. c.count = 0;
  714. c.error = 0;
  715. status = acpi_walk_resources(adev->handle, method,
  716. acpi_dev_process_resource, &c);
  717. if (ACPI_FAILURE(status)) {
  718. acpi_dev_free_resource_list(list);
  719. return c.error ? c.error : -EIO;
  720. }
  721. return c.count;
  722. }
  723. /**
  724. * acpi_dev_get_resources - Get current resources of a device.
  725. * @adev: ACPI device node to get the resources for.
  726. * @list: Head of the resultant list of resources (must be empty).
  727. * @preproc: The caller's preprocessing routine.
  728. * @preproc_data: Pointer passed to the caller's preprocessing routine.
  729. *
  730. * Evaluate the _CRS method for the given device node and process its output by
  731. * (1) executing the @preproc() routine provided by the caller, passing the
  732. * resource pointer and @preproc_data to it as arguments, for each ACPI resource
  733. * returned and (2) converting all of the returned ACPI resources into struct
  734. * resource objects if possible. If the return value of @preproc() in step (1)
  735. * is different from 0, step (2) is not applied to the given ACPI resource and
  736. * if that value is negative, the whole processing is aborted and that value is
  737. * returned as the final error code.
  738. *
  739. * The resultant struct resource objects are put on the list pointed to by
  740. * @list, that must be empty initially, as members of struct resource_entry
  741. * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
  742. * free that list.
  743. *
  744. * The number of resources in the output list is returned on success, an error
  745. * code reflecting the error condition is returned otherwise.
  746. */
  747. int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
  748. int (*preproc)(struct acpi_resource *, void *),
  749. void *preproc_data)
  750. {
  751. return __acpi_dev_get_resources(adev, list, preproc, preproc_data,
  752. METHOD_NAME__CRS);
  753. }
  754. EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
  755. static int is_memory(struct acpi_resource *ares, void *not_used)
  756. {
  757. struct resource_win win;
  758. struct resource *res = &win.res;
  759. memset(&win, 0, sizeof(win));
  760. if (acpi_dev_filter_resource_type(ares, IORESOURCE_MEM))
  761. return 1;
  762. return !(acpi_dev_resource_memory(ares, res)
  763. || acpi_dev_resource_address_space(ares, &win)
  764. || acpi_dev_resource_ext_address_space(ares, &win));
  765. }
  766. /**
  767. * acpi_dev_get_dma_resources - Get current DMA resources of a device.
  768. * @adev: ACPI device node to get the resources for.
  769. * @list: Head of the resultant list of resources (must be empty).
  770. *
  771. * Evaluate the _DMA method for the given device node and process its
  772. * output.
  773. *
  774. * The resultant struct resource objects are put on the list pointed to
  775. * by @list, that must be empty initially, as members of struct
  776. * resource_entry objects. Callers of this routine should use
  777. * %acpi_dev_free_resource_list() to free that list.
  778. *
  779. * The number of resources in the output list is returned on success,
  780. * an error code reflecting the error condition is returned otherwise.
  781. */
  782. int acpi_dev_get_dma_resources(struct acpi_device *adev, struct list_head *list)
  783. {
  784. return __acpi_dev_get_resources(adev, list, is_memory, NULL,
  785. METHOD_NAME__DMA);
  786. }
  787. EXPORT_SYMBOL_GPL(acpi_dev_get_dma_resources);
  788. /**
  789. * acpi_dev_get_memory_resources - Get current memory resources of a device.
  790. * @adev: ACPI device node to get the resources for.
  791. * @list: Head of the resultant list of resources (must be empty).
  792. *
  793. * This is a helper function that locates all memory type resources of @adev
  794. * with acpi_dev_get_resources().
  795. *
  796. * The number of resources in the output list is returned on success, an error
  797. * code reflecting the error condition is returned otherwise.
  798. */
  799. int acpi_dev_get_memory_resources(struct acpi_device *adev, struct list_head *list)
  800. {
  801. return acpi_dev_get_resources(adev, list, is_memory, NULL);
  802. }
  803. EXPORT_SYMBOL_GPL(acpi_dev_get_memory_resources);
  804. /**
  805. * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
  806. * types
  807. * @ares: Input ACPI resource object.
  808. * @types: Valid resource types of IORESOURCE_XXX
  809. *
  810. * This is a helper function to support acpi_dev_get_resources(), which filters
  811. * ACPI resource objects according to resource types.
  812. */
  813. int acpi_dev_filter_resource_type(struct acpi_resource *ares,
  814. unsigned long types)
  815. {
  816. unsigned long type = 0;
  817. switch (ares->type) {
  818. case ACPI_RESOURCE_TYPE_MEMORY24:
  819. case ACPI_RESOURCE_TYPE_MEMORY32:
  820. case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
  821. type = IORESOURCE_MEM;
  822. break;
  823. case ACPI_RESOURCE_TYPE_IO:
  824. case ACPI_RESOURCE_TYPE_FIXED_IO:
  825. type = IORESOURCE_IO;
  826. break;
  827. case ACPI_RESOURCE_TYPE_IRQ:
  828. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  829. type = IORESOURCE_IRQ;
  830. break;
  831. case ACPI_RESOURCE_TYPE_DMA:
  832. case ACPI_RESOURCE_TYPE_FIXED_DMA:
  833. type = IORESOURCE_DMA;
  834. break;
  835. case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
  836. type = IORESOURCE_REG;
  837. break;
  838. case ACPI_RESOURCE_TYPE_ADDRESS16:
  839. case ACPI_RESOURCE_TYPE_ADDRESS32:
  840. case ACPI_RESOURCE_TYPE_ADDRESS64:
  841. case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
  842. if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
  843. type = IORESOURCE_MEM;
  844. else if (ares->data.address.resource_type == ACPI_IO_RANGE)
  845. type = IORESOURCE_IO;
  846. else if (ares->data.address.resource_type ==
  847. ACPI_BUS_NUMBER_RANGE)
  848. type = IORESOURCE_BUS;
  849. break;
  850. default:
  851. break;
  852. }
  853. return (type & types) ? 0 : 1;
  854. }
  855. EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);
  856. static int acpi_dev_consumes_res(struct acpi_device *adev, struct resource *res)
  857. {
  858. struct list_head resource_list;
  859. struct resource_entry *rentry;
  860. int ret, found = 0;
  861. INIT_LIST_HEAD(&resource_list);
  862. ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  863. if (ret < 0)
  864. return 0;
  865. list_for_each_entry(rentry, &resource_list, node) {
  866. if (resource_contains(rentry->res, res)) {
  867. found = 1;
  868. break;
  869. }
  870. }
  871. acpi_dev_free_resource_list(&resource_list);
  872. return found;
  873. }
  874. static acpi_status acpi_res_consumer_cb(acpi_handle handle, u32 depth,
  875. void *context, void **ret)
  876. {
  877. struct resource *res = context;
  878. struct acpi_device **consumer = (struct acpi_device **) ret;
  879. struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
  880. if (!adev)
  881. return AE_OK;
  882. if (acpi_dev_consumes_res(adev, res)) {
  883. *consumer = adev;
  884. return AE_CTRL_TERMINATE;
  885. }
  886. return AE_OK;
  887. }
  888. /**
  889. * acpi_resource_consumer - Find the ACPI device that consumes @res.
  890. * @res: Resource to search for.
  891. *
  892. * Search the current resource settings (_CRS) of every ACPI device node
  893. * for @res. If we find an ACPI device whose _CRS includes @res, return
  894. * it. Otherwise, return NULL.
  895. */
  896. struct acpi_device *acpi_resource_consumer(struct resource *res)
  897. {
  898. struct acpi_device *consumer = NULL;
  899. acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer);
  900. return consumer;
  901. }