bus.c 25 KB

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