evregion.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. /* Call the setup handler with the deactivate notification */
  443. region_setup = handler_obj->address_space.setup;
  444. status =
  445. region_setup(region_obj, ACPI_REGION_DEACTIVATE,
  446. handler_obj->address_space.context,
  447. region_context);
  448. /* Init routine may fail, Just ignore errors */
  449. if (ACPI_FAILURE(status)) {
  450. ACPI_EXCEPTION((AE_INFO, status,
  451. "from region init, [%s]",
  452. acpi_ut_get_region_name
  453. (region_obj->region.space_id)));
  454. }
  455. region_obj->region.flags &= ~(AOPOBJ_SETUP_COMPLETE);
  456. /*
  457. * Remove handler reference in the region
  458. *
  459. * NOTE: this doesn't mean that the region goes away
  460. * The region is just inaccessible as indicated to
  461. * the _REG method
  462. *
  463. * If the region is on the handler's list
  464. * this better be the region's handler
  465. */
  466. region_obj->region.handler = NULL;
  467. acpi_ut_remove_reference(handler_obj);
  468. return_VOID;
  469. }
  470. /* Walk the linked list of handlers */
  471. last_obj_ptr = &obj_desc->region.next;
  472. obj_desc = obj_desc->region.next;
  473. }
  474. /* If we get here, the region was not in the handler's region list */
  475. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  476. "Cannot remove region %p from address handler %p\n",
  477. region_obj, handler_obj));
  478. return_VOID;
  479. }
  480. /*******************************************************************************
  481. *
  482. * FUNCTION: acpi_ev_attach_region
  483. *
  484. * PARAMETERS: handler_obj - Handler Object
  485. * region_obj - Region Object
  486. * acpi_ns_is_locked - Namespace Region Already Locked?
  487. *
  488. * RETURN: None
  489. *
  490. * DESCRIPTION: Create the association between the handler and the region
  491. * this is a two way association.
  492. *
  493. ******************************************************************************/
  494. acpi_status
  495. acpi_ev_attach_region(union acpi_operand_object *handler_obj,
  496. union acpi_operand_object *region_obj,
  497. u8 acpi_ns_is_locked)
  498. {
  499. ACPI_FUNCTION_TRACE(ev_attach_region);
  500. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  501. "Adding Region [%4.4s] %p to address handler %p [%s]\n",
  502. acpi_ut_get_node_name(region_obj->region.node),
  503. region_obj, handler_obj,
  504. acpi_ut_get_region_name(region_obj->region.
  505. space_id)));
  506. /* Link this region to the front of the handler's list */
  507. region_obj->region.next = handler_obj->address_space.region_list;
  508. handler_obj->address_space.region_list = region_obj;
  509. /* Install the region's handler */
  510. if (region_obj->region.handler) {
  511. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  512. }
  513. region_obj->region.handler = handler_obj;
  514. acpi_ut_add_reference(handler_obj);
  515. return_ACPI_STATUS(AE_OK);
  516. }
  517. /*******************************************************************************
  518. *
  519. * FUNCTION: acpi_ev_install_handler
  520. *
  521. * PARAMETERS: walk_namespace callback
  522. *
  523. * DESCRIPTION: This routine installs an address handler into objects that are
  524. * of type Region or Device.
  525. *
  526. * If the Object is a Device, and the device has a handler of
  527. * the same type then the search is terminated in that branch.
  528. *
  529. * This is because the existing handler is closer in proximity
  530. * to any more regions than the one we are trying to install.
  531. *
  532. ******************************************************************************/
  533. static acpi_status
  534. acpi_ev_install_handler(acpi_handle obj_handle,
  535. u32 level, void *context, void **return_value)
  536. {
  537. union acpi_operand_object *handler_obj;
  538. union acpi_operand_object *next_handler_obj;
  539. union acpi_operand_object *obj_desc;
  540. struct acpi_namespace_node *node;
  541. acpi_status status;
  542. ACPI_FUNCTION_NAME(ev_install_handler);
  543. handler_obj = (union acpi_operand_object *)context;
  544. /* Parameter validation */
  545. if (!handler_obj) {
  546. return (AE_OK);
  547. }
  548. /* Convert and validate the device handle */
  549. node = acpi_ns_map_handle_to_node(obj_handle);
  550. if (!node) {
  551. return (AE_BAD_PARAMETER);
  552. }
  553. /*
  554. * We only care about regions.and objects
  555. * that are allowed to have address space handlers
  556. */
  557. if ((node->type != ACPI_TYPE_DEVICE) &&
  558. (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  559. return (AE_OK);
  560. }
  561. /* Check for an existing internal object */
  562. obj_desc = acpi_ns_get_attached_object(node);
  563. if (!obj_desc) {
  564. /* No object, just exit */
  565. return (AE_OK);
  566. }
  567. /* Devices are handled different than regions */
  568. if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_DEVICE) {
  569. /* Check if this Device already has a handler for this address space */
  570. next_handler_obj = obj_desc->device.handler;
  571. while (next_handler_obj) {
  572. /* Found a handler, is it for the same address space? */
  573. if (next_handler_obj->address_space.space_id ==
  574. handler_obj->address_space.space_id) {
  575. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  576. "Found handler for region [%s] in device %p(%p) handler %p\n",
  577. acpi_ut_get_region_name
  578. (handler_obj->address_space.
  579. space_id), obj_desc,
  580. next_handler_obj,
  581. handler_obj));
  582. /*
  583. * Since the object we found it on was a device, then it
  584. * means that someone has already installed a handler for
  585. * the branch of the namespace from this device on. Just
  586. * bail out telling the walk routine to not traverse this
  587. * branch. This preserves the scoping rule for handlers.
  588. */
  589. return (AE_CTRL_DEPTH);
  590. }
  591. /* Walk the linked list of handlers attached to this device */
  592. next_handler_obj = next_handler_obj->address_space.next;
  593. }
  594. /*
  595. * As long as the device didn't have a handler for this
  596. * space we don't care about it. We just ignore it and
  597. * proceed.
  598. */
  599. return (AE_OK);
  600. }
  601. /* Object is a Region */
  602. if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
  603. /*
  604. * This region is for a different address space
  605. * -- just ignore it
  606. */
  607. return (AE_OK);
  608. }
  609. /*
  610. * Now we have a region and it is for the handler's address
  611. * space type.
  612. *
  613. * First disconnect region for any previous handler (if any)
  614. */
  615. acpi_ev_detach_region(obj_desc, FALSE);
  616. /* Connect the region to the new handler */
  617. status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
  618. return (status);
  619. }
  620. /*******************************************************************************
  621. *
  622. * FUNCTION: acpi_ev_install_space_handler
  623. *
  624. * PARAMETERS: Node - Namespace node for the device
  625. * space_id - The address space ID
  626. * Handler - Address of the handler
  627. * Setup - Address of the setup function
  628. * Context - Value passed to the handler on each access
  629. *
  630. * RETURN: Status
  631. *
  632. * DESCRIPTION: Install a handler for all op_regions of a given space_id.
  633. * Assumes namespace is locked
  634. *
  635. ******************************************************************************/
  636. acpi_status
  637. acpi_ev_install_space_handler(struct acpi_namespace_node * node,
  638. acpi_adr_space_type space_id,
  639. acpi_adr_space_handler handler,
  640. acpi_adr_space_setup setup, void *context)
  641. {
  642. union acpi_operand_object *obj_desc;
  643. union acpi_operand_object *handler_obj;
  644. acpi_status status;
  645. acpi_object_type type;
  646. u8 flags = 0;
  647. ACPI_FUNCTION_TRACE(ev_install_space_handler);
  648. /*
  649. * This registration is valid for only the types below
  650. * and the root. This is where the default handlers
  651. * get placed.
  652. */
  653. if ((node->type != ACPI_TYPE_DEVICE) &&
  654. (node->type != ACPI_TYPE_PROCESSOR) &&
  655. (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
  656. status = AE_BAD_PARAMETER;
  657. goto unlock_and_exit;
  658. }
  659. if (handler == ACPI_DEFAULT_HANDLER) {
  660. flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
  661. switch (space_id) {
  662. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  663. handler = acpi_ex_system_memory_space_handler;
  664. setup = acpi_ev_system_memory_region_setup;
  665. break;
  666. case ACPI_ADR_SPACE_SYSTEM_IO:
  667. handler = acpi_ex_system_io_space_handler;
  668. setup = acpi_ev_io_space_region_setup;
  669. break;
  670. case ACPI_ADR_SPACE_PCI_CONFIG:
  671. handler = acpi_ex_pci_config_space_handler;
  672. setup = acpi_ev_pci_config_region_setup;
  673. break;
  674. case ACPI_ADR_SPACE_CMOS:
  675. handler = acpi_ex_cmos_space_handler;
  676. setup = acpi_ev_cmos_region_setup;
  677. break;
  678. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  679. handler = acpi_ex_pci_bar_space_handler;
  680. setup = acpi_ev_pci_bar_region_setup;
  681. break;
  682. case ACPI_ADR_SPACE_DATA_TABLE:
  683. handler = acpi_ex_data_table_space_handler;
  684. setup = NULL;
  685. break;
  686. default:
  687. status = AE_BAD_PARAMETER;
  688. goto unlock_and_exit;
  689. }
  690. }
  691. /* If the caller hasn't specified a setup routine, use the default */
  692. if (!setup) {
  693. setup = acpi_ev_default_region_setup;
  694. }
  695. /* Check for an existing internal object */
  696. obj_desc = acpi_ns_get_attached_object(node);
  697. if (obj_desc) {
  698. /*
  699. * The attached device object already exists.
  700. * Make sure the handler is not already installed.
  701. */
  702. handler_obj = obj_desc->device.handler;
  703. /* Walk the handler list for this device */
  704. while (handler_obj) {
  705. /* Same space_id indicates a handler already installed */
  706. if (handler_obj->address_space.space_id == space_id) {
  707. if (handler_obj->address_space.handler ==
  708. handler) {
  709. /*
  710. * It is (relatively) OK to attempt to install the SAME
  711. * handler twice. This can easily happen
  712. * with PCI_Config space.
  713. */
  714. status = AE_SAME_HANDLER;
  715. goto unlock_and_exit;
  716. } else {
  717. /* A handler is already installed */
  718. status = AE_ALREADY_EXISTS;
  719. }
  720. goto unlock_and_exit;
  721. }
  722. /* Walk the linked list of handlers */
  723. handler_obj = handler_obj->address_space.next;
  724. }
  725. } else {
  726. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  727. "Creating object on Device %p while installing handler\n",
  728. node));
  729. /* obj_desc does not exist, create one */
  730. if (node->type == ACPI_TYPE_ANY) {
  731. type = ACPI_TYPE_DEVICE;
  732. } else {
  733. type = node->type;
  734. }
  735. obj_desc = acpi_ut_create_internal_object(type);
  736. if (!obj_desc) {
  737. status = AE_NO_MEMORY;
  738. goto unlock_and_exit;
  739. }
  740. /* Init new descriptor */
  741. obj_desc->common.type = (u8) type;
  742. /* Attach the new object to the Node */
  743. status = acpi_ns_attach_object(node, obj_desc, type);
  744. /* Remove local reference to the object */
  745. acpi_ut_remove_reference(obj_desc);
  746. if (ACPI_FAILURE(status)) {
  747. goto unlock_and_exit;
  748. }
  749. }
  750. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  751. "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
  752. acpi_ut_get_region_name(space_id), space_id,
  753. acpi_ut_get_node_name(node), node, obj_desc));
  754. /*
  755. * Install the handler
  756. *
  757. * At this point there is no existing handler.
  758. * Just allocate the object for the handler and link it
  759. * into the list.
  760. */
  761. handler_obj =
  762. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
  763. if (!handler_obj) {
  764. status = AE_NO_MEMORY;
  765. goto unlock_and_exit;
  766. }
  767. /* Init handler obj */
  768. handler_obj->address_space.space_id = (u8) space_id;
  769. handler_obj->address_space.handler_flags = flags;
  770. handler_obj->address_space.region_list = NULL;
  771. handler_obj->address_space.node = node;
  772. handler_obj->address_space.handler = handler;
  773. handler_obj->address_space.context = context;
  774. handler_obj->address_space.setup = setup;
  775. /* Install at head of Device.address_space list */
  776. handler_obj->address_space.next = obj_desc->device.handler;
  777. /*
  778. * The Device object is the first reference on the handler_obj.
  779. * Each region that uses the handler adds a reference.
  780. */
  781. obj_desc->device.handler = handler_obj;
  782. /*
  783. * Walk the namespace finding all of the regions this
  784. * handler will manage.
  785. *
  786. * Start at the device and search the branch toward
  787. * the leaf nodes until either the leaf is encountered or
  788. * a device is detected that has an address handler of the
  789. * same type.
  790. *
  791. * In either case, back up and search down the remainder
  792. * of the branch
  793. */
  794. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  795. ACPI_NS_WALK_UNLOCK,
  796. acpi_ev_install_handler, handler_obj,
  797. NULL);
  798. unlock_and_exit:
  799. return_ACPI_STATUS(status);
  800. }
  801. /*******************************************************************************
  802. *
  803. * FUNCTION: acpi_ev_execute_reg_methods
  804. *
  805. * PARAMETERS: Node - Namespace node for the device
  806. * space_id - The address space ID
  807. *
  808. * RETURN: Status
  809. *
  810. * DESCRIPTION: Run all _REG methods for the input Space ID;
  811. * Note: assumes namespace is locked, or system init time.
  812. *
  813. ******************************************************************************/
  814. acpi_status
  815. acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
  816. acpi_adr_space_type space_id)
  817. {
  818. acpi_status status;
  819. ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
  820. /*
  821. * Run all _REG methods for all Operation Regions for this
  822. * space ID. This is a separate walk in order to handle any
  823. * interdependencies between regions and _REG methods. (i.e. handlers
  824. * must be installed for all regions of this Space ID before we
  825. * can run any _REG methods)
  826. */
  827. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  828. ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
  829. &space_id, NULL);
  830. return_ACPI_STATUS(status);
  831. }
  832. /*******************************************************************************
  833. *
  834. * FUNCTION: acpi_ev_reg_run
  835. *
  836. * PARAMETERS: walk_namespace callback
  837. *
  838. * DESCRIPTION: Run _REG method for region objects of the requested space_iD
  839. *
  840. ******************************************************************************/
  841. static acpi_status
  842. acpi_ev_reg_run(acpi_handle obj_handle,
  843. u32 level, void *context, void **return_value)
  844. {
  845. union acpi_operand_object *obj_desc;
  846. struct acpi_namespace_node *node;
  847. acpi_adr_space_type space_id;
  848. acpi_status status;
  849. space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
  850. /* Convert and validate the device handle */
  851. node = acpi_ns_map_handle_to_node(obj_handle);
  852. if (!node) {
  853. return (AE_BAD_PARAMETER);
  854. }
  855. /*
  856. * We only care about regions.and objects
  857. * that are allowed to have address space handlers
  858. */
  859. if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  860. return (AE_OK);
  861. }
  862. /* Check for an existing internal object */
  863. obj_desc = acpi_ns_get_attached_object(node);
  864. if (!obj_desc) {
  865. /* No object, just exit */
  866. return (AE_OK);
  867. }
  868. /* Object is a Region */
  869. if (obj_desc->region.space_id != space_id) {
  870. /*
  871. * This region is for a different address space
  872. * -- just ignore it
  873. */
  874. return (AE_OK);
  875. }
  876. status = acpi_ev_execute_reg_method(obj_desc, 1);
  877. return (status);
  878. }