osl.c 40 KB

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