osl.c 25 KB

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