osl.c 40 KB

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