bus.c 41 KB

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