evregion.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:18k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /******************************************************************************
  2.  *
  3.  * Module Name: evregion - ACPI Address_space (Op_region) handler dispatch
  4.  *              $Revision: 113 $
  5.  *
  6.  *****************************************************************************/
  7. /*
  8.  *  Copyright (C) 2000, 2001 R. Byron Moore
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23.  */
  24. #include "acpi.h"
  25. #include "acevents.h"
  26. #include "acnamesp.h"
  27. #include "acinterp.h"
  28. #include "amlcode.h"
  29. #define _COMPONENT          ACPI_EVENTS
  30.  MODULE_NAME         ("evregion")
  31. /*******************************************************************************
  32.  *
  33.  * FUNCTION:    Acpi_ev_install_default_address_space_handlers
  34.  *
  35.  * PARAMETERS:
  36.  *
  37.  * RETURN:      Status
  38.  *
  39.  * DESCRIPTION: Installs the core subsystem address space handlers.
  40.  *
  41.  ******************************************************************************/
  42. acpi_status
  43. acpi_ev_install_default_address_space_handlers (
  44. void)
  45. {
  46. acpi_status             status;
  47. FUNCTION_TRACE ("Ev_install_default_address_space_handlers");
  48. /*
  49.  * All address spaces (PCI Config, EC, SMBus) are scope dependent
  50.  * and registration must occur for a specific device.  In the case
  51.  * system memory and IO address spaces there is currently no device
  52.  * associated with the address space.  For these we use the root.
  53.  * We install the default PCI config space handler at the root so
  54.  * that this space is immediately available even though the we have
  55.  * not enumerated all the PCI Root Buses yet.  This is to conform
  56.  * to the ACPI specification which states that the PCI config
  57.  * space must be always available -- even though we are nowhere
  58.  * near ready to find the PCI root buses at this point.
  59.  *
  60.  * NOTE: We ignore AE_EXIST because this means that a handler has
  61.  * already been installed (via Acpi_install_address_space_handler)
  62.  */
  63. status = acpi_install_address_space_handler (acpi_gbl_root_node,
  64.    ACPI_ADR_SPACE_SYSTEM_MEMORY,
  65.    ACPI_DEFAULT_HANDLER, NULL, NULL);
  66. if ((ACPI_FAILURE (status)) &&
  67. (status != AE_EXIST)) {
  68. return_ACPI_STATUS (status);
  69. }
  70. status = acpi_install_address_space_handler (acpi_gbl_root_node,
  71.    ACPI_ADR_SPACE_SYSTEM_IO,
  72.    ACPI_DEFAULT_HANDLER, NULL, NULL);
  73. if ((ACPI_FAILURE (status)) &&
  74. (status != AE_EXIST)) {
  75. return_ACPI_STATUS (status);
  76. }
  77. status = acpi_install_address_space_handler (acpi_gbl_root_node,
  78.    ACPI_ADR_SPACE_PCI_CONFIG,
  79.    ACPI_DEFAULT_HANDLER, NULL, NULL);
  80. if ((ACPI_FAILURE (status)) &&
  81. (status != AE_EXIST)) {
  82. return_ACPI_STATUS (status);
  83. }
  84. return_ACPI_STATUS (AE_OK);
  85. }
  86. /* TBD: [Restructure] Move elsewhere */
  87. /*******************************************************************************
  88.  *
  89.  * FUNCTION:    Acpi_ev_execute_reg_method
  90.  *
  91.  * PARAMETERS:  Region_obj          - Object structure
  92.  *              Function            - On (1) or Off (0)
  93.  *
  94.  * RETURN:      Status
  95.  *
  96.  * DESCRIPTION: Execute _REG method for a region
  97.  *
  98.  ******************************************************************************/
  99. static acpi_status
  100. acpi_ev_execute_reg_method (
  101. acpi_operand_object    *region_obj,
  102. u32                     function)
  103. {
  104. acpi_operand_object    *params[3];
  105. acpi_status             status;
  106. FUNCTION_TRACE ("Ev_execute_reg_method");
  107. if (region_obj->region.extra->extra.method_REG == NULL) {
  108. return_ACPI_STATUS (AE_OK);
  109. }
  110. /*
  111.  *  _REG method has two arguments
  112.  *  Arg0:   Integer: Operation region space ID
  113.  *          Same value as Region_obj->Region.Space_id
  114.  *  Arg1:   Integer: connection status
  115.  *          1 for connecting the handler,
  116.  *          0 for disconnecting the handler
  117.  *          Passed as a parameter
  118.  */
  119. params[0] = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
  120. if (!params[0]) {
  121. return_ACPI_STATUS (AE_NO_MEMORY);
  122. }
  123. params[1] = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
  124. if (!params[1]) {
  125. status = AE_NO_MEMORY;
  126. goto cleanup;
  127. }
  128. /*
  129.  *  Set up the parameter objects
  130.  */
  131. params[0]->integer.value  = region_obj->region.space_id;
  132. params[1]->integer.value = function;
  133. params[2] = NULL;
  134. /*
  135.  *  Execute the method, no return value
  136.  */
  137. DEBUG_EXEC(acpi_ut_display_init_pathname (region_obj->region.extra->extra.method_REG, " [Method]"));
  138. status = acpi_ns_evaluate_by_handle (region_obj->region.extra->extra.method_REG, params, NULL);
  139. acpi_ut_remove_reference (params[1]);
  140. cleanup:
  141. acpi_ut_remove_reference (params[0]);
  142. return_ACPI_STATUS (status);
  143. }
  144. /*******************************************************************************
  145.  *
  146.  * FUNCTION:    Acpi_ev_address_space_dispatch
  147.  *
  148.  * PARAMETERS:  Region_obj          - internal region object
  149.  *              Space_id            - ID of the address space (0-255)
  150.  *              Function            - Read or Write operation
  151.  *              Address             - Where in the space to read or write
  152.  *              Bit_width           - Field width in bits (8, 16, or 32)
  153.  *              Value               - Pointer to in or out value
  154.  *
  155.  * RETURN:      Status
  156.  *
  157.  * DESCRIPTION: Dispatch an address space or operation region access to
  158.  *              a previously installed handler.
  159.  *
  160.  ******************************************************************************/
  161. acpi_status
  162. acpi_ev_address_space_dispatch (
  163. acpi_operand_object     *region_obj,
  164. u32                     function,
  165. ACPI_PHYSICAL_ADDRESS   address,
  166. u32                     bit_width,
  167. u32                     *value)
  168. {
  169. acpi_status             status;
  170. acpi_adr_space_handler  handler;
  171. acpi_adr_space_setup    region_setup;
  172. acpi_operand_object     *handler_desc;
  173. void                    *region_context = NULL;
  174. FUNCTION_TRACE ("Ev_address_space_dispatch");
  175. /*
  176.  * Ensure that there is a handler associated with this region
  177.  */
  178. handler_desc = region_obj->region.addr_handler;
  179. if (!handler_desc) {
  180. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "no handler for region(%p) [%s]n",
  181. region_obj, acpi_ut_get_region_name (region_obj->region.space_id)));
  182. return_ACPI_STATUS(AE_NOT_EXIST);
  183. }
  184. /*
  185.  * It may be the case that the region has never been initialized
  186.  * Some types of regions require special init code
  187.  */
  188. if (!(region_obj->region.flags & AOPOBJ_INITIALIZED)) {
  189. /*
  190.  * This region has not been initialized yet, do it
  191.  */
  192. region_setup = handler_desc->addr_handler.setup;
  193. if (!region_setup) {
  194. /*
  195.  *  Bad news, no init routine and not init'd
  196.  */
  197. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "No init routine for region(%p) [%s]n",
  198. region_obj, acpi_ut_get_region_name (region_obj->region.space_id)));
  199. return_ACPI_STATUS (AE_UNKNOWN_STATUS);
  200. }
  201. /*
  202.  * We must exit the interpreter because the region setup will potentially
  203.  * execute control methods
  204.  */
  205. acpi_ex_exit_interpreter ();
  206. status = region_setup (region_obj, ACPI_REGION_ACTIVATE,
  207.   handler_desc->addr_handler.context, &region_context);
  208. /* Re-enter the interpreter */
  209. acpi_ex_enter_interpreter ();
  210. /*
  211.  *  Init routine may fail
  212.  */
  213. if (ACPI_FAILURE (status)) {
  214. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Region Init: %s [%s]n",
  215. acpi_format_exception (status),
  216. acpi_ut_get_region_name (region_obj->region.space_id)));
  217. return_ACPI_STATUS(status);
  218. }
  219. region_obj->region.flags |= AOPOBJ_INITIALIZED;
  220. /*
  221.  *  Save the returned context for use in all accesses to
  222.  *  this particular region.
  223.  */
  224. region_obj->region.extra->extra.region_context = region_context;
  225. }
  226. /*
  227.  *  We have everything we need, begin the process
  228.  */
  229. handler = handler_desc->addr_handler.handler;
  230. ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
  231. "Addrhandler %p (%p), Address %8.8X%8.8Xn",
  232. &region_obj->region.addr_handler->addr_handler, handler, HIDWORD(address),
  233. LODWORD(address)));
  234. if (!(handler_desc->addr_handler.flags & ADDR_HANDLER_DEFAULT_INSTALLED)) {
  235. /*
  236.  *  For handlers other than the default (supplied) handlers, we must
  237.  *  exit the interpreter because the handler *might* block -- we don't
  238.  *  know what it will do, so we can't hold the lock on the intepreter.
  239.  */
  240. acpi_ex_exit_interpreter();
  241. }
  242. /*
  243.  *  Invoke the handler.
  244.  */
  245. status = handler (function, address, bit_width, value,
  246.  handler_desc->addr_handler.context,
  247.  region_obj->region.extra->extra.region_context);
  248. if (ACPI_FAILURE (status)) {
  249. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Region handler: %s [%s]n",
  250. acpi_format_exception (status),
  251. acpi_ut_get_region_name (region_obj->region.space_id)));
  252. }
  253. if (!(handler_desc->addr_handler.flags & ADDR_HANDLER_DEFAULT_INSTALLED)) {
  254. /*
  255.  * We just returned from a non-default handler, we must re-enter the
  256.  * interpreter
  257.  */
  258. acpi_ex_enter_interpreter ();
  259. }
  260. return_ACPI_STATUS (status);
  261. }
  262. /*******************************************************************************
  263.  *
  264.  * FUNCTION:    Acpi_ev_disassociate_region_from_handler
  265.  *
  266.  * PARAMETERS:  Region_obj      - Region Object
  267.  *              Acpi_ns_is_locked - Namespace Region Already Locked?
  268.  *
  269.  * RETURN:      None
  270.  *
  271.  * DESCRIPTION: Break the association between the handler and the region
  272.  *              this is a two way association.
  273.  *
  274.  ******************************************************************************/
  275. void
  276. acpi_ev_disassociate_region_from_handler(
  277. acpi_operand_object     *region_obj,
  278. u8                      acpi_ns_is_locked)
  279. {
  280. acpi_operand_object     *handler_obj;
  281. acpi_operand_object     *obj_desc;
  282. acpi_operand_object     **last_obj_ptr;
  283. acpi_adr_space_setup    region_setup;
  284. void                    *region_context;
  285. acpi_status             status;
  286. FUNCTION_TRACE ("Ev_disassociate_region_from_handler");
  287. region_context = region_obj->region.extra->extra.region_context;
  288. /*
  289.  *  Get the address handler from the region object
  290.  */
  291. handler_obj = region_obj->region.addr_handler;
  292. if (!handler_obj) {
  293. /*
  294.  *  This region has no handler, all done
  295.  */
  296. return_VOID;
  297. }
  298. /*
  299.  *  Find this region in the handler's list
  300.  */
  301. obj_desc = handler_obj->addr_handler.region_list;
  302. last_obj_ptr = &handler_obj->addr_handler.region_list;
  303. while (obj_desc) {
  304. /*
  305.  *  See if this is the one
  306.  */
  307. if (obj_desc == region_obj) {
  308. ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
  309. "Removing Region %p from address handler %pn",
  310. region_obj, handler_obj));
  311. /*
  312.  *  This is it, remove it from the handler's list
  313.  */
  314. *last_obj_ptr = obj_desc->region.next;
  315. obj_desc->region.next = NULL;           /* Must clear field */
  316. if (acpi_ns_is_locked) {
  317. acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  318. }
  319. /*
  320.  *  Now stop region accesses by executing the _REG method
  321.  */
  322. acpi_ev_execute_reg_method (region_obj, 0);
  323. if (acpi_ns_is_locked) {
  324. acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
  325. }
  326. /*
  327.  *  Call the setup handler with the deactivate notification
  328.  */
  329. region_setup = handler_obj->addr_handler.setup;
  330. status = region_setup (region_obj, ACPI_REGION_DEACTIVATE,
  331.   handler_obj->addr_handler.context, &region_context);
  332. /*
  333.  *  Init routine may fail, Just ignore errors
  334.  */
  335. if (ACPI_FAILURE (status)) {
  336. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "%s from region init, [%s]n",
  337. acpi_format_exception (status),
  338. acpi_ut_get_region_name (region_obj->region.space_id)));
  339. }
  340. region_obj->region.flags &= ~(AOPOBJ_INITIALIZED);
  341. /*
  342.  *  Remove handler reference in the region
  343.  *
  344.  *  NOTE: this doesn't mean that the region goes away
  345.  *  The region is just inaccessible as indicated to
  346.  *  the _REG method
  347.  *
  348.  *  If the region is on the handler's list
  349.  *  this better be the region's handler
  350.  */
  351. region_obj->region.addr_handler = NULL;
  352. return_VOID;
  353. } /* found the right handler */
  354. /*
  355.  *  Move through the linked list of handlers
  356.  */
  357. last_obj_ptr = &obj_desc->region.next;
  358. obj_desc = obj_desc->region.next;
  359. }
  360. /*
  361.  *  If we get here, the region was not in the handler's region list
  362.  */
  363. ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
  364. "Cannot remove region %p from address handler %pn",
  365. region_obj, handler_obj));
  366. return_VOID;
  367. }
  368. /*******************************************************************************
  369.  *
  370.  * FUNCTION:    Acpi_ev_associate_region_and_handler
  371.  *
  372.  * PARAMETERS:  Handler_obj     - Handler Object
  373.  *              Region_obj      - Region Object
  374.  *              Acpi_ns_is_locked - Namespace Region Already Locked?
  375.  *
  376.  * RETURN:      None
  377.  *
  378.  * DESCRIPTION: Create the association between the handler and the region
  379.  *              this is a two way association.
  380.  *
  381.  ******************************************************************************/
  382. acpi_status
  383. acpi_ev_associate_region_and_handler (
  384. acpi_operand_object     *handler_obj,
  385. acpi_operand_object     *region_obj,
  386. u8                      acpi_ns_is_locked)
  387. {
  388. acpi_status     status;
  389. FUNCTION_TRACE ("Ev_associate_region_and_handler");
  390. ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
  391. "Adding Region %p to address handler %p [%s]n",
  392. region_obj, handler_obj, acpi_ut_get_region_name (region_obj->region.space_id)));
  393. /*
  394.  *  Link this region to the front of the handler's list
  395.  */
  396. region_obj->region.next = handler_obj->addr_handler.region_list;
  397. handler_obj->addr_handler.region_list = region_obj;
  398. /*
  399.  *  set the region's handler
  400.  */
  401. region_obj->region.addr_handler = handler_obj;
  402. /*
  403.  *  Last thing, tell all users that this region is usable
  404.  */
  405. if (acpi_ns_is_locked) {
  406. acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  407. }
  408. status = acpi_ev_execute_reg_method (region_obj, 1);
  409. if (acpi_ns_is_locked) {
  410. acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
  411. }
  412. return_ACPI_STATUS (status);
  413. }
  414. /*******************************************************************************
  415.  *
  416.  * FUNCTION:    Acpi_ev_addr_handler_helper
  417.  *
  418.  * PARAMETERS:  Handle              - Node to be dumped
  419.  *              Level               - Nesting level of the handle
  420.  *              Context             - Passed into Acpi_ns_walk_namespace
  421.  *
  422.  * DESCRIPTION: This routine checks to see if the object is a Region if it
  423.  *              is then the address handler is installed in it.
  424.  *
  425.  *              If the Object is a Device, and the device has a handler of
  426.  *              the same type then the search is terminated in that branch.
  427.  *
  428.  *              This is because the existing handler is closer in proximity
  429.  *              to any more regions than the one we are trying to install.
  430.  *
  431.  ******************************************************************************/
  432. acpi_status
  433. acpi_ev_addr_handler_helper (
  434. acpi_handle             obj_handle,
  435. u32                     level,
  436. void                    *context,
  437. void                    **return_value)
  438. {
  439. acpi_operand_object     *handler_obj;
  440. acpi_operand_object     *tmp_obj;
  441. acpi_operand_object     *obj_desc;
  442. acpi_namespace_node     *node;
  443. acpi_status             status;
  444. PROC_NAME ("Ev_addr_handler_helper");
  445. handler_obj = (acpi_operand_object *) context;
  446. /* Parameter validation */
  447. if (!handler_obj) {
  448. return (AE_OK);
  449. }
  450. /* Convert and validate the device handle */
  451. node = acpi_ns_map_handle_to_node (obj_handle);
  452. if (!node) {
  453. return (AE_BAD_PARAMETER);
  454. }
  455. /*
  456.  *  We only care about regions.and objects
  457.  *  that can have address handlers
  458.  */
  459. if ((node->type != ACPI_TYPE_DEVICE) &&
  460. (node->type != ACPI_TYPE_REGION) &&
  461. (node != acpi_gbl_root_node)) {
  462. return (AE_OK);
  463. }
  464. /* Check for an existing internal object */
  465. obj_desc = acpi_ns_get_attached_object (node);
  466. if (!obj_desc) {
  467. /*
  468.  *  The object DNE, we don't care about it
  469.  */
  470. return (AE_OK);
  471. }
  472. /*
  473.  *  Devices are handled different than regions
  474.  */
  475. if (IS_THIS_OBJECT_TYPE (obj_desc, ACPI_TYPE_DEVICE)) {
  476. /*
  477.  *  See if this guy has any handlers
  478.  */
  479. tmp_obj = obj_desc->device.addr_handler;
  480. while (tmp_obj) {
  481. /*
  482.  *  Now let's see if it's for the same address space.
  483.  */
  484. if (tmp_obj->addr_handler.space_id == handler_obj->addr_handler.space_id) {
  485. /*
  486.  *  It's for the same address space
  487.  */
  488. ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
  489. "Found handler for region [%s] in device %p(%p) handler %pn",
  490. acpi_ut_get_region_name (handler_obj->addr_handler.space_id),
  491. obj_desc, tmp_obj, handler_obj));
  492. /*
  493.  *  Since the object we found it on was a device, then it
  494.  *  means that someone has already installed a handler for
  495.  *  the branch of the namespace from this device on.  Just
  496.  *  bail out telling the walk routine to not traverse this
  497.  *  branch.  This preserves the scoping rule for handlers.
  498.  */
  499. return (AE_CTRL_DEPTH);
  500. }
  501. /*
  502.  *  Move through the linked list of handlers
  503.  */
  504. tmp_obj = tmp_obj->addr_handler.next;
  505. }
  506. /*
  507.  *  As long as the device didn't have a handler for this
  508.  *  space we don't care about it.  We just ignore it and
  509.  *  proceed.
  510.  */
  511. return (AE_OK);
  512. }
  513. /*
  514.  *  Only here if it was a region
  515.  */
  516. if (obj_desc->region.space_id != handler_obj->addr_handler.space_id) {
  517. /*
  518.  *  This region is for a different address space
  519.  *  ignore it
  520.  */
  521. return (AE_OK);
  522. }
  523. /*
  524.  *  Now we have a region and it is for the handler's address
  525.  *  space type.
  526.  *
  527.  *  First disconnect region for any previous handler (if any)
  528.  */
  529. acpi_ev_disassociate_region_from_handler (obj_desc, FALSE);
  530. /*
  531.  *  Then connect the region to the new handler
  532.  */
  533. status = acpi_ev_associate_region_and_handler (handler_obj, obj_desc, FALSE);
  534. return (status);
  535. }