osl.c 40 KB

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