osl.c 26 KB

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