resource.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. #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. if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  227. res->flags |= IORESOURCE_PREFETCH;
  228. break;
  229. case ACPI_IO_RANGE:
  230. acpi_dev_ioresource_flags(res, len, iodec,
  231. addr->info.io.translation_type);
  232. break;
  233. case ACPI_BUS_NUMBER_RANGE:
  234. res->flags = IORESOURCE_BUS;
  235. break;
  236. default:
  237. return false;
  238. }
  239. if (addr->producer_consumer == ACPI_PRODUCER)
  240. res->flags |= IORESOURCE_WINDOW;
  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. /*
  345. * DMI matches for boards where the DSDT specifies the kbd IRQ as
  346. * level active-low and using the override changes this to rising edge,
  347. * stopping the keyboard from working.
  348. */
  349. static const struct dmi_system_id irq1_level_low_skip_override[] = {
  350. {
  351. /* MEDION P15651 */
  352. .matches = {
  353. DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
  354. DMI_MATCH(DMI_BOARD_NAME, "M15T"),
  355. },
  356. },
  357. {
  358. /* MEDION S17405 */
  359. .matches = {
  360. DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
  361. DMI_MATCH(DMI_BOARD_NAME, "M17T"),
  362. },
  363. },
  364. {
  365. /* MEDION S17413 */
  366. .matches = {
  367. DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
  368. DMI_MATCH(DMI_BOARD_NAME, "M1xA"),
  369. },
  370. },
  371. {
  372. /* Asus Vivobook K3402ZA */
  373. .matches = {
  374. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  375. DMI_MATCH(DMI_BOARD_NAME, "K3402ZA"),
  376. },
  377. },
  378. {
  379. /* Asus Vivobook K3502ZA */
  380. .matches = {
  381. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  382. DMI_MATCH(DMI_BOARD_NAME, "K3502ZA"),
  383. },
  384. },
  385. {
  386. /* Asus Vivobook S5402ZA */
  387. .matches = {
  388. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  389. DMI_MATCH(DMI_BOARD_NAME, "S5402ZA"),
  390. },
  391. },
  392. {
  393. /* Asus Vivobook S5602ZA */
  394. .matches = {
  395. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  396. DMI_MATCH(DMI_BOARD_NAME, "S5602ZA"),
  397. },
  398. },
  399. {
  400. /* Asus Vivobook X1404VAP */
  401. .matches = {
  402. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  403. DMI_MATCH(DMI_BOARD_NAME, "X1404VAP"),
  404. },
  405. },
  406. {
  407. /* Asus Vivobook X1504VAP */
  408. .matches = {
  409. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  410. DMI_MATCH(DMI_BOARD_NAME, "X1504VAP"),
  411. },
  412. },
  413. {
  414. /* Asus Vivobook X1704VAP */
  415. .matches = {
  416. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  417. DMI_MATCH(DMI_BOARD_NAME, "X1704VAP"),
  418. },
  419. },
  420. {
  421. /* Asus ExpertBook B1402C* */
  422. .matches = {
  423. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  424. DMI_MATCH(DMI_BOARD_NAME, "B1402C"),
  425. },
  426. },
  427. {
  428. /* Asus ExpertBook B1502C* */
  429. .matches = {
  430. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  431. DMI_MATCH(DMI_BOARD_NAME, "B1502C"),
  432. },
  433. },
  434. {
  435. /* Asus ExpertBook B2402 (B2402CBA / B2402FBA / B2402CVA / B2402FVA) */
  436. .matches = {
  437. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  438. DMI_MATCH(DMI_BOARD_NAME, "B2402"),
  439. },
  440. },
  441. {
  442. /* Asus ExpertBook B2502 (B2502CBA / B2502FBA / B2502CVA / B2502FVA) */
  443. .matches = {
  444. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  445. DMI_MATCH(DMI_BOARD_NAME, "B2502"),
  446. },
  447. },
  448. {
  449. /* Asus Vivobook Go E1404GA* */
  450. .matches = {
  451. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  452. DMI_MATCH(DMI_BOARD_NAME, "E1404GA"),
  453. },
  454. },
  455. {
  456. /* Asus Vivobook E1504GA* */
  457. .matches = {
  458. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  459. DMI_MATCH(DMI_BOARD_NAME, "E1504GA"),
  460. },
  461. },
  462. {
  463. /* Asus Vivobook Pro N6506M* */
  464. .matches = {
  465. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  466. DMI_MATCH(DMI_BOARD_NAME, "N6506M"),
  467. },
  468. },
  469. {
  470. /* LG Electronics 17U70P */
  471. .matches = {
  472. DMI_MATCH(DMI_SYS_VENDOR, "LG Electronics"),
  473. DMI_MATCH(DMI_BOARD_NAME, "17U70P"),
  474. },
  475. },
  476. {
  477. /* LG Electronics 16T90SP */
  478. .matches = {
  479. DMI_MATCH(DMI_SYS_VENDOR, "LG Electronics"),
  480. DMI_MATCH(DMI_BOARD_NAME, "16T90SP"),
  481. },
  482. },
  483. { }
  484. };
  485. /*
  486. * DMI matches for AMD Zen boards where the DSDT specifies the kbd IRQ
  487. * as falling edge and this must be overridden to rising edge,
  488. * to have a working keyboard.
  489. */
  490. static const struct dmi_system_id irq1_edge_low_force_override[] = {
  491. {
  492. /* MECHREVO Jiaolong17KS Series GM7XG0M */
  493. .matches = {
  494. DMI_MATCH(DMI_BOARD_NAME, "GM7XG0M"),
  495. },
  496. },
  497. {
  498. /* XMG APEX 17 (M23) */
  499. .matches = {
  500. DMI_MATCH(DMI_BOARD_NAME, "GMxBGxx"),
  501. },
  502. },
  503. {
  504. /* TongFang GMxRGxx/XMG CORE 15 (M22)/TUXEDO Stellaris 15 Gen4 AMD */
  505. .matches = {
  506. DMI_MATCH(DMI_BOARD_NAME, "GMxRGxx"),
  507. },
  508. },
  509. {
  510. /* TongFang GMxXGxx/TUXEDO Polaris 15 Gen5 AMD */
  511. .matches = {
  512. DMI_MATCH(DMI_BOARD_NAME, "GMxXGxx"),
  513. },
  514. },
  515. {
  516. /* TongFang GMxXGxX/TUXEDO Polaris 15 Gen5 AMD */
  517. .matches = {
  518. DMI_MATCH(DMI_BOARD_NAME, "GMxXGxX"),
  519. },
  520. },
  521. {
  522. /* TongFang GMxXGxx sold as Eluktronics Inc. RP-15 */
  523. .matches = {
  524. DMI_MATCH(DMI_SYS_VENDOR, "Eluktronics Inc."),
  525. DMI_MATCH(DMI_BOARD_NAME, "RP-15"),
  526. },
  527. },
  528. {
  529. .matches = {
  530. DMI_MATCH(DMI_SYS_VENDOR, "Eluktronics Inc."),
  531. DMI_MATCH(DMI_BOARD_NAME, "MECH-17"),
  532. },
  533. },
  534. {
  535. /* TongFang GM6XGxX/TUXEDO Stellaris 16 Gen5 AMD */
  536. .matches = {
  537. DMI_MATCH(DMI_BOARD_NAME, "GM6XGxX"),
  538. },
  539. },
  540. {
  541. /* MAINGEAR Vector Pro 2 15 */
  542. .matches = {
  543. DMI_MATCH(DMI_SYS_VENDOR, "Micro Electronics Inc"),
  544. DMI_MATCH(DMI_PRODUCT_NAME, "MG-VCP2-15A3070T"),
  545. }
  546. },
  547. {
  548. /* MAINGEAR Vector Pro 2 17 */
  549. .matches = {
  550. DMI_MATCH(DMI_SYS_VENDOR, "Micro Electronics Inc"),
  551. DMI_MATCH(DMI_PRODUCT_NAME, "MG-VCP2-17A3070T"),
  552. },
  553. },
  554. {
  555. /* TongFang GM6BGEQ / PCSpecialist Elimina Pro 16 M, RTX 3050 */
  556. .matches = {
  557. DMI_MATCH(DMI_BOARD_NAME, "GM6BGEQ"),
  558. },
  559. },
  560. {
  561. /* TongFang GM6BG5Q, RTX 4050 */
  562. .matches = {
  563. DMI_MATCH(DMI_BOARD_NAME, "GM6BG5Q"),
  564. },
  565. },
  566. {
  567. /* TongFang GM6BG0Q / PCSpecialist Elimina Pro 16 M, RTX 4060 */
  568. .matches = {
  569. DMI_MATCH(DMI_BOARD_NAME, "GM6BG0Q"),
  570. },
  571. },
  572. {
  573. /* Infinity E15-5A165-BM */
  574. .matches = {
  575. DMI_MATCH(DMI_BOARD_NAME, "GM5RG1E0009COM"),
  576. },
  577. },
  578. {
  579. /* Infinity E15-5A305-1M */
  580. .matches = {
  581. DMI_MATCH(DMI_BOARD_NAME, "GM5RGEE0016COM"),
  582. },
  583. },
  584. {
  585. /* Lunnen Ground 15 / AMD Ryzen 5 5500U */
  586. .matches = {
  587. DMI_MATCH(DMI_SYS_VENDOR, "Lunnen"),
  588. DMI_MATCH(DMI_BOARD_NAME, "LLL5DAW"),
  589. },
  590. },
  591. {
  592. /* Lunnen Ground 16 / AMD Ryzen 7 5800U */
  593. .matches = {
  594. DMI_MATCH(DMI_SYS_VENDOR, "Lunnen"),
  595. DMI_MATCH(DMI_BOARD_NAME, "LL6FA"),
  596. },
  597. },
  598. {
  599. /* MAIBENBEN X577 */
  600. .matches = {
  601. DMI_MATCH(DMI_SYS_VENDOR, "MAIBENBEN"),
  602. DMI_MATCH(DMI_BOARD_NAME, "X577"),
  603. },
  604. },
  605. {
  606. /* Maibenben X565 */
  607. .matches = {
  608. DMI_MATCH(DMI_SYS_VENDOR, "MAIBENBEN"),
  609. DMI_MATCH(DMI_BOARD_NAME, "X565"),
  610. },
  611. },
  612. {
  613. /* TongFang GXxHRXx/TUXEDO InfinityBook Pro Gen9 AMD */
  614. .matches = {
  615. DMI_MATCH(DMI_BOARD_NAME, "GXxHRXx"),
  616. },
  617. },
  618. {
  619. /* TongFang GMxHGxx/TUXEDO Stellaris Slim Gen1 AMD */
  620. .matches = {
  621. DMI_MATCH(DMI_BOARD_NAME, "GMxHGxx"),
  622. },
  623. },
  624. {
  625. /*
  626. * TongFang GM5HG0A in case of the SKIKK Vanaheim relabel the
  627. * board-name is changed, so check OEM strings instead. Note
  628. * OEM string matches are always exact matches.
  629. * https://bugzilla.kernel.org/show_bug.cgi?id=219614
  630. */
  631. .matches = {
  632. DMI_EXACT_MATCH(DMI_OEM_STRING, "GM5HG0A"),
  633. },
  634. },
  635. { }
  636. };
  637. struct irq_override_cmp {
  638. const struct dmi_system_id *system;
  639. unsigned char irq;
  640. unsigned char triggering;
  641. unsigned char polarity;
  642. unsigned char shareable;
  643. bool override;
  644. };
  645. static const struct irq_override_cmp override_table[] = {
  646. { irq1_level_low_skip_override, 1, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_LOW, 0, false },
  647. { irq1_edge_low_force_override, 1, ACPI_EDGE_SENSITIVE, ACPI_ACTIVE_LOW, 1, true },
  648. };
  649. static bool acpi_dev_irq_override(u32 gsi, u8 triggering, u8 polarity,
  650. u8 shareable)
  651. {
  652. int i;
  653. for (i = 0; i < ARRAY_SIZE(override_table); i++) {
  654. const struct irq_override_cmp *entry = &override_table[i];
  655. if (entry->irq == gsi &&
  656. entry->triggering == triggering &&
  657. entry->polarity == polarity &&
  658. entry->shareable == shareable &&
  659. dmi_check_system(entry->system))
  660. return entry->override;
  661. }
  662. #ifdef CONFIG_X86
  663. /*
  664. * Always use the MADT override info, except for the i8042 PS/2 ctrl
  665. * IRQs (1 and 12). For these the DSDT IRQ settings should sometimes
  666. * be used otherwise PS/2 keyboards / mice will not work.
  667. */
  668. if (gsi != 1 && gsi != 12)
  669. return true;
  670. /* If the override comes from an INT_SRC_OVR MADT entry, honor it. */
  671. if (acpi_int_src_ovr[gsi])
  672. return true;
  673. /*
  674. * IRQ override isn't needed on modern AMD Zen systems and
  675. * this override breaks active low IRQs on AMD Ryzen 6000 and
  676. * newer systems. Skip it.
  677. */
  678. if (boot_cpu_has(X86_FEATURE_ZEN))
  679. return false;
  680. #endif
  681. return true;
  682. }
  683. static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
  684. u8 triggering, u8 polarity, u8 shareable,
  685. u8 wake_capable, bool check_override)
  686. {
  687. int irq, p, t;
  688. if (!valid_IRQ(gsi)) {
  689. irqresource_disabled(res, gsi);
  690. return;
  691. }
  692. /*
  693. * In IO-APIC mode, use overridden attribute. Two reasons:
  694. * 1. BIOS bug in DSDT
  695. * 2. BIOS uses IO-APIC mode Interrupt Source Override
  696. *
  697. * We do this only if we are dealing with IRQ() or IRQNoFlags()
  698. * resource (the legacy ISA resources). With modern ACPI 5 devices
  699. * using extended IRQ descriptors we take the IRQ configuration
  700. * from _CRS directly.
  701. */
  702. if (check_override &&
  703. acpi_dev_irq_override(gsi, triggering, polarity, shareable) &&
  704. !acpi_get_override_irq(gsi, &t, &p)) {
  705. u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
  706. u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
  707. if (triggering != trig || polarity != pol) {
  708. pr_warn("ACPI: IRQ %d override to %s%s, %s%s\n", gsi,
  709. t ? "level" : "edge",
  710. trig == triggering ? "" : "(!)",
  711. p ? "low" : "high",
  712. pol == polarity ? "" : "(!)");
  713. triggering = trig;
  714. polarity = pol;
  715. }
  716. }
  717. res->flags = acpi_dev_irq_flags(triggering, polarity, shareable, wake_capable);
  718. irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
  719. if (irq >= 0) {
  720. res->start = irq;
  721. res->end = irq;
  722. } else {
  723. irqresource_disabled(res, gsi);
  724. }
  725. }
  726. /**
  727. * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
  728. * @ares: Input ACPI resource object.
  729. * @index: Index into the array of GSIs represented by the resource.
  730. * @res: Output generic resource object.
  731. *
  732. * Check if the given ACPI resource object represents an interrupt resource
  733. * and @index does not exceed the resource's interrupt count (true is returned
  734. * in that case regardless of the results of the other checks)). If that's the
  735. * case, register the GSI corresponding to @index from the array of interrupts
  736. * represented by the resource and populate the generic resource object pointed
  737. * to by @res accordingly. If the registration of the GSI is not successful,
  738. * IORESOURCE_DISABLED will be set it that object's flags.
  739. *
  740. * Return:
  741. * 1) false with res->flags setting to zero: not the expected resource type
  742. * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
  743. * 3) true: valid assigned resource
  744. */
  745. bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
  746. struct resource *res)
  747. {
  748. struct acpi_resource_irq *irq;
  749. struct acpi_resource_extended_irq *ext_irq;
  750. switch (ares->type) {
  751. case ACPI_RESOURCE_TYPE_IRQ:
  752. /*
  753. * Per spec, only one interrupt per descriptor is allowed in
  754. * _CRS, but some firmware violates this, so parse them all.
  755. */
  756. irq = &ares->data.irq;
  757. if (index >= irq->interrupt_count) {
  758. irqresource_disabled(res, 0);
  759. return false;
  760. }
  761. acpi_dev_get_irqresource(res, irq->interrupts[index],
  762. irq->triggering, irq->polarity,
  763. irq->shareable, irq->wake_capable,
  764. true);
  765. break;
  766. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  767. ext_irq = &ares->data.extended_irq;
  768. if (index >= ext_irq->interrupt_count) {
  769. irqresource_disabled(res, 0);
  770. return false;
  771. }
  772. if (is_gsi(ext_irq))
  773. acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
  774. ext_irq->triggering, ext_irq->polarity,
  775. ext_irq->shareable, ext_irq->wake_capable,
  776. false);
  777. else
  778. irqresource_disabled(res, 0);
  779. break;
  780. default:
  781. res->flags = 0;
  782. return false;
  783. }
  784. return true;
  785. }
  786. EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
  787. /**
  788. * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
  789. * @list: The head of the resource list to free.
  790. */
  791. void acpi_dev_free_resource_list(struct list_head *list)
  792. {
  793. resource_list_free(list);
  794. }
  795. EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
  796. struct res_proc_context {
  797. struct list_head *list;
  798. int (*preproc)(struct acpi_resource *, void *);
  799. void *preproc_data;
  800. int count;
  801. int error;
  802. };
  803. static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
  804. struct res_proc_context *c)
  805. {
  806. struct resource_entry *rentry;
  807. rentry = resource_list_create_entry(NULL, 0);
  808. if (!rentry) {
  809. c->error = -ENOMEM;
  810. return AE_NO_MEMORY;
  811. }
  812. *rentry->res = win->res;
  813. rentry->offset = win->offset;
  814. resource_list_add_tail(rentry, c->list);
  815. c->count++;
  816. return AE_OK;
  817. }
  818. static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
  819. void *context)
  820. {
  821. struct res_proc_context *c = context;
  822. struct resource_win win;
  823. struct resource *res = &win.res;
  824. int i;
  825. if (c->preproc) {
  826. int ret;
  827. ret = c->preproc(ares, c->preproc_data);
  828. if (ret < 0) {
  829. c->error = ret;
  830. return AE_ABORT_METHOD;
  831. } else if (ret > 0) {
  832. return AE_OK;
  833. }
  834. }
  835. memset(&win, 0, sizeof(win));
  836. if (acpi_dev_resource_memory(ares, res)
  837. || acpi_dev_resource_io(ares, res)
  838. || acpi_dev_resource_address_space(ares, &win)
  839. || acpi_dev_resource_ext_address_space(ares, &win))
  840. return acpi_dev_new_resource_entry(&win, c);
  841. for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
  842. acpi_status status;
  843. status = acpi_dev_new_resource_entry(&win, c);
  844. if (ACPI_FAILURE(status))
  845. return status;
  846. }
  847. return AE_OK;
  848. }
  849. static int __acpi_dev_get_resources(struct acpi_device *adev,
  850. struct list_head *list,
  851. int (*preproc)(struct acpi_resource *, void *),
  852. void *preproc_data, char *method)
  853. {
  854. struct res_proc_context c;
  855. acpi_status status;
  856. if (!adev || !adev->handle || !list_empty(list))
  857. return -EINVAL;
  858. if (!acpi_has_method(adev->handle, method))
  859. return 0;
  860. c.list = list;
  861. c.preproc = preproc;
  862. c.preproc_data = preproc_data;
  863. c.count = 0;
  864. c.error = 0;
  865. status = acpi_walk_resources(adev->handle, method,
  866. acpi_dev_process_resource, &c);
  867. if (ACPI_FAILURE(status)) {
  868. acpi_dev_free_resource_list(list);
  869. return c.error ? c.error : -EIO;
  870. }
  871. return c.count;
  872. }
  873. /**
  874. * acpi_dev_get_resources - Get current resources of a device.
  875. * @adev: ACPI device node to get the resources for.
  876. * @list: Head of the resultant list of resources (must be empty).
  877. * @preproc: The caller's preprocessing routine.
  878. * @preproc_data: Pointer passed to the caller's preprocessing routine.
  879. *
  880. * Evaluate the _CRS method for the given device node and process its output by
  881. * (1) executing the @preproc() routine provided by the caller, passing the
  882. * resource pointer and @preproc_data to it as arguments, for each ACPI resource
  883. * returned and (2) converting all of the returned ACPI resources into struct
  884. * resource objects if possible. If the return value of @preproc() in step (1)
  885. * is different from 0, step (2) is not applied to the given ACPI resource and
  886. * if that value is negative, the whole processing is aborted and that value is
  887. * returned as the final error code.
  888. *
  889. * The resultant struct resource objects are put on the list pointed to by
  890. * @list, that must be empty initially, as members of struct resource_entry
  891. * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
  892. * free that list.
  893. *
  894. * The number of resources in the output list is returned on success, an error
  895. * code reflecting the error condition is returned otherwise.
  896. */
  897. int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
  898. int (*preproc)(struct acpi_resource *, void *),
  899. void *preproc_data)
  900. {
  901. return __acpi_dev_get_resources(adev, list, preproc, preproc_data,
  902. METHOD_NAME__CRS);
  903. }
  904. EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
  905. static int is_memory(struct acpi_resource *ares, void *not_used)
  906. {
  907. struct resource_win win;
  908. struct resource *res = &win.res;
  909. memset(&win, 0, sizeof(win));
  910. if (acpi_dev_filter_resource_type(ares, IORESOURCE_MEM))
  911. return 1;
  912. return !(acpi_dev_resource_memory(ares, res)
  913. || acpi_dev_resource_address_space(ares, &win)
  914. || acpi_dev_resource_ext_address_space(ares, &win));
  915. }
  916. /**
  917. * acpi_dev_get_dma_resources - Get current DMA resources of a device.
  918. * @adev: ACPI device node to get the resources for.
  919. * @list: Head of the resultant list of resources (must be empty).
  920. *
  921. * Evaluate the _DMA method for the given device node and process its
  922. * output.
  923. *
  924. * The resultant struct resource objects are put on the list pointed to
  925. * by @list, that must be empty initially, as members of struct
  926. * resource_entry objects. Callers of this routine should use
  927. * %acpi_dev_free_resource_list() to free that list.
  928. *
  929. * The number of resources in the output list is returned on success,
  930. * an error code reflecting the error condition is returned otherwise.
  931. */
  932. int acpi_dev_get_dma_resources(struct acpi_device *adev, struct list_head *list)
  933. {
  934. return __acpi_dev_get_resources(adev, list, is_memory, NULL,
  935. METHOD_NAME__DMA);
  936. }
  937. EXPORT_SYMBOL_GPL(acpi_dev_get_dma_resources);
  938. /**
  939. * acpi_dev_get_memory_resources - Get current memory resources of a device.
  940. * @adev: ACPI device node to get the resources for.
  941. * @list: Head of the resultant list of resources (must be empty).
  942. *
  943. * This is a helper function that locates all memory type resources of @adev
  944. * with acpi_dev_get_resources().
  945. *
  946. * The number of resources in the output list is returned on success, an error
  947. * code reflecting the error condition is returned otherwise.
  948. */
  949. int acpi_dev_get_memory_resources(struct acpi_device *adev, struct list_head *list)
  950. {
  951. return acpi_dev_get_resources(adev, list, is_memory, NULL);
  952. }
  953. EXPORT_SYMBOL_GPL(acpi_dev_get_memory_resources);
  954. /**
  955. * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
  956. * types
  957. * @ares: Input ACPI resource object.
  958. * @types: Valid resource types of IORESOURCE_XXX
  959. *
  960. * This is a helper function to support acpi_dev_get_resources(), which filters
  961. * ACPI resource objects according to resource types.
  962. */
  963. int acpi_dev_filter_resource_type(struct acpi_resource *ares,
  964. unsigned long types)
  965. {
  966. unsigned long type = 0;
  967. switch (ares->type) {
  968. case ACPI_RESOURCE_TYPE_MEMORY24:
  969. case ACPI_RESOURCE_TYPE_MEMORY32:
  970. case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
  971. type = IORESOURCE_MEM;
  972. break;
  973. case ACPI_RESOURCE_TYPE_IO:
  974. case ACPI_RESOURCE_TYPE_FIXED_IO:
  975. type = IORESOURCE_IO;
  976. break;
  977. case ACPI_RESOURCE_TYPE_IRQ:
  978. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  979. type = IORESOURCE_IRQ;
  980. break;
  981. case ACPI_RESOURCE_TYPE_DMA:
  982. case ACPI_RESOURCE_TYPE_FIXED_DMA:
  983. type = IORESOURCE_DMA;
  984. break;
  985. case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
  986. type = IORESOURCE_REG;
  987. break;
  988. case ACPI_RESOURCE_TYPE_ADDRESS16:
  989. case ACPI_RESOURCE_TYPE_ADDRESS32:
  990. case ACPI_RESOURCE_TYPE_ADDRESS64:
  991. case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
  992. if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
  993. type = IORESOURCE_MEM;
  994. else if (ares->data.address.resource_type == ACPI_IO_RANGE)
  995. type = IORESOURCE_IO;
  996. else if (ares->data.address.resource_type ==
  997. ACPI_BUS_NUMBER_RANGE)
  998. type = IORESOURCE_BUS;
  999. break;
  1000. default:
  1001. break;
  1002. }
  1003. return (type & types) ? 0 : 1;
  1004. }
  1005. EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);
  1006. static int acpi_dev_consumes_res(struct acpi_device *adev, struct resource *res)
  1007. {
  1008. struct list_head resource_list;
  1009. struct resource_entry *rentry;
  1010. int ret, found = 0;
  1011. INIT_LIST_HEAD(&resource_list);
  1012. ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  1013. if (ret < 0)
  1014. return 0;
  1015. list_for_each_entry(rentry, &resource_list, node) {
  1016. if (resource_contains(rentry->res, res)) {
  1017. found = 1;
  1018. break;
  1019. }
  1020. }
  1021. acpi_dev_free_resource_list(&resource_list);
  1022. return found;
  1023. }
  1024. static acpi_status acpi_res_consumer_cb(acpi_handle handle, u32 depth,
  1025. void *context, void **ret)
  1026. {
  1027. struct resource *res = context;
  1028. struct acpi_device **consumer = (struct acpi_device **) ret;
  1029. struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
  1030. if (!adev)
  1031. return AE_OK;
  1032. if (acpi_dev_consumes_res(adev, res)) {
  1033. *consumer = adev;
  1034. return AE_CTRL_TERMINATE;
  1035. }
  1036. return AE_OK;
  1037. }
  1038. /**
  1039. * acpi_resource_consumer - Find the ACPI device that consumes @res.
  1040. * @res: Resource to search for.
  1041. *
  1042. * Search the current resource settings (_CRS) of every ACPI device node
  1043. * for @res. If we find an ACPI device whose _CRS includes @res, return
  1044. * it. Otherwise, return NULL.
  1045. */
  1046. struct acpi_device *acpi_resource_consumer(struct resource *res)
  1047. {
  1048. struct acpi_device *consumer = NULL;
  1049. acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer);
  1050. return consumer;
  1051. }