bus.c 32 KB

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