evregion.c 29 KB

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