evregion.c 29 KB

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