osl.c 25 KB

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