osl.c 45 KB

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