evgpeblk.c 34 KB

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