evgpeblk.c 35 KB

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