osl.c 42 KB

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