evregion.c 30 KB

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