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. void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
  180. {
  181. iounmap(virt);
  182. }
  183. #ifdef ACPI_FUTURE_USAGE
  184. acpi_status
  185. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  186. {
  187. if (!phys || !virt)
  188. return AE_BAD_PARAMETER;
  189. *phys = virt_to_phys(virt);
  190. return AE_OK;
  191. }
  192. #endif
  193. #define ACPI_MAX_OVERRIDE_LEN 100
  194. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  195. acpi_status
  196. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  197. acpi_string * new_val)
  198. {
  199. if (!init_val || !new_val)
  200. return AE_BAD_PARAMETER;
  201. *new_val = NULL;
  202. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  203. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  204. acpi_os_name);
  205. *new_val = acpi_os_name;
  206. }
  207. return AE_OK;
  208. }
  209. acpi_status
  210. acpi_os_table_override(struct acpi_table_header * existing_table,
  211. struct acpi_table_header ** new_table)
  212. {
  213. if (!existing_table || !new_table)
  214. return AE_BAD_PARAMETER;
  215. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  216. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  217. *new_table = (struct acpi_table_header *)AmlCode;
  218. else
  219. *new_table = NULL;
  220. #else
  221. *new_table = NULL;
  222. #endif
  223. return AE_OK;
  224. }
  225. static irqreturn_t acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
  226. {
  227. return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
  228. }
  229. acpi_status
  230. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  231. void *context)
  232. {
  233. unsigned int irq;
  234. /*
  235. * Ignore the GSI from the core, and use the value in our copy of the
  236. * FADT. It may not be the same if an interrupt source override exists
  237. * for the SCI.
  238. */
  239. gsi = acpi_fadt.sci_int;
  240. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  241. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  242. gsi);
  243. return AE_OK;
  244. }
  245. acpi_irq_handler = handler;
  246. acpi_irq_context = context;
  247. if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
  248. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  249. return AE_NOT_ACQUIRED;
  250. }
  251. acpi_irq_irq = irq;
  252. return AE_OK;
  253. }
  254. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  255. {
  256. if (irq) {
  257. free_irq(irq, acpi_irq);
  258. acpi_irq_handler = NULL;
  259. acpi_irq_irq = 0;
  260. }
  261. return AE_OK;
  262. }
  263. /*
  264. * Running in interpreter thread context, safe to sleep
  265. */
  266. void acpi_os_sleep(acpi_integer ms)
  267. {
  268. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  269. }
  270. EXPORT_SYMBOL(acpi_os_sleep);
  271. void acpi_os_stall(u32 us)
  272. {
  273. while (us) {
  274. u32 delay = 1000;
  275. if (delay > us)
  276. delay = us;
  277. udelay(delay);
  278. touch_nmi_watchdog();
  279. us -= delay;
  280. }
  281. }
  282. EXPORT_SYMBOL(acpi_os_stall);
  283. /*
  284. * Support ACPI 3.0 AML Timer operand
  285. * Returns 64-bit free-running, monotonically increasing timer
  286. * with 100ns granularity
  287. */
  288. u64 acpi_os_get_timer(void)
  289. {
  290. static u64 t;
  291. #ifdef CONFIG_HPET
  292. /* TBD: use HPET if available */
  293. #endif
  294. #ifdef CONFIG_X86_PM_TIMER
  295. /* TBD: default to PM timer if HPET was not available */
  296. #endif
  297. if (!t)
  298. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  299. return ++t;
  300. }
  301. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  302. {
  303. u32 dummy;
  304. if (!value)
  305. value = &dummy;
  306. switch (width) {
  307. case 8:
  308. *(u8 *) value = inb(port);
  309. break;
  310. case 16:
  311. *(u16 *) value = inw(port);
  312. break;
  313. case 32:
  314. *(u32 *) value = inl(port);
  315. break;
  316. default:
  317. BUG();
  318. }
  319. return AE_OK;
  320. }
  321. EXPORT_SYMBOL(acpi_os_read_port);
  322. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  323. {
  324. switch (width) {
  325. case 8:
  326. outb(value, port);
  327. break;
  328. case 16:
  329. outw(value, port);
  330. break;
  331. case 32:
  332. outl(value, port);
  333. break;
  334. default:
  335. BUG();
  336. }
  337. return AE_OK;
  338. }
  339. EXPORT_SYMBOL(acpi_os_write_port);
  340. acpi_status
  341. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  342. {
  343. u32 dummy;
  344. void __iomem *virt_addr;
  345. int iomem = 0;
  346. if (efi_enabled) {
  347. if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
  348. /* HACK ALERT! We can use readb/w/l on real memory too.. */
  349. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  350. } else {
  351. iomem = 1;
  352. virt_addr = ioremap(phys_addr, width);
  353. }
  354. } else
  355. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  356. if (!value)
  357. value = &dummy;
  358. switch (width) {
  359. case 8:
  360. *(u8 *) value = readb(virt_addr);
  361. break;
  362. case 16:
  363. *(u16 *) value = readw(virt_addr);
  364. break;
  365. case 32:
  366. *(u32 *) value = readl(virt_addr);
  367. break;
  368. default:
  369. BUG();
  370. }
  371. if (efi_enabled) {
  372. if (iomem)
  373. iounmap(virt_addr);
  374. }
  375. return AE_OK;
  376. }
  377. acpi_status
  378. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  379. {
  380. void __iomem *virt_addr;
  381. int iomem = 0;
  382. if (efi_enabled) {
  383. if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
  384. /* HACK ALERT! We can use writeb/w/l on real memory too */
  385. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  386. } else {
  387. iomem = 1;
  388. virt_addr = ioremap(phys_addr, width);
  389. }
  390. } else
  391. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  392. switch (width) {
  393. case 8:
  394. writeb(value, virt_addr);
  395. break;
  396. case 16:
  397. writew(value, virt_addr);
  398. break;
  399. case 32:
  400. writel(value, virt_addr);
  401. break;
  402. default:
  403. BUG();
  404. }
  405. if (iomem)
  406. iounmap(virt_addr);
  407. return AE_OK;
  408. }
  409. acpi_status
  410. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  411. void *value, u32 width)
  412. {
  413. int result, size;
  414. if (!value)
  415. return AE_BAD_PARAMETER;
  416. switch (width) {
  417. case 8:
  418. size = 1;
  419. break;
  420. case 16:
  421. size = 2;
  422. break;
  423. case 32:
  424. size = 4;
  425. break;
  426. default:
  427. return AE_ERROR;
  428. }
  429. BUG_ON(!raw_pci_ops);
  430. result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
  431. PCI_DEVFN(pci_id->device, pci_id->function),
  432. reg, size, value);
  433. return (result ? AE_ERROR : AE_OK);
  434. }
  435. EXPORT_SYMBOL(acpi_os_read_pci_configuration);
  436. acpi_status
  437. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  438. acpi_integer value, u32 width)
  439. {
  440. int result, size;
  441. switch (width) {
  442. case 8:
  443. size = 1;
  444. break;
  445. case 16:
  446. size = 2;
  447. break;
  448. case 32:
  449. size = 4;
  450. break;
  451. default:
  452. return AE_ERROR;
  453. }
  454. BUG_ON(!raw_pci_ops);
  455. result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
  456. PCI_DEVFN(pci_id->device, pci_id->function),
  457. reg, size, value);
  458. return (result ? AE_ERROR : AE_OK);
  459. }
  460. /* TODO: Change code to take advantage of driver model more */
  461. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  462. acpi_handle chandle, /* current node */
  463. struct acpi_pci_id **id,
  464. int *is_bridge, u8 * bus_number)
  465. {
  466. acpi_handle handle;
  467. struct acpi_pci_id *pci_id = *id;
  468. acpi_status status;
  469. unsigned long temp;
  470. acpi_object_type type;
  471. u8 tu8;
  472. acpi_get_parent(chandle, &handle);
  473. if (handle != rhandle) {
  474. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  475. bus_number);
  476. status = acpi_get_type(handle, &type);
  477. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  478. return;
  479. status =
  480. acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  481. &temp);
  482. if (ACPI_SUCCESS(status)) {
  483. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  484. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  485. if (*is_bridge)
  486. pci_id->bus = *bus_number;
  487. /* any nicer way to get bus number of bridge ? */
  488. status =
  489. acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
  490. 8);
  491. if (ACPI_SUCCESS(status)
  492. && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
  493. status =
  494. acpi_os_read_pci_configuration(pci_id, 0x18,
  495. &tu8, 8);
  496. if (!ACPI_SUCCESS(status)) {
  497. /* Certainly broken... FIX ME */
  498. return;
  499. }
  500. *is_bridge = 1;
  501. pci_id->bus = tu8;
  502. status =
  503. acpi_os_read_pci_configuration(pci_id, 0x19,
  504. &tu8, 8);
  505. if (ACPI_SUCCESS(status)) {
  506. *bus_number = tu8;
  507. }
  508. } else
  509. *is_bridge = 0;
  510. }
  511. }
  512. }
  513. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  514. acpi_handle chandle, /* current node */
  515. struct acpi_pci_id **id)
  516. {
  517. int is_bridge = 1;
  518. u8 bus_number = (*id)->bus;
  519. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  520. }
  521. static void acpi_os_execute_deferred(void *context)
  522. {
  523. struct acpi_os_dpc *dpc = NULL;
  524. ACPI_FUNCTION_TRACE("os_execute_deferred");
  525. dpc = (struct acpi_os_dpc *)context;
  526. if (!dpc) {
  527. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
  528. return_VOID;
  529. }
  530. dpc->function(dpc->context);
  531. kfree(dpc);
  532. return_VOID;
  533. }
  534. acpi_status
  535. acpi_os_queue_for_execution(u32 priority,
  536. acpi_osd_exec_callback function, void *context)
  537. {
  538. acpi_status status = AE_OK;
  539. struct acpi_os_dpc *dpc;
  540. struct work_struct *task;
  541. ACPI_FUNCTION_TRACE("os_queue_for_execution");
  542. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  543. "Scheduling function [%p(%p)] for deferred execution.\n",
  544. function, context));
  545. if (!function)
  546. return_ACPI_STATUS(AE_BAD_PARAMETER);
  547. /*
  548. * Allocate/initialize DPC structure. Note that this memory will be
  549. * freed by the callee. The kernel handles the tq_struct list in a
  550. * way that allows us to also free its memory inside the callee.
  551. * Because we may want to schedule several tasks with different
  552. * parameters we can't use the approach some kernel code uses of
  553. * having a static tq_struct.
  554. * We can save time and code by allocating the DPC and tq_structs
  555. * from the same memory.
  556. */
  557. dpc =
  558. kmalloc(sizeof(struct acpi_os_dpc) + sizeof(struct work_struct),
  559. GFP_ATOMIC);
  560. if (!dpc)
  561. return_ACPI_STATUS(AE_NO_MEMORY);
  562. dpc->function = function;
  563. dpc->context = context;
  564. task = (void *)(dpc + 1);
  565. INIT_WORK(task, acpi_os_execute_deferred, (void *)dpc);
  566. if (!queue_work(kacpid_wq, task)) {
  567. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  568. "Call to queue_work() failed.\n"));
  569. kfree(dpc);
  570. status = AE_ERROR;
  571. }
  572. return_ACPI_STATUS(status);
  573. }
  574. EXPORT_SYMBOL(acpi_os_queue_for_execution);
  575. void acpi_os_wait_events_complete(void *context)
  576. {
  577. flush_workqueue(kacpid_wq);
  578. }
  579. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  580. /*
  581. * Allocate the memory for a spinlock and initialize it.
  582. */
  583. acpi_status acpi_os_create_lock(acpi_handle * out_handle)
  584. {
  585. spinlock_t *lock_ptr;
  586. ACPI_FUNCTION_TRACE("os_create_lock");
  587. lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
  588. spin_lock_init(lock_ptr);
  589. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
  590. *out_handle = lock_ptr;
  591. return_ACPI_STATUS(AE_OK);
  592. }
  593. /*
  594. * Deallocate the memory for a spinlock.
  595. */
  596. void acpi_os_delete_lock(acpi_handle handle)
  597. {
  598. ACPI_FUNCTION_TRACE("os_create_lock");
  599. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
  600. acpi_os_free(handle);
  601. return_VOID;
  602. }
  603. acpi_status
  604. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  605. {
  606. struct semaphore *sem = NULL;
  607. ACPI_FUNCTION_TRACE("os_create_semaphore");
  608. sem = acpi_os_allocate(sizeof(struct semaphore));
  609. if (!sem)
  610. return_ACPI_STATUS(AE_NO_MEMORY);
  611. memset(sem, 0, sizeof(struct semaphore));
  612. sema_init(sem, initial_units);
  613. *handle = (acpi_handle *) sem;
  614. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  615. *handle, initial_units));
  616. return_ACPI_STATUS(AE_OK);
  617. }
  618. EXPORT_SYMBOL(acpi_os_create_semaphore);
  619. /*
  620. * TODO: A better way to delete semaphores? Linux doesn't have a
  621. * 'delete_semaphore()' function -- may result in an invalid
  622. * pointer dereference for non-synchronized consumers. Should
  623. * we at least check for blocked threads and signal/cancel them?
  624. */
  625. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  626. {
  627. struct semaphore *sem = (struct semaphore *)handle;
  628. ACPI_FUNCTION_TRACE("os_delete_semaphore");
  629. if (!sem)
  630. return_ACPI_STATUS(AE_BAD_PARAMETER);
  631. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  632. acpi_os_free(sem);
  633. sem = NULL;
  634. return_ACPI_STATUS(AE_OK);
  635. }
  636. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  637. /*
  638. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  639. * improvise. The process is to sleep for one scheduler quantum
  640. * until the semaphore becomes available. Downside is that this
  641. * may result in starvation for timeout-based waits when there's
  642. * lots of semaphore activity.
  643. *
  644. * TODO: Support for units > 1?
  645. */
  646. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  647. {
  648. acpi_status status = AE_OK;
  649. struct semaphore *sem = (struct semaphore *)handle;
  650. int ret = 0;
  651. ACPI_FUNCTION_TRACE("os_wait_semaphore");
  652. if (!sem || (units < 1))
  653. return_ACPI_STATUS(AE_BAD_PARAMETER);
  654. if (units > 1)
  655. return_ACPI_STATUS(AE_SUPPORT);
  656. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  657. handle, units, timeout));
  658. if (in_atomic())
  659. timeout = 0;
  660. switch (timeout) {
  661. /*
  662. * No Wait:
  663. * --------
  664. * A zero timeout value indicates that we shouldn't wait - just
  665. * acquire the semaphore if available otherwise return AE_TIME
  666. * (a.k.a. 'would block').
  667. */
  668. case 0:
  669. if (down_trylock(sem))
  670. status = AE_TIME;
  671. break;
  672. /*
  673. * Wait Indefinitely:
  674. * ------------------
  675. */
  676. case ACPI_WAIT_FOREVER:
  677. down(sem);
  678. break;
  679. /*
  680. * Wait w/ Timeout:
  681. * ----------------
  682. */
  683. default:
  684. // TODO: A better timeout algorithm?
  685. {
  686. int i = 0;
  687. static const int quantum_ms = 1000 / HZ;
  688. ret = down_trylock(sem);
  689. for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
  690. schedule_timeout_interruptible(1);
  691. ret = down_trylock(sem);
  692. }
  693. if (ret != 0)
  694. status = AE_TIME;
  695. }
  696. break;
  697. }
  698. if (ACPI_FAILURE(status)) {
  699. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  700. "Failed to acquire semaphore[%p|%d|%d], %s\n",
  701. handle, units, timeout,
  702. acpi_format_exception(status)));
  703. } else {
  704. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  705. "Acquired semaphore[%p|%d|%d]\n", handle,
  706. units, timeout));
  707. }
  708. return_ACPI_STATUS(status);
  709. }
  710. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  711. /*
  712. * TODO: Support for units > 1?
  713. */
  714. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  715. {
  716. struct semaphore *sem = (struct semaphore *)handle;
  717. ACPI_FUNCTION_TRACE("os_signal_semaphore");
  718. if (!sem || (units < 1))
  719. return_ACPI_STATUS(AE_BAD_PARAMETER);
  720. if (units > 1)
  721. return_ACPI_STATUS(AE_SUPPORT);
  722. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  723. units));
  724. up(sem);
  725. return_ACPI_STATUS(AE_OK);
  726. }
  727. EXPORT_SYMBOL(acpi_os_signal_semaphore);
  728. #ifdef ACPI_FUTURE_USAGE
  729. u32 acpi_os_get_line(char *buffer)
  730. {
  731. #ifdef ENABLE_DEBUGGER
  732. if (acpi_in_debugger) {
  733. u32 chars;
  734. kdb_read(buffer, sizeof(line_buf));
  735. /* remove the CR kdb includes */
  736. chars = strlen(buffer) - 1;
  737. buffer[chars] = '\0';
  738. }
  739. #endif
  740. return 0;
  741. }
  742. #endif /* ACPI_FUTURE_USAGE */
  743. /* Assumes no unreadable holes inbetween */
  744. u8 acpi_os_readable(void *ptr, acpi_size len)
  745. {
  746. #if defined(__i386__) || defined(__x86_64__)
  747. char tmp;
  748. return !__get_user(tmp, (char __user *)ptr)
  749. && !__get_user(tmp, (char __user *)ptr + len - 1);
  750. #endif
  751. return 1;
  752. }
  753. #ifdef ACPI_FUTURE_USAGE
  754. u8 acpi_os_writable(void *ptr, acpi_size len)
  755. {
  756. /* could do dummy write (racy) or a kernel page table lookup.
  757. The later may be difficult at early boot when kmap doesn't work yet. */
  758. return 1;
  759. }
  760. #endif
  761. u32 acpi_os_get_thread_id(void)
  762. {
  763. if (!in_atomic())
  764. return current->pid;
  765. return 0;
  766. }
  767. acpi_status acpi_os_signal(u32 function, void *info)
  768. {
  769. switch (function) {
  770. case ACPI_SIGNAL_FATAL:
  771. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  772. break;
  773. case ACPI_SIGNAL_BREAKPOINT:
  774. /*
  775. * AML Breakpoint
  776. * ACPI spec. says to treat it as a NOP unless
  777. * you are debugging. So if/when we integrate
  778. * AML debugger into the kernel debugger its
  779. * hook will go here. But until then it is
  780. * not useful to print anything on breakpoints.
  781. */
  782. break;
  783. default:
  784. break;
  785. }
  786. return AE_OK;
  787. }
  788. EXPORT_SYMBOL(acpi_os_signal);
  789. static int __init acpi_os_name_setup(char *str)
  790. {
  791. char *p = acpi_os_name;
  792. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  793. if (!str || !*str)
  794. return 0;
  795. for (; count-- && str && *str; str++) {
  796. if (isalnum(*str) || *str == ' ' || *str == ':')
  797. *p++ = *str;
  798. else if (*str == '\'' || *str == '"')
  799. continue;
  800. else
  801. break;
  802. }
  803. *p = 0;
  804. return 1;
  805. }
  806. __setup("acpi_os_name=", acpi_os_name_setup);
  807. /*
  808. * _OSI control
  809. * empty string disables _OSI
  810. * TBD additional string adds to _OSI
  811. */
  812. static int __init acpi_osi_setup(char *str)
  813. {
  814. if (str == NULL || *str == '\0') {
  815. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  816. acpi_gbl_create_osi_method = FALSE;
  817. } else {
  818. /* TBD */
  819. printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n",
  820. str);
  821. }
  822. return 1;
  823. }
  824. __setup("acpi_osi=", acpi_osi_setup);
  825. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  826. static int __init acpi_serialize_setup(char *str)
  827. {
  828. printk(KERN_INFO PREFIX "serialize enabled\n");
  829. acpi_gbl_all_methods_serialized = TRUE;
  830. return 1;
  831. }
  832. __setup("acpi_serialize", acpi_serialize_setup);
  833. /*
  834. * Wake and Run-Time GPES are expected to be separate.
  835. * We disable wake-GPEs at run-time to prevent spurious
  836. * interrupts.
  837. *
  838. * However, if a system exists that shares Wake and
  839. * Run-time events on the same GPE this flag is available
  840. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  841. */
  842. static int __init acpi_wake_gpes_always_on_setup(char *str)
  843. {
  844. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  845. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  846. return 1;
  847. }
  848. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  849. static int __init acpi_hotkey_setup(char *str)
  850. {
  851. acpi_specific_hotkey_enabled = FALSE;
  852. return 1;
  853. }
  854. __setup("acpi_generic_hotkey", acpi_hotkey_setup);
  855. /*
  856. * max_cstate is defined in the base kernel so modules can
  857. * change it w/o depending on the state of the processor module.
  858. */
  859. unsigned int max_cstate = ACPI_PROCESSOR_MAX_POWER;
  860. EXPORT_SYMBOL(max_cstate);
  861. /*
  862. * Acquire a spinlock.
  863. *
  864. * handle is a pointer to the spinlock_t.
  865. * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
  866. * that indicates whether we are at interrupt level.
  867. */
  868. unsigned long acpi_os_acquire_lock(acpi_handle handle)
  869. {
  870. unsigned long 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, unsigned long 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