bus.c 26 KB

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