osl.c 28 KB

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