utils.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@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 as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/acpi.h>
  32. #include "internal.h"
  33. #define _COMPONENT ACPI_BUS_COMPONENT
  34. ACPI_MODULE_NAME("utils");
  35. /* --------------------------------------------------------------------------
  36. Object Evaluation Helpers
  37. -------------------------------------------------------------------------- */
  38. static void
  39. acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
  40. {
  41. #ifdef ACPI_DEBUG_OUTPUT
  42. char prefix[80] = {'\0'};
  43. struct acpi_buffer buffer = {sizeof(prefix), prefix};
  44. acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
  45. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
  46. (char *) prefix, p, acpi_format_exception(s)));
  47. #else
  48. return;
  49. #endif
  50. }
  51. acpi_status
  52. acpi_extract_package(union acpi_object *package,
  53. struct acpi_buffer *format, struct acpi_buffer *buffer)
  54. {
  55. u32 size_required = 0;
  56. u32 tail_offset = 0;
  57. char *format_string = NULL;
  58. u32 format_count = 0;
  59. u32 i = 0;
  60. u8 *head = NULL;
  61. u8 *tail = NULL;
  62. if (!package || (package->type != ACPI_TYPE_PACKAGE)
  63. || (package->package.count < 1)) {
  64. printk(KERN_WARNING PREFIX "Invalid package argument\n");
  65. return AE_BAD_PARAMETER;
  66. }
  67. if (!format || !format->pointer || (format->length < 1)) {
  68. printk(KERN_WARNING PREFIX "Invalid format argument\n");
  69. return AE_BAD_PARAMETER;
  70. }
  71. if (!buffer) {
  72. printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
  73. return AE_BAD_PARAMETER;
  74. }
  75. format_count = (format->length / sizeof(char)) - 1;
  76. if (format_count > package->package.count) {
  77. printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
  78. " than exist in package [%d].\n",
  79. format_count, package->package.count);
  80. return AE_BAD_DATA;
  81. }
  82. format_string = format->pointer;
  83. /*
  84. * Calculate size_required.
  85. */
  86. for (i = 0; i < format_count; i++) {
  87. union acpi_object *element = &(package->package.elements[i]);
  88. switch (element->type) {
  89. case ACPI_TYPE_INTEGER:
  90. switch (format_string[i]) {
  91. case 'N':
  92. size_required += sizeof(u64);
  93. tail_offset += sizeof(u64);
  94. break;
  95. case 'S':
  96. size_required +=
  97. sizeof(char *) + sizeof(u64) +
  98. sizeof(char);
  99. tail_offset += sizeof(char *);
  100. break;
  101. default:
  102. printk(KERN_WARNING PREFIX "Invalid package element"
  103. " [%d]: got number, expecting"
  104. " [%c]\n",
  105. i, format_string[i]);
  106. return AE_BAD_DATA;
  107. break;
  108. }
  109. break;
  110. case ACPI_TYPE_STRING:
  111. case ACPI_TYPE_BUFFER:
  112. switch (format_string[i]) {
  113. case 'S':
  114. size_required +=
  115. sizeof(char *) +
  116. (element->string.length * sizeof(char)) +
  117. sizeof(char);
  118. tail_offset += sizeof(char *);
  119. break;
  120. case 'B':
  121. size_required +=
  122. sizeof(u8 *) +
  123. (element->buffer.length * sizeof(u8));
  124. tail_offset += sizeof(u8 *);
  125. break;
  126. default:
  127. printk(KERN_WARNING PREFIX "Invalid package element"
  128. " [%d] got string/buffer,"
  129. " expecting [%c]\n",
  130. i, format_string[i]);
  131. return AE_BAD_DATA;
  132. break;
  133. }
  134. break;
  135. case ACPI_TYPE_PACKAGE:
  136. default:
  137. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  138. "Found unsupported element at index=%d\n",
  139. i));
  140. /* TBD: handle nested packages... */
  141. return AE_SUPPORT;
  142. break;
  143. }
  144. }
  145. /*
  146. * Validate output buffer.
  147. */
  148. if (buffer->length == ACPI_ALLOCATE_BUFFER) {
  149. buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
  150. if (!buffer->pointer)
  151. return AE_NO_MEMORY;
  152. buffer->length = size_required;
  153. } else {
  154. if (buffer->length < size_required) {
  155. buffer->length = size_required;
  156. return AE_BUFFER_OVERFLOW;
  157. } else if (buffer->length != size_required ||
  158. !buffer->pointer) {
  159. return AE_BAD_PARAMETER;
  160. }
  161. }
  162. head = buffer->pointer;
  163. tail = buffer->pointer + tail_offset;
  164. /*
  165. * Extract package data.
  166. */
  167. for (i = 0; i < format_count; i++) {
  168. u8 **pointer = NULL;
  169. union acpi_object *element = &(package->package.elements[i]);
  170. if (!element) {
  171. return AE_BAD_DATA;
  172. }
  173. switch (element->type) {
  174. case ACPI_TYPE_INTEGER:
  175. switch (format_string[i]) {
  176. case 'N':
  177. *((u64 *) head) =
  178. element->integer.value;
  179. head += sizeof(u64);
  180. break;
  181. case 'S':
  182. pointer = (u8 **) head;
  183. *pointer = tail;
  184. *((u64 *) tail) =
  185. element->integer.value;
  186. head += sizeof(u64 *);
  187. tail += sizeof(u64);
  188. /* NULL terminate string */
  189. *tail = (char)0;
  190. tail += sizeof(char);
  191. break;
  192. default:
  193. /* Should never get here */
  194. break;
  195. }
  196. break;
  197. case ACPI_TYPE_STRING:
  198. case ACPI_TYPE_BUFFER:
  199. switch (format_string[i]) {
  200. case 'S':
  201. pointer = (u8 **) head;
  202. *pointer = tail;
  203. memcpy(tail, element->string.pointer,
  204. element->string.length);
  205. head += sizeof(char *);
  206. tail += element->string.length * sizeof(char);
  207. /* NULL terminate string */
  208. *tail = (char)0;
  209. tail += sizeof(char);
  210. break;
  211. case 'B':
  212. pointer = (u8 **) head;
  213. *pointer = tail;
  214. memcpy(tail, element->buffer.pointer,
  215. element->buffer.length);
  216. head += sizeof(u8 *);
  217. tail += element->buffer.length * sizeof(u8);
  218. break;
  219. default:
  220. /* Should never get here */
  221. break;
  222. }
  223. break;
  224. case ACPI_TYPE_PACKAGE:
  225. /* TBD: handle nested packages... */
  226. default:
  227. /* Should never get here */
  228. break;
  229. }
  230. }
  231. return AE_OK;
  232. }
  233. EXPORT_SYMBOL(acpi_extract_package);
  234. acpi_status
  235. acpi_evaluate_integer(acpi_handle handle,
  236. acpi_string pathname,
  237. struct acpi_object_list *arguments, unsigned long long *data)
  238. {
  239. acpi_status status = AE_OK;
  240. union acpi_object element;
  241. struct acpi_buffer buffer = { 0, NULL };
  242. if (!data)
  243. return AE_BAD_PARAMETER;
  244. buffer.length = sizeof(union acpi_object);
  245. buffer.pointer = &element;
  246. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  247. if (ACPI_FAILURE(status)) {
  248. acpi_util_eval_error(handle, pathname, status);
  249. return status;
  250. }
  251. if (element.type != ACPI_TYPE_INTEGER) {
  252. acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
  253. return AE_BAD_DATA;
  254. }
  255. *data = element.integer.value;
  256. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
  257. return AE_OK;
  258. }
  259. EXPORT_SYMBOL(acpi_evaluate_integer);
  260. acpi_status
  261. acpi_evaluate_reference(acpi_handle handle,
  262. acpi_string pathname,
  263. struct acpi_object_list *arguments,
  264. struct acpi_handle_list *list)
  265. {
  266. acpi_status status = AE_OK;
  267. union acpi_object *package = NULL;
  268. union acpi_object *element = NULL;
  269. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  270. u32 i = 0;
  271. if (!list) {
  272. return AE_BAD_PARAMETER;
  273. }
  274. /* Evaluate object. */
  275. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  276. if (ACPI_FAILURE(status))
  277. goto end;
  278. package = buffer.pointer;
  279. if ((buffer.length == 0) || !package) {
  280. printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
  281. (unsigned)buffer.length, package);
  282. status = AE_BAD_DATA;
  283. acpi_util_eval_error(handle, pathname, status);
  284. goto end;
  285. }
  286. if (package->type != ACPI_TYPE_PACKAGE) {
  287. printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
  288. package->type);
  289. status = AE_BAD_DATA;
  290. acpi_util_eval_error(handle, pathname, status);
  291. goto end;
  292. }
  293. if (!package->package.count) {
  294. printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
  295. package);
  296. status = AE_BAD_DATA;
  297. acpi_util_eval_error(handle, pathname, status);
  298. goto end;
  299. }
  300. if (package->package.count > ACPI_MAX_HANDLES) {
  301. return AE_NO_MEMORY;
  302. }
  303. list->count = package->package.count;
  304. /* Extract package data. */
  305. for (i = 0; i < list->count; i++) {
  306. element = &(package->package.elements[i]);
  307. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  308. status = AE_BAD_DATA;
  309. printk(KERN_ERR PREFIX
  310. "Expecting a [Reference] package element, found type %X\n",
  311. element->type);
  312. acpi_util_eval_error(handle, pathname, status);
  313. break;
  314. }
  315. if (!element->reference.handle) {
  316. printk(KERN_WARNING PREFIX "Invalid reference in"
  317. " package %s\n", pathname);
  318. status = AE_NULL_ENTRY;
  319. break;
  320. }
  321. /* Get the acpi_handle. */
  322. list->handles[i] = element->reference.handle;
  323. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
  324. list->handles[i]));
  325. }
  326. end:
  327. if (ACPI_FAILURE(status)) {
  328. list->count = 0;
  329. //kfree(list->handles);
  330. }
  331. kfree(buffer.pointer);
  332. return status;
  333. }
  334. EXPORT_SYMBOL(acpi_evaluate_reference);
  335. acpi_status
  336. acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
  337. {
  338. acpi_status status;
  339. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  340. union acpi_object *output;
  341. status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
  342. if (ACPI_FAILURE(status))
  343. return status;
  344. output = buffer.pointer;
  345. if (!output || output->type != ACPI_TYPE_PACKAGE
  346. || !output->package.count
  347. || output->package.elements[0].type != ACPI_TYPE_BUFFER
  348. || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
  349. status = AE_TYPE;
  350. goto out;
  351. }
  352. status = acpi_decode_pld_buffer(
  353. output->package.elements[0].buffer.pointer,
  354. output->package.elements[0].buffer.length,
  355. pld);
  356. out:
  357. kfree(buffer.pointer);
  358. return status;
  359. }
  360. EXPORT_SYMBOL(acpi_get_physical_device_location);
  361. /**
  362. * acpi_evaluate_ost: Evaluate _OST for hotplug operations
  363. * @handle: ACPI device handle
  364. * @source_event: source event code
  365. * @status_code: status code
  366. * @status_buf: optional detailed information (NULL if none)
  367. *
  368. * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
  369. * must call this function when evaluating _OST for hotplug operations.
  370. * When the platform does not support _OST, this function has no effect.
  371. */
  372. acpi_status
  373. acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
  374. struct acpi_buffer *status_buf)
  375. {
  376. union acpi_object params[3] = {
  377. {.type = ACPI_TYPE_INTEGER,},
  378. {.type = ACPI_TYPE_INTEGER,},
  379. {.type = ACPI_TYPE_BUFFER,}
  380. };
  381. struct acpi_object_list arg_list = {3, params};
  382. params[0].integer.value = source_event;
  383. params[1].integer.value = status_code;
  384. if (status_buf != NULL) {
  385. params[2].buffer.pointer = status_buf->pointer;
  386. params[2].buffer.length = status_buf->length;
  387. } else {
  388. params[2].buffer.pointer = NULL;
  389. params[2].buffer.length = 0;
  390. }
  391. return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
  392. }
  393. EXPORT_SYMBOL(acpi_evaluate_ost);
  394. /**
  395. * acpi_handle_printk: Print message with ACPI prefix and object path
  396. *
  397. * This function is called through acpi_handle_<level> macros and prints
  398. * a message with ACPI prefix and object path. This function acquires
  399. * the global namespace mutex to obtain an object path. In interrupt
  400. * context, it shows the object path as <n/a>.
  401. */
  402. void
  403. acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
  404. {
  405. struct va_format vaf;
  406. va_list args;
  407. struct acpi_buffer buffer = {
  408. .length = ACPI_ALLOCATE_BUFFER,
  409. .pointer = NULL
  410. };
  411. const char *path;
  412. va_start(args, fmt);
  413. vaf.fmt = fmt;
  414. vaf.va = &args;
  415. if (in_interrupt() ||
  416. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
  417. path = "<n/a>";
  418. else
  419. path = buffer.pointer;
  420. printk("%sACPI: %s: %pV", level, path, &vaf);
  421. va_end(args);
  422. kfree(buffer.pointer);
  423. }
  424. EXPORT_SYMBOL(acpi_handle_printk);
  425. /**
  426. * acpi_has_method: Check whether @handle has a method named @name
  427. * @handle: ACPI device handle
  428. * @name: name of object or method
  429. *
  430. * Check whether @handle has a method named @name.
  431. */
  432. bool acpi_has_method(acpi_handle handle, char *name)
  433. {
  434. acpi_handle tmp;
  435. return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
  436. }
  437. EXPORT_SYMBOL(acpi_has_method);
  438. acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
  439. u64 arg)
  440. {
  441. union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
  442. struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
  443. obj.integer.value = arg;
  444. return acpi_evaluate_object(handle, method, &arg_list, NULL);
  445. }
  446. EXPORT_SYMBOL(acpi_execute_simple_method);
  447. /**
  448. * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
  449. * @handle: ACPI device handle
  450. *
  451. * Evaluate device's _EJ0 method for hotplug operations.
  452. */
  453. acpi_status acpi_evaluate_ej0(acpi_handle handle)
  454. {
  455. acpi_status status;
  456. status = acpi_execute_simple_method(handle, "_EJ0", 1);
  457. if (status == AE_NOT_FOUND)
  458. acpi_handle_warn(handle, "No _EJ0 support for device\n");
  459. else if (ACPI_FAILURE(status))
  460. acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
  461. return status;
  462. }
  463. /**
  464. * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
  465. * @handle: ACPI device handle
  466. * @lock: lock device if non-zero, otherwise unlock device
  467. *
  468. * Evaluate device's _LCK method if present to lock/unlock device
  469. */
  470. acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
  471. {
  472. acpi_status status;
  473. status = acpi_execute_simple_method(handle, "_LCK", !!lock);
  474. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  475. if (lock)
  476. acpi_handle_warn(handle,
  477. "Locking device failed (0x%x)\n", status);
  478. else
  479. acpi_handle_warn(handle,
  480. "Unlocking device failed (0x%x)\n", status);
  481. }
  482. return status;
  483. }
  484. /**
  485. * acpi_evaluate_dsm - evaluate device's _DSM method
  486. * @handle: ACPI device handle
  487. * @uuid: UUID of requested functions, should be 16 bytes
  488. * @rev: revision number of requested function
  489. * @func: requested function number
  490. * @argv4: the function specific parameter
  491. *
  492. * Evaluate device's _DSM method with specified UUID, revision id and
  493. * function number. Caller needs to free the returned object.
  494. *
  495. * Though ACPI defines the fourth parameter for _DSM should be a package,
  496. * some old BIOSes do expect a buffer or an integer etc.
  497. */
  498. union acpi_object *
  499. acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
  500. union acpi_object *argv4)
  501. {
  502. acpi_status ret;
  503. struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
  504. union acpi_object params[4];
  505. struct acpi_object_list input = {
  506. .count = 4,
  507. .pointer = params,
  508. };
  509. params[0].type = ACPI_TYPE_BUFFER;
  510. params[0].buffer.length = 16;
  511. params[0].buffer.pointer = (char *)uuid;
  512. params[1].type = ACPI_TYPE_INTEGER;
  513. params[1].integer.value = rev;
  514. params[2].type = ACPI_TYPE_INTEGER;
  515. params[2].integer.value = func;
  516. if (argv4) {
  517. params[3] = *argv4;
  518. } else {
  519. params[3].type = ACPI_TYPE_PACKAGE;
  520. params[3].package.count = 0;
  521. params[3].package.elements = NULL;
  522. }
  523. ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
  524. if (ACPI_SUCCESS(ret))
  525. return (union acpi_object *)buf.pointer;
  526. if (ret != AE_NOT_FOUND)
  527. acpi_handle_warn(handle,
  528. "failed to evaluate _DSM (0x%x)\n", ret);
  529. return NULL;
  530. }
  531. EXPORT_SYMBOL(acpi_evaluate_dsm);
  532. /**
  533. * acpi_check_dsm - check if _DSM method supports requested functions.
  534. * @handle: ACPI device handle
  535. * @uuid: UUID of requested functions, should be 16 bytes at least
  536. * @rev: revision number of requested functions
  537. * @funcs: bitmap of requested functions
  538. * @exclude: excluding special value, used to support i915 and nouveau
  539. *
  540. * Evaluate device's _DSM method to check whether it supports requested
  541. * functions. Currently only support 64 functions at maximum, should be
  542. * enough for now.
  543. */
  544. bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
  545. {
  546. int i;
  547. u64 mask = 0;
  548. union acpi_object *obj;
  549. if (funcs == 0)
  550. return false;
  551. obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
  552. if (!obj)
  553. return false;
  554. /* For compatibility, old BIOSes may return an integer */
  555. if (obj->type == ACPI_TYPE_INTEGER)
  556. mask = obj->integer.value;
  557. else if (obj->type == ACPI_TYPE_BUFFER)
  558. for (i = 0; i < obj->buffer.length && i < 8; i++)
  559. mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
  560. ACPI_FREE(obj);
  561. /*
  562. * Bit 0 indicates whether there's support for any functions other than
  563. * function 0 for the specified UUID and revision.
  564. */
  565. if ((mask & 0x1) && (mask & funcs) == funcs)
  566. return true;
  567. return false;
  568. }
  569. EXPORT_SYMBOL(acpi_check_dsm);