evregion.c 29 KB

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