bus.c 32 KB

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