bus.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
  4. *
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. */
  7. #define pr_fmt(fmt) "ACPI: " fmt
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/ioport.h>
  11. #include <linux/kernel.h>
  12. #include <linux/list.h>
  13. #include <linux/sched.h>
  14. #include <linux/pm.h>
  15. #include <linux/device.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/acpi.h>
  18. #include <linux/slab.h>
  19. #include <linux/regulator/machine.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/reboot.h>
  22. #include <linux/delay.h>
  23. #ifdef CONFIG_X86
  24. #include <asm/mpspec.h>
  25. #include <linux/dmi.h>
  26. #endif
  27. #include <linux/acpi_viot.h>
  28. #include <linux/pci.h>
  29. #include <acpi/apei.h>
  30. #include <linux/suspend.h>
  31. #include <linux/prmt.h>
  32. #include "internal.h"
  33. struct acpi_device *acpi_root;
  34. struct proc_dir_entry *acpi_root_dir;
  35. EXPORT_SYMBOL(acpi_root_dir);
  36. #ifdef CONFIG_X86
  37. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  38. static inline int set_copy_dsdt(const struct dmi_system_id *id)
  39. {
  40. return 0;
  41. }
  42. #else
  43. static int set_copy_dsdt(const struct dmi_system_id *id)
  44. {
  45. pr_notice("%s detected - force copy of DSDT to local memory\n", id->ident);
  46. acpi_gbl_copy_dsdt_locally = 1;
  47. return 0;
  48. }
  49. #endif
  50. static const struct dmi_system_id dsdt_dmi_table[] __initconst = {
  51. /*
  52. * Invoke DSDT corruption work-around on all Toshiba Satellite.
  53. * https://bugzilla.kernel.org/show_bug.cgi?id=14679
  54. */
  55. {
  56. .callback = set_copy_dsdt,
  57. .ident = "TOSHIBA Satellite",
  58. .matches = {
  59. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  60. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
  61. },
  62. },
  63. {}
  64. };
  65. #endif
  66. /* --------------------------------------------------------------------------
  67. Device Management
  68. -------------------------------------------------------------------------- */
  69. acpi_status acpi_bus_get_status_handle(acpi_handle handle,
  70. unsigned long long *sta)
  71. {
  72. acpi_status status;
  73. status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
  74. if (ACPI_SUCCESS(status))
  75. return AE_OK;
  76. if (status == AE_NOT_FOUND) {
  77. *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
  78. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
  79. return AE_OK;
  80. }
  81. return status;
  82. }
  83. EXPORT_SYMBOL_GPL(acpi_bus_get_status_handle);
  84. int acpi_bus_get_status(struct acpi_device *device)
  85. {
  86. acpi_status status;
  87. unsigned long long sta;
  88. if (acpi_device_override_status(device, &sta)) {
  89. acpi_set_device_status(device, sta);
  90. return 0;
  91. }
  92. /* Battery devices must have their deps met before calling _STA */
  93. if (acpi_device_is_battery(device) && device->dep_unmet) {
  94. acpi_set_device_status(device, 0);
  95. return 0;
  96. }
  97. status = acpi_bus_get_status_handle(device->handle, &sta);
  98. if (ACPI_FAILURE(status))
  99. return -ENODEV;
  100. if (!device->status.present && device->status.enabled) {
  101. pr_info(FW_BUG "Device [%s] status [%08x]: not present and enabled\n",
  102. device->pnp.bus_id, (u32)sta);
  103. device->status.enabled = 0;
  104. /*
  105. * The status is clearly invalid, so clear the functional bit as
  106. * well to avoid attempting to use the device.
  107. */
  108. device->status.functional = 0;
  109. }
  110. acpi_set_device_status(device, sta);
  111. if (device->status.functional && !device->status.present) {
  112. pr_debug("Device [%s] status [%08x]: functional but not present\n",
  113. device->pnp.bus_id, (u32)sta);
  114. }
  115. pr_debug("Device [%s] status [%08x]\n", device->pnp.bus_id, (u32)sta);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(acpi_bus_get_status);
  119. void acpi_bus_private_data_handler(acpi_handle handle,
  120. void *context)
  121. {
  122. return;
  123. }
  124. EXPORT_SYMBOL(acpi_bus_private_data_handler);
  125. int acpi_bus_attach_private_data(acpi_handle handle, void *data)
  126. {
  127. acpi_status status;
  128. status = acpi_attach_data(handle,
  129. acpi_bus_private_data_handler, data);
  130. if (ACPI_FAILURE(status)) {
  131. acpi_handle_debug(handle, "Error attaching device data\n");
  132. return -ENODEV;
  133. }
  134. return 0;
  135. }
  136. EXPORT_SYMBOL_GPL(acpi_bus_attach_private_data);
  137. int acpi_bus_get_private_data(acpi_handle handle, void **data)
  138. {
  139. acpi_status status;
  140. if (!data)
  141. return -EINVAL;
  142. status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
  143. if (ACPI_FAILURE(status)) {
  144. acpi_handle_debug(handle, "No context for object\n");
  145. return -ENODEV;
  146. }
  147. return 0;
  148. }
  149. EXPORT_SYMBOL_GPL(acpi_bus_get_private_data);
  150. void acpi_bus_detach_private_data(acpi_handle handle)
  151. {
  152. acpi_detach_data(handle, acpi_bus_private_data_handler);
  153. }
  154. EXPORT_SYMBOL_GPL(acpi_bus_detach_private_data);
  155. static void acpi_print_osc_error(acpi_handle handle,
  156. struct acpi_osc_context *context, char *error)
  157. {
  158. int i;
  159. acpi_handle_debug(handle, "(%s): %s\n", context->uuid_str, error);
  160. pr_debug("_OSC request data:");
  161. for (i = 0; i < context->cap.length; i += sizeof(u32))
  162. pr_debug(" %x", *((u32 *)(context->cap.pointer + i)));
  163. pr_debug("\n");
  164. }
  165. acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
  166. {
  167. acpi_status status;
  168. struct acpi_object_list input;
  169. union acpi_object in_params[4];
  170. union acpi_object *out_obj;
  171. guid_t guid;
  172. u32 errors;
  173. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  174. if (!context)
  175. return AE_ERROR;
  176. if (guid_parse(context->uuid_str, &guid))
  177. return AE_ERROR;
  178. context->ret.length = ACPI_ALLOCATE_BUFFER;
  179. context->ret.pointer = NULL;
  180. /* Setting up input parameters */
  181. input.count = 4;
  182. input.pointer = in_params;
  183. in_params[0].type = ACPI_TYPE_BUFFER;
  184. in_params[0].buffer.length = 16;
  185. in_params[0].buffer.pointer = (u8 *)&guid;
  186. in_params[1].type = ACPI_TYPE_INTEGER;
  187. in_params[1].integer.value = context->rev;
  188. in_params[2].type = ACPI_TYPE_INTEGER;
  189. in_params[2].integer.value = context->cap.length/sizeof(u32);
  190. in_params[3].type = ACPI_TYPE_BUFFER;
  191. in_params[3].buffer.length = context->cap.length;
  192. in_params[3].buffer.pointer = context->cap.pointer;
  193. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  194. if (ACPI_FAILURE(status))
  195. return status;
  196. if (!output.length)
  197. return AE_NULL_OBJECT;
  198. out_obj = output.pointer;
  199. if (out_obj->type != ACPI_TYPE_BUFFER
  200. || out_obj->buffer.length != context->cap.length) {
  201. acpi_print_osc_error(handle, context,
  202. "_OSC evaluation returned wrong type");
  203. status = AE_TYPE;
  204. goto out_kfree;
  205. }
  206. /* Need to ignore the bit0 in result code */
  207. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  208. if (errors) {
  209. if (errors & OSC_REQUEST_ERROR)
  210. acpi_print_osc_error(handle, context,
  211. "_OSC request failed");
  212. if (errors & OSC_INVALID_UUID_ERROR)
  213. acpi_print_osc_error(handle, context,
  214. "_OSC invalid UUID");
  215. if (errors & OSC_INVALID_REVISION_ERROR)
  216. acpi_print_osc_error(handle, context,
  217. "_OSC invalid revision");
  218. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  219. if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD]
  220. & OSC_QUERY_ENABLE)
  221. goto out_success;
  222. status = AE_SUPPORT;
  223. goto out_kfree;
  224. }
  225. status = AE_ERROR;
  226. goto out_kfree;
  227. }
  228. out_success:
  229. context->ret.length = out_obj->buffer.length;
  230. context->ret.pointer = kmemdup(out_obj->buffer.pointer,
  231. context->ret.length, GFP_KERNEL);
  232. if (!context->ret.pointer) {
  233. status = AE_NO_MEMORY;
  234. goto out_kfree;
  235. }
  236. status = AE_OK;
  237. out_kfree:
  238. kfree(output.pointer);
  239. return status;
  240. }
  241. EXPORT_SYMBOL(acpi_run_osc);
  242. bool osc_sb_apei_support_acked;
  243. /*
  244. * ACPI 6.0 Section 8.4.4.2 Idle State Coordination
  245. * OSPM supports platform coordinated low power idle(LPI) states
  246. */
  247. bool osc_pc_lpi_support_confirmed;
  248. EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed);
  249. /*
  250. * ACPI 6.2 Section 6.2.11.2 'Platform-Wide OSPM Capabilities':
  251. * Starting with ACPI Specification 6.2, all _CPC registers can be in
  252. * PCC, System Memory, System IO, or Functional Fixed Hardware address
  253. * spaces. OSPM support for this more flexible register space scheme is
  254. * indicated by the “Flexible Address Space for CPPC Registers” _OSC bit.
  255. *
  256. * Otherwise (cf ACPI 6.1, s8.4.7.1.1.X), _CPC registers must be in:
  257. * - PCC or Functional Fixed Hardware address space if defined
  258. * - SystemMemory address space (NULL register) if not defined
  259. */
  260. bool osc_cpc_flexible_adr_space_confirmed;
  261. EXPORT_SYMBOL_GPL(osc_cpc_flexible_adr_space_confirmed);
  262. /*
  263. * ACPI 6.4 Operating System Capabilities for USB.
  264. */
  265. bool osc_sb_native_usb4_support_confirmed;
  266. EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed);
  267. bool osc_sb_cppc2_support_acked;
  268. static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
  269. static void acpi_bus_osc_negotiate_platform_control(void)
  270. {
  271. u32 capbuf[2], *capbuf_ret;
  272. struct acpi_osc_context context = {
  273. .uuid_str = sb_uuid_str,
  274. .rev = 1,
  275. .cap.length = 8,
  276. .cap.pointer = capbuf,
  277. };
  278. acpi_handle handle;
  279. capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
  280. capbuf[OSC_SUPPORT_DWORD] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
  281. if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
  282. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PAD_SUPPORT;
  283. if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
  284. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PPC_OST_SUPPORT;
  285. if (IS_ENABLED(CONFIG_ACPI_THERMAL))
  286. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_FAST_THERMAL_SAMPLING_SUPPORT;
  287. if (IS_ENABLED(CONFIG_ACPI_BATTERY))
  288. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_BATTERY_CHARGE_LIMITING_SUPPORT;
  289. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT;
  290. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PCLPI_SUPPORT;
  291. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_OVER_16_PSTATES_SUPPORT;
  292. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GED_SUPPORT;
  293. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_IRQ_RESOURCE_SOURCE_SUPPORT;
  294. if (IS_ENABLED(CONFIG_ACPI_PRMT))
  295. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PRM_SUPPORT;
  296. if (IS_ENABLED(CONFIG_ACPI_FFH))
  297. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_FFH_OPR_SUPPORT;
  298. #ifdef CONFIG_ARM64
  299. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
  300. #endif
  301. #ifdef CONFIG_X86
  302. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
  303. #endif
  304. #ifdef CONFIG_ACPI_CPPC_LIB
  305. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_SUPPORT;
  306. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPCV2_SUPPORT;
  307. #endif
  308. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
  309. if (IS_ENABLED(CONFIG_SCHED_MC_PRIO))
  310. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_CPC_DIVERSE_HIGH_SUPPORT;
  311. if (IS_ENABLED(CONFIG_USB4))
  312. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_NATIVE_USB4_SUPPORT;
  313. if (!ghes_disable)
  314. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_APEI_SUPPORT;
  315. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  316. return;
  317. if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
  318. return;
  319. capbuf_ret = context.ret.pointer;
  320. if (context.ret.length <= OSC_SUPPORT_DWORD) {
  321. kfree(context.ret.pointer);
  322. return;
  323. }
  324. /*
  325. * Now run _OSC again with query flag clear and with the caps
  326. * supported by both the OS and the platform.
  327. */
  328. capbuf[OSC_QUERY_DWORD] = 0;
  329. capbuf[OSC_SUPPORT_DWORD] = capbuf_ret[OSC_SUPPORT_DWORD];
  330. kfree(context.ret.pointer);
  331. if (ACPI_FAILURE(acpi_run_osc(handle, &context)))
  332. return;
  333. capbuf_ret = context.ret.pointer;
  334. if (context.ret.length > OSC_SUPPORT_DWORD) {
  335. #ifdef CONFIG_ACPI_CPPC_LIB
  336. osc_sb_cppc2_support_acked = capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_CPCV2_SUPPORT;
  337. #endif
  338. osc_sb_apei_support_acked =
  339. capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT;
  340. osc_pc_lpi_support_confirmed =
  341. capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_PCLPI_SUPPORT;
  342. osc_sb_native_usb4_support_confirmed =
  343. capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_NATIVE_USB4_SUPPORT;
  344. osc_cpc_flexible_adr_space_confirmed =
  345. capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
  346. }
  347. kfree(context.ret.pointer);
  348. }
  349. /*
  350. * Native control of USB4 capabilities. If any of the tunneling bits is
  351. * set it means OS is in control and we use software based connection
  352. * manager.
  353. */
  354. u32 osc_sb_native_usb4_control;
  355. EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control);
  356. static void acpi_bus_decode_usb_osc(const char *msg, u32 bits)
  357. {
  358. pr_info("%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg,
  359. (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-',
  360. (bits & OSC_USB_DP_TUNNELING) ? '+' : '-',
  361. (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-',
  362. (bits & OSC_USB_XDOMAIN) ? '+' : '-');
  363. }
  364. static u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A";
  365. static void acpi_bus_osc_negotiate_usb_control(void)
  366. {
  367. u32 capbuf[3], *capbuf_ret;
  368. struct acpi_osc_context context = {
  369. .uuid_str = sb_usb_uuid_str,
  370. .rev = 1,
  371. .cap.length = sizeof(capbuf),
  372. .cap.pointer = capbuf,
  373. };
  374. acpi_handle handle;
  375. acpi_status status;
  376. u32 control;
  377. if (!osc_sb_native_usb4_support_confirmed)
  378. return;
  379. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  380. return;
  381. control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING |
  382. OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN;
  383. /*
  384. * Run _OSC first with query bit set, trying to get control over
  385. * all tunneling. The platform can then clear out bits in the
  386. * control dword that it does not want to grant to the OS.
  387. */
  388. capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
  389. capbuf[OSC_SUPPORT_DWORD] = 0;
  390. capbuf[OSC_CONTROL_DWORD] = control;
  391. status = acpi_run_osc(handle, &context);
  392. if (ACPI_FAILURE(status))
  393. return;
  394. if (context.ret.length != sizeof(capbuf)) {
  395. pr_info("USB4 _OSC: returned invalid length buffer\n");
  396. goto out_free;
  397. }
  398. /*
  399. * Run _OSC again now with query bit clear and the control dword
  400. * matching what the platform granted (which may not have all
  401. * the control bits set).
  402. */
  403. capbuf_ret = context.ret.pointer;
  404. capbuf[OSC_QUERY_DWORD] = 0;
  405. capbuf[OSC_CONTROL_DWORD] = capbuf_ret[OSC_CONTROL_DWORD];
  406. kfree(context.ret.pointer);
  407. status = acpi_run_osc(handle, &context);
  408. if (ACPI_FAILURE(status))
  409. return;
  410. if (context.ret.length != sizeof(capbuf)) {
  411. pr_info("USB4 _OSC: returned invalid length buffer\n");
  412. goto out_free;
  413. }
  414. osc_sb_native_usb4_control =
  415. control & acpi_osc_ctx_get_pci_control(&context);
  416. acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control);
  417. acpi_bus_decode_usb_osc("USB4 _OSC: OS controls",
  418. osc_sb_native_usb4_control);
  419. out_free:
  420. kfree(context.ret.pointer);
  421. }
  422. /* --------------------------------------------------------------------------
  423. Notification Handling
  424. -------------------------------------------------------------------------- */
  425. /**
  426. * acpi_bus_notify - Global system-level (0x00-0x7F) notifications handler
  427. * @handle: Target ACPI object.
  428. * @type: Notification type.
  429. * @data: Ignored.
  430. *
  431. * This only handles notifications related to device hotplug.
  432. */
  433. static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
  434. {
  435. struct acpi_device *adev;
  436. switch (type) {
  437. case ACPI_NOTIFY_BUS_CHECK:
  438. acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
  439. break;
  440. case ACPI_NOTIFY_DEVICE_CHECK:
  441. acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
  442. break;
  443. case ACPI_NOTIFY_DEVICE_WAKE:
  444. acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
  445. return;
  446. case ACPI_NOTIFY_EJECT_REQUEST:
  447. acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
  448. break;
  449. case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
  450. acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
  451. /* TBD: Exactly what does 'light' mean? */
  452. return;
  453. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  454. acpi_handle_err(handle, "Device cannot be configured due "
  455. "to a frequency mismatch\n");
  456. return;
  457. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  458. acpi_handle_err(handle, "Device cannot be configured due "
  459. "to a bus mode mismatch\n");
  460. return;
  461. case ACPI_NOTIFY_POWER_FAULT:
  462. acpi_handle_err(handle, "Device has suffered a power fault\n");
  463. return;
  464. default:
  465. acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
  466. return;
  467. }
  468. adev = acpi_get_acpi_dev(handle);
  469. if (adev && ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
  470. return;
  471. acpi_put_acpi_dev(adev);
  472. acpi_evaluate_ost(handle, type, ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
  473. }
  474. static void acpi_notify_device(acpi_handle handle, u32 event, void *data)
  475. {
  476. struct acpi_device *device = data;
  477. struct acpi_driver *acpi_drv = to_acpi_driver(device->dev.driver);
  478. acpi_drv->ops.notify(device, event);
  479. }
  480. static int acpi_device_install_notify_handler(struct acpi_device *device,
  481. struct acpi_driver *acpi_drv)
  482. {
  483. u32 type = acpi_drv->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS ?
  484. ACPI_ALL_NOTIFY : ACPI_DEVICE_NOTIFY;
  485. acpi_status status;
  486. status = acpi_install_notify_handler(device->handle, type,
  487. acpi_notify_device, device);
  488. if (ACPI_FAILURE(status))
  489. return -EINVAL;
  490. return 0;
  491. }
  492. static void acpi_device_remove_notify_handler(struct acpi_device *device,
  493. struct acpi_driver *acpi_drv)
  494. {
  495. u32 type = acpi_drv->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS ?
  496. ACPI_ALL_NOTIFY : ACPI_DEVICE_NOTIFY;
  497. acpi_remove_notify_handler(device->handle, type,
  498. acpi_notify_device);
  499. acpi_os_wait_events_complete();
  500. }
  501. int acpi_dev_install_notify_handler(struct acpi_device *adev,
  502. u32 handler_type,
  503. acpi_notify_handler handler, void *context)
  504. {
  505. acpi_status status;
  506. status = acpi_install_notify_handler(adev->handle, handler_type,
  507. handler, context);
  508. if (ACPI_FAILURE(status))
  509. return -ENODEV;
  510. return 0;
  511. }
  512. EXPORT_SYMBOL_GPL(acpi_dev_install_notify_handler);
  513. void acpi_dev_remove_notify_handler(struct acpi_device *adev,
  514. u32 handler_type,
  515. acpi_notify_handler handler)
  516. {
  517. acpi_remove_notify_handler(adev->handle, handler_type, handler);
  518. acpi_os_wait_events_complete();
  519. }
  520. EXPORT_SYMBOL_GPL(acpi_dev_remove_notify_handler);
  521. /* Handle events targeting \_SB device (at present only graceful shutdown) */
  522. #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
  523. #define ACPI_SB_INDICATE_INTERVAL 10000
  524. static void sb_notify_work(struct work_struct *dummy)
  525. {
  526. acpi_handle sb_handle;
  527. orderly_poweroff(true);
  528. /*
  529. * After initiating graceful shutdown, the ACPI spec requires OSPM
  530. * to evaluate _OST method once every 10seconds to indicate that
  531. * the shutdown is in progress
  532. */
  533. acpi_get_handle(NULL, "\\_SB", &sb_handle);
  534. while (1) {
  535. pr_info("Graceful shutdown in progress.\n");
  536. acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
  537. ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
  538. msleep(ACPI_SB_INDICATE_INTERVAL);
  539. }
  540. }
  541. static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
  542. {
  543. static DECLARE_WORK(acpi_sb_work, sb_notify_work);
  544. if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
  545. if (!work_busy(&acpi_sb_work))
  546. schedule_work(&acpi_sb_work);
  547. } else {
  548. pr_warn("event %x is not supported by \\_SB device\n", event);
  549. }
  550. }
  551. static int __init acpi_setup_sb_notify_handler(void)
  552. {
  553. acpi_handle sb_handle;
  554. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
  555. return -ENXIO;
  556. if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
  557. acpi_sb_notify, NULL)))
  558. return -EINVAL;
  559. return 0;
  560. }
  561. /* --------------------------------------------------------------------------
  562. Device Matching
  563. -------------------------------------------------------------------------- */
  564. /**
  565. * acpi_get_first_physical_node - Get first physical node of an ACPI device
  566. * @adev: ACPI device in question
  567. *
  568. * Return: First physical node of ACPI device @adev
  569. */
  570. struct device *acpi_get_first_physical_node(struct acpi_device *adev)
  571. {
  572. struct mutex *physical_node_lock = &adev->physical_node_lock;
  573. struct device *phys_dev;
  574. mutex_lock(physical_node_lock);
  575. if (list_empty(&adev->physical_node_list)) {
  576. phys_dev = NULL;
  577. } else {
  578. const struct acpi_device_physical_node *node;
  579. node = list_first_entry(&adev->physical_node_list,
  580. struct acpi_device_physical_node, node);
  581. phys_dev = node->dev;
  582. }
  583. mutex_unlock(physical_node_lock);
  584. return phys_dev;
  585. }
  586. EXPORT_SYMBOL_GPL(acpi_get_first_physical_node);
  587. static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
  588. const struct device *dev)
  589. {
  590. const struct device *phys_dev = acpi_get_first_physical_node(adev);
  591. return phys_dev && phys_dev == dev ? adev : NULL;
  592. }
  593. /**
  594. * acpi_device_is_first_physical_node - Is given dev first physical node
  595. * @adev: ACPI companion device
  596. * @dev: Physical device to check
  597. *
  598. * Function checks if given @dev is the first physical devices attached to
  599. * the ACPI companion device. This distinction is needed in some cases
  600. * where the same companion device is shared between many physical devices.
  601. *
  602. * Note that the caller have to provide valid @adev pointer.
  603. */
  604. bool acpi_device_is_first_physical_node(struct acpi_device *adev,
  605. const struct device *dev)
  606. {
  607. return !!acpi_primary_dev_companion(adev, dev);
  608. }
  609. /*
  610. * acpi_companion_match() - Can we match via ACPI companion device
  611. * @dev: Device in question
  612. *
  613. * Check if the given device has an ACPI companion and if that companion has
  614. * a valid list of PNP IDs, and if the device is the first (primary) physical
  615. * device associated with it. Return the companion pointer if that's the case
  616. * or NULL otherwise.
  617. *
  618. * If multiple physical devices are attached to a single ACPI companion, we need
  619. * to be careful. The usage scenario for this kind of relationship is that all
  620. * of the physical devices in question use resources provided by the ACPI
  621. * companion. A typical case is an MFD device where all the sub-devices share
  622. * the parent's ACPI companion. In such cases we can only allow the primary
  623. * (first) physical device to be matched with the help of the companion's PNP
  624. * IDs.
  625. *
  626. * Additional physical devices sharing the ACPI companion can still use
  627. * resources available from it but they will be matched normally using functions
  628. * provided by their bus types (and analogously for their modalias).
  629. */
  630. const struct acpi_device *acpi_companion_match(const struct device *dev)
  631. {
  632. struct acpi_device *adev;
  633. adev = ACPI_COMPANION(dev);
  634. if (!adev)
  635. return NULL;
  636. if (list_empty(&adev->pnp.ids))
  637. return NULL;
  638. return acpi_primary_dev_companion(adev, dev);
  639. }
  640. /**
  641. * acpi_of_match_device - Match device object using the "compatible" property.
  642. * @adev: ACPI device object to match.
  643. * @of_match_table: List of device IDs to match against.
  644. * @of_id: OF ID if matched
  645. *
  646. * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
  647. * identifiers and a _DSD object with the "compatible" property, use that
  648. * property to match against the given list of identifiers.
  649. */
  650. static bool acpi_of_match_device(const struct acpi_device *adev,
  651. const struct of_device_id *of_match_table,
  652. const struct of_device_id **of_id)
  653. {
  654. const union acpi_object *of_compatible, *obj;
  655. int i, nval;
  656. if (!adev)
  657. return false;
  658. of_compatible = adev->data.of_compatible;
  659. if (!of_match_table || !of_compatible)
  660. return false;
  661. if (of_compatible->type == ACPI_TYPE_PACKAGE) {
  662. nval = of_compatible->package.count;
  663. obj = of_compatible->package.elements;
  664. } else { /* Must be ACPI_TYPE_STRING. */
  665. nval = 1;
  666. obj = of_compatible;
  667. }
  668. /* Now we can look for the driver DT compatible strings */
  669. for (i = 0; i < nval; i++, obj++) {
  670. const struct of_device_id *id;
  671. for (id = of_match_table; id->compatible[0]; id++)
  672. if (!strcasecmp(obj->string.pointer, id->compatible)) {
  673. if (of_id)
  674. *of_id = id;
  675. return true;
  676. }
  677. }
  678. return false;
  679. }
  680. static bool acpi_of_modalias(struct acpi_device *adev,
  681. char *modalias, size_t len)
  682. {
  683. const union acpi_object *of_compatible;
  684. const union acpi_object *obj;
  685. const char *str, *chr;
  686. of_compatible = adev->data.of_compatible;
  687. if (!of_compatible)
  688. return false;
  689. if (of_compatible->type == ACPI_TYPE_PACKAGE)
  690. obj = of_compatible->package.elements;
  691. else /* Must be ACPI_TYPE_STRING. */
  692. obj = of_compatible;
  693. str = obj->string.pointer;
  694. chr = strchr(str, ',');
  695. strscpy(modalias, chr ? chr + 1 : str, len);
  696. return true;
  697. }
  698. /**
  699. * acpi_set_modalias - Set modalias using "compatible" property or supplied ID
  700. * @adev: ACPI device object to match
  701. * @default_id: ID string to use as default if no compatible string found
  702. * @modalias: Pointer to buffer that modalias value will be copied into
  703. * @len: Length of modalias buffer
  704. *
  705. * This is a counterpart of of_alias_from_compatible() for struct acpi_device
  706. * objects. If there is a compatible string for @adev, it will be copied to
  707. * @modalias with the vendor prefix stripped; otherwise, @default_id will be
  708. * used.
  709. */
  710. void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
  711. char *modalias, size_t len)
  712. {
  713. if (!acpi_of_modalias(adev, modalias, len))
  714. strscpy(modalias, default_id, len);
  715. }
  716. EXPORT_SYMBOL_GPL(acpi_set_modalias);
  717. static bool __acpi_match_device_cls(const struct acpi_device_id *id,
  718. struct acpi_hardware_id *hwid)
  719. {
  720. int i, msk, byte_shift;
  721. char buf[3];
  722. if (!id->cls)
  723. return false;
  724. /* Apply class-code bitmask, before checking each class-code byte */
  725. for (i = 1; i <= 3; i++) {
  726. byte_shift = 8 * (3 - i);
  727. msk = (id->cls_msk >> byte_shift) & 0xFF;
  728. if (!msk)
  729. continue;
  730. sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
  731. if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
  732. return false;
  733. }
  734. return true;
  735. }
  736. static bool __acpi_match_device(const struct acpi_device *device,
  737. const struct acpi_device_id *acpi_ids,
  738. const struct of_device_id *of_ids,
  739. const struct acpi_device_id **acpi_id,
  740. const struct of_device_id **of_id)
  741. {
  742. const struct acpi_device_id *id;
  743. struct acpi_hardware_id *hwid;
  744. /*
  745. * If the device is not present, it is unnecessary to load device
  746. * driver for it.
  747. */
  748. if (!device || !device->status.present)
  749. return false;
  750. list_for_each_entry(hwid, &device->pnp.ids, list) {
  751. /* First, check the ACPI/PNP IDs provided by the caller. */
  752. if (acpi_ids) {
  753. for (id = acpi_ids; id->id[0] || id->cls; id++) {
  754. if (id->id[0] && !strcmp((char *)id->id, hwid->id))
  755. goto out_acpi_match;
  756. if (id->cls && __acpi_match_device_cls(id, hwid))
  757. goto out_acpi_match;
  758. }
  759. }
  760. /*
  761. * Next, check ACPI_DT_NAMESPACE_HID and try to match the
  762. * "compatible" property if found.
  763. */
  764. if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id))
  765. return acpi_of_match_device(device, of_ids, of_id);
  766. }
  767. return false;
  768. out_acpi_match:
  769. if (acpi_id)
  770. *acpi_id = id;
  771. return true;
  772. }
  773. /**
  774. * acpi_match_acpi_device - Match an ACPI device against a given list of ACPI IDs
  775. * @ids: Array of struct acpi_device_id objects to match against.
  776. * @adev: The ACPI device pointer to match.
  777. *
  778. * Match the ACPI device @adev against a given list of ACPI IDs @ids.
  779. *
  780. * Return:
  781. * a pointer to the first matching ACPI ID on success or %NULL on failure.
  782. */
  783. const struct acpi_device_id *acpi_match_acpi_device(const struct acpi_device_id *ids,
  784. const struct acpi_device *adev)
  785. {
  786. const struct acpi_device_id *id = NULL;
  787. __acpi_match_device(adev, ids, NULL, &id, NULL);
  788. return id;
  789. }
  790. EXPORT_SYMBOL_GPL(acpi_match_acpi_device);
  791. /**
  792. * acpi_match_device - Match a struct device against a given list of ACPI IDs
  793. * @ids: Array of struct acpi_device_id object to match against.
  794. * @dev: The device structure to match.
  795. *
  796. * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
  797. * object for that handle and use that object to match against a given list of
  798. * device IDs.
  799. *
  800. * Return a pointer to the first matching ID on success or %NULL on failure.
  801. */
  802. const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
  803. const struct device *dev)
  804. {
  805. return acpi_match_acpi_device(ids, acpi_companion_match(dev));
  806. }
  807. EXPORT_SYMBOL_GPL(acpi_match_device);
  808. static const void *acpi_of_device_get_match_data(const struct device *dev)
  809. {
  810. struct acpi_device *adev = ACPI_COMPANION(dev);
  811. const struct of_device_id *match = NULL;
  812. if (!acpi_of_match_device(adev, dev->driver->of_match_table, &match))
  813. return NULL;
  814. return match->data;
  815. }
  816. const void *acpi_device_get_match_data(const struct device *dev)
  817. {
  818. const struct acpi_device_id *acpi_ids = dev->driver->acpi_match_table;
  819. const struct acpi_device_id *match;
  820. if (!acpi_ids)
  821. return acpi_of_device_get_match_data(dev);
  822. match = acpi_match_device(acpi_ids, dev);
  823. if (!match)
  824. return NULL;
  825. return (const void *)match->driver_data;
  826. }
  827. EXPORT_SYMBOL_GPL(acpi_device_get_match_data);
  828. int acpi_match_device_ids(struct acpi_device *device,
  829. const struct acpi_device_id *ids)
  830. {
  831. return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT;
  832. }
  833. EXPORT_SYMBOL(acpi_match_device_ids);
  834. bool acpi_driver_match_device(struct device *dev,
  835. const struct device_driver *drv)
  836. {
  837. const struct acpi_device_id *acpi_ids = drv->acpi_match_table;
  838. const struct of_device_id *of_ids = drv->of_match_table;
  839. if (!acpi_ids)
  840. return acpi_of_match_device(ACPI_COMPANION(dev), of_ids, NULL);
  841. return __acpi_match_device(acpi_companion_match(dev), acpi_ids, of_ids, NULL, NULL);
  842. }
  843. EXPORT_SYMBOL_GPL(acpi_driver_match_device);
  844. /* --------------------------------------------------------------------------
  845. ACPI Driver Management
  846. -------------------------------------------------------------------------- */
  847. /**
  848. * __acpi_bus_register_driver - register a driver with the ACPI bus
  849. * @driver: driver being registered
  850. * @owner: owning module/driver
  851. *
  852. * Registers a driver with the ACPI bus. Searches the namespace for all
  853. * devices that match the driver's criteria and binds. Returns zero for
  854. * success or a negative error status for failure.
  855. */
  856. int __acpi_bus_register_driver(struct acpi_driver *driver, struct module *owner)
  857. {
  858. if (acpi_disabled)
  859. return -ENODEV;
  860. driver->drv.name = driver->name;
  861. driver->drv.bus = &acpi_bus_type;
  862. driver->drv.owner = owner;
  863. return driver_register(&driver->drv);
  864. }
  865. EXPORT_SYMBOL(__acpi_bus_register_driver);
  866. /**
  867. * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
  868. * @driver: driver to unregister
  869. *
  870. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  871. * devices that match the driver's criteria and unbinds.
  872. */
  873. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  874. {
  875. driver_unregister(&driver->drv);
  876. }
  877. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  878. /* --------------------------------------------------------------------------
  879. ACPI Bus operations
  880. -------------------------------------------------------------------------- */
  881. static int acpi_bus_match(struct device *dev, const struct device_driver *drv)
  882. {
  883. struct acpi_device *acpi_dev = to_acpi_device(dev);
  884. const struct acpi_driver *acpi_drv = to_acpi_driver(drv);
  885. return acpi_dev->flags.match_driver
  886. && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
  887. }
  888. static int acpi_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
  889. {
  890. return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
  891. }
  892. static int acpi_device_probe(struct device *dev)
  893. {
  894. struct acpi_device *acpi_dev = to_acpi_device(dev);
  895. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  896. int ret;
  897. if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
  898. return -EINVAL;
  899. if (!acpi_drv->ops.add)
  900. return -ENOSYS;
  901. ret = acpi_drv->ops.add(acpi_dev);
  902. if (ret) {
  903. acpi_dev->driver_data = NULL;
  904. return ret;
  905. }
  906. pr_debug("Driver [%s] successfully bound to device [%s]\n",
  907. acpi_drv->name, acpi_dev->pnp.bus_id);
  908. if (acpi_drv->ops.notify) {
  909. ret = acpi_device_install_notify_handler(acpi_dev, acpi_drv);
  910. if (ret) {
  911. if (acpi_drv->ops.remove)
  912. acpi_drv->ops.remove(acpi_dev);
  913. acpi_dev->driver_data = NULL;
  914. return ret;
  915. }
  916. }
  917. pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name,
  918. acpi_dev->pnp.bus_id);
  919. get_device(dev);
  920. return 0;
  921. }
  922. static void acpi_device_remove(struct device *dev)
  923. {
  924. struct acpi_device *acpi_dev = to_acpi_device(dev);
  925. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  926. if (acpi_drv->ops.notify)
  927. acpi_device_remove_notify_handler(acpi_dev, acpi_drv);
  928. if (acpi_drv->ops.remove)
  929. acpi_drv->ops.remove(acpi_dev);
  930. acpi_dev->driver_data = NULL;
  931. put_device(dev);
  932. }
  933. const struct bus_type acpi_bus_type = {
  934. .name = "acpi",
  935. .match = acpi_bus_match,
  936. .probe = acpi_device_probe,
  937. .remove = acpi_device_remove,
  938. .uevent = acpi_device_uevent,
  939. };
  940. int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data)
  941. {
  942. return bus_for_each_dev(&acpi_bus_type, NULL, data, fn);
  943. }
  944. EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
  945. struct acpi_dev_walk_context {
  946. int (*fn)(struct acpi_device *, void *);
  947. void *data;
  948. };
  949. static int acpi_dev_for_one_check(struct device *dev, void *context)
  950. {
  951. struct acpi_dev_walk_context *adwc = context;
  952. if (dev->bus != &acpi_bus_type)
  953. return 0;
  954. return adwc->fn(to_acpi_device(dev), adwc->data);
  955. }
  956. EXPORT_SYMBOL_GPL(acpi_dev_for_each_child);
  957. int acpi_dev_for_each_child(struct acpi_device *adev,
  958. int (*fn)(struct acpi_device *, void *), void *data)
  959. {
  960. struct acpi_dev_walk_context adwc = {
  961. .fn = fn,
  962. .data = data,
  963. };
  964. return device_for_each_child(&adev->dev, &adwc, acpi_dev_for_one_check);
  965. }
  966. int acpi_dev_for_each_child_reverse(struct acpi_device *adev,
  967. int (*fn)(struct acpi_device *, void *),
  968. void *data)
  969. {
  970. struct acpi_dev_walk_context adwc = {
  971. .fn = fn,
  972. .data = data,
  973. };
  974. return device_for_each_child_reverse(&adev->dev, &adwc, acpi_dev_for_one_check);
  975. }
  976. /* --------------------------------------------------------------------------
  977. Initialization/Cleanup
  978. -------------------------------------------------------------------------- */
  979. static int __init acpi_bus_init_irq(void)
  980. {
  981. acpi_status status;
  982. char *message = NULL;
  983. /*
  984. * Let the system know what interrupt model we are using by
  985. * evaluating the \_PIC object, if exists.
  986. */
  987. switch (acpi_irq_model) {
  988. case ACPI_IRQ_MODEL_PIC:
  989. message = "PIC";
  990. break;
  991. case ACPI_IRQ_MODEL_IOAPIC:
  992. message = "IOAPIC";
  993. break;
  994. case ACPI_IRQ_MODEL_IOSAPIC:
  995. message = "IOSAPIC";
  996. break;
  997. case ACPI_IRQ_MODEL_GIC:
  998. message = "GIC";
  999. break;
  1000. case ACPI_IRQ_MODEL_PLATFORM:
  1001. message = "platform specific model";
  1002. break;
  1003. case ACPI_IRQ_MODEL_LPIC:
  1004. message = "LPIC";
  1005. break;
  1006. case ACPI_IRQ_MODEL_RINTC:
  1007. message = "RINTC";
  1008. break;
  1009. default:
  1010. pr_info("Unknown interrupt routing model\n");
  1011. return -ENODEV;
  1012. }
  1013. pr_info("Using %s for interrupt routing\n", message);
  1014. status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
  1015. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  1016. pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status));
  1017. return -ENODEV;
  1018. }
  1019. return 0;
  1020. }
  1021. /**
  1022. * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
  1023. *
  1024. * The ACPI tables are accessible after this, but the handling of events has not
  1025. * been initialized and the global lock is not available yet, so AML should not
  1026. * be executed at this point.
  1027. *
  1028. * Doing this before switching the EFI runtime services to virtual mode allows
  1029. * the EfiBootServices memory to be freed slightly earlier on boot.
  1030. */
  1031. void __init acpi_early_init(void)
  1032. {
  1033. acpi_status status;
  1034. if (acpi_disabled)
  1035. return;
  1036. pr_info("Core revision %08x\n", ACPI_CA_VERSION);
  1037. /* enable workarounds, unless strict ACPI spec. compliance */
  1038. if (!acpi_strict)
  1039. acpi_gbl_enable_interpreter_slack = TRUE;
  1040. acpi_permanent_mmap = true;
  1041. #ifdef CONFIG_X86
  1042. /*
  1043. * If the machine falls into the DMI check table,
  1044. * DSDT will be copied to memory.
  1045. * Note that calling dmi_check_system() here on other architectures
  1046. * would not be OK because only x86 initializes dmi early enough.
  1047. * Thankfully only x86 systems need such quirks for now.
  1048. */
  1049. dmi_check_system(dsdt_dmi_table);
  1050. #endif
  1051. status = acpi_reallocate_root_table();
  1052. if (ACPI_FAILURE(status)) {
  1053. pr_err("Unable to reallocate ACPI tables\n");
  1054. goto error0;
  1055. }
  1056. status = acpi_initialize_subsystem();
  1057. if (ACPI_FAILURE(status)) {
  1058. pr_err("Unable to initialize the ACPI Interpreter\n");
  1059. goto error0;
  1060. }
  1061. #ifdef CONFIG_X86
  1062. if (!acpi_ioapic) {
  1063. /* compatible (0) means level (3) */
  1064. if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
  1065. acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
  1066. acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
  1067. }
  1068. /* Set PIC-mode SCI trigger type */
  1069. acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
  1070. (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
  1071. } else {
  1072. /*
  1073. * now that acpi_gbl_FADT is initialized,
  1074. * update it with result from INT_SRC_OVR parsing
  1075. */
  1076. acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
  1077. }
  1078. #endif
  1079. return;
  1080. error0:
  1081. disable_acpi();
  1082. }
  1083. /**
  1084. * acpi_subsystem_init - Finalize the early initialization of ACPI.
  1085. *
  1086. * Switch over the platform to the ACPI mode (if possible).
  1087. *
  1088. * Doing this too early is generally unsafe, but at the same time it needs to be
  1089. * done before all things that really depend on ACPI. The right spot appears to
  1090. * be before finalizing the EFI initialization.
  1091. */
  1092. void __init acpi_subsystem_init(void)
  1093. {
  1094. acpi_status status;
  1095. if (acpi_disabled)
  1096. return;
  1097. status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
  1098. if (ACPI_FAILURE(status)) {
  1099. pr_err("Unable to enable ACPI\n");
  1100. disable_acpi();
  1101. } else {
  1102. /*
  1103. * If the system is using ACPI then we can be reasonably
  1104. * confident that any regulators are managed by the firmware
  1105. * so tell the regulator core it has everything it needs to
  1106. * know.
  1107. */
  1108. regulator_has_full_constraints();
  1109. }
  1110. }
  1111. static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
  1112. {
  1113. if (event == ACPI_TABLE_EVENT_LOAD)
  1114. acpi_scan_table_notify();
  1115. return acpi_sysfs_table_handler(event, table, context);
  1116. }
  1117. static int __init acpi_bus_init(void)
  1118. {
  1119. int result;
  1120. acpi_status status;
  1121. acpi_os_initialize1();
  1122. status = acpi_load_tables();
  1123. if (ACPI_FAILURE(status)) {
  1124. pr_err("Unable to load the System Description Tables\n");
  1125. goto error1;
  1126. }
  1127. /*
  1128. * ACPI 2.0 requires the EC driver to be loaded and work before the EC
  1129. * device is found in the namespace.
  1130. *
  1131. * This is accomplished by looking for the ECDT table and getting the EC
  1132. * parameters out of that.
  1133. *
  1134. * Do that before calling acpi_initialize_objects() which may trigger EC
  1135. * address space accesses.
  1136. */
  1137. acpi_ec_ecdt_probe();
  1138. status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
  1139. if (ACPI_FAILURE(status)) {
  1140. pr_err("Unable to start the ACPI Interpreter\n");
  1141. goto error1;
  1142. }
  1143. status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
  1144. if (ACPI_FAILURE(status)) {
  1145. pr_err("Unable to initialize ACPI objects\n");
  1146. goto error1;
  1147. }
  1148. /*
  1149. * _OSC method may exist in module level code,
  1150. * so it must be run after ACPI_FULL_INITIALIZATION
  1151. */
  1152. acpi_bus_osc_negotiate_platform_control();
  1153. acpi_bus_osc_negotiate_usb_control();
  1154. /*
  1155. * _PDC control method may load dynamic SSDT tables,
  1156. * and we need to install the table handler before that.
  1157. */
  1158. status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
  1159. acpi_sysfs_init();
  1160. acpi_early_processor_control_setup();
  1161. /*
  1162. * Maybe EC region is required at bus_scan/acpi_get_devices. So it
  1163. * is necessary to enable it as early as possible.
  1164. */
  1165. acpi_ec_dsdt_probe();
  1166. pr_info("Interpreter enabled\n");
  1167. /* Initialize sleep structures */
  1168. acpi_sleep_init();
  1169. /*
  1170. * Get the system interrupt model and evaluate \_PIC.
  1171. */
  1172. result = acpi_bus_init_irq();
  1173. if (result)
  1174. goto error1;
  1175. /*
  1176. * Register the for all standard device notifications.
  1177. */
  1178. status =
  1179. acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
  1180. &acpi_bus_notify, NULL);
  1181. if (ACPI_FAILURE(status)) {
  1182. pr_err("Unable to register for system notifications\n");
  1183. goto error1;
  1184. }
  1185. /*
  1186. * Create the top ACPI proc directory
  1187. */
  1188. acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
  1189. result = bus_register(&acpi_bus_type);
  1190. if (!result)
  1191. return 0;
  1192. /* Mimic structured exception handling */
  1193. error1:
  1194. acpi_terminate();
  1195. return -ENODEV;
  1196. }
  1197. struct kobject *acpi_kobj;
  1198. EXPORT_SYMBOL_GPL(acpi_kobj);
  1199. static int __init acpi_init(void)
  1200. {
  1201. int result;
  1202. if (acpi_disabled) {
  1203. pr_info("Interpreter disabled.\n");
  1204. return -ENODEV;
  1205. }
  1206. acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
  1207. if (!acpi_kobj) {
  1208. pr_err("Failed to register kobject\n");
  1209. return -ENOMEM;
  1210. }
  1211. init_prmt();
  1212. acpi_init_pcc();
  1213. result = acpi_bus_init();
  1214. if (result) {
  1215. kobject_put(acpi_kobj);
  1216. disable_acpi();
  1217. return result;
  1218. }
  1219. acpi_init_ffh();
  1220. pci_mmcfg_late_init();
  1221. acpi_viot_early_init();
  1222. acpi_hest_init();
  1223. acpi_ghes_init();
  1224. acpi_arm_init();
  1225. acpi_riscv_init();
  1226. acpi_scan_init();
  1227. acpi_ec_init();
  1228. acpi_debugfs_init();
  1229. acpi_sleep_proc_init();
  1230. acpi_wakeup_device_init();
  1231. acpi_debugger_init();
  1232. acpi_setup_sb_notify_handler();
  1233. acpi_viot_init();
  1234. return 0;
  1235. }
  1236. subsys_initcall(acpi_init);