bus.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
  3. *
  4. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/kernel.h>
  24. #include <linux/list.h>
  25. #include <linux/sched.h>
  26. #include <linux/pm.h>
  27. #include <linux/device.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/acpi.h>
  30. #include <linux/slab.h>
  31. #include <linux/regulator/machine.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/reboot.h>
  34. #include <linux/delay.h>
  35. #ifdef CONFIG_X86
  36. #include <asm/mpspec.h>
  37. #endif
  38. #include <linux/acpi_iort.h>
  39. #include <linux/pci.h>
  40. #include <acpi/apei.h>
  41. #include <linux/dmi.h>
  42. #include <linux/suspend.h>
  43. #include "internal.h"
  44. #define _COMPONENT ACPI_BUS_COMPONENT
  45. ACPI_MODULE_NAME("bus");
  46. struct acpi_device *acpi_root;
  47. struct proc_dir_entry *acpi_root_dir;
  48. EXPORT_SYMBOL(acpi_root_dir);
  49. #ifdef CONFIG_X86
  50. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  51. static inline int set_copy_dsdt(const struct dmi_system_id *id)
  52. {
  53. return 0;
  54. }
  55. #else
  56. static int set_copy_dsdt(const struct dmi_system_id *id)
  57. {
  58. printk(KERN_NOTICE "%s detected - "
  59. "force copy of DSDT to local memory\n", id->ident);
  60. acpi_gbl_copy_dsdt_locally = 1;
  61. return 0;
  62. }
  63. #endif
  64. static struct dmi_system_id dsdt_dmi_table[] __initdata = {
  65. /*
  66. * Invoke DSDT corruption work-around on all Toshiba Satellite.
  67. * https://bugzilla.kernel.org/show_bug.cgi?id=14679
  68. */
  69. {
  70. .callback = set_copy_dsdt,
  71. .ident = "TOSHIBA Satellite",
  72. .matches = {
  73. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  74. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
  75. },
  76. },
  77. {}
  78. };
  79. #else
  80. static struct dmi_system_id dsdt_dmi_table[] __initdata = {
  81. {}
  82. };
  83. #endif
  84. /* --------------------------------------------------------------------------
  85. Device Management
  86. -------------------------------------------------------------------------- */
  87. acpi_status acpi_bus_get_status_handle(acpi_handle handle,
  88. unsigned long long *sta)
  89. {
  90. acpi_status status;
  91. status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
  92. if (ACPI_SUCCESS(status))
  93. return AE_OK;
  94. if (status == AE_NOT_FOUND) {
  95. *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
  96. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
  97. return AE_OK;
  98. }
  99. return status;
  100. }
  101. int acpi_bus_get_status(struct acpi_device *device)
  102. {
  103. acpi_status status;
  104. unsigned long long sta;
  105. status = acpi_bus_get_status_handle(device->handle, &sta);
  106. if (ACPI_FAILURE(status))
  107. return -ENODEV;
  108. acpi_set_device_status(device, sta);
  109. if (device->status.functional && !device->status.present) {
  110. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]: "
  111. "functional but not present;\n",
  112. device->pnp.bus_id, (u32)sta));
  113. }
  114. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
  115. 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_str_to_uuid(char *str, u8 *uuid)
  166. {
  167. int i;
  168. static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21,
  169. 24, 26, 28, 30, 32, 34};
  170. if (strlen(str) != 36)
  171. return AE_BAD_PARAMETER;
  172. for (i = 0; i < 36; i++) {
  173. if (i == 8 || i == 13 || i == 18 || i == 23) {
  174. if (str[i] != '-')
  175. return AE_BAD_PARAMETER;
  176. } else if (!isxdigit(str[i]))
  177. return AE_BAD_PARAMETER;
  178. }
  179. for (i = 0; i < 16; i++) {
  180. uuid[i] = hex_to_bin(str[opc_map_to_uuid[i]]) << 4;
  181. uuid[i] |= hex_to_bin(str[opc_map_to_uuid[i] + 1]);
  182. }
  183. return AE_OK;
  184. }
  185. EXPORT_SYMBOL_GPL(acpi_str_to_uuid);
  186. acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
  187. {
  188. acpi_status status;
  189. struct acpi_object_list input;
  190. union acpi_object in_params[4];
  191. union acpi_object *out_obj;
  192. u8 uuid[16];
  193. u32 errors;
  194. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  195. if (!context)
  196. return AE_ERROR;
  197. if (ACPI_FAILURE(acpi_str_to_uuid(context->uuid_str, uuid)))
  198. return AE_ERROR;
  199. context->ret.length = ACPI_ALLOCATE_BUFFER;
  200. context->ret.pointer = NULL;
  201. /* Setting up input parameters */
  202. input.count = 4;
  203. input.pointer = in_params;
  204. in_params[0].type = ACPI_TYPE_BUFFER;
  205. in_params[0].buffer.length = 16;
  206. in_params[0].buffer.pointer = uuid;
  207. in_params[1].type = ACPI_TYPE_INTEGER;
  208. in_params[1].integer.value = context->rev;
  209. in_params[2].type = ACPI_TYPE_INTEGER;
  210. in_params[2].integer.value = context->cap.length/sizeof(u32);
  211. in_params[3].type = ACPI_TYPE_BUFFER;
  212. in_params[3].buffer.length = context->cap.length;
  213. in_params[3].buffer.pointer = context->cap.pointer;
  214. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  215. if (ACPI_FAILURE(status))
  216. return status;
  217. if (!output.length)
  218. return AE_NULL_OBJECT;
  219. out_obj = output.pointer;
  220. if (out_obj->type != ACPI_TYPE_BUFFER
  221. || out_obj->buffer.length != context->cap.length) {
  222. acpi_print_osc_error(handle, context,
  223. "_OSC evaluation returned wrong type");
  224. status = AE_TYPE;
  225. goto out_kfree;
  226. }
  227. /* Need to ignore the bit0 in result code */
  228. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  229. if (errors) {
  230. if (errors & OSC_REQUEST_ERROR)
  231. acpi_print_osc_error(handle, context,
  232. "_OSC request failed");
  233. if (errors & OSC_INVALID_UUID_ERROR)
  234. acpi_print_osc_error(handle, context,
  235. "_OSC invalid UUID");
  236. if (errors & OSC_INVALID_REVISION_ERROR)
  237. acpi_print_osc_error(handle, context,
  238. "_OSC invalid revision");
  239. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  240. if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD]
  241. & OSC_QUERY_ENABLE)
  242. goto out_success;
  243. status = AE_SUPPORT;
  244. goto out_kfree;
  245. }
  246. status = AE_ERROR;
  247. goto out_kfree;
  248. }
  249. out_success:
  250. context->ret.length = out_obj->buffer.length;
  251. context->ret.pointer = kmemdup(out_obj->buffer.pointer,
  252. context->ret.length, GFP_KERNEL);
  253. if (!context->ret.pointer) {
  254. status = AE_NO_MEMORY;
  255. goto out_kfree;
  256. }
  257. status = AE_OK;
  258. out_kfree:
  259. kfree(output.pointer);
  260. if (status != AE_OK)
  261. context->ret.pointer = NULL;
  262. return status;
  263. }
  264. EXPORT_SYMBOL(acpi_run_osc);
  265. bool osc_sb_apei_support_acked;
  266. /*
  267. * ACPI 6.0 Section 8.4.4.2 Idle State Coordination
  268. * OSPM supports platform coordinated low power idle(LPI) states
  269. */
  270. bool osc_pc_lpi_support_confirmed;
  271. EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed);
  272. static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
  273. static void acpi_bus_osc_support(void)
  274. {
  275. u32 capbuf[2];
  276. struct acpi_osc_context context = {
  277. .uuid_str = sb_uuid_str,
  278. .rev = 1,
  279. .cap.length = 8,
  280. .cap.pointer = capbuf,
  281. };
  282. acpi_handle handle;
  283. capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
  284. capbuf[OSC_SUPPORT_DWORD] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
  285. if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
  286. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PAD_SUPPORT;
  287. if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
  288. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PPC_OST_SUPPORT;
  289. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT;
  290. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PCLPI_SUPPORT;
  291. if (!ghes_disable)
  292. capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_APEI_SUPPORT;
  293. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  294. return;
  295. if (ACPI_SUCCESS(acpi_run_osc(handle, &context))) {
  296. u32 *capbuf_ret = context.ret.pointer;
  297. if (context.ret.length > OSC_SUPPORT_DWORD) {
  298. osc_sb_apei_support_acked =
  299. capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT;
  300. osc_pc_lpi_support_confirmed =
  301. capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_PCLPI_SUPPORT;
  302. }
  303. kfree(context.ret.pointer);
  304. }
  305. /* do we need to check other returned cap? Sounds no */
  306. }
  307. /* --------------------------------------------------------------------------
  308. Notification Handling
  309. -------------------------------------------------------------------------- */
  310. /**
  311. * acpi_bus_notify
  312. * ---------------
  313. * Callback for all 'system-level' device notifications (values 0x00-0x7F).
  314. */
  315. static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
  316. {
  317. struct acpi_device *adev;
  318. struct acpi_driver *driver;
  319. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  320. bool hotplug_event = false;
  321. switch (type) {
  322. case ACPI_NOTIFY_BUS_CHECK:
  323. acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
  324. hotplug_event = true;
  325. break;
  326. case ACPI_NOTIFY_DEVICE_CHECK:
  327. acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
  328. hotplug_event = true;
  329. break;
  330. case ACPI_NOTIFY_DEVICE_WAKE:
  331. acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
  332. break;
  333. case ACPI_NOTIFY_EJECT_REQUEST:
  334. acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
  335. hotplug_event = true;
  336. break;
  337. case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
  338. acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
  339. /* TBD: Exactly what does 'light' mean? */
  340. break;
  341. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  342. acpi_handle_err(handle, "Device cannot be configured due "
  343. "to a frequency mismatch\n");
  344. break;
  345. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  346. acpi_handle_err(handle, "Device cannot be configured due "
  347. "to a bus mode mismatch\n");
  348. break;
  349. case ACPI_NOTIFY_POWER_FAULT:
  350. acpi_handle_err(handle, "Device has suffered a power fault\n");
  351. break;
  352. default:
  353. acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
  354. break;
  355. }
  356. adev = acpi_bus_get_acpi_device(handle);
  357. if (!adev)
  358. goto err;
  359. driver = adev->driver;
  360. if (driver && driver->ops.notify &&
  361. (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
  362. driver->ops.notify(adev, type);
  363. if (hotplug_event && ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
  364. return;
  365. acpi_bus_put_acpi_device(adev);
  366. return;
  367. err:
  368. acpi_evaluate_ost(handle, type, ost_code, NULL);
  369. }
  370. static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
  371. {
  372. struct acpi_device *device = data;
  373. device->driver->ops.notify(device, event);
  374. }
  375. static void acpi_device_notify_fixed(void *data)
  376. {
  377. struct acpi_device *device = data;
  378. /* Fixed hardware devices have no handles */
  379. acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
  380. }
  381. static u32 acpi_device_fixed_event(void *data)
  382. {
  383. acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
  384. return ACPI_INTERRUPT_HANDLED;
  385. }
  386. static int acpi_device_install_notify_handler(struct acpi_device *device)
  387. {
  388. acpi_status status;
  389. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  390. status =
  391. acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  392. acpi_device_fixed_event,
  393. device);
  394. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  395. status =
  396. acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  397. acpi_device_fixed_event,
  398. device);
  399. else
  400. status = acpi_install_notify_handler(device->handle,
  401. ACPI_DEVICE_NOTIFY,
  402. acpi_device_notify,
  403. device);
  404. if (ACPI_FAILURE(status))
  405. return -EINVAL;
  406. return 0;
  407. }
  408. static void acpi_device_remove_notify_handler(struct acpi_device *device)
  409. {
  410. if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
  411. acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  412. acpi_device_fixed_event);
  413. else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
  414. acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  415. acpi_device_fixed_event);
  416. else
  417. acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
  418. acpi_device_notify);
  419. }
  420. /* Handle events targeting \_SB device (at present only graceful shutdown) */
  421. #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
  422. #define ACPI_SB_INDICATE_INTERVAL 10000
  423. static void sb_notify_work(struct work_struct *dummy)
  424. {
  425. acpi_handle sb_handle;
  426. orderly_poweroff(true);
  427. /*
  428. * After initiating graceful shutdown, the ACPI spec requires OSPM
  429. * to evaluate _OST method once every 10seconds to indicate that
  430. * the shutdown is in progress
  431. */
  432. acpi_get_handle(NULL, "\\_SB", &sb_handle);
  433. while (1) {
  434. pr_info("Graceful shutdown in progress.\n");
  435. acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
  436. ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
  437. msleep(ACPI_SB_INDICATE_INTERVAL);
  438. }
  439. }
  440. static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
  441. {
  442. static DECLARE_WORK(acpi_sb_work, sb_notify_work);
  443. if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
  444. if (!work_busy(&acpi_sb_work))
  445. schedule_work(&acpi_sb_work);
  446. } else
  447. pr_warn("event %x is not supported by \\_SB device\n", event);
  448. }
  449. static int __init acpi_setup_sb_notify_handler(void)
  450. {
  451. acpi_handle sb_handle;
  452. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
  453. return -ENXIO;
  454. if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
  455. acpi_sb_notify, NULL)))
  456. return -EINVAL;
  457. return 0;
  458. }
  459. /* --------------------------------------------------------------------------
  460. Device Matching
  461. -------------------------------------------------------------------------- */
  462. /**
  463. * acpi_get_first_physical_node - Get first physical node of an ACPI device
  464. * @adev: ACPI device in question
  465. *
  466. * Return: First physical node of ACPI device @adev
  467. */
  468. struct device *acpi_get_first_physical_node(struct acpi_device *adev)
  469. {
  470. struct mutex *physical_node_lock = &adev->physical_node_lock;
  471. struct device *phys_dev;
  472. mutex_lock(physical_node_lock);
  473. if (list_empty(&adev->physical_node_list)) {
  474. phys_dev = NULL;
  475. } else {
  476. const struct acpi_device_physical_node *node;
  477. node = list_first_entry(&adev->physical_node_list,
  478. struct acpi_device_physical_node, node);
  479. phys_dev = node->dev;
  480. }
  481. mutex_unlock(physical_node_lock);
  482. return phys_dev;
  483. }
  484. static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
  485. const struct device *dev)
  486. {
  487. const struct device *phys_dev = acpi_get_first_physical_node(adev);
  488. return phys_dev && phys_dev == dev ? adev : NULL;
  489. }
  490. /**
  491. * acpi_device_is_first_physical_node - Is given dev first physical node
  492. * @adev: ACPI companion device
  493. * @dev: Physical device to check
  494. *
  495. * Function checks if given @dev is the first physical devices attached to
  496. * the ACPI companion device. This distinction is needed in some cases
  497. * where the same companion device is shared between many physical devices.
  498. *
  499. * Note that the caller have to provide valid @adev pointer.
  500. */
  501. bool acpi_device_is_first_physical_node(struct acpi_device *adev,
  502. const struct device *dev)
  503. {
  504. return !!acpi_primary_dev_companion(adev, dev);
  505. }
  506. /*
  507. * acpi_companion_match() - Can we match via ACPI companion device
  508. * @dev: Device in question
  509. *
  510. * Check if the given device has an ACPI companion and if that companion has
  511. * a valid list of PNP IDs, and if the device is the first (primary) physical
  512. * device associated with it. Return the companion pointer if that's the case
  513. * or NULL otherwise.
  514. *
  515. * If multiple physical devices are attached to a single ACPI companion, we need
  516. * to be careful. The usage scenario for this kind of relationship is that all
  517. * of the physical devices in question use resources provided by the ACPI
  518. * companion. A typical case is an MFD device where all the sub-devices share
  519. * the parent's ACPI companion. In such cases we can only allow the primary
  520. * (first) physical device to be matched with the help of the companion's PNP
  521. * IDs.
  522. *
  523. * Additional physical devices sharing the ACPI companion can still use
  524. * resources available from it but they will be matched normally using functions
  525. * provided by their bus types (and analogously for their modalias).
  526. */
  527. struct acpi_device *acpi_companion_match(const struct device *dev)
  528. {
  529. struct acpi_device *adev;
  530. adev = ACPI_COMPANION(dev);
  531. if (!adev)
  532. return NULL;
  533. if (list_empty(&adev->pnp.ids))
  534. return NULL;
  535. return acpi_primary_dev_companion(adev, dev);
  536. }
  537. /**
  538. * acpi_of_match_device - Match device object using the "compatible" property.
  539. * @adev: ACPI device object to match.
  540. * @of_match_table: List of device IDs to match against.
  541. *
  542. * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
  543. * identifiers and a _DSD object with the "compatible" property, use that
  544. * property to match against the given list of identifiers.
  545. */
  546. static bool acpi_of_match_device(struct acpi_device *adev,
  547. const struct of_device_id *of_match_table)
  548. {
  549. const union acpi_object *of_compatible, *obj;
  550. int i, nval;
  551. if (!adev)
  552. return false;
  553. of_compatible = adev->data.of_compatible;
  554. if (!of_match_table || !of_compatible)
  555. return false;
  556. if (of_compatible->type == ACPI_TYPE_PACKAGE) {
  557. nval = of_compatible->package.count;
  558. obj = of_compatible->package.elements;
  559. } else { /* Must be ACPI_TYPE_STRING. */
  560. nval = 1;
  561. obj = of_compatible;
  562. }
  563. /* Now we can look for the driver DT compatible strings */
  564. for (i = 0; i < nval; i++, obj++) {
  565. const struct of_device_id *id;
  566. for (id = of_match_table; id->compatible[0]; id++)
  567. if (!strcasecmp(obj->string.pointer, id->compatible))
  568. return true;
  569. }
  570. return false;
  571. }
  572. static bool __acpi_match_device_cls(const struct acpi_device_id *id,
  573. struct acpi_hardware_id *hwid)
  574. {
  575. int i, msk, byte_shift;
  576. char buf[3];
  577. if (!id->cls)
  578. return false;
  579. /* Apply class-code bitmask, before checking each class-code byte */
  580. for (i = 1; i <= 3; i++) {
  581. byte_shift = 8 * (3 - i);
  582. msk = (id->cls_msk >> byte_shift) & 0xFF;
  583. if (!msk)
  584. continue;
  585. sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
  586. if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
  587. return false;
  588. }
  589. return true;
  590. }
  591. static const struct acpi_device_id *__acpi_match_device(
  592. struct acpi_device *device,
  593. const struct acpi_device_id *ids,
  594. const struct of_device_id *of_ids)
  595. {
  596. const struct acpi_device_id *id;
  597. struct acpi_hardware_id *hwid;
  598. /*
  599. * If the device is not present, it is unnecessary to load device
  600. * driver for it.
  601. */
  602. if (!device || !device->status.present)
  603. return NULL;
  604. list_for_each_entry(hwid, &device->pnp.ids, list) {
  605. /* First, check the ACPI/PNP IDs provided by the caller. */
  606. for (id = ids; id->id[0] || id->cls; id++) {
  607. if (id->id[0] && !strcmp((char *) id->id, hwid->id))
  608. return id;
  609. else if (id->cls && __acpi_match_device_cls(id, hwid))
  610. return id;
  611. }
  612. /*
  613. * Next, check ACPI_DT_NAMESPACE_HID and try to match the
  614. * "compatible" property if found.
  615. *
  616. * The id returned by the below is not valid, but the only
  617. * caller passing non-NULL of_ids here is only interested in
  618. * whether or not the return value is NULL.
  619. */
  620. if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id)
  621. && acpi_of_match_device(device, of_ids))
  622. return id;
  623. }
  624. return NULL;
  625. }
  626. /**
  627. * acpi_match_device - Match a struct device against a given list of ACPI IDs
  628. * @ids: Array of struct acpi_device_id object to match against.
  629. * @dev: The device structure to match.
  630. *
  631. * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
  632. * object for that handle and use that object to match against a given list of
  633. * device IDs.
  634. *
  635. * Return a pointer to the first matching ID on success or %NULL on failure.
  636. */
  637. const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
  638. const struct device *dev)
  639. {
  640. return __acpi_match_device(acpi_companion_match(dev), ids, NULL);
  641. }
  642. EXPORT_SYMBOL_GPL(acpi_match_device);
  643. int acpi_match_device_ids(struct acpi_device *device,
  644. const struct acpi_device_id *ids)
  645. {
  646. return __acpi_match_device(device, ids, NULL) ? 0 : -ENOENT;
  647. }
  648. EXPORT_SYMBOL(acpi_match_device_ids);
  649. bool acpi_driver_match_device(struct device *dev,
  650. const struct device_driver *drv)
  651. {
  652. if (!drv->acpi_match_table)
  653. return acpi_of_match_device(ACPI_COMPANION(dev),
  654. drv->of_match_table);
  655. return !!__acpi_match_device(acpi_companion_match(dev),
  656. drv->acpi_match_table, drv->of_match_table);
  657. }
  658. EXPORT_SYMBOL_GPL(acpi_driver_match_device);
  659. /* --------------------------------------------------------------------------
  660. ACPI Driver Management
  661. -------------------------------------------------------------------------- */
  662. /**
  663. * acpi_bus_register_driver - register a driver with the ACPI bus
  664. * @driver: driver being registered
  665. *
  666. * Registers a driver with the ACPI bus. Searches the namespace for all
  667. * devices that match the driver's criteria and binds. Returns zero for
  668. * success or a negative error status for failure.
  669. */
  670. int acpi_bus_register_driver(struct acpi_driver *driver)
  671. {
  672. int ret;
  673. if (acpi_disabled)
  674. return -ENODEV;
  675. driver->drv.name = driver->name;
  676. driver->drv.bus = &acpi_bus_type;
  677. driver->drv.owner = driver->owner;
  678. ret = driver_register(&driver->drv);
  679. return ret;
  680. }
  681. EXPORT_SYMBOL(acpi_bus_register_driver);
  682. /**
  683. * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
  684. * @driver: driver to unregister
  685. *
  686. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  687. * devices that match the driver's criteria and unbinds.
  688. */
  689. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  690. {
  691. driver_unregister(&driver->drv);
  692. }
  693. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  694. /* --------------------------------------------------------------------------
  695. ACPI Bus operations
  696. -------------------------------------------------------------------------- */
  697. static int acpi_bus_match(struct device *dev, struct device_driver *drv)
  698. {
  699. struct acpi_device *acpi_dev = to_acpi_device(dev);
  700. struct acpi_driver *acpi_drv = to_acpi_driver(drv);
  701. return acpi_dev->flags.match_driver
  702. && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
  703. }
  704. static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  705. {
  706. return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
  707. }
  708. static int acpi_device_probe(struct device *dev)
  709. {
  710. struct acpi_device *acpi_dev = to_acpi_device(dev);
  711. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  712. int ret;
  713. if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
  714. return -EINVAL;
  715. if (!acpi_drv->ops.add)
  716. return -ENOSYS;
  717. ret = acpi_drv->ops.add(acpi_dev);
  718. if (ret)
  719. return ret;
  720. acpi_dev->driver = acpi_drv;
  721. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  722. "Driver [%s] successfully bound to device [%s]\n",
  723. acpi_drv->name, acpi_dev->pnp.bus_id));
  724. if (acpi_drv->ops.notify) {
  725. ret = acpi_device_install_notify_handler(acpi_dev);
  726. if (ret) {
  727. if (acpi_drv->ops.remove)
  728. acpi_drv->ops.remove(acpi_dev);
  729. acpi_dev->driver = NULL;
  730. acpi_dev->driver_data = NULL;
  731. return ret;
  732. }
  733. }
  734. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
  735. acpi_drv->name, acpi_dev->pnp.bus_id));
  736. get_device(dev);
  737. return 0;
  738. }
  739. static int acpi_device_remove(struct device * dev)
  740. {
  741. struct acpi_device *acpi_dev = to_acpi_device(dev);
  742. struct acpi_driver *acpi_drv = acpi_dev->driver;
  743. if (acpi_drv) {
  744. if (acpi_drv->ops.notify)
  745. acpi_device_remove_notify_handler(acpi_dev);
  746. if (acpi_drv->ops.remove)
  747. acpi_drv->ops.remove(acpi_dev);
  748. }
  749. acpi_dev->driver = NULL;
  750. acpi_dev->driver_data = NULL;
  751. put_device(dev);
  752. return 0;
  753. }
  754. struct bus_type acpi_bus_type = {
  755. .name = "acpi",
  756. .match = acpi_bus_match,
  757. .probe = acpi_device_probe,
  758. .remove = acpi_device_remove,
  759. .uevent = acpi_device_uevent,
  760. };
  761. /* --------------------------------------------------------------------------
  762. Initialization/Cleanup
  763. -------------------------------------------------------------------------- */
  764. static int __init acpi_bus_init_irq(void)
  765. {
  766. acpi_status status;
  767. char *message = NULL;
  768. /*
  769. * Let the system know what interrupt model we are using by
  770. * evaluating the \_PIC object, if exists.
  771. */
  772. switch (acpi_irq_model) {
  773. case ACPI_IRQ_MODEL_PIC:
  774. message = "PIC";
  775. break;
  776. case ACPI_IRQ_MODEL_IOAPIC:
  777. message = "IOAPIC";
  778. break;
  779. case ACPI_IRQ_MODEL_IOSAPIC:
  780. message = "IOSAPIC";
  781. break;
  782. case ACPI_IRQ_MODEL_GIC:
  783. message = "GIC";
  784. break;
  785. case ACPI_IRQ_MODEL_PLATFORM:
  786. message = "platform specific model";
  787. break;
  788. default:
  789. printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
  790. return -ENODEV;
  791. }
  792. printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
  793. status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
  794. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  795. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
  796. return -ENODEV;
  797. }
  798. return 0;
  799. }
  800. /**
  801. * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
  802. *
  803. * The ACPI tables are accessible after this, but the handling of events has not
  804. * been initialized and the global lock is not available yet, so AML should not
  805. * be executed at this point.
  806. *
  807. * Doing this before switching the EFI runtime services to virtual mode allows
  808. * the EfiBootServices memory to be freed slightly earlier on boot.
  809. */
  810. void __init acpi_early_init(void)
  811. {
  812. acpi_status status;
  813. if (acpi_disabled)
  814. return;
  815. printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
  816. /* It's safe to verify table checksums during late stage */
  817. acpi_gbl_verify_table_checksum = TRUE;
  818. /* enable workarounds, unless strict ACPI spec. compliance */
  819. if (!acpi_strict)
  820. acpi_gbl_enable_interpreter_slack = TRUE;
  821. acpi_gbl_permanent_mmap = 1;
  822. /*
  823. * If the machine falls into the DMI check table,
  824. * DSDT will be copied to memory
  825. */
  826. dmi_check_system(dsdt_dmi_table);
  827. status = acpi_reallocate_root_table();
  828. if (ACPI_FAILURE(status)) {
  829. printk(KERN_ERR PREFIX
  830. "Unable to reallocate ACPI tables\n");
  831. goto error0;
  832. }
  833. status = acpi_initialize_subsystem();
  834. if (ACPI_FAILURE(status)) {
  835. printk(KERN_ERR PREFIX
  836. "Unable to initialize the ACPI Interpreter\n");
  837. goto error0;
  838. }
  839. if (!acpi_gbl_parse_table_as_term_list &&
  840. acpi_gbl_group_module_level_code) {
  841. status = acpi_load_tables();
  842. if (ACPI_FAILURE(status)) {
  843. printk(KERN_ERR PREFIX
  844. "Unable to load the System Description Tables\n");
  845. goto error0;
  846. }
  847. }
  848. #ifdef CONFIG_X86
  849. if (!acpi_ioapic) {
  850. /* compatible (0) means level (3) */
  851. if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
  852. acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
  853. acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
  854. }
  855. /* Set PIC-mode SCI trigger type */
  856. acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
  857. (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
  858. } else {
  859. /*
  860. * now that acpi_gbl_FADT is initialized,
  861. * update it with result from INT_SRC_OVR parsing
  862. */
  863. acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
  864. }
  865. #endif
  866. return;
  867. error0:
  868. disable_acpi();
  869. }
  870. /**
  871. * acpi_subsystem_init - Finalize the early initialization of ACPI.
  872. *
  873. * Switch over the platform to the ACPI mode (if possible).
  874. *
  875. * Doing this too early is generally unsafe, but at the same time it needs to be
  876. * done before all things that really depend on ACPI. The right spot appears to
  877. * be before finalizing the EFI initialization.
  878. */
  879. void __init acpi_subsystem_init(void)
  880. {
  881. acpi_status status;
  882. if (acpi_disabled)
  883. return;
  884. status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
  885. if (ACPI_FAILURE(status)) {
  886. printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
  887. disable_acpi();
  888. } else {
  889. /*
  890. * If the system is using ACPI then we can be reasonably
  891. * confident that any regulators are managed by the firmware
  892. * so tell the regulator core it has everything it needs to
  893. * know.
  894. */
  895. regulator_has_full_constraints();
  896. }
  897. }
  898. static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
  899. {
  900. acpi_scan_table_handler(event, table, context);
  901. return acpi_sysfs_table_handler(event, table, context);
  902. }
  903. static int __init acpi_bus_init(void)
  904. {
  905. int result;
  906. acpi_status status;
  907. acpi_os_initialize1();
  908. /*
  909. * ACPI 2.0 requires the EC driver to be loaded and work before
  910. * the EC device is found in the namespace (i.e. before
  911. * acpi_load_tables() is called).
  912. *
  913. * This is accomplished by looking for the ECDT table, and getting
  914. * the EC parameters out of that.
  915. */
  916. status = acpi_ec_ecdt_probe();
  917. /* Ignore result. Not having an ECDT is not fatal. */
  918. if (acpi_gbl_parse_table_as_term_list ||
  919. !acpi_gbl_group_module_level_code) {
  920. status = acpi_load_tables();
  921. if (ACPI_FAILURE(status)) {
  922. printk(KERN_ERR PREFIX
  923. "Unable to load the System Description Tables\n");
  924. goto error1;
  925. }
  926. }
  927. status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
  928. if (ACPI_FAILURE(status)) {
  929. printk(KERN_ERR PREFIX
  930. "Unable to start the ACPI Interpreter\n");
  931. goto error1;
  932. }
  933. status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
  934. if (ACPI_FAILURE(status)) {
  935. printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
  936. goto error1;
  937. }
  938. /* Set capability bits for _OSC under processor scope */
  939. acpi_early_processor_osc();
  940. /*
  941. * _OSC method may exist in module level code,
  942. * so it must be run after ACPI_FULL_INITIALIZATION
  943. */
  944. acpi_bus_osc_support();
  945. /*
  946. * _PDC control method may load dynamic SSDT tables,
  947. * and we need to install the table handler before that.
  948. */
  949. status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
  950. acpi_sysfs_init();
  951. acpi_early_processor_set_pdc();
  952. /*
  953. * Maybe EC region is required at bus_scan/acpi_get_devices. So it
  954. * is necessary to enable it as early as possible.
  955. */
  956. acpi_ec_dsdt_probe();
  957. printk(KERN_INFO PREFIX "Interpreter enabled\n");
  958. /* Initialize sleep structures */
  959. acpi_sleep_init();
  960. /*
  961. * Get the system interrupt model and evaluate \_PIC.
  962. */
  963. result = acpi_bus_init_irq();
  964. if (result)
  965. goto error1;
  966. /*
  967. * Register the for all standard device notifications.
  968. */
  969. status =
  970. acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
  971. &acpi_bus_notify, NULL);
  972. if (ACPI_FAILURE(status)) {
  973. printk(KERN_ERR PREFIX
  974. "Unable to register for device notifications\n");
  975. goto error1;
  976. }
  977. /*
  978. * Create the top ACPI proc directory
  979. */
  980. acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
  981. result = bus_register(&acpi_bus_type);
  982. if (!result)
  983. return 0;
  984. /* Mimic structured exception handling */
  985. error1:
  986. acpi_terminate();
  987. return -ENODEV;
  988. }
  989. struct kobject *acpi_kobj;
  990. EXPORT_SYMBOL_GPL(acpi_kobj);
  991. static int __init acpi_init(void)
  992. {
  993. int result;
  994. if (acpi_disabled) {
  995. printk(KERN_INFO PREFIX "Interpreter disabled.\n");
  996. return -ENODEV;
  997. }
  998. acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
  999. if (!acpi_kobj) {
  1000. printk(KERN_WARNING "%s: kset create error\n", __func__);
  1001. acpi_kobj = NULL;
  1002. }
  1003. init_acpi_device_notify();
  1004. result = acpi_bus_init();
  1005. if (result) {
  1006. disable_acpi();
  1007. return result;
  1008. }
  1009. pci_mmcfg_late_init();
  1010. acpi_iort_init();
  1011. acpi_scan_init();
  1012. acpi_ec_init();
  1013. acpi_debugfs_init();
  1014. acpi_sleep_proc_init();
  1015. acpi_wakeup_device_init();
  1016. acpi_debugger_init();
  1017. acpi_setup_sb_notify_handler();
  1018. return 0;
  1019. }
  1020. subsys_initcall(acpi_init);