osl.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*
  2. * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
  3. *
  4. * Copyright (C) 2000 Andrew Henroid
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. * Copyright (c) 2008 Intel Corporation
  8. * Author: Matthew Wilcox <willy@linux.intel.com>
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pci.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/kmod.h>
  33. #include <linux/delay.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/nmi.h>
  36. #include <linux/acpi.h>
  37. #include <linux/efi.h>
  38. #include <linux/ioport.h>
  39. #include <linux/list.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/semaphore.h>
  42. #include <asm/io.h>
  43. #include <asm/uaccess.h>
  44. #include <linux/io-64-nonatomic-lo-hi.h>
  45. #include "internal.h"
  46. #define _COMPONENT ACPI_OS_SERVICES
  47. ACPI_MODULE_NAME("osl");
  48. struct acpi_os_dpc {
  49. acpi_osd_exec_callback function;
  50. void *context;
  51. struct work_struct work;
  52. };
  53. #ifdef ENABLE_DEBUGGER
  54. #include <linux/kdb.h>
  55. /* stuff for debugger support */
  56. int acpi_in_debugger;
  57. EXPORT_SYMBOL(acpi_in_debugger);
  58. #endif /*ENABLE_DEBUGGER */
  59. static int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
  60. u32 pm1b_ctrl);
  61. static int (*__acpi_os_prepare_extended_sleep)(u8 sleep_state, u32 val_a,
  62. u32 val_b);
  63. static acpi_osd_handler acpi_irq_handler;
  64. static void *acpi_irq_context;
  65. static struct workqueue_struct *kacpid_wq;
  66. static struct workqueue_struct *kacpi_notify_wq;
  67. static struct workqueue_struct *kacpi_hotplug_wq;
  68. static bool acpi_os_initialized;
  69. unsigned int acpi_sci_irq = INVALID_ACPI_IRQ;
  70. /*
  71. * This list of permanent mappings is for memory that may be accessed from
  72. * interrupt context, where we can't do the ioremap().
  73. */
  74. struct acpi_ioremap {
  75. struct list_head list;
  76. void __iomem *virt;
  77. acpi_physical_address phys;
  78. acpi_size size;
  79. unsigned long refcount;
  80. };
  81. static LIST_HEAD(acpi_ioremaps);
  82. static DEFINE_MUTEX(acpi_ioremap_lock);
  83. static void __init acpi_request_region (struct acpi_generic_address *gas,
  84. unsigned int length, char *desc)
  85. {
  86. u64 addr;
  87. /* Handle possible alignment issues */
  88. memcpy(&addr, &gas->address, sizeof(addr));
  89. if (!addr || !length)
  90. return;
  91. /* Resources are never freed */
  92. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  93. request_region(addr, length, desc);
  94. else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  95. request_mem_region(addr, length, desc);
  96. }
  97. static int __init acpi_reserve_resources(void)
  98. {
  99. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  100. "ACPI PM1a_EVT_BLK");
  101. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  102. "ACPI PM1b_EVT_BLK");
  103. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  104. "ACPI PM1a_CNT_BLK");
  105. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  106. "ACPI PM1b_CNT_BLK");
  107. if (acpi_gbl_FADT.pm_timer_length == 4)
  108. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  109. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  110. "ACPI PM2_CNT_BLK");
  111. /* Length of GPE blocks must be a non-negative multiple of 2 */
  112. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  113. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  114. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  115. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  116. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  117. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  118. return 0;
  119. }
  120. fs_initcall_sync(acpi_reserve_resources);
  121. void acpi_os_printf(const char *fmt, ...)
  122. {
  123. va_list args;
  124. va_start(args, fmt);
  125. acpi_os_vprintf(fmt, args);
  126. va_end(args);
  127. }
  128. EXPORT_SYMBOL(acpi_os_printf);
  129. void acpi_os_vprintf(const char *fmt, va_list args)
  130. {
  131. static char buffer[512];
  132. vsprintf(buffer, fmt, args);
  133. #ifdef ENABLE_DEBUGGER
  134. if (acpi_in_debugger) {
  135. kdb_printf("%s", buffer);
  136. } else {
  137. if (printk_get_level(buffer))
  138. printk("%s", buffer);
  139. else
  140. printk(KERN_CONT "%s", buffer);
  141. }
  142. #else
  143. if (acpi_debugger_write_log(buffer) < 0) {
  144. if (printk_get_level(buffer))
  145. printk("%s", buffer);
  146. else
  147. printk(KERN_CONT "%s", buffer);
  148. }
  149. #endif
  150. }
  151. #ifdef CONFIG_KEXEC
  152. static unsigned long acpi_rsdp;
  153. static int __init setup_acpi_rsdp(char *arg)
  154. {
  155. if (kstrtoul(arg, 16, &acpi_rsdp))
  156. return -EINVAL;
  157. return 0;
  158. }
  159. early_param("acpi_rsdp", setup_acpi_rsdp);
  160. #endif
  161. acpi_physical_address __init acpi_os_get_root_pointer(void)
  162. {
  163. #ifdef CONFIG_KEXEC
  164. if (acpi_rsdp)
  165. return acpi_rsdp;
  166. #endif
  167. if (efi_enabled(EFI_CONFIG_TABLES)) {
  168. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  169. return efi.acpi20;
  170. else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  171. return efi.acpi;
  172. else {
  173. printk(KERN_ERR PREFIX
  174. "System description tables not found\n");
  175. return 0;
  176. }
  177. } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP)) {
  178. acpi_physical_address pa = 0;
  179. acpi_find_root_pointer(&pa);
  180. return pa;
  181. }
  182. return 0;
  183. }
  184. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  185. static struct acpi_ioremap *
  186. acpi_map_lookup(acpi_physical_address phys, acpi_size size)
  187. {
  188. struct acpi_ioremap *map;
  189. list_for_each_entry_rcu(map, &acpi_ioremaps, list)
  190. if (map->phys <= phys &&
  191. phys + size <= map->phys + map->size)
  192. return map;
  193. return NULL;
  194. }
  195. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  196. static void __iomem *
  197. acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
  198. {
  199. struct acpi_ioremap *map;
  200. map = acpi_map_lookup(phys, size);
  201. if (map)
  202. return map->virt + (phys - map->phys);
  203. return NULL;
  204. }
  205. void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
  206. {
  207. struct acpi_ioremap *map;
  208. void __iomem *virt = NULL;
  209. mutex_lock(&acpi_ioremap_lock);
  210. map = acpi_map_lookup(phys, size);
  211. if (map) {
  212. virt = map->virt + (phys - map->phys);
  213. map->refcount++;
  214. }
  215. mutex_unlock(&acpi_ioremap_lock);
  216. return virt;
  217. }
  218. EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
  219. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  220. static struct acpi_ioremap *
  221. acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
  222. {
  223. struct acpi_ioremap *map;
  224. list_for_each_entry_rcu(map, &acpi_ioremaps, list)
  225. if (map->virt <= virt &&
  226. virt + size <= map->virt + map->size)
  227. return map;
  228. return NULL;
  229. }
  230. #if defined(CONFIG_IA64) || defined(CONFIG_ARM64)
  231. /* ioremap will take care of cache attributes */
  232. #define should_use_kmap(pfn) 0
  233. #else
  234. #define should_use_kmap(pfn) page_is_ram(pfn)
  235. #endif
  236. static void __iomem *acpi_map(acpi_physical_address pg_off, unsigned long pg_sz)
  237. {
  238. unsigned long pfn;
  239. pfn = pg_off >> PAGE_SHIFT;
  240. if (should_use_kmap(pfn)) {
  241. if (pg_sz > PAGE_SIZE)
  242. return NULL;
  243. return (void __iomem __force *)kmap(pfn_to_page(pfn));
  244. } else
  245. return acpi_os_ioremap(pg_off, pg_sz);
  246. }
  247. static void acpi_unmap(acpi_physical_address pg_off, void __iomem *vaddr)
  248. {
  249. unsigned long pfn;
  250. pfn = pg_off >> PAGE_SHIFT;
  251. if (should_use_kmap(pfn))
  252. kunmap(pfn_to_page(pfn));
  253. else
  254. iounmap(vaddr);
  255. }
  256. /**
  257. * acpi_os_map_iomem - Get a virtual address for a given physical address range.
  258. * @phys: Start of the physical address range to map.
  259. * @size: Size of the physical address range to map.
  260. *
  261. * Look up the given physical address range in the list of existing ACPI memory
  262. * mappings. If found, get a reference to it and return a pointer to it (its
  263. * virtual address). If not found, map it, add it to that list and return a
  264. * pointer to it.
  265. *
  266. * During early init (when acpi_gbl_permanent_mmap has not been set yet) this
  267. * routine simply calls __acpi_map_table() to get the job done.
  268. */
  269. void __iomem *__ref
  270. acpi_os_map_iomem(acpi_physical_address phys, acpi_size size)
  271. {
  272. struct acpi_ioremap *map;
  273. void __iomem *virt;
  274. acpi_physical_address pg_off;
  275. acpi_size pg_sz;
  276. if (phys > ULONG_MAX) {
  277. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  278. return NULL;
  279. }
  280. if (!acpi_gbl_permanent_mmap)
  281. return __acpi_map_table((unsigned long)phys, size);
  282. mutex_lock(&acpi_ioremap_lock);
  283. /* Check if there's a suitable mapping already. */
  284. map = acpi_map_lookup(phys, size);
  285. if (map) {
  286. map->refcount++;
  287. goto out;
  288. }
  289. map = kzalloc(sizeof(*map), GFP_KERNEL);
  290. if (!map) {
  291. mutex_unlock(&acpi_ioremap_lock);
  292. return NULL;
  293. }
  294. pg_off = round_down(phys, PAGE_SIZE);
  295. pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
  296. virt = acpi_map(pg_off, pg_sz);
  297. if (!virt) {
  298. mutex_unlock(&acpi_ioremap_lock);
  299. kfree(map);
  300. return NULL;
  301. }
  302. INIT_LIST_HEAD(&map->list);
  303. map->virt = virt;
  304. map->phys = pg_off;
  305. map->size = pg_sz;
  306. map->refcount = 1;
  307. list_add_tail_rcu(&map->list, &acpi_ioremaps);
  308. out:
  309. mutex_unlock(&acpi_ioremap_lock);
  310. return map->virt + (phys - map->phys);
  311. }
  312. EXPORT_SYMBOL_GPL(acpi_os_map_iomem);
  313. void *__ref acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  314. {
  315. return (void *)acpi_os_map_iomem(phys, size);
  316. }
  317. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  318. static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
  319. {
  320. if (!--map->refcount)
  321. list_del_rcu(&map->list);
  322. }
  323. static void acpi_os_map_cleanup(struct acpi_ioremap *map)
  324. {
  325. if (!map->refcount) {
  326. synchronize_rcu_expedited();
  327. acpi_unmap(map->phys, map->virt);
  328. kfree(map);
  329. }
  330. }
  331. /**
  332. * acpi_os_unmap_iomem - Drop a memory mapping reference.
  333. * @virt: Start of the address range to drop a reference to.
  334. * @size: Size of the address range to drop a reference to.
  335. *
  336. * Look up the given virtual address range in the list of existing ACPI memory
  337. * mappings, drop a reference to it and unmap it if there are no more active
  338. * references to it.
  339. *
  340. * During early init (when acpi_gbl_permanent_mmap has not been set yet) this
  341. * routine simply calls __acpi_unmap_table() to get the job done. Since
  342. * __acpi_unmap_table() is an __init function, the __ref annotation is needed
  343. * here.
  344. */
  345. void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
  346. {
  347. struct acpi_ioremap *map;
  348. if (!acpi_gbl_permanent_mmap) {
  349. __acpi_unmap_table(virt, size);
  350. return;
  351. }
  352. mutex_lock(&acpi_ioremap_lock);
  353. map = acpi_map_lookup_virt(virt, size);
  354. if (!map) {
  355. mutex_unlock(&acpi_ioremap_lock);
  356. WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
  357. return;
  358. }
  359. acpi_os_drop_map_ref(map);
  360. mutex_unlock(&acpi_ioremap_lock);
  361. acpi_os_map_cleanup(map);
  362. }
  363. EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
  364. void __ref acpi_os_unmap_memory(void *virt, acpi_size size)
  365. {
  366. return acpi_os_unmap_iomem((void __iomem *)virt, size);
  367. }
  368. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  369. void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
  370. {
  371. if (!acpi_gbl_permanent_mmap)
  372. __acpi_unmap_table(virt, size);
  373. }
  374. int acpi_os_map_generic_address(struct acpi_generic_address *gas)
  375. {
  376. u64 addr;
  377. void __iomem *virt;
  378. if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  379. return 0;
  380. /* Handle possible alignment issues */
  381. memcpy(&addr, &gas->address, sizeof(addr));
  382. if (!addr || !gas->bit_width)
  383. return -EINVAL;
  384. virt = acpi_os_map_iomem(addr, gas->bit_width / 8);
  385. if (!virt)
  386. return -EIO;
  387. return 0;
  388. }
  389. EXPORT_SYMBOL(acpi_os_map_generic_address);
  390. void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
  391. {
  392. u64 addr;
  393. struct acpi_ioremap *map;
  394. if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  395. return;
  396. /* Handle possible alignment issues */
  397. memcpy(&addr, &gas->address, sizeof(addr));
  398. if (!addr || !gas->bit_width)
  399. return;
  400. mutex_lock(&acpi_ioremap_lock);
  401. map = acpi_map_lookup(addr, gas->bit_width / 8);
  402. if (!map) {
  403. mutex_unlock(&acpi_ioremap_lock);
  404. return;
  405. }
  406. acpi_os_drop_map_ref(map);
  407. mutex_unlock(&acpi_ioremap_lock);
  408. acpi_os_map_cleanup(map);
  409. }
  410. EXPORT_SYMBOL(acpi_os_unmap_generic_address);
  411. #ifdef ACPI_FUTURE_USAGE
  412. acpi_status
  413. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  414. {
  415. if (!phys || !virt)
  416. return AE_BAD_PARAMETER;
  417. *phys = virt_to_phys(virt);
  418. return AE_OK;
  419. }
  420. #endif
  421. #ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
  422. static bool acpi_rev_override;
  423. int __init acpi_rev_override_setup(char *str)
  424. {
  425. acpi_rev_override = true;
  426. return 1;
  427. }
  428. __setup("acpi_rev_override", acpi_rev_override_setup);
  429. #else
  430. #define acpi_rev_override false
  431. #endif
  432. #define ACPI_MAX_OVERRIDE_LEN 100
  433. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  434. acpi_status
  435. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  436. acpi_string *new_val)
  437. {
  438. if (!init_val || !new_val)
  439. return AE_BAD_PARAMETER;
  440. *new_val = NULL;
  441. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  442. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  443. acpi_os_name);
  444. *new_val = acpi_os_name;
  445. }
  446. if (!memcmp(init_val->name, "_REV", 4) && acpi_rev_override) {
  447. printk(KERN_INFO PREFIX "Overriding _REV return value to 5\n");
  448. *new_val = (char *)5;
  449. }
  450. return AE_OK;
  451. }
  452. static irqreturn_t acpi_irq(int irq, void *dev_id)
  453. {
  454. u32 handled;
  455. handled = (*acpi_irq_handler) (acpi_irq_context);
  456. if (handled) {
  457. acpi_irq_handled++;
  458. return IRQ_HANDLED;
  459. } else {
  460. acpi_irq_not_handled++;
  461. return IRQ_NONE;
  462. }
  463. }
  464. acpi_status
  465. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  466. void *context)
  467. {
  468. unsigned int irq;
  469. acpi_irq_stats_init();
  470. /*
  471. * ACPI interrupts different from the SCI in our copy of the FADT are
  472. * not supported.
  473. */
  474. if (gsi != acpi_gbl_FADT.sci_interrupt)
  475. return AE_BAD_PARAMETER;
  476. if (acpi_irq_handler)
  477. return AE_ALREADY_ACQUIRED;
  478. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  479. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  480. gsi);
  481. return AE_OK;
  482. }
  483. acpi_irq_handler = handler;
  484. acpi_irq_context = context;
  485. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  486. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  487. acpi_irq_handler = NULL;
  488. return AE_NOT_ACQUIRED;
  489. }
  490. acpi_sci_irq = irq;
  491. return AE_OK;
  492. }
  493. acpi_status acpi_os_remove_interrupt_handler(u32 gsi, acpi_osd_handler handler)
  494. {
  495. if (gsi != acpi_gbl_FADT.sci_interrupt || !acpi_sci_irq_valid())
  496. return AE_BAD_PARAMETER;
  497. free_irq(acpi_sci_irq, acpi_irq);
  498. acpi_irq_handler = NULL;
  499. acpi_sci_irq = INVALID_ACPI_IRQ;
  500. return AE_OK;
  501. }
  502. /*
  503. * Running in interpreter thread context, safe to sleep
  504. */
  505. void acpi_os_sleep(u64 ms)
  506. {
  507. msleep(ms);
  508. }
  509. void acpi_os_stall(u32 us)
  510. {
  511. while (us) {
  512. u32 delay = 1000;
  513. if (delay > us)
  514. delay = us;
  515. udelay(delay);
  516. touch_nmi_watchdog();
  517. us -= delay;
  518. }
  519. }
  520. /*
  521. * Support ACPI 3.0 AML Timer operand
  522. * Returns 64-bit free-running, monotonically increasing timer
  523. * with 100ns granularity
  524. */
  525. u64 acpi_os_get_timer(void)
  526. {
  527. u64 time_ns = ktime_to_ns(ktime_get());
  528. do_div(time_ns, 100);
  529. return time_ns;
  530. }
  531. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  532. {
  533. u32 dummy;
  534. if (!value)
  535. value = &dummy;
  536. *value = 0;
  537. if (width <= 8) {
  538. *(u8 *) value = inb(port);
  539. } else if (width <= 16) {
  540. *(u16 *) value = inw(port);
  541. } else if (width <= 32) {
  542. *(u32 *) value = inl(port);
  543. } else {
  544. BUG();
  545. }
  546. return AE_OK;
  547. }
  548. EXPORT_SYMBOL(acpi_os_read_port);
  549. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  550. {
  551. if (width <= 8) {
  552. outb(value, port);
  553. } else if (width <= 16) {
  554. outw(value, port);
  555. } else if (width <= 32) {
  556. outl(value, port);
  557. } else {
  558. BUG();
  559. }
  560. return AE_OK;
  561. }
  562. EXPORT_SYMBOL(acpi_os_write_port);
  563. acpi_status
  564. acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
  565. {
  566. void __iomem *virt_addr;
  567. unsigned int size = width / 8;
  568. bool unmap = false;
  569. u64 dummy;
  570. rcu_read_lock();
  571. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  572. if (!virt_addr) {
  573. rcu_read_unlock();
  574. virt_addr = acpi_os_ioremap(phys_addr, size);
  575. if (!virt_addr)
  576. return AE_BAD_ADDRESS;
  577. unmap = true;
  578. }
  579. if (!value)
  580. value = &dummy;
  581. switch (width) {
  582. case 8:
  583. *(u8 *) value = readb(virt_addr);
  584. break;
  585. case 16:
  586. *(u16 *) value = readw(virt_addr);
  587. break;
  588. case 32:
  589. *(u32 *) value = readl(virt_addr);
  590. break;
  591. case 64:
  592. *(u64 *) value = readq(virt_addr);
  593. break;
  594. default:
  595. BUG();
  596. }
  597. if (unmap)
  598. iounmap(virt_addr);
  599. else
  600. rcu_read_unlock();
  601. return AE_OK;
  602. }
  603. acpi_status
  604. acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
  605. {
  606. void __iomem *virt_addr;
  607. unsigned int size = width / 8;
  608. bool unmap = false;
  609. rcu_read_lock();
  610. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  611. if (!virt_addr) {
  612. rcu_read_unlock();
  613. virt_addr = acpi_os_ioremap(phys_addr, size);
  614. if (!virt_addr)
  615. return AE_BAD_ADDRESS;
  616. unmap = true;
  617. }
  618. switch (width) {
  619. case 8:
  620. writeb(value, virt_addr);
  621. break;
  622. case 16:
  623. writew(value, virt_addr);
  624. break;
  625. case 32:
  626. writel(value, virt_addr);
  627. break;
  628. case 64:
  629. writeq(value, virt_addr);
  630. break;
  631. default:
  632. BUG();
  633. }
  634. if (unmap)
  635. iounmap(virt_addr);
  636. else
  637. rcu_read_unlock();
  638. return AE_OK;
  639. }
  640. acpi_status
  641. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  642. u64 *value, u32 width)
  643. {
  644. int result, size;
  645. u32 value32;
  646. if (!value)
  647. return AE_BAD_PARAMETER;
  648. switch (width) {
  649. case 8:
  650. size = 1;
  651. break;
  652. case 16:
  653. size = 2;
  654. break;
  655. case 32:
  656. size = 4;
  657. break;
  658. default:
  659. return AE_ERROR;
  660. }
  661. result = raw_pci_read(pci_id->segment, pci_id->bus,
  662. PCI_DEVFN(pci_id->device, pci_id->function),
  663. reg, size, &value32);
  664. *value = value32;
  665. return (result ? AE_ERROR : AE_OK);
  666. }
  667. acpi_status
  668. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  669. u64 value, u32 width)
  670. {
  671. int result, size;
  672. switch (width) {
  673. case 8:
  674. size = 1;
  675. break;
  676. case 16:
  677. size = 2;
  678. break;
  679. case 32:
  680. size = 4;
  681. break;
  682. default:
  683. return AE_ERROR;
  684. }
  685. result = raw_pci_write(pci_id->segment, pci_id->bus,
  686. PCI_DEVFN(pci_id->device, pci_id->function),
  687. reg, size, value);
  688. return (result ? AE_ERROR : AE_OK);
  689. }
  690. static void acpi_os_execute_deferred(struct work_struct *work)
  691. {
  692. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  693. dpc->function(dpc->context);
  694. kfree(dpc);
  695. }
  696. #ifdef CONFIG_ACPI_DEBUGGER
  697. static struct acpi_debugger acpi_debugger;
  698. static bool acpi_debugger_initialized;
  699. int acpi_register_debugger(struct module *owner,
  700. const struct acpi_debugger_ops *ops)
  701. {
  702. int ret = 0;
  703. mutex_lock(&acpi_debugger.lock);
  704. if (acpi_debugger.ops) {
  705. ret = -EBUSY;
  706. goto err_lock;
  707. }
  708. acpi_debugger.owner = owner;
  709. acpi_debugger.ops = ops;
  710. err_lock:
  711. mutex_unlock(&acpi_debugger.lock);
  712. return ret;
  713. }
  714. EXPORT_SYMBOL(acpi_register_debugger);
  715. void acpi_unregister_debugger(const struct acpi_debugger_ops *ops)
  716. {
  717. mutex_lock(&acpi_debugger.lock);
  718. if (ops == acpi_debugger.ops) {
  719. acpi_debugger.ops = NULL;
  720. acpi_debugger.owner = NULL;
  721. }
  722. mutex_unlock(&acpi_debugger.lock);
  723. }
  724. EXPORT_SYMBOL(acpi_unregister_debugger);
  725. int acpi_debugger_create_thread(acpi_osd_exec_callback function, void *context)
  726. {
  727. int ret;
  728. int (*func)(acpi_osd_exec_callback, void *);
  729. struct module *owner;
  730. if (!acpi_debugger_initialized)
  731. return -ENODEV;
  732. mutex_lock(&acpi_debugger.lock);
  733. if (!acpi_debugger.ops) {
  734. ret = -ENODEV;
  735. goto err_lock;
  736. }
  737. if (!try_module_get(acpi_debugger.owner)) {
  738. ret = -ENODEV;
  739. goto err_lock;
  740. }
  741. func = acpi_debugger.ops->create_thread;
  742. owner = acpi_debugger.owner;
  743. mutex_unlock(&acpi_debugger.lock);
  744. ret = func(function, context);
  745. mutex_lock(&acpi_debugger.lock);
  746. module_put(owner);
  747. err_lock:
  748. mutex_unlock(&acpi_debugger.lock);
  749. return ret;
  750. }
  751. ssize_t acpi_debugger_write_log(const char *msg)
  752. {
  753. ssize_t ret;
  754. ssize_t (*func)(const char *);
  755. struct module *owner;
  756. if (!acpi_debugger_initialized)
  757. return -ENODEV;
  758. mutex_lock(&acpi_debugger.lock);
  759. if (!acpi_debugger.ops) {
  760. ret = -ENODEV;
  761. goto err_lock;
  762. }
  763. if (!try_module_get(acpi_debugger.owner)) {
  764. ret = -ENODEV;
  765. goto err_lock;
  766. }
  767. func = acpi_debugger.ops->write_log;
  768. owner = acpi_debugger.owner;
  769. mutex_unlock(&acpi_debugger.lock);
  770. ret = func(msg);
  771. mutex_lock(&acpi_debugger.lock);
  772. module_put(owner);
  773. err_lock:
  774. mutex_unlock(&acpi_debugger.lock);
  775. return ret;
  776. }
  777. ssize_t acpi_debugger_read_cmd(char *buffer, size_t buffer_length)
  778. {
  779. ssize_t ret;
  780. ssize_t (*func)(char *, size_t);
  781. struct module *owner;
  782. if (!acpi_debugger_initialized)
  783. return -ENODEV;
  784. mutex_lock(&acpi_debugger.lock);
  785. if (!acpi_debugger.ops) {
  786. ret = -ENODEV;
  787. goto err_lock;
  788. }
  789. if (!try_module_get(acpi_debugger.owner)) {
  790. ret = -ENODEV;
  791. goto err_lock;
  792. }
  793. func = acpi_debugger.ops->read_cmd;
  794. owner = acpi_debugger.owner;
  795. mutex_unlock(&acpi_debugger.lock);
  796. ret = func(buffer, buffer_length);
  797. mutex_lock(&acpi_debugger.lock);
  798. module_put(owner);
  799. err_lock:
  800. mutex_unlock(&acpi_debugger.lock);
  801. return ret;
  802. }
  803. int acpi_debugger_wait_command_ready(void)
  804. {
  805. int ret;
  806. int (*func)(bool, char *, size_t);
  807. struct module *owner;
  808. if (!acpi_debugger_initialized)
  809. return -ENODEV;
  810. mutex_lock(&acpi_debugger.lock);
  811. if (!acpi_debugger.ops) {
  812. ret = -ENODEV;
  813. goto err_lock;
  814. }
  815. if (!try_module_get(acpi_debugger.owner)) {
  816. ret = -ENODEV;
  817. goto err_lock;
  818. }
  819. func = acpi_debugger.ops->wait_command_ready;
  820. owner = acpi_debugger.owner;
  821. mutex_unlock(&acpi_debugger.lock);
  822. ret = func(acpi_gbl_method_executing,
  823. acpi_gbl_db_line_buf, ACPI_DB_LINE_BUFFER_SIZE);
  824. mutex_lock(&acpi_debugger.lock);
  825. module_put(owner);
  826. err_lock:
  827. mutex_unlock(&acpi_debugger.lock);
  828. return ret;
  829. }
  830. int acpi_debugger_notify_command_complete(void)
  831. {
  832. int ret;
  833. int (*func)(void);
  834. struct module *owner;
  835. if (!acpi_debugger_initialized)
  836. return -ENODEV;
  837. mutex_lock(&acpi_debugger.lock);
  838. if (!acpi_debugger.ops) {
  839. ret = -ENODEV;
  840. goto err_lock;
  841. }
  842. if (!try_module_get(acpi_debugger.owner)) {
  843. ret = -ENODEV;
  844. goto err_lock;
  845. }
  846. func = acpi_debugger.ops->notify_command_complete;
  847. owner = acpi_debugger.owner;
  848. mutex_unlock(&acpi_debugger.lock);
  849. ret = func();
  850. mutex_lock(&acpi_debugger.lock);
  851. module_put(owner);
  852. err_lock:
  853. mutex_unlock(&acpi_debugger.lock);
  854. return ret;
  855. }
  856. int __init acpi_debugger_init(void)
  857. {
  858. mutex_init(&acpi_debugger.lock);
  859. acpi_debugger_initialized = true;
  860. return 0;
  861. }
  862. #endif
  863. /*******************************************************************************
  864. *
  865. * FUNCTION: acpi_os_execute
  866. *
  867. * PARAMETERS: Type - Type of the callback
  868. * Function - Function to be executed
  869. * Context - Function parameters
  870. *
  871. * RETURN: Status
  872. *
  873. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  874. * immediately executes function on a separate thread.
  875. *
  876. ******************************************************************************/
  877. acpi_status acpi_os_execute(acpi_execute_type type,
  878. acpi_osd_exec_callback function, void *context)
  879. {
  880. acpi_status status = AE_OK;
  881. struct acpi_os_dpc *dpc;
  882. struct workqueue_struct *queue;
  883. int ret;
  884. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  885. "Scheduling function [%p(%p)] for deferred execution.\n",
  886. function, context));
  887. if (type == OSL_DEBUGGER_MAIN_THREAD) {
  888. ret = acpi_debugger_create_thread(function, context);
  889. if (ret) {
  890. pr_err("Call to kthread_create() failed.\n");
  891. status = AE_ERROR;
  892. }
  893. goto out_thread;
  894. }
  895. /*
  896. * Allocate/initialize DPC structure. Note that this memory will be
  897. * freed by the callee. The kernel handles the work_struct list in a
  898. * way that allows us to also free its memory inside the callee.
  899. * Because we may want to schedule several tasks with different
  900. * parameters we can't use the approach some kernel code uses of
  901. * having a static work_struct.
  902. */
  903. dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  904. if (!dpc)
  905. return AE_NO_MEMORY;
  906. dpc->function = function;
  907. dpc->context = context;
  908. /*
  909. * To prevent lockdep from complaining unnecessarily, make sure that
  910. * there is a different static lockdep key for each workqueue by using
  911. * INIT_WORK() for each of them separately.
  912. */
  913. if (type == OSL_NOTIFY_HANDLER) {
  914. queue = kacpi_notify_wq;
  915. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  916. } else if (type == OSL_GPE_HANDLER) {
  917. queue = kacpid_wq;
  918. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  919. } else {
  920. pr_err("Unsupported os_execute type %d.\n", type);
  921. status = AE_ERROR;
  922. }
  923. if (ACPI_FAILURE(status))
  924. goto err_workqueue;
  925. /*
  926. * On some machines, a software-initiated SMI causes corruption unless
  927. * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
  928. * typically it's done in GPE-related methods that are run via
  929. * workqueues, so we can avoid the known corruption cases by always
  930. * queueing on CPU 0.
  931. */
  932. ret = queue_work_on(0, queue, &dpc->work);
  933. if (!ret) {
  934. printk(KERN_ERR PREFIX
  935. "Call to queue_work() failed.\n");
  936. status = AE_ERROR;
  937. }
  938. err_workqueue:
  939. if (ACPI_FAILURE(status))
  940. kfree(dpc);
  941. out_thread:
  942. return status;
  943. }
  944. EXPORT_SYMBOL(acpi_os_execute);
  945. void acpi_os_wait_events_complete(void)
  946. {
  947. /*
  948. * Make sure the GPE handler or the fixed event handler is not used
  949. * on another CPU after removal.
  950. */
  951. if (acpi_sci_irq_valid())
  952. synchronize_hardirq(acpi_sci_irq);
  953. flush_workqueue(kacpid_wq);
  954. flush_workqueue(kacpi_notify_wq);
  955. }
  956. struct acpi_hp_work {
  957. struct work_struct work;
  958. struct acpi_device *adev;
  959. u32 src;
  960. };
  961. static void acpi_hotplug_work_fn(struct work_struct *work)
  962. {
  963. struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
  964. acpi_os_wait_events_complete();
  965. acpi_device_hotplug(hpw->adev, hpw->src);
  966. kfree(hpw);
  967. }
  968. acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
  969. {
  970. struct acpi_hp_work *hpw;
  971. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  972. "Scheduling hotplug event (%p, %u) for deferred execution.\n",
  973. adev, src));
  974. hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
  975. if (!hpw)
  976. return AE_NO_MEMORY;
  977. INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
  978. hpw->adev = adev;
  979. hpw->src = src;
  980. /*
  981. * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
  982. * the hotplug code may call driver .remove() functions, which may
  983. * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
  984. * these workqueues.
  985. */
  986. if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
  987. kfree(hpw);
  988. return AE_ERROR;
  989. }
  990. return AE_OK;
  991. }
  992. bool acpi_queue_hotplug_work(struct work_struct *work)
  993. {
  994. return queue_work(kacpi_hotplug_wq, work);
  995. }
  996. acpi_status
  997. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  998. {
  999. struct semaphore *sem = NULL;
  1000. sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
  1001. if (!sem)
  1002. return AE_NO_MEMORY;
  1003. sema_init(sem, initial_units);
  1004. *handle = (acpi_handle *) sem;
  1005. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  1006. *handle, initial_units));
  1007. return AE_OK;
  1008. }
  1009. /*
  1010. * TODO: A better way to delete semaphores? Linux doesn't have a
  1011. * 'delete_semaphore()' function -- may result in an invalid
  1012. * pointer dereference for non-synchronized consumers. Should
  1013. * we at least check for blocked threads and signal/cancel them?
  1014. */
  1015. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  1016. {
  1017. struct semaphore *sem = (struct semaphore *)handle;
  1018. if (!sem)
  1019. return AE_BAD_PARAMETER;
  1020. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  1021. BUG_ON(!list_empty(&sem->wait_list));
  1022. kfree(sem);
  1023. sem = NULL;
  1024. return AE_OK;
  1025. }
  1026. /*
  1027. * TODO: Support for units > 1?
  1028. */
  1029. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  1030. {
  1031. acpi_status status = AE_OK;
  1032. struct semaphore *sem = (struct semaphore *)handle;
  1033. long jiffies;
  1034. int ret = 0;
  1035. if (!acpi_os_initialized)
  1036. return AE_OK;
  1037. if (!sem || (units < 1))
  1038. return AE_BAD_PARAMETER;
  1039. if (units > 1)
  1040. return AE_SUPPORT;
  1041. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  1042. handle, units, timeout));
  1043. if (timeout == ACPI_WAIT_FOREVER)
  1044. jiffies = MAX_SCHEDULE_TIMEOUT;
  1045. else
  1046. jiffies = msecs_to_jiffies(timeout);
  1047. ret = down_timeout(sem, jiffies);
  1048. if (ret)
  1049. status = AE_TIME;
  1050. if (ACPI_FAILURE(status)) {
  1051. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1052. "Failed to acquire semaphore[%p|%d|%d], %s",
  1053. handle, units, timeout,
  1054. acpi_format_exception(status)));
  1055. } else {
  1056. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1057. "Acquired semaphore[%p|%d|%d]", handle,
  1058. units, timeout));
  1059. }
  1060. return status;
  1061. }
  1062. /*
  1063. * TODO: Support for units > 1?
  1064. */
  1065. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  1066. {
  1067. struct semaphore *sem = (struct semaphore *)handle;
  1068. if (!acpi_os_initialized)
  1069. return AE_OK;
  1070. if (!sem || (units < 1))
  1071. return AE_BAD_PARAMETER;
  1072. if (units > 1)
  1073. return AE_SUPPORT;
  1074. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  1075. units));
  1076. up(sem);
  1077. return AE_OK;
  1078. }
  1079. acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
  1080. {
  1081. #ifdef ENABLE_DEBUGGER
  1082. if (acpi_in_debugger) {
  1083. u32 chars;
  1084. kdb_read(buffer, buffer_length);
  1085. /* remove the CR kdb includes */
  1086. chars = strlen(buffer) - 1;
  1087. buffer[chars] = '\0';
  1088. }
  1089. #else
  1090. int ret;
  1091. ret = acpi_debugger_read_cmd(buffer, buffer_length);
  1092. if (ret < 0)
  1093. return AE_ERROR;
  1094. if (bytes_read)
  1095. *bytes_read = ret;
  1096. #endif
  1097. return AE_OK;
  1098. }
  1099. EXPORT_SYMBOL(acpi_os_get_line);
  1100. acpi_status acpi_os_wait_command_ready(void)
  1101. {
  1102. int ret;
  1103. ret = acpi_debugger_wait_command_ready();
  1104. if (ret < 0)
  1105. return AE_ERROR;
  1106. return AE_OK;
  1107. }
  1108. acpi_status acpi_os_notify_command_complete(void)
  1109. {
  1110. int ret;
  1111. ret = acpi_debugger_notify_command_complete();
  1112. if (ret < 0)
  1113. return AE_ERROR;
  1114. return AE_OK;
  1115. }
  1116. acpi_status acpi_os_signal(u32 function, void *info)
  1117. {
  1118. switch (function) {
  1119. case ACPI_SIGNAL_FATAL:
  1120. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  1121. break;
  1122. case ACPI_SIGNAL_BREAKPOINT:
  1123. /*
  1124. * AML Breakpoint
  1125. * ACPI spec. says to treat it as a NOP unless
  1126. * you are debugging. So if/when we integrate
  1127. * AML debugger into the kernel debugger its
  1128. * hook will go here. But until then it is
  1129. * not useful to print anything on breakpoints.
  1130. */
  1131. break;
  1132. default:
  1133. break;
  1134. }
  1135. return AE_OK;
  1136. }
  1137. static int __init acpi_os_name_setup(char *str)
  1138. {
  1139. char *p = acpi_os_name;
  1140. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  1141. if (!str || !*str)
  1142. return 0;
  1143. for (; count-- && *str; str++) {
  1144. if (isalnum(*str) || *str == ' ' || *str == ':')
  1145. *p++ = *str;
  1146. else if (*str == '\'' || *str == '"')
  1147. continue;
  1148. else
  1149. break;
  1150. }
  1151. *p = 0;
  1152. return 1;
  1153. }
  1154. __setup("acpi_os_name=", acpi_os_name_setup);
  1155. /*
  1156. * Disable the auto-serialization of named objects creation methods.
  1157. *
  1158. * This feature is enabled by default. It marks the AML control methods
  1159. * that contain the opcodes to create named objects as "Serialized".
  1160. */
  1161. static int __init acpi_no_auto_serialize_setup(char *str)
  1162. {
  1163. acpi_gbl_auto_serialize_methods = FALSE;
  1164. pr_info("ACPI: auto-serialization disabled\n");
  1165. return 1;
  1166. }
  1167. __setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
  1168. /* Check of resource interference between native drivers and ACPI
  1169. * OperationRegions (SystemIO and System Memory only).
  1170. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  1171. * in arbitrary AML code and can interfere with legacy drivers.
  1172. * acpi_enforce_resources= can be set to:
  1173. *
  1174. * - strict (default) (2)
  1175. * -> further driver trying to access the resources will not load
  1176. * - lax (1)
  1177. * -> further driver trying to access the resources will load, but you
  1178. * get a system message that something might go wrong...
  1179. *
  1180. * - no (0)
  1181. * -> ACPI Operation Region resources will not be registered
  1182. *
  1183. */
  1184. #define ENFORCE_RESOURCES_STRICT 2
  1185. #define ENFORCE_RESOURCES_LAX 1
  1186. #define ENFORCE_RESOURCES_NO 0
  1187. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1188. static int __init acpi_enforce_resources_setup(char *str)
  1189. {
  1190. if (str == NULL || *str == '\0')
  1191. return 0;
  1192. if (!strcmp("strict", str))
  1193. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1194. else if (!strcmp("lax", str))
  1195. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  1196. else if (!strcmp("no", str))
  1197. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  1198. return 1;
  1199. }
  1200. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  1201. /* Check for resource conflicts between ACPI OperationRegions and native
  1202. * drivers */
  1203. int acpi_check_resource_conflict(const struct resource *res)
  1204. {
  1205. acpi_adr_space_type space_id;
  1206. acpi_size length;
  1207. u8 warn = 0;
  1208. int clash = 0;
  1209. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1210. return 0;
  1211. if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
  1212. return 0;
  1213. if (res->flags & IORESOURCE_IO)
  1214. space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  1215. else
  1216. space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
  1217. length = resource_size(res);
  1218. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO)
  1219. warn = 1;
  1220. clash = acpi_check_address_range(space_id, res->start, length, warn);
  1221. if (clash) {
  1222. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
  1223. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  1224. printk(KERN_NOTICE "ACPI: This conflict may"
  1225. " cause random problems and system"
  1226. " instability\n");
  1227. printk(KERN_INFO "ACPI: If an ACPI driver is available"
  1228. " for this device, you should use it instead of"
  1229. " the native driver\n");
  1230. }
  1231. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  1232. return -EBUSY;
  1233. }
  1234. return 0;
  1235. }
  1236. EXPORT_SYMBOL(acpi_check_resource_conflict);
  1237. int acpi_check_region(resource_size_t start, resource_size_t n,
  1238. const char *name)
  1239. {
  1240. struct resource res = {
  1241. .start = start,
  1242. .end = start + n - 1,
  1243. .name = name,
  1244. .flags = IORESOURCE_IO,
  1245. };
  1246. return acpi_check_resource_conflict(&res);
  1247. }
  1248. EXPORT_SYMBOL(acpi_check_region);
  1249. /*
  1250. * Let drivers know whether the resource checks are effective
  1251. */
  1252. int acpi_resources_are_enforced(void)
  1253. {
  1254. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  1255. }
  1256. EXPORT_SYMBOL(acpi_resources_are_enforced);
  1257. /*
  1258. * Deallocate the memory for a spinlock.
  1259. */
  1260. void acpi_os_delete_lock(acpi_spinlock handle)
  1261. {
  1262. ACPI_FREE(handle);
  1263. }
  1264. /*
  1265. * Acquire a spinlock.
  1266. *
  1267. * handle is a pointer to the spinlock_t.
  1268. */
  1269. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1270. {
  1271. acpi_cpu_flags flags;
  1272. spin_lock_irqsave(lockp, flags);
  1273. return flags;
  1274. }
  1275. /*
  1276. * Release a spinlock. See above.
  1277. */
  1278. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  1279. {
  1280. spin_unlock_irqrestore(lockp, flags);
  1281. }
  1282. #ifndef ACPI_USE_LOCAL_CACHE
  1283. /*******************************************************************************
  1284. *
  1285. * FUNCTION: acpi_os_create_cache
  1286. *
  1287. * PARAMETERS: name - Ascii name for the cache
  1288. * size - Size of each cached object
  1289. * depth - Maximum depth of the cache (in objects) <ignored>
  1290. * cache - Where the new cache object is returned
  1291. *
  1292. * RETURN: status
  1293. *
  1294. * DESCRIPTION: Create a cache object
  1295. *
  1296. ******************************************************************************/
  1297. acpi_status
  1298. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  1299. {
  1300. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1301. if (*cache == NULL)
  1302. return AE_ERROR;
  1303. else
  1304. return AE_OK;
  1305. }
  1306. /*******************************************************************************
  1307. *
  1308. * FUNCTION: acpi_os_purge_cache
  1309. *
  1310. * PARAMETERS: Cache - Handle to cache object
  1311. *
  1312. * RETURN: Status
  1313. *
  1314. * DESCRIPTION: Free all objects within the requested cache.
  1315. *
  1316. ******************************************************************************/
  1317. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  1318. {
  1319. kmem_cache_shrink(cache);
  1320. return (AE_OK);
  1321. }
  1322. /*******************************************************************************
  1323. *
  1324. * FUNCTION: acpi_os_delete_cache
  1325. *
  1326. * PARAMETERS: Cache - Handle to cache object
  1327. *
  1328. * RETURN: Status
  1329. *
  1330. * DESCRIPTION: Free all objects within the requested cache and delete the
  1331. * cache object.
  1332. *
  1333. ******************************************************************************/
  1334. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  1335. {
  1336. kmem_cache_destroy(cache);
  1337. return (AE_OK);
  1338. }
  1339. /*******************************************************************************
  1340. *
  1341. * FUNCTION: acpi_os_release_object
  1342. *
  1343. * PARAMETERS: Cache - Handle to cache object
  1344. * Object - The object to be released
  1345. *
  1346. * RETURN: None
  1347. *
  1348. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1349. * the object is deleted.
  1350. *
  1351. ******************************************************************************/
  1352. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1353. {
  1354. kmem_cache_free(cache, object);
  1355. return (AE_OK);
  1356. }
  1357. #endif
  1358. static int __init acpi_no_static_ssdt_setup(char *s)
  1359. {
  1360. acpi_gbl_disable_ssdt_table_install = TRUE;
  1361. pr_info("ACPI: static SSDT installation disabled\n");
  1362. return 0;
  1363. }
  1364. early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
  1365. static int __init acpi_disable_return_repair(char *s)
  1366. {
  1367. printk(KERN_NOTICE PREFIX
  1368. "ACPI: Predefined validation mechanism disabled\n");
  1369. acpi_gbl_disable_auto_repair = TRUE;
  1370. return 1;
  1371. }
  1372. __setup("acpica_no_return_repair", acpi_disable_return_repair);
  1373. acpi_status __init acpi_os_initialize(void)
  1374. {
  1375. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1376. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1377. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1378. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1379. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
  1380. /*
  1381. * Use acpi_os_map_generic_address to pre-map the reset
  1382. * register if it's in system memory.
  1383. */
  1384. int rv;
  1385. rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
  1386. pr_debug(PREFIX "%s: map reset_reg status %d\n", __func__, rv);
  1387. }
  1388. acpi_os_initialized = true;
  1389. return AE_OK;
  1390. }
  1391. acpi_status __init acpi_os_initialize1(void)
  1392. {
  1393. kacpid_wq = alloc_workqueue("kacpid", 0, 1);
  1394. kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
  1395. kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
  1396. BUG_ON(!kacpid_wq);
  1397. BUG_ON(!kacpi_notify_wq);
  1398. BUG_ON(!kacpi_hotplug_wq);
  1399. acpi_osi_init();
  1400. return AE_OK;
  1401. }
  1402. acpi_status acpi_os_terminate(void)
  1403. {
  1404. if (acpi_irq_handler) {
  1405. acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
  1406. acpi_irq_handler);
  1407. }
  1408. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1409. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1410. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1411. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1412. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
  1413. acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
  1414. destroy_workqueue(kacpid_wq);
  1415. destroy_workqueue(kacpi_notify_wq);
  1416. destroy_workqueue(kacpi_hotplug_wq);
  1417. return AE_OK;
  1418. }
  1419. acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
  1420. u32 pm1b_control)
  1421. {
  1422. int rc = 0;
  1423. if (__acpi_os_prepare_sleep)
  1424. rc = __acpi_os_prepare_sleep(sleep_state,
  1425. pm1a_control, pm1b_control);
  1426. if (rc < 0)
  1427. return AE_ERROR;
  1428. else if (rc > 0)
  1429. return AE_CTRL_SKIP;
  1430. return AE_OK;
  1431. }
  1432. void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
  1433. u32 pm1a_ctrl, u32 pm1b_ctrl))
  1434. {
  1435. __acpi_os_prepare_sleep = func;
  1436. }
  1437. acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
  1438. u32 val_b)
  1439. {
  1440. int rc = 0;
  1441. if (__acpi_os_prepare_extended_sleep)
  1442. rc = __acpi_os_prepare_extended_sleep(sleep_state,
  1443. val_a, val_b);
  1444. if (rc < 0)
  1445. return AE_ERROR;
  1446. else if (rc > 0)
  1447. return AE_CTRL_SKIP;
  1448. return AE_OK;
  1449. }
  1450. void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
  1451. u32 val_a, u32 val_b))
  1452. {
  1453. __acpi_os_prepare_extended_sleep = func;
  1454. }