nsinit.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: nsinit - namespace initialization
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acnamesp.h"
  12. #include "acdispat.h"
  13. #include "acinterp.h"
  14. #include "acevents.h"
  15. #define _COMPONENT ACPI_NAMESPACE
  16. ACPI_MODULE_NAME("nsinit")
  17. /* Local prototypes */
  18. static acpi_status
  19. acpi_ns_init_one_object(acpi_handle obj_handle,
  20. u32 level, void *context, void **return_value);
  21. static acpi_status
  22. acpi_ns_init_one_device(acpi_handle obj_handle,
  23. u32 nesting_level, void *context, void **return_value);
  24. static acpi_status
  25. acpi_ns_find_ini_methods(acpi_handle obj_handle,
  26. u32 nesting_level, void *context, void **return_value);
  27. /*******************************************************************************
  28. *
  29. * FUNCTION: acpi_ns_initialize_objects
  30. *
  31. * PARAMETERS: None
  32. *
  33. * RETURN: Status
  34. *
  35. * DESCRIPTION: Walk the entire namespace and perform any necessary
  36. * initialization on the objects found therein
  37. *
  38. ******************************************************************************/
  39. acpi_status acpi_ns_initialize_objects(void)
  40. {
  41. acpi_status status;
  42. struct acpi_init_walk_info info;
  43. ACPI_FUNCTION_TRACE(ns_initialize_objects);
  44. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  45. "[Init] Completing Initialization of ACPI Objects\n"));
  46. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  47. "**** Starting initialization of namespace objects ****\n"));
  48. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
  49. "Completing Region/Field/Buffer/Package initialization:\n"));
  50. /* Set all init info to zero */
  51. memset(&info, 0, sizeof(struct acpi_init_walk_info));
  52. /* Walk entire namespace from the supplied root */
  53. status = acpi_walk_namespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
  54. ACPI_UINT32_MAX, acpi_ns_init_one_object,
  55. NULL, &info, NULL);
  56. if (ACPI_FAILURE(status)) {
  57. ACPI_EXCEPTION((AE_INFO, status, "During WalkNamespace"));
  58. }
  59. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
  60. " Initialized %u/%u Regions %u/%u Fields %u/%u "
  61. "Buffers %u/%u Packages (%u nodes)\n",
  62. info.op_region_init, info.op_region_count,
  63. info.field_init, info.field_count,
  64. info.buffer_init, info.buffer_count,
  65. info.package_init, info.package_count,
  66. info.object_count));
  67. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  68. "%u Control Methods found\n%u Op Regions found\n",
  69. info.method_count, info.op_region_count));
  70. return_ACPI_STATUS(AE_OK);
  71. }
  72. /*******************************************************************************
  73. *
  74. * FUNCTION: acpi_ns_initialize_devices
  75. *
  76. * PARAMETERS: None
  77. *
  78. * RETURN: acpi_status
  79. *
  80. * DESCRIPTION: Walk the entire namespace and initialize all ACPI devices.
  81. * This means running _INI on all present devices.
  82. *
  83. * Note: We install PCI config space handler on region access,
  84. * not here.
  85. *
  86. ******************************************************************************/
  87. acpi_status acpi_ns_initialize_devices(u32 flags)
  88. {
  89. acpi_status status = AE_OK;
  90. struct acpi_device_walk_info info;
  91. acpi_handle handle;
  92. ACPI_FUNCTION_TRACE(ns_initialize_devices);
  93. if (!(flags & ACPI_NO_DEVICE_INIT)) {
  94. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  95. "[Init] Initializing ACPI Devices\n"));
  96. /* Init counters */
  97. info.device_count = 0;
  98. info.num_STA = 0;
  99. info.num_INI = 0;
  100. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
  101. "Initializing Device/Processor/Thermal objects "
  102. "and executing _INI/_STA methods:\n"));
  103. /* Tree analysis: find all subtrees that contain _INI methods */
  104. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
  105. ACPI_UINT32_MAX, FALSE,
  106. acpi_ns_find_ini_methods, NULL,
  107. &info, NULL);
  108. if (ACPI_FAILURE(status)) {
  109. goto error_exit;
  110. }
  111. /* Allocate the evaluation information block */
  112. info.evaluate_info =
  113. ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  114. if (!info.evaluate_info) {
  115. status = AE_NO_MEMORY;
  116. goto error_exit;
  117. }
  118. /*
  119. * Execute the "global" _INI method that may appear at the root.
  120. * This support is provided for Windows compatibility (Vista+) and
  121. * is not part of the ACPI specification.
  122. */
  123. info.evaluate_info->prefix_node = acpi_gbl_root_node;
  124. info.evaluate_info->relative_pathname = METHOD_NAME__INI;
  125. info.evaluate_info->parameters = NULL;
  126. info.evaluate_info->flags = ACPI_IGNORE_RETURN_VALUE;
  127. status = acpi_ns_evaluate(info.evaluate_info);
  128. if (ACPI_SUCCESS(status)) {
  129. info.num_INI++;
  130. }
  131. /*
  132. * Execute \_SB._INI.
  133. * There appears to be a strict order requirement for \_SB._INI,
  134. * which should be evaluated before any _REG evaluations.
  135. */
  136. status = acpi_get_handle(NULL, "\\_SB", &handle);
  137. if (ACPI_SUCCESS(status)) {
  138. memset(info.evaluate_info, 0,
  139. sizeof(struct acpi_evaluate_info));
  140. info.evaluate_info->prefix_node = handle;
  141. info.evaluate_info->relative_pathname =
  142. METHOD_NAME__INI;
  143. info.evaluate_info->parameters = NULL;
  144. info.evaluate_info->flags = ACPI_IGNORE_RETURN_VALUE;
  145. status = acpi_ns_evaluate(info.evaluate_info);
  146. if (ACPI_SUCCESS(status)) {
  147. info.num_INI++;
  148. }
  149. }
  150. }
  151. /*
  152. * Run all _REG methods
  153. *
  154. * Note: Any objects accessed by the _REG methods will be automatically
  155. * initialized, even if they contain executable AML (see the call to
  156. * acpi_ns_initialize_objects below).
  157. *
  158. * Note: According to the ACPI specification, we actually needn't execute
  159. * _REG for system_memory/system_io operation regions, but for PCI_Config
  160. * operation regions, it is required to evaluate _REG for those on a PCI
  161. * root bus that doesn't contain _BBN object. So this code is kept here
  162. * in order not to break things.
  163. */
  164. if (!(flags & ACPI_NO_ADDRESS_SPACE_INIT)) {
  165. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  166. "[Init] Executing _REG OpRegion methods\n"));
  167. status = acpi_ev_initialize_op_regions();
  168. if (ACPI_FAILURE(status)) {
  169. goto error_exit;
  170. }
  171. }
  172. if (!(flags & ACPI_NO_DEVICE_INIT)) {
  173. /* Walk namespace to execute all _INIs on present devices */
  174. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
  175. ACPI_UINT32_MAX, FALSE,
  176. acpi_ns_init_one_device, NULL,
  177. &info, NULL);
  178. /*
  179. * Any _OSI requests should be completed by now. If the BIOS has
  180. * requested any Windows OSI strings, we will always truncate
  181. * I/O addresses to 16 bits -- for Windows compatibility.
  182. */
  183. if (acpi_gbl_osi_data >= ACPI_OSI_WIN_2000) {
  184. acpi_gbl_truncate_io_addresses = TRUE;
  185. }
  186. ACPI_FREE(info.evaluate_info);
  187. if (ACPI_FAILURE(status)) {
  188. goto error_exit;
  189. }
  190. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
  191. " Executed %u _INI methods requiring %u _STA executions "
  192. "(examined %u objects)\n",
  193. info.num_INI, info.num_STA,
  194. info.device_count));
  195. }
  196. return_ACPI_STATUS(status);
  197. error_exit:
  198. ACPI_EXCEPTION((AE_INFO, status, "During device initialization"));
  199. return_ACPI_STATUS(status);
  200. }
  201. /*******************************************************************************
  202. *
  203. * FUNCTION: acpi_ns_init_one_object
  204. *
  205. * PARAMETERS: obj_handle - Node
  206. * level - Current nesting level
  207. * context - Points to a init info struct
  208. * return_value - Not used
  209. *
  210. * RETURN: Status
  211. *
  212. * DESCRIPTION: Callback from acpi_walk_namespace. Invoked for every object
  213. * within the namespace.
  214. *
  215. * Currently, the only objects that require initialization are:
  216. * 1) Methods
  217. * 2) Op Regions
  218. *
  219. ******************************************************************************/
  220. static acpi_status
  221. acpi_ns_init_one_object(acpi_handle obj_handle,
  222. u32 level, void *context, void **return_value)
  223. {
  224. acpi_object_type type;
  225. acpi_status status = AE_OK;
  226. struct acpi_init_walk_info *info =
  227. (struct acpi_init_walk_info *)context;
  228. struct acpi_namespace_node *node =
  229. (struct acpi_namespace_node *)obj_handle;
  230. union acpi_operand_object *obj_desc;
  231. ACPI_FUNCTION_NAME(ns_init_one_object);
  232. info->object_count++;
  233. /* And even then, we are only interested in a few object types */
  234. type = acpi_ns_get_type(obj_handle);
  235. obj_desc = acpi_ns_get_attached_object(node);
  236. if (!obj_desc) {
  237. return (AE_OK);
  238. }
  239. /* Increment counters for object types we are looking for */
  240. switch (type) {
  241. case ACPI_TYPE_REGION:
  242. info->op_region_count++;
  243. break;
  244. case ACPI_TYPE_BUFFER_FIELD:
  245. info->field_count++;
  246. break;
  247. case ACPI_TYPE_LOCAL_BANK_FIELD:
  248. info->field_count++;
  249. break;
  250. case ACPI_TYPE_BUFFER:
  251. info->buffer_count++;
  252. break;
  253. case ACPI_TYPE_PACKAGE:
  254. info->package_count++;
  255. break;
  256. default:
  257. /* No init required, just exit now */
  258. return (AE_OK);
  259. }
  260. /* If the object is already initialized, nothing else to do */
  261. if (obj_desc->common.flags & AOPOBJ_DATA_VALID) {
  262. return (AE_OK);
  263. }
  264. /* Must lock the interpreter before executing AML code */
  265. acpi_ex_enter_interpreter();
  266. /*
  267. * Each of these types can contain executable AML code within the
  268. * declaration.
  269. */
  270. switch (type) {
  271. case ACPI_TYPE_REGION:
  272. info->op_region_init++;
  273. status = acpi_ds_get_region_arguments(obj_desc);
  274. break;
  275. case ACPI_TYPE_BUFFER_FIELD:
  276. info->field_init++;
  277. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  278. break;
  279. case ACPI_TYPE_LOCAL_BANK_FIELD:
  280. info->field_init++;
  281. status = acpi_ds_get_bank_field_arguments(obj_desc);
  282. break;
  283. case ACPI_TYPE_BUFFER:
  284. info->buffer_init++;
  285. status = acpi_ds_get_buffer_arguments(obj_desc);
  286. break;
  287. case ACPI_TYPE_PACKAGE:
  288. info->package_init++;
  289. status = acpi_ds_get_package_arguments(obj_desc);
  290. if (ACPI_FAILURE(status)) {
  291. break;
  292. }
  293. ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
  294. "%s: Completing resolution of Package elements\n",
  295. ACPI_GET_FUNCTION_NAME));
  296. /*
  297. * Resolve all named references in package objects (and all
  298. * sub-packages). This action has been deferred until the entire
  299. * namespace has been loaded, in order to support external and
  300. * forward references from individual package elements (05/2017).
  301. */
  302. status = acpi_ut_walk_package_tree(obj_desc, NULL,
  303. acpi_ds_init_package_element,
  304. NULL);
  305. obj_desc->package.flags |= AOPOBJ_DATA_VALID;
  306. break;
  307. default:
  308. /* No other types can get here */
  309. break;
  310. }
  311. if (ACPI_FAILURE(status)) {
  312. ACPI_EXCEPTION((AE_INFO, status,
  313. "Could not execute arguments for [%4.4s] (%s)",
  314. acpi_ut_get_node_name(node),
  315. acpi_ut_get_type_name(type)));
  316. }
  317. /*
  318. * We ignore errors from above, and always return OK, since we don't want
  319. * to abort the walk on any single error.
  320. */
  321. acpi_ex_exit_interpreter();
  322. return (AE_OK);
  323. }
  324. /*******************************************************************************
  325. *
  326. * FUNCTION: acpi_ns_find_ini_methods
  327. *
  328. * PARAMETERS: acpi_walk_callback
  329. *
  330. * RETURN: acpi_status
  331. *
  332. * DESCRIPTION: Called during namespace walk. Finds objects named _INI under
  333. * device/processor/thermal objects, and marks the entire subtree
  334. * with a SUBTREE_HAS_INI flag. This flag is used during the
  335. * subsequent device initialization walk to avoid entire subtrees
  336. * that do not contain an _INI.
  337. *
  338. ******************************************************************************/
  339. static acpi_status
  340. acpi_ns_find_ini_methods(acpi_handle obj_handle,
  341. u32 nesting_level, void *context, void **return_value)
  342. {
  343. struct acpi_device_walk_info *info =
  344. ACPI_CAST_PTR(struct acpi_device_walk_info, context);
  345. struct acpi_namespace_node *node;
  346. struct acpi_namespace_node *parent_node;
  347. /* Keep count of device/processor/thermal objects */
  348. node = ACPI_CAST_PTR(struct acpi_namespace_node, obj_handle);
  349. if ((node->type == ACPI_TYPE_DEVICE) ||
  350. (node->type == ACPI_TYPE_PROCESSOR) ||
  351. (node->type == ACPI_TYPE_THERMAL)) {
  352. info->device_count++;
  353. return (AE_OK);
  354. }
  355. /* We are only looking for methods named _INI */
  356. if (!ACPI_COMPARE_NAME(node->name.ascii, METHOD_NAME__INI)) {
  357. return (AE_OK);
  358. }
  359. /*
  360. * The only _INI methods that we care about are those that are
  361. * present under Device, Processor, and Thermal objects.
  362. */
  363. parent_node = node->parent;
  364. switch (parent_node->type) {
  365. case ACPI_TYPE_DEVICE:
  366. case ACPI_TYPE_PROCESSOR:
  367. case ACPI_TYPE_THERMAL:
  368. /* Mark parent and bubble up the INI present flag to the root */
  369. while (parent_node) {
  370. parent_node->flags |= ANOBJ_SUBTREE_HAS_INI;
  371. parent_node = parent_node->parent;
  372. }
  373. break;
  374. default:
  375. break;
  376. }
  377. return (AE_OK);
  378. }
  379. /*******************************************************************************
  380. *
  381. * FUNCTION: acpi_ns_init_one_device
  382. *
  383. * PARAMETERS: acpi_walk_callback
  384. *
  385. * RETURN: acpi_status
  386. *
  387. * DESCRIPTION: This is called once per device soon after ACPI is enabled
  388. * to initialize each device. It determines if the device is
  389. * present, and if so, calls _INI.
  390. *
  391. ******************************************************************************/
  392. static acpi_status
  393. acpi_ns_init_one_device(acpi_handle obj_handle,
  394. u32 nesting_level, void *context, void **return_value)
  395. {
  396. struct acpi_device_walk_info *walk_info =
  397. ACPI_CAST_PTR(struct acpi_device_walk_info, context);
  398. struct acpi_evaluate_info *info = walk_info->evaluate_info;
  399. u32 flags;
  400. acpi_status status;
  401. struct acpi_namespace_node *device_node;
  402. ACPI_FUNCTION_TRACE(ns_init_one_device);
  403. /* We are interested in Devices, Processors and thermal_zones only */
  404. device_node = ACPI_CAST_PTR(struct acpi_namespace_node, obj_handle);
  405. if ((device_node->type != ACPI_TYPE_DEVICE) &&
  406. (device_node->type != ACPI_TYPE_PROCESSOR) &&
  407. (device_node->type != ACPI_TYPE_THERMAL)) {
  408. return_ACPI_STATUS(AE_OK);
  409. }
  410. /*
  411. * Because of an earlier namespace analysis, all subtrees that contain an
  412. * _INI method are tagged.
  413. *
  414. * If this device subtree does not contain any _INI methods, we
  415. * can exit now and stop traversing this entire subtree.
  416. */
  417. if (!(device_node->flags & ANOBJ_SUBTREE_HAS_INI)) {
  418. return_ACPI_STATUS(AE_CTRL_DEPTH);
  419. }
  420. /*
  421. * Run _STA to determine if this device is present and functioning. We
  422. * must know this information for two important reasons (from ACPI spec):
  423. *
  424. * 1) We can only run _INI if the device is present.
  425. * 2) We must abort the device tree walk on this subtree if the device is
  426. * not present and is not functional (we will not examine the children)
  427. *
  428. * The _STA method is not required to be present under the device, we
  429. * assume the device is present if _STA does not exist.
  430. */
  431. ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
  432. (ACPI_TYPE_METHOD, device_node, METHOD_NAME__STA));
  433. status = acpi_ut_execute_STA(device_node, &flags);
  434. if (ACPI_FAILURE(status)) {
  435. /* Ignore error and move on to next device */
  436. return_ACPI_STATUS(AE_OK);
  437. }
  438. /*
  439. * Flags == -1 means that _STA was not found. In this case, we assume that
  440. * the device is both present and functional.
  441. *
  442. * From the ACPI spec, description of _STA:
  443. *
  444. * "If a device object (including the processor object) does not have an
  445. * _STA object, then OSPM assumes that all of the above bits are set (in
  446. * other words, the device is present, ..., and functioning)"
  447. */
  448. if (flags != ACPI_UINT32_MAX) {
  449. walk_info->num_STA++;
  450. }
  451. /*
  452. * Examine the PRESENT and FUNCTIONING status bits
  453. *
  454. * Note: ACPI spec does not seem to specify behavior for the present but
  455. * not functioning case, so we assume functioning if present.
  456. */
  457. if (!(flags & ACPI_STA_DEVICE_PRESENT)) {
  458. /* Device is not present, we must examine the Functioning bit */
  459. if (flags & ACPI_STA_DEVICE_FUNCTIONING) {
  460. /*
  461. * Device is not present but is "functioning". In this case,
  462. * we will not run _INI, but we continue to examine the children
  463. * of this device.
  464. *
  465. * From the ACPI spec, description of _STA: (note - no mention
  466. * of whether to run _INI or not on the device in question)
  467. *
  468. * "_STA may return bit 0 clear (not present) with bit 3 set
  469. * (device is functional). This case is used to indicate a valid
  470. * device for which no device driver should be loaded (for example,
  471. * a bridge device.) Children of this device may be present and
  472. * valid. OSPM should continue enumeration below a device whose
  473. * _STA returns this bit combination"
  474. */
  475. return_ACPI_STATUS(AE_OK);
  476. } else {
  477. /*
  478. * Device is not present and is not functioning. We must abort the
  479. * walk of this subtree immediately -- don't look at the children
  480. * of such a device.
  481. *
  482. * From the ACPI spec, description of _INI:
  483. *
  484. * "If the _STA method indicates that the device is not present,
  485. * OSPM will not run the _INI and will not examine the children
  486. * of the device for _INI methods"
  487. */
  488. return_ACPI_STATUS(AE_CTRL_DEPTH);
  489. }
  490. }
  491. /*
  492. * The device is present or is assumed present if no _STA exists.
  493. * Run the _INI if it exists (not required to exist)
  494. *
  495. * Note: We know there is an _INI within this subtree, but it may not be
  496. * under this particular device, it may be lower in the branch.
  497. */
  498. if (!ACPI_COMPARE_NAME(device_node->name.ascii, "_SB_") ||
  499. device_node->parent != acpi_gbl_root_node) {
  500. ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
  501. (ACPI_TYPE_METHOD, device_node,
  502. METHOD_NAME__INI));
  503. memset(info, 0, sizeof(struct acpi_evaluate_info));
  504. info->prefix_node = device_node;
  505. info->relative_pathname = METHOD_NAME__INI;
  506. info->parameters = NULL;
  507. info->flags = ACPI_IGNORE_RETURN_VALUE;
  508. status = acpi_ns_evaluate(info);
  509. if (ACPI_SUCCESS(status)) {
  510. walk_info->num_INI++;
  511. }
  512. #ifdef ACPI_DEBUG_OUTPUT
  513. else if (status != AE_NOT_FOUND) {
  514. /* Ignore error and move on to next device */
  515. char *scope_name =
  516. acpi_ns_get_normalized_pathname(device_node, TRUE);
  517. ACPI_EXCEPTION((AE_INFO, status,
  518. "during %s._INI execution",
  519. scope_name));
  520. ACPI_FREE(scope_name);
  521. }
  522. #endif
  523. }
  524. /* Ignore errors from above */
  525. status = AE_OK;
  526. /*
  527. * The _INI method has been run if present; call the Global Initialization
  528. * Handler for this device.
  529. */
  530. if (acpi_gbl_init_handler) {
  531. status =
  532. acpi_gbl_init_handler(device_node, ACPI_INIT_DEVICE_INI);
  533. }
  534. return_ACPI_STATUS(status);
  535. }