evmisc.c 18 KB

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