osl.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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/config.h>
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/mm.h>
  32. #include <linux/pci.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/kmod.h>
  36. #include <linux/delay.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/nmi.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. };
  52. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  53. #include CONFIG_ACPI_CUSTOM_DSDT_FILE
  54. #endif
  55. #ifdef ENABLE_DEBUGGER
  56. #include <linux/kdb.h>
  57. /* stuff for debugger support */
  58. int acpi_in_debugger;
  59. EXPORT_SYMBOL(acpi_in_debugger);
  60. extern char line_buf[80];
  61. #endif /*ENABLE_DEBUGGER */
  62. int acpi_specific_hotkey_enabled = TRUE;
  63. EXPORT_SYMBOL(acpi_specific_hotkey_enabled);
  64. static unsigned int acpi_irq_irq;
  65. static acpi_osd_handler acpi_irq_handler;
  66. static void *acpi_irq_context;
  67. static struct workqueue_struct *kacpid_wq;
  68. acpi_status acpi_os_initialize(void)
  69. {
  70. return AE_OK;
  71. }
  72. acpi_status acpi_os_initialize1(void)
  73. {
  74. /*
  75. * Initialize PCI configuration space access, as we'll need to access
  76. * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
  77. */
  78. if (!raw_pci_ops) {
  79. printk(KERN_ERR PREFIX
  80. "Access to PCI configuration space unavailable\n");
  81. return AE_NULL_ENTRY;
  82. }
  83. kacpid_wq = create_singlethread_workqueue("kacpid");
  84. BUG_ON(!kacpid_wq);
  85. return AE_OK;
  86. }
  87. acpi_status acpi_os_terminate(void)
  88. {
  89. if (acpi_irq_handler) {
  90. acpi_os_remove_interrupt_handler(acpi_irq_irq,
  91. acpi_irq_handler);
  92. }
  93. destroy_workqueue(kacpid_wq);
  94. return AE_OK;
  95. }
  96. void acpi_os_printf(const char *fmt, ...)
  97. {
  98. va_list args;
  99. va_start(args, fmt);
  100. acpi_os_vprintf(fmt, args);
  101. va_end(args);
  102. }
  103. EXPORT_SYMBOL(acpi_os_printf);
  104. void acpi_os_vprintf(const char *fmt, va_list args)
  105. {
  106. static char buffer[512];
  107. vsprintf(buffer, fmt, args);
  108. #ifdef ENABLE_DEBUGGER
  109. if (acpi_in_debugger) {
  110. kdb_printf("%s", buffer);
  111. } else {
  112. printk("%s", buffer);
  113. }
  114. #else
  115. printk("%s", buffer);
  116. #endif
  117. }
  118. extern int acpi_in_resume;
  119. void *acpi_os_allocate(acpi_size size)
  120. {
  121. if (acpi_in_resume)
  122. return kmalloc(size, GFP_ATOMIC);
  123. else
  124. return kmalloc(size, GFP_KERNEL);
  125. }
  126. void acpi_os_free(void *ptr)
  127. {
  128. kfree(ptr);
  129. }
  130. EXPORT_SYMBOL(acpi_os_free);
  131. acpi_status acpi_os_get_root_pointer(u32 flags, struct acpi_pointer *addr)
  132. {
  133. if (efi_enabled) {
  134. addr->pointer_type = ACPI_PHYSICAL_POINTER;
  135. if (efi.acpi20)
  136. addr->pointer.physical =
  137. (acpi_physical_address) virt_to_phys(efi.acpi20);
  138. else if (efi.acpi)
  139. addr->pointer.physical =
  140. (acpi_physical_address) virt_to_phys(efi.acpi);
  141. else {
  142. printk(KERN_ERR PREFIX
  143. "System description tables not found\n");
  144. return AE_NOT_FOUND;
  145. }
  146. } else {
  147. if (ACPI_FAILURE(acpi_find_root_pointer(flags, addr))) {
  148. printk(KERN_ERR PREFIX
  149. "System description tables not found\n");
  150. return AE_NOT_FOUND;
  151. }
  152. }
  153. return AE_OK;
  154. }
  155. acpi_status
  156. acpi_os_map_memory(acpi_physical_address phys, acpi_size size,
  157. void __iomem ** virt)
  158. {
  159. if (efi_enabled) {
  160. if (EFI_MEMORY_WB & efi_mem_attributes(phys)) {
  161. *virt = (void __iomem *)phys_to_virt(phys);
  162. } else {
  163. *virt = ioremap(phys, size);
  164. }
  165. } else {
  166. if (phys > ULONG_MAX) {
  167. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  168. return AE_BAD_PARAMETER;
  169. }
  170. /*
  171. * ioremap checks to ensure this is in reserved space
  172. */
  173. *virt = ioremap((unsigned long)phys, size);
  174. }
  175. if (!*virt)
  176. return AE_NO_MEMORY;
  177. return AE_OK;
  178. }
  179. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  180. void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
  181. {
  182. iounmap(virt);
  183. }
  184. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  185. #ifdef ACPI_FUTURE_USAGE
  186. acpi_status
  187. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  188. {
  189. if (!phys || !virt)
  190. return AE_BAD_PARAMETER;
  191. *phys = virt_to_phys(virt);
  192. return AE_OK;
  193. }
  194. #endif
  195. #define ACPI_MAX_OVERRIDE_LEN 100
  196. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  197. acpi_status
  198. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  199. acpi_string * new_val)
  200. {
  201. if (!init_val || !new_val)
  202. return AE_BAD_PARAMETER;
  203. *new_val = NULL;
  204. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  205. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  206. acpi_os_name);
  207. *new_val = acpi_os_name;
  208. }
  209. return AE_OK;
  210. }
  211. acpi_status
  212. acpi_os_table_override(struct acpi_table_header * existing_table,
  213. struct acpi_table_header ** new_table)
  214. {
  215. if (!existing_table || !new_table)
  216. return AE_BAD_PARAMETER;
  217. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  218. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  219. *new_table = (struct acpi_table_header *)AmlCode;
  220. else
  221. *new_table = NULL;
  222. #else
  223. *new_table = NULL;
  224. #endif
  225. return AE_OK;
  226. }
  227. static irqreturn_t acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
  228. {
  229. return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
  230. }
  231. acpi_status
  232. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  233. void *context)
  234. {
  235. unsigned int irq;
  236. /*
  237. * Ignore the GSI from the core, and use the value in our copy of the
  238. * FADT. It may not be the same if an interrupt source override exists
  239. * for the SCI.
  240. */
  241. gsi = acpi_fadt.sci_int;
  242. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  243. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  244. gsi);
  245. return AE_OK;
  246. }
  247. acpi_irq_handler = handler;
  248. acpi_irq_context = context;
  249. if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
  250. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  251. return AE_NOT_ACQUIRED;
  252. }
  253. acpi_irq_irq = irq;
  254. return AE_OK;
  255. }
  256. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  257. {
  258. if (irq) {
  259. free_irq(irq, acpi_irq);
  260. acpi_irq_handler = NULL;
  261. acpi_irq_irq = 0;
  262. }
  263. return AE_OK;
  264. }
  265. /*
  266. * Running in interpreter thread context, safe to sleep
  267. */
  268. void acpi_os_sleep(acpi_integer ms)
  269. {
  270. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  271. }
  272. EXPORT_SYMBOL(acpi_os_sleep);
  273. void acpi_os_stall(u32 us)
  274. {
  275. while (us) {
  276. u32 delay = 1000;
  277. if (delay > us)
  278. delay = us;
  279. udelay(delay);
  280. touch_nmi_watchdog();
  281. us -= delay;
  282. }
  283. }
  284. EXPORT_SYMBOL(acpi_os_stall);
  285. /*
  286. * Support ACPI 3.0 AML Timer operand
  287. * Returns 64-bit free-running, monotonically increasing timer
  288. * with 100ns granularity
  289. */
  290. u64 acpi_os_get_timer(void)
  291. {
  292. static u64 t;
  293. #ifdef CONFIG_HPET
  294. /* TBD: use HPET if available */
  295. #endif
  296. #ifdef CONFIG_X86_PM_TIMER
  297. /* TBD: default to PM timer if HPET was not available */
  298. #endif
  299. if (!t)
  300. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  301. return ++t;
  302. }
  303. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  304. {
  305. u32 dummy;
  306. if (!value)
  307. value = &dummy;
  308. switch (width) {
  309. case 8:
  310. *(u8 *) value = inb(port);
  311. break;
  312. case 16:
  313. *(u16 *) value = inw(port);
  314. break;
  315. case 32:
  316. *(u32 *) value = inl(port);
  317. break;
  318. default:
  319. BUG();
  320. }
  321. return AE_OK;
  322. }
  323. EXPORT_SYMBOL(acpi_os_read_port);
  324. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  325. {
  326. switch (width) {
  327. case 8:
  328. outb(value, port);
  329. break;
  330. case 16:
  331. outw(value, port);
  332. break;
  333. case 32:
  334. outl(value, port);
  335. break;
  336. default:
  337. BUG();
  338. }
  339. return AE_OK;
  340. }
  341. EXPORT_SYMBOL(acpi_os_write_port);
  342. acpi_status
  343. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  344. {
  345. u32 dummy;
  346. void __iomem *virt_addr;
  347. int iomem = 0;
  348. if (efi_enabled) {
  349. if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
  350. /* HACK ALERT! We can use readb/w/l on real memory too.. */
  351. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  352. } else {
  353. iomem = 1;
  354. virt_addr = ioremap(phys_addr, width);
  355. }
  356. } else
  357. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  358. if (!value)
  359. value = &dummy;
  360. switch (width) {
  361. case 8:
  362. *(u8 *) value = readb(virt_addr);
  363. break;
  364. case 16:
  365. *(u16 *) value = readw(virt_addr);
  366. break;
  367. case 32:
  368. *(u32 *) value = readl(virt_addr);
  369. break;
  370. default:
  371. BUG();
  372. }
  373. if (efi_enabled) {
  374. if (iomem)
  375. iounmap(virt_addr);
  376. }
  377. return AE_OK;
  378. }
  379. acpi_status
  380. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  381. {
  382. void __iomem *virt_addr;
  383. int iomem = 0;
  384. if (efi_enabled) {
  385. if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
  386. /* HACK ALERT! We can use writeb/w/l on real memory too */
  387. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  388. } else {
  389. iomem = 1;
  390. virt_addr = ioremap(phys_addr, width);
  391. }
  392. } else
  393. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  394. switch (width) {
  395. case 8:
  396. writeb(value, virt_addr);
  397. break;
  398. case 16:
  399. writew(value, virt_addr);
  400. break;
  401. case 32:
  402. writel(value, virt_addr);
  403. break;
  404. default:
  405. BUG();
  406. }
  407. if (iomem)
  408. iounmap(virt_addr);
  409. return AE_OK;
  410. }
  411. acpi_status
  412. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  413. void *value, u32 width)
  414. {
  415. int result, size;
  416. if (!value)
  417. return AE_BAD_PARAMETER;
  418. switch (width) {
  419. case 8:
  420. size = 1;
  421. break;
  422. case 16:
  423. size = 2;
  424. break;
  425. case 32:
  426. size = 4;
  427. break;
  428. default:
  429. return AE_ERROR;
  430. }
  431. BUG_ON(!raw_pci_ops);
  432. result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
  433. PCI_DEVFN(pci_id->device, pci_id->function),
  434. reg, size, value);
  435. return (result ? AE_ERROR : AE_OK);
  436. }
  437. EXPORT_SYMBOL(acpi_os_read_pci_configuration);
  438. acpi_status
  439. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  440. acpi_integer value, u32 width)
  441. {
  442. int result, size;
  443. switch (width) {
  444. case 8:
  445. size = 1;
  446. break;
  447. case 16:
  448. size = 2;
  449. break;
  450. case 32:
  451. size = 4;
  452. break;
  453. default:
  454. return AE_ERROR;
  455. }
  456. BUG_ON(!raw_pci_ops);
  457. result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
  458. PCI_DEVFN(pci_id->device, pci_id->function),
  459. reg, size, value);
  460. return (result ? AE_ERROR : AE_OK);
  461. }
  462. /* TODO: Change code to take advantage of driver model more */
  463. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  464. acpi_handle chandle, /* current node */
  465. struct acpi_pci_id **id,
  466. int *is_bridge, u8 * bus_number)
  467. {
  468. acpi_handle handle;
  469. struct acpi_pci_id *pci_id = *id;
  470. acpi_status status;
  471. unsigned long temp;
  472. acpi_object_type type;
  473. u8 tu8;
  474. acpi_get_parent(chandle, &handle);
  475. if (handle != rhandle) {
  476. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  477. bus_number);
  478. status = acpi_get_type(handle, &type);
  479. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  480. return;
  481. status =
  482. acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  483. &temp);
  484. if (ACPI_SUCCESS(status)) {
  485. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  486. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  487. if (*is_bridge)
  488. pci_id->bus = *bus_number;
  489. /* any nicer way to get bus number of bridge ? */
  490. status =
  491. acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
  492. 8);
  493. if (ACPI_SUCCESS(status)
  494. && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
  495. status =
  496. acpi_os_read_pci_configuration(pci_id, 0x18,
  497. &tu8, 8);
  498. if (!ACPI_SUCCESS(status)) {
  499. /* Certainly broken... FIX ME */
  500. return;
  501. }
  502. *is_bridge = 1;
  503. pci_id->bus = tu8;
  504. status =
  505. acpi_os_read_pci_configuration(pci_id, 0x19,
  506. &tu8, 8);
  507. if (ACPI_SUCCESS(status)) {
  508. *bus_number = tu8;
  509. }
  510. } else
  511. *is_bridge = 0;
  512. }
  513. }
  514. }
  515. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  516. acpi_handle chandle, /* current node */
  517. struct acpi_pci_id **id)
  518. {
  519. int is_bridge = 1;
  520. u8 bus_number = (*id)->bus;
  521. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  522. }
  523. static void acpi_os_execute_deferred(void *context)
  524. {
  525. struct acpi_os_dpc *dpc = NULL;
  526. ACPI_FUNCTION_TRACE("os_execute_deferred");
  527. dpc = (struct acpi_os_dpc *)context;
  528. if (!dpc) {
  529. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
  530. return_VOID;
  531. }
  532. dpc->function(dpc->context);
  533. kfree(dpc);
  534. return_VOID;
  535. }
  536. acpi_status
  537. acpi_os_queue_for_execution(u32 priority,
  538. acpi_osd_exec_callback function, void *context)
  539. {
  540. acpi_status status = AE_OK;
  541. struct acpi_os_dpc *dpc;
  542. struct work_struct *task;
  543. ACPI_FUNCTION_TRACE("os_queue_for_execution");
  544. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  545. "Scheduling function [%p(%p)] for deferred execution.\n",
  546. function, context));
  547. if (!function)
  548. return_ACPI_STATUS(AE_BAD_PARAMETER);
  549. /*
  550. * Allocate/initialize DPC structure. Note that this memory will be
  551. * freed by the callee. The kernel handles the tq_struct list in a
  552. * way that allows us to also free its memory inside the callee.
  553. * Because we may want to schedule several tasks with different
  554. * parameters we can't use the approach some kernel code uses of
  555. * having a static tq_struct.
  556. * We can save time and code by allocating the DPC and tq_structs
  557. * from the same memory.
  558. */
  559. dpc =
  560. kmalloc(sizeof(struct acpi_os_dpc) + sizeof(struct work_struct),
  561. GFP_ATOMIC);
  562. if (!dpc)
  563. return_ACPI_STATUS(AE_NO_MEMORY);
  564. dpc->function = function;
  565. dpc->context = context;
  566. task = (void *)(dpc + 1);
  567. INIT_WORK(task, acpi_os_execute_deferred, (void *)dpc);
  568. if (!queue_work(kacpid_wq, task)) {
  569. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  570. "Call to queue_work() failed.\n"));
  571. kfree(dpc);
  572. status = AE_ERROR;
  573. }
  574. return_ACPI_STATUS(status);
  575. }
  576. EXPORT_SYMBOL(acpi_os_queue_for_execution);
  577. void acpi_os_wait_events_complete(void *context)
  578. {
  579. flush_workqueue(kacpid_wq);
  580. }
  581. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  582. /*
  583. * Allocate the memory for a spinlock and initialize it.
  584. */
  585. acpi_status acpi_os_create_lock(acpi_handle * out_handle)
  586. {
  587. spinlock_t *lock_ptr;
  588. ACPI_FUNCTION_TRACE("os_create_lock");
  589. lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
  590. spin_lock_init(lock_ptr);
  591. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
  592. *out_handle = lock_ptr;
  593. return_ACPI_STATUS(AE_OK);
  594. }
  595. /*
  596. * Deallocate the memory for a spinlock.
  597. */
  598. void acpi_os_delete_lock(acpi_handle handle)
  599. {
  600. ACPI_FUNCTION_TRACE("os_create_lock");
  601. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
  602. acpi_os_free(handle);
  603. return_VOID;
  604. }
  605. acpi_status
  606. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  607. {
  608. struct semaphore *sem = NULL;
  609. ACPI_FUNCTION_TRACE("os_create_semaphore");
  610. sem = acpi_os_allocate(sizeof(struct semaphore));
  611. if (!sem)
  612. return_ACPI_STATUS(AE_NO_MEMORY);
  613. memset(sem, 0, sizeof(struct semaphore));
  614. sema_init(sem, initial_units);
  615. *handle = (acpi_handle *) sem;
  616. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  617. *handle, initial_units));
  618. return_ACPI_STATUS(AE_OK);
  619. }
  620. EXPORT_SYMBOL(acpi_os_create_semaphore);
  621. /*
  622. * TODO: A better way to delete semaphores? Linux doesn't have a
  623. * 'delete_semaphore()' function -- may result in an invalid
  624. * pointer dereference for non-synchronized consumers. Should
  625. * we at least check for blocked threads and signal/cancel them?
  626. */
  627. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  628. {
  629. struct semaphore *sem = (struct semaphore *)handle;
  630. ACPI_FUNCTION_TRACE("os_delete_semaphore");
  631. if (!sem)
  632. return_ACPI_STATUS(AE_BAD_PARAMETER);
  633. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  634. acpi_os_free(sem);
  635. sem = NULL;
  636. return_ACPI_STATUS(AE_OK);
  637. }
  638. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  639. /*
  640. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  641. * improvise. The process is to sleep for one scheduler quantum
  642. * until the semaphore becomes available. Downside is that this
  643. * may result in starvation for timeout-based waits when there's
  644. * lots of semaphore activity.
  645. *
  646. * TODO: Support for units > 1?
  647. */
  648. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  649. {
  650. acpi_status status = AE_OK;
  651. struct semaphore *sem = (struct semaphore *)handle;
  652. int ret = 0;
  653. ACPI_FUNCTION_TRACE("os_wait_semaphore");
  654. if (!sem || (units < 1))
  655. return_ACPI_STATUS(AE_BAD_PARAMETER);
  656. if (units > 1)
  657. return_ACPI_STATUS(AE_SUPPORT);
  658. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  659. handle, units, timeout));
  660. if (in_atomic())
  661. timeout = 0;
  662. switch (timeout) {
  663. /*
  664. * No Wait:
  665. * --------
  666. * A zero timeout value indicates that we shouldn't wait - just
  667. * acquire the semaphore if available otherwise return AE_TIME
  668. * (a.k.a. 'would block').
  669. */
  670. case 0:
  671. if (down_trylock(sem))
  672. status = AE_TIME;
  673. break;
  674. /*
  675. * Wait Indefinitely:
  676. * ------------------
  677. */
  678. case ACPI_WAIT_FOREVER:
  679. down(sem);
  680. break;
  681. /*
  682. * Wait w/ Timeout:
  683. * ----------------
  684. */
  685. default:
  686. // TODO: A better timeout algorithm?
  687. {
  688. int i = 0;
  689. static const int quantum_ms = 1000 / HZ;
  690. ret = down_trylock(sem);
  691. for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
  692. schedule_timeout_interruptible(1);
  693. ret = down_trylock(sem);
  694. }
  695. if (ret != 0)
  696. status = AE_TIME;
  697. }
  698. break;
  699. }
  700. if (ACPI_FAILURE(status)) {
  701. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  702. "Failed to acquire semaphore[%p|%d|%d], %s\n",
  703. handle, units, timeout,
  704. acpi_format_exception(status)));
  705. } else {
  706. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  707. "Acquired semaphore[%p|%d|%d]\n", handle,
  708. units, timeout));
  709. }
  710. return_ACPI_STATUS(status);
  711. }
  712. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  713. /*
  714. * TODO: Support for units > 1?
  715. */
  716. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  717. {
  718. struct semaphore *sem = (struct semaphore *)handle;
  719. ACPI_FUNCTION_TRACE("os_signal_semaphore");
  720. if (!sem || (units < 1))
  721. return_ACPI_STATUS(AE_BAD_PARAMETER);
  722. if (units > 1)
  723. return_ACPI_STATUS(AE_SUPPORT);
  724. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  725. units));
  726. up(sem);
  727. return_ACPI_STATUS(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. /* Assumes no unreadable holes inbetween */
  746. u8 acpi_os_readable(void *ptr, acpi_size len)
  747. {
  748. #if defined(__i386__) || defined(__x86_64__)
  749. char tmp;
  750. return !__get_user(tmp, (char __user *)ptr)
  751. && !__get_user(tmp, (char __user *)ptr + len - 1);
  752. #endif
  753. return 1;
  754. }
  755. #ifdef ACPI_FUTURE_USAGE
  756. u8 acpi_os_writable(void *ptr, acpi_size len)
  757. {
  758. /* could do dummy write (racy) or a kernel page table lookup.
  759. The later may be difficult at early boot when kmap doesn't work yet. */
  760. return 1;
  761. }
  762. #endif
  763. u32 acpi_os_get_thread_id(void)
  764. {
  765. if (!in_atomic())
  766. return current->pid;
  767. return 0;
  768. }
  769. acpi_status acpi_os_signal(u32 function, void *info)
  770. {
  771. switch (function) {
  772. case ACPI_SIGNAL_FATAL:
  773. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  774. break;
  775. case ACPI_SIGNAL_BREAKPOINT:
  776. /*
  777. * AML Breakpoint
  778. * ACPI spec. says to treat it as a NOP unless
  779. * you are debugging. So if/when we integrate
  780. * AML debugger into the kernel debugger its
  781. * hook will go here. But until then it is
  782. * not useful to print anything on breakpoints.
  783. */
  784. break;
  785. default:
  786. break;
  787. }
  788. return AE_OK;
  789. }
  790. EXPORT_SYMBOL(acpi_os_signal);
  791. static int __init acpi_os_name_setup(char *str)
  792. {
  793. char *p = acpi_os_name;
  794. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  795. if (!str || !*str)
  796. return 0;
  797. for (; count-- && str && *str; str++) {
  798. if (isalnum(*str) || *str == ' ' || *str == ':')
  799. *p++ = *str;
  800. else if (*str == '\'' || *str == '"')
  801. continue;
  802. else
  803. break;
  804. }
  805. *p = 0;
  806. return 1;
  807. }
  808. __setup("acpi_os_name=", acpi_os_name_setup);
  809. /*
  810. * _OSI control
  811. * empty string disables _OSI
  812. * TBD additional string adds to _OSI
  813. */
  814. static int __init acpi_osi_setup(char *str)
  815. {
  816. if (str == NULL || *str == '\0') {
  817. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  818. acpi_gbl_create_osi_method = FALSE;
  819. } else {
  820. /* TBD */
  821. printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n",
  822. str);
  823. }
  824. return 1;
  825. }
  826. __setup("acpi_osi=", acpi_osi_setup);
  827. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  828. static int __init acpi_serialize_setup(char *str)
  829. {
  830. printk(KERN_INFO PREFIX "serialize enabled\n");
  831. acpi_gbl_all_methods_serialized = TRUE;
  832. return 1;
  833. }
  834. __setup("acpi_serialize", acpi_serialize_setup);
  835. /*
  836. * Wake and Run-Time GPES are expected to be separate.
  837. * We disable wake-GPEs at run-time to prevent spurious
  838. * interrupts.
  839. *
  840. * However, if a system exists that shares Wake and
  841. * Run-time events on the same GPE this flag is available
  842. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  843. */
  844. static int __init acpi_wake_gpes_always_on_setup(char *str)
  845. {
  846. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  847. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  848. return 1;
  849. }
  850. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  851. static int __init acpi_hotkey_setup(char *str)
  852. {
  853. acpi_specific_hotkey_enabled = FALSE;
  854. return 1;
  855. }
  856. __setup("acpi_generic_hotkey", acpi_hotkey_setup);
  857. /*
  858. * max_cstate is defined in the base kernel so modules can
  859. * change it w/o depending on the state of the processor module.
  860. */
  861. unsigned int max_cstate = ACPI_PROCESSOR_MAX_POWER;
  862. EXPORT_SYMBOL(max_cstate);
  863. /*
  864. * Acquire a spinlock.
  865. *
  866. * handle is a pointer to the spinlock_t.
  867. */
  868. acpi_cpu_flags acpi_os_acquire_lock(acpi_handle handle)
  869. {
  870. acpi_cpu_flags flags;
  871. spin_lock_irqsave((spinlock_t *) handle, flags);
  872. return flags;
  873. }
  874. /*
  875. * Release a spinlock. See above.
  876. */
  877. void acpi_os_release_lock(acpi_handle handle, acpi_cpu_flags flags)
  878. {
  879. spin_unlock_irqrestore((spinlock_t *) handle, flags);
  880. }
  881. #ifndef ACPI_USE_LOCAL_CACHE
  882. /*******************************************************************************
  883. *
  884. * FUNCTION: acpi_os_create_cache
  885. *
  886. * PARAMETERS: CacheName - Ascii name for the cache
  887. * ObjectSize - Size of each cached object
  888. * MaxDepth - Maximum depth of the cache (in objects)
  889. * ReturnCache - Where the new cache object is returned
  890. *
  891. * RETURN: Status
  892. *
  893. * DESCRIPTION: Create a cache object
  894. *
  895. ******************************************************************************/
  896. acpi_status
  897. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  898. {
  899. *cache = kmem_cache_create(name, size, 0, 0, NULL, NULL);
  900. return AE_OK;
  901. }
  902. /*******************************************************************************
  903. *
  904. * FUNCTION: acpi_os_purge_cache
  905. *
  906. * PARAMETERS: Cache - Handle to cache object
  907. *
  908. * RETURN: Status
  909. *
  910. * DESCRIPTION: Free all objects within the requested cache.
  911. *
  912. ******************************************************************************/
  913. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  914. {
  915. (void)kmem_cache_shrink(cache);
  916. return (AE_OK);
  917. }
  918. /*******************************************************************************
  919. *
  920. * FUNCTION: acpi_os_delete_cache
  921. *
  922. * PARAMETERS: Cache - Handle to cache object
  923. *
  924. * RETURN: Status
  925. *
  926. * DESCRIPTION: Free all objects within the requested cache and delete the
  927. * cache object.
  928. *
  929. ******************************************************************************/
  930. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  931. {
  932. (void)kmem_cache_destroy(cache);
  933. return (AE_OK);
  934. }
  935. /*******************************************************************************
  936. *
  937. * FUNCTION: acpi_os_release_object
  938. *
  939. * PARAMETERS: Cache - Handle to cache object
  940. * Object - The object to be released
  941. *
  942. * RETURN: None
  943. *
  944. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  945. * the object is deleted.
  946. *
  947. ******************************************************************************/
  948. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  949. {
  950. kmem_cache_free(cache, object);
  951. return (AE_OK);
  952. }
  953. /*******************************************************************************
  954. *
  955. * FUNCTION: acpi_os_acquire_object
  956. *
  957. * PARAMETERS: Cache - Handle to cache object
  958. * ReturnObject - Where the object is returned
  959. *
  960. * RETURN: Status
  961. *
  962. * DESCRIPTION: Get an object from the specified cache. If cache is empty,
  963. * the object is allocated.
  964. *
  965. ******************************************************************************/
  966. void *acpi_os_acquire_object(acpi_cache_t * cache)
  967. {
  968. void *object = kmem_cache_alloc(cache, GFP_KERNEL);
  969. WARN_ON(!object);
  970. return object;
  971. }
  972. #endif