bus.c 25 KB

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