osl.c 45 KB

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