osl.c 30 KB

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