bus.c 26 KB

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