osl.c 23 KB

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