evregion.c 29 KB

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