resource.c 15 KB

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