osl.c 23 KB

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