osl.c 44 KB

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