evgpeblk.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /******************************************************************************
  2. *
  3. * Module Name: evgpeblk - GPE block creation and initialization.
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, 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_install_gpe_block(struct acpi_gpe_block_info *gpe_block,
  51. u32 interrupt_number);
  52. static acpi_status
  53. acpi_ev_create_gpe_info_blocks(struct acpi_gpe_block_info *gpe_block);
  54. /*******************************************************************************
  55. *
  56. * FUNCTION: acpi_ev_install_gpe_block
  57. *
  58. * PARAMETERS: gpe_block - New GPE block
  59. * interrupt_number - Xrupt to be associated with this
  60. * GPE block
  61. *
  62. * RETURN: Status
  63. *
  64. * DESCRIPTION: Install new GPE block with mutex support
  65. *
  66. ******************************************************************************/
  67. static acpi_status
  68. acpi_ev_install_gpe_block(struct acpi_gpe_block_info *gpe_block,
  69. u32 interrupt_number)
  70. {
  71. struct acpi_gpe_block_info *next_gpe_block;
  72. struct acpi_gpe_xrupt_info *gpe_xrupt_block;
  73. acpi_status status;
  74. acpi_cpu_flags flags;
  75. ACPI_FUNCTION_TRACE(ev_install_gpe_block);
  76. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  77. if (ACPI_FAILURE(status)) {
  78. return_ACPI_STATUS(status);
  79. }
  80. gpe_xrupt_block = acpi_ev_get_gpe_xrupt_block(interrupt_number);
  81. if (!gpe_xrupt_block) {
  82. status = AE_NO_MEMORY;
  83. goto unlock_and_exit;
  84. }
  85. /* Install the new block at the end of the list with lock */
  86. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  87. if (gpe_xrupt_block->gpe_block_list_head) {
  88. next_gpe_block = gpe_xrupt_block->gpe_block_list_head;
  89. while (next_gpe_block->next) {
  90. next_gpe_block = next_gpe_block->next;
  91. }
  92. next_gpe_block->next = gpe_block;
  93. gpe_block->previous = next_gpe_block;
  94. } else {
  95. gpe_xrupt_block->gpe_block_list_head = gpe_block;
  96. }
  97. gpe_block->xrupt_block = gpe_xrupt_block;
  98. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  99. unlock_and_exit:
  100. status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  101. return_ACPI_STATUS(status);
  102. }
  103. /*******************************************************************************
  104. *
  105. * FUNCTION: acpi_ev_delete_gpe_block
  106. *
  107. * PARAMETERS: gpe_block - Existing GPE block
  108. *
  109. * RETURN: Status
  110. *
  111. * DESCRIPTION: Remove a GPE block
  112. *
  113. ******************************************************************************/
  114. acpi_status acpi_ev_delete_gpe_block(struct acpi_gpe_block_info *gpe_block)
  115. {
  116. acpi_status status;
  117. acpi_cpu_flags flags;
  118. ACPI_FUNCTION_TRACE(ev_install_gpe_block);
  119. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  120. if (ACPI_FAILURE(status)) {
  121. return_ACPI_STATUS(status);
  122. }
  123. /* Disable all GPEs in this block */
  124. status =
  125. acpi_hw_disable_gpe_block(gpe_block->xrupt_block, gpe_block, NULL);
  126. if (!gpe_block->previous && !gpe_block->next) {
  127. /* This is the last gpe_block on this interrupt */
  128. status = acpi_ev_delete_gpe_xrupt(gpe_block->xrupt_block);
  129. if (ACPI_FAILURE(status)) {
  130. goto unlock_and_exit;
  131. }
  132. } else {
  133. /* Remove the block on this interrupt with lock */
  134. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  135. if (gpe_block->previous) {
  136. gpe_block->previous->next = gpe_block->next;
  137. } else {
  138. gpe_block->xrupt_block->gpe_block_list_head =
  139. gpe_block->next;
  140. }
  141. if (gpe_block->next) {
  142. gpe_block->next->previous = gpe_block->previous;
  143. }
  144. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  145. }
  146. acpi_current_gpe_count -= gpe_block->gpe_count;
  147. /* Free the gpe_block */
  148. ACPI_FREE(gpe_block->register_info);
  149. ACPI_FREE(gpe_block->event_info);
  150. ACPI_FREE(gpe_block);
  151. unlock_and_exit:
  152. status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  153. return_ACPI_STATUS(status);
  154. }
  155. /*******************************************************************************
  156. *
  157. * FUNCTION: acpi_ev_create_gpe_info_blocks
  158. *
  159. * PARAMETERS: gpe_block - New GPE block
  160. *
  161. * RETURN: Status
  162. *
  163. * DESCRIPTION: Create the register_info and event_info blocks for this GPE block
  164. *
  165. ******************************************************************************/
  166. static acpi_status
  167. acpi_ev_create_gpe_info_blocks(struct acpi_gpe_block_info *gpe_block)
  168. {
  169. struct acpi_gpe_register_info *gpe_register_info = NULL;
  170. struct acpi_gpe_event_info *gpe_event_info = NULL;
  171. struct acpi_gpe_event_info *this_event;
  172. struct acpi_gpe_register_info *this_register;
  173. u32 i;
  174. u32 j;
  175. acpi_status status;
  176. ACPI_FUNCTION_TRACE(ev_create_gpe_info_blocks);
  177. /* Allocate the GPE register information block */
  178. gpe_register_info = ACPI_ALLOCATE_ZEROED((acpi_size) gpe_block->
  179. register_count *
  180. sizeof(struct
  181. acpi_gpe_register_info));
  182. if (!gpe_register_info) {
  183. ACPI_ERROR((AE_INFO,
  184. "Could not allocate the GpeRegisterInfo table"));
  185. return_ACPI_STATUS(AE_NO_MEMORY);
  186. }
  187. /*
  188. * Allocate the GPE event_info block. There are eight distinct GPEs
  189. * per register. Initialization to zeros is sufficient.
  190. */
  191. gpe_event_info = ACPI_ALLOCATE_ZEROED((acpi_size) gpe_block->gpe_count *
  192. sizeof(struct
  193. acpi_gpe_event_info));
  194. if (!gpe_event_info) {
  195. ACPI_ERROR((AE_INFO,
  196. "Could not allocate the GpeEventInfo table"));
  197. status = AE_NO_MEMORY;
  198. goto error_exit;
  199. }
  200. /* Save the new Info arrays in the GPE block */
  201. gpe_block->register_info = gpe_register_info;
  202. gpe_block->event_info = gpe_event_info;
  203. /*
  204. * Initialize the GPE Register and Event structures. A goal of these
  205. * tables is to hide the fact that there are two separate GPE register
  206. * sets in a given GPE hardware block, the status registers occupy the
  207. * first half, and the enable registers occupy the second half.
  208. */
  209. this_register = gpe_register_info;
  210. this_event = gpe_event_info;
  211. for (i = 0; i < gpe_block->register_count; i++) {
  212. /* Init the register_info for this GPE register (8 GPEs) */
  213. this_register->base_gpe_number =
  214. (u8) (gpe_block->block_base_number +
  215. (i * ACPI_GPE_REGISTER_WIDTH));
  216. this_register->status_address.address =
  217. gpe_block->block_address.address + i;
  218. this_register->enable_address.address =
  219. gpe_block->block_address.address + i +
  220. gpe_block->register_count;
  221. this_register->status_address.space_id =
  222. gpe_block->block_address.space_id;
  223. this_register->enable_address.space_id =
  224. gpe_block->block_address.space_id;
  225. this_register->status_address.bit_width =
  226. ACPI_GPE_REGISTER_WIDTH;
  227. this_register->enable_address.bit_width =
  228. ACPI_GPE_REGISTER_WIDTH;
  229. this_register->status_address.bit_offset = 0;
  230. this_register->enable_address.bit_offset = 0;
  231. /* Init the event_info for each GPE within this register */
  232. for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
  233. this_event->gpe_number =
  234. (u8) (this_register->base_gpe_number + j);
  235. this_event->register_info = this_register;
  236. this_event++;
  237. }
  238. /* Disable all GPEs within this register */
  239. status = acpi_hw_write(0x00, &this_register->enable_address);
  240. if (ACPI_FAILURE(status)) {
  241. goto error_exit;
  242. }
  243. /* Clear any pending GPE events within this register */
  244. status = acpi_hw_write(0xFF, &this_register->status_address);
  245. if (ACPI_FAILURE(status)) {
  246. goto error_exit;
  247. }
  248. this_register++;
  249. }
  250. return_ACPI_STATUS(AE_OK);
  251. error_exit:
  252. if (gpe_register_info) {
  253. ACPI_FREE(gpe_register_info);
  254. }
  255. if (gpe_event_info) {
  256. ACPI_FREE(gpe_event_info);
  257. }
  258. return_ACPI_STATUS(status);
  259. }
  260. /*******************************************************************************
  261. *
  262. * FUNCTION: acpi_ev_create_gpe_block
  263. *
  264. * PARAMETERS: gpe_device - Handle to the parent GPE block
  265. * gpe_block_address - Address and space_iD
  266. * register_count - Number of GPE register pairs in the block
  267. * gpe_block_base_number - Starting GPE number for the block
  268. * interrupt_number - H/W interrupt for the block
  269. * return_gpe_block - Where the new block descriptor is returned
  270. *
  271. * RETURN: Status
  272. *
  273. * DESCRIPTION: Create and Install a block of GPE registers. All GPEs within
  274. * the block are disabled at exit.
  275. * Note: Assumes namespace is locked.
  276. *
  277. ******************************************************************************/
  278. acpi_status
  279. acpi_ev_create_gpe_block(struct acpi_namespace_node *gpe_device,
  280. struct acpi_generic_address *gpe_block_address,
  281. u32 register_count,
  282. u8 gpe_block_base_number,
  283. u32 interrupt_number,
  284. struct acpi_gpe_block_info **return_gpe_block)
  285. {
  286. acpi_status status;
  287. struct acpi_gpe_block_info *gpe_block;
  288. struct acpi_gpe_walk_info walk_info;
  289. ACPI_FUNCTION_TRACE(ev_create_gpe_block);
  290. if (!register_count) {
  291. return_ACPI_STATUS(AE_OK);
  292. }
  293. /* Allocate a new GPE block */
  294. gpe_block = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_block_info));
  295. if (!gpe_block) {
  296. return_ACPI_STATUS(AE_NO_MEMORY);
  297. }
  298. /* Initialize the new GPE block */
  299. gpe_block->node = gpe_device;
  300. gpe_block->gpe_count = (u16)(register_count * ACPI_GPE_REGISTER_WIDTH);
  301. gpe_block->initialized = FALSE;
  302. gpe_block->register_count = register_count;
  303. gpe_block->block_base_number = gpe_block_base_number;
  304. ACPI_MEMCPY(&gpe_block->block_address, gpe_block_address,
  305. sizeof(struct acpi_generic_address));
  306. /*
  307. * Create the register_info and event_info sub-structures
  308. * Note: disables and clears all GPEs in the block
  309. */
  310. status = acpi_ev_create_gpe_info_blocks(gpe_block);
  311. if (ACPI_FAILURE(status)) {
  312. ACPI_FREE(gpe_block);
  313. return_ACPI_STATUS(status);
  314. }
  315. /* Install the new block in the global lists */
  316. status = acpi_ev_install_gpe_block(gpe_block, interrupt_number);
  317. if (ACPI_FAILURE(status)) {
  318. ACPI_FREE(gpe_block);
  319. return_ACPI_STATUS(status);
  320. }
  321. acpi_gbl_all_gpes_initialized = FALSE;
  322. /* Find all GPE methods (_Lxx or_Exx) for this block */
  323. walk_info.gpe_block = gpe_block;
  324. walk_info.gpe_device = gpe_device;
  325. walk_info.execute_by_owner_id = FALSE;
  326. status = acpi_ns_walk_namespace(ACPI_TYPE_METHOD, gpe_device,
  327. ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK,
  328. acpi_ev_match_gpe_method, NULL,
  329. &walk_info, NULL);
  330. /* Return the new block */
  331. if (return_gpe_block) {
  332. (*return_gpe_block) = gpe_block;
  333. }
  334. ACPI_DEBUG_PRINT((ACPI_DB_INIT,
  335. "GPE %02X to %02X [%4.4s] %u regs on int 0x%X\n",
  336. (u32) gpe_block->block_base_number,
  337. (u32) (gpe_block->block_base_number +
  338. (gpe_block->gpe_count - 1)),
  339. gpe_device->name.ascii, gpe_block->register_count,
  340. interrupt_number));
  341. /* Update global count of currently available GPEs */
  342. acpi_current_gpe_count += gpe_block->gpe_count;
  343. return_ACPI_STATUS(AE_OK);
  344. }
  345. /*******************************************************************************
  346. *
  347. * FUNCTION: acpi_ev_initialize_gpe_block
  348. *
  349. * PARAMETERS: acpi_gpe_callback
  350. *
  351. * RETURN: Status
  352. *
  353. * DESCRIPTION: Initialize and enable a GPE block. Enable GPEs that have
  354. * associated methods.
  355. * Note: Assumes namespace is locked.
  356. *
  357. ******************************************************************************/
  358. acpi_status
  359. acpi_ev_initialize_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  360. struct acpi_gpe_block_info *gpe_block,
  361. void *ignored)
  362. {
  363. acpi_status status;
  364. struct acpi_gpe_event_info *gpe_event_info;
  365. u32 gpe_enabled_count;
  366. u32 gpe_index;
  367. u32 i;
  368. u32 j;
  369. ACPI_FUNCTION_TRACE(ev_initialize_gpe_block);
  370. /*
  371. * Ignore a null GPE block (e.g., if no GPE block 1 exists), and
  372. * any GPE blocks that have been initialized already.
  373. */
  374. if (!gpe_block || gpe_block->initialized) {
  375. return_ACPI_STATUS(AE_OK);
  376. }
  377. /*
  378. * Enable all GPEs that have a corresponding method and have the
  379. * ACPI_GPE_CAN_WAKE flag unset. Any other GPEs within this block
  380. * must be enabled via the acpi_enable_gpe() interface.
  381. */
  382. gpe_enabled_count = 0;
  383. for (i = 0; i < gpe_block->register_count; i++) {
  384. for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
  385. /* Get the info block for this particular GPE */
  386. gpe_index = (i * ACPI_GPE_REGISTER_WIDTH) + j;
  387. gpe_event_info = &gpe_block->event_info[gpe_index];
  388. /*
  389. * Ignore GPEs that have no corresponding _Lxx/_Exx method
  390. * and GPEs that are used to wake the system
  391. */
  392. if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
  393. ACPI_GPE_DISPATCH_NONE)
  394. || ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)
  395. == ACPI_GPE_DISPATCH_HANDLER)
  396. || (gpe_event_info->flags & ACPI_GPE_CAN_WAKE)) {
  397. continue;
  398. }
  399. status = acpi_ev_add_gpe_reference(gpe_event_info);
  400. if (ACPI_FAILURE(status)) {
  401. ACPI_EXCEPTION((AE_INFO, status,
  402. "Could not enable GPE 0x%02X",
  403. gpe_index + gpe_block->block_base_number));
  404. continue;
  405. }
  406. gpe_enabled_count++;
  407. }
  408. }
  409. if (gpe_enabled_count) {
  410. ACPI_DEBUG_PRINT((ACPI_DB_INIT,
  411. "Enabled %u GPEs in this block\n",
  412. gpe_enabled_count));
  413. }
  414. gpe_block->initialized = TRUE;
  415. return_ACPI_STATUS(AE_OK);
  416. }