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 <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. if (!ghes_disable)
  465. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_APEI_SUPPORT;
  466. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  467. return;
  468. if (ACPI_SUCCESS(acpi_run_osc(handle, &context))) {
  469. u32 *capbuf_ret = context.ret.pointer;
  470. if (context.ret.length > OSC_SUPPORT_TYPE)
  471. osc_sb_apei_support_acked =
  472. capbuf_ret[OSC_SUPPORT_TYPE] & OSC_SB_APEI_SUPPORT;
  473. kfree(context.ret.pointer);
  474. }
  475. /* do we need to check other returned cap? Sounds no */
  476. }
  477. /* --------------------------------------------------------------------------
  478. Event Management
  479. -------------------------------------------------------------------------- */
  480. #ifdef CONFIG_ACPI_PROC_EVENT
  481. static DEFINE_SPINLOCK(acpi_bus_event_lock);
  482. LIST_HEAD(acpi_bus_event_list);
  483. DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
  484. extern int event_is_open;
  485. int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
  486. {
  487. struct acpi_bus_event *event;
  488. unsigned long flags = 0;
  489. /* drop event on the floor if no one's listening */
  490. if (!event_is_open)
  491. return 0;
  492. event = kzalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
  493. if (!event)
  494. return -ENOMEM;
  495. strcpy(event->device_class, device_class);
  496. strcpy(event->bus_id, bus_id);
  497. event->type = type;
  498. event->data = data;
  499. spin_lock_irqsave(&acpi_bus_event_lock, flags);
  500. list_add_tail(&event->node, &acpi_bus_event_list);
  501. spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
  502. wake_up_interruptible(&acpi_bus_event_queue);
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
  506. int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
  507. {
  508. if (!device)
  509. return -EINVAL;
  510. return acpi_bus_generate_proc_event4(device->pnp.device_class,
  511. device->pnp.bus_id, type, data);
  512. }
  513. EXPORT_SYMBOL(acpi_bus_generate_proc_event);
  514. int acpi_bus_receive_event(struct acpi_bus_event *event)
  515. {
  516. unsigned long flags = 0;
  517. struct acpi_bus_event *entry = NULL;
  518. DECLARE_WAITQUEUE(wait, current);
  519. if (!event)
  520. return -EINVAL;
  521. if (list_empty(&acpi_bus_event_list)) {
  522. set_current_state(TASK_INTERRUPTIBLE);
  523. add_wait_queue(&acpi_bus_event_queue, &wait);
  524. if (list_empty(&acpi_bus_event_list))
  525. schedule();
  526. remove_wait_queue(&acpi_bus_event_queue, &wait);
  527. set_current_state(TASK_RUNNING);
  528. if (signal_pending(current))
  529. return -ERESTARTSYS;
  530. }
  531. spin_lock_irqsave(&acpi_bus_event_lock, flags);
  532. if (!list_empty(&acpi_bus_event_list)) {
  533. entry = list_entry(acpi_bus_event_list.next,
  534. struct acpi_bus_event, node);
  535. list_del(&entry->node);
  536. }
  537. spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
  538. if (!entry)
  539. return -ENODEV;
  540. memcpy(event, entry, sizeof(struct acpi_bus_event));
  541. kfree(entry);
  542. return 0;
  543. }
  544. #endif /* CONFIG_ACPI_PROC_EVENT */
  545. /* --------------------------------------------------------------------------
  546. Notification Handling
  547. -------------------------------------------------------------------------- */
  548. static void acpi_bus_check_device(acpi_handle handle)
  549. {
  550. struct acpi_device *device;
  551. acpi_status status;
  552. struct acpi_device_status old_status;
  553. if (acpi_bus_get_device(handle, &device))
  554. return;
  555. if (!device)
  556. return;
  557. old_status = device->status;
  558. /*
  559. * Make sure this device's parent is present before we go about
  560. * messing with the device.
  561. */
  562. if (device->parent && !device->parent->status.present) {
  563. device->status = device->parent->status;
  564. return;
  565. }
  566. status = acpi_bus_get_status(device);
  567. if (ACPI_FAILURE(status))
  568. return;
  569. if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
  570. return;
  571. /*
  572. * Device Insertion/Removal
  573. */
  574. if ((device->status.present) && !(old_status.present)) {
  575. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
  576. /* TBD: Handle device insertion */
  577. } else if (!(device->status.present) && (old_status.present)) {
  578. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
  579. /* TBD: Handle device removal */
  580. }
  581. }
  582. static void acpi_bus_check_scope(acpi_handle handle)
  583. {
  584. /* Status Change? */
  585. acpi_bus_check_device(handle);
  586. /*
  587. * TBD: Enumerate child devices within this device's scope and
  588. * run acpi_bus_check_device()'s on them.
  589. */
  590. }
  591. static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list);
  592. int register_acpi_bus_notifier(struct notifier_block *nb)
  593. {
  594. return blocking_notifier_chain_register(&acpi_bus_notify_list, nb);
  595. }
  596. EXPORT_SYMBOL_GPL(register_acpi_bus_notifier);
  597. void unregister_acpi_bus_notifier(struct notifier_block *nb)
  598. {
  599. blocking_notifier_chain_unregister(&acpi_bus_notify_list, nb);
  600. }
  601. EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier);
  602. /**
  603. * acpi_bus_notify
  604. * ---------------
  605. * Callback for all 'system-level' device notifications (values 0x00-0x7F).
  606. */
  607. static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
  608. {
  609. struct acpi_device *device = NULL;
  610. struct acpi_driver *driver;
  611. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
  612. type, handle));
  613. blocking_notifier_call_chain(&acpi_bus_notify_list,
  614. type, (void *)handle);
  615. switch (type) {
  616. case ACPI_NOTIFY_BUS_CHECK:
  617. acpi_bus_check_scope(handle);
  618. /*
  619. * TBD: We'll need to outsource certain events to non-ACPI
  620. * drivers via the device manager (device.c).
  621. */
  622. break;
  623. case ACPI_NOTIFY_DEVICE_CHECK:
  624. acpi_bus_check_device(handle);
  625. /*
  626. * TBD: We'll need to outsource certain events to non-ACPI
  627. * drivers via the device manager (device.c).
  628. */
  629. break;
  630. case ACPI_NOTIFY_DEVICE_WAKE:
  631. /* TBD */
  632. break;
  633. case ACPI_NOTIFY_EJECT_REQUEST:
  634. /* TBD */
  635. break;
  636. case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
  637. /* TBD: Exactly what does 'light' mean? */
  638. break;
  639. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  640. /* TBD */
  641. break;
  642. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  643. /* TBD */
  644. break;
  645. case ACPI_NOTIFY_POWER_FAULT:
  646. /* TBD */
  647. break;
  648. default:
  649. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  650. "Received unknown/unsupported notification [%08x]\n",
  651. type));
  652. break;
  653. }
  654. acpi_bus_get_device(handle, &device);
  655. if (device) {
  656. driver = device->driver;
  657. if (driver && driver->ops.notify &&
  658. (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
  659. driver->ops.notify(device, type);
  660. }
  661. }
  662. /* --------------------------------------------------------------------------
  663. Initialization/Cleanup
  664. -------------------------------------------------------------------------- */
  665. static int __init acpi_bus_init_irq(void)
  666. {
  667. acpi_status status = AE_OK;
  668. union acpi_object arg = { ACPI_TYPE_INTEGER };
  669. struct acpi_object_list arg_list = { 1, &arg };
  670. char *message = NULL;
  671. /*
  672. * Let the system know what interrupt model we are using by
  673. * evaluating the \_PIC object, if exists.
  674. */
  675. switch (acpi_irq_model) {
  676. case ACPI_IRQ_MODEL_PIC:
  677. message = "PIC";
  678. break;
  679. case ACPI_IRQ_MODEL_IOAPIC:
  680. message = "IOAPIC";
  681. break;
  682. case ACPI_IRQ_MODEL_IOSAPIC:
  683. message = "IOSAPIC";
  684. break;
  685. case ACPI_IRQ_MODEL_PLATFORM:
  686. message = "platform specific model";
  687. break;
  688. default:
  689. printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
  690. return -ENODEV;
  691. }
  692. printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
  693. arg.integer.value = acpi_irq_model;
  694. status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
  695. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  696. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
  697. return -ENODEV;
  698. }
  699. return 0;
  700. }
  701. u8 acpi_gbl_permanent_mmap;
  702. void __init acpi_early_init(void)
  703. {
  704. acpi_status status = AE_OK;
  705. if (acpi_disabled)
  706. return;
  707. printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
  708. /* enable workarounds, unless strict ACPI spec. compliance */
  709. if (!acpi_strict)
  710. acpi_gbl_enable_interpreter_slack = TRUE;
  711. acpi_gbl_permanent_mmap = 1;
  712. /*
  713. * If the machine falls into the DMI check table,
  714. * DSDT will be copied to memory
  715. */
  716. dmi_check_system(dsdt_dmi_table);
  717. status = acpi_reallocate_root_table();
  718. if (ACPI_FAILURE(status)) {
  719. printk(KERN_ERR PREFIX
  720. "Unable to reallocate ACPI tables\n");
  721. goto error0;
  722. }
  723. status = acpi_initialize_subsystem();
  724. if (ACPI_FAILURE(status)) {
  725. printk(KERN_ERR PREFIX
  726. "Unable to initialize the ACPI Interpreter\n");
  727. goto error0;
  728. }
  729. status = acpi_load_tables();
  730. if (ACPI_FAILURE(status)) {
  731. printk(KERN_ERR PREFIX
  732. "Unable to load the System Description Tables\n");
  733. goto error0;
  734. }
  735. #ifdef CONFIG_X86
  736. if (!acpi_ioapic) {
  737. /* compatible (0) means level (3) */
  738. if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
  739. acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
  740. acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
  741. }
  742. /* Set PIC-mode SCI trigger type */
  743. acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
  744. (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
  745. } else {
  746. /*
  747. * now that acpi_gbl_FADT is initialized,
  748. * update it with result from INT_SRC_OVR parsing
  749. */
  750. acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
  751. }
  752. #endif
  753. status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
  754. if (ACPI_FAILURE(status)) {
  755. printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
  756. goto error0;
  757. }
  758. return;
  759. error0:
  760. disable_acpi();
  761. return;
  762. }
  763. static int __init acpi_bus_init(void)
  764. {
  765. int result = 0;
  766. acpi_status status = AE_OK;
  767. extern acpi_status acpi_os_initialize1(void);
  768. acpi_os_initialize1();
  769. status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
  770. if (ACPI_FAILURE(status)) {
  771. printk(KERN_ERR PREFIX
  772. "Unable to start the ACPI Interpreter\n");
  773. goto error1;
  774. }
  775. /*
  776. * ACPI 2.0 requires the EC driver to be loaded and work before
  777. * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
  778. * is called).
  779. *
  780. * This is accomplished by looking for the ECDT table, and getting
  781. * the EC parameters out of that.
  782. */
  783. status = acpi_ec_ecdt_probe();
  784. /* Ignore result. Not having an ECDT is not fatal. */
  785. acpi_bus_osc_support();
  786. status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
  787. if (ACPI_FAILURE(status)) {
  788. printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
  789. goto error1;
  790. }
  791. /*
  792. * _PDC control method may load dynamic SSDT tables,
  793. * and we need to install the table handler before that.
  794. */
  795. acpi_sysfs_init();
  796. acpi_early_processor_set_pdc();
  797. /*
  798. * Maybe EC region is required at bus_scan/acpi_get_devices. So it
  799. * is necessary to enable it as early as possible.
  800. */
  801. acpi_boot_ec_enable();
  802. printk(KERN_INFO PREFIX "Interpreter enabled\n");
  803. /* Initialize sleep structures */
  804. acpi_sleep_init();
  805. /*
  806. * Get the system interrupt model and evaluate \_PIC.
  807. */
  808. result = acpi_bus_init_irq();
  809. if (result)
  810. goto error1;
  811. /*
  812. * Register the for all standard device notifications.
  813. */
  814. status =
  815. acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
  816. &acpi_bus_notify, NULL);
  817. if (ACPI_FAILURE(status)) {
  818. printk(KERN_ERR PREFIX
  819. "Unable to register for device notifications\n");
  820. goto error1;
  821. }
  822. /*
  823. * Create the top ACPI proc directory
  824. */
  825. acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
  826. return 0;
  827. /* Mimic structured exception handling */
  828. error1:
  829. acpi_terminate();
  830. return -ENODEV;
  831. }
  832. struct kobject *acpi_kobj;
  833. EXPORT_SYMBOL_GPL(acpi_kobj);
  834. static int __init acpi_init(void)
  835. {
  836. int result;
  837. if (acpi_disabled) {
  838. printk(KERN_INFO PREFIX "Interpreter disabled.\n");
  839. return -ENODEV;
  840. }
  841. acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
  842. if (!acpi_kobj) {
  843. printk(KERN_WARNING "%s: kset create error\n", __func__);
  844. acpi_kobj = NULL;
  845. }
  846. init_acpi_device_notify();
  847. result = acpi_bus_init();
  848. if (result) {
  849. disable_acpi();
  850. return result;
  851. }
  852. pci_mmcfg_late_init();
  853. acpi_scan_init();
  854. acpi_ec_init();
  855. acpi_debugfs_init();
  856. acpi_sleep_proc_init();
  857. acpi_wakeup_device_init();
  858. return 0;
  859. }
  860. subsys_initcall(acpi_init);