osl.c 43 KB

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