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 - 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. };
  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_DEBUG_PRINT((ACPI_DB_ERROR,
  256. "No handler for Region [%4.4s] (%p) [%s]\n",
  257. acpi_ut_get_node_name(region_obj->region.
  258. node), 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_DEBUG_PRINT((ACPI_DB_ERROR,
  275. "No init routine for region(%p) [%s]\n",
  276. region_obj,
  277. acpi_ut_get_region_name(region_obj->
  278. region.
  279. space_id)));
  280. return_ACPI_STATUS(AE_NOT_EXIST);
  281. }
  282. /*
  283. * We must exit the interpreter because the region
  284. * setup will potentially execute control methods
  285. * (e.g., _REG method for this region)
  286. */
  287. acpi_ex_exit_interpreter();
  288. status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
  289. handler_desc->address_space.context,
  290. &region_context);
  291. /* Re-enter the interpreter */
  292. status2 = acpi_ex_enter_interpreter();
  293. if (ACPI_FAILURE(status2)) {
  294. return_ACPI_STATUS(status2);
  295. }
  296. /* Check for failure of the Region Setup */
  297. if (ACPI_FAILURE(status)) {
  298. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  299. "Region Init: %s [%s]\n",
  300. acpi_format_exception(status),
  301. acpi_ut_get_region_name(region_obj->
  302. region.
  303. 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. } else {
  315. /*
  316. * Save the returned context for use in all accesses to
  317. * this particular region
  318. */
  319. region_obj2->extra.region_context =
  320. 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.
  331. space_id)));
  332. if (!
  333. (handler_desc->address_space.
  334. hflags & ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
  335. /*
  336. * For handlers other than the default (supplied) handlers, we must
  337. * exit the interpreter because the handler *might* block -- we don't
  338. * know what it will do, so we can't hold the lock on the intepreter.
  339. */
  340. acpi_ex_exit_interpreter();
  341. }
  342. /* Call the handler */
  343. status = handler(function, address, bit_width, value,
  344. handler_desc->address_space.context,
  345. region_obj2->extra.region_context);
  346. if (ACPI_FAILURE(status)) {
  347. ACPI_REPORT_ERROR(("Handler for [%s] returned %s\n",
  348. acpi_ut_get_region_name(region_obj->region.
  349. space_id),
  350. acpi_format_exception(status)));
  351. }
  352. if (!
  353. (handler_desc->address_space.
  354. hflags & ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
  355. /*
  356. * We just returned from a non-default handler, we must re-enter the
  357. * interpreter
  358. */
  359. status2 = acpi_ex_enter_interpreter();
  360. if (ACPI_FAILURE(status2)) {
  361. return_ACPI_STATUS(status2);
  362. }
  363. }
  364. return_ACPI_STATUS(status);
  365. }
  366. /*******************************************************************************
  367. *
  368. * FUNCTION: acpi_ev_detach_region
  369. *
  370. * PARAMETERS: region_obj - Region Object
  371. * acpi_ns_is_locked - Namespace Region Already Locked?
  372. *
  373. * RETURN: None
  374. *
  375. * DESCRIPTION: Break the association between the handler and the region
  376. * this is a two way association.
  377. *
  378. ******************************************************************************/
  379. void
  380. acpi_ev_detach_region(union acpi_operand_object *region_obj,
  381. u8 acpi_ns_is_locked)
  382. {
  383. union acpi_operand_object *handler_obj;
  384. union acpi_operand_object *obj_desc;
  385. union acpi_operand_object **last_obj_ptr;
  386. acpi_adr_space_setup region_setup;
  387. void **region_context;
  388. union acpi_operand_object *region_obj2;
  389. acpi_status status;
  390. ACPI_FUNCTION_TRACE("ev_detach_region");
  391. region_obj2 = acpi_ns_get_secondary_object(region_obj);
  392. if (!region_obj2) {
  393. return_VOID;
  394. }
  395. region_context = &region_obj2->extra.region_context;
  396. /* Get the address handler from the region object */
  397. handler_obj = region_obj->region.handler;
  398. if (!handler_obj) {
  399. /* This region has no handler, all done */
  400. return_VOID;
  401. }
  402. /* Find this region in the handler's list */
  403. obj_desc = handler_obj->address_space.region_list;
  404. last_obj_ptr = &handler_obj->address_space.region_list;
  405. while (obj_desc) {
  406. /* Is this the correct Region? */
  407. if (obj_desc == region_obj) {
  408. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  409. "Removing Region %p from address handler %p\n",
  410. region_obj, handler_obj));
  411. /* This is it, remove it from the handler's list */
  412. *last_obj_ptr = obj_desc->region.next;
  413. obj_desc->region.next = NULL; /* Must clear field */
  414. if (acpi_ns_is_locked) {
  415. status =
  416. acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  417. if (ACPI_FAILURE(status)) {
  418. return_VOID;
  419. }
  420. }
  421. /* Now stop region accesses by executing the _REG method */
  422. status = acpi_ev_execute_reg_method(region_obj, 0);
  423. if (ACPI_FAILURE(status)) {
  424. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  425. "%s from region _REG, [%s]\n",
  426. acpi_format_exception(status),
  427. acpi_ut_get_region_name
  428. (region_obj->region.
  429. space_id)));
  430. }
  431. if (acpi_ns_is_locked) {
  432. status =
  433. acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  434. if (ACPI_FAILURE(status)) {
  435. return_VOID;
  436. }
  437. }
  438. /* Call the setup handler with the deactivate notification */
  439. region_setup = handler_obj->address_space.setup;
  440. status =
  441. region_setup(region_obj, ACPI_REGION_DEACTIVATE,
  442. handler_obj->address_space.context,
  443. region_context);
  444. /* Init routine may fail, Just ignore errors */
  445. if (ACPI_FAILURE(status)) {
  446. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  447. "%s from region init, [%s]\n",
  448. acpi_format_exception(status),
  449. acpi_ut_get_region_name
  450. (region_obj->region.
  451. space_id)));
  452. }
  453. region_obj->region.flags &= ~(AOPOBJ_SETUP_COMPLETE);
  454. /*
  455. * Remove handler reference in the region
  456. *
  457. * NOTE: this doesn't mean that the region goes away
  458. * The region is just inaccessible as indicated to
  459. * the _REG method
  460. *
  461. * If the region is on the handler's list
  462. * this better be the region's handler
  463. */
  464. region_obj->region.handler = NULL;
  465. acpi_ut_remove_reference(handler_obj);
  466. return_VOID;
  467. }
  468. /* Walk the linked list of handlers */
  469. last_obj_ptr = &obj_desc->region.next;
  470. obj_desc = obj_desc->region.next;
  471. }
  472. /* If we get here, the region was not in the handler's region list */
  473. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  474. "Cannot remove region %p from address handler %p\n",
  475. region_obj, handler_obj));
  476. return_VOID;
  477. }
  478. /*******************************************************************************
  479. *
  480. * FUNCTION: acpi_ev_attach_region
  481. *
  482. * PARAMETERS: handler_obj - Handler Object
  483. * region_obj - Region Object
  484. * acpi_ns_is_locked - Namespace Region Already Locked?
  485. *
  486. * RETURN: None
  487. *
  488. * DESCRIPTION: Create the association between the handler and the region
  489. * this is a two way association.
  490. *
  491. ******************************************************************************/
  492. acpi_status
  493. acpi_ev_attach_region(union acpi_operand_object *handler_obj,
  494. union acpi_operand_object *region_obj,
  495. u8 acpi_ns_is_locked)
  496. {
  497. ACPI_FUNCTION_TRACE("ev_attach_region");
  498. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  499. "Adding Region [%4.4s] %p to address handler %p [%s]\n",
  500. acpi_ut_get_node_name(region_obj->region.node),
  501. region_obj, handler_obj,
  502. acpi_ut_get_region_name(region_obj->region.
  503. space_id)));
  504. /* Link this region to the front of the handler's list */
  505. region_obj->region.next = handler_obj->address_space.region_list;
  506. handler_obj->address_space.region_list = region_obj;
  507. /* Install the region's handler */
  508. if (region_obj->region.handler) {
  509. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  510. }
  511. region_obj->region.handler = handler_obj;
  512. acpi_ut_add_reference(handler_obj);
  513. return_ACPI_STATUS(AE_OK);
  514. }
  515. /*******************************************************************************
  516. *
  517. * FUNCTION: acpi_ev_install_handler
  518. *
  519. * PARAMETERS: walk_namespace callback
  520. *
  521. * DESCRIPTION: This routine installs an address handler into objects that are
  522. * of type Region or Device.
  523. *
  524. * If the Object is a Device, and the device has a handler of
  525. * the same type then the search is terminated in that branch.
  526. *
  527. * This is because the existing handler is closer in proximity
  528. * to any more regions than the one we are trying to install.
  529. *
  530. ******************************************************************************/
  531. static acpi_status
  532. acpi_ev_install_handler(acpi_handle obj_handle,
  533. u32 level, void *context, void **return_value)
  534. {
  535. union acpi_operand_object *handler_obj;
  536. union acpi_operand_object *next_handler_obj;
  537. union acpi_operand_object *obj_desc;
  538. struct acpi_namespace_node *node;
  539. acpi_status status;
  540. ACPI_FUNCTION_NAME("ev_install_handler");
  541. handler_obj = (union acpi_operand_object *)context;
  542. /* Parameter validation */
  543. if (!handler_obj) {
  544. return (AE_OK);
  545. }
  546. /* Convert and validate the device handle */
  547. node = acpi_ns_map_handle_to_node(obj_handle);
  548. if (!node) {
  549. return (AE_BAD_PARAMETER);
  550. }
  551. /*
  552. * We only care about regions.and objects
  553. * that are allowed to have address space handlers
  554. */
  555. if ((node->type != ACPI_TYPE_DEVICE) &&
  556. (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  557. return (AE_OK);
  558. }
  559. /* Check for an existing internal object */
  560. obj_desc = acpi_ns_get_attached_object(node);
  561. if (!obj_desc) {
  562. /* No object, just exit */
  563. return (AE_OK);
  564. }
  565. /* Devices are handled different than regions */
  566. if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_DEVICE) {
  567. /* Check if this Device already has a handler for this address space */
  568. next_handler_obj = obj_desc->device.handler;
  569. while (next_handler_obj) {
  570. /* Found a handler, is it for the same address space? */
  571. if (next_handler_obj->address_space.space_id ==
  572. handler_obj->address_space.space_id) {
  573. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  574. "Found handler for region [%s] in device %p(%p) handler %p\n",
  575. acpi_ut_get_region_name
  576. (handler_obj->address_space.
  577. space_id), obj_desc,
  578. next_handler_obj,
  579. handler_obj));
  580. /*
  581. * Since the object we found it on was a device, then it
  582. * means that someone has already installed a handler for
  583. * the branch of the namespace from this device on. Just
  584. * bail out telling the walk routine to not traverse this
  585. * branch. This preserves the scoping rule for handlers.
  586. */
  587. return (AE_CTRL_DEPTH);
  588. }
  589. /* Walk the linked list of handlers attached to this device */
  590. next_handler_obj = next_handler_obj->address_space.next;
  591. }
  592. /*
  593. * As long as the device didn't have a handler for this
  594. * space we don't care about it. We just ignore it and
  595. * proceed.
  596. */
  597. return (AE_OK);
  598. }
  599. /* Object is a Region */
  600. if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
  601. /*
  602. * This region is for a different address space
  603. * -- just ignore it
  604. */
  605. return (AE_OK);
  606. }
  607. /*
  608. * Now we have a region and it is for the handler's address
  609. * space type.
  610. *
  611. * First disconnect region for any previous handler (if any)
  612. */
  613. acpi_ev_detach_region(obj_desc, FALSE);
  614. /* Connect the region to the new handler */
  615. status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
  616. return (status);
  617. }
  618. /*******************************************************************************
  619. *
  620. * FUNCTION: acpi_ev_install_space_handler
  621. *
  622. * PARAMETERS: Node - Namespace node for the device
  623. * space_id - The address space ID
  624. * Handler - Address of the handler
  625. * Setup - Address of the setup function
  626. * Context - Value passed to the handler on each access
  627. *
  628. * RETURN: Status
  629. *
  630. * DESCRIPTION: Install a handler for all op_regions of a given space_id.
  631. * Assumes namespace is locked
  632. *
  633. ******************************************************************************/
  634. acpi_status
  635. acpi_ev_install_space_handler(struct acpi_namespace_node * node,
  636. acpi_adr_space_type space_id,
  637. acpi_adr_space_handler handler,
  638. acpi_adr_space_setup setup, void *context)
  639. {
  640. union acpi_operand_object *obj_desc;
  641. union acpi_operand_object *handler_obj;
  642. acpi_status status;
  643. acpi_object_type type;
  644. u16 flags = 0;
  645. ACPI_FUNCTION_TRACE("ev_install_space_handler");
  646. /*
  647. * This registration is valid for only the types below
  648. * and the root. This is where the default handlers
  649. * get placed.
  650. */
  651. if ((node->type != ACPI_TYPE_DEVICE) &&
  652. (node->type != ACPI_TYPE_PROCESSOR) &&
  653. (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
  654. status = AE_BAD_PARAMETER;
  655. goto unlock_and_exit;
  656. }
  657. if (handler == ACPI_DEFAULT_HANDLER) {
  658. flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
  659. switch (space_id) {
  660. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  661. handler = acpi_ex_system_memory_space_handler;
  662. setup = acpi_ev_system_memory_region_setup;
  663. break;
  664. case ACPI_ADR_SPACE_SYSTEM_IO:
  665. handler = acpi_ex_system_io_space_handler;
  666. setup = acpi_ev_io_space_region_setup;
  667. break;
  668. case ACPI_ADR_SPACE_PCI_CONFIG:
  669. handler = acpi_ex_pci_config_space_handler;
  670. setup = acpi_ev_pci_config_region_setup;
  671. break;
  672. case ACPI_ADR_SPACE_CMOS:
  673. handler = acpi_ex_cmos_space_handler;
  674. setup = acpi_ev_cmos_region_setup;
  675. break;
  676. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  677. handler = acpi_ex_pci_bar_space_handler;
  678. setup = acpi_ev_pci_bar_region_setup;
  679. break;
  680. case ACPI_ADR_SPACE_DATA_TABLE:
  681. handler = acpi_ex_data_table_space_handler;
  682. setup = NULL;
  683. break;
  684. default:
  685. status = AE_BAD_PARAMETER;
  686. goto unlock_and_exit;
  687. }
  688. }
  689. /* If the caller hasn't specified a setup routine, use the default */
  690. if (!setup) {
  691. setup = acpi_ev_default_region_setup;
  692. }
  693. /* Check for an existing internal object */
  694. obj_desc = acpi_ns_get_attached_object(node);
  695. if (obj_desc) {
  696. /*
  697. * The attached device object already exists.
  698. * Make sure the handler is not already installed.
  699. */
  700. handler_obj = obj_desc->device.handler;
  701. /* Walk the handler list for this device */
  702. while (handler_obj) {
  703. /* Same space_id indicates a handler already installed */
  704. if (handler_obj->address_space.space_id == space_id) {
  705. if (handler_obj->address_space.handler ==
  706. handler) {
  707. /*
  708. * It is (relatively) OK to attempt to install the SAME
  709. * handler twice. This can easily happen
  710. * with PCI_Config space.
  711. */
  712. status = AE_SAME_HANDLER;
  713. goto unlock_and_exit;
  714. } else {
  715. /* A handler is already installed */
  716. status = AE_ALREADY_EXISTS;
  717. }
  718. goto unlock_and_exit;
  719. }
  720. /* Walk the linked list of handlers */
  721. handler_obj = handler_obj->address_space.next;
  722. }
  723. } else {
  724. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  725. "Creating object on Device %p while installing handler\n",
  726. node));
  727. /* obj_desc does not exist, create one */
  728. if (node->type == ACPI_TYPE_ANY) {
  729. type = ACPI_TYPE_DEVICE;
  730. } else {
  731. type = node->type;
  732. }
  733. obj_desc = acpi_ut_create_internal_object(type);
  734. if (!obj_desc) {
  735. status = AE_NO_MEMORY;
  736. goto unlock_and_exit;
  737. }
  738. /* Init new descriptor */
  739. obj_desc->common.type = (u8) type;
  740. /* Attach the new object to the Node */
  741. status = acpi_ns_attach_object(node, obj_desc, type);
  742. /* Remove local reference to the object */
  743. acpi_ut_remove_reference(obj_desc);
  744. if (ACPI_FAILURE(status)) {
  745. goto unlock_and_exit;
  746. }
  747. }
  748. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  749. "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
  750. acpi_ut_get_region_name(space_id), space_id,
  751. acpi_ut_get_node_name(node), node, obj_desc));
  752. /*
  753. * Install the handler
  754. *
  755. * At this point there is no existing handler.
  756. * Just allocate the object for the handler and link it
  757. * into the list.
  758. */
  759. handler_obj =
  760. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
  761. if (!handler_obj) {
  762. status = AE_NO_MEMORY;
  763. goto unlock_and_exit;
  764. }
  765. /* Init handler obj */
  766. handler_obj->address_space.space_id = (u8) space_id;
  767. handler_obj->address_space.hflags = flags;
  768. handler_obj->address_space.region_list = NULL;
  769. handler_obj->address_space.node = node;
  770. handler_obj->address_space.handler = handler;
  771. handler_obj->address_space.context = context;
  772. handler_obj->address_space.setup = setup;
  773. /* Install at head of Device.address_space list */
  774. handler_obj->address_space.next = obj_desc->device.handler;
  775. /*
  776. * The Device object is the first reference on the handler_obj.
  777. * Each region that uses the handler adds a reference.
  778. */
  779. obj_desc->device.handler = handler_obj;
  780. /*
  781. * Walk the namespace finding all of the regions this
  782. * handler will manage.
  783. *
  784. * Start at the device and search the branch toward
  785. * the leaf nodes until either the leaf is encountered or
  786. * a device is detected that has an address handler of the
  787. * same type.
  788. *
  789. * In either case, back up and search down the remainder
  790. * of the branch
  791. */
  792. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  793. ACPI_NS_WALK_UNLOCK,
  794. acpi_ev_install_handler, handler_obj,
  795. NULL);
  796. unlock_and_exit:
  797. return_ACPI_STATUS(status);
  798. }
  799. /*******************************************************************************
  800. *
  801. * FUNCTION: acpi_ev_execute_reg_methods
  802. *
  803. * PARAMETERS: Node - Namespace node for the device
  804. * space_id - The address space ID
  805. *
  806. * RETURN: Status
  807. *
  808. * DESCRIPTION: Run all _REG methods for the input Space ID;
  809. * Note: assumes namespace is locked, or system init time.
  810. *
  811. ******************************************************************************/
  812. acpi_status
  813. acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
  814. acpi_adr_space_type space_id)
  815. {
  816. acpi_status status;
  817. ACPI_FUNCTION_TRACE("ev_execute_reg_methods");
  818. /*
  819. * Run all _REG methods for all Operation Regions for this
  820. * space ID. This is a separate walk in order to handle any
  821. * interdependencies between regions and _REG methods. (i.e. handlers
  822. * must be installed for all regions of this Space ID before we
  823. * can run any _REG methods)
  824. */
  825. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  826. ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
  827. &space_id, NULL);
  828. return_ACPI_STATUS(status);
  829. }
  830. /*******************************************************************************
  831. *
  832. * FUNCTION: acpi_ev_reg_run
  833. *
  834. * PARAMETERS: walk_namespace callback
  835. *
  836. * DESCRIPTION: Run _REg method for region objects of the requested space_iD
  837. *
  838. ******************************************************************************/
  839. static acpi_status
  840. acpi_ev_reg_run(acpi_handle obj_handle,
  841. u32 level, void *context, void **return_value)
  842. {
  843. union acpi_operand_object *obj_desc;
  844. struct acpi_namespace_node *node;
  845. acpi_adr_space_type space_id;
  846. acpi_status status;
  847. space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
  848. /* Convert and validate the device handle */
  849. node = acpi_ns_map_handle_to_node(obj_handle);
  850. if (!node) {
  851. return (AE_BAD_PARAMETER);
  852. }
  853. /*
  854. * We only care about regions.and objects
  855. * that are allowed to have address space handlers
  856. */
  857. if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  858. return (AE_OK);
  859. }
  860. /* Check for an existing internal object */
  861. obj_desc = acpi_ns_get_attached_object(node);
  862. if (!obj_desc) {
  863. /* No object, just exit */
  864. return (AE_OK);
  865. }
  866. /* Object is a Region */
  867. if (obj_desc->region.space_id != space_id) {
  868. /*
  869. * This region is for a different address space
  870. * -- just ignore it
  871. */
  872. return (AE_OK);
  873. }
  874. status = acpi_ev_execute_reg_method(obj_desc, 1);
  875. return (status);
  876. }