evregion.c 35 KB

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