bus.c 26 KB

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