bus.c 25 KB

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