evmisc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /******************************************************************************
  2. *
  3. * Module Name: evmisc - Miscellaneous event manager support functions
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, 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. #ifdef ACPI_DEBUG_OUTPUT
  49. static const char *acpi_notify_value_names[] =
  50. {
  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. /* Local prototypes */
  62. static void ACPI_SYSTEM_XFACE
  63. acpi_ev_notify_dispatch (
  64. void *context);
  65. static void ACPI_SYSTEM_XFACE
  66. acpi_ev_global_lock_thread (
  67. void *context);
  68. static u32
  69. acpi_ev_global_lock_handler (
  70. void *context);
  71. /*******************************************************************************
  72. *
  73. * FUNCTION: acpi_ev_is_notify_object
  74. *
  75. * PARAMETERS: Node - Node to check
  76. *
  77. * RETURN: TRUE if notifies allowed on this object
  78. *
  79. * DESCRIPTION: Check type of node for a object that supports notifies.
  80. *
  81. * TBD: This could be replaced by a flag bit in the node.
  82. *
  83. ******************************************************************************/
  84. u8
  85. acpi_ev_is_notify_object (
  86. struct acpi_namespace_node *node)
  87. {
  88. switch (node->type) {
  89. case ACPI_TYPE_DEVICE:
  90. case ACPI_TYPE_PROCESSOR:
  91. case ACPI_TYPE_POWER:
  92. case ACPI_TYPE_THERMAL:
  93. /*
  94. * These are the ONLY objects that can receive ACPI notifications
  95. */
  96. return (TRUE);
  97. default:
  98. return (FALSE);
  99. }
  100. }
  101. /*******************************************************************************
  102. *
  103. * FUNCTION: acpi_ev_queue_notify_request
  104. *
  105. * PARAMETERS: Node - NS node for the notified object
  106. * notify_value - Value from the Notify() request
  107. *
  108. * RETURN: Status
  109. *
  110. * DESCRIPTION: Dispatch a device notification event to a previously
  111. * installed handler.
  112. *
  113. ******************************************************************************/
  114. acpi_status
  115. acpi_ev_queue_notify_request (
  116. struct acpi_namespace_node *node,
  117. u32 notify_value)
  118. {
  119. union acpi_operand_object *obj_desc;
  120. union acpi_operand_object *handler_obj = NULL;
  121. union acpi_generic_state *notify_info;
  122. acpi_status status = AE_OK;
  123. ACPI_FUNCTION_NAME ("ev_queue_notify_request");
  124. /*
  125. * For value 3 (Ejection Request), some device method may need to be run.
  126. * For value 2 (Device Wake) if _PRW exists, the _PS0 method may need
  127. * to be run.
  128. * For value 0x80 (Status Change) on the power button or sleep button,
  129. * initiate soft-off or sleep operation?
  130. */
  131. ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
  132. "Dispatching Notify(%X) on node %p\n", notify_value, node));
  133. if (notify_value <= 7) {
  134. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Notify value: %s\n",
  135. acpi_notify_value_names[notify_value]));
  136. }
  137. else {
  138. ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
  139. "Notify value: 0x%2.2X **Device Specific**\n",
  140. notify_value));
  141. }
  142. /* Get the notify object attached to the NS Node */
  143. obj_desc = acpi_ns_get_attached_object (node);
  144. if (obj_desc) {
  145. /* We have the notify object, Get the right handler */
  146. switch (node->type) {
  147. case ACPI_TYPE_DEVICE:
  148. case ACPI_TYPE_THERMAL:
  149. case ACPI_TYPE_PROCESSOR:
  150. case ACPI_TYPE_POWER:
  151. if (notify_value <= ACPI_MAX_SYS_NOTIFY) {
  152. handler_obj = obj_desc->common_notify.system_notify;
  153. }
  154. else {
  155. handler_obj = obj_desc->common_notify.device_notify;
  156. }
  157. break;
  158. default:
  159. /* All other types are not supported */
  160. return (AE_TYPE);
  161. }
  162. }
  163. /* If there is any handler to run, schedule the dispatcher */
  164. if ((acpi_gbl_system_notify.handler && (notify_value <= ACPI_MAX_SYS_NOTIFY)) ||
  165. (acpi_gbl_device_notify.handler && (notify_value > ACPI_MAX_SYS_NOTIFY)) ||
  166. handler_obj) {
  167. notify_info = acpi_ut_create_generic_state ();
  168. if (!notify_info) {
  169. return (AE_NO_MEMORY);
  170. }
  171. notify_info->common.data_type = ACPI_DESC_TYPE_STATE_NOTIFY;
  172. notify_info->notify.node = node;
  173. notify_info->notify.value = (u16) notify_value;
  174. notify_info->notify.handler_obj = handler_obj;
  175. status = acpi_os_queue_for_execution (OSD_PRIORITY_HIGH,
  176. acpi_ev_notify_dispatch, notify_info);
  177. if (ACPI_FAILURE (status)) {
  178. acpi_ut_delete_generic_state (notify_info);
  179. }
  180. }
  181. if (!handler_obj) {
  182. /*
  183. * There is no per-device notify handler for this device.
  184. * This may or may not be a problem.
  185. */
  186. ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
  187. "No notify handler for Notify(%4.4s, %X) node %p\n",
  188. acpi_ut_get_node_name (node), notify_value, node));
  189. }
  190. return (status);
  191. }
  192. /*******************************************************************************
  193. *
  194. * FUNCTION: acpi_ev_notify_dispatch
  195. *
  196. * PARAMETERS: Context - To be passed to the notify handler
  197. *
  198. * RETURN: None.
  199. *
  200. * DESCRIPTION: Dispatch a device notification event to a previously
  201. * installed handler.
  202. *
  203. ******************************************************************************/
  204. static void ACPI_SYSTEM_XFACE
  205. acpi_ev_notify_dispatch (
  206. void *context)
  207. {
  208. union acpi_generic_state *notify_info = (union acpi_generic_state *) context;
  209. acpi_notify_handler global_handler = NULL;
  210. void *global_context = NULL;
  211. union acpi_operand_object *handler_obj;
  212. ACPI_FUNCTION_ENTRY ();
  213. /*
  214. * We will invoke a global notify handler if installed.
  215. * This is done _before_ we invoke the per-device handler attached
  216. * to the device.
  217. */
  218. if (notify_info->notify.value <= ACPI_MAX_SYS_NOTIFY) {
  219. /* Global system notification handler */
  220. if (acpi_gbl_system_notify.handler) {
  221. global_handler = acpi_gbl_system_notify.handler;
  222. global_context = acpi_gbl_system_notify.context;
  223. }
  224. }
  225. else {
  226. /* Global driver notification handler */
  227. if (acpi_gbl_device_notify.handler) {
  228. global_handler = acpi_gbl_device_notify.handler;
  229. global_context = acpi_gbl_device_notify.context;
  230. }
  231. }
  232. /* Invoke the system handler first, if present */
  233. if (global_handler) {
  234. global_handler (notify_info->notify.node, notify_info->notify.value,
  235. global_context);
  236. }
  237. /* Now invoke the per-device handler, if present */
  238. handler_obj = notify_info->notify.handler_obj;
  239. if (handler_obj) {
  240. handler_obj->notify.handler (notify_info->notify.node,
  241. notify_info->notify.value,
  242. handler_obj->notify.context);
  243. }
  244. /* All done with the info object */
  245. acpi_ut_delete_generic_state (notify_info);
  246. }
  247. /*******************************************************************************
  248. *
  249. * FUNCTION: acpi_ev_global_lock_thread
  250. *
  251. * PARAMETERS: Context - From thread interface, not used
  252. *
  253. * RETURN: None
  254. *
  255. * DESCRIPTION: Invoked by SCI interrupt handler upon acquisition of the
  256. * Global Lock. Simply signal all threads that are waiting
  257. * for the lock.
  258. *
  259. ******************************************************************************/
  260. static void ACPI_SYSTEM_XFACE
  261. acpi_ev_global_lock_thread (
  262. void *context)
  263. {
  264. acpi_status status;
  265. /* Signal threads that are waiting for the lock */
  266. if (acpi_gbl_global_lock_thread_count) {
  267. /* Send sufficient units to the semaphore */
  268. status = acpi_os_signal_semaphore (acpi_gbl_global_lock_semaphore,
  269. acpi_gbl_global_lock_thread_count);
  270. if (ACPI_FAILURE (status)) {
  271. ACPI_REPORT_ERROR (("Could not signal Global Lock semaphore\n"));
  272. }
  273. }
  274. }
  275. /*******************************************************************************
  276. *
  277. * FUNCTION: acpi_ev_global_lock_handler
  278. *
  279. * PARAMETERS: Context - From thread interface, not used
  280. *
  281. * RETURN: ACPI_INTERRUPT_HANDLED or ACPI_INTERRUPT_NOT_HANDLED
  282. *
  283. * DESCRIPTION: Invoked directly from the SCI handler when a global lock
  284. * release interrupt occurs. Grab the global lock and queue
  285. * the global lock thread for execution
  286. *
  287. ******************************************************************************/
  288. static u32
  289. acpi_ev_global_lock_handler (
  290. void *context)
  291. {
  292. u8 acquired = FALSE;
  293. acpi_status status;
  294. /*
  295. * Attempt to get the lock
  296. * If we don't get it now, it will be marked pending and we will
  297. * take another interrupt when it becomes free.
  298. */
  299. ACPI_ACQUIRE_GLOBAL_LOCK (acpi_gbl_common_fACS.global_lock, acquired);
  300. if (acquired) {
  301. /* Got the lock, now wake all threads waiting for it */
  302. acpi_gbl_global_lock_acquired = TRUE;
  303. /* Run the Global Lock thread which will signal all waiting threads */
  304. status = acpi_os_queue_for_execution (OSD_PRIORITY_HIGH,
  305. acpi_ev_global_lock_thread, context);
  306. if (ACPI_FAILURE (status)) {
  307. ACPI_REPORT_ERROR (("Could not queue Global Lock thread, %s\n",
  308. acpi_format_exception (status)));
  309. return (ACPI_INTERRUPT_NOT_HANDLED);
  310. }
  311. }
  312. return (ACPI_INTERRUPT_HANDLED);
  313. }
  314. /*******************************************************************************
  315. *
  316. * FUNCTION: acpi_ev_init_global_lock_handler
  317. *
  318. * PARAMETERS: None
  319. *
  320. * RETURN: Status
  321. *
  322. * DESCRIPTION: Install a handler for the global lock release event
  323. *
  324. ******************************************************************************/
  325. acpi_status
  326. acpi_ev_init_global_lock_handler (
  327. void)
  328. {
  329. acpi_status status;
  330. ACPI_FUNCTION_TRACE ("ev_init_global_lock_handler");
  331. acpi_gbl_global_lock_present = TRUE;
  332. status = acpi_install_fixed_event_handler (ACPI_EVENT_GLOBAL,
  333. acpi_ev_global_lock_handler, NULL);
  334. /*
  335. * If the global lock does not exist on this platform, the attempt
  336. * to enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick)
  337. * Map to AE_OK, but mark global lock as not present.
  338. * Any attempt to actually use the global lock will be flagged
  339. * with an error.
  340. */
  341. if (status == AE_NO_HARDWARE_RESPONSE) {
  342. acpi_gbl_global_lock_present = FALSE;
  343. status = AE_OK;
  344. }
  345. return_ACPI_STATUS (status);
  346. }
  347. /******************************************************************************
  348. *
  349. * FUNCTION: acpi_ev_acquire_global_lock
  350. *
  351. * PARAMETERS: Timeout - Max time to wait for the lock, in millisec.
  352. *
  353. * RETURN: Status
  354. *
  355. * DESCRIPTION: Attempt to gain ownership of the Global Lock.
  356. *
  357. *****************************************************************************/
  358. acpi_status
  359. acpi_ev_acquire_global_lock (
  360. u16 timeout)
  361. {
  362. acpi_status status = AE_OK;
  363. u8 acquired = FALSE;
  364. ACPI_FUNCTION_TRACE ("ev_acquire_global_lock");
  365. #ifndef ACPI_APPLICATION
  366. /* Make sure that we actually have a global lock */
  367. if (!acpi_gbl_global_lock_present) {
  368. return_ACPI_STATUS (AE_NO_GLOBAL_LOCK);
  369. }
  370. #endif
  371. /* One more thread wants the global lock */
  372. acpi_gbl_global_lock_thread_count++;
  373. /*
  374. * If we (OS side vs. BIOS side) have the hardware lock already,
  375. * we are done
  376. */
  377. if (acpi_gbl_global_lock_acquired) {
  378. return_ACPI_STATUS (AE_OK);
  379. }
  380. /* We must acquire the actual hardware lock */
  381. ACPI_ACQUIRE_GLOBAL_LOCK (acpi_gbl_common_fACS.global_lock, acquired);
  382. if (acquired) {
  383. /* We got the lock */
  384. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Acquired the HW Global Lock\n"));
  385. acpi_gbl_global_lock_acquired = TRUE;
  386. return_ACPI_STATUS (AE_OK);
  387. }
  388. /*
  389. * Did not get the lock. The pending bit was set above, and we must now
  390. * wait until we get the global lock released interrupt.
  391. */
  392. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Waiting for the HW Global Lock\n"));
  393. /*
  394. * Acquire the global lock semaphore first.
  395. * Since this wait will block, we must release the interpreter
  396. */
  397. status = acpi_ex_system_wait_semaphore (acpi_gbl_global_lock_semaphore,
  398. timeout);
  399. return_ACPI_STATUS (status);
  400. }
  401. /*******************************************************************************
  402. *
  403. * FUNCTION: acpi_ev_release_global_lock
  404. *
  405. * PARAMETERS: None
  406. *
  407. * RETURN: Status
  408. *
  409. * DESCRIPTION: Releases ownership of the Global Lock.
  410. *
  411. ******************************************************************************/
  412. acpi_status
  413. acpi_ev_release_global_lock (
  414. void)
  415. {
  416. u8 pending = FALSE;
  417. acpi_status status = AE_OK;
  418. ACPI_FUNCTION_TRACE ("ev_release_global_lock");
  419. if (!acpi_gbl_global_lock_thread_count) {
  420. ACPI_REPORT_WARNING((
  421. "Cannot release HW Global Lock, it has not been acquired\n"));
  422. return_ACPI_STATUS (AE_NOT_ACQUIRED);
  423. }
  424. /* One fewer thread has the global lock */
  425. acpi_gbl_global_lock_thread_count--;
  426. if (acpi_gbl_global_lock_thread_count) {
  427. /* There are still some threads holding the lock, cannot release */
  428. return_ACPI_STATUS (AE_OK);
  429. }
  430. /*
  431. * No more threads holding lock, we can do the actual hardware
  432. * release
  433. */
  434. ACPI_RELEASE_GLOBAL_LOCK (acpi_gbl_common_fACS.global_lock, pending);
  435. acpi_gbl_global_lock_acquired = FALSE;
  436. /*
  437. * If the pending bit was set, we must write GBL_RLS to the control
  438. * register
  439. */
  440. if (pending) {
  441. status = acpi_set_register (ACPI_BITREG_GLOBAL_LOCK_RELEASE,
  442. 1, ACPI_MTX_LOCK);
  443. }
  444. return_ACPI_STATUS (status);
  445. }
  446. /******************************************************************************
  447. *
  448. * FUNCTION: acpi_ev_terminate
  449. *
  450. * PARAMETERS: none
  451. *
  452. * RETURN: none
  453. *
  454. * DESCRIPTION: Disable events and free memory allocated for table storage.
  455. *
  456. ******************************************************************************/
  457. void
  458. acpi_ev_terminate (
  459. void)
  460. {
  461. acpi_native_uint i;
  462. acpi_status status;
  463. ACPI_FUNCTION_TRACE ("ev_terminate");
  464. if (acpi_gbl_events_initialized) {
  465. /*
  466. * Disable all event-related functionality.
  467. * In all cases, on error, print a message but obviously we don't abort.
  468. */
  469. /* Disable all fixed events */
  470. for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++) {
  471. status = acpi_disable_event ((u32) i, 0);
  472. if (ACPI_FAILURE (status)) {
  473. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  474. "Could not disable fixed event %d\n", (u32) i));
  475. }
  476. }
  477. /* Disable all GPEs in all GPE blocks */
  478. status = acpi_ev_walk_gpe_list (acpi_hw_disable_gpe_block, ACPI_NOT_ISR);
  479. /* Remove SCI handler */
  480. status = acpi_ev_remove_sci_handler ();
  481. if (ACPI_FAILURE(status)) {
  482. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  483. "Could not remove SCI handler\n"));
  484. }
  485. }
  486. /* Deallocate all handler objects installed within GPE info structs */
  487. status = acpi_ev_walk_gpe_list (acpi_ev_delete_gpe_handlers, ACPI_NOT_ISR);
  488. /* Return to original mode if necessary */
  489. if (acpi_gbl_original_mode == ACPI_SYS_MODE_LEGACY) {
  490. status = acpi_disable ();
  491. if (ACPI_FAILURE (status)) {
  492. ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "acpi_disable failed\n"));
  493. }
  494. }
  495. return_VOID;
  496. }