evgpeblk.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /******************************************************************************
  2. *
  3. * Module Name: evgpeblk - GPE block creation and initialization.
  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. #define _COMPONENT ACPI_EVENTS
  46. ACPI_MODULE_NAME ("evgpeblk")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ev_valid_gpe_event
  50. *
  51. * PARAMETERS: gpe_event_info - Info for this GPE
  52. *
  53. * RETURN: TRUE if the gpe_event is valid
  54. *
  55. * DESCRIPTION: Validate a GPE event. DO NOT CALL FROM INTERRUPT LEVEL.
  56. * Should be called only when the GPE lists are semaphore locked
  57. * and not subject to change.
  58. *
  59. ******************************************************************************/
  60. u8
  61. acpi_ev_valid_gpe_event (
  62. struct acpi_gpe_event_info *gpe_event_info)
  63. {
  64. struct acpi_gpe_xrupt_info *gpe_xrupt_block;
  65. struct acpi_gpe_block_info *gpe_block;
  66. ACPI_FUNCTION_ENTRY ();
  67. /* No need for spin lock since we are not changing any list elements */
  68. /* Walk the GPE interrupt levels */
  69. gpe_xrupt_block = acpi_gbl_gpe_xrupt_list_head;
  70. while (gpe_xrupt_block) {
  71. gpe_block = gpe_xrupt_block->gpe_block_list_head;
  72. /* Walk the GPE blocks on this interrupt level */
  73. while (gpe_block) {
  74. if ((&gpe_block->event_info[0] <= gpe_event_info) &&
  75. (&gpe_block->event_info[((acpi_size) gpe_block->register_count) * 8] > gpe_event_info)) {
  76. return (TRUE);
  77. }
  78. gpe_block = gpe_block->next;
  79. }
  80. gpe_xrupt_block = gpe_xrupt_block->next;
  81. }
  82. return (FALSE);
  83. }
  84. /*******************************************************************************
  85. *
  86. * FUNCTION: acpi_ev_walk_gpe_list
  87. *
  88. * PARAMETERS: gpe_walk_callback - Routine called for each GPE block
  89. * Flags - ACPI_NOT_ISR or ACPI_ISR
  90. *
  91. * RETURN: Status
  92. *
  93. * DESCRIPTION: Walk the GPE lists.
  94. *
  95. ******************************************************************************/
  96. acpi_status
  97. acpi_ev_walk_gpe_list (
  98. ACPI_GPE_CALLBACK gpe_walk_callback,
  99. u32 flags)
  100. {
  101. struct acpi_gpe_block_info *gpe_block;
  102. struct acpi_gpe_xrupt_info *gpe_xrupt_info;
  103. acpi_status status = AE_OK;
  104. ACPI_FUNCTION_TRACE ("ev_walk_gpe_list");
  105. acpi_os_acquire_lock (acpi_gbl_gpe_lock, flags);
  106. /* Walk the interrupt level descriptor list */
  107. gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
  108. while (gpe_xrupt_info) {
  109. /* Walk all Gpe Blocks attached to this interrupt level */
  110. gpe_block = gpe_xrupt_info->gpe_block_list_head;
  111. while (gpe_block) {
  112. /* One callback per GPE block */
  113. status = gpe_walk_callback (gpe_xrupt_info, gpe_block);
  114. if (ACPI_FAILURE (status)) {
  115. goto unlock_and_exit;
  116. }
  117. gpe_block = gpe_block->next;
  118. }
  119. gpe_xrupt_info = gpe_xrupt_info->next;
  120. }
  121. unlock_and_exit:
  122. acpi_os_release_lock (acpi_gbl_gpe_lock, flags);
  123. return_ACPI_STATUS (status);
  124. }
  125. /******************************************************************************
  126. *
  127. * FUNCTION: acpi_ev_delete_gpe_handlers
  128. *
  129. * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
  130. * gpe_block - Gpe Block info
  131. *
  132. * RETURN: Status
  133. *
  134. * DESCRIPTION: Delete all Handler objects found in the GPE data structs.
  135. * Used only prior to termination.
  136. *
  137. ******************************************************************************/
  138. acpi_status
  139. acpi_ev_delete_gpe_handlers (
  140. struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  141. struct acpi_gpe_block_info *gpe_block)
  142. {
  143. struct acpi_gpe_event_info *gpe_event_info;
  144. acpi_native_uint i;
  145. acpi_native_uint j;
  146. ACPI_FUNCTION_TRACE ("ev_delete_gpe_handlers");
  147. /* Examine each GPE Register within the block */
  148. for (i = 0; i < gpe_block->register_count; i++) {
  149. /* Now look at the individual GPEs in this byte register */
  150. for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
  151. gpe_event_info = &gpe_block->event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j];
  152. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_HANDLER) {
  153. ACPI_MEM_FREE (gpe_event_info->dispatch.handler);
  154. gpe_event_info->dispatch.handler = NULL;
  155. gpe_event_info->flags &= ~ACPI_GPE_DISPATCH_MASK;
  156. }
  157. }
  158. }
  159. return_ACPI_STATUS (AE_OK);
  160. }
  161. /*******************************************************************************
  162. *
  163. * FUNCTION: acpi_ev_save_method_info
  164. *
  165. * PARAMETERS: Callback from walk_namespace
  166. *
  167. * RETURN: Status
  168. *
  169. * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
  170. * control method under the _GPE portion of the namespace.
  171. * Extract the name and GPE type from the object, saving this
  172. * information for quick lookup during GPE dispatch
  173. *
  174. * The name of each GPE control method is of the form:
  175. * "_Lxx" or "_Exx"
  176. * Where:
  177. * L - means that the GPE is level triggered
  178. * E - means that the GPE is edge triggered
  179. * xx - is the GPE number [in HEX]
  180. *
  181. ******************************************************************************/
  182. static acpi_status
  183. acpi_ev_save_method_info (
  184. acpi_handle obj_handle,
  185. u32 level,
  186. void *obj_desc,
  187. void **return_value)
  188. {
  189. struct acpi_gpe_block_info *gpe_block = (void *) obj_desc;
  190. struct acpi_gpe_event_info *gpe_event_info;
  191. u32 gpe_number;
  192. char name[ACPI_NAME_SIZE + 1];
  193. u8 type;
  194. acpi_status status;
  195. ACPI_FUNCTION_TRACE ("ev_save_method_info");
  196. /*
  197. * _Lxx and _Exx GPE method support
  198. *
  199. * 1) Extract the name from the object and convert to a string
  200. */
  201. ACPI_MOVE_32_TO_32 (name,
  202. &((struct acpi_namespace_node *) obj_handle)->name.integer);
  203. name[ACPI_NAME_SIZE] = 0;
  204. /*
  205. * 2) Edge/Level determination is based on the 2nd character
  206. * of the method name
  207. *
  208. * NOTE: Default GPE type is RUNTIME. May be changed later to WAKE
  209. * if a _PRW object is found that points to this GPE.
  210. */
  211. switch (name[1]) {
  212. case 'L':
  213. type = ACPI_GPE_LEVEL_TRIGGERED;
  214. break;
  215. case 'E':
  216. type = ACPI_GPE_EDGE_TRIGGERED;
  217. break;
  218. default:
  219. /* Unknown method type, just ignore it! */
  220. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  221. "Unknown GPE method type: %s (name not of form _Lxx or _Exx)\n",
  222. name));
  223. return_ACPI_STATUS (AE_OK);
  224. }
  225. /* Convert the last two characters of the name to the GPE Number */
  226. gpe_number = ACPI_STRTOUL (&name[2], NULL, 16);
  227. if (gpe_number == ACPI_UINT32_MAX) {
  228. /* Conversion failed; invalid method, just ignore it */
  229. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  230. "Could not extract GPE number from name: %s (name is not of form _Lxx or _Exx)\n",
  231. name));
  232. return_ACPI_STATUS (AE_OK);
  233. }
  234. /* Ensure that we have a valid GPE number for this GPE block */
  235. if ((gpe_number < gpe_block->block_base_number) ||
  236. (gpe_number >= (gpe_block->block_base_number + (gpe_block->register_count * 8)))) {
  237. /*
  238. * Not valid for this GPE block, just ignore it
  239. * However, it may be valid for a different GPE block, since GPE0 and GPE1
  240. * methods both appear under \_GPE.
  241. */
  242. return_ACPI_STATUS (AE_OK);
  243. }
  244. /*
  245. * Now we can add this information to the gpe_event_info block
  246. * for use during dispatch of this GPE. Default type is RUNTIME, although
  247. * this may change when the _PRW methods are executed later.
  248. */
  249. gpe_event_info = &gpe_block->event_info[gpe_number - gpe_block->block_base_number];
  250. gpe_event_info->flags = (u8) (type | ACPI_GPE_DISPATCH_METHOD |
  251. ACPI_GPE_TYPE_RUNTIME);
  252. gpe_event_info->dispatch.method_node = (struct acpi_namespace_node *) obj_handle;
  253. /* Update enable mask, but don't enable the HW GPE as of yet */
  254. status = acpi_ev_enable_gpe (gpe_event_info, FALSE);
  255. ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
  256. "Registered GPE method %s as GPE number 0x%.2X\n",
  257. name, gpe_number));
  258. return_ACPI_STATUS (status);
  259. }
  260. /*******************************************************************************
  261. *
  262. * FUNCTION: acpi_ev_match_prw_and_gpe
  263. *
  264. * PARAMETERS: Callback from walk_namespace
  265. *
  266. * RETURN: Status. NOTE: We ignore errors so that the _PRW walk is
  267. * not aborted on a single _PRW failure.
  268. *
  269. * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
  270. * Device. Run the _PRW method. If present, extract the GPE
  271. * number and mark the GPE as a WAKE GPE.
  272. *
  273. ******************************************************************************/
  274. static acpi_status
  275. acpi_ev_match_prw_and_gpe (
  276. acpi_handle obj_handle,
  277. u32 level,
  278. void *info,
  279. void **return_value)
  280. {
  281. struct acpi_gpe_walk_info *gpe_info = (void *) info;
  282. struct acpi_namespace_node *gpe_device;
  283. struct acpi_gpe_block_info *gpe_block;
  284. struct acpi_namespace_node *target_gpe_device;
  285. struct acpi_gpe_event_info *gpe_event_info;
  286. union acpi_operand_object *pkg_desc;
  287. union acpi_operand_object *obj_desc;
  288. u32 gpe_number;
  289. acpi_status status;
  290. ACPI_FUNCTION_TRACE ("ev_match_prw_and_gpe");
  291. /* Check for a _PRW method under this device */
  292. status = acpi_ut_evaluate_object (obj_handle, METHOD_NAME__PRW,
  293. ACPI_BTYPE_PACKAGE, &pkg_desc);
  294. if (ACPI_FAILURE (status)) {
  295. /* Ignore all errors from _PRW, we don't want to abort the subsystem */
  296. return_ACPI_STATUS (AE_OK);
  297. }
  298. /* The returned _PRW package must have at least two elements */
  299. if (pkg_desc->package.count < 2) {
  300. goto cleanup;
  301. }
  302. /* Extract pointers from the input context */
  303. gpe_device = gpe_info->gpe_device;
  304. gpe_block = gpe_info->gpe_block;
  305. /*
  306. * The _PRW object must return a package, we are only interested
  307. * in the first element
  308. */
  309. obj_desc = pkg_desc->package.elements[0];
  310. if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_INTEGER) {
  311. /* Use FADT-defined GPE device (from definition of _PRW) */
  312. target_gpe_device = acpi_gbl_fadt_gpe_device;
  313. /* Integer is the GPE number in the FADT described GPE blocks */
  314. gpe_number = (u32) obj_desc->integer.value;
  315. }
  316. else if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_PACKAGE) {
  317. /* Package contains a GPE reference and GPE number within a GPE block */
  318. if ((obj_desc->package.count < 2) ||
  319. (ACPI_GET_OBJECT_TYPE (obj_desc->package.elements[0]) != ACPI_TYPE_LOCAL_REFERENCE) ||
  320. (ACPI_GET_OBJECT_TYPE (obj_desc->package.elements[1]) != ACPI_TYPE_INTEGER)) {
  321. goto cleanup;
  322. }
  323. /* Get GPE block reference and decode */
  324. target_gpe_device = obj_desc->package.elements[0]->reference.node;
  325. gpe_number = (u32) obj_desc->package.elements[1]->integer.value;
  326. }
  327. else {
  328. /* Unknown type, just ignore it */
  329. goto cleanup;
  330. }
  331. /*
  332. * Is this GPE within this block?
  333. *
  334. * TRUE iff these conditions are true:
  335. * 1) The GPE devices match.
  336. * 2) The GPE index(number) is within the range of the Gpe Block
  337. * associated with the GPE device.
  338. */
  339. if ((gpe_device == target_gpe_device) &&
  340. (gpe_number >= gpe_block->block_base_number) &&
  341. (gpe_number < gpe_block->block_base_number + (gpe_block->register_count * 8))) {
  342. gpe_event_info = &gpe_block->event_info[gpe_number - gpe_block->block_base_number];
  343. /* Mark GPE for WAKE-ONLY but WAKE_DISABLED */
  344. gpe_event_info->flags &= ~(ACPI_GPE_WAKE_ENABLED | ACPI_GPE_RUN_ENABLED);
  345. status = acpi_ev_set_gpe_type (gpe_event_info, ACPI_GPE_TYPE_WAKE);
  346. if (ACPI_FAILURE (status)) {
  347. goto cleanup;
  348. }
  349. status = acpi_ev_update_gpe_enable_masks (gpe_event_info, ACPI_GPE_DISABLE);
  350. }
  351. cleanup:
  352. acpi_ut_remove_reference (pkg_desc);
  353. return_ACPI_STATUS (AE_OK);
  354. }
  355. /*******************************************************************************
  356. *
  357. * FUNCTION: acpi_ev_get_gpe_xrupt_block
  358. *
  359. * PARAMETERS: interrupt_level - Interrupt for a GPE block
  360. *
  361. * RETURN: A GPE interrupt block
  362. *
  363. * DESCRIPTION: Get or Create a GPE interrupt block. There is one interrupt
  364. * block per unique interrupt level used for GPEs.
  365. * Should be called only when the GPE lists are semaphore locked
  366. * and not subject to change.
  367. *
  368. ******************************************************************************/
  369. static struct acpi_gpe_xrupt_info *
  370. acpi_ev_get_gpe_xrupt_block (
  371. u32 interrupt_level)
  372. {
  373. struct acpi_gpe_xrupt_info *next_gpe_xrupt;
  374. struct acpi_gpe_xrupt_info *gpe_xrupt;
  375. acpi_status status;
  376. ACPI_FUNCTION_TRACE ("ev_get_gpe_xrupt_block");
  377. /* No need for spin lock since we are not changing any list elements here */
  378. next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
  379. while (next_gpe_xrupt) {
  380. if (next_gpe_xrupt->interrupt_level == interrupt_level) {
  381. return_PTR (next_gpe_xrupt);
  382. }
  383. next_gpe_xrupt = next_gpe_xrupt->next;
  384. }
  385. /* Not found, must allocate a new xrupt descriptor */
  386. gpe_xrupt = ACPI_MEM_CALLOCATE (sizeof (struct acpi_gpe_xrupt_info));
  387. if (!gpe_xrupt) {
  388. return_PTR (NULL);
  389. }
  390. gpe_xrupt->interrupt_level = interrupt_level;
  391. /* Install new interrupt descriptor with spin lock */
  392. acpi_os_acquire_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
  393. if (acpi_gbl_gpe_xrupt_list_head) {
  394. next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
  395. while (next_gpe_xrupt->next) {
  396. next_gpe_xrupt = next_gpe_xrupt->next;
  397. }
  398. next_gpe_xrupt->next = gpe_xrupt;
  399. gpe_xrupt->previous = next_gpe_xrupt;
  400. }
  401. else {
  402. acpi_gbl_gpe_xrupt_list_head = gpe_xrupt;
  403. }
  404. acpi_os_release_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
  405. /* Install new interrupt handler if not SCI_INT */
  406. if (interrupt_level != acpi_gbl_FADT->sci_int) {
  407. status = acpi_os_install_interrupt_handler (interrupt_level,
  408. acpi_ev_gpe_xrupt_handler, gpe_xrupt);
  409. if (ACPI_FAILURE (status)) {
  410. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  411. "Could not install GPE interrupt handler at level 0x%X\n",
  412. interrupt_level));
  413. return_PTR (NULL);
  414. }
  415. }
  416. return_PTR (gpe_xrupt);
  417. }
  418. /*******************************************************************************
  419. *
  420. * FUNCTION: acpi_ev_delete_gpe_xrupt
  421. *
  422. * PARAMETERS: gpe_xrupt - A GPE interrupt info block
  423. *
  424. * RETURN: Status
  425. *
  426. * DESCRIPTION: Remove and free a gpe_xrupt block. Remove an associated
  427. * interrupt handler if not the SCI interrupt.
  428. *
  429. ******************************************************************************/
  430. static acpi_status
  431. acpi_ev_delete_gpe_xrupt (
  432. struct acpi_gpe_xrupt_info *gpe_xrupt)
  433. {
  434. acpi_status status;
  435. ACPI_FUNCTION_TRACE ("ev_delete_gpe_xrupt");
  436. /* We never want to remove the SCI interrupt handler */
  437. if (gpe_xrupt->interrupt_level == acpi_gbl_FADT->sci_int) {
  438. gpe_xrupt->gpe_block_list_head = NULL;
  439. return_ACPI_STATUS (AE_OK);
  440. }
  441. /* Disable this interrupt */
  442. status = acpi_os_remove_interrupt_handler (gpe_xrupt->interrupt_level,
  443. acpi_ev_gpe_xrupt_handler);
  444. if (ACPI_FAILURE (status)) {
  445. return_ACPI_STATUS (status);
  446. }
  447. /* Unlink the interrupt block with lock */
  448. acpi_os_acquire_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
  449. if (gpe_xrupt->previous) {
  450. gpe_xrupt->previous->next = gpe_xrupt->next;
  451. }
  452. if (gpe_xrupt->next) {
  453. gpe_xrupt->next->previous = gpe_xrupt->previous;
  454. }
  455. acpi_os_release_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
  456. /* Free the block */
  457. ACPI_MEM_FREE (gpe_xrupt);
  458. return_ACPI_STATUS (AE_OK);
  459. }
  460. /*******************************************************************************
  461. *
  462. * FUNCTION: acpi_ev_install_gpe_block
  463. *
  464. * PARAMETERS: gpe_block - New GPE block
  465. * interrupt_level - Level to be associated with this GPE block
  466. *
  467. * RETURN: Status
  468. *
  469. * DESCRIPTION: Install new GPE block with mutex support
  470. *
  471. ******************************************************************************/
  472. static acpi_status
  473. acpi_ev_install_gpe_block (
  474. struct acpi_gpe_block_info *gpe_block,
  475. u32 interrupt_level)
  476. {
  477. struct acpi_gpe_block_info *next_gpe_block;
  478. struct acpi_gpe_xrupt_info *gpe_xrupt_block;
  479. acpi_status status;
  480. ACPI_FUNCTION_TRACE ("ev_install_gpe_block");
  481. status = acpi_ut_acquire_mutex (ACPI_MTX_EVENTS);
  482. if (ACPI_FAILURE (status)) {
  483. return_ACPI_STATUS (status);
  484. }
  485. gpe_xrupt_block = acpi_ev_get_gpe_xrupt_block (interrupt_level);
  486. if (!gpe_xrupt_block) {
  487. status = AE_NO_MEMORY;
  488. goto unlock_and_exit;
  489. }
  490. /* Install the new block at the end of the list for this interrupt with lock */
  491. acpi_os_acquire_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
  492. if (gpe_xrupt_block->gpe_block_list_head) {
  493. next_gpe_block = gpe_xrupt_block->gpe_block_list_head;
  494. while (next_gpe_block->next) {
  495. next_gpe_block = next_gpe_block->next;
  496. }
  497. next_gpe_block->next = gpe_block;
  498. gpe_block->previous = next_gpe_block;
  499. }
  500. else {
  501. gpe_xrupt_block->gpe_block_list_head = gpe_block;
  502. }
  503. gpe_block->xrupt_block = gpe_xrupt_block;
  504. acpi_os_release_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
  505. unlock_and_exit:
  506. status = acpi_ut_release_mutex (ACPI_MTX_EVENTS);
  507. return_ACPI_STATUS (status);
  508. }
  509. /*******************************************************************************
  510. *
  511. * FUNCTION: acpi_ev_delete_gpe_block
  512. *
  513. * PARAMETERS: gpe_block - Existing GPE block
  514. *
  515. * RETURN: Status
  516. *
  517. * DESCRIPTION: Remove a GPE block
  518. *
  519. ******************************************************************************/
  520. acpi_status
  521. acpi_ev_delete_gpe_block (
  522. struct acpi_gpe_block_info *gpe_block)
  523. {
  524. acpi_status status;
  525. ACPI_FUNCTION_TRACE ("ev_install_gpe_block");
  526. status = acpi_ut_acquire_mutex (ACPI_MTX_EVENTS);
  527. if (ACPI_FAILURE (status)) {
  528. return_ACPI_STATUS (status);
  529. }
  530. /* Disable all GPEs in this block */
  531. status = acpi_hw_disable_gpe_block (gpe_block->xrupt_block, gpe_block);
  532. if (!gpe_block->previous && !gpe_block->next) {
  533. /* This is the last gpe_block on this interrupt */
  534. status = acpi_ev_delete_gpe_xrupt (gpe_block->xrupt_block);
  535. if (ACPI_FAILURE (status)) {
  536. goto unlock_and_exit;
  537. }
  538. }
  539. else {
  540. /* Remove the block on this interrupt with lock */
  541. acpi_os_acquire_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
  542. if (gpe_block->previous) {
  543. gpe_block->previous->next = gpe_block->next;
  544. }
  545. else {
  546. gpe_block->xrupt_block->gpe_block_list_head = gpe_block->next;
  547. }
  548. if (gpe_block->next) {
  549. gpe_block->next->previous = gpe_block->previous;
  550. }
  551. acpi_os_release_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
  552. }
  553. /* Free the gpe_block */
  554. ACPI_MEM_FREE (gpe_block->register_info);
  555. ACPI_MEM_FREE (gpe_block->event_info);
  556. ACPI_MEM_FREE (gpe_block);
  557. unlock_and_exit:
  558. status = acpi_ut_release_mutex (ACPI_MTX_EVENTS);
  559. return_ACPI_STATUS (status);
  560. }
  561. /*******************************************************************************
  562. *
  563. * FUNCTION: acpi_ev_create_gpe_info_blocks
  564. *
  565. * PARAMETERS: gpe_block - New GPE block
  566. *
  567. * RETURN: Status
  568. *
  569. * DESCRIPTION: Create the register_info and event_info blocks for this GPE block
  570. *
  571. ******************************************************************************/
  572. static acpi_status
  573. acpi_ev_create_gpe_info_blocks (
  574. struct acpi_gpe_block_info *gpe_block)
  575. {
  576. struct acpi_gpe_register_info *gpe_register_info = NULL;
  577. struct acpi_gpe_event_info *gpe_event_info = NULL;
  578. struct acpi_gpe_event_info *this_event;
  579. struct acpi_gpe_register_info *this_register;
  580. acpi_native_uint i;
  581. acpi_native_uint j;
  582. acpi_status status;
  583. ACPI_FUNCTION_TRACE ("ev_create_gpe_info_blocks");
  584. /* Allocate the GPE register information block */
  585. gpe_register_info = ACPI_MEM_CALLOCATE (
  586. (acpi_size) gpe_block->register_count *
  587. sizeof (struct acpi_gpe_register_info));
  588. if (!gpe_register_info) {
  589. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  590. "Could not allocate the gpe_register_info table\n"));
  591. return_ACPI_STATUS (AE_NO_MEMORY);
  592. }
  593. /*
  594. * Allocate the GPE event_info block. There are eight distinct GPEs
  595. * per register. Initialization to zeros is sufficient.
  596. */
  597. gpe_event_info = ACPI_MEM_CALLOCATE (
  598. ((acpi_size) gpe_block->register_count * ACPI_GPE_REGISTER_WIDTH) *
  599. sizeof (struct acpi_gpe_event_info));
  600. if (!gpe_event_info) {
  601. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not allocate the gpe_event_info table\n"));
  602. status = AE_NO_MEMORY;
  603. goto error_exit;
  604. }
  605. /* Save the new Info arrays in the GPE block */
  606. gpe_block->register_info = gpe_register_info;
  607. gpe_block->event_info = gpe_event_info;
  608. /*
  609. * Initialize the GPE Register and Event structures. A goal of these
  610. * tables is to hide the fact that there are two separate GPE register sets
  611. * in a given gpe hardware block, the status registers occupy the first half,
  612. * and the enable registers occupy the second half.
  613. */
  614. this_register = gpe_register_info;
  615. this_event = gpe_event_info;
  616. for (i = 0; i < gpe_block->register_count; i++) {
  617. /* Init the register_info for this GPE register (8 GPEs) */
  618. this_register->base_gpe_number = (u8) (gpe_block->block_base_number +
  619. (i * ACPI_GPE_REGISTER_WIDTH));
  620. ACPI_STORE_ADDRESS (this_register->status_address.address,
  621. (gpe_block->block_address.address
  622. + i));
  623. ACPI_STORE_ADDRESS (this_register->enable_address.address,
  624. (gpe_block->block_address.address
  625. + i
  626. + gpe_block->register_count));
  627. this_register->status_address.address_space_id = gpe_block->block_address.address_space_id;
  628. this_register->enable_address.address_space_id = gpe_block->block_address.address_space_id;
  629. this_register->status_address.register_bit_width = ACPI_GPE_REGISTER_WIDTH;
  630. this_register->enable_address.register_bit_width = ACPI_GPE_REGISTER_WIDTH;
  631. this_register->status_address.register_bit_offset = ACPI_GPE_REGISTER_WIDTH;
  632. this_register->enable_address.register_bit_offset = ACPI_GPE_REGISTER_WIDTH;
  633. /* Init the event_info for each GPE within this register */
  634. for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
  635. this_event->register_bit = acpi_gbl_decode_to8bit[j];
  636. this_event->register_info = this_register;
  637. this_event++;
  638. }
  639. /*
  640. * Clear the status/enable registers. Note that status registers
  641. * are cleared by writing a '1', while enable registers are cleared
  642. * by writing a '0'.
  643. */
  644. status = acpi_hw_low_level_write (ACPI_GPE_REGISTER_WIDTH, 0x00,
  645. &this_register->enable_address);
  646. if (ACPI_FAILURE (status)) {
  647. goto error_exit;
  648. }
  649. status = acpi_hw_low_level_write (ACPI_GPE_REGISTER_WIDTH, 0xFF,
  650. &this_register->status_address);
  651. if (ACPI_FAILURE (status)) {
  652. goto error_exit;
  653. }
  654. this_register++;
  655. }
  656. return_ACPI_STATUS (AE_OK);
  657. error_exit:
  658. if (gpe_register_info) {
  659. ACPI_MEM_FREE (gpe_register_info);
  660. }
  661. if (gpe_event_info) {
  662. ACPI_MEM_FREE (gpe_event_info);
  663. }
  664. return_ACPI_STATUS (status);
  665. }
  666. /*******************************************************************************
  667. *
  668. * FUNCTION: acpi_ev_create_gpe_block
  669. *
  670. * PARAMETERS: gpe_device - Handle to the parent GPE block
  671. * gpe_block_address - Address and space_iD
  672. * register_count - Number of GPE register pairs in the block
  673. * gpe_block_base_number - Starting GPE number for the block
  674. * interrupt_level - H/W interrupt for the block
  675. * return_gpe_block - Where the new block descriptor is returned
  676. *
  677. * RETURN: Status
  678. *
  679. * DESCRIPTION: Create and Install a block of GPE registers
  680. *
  681. ******************************************************************************/
  682. acpi_status
  683. acpi_ev_create_gpe_block (
  684. struct acpi_namespace_node *gpe_device,
  685. struct acpi_generic_address *gpe_block_address,
  686. u32 register_count,
  687. u8 gpe_block_base_number,
  688. u32 interrupt_level,
  689. struct acpi_gpe_block_info **return_gpe_block)
  690. {
  691. struct acpi_gpe_block_info *gpe_block;
  692. struct acpi_gpe_event_info *gpe_event_info;
  693. acpi_native_uint i;
  694. acpi_native_uint j;
  695. u32 wake_gpe_count;
  696. u32 gpe_enabled_count;
  697. acpi_status status;
  698. struct acpi_gpe_walk_info gpe_info;
  699. ACPI_FUNCTION_TRACE ("ev_create_gpe_block");
  700. if (!register_count) {
  701. return_ACPI_STATUS (AE_OK);
  702. }
  703. /* Allocate a new GPE block */
  704. gpe_block = ACPI_MEM_CALLOCATE (sizeof (struct acpi_gpe_block_info));
  705. if (!gpe_block) {
  706. return_ACPI_STATUS (AE_NO_MEMORY);
  707. }
  708. /* Initialize the new GPE block */
  709. gpe_block->register_count = register_count;
  710. gpe_block->block_base_number = gpe_block_base_number;
  711. gpe_block->node = gpe_device;
  712. ACPI_MEMCPY (&gpe_block->block_address, gpe_block_address, sizeof (struct acpi_generic_address));
  713. /* Create the register_info and event_info sub-structures */
  714. status = acpi_ev_create_gpe_info_blocks (gpe_block);
  715. if (ACPI_FAILURE (status)) {
  716. ACPI_MEM_FREE (gpe_block);
  717. return_ACPI_STATUS (status);
  718. }
  719. /* Install the new block in the global list(s) */
  720. status = acpi_ev_install_gpe_block (gpe_block, interrupt_level);
  721. if (ACPI_FAILURE (status)) {
  722. ACPI_MEM_FREE (gpe_block);
  723. return_ACPI_STATUS (status);
  724. }
  725. /* Find all GPE methods (_Lxx, _Exx) for this block */
  726. status = acpi_ns_walk_namespace (ACPI_TYPE_METHOD, gpe_device,
  727. ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK, acpi_ev_save_method_info,
  728. gpe_block, NULL);
  729. /*
  730. * Runtime option: Should Wake GPEs be enabled at runtime? The default
  731. * is No, they should only be enabled just as the machine goes to sleep.
  732. */
  733. if (acpi_gbl_leave_wake_gpes_disabled) {
  734. /*
  735. * Differentiate RUNTIME vs WAKE GPEs, via the _PRW control methods.
  736. * (Each GPE that has one or more _PRWs that reference it is by
  737. * definition a WAKE GPE and will not be enabled while the machine
  738. * is running.)
  739. */
  740. gpe_info.gpe_block = gpe_block;
  741. gpe_info.gpe_device = gpe_device;
  742. status = acpi_ns_walk_namespace (ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  743. ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK, acpi_ev_match_prw_and_gpe,
  744. &gpe_info, NULL);
  745. }
  746. /*
  747. * Enable all GPEs in this block that are 1) "runtime" or "run/wake" GPEs,
  748. * and 2) have a corresponding _Lxx or _Exx method. All other GPEs must
  749. * be enabled via the acpi_enable_gpe() external interface.
  750. */
  751. wake_gpe_count = 0;
  752. gpe_enabled_count = 0;
  753. for (i = 0; i < gpe_block->register_count; i++) {
  754. for (j = 0; j < 8; j++) {
  755. /* Get the info block for this particular GPE */
  756. gpe_event_info = &gpe_block->event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j];
  757. if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_METHOD) &&
  758. (gpe_event_info->flags & ACPI_GPE_TYPE_RUNTIME)) {
  759. gpe_enabled_count++;
  760. }
  761. if (gpe_event_info->flags & ACPI_GPE_TYPE_WAKE) {
  762. wake_gpe_count++;
  763. }
  764. }
  765. }
  766. /* Dump info about this GPE block */
  767. ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
  768. "GPE %02X to %02X [%4.4s] %u regs on int 0x%X\n",
  769. (u32) gpe_block->block_base_number,
  770. (u32) (gpe_block->block_base_number +
  771. ((gpe_block->register_count * ACPI_GPE_REGISTER_WIDTH) -1)),
  772. gpe_device->name.ascii,
  773. gpe_block->register_count,
  774. interrupt_level));
  775. /* Enable all valid GPEs found above */
  776. status = acpi_hw_enable_runtime_gpe_block (NULL, gpe_block);
  777. ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
  778. "Found %u Wake, Enabled %u Runtime GPEs in this block\n",
  779. wake_gpe_count, gpe_enabled_count));
  780. /* Return the new block */
  781. if (return_gpe_block) {
  782. (*return_gpe_block) = gpe_block;
  783. }
  784. return_ACPI_STATUS (AE_OK);
  785. }
  786. /*******************************************************************************
  787. *
  788. * FUNCTION: acpi_ev_gpe_initialize
  789. *
  790. * PARAMETERS: None
  791. *
  792. * RETURN: Status
  793. *
  794. * DESCRIPTION: Initialize the GPE data structures
  795. *
  796. ******************************************************************************/
  797. acpi_status
  798. acpi_ev_gpe_initialize (
  799. void)
  800. {
  801. u32 register_count0 = 0;
  802. u32 register_count1 = 0;
  803. u32 gpe_number_max = 0;
  804. acpi_status status;
  805. ACPI_FUNCTION_TRACE ("ev_gpe_initialize");
  806. status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
  807. if (ACPI_FAILURE (status)) {
  808. return_ACPI_STATUS (status);
  809. }
  810. /*
  811. * Initialize the GPE Block(s) defined in the FADT
  812. *
  813. * Why the GPE register block lengths are divided by 2: From the ACPI Spec,
  814. * section "General-Purpose Event Registers", we have:
  815. *
  816. * "Each register block contains two registers of equal length
  817. * GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
  818. * GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
  819. * The length of the GPE1_STS and GPE1_EN registers is equal to
  820. * half the GPE1_LEN. If a generic register block is not supported
  821. * then its respective block pointer and block length values in the
  822. * FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
  823. * to be the same size."
  824. */
  825. /*
  826. * Determine the maximum GPE number for this machine.
  827. *
  828. * Note: both GPE0 and GPE1 are optional, and either can exist without
  829. * the other.
  830. *
  831. * If EITHER the register length OR the block address are zero, then that
  832. * particular block is not supported.
  833. */
  834. if (acpi_gbl_FADT->gpe0_blk_len &&
  835. acpi_gbl_FADT->xgpe0_blk.address) {
  836. /* GPE block 0 exists (has both length and address > 0) */
  837. register_count0 = (u16) (acpi_gbl_FADT->gpe0_blk_len / 2);
  838. gpe_number_max = (register_count0 * ACPI_GPE_REGISTER_WIDTH) - 1;
  839. /* Install GPE Block 0 */
  840. status = acpi_ev_create_gpe_block (acpi_gbl_fadt_gpe_device, &acpi_gbl_FADT->xgpe0_blk,
  841. register_count0, 0, acpi_gbl_FADT->sci_int, &acpi_gbl_gpe_fadt_blocks[0]);
  842. if (ACPI_FAILURE (status)) {
  843. ACPI_REPORT_ERROR ((
  844. "Could not create GPE Block 0, %s\n",
  845. acpi_format_exception (status)));
  846. }
  847. }
  848. if (acpi_gbl_FADT->gpe1_blk_len &&
  849. acpi_gbl_FADT->xgpe1_blk.address) {
  850. /* GPE block 1 exists (has both length and address > 0) */
  851. register_count1 = (u16) (acpi_gbl_FADT->gpe1_blk_len / 2);
  852. /* Check for GPE0/GPE1 overlap (if both banks exist) */
  853. if ((register_count0) &&
  854. (gpe_number_max >= acpi_gbl_FADT->gpe1_base)) {
  855. ACPI_REPORT_ERROR ((
  856. "GPE0 block (GPE 0 to %d) overlaps the GPE1 block (GPE %d to %d) - Ignoring GPE1\n",
  857. gpe_number_max, acpi_gbl_FADT->gpe1_base,
  858. acpi_gbl_FADT->gpe1_base +
  859. ((register_count1 * ACPI_GPE_REGISTER_WIDTH) - 1)));
  860. /* Ignore GPE1 block by setting the register count to zero */
  861. register_count1 = 0;
  862. }
  863. else {
  864. /* Install GPE Block 1 */
  865. status = acpi_ev_create_gpe_block (acpi_gbl_fadt_gpe_device, &acpi_gbl_FADT->xgpe1_blk,
  866. register_count1, acpi_gbl_FADT->gpe1_base,
  867. acpi_gbl_FADT->sci_int, &acpi_gbl_gpe_fadt_blocks[1]);
  868. if (ACPI_FAILURE (status)) {
  869. ACPI_REPORT_ERROR ((
  870. "Could not create GPE Block 1, %s\n",
  871. acpi_format_exception (status)));
  872. }
  873. /*
  874. * GPE0 and GPE1 do not have to be contiguous in the GPE number
  875. * space. However, GPE0 always starts at GPE number zero.
  876. */
  877. gpe_number_max = acpi_gbl_FADT->gpe1_base +
  878. ((register_count1 * ACPI_GPE_REGISTER_WIDTH) - 1);
  879. }
  880. }
  881. /* Exit if there are no GPE registers */
  882. if ((register_count0 + register_count1) == 0) {
  883. /* GPEs are not required by ACPI, this is OK */
  884. ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
  885. "There are no GPE blocks defined in the FADT\n"));
  886. status = AE_OK;
  887. goto cleanup;
  888. }
  889. /* Check for Max GPE number out-of-range */
  890. if (gpe_number_max > ACPI_GPE_MAX) {
  891. ACPI_REPORT_ERROR (("Maximum GPE number from FADT is too large: 0x%X\n",
  892. gpe_number_max));
  893. status = AE_BAD_VALUE;
  894. goto cleanup;
  895. }
  896. cleanup:
  897. (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  898. return_ACPI_STATUS (AE_OK);
  899. }