evregion.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. * region_offset - Where in the region 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. u32 region_offset,
  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(region_obj->region.address +
  332. region_offset),
  333. acpi_ut_get_region_name(region_obj->region.
  334. space_id)));
  335. if (!(handler_desc->address_space.handler_flags &
  336. ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
  337. /*
  338. * For handlers other than the default (supplied) handlers, we must
  339. * exit the interpreter because the handler *might* block -- we don't
  340. * know what it will do, so we can't hold the lock on the intepreter.
  341. */
  342. acpi_ex_exit_interpreter();
  343. }
  344. /* Call the handler */
  345. status = handler(function,
  346. (region_obj->region.address + region_offset),
  347. bit_width, value, handler_desc->address_space.context,
  348. region_obj2->extra.region_context);
  349. if (ACPI_FAILURE(status)) {
  350. ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
  351. acpi_ut_get_region_name(region_obj->region.
  352. space_id)));
  353. }
  354. if (!(handler_desc->address_space.handler_flags &
  355. ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
  356. /*
  357. * We just returned from a non-default handler, we must re-enter the
  358. * interpreter
  359. */
  360. acpi_ex_enter_interpreter();
  361. }
  362. return_ACPI_STATUS(status);
  363. }
  364. /*******************************************************************************
  365. *
  366. * FUNCTION: acpi_ev_detach_region
  367. *
  368. * PARAMETERS: region_obj - Region Object
  369. * acpi_ns_is_locked - Namespace Region Already Locked?
  370. *
  371. * RETURN: None
  372. *
  373. * DESCRIPTION: Break the association between the handler and the region
  374. * this is a two way association.
  375. *
  376. ******************************************************************************/
  377. void
  378. acpi_ev_detach_region(union acpi_operand_object *region_obj,
  379. u8 acpi_ns_is_locked)
  380. {
  381. union acpi_operand_object *handler_obj;
  382. union acpi_operand_object *obj_desc;
  383. union acpi_operand_object **last_obj_ptr;
  384. acpi_adr_space_setup region_setup;
  385. void **region_context;
  386. union acpi_operand_object *region_obj2;
  387. acpi_status status;
  388. ACPI_FUNCTION_TRACE(ev_detach_region);
  389. region_obj2 = acpi_ns_get_secondary_object(region_obj);
  390. if (!region_obj2) {
  391. return_VOID;
  392. }
  393. region_context = &region_obj2->extra.region_context;
  394. /* Get the address handler from the region object */
  395. handler_obj = region_obj->region.handler;
  396. if (!handler_obj) {
  397. /* This region has no handler, all done */
  398. return_VOID;
  399. }
  400. /* Find this region in the handler's list */
  401. obj_desc = handler_obj->address_space.region_list;
  402. last_obj_ptr = &handler_obj->address_space.region_list;
  403. while (obj_desc) {
  404. /* Is this the correct Region? */
  405. if (obj_desc == region_obj) {
  406. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  407. "Removing Region %p from address handler %p\n",
  408. region_obj, handler_obj));
  409. /* This is it, remove it from the handler's list */
  410. *last_obj_ptr = obj_desc->region.next;
  411. obj_desc->region.next = NULL; /* Must clear field */
  412. if (acpi_ns_is_locked) {
  413. status =
  414. acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  415. if (ACPI_FAILURE(status)) {
  416. return_VOID;
  417. }
  418. }
  419. /* Now stop region accesses by executing the _REG method */
  420. status = acpi_ev_execute_reg_method(region_obj, 0);
  421. if (ACPI_FAILURE(status)) {
  422. ACPI_EXCEPTION((AE_INFO, status,
  423. "from region _REG, [%s]",
  424. acpi_ut_get_region_name
  425. (region_obj->region.space_id)));
  426. }
  427. if (acpi_ns_is_locked) {
  428. status =
  429. acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  430. if (ACPI_FAILURE(status)) {
  431. return_VOID;
  432. }
  433. }
  434. /*
  435. * If the region has been activated, call the setup handler with
  436. * the deactivate notification
  437. */
  438. if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
  439. region_setup = handler_obj->address_space.setup;
  440. status =
  441. region_setup(region_obj,
  442. ACPI_REGION_DEACTIVATE,
  443. handler_obj->address_space.
  444. context, region_context);
  445. /* Init routine may fail, Just ignore errors */
  446. if (ACPI_FAILURE(status)) {
  447. ACPI_EXCEPTION((AE_INFO, status,
  448. "from region handler - deactivate, [%s]",
  449. acpi_ut_get_region_name
  450. (region_obj->region.
  451. space_id)));
  452. }
  453. region_obj->region.flags &=
  454. ~(AOPOBJ_SETUP_COMPLETE);
  455. }
  456. /*
  457. * Remove handler reference in the region
  458. *
  459. * NOTE: this doesn't mean that the region goes away, the region
  460. * is just inaccessible as indicated to the _REG method
  461. *
  462. * If the region is on the handler's list, this must be the
  463. * region's handler
  464. */
  465. region_obj->region.handler = NULL;
  466. acpi_ut_remove_reference(handler_obj);
  467. return_VOID;
  468. }
  469. /* Walk the linked list of handlers */
  470. last_obj_ptr = &obj_desc->region.next;
  471. obj_desc = obj_desc->region.next;
  472. }
  473. /* If we get here, the region was not in the handler's region list */
  474. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  475. "Cannot remove region %p from address handler %p\n",
  476. region_obj, handler_obj));
  477. return_VOID;
  478. }
  479. /*******************************************************************************
  480. *
  481. * FUNCTION: acpi_ev_attach_region
  482. *
  483. * PARAMETERS: handler_obj - Handler Object
  484. * region_obj - Region Object
  485. * acpi_ns_is_locked - Namespace Region Already Locked?
  486. *
  487. * RETURN: None
  488. *
  489. * DESCRIPTION: Create the association between the handler and the region
  490. * this is a two way association.
  491. *
  492. ******************************************************************************/
  493. acpi_status
  494. acpi_ev_attach_region(union acpi_operand_object *handler_obj,
  495. union acpi_operand_object *region_obj,
  496. u8 acpi_ns_is_locked)
  497. {
  498. ACPI_FUNCTION_TRACE(ev_attach_region);
  499. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  500. "Adding Region [%4.4s] %p to address handler %p [%s]\n",
  501. acpi_ut_get_node_name(region_obj->region.node),
  502. region_obj, handler_obj,
  503. acpi_ut_get_region_name(region_obj->region.
  504. space_id)));
  505. /* Link this region to the front of the handler's list */
  506. region_obj->region.next = handler_obj->address_space.region_list;
  507. handler_obj->address_space.region_list = region_obj;
  508. /* Install the region's handler */
  509. if (region_obj->region.handler) {
  510. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  511. }
  512. region_obj->region.handler = handler_obj;
  513. acpi_ut_add_reference(handler_obj);
  514. return_ACPI_STATUS(AE_OK);
  515. }
  516. /*******************************************************************************
  517. *
  518. * FUNCTION: acpi_ev_install_handler
  519. *
  520. * PARAMETERS: walk_namespace callback
  521. *
  522. * DESCRIPTION: This routine installs an address handler into objects that are
  523. * of type Region or Device.
  524. *
  525. * If the Object is a Device, and the device has a handler of
  526. * the same type then the search is terminated in that branch.
  527. *
  528. * This is because the existing handler is closer in proximity
  529. * to any more regions than the one we are trying to install.
  530. *
  531. ******************************************************************************/
  532. static acpi_status
  533. acpi_ev_install_handler(acpi_handle obj_handle,
  534. u32 level, void *context, void **return_value)
  535. {
  536. union acpi_operand_object *handler_obj;
  537. union acpi_operand_object *next_handler_obj;
  538. union acpi_operand_object *obj_desc;
  539. struct acpi_namespace_node *node;
  540. acpi_status status;
  541. ACPI_FUNCTION_NAME(ev_install_handler);
  542. handler_obj = (union acpi_operand_object *)context;
  543. /* Parameter validation */
  544. if (!handler_obj) {
  545. return (AE_OK);
  546. }
  547. /* Convert and validate the device handle */
  548. node = acpi_ns_map_handle_to_node(obj_handle);
  549. if (!node) {
  550. return (AE_BAD_PARAMETER);
  551. }
  552. /*
  553. * We only care about regions and objects that are allowed to have
  554. * address space handlers
  555. */
  556. if ((node->type != ACPI_TYPE_DEVICE) &&
  557. (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  558. return (AE_OK);
  559. }
  560. /* Check for an existing internal object */
  561. obj_desc = acpi_ns_get_attached_object(node);
  562. if (!obj_desc) {
  563. /* No object, just exit */
  564. return (AE_OK);
  565. }
  566. /* Devices are handled different than regions */
  567. if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
  568. /* Check if this Device already has a handler for this address space */
  569. next_handler_obj = obj_desc->device.handler;
  570. while (next_handler_obj) {
  571. /* Found a handler, is it for the same address space? */
  572. if (next_handler_obj->address_space.space_id ==
  573. handler_obj->address_space.space_id) {
  574. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  575. "Found handler for region [%s] in device %p(%p) "
  576. "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 space we
  596. * don't care about it. We just ignore it and proceed.
  597. */
  598. return (AE_OK);
  599. }
  600. /* Object is a Region */
  601. if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
  602. /* This region is for a different address space, just ignore it */
  603. return (AE_OK);
  604. }
  605. /*
  606. * Now we have a region and it is for the handler's address space type.
  607. *
  608. * First disconnect region for any previous handler (if any)
  609. */
  610. acpi_ev_detach_region(obj_desc, FALSE);
  611. /* Connect the region to the new handler */
  612. status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
  613. return (status);
  614. }
  615. /*******************************************************************************
  616. *
  617. * FUNCTION: acpi_ev_install_space_handler
  618. *
  619. * PARAMETERS: Node - Namespace node for the device
  620. * space_id - The address space ID
  621. * Handler - Address of the handler
  622. * Setup - Address of the setup function
  623. * Context - Value passed to the handler on each access
  624. *
  625. * RETURN: Status
  626. *
  627. * DESCRIPTION: Install a handler for all op_regions of a given space_id.
  628. * Assumes namespace is locked
  629. *
  630. ******************************************************************************/
  631. acpi_status
  632. acpi_ev_install_space_handler(struct acpi_namespace_node * node,
  633. acpi_adr_space_type space_id,
  634. acpi_adr_space_handler handler,
  635. acpi_adr_space_setup setup, void *context)
  636. {
  637. union acpi_operand_object *obj_desc;
  638. union acpi_operand_object *handler_obj;
  639. acpi_status status;
  640. acpi_object_type type;
  641. u8 flags = 0;
  642. ACPI_FUNCTION_TRACE(ev_install_space_handler);
  643. /*
  644. * This registration is valid for only the types below and the root. This
  645. * is where the default handlers get placed.
  646. */
  647. if ((node->type != ACPI_TYPE_DEVICE) &&
  648. (node->type != ACPI_TYPE_PROCESSOR) &&
  649. (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
  650. status = AE_BAD_PARAMETER;
  651. goto unlock_and_exit;
  652. }
  653. if (handler == ACPI_DEFAULT_HANDLER) {
  654. flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
  655. switch (space_id) {
  656. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  657. handler = acpi_ex_system_memory_space_handler;
  658. setup = acpi_ev_system_memory_region_setup;
  659. break;
  660. case ACPI_ADR_SPACE_SYSTEM_IO:
  661. handler = acpi_ex_system_io_space_handler;
  662. setup = acpi_ev_io_space_region_setup;
  663. break;
  664. case ACPI_ADR_SPACE_PCI_CONFIG:
  665. handler = acpi_ex_pci_config_space_handler;
  666. setup = acpi_ev_pci_config_region_setup;
  667. break;
  668. case ACPI_ADR_SPACE_CMOS:
  669. handler = acpi_ex_cmos_space_handler;
  670. setup = acpi_ev_cmos_region_setup;
  671. break;
  672. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  673. handler = acpi_ex_pci_bar_space_handler;
  674. setup = acpi_ev_pci_bar_region_setup;
  675. break;
  676. case ACPI_ADR_SPACE_DATA_TABLE:
  677. handler = acpi_ex_data_table_space_handler;
  678. setup = NULL;
  679. break;
  680. default:
  681. status = AE_BAD_PARAMETER;
  682. goto unlock_and_exit;
  683. }
  684. }
  685. /* If the caller hasn't specified a setup routine, use the default */
  686. if (!setup) {
  687. setup = acpi_ev_default_region_setup;
  688. }
  689. /* Check for an existing internal object */
  690. obj_desc = acpi_ns_get_attached_object(node);
  691. if (obj_desc) {
  692. /*
  693. * The attached device object already exists. Make sure the handler
  694. * is not already installed.
  695. */
  696. handler_obj = obj_desc->device.handler;
  697. /* Walk the handler list for this device */
  698. while (handler_obj) {
  699. /* Same space_id indicates a handler already installed */
  700. if (handler_obj->address_space.space_id == space_id) {
  701. if (handler_obj->address_space.handler ==
  702. handler) {
  703. /*
  704. * It is (relatively) OK to attempt to install the SAME
  705. * handler twice. This can easily happen with the
  706. * PCI_Config space.
  707. */
  708. status = AE_SAME_HANDLER;
  709. goto unlock_and_exit;
  710. } else {
  711. /* A handler is already installed */
  712. status = AE_ALREADY_EXISTS;
  713. }
  714. goto unlock_and_exit;
  715. }
  716. /* Walk the linked list of handlers */
  717. handler_obj = handler_obj->address_space.next;
  718. }
  719. } else {
  720. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  721. "Creating object on Device %p while installing handler\n",
  722. node));
  723. /* obj_desc does not exist, create one */
  724. if (node->type == ACPI_TYPE_ANY) {
  725. type = ACPI_TYPE_DEVICE;
  726. } else {
  727. type = node->type;
  728. }
  729. obj_desc = acpi_ut_create_internal_object(type);
  730. if (!obj_desc) {
  731. status = AE_NO_MEMORY;
  732. goto unlock_and_exit;
  733. }
  734. /* Init new descriptor */
  735. obj_desc->common.type = (u8) type;
  736. /* Attach the new object to the Node */
  737. status = acpi_ns_attach_object(node, obj_desc, type);
  738. /* Remove local reference to the object */
  739. acpi_ut_remove_reference(obj_desc);
  740. if (ACPI_FAILURE(status)) {
  741. goto unlock_and_exit;
  742. }
  743. }
  744. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  745. "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
  746. acpi_ut_get_region_name(space_id), space_id,
  747. acpi_ut_get_node_name(node), node, obj_desc));
  748. /*
  749. * Install the handler
  750. *
  751. * At this point there is no existing handler. Just allocate the object
  752. * for the handler and link it into the list.
  753. */
  754. handler_obj =
  755. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
  756. if (!handler_obj) {
  757. status = AE_NO_MEMORY;
  758. goto unlock_and_exit;
  759. }
  760. /* Init handler obj */
  761. handler_obj->address_space.space_id = (u8) space_id;
  762. handler_obj->address_space.handler_flags = flags;
  763. handler_obj->address_space.region_list = NULL;
  764. handler_obj->address_space.node = node;
  765. handler_obj->address_space.handler = handler;
  766. handler_obj->address_space.context = context;
  767. handler_obj->address_space.setup = setup;
  768. /* Install at head of Device.address_space list */
  769. handler_obj->address_space.next = obj_desc->device.handler;
  770. /*
  771. * The Device object is the first reference on the handler_obj.
  772. * Each region that uses the handler adds a reference.
  773. */
  774. obj_desc->device.handler = handler_obj;
  775. /*
  776. * Walk the namespace finding all of the regions this
  777. * handler will manage.
  778. *
  779. * Start at the device and search the branch toward
  780. * the leaf nodes until either the leaf is encountered or
  781. * a device is detected that has an address handler of the
  782. * same type.
  783. *
  784. * In either case, back up and search down the remainder
  785. * of the branch
  786. */
  787. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  788. ACPI_NS_WALK_UNLOCK,
  789. acpi_ev_install_handler, handler_obj,
  790. NULL);
  791. unlock_and_exit:
  792. return_ACPI_STATUS(status);
  793. }
  794. /*******************************************************************************
  795. *
  796. * FUNCTION: acpi_ev_execute_reg_methods
  797. *
  798. * PARAMETERS: Node - Namespace node for the device
  799. * space_id - The address space ID
  800. *
  801. * RETURN: Status
  802. *
  803. * DESCRIPTION: Run all _REG methods for the input Space ID;
  804. * Note: assumes namespace is locked, or system init time.
  805. *
  806. ******************************************************************************/
  807. acpi_status
  808. acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
  809. acpi_adr_space_type space_id)
  810. {
  811. acpi_status status;
  812. ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
  813. /*
  814. * Run all _REG methods for all Operation Regions for this space ID. This
  815. * is a separate walk in order to handle any interdependencies between
  816. * regions and _REG methods. (i.e. handlers must be installed for all
  817. * regions of this Space ID before we can run any _REG methods)
  818. */
  819. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
  820. ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
  821. &space_id, NULL);
  822. return_ACPI_STATUS(status);
  823. }
  824. /*******************************************************************************
  825. *
  826. * FUNCTION: acpi_ev_reg_run
  827. *
  828. * PARAMETERS: walk_namespace callback
  829. *
  830. * DESCRIPTION: Run _REG method for region objects of the requested space_iD
  831. *
  832. ******************************************************************************/
  833. static acpi_status
  834. acpi_ev_reg_run(acpi_handle obj_handle,
  835. u32 level, void *context, void **return_value)
  836. {
  837. union acpi_operand_object *obj_desc;
  838. struct acpi_namespace_node *node;
  839. acpi_adr_space_type space_id;
  840. acpi_status status;
  841. space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
  842. /* Convert and validate the device handle */
  843. node = acpi_ns_map_handle_to_node(obj_handle);
  844. if (!node) {
  845. return (AE_BAD_PARAMETER);
  846. }
  847. /*
  848. * We only care about regions.and objects that are allowed to have address
  849. * space handlers
  850. */
  851. if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  852. return (AE_OK);
  853. }
  854. /* Check for an existing internal object */
  855. obj_desc = acpi_ns_get_attached_object(node);
  856. if (!obj_desc) {
  857. /* No object, just exit */
  858. return (AE_OK);
  859. }
  860. /* Object is a Region */
  861. if (obj_desc->region.space_id != space_id) {
  862. /* This region is for a different address space, just ignore it */
  863. return (AE_OK);
  864. }
  865. status = acpi_ev_execute_reg_method(obj_desc, 1);
  866. return (status);
  867. }