osl.c 53 KB

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