osl.c 39 KB

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