evregion.c 31 KB

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