bus.c 26 KB

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