bus.c 29 KB

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