evrgnini.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. /* Local prototypes */
  17. static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node);
  18. /*******************************************************************************
  19. *
  20. * FUNCTION: acpi_ev_system_memory_region_setup
  21. *
  22. * PARAMETERS: handle - Region we are interested in
  23. * function - Start or stop
  24. * handler_context - Address space handler context
  25. * region_context - Region specific context
  26. *
  27. * RETURN: Status
  28. *
  29. * DESCRIPTION: Setup a system_memory operation region
  30. *
  31. ******************************************************************************/
  32. acpi_status
  33. acpi_ev_system_memory_region_setup(acpi_handle handle,
  34. u32 function,
  35. void *handler_context, void **region_context)
  36. {
  37. union acpi_operand_object *region_desc =
  38. (union acpi_operand_object *)handle;
  39. struct acpi_mem_space_context *local_region_context;
  40. ACPI_FUNCTION_TRACE(ev_system_memory_region_setup);
  41. if (function == ACPI_REGION_DEACTIVATE) {
  42. if (*region_context) {
  43. local_region_context =
  44. (struct acpi_mem_space_context *)*region_context;
  45. /* Delete a cached mapping if present */
  46. if (local_region_context->mapped_length) {
  47. acpi_os_unmap_memory(local_region_context->
  48. mapped_logical_address,
  49. local_region_context->
  50. mapped_length);
  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. status = AE_OK;
  174. } else {
  175. ACPI_EXCEPTION((AE_INFO, status,
  176. "Could not install PciConfig handler "
  177. "for Root Bridge %4.4s",
  178. acpi_ut_get_node_name
  179. (pci_root_node)));
  180. }
  181. }
  182. break;
  183. }
  184. pci_root_node = pci_root_node->parent;
  185. }
  186. /* PCI root bridge not found, use namespace root node */
  187. } else {
  188. pci_root_node = handler_obj->address_space.node;
  189. }
  190. /*
  191. * If this region is now initialized, we are done.
  192. * (install_address_space_handler could have initialized it)
  193. */
  194. if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
  195. return_ACPI_STATUS(AE_OK);
  196. }
  197. /* Region is still not initialized. Create a new context */
  198. pci_id = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pci_id));
  199. if (!pci_id) {
  200. return_ACPI_STATUS(AE_NO_MEMORY);
  201. }
  202. /*
  203. * For PCI_Config space access, we need the segment, bus, device and
  204. * function numbers. Acquire them here.
  205. *
  206. * Find the parent device object. (This allows the operation region to be
  207. * within a subscope under the device, such as a control method.)
  208. */
  209. pci_device_node = region_obj->region.node;
  210. while (pci_device_node && (pci_device_node->type != ACPI_TYPE_DEVICE)) {
  211. pci_device_node = pci_device_node->parent;
  212. }
  213. if (!pci_device_node) {
  214. ACPI_FREE(pci_id);
  215. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  216. }
  217. /*
  218. * Get the PCI device and function numbers from the _ADR object
  219. * contained in the parent's scope.
  220. */
  221. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR,
  222. pci_device_node, &pci_value);
  223. /*
  224. * The default is zero, and since the allocation above zeroed the data,
  225. * just do nothing on failure.
  226. */
  227. if (ACPI_SUCCESS(status)) {
  228. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(pci_value));
  229. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(pci_value));
  230. }
  231. /* The PCI segment number comes from the _SEG method */
  232. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__SEG,
  233. pci_root_node, &pci_value);
  234. if (ACPI_SUCCESS(status)) {
  235. pci_id->segment = ACPI_LOWORD(pci_value);
  236. }
  237. /* The PCI bus number comes from the _BBN method */
  238. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__BBN,
  239. pci_root_node, &pci_value);
  240. if (ACPI_SUCCESS(status)) {
  241. pci_id->bus = ACPI_LOWORD(pci_value);
  242. }
  243. /* Complete/update the PCI ID for this device */
  244. status =
  245. acpi_hw_derive_pci_id(pci_id, pci_root_node,
  246. region_obj->region.node);
  247. if (ACPI_FAILURE(status)) {
  248. ACPI_FREE(pci_id);
  249. return_ACPI_STATUS(status);
  250. }
  251. *region_context = pci_id;
  252. return_ACPI_STATUS(AE_OK);
  253. }
  254. /*******************************************************************************
  255. *
  256. * FUNCTION: acpi_ev_is_pci_root_bridge
  257. *
  258. * PARAMETERS: node - Device node being examined
  259. *
  260. * RETURN: TRUE if device is a PCI/PCI-Express Root Bridge
  261. *
  262. * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
  263. * examining the _HID and _CID for the device.
  264. *
  265. ******************************************************************************/
  266. static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node)
  267. {
  268. acpi_status status;
  269. struct acpi_pnp_device_id *hid;
  270. struct acpi_pnp_device_id_list *cid;
  271. u32 i;
  272. u8 match;
  273. /* Get the _HID and check for a PCI Root Bridge */
  274. status = acpi_ut_execute_HID(node, &hid);
  275. if (ACPI_FAILURE(status)) {
  276. return (FALSE);
  277. }
  278. match = acpi_ut_is_pci_root_bridge(hid->string);
  279. ACPI_FREE(hid);
  280. if (match) {
  281. return (TRUE);
  282. }
  283. /* The _HID did not match. Get the _CID and check for a PCI Root Bridge */
  284. status = acpi_ut_execute_CID(node, &cid);
  285. if (ACPI_FAILURE(status)) {
  286. return (FALSE);
  287. }
  288. /* Check all _CIDs in the returned list */
  289. for (i = 0; i < cid->count; i++) {
  290. if (acpi_ut_is_pci_root_bridge(cid->ids[i].string)) {
  291. ACPI_FREE(cid);
  292. return (TRUE);
  293. }
  294. }
  295. ACPI_FREE(cid);
  296. return (FALSE);
  297. }
  298. /*******************************************************************************
  299. *
  300. * FUNCTION: acpi_ev_pci_bar_region_setup
  301. *
  302. * PARAMETERS: handle - Region we are interested in
  303. * function - Start or stop
  304. * handler_context - Address space handler context
  305. * region_context - Region specific context
  306. *
  307. * RETURN: Status
  308. *
  309. * DESCRIPTION: Setup a pci_BAR operation region
  310. *
  311. * MUTEX: Assumes namespace is not locked
  312. *
  313. ******************************************************************************/
  314. acpi_status
  315. acpi_ev_pci_bar_region_setup(acpi_handle handle,
  316. u32 function,
  317. void *handler_context, void **region_context)
  318. {
  319. ACPI_FUNCTION_TRACE(ev_pci_bar_region_setup);
  320. return_ACPI_STATUS(AE_OK);
  321. }
  322. /*******************************************************************************
  323. *
  324. * FUNCTION: acpi_ev_cmos_region_setup
  325. *
  326. * PARAMETERS: handle - Region we are interested in
  327. * function - Start or stop
  328. * handler_context - Address space handler context
  329. * region_context - Region specific context
  330. *
  331. * RETURN: Status
  332. *
  333. * DESCRIPTION: Setup a CMOS operation region
  334. *
  335. * MUTEX: Assumes namespace is not locked
  336. *
  337. ******************************************************************************/
  338. acpi_status
  339. acpi_ev_cmos_region_setup(acpi_handle handle,
  340. u32 function,
  341. void *handler_context, void **region_context)
  342. {
  343. ACPI_FUNCTION_TRACE(ev_cmos_region_setup);
  344. return_ACPI_STATUS(AE_OK);
  345. }
  346. /*******************************************************************************
  347. *
  348. * FUNCTION: acpi_ev_default_region_setup
  349. *
  350. * PARAMETERS: handle - Region we are interested in
  351. * function - Start or stop
  352. * handler_context - Address space handler context
  353. * region_context - Region specific context
  354. *
  355. * RETURN: Status
  356. *
  357. * DESCRIPTION: Default region initialization
  358. *
  359. ******************************************************************************/
  360. acpi_status
  361. acpi_ev_default_region_setup(acpi_handle handle,
  362. u32 function,
  363. void *handler_context, void **region_context)
  364. {
  365. ACPI_FUNCTION_TRACE(ev_default_region_setup);
  366. if (function == ACPI_REGION_DEACTIVATE) {
  367. *region_context = NULL;
  368. } else {
  369. *region_context = handler_context;
  370. }
  371. return_ACPI_STATUS(AE_OK);
  372. }
  373. /*******************************************************************************
  374. *
  375. * FUNCTION: acpi_ev_initialize_region
  376. *
  377. * PARAMETERS: region_obj - Region we are initializing
  378. *
  379. * RETURN: Status
  380. *
  381. * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
  382. * for execution at a later time
  383. *
  384. * Get the appropriate address space handler for a newly
  385. * created region.
  386. *
  387. * This also performs address space specific initialization. For
  388. * example, PCI regions must have an _ADR object that contains
  389. * a PCI address in the scope of the definition. This address is
  390. * required to perform an access to PCI config space.
  391. *
  392. * MUTEX: Interpreter should be unlocked, because we may run the _REG
  393. * method for this region.
  394. *
  395. * NOTE: Possible incompliance:
  396. * There is a behavior conflict in automatic _REG execution:
  397. * 1. When the interpreter is evaluating a method, we can only
  398. * automatically run _REG for the following case:
  399. * operation_region (OPR1, 0x80, 0x1000010, 0x4)
  400. * 2. When the interpreter is loading a table, we can also
  401. * automatically run _REG for the following case:
  402. * operation_region (OPR1, 0x80, 0x1000010, 0x4)
  403. * Though this may not be compliant to the de-facto standard, the
  404. * logic is kept in order not to trigger regressions. And keeping
  405. * this logic should be taken care by the caller of this function.
  406. *
  407. ******************************************************************************/
  408. acpi_status acpi_ev_initialize_region(union acpi_operand_object *region_obj)
  409. {
  410. union acpi_operand_object *handler_obj;
  411. union acpi_operand_object *obj_desc;
  412. acpi_adr_space_type space_id;
  413. struct acpi_namespace_node *node;
  414. ACPI_FUNCTION_TRACE(ev_initialize_region);
  415. if (!region_obj) {
  416. return_ACPI_STATUS(AE_BAD_PARAMETER);
  417. }
  418. if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
  419. return_ACPI_STATUS(AE_OK);
  420. }
  421. region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;
  422. node = region_obj->region.node->parent;
  423. space_id = region_obj->region.space_id;
  424. /*
  425. * The following loop depends upon the root Node having no parent
  426. * ie: acpi_gbl_root_node->Parent being set to NULL
  427. */
  428. while (node) {
  429. /* Check to see if a handler exists */
  430. handler_obj = NULL;
  431. obj_desc = acpi_ns_get_attached_object(node);
  432. if (obj_desc) {
  433. /* Can only be a handler if the object exists */
  434. switch (node->type) {
  435. case ACPI_TYPE_DEVICE:
  436. case ACPI_TYPE_PROCESSOR:
  437. case ACPI_TYPE_THERMAL:
  438. handler_obj = obj_desc->common_notify.handler;
  439. break;
  440. case ACPI_TYPE_METHOD:
  441. /*
  442. * If we are executing module level code, the original
  443. * Node's object was replaced by this Method object and we
  444. * saved the handler in the method object.
  445. *
  446. * Note: Only used for the legacy MLC support. Will
  447. * be removed in the future.
  448. *
  449. * See acpi_ns_exec_module_code
  450. */
  451. if (!acpi_gbl_execute_tables_as_methods &&
  452. obj_desc->method.
  453. info_flags & ACPI_METHOD_MODULE_LEVEL) {
  454. handler_obj =
  455. obj_desc->method.dispatch.handler;
  456. }
  457. break;
  458. default:
  459. /* Ignore other objects */
  460. break;
  461. }
  462. handler_obj =
  463. acpi_ev_find_region_handler(space_id, handler_obj);
  464. if (handler_obj) {
  465. /* Found correct handler */
  466. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  467. "Found handler %p for region %p in obj %p\n",
  468. handler_obj, region_obj,
  469. obj_desc));
  470. (void)acpi_ev_attach_region(handler_obj,
  471. region_obj, FALSE);
  472. /*
  473. * Tell all users that this region is usable by
  474. * running the _REG method
  475. */
  476. acpi_ex_exit_interpreter();
  477. (void)acpi_ev_execute_reg_method(region_obj,
  478. ACPI_REG_CONNECT);
  479. acpi_ex_enter_interpreter();
  480. return_ACPI_STATUS(AE_OK);
  481. }
  482. }
  483. /* This node does not have the handler we need; Pop up one level */
  484. node = node->parent;
  485. }
  486. /*
  487. * If we get here, there is no handler for this region. This is not
  488. * fatal because many regions get created before a handler is installed
  489. * for said region.
  490. */
  491. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  492. "No handler for RegionType %s(%X) (RegionObj %p)\n",
  493. acpi_ut_get_region_name(space_id), space_id,
  494. region_obj));
  495. return_ACPI_STATUS(AE_OK);
  496. }