bus.c 25 KB

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