resource.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/acpi.h>
  25. #include <linux/device.h>
  26. #include <linux/export.h>
  27. #include <linux/ioport.h>
  28. #include <linux/slab.h>
  29. #ifdef CONFIG_X86
  30. #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
  31. #else
  32. #define valid_IRQ(i) (true)
  33. #endif
  34. static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
  35. {
  36. u64 reslen = end - start + 1;
  37. /*
  38. * CHECKME: len might be required to check versus a minimum
  39. * length as well. 1 for io is fine, but for memory it does
  40. * not make any sense at all.
  41. */
  42. if (len && reslen && reslen == len && start <= end)
  43. return true;
  44. pr_info("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
  45. io ? "io" : "mem", start, end, len);
  46. return false;
  47. }
  48. static void acpi_dev_memresource_flags(struct resource *res, u64 len,
  49. u8 write_protect)
  50. {
  51. res->flags = IORESOURCE_MEM;
  52. if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
  53. res->flags |= IORESOURCE_DISABLED;
  54. if (write_protect == ACPI_READ_WRITE_MEMORY)
  55. res->flags |= IORESOURCE_MEM_WRITEABLE;
  56. }
  57. static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
  58. u8 write_protect)
  59. {
  60. res->start = start;
  61. res->end = start + len - 1;
  62. acpi_dev_memresource_flags(res, len, write_protect);
  63. }
  64. /**
  65. * acpi_dev_resource_memory - Extract ACPI memory resource information.
  66. * @ares: Input ACPI resource object.
  67. * @res: Output generic resource object.
  68. *
  69. * Check if the given ACPI resource object represents a memory resource and
  70. * if that's the case, use the information in it to populate the generic
  71. * resource object pointed to by @res.
  72. */
  73. bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
  74. {
  75. struct acpi_resource_memory24 *memory24;
  76. struct acpi_resource_memory32 *memory32;
  77. struct acpi_resource_fixed_memory32 *fixed_memory32;
  78. switch (ares->type) {
  79. case ACPI_RESOURCE_TYPE_MEMORY24:
  80. memory24 = &ares->data.memory24;
  81. acpi_dev_get_memresource(res, memory24->minimum << 8,
  82. memory24->address_length << 8,
  83. memory24->write_protect);
  84. break;
  85. case ACPI_RESOURCE_TYPE_MEMORY32:
  86. memory32 = &ares->data.memory32;
  87. acpi_dev_get_memresource(res, memory32->minimum,
  88. memory32->address_length,
  89. memory32->write_protect);
  90. break;
  91. case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
  92. fixed_memory32 = &ares->data.fixed_memory32;
  93. acpi_dev_get_memresource(res, fixed_memory32->address,
  94. fixed_memory32->address_length,
  95. fixed_memory32->write_protect);
  96. break;
  97. default:
  98. return false;
  99. }
  100. return !(res->flags & IORESOURCE_DISABLED);
  101. }
  102. EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
  103. static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
  104. u8 io_decode)
  105. {
  106. res->flags = IORESOURCE_IO;
  107. if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
  108. res->flags |= IORESOURCE_DISABLED;
  109. if (res->end >= 0x10003)
  110. res->flags |= IORESOURCE_DISABLED;
  111. if (io_decode == ACPI_DECODE_16)
  112. res->flags |= IORESOURCE_IO_16BIT_ADDR;
  113. }
  114. static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
  115. u8 io_decode)
  116. {
  117. res->start = start;
  118. res->end = start + len - 1;
  119. acpi_dev_ioresource_flags(res, len, io_decode);
  120. }
  121. /**
  122. * acpi_dev_resource_io - Extract ACPI I/O resource information.
  123. * @ares: Input ACPI resource object.
  124. * @res: Output generic resource object.
  125. *
  126. * Check if the given ACPI resource object represents an I/O resource and
  127. * if that's the case, use the information in it to populate the generic
  128. * resource object pointed to by @res.
  129. */
  130. bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
  131. {
  132. struct acpi_resource_io *io;
  133. struct acpi_resource_fixed_io *fixed_io;
  134. switch (ares->type) {
  135. case ACPI_RESOURCE_TYPE_IO:
  136. io = &ares->data.io;
  137. acpi_dev_get_ioresource(res, io->minimum,
  138. io->address_length,
  139. io->io_decode);
  140. break;
  141. case ACPI_RESOURCE_TYPE_FIXED_IO:
  142. fixed_io = &ares->data.fixed_io;
  143. acpi_dev_get_ioresource(res, fixed_io->address,
  144. fixed_io->address_length,
  145. ACPI_DECODE_10);
  146. break;
  147. default:
  148. return false;
  149. }
  150. return !(res->flags & IORESOURCE_DISABLED);
  151. }
  152. EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
  153. static bool acpi_decode_space(struct resource *res,
  154. struct acpi_resource_address *addr,
  155. struct acpi_address64_attribute *attr)
  156. {
  157. u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
  158. bool wp = addr->info.mem.write_protect;
  159. u64 len = attr->address_length;
  160. res->start = attr->minimum;
  161. res->end = attr->maximum;
  162. switch (addr->resource_type) {
  163. case ACPI_MEMORY_RANGE:
  164. acpi_dev_memresource_flags(res, len, wp);
  165. break;
  166. case ACPI_IO_RANGE:
  167. acpi_dev_ioresource_flags(res, len, iodec);
  168. break;
  169. case ACPI_BUS_NUMBER_RANGE:
  170. res->flags = IORESOURCE_BUS;
  171. break;
  172. default:
  173. return false;
  174. }
  175. if (addr->producer_consumer == ACPI_PRODUCER)
  176. res->flags |= IORESOURCE_WINDOW;
  177. if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  178. res->flags |= IORESOURCE_PREFETCH;
  179. return !(res->flags & IORESOURCE_DISABLED);
  180. }
  181. /**
  182. * acpi_dev_resource_address_space - Extract ACPI address space information.
  183. * @ares: Input ACPI resource object.
  184. * @res: Output generic resource object.
  185. *
  186. * Check if the given ACPI resource object represents an address space resource
  187. * and if that's the case, use the information in it to populate the generic
  188. * resource object pointed to by @res.
  189. */
  190. bool acpi_dev_resource_address_space(struct acpi_resource *ares,
  191. struct resource *res)
  192. {
  193. struct acpi_resource_address64 addr;
  194. if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
  195. return false;
  196. return acpi_decode_space(res, (struct acpi_resource_address *)&addr,
  197. &addr.address);
  198. }
  199. EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
  200. /**
  201. * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
  202. * @ares: Input ACPI resource object.
  203. * @res: Output generic resource object.
  204. *
  205. * Check if the given ACPI resource object represents an extended address space
  206. * resource and if that's the case, use the information in it to populate the
  207. * generic resource object pointed to by @res.
  208. */
  209. bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
  210. struct resource *res)
  211. {
  212. struct acpi_resource_extended_address64 *ext_addr;
  213. if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
  214. return false;
  215. ext_addr = &ares->data.ext_address64;
  216. return acpi_decode_space(res, (struct acpi_resource_address *)ext_addr,
  217. &ext_addr->address);
  218. }
  219. EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
  220. /**
  221. * acpi_dev_irq_flags - Determine IRQ resource flags.
  222. * @triggering: Triggering type as provided by ACPI.
  223. * @polarity: Interrupt polarity as provided by ACPI.
  224. * @shareable: Whether or not the interrupt is shareable.
  225. */
  226. unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
  227. {
  228. unsigned long flags;
  229. if (triggering == ACPI_LEVEL_SENSITIVE)
  230. flags = polarity == ACPI_ACTIVE_LOW ?
  231. IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
  232. else
  233. flags = polarity == ACPI_ACTIVE_LOW ?
  234. IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
  235. if (shareable == ACPI_SHARED)
  236. flags |= IORESOURCE_IRQ_SHAREABLE;
  237. return flags | IORESOURCE_IRQ;
  238. }
  239. EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
  240. static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
  241. {
  242. res->start = gsi;
  243. res->end = gsi;
  244. res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED;
  245. }
  246. static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
  247. u8 triggering, u8 polarity, u8 shareable,
  248. bool legacy)
  249. {
  250. int irq, p, t;
  251. if (!valid_IRQ(gsi)) {
  252. acpi_dev_irqresource_disabled(res, gsi);
  253. return;
  254. }
  255. /*
  256. * In IO-APIC mode, use overrided attribute. Two reasons:
  257. * 1. BIOS bug in DSDT
  258. * 2. BIOS uses IO-APIC mode Interrupt Source Override
  259. *
  260. * We do this only if we are dealing with IRQ() or IRQNoFlags()
  261. * resource (the legacy ISA resources). With modern ACPI 5 devices
  262. * using extended IRQ descriptors we take the IRQ configuration
  263. * from _CRS directly.
  264. */
  265. if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
  266. u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
  267. u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
  268. if (triggering != trig || polarity != pol) {
  269. pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
  270. t ? "level" : "edge", p ? "low" : "high");
  271. triggering = trig;
  272. polarity = pol;
  273. }
  274. }
  275. res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
  276. irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
  277. if (irq >= 0) {
  278. res->start = irq;
  279. res->end = irq;
  280. } else {
  281. acpi_dev_irqresource_disabled(res, gsi);
  282. }
  283. }
  284. /**
  285. * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
  286. * @ares: Input ACPI resource object.
  287. * @index: Index into the array of GSIs represented by the resource.
  288. * @res: Output generic resource object.
  289. *
  290. * Check if the given ACPI resource object represents an interrupt resource
  291. * and @index does not exceed the resource's interrupt count (true is returned
  292. * in that case regardless of the results of the other checks)). If that's the
  293. * case, register the GSI corresponding to @index from the array of interrupts
  294. * represented by the resource and populate the generic resource object pointed
  295. * to by @res accordingly. If the registration of the GSI is not successful,
  296. * IORESOURCE_DISABLED will be set it that object's flags.
  297. */
  298. bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
  299. struct resource *res)
  300. {
  301. struct acpi_resource_irq *irq;
  302. struct acpi_resource_extended_irq *ext_irq;
  303. switch (ares->type) {
  304. case ACPI_RESOURCE_TYPE_IRQ:
  305. /*
  306. * Per spec, only one interrupt per descriptor is allowed in
  307. * _CRS, but some firmware violates this, so parse them all.
  308. */
  309. irq = &ares->data.irq;
  310. if (index >= irq->interrupt_count) {
  311. acpi_dev_irqresource_disabled(res, 0);
  312. return false;
  313. }
  314. acpi_dev_get_irqresource(res, irq->interrupts[index],
  315. irq->triggering, irq->polarity,
  316. irq->sharable, true);
  317. break;
  318. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  319. ext_irq = &ares->data.extended_irq;
  320. if (index >= ext_irq->interrupt_count) {
  321. acpi_dev_irqresource_disabled(res, 0);
  322. return false;
  323. }
  324. acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
  325. ext_irq->triggering, ext_irq->polarity,
  326. ext_irq->sharable, false);
  327. break;
  328. default:
  329. return false;
  330. }
  331. return true;
  332. }
  333. EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
  334. /**
  335. * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
  336. * @list: The head of the resource list to free.
  337. */
  338. void acpi_dev_free_resource_list(struct list_head *list)
  339. {
  340. struct resource_list_entry *rentry, *re;
  341. list_for_each_entry_safe(rentry, re, list, node) {
  342. list_del(&rentry->node);
  343. kfree(rentry);
  344. }
  345. }
  346. EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
  347. struct res_proc_context {
  348. struct list_head *list;
  349. int (*preproc)(struct acpi_resource *, void *);
  350. void *preproc_data;
  351. int count;
  352. int error;
  353. };
  354. static acpi_status acpi_dev_new_resource_entry(struct resource *r,
  355. struct res_proc_context *c)
  356. {
  357. struct resource_list_entry *rentry;
  358. rentry = kmalloc(sizeof(*rentry), GFP_KERNEL);
  359. if (!rentry) {
  360. c->error = -ENOMEM;
  361. return AE_NO_MEMORY;
  362. }
  363. rentry->res = *r;
  364. list_add_tail(&rentry->node, c->list);
  365. c->count++;
  366. return AE_OK;
  367. }
  368. static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
  369. void *context)
  370. {
  371. struct res_proc_context *c = context;
  372. struct resource r;
  373. int i;
  374. if (c->preproc) {
  375. int ret;
  376. ret = c->preproc(ares, c->preproc_data);
  377. if (ret < 0) {
  378. c->error = ret;
  379. return AE_CTRL_TERMINATE;
  380. } else if (ret > 0) {
  381. return AE_OK;
  382. }
  383. }
  384. memset(&r, 0, sizeof(r));
  385. if (acpi_dev_resource_memory(ares, &r)
  386. || acpi_dev_resource_io(ares, &r)
  387. || acpi_dev_resource_address_space(ares, &r)
  388. || acpi_dev_resource_ext_address_space(ares, &r))
  389. return acpi_dev_new_resource_entry(&r, c);
  390. for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) {
  391. acpi_status status;
  392. status = acpi_dev_new_resource_entry(&r, c);
  393. if (ACPI_FAILURE(status))
  394. return status;
  395. }
  396. return AE_OK;
  397. }
  398. /**
  399. * acpi_dev_get_resources - Get current resources of a device.
  400. * @adev: ACPI device node to get the resources for.
  401. * @list: Head of the resultant list of resources (must be empty).
  402. * @preproc: The caller's preprocessing routine.
  403. * @preproc_data: Pointer passed to the caller's preprocessing routine.
  404. *
  405. * Evaluate the _CRS method for the given device node and process its output by
  406. * (1) executing the @preproc() rountine provided by the caller, passing the
  407. * resource pointer and @preproc_data to it as arguments, for each ACPI resource
  408. * returned and (2) converting all of the returned ACPI resources into struct
  409. * resource objects if possible. If the return value of @preproc() in step (1)
  410. * is different from 0, step (2) is not applied to the given ACPI resource and
  411. * if that value is negative, the whole processing is aborted and that value is
  412. * returned as the final error code.
  413. *
  414. * The resultant struct resource objects are put on the list pointed to by
  415. * @list, that must be empty initially, as members of struct resource_list_entry
  416. * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
  417. * free that list.
  418. *
  419. * The number of resources in the output list is returned on success, an error
  420. * code reflecting the error condition is returned otherwise.
  421. */
  422. int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
  423. int (*preproc)(struct acpi_resource *, void *),
  424. void *preproc_data)
  425. {
  426. struct res_proc_context c;
  427. acpi_status status;
  428. if (!adev || !adev->handle || !list_empty(list))
  429. return -EINVAL;
  430. if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
  431. return 0;
  432. c.list = list;
  433. c.preproc = preproc;
  434. c.preproc_data = preproc_data;
  435. c.count = 0;
  436. c.error = 0;
  437. status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
  438. acpi_dev_process_resource, &c);
  439. if (ACPI_FAILURE(status)) {
  440. acpi_dev_free_resource_list(list);
  441. return c.error ? c.error : -EIO;
  442. }
  443. return c.count;
  444. }
  445. EXPORT_SYMBOL_GPL(acpi_dev_get_resources);