bus.c 30 KB

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