bus.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
  417. const struct device *dev)
  418. {
  419. struct mutex *physical_node_lock = &adev->physical_node_lock;
  420. mutex_lock(physical_node_lock);
  421. if (list_empty(&adev->physical_node_list)) {
  422. adev = NULL;
  423. } else {
  424. const struct acpi_device_physical_node *node;
  425. node = list_first_entry(&adev->physical_node_list,
  426. struct acpi_device_physical_node, node);
  427. if (node->dev != dev)
  428. adev = NULL;
  429. }
  430. mutex_unlock(physical_node_lock);
  431. return adev;
  432. }
  433. /**
  434. * acpi_device_is_first_physical_node - Is given dev first physical node
  435. * @adev: ACPI companion device
  436. * @dev: Physical device to check
  437. *
  438. * Function checks if given @dev is the first physical devices attached to
  439. * the ACPI companion device. This distinction is needed in some cases
  440. * where the same companion device is shared between many physical devices.
  441. *
  442. * Note that the caller have to provide valid @adev pointer.
  443. */
  444. bool acpi_device_is_first_physical_node(struct acpi_device *adev,
  445. const struct device *dev)
  446. {
  447. return !!acpi_primary_dev_companion(adev, dev);
  448. }
  449. /*
  450. * acpi_companion_match() - Can we match via ACPI companion device
  451. * @dev: Device in question
  452. *
  453. * Check if the given device has an ACPI companion and if that companion has
  454. * a valid list of PNP IDs, and if the device is the first (primary) physical
  455. * device associated with it. Return the companion pointer if that's the case
  456. * or NULL otherwise.
  457. *
  458. * If multiple physical devices are attached to a single ACPI companion, we need
  459. * to be careful. The usage scenario for this kind of relationship is that all
  460. * of the physical devices in question use resources provided by the ACPI
  461. * companion. A typical case is an MFD device where all the sub-devices share
  462. * the parent's ACPI companion. In such cases we can only allow the primary
  463. * (first) physical device to be matched with the help of the companion's PNP
  464. * IDs.
  465. *
  466. * Additional physical devices sharing the ACPI companion can still use
  467. * resources available from it but they will be matched normally using functions
  468. * provided by their bus types (and analogously for their modalias).
  469. */
  470. struct acpi_device *acpi_companion_match(const struct device *dev)
  471. {
  472. struct acpi_device *adev;
  473. adev = ACPI_COMPANION(dev);
  474. if (!adev)
  475. return NULL;
  476. if (list_empty(&adev->pnp.ids))
  477. return NULL;
  478. return acpi_primary_dev_companion(adev, dev);
  479. }
  480. /**
  481. * acpi_of_match_device - Match device object using the "compatible" property.
  482. * @adev: ACPI device object to match.
  483. * @of_match_table: List of device IDs to match against.
  484. *
  485. * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
  486. * identifiers and a _DSD object with the "compatible" property, use that
  487. * property to match against the given list of identifiers.
  488. */
  489. static bool acpi_of_match_device(struct acpi_device *adev,
  490. const struct of_device_id *of_match_table)
  491. {
  492. const union acpi_object *of_compatible, *obj;
  493. int i, nval;
  494. if (!adev)
  495. return false;
  496. of_compatible = adev->data.of_compatible;
  497. if (!of_match_table || !of_compatible)
  498. return false;
  499. if (of_compatible->type == ACPI_TYPE_PACKAGE) {
  500. nval = of_compatible->package.count;
  501. obj = of_compatible->package.elements;
  502. } else { /* Must be ACPI_TYPE_STRING. */
  503. nval = 1;
  504. obj = of_compatible;
  505. }
  506. /* Now we can look for the driver DT compatible strings */
  507. for (i = 0; i < nval; i++, obj++) {
  508. const struct of_device_id *id;
  509. for (id = of_match_table; id->compatible[0]; id++)
  510. if (!strcasecmp(obj->string.pointer, id->compatible))
  511. return true;
  512. }
  513. return false;
  514. }
  515. static bool __acpi_match_device_cls(const struct acpi_device_id *id,
  516. struct acpi_hardware_id *hwid)
  517. {
  518. int i, msk, byte_shift;
  519. char buf[3];
  520. if (!id->cls)
  521. return false;
  522. /* Apply class-code bitmask, before checking each class-code byte */
  523. for (i = 1; i <= 3; i++) {
  524. byte_shift = 8 * (3 - i);
  525. msk = (id->cls_msk >> byte_shift) & 0xFF;
  526. if (!msk)
  527. continue;
  528. sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
  529. if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
  530. return false;
  531. }
  532. return true;
  533. }
  534. static const struct acpi_device_id *__acpi_match_device(
  535. struct acpi_device *device,
  536. const struct acpi_device_id *ids,
  537. const struct of_device_id *of_ids)
  538. {
  539. const struct acpi_device_id *id;
  540. struct acpi_hardware_id *hwid;
  541. /*
  542. * If the device is not present, it is unnecessary to load device
  543. * driver for it.
  544. */
  545. if (!device || !device->status.present)
  546. return NULL;
  547. list_for_each_entry(hwid, &device->pnp.ids, list) {
  548. /* First, check the ACPI/PNP IDs provided by the caller. */
  549. for (id = ids; id->id[0] || id->cls; id++) {
  550. if (id->id[0] && !strcmp((char *) id->id, hwid->id))
  551. return id;
  552. else if (id->cls && __acpi_match_device_cls(id, hwid))
  553. return id;
  554. }
  555. /*
  556. * Next, check ACPI_DT_NAMESPACE_HID and try to match the
  557. * "compatible" property if found.
  558. *
  559. * The id returned by the below is not valid, but the only
  560. * caller passing non-NULL of_ids here is only interested in
  561. * whether or not the return value is NULL.
  562. */
  563. if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id)
  564. && acpi_of_match_device(device, of_ids))
  565. return id;
  566. }
  567. return NULL;
  568. }
  569. /**
  570. * acpi_match_device - Match a struct device against a given list of ACPI IDs
  571. * @ids: Array of struct acpi_device_id object to match against.
  572. * @dev: The device structure to match.
  573. *
  574. * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
  575. * object for that handle and use that object to match against a given list of
  576. * device IDs.
  577. *
  578. * Return a pointer to the first matching ID on success or %NULL on failure.
  579. */
  580. const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
  581. const struct device *dev)
  582. {
  583. return __acpi_match_device(acpi_companion_match(dev), ids, NULL);
  584. }
  585. EXPORT_SYMBOL_GPL(acpi_match_device);
  586. int acpi_match_device_ids(struct acpi_device *device,
  587. const struct acpi_device_id *ids)
  588. {
  589. return __acpi_match_device(device, ids, NULL) ? 0 : -ENOENT;
  590. }
  591. EXPORT_SYMBOL(acpi_match_device_ids);
  592. bool acpi_driver_match_device(struct device *dev,
  593. const struct device_driver *drv)
  594. {
  595. if (!drv->acpi_match_table)
  596. return acpi_of_match_device(ACPI_COMPANION(dev),
  597. drv->of_match_table);
  598. return !!__acpi_match_device(acpi_companion_match(dev),
  599. drv->acpi_match_table, drv->of_match_table);
  600. }
  601. EXPORT_SYMBOL_GPL(acpi_driver_match_device);
  602. /* --------------------------------------------------------------------------
  603. ACPI Driver Management
  604. -------------------------------------------------------------------------- */
  605. /**
  606. * acpi_bus_register_driver - register a driver with the ACPI bus
  607. * @driver: driver being registered
  608. *
  609. * Registers a driver with the ACPI bus. Searches the namespace for all
  610. * devices that match the driver's criteria and binds. Returns zero for
  611. * success or a negative error status for failure.
  612. */
  613. int acpi_bus_register_driver(struct acpi_driver *driver)
  614. {
  615. int ret;
  616. if (acpi_disabled)
  617. return -ENODEV;
  618. driver->drv.name = driver->name;
  619. driver->drv.bus = &acpi_bus_type;
  620. driver->drv.owner = driver->owner;
  621. ret = driver_register(&driver->drv);
  622. return ret;
  623. }
  624. EXPORT_SYMBOL(acpi_bus_register_driver);
  625. /**
  626. * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
  627. * @driver: driver to unregister
  628. *
  629. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  630. * devices that match the driver's criteria and unbinds.
  631. */
  632. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  633. {
  634. driver_unregister(&driver->drv);
  635. }
  636. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  637. /* --------------------------------------------------------------------------
  638. ACPI Bus operations
  639. -------------------------------------------------------------------------- */
  640. static int acpi_bus_match(struct device *dev, struct device_driver *drv)
  641. {
  642. struct acpi_device *acpi_dev = to_acpi_device(dev);
  643. struct acpi_driver *acpi_drv = to_acpi_driver(drv);
  644. return acpi_dev->flags.match_driver
  645. && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
  646. }
  647. static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  648. {
  649. return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
  650. }
  651. static int acpi_device_probe(struct device *dev)
  652. {
  653. struct acpi_device *acpi_dev = to_acpi_device(dev);
  654. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  655. int ret;
  656. if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
  657. return -EINVAL;
  658. if (!acpi_drv->ops.add)
  659. return -ENOSYS;
  660. ret = acpi_drv->ops.add(acpi_dev);
  661. if (ret)
  662. return ret;
  663. acpi_dev->driver = acpi_drv;
  664. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  665. "Driver [%s] successfully bound to device [%s]\n",
  666. acpi_drv->name, acpi_dev->pnp.bus_id));
  667. if (acpi_drv->ops.notify) {
  668. ret = acpi_device_install_notify_handler(acpi_dev);
  669. if (ret) {
  670. if (acpi_drv->ops.remove)
  671. acpi_drv->ops.remove(acpi_dev);
  672. acpi_dev->driver = NULL;
  673. acpi_dev->driver_data = NULL;
  674. return ret;
  675. }
  676. }
  677. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
  678. acpi_drv->name, acpi_dev->pnp.bus_id));
  679. get_device(dev);
  680. return 0;
  681. }
  682. static int acpi_device_remove(struct device * dev)
  683. {
  684. struct acpi_device *acpi_dev = to_acpi_device(dev);
  685. struct acpi_driver *acpi_drv = acpi_dev->driver;
  686. if (acpi_drv) {
  687. if (acpi_drv->ops.notify)
  688. acpi_device_remove_notify_handler(acpi_dev);
  689. if (acpi_drv->ops.remove)
  690. acpi_drv->ops.remove(acpi_dev);
  691. }
  692. acpi_dev->driver = NULL;
  693. acpi_dev->driver_data = NULL;
  694. put_device(dev);
  695. return 0;
  696. }
  697. struct bus_type acpi_bus_type = {
  698. .name = "acpi",
  699. .match = acpi_bus_match,
  700. .probe = acpi_device_probe,
  701. .remove = acpi_device_remove,
  702. .uevent = acpi_device_uevent,
  703. };
  704. /* --------------------------------------------------------------------------
  705. Initialization/Cleanup
  706. -------------------------------------------------------------------------- */
  707. static int __init acpi_bus_init_irq(void)
  708. {
  709. acpi_status status;
  710. char *message = NULL;
  711. /*
  712. * Let the system know what interrupt model we are using by
  713. * evaluating the \_PIC object, if exists.
  714. */
  715. switch (acpi_irq_model) {
  716. case ACPI_IRQ_MODEL_PIC:
  717. message = "PIC";
  718. break;
  719. case ACPI_IRQ_MODEL_IOAPIC:
  720. message = "IOAPIC";
  721. break;
  722. case ACPI_IRQ_MODEL_IOSAPIC:
  723. message = "IOSAPIC";
  724. break;
  725. case ACPI_IRQ_MODEL_GIC:
  726. message = "GIC";
  727. break;
  728. case ACPI_IRQ_MODEL_PLATFORM:
  729. message = "platform specific model";
  730. break;
  731. default:
  732. printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
  733. return -ENODEV;
  734. }
  735. printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
  736. status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
  737. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  738. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
  739. return -ENODEV;
  740. }
  741. return 0;
  742. }
  743. /**
  744. * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
  745. *
  746. * The ACPI tables are accessible after this, but the handling of events has not
  747. * been initialized and the global lock is not available yet, so AML should not
  748. * be executed at this point.
  749. *
  750. * Doing this before switching the EFI runtime services to virtual mode allows
  751. * the EfiBootServices memory to be freed slightly earlier on boot.
  752. */
  753. void __init acpi_early_init(void)
  754. {
  755. acpi_status status;
  756. if (acpi_disabled)
  757. return;
  758. printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
  759. /* It's safe to verify table checksums during late stage */
  760. acpi_gbl_verify_table_checksum = TRUE;
  761. /* enable workarounds, unless strict ACPI spec. compliance */
  762. if (!acpi_strict)
  763. acpi_gbl_enable_interpreter_slack = TRUE;
  764. acpi_gbl_permanent_mmap = 1;
  765. /*
  766. * If the machine falls into the DMI check table,
  767. * DSDT will be copied to memory
  768. */
  769. dmi_check_system(dsdt_dmi_table);
  770. status = acpi_reallocate_root_table();
  771. if (ACPI_FAILURE(status)) {
  772. printk(KERN_ERR PREFIX
  773. "Unable to reallocate ACPI tables\n");
  774. goto error0;
  775. }
  776. status = acpi_initialize_subsystem();
  777. if (ACPI_FAILURE(status)) {
  778. printk(KERN_ERR PREFIX
  779. "Unable to initialize the ACPI Interpreter\n");
  780. goto error0;
  781. }
  782. status = acpi_load_tables();
  783. if (ACPI_FAILURE(status)) {
  784. printk(KERN_ERR PREFIX
  785. "Unable to load the System Description Tables\n");
  786. goto error0;
  787. }
  788. #ifdef CONFIG_X86
  789. if (!acpi_ioapic) {
  790. /* compatible (0) means level (3) */
  791. if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
  792. acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
  793. acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
  794. }
  795. /* Set PIC-mode SCI trigger type */
  796. acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
  797. (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
  798. } else {
  799. /*
  800. * now that acpi_gbl_FADT is initialized,
  801. * update it with result from INT_SRC_OVR parsing
  802. */
  803. acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
  804. }
  805. #endif
  806. return;
  807. error0:
  808. disable_acpi();
  809. }
  810. /**
  811. * acpi_subsystem_init - Finalize the early initialization of ACPI.
  812. *
  813. * Switch over the platform to the ACPI mode (if possible), initialize the
  814. * handling of ACPI events, install the interrupt and global lock handlers.
  815. *
  816. * Doing this too early is generally unsafe, but at the same time it needs to be
  817. * done before all things that really depend on ACPI. The right spot appears to
  818. * be before finalizing the EFI initialization.
  819. */
  820. void __init acpi_subsystem_init(void)
  821. {
  822. acpi_status status;
  823. if (acpi_disabled)
  824. return;
  825. status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
  826. if (ACPI_FAILURE(status)) {
  827. printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
  828. disable_acpi();
  829. } else {
  830. /*
  831. * If the system is using ACPI then we can be reasonably
  832. * confident that any regulators are managed by the firmware
  833. * so tell the regulator core it has everything it needs to
  834. * know.
  835. */
  836. regulator_has_full_constraints();
  837. }
  838. }
  839. static int __init acpi_bus_init(void)
  840. {
  841. int result;
  842. acpi_status status;
  843. acpi_os_initialize1();
  844. status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
  845. if (ACPI_FAILURE(status)) {
  846. printk(KERN_ERR PREFIX
  847. "Unable to start the ACPI Interpreter\n");
  848. goto error1;
  849. }
  850. /*
  851. * ACPI 2.0 requires the EC driver to be loaded and work before
  852. * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
  853. * is called).
  854. *
  855. * This is accomplished by looking for the ECDT table, and getting
  856. * the EC parameters out of that.
  857. */
  858. status = acpi_ec_ecdt_probe();
  859. /* Ignore result. Not having an ECDT is not fatal. */
  860. status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
  861. if (ACPI_FAILURE(status)) {
  862. printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
  863. goto error1;
  864. }
  865. /*
  866. * _OSC method may exist in module level code,
  867. * so it must be run after ACPI_FULL_INITIALIZATION
  868. */
  869. acpi_bus_osc_support();
  870. /*
  871. * _PDC control method may load dynamic SSDT tables,
  872. * and we need to install the table handler before that.
  873. */
  874. acpi_sysfs_init();
  875. acpi_early_processor_set_pdc();
  876. /*
  877. * Maybe EC region is required at bus_scan/acpi_get_devices. So it
  878. * is necessary to enable it as early as possible.
  879. */
  880. acpi_boot_ec_enable();
  881. printk(KERN_INFO PREFIX "Interpreter enabled\n");
  882. /* Initialize sleep structures */
  883. acpi_sleep_init();
  884. /*
  885. * Get the system interrupt model and evaluate \_PIC.
  886. */
  887. result = acpi_bus_init_irq();
  888. if (result)
  889. goto error1;
  890. /*
  891. * Register the for all standard device notifications.
  892. */
  893. status =
  894. acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
  895. &acpi_bus_notify, NULL);
  896. if (ACPI_FAILURE(status)) {
  897. printk(KERN_ERR PREFIX
  898. "Unable to register for device notifications\n");
  899. goto error1;
  900. }
  901. /*
  902. * Create the top ACPI proc directory
  903. */
  904. acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
  905. result = bus_register(&acpi_bus_type);
  906. if (!result)
  907. return 0;
  908. /* Mimic structured exception handling */
  909. error1:
  910. acpi_terminate();
  911. return -ENODEV;
  912. }
  913. struct kobject *acpi_kobj;
  914. EXPORT_SYMBOL_GPL(acpi_kobj);
  915. static int __init acpi_init(void)
  916. {
  917. int result;
  918. if (acpi_disabled) {
  919. printk(KERN_INFO PREFIX "Interpreter disabled.\n");
  920. return -ENODEV;
  921. }
  922. acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
  923. if (!acpi_kobj) {
  924. printk(KERN_WARNING "%s: kset create error\n", __func__);
  925. acpi_kobj = NULL;
  926. }
  927. init_acpi_device_notify();
  928. result = acpi_bus_init();
  929. if (result) {
  930. disable_acpi();
  931. return result;
  932. }
  933. pci_mmcfg_late_init();
  934. acpi_scan_init();
  935. acpi_ec_init();
  936. acpi_debugfs_init();
  937. acpi_sleep_proc_init();
  938. acpi_wakeup_device_init();
  939. return 0;
  940. }
  941. subsys_initcall(acpi_init);