evrgnini.c 17 KB

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