osl.c 44 KB

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