evmisc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /******************************************************************************
  2. *
  3. * Module Name: evmisc - Miscellaneous event manager support functions
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include <acpi/acevents.h>
  44. #include <acpi/acnamesp.h>
  45. #include <acpi/acinterp.h>
  46. #define _COMPONENT ACPI_EVENTS
  47. ACPI_MODULE_NAME("evmisc")
  48. /* Names for Notify() values, used for debug output */
  49. #ifdef ACPI_DEBUG_OUTPUT
  50. static const char *acpi_notify_value_names[] = {
  51. "Bus Check",
  52. "Device Check",
  53. "Device Wake",
  54. "Eject Request",
  55. "Device Check Light",
  56. "Frequency Mismatch",
  57. "Bus Mode Mismatch",
  58. "Power Fault"
  59. };
  60. #endif
  61. /* Pointer to FACS needed for the Global Lock */
  62. static struct acpi_table_facs *facs = NULL;
  63. /* Local prototypes */
  64. static void ACPI_SYSTEM_XFACE acpi_ev_notify_dispatch(void *context);
  65. static u32 acpi_ev_global_lock_handler(void *context);
  66. /*******************************************************************************
  67. *
  68. * FUNCTION: acpi_ev_is_notify_object
  69. *
  70. * PARAMETERS: Node - Node to check
  71. *
  72. * RETURN: TRUE if notifies allowed on this object
  73. *
  74. * DESCRIPTION: Check type of node for a object that supports notifies.
  75. *
  76. * TBD: This could be replaced by a flag bit in the node.
  77. *
  78. ******************************************************************************/
  79. u8 acpi_ev_is_notify_object(struct acpi_namespace_node *node)
  80. {
  81. switch (node->type) {
  82. case ACPI_TYPE_DEVICE:
  83. case ACPI_TYPE_PROCESSOR:
  84. case ACPI_TYPE_POWER:
  85. case ACPI_TYPE_THERMAL:
  86. /*
  87. * These are the ONLY objects that can receive ACPI notifications
  88. */
  89. return (TRUE);
  90. default:
  91. return (FALSE);
  92. }
  93. }
  94. /*******************************************************************************
  95. *
  96. * FUNCTION: acpi_ev_queue_notify_request
  97. *
  98. * PARAMETERS: Node - NS node for the notified object
  99. * notify_value - Value from the Notify() request
  100. *
  101. * RETURN: Status
  102. *
  103. * DESCRIPTION: Dispatch a device notification event to a previously
  104. * installed handler.
  105. *
  106. ******************************************************************************/
  107. acpi_status
  108. acpi_ev_queue_notify_request(struct acpi_namespace_node * node,
  109. u32 notify_value)
  110. {
  111. union acpi_operand_object *obj_desc;
  112. union acpi_operand_object *handler_obj = NULL;
  113. union acpi_generic_state *notify_info;
  114. acpi_status status = AE_OK;
  115. ACPI_FUNCTION_NAME(ev_queue_notify_request);
  116. /*
  117. * For value 3 (Ejection Request), some device method may need to be run.
  118. * For value 2 (Device Wake) if _PRW exists, the _PS0 method may need
  119. * to be run.
  120. * For value 0x80 (Status Change) on the power button or sleep button,
  121. * initiate soft-off or sleep operation?
  122. */
  123. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  124. "Dispatching Notify(%X) on node %p\n", notify_value,
  125. node));
  126. if (notify_value <= 7) {
  127. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notify value: %s\n",
  128. acpi_notify_value_names[notify_value]));
  129. } else {
  130. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  131. "Notify value: 0x%2.2X **Device Specific**\n",
  132. notify_value));
  133. }
  134. /* Get the notify object attached to the NS Node */
  135. obj_desc = acpi_ns_get_attached_object(node);
  136. if (obj_desc) {
  137. /* We have the notify object, Get the right handler */
  138. switch (node->type) {
  139. case ACPI_TYPE_DEVICE:
  140. case ACPI_TYPE_THERMAL:
  141. case ACPI_TYPE_PROCESSOR:
  142. case ACPI_TYPE_POWER:
  143. if (notify_value <= ACPI_MAX_SYS_NOTIFY) {
  144. handler_obj =
  145. obj_desc->common_notify.system_notify;
  146. } else {
  147. handler_obj =
  148. obj_desc->common_notify.device_notify;
  149. }
  150. break;
  151. default:
  152. /* All other types are not supported */
  153. return (AE_TYPE);
  154. }
  155. }
  156. /* If there is any handler to run, schedule the dispatcher */
  157. if ((acpi_gbl_system_notify.handler
  158. && (notify_value <= ACPI_MAX_SYS_NOTIFY))
  159. || (acpi_gbl_device_notify.handler
  160. && (notify_value > ACPI_MAX_SYS_NOTIFY)) || handler_obj) {
  161. notify_info = acpi_ut_create_generic_state();
  162. if (!notify_info) {
  163. return (AE_NO_MEMORY);
  164. }
  165. notify_info->common.descriptor_type =
  166. ACPI_DESC_TYPE_STATE_NOTIFY;
  167. notify_info->notify.node = node;
  168. notify_info->notify.value = (u16) notify_value;
  169. notify_info->notify.handler_obj = handler_obj;
  170. status =
  171. acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_notify_dispatch,
  172. notify_info);
  173. if (ACPI_FAILURE(status)) {
  174. acpi_ut_delete_generic_state(notify_info);
  175. }
  176. }
  177. if (!handler_obj) {
  178. /*
  179. * There is no per-device notify handler for this device.
  180. * This may or may not be a problem.
  181. */
  182. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  183. "No notify handler for Notify(%4.4s, %X) node %p\n",
  184. acpi_ut_get_node_name(node), notify_value,
  185. node));
  186. }
  187. return (status);
  188. }
  189. /*******************************************************************************
  190. *
  191. * FUNCTION: acpi_ev_notify_dispatch
  192. *
  193. * PARAMETERS: Context - To be passed to the notify handler
  194. *
  195. * RETURN: None.
  196. *
  197. * DESCRIPTION: Dispatch a device notification event to a previously
  198. * installed handler.
  199. *
  200. ******************************************************************************/
  201. static void ACPI_SYSTEM_XFACE acpi_ev_notify_dispatch(void *context)
  202. {
  203. union acpi_generic_state *notify_info =
  204. (union acpi_generic_state *)context;
  205. acpi_notify_handler global_handler = NULL;
  206. void *global_context = NULL;
  207. union acpi_operand_object *handler_obj;
  208. ACPI_FUNCTION_ENTRY();
  209. /*
  210. * We will invoke a global notify handler if installed.
  211. * This is done _before_ we invoke the per-device handler attached
  212. * to the device.
  213. */
  214. if (notify_info->notify.value <= ACPI_MAX_SYS_NOTIFY) {
  215. /* Global system notification handler */
  216. if (acpi_gbl_system_notify.handler) {
  217. global_handler = acpi_gbl_system_notify.handler;
  218. global_context = acpi_gbl_system_notify.context;
  219. }
  220. } else {
  221. /* Global driver notification handler */
  222. if (acpi_gbl_device_notify.handler) {
  223. global_handler = acpi_gbl_device_notify.handler;
  224. global_context = acpi_gbl_device_notify.context;
  225. }
  226. }
  227. /* Invoke the system handler first, if present */
  228. if (global_handler) {
  229. global_handler(notify_info->notify.node,
  230. notify_info->notify.value, global_context);
  231. }
  232. /* Now invoke the per-device handler, if present */
  233. handler_obj = notify_info->notify.handler_obj;
  234. if (handler_obj) {
  235. handler_obj->notify.handler(notify_info->notify.node,
  236. notify_info->notify.value,
  237. handler_obj->notify.context);
  238. }
  239. /* All done with the info object */
  240. acpi_ut_delete_generic_state(notify_info);
  241. }
  242. /*******************************************************************************
  243. *
  244. * FUNCTION: acpi_ev_global_lock_handler
  245. *
  246. * PARAMETERS: Context - From thread interface, not used
  247. *
  248. * RETURN: ACPI_INTERRUPT_HANDLED
  249. *
  250. * DESCRIPTION: Invoked directly from the SCI handler when a global lock
  251. * release interrupt occurs. Attempt to acquire the global lock,
  252. * if successful, signal the thread waiting for the lock.
  253. *
  254. * NOTE: Assumes that the semaphore can be signaled from interrupt level. If
  255. * this is not possible for some reason, a separate thread will have to be
  256. * scheduled to do this.
  257. *
  258. ******************************************************************************/
  259. static u32 acpi_ev_global_lock_handler(void *context)
  260. {
  261. u8 acquired = FALSE;
  262. /*
  263. * Attempt to get the lock.
  264. *
  265. * If we don't get it now, it will be marked pending and we will
  266. * take another interrupt when it becomes free.
  267. */
  268. ACPI_ACQUIRE_GLOBAL_LOCK(facs, acquired);
  269. if (acquired) {
  270. /* Got the lock, now wake all threads waiting for it */
  271. acpi_gbl_global_lock_acquired = TRUE;
  272. /* Send a unit to the semaphore */
  273. if (ACPI_FAILURE(acpi_os_signal_semaphore(
  274. acpi_gbl_global_lock_semaphore, 1))) {
  275. ACPI_ERROR((AE_INFO,
  276. "Could not signal Global Lock semaphore"));
  277. }
  278. }
  279. return (ACPI_INTERRUPT_HANDLED);
  280. }
  281. /*******************************************************************************
  282. *
  283. * FUNCTION: acpi_ev_init_global_lock_handler
  284. *
  285. * PARAMETERS: None
  286. *
  287. * RETURN: Status
  288. *
  289. * DESCRIPTION: Install a handler for the global lock release event
  290. *
  291. ******************************************************************************/
  292. acpi_status acpi_ev_init_global_lock_handler(void)
  293. {
  294. acpi_status status;
  295. ACPI_FUNCTION_TRACE(ev_init_global_lock_handler);
  296. status =
  297. acpi_get_table(ACPI_SIG_FACS, 0,
  298. (struct acpi_table_header **)&facs);
  299. if (ACPI_FAILURE(status)) {
  300. return_ACPI_STATUS(status);
  301. }
  302. acpi_gbl_global_lock_present = TRUE;
  303. status = acpi_install_fixed_event_handler(ACPI_EVENT_GLOBAL,
  304. acpi_ev_global_lock_handler,
  305. NULL);
  306. /*
  307. * If the global lock does not exist on this platform, the attempt
  308. * to enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick)
  309. * Map to AE_OK, but mark global lock as not present.
  310. * Any attempt to actually use the global lock will be flagged
  311. * with an error.
  312. */
  313. if (status == AE_NO_HARDWARE_RESPONSE) {
  314. ACPI_ERROR((AE_INFO,
  315. "No response from Global Lock hardware, disabling lock"));
  316. acpi_gbl_global_lock_present = FALSE;
  317. status = AE_OK;
  318. }
  319. return_ACPI_STATUS(status);
  320. }
  321. /******************************************************************************
  322. *
  323. * FUNCTION: acpi_ev_acquire_global_lock
  324. *
  325. * PARAMETERS: Timeout - Max time to wait for the lock, in millisec.
  326. *
  327. * RETURN: Status
  328. *
  329. * DESCRIPTION: Attempt to gain ownership of the Global Lock.
  330. *
  331. * MUTEX: Interpreter must be locked
  332. *
  333. * Note: The original implementation allowed multiple threads to "acquire" the
  334. * Global Lock, and the OS would hold the lock until the last thread had
  335. * released it. However, this could potentially starve the BIOS out of the
  336. * lock, especially in the case where there is a tight handshake between the
  337. * Embedded Controller driver and the BIOS. Therefore, this implementation
  338. * allows only one thread to acquire the HW Global Lock at a time, and makes
  339. * the global lock appear as a standard mutex on the OS side.
  340. *
  341. *****************************************************************************/
  342. acpi_status acpi_ev_acquire_global_lock(u16 timeout)
  343. {
  344. acpi_status status = AE_OK;
  345. u8 acquired = FALSE;
  346. ACPI_FUNCTION_TRACE(ev_acquire_global_lock);
  347. /*
  348. * Only one thread can acquire the GL at a time, the global_lock_mutex
  349. * enforces this. This interface releases the interpreter if we must wait.
  350. */
  351. status = acpi_ex_system_wait_mutex(acpi_gbl_global_lock_mutex, timeout);
  352. if (ACPI_FAILURE(status)) {
  353. return_ACPI_STATUS(status);
  354. }
  355. /*
  356. * Make sure that a global lock actually exists. If not, just treat
  357. * the lock as a standard mutex.
  358. */
  359. if (!acpi_gbl_global_lock_present) {
  360. acpi_gbl_global_lock_acquired = TRUE;
  361. return_ACPI_STATUS(AE_OK);
  362. }
  363. /* Attempt to acquire the actual hardware lock */
  364. ACPI_ACQUIRE_GLOBAL_LOCK(facs, acquired);
  365. if (acquired) {
  366. /* We got the lock */
  367. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  368. "Acquired hardware Global Lock\n"));
  369. acpi_gbl_global_lock_acquired = TRUE;
  370. return_ACPI_STATUS(AE_OK);
  371. }
  372. /*
  373. * Did not get the lock. The pending bit was set above, and we must now
  374. * wait until we get the global lock released interrupt.
  375. */
  376. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Waiting for hardware Global Lock\n"));
  377. /*
  378. * Wait for handshake with the global lock interrupt handler.
  379. * This interface releases the interpreter if we must wait.
  380. */
  381. status = acpi_ex_system_wait_semaphore(acpi_gbl_global_lock_semaphore,
  382. ACPI_WAIT_FOREVER);
  383. return_ACPI_STATUS(status);
  384. }
  385. /*******************************************************************************
  386. *
  387. * FUNCTION: acpi_ev_release_global_lock
  388. *
  389. * PARAMETERS: None
  390. *
  391. * RETURN: Status
  392. *
  393. * DESCRIPTION: Releases ownership of the Global Lock.
  394. *
  395. ******************************************************************************/
  396. acpi_status acpi_ev_release_global_lock(void)
  397. {
  398. u8 pending = FALSE;
  399. acpi_status status = AE_OK;
  400. ACPI_FUNCTION_TRACE(ev_release_global_lock);
  401. /* Lock must be already acquired */
  402. if (!acpi_gbl_global_lock_acquired) {
  403. ACPI_WARNING((AE_INFO,
  404. "Cannot release the ACPI Global Lock, it has not been acquired"));
  405. return_ACPI_STATUS(AE_NOT_ACQUIRED);
  406. }
  407. if (acpi_gbl_global_lock_present) {
  408. /* Allow any thread to release the lock */
  409. ACPI_RELEASE_GLOBAL_LOCK(facs, pending);
  410. /*
  411. * If the pending bit was set, we must write GBL_RLS to the control
  412. * register
  413. */
  414. if (pending) {
  415. status =
  416. acpi_set_register(ACPI_BITREG_GLOBAL_LOCK_RELEASE,
  417. 1);
  418. }
  419. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  420. "Released hardware Global Lock\n"));
  421. }
  422. acpi_gbl_global_lock_acquired = FALSE;
  423. /* Release the local GL mutex */
  424. acpi_os_release_mutex(acpi_gbl_global_lock_mutex);
  425. return_ACPI_STATUS(status);
  426. }
  427. /******************************************************************************
  428. *
  429. * FUNCTION: acpi_ev_terminate
  430. *
  431. * PARAMETERS: none
  432. *
  433. * RETURN: none
  434. *
  435. * DESCRIPTION: Disable events and free memory allocated for table storage.
  436. *
  437. ******************************************************************************/
  438. void acpi_ev_terminate(void)
  439. {
  440. acpi_native_uint i;
  441. acpi_status status;
  442. ACPI_FUNCTION_TRACE(ev_terminate);
  443. if (acpi_gbl_events_initialized) {
  444. /*
  445. * Disable all event-related functionality.
  446. * In all cases, on error, print a message but obviously we don't abort.
  447. */
  448. /* Disable all fixed events */
  449. for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++) {
  450. status = acpi_disable_event((u32) i, 0);
  451. if (ACPI_FAILURE(status)) {
  452. ACPI_ERROR((AE_INFO,
  453. "Could not disable fixed event %d",
  454. (u32) i));
  455. }
  456. }
  457. /* Disable all GPEs in all GPE blocks */
  458. status = acpi_ev_walk_gpe_list(acpi_hw_disable_gpe_block);
  459. /* Remove SCI handler */
  460. status = acpi_ev_remove_sci_handler();
  461. if (ACPI_FAILURE(status)) {
  462. ACPI_ERROR((AE_INFO, "Could not remove SCI handler"));
  463. }
  464. }
  465. /* Deallocate all handler objects installed within GPE info structs */
  466. status = acpi_ev_walk_gpe_list(acpi_ev_delete_gpe_handlers);
  467. /* Return to original mode if necessary */
  468. if (acpi_gbl_original_mode == ACPI_SYS_MODE_LEGACY) {
  469. status = acpi_disable();
  470. if (ACPI_FAILURE(status)) {
  471. ACPI_WARNING((AE_INFO, "AcpiDisable failed"));
  472. }
  473. }
  474. return_VOID;
  475. }