resource.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. #ifdef CONFIG_X86
  19. #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
  20. static inline bool acpi_iospace_resource_valid(struct resource *res)
  21. {
  22. /* On X86 IO space is limited to the [0 - 64K] IO port range */
  23. return res->end < 0x10003;
  24. }
  25. #else
  26. #define valid_IRQ(i) (true)
  27. /*
  28. * ACPI IO descriptors on arches other than X86 contain MMIO CPU physical
  29. * addresses mapping IO space in CPU physical address space, IO space
  30. * resources can be placed anywhere in the 64-bit physical address space.
  31. */
  32. static inline bool
  33. acpi_iospace_resource_valid(struct resource *res) { return true; }
  34. #endif
  35. #if IS_ENABLED(CONFIG_ACPI_GENERIC_GSI)
  36. static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
  37. {
  38. return ext_irq->resource_source.string_length == 0 &&
  39. ext_irq->producer_consumer == ACPI_CONSUMER;
  40. }
  41. #else
  42. static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
  43. {
  44. return true;
  45. }
  46. #endif
  47. static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
  48. {
  49. u64 reslen = end - start + 1;
  50. /*
  51. * CHECKME: len might be required to check versus a minimum
  52. * length as well. 1 for io is fine, but for memory it does
  53. * not make any sense at all.
  54. * Note: some BIOSes report incorrect length for ACPI address space
  55. * descriptor, so remove check of 'reslen == len' to avoid regression.
  56. */
  57. if (len && reslen && start <= end)
  58. return true;
  59. pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
  60. io ? "io" : "mem", start, end, len);
  61. return false;
  62. }
  63. static void acpi_dev_memresource_flags(struct resource *res, u64 len,
  64. u8 write_protect)
  65. {
  66. res->flags = IORESOURCE_MEM;
  67. if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
  68. res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
  69. if (write_protect == ACPI_READ_WRITE_MEMORY)
  70. res->flags |= IORESOURCE_MEM_WRITEABLE;
  71. }
  72. static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
  73. u8 write_protect)
  74. {
  75. res->start = start;
  76. res->end = start + len - 1;
  77. acpi_dev_memresource_flags(res, len, write_protect);
  78. }
  79. /**
  80. * acpi_dev_resource_memory - Extract ACPI memory resource information.
  81. * @ares: Input ACPI resource object.
  82. * @res: Output generic resource object.
  83. *
  84. * Check if the given ACPI resource object represents a memory resource and
  85. * if that's the case, use the information in it to populate the generic
  86. * resource object pointed to by @res.
  87. *
  88. * Return:
  89. * 1) false with res->flags setting to zero: not the expected resource type
  90. * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
  91. * 3) true: valid assigned resource
  92. */
  93. bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
  94. {
  95. struct acpi_resource_memory24 *memory24;
  96. struct acpi_resource_memory32 *memory32;
  97. struct acpi_resource_fixed_memory32 *fixed_memory32;
  98. switch (ares->type) {
  99. case ACPI_RESOURCE_TYPE_MEMORY24:
  100. memory24 = &ares->data.memory24;
  101. acpi_dev_get_memresource(res, memory24->minimum << 8,
  102. memory24->address_length << 8,
  103. memory24->write_protect);
  104. break;
  105. case ACPI_RESOURCE_TYPE_MEMORY32:
  106. memory32 = &ares->data.memory32;
  107. acpi_dev_get_memresource(res, memory32->minimum,
  108. memory32->address_length,
  109. memory32->write_protect);
  110. break;
  111. case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
  112. fixed_memory32 = &ares->data.fixed_memory32;
  113. acpi_dev_get_memresource(res, fixed_memory32->address,
  114. fixed_memory32->address_length,
  115. fixed_memory32->write_protect);
  116. break;
  117. default:
  118. res->flags = 0;
  119. return false;
  120. }
  121. return !(res->flags & IORESOURCE_DISABLED);
  122. }
  123. EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
  124. static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
  125. u8 io_decode, u8 translation_type)
  126. {
  127. res->flags = IORESOURCE_IO;
  128. if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
  129. res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
  130. if (!acpi_iospace_resource_valid(res))
  131. res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
  132. if (io_decode == ACPI_DECODE_16)
  133. res->flags |= IORESOURCE_IO_16BIT_ADDR;
  134. if (translation_type == ACPI_SPARSE_TRANSLATION)
  135. res->flags |= IORESOURCE_IO_SPARSE;
  136. }
  137. static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
  138. u8 io_decode)
  139. {
  140. res->start = start;
  141. res->end = start + len - 1;
  142. acpi_dev_ioresource_flags(res, len, io_decode, 0);
  143. }
  144. /**
  145. * acpi_dev_resource_io - Extract ACPI I/O resource information.
  146. * @ares: Input ACPI resource object.
  147. * @res: Output generic resource object.
  148. *
  149. * Check if the given ACPI resource object represents an I/O resource and
  150. * if that's the case, use the information in it to populate the generic
  151. * resource object pointed to by @res.
  152. *
  153. * Return:
  154. * 1) false with res->flags setting to zero: not the expected resource type
  155. * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
  156. * 3) true: valid assigned resource
  157. */
  158. bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
  159. {
  160. struct acpi_resource_io *io;
  161. struct acpi_resource_fixed_io *fixed_io;
  162. switch (ares->type) {
  163. case ACPI_RESOURCE_TYPE_IO:
  164. io = &ares->data.io;
  165. acpi_dev_get_ioresource(res, io->minimum,
  166. io->address_length,
  167. io->io_decode);
  168. break;
  169. case ACPI_RESOURCE_TYPE_FIXED_IO:
  170. fixed_io = &ares->data.fixed_io;
  171. acpi_dev_get_ioresource(res, fixed_io->address,
  172. fixed_io->address_length,
  173. ACPI_DECODE_10);
  174. break;
  175. default:
  176. res->flags = 0;
  177. return false;
  178. }
  179. return !(res->flags & IORESOURCE_DISABLED);
  180. }
  181. EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
  182. static bool acpi_decode_space(struct resource_win *win,
  183. struct acpi_resource_address *addr,
  184. struct acpi_address64_attribute *attr)
  185. {
  186. u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
  187. bool wp = addr->info.mem.write_protect;
  188. u64 len = attr->address_length;
  189. u64 start, end, offset = 0;
  190. struct resource *res = &win->res;
  191. /*
  192. * Filter out invalid descriptor according to ACPI Spec 5.0, section
  193. * 6.4.3.5 Address Space Resource Descriptors.
  194. */
  195. if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
  196. (addr->min_address_fixed && addr->max_address_fixed && !len))
  197. pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
  198. addr->min_address_fixed, addr->max_address_fixed, len);
  199. /*
  200. * For bridges that translate addresses across the bridge,
  201. * translation_offset is the offset that must be added to the
  202. * address on the secondary side to obtain the address on the
  203. * primary side. Non-bridge devices must list 0 for all Address
  204. * Translation offset bits.
  205. */
  206. if (addr->producer_consumer == ACPI_PRODUCER)
  207. offset = attr->translation_offset;
  208. else if (attr->translation_offset)
  209. pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
  210. attr->translation_offset);
  211. start = attr->minimum + offset;
  212. end = attr->maximum + offset;
  213. win->offset = offset;
  214. res->start = start;
  215. res->end = end;
  216. if (sizeof(resource_size_t) < sizeof(u64) &&
  217. (offset != win->offset || start != res->start || end != res->end)) {
  218. pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
  219. attr->minimum, attr->maximum);
  220. return false;
  221. }
  222. switch (addr->resource_type) {
  223. case ACPI_MEMORY_RANGE:
  224. acpi_dev_memresource_flags(res, len, wp);
  225. break;
  226. case ACPI_IO_RANGE:
  227. acpi_dev_ioresource_flags(res, len, iodec,
  228. addr->info.io.translation_type);
  229. break;
  230. case ACPI_BUS_NUMBER_RANGE:
  231. res->flags = IORESOURCE_BUS;
  232. break;
  233. default:
  234. return false;
  235. }
  236. if (addr->producer_consumer == ACPI_PRODUCER)
  237. res->flags |= IORESOURCE_WINDOW;
  238. if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  239. res->flags |= IORESOURCE_PREFETCH;
  240. return !(res->flags & IORESOURCE_DISABLED);
  241. }
  242. /**
  243. * acpi_dev_resource_address_space - Extract ACPI address space information.
  244. * @ares: Input ACPI resource object.
  245. * @win: Output generic resource object.
  246. *
  247. * Check if the given ACPI resource object represents an address space resource
  248. * and if that's the case, use the information in it to populate the generic
  249. * resource object pointed to by @win.
  250. *
  251. * Return:
  252. * 1) false with win->res.flags setting to zero: not the expected resource type
  253. * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
  254. * resource
  255. * 3) true: valid assigned resource
  256. */
  257. bool acpi_dev_resource_address_space(struct acpi_resource *ares,
  258. struct resource_win *win)
  259. {
  260. struct acpi_resource_address64 addr;
  261. win->res.flags = 0;
  262. if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
  263. return false;
  264. return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
  265. &addr.address);
  266. }
  267. EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
  268. /**
  269. * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
  270. * @ares: Input ACPI resource object.
  271. * @win: Output generic resource object.
  272. *
  273. * Check if the given ACPI resource object represents an extended address space
  274. * resource and if that's the case, use the information in it to populate the
  275. * generic resource object pointed to by @win.
  276. *
  277. * Return:
  278. * 1) false with win->res.flags setting to zero: not the expected resource type
  279. * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
  280. * resource
  281. * 3) true: valid assigned resource
  282. */
  283. bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
  284. struct resource_win *win)
  285. {
  286. struct acpi_resource_extended_address64 *ext_addr;
  287. win->res.flags = 0;
  288. if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
  289. return false;
  290. ext_addr = &ares->data.ext_address64;
  291. return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
  292. &ext_addr->address);
  293. }
  294. EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
  295. /**
  296. * acpi_dev_irq_flags - Determine IRQ resource flags.
  297. * @triggering: Triggering type as provided by ACPI.
  298. * @polarity: Interrupt polarity as provided by ACPI.
  299. * @shareable: Whether or not the interrupt is shareable.
  300. */
  301. unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
  302. {
  303. unsigned long flags;
  304. if (triggering == ACPI_LEVEL_SENSITIVE)
  305. flags = polarity == ACPI_ACTIVE_LOW ?
  306. IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
  307. else
  308. flags = polarity == ACPI_ACTIVE_LOW ?
  309. IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
  310. if (shareable == ACPI_SHARED)
  311. flags |= IORESOURCE_IRQ_SHAREABLE;
  312. return flags | IORESOURCE_IRQ;
  313. }
  314. EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
  315. /**
  316. * acpi_dev_get_irq_type - Determine irq type.
  317. * @triggering: Triggering type as provided by ACPI.
  318. * @polarity: Interrupt polarity as provided by ACPI.
  319. */
  320. unsigned int acpi_dev_get_irq_type(int triggering, int polarity)
  321. {
  322. switch (polarity) {
  323. case ACPI_ACTIVE_LOW:
  324. return triggering == ACPI_EDGE_SENSITIVE ?
  325. IRQ_TYPE_EDGE_FALLING :
  326. IRQ_TYPE_LEVEL_LOW;
  327. case ACPI_ACTIVE_HIGH:
  328. return triggering == ACPI_EDGE_SENSITIVE ?
  329. IRQ_TYPE_EDGE_RISING :
  330. IRQ_TYPE_LEVEL_HIGH;
  331. case ACPI_ACTIVE_BOTH:
  332. if (triggering == ACPI_EDGE_SENSITIVE)
  333. return IRQ_TYPE_EDGE_BOTH;
  334. fallthrough;
  335. default:
  336. return IRQ_TYPE_NONE;
  337. }
  338. }
  339. EXPORT_SYMBOL_GPL(acpi_dev_get_irq_type);
  340. static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
  341. {
  342. res->start = gsi;
  343. res->end = gsi;
  344. res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
  345. }
  346. static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
  347. u8 triggering, u8 polarity, u8 shareable,
  348. bool legacy)
  349. {
  350. int irq, p, t;
  351. if (!valid_IRQ(gsi)) {
  352. acpi_dev_irqresource_disabled(res, gsi);
  353. return;
  354. }
  355. /*
  356. * In IO-APIC mode, use overridden attribute. Two reasons:
  357. * 1. BIOS bug in DSDT
  358. * 2. BIOS uses IO-APIC mode Interrupt Source Override
  359. *
  360. * We do this only if we are dealing with IRQ() or IRQNoFlags()
  361. * resource (the legacy ISA resources). With modern ACPI 5 devices
  362. * using extended IRQ descriptors we take the IRQ configuration
  363. * from _CRS directly.
  364. */
  365. if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
  366. u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
  367. u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
  368. if (triggering != trig || polarity != pol) {
  369. pr_warn("ACPI: IRQ %d override to %s, %s\n", gsi,
  370. t ? "level" : "edge", p ? "low" : "high");
  371. triggering = trig;
  372. polarity = pol;
  373. }
  374. }
  375. res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
  376. irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
  377. if (irq >= 0) {
  378. res->start = irq;
  379. res->end = irq;
  380. } else {
  381. acpi_dev_irqresource_disabled(res, gsi);
  382. }
  383. }
  384. /**
  385. * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
  386. * @ares: Input ACPI resource object.
  387. * @index: Index into the array of GSIs represented by the resource.
  388. * @res: Output generic resource object.
  389. *
  390. * Check if the given ACPI resource object represents an interrupt resource
  391. * and @index does not exceed the resource's interrupt count (true is returned
  392. * in that case regardless of the results of the other checks)). If that's the
  393. * case, register the GSI corresponding to @index from the array of interrupts
  394. * represented by the resource and populate the generic resource object pointed
  395. * to by @res accordingly. If the registration of the GSI is not successful,
  396. * IORESOURCE_DISABLED will be set it that object's flags.
  397. *
  398. * Return:
  399. * 1) false with res->flags setting to zero: not the expected resource type
  400. * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
  401. * 3) true: valid assigned resource
  402. */
  403. bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
  404. struct resource *res)
  405. {
  406. struct acpi_resource_irq *irq;
  407. struct acpi_resource_extended_irq *ext_irq;
  408. switch (ares->type) {
  409. case ACPI_RESOURCE_TYPE_IRQ:
  410. /*
  411. * Per spec, only one interrupt per descriptor is allowed in
  412. * _CRS, but some firmware violates this, so parse them all.
  413. */
  414. irq = &ares->data.irq;
  415. if (index >= irq->interrupt_count) {
  416. acpi_dev_irqresource_disabled(res, 0);
  417. return false;
  418. }
  419. acpi_dev_get_irqresource(res, irq->interrupts[index],
  420. irq->triggering, irq->polarity,
  421. irq->shareable, true);
  422. break;
  423. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  424. ext_irq = &ares->data.extended_irq;
  425. if (index >= ext_irq->interrupt_count) {
  426. acpi_dev_irqresource_disabled(res, 0);
  427. return false;
  428. }
  429. if (is_gsi(ext_irq))
  430. acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
  431. ext_irq->triggering, ext_irq->polarity,
  432. ext_irq->shareable, false);
  433. else
  434. acpi_dev_irqresource_disabled(res, 0);
  435. break;
  436. default:
  437. res->flags = 0;
  438. return false;
  439. }
  440. return true;
  441. }
  442. EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
  443. /**
  444. * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
  445. * @list: The head of the resource list to free.
  446. */
  447. void acpi_dev_free_resource_list(struct list_head *list)
  448. {
  449. resource_list_free(list);
  450. }
  451. EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
  452. struct res_proc_context {
  453. struct list_head *list;
  454. int (*preproc)(struct acpi_resource *, void *);
  455. void *preproc_data;
  456. int count;
  457. int error;
  458. };
  459. static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
  460. struct res_proc_context *c)
  461. {
  462. struct resource_entry *rentry;
  463. rentry = resource_list_create_entry(NULL, 0);
  464. if (!rentry) {
  465. c->error = -ENOMEM;
  466. return AE_NO_MEMORY;
  467. }
  468. *rentry->res = win->res;
  469. rentry->offset = win->offset;
  470. resource_list_add_tail(rentry, c->list);
  471. c->count++;
  472. return AE_OK;
  473. }
  474. static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
  475. void *context)
  476. {
  477. struct res_proc_context *c = context;
  478. struct resource_win win;
  479. struct resource *res = &win.res;
  480. int i;
  481. if (c->preproc) {
  482. int ret;
  483. ret = c->preproc(ares, c->preproc_data);
  484. if (ret < 0) {
  485. c->error = ret;
  486. return AE_ABORT_METHOD;
  487. } else if (ret > 0) {
  488. return AE_OK;
  489. }
  490. }
  491. memset(&win, 0, sizeof(win));
  492. if (acpi_dev_resource_memory(ares, res)
  493. || acpi_dev_resource_io(ares, res)
  494. || acpi_dev_resource_address_space(ares, &win)
  495. || acpi_dev_resource_ext_address_space(ares, &win))
  496. return acpi_dev_new_resource_entry(&win, c);
  497. for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
  498. acpi_status status;
  499. status = acpi_dev_new_resource_entry(&win, c);
  500. if (ACPI_FAILURE(status))
  501. return status;
  502. }
  503. return AE_OK;
  504. }
  505. static int __acpi_dev_get_resources(struct acpi_device *adev,
  506. struct list_head *list,
  507. int (*preproc)(struct acpi_resource *, void *),
  508. void *preproc_data, char *method)
  509. {
  510. struct res_proc_context c;
  511. acpi_status status;
  512. if (!adev || !adev->handle || !list_empty(list))
  513. return -EINVAL;
  514. if (!acpi_has_method(adev->handle, method))
  515. return 0;
  516. c.list = list;
  517. c.preproc = preproc;
  518. c.preproc_data = preproc_data;
  519. c.count = 0;
  520. c.error = 0;
  521. status = acpi_walk_resources(adev->handle, method,
  522. acpi_dev_process_resource, &c);
  523. if (ACPI_FAILURE(status)) {
  524. acpi_dev_free_resource_list(list);
  525. return c.error ? c.error : -EIO;
  526. }
  527. return c.count;
  528. }
  529. /**
  530. * acpi_dev_get_resources - Get current resources of a device.
  531. * @adev: ACPI device node to get the resources for.
  532. * @list: Head of the resultant list of resources (must be empty).
  533. * @preproc: The caller's preprocessing routine.
  534. * @preproc_data: Pointer passed to the caller's preprocessing routine.
  535. *
  536. * Evaluate the _CRS method for the given device node and process its output by
  537. * (1) executing the @preproc() rountine provided by the caller, passing the
  538. * resource pointer and @preproc_data to it as arguments, for each ACPI resource
  539. * returned and (2) converting all of the returned ACPI resources into struct
  540. * resource objects if possible. If the return value of @preproc() in step (1)
  541. * is different from 0, step (2) is not applied to the given ACPI resource and
  542. * if that value is negative, the whole processing is aborted and that value is
  543. * returned as the final error code.
  544. *
  545. * The resultant struct resource objects are put on the list pointed to by
  546. * @list, that must be empty initially, as members of struct resource_entry
  547. * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
  548. * free that list.
  549. *
  550. * The number of resources in the output list is returned on success, an error
  551. * code reflecting the error condition is returned otherwise.
  552. */
  553. int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
  554. int (*preproc)(struct acpi_resource *, void *),
  555. void *preproc_data)
  556. {
  557. return __acpi_dev_get_resources(adev, list, preproc, preproc_data,
  558. METHOD_NAME__CRS);
  559. }
  560. EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
  561. static int is_memory(struct acpi_resource *ares, void *not_used)
  562. {
  563. struct resource_win win;
  564. struct resource *res = &win.res;
  565. memset(&win, 0, sizeof(win));
  566. return !(acpi_dev_resource_memory(ares, res)
  567. || acpi_dev_resource_address_space(ares, &win)
  568. || acpi_dev_resource_ext_address_space(ares, &win));
  569. }
  570. /**
  571. * acpi_dev_get_dma_resources - Get current DMA resources of a device.
  572. * @adev: ACPI device node to get the resources for.
  573. * @list: Head of the resultant list of resources (must be empty).
  574. *
  575. * Evaluate the _DMA method for the given device node and process its
  576. * output.
  577. *
  578. * The resultant struct resource objects are put on the list pointed to
  579. * by @list, that must be empty initially, as members of struct
  580. * resource_entry objects. Callers of this routine should use
  581. * %acpi_dev_free_resource_list() to free that list.
  582. *
  583. * The number of resources in the output list is returned on success,
  584. * an error code reflecting the error condition is returned otherwise.
  585. */
  586. int acpi_dev_get_dma_resources(struct acpi_device *adev, struct list_head *list)
  587. {
  588. return __acpi_dev_get_resources(adev, list, is_memory, NULL,
  589. METHOD_NAME__DMA);
  590. }
  591. EXPORT_SYMBOL_GPL(acpi_dev_get_dma_resources);
  592. /**
  593. * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
  594. * types
  595. * @ares: Input ACPI resource object.
  596. * @types: Valid resource types of IORESOURCE_XXX
  597. *
  598. * This is a helper function to support acpi_dev_get_resources(), which filters
  599. * ACPI resource objects according to resource types.
  600. */
  601. int acpi_dev_filter_resource_type(struct acpi_resource *ares,
  602. unsigned long types)
  603. {
  604. unsigned long type = 0;
  605. switch (ares->type) {
  606. case ACPI_RESOURCE_TYPE_MEMORY24:
  607. case ACPI_RESOURCE_TYPE_MEMORY32:
  608. case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
  609. type = IORESOURCE_MEM;
  610. break;
  611. case ACPI_RESOURCE_TYPE_IO:
  612. case ACPI_RESOURCE_TYPE_FIXED_IO:
  613. type = IORESOURCE_IO;
  614. break;
  615. case ACPI_RESOURCE_TYPE_IRQ:
  616. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  617. type = IORESOURCE_IRQ;
  618. break;
  619. case ACPI_RESOURCE_TYPE_DMA:
  620. case ACPI_RESOURCE_TYPE_FIXED_DMA:
  621. type = IORESOURCE_DMA;
  622. break;
  623. case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
  624. type = IORESOURCE_REG;
  625. break;
  626. case ACPI_RESOURCE_TYPE_ADDRESS16:
  627. case ACPI_RESOURCE_TYPE_ADDRESS32:
  628. case ACPI_RESOURCE_TYPE_ADDRESS64:
  629. case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
  630. if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
  631. type = IORESOURCE_MEM;
  632. else if (ares->data.address.resource_type == ACPI_IO_RANGE)
  633. type = IORESOURCE_IO;
  634. else if (ares->data.address.resource_type ==
  635. ACPI_BUS_NUMBER_RANGE)
  636. type = IORESOURCE_BUS;
  637. break;
  638. default:
  639. break;
  640. }
  641. return (type & types) ? 0 : 1;
  642. }
  643. EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);
  644. static int acpi_dev_consumes_res(struct acpi_device *adev, struct resource *res)
  645. {
  646. struct list_head resource_list;
  647. struct resource_entry *rentry;
  648. int ret, found = 0;
  649. INIT_LIST_HEAD(&resource_list);
  650. ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  651. if (ret < 0)
  652. return 0;
  653. list_for_each_entry(rentry, &resource_list, node) {
  654. if (resource_contains(rentry->res, res)) {
  655. found = 1;
  656. break;
  657. }
  658. }
  659. acpi_dev_free_resource_list(&resource_list);
  660. return found;
  661. }
  662. static acpi_status acpi_res_consumer_cb(acpi_handle handle, u32 depth,
  663. void *context, void **ret)
  664. {
  665. struct resource *res = context;
  666. struct acpi_device **consumer = (struct acpi_device **) ret;
  667. struct acpi_device *adev;
  668. if (acpi_bus_get_device(handle, &adev))
  669. return AE_OK;
  670. if (acpi_dev_consumes_res(adev, res)) {
  671. *consumer = adev;
  672. return AE_CTRL_TERMINATE;
  673. }
  674. return AE_OK;
  675. }
  676. /**
  677. * acpi_resource_consumer - Find the ACPI device that consumes @res.
  678. * @res: Resource to search for.
  679. *
  680. * Search the current resource settings (_CRS) of every ACPI device node
  681. * for @res. If we find an ACPI device whose _CRS includes @res, return
  682. * it. Otherwise, return NULL.
  683. */
  684. struct acpi_device *acpi_resource_consumer(struct resource *res)
  685. {
  686. struct acpi_device *consumer = NULL;
  687. acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer);
  688. return consumer;
  689. }