evregion.c 31 KB

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