osl.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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_obj(*map);
  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_percpu_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. u64 usec = ms * USEC_PER_MSEC, delta_us = 50;
  506. /*
  507. * Use a hrtimer because the timer wheel timers are optimized for
  508. * cancelation before they expire and this timer is not going to be
  509. * canceled.
  510. *
  511. * Set the delta between the requested sleep time and the effective
  512. * deadline to at least 50 us in case there is an opportunity for timer
  513. * coalescing.
  514. *
  515. * Moreover, longer sleeps can be assumed to need somewhat less timer
  516. * precision, so sacrifice some of it for making the timer a more likely
  517. * candidate for coalescing by setting the delta to 1% of the sleep time
  518. * if it is above 5 ms (this value is chosen so that the delta is a
  519. * continuous function of the sleep time).
  520. */
  521. if (ms > 5)
  522. delta_us = (USEC_PER_MSEC / 100) * ms;
  523. usleep_range(usec, usec + delta_us);
  524. }
  525. void acpi_os_stall(u32 us)
  526. {
  527. while (us) {
  528. u32 delay = 1000;
  529. if (delay > us)
  530. delay = us;
  531. udelay(delay);
  532. touch_nmi_watchdog();
  533. us -= delay;
  534. }
  535. }
  536. /*
  537. * Support ACPI 3.0 AML Timer operand. Returns a 64-bit free-running,
  538. * monotonically increasing timer with 100ns granularity. Do not use
  539. * ktime_get() to implement this function because this function may get
  540. * called after timekeeping has been suspended. Note: calling this function
  541. * after timekeeping has been suspended may lead to unexpected results
  542. * because when timekeeping is suspended the jiffies counter is not
  543. * incremented. See also timekeeping_suspend().
  544. */
  545. u64 acpi_os_get_timer(void)
  546. {
  547. return (get_jiffies_64() - INITIAL_JIFFIES) *
  548. (ACPI_100NSEC_PER_SEC / HZ);
  549. }
  550. acpi_status acpi_os_read_port(acpi_io_address port, u32 *value, u32 width)
  551. {
  552. u32 dummy;
  553. if (!IS_ENABLED(CONFIG_HAS_IOPORT)) {
  554. /*
  555. * set all-1 result as if reading from non-existing
  556. * I/O port
  557. */
  558. *value = GENMASK(width, 0);
  559. return AE_NOT_IMPLEMENTED;
  560. }
  561. if (value)
  562. *value = 0;
  563. else
  564. value = &dummy;
  565. if (width <= 8) {
  566. *value = inb(port);
  567. } else if (width <= 16) {
  568. *value = inw(port);
  569. } else if (width <= 32) {
  570. *value = inl(port);
  571. } else {
  572. pr_debug("%s: Access width %d not supported\n", __func__, width);
  573. return AE_BAD_PARAMETER;
  574. }
  575. return AE_OK;
  576. }
  577. EXPORT_SYMBOL(acpi_os_read_port);
  578. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  579. {
  580. if (!IS_ENABLED(CONFIG_HAS_IOPORT))
  581. return AE_NOT_IMPLEMENTED;
  582. if (width <= 8) {
  583. outb(value, port);
  584. } else if (width <= 16) {
  585. outw(value, port);
  586. } else if (width <= 32) {
  587. outl(value, port);
  588. } else {
  589. pr_debug("%s: Access width %d not supported\n", __func__, width);
  590. return AE_BAD_PARAMETER;
  591. }
  592. return AE_OK;
  593. }
  594. EXPORT_SYMBOL(acpi_os_write_port);
  595. int acpi_os_read_iomem(void __iomem *virt_addr, u64 *value, u32 width)
  596. {
  597. switch (width) {
  598. case 8:
  599. *(u8 *) value = readb(virt_addr);
  600. break;
  601. case 16:
  602. *(u16 *) value = readw(virt_addr);
  603. break;
  604. case 32:
  605. *(u32 *) value = readl(virt_addr);
  606. break;
  607. case 64:
  608. *(u64 *) value = readq(virt_addr);
  609. break;
  610. default:
  611. return -EINVAL;
  612. }
  613. return 0;
  614. }
  615. acpi_status
  616. acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
  617. {
  618. void __iomem *virt_addr;
  619. unsigned int size = width / 8;
  620. bool unmap = false;
  621. u64 dummy;
  622. int error;
  623. rcu_read_lock();
  624. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  625. if (!virt_addr) {
  626. rcu_read_unlock();
  627. virt_addr = acpi_os_ioremap(phys_addr, size);
  628. if (!virt_addr)
  629. return AE_BAD_ADDRESS;
  630. unmap = true;
  631. }
  632. if (!value)
  633. value = &dummy;
  634. error = acpi_os_read_iomem(virt_addr, value, width);
  635. BUG_ON(error);
  636. if (unmap)
  637. iounmap(virt_addr);
  638. else
  639. rcu_read_unlock();
  640. return AE_OK;
  641. }
  642. acpi_status
  643. acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
  644. {
  645. void __iomem *virt_addr;
  646. unsigned int size = width / 8;
  647. bool unmap = false;
  648. rcu_read_lock();
  649. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  650. if (!virt_addr) {
  651. rcu_read_unlock();
  652. virt_addr = acpi_os_ioremap(phys_addr, size);
  653. if (!virt_addr)
  654. return AE_BAD_ADDRESS;
  655. unmap = true;
  656. }
  657. switch (width) {
  658. case 8:
  659. writeb(value, virt_addr);
  660. break;
  661. case 16:
  662. writew(value, virt_addr);
  663. break;
  664. case 32:
  665. writel(value, virt_addr);
  666. break;
  667. case 64:
  668. writeq(value, virt_addr);
  669. break;
  670. default:
  671. BUG();
  672. }
  673. if (unmap)
  674. iounmap(virt_addr);
  675. else
  676. rcu_read_unlock();
  677. return AE_OK;
  678. }
  679. #ifdef CONFIG_PCI
  680. acpi_status
  681. acpi_os_read_pci_configuration(struct acpi_pci_id *pci_id, u32 reg,
  682. u64 *value, u32 width)
  683. {
  684. int result, size;
  685. u32 value32;
  686. if (!value)
  687. return AE_BAD_PARAMETER;
  688. switch (width) {
  689. case 8:
  690. size = 1;
  691. break;
  692. case 16:
  693. size = 2;
  694. break;
  695. case 32:
  696. size = 4;
  697. break;
  698. default:
  699. return AE_ERROR;
  700. }
  701. result = raw_pci_read(pci_id->segment, pci_id->bus,
  702. PCI_DEVFN(pci_id->device, pci_id->function),
  703. reg, size, &value32);
  704. *value = value32;
  705. return (result ? AE_ERROR : AE_OK);
  706. }
  707. acpi_status
  708. acpi_os_write_pci_configuration(struct acpi_pci_id *pci_id, u32 reg,
  709. u64 value, u32 width)
  710. {
  711. int result, size;
  712. switch (width) {
  713. case 8:
  714. size = 1;
  715. break;
  716. case 16:
  717. size = 2;
  718. break;
  719. case 32:
  720. size = 4;
  721. break;
  722. default:
  723. return AE_ERROR;
  724. }
  725. result = raw_pci_write(pci_id->segment, pci_id->bus,
  726. PCI_DEVFN(pci_id->device, pci_id->function),
  727. reg, size, value);
  728. return (result ? AE_ERROR : AE_OK);
  729. }
  730. #endif
  731. static void acpi_os_execute_deferred(struct work_struct *work)
  732. {
  733. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  734. dpc->function(dpc->context);
  735. kfree(dpc);
  736. }
  737. #ifdef CONFIG_ACPI_DEBUGGER
  738. static struct acpi_debugger acpi_debugger;
  739. static bool acpi_debugger_initialized;
  740. int acpi_register_debugger(struct module *owner,
  741. const struct acpi_debugger_ops *ops)
  742. {
  743. int ret = 0;
  744. mutex_lock(&acpi_debugger.lock);
  745. if (acpi_debugger.ops) {
  746. ret = -EBUSY;
  747. goto err_lock;
  748. }
  749. acpi_debugger.owner = owner;
  750. acpi_debugger.ops = ops;
  751. err_lock:
  752. mutex_unlock(&acpi_debugger.lock);
  753. return ret;
  754. }
  755. EXPORT_SYMBOL(acpi_register_debugger);
  756. void acpi_unregister_debugger(const struct acpi_debugger_ops *ops)
  757. {
  758. mutex_lock(&acpi_debugger.lock);
  759. if (ops == acpi_debugger.ops) {
  760. acpi_debugger.ops = NULL;
  761. acpi_debugger.owner = NULL;
  762. }
  763. mutex_unlock(&acpi_debugger.lock);
  764. }
  765. EXPORT_SYMBOL(acpi_unregister_debugger);
  766. int acpi_debugger_create_thread(acpi_osd_exec_callback function, void *context)
  767. {
  768. int ret;
  769. int (*func)(acpi_osd_exec_callback, void *);
  770. struct module *owner;
  771. if (!acpi_debugger_initialized)
  772. return -ENODEV;
  773. mutex_lock(&acpi_debugger.lock);
  774. if (!acpi_debugger.ops) {
  775. ret = -ENODEV;
  776. goto err_lock;
  777. }
  778. if (!try_module_get(acpi_debugger.owner)) {
  779. ret = -ENODEV;
  780. goto err_lock;
  781. }
  782. func = acpi_debugger.ops->create_thread;
  783. owner = acpi_debugger.owner;
  784. mutex_unlock(&acpi_debugger.lock);
  785. ret = func(function, context);
  786. mutex_lock(&acpi_debugger.lock);
  787. module_put(owner);
  788. err_lock:
  789. mutex_unlock(&acpi_debugger.lock);
  790. return ret;
  791. }
  792. ssize_t acpi_debugger_write_log(const char *msg)
  793. {
  794. ssize_t ret;
  795. ssize_t (*func)(const char *);
  796. struct module *owner;
  797. if (!acpi_debugger_initialized)
  798. return -ENODEV;
  799. mutex_lock(&acpi_debugger.lock);
  800. if (!acpi_debugger.ops) {
  801. ret = -ENODEV;
  802. goto err_lock;
  803. }
  804. if (!try_module_get(acpi_debugger.owner)) {
  805. ret = -ENODEV;
  806. goto err_lock;
  807. }
  808. func = acpi_debugger.ops->write_log;
  809. owner = acpi_debugger.owner;
  810. mutex_unlock(&acpi_debugger.lock);
  811. ret = func(msg);
  812. mutex_lock(&acpi_debugger.lock);
  813. module_put(owner);
  814. err_lock:
  815. mutex_unlock(&acpi_debugger.lock);
  816. return ret;
  817. }
  818. ssize_t acpi_debugger_read_cmd(char *buffer, size_t buffer_length)
  819. {
  820. ssize_t ret;
  821. ssize_t (*func)(char *, size_t);
  822. struct module *owner;
  823. if (!acpi_debugger_initialized)
  824. return -ENODEV;
  825. mutex_lock(&acpi_debugger.lock);
  826. if (!acpi_debugger.ops) {
  827. ret = -ENODEV;
  828. goto err_lock;
  829. }
  830. if (!try_module_get(acpi_debugger.owner)) {
  831. ret = -ENODEV;
  832. goto err_lock;
  833. }
  834. func = acpi_debugger.ops->read_cmd;
  835. owner = acpi_debugger.owner;
  836. mutex_unlock(&acpi_debugger.lock);
  837. ret = func(buffer, buffer_length);
  838. mutex_lock(&acpi_debugger.lock);
  839. module_put(owner);
  840. err_lock:
  841. mutex_unlock(&acpi_debugger.lock);
  842. return ret;
  843. }
  844. int acpi_debugger_wait_command_ready(void)
  845. {
  846. int ret;
  847. int (*func)(bool, char *, size_t);
  848. struct module *owner;
  849. if (!acpi_debugger_initialized)
  850. return -ENODEV;
  851. mutex_lock(&acpi_debugger.lock);
  852. if (!acpi_debugger.ops) {
  853. ret = -ENODEV;
  854. goto err_lock;
  855. }
  856. if (!try_module_get(acpi_debugger.owner)) {
  857. ret = -ENODEV;
  858. goto err_lock;
  859. }
  860. func = acpi_debugger.ops->wait_command_ready;
  861. owner = acpi_debugger.owner;
  862. mutex_unlock(&acpi_debugger.lock);
  863. ret = func(acpi_gbl_method_executing,
  864. acpi_gbl_db_line_buf, ACPI_DB_LINE_BUFFER_SIZE);
  865. mutex_lock(&acpi_debugger.lock);
  866. module_put(owner);
  867. err_lock:
  868. mutex_unlock(&acpi_debugger.lock);
  869. return ret;
  870. }
  871. int acpi_debugger_notify_command_complete(void)
  872. {
  873. int ret;
  874. int (*func)(void);
  875. struct module *owner;
  876. if (!acpi_debugger_initialized)
  877. return -ENODEV;
  878. mutex_lock(&acpi_debugger.lock);
  879. if (!acpi_debugger.ops) {
  880. ret = -ENODEV;
  881. goto err_lock;
  882. }
  883. if (!try_module_get(acpi_debugger.owner)) {
  884. ret = -ENODEV;
  885. goto err_lock;
  886. }
  887. func = acpi_debugger.ops->notify_command_complete;
  888. owner = acpi_debugger.owner;
  889. mutex_unlock(&acpi_debugger.lock);
  890. ret = func();
  891. mutex_lock(&acpi_debugger.lock);
  892. module_put(owner);
  893. err_lock:
  894. mutex_unlock(&acpi_debugger.lock);
  895. return ret;
  896. }
  897. int __init acpi_debugger_init(void)
  898. {
  899. mutex_init(&acpi_debugger.lock);
  900. acpi_debugger_initialized = true;
  901. return 0;
  902. }
  903. #endif
  904. /*******************************************************************************
  905. *
  906. * FUNCTION: acpi_os_execute
  907. *
  908. * PARAMETERS: Type - Type of the callback
  909. * Function - Function to be executed
  910. * Context - Function parameters
  911. *
  912. * RETURN: Status
  913. *
  914. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  915. * immediately executes function on a separate thread.
  916. *
  917. ******************************************************************************/
  918. acpi_status acpi_os_execute(acpi_execute_type type,
  919. acpi_osd_exec_callback function, void *context)
  920. {
  921. struct acpi_os_dpc *dpc;
  922. int ret;
  923. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  924. "Scheduling function [%p(%p)] for deferred execution.\n",
  925. function, context));
  926. if (type == OSL_DEBUGGER_MAIN_THREAD) {
  927. ret = acpi_debugger_create_thread(function, context);
  928. if (ret) {
  929. pr_err("Kernel thread creation failed\n");
  930. return AE_ERROR;
  931. }
  932. return AE_OK;
  933. }
  934. /*
  935. * Allocate/initialize DPC structure. Note that this memory will be
  936. * freed by the callee. The kernel handles the work_struct list in a
  937. * way that allows us to also free its memory inside the callee.
  938. * Because we may want to schedule several tasks with different
  939. * parameters we can't use the approach some kernel code uses of
  940. * having a static work_struct.
  941. */
  942. dpc = kzalloc_obj(struct acpi_os_dpc, GFP_ATOMIC);
  943. if (!dpc)
  944. return AE_NO_MEMORY;
  945. dpc->function = function;
  946. dpc->context = context;
  947. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  948. /*
  949. * To prevent lockdep from complaining unnecessarily, make sure that
  950. * there is a different static lockdep key for each workqueue by using
  951. * INIT_WORK() for each of them separately.
  952. */
  953. switch (type) {
  954. case OSL_NOTIFY_HANDLER:
  955. ret = queue_work(kacpi_notify_wq, &dpc->work);
  956. break;
  957. case OSL_GPE_HANDLER:
  958. /*
  959. * On some machines, a software-initiated SMI causes corruption
  960. * unless the SMI runs on CPU 0. An SMI can be initiated by
  961. * any AML, but typically it's done in GPE-related methods that
  962. * are run via workqueues, so we can avoid the known corruption
  963. * cases by always queueing on CPU 0.
  964. */
  965. ret = queue_work_on(0, kacpid_wq, &dpc->work);
  966. break;
  967. default:
  968. pr_err("Unsupported os_execute type %d.\n", type);
  969. goto err;
  970. }
  971. if (!ret) {
  972. pr_err("Unable to queue work\n");
  973. goto err;
  974. }
  975. return AE_OK;
  976. err:
  977. kfree(dpc);
  978. return AE_ERROR;
  979. }
  980. EXPORT_SYMBOL(acpi_os_execute);
  981. void acpi_os_wait_events_complete(void)
  982. {
  983. /*
  984. * Make sure the GPE handler or the fixed event handler is not used
  985. * on another CPU after removal.
  986. */
  987. if (acpi_sci_irq_valid())
  988. synchronize_hardirq(acpi_sci_irq);
  989. flush_workqueue(kacpid_wq);
  990. flush_workqueue(kacpi_notify_wq);
  991. }
  992. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  993. struct acpi_hp_work {
  994. struct work_struct work;
  995. struct acpi_device *adev;
  996. u32 src;
  997. };
  998. static void acpi_hotplug_work_fn(struct work_struct *work)
  999. {
  1000. struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
  1001. acpi_os_wait_events_complete();
  1002. acpi_device_hotplug(hpw->adev, hpw->src);
  1003. kfree(hpw);
  1004. }
  1005. acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
  1006. {
  1007. struct acpi_hp_work *hpw;
  1008. acpi_handle_debug(adev->handle,
  1009. "Scheduling hotplug event %u for deferred handling\n",
  1010. src);
  1011. hpw = kmalloc_obj(*hpw);
  1012. if (!hpw)
  1013. return AE_NO_MEMORY;
  1014. INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
  1015. hpw->adev = adev;
  1016. hpw->src = src;
  1017. /*
  1018. * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
  1019. * the hotplug code may call driver .remove() functions, which may
  1020. * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
  1021. * these workqueues.
  1022. */
  1023. if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
  1024. kfree(hpw);
  1025. return AE_ERROR;
  1026. }
  1027. return AE_OK;
  1028. }
  1029. bool acpi_queue_hotplug_work(struct work_struct *work)
  1030. {
  1031. return queue_work(kacpi_hotplug_wq, work);
  1032. }
  1033. acpi_status
  1034. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle *handle)
  1035. {
  1036. struct semaphore *sem = NULL;
  1037. sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
  1038. if (!sem)
  1039. return AE_NO_MEMORY;
  1040. sema_init(sem, initial_units);
  1041. *handle = (acpi_handle *) sem;
  1042. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  1043. *handle, initial_units));
  1044. return AE_OK;
  1045. }
  1046. /*
  1047. * TODO: A better way to delete semaphores? Linux doesn't have a
  1048. * 'delete_semaphore()' function -- may result in an invalid
  1049. * pointer dereference for non-synchronized consumers. Should
  1050. * we at least check for blocked threads and signal/cancel them?
  1051. */
  1052. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  1053. {
  1054. struct semaphore *sem = (struct semaphore *)handle;
  1055. if (!sem)
  1056. return AE_BAD_PARAMETER;
  1057. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  1058. BUG_ON(!list_empty(&sem->wait_list));
  1059. kfree(sem);
  1060. sem = NULL;
  1061. return AE_OK;
  1062. }
  1063. /*
  1064. * TODO: Support for units > 1?
  1065. */
  1066. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  1067. {
  1068. acpi_status status = AE_OK;
  1069. struct semaphore *sem = (struct semaphore *)handle;
  1070. long jiffies;
  1071. int ret = 0;
  1072. if (!acpi_os_initialized)
  1073. return AE_OK;
  1074. if (!sem || (units < 1))
  1075. return AE_BAD_PARAMETER;
  1076. if (units > 1)
  1077. return AE_SUPPORT;
  1078. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  1079. handle, units, timeout));
  1080. if (timeout == ACPI_WAIT_FOREVER)
  1081. jiffies = MAX_SCHEDULE_TIMEOUT;
  1082. else
  1083. jiffies = msecs_to_jiffies(timeout);
  1084. ret = down_timeout(sem, jiffies);
  1085. if (ret)
  1086. status = AE_TIME;
  1087. if (ACPI_FAILURE(status)) {
  1088. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1089. "Failed to acquire semaphore[%p|%d|%d], %s",
  1090. handle, units, timeout,
  1091. acpi_format_exception(status)));
  1092. } else {
  1093. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1094. "Acquired semaphore[%p|%d|%d]", handle,
  1095. units, timeout));
  1096. }
  1097. return status;
  1098. }
  1099. /*
  1100. * TODO: Support for units > 1?
  1101. */
  1102. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  1103. {
  1104. struct semaphore *sem = (struct semaphore *)handle;
  1105. if (!acpi_os_initialized)
  1106. return AE_OK;
  1107. if (!sem || (units < 1))
  1108. return AE_BAD_PARAMETER;
  1109. if (units > 1)
  1110. return AE_SUPPORT;
  1111. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  1112. units));
  1113. up(sem);
  1114. return AE_OK;
  1115. }
  1116. acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
  1117. {
  1118. #ifdef ENABLE_DEBUGGER
  1119. if (acpi_in_debugger) {
  1120. u32 chars;
  1121. kdb_read(buffer, buffer_length);
  1122. /* remove the CR kdb includes */
  1123. chars = strlen(buffer) - 1;
  1124. buffer[chars] = '\0';
  1125. }
  1126. #else
  1127. int ret;
  1128. ret = acpi_debugger_read_cmd(buffer, buffer_length);
  1129. if (ret < 0)
  1130. return AE_ERROR;
  1131. if (bytes_read)
  1132. *bytes_read = ret;
  1133. #endif
  1134. return AE_OK;
  1135. }
  1136. EXPORT_SYMBOL(acpi_os_get_line);
  1137. acpi_status acpi_os_wait_command_ready(void)
  1138. {
  1139. int ret;
  1140. ret = acpi_debugger_wait_command_ready();
  1141. if (ret < 0)
  1142. return AE_ERROR;
  1143. return AE_OK;
  1144. }
  1145. acpi_status acpi_os_notify_command_complete(void)
  1146. {
  1147. int ret;
  1148. ret = acpi_debugger_notify_command_complete();
  1149. if (ret < 0)
  1150. return AE_ERROR;
  1151. return AE_OK;
  1152. }
  1153. acpi_status acpi_os_signal(u32 function, void *info)
  1154. {
  1155. switch (function) {
  1156. case ACPI_SIGNAL_FATAL:
  1157. pr_err("Fatal opcode executed\n");
  1158. break;
  1159. case ACPI_SIGNAL_BREAKPOINT:
  1160. /*
  1161. * AML Breakpoint
  1162. * ACPI spec. says to treat it as a NOP unless
  1163. * you are debugging. So if/when we integrate
  1164. * AML debugger into the kernel debugger its
  1165. * hook will go here. But until then it is
  1166. * not useful to print anything on breakpoints.
  1167. */
  1168. break;
  1169. default:
  1170. break;
  1171. }
  1172. return AE_OK;
  1173. }
  1174. static int __init acpi_os_name_setup(char *str)
  1175. {
  1176. char *p = acpi_os_name;
  1177. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  1178. if (!str || !*str)
  1179. return 0;
  1180. for (; count-- && *str; str++) {
  1181. if (isalnum(*str) || *str == ' ' || *str == ':')
  1182. *p++ = *str;
  1183. else if (*str == '\'' || *str == '"')
  1184. continue;
  1185. else
  1186. break;
  1187. }
  1188. *p = 0;
  1189. return 1;
  1190. }
  1191. __setup("acpi_os_name=", acpi_os_name_setup);
  1192. /*
  1193. * Disable the auto-serialization of named objects creation methods.
  1194. *
  1195. * This feature is enabled by default. It marks the AML control methods
  1196. * that contain the opcodes to create named objects as "Serialized".
  1197. */
  1198. static int __init acpi_no_auto_serialize_setup(char *str)
  1199. {
  1200. acpi_gbl_auto_serialize_methods = FALSE;
  1201. pr_info("Auto-serialization disabled\n");
  1202. return 1;
  1203. }
  1204. __setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
  1205. /* Check of resource interference between native drivers and ACPI
  1206. * OperationRegions (SystemIO and System Memory only).
  1207. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  1208. * in arbitrary AML code and can interfere with legacy drivers.
  1209. * acpi_enforce_resources= can be set to:
  1210. *
  1211. * - strict (default) (2)
  1212. * -> further driver trying to access the resources will not load
  1213. * - lax (1)
  1214. * -> further driver trying to access the resources will load, but you
  1215. * get a system message that something might go wrong...
  1216. *
  1217. * - no (0)
  1218. * -> ACPI Operation Region resources will not be registered
  1219. *
  1220. */
  1221. #define ENFORCE_RESOURCES_STRICT 2
  1222. #define ENFORCE_RESOURCES_LAX 1
  1223. #define ENFORCE_RESOURCES_NO 0
  1224. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1225. static int __init acpi_enforce_resources_setup(char *str)
  1226. {
  1227. if (str == NULL || *str == '\0')
  1228. return 0;
  1229. if (!strcmp("strict", str))
  1230. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1231. else if (!strcmp("lax", str))
  1232. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  1233. else if (!strcmp("no", str))
  1234. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  1235. return 1;
  1236. }
  1237. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  1238. /* Check for resource conflicts between ACPI OperationRegions and native
  1239. * drivers */
  1240. int acpi_check_resource_conflict(const struct resource *res)
  1241. {
  1242. acpi_adr_space_type space_id;
  1243. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1244. return 0;
  1245. if (res->flags & IORESOURCE_IO)
  1246. space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  1247. else if (res->flags & IORESOURCE_MEM)
  1248. space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
  1249. else
  1250. return 0;
  1251. if (!acpi_check_address_range(space_id, res->start, resource_size(res), 1))
  1252. return 0;
  1253. pr_info("Resource conflict; ACPI support missing from driver?\n");
  1254. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  1255. return -EBUSY;
  1256. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  1257. pr_notice("Resource conflict: System may be unstable or behave erratically\n");
  1258. return 0;
  1259. }
  1260. EXPORT_SYMBOL(acpi_check_resource_conflict);
  1261. int acpi_check_region(resource_size_t start, resource_size_t n,
  1262. const char *name)
  1263. {
  1264. struct resource res = DEFINE_RES_IO_NAMED(start, n, name);
  1265. return acpi_check_resource_conflict(&res);
  1266. }
  1267. EXPORT_SYMBOL(acpi_check_region);
  1268. /*
  1269. * Let drivers know whether the resource checks are effective
  1270. */
  1271. int acpi_resources_are_enforced(void)
  1272. {
  1273. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  1274. }
  1275. EXPORT_SYMBOL(acpi_resources_are_enforced);
  1276. /*
  1277. * Deallocate the memory for a spinlock.
  1278. */
  1279. void acpi_os_delete_lock(acpi_spinlock handle)
  1280. {
  1281. ACPI_FREE(handle);
  1282. }
  1283. /*
  1284. * Acquire a spinlock.
  1285. *
  1286. * handle is a pointer to the spinlock_t.
  1287. */
  1288. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1289. __acquires(lockp)
  1290. {
  1291. spin_lock(lockp);
  1292. return 0;
  1293. }
  1294. /*
  1295. * Release a spinlock. See above.
  1296. */
  1297. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags not_used)
  1298. __releases(lockp)
  1299. {
  1300. spin_unlock(lockp);
  1301. }
  1302. #ifndef ACPI_USE_LOCAL_CACHE
  1303. /*******************************************************************************
  1304. *
  1305. * FUNCTION: acpi_os_create_cache
  1306. *
  1307. * PARAMETERS: name - Ascii name for the cache
  1308. * size - Size of each cached object
  1309. * depth - Maximum depth of the cache (in objects) <ignored>
  1310. * cache - Where the new cache object is returned
  1311. *
  1312. * RETURN: status
  1313. *
  1314. * DESCRIPTION: Create a cache object
  1315. *
  1316. ******************************************************************************/
  1317. acpi_status
  1318. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t **cache)
  1319. {
  1320. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1321. if (*cache == NULL)
  1322. return AE_ERROR;
  1323. else
  1324. return AE_OK;
  1325. }
  1326. /*******************************************************************************
  1327. *
  1328. * FUNCTION: acpi_os_purge_cache
  1329. *
  1330. * PARAMETERS: Cache - Handle to cache object
  1331. *
  1332. * RETURN: Status
  1333. *
  1334. * DESCRIPTION: Free all objects within the requested cache.
  1335. *
  1336. ******************************************************************************/
  1337. acpi_status acpi_os_purge_cache(acpi_cache_t *cache)
  1338. {
  1339. kmem_cache_shrink(cache);
  1340. return AE_OK;
  1341. }
  1342. /*******************************************************************************
  1343. *
  1344. * FUNCTION: acpi_os_delete_cache
  1345. *
  1346. * PARAMETERS: Cache - Handle to cache object
  1347. *
  1348. * RETURN: Status
  1349. *
  1350. * DESCRIPTION: Free all objects within the requested cache and delete the
  1351. * cache object.
  1352. *
  1353. ******************************************************************************/
  1354. acpi_status acpi_os_delete_cache(acpi_cache_t *cache)
  1355. {
  1356. kmem_cache_destroy(cache);
  1357. return AE_OK;
  1358. }
  1359. /*******************************************************************************
  1360. *
  1361. * FUNCTION: acpi_os_release_object
  1362. *
  1363. * PARAMETERS: Cache - Handle to cache object
  1364. * Object - The object to be released
  1365. *
  1366. * RETURN: None
  1367. *
  1368. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1369. * the object is deleted.
  1370. *
  1371. ******************************************************************************/
  1372. acpi_status acpi_os_release_object(acpi_cache_t *cache, void *object)
  1373. {
  1374. kmem_cache_free(cache, object);
  1375. return AE_OK;
  1376. }
  1377. #endif
  1378. static int __init acpi_no_static_ssdt_setup(char *s)
  1379. {
  1380. acpi_gbl_disable_ssdt_table_install = TRUE;
  1381. pr_info("Static SSDT installation disabled\n");
  1382. return 0;
  1383. }
  1384. early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
  1385. static int __init acpi_disable_return_repair(char *s)
  1386. {
  1387. pr_notice("Predefined validation mechanism disabled\n");
  1388. acpi_gbl_disable_auto_repair = TRUE;
  1389. return 1;
  1390. }
  1391. __setup("acpica_no_return_repair", acpi_disable_return_repair);
  1392. acpi_status __init acpi_os_initialize(void)
  1393. {
  1394. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1395. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1396. acpi_gbl_xgpe0_block_logical_address =
  1397. (unsigned long)acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1398. acpi_gbl_xgpe1_block_logical_address =
  1399. (unsigned long)acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1400. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
  1401. /*
  1402. * Use acpi_os_map_generic_address to pre-map the reset
  1403. * register if it's in system memory.
  1404. */
  1405. void __iomem *rv;
  1406. rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
  1407. pr_debug("%s: Reset register mapping %s\n", __func__,
  1408. rv ? "successful" : "failed");
  1409. }
  1410. acpi_os_initialized = true;
  1411. return AE_OK;
  1412. }
  1413. acpi_status __init acpi_os_initialize1(void)
  1414. {
  1415. kacpid_wq = alloc_workqueue("kacpid", WQ_PERCPU, 1);
  1416. kacpi_notify_wq = alloc_workqueue("kacpi_notify", WQ_PERCPU, 0);
  1417. kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
  1418. BUG_ON(!kacpid_wq);
  1419. BUG_ON(!kacpi_notify_wq);
  1420. BUG_ON(!kacpi_hotplug_wq);
  1421. acpi_osi_init();
  1422. return AE_OK;
  1423. }
  1424. acpi_status acpi_os_terminate(void)
  1425. {
  1426. if (acpi_irq_handler) {
  1427. acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
  1428. acpi_irq_handler);
  1429. }
  1430. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1431. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1432. acpi_gbl_xgpe0_block_logical_address = 0UL;
  1433. acpi_gbl_xgpe1_block_logical_address = 0UL;
  1434. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1435. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1436. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
  1437. acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
  1438. destroy_workqueue(kacpid_wq);
  1439. destroy_workqueue(kacpi_notify_wq);
  1440. destroy_workqueue(kacpi_hotplug_wq);
  1441. return AE_OK;
  1442. }
  1443. acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
  1444. u32 pm1b_control)
  1445. {
  1446. int rc = 0;
  1447. if (__acpi_os_prepare_sleep)
  1448. rc = __acpi_os_prepare_sleep(sleep_state,
  1449. pm1a_control, pm1b_control);
  1450. if (rc < 0)
  1451. return AE_ERROR;
  1452. else if (rc > 0)
  1453. return AE_CTRL_TERMINATE;
  1454. return AE_OK;
  1455. }
  1456. void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
  1457. u32 pm1a_ctrl, u32 pm1b_ctrl))
  1458. {
  1459. __acpi_os_prepare_sleep = func;
  1460. }
  1461. #if (ACPI_REDUCED_HARDWARE)
  1462. acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
  1463. u32 val_b)
  1464. {
  1465. int rc = 0;
  1466. if (__acpi_os_prepare_extended_sleep)
  1467. rc = __acpi_os_prepare_extended_sleep(sleep_state,
  1468. val_a, val_b);
  1469. if (rc < 0)
  1470. return AE_ERROR;
  1471. else if (rc > 0)
  1472. return AE_CTRL_TERMINATE;
  1473. return AE_OK;
  1474. }
  1475. #else
  1476. acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
  1477. u32 val_b)
  1478. {
  1479. return AE_OK;
  1480. }
  1481. #endif
  1482. void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
  1483. u32 val_a, u32 val_b))
  1484. {
  1485. __acpi_os_prepare_extended_sleep = func;
  1486. }
  1487. acpi_status acpi_os_enter_sleep(u8 sleep_state,
  1488. u32 reg_a_value, u32 reg_b_value)
  1489. {
  1490. acpi_status status;
  1491. if (acpi_gbl_reduced_hardware)
  1492. status = acpi_os_prepare_extended_sleep(sleep_state,
  1493. reg_a_value,
  1494. reg_b_value);
  1495. else
  1496. status = acpi_os_prepare_sleep(sleep_state,
  1497. reg_a_value, reg_b_value);
  1498. return status;
  1499. }