evgpeblk.c 35 KB

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