osl.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
  3. *
  4. * Copyright (C) 2000 Andrew Henroid
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/slab.h>
  30. #include <linux/mm.h>
  31. #include <linux/pci.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/kmod.h>
  34. #include <linux/delay.h>
  35. #include <linux/dmi.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/nmi.h>
  38. #include <linux/acpi.h>
  39. #include <acpi/acpi.h>
  40. #include <asm/io.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/processor.h>
  43. #include <asm/uaccess.h>
  44. #include <linux/efi.h>
  45. #define _COMPONENT ACPI_OS_SERVICES
  46. ACPI_MODULE_NAME("osl");
  47. #define PREFIX "ACPI: "
  48. struct acpi_os_dpc {
  49. acpi_osd_exec_callback function;
  50. void *context;
  51. struct work_struct work;
  52. };
  53. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  54. #include CONFIG_ACPI_CUSTOM_DSDT_FILE
  55. #endif
  56. #ifdef ENABLE_DEBUGGER
  57. #include <linux/kdb.h>
  58. /* stuff for debugger support */
  59. int acpi_in_debugger;
  60. EXPORT_SYMBOL(acpi_in_debugger);
  61. extern char line_buf[80];
  62. #endif /*ENABLE_DEBUGGER */
  63. static unsigned int acpi_irq_irq;
  64. static acpi_osd_handler acpi_irq_handler;
  65. static void *acpi_irq_context;
  66. static struct workqueue_struct *kacpid_wq;
  67. static struct workqueue_struct *kacpi_notify_wq;
  68. #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
  69. static char osi_additional_string[OSI_STRING_LENGTH_MAX];
  70. static int osi_linux; /* disable _OSI(Linux) by default */
  71. static void __init acpi_request_region (struct acpi_generic_address *addr,
  72. unsigned int length, char *desc)
  73. {
  74. struct resource *res;
  75. if (!addr->address || !length)
  76. return;
  77. if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  78. res = request_region(addr->address, length, desc);
  79. else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  80. res = request_mem_region(addr->address, length, desc);
  81. }
  82. static int __init acpi_reserve_resources(void)
  83. {
  84. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  85. "ACPI PM1a_EVT_BLK");
  86. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  87. "ACPI PM1b_EVT_BLK");
  88. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  89. "ACPI PM1a_CNT_BLK");
  90. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  91. "ACPI PM1b_CNT_BLK");
  92. if (acpi_gbl_FADT.pm_timer_length == 4)
  93. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  94. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  95. "ACPI PM2_CNT_BLK");
  96. /* Length of GPE blocks must be a non-negative multiple of 2 */
  97. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  98. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  99. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  100. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  101. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  102. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  103. return 0;
  104. }
  105. device_initcall(acpi_reserve_resources);
  106. acpi_status __init acpi_os_initialize(void)
  107. {
  108. return AE_OK;
  109. }
  110. acpi_status acpi_os_initialize1(void)
  111. {
  112. /*
  113. * Initialize PCI configuration space access, as we'll need to access
  114. * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
  115. */
  116. if (!raw_pci_ops) {
  117. printk(KERN_ERR PREFIX
  118. "Access to PCI configuration space unavailable\n");
  119. return AE_NULL_ENTRY;
  120. }
  121. kacpid_wq = create_singlethread_workqueue("kacpid");
  122. kacpi_notify_wq = create_singlethread_workqueue("kacpi_notify");
  123. BUG_ON(!kacpid_wq);
  124. BUG_ON(!kacpi_notify_wq);
  125. return AE_OK;
  126. }
  127. acpi_status acpi_os_terminate(void)
  128. {
  129. if (acpi_irq_handler) {
  130. acpi_os_remove_interrupt_handler(acpi_irq_irq,
  131. acpi_irq_handler);
  132. }
  133. destroy_workqueue(kacpid_wq);
  134. destroy_workqueue(kacpi_notify_wq);
  135. return AE_OK;
  136. }
  137. void acpi_os_printf(const char *fmt, ...)
  138. {
  139. va_list args;
  140. va_start(args, fmt);
  141. acpi_os_vprintf(fmt, args);
  142. va_end(args);
  143. }
  144. EXPORT_SYMBOL(acpi_os_printf);
  145. void acpi_os_vprintf(const char *fmt, va_list args)
  146. {
  147. static char buffer[512];
  148. vsprintf(buffer, fmt, args);
  149. #ifdef ENABLE_DEBUGGER
  150. if (acpi_in_debugger) {
  151. kdb_printf("%s", buffer);
  152. } else {
  153. printk("%s", buffer);
  154. }
  155. #else
  156. printk("%s", buffer);
  157. #endif
  158. }
  159. acpi_physical_address __init acpi_os_get_root_pointer(void)
  160. {
  161. if (efi_enabled) {
  162. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  163. return efi.acpi20;
  164. else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  165. return efi.acpi;
  166. else {
  167. printk(KERN_ERR PREFIX
  168. "System description tables not found\n");
  169. return 0;
  170. }
  171. } else
  172. return acpi_find_rsdp();
  173. }
  174. void __iomem *acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  175. {
  176. if (phys > ULONG_MAX) {
  177. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  178. return NULL;
  179. }
  180. if (acpi_gbl_permanent_mmap)
  181. /*
  182. * ioremap checks to ensure this is in reserved space
  183. */
  184. return ioremap((unsigned long)phys, size);
  185. else
  186. return __acpi_map_table((unsigned long)phys, size);
  187. }
  188. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  189. void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
  190. {
  191. if (acpi_gbl_permanent_mmap) {
  192. iounmap(virt);
  193. }
  194. }
  195. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  196. #ifdef ACPI_FUTURE_USAGE
  197. acpi_status
  198. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  199. {
  200. if (!phys || !virt)
  201. return AE_BAD_PARAMETER;
  202. *phys = virt_to_phys(virt);
  203. return AE_OK;
  204. }
  205. #endif
  206. #define ACPI_MAX_OVERRIDE_LEN 100
  207. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  208. acpi_status
  209. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  210. acpi_string * new_val)
  211. {
  212. if (!init_val || !new_val)
  213. return AE_BAD_PARAMETER;
  214. *new_val = NULL;
  215. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  216. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  217. acpi_os_name);
  218. *new_val = acpi_os_name;
  219. }
  220. return AE_OK;
  221. }
  222. acpi_status
  223. acpi_os_table_override(struct acpi_table_header * existing_table,
  224. struct acpi_table_header ** new_table)
  225. {
  226. if (!existing_table || !new_table)
  227. return AE_BAD_PARAMETER;
  228. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  229. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  230. *new_table = (struct acpi_table_header *)AmlCode;
  231. else
  232. *new_table = NULL;
  233. #else
  234. *new_table = NULL;
  235. #endif
  236. return AE_OK;
  237. }
  238. static irqreturn_t acpi_irq(int irq, void *dev_id)
  239. {
  240. return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
  241. }
  242. acpi_status
  243. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  244. void *context)
  245. {
  246. unsigned int irq;
  247. /*
  248. * Ignore the GSI from the core, and use the value in our copy of the
  249. * FADT. It may not be the same if an interrupt source override exists
  250. * for the SCI.
  251. */
  252. gsi = acpi_gbl_FADT.sci_interrupt;
  253. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  254. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  255. gsi);
  256. return AE_OK;
  257. }
  258. acpi_irq_handler = handler;
  259. acpi_irq_context = context;
  260. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  261. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  262. return AE_NOT_ACQUIRED;
  263. }
  264. acpi_irq_irq = irq;
  265. return AE_OK;
  266. }
  267. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  268. {
  269. if (irq) {
  270. free_irq(irq, acpi_irq);
  271. acpi_irq_handler = NULL;
  272. acpi_irq_irq = 0;
  273. }
  274. return AE_OK;
  275. }
  276. /*
  277. * Running in interpreter thread context, safe to sleep
  278. */
  279. void acpi_os_sleep(acpi_integer ms)
  280. {
  281. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  282. }
  283. EXPORT_SYMBOL(acpi_os_sleep);
  284. void acpi_os_stall(u32 us)
  285. {
  286. while (us) {
  287. u32 delay = 1000;
  288. if (delay > us)
  289. delay = us;
  290. udelay(delay);
  291. touch_nmi_watchdog();
  292. us -= delay;
  293. }
  294. }
  295. EXPORT_SYMBOL(acpi_os_stall);
  296. /*
  297. * Support ACPI 3.0 AML Timer operand
  298. * Returns 64-bit free-running, monotonically increasing timer
  299. * with 100ns granularity
  300. */
  301. u64 acpi_os_get_timer(void)
  302. {
  303. static u64 t;
  304. #ifdef CONFIG_HPET
  305. /* TBD: use HPET if available */
  306. #endif
  307. #ifdef CONFIG_X86_PM_TIMER
  308. /* TBD: default to PM timer if HPET was not available */
  309. #endif
  310. if (!t)
  311. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  312. return ++t;
  313. }
  314. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  315. {
  316. u32 dummy;
  317. if (!value)
  318. value = &dummy;
  319. *value = 0;
  320. if (width <= 8) {
  321. *(u8 *) value = inb(port);
  322. } else if (width <= 16) {
  323. *(u16 *) value = inw(port);
  324. } else if (width <= 32) {
  325. *(u32 *) value = inl(port);
  326. } else {
  327. BUG();
  328. }
  329. return AE_OK;
  330. }
  331. EXPORT_SYMBOL(acpi_os_read_port);
  332. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  333. {
  334. if (width <= 8) {
  335. outb(value, port);
  336. } else if (width <= 16) {
  337. outw(value, port);
  338. } else if (width <= 32) {
  339. outl(value, port);
  340. } else {
  341. BUG();
  342. }
  343. return AE_OK;
  344. }
  345. EXPORT_SYMBOL(acpi_os_write_port);
  346. acpi_status
  347. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  348. {
  349. u32 dummy;
  350. void __iomem *virt_addr;
  351. virt_addr = ioremap(phys_addr, width);
  352. if (!value)
  353. value = &dummy;
  354. switch (width) {
  355. case 8:
  356. *(u8 *) value = readb(virt_addr);
  357. break;
  358. case 16:
  359. *(u16 *) value = readw(virt_addr);
  360. break;
  361. case 32:
  362. *(u32 *) value = readl(virt_addr);
  363. break;
  364. default:
  365. BUG();
  366. }
  367. iounmap(virt_addr);
  368. return AE_OK;
  369. }
  370. acpi_status
  371. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  372. {
  373. void __iomem *virt_addr;
  374. virt_addr = ioremap(phys_addr, width);
  375. switch (width) {
  376. case 8:
  377. writeb(value, virt_addr);
  378. break;
  379. case 16:
  380. writew(value, virt_addr);
  381. break;
  382. case 32:
  383. writel(value, virt_addr);
  384. break;
  385. default:
  386. BUG();
  387. }
  388. iounmap(virt_addr);
  389. return AE_OK;
  390. }
  391. acpi_status
  392. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  393. void *value, u32 width)
  394. {
  395. int result, size;
  396. if (!value)
  397. return AE_BAD_PARAMETER;
  398. switch (width) {
  399. case 8:
  400. size = 1;
  401. break;
  402. case 16:
  403. size = 2;
  404. break;
  405. case 32:
  406. size = 4;
  407. break;
  408. default:
  409. return AE_ERROR;
  410. }
  411. BUG_ON(!raw_pci_ops);
  412. result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
  413. PCI_DEVFN(pci_id->device, pci_id->function),
  414. reg, size, value);
  415. return (result ? AE_ERROR : AE_OK);
  416. }
  417. EXPORT_SYMBOL(acpi_os_read_pci_configuration);
  418. acpi_status
  419. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  420. acpi_integer value, u32 width)
  421. {
  422. int result, size;
  423. switch (width) {
  424. case 8:
  425. size = 1;
  426. break;
  427. case 16:
  428. size = 2;
  429. break;
  430. case 32:
  431. size = 4;
  432. break;
  433. default:
  434. return AE_ERROR;
  435. }
  436. BUG_ON(!raw_pci_ops);
  437. result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
  438. PCI_DEVFN(pci_id->device, pci_id->function),
  439. reg, size, value);
  440. return (result ? AE_ERROR : AE_OK);
  441. }
  442. /* TODO: Change code to take advantage of driver model more */
  443. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  444. acpi_handle chandle, /* current node */
  445. struct acpi_pci_id **id,
  446. int *is_bridge, u8 * bus_number)
  447. {
  448. acpi_handle handle;
  449. struct acpi_pci_id *pci_id = *id;
  450. acpi_status status;
  451. unsigned long temp;
  452. acpi_object_type type;
  453. u8 tu8;
  454. acpi_get_parent(chandle, &handle);
  455. if (handle != rhandle) {
  456. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  457. bus_number);
  458. status = acpi_get_type(handle, &type);
  459. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  460. return;
  461. status =
  462. acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  463. &temp);
  464. if (ACPI_SUCCESS(status)) {
  465. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  466. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  467. if (*is_bridge)
  468. pci_id->bus = *bus_number;
  469. /* any nicer way to get bus number of bridge ? */
  470. status =
  471. acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
  472. 8);
  473. if (ACPI_SUCCESS(status)
  474. && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
  475. status =
  476. acpi_os_read_pci_configuration(pci_id, 0x18,
  477. &tu8, 8);
  478. if (!ACPI_SUCCESS(status)) {
  479. /* Certainly broken... FIX ME */
  480. return;
  481. }
  482. *is_bridge = 1;
  483. pci_id->bus = tu8;
  484. status =
  485. acpi_os_read_pci_configuration(pci_id, 0x19,
  486. &tu8, 8);
  487. if (ACPI_SUCCESS(status)) {
  488. *bus_number = tu8;
  489. }
  490. } else
  491. *is_bridge = 0;
  492. }
  493. }
  494. }
  495. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  496. acpi_handle chandle, /* current node */
  497. struct acpi_pci_id **id)
  498. {
  499. int is_bridge = 1;
  500. u8 bus_number = (*id)->bus;
  501. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  502. }
  503. static void acpi_os_execute_deferred(struct work_struct *work)
  504. {
  505. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  506. if (!dpc) {
  507. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  508. return;
  509. }
  510. dpc->function(dpc->context);
  511. kfree(dpc);
  512. /* Yield cpu to notify thread */
  513. cond_resched();
  514. return;
  515. }
  516. static void acpi_os_execute_notify(struct work_struct *work)
  517. {
  518. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  519. if (!dpc) {
  520. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  521. return;
  522. }
  523. dpc->function(dpc->context);
  524. kfree(dpc);
  525. return;
  526. }
  527. /*******************************************************************************
  528. *
  529. * FUNCTION: acpi_os_execute
  530. *
  531. * PARAMETERS: Type - Type of the callback
  532. * Function - Function to be executed
  533. * Context - Function parameters
  534. *
  535. * RETURN: Status
  536. *
  537. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  538. * immediately executes function on a separate thread.
  539. *
  540. ******************************************************************************/
  541. acpi_status acpi_os_execute(acpi_execute_type type,
  542. acpi_osd_exec_callback function, void *context)
  543. {
  544. acpi_status status = AE_OK;
  545. struct acpi_os_dpc *dpc;
  546. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  547. "Scheduling function [%p(%p)] for deferred execution.\n",
  548. function, context));
  549. if (!function)
  550. return AE_BAD_PARAMETER;
  551. /*
  552. * Allocate/initialize DPC structure. Note that this memory will be
  553. * freed by the callee. The kernel handles the work_struct list in a
  554. * way that allows us to also free its memory inside the callee.
  555. * Because we may want to schedule several tasks with different
  556. * parameters we can't use the approach some kernel code uses of
  557. * having a static work_struct.
  558. */
  559. dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  560. if (!dpc)
  561. return_ACPI_STATUS(AE_NO_MEMORY);
  562. dpc->function = function;
  563. dpc->context = context;
  564. if (type == OSL_NOTIFY_HANDLER) {
  565. INIT_WORK(&dpc->work, acpi_os_execute_notify);
  566. if (!queue_work(kacpi_notify_wq, &dpc->work)) {
  567. status = AE_ERROR;
  568. kfree(dpc);
  569. }
  570. } else {
  571. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  572. if (!queue_work(kacpid_wq, &dpc->work)) {
  573. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  574. "Call to queue_work() failed.\n"));
  575. status = AE_ERROR;
  576. kfree(dpc);
  577. }
  578. }
  579. return_ACPI_STATUS(status);
  580. }
  581. EXPORT_SYMBOL(acpi_os_execute);
  582. void acpi_os_wait_events_complete(void *context)
  583. {
  584. flush_workqueue(kacpid_wq);
  585. }
  586. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  587. /*
  588. * Allocate the memory for a spinlock and initialize it.
  589. */
  590. acpi_status acpi_os_create_lock(acpi_spinlock * handle)
  591. {
  592. spin_lock_init(*handle);
  593. return AE_OK;
  594. }
  595. /*
  596. * Deallocate the memory for a spinlock.
  597. */
  598. void acpi_os_delete_lock(acpi_spinlock handle)
  599. {
  600. return;
  601. }
  602. acpi_status
  603. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  604. {
  605. struct semaphore *sem = NULL;
  606. sem = acpi_os_allocate(sizeof(struct semaphore));
  607. if (!sem)
  608. return AE_NO_MEMORY;
  609. memset(sem, 0, sizeof(struct semaphore));
  610. sema_init(sem, initial_units);
  611. *handle = (acpi_handle *) sem;
  612. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  613. *handle, initial_units));
  614. return AE_OK;
  615. }
  616. EXPORT_SYMBOL(acpi_os_create_semaphore);
  617. /*
  618. * TODO: A better way to delete semaphores? Linux doesn't have a
  619. * 'delete_semaphore()' function -- may result in an invalid
  620. * pointer dereference for non-synchronized consumers. Should
  621. * we at least check for blocked threads and signal/cancel them?
  622. */
  623. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  624. {
  625. struct semaphore *sem = (struct semaphore *)handle;
  626. if (!sem)
  627. return AE_BAD_PARAMETER;
  628. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  629. kfree(sem);
  630. sem = NULL;
  631. return AE_OK;
  632. }
  633. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  634. /*
  635. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  636. * improvise. The process is to sleep for one scheduler quantum
  637. * until the semaphore becomes available. Downside is that this
  638. * may result in starvation for timeout-based waits when there's
  639. * lots of semaphore activity.
  640. *
  641. * TODO: Support for units > 1?
  642. */
  643. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  644. {
  645. acpi_status status = AE_OK;
  646. struct semaphore *sem = (struct semaphore *)handle;
  647. int ret = 0;
  648. if (!sem || (units < 1))
  649. return AE_BAD_PARAMETER;
  650. if (units > 1)
  651. return AE_SUPPORT;
  652. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  653. handle, units, timeout));
  654. /*
  655. * This can be called during resume with interrupts off.
  656. * Like boot-time, we should be single threaded and will
  657. * always get the lock if we try -- timeout or not.
  658. * If this doesn't succeed, then we will oops courtesy of
  659. * might_sleep() in down().
  660. */
  661. if (!down_trylock(sem))
  662. return AE_OK;
  663. switch (timeout) {
  664. /*
  665. * No Wait:
  666. * --------
  667. * A zero timeout value indicates that we shouldn't wait - just
  668. * acquire the semaphore if available otherwise return AE_TIME
  669. * (a.k.a. 'would block').
  670. */
  671. case 0:
  672. if (down_trylock(sem))
  673. status = AE_TIME;
  674. break;
  675. /*
  676. * Wait Indefinitely:
  677. * ------------------
  678. */
  679. case ACPI_WAIT_FOREVER:
  680. down(sem);
  681. break;
  682. /*
  683. * Wait w/ Timeout:
  684. * ----------------
  685. */
  686. default:
  687. // TODO: A better timeout algorithm?
  688. {
  689. int i = 0;
  690. static const int quantum_ms = 1000 / HZ;
  691. ret = down_trylock(sem);
  692. for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
  693. schedule_timeout_interruptible(1);
  694. ret = down_trylock(sem);
  695. }
  696. if (ret != 0)
  697. status = AE_TIME;
  698. }
  699. break;
  700. }
  701. if (ACPI_FAILURE(status)) {
  702. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  703. "Failed to acquire semaphore[%p|%d|%d], %s",
  704. handle, units, timeout,
  705. acpi_format_exception(status)));
  706. } else {
  707. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  708. "Acquired semaphore[%p|%d|%d]", handle,
  709. units, timeout));
  710. }
  711. return status;
  712. }
  713. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  714. /*
  715. * TODO: Support for units > 1?
  716. */
  717. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  718. {
  719. struct semaphore *sem = (struct semaphore *)handle;
  720. if (!sem || (units < 1))
  721. return AE_BAD_PARAMETER;
  722. if (units > 1)
  723. return AE_SUPPORT;
  724. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  725. units));
  726. up(sem);
  727. return AE_OK;
  728. }
  729. EXPORT_SYMBOL(acpi_os_signal_semaphore);
  730. #ifdef ACPI_FUTURE_USAGE
  731. u32 acpi_os_get_line(char *buffer)
  732. {
  733. #ifdef ENABLE_DEBUGGER
  734. if (acpi_in_debugger) {
  735. u32 chars;
  736. kdb_read(buffer, sizeof(line_buf));
  737. /* remove the CR kdb includes */
  738. chars = strlen(buffer) - 1;
  739. buffer[chars] = '\0';
  740. }
  741. #endif
  742. return 0;
  743. }
  744. #endif /* ACPI_FUTURE_USAGE */
  745. acpi_status acpi_os_signal(u32 function, void *info)
  746. {
  747. switch (function) {
  748. case ACPI_SIGNAL_FATAL:
  749. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  750. break;
  751. case ACPI_SIGNAL_BREAKPOINT:
  752. /*
  753. * AML Breakpoint
  754. * ACPI spec. says to treat it as a NOP unless
  755. * you are debugging. So if/when we integrate
  756. * AML debugger into the kernel debugger its
  757. * hook will go here. But until then it is
  758. * not useful to print anything on breakpoints.
  759. */
  760. break;
  761. default:
  762. break;
  763. }
  764. return AE_OK;
  765. }
  766. EXPORT_SYMBOL(acpi_os_signal);
  767. static int __init acpi_os_name_setup(char *str)
  768. {
  769. char *p = acpi_os_name;
  770. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  771. if (!str || !*str)
  772. return 0;
  773. for (; count-- && str && *str; str++) {
  774. if (isalnum(*str) || *str == ' ' || *str == ':')
  775. *p++ = *str;
  776. else if (*str == '\'' || *str == '"')
  777. continue;
  778. else
  779. break;
  780. }
  781. *p = 0;
  782. return 1;
  783. }
  784. __setup("acpi_os_name=", acpi_os_name_setup);
  785. static void enable_osi_linux(int enable) {
  786. if (osi_linux != enable)
  787. printk(KERN_INFO PREFIX "%sabled _OSI(Linux)\n",
  788. enable ? "En": "Dis");
  789. osi_linux = enable;
  790. return;
  791. }
  792. /*
  793. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  794. *
  795. * empty string disables _OSI
  796. * string starting with '!' disables that string
  797. * otherwise string is added to list, augmenting built-in strings
  798. */
  799. static int __init acpi_osi_setup(char *str)
  800. {
  801. if (str == NULL || *str == '\0') {
  802. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  803. acpi_gbl_create_osi_method = FALSE;
  804. } else if (!strcmp("!Linux", str)) {
  805. enable_osi_linux(0);
  806. } else if (*str == '!') {
  807. if (acpi_osi_invalidate(++str) == AE_OK)
  808. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  809. } else if (!strcmp("Linux", str)) {
  810. enable_osi_linux(1);
  811. } else if (*osi_additional_string == '\0') {
  812. strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
  813. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  814. }
  815. return 1;
  816. }
  817. __setup("acpi_osi=", acpi_osi_setup);
  818. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  819. static int __init acpi_serialize_setup(char *str)
  820. {
  821. printk(KERN_INFO PREFIX "serialize enabled\n");
  822. acpi_gbl_all_methods_serialized = TRUE;
  823. return 1;
  824. }
  825. __setup("acpi_serialize", acpi_serialize_setup);
  826. /*
  827. * Wake and Run-Time GPES are expected to be separate.
  828. * We disable wake-GPEs at run-time to prevent spurious
  829. * interrupts.
  830. *
  831. * However, if a system exists that shares Wake and
  832. * Run-time events on the same GPE this flag is available
  833. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  834. */
  835. static int __init acpi_wake_gpes_always_on_setup(char *str)
  836. {
  837. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  838. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  839. return 1;
  840. }
  841. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  842. /*
  843. * Acquire a spinlock.
  844. *
  845. * handle is a pointer to the spinlock_t.
  846. */
  847. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  848. {
  849. acpi_cpu_flags flags;
  850. spin_lock_irqsave(lockp, flags);
  851. return flags;
  852. }
  853. /*
  854. * Release a spinlock. See above.
  855. */
  856. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  857. {
  858. spin_unlock_irqrestore(lockp, flags);
  859. }
  860. #ifndef ACPI_USE_LOCAL_CACHE
  861. /*******************************************************************************
  862. *
  863. * FUNCTION: acpi_os_create_cache
  864. *
  865. * PARAMETERS: name - Ascii name for the cache
  866. * size - Size of each cached object
  867. * depth - Maximum depth of the cache (in objects) <ignored>
  868. * cache - Where the new cache object is returned
  869. *
  870. * RETURN: status
  871. *
  872. * DESCRIPTION: Create a cache object
  873. *
  874. ******************************************************************************/
  875. acpi_status
  876. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  877. {
  878. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  879. if (*cache == NULL)
  880. return AE_ERROR;
  881. else
  882. return AE_OK;
  883. }
  884. /*******************************************************************************
  885. *
  886. * FUNCTION: acpi_os_purge_cache
  887. *
  888. * PARAMETERS: Cache - Handle to cache object
  889. *
  890. * RETURN: Status
  891. *
  892. * DESCRIPTION: Free all objects within the requested cache.
  893. *
  894. ******************************************************************************/
  895. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  896. {
  897. kmem_cache_shrink(cache);
  898. return (AE_OK);
  899. }
  900. /*******************************************************************************
  901. *
  902. * FUNCTION: acpi_os_delete_cache
  903. *
  904. * PARAMETERS: Cache - Handle to cache object
  905. *
  906. * RETURN: Status
  907. *
  908. * DESCRIPTION: Free all objects within the requested cache and delete the
  909. * cache object.
  910. *
  911. ******************************************************************************/
  912. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  913. {
  914. kmem_cache_destroy(cache);
  915. return (AE_OK);
  916. }
  917. /*******************************************************************************
  918. *
  919. * FUNCTION: acpi_os_release_object
  920. *
  921. * PARAMETERS: Cache - Handle to cache object
  922. * Object - The object to be released
  923. *
  924. * RETURN: None
  925. *
  926. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  927. * the object is deleted.
  928. *
  929. ******************************************************************************/
  930. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  931. {
  932. kmem_cache_free(cache, object);
  933. return (AE_OK);
  934. }
  935. /**
  936. * acpi_dmi_dump - dump DMI slots needed for blacklist entry
  937. *
  938. * Returns 0 on success
  939. */
  940. int acpi_dmi_dump(void)
  941. {
  942. if (!dmi_available)
  943. return -1;
  944. printk(KERN_NOTICE PREFIX "DMI System Vendor: %s\n",
  945. dmi_get_slot(DMI_SYS_VENDOR));
  946. printk(KERN_NOTICE PREFIX "DMI Product Name: %s\n",
  947. dmi_get_slot(DMI_PRODUCT_NAME));
  948. printk(KERN_NOTICE PREFIX "DMI Product Version: %s\n",
  949. dmi_get_slot(DMI_PRODUCT_VERSION));
  950. printk(KERN_NOTICE PREFIX "DMI Board Name: %s\n",
  951. dmi_get_slot(DMI_BOARD_NAME));
  952. printk(KERN_NOTICE PREFIX "DMI BIOS Vendor: %s\n",
  953. dmi_get_slot(DMI_BIOS_VENDOR));
  954. printk(KERN_NOTICE PREFIX "DMI BIOS Date: %s\n",
  955. dmi_get_slot(DMI_BIOS_DATE));
  956. return 0;
  957. }
  958. /******************************************************************************
  959. *
  960. * FUNCTION: acpi_os_validate_interface
  961. *
  962. * PARAMETERS: interface - Requested interface to be validated
  963. *
  964. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  965. *
  966. * DESCRIPTION: Match an interface string to the interfaces supported by the
  967. * host. Strings originate from an AML call to the _OSI method.
  968. *
  969. *****************************************************************************/
  970. acpi_status
  971. acpi_os_validate_interface (char *interface)
  972. {
  973. if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
  974. return AE_OK;
  975. if (!strcmp("Linux", interface)) {
  976. printk(KERN_WARNING PREFIX
  977. "System BIOS is requesting _OSI(Linux)\n");
  978. if (acpi_dmi_dump())
  979. printk(KERN_NOTICE PREFIX
  980. "[please extract dmidecode output]\n");
  981. printk(KERN_NOTICE PREFIX
  982. "Please send DMI info above to "
  983. "linux-acpi@vger.kernel.org\n");
  984. printk(KERN_NOTICE PREFIX
  985. "If \"acpi_osi=%sLinux\" works better, "
  986. "please notify linux-acpi@vger.kernel.org\n",
  987. osi_linux ? "!" : "");
  988. if(osi_linux)
  989. return AE_OK;
  990. }
  991. return AE_SUPPORT;
  992. }
  993. /******************************************************************************
  994. *
  995. * FUNCTION: acpi_os_validate_address
  996. *
  997. * PARAMETERS: space_id - ACPI space ID
  998. * address - Physical address
  999. * length - Address length
  1000. *
  1001. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  1002. * should return AE_AML_ILLEGAL_ADDRESS.
  1003. *
  1004. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  1005. * the addresses accessed by AML operation regions.
  1006. *
  1007. *****************************************************************************/
  1008. acpi_status
  1009. acpi_os_validate_address (
  1010. u8 space_id,
  1011. acpi_physical_address address,
  1012. acpi_size length)
  1013. {
  1014. return AE_OK;
  1015. }
  1016. #endif