bus.c 33 KB

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