resource.c 23 KB

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