osl.c 30 KB

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