evregion.c 29 KB

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