evregion.c 30 KB

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