osl.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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. * Copyright (c) 2008 Intel Corporation
  8. * Author: Matthew Wilcox <willy@linux.intel.com>
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pci.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/kmod.h>
  33. #include <linux/delay.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/nmi.h>
  36. #include <linux/acpi.h>
  37. #include <linux/efi.h>
  38. #include <linux/ioport.h>
  39. #include <linux/list.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/semaphore.h>
  42. #include <asm/io.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/io-64-nonatomic-lo-hi.h>
  45. #include "acpica/accommon.h"
  46. #include "acpica/acnamesp.h"
  47. #include "internal.h"
  48. #define _COMPONENT ACPI_OS_SERVICES
  49. ACPI_MODULE_NAME("osl");
  50. struct acpi_os_dpc {
  51. acpi_osd_exec_callback function;
  52. void *context;
  53. struct work_struct work;
  54. };
  55. #ifdef ENABLE_DEBUGGER
  56. #include <linux/kdb.h>
  57. /* stuff for debugger support */
  58. int acpi_in_debugger;
  59. EXPORT_SYMBOL(acpi_in_debugger);
  60. #endif /*ENABLE_DEBUGGER */
  61. static int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
  62. u32 pm1b_ctrl);
  63. static int (*__acpi_os_prepare_extended_sleep)(u8 sleep_state, u32 val_a,
  64. u32 val_b);
  65. static acpi_osd_handler acpi_irq_handler;
  66. static void *acpi_irq_context;
  67. static struct workqueue_struct *kacpid_wq;
  68. static struct workqueue_struct *kacpi_notify_wq;
  69. static struct workqueue_struct *kacpi_hotplug_wq;
  70. static bool acpi_os_initialized;
  71. unsigned int acpi_sci_irq = INVALID_ACPI_IRQ;
  72. bool acpi_permanent_mmap = false;
  73. /*
  74. * This list of permanent mappings is for memory that may be accessed from
  75. * interrupt context, where we can't do the ioremap().
  76. */
  77. struct acpi_ioremap {
  78. struct list_head list;
  79. void __iomem *virt;
  80. acpi_physical_address phys;
  81. acpi_size size;
  82. unsigned long refcount;
  83. };
  84. static LIST_HEAD(acpi_ioremaps);
  85. static DEFINE_MUTEX(acpi_ioremap_lock);
  86. static void __init acpi_request_region (struct acpi_generic_address *gas,
  87. unsigned int length, char *desc)
  88. {
  89. u64 addr;
  90. /* Handle possible alignment issues */
  91. memcpy(&addr, &gas->address, sizeof(addr));
  92. if (!addr || !length)
  93. return;
  94. /* Resources are never freed */
  95. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  96. request_region(addr, length, desc);
  97. else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  98. request_mem_region(addr, length, desc);
  99. }
  100. static int __init acpi_reserve_resources(void)
  101. {
  102. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  103. "ACPI PM1a_EVT_BLK");
  104. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  105. "ACPI PM1b_EVT_BLK");
  106. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  107. "ACPI PM1a_CNT_BLK");
  108. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  109. "ACPI PM1b_CNT_BLK");
  110. if (acpi_gbl_FADT.pm_timer_length == 4)
  111. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  112. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  113. "ACPI PM2_CNT_BLK");
  114. /* Length of GPE blocks must be a non-negative multiple of 2 */
  115. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  116. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  117. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  118. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  119. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  120. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  121. return 0;
  122. }
  123. fs_initcall_sync(acpi_reserve_resources);
  124. void acpi_os_printf(const char *fmt, ...)
  125. {
  126. va_list args;
  127. va_start(args, fmt);
  128. acpi_os_vprintf(fmt, args);
  129. va_end(args);
  130. }
  131. EXPORT_SYMBOL(acpi_os_printf);
  132. void acpi_os_vprintf(const char *fmt, va_list args)
  133. {
  134. static char buffer[512];
  135. vsprintf(buffer, fmt, args);
  136. #ifdef ENABLE_DEBUGGER
  137. if (acpi_in_debugger) {
  138. kdb_printf("%s", buffer);
  139. } else {
  140. if (printk_get_level(buffer))
  141. printk("%s", buffer);
  142. else
  143. printk(KERN_CONT "%s", buffer);
  144. }
  145. #else
  146. if (acpi_debugger_write_log(buffer) < 0) {
  147. if (printk_get_level(buffer))
  148. printk("%s", buffer);
  149. else
  150. printk(KERN_CONT "%s", buffer);
  151. }
  152. #endif
  153. }
  154. #ifdef CONFIG_KEXEC
  155. static unsigned long acpi_rsdp;
  156. static int __init setup_acpi_rsdp(char *arg)
  157. {
  158. return kstrtoul(arg, 16, &acpi_rsdp);
  159. }
  160. early_param("acpi_rsdp", setup_acpi_rsdp);
  161. #endif
  162. acpi_physical_address __init acpi_os_get_root_pointer(void)
  163. {
  164. acpi_physical_address pa;
  165. #ifdef CONFIG_KEXEC
  166. if (acpi_rsdp)
  167. return acpi_rsdp;
  168. #endif
  169. pa = acpi_arch_get_root_pointer();
  170. if (pa)
  171. return pa;
  172. if (efi_enabled(EFI_CONFIG_TABLES)) {
  173. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  174. return efi.acpi20;
  175. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  176. return efi.acpi;
  177. pr_err(PREFIX "System description tables not found\n");
  178. } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP)) {
  179. acpi_find_root_pointer(&pa);
  180. }
  181. return pa;
  182. }
  183. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  184. static struct acpi_ioremap *
  185. acpi_map_lookup(acpi_physical_address phys, acpi_size size)
  186. {
  187. struct acpi_ioremap *map;
  188. list_for_each_entry_rcu(map, &acpi_ioremaps, list)
  189. if (map->phys <= phys &&
  190. phys + size <= map->phys + map->size)
  191. return map;
  192. return NULL;
  193. }
  194. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  195. static void __iomem *
  196. acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
  197. {
  198. struct acpi_ioremap *map;
  199. map = acpi_map_lookup(phys, size);
  200. if (map)
  201. return map->virt + (phys - map->phys);
  202. return NULL;
  203. }
  204. void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
  205. {
  206. struct acpi_ioremap *map;
  207. void __iomem *virt = NULL;
  208. mutex_lock(&acpi_ioremap_lock);
  209. map = acpi_map_lookup(phys, size);
  210. if (map) {
  211. virt = map->virt + (phys - map->phys);
  212. map->refcount++;
  213. }
  214. mutex_unlock(&acpi_ioremap_lock);
  215. return virt;
  216. }
  217. EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
  218. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  219. static struct acpi_ioremap *
  220. acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
  221. {
  222. struct acpi_ioremap *map;
  223. list_for_each_entry_rcu(map, &acpi_ioremaps, list)
  224. if (map->virt <= virt &&
  225. virt + size <= map->virt + map->size)
  226. return map;
  227. return NULL;
  228. }
  229. #if defined(CONFIG_IA64) || defined(CONFIG_ARM64)
  230. /* ioremap will take care of cache attributes */
  231. #define should_use_kmap(pfn) 0
  232. #else
  233. #define should_use_kmap(pfn) page_is_ram(pfn)
  234. #endif
  235. static void __iomem *acpi_map(acpi_physical_address pg_off, unsigned long pg_sz)
  236. {
  237. unsigned long pfn;
  238. pfn = pg_off >> PAGE_SHIFT;
  239. if (should_use_kmap(pfn)) {
  240. if (pg_sz > PAGE_SIZE)
  241. return NULL;
  242. return (void __iomem __force *)kmap(pfn_to_page(pfn));
  243. } else
  244. return acpi_os_ioremap(pg_off, pg_sz);
  245. }
  246. static void acpi_unmap(acpi_physical_address pg_off, void __iomem *vaddr)
  247. {
  248. unsigned long pfn;
  249. pfn = pg_off >> PAGE_SHIFT;
  250. if (should_use_kmap(pfn))
  251. kunmap(pfn_to_page(pfn));
  252. else
  253. iounmap(vaddr);
  254. }
  255. /**
  256. * acpi_os_map_iomem - Get a virtual address for a given physical address range.
  257. * @phys: Start of the physical address range to map.
  258. * @size: Size of the physical address range to map.
  259. *
  260. * Look up the given physical address range in the list of existing ACPI memory
  261. * mappings. If found, get a reference to it and return a pointer to it (its
  262. * virtual address). If not found, map it, add it to that list and return a
  263. * pointer to it.
  264. *
  265. * During early init (when acpi_permanent_mmap has not been set yet) this
  266. * routine simply calls __acpi_map_table() to get the job done.
  267. */
  268. void __iomem *__ref
  269. acpi_os_map_iomem(acpi_physical_address phys, acpi_size size)
  270. {
  271. struct acpi_ioremap *map;
  272. void __iomem *virt;
  273. acpi_physical_address pg_off;
  274. acpi_size pg_sz;
  275. if (phys > ULONG_MAX) {
  276. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  277. return NULL;
  278. }
  279. if (!acpi_permanent_mmap)
  280. return __acpi_map_table((unsigned long)phys, size);
  281. mutex_lock(&acpi_ioremap_lock);
  282. /* Check if there's a suitable mapping already. */
  283. map = acpi_map_lookup(phys, size);
  284. if (map) {
  285. map->refcount++;
  286. goto out;
  287. }
  288. map = kzalloc(sizeof(*map), GFP_KERNEL);
  289. if (!map) {
  290. mutex_unlock(&acpi_ioremap_lock);
  291. return NULL;
  292. }
  293. pg_off = round_down(phys, PAGE_SIZE);
  294. pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
  295. virt = acpi_map(pg_off, pg_sz);
  296. if (!virt) {
  297. mutex_unlock(&acpi_ioremap_lock);
  298. kfree(map);
  299. return NULL;
  300. }
  301. INIT_LIST_HEAD(&map->list);
  302. map->virt = virt;
  303. map->phys = pg_off;
  304. map->size = pg_sz;
  305. map->refcount = 1;
  306. list_add_tail_rcu(&map->list, &acpi_ioremaps);
  307. out:
  308. mutex_unlock(&acpi_ioremap_lock);
  309. return map->virt + (phys - map->phys);
  310. }
  311. EXPORT_SYMBOL_GPL(acpi_os_map_iomem);
  312. void *__ref acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  313. {
  314. return (void *)acpi_os_map_iomem(phys, size);
  315. }
  316. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  317. static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
  318. {
  319. if (!--map->refcount)
  320. list_del_rcu(&map->list);
  321. }
  322. static void acpi_os_map_cleanup(struct acpi_ioremap *map)
  323. {
  324. if (!map->refcount) {
  325. synchronize_rcu_expedited();
  326. acpi_unmap(map->phys, map->virt);
  327. kfree(map);
  328. }
  329. }
  330. /**
  331. * acpi_os_unmap_iomem - Drop a memory mapping reference.
  332. * @virt: Start of the address range to drop a reference to.
  333. * @size: Size of the address range to drop a reference to.
  334. *
  335. * Look up the given virtual address range in the list of existing ACPI memory
  336. * mappings, drop a reference to it and unmap it if there are no more active
  337. * references to it.
  338. *
  339. * During early init (when acpi_permanent_mmap has not been set yet) this
  340. * routine simply calls __acpi_unmap_table() to get the job done. Since
  341. * __acpi_unmap_table() is an __init function, the __ref annotation is needed
  342. * here.
  343. */
  344. void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
  345. {
  346. struct acpi_ioremap *map;
  347. if (!acpi_permanent_mmap) {
  348. __acpi_unmap_table(virt, size);
  349. return;
  350. }
  351. mutex_lock(&acpi_ioremap_lock);
  352. map = acpi_map_lookup_virt(virt, size);
  353. if (!map) {
  354. mutex_unlock(&acpi_ioremap_lock);
  355. WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
  356. return;
  357. }
  358. acpi_os_drop_map_ref(map);
  359. mutex_unlock(&acpi_ioremap_lock);
  360. acpi_os_map_cleanup(map);
  361. }
  362. EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
  363. void __ref acpi_os_unmap_memory(void *virt, acpi_size size)
  364. {
  365. return acpi_os_unmap_iomem((void __iomem *)virt, size);
  366. }
  367. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  368. int acpi_os_map_generic_address(struct acpi_generic_address *gas)
  369. {
  370. u64 addr;
  371. void __iomem *virt;
  372. if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  373. return 0;
  374. /* Handle possible alignment issues */
  375. memcpy(&addr, &gas->address, sizeof(addr));
  376. if (!addr || !gas->bit_width)
  377. return -EINVAL;
  378. virt = acpi_os_map_iomem(addr, gas->bit_width / 8);
  379. if (!virt)
  380. return -EIO;
  381. return 0;
  382. }
  383. EXPORT_SYMBOL(acpi_os_map_generic_address);
  384. void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
  385. {
  386. u64 addr;
  387. struct acpi_ioremap *map;
  388. if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  389. return;
  390. /* Handle possible alignment issues */
  391. memcpy(&addr, &gas->address, sizeof(addr));
  392. if (!addr || !gas->bit_width)
  393. return;
  394. mutex_lock(&acpi_ioremap_lock);
  395. map = acpi_map_lookup(addr, gas->bit_width / 8);
  396. if (!map) {
  397. mutex_unlock(&acpi_ioremap_lock);
  398. return;
  399. }
  400. acpi_os_drop_map_ref(map);
  401. mutex_unlock(&acpi_ioremap_lock);
  402. acpi_os_map_cleanup(map);
  403. }
  404. EXPORT_SYMBOL(acpi_os_unmap_generic_address);
  405. #ifdef ACPI_FUTURE_USAGE
  406. acpi_status
  407. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  408. {
  409. if (!phys || !virt)
  410. return AE_BAD_PARAMETER;
  411. *phys = virt_to_phys(virt);
  412. return AE_OK;
  413. }
  414. #endif
  415. #ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
  416. static bool acpi_rev_override;
  417. int __init acpi_rev_override_setup(char *str)
  418. {
  419. acpi_rev_override = true;
  420. return 1;
  421. }
  422. __setup("acpi_rev_override", acpi_rev_override_setup);
  423. #else
  424. #define acpi_rev_override false
  425. #endif
  426. #define ACPI_MAX_OVERRIDE_LEN 100
  427. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  428. acpi_status
  429. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  430. acpi_string *new_val)
  431. {
  432. if (!init_val || !new_val)
  433. return AE_BAD_PARAMETER;
  434. *new_val = NULL;
  435. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  436. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  437. acpi_os_name);
  438. *new_val = acpi_os_name;
  439. }
  440. if (!memcmp(init_val->name, "_REV", 4) && acpi_rev_override) {
  441. printk(KERN_INFO PREFIX "Overriding _REV return value to 5\n");
  442. *new_val = (char *)5;
  443. }
  444. return AE_OK;
  445. }
  446. static irqreturn_t acpi_irq(int irq, void *dev_id)
  447. {
  448. u32 handled;
  449. handled = (*acpi_irq_handler) (acpi_irq_context);
  450. if (handled) {
  451. acpi_irq_handled++;
  452. return IRQ_HANDLED;
  453. } else {
  454. acpi_irq_not_handled++;
  455. return IRQ_NONE;
  456. }
  457. }
  458. acpi_status
  459. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  460. void *context)
  461. {
  462. unsigned int irq;
  463. acpi_irq_stats_init();
  464. /*
  465. * ACPI interrupts different from the SCI in our copy of the FADT are
  466. * not supported.
  467. */
  468. if (gsi != acpi_gbl_FADT.sci_interrupt)
  469. return AE_BAD_PARAMETER;
  470. if (acpi_irq_handler)
  471. return AE_ALREADY_ACQUIRED;
  472. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  473. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  474. gsi);
  475. return AE_OK;
  476. }
  477. acpi_irq_handler = handler;
  478. acpi_irq_context = context;
  479. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  480. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  481. acpi_irq_handler = NULL;
  482. return AE_NOT_ACQUIRED;
  483. }
  484. acpi_sci_irq = irq;
  485. return AE_OK;
  486. }
  487. acpi_status acpi_os_remove_interrupt_handler(u32 gsi, acpi_osd_handler handler)
  488. {
  489. if (gsi != acpi_gbl_FADT.sci_interrupt || !acpi_sci_irq_valid())
  490. return AE_BAD_PARAMETER;
  491. free_irq(acpi_sci_irq, acpi_irq);
  492. acpi_irq_handler = NULL;
  493. acpi_sci_irq = INVALID_ACPI_IRQ;
  494. return AE_OK;
  495. }
  496. /*
  497. * Running in interpreter thread context, safe to sleep
  498. */
  499. void acpi_os_sleep(u64 ms)
  500. {
  501. msleep(ms);
  502. }
  503. void acpi_os_stall(u32 us)
  504. {
  505. while (us) {
  506. u32 delay = 1000;
  507. if (delay > us)
  508. delay = us;
  509. udelay(delay);
  510. touch_nmi_watchdog();
  511. us -= delay;
  512. }
  513. }
  514. /*
  515. * Support ACPI 3.0 AML Timer operand. Returns a 64-bit free-running,
  516. * monotonically increasing timer with 100ns granularity. Do not use
  517. * ktime_get() to implement this function because this function may get
  518. * called after timekeeping has been suspended. Note: calling this function
  519. * after timekeeping has been suspended may lead to unexpected results
  520. * because when timekeeping is suspended the jiffies counter is not
  521. * incremented. See also timekeeping_suspend().
  522. */
  523. u64 acpi_os_get_timer(void)
  524. {
  525. return (get_jiffies_64() - INITIAL_JIFFIES) *
  526. (ACPI_100NSEC_PER_SEC / HZ);
  527. }
  528. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  529. {
  530. u32 dummy;
  531. if (!value)
  532. value = &dummy;
  533. *value = 0;
  534. if (width <= 8) {
  535. *(u8 *) value = inb(port);
  536. } else if (width <= 16) {
  537. *(u16 *) value = inw(port);
  538. } else if (width <= 32) {
  539. *(u32 *) value = inl(port);
  540. } else {
  541. BUG();
  542. }
  543. return AE_OK;
  544. }
  545. EXPORT_SYMBOL(acpi_os_read_port);
  546. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  547. {
  548. if (width <= 8) {
  549. outb(value, port);
  550. } else if (width <= 16) {
  551. outw(value, port);
  552. } else if (width <= 32) {
  553. outl(value, port);
  554. } else {
  555. BUG();
  556. }
  557. return AE_OK;
  558. }
  559. EXPORT_SYMBOL(acpi_os_write_port);
  560. int acpi_os_read_iomem(void __iomem *virt_addr, u64 *value, u32 width)
  561. {
  562. switch (width) {
  563. case 8:
  564. *(u8 *) value = readb(virt_addr);
  565. break;
  566. case 16:
  567. *(u16 *) value = readw(virt_addr);
  568. break;
  569. case 32:
  570. *(u32 *) value = readl(virt_addr);
  571. break;
  572. case 64:
  573. *(u64 *) value = readq(virt_addr);
  574. break;
  575. default:
  576. return -EINVAL;
  577. }
  578. return 0;
  579. }
  580. acpi_status
  581. acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
  582. {
  583. void __iomem *virt_addr;
  584. unsigned int size = width / 8;
  585. bool unmap = false;
  586. u64 dummy;
  587. int error;
  588. rcu_read_lock();
  589. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  590. if (!virt_addr) {
  591. rcu_read_unlock();
  592. virt_addr = acpi_os_ioremap(phys_addr, size);
  593. if (!virt_addr)
  594. return AE_BAD_ADDRESS;
  595. unmap = true;
  596. }
  597. if (!value)
  598. value = &dummy;
  599. error = acpi_os_read_iomem(virt_addr, value, width);
  600. BUG_ON(error);
  601. if (unmap)
  602. iounmap(virt_addr);
  603. else
  604. rcu_read_unlock();
  605. return AE_OK;
  606. }
  607. acpi_status
  608. acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
  609. {
  610. void __iomem *virt_addr;
  611. unsigned int size = width / 8;
  612. bool unmap = false;
  613. rcu_read_lock();
  614. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  615. if (!virt_addr) {
  616. rcu_read_unlock();
  617. virt_addr = acpi_os_ioremap(phys_addr, size);
  618. if (!virt_addr)
  619. return AE_BAD_ADDRESS;
  620. unmap = true;
  621. }
  622. switch (width) {
  623. case 8:
  624. writeb(value, virt_addr);
  625. break;
  626. case 16:
  627. writew(value, virt_addr);
  628. break;
  629. case 32:
  630. writel(value, virt_addr);
  631. break;
  632. case 64:
  633. writeq(value, virt_addr);
  634. break;
  635. default:
  636. BUG();
  637. }
  638. if (unmap)
  639. iounmap(virt_addr);
  640. else
  641. rcu_read_unlock();
  642. return AE_OK;
  643. }
  644. acpi_status
  645. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  646. u64 *value, u32 width)
  647. {
  648. int result, size;
  649. u32 value32;
  650. if (!value)
  651. return AE_BAD_PARAMETER;
  652. switch (width) {
  653. case 8:
  654. size = 1;
  655. break;
  656. case 16:
  657. size = 2;
  658. break;
  659. case 32:
  660. size = 4;
  661. break;
  662. default:
  663. return AE_ERROR;
  664. }
  665. result = raw_pci_read(pci_id->segment, pci_id->bus,
  666. PCI_DEVFN(pci_id->device, pci_id->function),
  667. reg, size, &value32);
  668. *value = value32;
  669. return (result ? AE_ERROR : AE_OK);
  670. }
  671. acpi_status
  672. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  673. u64 value, u32 width)
  674. {
  675. int result, size;
  676. switch (width) {
  677. case 8:
  678. size = 1;
  679. break;
  680. case 16:
  681. size = 2;
  682. break;
  683. case 32:
  684. size = 4;
  685. break;
  686. default:
  687. return AE_ERROR;
  688. }
  689. result = raw_pci_write(pci_id->segment, pci_id->bus,
  690. PCI_DEVFN(pci_id->device, pci_id->function),
  691. reg, size, value);
  692. return (result ? AE_ERROR : AE_OK);
  693. }
  694. static void acpi_os_execute_deferred(struct work_struct *work)
  695. {
  696. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  697. dpc->function(dpc->context);
  698. kfree(dpc);
  699. }
  700. #ifdef CONFIG_ACPI_DEBUGGER
  701. static struct acpi_debugger acpi_debugger;
  702. static bool acpi_debugger_initialized;
  703. int acpi_register_debugger(struct module *owner,
  704. const struct acpi_debugger_ops *ops)
  705. {
  706. int ret = 0;
  707. mutex_lock(&acpi_debugger.lock);
  708. if (acpi_debugger.ops) {
  709. ret = -EBUSY;
  710. goto err_lock;
  711. }
  712. acpi_debugger.owner = owner;
  713. acpi_debugger.ops = ops;
  714. err_lock:
  715. mutex_unlock(&acpi_debugger.lock);
  716. return ret;
  717. }
  718. EXPORT_SYMBOL(acpi_register_debugger);
  719. void acpi_unregister_debugger(const struct acpi_debugger_ops *ops)
  720. {
  721. mutex_lock(&acpi_debugger.lock);
  722. if (ops == acpi_debugger.ops) {
  723. acpi_debugger.ops = NULL;
  724. acpi_debugger.owner = NULL;
  725. }
  726. mutex_unlock(&acpi_debugger.lock);
  727. }
  728. EXPORT_SYMBOL(acpi_unregister_debugger);
  729. int acpi_debugger_create_thread(acpi_osd_exec_callback function, void *context)
  730. {
  731. int ret;
  732. int (*func)(acpi_osd_exec_callback, void *);
  733. struct module *owner;
  734. if (!acpi_debugger_initialized)
  735. return -ENODEV;
  736. mutex_lock(&acpi_debugger.lock);
  737. if (!acpi_debugger.ops) {
  738. ret = -ENODEV;
  739. goto err_lock;
  740. }
  741. if (!try_module_get(acpi_debugger.owner)) {
  742. ret = -ENODEV;
  743. goto err_lock;
  744. }
  745. func = acpi_debugger.ops->create_thread;
  746. owner = acpi_debugger.owner;
  747. mutex_unlock(&acpi_debugger.lock);
  748. ret = func(function, context);
  749. mutex_lock(&acpi_debugger.lock);
  750. module_put(owner);
  751. err_lock:
  752. mutex_unlock(&acpi_debugger.lock);
  753. return ret;
  754. }
  755. ssize_t acpi_debugger_write_log(const char *msg)
  756. {
  757. ssize_t ret;
  758. ssize_t (*func)(const char *);
  759. struct module *owner;
  760. if (!acpi_debugger_initialized)
  761. return -ENODEV;
  762. mutex_lock(&acpi_debugger.lock);
  763. if (!acpi_debugger.ops) {
  764. ret = -ENODEV;
  765. goto err_lock;
  766. }
  767. if (!try_module_get(acpi_debugger.owner)) {
  768. ret = -ENODEV;
  769. goto err_lock;
  770. }
  771. func = acpi_debugger.ops->write_log;
  772. owner = acpi_debugger.owner;
  773. mutex_unlock(&acpi_debugger.lock);
  774. ret = func(msg);
  775. mutex_lock(&acpi_debugger.lock);
  776. module_put(owner);
  777. err_lock:
  778. mutex_unlock(&acpi_debugger.lock);
  779. return ret;
  780. }
  781. ssize_t acpi_debugger_read_cmd(char *buffer, size_t buffer_length)
  782. {
  783. ssize_t ret;
  784. ssize_t (*func)(char *, size_t);
  785. struct module *owner;
  786. if (!acpi_debugger_initialized)
  787. return -ENODEV;
  788. mutex_lock(&acpi_debugger.lock);
  789. if (!acpi_debugger.ops) {
  790. ret = -ENODEV;
  791. goto err_lock;
  792. }
  793. if (!try_module_get(acpi_debugger.owner)) {
  794. ret = -ENODEV;
  795. goto err_lock;
  796. }
  797. func = acpi_debugger.ops->read_cmd;
  798. owner = acpi_debugger.owner;
  799. mutex_unlock(&acpi_debugger.lock);
  800. ret = func(buffer, buffer_length);
  801. mutex_lock(&acpi_debugger.lock);
  802. module_put(owner);
  803. err_lock:
  804. mutex_unlock(&acpi_debugger.lock);
  805. return ret;
  806. }
  807. int acpi_debugger_wait_command_ready(void)
  808. {
  809. int ret;
  810. int (*func)(bool, char *, size_t);
  811. struct module *owner;
  812. if (!acpi_debugger_initialized)
  813. return -ENODEV;
  814. mutex_lock(&acpi_debugger.lock);
  815. if (!acpi_debugger.ops) {
  816. ret = -ENODEV;
  817. goto err_lock;
  818. }
  819. if (!try_module_get(acpi_debugger.owner)) {
  820. ret = -ENODEV;
  821. goto err_lock;
  822. }
  823. func = acpi_debugger.ops->wait_command_ready;
  824. owner = acpi_debugger.owner;
  825. mutex_unlock(&acpi_debugger.lock);
  826. ret = func(acpi_gbl_method_executing,
  827. acpi_gbl_db_line_buf, ACPI_DB_LINE_BUFFER_SIZE);
  828. mutex_lock(&acpi_debugger.lock);
  829. module_put(owner);
  830. err_lock:
  831. mutex_unlock(&acpi_debugger.lock);
  832. return ret;
  833. }
  834. int acpi_debugger_notify_command_complete(void)
  835. {
  836. int ret;
  837. int (*func)(void);
  838. struct module *owner;
  839. if (!acpi_debugger_initialized)
  840. return -ENODEV;
  841. mutex_lock(&acpi_debugger.lock);
  842. if (!acpi_debugger.ops) {
  843. ret = -ENODEV;
  844. goto err_lock;
  845. }
  846. if (!try_module_get(acpi_debugger.owner)) {
  847. ret = -ENODEV;
  848. goto err_lock;
  849. }
  850. func = acpi_debugger.ops->notify_command_complete;
  851. owner = acpi_debugger.owner;
  852. mutex_unlock(&acpi_debugger.lock);
  853. ret = func();
  854. mutex_lock(&acpi_debugger.lock);
  855. module_put(owner);
  856. err_lock:
  857. mutex_unlock(&acpi_debugger.lock);
  858. return ret;
  859. }
  860. int __init acpi_debugger_init(void)
  861. {
  862. mutex_init(&acpi_debugger.lock);
  863. acpi_debugger_initialized = true;
  864. return 0;
  865. }
  866. #endif
  867. /*******************************************************************************
  868. *
  869. * FUNCTION: acpi_os_execute
  870. *
  871. * PARAMETERS: Type - Type of the callback
  872. * Function - Function to be executed
  873. * Context - Function parameters
  874. *
  875. * RETURN: Status
  876. *
  877. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  878. * immediately executes function on a separate thread.
  879. *
  880. ******************************************************************************/
  881. acpi_status acpi_os_execute(acpi_execute_type type,
  882. acpi_osd_exec_callback function, void *context)
  883. {
  884. acpi_status status = AE_OK;
  885. struct acpi_os_dpc *dpc;
  886. struct workqueue_struct *queue;
  887. int ret;
  888. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  889. "Scheduling function [%p(%p)] for deferred execution.\n",
  890. function, context));
  891. if (type == OSL_DEBUGGER_MAIN_THREAD) {
  892. ret = acpi_debugger_create_thread(function, context);
  893. if (ret) {
  894. pr_err("Call to kthread_create() failed.\n");
  895. status = AE_ERROR;
  896. }
  897. goto out_thread;
  898. }
  899. /*
  900. * Allocate/initialize DPC structure. Note that this memory will be
  901. * freed by the callee. The kernel handles the work_struct list in a
  902. * way that allows us to also free its memory inside the callee.
  903. * Because we may want to schedule several tasks with different
  904. * parameters we can't use the approach some kernel code uses of
  905. * having a static work_struct.
  906. */
  907. dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  908. if (!dpc)
  909. return AE_NO_MEMORY;
  910. dpc->function = function;
  911. dpc->context = context;
  912. /*
  913. * To prevent lockdep from complaining unnecessarily, make sure that
  914. * there is a different static lockdep key for each workqueue by using
  915. * INIT_WORK() for each of them separately.
  916. */
  917. if (type == OSL_NOTIFY_HANDLER) {
  918. queue = kacpi_notify_wq;
  919. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  920. } else if (type == OSL_GPE_HANDLER) {
  921. queue = kacpid_wq;
  922. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  923. } else {
  924. pr_err("Unsupported os_execute type %d.\n", type);
  925. status = AE_ERROR;
  926. }
  927. if (ACPI_FAILURE(status))
  928. goto err_workqueue;
  929. /*
  930. * On some machines, a software-initiated SMI causes corruption unless
  931. * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
  932. * typically it's done in GPE-related methods that are run via
  933. * workqueues, so we can avoid the known corruption cases by always
  934. * queueing on CPU 0.
  935. */
  936. ret = queue_work_on(0, queue, &dpc->work);
  937. if (!ret) {
  938. printk(KERN_ERR PREFIX
  939. "Call to queue_work() failed.\n");
  940. status = AE_ERROR;
  941. }
  942. err_workqueue:
  943. if (ACPI_FAILURE(status))
  944. kfree(dpc);
  945. out_thread:
  946. return status;
  947. }
  948. EXPORT_SYMBOL(acpi_os_execute);
  949. void acpi_os_wait_events_complete(void)
  950. {
  951. /*
  952. * Make sure the GPE handler or the fixed event handler is not used
  953. * on another CPU after removal.
  954. */
  955. if (acpi_sci_irq_valid())
  956. synchronize_hardirq(acpi_sci_irq);
  957. flush_workqueue(kacpid_wq);
  958. flush_workqueue(kacpi_notify_wq);
  959. }
  960. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  961. struct acpi_hp_work {
  962. struct work_struct work;
  963. struct acpi_device *adev;
  964. u32 src;
  965. };
  966. static void acpi_hotplug_work_fn(struct work_struct *work)
  967. {
  968. struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
  969. acpi_os_wait_events_complete();
  970. acpi_device_hotplug(hpw->adev, hpw->src);
  971. kfree(hpw);
  972. }
  973. acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
  974. {
  975. struct acpi_hp_work *hpw;
  976. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  977. "Scheduling hotplug event (%p, %u) for deferred execution.\n",
  978. adev, src));
  979. hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
  980. if (!hpw)
  981. return AE_NO_MEMORY;
  982. INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
  983. hpw->adev = adev;
  984. hpw->src = src;
  985. /*
  986. * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
  987. * the hotplug code may call driver .remove() functions, which may
  988. * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
  989. * these workqueues.
  990. */
  991. if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
  992. kfree(hpw);
  993. return AE_ERROR;
  994. }
  995. return AE_OK;
  996. }
  997. bool acpi_queue_hotplug_work(struct work_struct *work)
  998. {
  999. return queue_work(kacpi_hotplug_wq, work);
  1000. }
  1001. acpi_status
  1002. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  1003. {
  1004. struct semaphore *sem = NULL;
  1005. sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
  1006. if (!sem)
  1007. return AE_NO_MEMORY;
  1008. sema_init(sem, initial_units);
  1009. *handle = (acpi_handle *) sem;
  1010. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  1011. *handle, initial_units));
  1012. return AE_OK;
  1013. }
  1014. /*
  1015. * TODO: A better way to delete semaphores? Linux doesn't have a
  1016. * 'delete_semaphore()' function -- may result in an invalid
  1017. * pointer dereference for non-synchronized consumers. Should
  1018. * we at least check for blocked threads and signal/cancel them?
  1019. */
  1020. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  1021. {
  1022. struct semaphore *sem = (struct semaphore *)handle;
  1023. if (!sem)
  1024. return AE_BAD_PARAMETER;
  1025. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  1026. BUG_ON(!list_empty(&sem->wait_list));
  1027. kfree(sem);
  1028. sem = NULL;
  1029. return AE_OK;
  1030. }
  1031. /*
  1032. * TODO: Support for units > 1?
  1033. */
  1034. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  1035. {
  1036. acpi_status status = AE_OK;
  1037. struct semaphore *sem = (struct semaphore *)handle;
  1038. long jiffies;
  1039. int ret = 0;
  1040. if (!acpi_os_initialized)
  1041. return AE_OK;
  1042. if (!sem || (units < 1))
  1043. return AE_BAD_PARAMETER;
  1044. if (units > 1)
  1045. return AE_SUPPORT;
  1046. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  1047. handle, units, timeout));
  1048. if (timeout == ACPI_WAIT_FOREVER)
  1049. jiffies = MAX_SCHEDULE_TIMEOUT;
  1050. else
  1051. jiffies = msecs_to_jiffies(timeout);
  1052. ret = down_timeout(sem, jiffies);
  1053. if (ret)
  1054. status = AE_TIME;
  1055. if (ACPI_FAILURE(status)) {
  1056. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1057. "Failed to acquire semaphore[%p|%d|%d], %s",
  1058. handle, units, timeout,
  1059. acpi_format_exception(status)));
  1060. } else {
  1061. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1062. "Acquired semaphore[%p|%d|%d]", handle,
  1063. units, timeout));
  1064. }
  1065. return status;
  1066. }
  1067. /*
  1068. * TODO: Support for units > 1?
  1069. */
  1070. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  1071. {
  1072. struct semaphore *sem = (struct semaphore *)handle;
  1073. if (!acpi_os_initialized)
  1074. return AE_OK;
  1075. if (!sem || (units < 1))
  1076. return AE_BAD_PARAMETER;
  1077. if (units > 1)
  1078. return AE_SUPPORT;
  1079. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  1080. units));
  1081. up(sem);
  1082. return AE_OK;
  1083. }
  1084. acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
  1085. {
  1086. #ifdef ENABLE_DEBUGGER
  1087. if (acpi_in_debugger) {
  1088. u32 chars;
  1089. kdb_read(buffer, buffer_length);
  1090. /* remove the CR kdb includes */
  1091. chars = strlen(buffer) - 1;
  1092. buffer[chars] = '\0';
  1093. }
  1094. #else
  1095. int ret;
  1096. ret = acpi_debugger_read_cmd(buffer, buffer_length);
  1097. if (ret < 0)
  1098. return AE_ERROR;
  1099. if (bytes_read)
  1100. *bytes_read = ret;
  1101. #endif
  1102. return AE_OK;
  1103. }
  1104. EXPORT_SYMBOL(acpi_os_get_line);
  1105. acpi_status acpi_os_wait_command_ready(void)
  1106. {
  1107. int ret;
  1108. ret = acpi_debugger_wait_command_ready();
  1109. if (ret < 0)
  1110. return AE_ERROR;
  1111. return AE_OK;
  1112. }
  1113. acpi_status acpi_os_notify_command_complete(void)
  1114. {
  1115. int ret;
  1116. ret = acpi_debugger_notify_command_complete();
  1117. if (ret < 0)
  1118. return AE_ERROR;
  1119. return AE_OK;
  1120. }
  1121. acpi_status acpi_os_signal(u32 function, void *info)
  1122. {
  1123. switch (function) {
  1124. case ACPI_SIGNAL_FATAL:
  1125. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  1126. break;
  1127. case ACPI_SIGNAL_BREAKPOINT:
  1128. /*
  1129. * AML Breakpoint
  1130. * ACPI spec. says to treat it as a NOP unless
  1131. * you are debugging. So if/when we integrate
  1132. * AML debugger into the kernel debugger its
  1133. * hook will go here. But until then it is
  1134. * not useful to print anything on breakpoints.
  1135. */
  1136. break;
  1137. default:
  1138. break;
  1139. }
  1140. return AE_OK;
  1141. }
  1142. static int __init acpi_os_name_setup(char *str)
  1143. {
  1144. char *p = acpi_os_name;
  1145. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  1146. if (!str || !*str)
  1147. return 0;
  1148. for (; count-- && *str; str++) {
  1149. if (isalnum(*str) || *str == ' ' || *str == ':')
  1150. *p++ = *str;
  1151. else if (*str == '\'' || *str == '"')
  1152. continue;
  1153. else
  1154. break;
  1155. }
  1156. *p = 0;
  1157. return 1;
  1158. }
  1159. __setup("acpi_os_name=", acpi_os_name_setup);
  1160. /*
  1161. * Disable the auto-serialization of named objects creation methods.
  1162. *
  1163. * This feature is enabled by default. It marks the AML control methods
  1164. * that contain the opcodes to create named objects as "Serialized".
  1165. */
  1166. static int __init acpi_no_auto_serialize_setup(char *str)
  1167. {
  1168. acpi_gbl_auto_serialize_methods = FALSE;
  1169. pr_info("ACPI: auto-serialization disabled\n");
  1170. return 1;
  1171. }
  1172. __setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
  1173. /* Check of resource interference between native drivers and ACPI
  1174. * OperationRegions (SystemIO and System Memory only).
  1175. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  1176. * in arbitrary AML code and can interfere with legacy drivers.
  1177. * acpi_enforce_resources= can be set to:
  1178. *
  1179. * - strict (default) (2)
  1180. * -> further driver trying to access the resources will not load
  1181. * - lax (1)
  1182. * -> further driver trying to access the resources will load, but you
  1183. * get a system message that something might go wrong...
  1184. *
  1185. * - no (0)
  1186. * -> ACPI Operation Region resources will not be registered
  1187. *
  1188. */
  1189. #define ENFORCE_RESOURCES_STRICT 2
  1190. #define ENFORCE_RESOURCES_LAX 1
  1191. #define ENFORCE_RESOURCES_NO 0
  1192. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1193. static int __init acpi_enforce_resources_setup(char *str)
  1194. {
  1195. if (str == NULL || *str == '\0')
  1196. return 0;
  1197. if (!strcmp("strict", str))
  1198. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1199. else if (!strcmp("lax", str))
  1200. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  1201. else if (!strcmp("no", str))
  1202. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  1203. return 1;
  1204. }
  1205. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  1206. /* Check for resource conflicts between ACPI OperationRegions and native
  1207. * drivers */
  1208. int acpi_check_resource_conflict(const struct resource *res)
  1209. {
  1210. acpi_adr_space_type space_id;
  1211. acpi_size length;
  1212. u8 warn = 0;
  1213. int clash = 0;
  1214. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1215. return 0;
  1216. if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
  1217. return 0;
  1218. if (res->flags & IORESOURCE_IO)
  1219. space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  1220. else
  1221. space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
  1222. length = resource_size(res);
  1223. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO)
  1224. warn = 1;
  1225. clash = acpi_check_address_range(space_id, res->start, length, warn);
  1226. if (clash) {
  1227. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
  1228. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  1229. printk(KERN_NOTICE "ACPI: This conflict may"
  1230. " cause random problems and system"
  1231. " instability\n");
  1232. printk(KERN_INFO "ACPI: If an ACPI driver is available"
  1233. " for this device, you should use it instead of"
  1234. " the native driver\n");
  1235. }
  1236. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  1237. return -EBUSY;
  1238. }
  1239. return 0;
  1240. }
  1241. EXPORT_SYMBOL(acpi_check_resource_conflict);
  1242. int acpi_check_region(resource_size_t start, resource_size_t n,
  1243. const char *name)
  1244. {
  1245. struct resource res = {
  1246. .start = start,
  1247. .end = start + n - 1,
  1248. .name = name,
  1249. .flags = IORESOURCE_IO,
  1250. };
  1251. return acpi_check_resource_conflict(&res);
  1252. }
  1253. EXPORT_SYMBOL(acpi_check_region);
  1254. static acpi_status acpi_deactivate_mem_region(acpi_handle handle, u32 level,
  1255. void *_res, void **return_value)
  1256. {
  1257. struct acpi_mem_space_context **mem_ctx;
  1258. union acpi_operand_object *handler_obj;
  1259. union acpi_operand_object *region_obj2;
  1260. union acpi_operand_object *region_obj;
  1261. struct resource *res = _res;
  1262. acpi_status status;
  1263. region_obj = acpi_ns_get_attached_object(handle);
  1264. if (!region_obj)
  1265. return AE_OK;
  1266. handler_obj = region_obj->region.handler;
  1267. if (!handler_obj)
  1268. return AE_OK;
  1269. if (region_obj->region.space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  1270. return AE_OK;
  1271. if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE))
  1272. return AE_OK;
  1273. region_obj2 = acpi_ns_get_secondary_object(region_obj);
  1274. if (!region_obj2)
  1275. return AE_OK;
  1276. mem_ctx = (void *)&region_obj2->extra.region_context;
  1277. if (!(mem_ctx[0]->address >= res->start &&
  1278. mem_ctx[0]->address < res->end))
  1279. return AE_OK;
  1280. status = handler_obj->address_space.setup(region_obj,
  1281. ACPI_REGION_DEACTIVATE,
  1282. NULL, (void **)mem_ctx);
  1283. if (ACPI_SUCCESS(status))
  1284. region_obj->region.flags &= ~(AOPOBJ_SETUP_COMPLETE);
  1285. return status;
  1286. }
  1287. /**
  1288. * acpi_release_memory - Release any mappings done to a memory region
  1289. * @handle: Handle to namespace node
  1290. * @res: Memory resource
  1291. * @level: A level that terminates the search
  1292. *
  1293. * Walks through @handle and unmaps all SystemMemory Operation Regions that
  1294. * overlap with @res and that have already been activated (mapped).
  1295. *
  1296. * This is a helper that allows drivers to place special requirements on memory
  1297. * region that may overlap with operation regions, primarily allowing them to
  1298. * safely map the region as non-cached memory.
  1299. *
  1300. * The unmapped Operation Regions will be automatically remapped next time they
  1301. * are called, so the drivers do not need to do anything else.
  1302. */
  1303. acpi_status acpi_release_memory(acpi_handle handle, struct resource *res,
  1304. u32 level)
  1305. {
  1306. if (!(res->flags & IORESOURCE_MEM))
  1307. return AE_TYPE;
  1308. return acpi_walk_namespace(ACPI_TYPE_REGION, handle, level,
  1309. acpi_deactivate_mem_region, NULL, res, NULL);
  1310. }
  1311. EXPORT_SYMBOL_GPL(acpi_release_memory);
  1312. /*
  1313. * Let drivers know whether the resource checks are effective
  1314. */
  1315. int acpi_resources_are_enforced(void)
  1316. {
  1317. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  1318. }
  1319. EXPORT_SYMBOL(acpi_resources_are_enforced);
  1320. /*
  1321. * Deallocate the memory for a spinlock.
  1322. */
  1323. void acpi_os_delete_lock(acpi_spinlock handle)
  1324. {
  1325. ACPI_FREE(handle);
  1326. }
  1327. /*
  1328. * Acquire a spinlock.
  1329. *
  1330. * handle is a pointer to the spinlock_t.
  1331. */
  1332. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1333. {
  1334. acpi_cpu_flags flags;
  1335. spin_lock_irqsave(lockp, flags);
  1336. return flags;
  1337. }
  1338. /*
  1339. * Release a spinlock. See above.
  1340. */
  1341. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  1342. {
  1343. spin_unlock_irqrestore(lockp, flags);
  1344. }
  1345. #ifndef ACPI_USE_LOCAL_CACHE
  1346. /*******************************************************************************
  1347. *
  1348. * FUNCTION: acpi_os_create_cache
  1349. *
  1350. * PARAMETERS: name - Ascii name for the cache
  1351. * size - Size of each cached object
  1352. * depth - Maximum depth of the cache (in objects) <ignored>
  1353. * cache - Where the new cache object is returned
  1354. *
  1355. * RETURN: status
  1356. *
  1357. * DESCRIPTION: Create a cache object
  1358. *
  1359. ******************************************************************************/
  1360. acpi_status
  1361. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  1362. {
  1363. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1364. if (*cache == NULL)
  1365. return AE_ERROR;
  1366. else
  1367. return AE_OK;
  1368. }
  1369. /*******************************************************************************
  1370. *
  1371. * FUNCTION: acpi_os_purge_cache
  1372. *
  1373. * PARAMETERS: Cache - Handle to cache object
  1374. *
  1375. * RETURN: Status
  1376. *
  1377. * DESCRIPTION: Free all objects within the requested cache.
  1378. *
  1379. ******************************************************************************/
  1380. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  1381. {
  1382. kmem_cache_shrink(cache);
  1383. return (AE_OK);
  1384. }
  1385. /*******************************************************************************
  1386. *
  1387. * FUNCTION: acpi_os_delete_cache
  1388. *
  1389. * PARAMETERS: Cache - Handle to cache object
  1390. *
  1391. * RETURN: Status
  1392. *
  1393. * DESCRIPTION: Free all objects within the requested cache and delete the
  1394. * cache object.
  1395. *
  1396. ******************************************************************************/
  1397. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  1398. {
  1399. kmem_cache_destroy(cache);
  1400. return (AE_OK);
  1401. }
  1402. /*******************************************************************************
  1403. *
  1404. * FUNCTION: acpi_os_release_object
  1405. *
  1406. * PARAMETERS: Cache - Handle to cache object
  1407. * Object - The object to be released
  1408. *
  1409. * RETURN: None
  1410. *
  1411. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1412. * the object is deleted.
  1413. *
  1414. ******************************************************************************/
  1415. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1416. {
  1417. kmem_cache_free(cache, object);
  1418. return (AE_OK);
  1419. }
  1420. #endif
  1421. static int __init acpi_no_static_ssdt_setup(char *s)
  1422. {
  1423. acpi_gbl_disable_ssdt_table_install = TRUE;
  1424. pr_info("ACPI: static SSDT installation disabled\n");
  1425. return 0;
  1426. }
  1427. early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
  1428. static int __init acpi_disable_return_repair(char *s)
  1429. {
  1430. printk(KERN_NOTICE PREFIX
  1431. "ACPI: Predefined validation mechanism disabled\n");
  1432. acpi_gbl_disable_auto_repair = TRUE;
  1433. return 1;
  1434. }
  1435. __setup("acpica_no_return_repair", acpi_disable_return_repair);
  1436. acpi_status __init acpi_os_initialize(void)
  1437. {
  1438. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1439. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1440. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1441. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1442. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
  1443. /*
  1444. * Use acpi_os_map_generic_address to pre-map the reset
  1445. * register if it's in system memory.
  1446. */
  1447. int rv;
  1448. rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
  1449. pr_debug(PREFIX "%s: map reset_reg status %d\n", __func__, rv);
  1450. }
  1451. acpi_os_initialized = true;
  1452. return AE_OK;
  1453. }
  1454. acpi_status __init acpi_os_initialize1(void)
  1455. {
  1456. kacpid_wq = alloc_workqueue("kacpid", 0, 1);
  1457. kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
  1458. kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
  1459. BUG_ON(!kacpid_wq);
  1460. BUG_ON(!kacpi_notify_wq);
  1461. BUG_ON(!kacpi_hotplug_wq);
  1462. acpi_osi_init();
  1463. return AE_OK;
  1464. }
  1465. acpi_status acpi_os_terminate(void)
  1466. {
  1467. if (acpi_irq_handler) {
  1468. acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
  1469. acpi_irq_handler);
  1470. }
  1471. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1472. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1473. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1474. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1475. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
  1476. acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
  1477. destroy_workqueue(kacpid_wq);
  1478. destroy_workqueue(kacpi_notify_wq);
  1479. destroy_workqueue(kacpi_hotplug_wq);
  1480. return AE_OK;
  1481. }
  1482. acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
  1483. u32 pm1b_control)
  1484. {
  1485. int rc = 0;
  1486. if (__acpi_os_prepare_sleep)
  1487. rc = __acpi_os_prepare_sleep(sleep_state,
  1488. pm1a_control, pm1b_control);
  1489. if (rc < 0)
  1490. return AE_ERROR;
  1491. else if (rc > 0)
  1492. return AE_CTRL_TERMINATE;
  1493. return AE_OK;
  1494. }
  1495. void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
  1496. u32 pm1a_ctrl, u32 pm1b_ctrl))
  1497. {
  1498. __acpi_os_prepare_sleep = func;
  1499. }
  1500. #if (ACPI_REDUCED_HARDWARE)
  1501. acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
  1502. u32 val_b)
  1503. {
  1504. int rc = 0;
  1505. if (__acpi_os_prepare_extended_sleep)
  1506. rc = __acpi_os_prepare_extended_sleep(sleep_state,
  1507. val_a, val_b);
  1508. if (rc < 0)
  1509. return AE_ERROR;
  1510. else if (rc > 0)
  1511. return AE_CTRL_TERMINATE;
  1512. return AE_OK;
  1513. }
  1514. #else
  1515. acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
  1516. u32 val_b)
  1517. {
  1518. return AE_OK;
  1519. }
  1520. #endif
  1521. void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
  1522. u32 val_a, u32 val_b))
  1523. {
  1524. __acpi_os_prepare_extended_sleep = func;
  1525. }
  1526. acpi_status acpi_os_enter_sleep(u8 sleep_state,
  1527. u32 reg_a_value, u32 reg_b_value)
  1528. {
  1529. acpi_status status;
  1530. if (acpi_gbl_reduced_hardware)
  1531. status = acpi_os_prepare_extended_sleep(sleep_state,
  1532. reg_a_value,
  1533. reg_b_value);
  1534. else
  1535. status = acpi_os_prepare_sleep(sleep_state,
  1536. reg_a_value, reg_b_value);
  1537. return status;
  1538. }