osl.c 30 KB

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