evrgnini.c 17 KB

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