osl.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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 <asm/uaccess.h>
  44. #include "internal.h"
  45. #define _COMPONENT ACPI_OS_SERVICES
  46. ACPI_MODULE_NAME("osl");
  47. struct acpi_os_dpc {
  48. acpi_osd_exec_callback function;
  49. void *context;
  50. struct work_struct work;
  51. };
  52. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  53. #include CONFIG_ACPI_CUSTOM_DSDT_FILE
  54. #endif
  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. extern char line_buf[80];
  61. #endif /*ENABLE_DEBUGGER */
  62. static int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
  63. u32 pm1b_ctrl);
  64. static int (*__acpi_os_prepare_extended_sleep)(u8 sleep_state, u32 val_a,
  65. u32 val_b);
  66. static acpi_osd_handler acpi_irq_handler;
  67. static void *acpi_irq_context;
  68. static struct workqueue_struct *kacpid_wq;
  69. static struct workqueue_struct *kacpi_notify_wq;
  70. static struct workqueue_struct *kacpi_hotplug_wq;
  71. static bool acpi_os_initialized;
  72. /*
  73. * This list of permanent mappings is for memory that may be accessed from
  74. * interrupt context, where we can't do the ioremap().
  75. */
  76. struct acpi_ioremap {
  77. struct list_head list;
  78. void __iomem *virt;
  79. acpi_physical_address phys;
  80. acpi_size size;
  81. unsigned long refcount;
  82. };
  83. static LIST_HEAD(acpi_ioremaps);
  84. static DEFINE_MUTEX(acpi_ioremap_lock);
  85. static void __init acpi_osi_setup_late(void);
  86. /*
  87. * The story of _OSI(Linux)
  88. *
  89. * From pre-history through Linux-2.6.22,
  90. * Linux responded TRUE upon a BIOS OSI(Linux) query.
  91. *
  92. * Unfortunately, reference BIOS writers got wind of this
  93. * and put OSI(Linux) in their example code, quickly exposing
  94. * this string as ill-conceived and opening the door to
  95. * an un-bounded number of BIOS incompatibilities.
  96. *
  97. * For example, OSI(Linux) was used on resume to re-POST a
  98. * video card on one system, because Linux at that time
  99. * could not do a speedy restore in its native driver.
  100. * But then upon gaining quick native restore capability,
  101. * Linux has no way to tell the BIOS to skip the time-consuming
  102. * POST -- putting Linux at a permanent performance disadvantage.
  103. * On another system, the BIOS writer used OSI(Linux)
  104. * to infer native OS support for IPMI! On other systems,
  105. * OSI(Linux) simply got in the way of Linux claiming to
  106. * be compatible with other operating systems, exposing
  107. * BIOS issues such as skipped device initialization.
  108. *
  109. * So "Linux" turned out to be a really poor chose of
  110. * OSI string, and from Linux-2.6.23 onward we respond FALSE.
  111. *
  112. * BIOS writers should NOT query _OSI(Linux) on future systems.
  113. * Linux will complain on the console when it sees it, and return FALSE.
  114. * To get Linux to return TRUE for your system will require
  115. * a kernel source update to add a DMI entry,
  116. * or boot with "acpi_osi=Linux"
  117. */
  118. static struct osi_linux {
  119. unsigned int enable:1;
  120. unsigned int dmi:1;
  121. unsigned int cmdline:1;
  122. unsigned int default_disabling:1;
  123. } osi_linux = {0, 0, 0, 0};
  124. static u32 acpi_osi_handler(acpi_string interface, u32 supported)
  125. {
  126. if (!strcmp("Linux", interface)) {
  127. printk_once(KERN_NOTICE FW_BUG PREFIX
  128. "BIOS _OSI(Linux) query %s%s\n",
  129. osi_linux.enable ? "honored" : "ignored",
  130. osi_linux.cmdline ? " via cmdline" :
  131. osi_linux.dmi ? " via DMI" : "");
  132. }
  133. if (!strcmp("Darwin", interface)) {
  134. /*
  135. * Apple firmware will behave poorly if it receives positive
  136. * answers to "Darwin" and any other OS. Respond positively
  137. * to Darwin and then disable all other vendor strings.
  138. */
  139. acpi_update_interfaces(ACPI_DISABLE_ALL_VENDOR_STRINGS);
  140. supported = ACPI_UINT32_MAX;
  141. }
  142. return supported;
  143. }
  144. static void __init acpi_request_region (struct acpi_generic_address *gas,
  145. unsigned int length, char *desc)
  146. {
  147. u64 addr;
  148. /* Handle possible alignment issues */
  149. memcpy(&addr, &gas->address, sizeof(addr));
  150. if (!addr || !length)
  151. return;
  152. /* Resources are never freed */
  153. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  154. request_region(addr, length, desc);
  155. else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  156. request_mem_region(addr, length, desc);
  157. }
  158. static int __init acpi_reserve_resources(void)
  159. {
  160. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  161. "ACPI PM1a_EVT_BLK");
  162. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  163. "ACPI PM1b_EVT_BLK");
  164. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  165. "ACPI PM1a_CNT_BLK");
  166. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  167. "ACPI PM1b_CNT_BLK");
  168. if (acpi_gbl_FADT.pm_timer_length == 4)
  169. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  170. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  171. "ACPI PM2_CNT_BLK");
  172. /* Length of GPE blocks must be a non-negative multiple of 2 */
  173. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  174. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  175. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  176. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  177. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  178. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  179. return 0;
  180. }
  181. fs_initcall_sync(acpi_reserve_resources);
  182. void acpi_os_printf(const char *fmt, ...)
  183. {
  184. va_list args;
  185. va_start(args, fmt);
  186. acpi_os_vprintf(fmt, args);
  187. va_end(args);
  188. }
  189. void acpi_os_vprintf(const char *fmt, va_list args)
  190. {
  191. static char buffer[512];
  192. vsprintf(buffer, fmt, args);
  193. #ifdef ENABLE_DEBUGGER
  194. if (acpi_in_debugger) {
  195. kdb_printf("%s", buffer);
  196. } else {
  197. printk(KERN_CONT "%s", buffer);
  198. }
  199. #else
  200. printk(KERN_CONT "%s", buffer);
  201. #endif
  202. }
  203. #ifdef CONFIG_KEXEC
  204. static unsigned long acpi_rsdp;
  205. static int __init setup_acpi_rsdp(char *arg)
  206. {
  207. if (kstrtoul(arg, 16, &acpi_rsdp))
  208. return -EINVAL;
  209. return 0;
  210. }
  211. early_param("acpi_rsdp", setup_acpi_rsdp);
  212. #endif
  213. acpi_physical_address __init acpi_os_get_root_pointer(void)
  214. {
  215. #ifdef CONFIG_KEXEC
  216. if (acpi_rsdp)
  217. return acpi_rsdp;
  218. #endif
  219. if (efi_enabled(EFI_CONFIG_TABLES)) {
  220. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  221. return efi.acpi20;
  222. else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  223. return efi.acpi;
  224. else {
  225. printk(KERN_ERR PREFIX
  226. "System description tables not found\n");
  227. return 0;
  228. }
  229. } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP)) {
  230. acpi_physical_address pa = 0;
  231. acpi_find_root_pointer(&pa);
  232. return pa;
  233. }
  234. return 0;
  235. }
  236. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  237. static struct acpi_ioremap *
  238. acpi_map_lookup(acpi_physical_address phys, acpi_size size)
  239. {
  240. struct acpi_ioremap *map;
  241. list_for_each_entry_rcu(map, &acpi_ioremaps, list)
  242. if (map->phys <= phys &&
  243. phys + size <= map->phys + map->size)
  244. return map;
  245. return NULL;
  246. }
  247. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  248. static void __iomem *
  249. acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
  250. {
  251. struct acpi_ioremap *map;
  252. map = acpi_map_lookup(phys, size);
  253. if (map)
  254. return map->virt + (phys - map->phys);
  255. return NULL;
  256. }
  257. void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
  258. {
  259. struct acpi_ioremap *map;
  260. void __iomem *virt = NULL;
  261. mutex_lock(&acpi_ioremap_lock);
  262. map = acpi_map_lookup(phys, size);
  263. if (map) {
  264. virt = map->virt + (phys - map->phys);
  265. map->refcount++;
  266. }
  267. mutex_unlock(&acpi_ioremap_lock);
  268. return virt;
  269. }
  270. EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
  271. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  272. static struct acpi_ioremap *
  273. acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
  274. {
  275. struct acpi_ioremap *map;
  276. list_for_each_entry_rcu(map, &acpi_ioremaps, list)
  277. if (map->virt <= virt &&
  278. virt + size <= map->virt + map->size)
  279. return map;
  280. return NULL;
  281. }
  282. #if defined(CONFIG_IA64) || defined(CONFIG_ARM64)
  283. /* ioremap will take care of cache attributes */
  284. #define should_use_kmap(pfn) 0
  285. #else
  286. #define should_use_kmap(pfn) page_is_ram(pfn)
  287. #endif
  288. static void __iomem *acpi_map(acpi_physical_address pg_off, unsigned long pg_sz)
  289. {
  290. unsigned long pfn;
  291. pfn = pg_off >> PAGE_SHIFT;
  292. if (should_use_kmap(pfn)) {
  293. if (pg_sz > PAGE_SIZE)
  294. return NULL;
  295. return (void __iomem __force *)kmap(pfn_to_page(pfn));
  296. } else
  297. return acpi_os_ioremap(pg_off, pg_sz);
  298. }
  299. static void acpi_unmap(acpi_physical_address pg_off, void __iomem *vaddr)
  300. {
  301. unsigned long pfn;
  302. pfn = pg_off >> PAGE_SHIFT;
  303. if (should_use_kmap(pfn))
  304. kunmap(pfn_to_page(pfn));
  305. else
  306. iounmap(vaddr);
  307. }
  308. void __iomem *__init_refok
  309. acpi_os_map_iomem(acpi_physical_address phys, acpi_size size)
  310. {
  311. struct acpi_ioremap *map;
  312. void __iomem *virt;
  313. acpi_physical_address pg_off;
  314. acpi_size pg_sz;
  315. if (phys > ULONG_MAX) {
  316. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  317. return NULL;
  318. }
  319. if (!acpi_gbl_permanent_mmap)
  320. return __acpi_map_table((unsigned long)phys, size);
  321. mutex_lock(&acpi_ioremap_lock);
  322. /* Check if there's a suitable mapping already. */
  323. map = acpi_map_lookup(phys, size);
  324. if (map) {
  325. map->refcount++;
  326. goto out;
  327. }
  328. map = kzalloc(sizeof(*map), GFP_KERNEL);
  329. if (!map) {
  330. mutex_unlock(&acpi_ioremap_lock);
  331. return NULL;
  332. }
  333. pg_off = round_down(phys, PAGE_SIZE);
  334. pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
  335. virt = acpi_map(pg_off, pg_sz);
  336. if (!virt) {
  337. mutex_unlock(&acpi_ioremap_lock);
  338. kfree(map);
  339. return NULL;
  340. }
  341. INIT_LIST_HEAD(&map->list);
  342. map->virt = virt;
  343. map->phys = pg_off;
  344. map->size = pg_sz;
  345. map->refcount = 1;
  346. list_add_tail_rcu(&map->list, &acpi_ioremaps);
  347. out:
  348. mutex_unlock(&acpi_ioremap_lock);
  349. return map->virt + (phys - map->phys);
  350. }
  351. EXPORT_SYMBOL_GPL(acpi_os_map_iomem);
  352. void *__init_refok
  353. acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  354. {
  355. return (void *)acpi_os_map_iomem(phys, size);
  356. }
  357. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  358. static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
  359. {
  360. if (!--map->refcount)
  361. list_del_rcu(&map->list);
  362. }
  363. static void acpi_os_map_cleanup(struct acpi_ioremap *map)
  364. {
  365. if (!map->refcount) {
  366. synchronize_rcu_expedited();
  367. acpi_unmap(map->phys, map->virt);
  368. kfree(map);
  369. }
  370. }
  371. void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
  372. {
  373. struct acpi_ioremap *map;
  374. if (!acpi_gbl_permanent_mmap) {
  375. __acpi_unmap_table(virt, size);
  376. return;
  377. }
  378. mutex_lock(&acpi_ioremap_lock);
  379. map = acpi_map_lookup_virt(virt, size);
  380. if (!map) {
  381. mutex_unlock(&acpi_ioremap_lock);
  382. WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
  383. return;
  384. }
  385. acpi_os_drop_map_ref(map);
  386. mutex_unlock(&acpi_ioremap_lock);
  387. acpi_os_map_cleanup(map);
  388. }
  389. EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
  390. void __ref acpi_os_unmap_memory(void *virt, acpi_size size)
  391. {
  392. return acpi_os_unmap_iomem((void __iomem *)virt, size);
  393. }
  394. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  395. void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
  396. {
  397. if (!acpi_gbl_permanent_mmap)
  398. __acpi_unmap_table(virt, size);
  399. }
  400. int acpi_os_map_generic_address(struct acpi_generic_address *gas)
  401. {
  402. u64 addr;
  403. void __iomem *virt;
  404. if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  405. return 0;
  406. /* Handle possible alignment issues */
  407. memcpy(&addr, &gas->address, sizeof(addr));
  408. if (!addr || !gas->bit_width)
  409. return -EINVAL;
  410. virt = acpi_os_map_iomem(addr, gas->bit_width / 8);
  411. if (!virt)
  412. return -EIO;
  413. return 0;
  414. }
  415. EXPORT_SYMBOL(acpi_os_map_generic_address);
  416. void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
  417. {
  418. u64 addr;
  419. struct acpi_ioremap *map;
  420. if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  421. return;
  422. /* Handle possible alignment issues */
  423. memcpy(&addr, &gas->address, sizeof(addr));
  424. if (!addr || !gas->bit_width)
  425. return;
  426. mutex_lock(&acpi_ioremap_lock);
  427. map = acpi_map_lookup(addr, gas->bit_width / 8);
  428. if (!map) {
  429. mutex_unlock(&acpi_ioremap_lock);
  430. return;
  431. }
  432. acpi_os_drop_map_ref(map);
  433. mutex_unlock(&acpi_ioremap_lock);
  434. acpi_os_map_cleanup(map);
  435. }
  436. EXPORT_SYMBOL(acpi_os_unmap_generic_address);
  437. #ifdef ACPI_FUTURE_USAGE
  438. acpi_status
  439. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  440. {
  441. if (!phys || !virt)
  442. return AE_BAD_PARAMETER;
  443. *phys = virt_to_phys(virt);
  444. return AE_OK;
  445. }
  446. #endif
  447. #ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
  448. static bool acpi_rev_override;
  449. int __init acpi_rev_override_setup(char *str)
  450. {
  451. acpi_rev_override = true;
  452. return 1;
  453. }
  454. __setup("acpi_rev_override", acpi_rev_override_setup);
  455. #else
  456. #define acpi_rev_override false
  457. #endif
  458. #define ACPI_MAX_OVERRIDE_LEN 100
  459. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  460. acpi_status
  461. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  462. char **new_val)
  463. {
  464. if (!init_val || !new_val)
  465. return AE_BAD_PARAMETER;
  466. *new_val = NULL;
  467. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  468. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  469. acpi_os_name);
  470. *new_val = acpi_os_name;
  471. }
  472. if (!memcmp(init_val->name, "_REV", 4) && acpi_rev_override) {
  473. printk(KERN_INFO PREFIX "Overriding _REV return value to 5\n");
  474. *new_val = (char *)5;
  475. }
  476. return AE_OK;
  477. }
  478. #ifdef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
  479. #include <linux/earlycpio.h>
  480. #include <linux/memblock.h>
  481. static u64 acpi_tables_addr;
  482. static int all_tables_size;
  483. /* Copied from acpica/tbutils.c:acpi_tb_checksum() */
  484. static u8 __init acpi_table_checksum(u8 *buffer, u32 length)
  485. {
  486. u8 sum = 0;
  487. u8 *end = buffer + length;
  488. while (buffer < end)
  489. sum = (u8) (sum + *(buffer++));
  490. return sum;
  491. }
  492. /* All but ACPI_SIG_RSDP and ACPI_SIG_FACS: */
  493. static const char * const table_sigs[] = {
  494. ACPI_SIG_BERT, ACPI_SIG_CPEP, ACPI_SIG_ECDT, ACPI_SIG_EINJ,
  495. ACPI_SIG_ERST, ACPI_SIG_HEST, ACPI_SIG_MADT, ACPI_SIG_MSCT,
  496. ACPI_SIG_SBST, ACPI_SIG_SLIT, ACPI_SIG_SRAT, ACPI_SIG_ASF,
  497. ACPI_SIG_BOOT, ACPI_SIG_DBGP, ACPI_SIG_DMAR, ACPI_SIG_HPET,
  498. ACPI_SIG_IBFT, ACPI_SIG_IVRS, ACPI_SIG_MCFG, ACPI_SIG_MCHI,
  499. ACPI_SIG_SLIC, ACPI_SIG_SPCR, ACPI_SIG_SPMI, ACPI_SIG_TCPA,
  500. ACPI_SIG_UEFI, ACPI_SIG_WAET, ACPI_SIG_WDAT, ACPI_SIG_WDDT,
  501. ACPI_SIG_WDRT, ACPI_SIG_DSDT, ACPI_SIG_FADT, ACPI_SIG_PSDT,
  502. ACPI_SIG_RSDT, ACPI_SIG_XSDT, ACPI_SIG_SSDT, NULL };
  503. #define ACPI_HEADER_SIZE sizeof(struct acpi_table_header)
  504. #define ACPI_OVERRIDE_TABLES 64
  505. static struct cpio_data __initdata acpi_initrd_files[ACPI_OVERRIDE_TABLES];
  506. #define MAP_CHUNK_SIZE (NR_FIX_BTMAPS << PAGE_SHIFT)
  507. void __init acpi_initrd_override(void *data, size_t size)
  508. {
  509. int sig, no, table_nr = 0, total_offset = 0;
  510. long offset = 0;
  511. struct acpi_table_header *table;
  512. char cpio_path[32] = "kernel/firmware/acpi/";
  513. struct cpio_data file;
  514. if (data == NULL || size == 0)
  515. return;
  516. for (no = 0; no < ACPI_OVERRIDE_TABLES; no++) {
  517. file = find_cpio_data(cpio_path, data, size, &offset);
  518. if (!file.data)
  519. break;
  520. data += offset;
  521. size -= offset;
  522. if (file.size < sizeof(struct acpi_table_header)) {
  523. pr_err("ACPI OVERRIDE: Table smaller than ACPI header [%s%s]\n",
  524. cpio_path, file.name);
  525. continue;
  526. }
  527. table = file.data;
  528. for (sig = 0; table_sigs[sig]; sig++)
  529. if (!memcmp(table->signature, table_sigs[sig], 4))
  530. break;
  531. if (!table_sigs[sig]) {
  532. pr_err("ACPI OVERRIDE: Unknown signature [%s%s]\n",
  533. cpio_path, file.name);
  534. continue;
  535. }
  536. if (file.size != table->length) {
  537. pr_err("ACPI OVERRIDE: File length does not match table length [%s%s]\n",
  538. cpio_path, file.name);
  539. continue;
  540. }
  541. if (acpi_table_checksum(file.data, table->length)) {
  542. pr_err("ACPI OVERRIDE: Bad table checksum [%s%s]\n",
  543. cpio_path, file.name);
  544. continue;
  545. }
  546. pr_info("%4.4s ACPI table found in initrd [%s%s][0x%x]\n",
  547. table->signature, cpio_path, file.name, table->length);
  548. all_tables_size += table->length;
  549. acpi_initrd_files[table_nr].data = file.data;
  550. acpi_initrd_files[table_nr].size = file.size;
  551. table_nr++;
  552. }
  553. if (table_nr == 0)
  554. return;
  555. acpi_tables_addr =
  556. memblock_find_in_range(0, max_low_pfn_mapped << PAGE_SHIFT,
  557. all_tables_size, PAGE_SIZE);
  558. if (!acpi_tables_addr) {
  559. WARN_ON(1);
  560. return;
  561. }
  562. /*
  563. * Only calling e820_add_reserve does not work and the
  564. * tables are invalid (memory got used) later.
  565. * memblock_reserve works as expected and the tables won't get modified.
  566. * But it's not enough on X86 because ioremap will
  567. * complain later (used by acpi_os_map_memory) that the pages
  568. * that should get mapped are not marked "reserved".
  569. * Both memblock_reserve and e820_add_region (via arch_reserve_mem_area)
  570. * works fine.
  571. */
  572. memblock_reserve(acpi_tables_addr, all_tables_size);
  573. arch_reserve_mem_area(acpi_tables_addr, all_tables_size);
  574. /*
  575. * early_ioremap only can remap 256k one time. If we map all
  576. * tables one time, we will hit the limit. Need to map chunks
  577. * one by one during copying the same as that in relocate_initrd().
  578. */
  579. for (no = 0; no < table_nr; no++) {
  580. unsigned char *src_p = acpi_initrd_files[no].data;
  581. phys_addr_t size = acpi_initrd_files[no].size;
  582. phys_addr_t dest_addr = acpi_tables_addr + total_offset;
  583. phys_addr_t slop, clen;
  584. char *dest_p;
  585. total_offset += size;
  586. while (size) {
  587. slop = dest_addr & ~PAGE_MASK;
  588. clen = size;
  589. if (clen > MAP_CHUNK_SIZE - slop)
  590. clen = MAP_CHUNK_SIZE - slop;
  591. dest_p = early_ioremap(dest_addr & PAGE_MASK,
  592. clen + slop);
  593. memcpy(dest_p + slop, src_p, clen);
  594. early_iounmap(dest_p, clen + slop);
  595. src_p += clen;
  596. dest_addr += clen;
  597. size -= clen;
  598. }
  599. }
  600. }
  601. #endif /* CONFIG_ACPI_INITRD_TABLE_OVERRIDE */
  602. static void acpi_table_taint(struct acpi_table_header *table)
  603. {
  604. pr_warn(PREFIX
  605. "Override [%4.4s-%8.8s], this is unsafe: tainting kernel\n",
  606. table->signature, table->oem_table_id);
  607. add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
  608. }
  609. acpi_status
  610. acpi_os_table_override(struct acpi_table_header * existing_table,
  611. struct acpi_table_header ** new_table)
  612. {
  613. if (!existing_table || !new_table)
  614. return AE_BAD_PARAMETER;
  615. *new_table = NULL;
  616. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  617. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  618. *new_table = (struct acpi_table_header *)AmlCode;
  619. #endif
  620. if (*new_table != NULL)
  621. acpi_table_taint(existing_table);
  622. return AE_OK;
  623. }
  624. acpi_status
  625. acpi_os_physical_table_override(struct acpi_table_header *existing_table,
  626. acpi_physical_address *address,
  627. u32 *table_length)
  628. {
  629. #ifndef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
  630. *table_length = 0;
  631. *address = 0;
  632. return AE_OK;
  633. #else
  634. int table_offset = 0;
  635. struct acpi_table_header *table;
  636. *table_length = 0;
  637. *address = 0;
  638. if (!acpi_tables_addr)
  639. return AE_OK;
  640. do {
  641. if (table_offset + ACPI_HEADER_SIZE > all_tables_size) {
  642. WARN_ON(1);
  643. return AE_OK;
  644. }
  645. table = acpi_os_map_memory(acpi_tables_addr + table_offset,
  646. ACPI_HEADER_SIZE);
  647. if (table_offset + table->length > all_tables_size) {
  648. acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
  649. WARN_ON(1);
  650. return AE_OK;
  651. }
  652. table_offset += table->length;
  653. if (memcmp(existing_table->signature, table->signature, 4)) {
  654. acpi_os_unmap_memory(table,
  655. ACPI_HEADER_SIZE);
  656. continue;
  657. }
  658. /* Only override tables with matching oem id */
  659. if (memcmp(table->oem_table_id, existing_table->oem_table_id,
  660. ACPI_OEM_TABLE_ID_SIZE)) {
  661. acpi_os_unmap_memory(table,
  662. ACPI_HEADER_SIZE);
  663. continue;
  664. }
  665. table_offset -= table->length;
  666. *table_length = table->length;
  667. acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
  668. *address = acpi_tables_addr + table_offset;
  669. break;
  670. } while (table_offset + ACPI_HEADER_SIZE < all_tables_size);
  671. if (*address != 0)
  672. acpi_table_taint(existing_table);
  673. return AE_OK;
  674. #endif
  675. }
  676. static irqreturn_t acpi_irq(int irq, void *dev_id)
  677. {
  678. u32 handled;
  679. handled = (*acpi_irq_handler) (acpi_irq_context);
  680. if (handled) {
  681. acpi_irq_handled++;
  682. return IRQ_HANDLED;
  683. } else {
  684. acpi_irq_not_handled++;
  685. return IRQ_NONE;
  686. }
  687. }
  688. acpi_status
  689. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  690. void *context)
  691. {
  692. unsigned int irq;
  693. acpi_irq_stats_init();
  694. /*
  695. * ACPI interrupts different from the SCI in our copy of the FADT are
  696. * not supported.
  697. */
  698. if (gsi != acpi_gbl_FADT.sci_interrupt)
  699. return AE_BAD_PARAMETER;
  700. if (acpi_irq_handler)
  701. return AE_ALREADY_ACQUIRED;
  702. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  703. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  704. gsi);
  705. return AE_OK;
  706. }
  707. acpi_irq_handler = handler;
  708. acpi_irq_context = context;
  709. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  710. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  711. acpi_irq_handler = NULL;
  712. return AE_NOT_ACQUIRED;
  713. }
  714. return AE_OK;
  715. }
  716. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  717. {
  718. if (irq != acpi_gbl_FADT.sci_interrupt)
  719. return AE_BAD_PARAMETER;
  720. free_irq(irq, acpi_irq);
  721. acpi_irq_handler = NULL;
  722. return AE_OK;
  723. }
  724. /*
  725. * Running in interpreter thread context, safe to sleep
  726. */
  727. void acpi_os_sleep(u64 ms)
  728. {
  729. msleep(ms);
  730. }
  731. void acpi_os_stall(u32 us)
  732. {
  733. while (us) {
  734. u32 delay = 1000;
  735. if (delay > us)
  736. delay = us;
  737. udelay(delay);
  738. touch_nmi_watchdog();
  739. us -= delay;
  740. }
  741. }
  742. /*
  743. * Support ACPI 3.0 AML Timer operand
  744. * Returns 64-bit free-running, monotonically increasing timer
  745. * with 100ns granularity
  746. */
  747. u64 acpi_os_get_timer(void)
  748. {
  749. u64 time_ns = ktime_to_ns(ktime_get());
  750. do_div(time_ns, 100);
  751. return time_ns;
  752. }
  753. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  754. {
  755. u32 dummy;
  756. if (!value)
  757. value = &dummy;
  758. *value = 0;
  759. if (width <= 8) {
  760. *(u8 *) value = inb(port);
  761. } else if (width <= 16) {
  762. *(u16 *) value = inw(port);
  763. } else if (width <= 32) {
  764. *(u32 *) value = inl(port);
  765. } else {
  766. BUG();
  767. }
  768. return AE_OK;
  769. }
  770. EXPORT_SYMBOL(acpi_os_read_port);
  771. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  772. {
  773. if (width <= 8) {
  774. outb(value, port);
  775. } else if (width <= 16) {
  776. outw(value, port);
  777. } else if (width <= 32) {
  778. outl(value, port);
  779. } else {
  780. BUG();
  781. }
  782. return AE_OK;
  783. }
  784. EXPORT_SYMBOL(acpi_os_write_port);
  785. #ifdef readq
  786. static inline u64 read64(const volatile void __iomem *addr)
  787. {
  788. return readq(addr);
  789. }
  790. #else
  791. static inline u64 read64(const volatile void __iomem *addr)
  792. {
  793. u64 l, h;
  794. l = readl(addr);
  795. h = readl(addr+4);
  796. return l | (h << 32);
  797. }
  798. #endif
  799. acpi_status
  800. acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
  801. {
  802. void __iomem *virt_addr;
  803. unsigned int size = width / 8;
  804. bool unmap = false;
  805. u64 dummy;
  806. rcu_read_lock();
  807. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  808. if (!virt_addr) {
  809. rcu_read_unlock();
  810. virt_addr = acpi_os_ioremap(phys_addr, size);
  811. if (!virt_addr)
  812. return AE_BAD_ADDRESS;
  813. unmap = true;
  814. }
  815. if (!value)
  816. value = &dummy;
  817. switch (width) {
  818. case 8:
  819. *(u8 *) value = readb(virt_addr);
  820. break;
  821. case 16:
  822. *(u16 *) value = readw(virt_addr);
  823. break;
  824. case 32:
  825. *(u32 *) value = readl(virt_addr);
  826. break;
  827. case 64:
  828. *(u64 *) value = read64(virt_addr);
  829. break;
  830. default:
  831. BUG();
  832. }
  833. if (unmap)
  834. iounmap(virt_addr);
  835. else
  836. rcu_read_unlock();
  837. return AE_OK;
  838. }
  839. #ifdef writeq
  840. static inline void write64(u64 val, volatile void __iomem *addr)
  841. {
  842. writeq(val, addr);
  843. }
  844. #else
  845. static inline void write64(u64 val, volatile void __iomem *addr)
  846. {
  847. writel(val, addr);
  848. writel(val>>32, addr+4);
  849. }
  850. #endif
  851. acpi_status
  852. acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
  853. {
  854. void __iomem *virt_addr;
  855. unsigned int size = width / 8;
  856. bool unmap = false;
  857. rcu_read_lock();
  858. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  859. if (!virt_addr) {
  860. rcu_read_unlock();
  861. virt_addr = acpi_os_ioremap(phys_addr, size);
  862. if (!virt_addr)
  863. return AE_BAD_ADDRESS;
  864. unmap = true;
  865. }
  866. switch (width) {
  867. case 8:
  868. writeb(value, virt_addr);
  869. break;
  870. case 16:
  871. writew(value, virt_addr);
  872. break;
  873. case 32:
  874. writel(value, virt_addr);
  875. break;
  876. case 64:
  877. write64(value, virt_addr);
  878. break;
  879. default:
  880. BUG();
  881. }
  882. if (unmap)
  883. iounmap(virt_addr);
  884. else
  885. rcu_read_unlock();
  886. return AE_OK;
  887. }
  888. acpi_status
  889. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  890. u64 *value, u32 width)
  891. {
  892. int result, size;
  893. u32 value32;
  894. if (!value)
  895. return AE_BAD_PARAMETER;
  896. switch (width) {
  897. case 8:
  898. size = 1;
  899. break;
  900. case 16:
  901. size = 2;
  902. break;
  903. case 32:
  904. size = 4;
  905. break;
  906. default:
  907. return AE_ERROR;
  908. }
  909. result = raw_pci_read(pci_id->segment, pci_id->bus,
  910. PCI_DEVFN(pci_id->device, pci_id->function),
  911. reg, size, &value32);
  912. *value = value32;
  913. return (result ? AE_ERROR : AE_OK);
  914. }
  915. acpi_status
  916. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  917. u64 value, u32 width)
  918. {
  919. int result, size;
  920. switch (width) {
  921. case 8:
  922. size = 1;
  923. break;
  924. case 16:
  925. size = 2;
  926. break;
  927. case 32:
  928. size = 4;
  929. break;
  930. default:
  931. return AE_ERROR;
  932. }
  933. result = raw_pci_write(pci_id->segment, pci_id->bus,
  934. PCI_DEVFN(pci_id->device, pci_id->function),
  935. reg, size, value);
  936. return (result ? AE_ERROR : AE_OK);
  937. }
  938. static void acpi_os_execute_deferred(struct work_struct *work)
  939. {
  940. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  941. dpc->function(dpc->context);
  942. kfree(dpc);
  943. }
  944. /*******************************************************************************
  945. *
  946. * FUNCTION: acpi_os_execute
  947. *
  948. * PARAMETERS: Type - Type of the callback
  949. * Function - Function to be executed
  950. * Context - Function parameters
  951. *
  952. * RETURN: Status
  953. *
  954. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  955. * immediately executes function on a separate thread.
  956. *
  957. ******************************************************************************/
  958. acpi_status acpi_os_execute(acpi_execute_type type,
  959. acpi_osd_exec_callback function, void *context)
  960. {
  961. acpi_status status = AE_OK;
  962. struct acpi_os_dpc *dpc;
  963. struct workqueue_struct *queue;
  964. int ret;
  965. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  966. "Scheduling function [%p(%p)] for deferred execution.\n",
  967. function, context));
  968. /*
  969. * Allocate/initialize DPC structure. Note that this memory will be
  970. * freed by the callee. The kernel handles the work_struct list in a
  971. * way that allows us to also free its memory inside the callee.
  972. * Because we may want to schedule several tasks with different
  973. * parameters we can't use the approach some kernel code uses of
  974. * having a static work_struct.
  975. */
  976. dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  977. if (!dpc)
  978. return AE_NO_MEMORY;
  979. dpc->function = function;
  980. dpc->context = context;
  981. /*
  982. * To prevent lockdep from complaining unnecessarily, make sure that
  983. * there is a different static lockdep key for each workqueue by using
  984. * INIT_WORK() for each of them separately.
  985. */
  986. if (type == OSL_NOTIFY_HANDLER) {
  987. queue = kacpi_notify_wq;
  988. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  989. } else {
  990. queue = kacpid_wq;
  991. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  992. }
  993. /*
  994. * On some machines, a software-initiated SMI causes corruption unless
  995. * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
  996. * typically it's done in GPE-related methods that are run via
  997. * workqueues, so we can avoid the known corruption cases by always
  998. * queueing on CPU 0.
  999. */
  1000. ret = queue_work_on(0, queue, &dpc->work);
  1001. if (!ret) {
  1002. printk(KERN_ERR PREFIX
  1003. "Call to queue_work() failed.\n");
  1004. status = AE_ERROR;
  1005. kfree(dpc);
  1006. }
  1007. return status;
  1008. }
  1009. EXPORT_SYMBOL(acpi_os_execute);
  1010. void acpi_os_wait_events_complete(void)
  1011. {
  1012. /*
  1013. * Make sure the GPE handler or the fixed event handler is not used
  1014. * on another CPU after removal.
  1015. */
  1016. if (acpi_irq_handler)
  1017. synchronize_hardirq(acpi_gbl_FADT.sci_interrupt);
  1018. flush_workqueue(kacpid_wq);
  1019. flush_workqueue(kacpi_notify_wq);
  1020. }
  1021. struct acpi_hp_work {
  1022. struct work_struct work;
  1023. struct acpi_device *adev;
  1024. u32 src;
  1025. };
  1026. static void acpi_hotplug_work_fn(struct work_struct *work)
  1027. {
  1028. struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
  1029. acpi_os_wait_events_complete();
  1030. acpi_device_hotplug(hpw->adev, hpw->src);
  1031. kfree(hpw);
  1032. }
  1033. acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
  1034. {
  1035. struct acpi_hp_work *hpw;
  1036. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  1037. "Scheduling hotplug event (%p, %u) for deferred execution.\n",
  1038. adev, src));
  1039. hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
  1040. if (!hpw)
  1041. return AE_NO_MEMORY;
  1042. INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
  1043. hpw->adev = adev;
  1044. hpw->src = src;
  1045. /*
  1046. * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
  1047. * the hotplug code may call driver .remove() functions, which may
  1048. * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
  1049. * these workqueues.
  1050. */
  1051. if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
  1052. kfree(hpw);
  1053. return AE_ERROR;
  1054. }
  1055. return AE_OK;
  1056. }
  1057. bool acpi_queue_hotplug_work(struct work_struct *work)
  1058. {
  1059. return queue_work(kacpi_hotplug_wq, work);
  1060. }
  1061. acpi_status
  1062. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  1063. {
  1064. struct semaphore *sem = NULL;
  1065. sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
  1066. if (!sem)
  1067. return AE_NO_MEMORY;
  1068. sema_init(sem, initial_units);
  1069. *handle = (acpi_handle *) sem;
  1070. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  1071. *handle, initial_units));
  1072. return AE_OK;
  1073. }
  1074. /*
  1075. * TODO: A better way to delete semaphores? Linux doesn't have a
  1076. * 'delete_semaphore()' function -- may result in an invalid
  1077. * pointer dereference for non-synchronized consumers. Should
  1078. * we at least check for blocked threads and signal/cancel them?
  1079. */
  1080. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  1081. {
  1082. struct semaphore *sem = (struct semaphore *)handle;
  1083. if (!sem)
  1084. return AE_BAD_PARAMETER;
  1085. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  1086. BUG_ON(!list_empty(&sem->wait_list));
  1087. kfree(sem);
  1088. sem = NULL;
  1089. return AE_OK;
  1090. }
  1091. /*
  1092. * TODO: Support for units > 1?
  1093. */
  1094. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  1095. {
  1096. acpi_status status = AE_OK;
  1097. struct semaphore *sem = (struct semaphore *)handle;
  1098. long jiffies;
  1099. int ret = 0;
  1100. if (!acpi_os_initialized)
  1101. return AE_OK;
  1102. if (!sem || (units < 1))
  1103. return AE_BAD_PARAMETER;
  1104. if (units > 1)
  1105. return AE_SUPPORT;
  1106. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  1107. handle, units, timeout));
  1108. if (timeout == ACPI_WAIT_FOREVER)
  1109. jiffies = MAX_SCHEDULE_TIMEOUT;
  1110. else
  1111. jiffies = msecs_to_jiffies(timeout);
  1112. ret = down_timeout(sem, jiffies);
  1113. if (ret)
  1114. status = AE_TIME;
  1115. if (ACPI_FAILURE(status)) {
  1116. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1117. "Failed to acquire semaphore[%p|%d|%d], %s",
  1118. handle, units, timeout,
  1119. acpi_format_exception(status)));
  1120. } else {
  1121. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1122. "Acquired semaphore[%p|%d|%d]", handle,
  1123. units, timeout));
  1124. }
  1125. return status;
  1126. }
  1127. /*
  1128. * TODO: Support for units > 1?
  1129. */
  1130. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  1131. {
  1132. struct semaphore *sem = (struct semaphore *)handle;
  1133. if (!acpi_os_initialized)
  1134. return AE_OK;
  1135. if (!sem || (units < 1))
  1136. return AE_BAD_PARAMETER;
  1137. if (units > 1)
  1138. return AE_SUPPORT;
  1139. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  1140. units));
  1141. up(sem);
  1142. return AE_OK;
  1143. }
  1144. #ifdef ACPI_FUTURE_USAGE
  1145. u32 acpi_os_get_line(char *buffer)
  1146. {
  1147. #ifdef ENABLE_DEBUGGER
  1148. if (acpi_in_debugger) {
  1149. u32 chars;
  1150. kdb_read(buffer, sizeof(line_buf));
  1151. /* remove the CR kdb includes */
  1152. chars = strlen(buffer) - 1;
  1153. buffer[chars] = '\0';
  1154. }
  1155. #endif
  1156. return 0;
  1157. }
  1158. #endif /* ACPI_FUTURE_USAGE */
  1159. acpi_status acpi_os_signal(u32 function, void *info)
  1160. {
  1161. switch (function) {
  1162. case ACPI_SIGNAL_FATAL:
  1163. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  1164. break;
  1165. case ACPI_SIGNAL_BREAKPOINT:
  1166. /*
  1167. * AML Breakpoint
  1168. * ACPI spec. says to treat it as a NOP unless
  1169. * you are debugging. So if/when we integrate
  1170. * AML debugger into the kernel debugger its
  1171. * hook will go here. But until then it is
  1172. * not useful to print anything on breakpoints.
  1173. */
  1174. break;
  1175. default:
  1176. break;
  1177. }
  1178. return AE_OK;
  1179. }
  1180. static int __init acpi_os_name_setup(char *str)
  1181. {
  1182. char *p = acpi_os_name;
  1183. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  1184. if (!str || !*str)
  1185. return 0;
  1186. for (; count-- && *str; str++) {
  1187. if (isalnum(*str) || *str == ' ' || *str == ':')
  1188. *p++ = *str;
  1189. else if (*str == '\'' || *str == '"')
  1190. continue;
  1191. else
  1192. break;
  1193. }
  1194. *p = 0;
  1195. return 1;
  1196. }
  1197. __setup("acpi_os_name=", acpi_os_name_setup);
  1198. #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
  1199. #define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
  1200. struct osi_setup_entry {
  1201. char string[OSI_STRING_LENGTH_MAX];
  1202. bool enable;
  1203. };
  1204. static struct osi_setup_entry
  1205. osi_setup_entries[OSI_STRING_ENTRIES_MAX] __initdata = {
  1206. {"Module Device", true},
  1207. {"Processor Device", true},
  1208. {"3.0 _SCP Extensions", true},
  1209. {"Processor Aggregator Device", true},
  1210. };
  1211. void __init acpi_osi_setup(char *str)
  1212. {
  1213. struct osi_setup_entry *osi;
  1214. bool enable = true;
  1215. int i;
  1216. if (!acpi_gbl_create_osi_method)
  1217. return;
  1218. if (str == NULL || *str == '\0') {
  1219. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  1220. acpi_gbl_create_osi_method = FALSE;
  1221. return;
  1222. }
  1223. if (*str == '!') {
  1224. str++;
  1225. if (*str == '\0') {
  1226. osi_linux.default_disabling = 1;
  1227. return;
  1228. } else if (*str == '*') {
  1229. acpi_update_interfaces(ACPI_DISABLE_ALL_STRINGS);
  1230. for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
  1231. osi = &osi_setup_entries[i];
  1232. osi->enable = false;
  1233. }
  1234. return;
  1235. }
  1236. enable = false;
  1237. }
  1238. for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
  1239. osi = &osi_setup_entries[i];
  1240. if (!strcmp(osi->string, str)) {
  1241. osi->enable = enable;
  1242. break;
  1243. } else if (osi->string[0] == '\0') {
  1244. osi->enable = enable;
  1245. strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
  1246. break;
  1247. }
  1248. }
  1249. }
  1250. static void __init set_osi_linux(unsigned int enable)
  1251. {
  1252. if (osi_linux.enable != enable)
  1253. osi_linux.enable = enable;
  1254. if (osi_linux.enable)
  1255. acpi_osi_setup("Linux");
  1256. else
  1257. acpi_osi_setup("!Linux");
  1258. return;
  1259. }
  1260. static void __init acpi_cmdline_osi_linux(unsigned int enable)
  1261. {
  1262. osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
  1263. osi_linux.dmi = 0;
  1264. set_osi_linux(enable);
  1265. return;
  1266. }
  1267. void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
  1268. {
  1269. printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
  1270. if (enable == -1)
  1271. return;
  1272. osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
  1273. set_osi_linux(enable);
  1274. return;
  1275. }
  1276. /*
  1277. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  1278. *
  1279. * empty string disables _OSI
  1280. * string starting with '!' disables that string
  1281. * otherwise string is added to list, augmenting built-in strings
  1282. */
  1283. static void __init acpi_osi_setup_late(void)
  1284. {
  1285. struct osi_setup_entry *osi;
  1286. char *str;
  1287. int i;
  1288. acpi_status status;
  1289. if (osi_linux.default_disabling) {
  1290. status = acpi_update_interfaces(ACPI_DISABLE_ALL_VENDOR_STRINGS);
  1291. if (ACPI_SUCCESS(status))
  1292. printk(KERN_INFO PREFIX "Disabled all _OSI OS vendors\n");
  1293. }
  1294. for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
  1295. osi = &osi_setup_entries[i];
  1296. str = osi->string;
  1297. if (*str == '\0')
  1298. break;
  1299. if (osi->enable) {
  1300. status = acpi_install_interface(str);
  1301. if (ACPI_SUCCESS(status))
  1302. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  1303. } else {
  1304. status = acpi_remove_interface(str);
  1305. if (ACPI_SUCCESS(status))
  1306. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  1307. }
  1308. }
  1309. }
  1310. static int __init osi_setup(char *str)
  1311. {
  1312. if (str && !strcmp("Linux", str))
  1313. acpi_cmdline_osi_linux(1);
  1314. else if (str && !strcmp("!Linux", str))
  1315. acpi_cmdline_osi_linux(0);
  1316. else
  1317. acpi_osi_setup(str);
  1318. return 1;
  1319. }
  1320. __setup("acpi_osi=", osi_setup);
  1321. /*
  1322. * Disable the auto-serialization of named objects creation methods.
  1323. *
  1324. * This feature is enabled by default. It marks the AML control methods
  1325. * that contain the opcodes to create named objects as "Serialized".
  1326. */
  1327. static int __init acpi_no_auto_serialize_setup(char *str)
  1328. {
  1329. acpi_gbl_auto_serialize_methods = FALSE;
  1330. pr_info("ACPI: auto-serialization disabled\n");
  1331. return 1;
  1332. }
  1333. __setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
  1334. /* Check of resource interference between native drivers and ACPI
  1335. * OperationRegions (SystemIO and System Memory only).
  1336. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  1337. * in arbitrary AML code and can interfere with legacy drivers.
  1338. * acpi_enforce_resources= can be set to:
  1339. *
  1340. * - strict (default) (2)
  1341. * -> further driver trying to access the resources will not load
  1342. * - lax (1)
  1343. * -> further driver trying to access the resources will load, but you
  1344. * get a system message that something might go wrong...
  1345. *
  1346. * - no (0)
  1347. * -> ACPI Operation Region resources will not be registered
  1348. *
  1349. */
  1350. #define ENFORCE_RESOURCES_STRICT 2
  1351. #define ENFORCE_RESOURCES_LAX 1
  1352. #define ENFORCE_RESOURCES_NO 0
  1353. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1354. static int __init acpi_enforce_resources_setup(char *str)
  1355. {
  1356. if (str == NULL || *str == '\0')
  1357. return 0;
  1358. if (!strcmp("strict", str))
  1359. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1360. else if (!strcmp("lax", str))
  1361. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  1362. else if (!strcmp("no", str))
  1363. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  1364. return 1;
  1365. }
  1366. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  1367. /* Check for resource conflicts between ACPI OperationRegions and native
  1368. * drivers */
  1369. int acpi_check_resource_conflict(const struct resource *res)
  1370. {
  1371. acpi_adr_space_type space_id;
  1372. acpi_size length;
  1373. u8 warn = 0;
  1374. int clash = 0;
  1375. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1376. return 0;
  1377. if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
  1378. return 0;
  1379. if (res->flags & IORESOURCE_IO)
  1380. space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  1381. else
  1382. space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
  1383. length = resource_size(res);
  1384. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO)
  1385. warn = 1;
  1386. clash = acpi_check_address_range(space_id, res->start, length, warn);
  1387. if (clash) {
  1388. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
  1389. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  1390. printk(KERN_NOTICE "ACPI: This conflict may"
  1391. " cause random problems and system"
  1392. " instability\n");
  1393. printk(KERN_INFO "ACPI: If an ACPI driver is available"
  1394. " for this device, you should use it instead of"
  1395. " the native driver\n");
  1396. }
  1397. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  1398. return -EBUSY;
  1399. }
  1400. return 0;
  1401. }
  1402. EXPORT_SYMBOL(acpi_check_resource_conflict);
  1403. int acpi_check_region(resource_size_t start, resource_size_t n,
  1404. const char *name)
  1405. {
  1406. struct resource res = {
  1407. .start = start,
  1408. .end = start + n - 1,
  1409. .name = name,
  1410. .flags = IORESOURCE_IO,
  1411. };
  1412. return acpi_check_resource_conflict(&res);
  1413. }
  1414. EXPORT_SYMBOL(acpi_check_region);
  1415. /*
  1416. * Let drivers know whether the resource checks are effective
  1417. */
  1418. int acpi_resources_are_enforced(void)
  1419. {
  1420. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  1421. }
  1422. EXPORT_SYMBOL(acpi_resources_are_enforced);
  1423. bool acpi_osi_is_win8(void)
  1424. {
  1425. return acpi_gbl_osi_data >= ACPI_OSI_WIN_8;
  1426. }
  1427. EXPORT_SYMBOL(acpi_osi_is_win8);
  1428. /*
  1429. * Deallocate the memory for a spinlock.
  1430. */
  1431. void acpi_os_delete_lock(acpi_spinlock handle)
  1432. {
  1433. ACPI_FREE(handle);
  1434. }
  1435. /*
  1436. * Acquire a spinlock.
  1437. *
  1438. * handle is a pointer to the spinlock_t.
  1439. */
  1440. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1441. {
  1442. acpi_cpu_flags flags;
  1443. spin_lock_irqsave(lockp, flags);
  1444. return flags;
  1445. }
  1446. /*
  1447. * Release a spinlock. See above.
  1448. */
  1449. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  1450. {
  1451. spin_unlock_irqrestore(lockp, flags);
  1452. }
  1453. #ifndef ACPI_USE_LOCAL_CACHE
  1454. /*******************************************************************************
  1455. *
  1456. * FUNCTION: acpi_os_create_cache
  1457. *
  1458. * PARAMETERS: name - Ascii name for the cache
  1459. * size - Size of each cached object
  1460. * depth - Maximum depth of the cache (in objects) <ignored>
  1461. * cache - Where the new cache object is returned
  1462. *
  1463. * RETURN: status
  1464. *
  1465. * DESCRIPTION: Create a cache object
  1466. *
  1467. ******************************************************************************/
  1468. acpi_status
  1469. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  1470. {
  1471. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1472. if (*cache == NULL)
  1473. return AE_ERROR;
  1474. else
  1475. return AE_OK;
  1476. }
  1477. /*******************************************************************************
  1478. *
  1479. * FUNCTION: acpi_os_purge_cache
  1480. *
  1481. * PARAMETERS: Cache - Handle to cache object
  1482. *
  1483. * RETURN: Status
  1484. *
  1485. * DESCRIPTION: Free all objects within the requested cache.
  1486. *
  1487. ******************************************************************************/
  1488. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  1489. {
  1490. kmem_cache_shrink(cache);
  1491. return (AE_OK);
  1492. }
  1493. /*******************************************************************************
  1494. *
  1495. * FUNCTION: acpi_os_delete_cache
  1496. *
  1497. * PARAMETERS: Cache - Handle to cache object
  1498. *
  1499. * RETURN: Status
  1500. *
  1501. * DESCRIPTION: Free all objects within the requested cache and delete the
  1502. * cache object.
  1503. *
  1504. ******************************************************************************/
  1505. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  1506. {
  1507. kmem_cache_destroy(cache);
  1508. return (AE_OK);
  1509. }
  1510. /*******************************************************************************
  1511. *
  1512. * FUNCTION: acpi_os_release_object
  1513. *
  1514. * PARAMETERS: Cache - Handle to cache object
  1515. * Object - The object to be released
  1516. *
  1517. * RETURN: None
  1518. *
  1519. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1520. * the object is deleted.
  1521. *
  1522. ******************************************************************************/
  1523. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1524. {
  1525. kmem_cache_free(cache, object);
  1526. return (AE_OK);
  1527. }
  1528. #endif
  1529. static int __init acpi_no_static_ssdt_setup(char *s)
  1530. {
  1531. acpi_gbl_disable_ssdt_table_install = TRUE;
  1532. pr_info("ACPI: static SSDT installation disabled\n");
  1533. return 0;
  1534. }
  1535. early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
  1536. static int __init acpi_disable_return_repair(char *s)
  1537. {
  1538. printk(KERN_NOTICE PREFIX
  1539. "ACPI: Predefined validation mechanism disabled\n");
  1540. acpi_gbl_disable_auto_repair = TRUE;
  1541. return 1;
  1542. }
  1543. __setup("acpica_no_return_repair", acpi_disable_return_repair);
  1544. acpi_status __init acpi_os_initialize(void)
  1545. {
  1546. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1547. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1548. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1549. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1550. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
  1551. /*
  1552. * Use acpi_os_map_generic_address to pre-map the reset
  1553. * register if it's in system memory.
  1554. */
  1555. int rv;
  1556. rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
  1557. pr_debug(PREFIX "%s: map reset_reg status %d\n", __func__, rv);
  1558. }
  1559. acpi_os_initialized = true;
  1560. return AE_OK;
  1561. }
  1562. acpi_status __init acpi_os_initialize1(void)
  1563. {
  1564. kacpid_wq = alloc_workqueue("kacpid", 0, 1);
  1565. kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
  1566. kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
  1567. BUG_ON(!kacpid_wq);
  1568. BUG_ON(!kacpi_notify_wq);
  1569. BUG_ON(!kacpi_hotplug_wq);
  1570. acpi_install_interface_handler(acpi_osi_handler);
  1571. acpi_osi_setup_late();
  1572. return AE_OK;
  1573. }
  1574. acpi_status acpi_os_terminate(void)
  1575. {
  1576. if (acpi_irq_handler) {
  1577. acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
  1578. acpi_irq_handler);
  1579. }
  1580. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1581. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1582. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1583. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1584. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
  1585. acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
  1586. destroy_workqueue(kacpid_wq);
  1587. destroy_workqueue(kacpi_notify_wq);
  1588. destroy_workqueue(kacpi_hotplug_wq);
  1589. return AE_OK;
  1590. }
  1591. acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
  1592. u32 pm1b_control)
  1593. {
  1594. int rc = 0;
  1595. if (__acpi_os_prepare_sleep)
  1596. rc = __acpi_os_prepare_sleep(sleep_state,
  1597. pm1a_control, pm1b_control);
  1598. if (rc < 0)
  1599. return AE_ERROR;
  1600. else if (rc > 0)
  1601. return AE_CTRL_SKIP;
  1602. return AE_OK;
  1603. }
  1604. void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
  1605. u32 pm1a_ctrl, u32 pm1b_ctrl))
  1606. {
  1607. __acpi_os_prepare_sleep = func;
  1608. }
  1609. acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
  1610. u32 val_b)
  1611. {
  1612. int rc = 0;
  1613. if (__acpi_os_prepare_extended_sleep)
  1614. rc = __acpi_os_prepare_extended_sleep(sleep_state,
  1615. val_a, val_b);
  1616. if (rc < 0)
  1617. return AE_ERROR;
  1618. else if (rc > 0)
  1619. return AE_CTRL_SKIP;
  1620. return AE_OK;
  1621. }
  1622. void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
  1623. u32 val_a, u32 val_b))
  1624. {
  1625. __acpi_os_prepare_extended_sleep = func;
  1626. }