evregion.c 31 KB

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