utils.c 17 KB

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