osl.c 26 KB

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