osl.c 42 KB

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