evregion.c 30 KB

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