osl.c 29 KB

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