evregion.c 29 KB

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