evregion.c 29 KB

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