evregion.c 31 KB

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