evregion.c 29 KB

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