resource.c 20 KB

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