osl.c 42 KB

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