osl.c 45 KB

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