resource.c 15 KB

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