evmisc.c 18 KB

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