evregion.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /******************************************************************************
  2. *
  3. * Module Name: evregion - ACPI address_space (op_region) handler dispatch
  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. #include "acinterp.h"
  47. #define _COMPONENT ACPI_EVENTS
  48. ACPI_MODULE_NAME("evregion")
  49. /* Local prototypes */
  50. static u8
  51. acpi_ev_has_default_handler(struct acpi_namespace_node *node,
  52. acpi_adr_space_type space_id);
  53. static void acpi_ev_orphan_ec_reg_method(void);
  54. static acpi_status
  55. acpi_ev_reg_run(acpi_handle obj_handle,
  56. u32 level, void *context, void **return_value);
  57. static acpi_status
  58. acpi_ev_install_handler(acpi_handle obj_handle,
  59. u32 level, void *context, void **return_value);
  60. /* These are the address spaces that will get default handlers */
  61. #define ACPI_NUM_DEFAULT_SPACES 4
  62. static u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {
  63. ACPI_ADR_SPACE_SYSTEM_MEMORY,
  64. ACPI_ADR_SPACE_SYSTEM_IO,
  65. ACPI_ADR_SPACE_PCI_CONFIG,
  66. ACPI_ADR_SPACE_DATA_TABLE
  67. };
  68. /*******************************************************************************
  69. *
  70. * FUNCTION: acpi_ev_install_region_handlers
  71. *
  72. * PARAMETERS: None
  73. *
  74. * RETURN: Status
  75. *
  76. * DESCRIPTION: Installs the core subsystem default address space handlers.
  77. *
  78. ******************************************************************************/
  79. acpi_status acpi_ev_install_region_handlers(void)
  80. {
  81. acpi_status status;
  82. u32 i;
  83. ACPI_FUNCTION_TRACE(ev_install_region_handlers);
  84. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  85. if (ACPI_FAILURE(status)) {
  86. return_ACPI_STATUS(status);
  87. }
  88. /*
  89. * All address spaces (PCI Config, EC, SMBus) are scope dependent and
  90. * registration must occur for a specific device.
  91. *
  92. * In the case of the system memory and IO address spaces there is
  93. * currently no device associated with the address space. For these we
  94. * use the root.
  95. *
  96. * We install the default PCI config space handler at the root so that
  97. * this space is immediately available even though the we have not
  98. * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
  99. * specification which states that the PCI config space must be always
  100. * available -- even though we are nowhere near ready to find the PCI root
  101. * buses at this point.
  102. *
  103. * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
  104. * has already been installed (via acpi_install_address_space_handler).
  105. * Similar for AE_SAME_HANDLER.
  106. */
  107. for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
  108. status = acpi_ev_install_space_handler(acpi_gbl_root_node,
  109. acpi_gbl_default_address_spaces
  110. [i],
  111. ACPI_DEFAULT_HANDLER,
  112. NULL, NULL);
  113. switch (status) {
  114. case AE_OK:
  115. case AE_SAME_HANDLER:
  116. case AE_ALREADY_EXISTS:
  117. /* These exceptions are all OK */
  118. status = AE_OK;
  119. break;
  120. default:
  121. goto unlock_and_exit;
  122. }
  123. }
  124. unlock_and_exit:
  125. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  126. return_ACPI_STATUS(status);
  127. }
  128. /*******************************************************************************
  129. *
  130. * FUNCTION: acpi_ev_has_default_handler
  131. *
  132. * PARAMETERS: Node - Namespace node for the device
  133. * space_id - The address space ID
  134. *
  135. * RETURN: TRUE if default handler is installed, FALSE otherwise
  136. *
  137. * DESCRIPTION: Check if the default handler is installed for the requested
  138. * space ID.
  139. *
  140. ******************************************************************************/
  141. static u8
  142. acpi_ev_has_default_handler(struct acpi_namespace_node *node,
  143. acpi_adr_space_type space_id)
  144. {
  145. union acpi_operand_object *obj_desc;
  146. union acpi_operand_object *handler_obj;
  147. /* Must have an existing internal object */
  148. obj_desc = acpi_ns_get_attached_object(node);
  149. if (obj_desc) {
  150. handler_obj = obj_desc->device.handler;
  151. /* Walk the linked list of handlers for this object */
  152. while (handler_obj) {
  153. if (handler_obj->address_space.space_id == space_id) {
  154. if (handler_obj->address_space.handler_flags &
  155. ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
  156. return (TRUE);
  157. }
  158. }
  159. handler_obj = handler_obj->address_space.next;
  160. }
  161. }
  162. return (FALSE);
  163. }
  164. /*******************************************************************************
  165. *
  166. * FUNCTION: acpi_ev_initialize_op_regions
  167. *
  168. * PARAMETERS: None
  169. *
  170. * RETURN: Status
  171. *
  172. * DESCRIPTION: Execute _REG methods for all Operation Regions that have
  173. * an installed default region handler.
  174. *
  175. ******************************************************************************/
  176. acpi_status acpi_ev_initialize_op_regions(void)
  177. {
  178. acpi_status status;
  179. u32 i;
  180. ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
  181. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  182. if (ACPI_FAILURE(status)) {
  183. return_ACPI_STATUS(status);
  184. }
  185. /* Run the _REG methods for op_regions in each default address space */
  186. for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
  187. /*
  188. * Make sure the installed handler is the DEFAULT handler. If not the
  189. * default, the _REG methods will have already been run (when the
  190. * handler was installed)
  191. */
  192. if (acpi_ev_has_default_handler(acpi_gbl_root_node,
  193. acpi_gbl_default_address_spaces
  194. [i])) {
  195. status =
  196. acpi_ev_execute_reg_methods(acpi_gbl_root_node,
  197. acpi_gbl_default_address_spaces
  198. [i]);
  199. }
  200. }
  201. acpi_gbl_reg_methods_executed = TRUE;
  202. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  203. return_ACPI_STATUS(status);
  204. }
  205. /*******************************************************************************
  206. *
  207. * FUNCTION: acpi_ev_execute_reg_method
  208. *
  209. * PARAMETERS: region_obj - Region object
  210. * Function - Passed to _REG: On (1) or Off (0)
  211. *
  212. * RETURN: Status
  213. *
  214. * DESCRIPTION: Execute _REG method for a region
  215. *
  216. ******************************************************************************/
  217. acpi_status
  218. acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
  219. {
  220. struct acpi_evaluate_info *info;
  221. union acpi_operand_object *args[3];
  222. union acpi_operand_object *region_obj2;
  223. acpi_status status;
  224. ACPI_FUNCTION_TRACE(ev_execute_reg_method);
  225. region_obj2 = acpi_ns_get_secondary_object(region_obj);
  226. if (!region_obj2) {
  227. return_ACPI_STATUS(AE_NOT_EXIST);
  228. }
  229. if (region_obj2->extra.method_REG == NULL) {
  230. return_ACPI_STATUS(AE_OK);
  231. }
  232. /* Allocate and initialize the evaluation information block */
  233. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  234. if (!info) {
  235. return_ACPI_STATUS(AE_NO_MEMORY);
  236. }
  237. info->prefix_node = region_obj2->extra.method_REG;
  238. info->pathname = NULL;
  239. info->parameters = args;
  240. info->flags = ACPI_IGNORE_RETURN_VALUE;
  241. /*
  242. * The _REG method has two arguments:
  243. *
  244. * Arg0 - Integer:
  245. * Operation region space ID Same value as region_obj->Region.space_id
  246. *
  247. * Arg1 - Integer:
  248. * connection status 1 for connecting the handler, 0 for disconnecting
  249. * the handler (Passed as a parameter)
  250. */
  251. args[0] =
  252. acpi_ut_create_integer_object((u64) region_obj->region.space_id);
  253. if (!args[0]) {
  254. status = AE_NO_MEMORY;
  255. goto cleanup1;
  256. }
  257. args[1] = acpi_ut_create_integer_object((u64) function);
  258. if (!args[1]) {
  259. status = AE_NO_MEMORY;
  260. goto cleanup2;
  261. }
  262. args[2] = NULL; /* Terminate list */
  263. /* Execute the method, no return value */
  264. ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
  265. (ACPI_TYPE_METHOD, info->prefix_node, NULL));
  266. status = acpi_ns_evaluate(info);
  267. acpi_ut_remove_reference(args[1]);
  268. cleanup2:
  269. acpi_ut_remove_reference(args[0]);
  270. cleanup1:
  271. ACPI_FREE(info);
  272. return_ACPI_STATUS(status);
  273. }
  274. /*******************************************************************************
  275. *
  276. * FUNCTION: acpi_ev_address_space_dispatch
  277. *
  278. * PARAMETERS: region_obj - Internal region object
  279. * Function - Read or Write operation
  280. * region_offset - Where in the region to read or write
  281. * bit_width - Field width in bits (8, 16, 32, or 64)
  282. * Value - Pointer to in or out value, must be
  283. * a full 64-bit integer
  284. *
  285. * RETURN: Status
  286. *
  287. * DESCRIPTION: Dispatch an address space or operation region access to
  288. * a previously installed handler.
  289. *
  290. ******************************************************************************/
  291. acpi_status
  292. acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
  293. u32 function,
  294. u32 region_offset, u32 bit_width, u64 *value)
  295. {
  296. acpi_status status;
  297. acpi_adr_space_handler handler;
  298. acpi_adr_space_setup region_setup;
  299. union acpi_operand_object *handler_desc;
  300. union acpi_operand_object *region_obj2;
  301. void *region_context = NULL;
  302. ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
  303. region_obj2 = acpi_ns_get_secondary_object(region_obj);
  304. if (!region_obj2) {
  305. return_ACPI_STATUS(AE_NOT_EXIST);
  306. }
  307. /* Ensure that there is a handler associated with this region */
  308. handler_desc = region_obj->region.handler;
  309. if (!handler_desc) {
  310. ACPI_ERROR((AE_INFO,
  311. "No handler for Region [%4.4s] (%p) [%s]",
  312. acpi_ut_get_node_name(region_obj->region.node),
  313. region_obj,
  314. acpi_ut_get_region_name(region_obj->region.
  315. space_id)));
  316. return_ACPI_STATUS(AE_NOT_EXIST);
  317. }
  318. /*
  319. * It may be the case that the region has never been initialized.
  320. * Some types of regions require special init code
  321. */
  322. if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
  323. /* This region has not been initialized yet, do it */
  324. region_setup = handler_desc->address_space.setup;
  325. if (!region_setup) {
  326. /* No initialization routine, exit with error */
  327. ACPI_ERROR((AE_INFO,
  328. "No init routine for region(%p) [%s]",
  329. region_obj,
  330. acpi_ut_get_region_name(region_obj->region.
  331. space_id)));
  332. return_ACPI_STATUS(AE_NOT_EXIST);
  333. }
  334. /*
  335. * We must exit the interpreter because the region setup will
  336. * potentially execute control methods (for example, the _REG method
  337. * for this region)
  338. */
  339. acpi_ex_exit_interpreter();
  340. status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
  341. handler_desc->address_space.context,
  342. &region_context);
  343. /* Re-enter the interpreter */
  344. acpi_ex_enter_interpreter();
  345. /* Check for failure of the Region Setup */
  346. if (ACPI_FAILURE(status)) {
  347. ACPI_EXCEPTION((AE_INFO, status,
  348. "During region initialization: [%s]",
  349. acpi_ut_get_region_name(region_obj->
  350. region.
  351. space_id)));
  352. return_ACPI_STATUS(status);
  353. }
  354. /* Region initialization may have been completed by region_setup */
  355. if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
  356. region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
  357. if (region_obj2->extra.region_context) {
  358. /* The handler for this region was already installed */
  359. ACPI_FREE(region_context);
  360. } else {
  361. /*
  362. * Save the returned context for use in all accesses to
  363. * this particular region
  364. */
  365. region_obj2->extra.region_context =
  366. region_context;
  367. }
  368. }
  369. }
  370. /* We have everything we need, we can invoke the address space handler */
  371. handler = handler_desc->address_space.handler;
  372. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  373. "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
  374. &region_obj->region.handler->address_space, handler,
  375. ACPI_FORMAT_NATIVE_UINT(region_obj->region.address +
  376. region_offset),
  377. acpi_ut_get_region_name(region_obj->region.
  378. space_id)));
  379. if (!(handler_desc->address_space.handler_flags &
  380. ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
  381. /*
  382. * For handlers other than the default (supplied) handlers, we must
  383. * exit the interpreter because the handler *might* block -- we don't
  384. * know what it will do, so we can't hold the lock on the intepreter.
  385. */
  386. acpi_ex_exit_interpreter();
  387. }
  388. /* Call the handler */
  389. status = handler(function,
  390. (region_obj->region.address + region_offset),
  391. bit_width, value, handler_desc->address_space.context,
  392. region_obj2->extra.region_context);
  393. if (ACPI_FAILURE(status)) {
  394. ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
  395. acpi_ut_get_region_name(region_obj->region.
  396. space_id)));
  397. }
  398. if (!(handler_desc->address_space.handler_flags &
  399. ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
  400. /*
  401. * We just returned from a non-default handler, we must re-enter the
  402. * interpreter
  403. */
  404. acpi_ex_enter_interpreter();
  405. }
  406. return_ACPI_STATUS(status);
  407. }
  408. /*******************************************************************************
  409. *
  410. * FUNCTION: acpi_ev_detach_region
  411. *
  412. * PARAMETERS: region_obj - Region Object
  413. * acpi_ns_is_locked - Namespace Region Already Locked?
  414. *
  415. * RETURN: None
  416. *
  417. * DESCRIPTION: Break the association between the handler and the region
  418. * this is a two way association.
  419. *
  420. ******************************************************************************/
  421. void
  422. acpi_ev_detach_region(union acpi_operand_object *region_obj,
  423. u8 acpi_ns_is_locked)
  424. {
  425. union acpi_operand_object *handler_obj;
  426. union acpi_operand_object *obj_desc;
  427. union acpi_operand_object **last_obj_ptr;
  428. acpi_adr_space_setup region_setup;
  429. void **region_context;
  430. union acpi_operand_object *region_obj2;
  431. acpi_status status;
  432. ACPI_FUNCTION_TRACE(ev_detach_region);
  433. region_obj2 = acpi_ns_get_secondary_object(region_obj);
  434. if (!region_obj2) {
  435. return_VOID;
  436. }
  437. region_context = &region_obj2->extra.region_context;
  438. /* Get the address handler from the region object */
  439. handler_obj = region_obj->region.handler;
  440. if (!handler_obj) {
  441. /* This region has no handler, all done */
  442. return_VOID;
  443. }
  444. /* Find this region in the handler's list */
  445. obj_desc = handler_obj->address_space.region_list;
  446. last_obj_ptr = &handler_obj->address_space.region_list;
  447. while (obj_desc) {
  448. /* Is this the correct Region? */
  449. if (obj_desc == region_obj) {
  450. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  451. "Removing Region %p from address handler %p\n",
  452. region_obj, handler_obj));
  453. /* This is it, remove it from the handler's list */
  454. *last_obj_ptr = obj_desc->region.next;
  455. obj_desc->region.next = NULL; /* Must clear field */
  456. if (acpi_ns_is_locked) {
  457. status =
  458. acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  459. if (ACPI_FAILURE(status)) {
  460. return_VOID;
  461. }
  462. }
  463. /* Now stop region accesses by executing the _REG method */
  464. status =
  465. acpi_ev_execute_reg_method(region_obj,
  466. ACPI_REG_DISCONNECT);
  467. if (ACPI_FAILURE(status)) {
  468. ACPI_EXCEPTION((AE_INFO, status,
  469. "from region _REG, [%s]",
  470. acpi_ut_get_region_name
  471. (region_obj->region.space_id)));
  472. }
  473. if (acpi_ns_is_locked) {
  474. status =
  475. acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  476. if (ACPI_FAILURE(status)) {
  477. return_VOID;
  478. }
  479. }
  480. /*
  481. * If the region has been activated, call the setup handler with
  482. * the deactivate notification
  483. */
  484. if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
  485. region_setup = handler_obj->address_space.setup;
  486. status =
  487. region_setup(region_obj,
  488. ACPI_REGION_DEACTIVATE,
  489. handler_obj->address_space.
  490. context, region_context);
  491. /* Init routine may fail, Just ignore errors */
  492. if (ACPI_FAILURE(status)) {
  493. ACPI_EXCEPTION((AE_INFO, status,
  494. "from region handler - deactivate, [%s]",
  495. acpi_ut_get_region_name
  496. (region_obj->region.
  497. space_id)));
  498. }
  499. region_obj->region.flags &=
  500. ~(AOPOBJ_SETUP_COMPLETE);
  501. }
  502. /*
  503. * Remove handler reference in the region
  504. *
  505. * NOTE: this doesn't mean that the region goes away, the region
  506. * is just inaccessible as indicated to the _REG method
  507. *
  508. * If the region is on the handler's list, this must be the
  509. * region's handler
  510. */
  511. region_obj->region.handler = NULL;
  512. acpi_ut_remove_reference(handler_obj);
  513. return_VOID;
  514. }
  515. /* Walk the linked list of handlers */
  516. last_obj_ptr = &obj_desc->region.next;
  517. obj_desc = obj_desc->region.next;
  518. }
  519. /* If we get here, the region was not in the handler's region list */
  520. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  521. "Cannot remove region %p from address handler %p\n",
  522. region_obj, handler_obj));
  523. return_VOID;
  524. }
  525. /*******************************************************************************
  526. *
  527. * FUNCTION: acpi_ev_attach_region
  528. *
  529. * PARAMETERS: handler_obj - Handler Object
  530. * region_obj - Region Object
  531. * acpi_ns_is_locked - Namespace Region Already Locked?
  532. *
  533. * RETURN: None
  534. *
  535. * DESCRIPTION: Create the association between the handler and the region
  536. * this is a two way association.
  537. *
  538. ******************************************************************************/
  539. acpi_status
  540. acpi_ev_attach_region(union acpi_operand_object *handler_obj,
  541. union acpi_operand_object *region_obj,
  542. u8 acpi_ns_is_locked)
  543. {
  544. ACPI_FUNCTION_TRACE(ev_attach_region);
  545. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  546. "Adding Region [%4.4s] %p to address handler %p [%s]\n",
  547. acpi_ut_get_node_name(region_obj->region.node),
  548. region_obj, handler_obj,
  549. acpi_ut_get_region_name(region_obj->region.
  550. space_id)));
  551. /* Link this region to the front of the handler's list */
  552. region_obj->region.next = handler_obj->address_space.region_list;
  553. handler_obj->address_space.region_list = region_obj;
  554. /* Install the region's handler */
  555. if (region_obj->region.handler) {
  556. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  557. }
  558. region_obj->region.handler = handler_obj;
  559. acpi_ut_add_reference(handler_obj);
  560. return_ACPI_STATUS(AE_OK);
  561. }
  562. /*******************************************************************************
  563. *
  564. * FUNCTION: acpi_ev_install_handler
  565. *
  566. * PARAMETERS: walk_namespace callback
  567. *
  568. * DESCRIPTION: This routine installs an address handler into objects that are
  569. * of type Region or Device.
  570. *
  571. * If the Object is a Device, and the device has a handler of
  572. * the same type then the search is terminated in that branch.
  573. *
  574. * This is because the existing handler is closer in proximity
  575. * to any more regions than the one we are trying to install.
  576. *
  577. ******************************************************************************/
  578. static acpi_status
  579. acpi_ev_install_handler(acpi_handle obj_handle,
  580. u32 level, void *context, void **return_value)
  581. {
  582. union acpi_operand_object *handler_obj;
  583. union acpi_operand_object *next_handler_obj;
  584. union acpi_operand_object *obj_desc;
  585. struct acpi_namespace_node *node;
  586. acpi_status status;
  587. ACPI_FUNCTION_NAME(ev_install_handler);
  588. handler_obj = (union acpi_operand_object *)context;
  589. /* Parameter validation */
  590. if (!handler_obj) {
  591. return (AE_OK);
  592. }
  593. /* Convert and validate the device handle */
  594. node = acpi_ns_validate_handle(obj_handle);
  595. if (!node) {
  596. return (AE_BAD_PARAMETER);
  597. }
  598. /*
  599. * We only care about regions and objects that are allowed to have
  600. * address space handlers
  601. */
  602. if ((node->type != ACPI_TYPE_DEVICE) &&
  603. (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  604. return (AE_OK);
  605. }
  606. /* Check for an existing internal object */
  607. obj_desc = acpi_ns_get_attached_object(node);
  608. if (!obj_desc) {
  609. /* No object, just exit */
  610. return (AE_OK);
  611. }
  612. /* Devices are handled different than regions */
  613. if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
  614. /* Check if this Device already has a handler for this address space */
  615. next_handler_obj = obj_desc->device.handler;
  616. while (next_handler_obj) {
  617. /* Found a handler, is it for the same address space? */
  618. if (next_handler_obj->address_space.space_id ==
  619. handler_obj->address_space.space_id) {
  620. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  621. "Found handler for region [%s] in device %p(%p) "
  622. "handler %p\n",
  623. acpi_ut_get_region_name
  624. (handler_obj->address_space.
  625. space_id), obj_desc,
  626. next_handler_obj,
  627. handler_obj));
  628. /*
  629. * Since the object we found it on was a device, then it
  630. * means that someone has already installed a handler for
  631. * the branch of the namespace from this device on. Just
  632. * bail out telling the walk routine to not traverse this
  633. * branch. This preserves the scoping rule for handlers.
  634. */
  635. return (AE_CTRL_DEPTH);
  636. }
  637. /* Walk the linked list of handlers attached to this device */
  638. next_handler_obj = next_handler_obj->address_space.next;
  639. }
  640. /*
  641. * As long as the device didn't have a handler for this space we
  642. * don't care about it. We just ignore it and proceed.
  643. */
  644. return (AE_OK);
  645. }
  646. /* Object is a Region */
  647. if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
  648. /* This region is for a different address space, just ignore it */
  649. return (AE_OK);
  650. }
  651. /*
  652. * Now we have a region and it is for the handler's address space type.
  653. *
  654. * First disconnect region for any previous handler (if any)
  655. */
  656. acpi_ev_detach_region(obj_desc, FALSE);
  657. /* Connect the region to the new handler */
  658. status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
  659. return (status);
  660. }
  661. /*******************************************************************************
  662. *
  663. * FUNCTION: acpi_ev_install_space_handler
  664. *
  665. * PARAMETERS: Node - Namespace node for the device
  666. * space_id - The address space ID
  667. * Handler - Address of the handler
  668. * Setup - Address of the setup function
  669. * Context - Value passed to the handler on each access
  670. *
  671. * RETURN: Status
  672. *
  673. * DESCRIPTION: Install a handler for all op_regions of a given space_id.
  674. * Assumes namespace is locked
  675. *
  676. ******************************************************************************/
  677. acpi_status
  678. acpi_ev_install_space_handler(struct acpi_namespace_node * node,
  679. acpi_adr_space_type space_id,
  680. acpi_adr_space_handler handler,
  681. acpi_adr_space_setup setup, void *context)
  682. {
  683. union acpi_operand_object *obj_desc;
  684. union acpi_operand_object *handler_obj;
  685. acpi_status status;
  686. acpi_object_type type;
  687. u8 flags = 0;
  688. ACPI_FUNCTION_TRACE(ev_install_space_handler);
  689. /*
  690. * This registration is valid for only the types below and the root. This
  691. * is where the default handlers get placed.
  692. */
  693. if ((node->type != ACPI_TYPE_DEVICE) &&
  694. (node->type != ACPI_TYPE_PROCESSOR) &&
  695. (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
  696. status = AE_BAD_PARAMETER;
  697. goto unlock_and_exit;
  698. }
  699. if (handler == ACPI_DEFAULT_HANDLER) {
  700. flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
  701. switch (space_id) {
  702. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  703. handler = acpi_ex_system_memory_space_handler;
  704. setup = acpi_ev_system_memory_region_setup;
  705. break;
  706. case ACPI_ADR_SPACE_SYSTEM_IO:
  707. handler = acpi_ex_system_io_space_handler;
  708. setup = acpi_ev_io_space_region_setup;
  709. break;
  710. case ACPI_ADR_SPACE_PCI_CONFIG:
  711. handler = acpi_ex_pci_config_space_handler;
  712. setup = acpi_ev_pci_config_region_setup;
  713. break;
  714. case ACPI_ADR_SPACE_CMOS:
  715. handler = acpi_ex_cmos_space_handler;
  716. setup = acpi_ev_cmos_region_setup;
  717. break;
  718. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  719. handler = acpi_ex_pci_bar_space_handler;
  720. setup = acpi_ev_pci_bar_region_setup;
  721. break;
  722. case ACPI_ADR_SPACE_DATA_TABLE:
  723. handler = acpi_ex_data_table_space_handler;
  724. setup = NULL;
  725. break;
  726. default:
  727. status = AE_BAD_PARAMETER;
  728. goto unlock_and_exit;
  729. }
  730. }
  731. /* If the caller hasn't specified a setup routine, use the default */
  732. if (!setup) {
  733. setup = acpi_ev_default_region_setup;
  734. }
  735. /* Check for an existing internal object */
  736. obj_desc = acpi_ns_get_attached_object(node);
  737. if (obj_desc) {
  738. /*
  739. * The attached device object already exists. Make sure the handler
  740. * is not already installed.
  741. */
  742. handler_obj = obj_desc->device.handler;
  743. /* Walk the handler list for this device */
  744. while (handler_obj) {
  745. /* Same space_id indicates a handler already installed */
  746. if (handler_obj->address_space.space_id == space_id) {
  747. if (handler_obj->address_space.handler ==
  748. handler) {
  749. /*
  750. * It is (relatively) OK to attempt to install the SAME
  751. * handler twice. This can easily happen with the
  752. * PCI_Config space.
  753. */
  754. status = AE_SAME_HANDLER;
  755. goto unlock_and_exit;
  756. } else {
  757. /* A handler is already installed */
  758. status = AE_ALREADY_EXISTS;
  759. }
  760. goto unlock_and_exit;
  761. }
  762. /* Walk the linked list of handlers */
  763. handler_obj = handler_obj->address_space.next;
  764. }
  765. } else {
  766. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  767. "Creating object on Device %p while installing handler\n",
  768. node));
  769. /* obj_desc does not exist, create one */
  770. if (node->type == ACPI_TYPE_ANY) {
  771. type = ACPI_TYPE_DEVICE;
  772. } else {
  773. type = node->type;
  774. }
  775. obj_desc = acpi_ut_create_internal_object(type);
  776. if (!obj_desc) {
  777. status = AE_NO_MEMORY;
  778. goto unlock_and_exit;
  779. }
  780. /* Init new descriptor */
  781. obj_desc->common.type = (u8) type;
  782. /* Attach the new object to the Node */
  783. status = acpi_ns_attach_object(node, obj_desc, type);
  784. /* Remove local reference to the object */
  785. acpi_ut_remove_reference(obj_desc);
  786. if (ACPI_FAILURE(status)) {
  787. goto unlock_and_exit;
  788. }
  789. }
  790. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  791. "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
  792. acpi_ut_get_region_name(space_id), space_id,
  793. acpi_ut_get_node_name(node), node, obj_desc));
  794. /*
  795. * Install the handler
  796. *
  797. * At this point there is no existing handler. Just allocate the object
  798. * for the handler and link it into the list.
  799. */
  800. handler_obj =
  801. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
  802. if (!handler_obj) {
  803. status = AE_NO_MEMORY;
  804. goto unlock_and_exit;
  805. }
  806. /* Init handler obj */
  807. handler_obj->address_space.space_id = (u8) space_id;
  808. handler_obj->address_space.handler_flags = flags;
  809. handler_obj->address_space.region_list = NULL;
  810. handler_obj->address_space.node = node;
  811. handler_obj->address_space.handler = handler;
  812. handler_obj->address_space.context = context;
  813. handler_obj->address_space.setup = setup;
  814. /* Install at head of Device.address_space list */
  815. handler_obj->address_space.next = obj_desc->device.handler;
  816. /*
  817. * The Device object is the first reference on the handler_obj.
  818. * Each region that uses the handler adds a reference.
  819. */
  820. obj_desc->device.handler = handler_obj;
  821. /*
  822. * Walk the namespace finding all of the regions this
  823. * handler will manage.
  824. *
  825. * Start at the device and search the branch toward
  826. * the leaf nodes until either the leaf is encountered or
  827. * a device is detected that has an address handler of the
  828. * same type.
  829. *
  830. * In either case, back up and search down the remainder
  831. * of the branch
  832. */
  833. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  834. ACPI_NS_WALK_UNLOCK,
  835. acpi_ev_install_handler, NULL,
  836. handler_obj, NULL);
  837. unlock_and_exit:
  838. return_ACPI_STATUS(status);
  839. }
  840. /*******************************************************************************
  841. *
  842. * FUNCTION: acpi_ev_execute_reg_methods
  843. *
  844. * PARAMETERS: Node - Namespace node for the device
  845. * space_id - The address space ID
  846. *
  847. * RETURN: Status
  848. *
  849. * DESCRIPTION: Run all _REG methods for the input Space ID;
  850. * Note: assumes namespace is locked, or system init time.
  851. *
  852. ******************************************************************************/
  853. acpi_status
  854. acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
  855. acpi_adr_space_type space_id)
  856. {
  857. acpi_status status;
  858. ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
  859. /*
  860. * Run all _REG methods for all Operation Regions for this space ID. This
  861. * is a separate walk in order to handle any interdependencies between
  862. * regions and _REG methods. (i.e. handlers must be installed for all
  863. * regions of this Space ID before we can run any _REG methods)
  864. */
  865. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  866. ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
  867. NULL, &space_id, NULL);
  868. /* Special case for EC: handle "orphan" _REG methods with no region */
  869. if (space_id == ACPI_ADR_SPACE_EC) {
  870. acpi_ev_orphan_ec_reg_method();
  871. }
  872. return_ACPI_STATUS(status);
  873. }
  874. /*******************************************************************************
  875. *
  876. * FUNCTION: acpi_ev_reg_run
  877. *
  878. * PARAMETERS: walk_namespace callback
  879. *
  880. * DESCRIPTION: Run _REG method for region objects of the requested space_iD
  881. *
  882. ******************************************************************************/
  883. static acpi_status
  884. acpi_ev_reg_run(acpi_handle obj_handle,
  885. u32 level, void *context, void **return_value)
  886. {
  887. union acpi_operand_object *obj_desc;
  888. struct acpi_namespace_node *node;
  889. acpi_adr_space_type space_id;
  890. acpi_status status;
  891. space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
  892. /* Convert and validate the device handle */
  893. node = acpi_ns_validate_handle(obj_handle);
  894. if (!node) {
  895. return (AE_BAD_PARAMETER);
  896. }
  897. /*
  898. * We only care about regions.and objects that are allowed to have address
  899. * space handlers
  900. */
  901. if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  902. return (AE_OK);
  903. }
  904. /* Check for an existing internal object */
  905. obj_desc = acpi_ns_get_attached_object(node);
  906. if (!obj_desc) {
  907. /* No object, just exit */
  908. return (AE_OK);
  909. }
  910. /* Object is a Region */
  911. if (obj_desc->region.space_id != space_id) {
  912. /* This region is for a different address space, just ignore it */
  913. return (AE_OK);
  914. }
  915. status = acpi_ev_execute_reg_method(obj_desc, ACPI_REG_CONNECT);
  916. return (status);
  917. }
  918. /*******************************************************************************
  919. *
  920. * FUNCTION: acpi_ev_orphan_ec_reg_method
  921. *
  922. * PARAMETERS: None
  923. *
  924. * RETURN: None
  925. *
  926. * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
  927. * device. This is a _REG method that has no corresponding region
  928. * within the EC device scope. The orphan _REG method appears to
  929. * have been enabled by the description of the ECDT in the ACPI
  930. * specification: "The availability of the region space can be
  931. * detected by providing a _REG method object underneath the
  932. * Embedded Controller device."
  933. *
  934. * To quickly access the EC device, we use the EC_ID that appears
  935. * within the ECDT. Otherwise, we would need to perform a time-
  936. * consuming namespace walk, executing _HID methods to find the
  937. * EC device.
  938. *
  939. ******************************************************************************/
  940. static void acpi_ev_orphan_ec_reg_method(void)
  941. {
  942. struct acpi_table_ecdt *table;
  943. acpi_status status;
  944. struct acpi_object_list args;
  945. union acpi_object objects[2];
  946. struct acpi_namespace_node *ec_device_node;
  947. struct acpi_namespace_node *reg_method;
  948. struct acpi_namespace_node *next_node;
  949. ACPI_FUNCTION_TRACE(ev_orphan_ec_reg_method);
  950. /* Get the ECDT (if present in system) */
  951. status = acpi_get_table(ACPI_SIG_ECDT, 0,
  952. ACPI_CAST_INDIRECT_PTR(struct acpi_table_header,
  953. &table));
  954. if (ACPI_FAILURE(status)) {
  955. return_VOID;
  956. }
  957. /* We need a valid EC_ID string */
  958. if (!(*table->id)) {
  959. return_VOID;
  960. }
  961. /* Namespace is currently locked, must release */
  962. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  963. /* Get a handle to the EC device referenced in the ECDT */
  964. status = acpi_get_handle(NULL,
  965. ACPI_CAST_PTR(char, table->id),
  966. ACPI_CAST_PTR(acpi_handle, &ec_device_node));
  967. if (ACPI_FAILURE(status)) {
  968. goto exit;
  969. }
  970. /* Get a handle to a _REG method immediately under the EC device */
  971. status = acpi_get_handle(ec_device_node,
  972. METHOD_NAME__REG, ACPI_CAST_PTR(acpi_handle,
  973. &reg_method));
  974. if (ACPI_FAILURE(status)) {
  975. goto exit;
  976. }
  977. /*
  978. * Execute the _REG method only if there is no Operation Region in
  979. * this scope with the Embedded Controller space ID. Otherwise, it
  980. * will already have been executed. Note, this allows for Regions
  981. * with other space IDs to be present; but the code below will then
  982. * execute the _REG method with the EC space ID argument.
  983. */
  984. next_node = acpi_ns_get_next_node(ec_device_node, NULL);
  985. while (next_node) {
  986. if ((next_node->type == ACPI_TYPE_REGION) &&
  987. (next_node->object) &&
  988. (next_node->object->region.space_id == ACPI_ADR_SPACE_EC)) {
  989. goto exit; /* Do not execute _REG */
  990. }
  991. next_node = acpi_ns_get_next_node(ec_device_node, next_node);
  992. }
  993. /* Evaluate the _REG(EC,Connect) method */
  994. args.count = 2;
  995. args.pointer = objects;
  996. objects[0].type = ACPI_TYPE_INTEGER;
  997. objects[0].integer.value = ACPI_ADR_SPACE_EC;
  998. objects[1].type = ACPI_TYPE_INTEGER;
  999. objects[1].integer.value = ACPI_REG_CONNECT;
  1000. status = acpi_evaluate_object(reg_method, NULL, &args, NULL);
  1001. exit:
  1002. /* We ignore all errors from above, don't care */
  1003. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  1004. return_VOID;
  1005. }