resource.c 23 KB

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