evgpeblk.c 34 KB

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