evmisc.c 16 KB

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