osl.c 41 KB

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