bus.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/ioport.h>
  27. #include <linux/kernel.h>
  28. #include <linux/list.h>
  29. #include <linux/sched.h>
  30. #include <linux/pm.h>
  31. #include <linux/device.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/acpi.h>
  34. #include <linux/slab.h>
  35. #ifdef CONFIG_X86
  36. #include <asm/mpspec.h>
  37. #endif
  38. #include <linux/pci.h>
  39. #include <acpi/acpi_bus.h>
  40. #include <acpi/acpi_drivers.h>
  41. #include <acpi/apei.h>
  42. #include <linux/dmi.h>
  43. #include <linux/suspend.h>
  44. #include "internal.h"
  45. #define _COMPONENT ACPI_BUS_COMPONENT
  46. ACPI_MODULE_NAME("bus");
  47. struct acpi_device *acpi_root;
  48. struct proc_dir_entry *acpi_root_dir;
  49. EXPORT_SYMBOL(acpi_root_dir);
  50. #define STRUCT_TO_INT(s) (*((int*)&s))
  51. #ifdef CONFIG_X86
  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. static struct dmi_system_id dsdt_dmi_table[] __initdata = {
  60. /*
  61. * Invoke DSDT corruption work-around on all Toshiba Satellite.
  62. * https://bugzilla.kernel.org/show_bug.cgi?id=14679
  63. */
  64. {
  65. .callback = set_copy_dsdt,
  66. .ident = "TOSHIBA Satellite",
  67. .matches = {
  68. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  69. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
  70. },
  71. },
  72. {}
  73. };
  74. #else
  75. static struct dmi_system_id dsdt_dmi_table[] __initdata = {
  76. {}
  77. };
  78. #endif
  79. /* --------------------------------------------------------------------------
  80. Device Management
  81. -------------------------------------------------------------------------- */
  82. int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
  83. {
  84. acpi_status status = AE_OK;
  85. if (!device)
  86. return -EINVAL;
  87. /* TBD: Support fixed-feature devices */
  88. status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
  89. if (ACPI_FAILURE(status) || !*device) {
  90. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
  91. handle));
  92. return -ENODEV;
  93. }
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(acpi_bus_get_device);
  97. acpi_status acpi_bus_get_status_handle(acpi_handle handle,
  98. unsigned long long *sta)
  99. {
  100. acpi_status status;
  101. status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
  102. if (ACPI_SUCCESS(status))
  103. return AE_OK;
  104. if (status == AE_NOT_FOUND) {
  105. *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
  106. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
  107. return AE_OK;
  108. }
  109. return status;
  110. }
  111. int acpi_bus_get_status(struct acpi_device *device)
  112. {
  113. acpi_status status;
  114. unsigned long long sta;
  115. status = acpi_bus_get_status_handle(device->handle, &sta);
  116. if (ACPI_FAILURE(status))
  117. return -ENODEV;
  118. STRUCT_TO_INT(device->status) = (int) sta;
  119. if (device->status.functional && !device->status.present) {
  120. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]: "
  121. "functional but not present;\n",
  122. device->pnp.bus_id,
  123. (u32) STRUCT_TO_INT(device->status)));
  124. }
  125. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
  126. device->pnp.bus_id,
  127. (u32) STRUCT_TO_INT(device->status)));
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(acpi_bus_get_status);
  131. void acpi_bus_private_data_handler(acpi_handle handle,
  132. void *context)
  133. {
  134. return;
  135. }
  136. EXPORT_SYMBOL(acpi_bus_private_data_handler);
  137. int acpi_bus_get_private_data(acpi_handle handle, void **data)
  138. {
  139. acpi_status status = AE_OK;
  140. if (!*data)
  141. return -EINVAL;
  142. status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
  143. if (ACPI_FAILURE(status) || !*data) {
  144. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
  145. handle));
  146. return -ENODEV;
  147. }
  148. return 0;
  149. }
  150. EXPORT_SYMBOL(acpi_bus_get_private_data);
  151. /* --------------------------------------------------------------------------
  152. Power Management
  153. -------------------------------------------------------------------------- */
  154. static const char *state_string(int state)
  155. {
  156. switch (state) {
  157. case ACPI_STATE_D0:
  158. return "D0";
  159. case ACPI_STATE_D1:
  160. return "D1";
  161. case ACPI_STATE_D2:
  162. return "D2";
  163. case ACPI_STATE_D3_HOT:
  164. return "D3hot";
  165. case ACPI_STATE_D3_COLD:
  166. return "D3";
  167. default:
  168. return "(unknown)";
  169. }
  170. }
  171. static int __acpi_bus_get_power(struct acpi_device *device, int *state)
  172. {
  173. int result = ACPI_STATE_UNKNOWN;
  174. if (!device || !state)
  175. return -EINVAL;
  176. if (!device->flags.power_manageable) {
  177. /* TBD: Non-recursive algorithm for walking up hierarchy. */
  178. *state = device->parent ?
  179. device->parent->power.state : ACPI_STATE_D0;
  180. goto out;
  181. }
  182. /*
  183. * Get the device's power state either directly (via _PSC) or
  184. * indirectly (via power resources).
  185. */
  186. if (device->power.flags.explicit_get) {
  187. unsigned long long psc;
  188. acpi_status status = acpi_evaluate_integer(device->handle,
  189. "_PSC", NULL, &psc);
  190. if (ACPI_FAILURE(status))
  191. return -ENODEV;
  192. result = psc;
  193. }
  194. /* The test below covers ACPI_STATE_UNKNOWN too. */
  195. if (result <= ACPI_STATE_D2) {
  196. ; /* Do nothing. */
  197. } else if (device->power.flags.power_resources) {
  198. int error = acpi_power_get_inferred_state(device, &result);
  199. if (error)
  200. return error;
  201. } else if (result == ACPI_STATE_D3_HOT) {
  202. result = ACPI_STATE_D3;
  203. }
  204. /*
  205. * If we were unsure about the device parent's power state up to this
  206. * point, the fact that the device is in D0 implies that the parent has
  207. * to be in D0 too.
  208. */
  209. if (device->parent && device->parent->power.state == ACPI_STATE_UNKNOWN
  210. && result == ACPI_STATE_D0)
  211. device->parent->power.state = ACPI_STATE_D0;
  212. *state = result;
  213. out:
  214. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
  215. device->pnp.bus_id, state_string(*state)));
  216. return 0;
  217. }
  218. static int __acpi_bus_set_power(struct acpi_device *device, int state)
  219. {
  220. int result = 0;
  221. acpi_status status = AE_OK;
  222. char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
  223. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  224. return -EINVAL;
  225. /* Make sure this is a valid target state */
  226. if (state == device->power.state) {
  227. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n",
  228. state_string(state)));
  229. return 0;
  230. }
  231. if (!device->power.states[state].flags.valid) {
  232. printk(KERN_WARNING PREFIX "Device does not support %s\n",
  233. state_string(state));
  234. return -ENODEV;
  235. }
  236. if (device->parent && (state < device->parent->power.state)) {
  237. printk(KERN_WARNING PREFIX
  238. "Cannot set device to a higher-powered"
  239. " state than parent\n");
  240. return -ENODEV;
  241. }
  242. /* For D3cold we should execute _PS3, not _PS4. */
  243. if (state == ACPI_STATE_D3_COLD)
  244. object_name[3] = '3';
  245. /*
  246. * Transition Power
  247. * ----------------
  248. * On transitions to a high-powered state we first apply power (via
  249. * power resources) then evalute _PSx. Conversly for transitions to
  250. * a lower-powered state.
  251. */
  252. if (state < device->power.state) {
  253. if (device->power.flags.power_resources) {
  254. result = acpi_power_transition(device, state);
  255. if (result)
  256. goto end;
  257. }
  258. if (device->power.states[state].flags.explicit_set) {
  259. status = acpi_evaluate_object(device->handle,
  260. object_name, NULL, NULL);
  261. if (ACPI_FAILURE(status)) {
  262. result = -ENODEV;
  263. goto end;
  264. }
  265. }
  266. } else {
  267. if (device->power.states[state].flags.explicit_set) {
  268. status = acpi_evaluate_object(device->handle,
  269. object_name, NULL, NULL);
  270. if (ACPI_FAILURE(status)) {
  271. result = -ENODEV;
  272. goto end;
  273. }
  274. }
  275. if (device->power.flags.power_resources) {
  276. result = acpi_power_transition(device, state);
  277. if (result)
  278. goto end;
  279. }
  280. }
  281. end:
  282. if (result)
  283. printk(KERN_WARNING PREFIX
  284. "Device [%s] failed to transition to %s\n",
  285. device->pnp.bus_id, state_string(state));
  286. else {
  287. device->power.state = state;
  288. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  289. "Device [%s] transitioned to %s\n",
  290. device->pnp.bus_id, state_string(state)));
  291. }
  292. return result;
  293. }
  294. int acpi_bus_set_power(acpi_handle handle, int state)
  295. {
  296. struct acpi_device *device;
  297. int result;
  298. result = acpi_bus_get_device(handle, &device);
  299. if (result)
  300. return result;
  301. if (!device->flags.power_manageable) {
  302. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  303. "Device [%s] is not power manageable\n",
  304. dev_name(&device->dev)));
  305. return -ENODEV;
  306. }
  307. return __acpi_bus_set_power(device, state);
  308. }
  309. EXPORT_SYMBOL(acpi_bus_set_power);
  310. int acpi_bus_init_power(struct acpi_device *device)
  311. {
  312. int state;
  313. int result;
  314. if (!device)
  315. return -EINVAL;
  316. device->power.state = ACPI_STATE_UNKNOWN;
  317. result = __acpi_bus_get_power(device, &state);
  318. if (result)
  319. return result;
  320. if (device->power.flags.power_resources)
  321. result = acpi_power_on_resources(device, state);
  322. if (!result)
  323. device->power.state = state;
  324. return result;
  325. }
  326. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  327. {
  328. struct acpi_device *device;
  329. int state;
  330. int result;
  331. result = acpi_bus_get_device(handle, &device);
  332. if (result)
  333. return result;
  334. result = __acpi_bus_get_power(device, &state);
  335. if (result)
  336. return result;
  337. result = __acpi_bus_set_power(device, state);
  338. if (!result && state_p)
  339. *state_p = state;
  340. return result;
  341. }
  342. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  343. bool acpi_bus_power_manageable(acpi_handle handle)
  344. {
  345. struct acpi_device *device;
  346. int result;
  347. result = acpi_bus_get_device(handle, &device);
  348. return result ? false : device->flags.power_manageable;
  349. }
  350. EXPORT_SYMBOL(acpi_bus_power_manageable);
  351. bool acpi_bus_can_wakeup(acpi_handle handle)
  352. {
  353. struct acpi_device *device;
  354. int result;
  355. result = acpi_bus_get_device(handle, &device);
  356. return result ? false : device->wakeup.flags.valid;
  357. }
  358. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  359. static void acpi_print_osc_error(acpi_handle handle,
  360. struct acpi_osc_context *context, char *error)
  361. {
  362. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
  363. int i;
  364. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer)))
  365. printk(KERN_DEBUG "%s\n", error);
  366. else {
  367. printk(KERN_DEBUG "%s:%s\n", (char *)buffer.pointer, error);
  368. kfree(buffer.pointer);
  369. }
  370. printk(KERN_DEBUG"_OSC request data:");
  371. for (i = 0; i < context->cap.length; i += sizeof(u32))
  372. printk("%x ", *((u32 *)(context->cap.pointer + i)));
  373. printk("\n");
  374. }
  375. static acpi_status acpi_str_to_uuid(char *str, u8 *uuid)
  376. {
  377. int i;
  378. static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21,
  379. 24, 26, 28, 30, 32, 34};
  380. if (strlen(str) != 36)
  381. return AE_BAD_PARAMETER;
  382. for (i = 0; i < 36; i++) {
  383. if (i == 8 || i == 13 || i == 18 || i == 23) {
  384. if (str[i] != '-')
  385. return AE_BAD_PARAMETER;
  386. } else if (!isxdigit(str[i]))
  387. return AE_BAD_PARAMETER;
  388. }
  389. for (i = 0; i < 16; i++) {
  390. uuid[i] = hex_to_bin(str[opc_map_to_uuid[i]]) << 4;
  391. uuid[i] |= hex_to_bin(str[opc_map_to_uuid[i] + 1]);
  392. }
  393. return AE_OK;
  394. }
  395. acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
  396. {
  397. acpi_status status;
  398. struct acpi_object_list input;
  399. union acpi_object in_params[4];
  400. union acpi_object *out_obj;
  401. u8 uuid[16];
  402. u32 errors;
  403. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  404. if (!context)
  405. return AE_ERROR;
  406. if (ACPI_FAILURE(acpi_str_to_uuid(context->uuid_str, uuid)))
  407. return AE_ERROR;
  408. context->ret.length = ACPI_ALLOCATE_BUFFER;
  409. context->ret.pointer = NULL;
  410. /* Setting up input parameters */
  411. input.count = 4;
  412. input.pointer = in_params;
  413. in_params[0].type = ACPI_TYPE_BUFFER;
  414. in_params[0].buffer.length = 16;
  415. in_params[0].buffer.pointer = uuid;
  416. in_params[1].type = ACPI_TYPE_INTEGER;
  417. in_params[1].integer.value = context->rev;
  418. in_params[2].type = ACPI_TYPE_INTEGER;
  419. in_params[2].integer.value = context->cap.length/sizeof(u32);
  420. in_params[3].type = ACPI_TYPE_BUFFER;
  421. in_params[3].buffer.length = context->cap.length;
  422. in_params[3].buffer.pointer = context->cap.pointer;
  423. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  424. if (ACPI_FAILURE(status))
  425. return status;
  426. if (!output.length)
  427. return AE_NULL_OBJECT;
  428. out_obj = output.pointer;
  429. if (out_obj->type != ACPI_TYPE_BUFFER
  430. || out_obj->buffer.length != context->cap.length) {
  431. acpi_print_osc_error(handle, context,
  432. "_OSC evaluation returned wrong type");
  433. status = AE_TYPE;
  434. goto out_kfree;
  435. }
  436. /* Need to ignore the bit0 in result code */
  437. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  438. if (errors) {
  439. if (errors & OSC_REQUEST_ERROR)
  440. acpi_print_osc_error(handle, context,
  441. "_OSC request failed");
  442. if (errors & OSC_INVALID_UUID_ERROR)
  443. acpi_print_osc_error(handle, context,
  444. "_OSC invalid UUID");
  445. if (errors & OSC_INVALID_REVISION_ERROR)
  446. acpi_print_osc_error(handle, context,
  447. "_OSC invalid revision");
  448. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  449. if (((u32 *)context->cap.pointer)[OSC_QUERY_TYPE]
  450. & OSC_QUERY_ENABLE)
  451. goto out_success;
  452. status = AE_SUPPORT;
  453. goto out_kfree;
  454. }
  455. status = AE_ERROR;
  456. goto out_kfree;
  457. }
  458. out_success:
  459. context->ret.length = out_obj->buffer.length;
  460. context->ret.pointer = kmalloc(context->ret.length, GFP_KERNEL);
  461. if (!context->ret.pointer) {
  462. status = AE_NO_MEMORY;
  463. goto out_kfree;
  464. }
  465. memcpy(context->ret.pointer, out_obj->buffer.pointer,
  466. context->ret.length);
  467. status = AE_OK;
  468. out_kfree:
  469. kfree(output.pointer);
  470. if (status != AE_OK)
  471. context->ret.pointer = NULL;
  472. return status;
  473. }
  474. EXPORT_SYMBOL(acpi_run_osc);
  475. bool osc_sb_apei_support_acked;
  476. static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
  477. static void acpi_bus_osc_support(void)
  478. {
  479. u32 capbuf[2];
  480. struct acpi_osc_context context = {
  481. .uuid_str = sb_uuid_str,
  482. .rev = 1,
  483. .cap.length = 8,
  484. .cap.pointer = capbuf,
  485. };
  486. acpi_handle handle;
  487. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  488. capbuf[OSC_SUPPORT_TYPE] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
  489. #if defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) ||\
  490. defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
  491. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PAD_SUPPORT;
  492. #endif
  493. #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
  494. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PPC_OST_SUPPORT;
  495. #endif
  496. #ifdef ACPI_HOTPLUG_OST
  497. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_HOTPLUG_OST_SUPPORT;
  498. #endif
  499. if (!ghes_disable)
  500. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_APEI_SUPPORT;
  501. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  502. return;
  503. if (ACPI_SUCCESS(acpi_run_osc(handle, &context))) {
  504. u32 *capbuf_ret = context.ret.pointer;
  505. if (context.ret.length > OSC_SUPPORT_TYPE)
  506. osc_sb_apei_support_acked =
  507. capbuf_ret[OSC_SUPPORT_TYPE] & OSC_SB_APEI_SUPPORT;
  508. kfree(context.ret.pointer);
  509. }
  510. /* do we need to check other returned cap? Sounds no */
  511. }
  512. /* --------------------------------------------------------------------------
  513. Event Management
  514. -------------------------------------------------------------------------- */
  515. #ifdef CONFIG_ACPI_PROC_EVENT
  516. static DEFINE_SPINLOCK(acpi_bus_event_lock);
  517. LIST_HEAD(acpi_bus_event_list);
  518. DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
  519. extern int event_is_open;
  520. int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
  521. {
  522. struct acpi_bus_event *event;
  523. unsigned long flags = 0;
  524. /* drop event on the floor if no one's listening */
  525. if (!event_is_open)
  526. return 0;
  527. event = kzalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
  528. if (!event)
  529. return -ENOMEM;
  530. strcpy(event->device_class, device_class);
  531. strcpy(event->bus_id, bus_id);
  532. event->type = type;
  533. event->data = data;
  534. spin_lock_irqsave(&acpi_bus_event_lock, flags);
  535. list_add_tail(&event->node, &acpi_bus_event_list);
  536. spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
  537. wake_up_interruptible(&acpi_bus_event_queue);
  538. return 0;
  539. }
  540. EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
  541. int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
  542. {
  543. if (!device)
  544. return -EINVAL;
  545. return acpi_bus_generate_proc_event4(device->pnp.device_class,
  546. device->pnp.bus_id, type, data);
  547. }
  548. EXPORT_SYMBOL(acpi_bus_generate_proc_event);
  549. int acpi_bus_receive_event(struct acpi_bus_event *event)
  550. {
  551. unsigned long flags = 0;
  552. struct acpi_bus_event *entry = NULL;
  553. DECLARE_WAITQUEUE(wait, current);
  554. if (!event)
  555. return -EINVAL;
  556. if (list_empty(&acpi_bus_event_list)) {
  557. set_current_state(TASK_INTERRUPTIBLE);
  558. add_wait_queue(&acpi_bus_event_queue, &wait);
  559. if (list_empty(&acpi_bus_event_list))
  560. schedule();
  561. remove_wait_queue(&acpi_bus_event_queue, &wait);
  562. set_current_state(TASK_RUNNING);
  563. if (signal_pending(current))
  564. return -ERESTARTSYS;
  565. }
  566. spin_lock_irqsave(&acpi_bus_event_lock, flags);
  567. if (!list_empty(&acpi_bus_event_list)) {
  568. entry = list_entry(acpi_bus_event_list.next,
  569. struct acpi_bus_event, node);
  570. list_del(&entry->node);
  571. }
  572. spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
  573. if (!entry)
  574. return -ENODEV;
  575. memcpy(event, entry, sizeof(struct acpi_bus_event));
  576. kfree(entry);
  577. return 0;
  578. }
  579. #endif /* CONFIG_ACPI_PROC_EVENT */
  580. /* --------------------------------------------------------------------------
  581. Notification Handling
  582. -------------------------------------------------------------------------- */
  583. static void acpi_bus_check_device(acpi_handle handle)
  584. {
  585. struct acpi_device *device;
  586. acpi_status status;
  587. struct acpi_device_status old_status;
  588. if (acpi_bus_get_device(handle, &device))
  589. return;
  590. if (!device)
  591. return;
  592. old_status = device->status;
  593. /*
  594. * Make sure this device's parent is present before we go about
  595. * messing with the device.
  596. */
  597. if (device->parent && !device->parent->status.present) {
  598. device->status = device->parent->status;
  599. return;
  600. }
  601. status = acpi_bus_get_status(device);
  602. if (ACPI_FAILURE(status))
  603. return;
  604. if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
  605. return;
  606. /*
  607. * Device Insertion/Removal
  608. */
  609. if ((device->status.present) && !(old_status.present)) {
  610. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
  611. /* TBD: Handle device insertion */
  612. } else if (!(device->status.present) && (old_status.present)) {
  613. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
  614. /* TBD: Handle device removal */
  615. }
  616. }
  617. static void acpi_bus_check_scope(acpi_handle handle)
  618. {
  619. /* Status Change? */
  620. acpi_bus_check_device(handle);
  621. /*
  622. * TBD: Enumerate child devices within this device's scope and
  623. * run acpi_bus_check_device()'s on them.
  624. */
  625. }
  626. static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list);
  627. int register_acpi_bus_notifier(struct notifier_block *nb)
  628. {
  629. return blocking_notifier_chain_register(&acpi_bus_notify_list, nb);
  630. }
  631. EXPORT_SYMBOL_GPL(register_acpi_bus_notifier);
  632. void unregister_acpi_bus_notifier(struct notifier_block *nb)
  633. {
  634. blocking_notifier_chain_unregister(&acpi_bus_notify_list, nb);
  635. }
  636. EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier);
  637. /**
  638. * acpi_bus_notify
  639. * ---------------
  640. * Callback for all 'system-level' device notifications (values 0x00-0x7F).
  641. */
  642. static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
  643. {
  644. struct acpi_device *device = NULL;
  645. struct acpi_driver *driver;
  646. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
  647. type, handle));
  648. blocking_notifier_call_chain(&acpi_bus_notify_list,
  649. type, (void *)handle);
  650. switch (type) {
  651. case ACPI_NOTIFY_BUS_CHECK:
  652. acpi_bus_check_scope(handle);
  653. /*
  654. * TBD: We'll need to outsource certain events to non-ACPI
  655. * drivers via the device manager (device.c).
  656. */
  657. break;
  658. case ACPI_NOTIFY_DEVICE_CHECK:
  659. acpi_bus_check_device(handle);
  660. /*
  661. * TBD: We'll need to outsource certain events to non-ACPI
  662. * drivers via the device manager (device.c).
  663. */
  664. break;
  665. case ACPI_NOTIFY_DEVICE_WAKE:
  666. /* TBD */
  667. break;
  668. case ACPI_NOTIFY_EJECT_REQUEST:
  669. /* TBD */
  670. break;
  671. case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
  672. /* TBD: Exactly what does 'light' mean? */
  673. break;
  674. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  675. /* TBD */
  676. break;
  677. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  678. /* TBD */
  679. break;
  680. case ACPI_NOTIFY_POWER_FAULT:
  681. /* TBD */
  682. break;
  683. default:
  684. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  685. "Received unknown/unsupported notification [%08x]\n",
  686. type));
  687. break;
  688. }
  689. acpi_bus_get_device(handle, &device);
  690. if (device) {
  691. driver = device->driver;
  692. if (driver && driver->ops.notify &&
  693. (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
  694. driver->ops.notify(device, type);
  695. }
  696. }
  697. /* --------------------------------------------------------------------------
  698. Initialization/Cleanup
  699. -------------------------------------------------------------------------- */
  700. static int __init acpi_bus_init_irq(void)
  701. {
  702. acpi_status status = AE_OK;
  703. union acpi_object arg = { ACPI_TYPE_INTEGER };
  704. struct acpi_object_list arg_list = { 1, &arg };
  705. char *message = NULL;
  706. /*
  707. * Let the system know what interrupt model we are using by
  708. * evaluating the \_PIC object, if exists.
  709. */
  710. switch (acpi_irq_model) {
  711. case ACPI_IRQ_MODEL_PIC:
  712. message = "PIC";
  713. break;
  714. case ACPI_IRQ_MODEL_IOAPIC:
  715. message = "IOAPIC";
  716. break;
  717. case ACPI_IRQ_MODEL_IOSAPIC:
  718. message = "IOSAPIC";
  719. break;
  720. case ACPI_IRQ_MODEL_PLATFORM:
  721. message = "platform specific model";
  722. break;
  723. default:
  724. printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
  725. return -ENODEV;
  726. }
  727. printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
  728. arg.integer.value = acpi_irq_model;
  729. status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
  730. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  731. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
  732. return -ENODEV;
  733. }
  734. return 0;
  735. }
  736. u8 acpi_gbl_permanent_mmap;
  737. void __init acpi_early_init(void)
  738. {
  739. acpi_status status = AE_OK;
  740. if (acpi_disabled)
  741. return;
  742. printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
  743. /* enable workarounds, unless strict ACPI spec. compliance */
  744. if (!acpi_strict)
  745. acpi_gbl_enable_interpreter_slack = TRUE;
  746. acpi_gbl_permanent_mmap = 1;
  747. /*
  748. * If the machine falls into the DMI check table,
  749. * DSDT will be copied to memory
  750. */
  751. dmi_check_system(dsdt_dmi_table);
  752. status = acpi_reallocate_root_table();
  753. if (ACPI_FAILURE(status)) {
  754. printk(KERN_ERR PREFIX
  755. "Unable to reallocate ACPI tables\n");
  756. goto error0;
  757. }
  758. status = acpi_initialize_subsystem();
  759. if (ACPI_FAILURE(status)) {
  760. printk(KERN_ERR PREFIX
  761. "Unable to initialize the ACPI Interpreter\n");
  762. goto error0;
  763. }
  764. status = acpi_load_tables();
  765. if (ACPI_FAILURE(status)) {
  766. printk(KERN_ERR PREFIX
  767. "Unable to load the System Description Tables\n");
  768. goto error0;
  769. }
  770. #ifdef CONFIG_X86
  771. if (!acpi_ioapic) {
  772. /* compatible (0) means level (3) */
  773. if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
  774. acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
  775. acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
  776. }
  777. /* Set PIC-mode SCI trigger type */
  778. acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
  779. (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
  780. } else {
  781. /*
  782. * now that acpi_gbl_FADT is initialized,
  783. * update it with result from INT_SRC_OVR parsing
  784. */
  785. acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
  786. }
  787. #endif
  788. status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
  789. if (ACPI_FAILURE(status)) {
  790. printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
  791. goto error0;
  792. }
  793. return;
  794. error0:
  795. disable_acpi();
  796. return;
  797. }
  798. static int __init acpi_bus_init(void)
  799. {
  800. int result = 0;
  801. acpi_status status = AE_OK;
  802. extern acpi_status acpi_os_initialize1(void);
  803. acpi_os_initialize1();
  804. status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
  805. if (ACPI_FAILURE(status)) {
  806. printk(KERN_ERR PREFIX
  807. "Unable to start the ACPI Interpreter\n");
  808. goto error1;
  809. }
  810. /*
  811. * ACPI 2.0 requires the EC driver to be loaded and work before
  812. * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
  813. * is called).
  814. *
  815. * This is accomplished by looking for the ECDT table, and getting
  816. * the EC parameters out of that.
  817. */
  818. status = acpi_ec_ecdt_probe();
  819. /* Ignore result. Not having an ECDT is not fatal. */
  820. acpi_bus_osc_support();
  821. status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
  822. if (ACPI_FAILURE(status)) {
  823. printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
  824. goto error1;
  825. }
  826. /*
  827. * _PDC control method may load dynamic SSDT tables,
  828. * and we need to install the table handler before that.
  829. */
  830. acpi_sysfs_init();
  831. acpi_early_processor_set_pdc();
  832. /*
  833. * Maybe EC region is required at bus_scan/acpi_get_devices. So it
  834. * is necessary to enable it as early as possible.
  835. */
  836. acpi_boot_ec_enable();
  837. printk(KERN_INFO PREFIX "Interpreter enabled\n");
  838. /* Initialize sleep structures */
  839. acpi_sleep_init();
  840. /*
  841. * Get the system interrupt model and evaluate \_PIC.
  842. */
  843. result = acpi_bus_init_irq();
  844. if (result)
  845. goto error1;
  846. /*
  847. * Register the for all standard device notifications.
  848. */
  849. status =
  850. acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
  851. &acpi_bus_notify, NULL);
  852. if (ACPI_FAILURE(status)) {
  853. printk(KERN_ERR PREFIX
  854. "Unable to register for device notifications\n");
  855. goto error1;
  856. }
  857. /*
  858. * Create the top ACPI proc directory
  859. */
  860. acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
  861. return 0;
  862. /* Mimic structured exception handling */
  863. error1:
  864. acpi_terminate();
  865. return -ENODEV;
  866. }
  867. struct kobject *acpi_kobj;
  868. EXPORT_SYMBOL_GPL(acpi_kobj);
  869. static int __init acpi_init(void)
  870. {
  871. int result;
  872. if (acpi_disabled) {
  873. printk(KERN_INFO PREFIX "Interpreter disabled.\n");
  874. return -ENODEV;
  875. }
  876. acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
  877. if (!acpi_kobj) {
  878. printk(KERN_WARNING "%s: kset create error\n", __func__);
  879. acpi_kobj = NULL;
  880. }
  881. init_acpi_device_notify();
  882. result = acpi_bus_init();
  883. if (result) {
  884. disable_acpi();
  885. return result;
  886. }
  887. pci_mmcfg_late_init();
  888. acpi_scan_init();
  889. acpi_ec_init();
  890. acpi_debugfs_init();
  891. acpi_sleep_proc_init();
  892. acpi_wakeup_device_init();
  893. return 0;
  894. }
  895. subsys_initcall(acpi_init);