evrgnini.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /******************************************************************************
  2. *
  3. * Module Name: evrgnini- ACPI address_space (op_region) init
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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. #define _COMPONENT ACPI_EVENTS
  47. ACPI_MODULE_NAME("evrgnini")
  48. /* Local prototypes */
  49. static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node);
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_ev_system_memory_region_setup
  53. *
  54. * PARAMETERS: handle - Region we are interested in
  55. * function - Start or stop
  56. * handler_context - Address space handler context
  57. * region_context - Region specific context
  58. *
  59. * RETURN: Status
  60. *
  61. * DESCRIPTION: Setup a system_memory operation region
  62. *
  63. ******************************************************************************/
  64. acpi_status
  65. acpi_ev_system_memory_region_setup(acpi_handle handle,
  66. u32 function,
  67. void *handler_context, void **region_context)
  68. {
  69. union acpi_operand_object *region_desc =
  70. (union acpi_operand_object *)handle;
  71. struct acpi_mem_space_context *local_region_context;
  72. ACPI_FUNCTION_TRACE(ev_system_memory_region_setup);
  73. if (function == ACPI_REGION_DEACTIVATE) {
  74. if (*region_context) {
  75. local_region_context =
  76. (struct acpi_mem_space_context *)*region_context;
  77. /* Delete a cached mapping if present */
  78. if (local_region_context->mapped_length) {
  79. acpi_os_unmap_memory(local_region_context->
  80. mapped_logical_address,
  81. local_region_context->
  82. mapped_length);
  83. }
  84. ACPI_FREE(local_region_context);
  85. *region_context = NULL;
  86. }
  87. return_ACPI_STATUS(AE_OK);
  88. }
  89. /* Create a new context */
  90. local_region_context =
  91. ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_mem_space_context));
  92. if (!(local_region_context)) {
  93. return_ACPI_STATUS(AE_NO_MEMORY);
  94. }
  95. /* Save the region length and address for use in the handler */
  96. local_region_context->length = region_desc->region.length;
  97. local_region_context->address = region_desc->region.address;
  98. *region_context = local_region_context;
  99. return_ACPI_STATUS(AE_OK);
  100. }
  101. /*******************************************************************************
  102. *
  103. * FUNCTION: acpi_ev_io_space_region_setup
  104. *
  105. * PARAMETERS: handle - Region we are interested in
  106. * function - Start or stop
  107. * handler_context - Address space handler context
  108. * region_context - Region specific context
  109. *
  110. * RETURN: Status
  111. *
  112. * DESCRIPTION: Setup a IO operation region
  113. *
  114. ******************************************************************************/
  115. acpi_status
  116. acpi_ev_io_space_region_setup(acpi_handle handle,
  117. u32 function,
  118. void *handler_context, void **region_context)
  119. {
  120. ACPI_FUNCTION_TRACE(ev_io_space_region_setup);
  121. if (function == ACPI_REGION_DEACTIVATE) {
  122. *region_context = NULL;
  123. } else {
  124. *region_context = handler_context;
  125. }
  126. return_ACPI_STATUS(AE_OK);
  127. }
  128. /*******************************************************************************
  129. *
  130. * FUNCTION: acpi_ev_pci_config_region_setup
  131. *
  132. * PARAMETERS: handle - Region we are interested in
  133. * function - Start or stop
  134. * handler_context - Address space handler context
  135. * region_context - Region specific context
  136. *
  137. * RETURN: Status
  138. *
  139. * DESCRIPTION: Setup a PCI_Config operation region
  140. *
  141. * MUTEX: Assumes namespace is not locked
  142. *
  143. ******************************************************************************/
  144. acpi_status
  145. acpi_ev_pci_config_region_setup(acpi_handle handle,
  146. u32 function,
  147. void *handler_context, void **region_context)
  148. {
  149. acpi_status status = AE_OK;
  150. u64 pci_value;
  151. struct acpi_pci_id *pci_id = *region_context;
  152. union acpi_operand_object *handler_obj;
  153. struct acpi_namespace_node *parent_node;
  154. struct acpi_namespace_node *pci_root_node;
  155. struct acpi_namespace_node *pci_device_node;
  156. union acpi_operand_object *region_obj =
  157. (union acpi_operand_object *)handle;
  158. ACPI_FUNCTION_TRACE(ev_pci_config_region_setup);
  159. handler_obj = region_obj->region.handler;
  160. if (!handler_obj) {
  161. /*
  162. * No installed handler. This shouldn't happen because the dispatch
  163. * routine checks before we get here, but we check again just in case.
  164. */
  165. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  166. "Attempting to init a region %p, with no handler\n",
  167. region_obj));
  168. return_ACPI_STATUS(AE_NOT_EXIST);
  169. }
  170. *region_context = NULL;
  171. if (function == ACPI_REGION_DEACTIVATE) {
  172. if (pci_id) {
  173. ACPI_FREE(pci_id);
  174. }
  175. return_ACPI_STATUS(status);
  176. }
  177. parent_node = region_obj->region.node->parent;
  178. /*
  179. * Get the _SEG and _BBN values from the device upon which the handler
  180. * is installed.
  181. *
  182. * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
  183. * This is the device the handler has been registered to handle.
  184. */
  185. /*
  186. * If the address_space.Node is still pointing to the root, we need
  187. * to scan upward for a PCI Root bridge and re-associate the op_region
  188. * handlers with that device.
  189. */
  190. if (handler_obj->address_space.node == acpi_gbl_root_node) {
  191. /* Start search from the parent object */
  192. pci_root_node = parent_node;
  193. while (pci_root_node != acpi_gbl_root_node) {
  194. /* Get the _HID/_CID in order to detect a root_bridge */
  195. if (acpi_ev_is_pci_root_bridge(pci_root_node)) {
  196. /* Install a handler for this PCI root bridge */
  197. status = acpi_install_address_space_handler((acpi_handle) pci_root_node, ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
  198. if (ACPI_FAILURE(status)) {
  199. if (status == AE_SAME_HANDLER) {
  200. /*
  201. * It is OK if the handler is already installed on the
  202. * root bridge. Still need to return a context object
  203. * for the new PCI_Config operation region, however.
  204. */
  205. status = AE_OK;
  206. } else {
  207. ACPI_EXCEPTION((AE_INFO, status,
  208. "Could not install PciConfig handler "
  209. "for Root Bridge %4.4s",
  210. acpi_ut_get_node_name
  211. (pci_root_node)));
  212. }
  213. }
  214. break;
  215. }
  216. pci_root_node = pci_root_node->parent;
  217. }
  218. /* PCI root bridge not found, use namespace root node */
  219. } else {
  220. pci_root_node = handler_obj->address_space.node;
  221. }
  222. /*
  223. * If this region is now initialized, we are done.
  224. * (install_address_space_handler could have initialized it)
  225. */
  226. if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
  227. return_ACPI_STATUS(AE_OK);
  228. }
  229. /* Region is still not initialized. Create a new context */
  230. pci_id = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pci_id));
  231. if (!pci_id) {
  232. return_ACPI_STATUS(AE_NO_MEMORY);
  233. }
  234. /*
  235. * For PCI_Config space access, we need the segment, bus, device and
  236. * function numbers. Acquire them here.
  237. *
  238. * Find the parent device object. (This allows the operation region to be
  239. * within a subscope under the device, such as a control method.)
  240. */
  241. pci_device_node = region_obj->region.node;
  242. while (pci_device_node && (pci_device_node->type != ACPI_TYPE_DEVICE)) {
  243. pci_device_node = pci_device_node->parent;
  244. }
  245. if (!pci_device_node) {
  246. ACPI_FREE(pci_id);
  247. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  248. }
  249. /*
  250. * Get the PCI device and function numbers from the _ADR object
  251. * contained in the parent's scope.
  252. */
  253. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR,
  254. pci_device_node, &pci_value);
  255. /*
  256. * The default is zero, and since the allocation above zeroed the data,
  257. * just do nothing on failure.
  258. */
  259. if (ACPI_SUCCESS(status)) {
  260. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(pci_value));
  261. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(pci_value));
  262. }
  263. /* The PCI segment number comes from the _SEG method */
  264. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__SEG,
  265. pci_root_node, &pci_value);
  266. if (ACPI_SUCCESS(status)) {
  267. pci_id->segment = ACPI_LOWORD(pci_value);
  268. }
  269. /* The PCI bus number comes from the _BBN method */
  270. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__BBN,
  271. pci_root_node, &pci_value);
  272. if (ACPI_SUCCESS(status)) {
  273. pci_id->bus = ACPI_LOWORD(pci_value);
  274. }
  275. /* Complete/update the PCI ID for this device */
  276. status =
  277. acpi_hw_derive_pci_id(pci_id, pci_root_node,
  278. region_obj->region.node);
  279. if (ACPI_FAILURE(status)) {
  280. ACPI_FREE(pci_id);
  281. return_ACPI_STATUS(status);
  282. }
  283. *region_context = pci_id;
  284. return_ACPI_STATUS(AE_OK);
  285. }
  286. /*******************************************************************************
  287. *
  288. * FUNCTION: acpi_ev_is_pci_root_bridge
  289. *
  290. * PARAMETERS: node - Device node being examined
  291. *
  292. * RETURN: TRUE if device is a PCI/PCI-Express Root Bridge
  293. *
  294. * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
  295. * examining the _HID and _CID for the device.
  296. *
  297. ******************************************************************************/
  298. static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node)
  299. {
  300. acpi_status status;
  301. struct acpi_pnp_device_id *hid;
  302. struct acpi_pnp_device_id_list *cid;
  303. u32 i;
  304. u8 match;
  305. /* Get the _HID and check for a PCI Root Bridge */
  306. status = acpi_ut_execute_HID(node, &hid);
  307. if (ACPI_FAILURE(status)) {
  308. return (FALSE);
  309. }
  310. match = acpi_ut_is_pci_root_bridge(hid->string);
  311. ACPI_FREE(hid);
  312. if (match) {
  313. return (TRUE);
  314. }
  315. /* The _HID did not match. Get the _CID and check for a PCI Root Bridge */
  316. status = acpi_ut_execute_CID(node, &cid);
  317. if (ACPI_FAILURE(status)) {
  318. return (FALSE);
  319. }
  320. /* Check all _CIDs in the returned list */
  321. for (i = 0; i < cid->count; i++) {
  322. if (acpi_ut_is_pci_root_bridge(cid->ids[i].string)) {
  323. ACPI_FREE(cid);
  324. return (TRUE);
  325. }
  326. }
  327. ACPI_FREE(cid);
  328. return (FALSE);
  329. }
  330. /*******************************************************************************
  331. *
  332. * FUNCTION: acpi_ev_pci_bar_region_setup
  333. *
  334. * PARAMETERS: handle - Region we are interested in
  335. * function - Start or stop
  336. * handler_context - Address space handler context
  337. * region_context - Region specific context
  338. *
  339. * RETURN: Status
  340. *
  341. * DESCRIPTION: Setup a pci_BAR operation region
  342. *
  343. * MUTEX: Assumes namespace is not locked
  344. *
  345. ******************************************************************************/
  346. acpi_status
  347. acpi_ev_pci_bar_region_setup(acpi_handle handle,
  348. u32 function,
  349. void *handler_context, void **region_context)
  350. {
  351. ACPI_FUNCTION_TRACE(ev_pci_bar_region_setup);
  352. return_ACPI_STATUS(AE_OK);
  353. }
  354. /*******************************************************************************
  355. *
  356. * FUNCTION: acpi_ev_cmos_region_setup
  357. *
  358. * PARAMETERS: handle - Region we are interested in
  359. * function - Start or stop
  360. * handler_context - Address space handler context
  361. * region_context - Region specific context
  362. *
  363. * RETURN: Status
  364. *
  365. * DESCRIPTION: Setup a CMOS operation region
  366. *
  367. * MUTEX: Assumes namespace is not locked
  368. *
  369. ******************************************************************************/
  370. acpi_status
  371. acpi_ev_cmos_region_setup(acpi_handle handle,
  372. u32 function,
  373. void *handler_context, void **region_context)
  374. {
  375. ACPI_FUNCTION_TRACE(ev_cmos_region_setup);
  376. return_ACPI_STATUS(AE_OK);
  377. }
  378. /*******************************************************************************
  379. *
  380. * FUNCTION: acpi_ev_default_region_setup
  381. *
  382. * PARAMETERS: handle - Region we are interested in
  383. * function - Start or stop
  384. * handler_context - Address space handler context
  385. * region_context - Region specific context
  386. *
  387. * RETURN: Status
  388. *
  389. * DESCRIPTION: Default region initialization
  390. *
  391. ******************************************************************************/
  392. acpi_status
  393. acpi_ev_default_region_setup(acpi_handle handle,
  394. u32 function,
  395. void *handler_context, void **region_context)
  396. {
  397. ACPI_FUNCTION_TRACE(ev_default_region_setup);
  398. if (function == ACPI_REGION_DEACTIVATE) {
  399. *region_context = NULL;
  400. } else {
  401. *region_context = handler_context;
  402. }
  403. return_ACPI_STATUS(AE_OK);
  404. }
  405. /*******************************************************************************
  406. *
  407. * FUNCTION: acpi_ev_initialize_region
  408. *
  409. * PARAMETERS: region_obj - Region we are initializing
  410. * acpi_ns_locked - Is namespace locked?
  411. *
  412. * RETURN: Status
  413. *
  414. * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
  415. * for execution at a later time
  416. *
  417. * Get the appropriate address space handler for a newly
  418. * created region.
  419. *
  420. * This also performs address space specific initialization. For
  421. * example, PCI regions must have an _ADR object that contains
  422. * a PCI address in the scope of the definition. This address is
  423. * required to perform an access to PCI config space.
  424. *
  425. * MUTEX: Interpreter should be unlocked, because we may run the _REG
  426. * method for this region.
  427. *
  428. ******************************************************************************/
  429. acpi_status
  430. acpi_ev_initialize_region(union acpi_operand_object *region_obj,
  431. u8 acpi_ns_locked)
  432. {
  433. union acpi_operand_object *handler_obj;
  434. union acpi_operand_object *obj_desc;
  435. acpi_adr_space_type space_id;
  436. struct acpi_namespace_node *node;
  437. acpi_status status;
  438. struct acpi_namespace_node *method_node;
  439. acpi_name *reg_name_ptr = (acpi_name *) METHOD_NAME__REG;
  440. union acpi_operand_object *region_obj2;
  441. ACPI_FUNCTION_TRACE_U32(ev_initialize_region, acpi_ns_locked);
  442. if (!region_obj) {
  443. return_ACPI_STATUS(AE_BAD_PARAMETER);
  444. }
  445. if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
  446. return_ACPI_STATUS(AE_OK);
  447. }
  448. region_obj2 = acpi_ns_get_secondary_object(region_obj);
  449. if (!region_obj2) {
  450. return_ACPI_STATUS(AE_NOT_EXIST);
  451. }
  452. node = region_obj->region.node->parent;
  453. space_id = region_obj->region.space_id;
  454. /* Setup defaults */
  455. region_obj->region.handler = NULL;
  456. region_obj2->extra.method_REG = NULL;
  457. region_obj->common.flags &= ~(AOPOBJ_SETUP_COMPLETE);
  458. region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;
  459. /* Find any "_REG" method associated with this region definition */
  460. status =
  461. acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD,
  462. &method_node);
  463. if (ACPI_SUCCESS(status)) {
  464. /*
  465. * The _REG method is optional and there can be only one per region
  466. * definition. This will be executed when the handler is attached
  467. * or removed
  468. */
  469. region_obj2->extra.method_REG = method_node;
  470. }
  471. /*
  472. * The following loop depends upon the root Node having no parent
  473. * ie: acpi_gbl_root_node->parent_entry being set to NULL
  474. */
  475. while (node) {
  476. /* Check to see if a handler exists */
  477. handler_obj = NULL;
  478. obj_desc = acpi_ns_get_attached_object(node);
  479. if (obj_desc) {
  480. /* Can only be a handler if the object exists */
  481. switch (node->type) {
  482. case ACPI_TYPE_DEVICE:
  483. handler_obj = obj_desc->device.handler;
  484. break;
  485. case ACPI_TYPE_PROCESSOR:
  486. handler_obj = obj_desc->processor.handler;
  487. break;
  488. case ACPI_TYPE_THERMAL:
  489. handler_obj = obj_desc->thermal_zone.handler;
  490. break;
  491. case ACPI_TYPE_METHOD:
  492. /*
  493. * If we are executing module level code, the original
  494. * Node's object was replaced by this Method object and we
  495. * saved the handler in the method object.
  496. *
  497. * See acpi_ns_exec_module_code
  498. */
  499. if (obj_desc->method.
  500. info_flags & ACPI_METHOD_MODULE_LEVEL) {
  501. handler_obj =
  502. obj_desc->method.dispatch.handler;
  503. }
  504. break;
  505. default:
  506. /* Ignore other objects */
  507. break;
  508. }
  509. while (handler_obj) {
  510. /* Is this handler of the correct type? */
  511. if (handler_obj->address_space.space_id ==
  512. space_id) {
  513. /* Found correct handler */
  514. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  515. "Found handler %p for region %p in obj %p\n",
  516. handler_obj,
  517. region_obj,
  518. obj_desc));
  519. status =
  520. acpi_ev_attach_region(handler_obj,
  521. region_obj,
  522. acpi_ns_locked);
  523. /*
  524. * Tell all users that this region is usable by
  525. * running the _REG method
  526. */
  527. if (acpi_ns_locked) {
  528. status =
  529. acpi_ut_release_mutex
  530. (ACPI_MTX_NAMESPACE);
  531. if (ACPI_FAILURE(status)) {
  532. return_ACPI_STATUS
  533. (status);
  534. }
  535. }
  536. status =
  537. acpi_ev_execute_reg_method
  538. (region_obj, ACPI_REG_CONNECT);
  539. if (acpi_ns_locked) {
  540. status =
  541. acpi_ut_acquire_mutex
  542. (ACPI_MTX_NAMESPACE);
  543. if (ACPI_FAILURE(status)) {
  544. return_ACPI_STATUS
  545. (status);
  546. }
  547. }
  548. return_ACPI_STATUS(AE_OK);
  549. }
  550. /* Try next handler in the list */
  551. handler_obj = handler_obj->address_space.next;
  552. }
  553. }
  554. /* This node does not have the handler we need; Pop up one level */
  555. node = node->parent;
  556. }
  557. /* If we get here, there is no handler for this region */
  558. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  559. "No handler for RegionType %s(%X) (RegionObj %p)\n",
  560. acpi_ut_get_region_name(space_id), space_id,
  561. region_obj));
  562. return_ACPI_STATUS(AE_NOT_EXIST);
  563. }