osl.c 45 KB

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