evmisc.c 17 KB

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