osl.c 43 KB

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