osl.c 25 KB

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