osl.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  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(EFI_CONFIG_TABLES)) {
  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, 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, LOCKDEP_NOW_UNRELIABLE);
  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 | IRQF_NO_SUSPEND, "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. u64 time_ns = ktime_to_ns(ktime_get());
  695. do_div(time_ns, 100);
  696. return time_ns;
  697. }
  698. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  699. {
  700. u32 dummy;
  701. if (!value)
  702. value = &dummy;
  703. *value = 0;
  704. if (width <= 8) {
  705. *(u8 *) value = inb(port);
  706. } else if (width <= 16) {
  707. *(u16 *) value = inw(port);
  708. } else if (width <= 32) {
  709. *(u32 *) value = inl(port);
  710. } else {
  711. BUG();
  712. }
  713. return AE_OK;
  714. }
  715. EXPORT_SYMBOL(acpi_os_read_port);
  716. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  717. {
  718. if (width <= 8) {
  719. outb(value, port);
  720. } else if (width <= 16) {
  721. outw(value, port);
  722. } else if (width <= 32) {
  723. outl(value, port);
  724. } else {
  725. BUG();
  726. }
  727. return AE_OK;
  728. }
  729. EXPORT_SYMBOL(acpi_os_write_port);
  730. #ifdef readq
  731. static inline u64 read64(const volatile void __iomem *addr)
  732. {
  733. return readq(addr);
  734. }
  735. #else
  736. static inline u64 read64(const volatile void __iomem *addr)
  737. {
  738. u64 l, h;
  739. l = readl(addr);
  740. h = readl(addr+4);
  741. return l | (h << 32);
  742. }
  743. #endif
  744. acpi_status
  745. acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
  746. {
  747. void __iomem *virt_addr;
  748. unsigned int size = width / 8;
  749. bool unmap = false;
  750. u64 dummy;
  751. rcu_read_lock();
  752. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  753. if (!virt_addr) {
  754. rcu_read_unlock();
  755. virt_addr = acpi_os_ioremap(phys_addr, size);
  756. if (!virt_addr)
  757. return AE_BAD_ADDRESS;
  758. unmap = true;
  759. }
  760. if (!value)
  761. value = &dummy;
  762. switch (width) {
  763. case 8:
  764. *(u8 *) value = readb(virt_addr);
  765. break;
  766. case 16:
  767. *(u16 *) value = readw(virt_addr);
  768. break;
  769. case 32:
  770. *(u32 *) value = readl(virt_addr);
  771. break;
  772. case 64:
  773. *(u64 *) value = read64(virt_addr);
  774. break;
  775. default:
  776. BUG();
  777. }
  778. if (unmap)
  779. iounmap(virt_addr);
  780. else
  781. rcu_read_unlock();
  782. return AE_OK;
  783. }
  784. #ifdef writeq
  785. static inline void write64(u64 val, volatile void __iomem *addr)
  786. {
  787. writeq(val, addr);
  788. }
  789. #else
  790. static inline void write64(u64 val, volatile void __iomem *addr)
  791. {
  792. writel(val, addr);
  793. writel(val>>32, addr+4);
  794. }
  795. #endif
  796. acpi_status
  797. acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
  798. {
  799. void __iomem *virt_addr;
  800. unsigned int size = width / 8;
  801. bool unmap = false;
  802. rcu_read_lock();
  803. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  804. if (!virt_addr) {
  805. rcu_read_unlock();
  806. virt_addr = acpi_os_ioremap(phys_addr, size);
  807. if (!virt_addr)
  808. return AE_BAD_ADDRESS;
  809. unmap = true;
  810. }
  811. switch (width) {
  812. case 8:
  813. writeb(value, virt_addr);
  814. break;
  815. case 16:
  816. writew(value, virt_addr);
  817. break;
  818. case 32:
  819. writel(value, virt_addr);
  820. break;
  821. case 64:
  822. write64(value, virt_addr);
  823. break;
  824. default:
  825. BUG();
  826. }
  827. if (unmap)
  828. iounmap(virt_addr);
  829. else
  830. rcu_read_unlock();
  831. return AE_OK;
  832. }
  833. acpi_status
  834. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  835. u64 *value, u32 width)
  836. {
  837. int result, size;
  838. u32 value32;
  839. if (!value)
  840. return AE_BAD_PARAMETER;
  841. switch (width) {
  842. case 8:
  843. size = 1;
  844. break;
  845. case 16:
  846. size = 2;
  847. break;
  848. case 32:
  849. size = 4;
  850. break;
  851. default:
  852. return AE_ERROR;
  853. }
  854. result = raw_pci_read(pci_id->segment, pci_id->bus,
  855. PCI_DEVFN(pci_id->device, pci_id->function),
  856. reg, size, &value32);
  857. *value = value32;
  858. return (result ? AE_ERROR : AE_OK);
  859. }
  860. acpi_status
  861. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  862. u64 value, u32 width)
  863. {
  864. int result, size;
  865. switch (width) {
  866. case 8:
  867. size = 1;
  868. break;
  869. case 16:
  870. size = 2;
  871. break;
  872. case 32:
  873. size = 4;
  874. break;
  875. default:
  876. return AE_ERROR;
  877. }
  878. result = raw_pci_write(pci_id->segment, pci_id->bus,
  879. PCI_DEVFN(pci_id->device, pci_id->function),
  880. reg, size, value);
  881. return (result ? AE_ERROR : AE_OK);
  882. }
  883. static void acpi_os_execute_deferred(struct work_struct *work)
  884. {
  885. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  886. if (dpc->wait)
  887. acpi_os_wait_events_complete();
  888. dpc->function(dpc->context);
  889. kfree(dpc);
  890. }
  891. /*******************************************************************************
  892. *
  893. * FUNCTION: acpi_os_execute
  894. *
  895. * PARAMETERS: Type - Type of the callback
  896. * Function - Function to be executed
  897. * Context - Function parameters
  898. *
  899. * RETURN: Status
  900. *
  901. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  902. * immediately executes function on a separate thread.
  903. *
  904. ******************************************************************************/
  905. static acpi_status __acpi_os_execute(acpi_execute_type type,
  906. acpi_osd_exec_callback function, void *context, int hp)
  907. {
  908. acpi_status status = AE_OK;
  909. struct acpi_os_dpc *dpc;
  910. struct workqueue_struct *queue;
  911. int ret;
  912. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  913. "Scheduling function [%p(%p)] for deferred execution.\n",
  914. function, context));
  915. /*
  916. * Allocate/initialize DPC structure. Note that this memory will be
  917. * freed by the callee. The kernel handles the work_struct list in a
  918. * way that allows us to also free its memory inside the callee.
  919. * Because we may want to schedule several tasks with different
  920. * parameters we can't use the approach some kernel code uses of
  921. * having a static work_struct.
  922. */
  923. dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  924. if (!dpc)
  925. return AE_NO_MEMORY;
  926. dpc->function = function;
  927. dpc->context = context;
  928. /*
  929. * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
  930. * because the hotplug code may call driver .remove() functions,
  931. * which invoke flush_scheduled_work/acpi_os_wait_events_complete
  932. * to flush these workqueues.
  933. *
  934. * To prevent lockdep from complaining unnecessarily, make sure that
  935. * there is a different static lockdep key for each workqueue by using
  936. * INIT_WORK() for each of them separately.
  937. */
  938. if (hp) {
  939. queue = kacpi_hotplug_wq;
  940. dpc->wait = 1;
  941. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  942. } else if (type == OSL_NOTIFY_HANDLER) {
  943. queue = kacpi_notify_wq;
  944. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  945. } else {
  946. queue = kacpid_wq;
  947. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  948. }
  949. /*
  950. * On some machines, a software-initiated SMI causes corruption unless
  951. * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
  952. * typically it's done in GPE-related methods that are run via
  953. * workqueues, so we can avoid the known corruption cases by always
  954. * queueing on CPU 0.
  955. */
  956. ret = queue_work_on(0, queue, &dpc->work);
  957. if (!ret) {
  958. printk(KERN_ERR PREFIX
  959. "Call to queue_work() failed.\n");
  960. status = AE_ERROR;
  961. kfree(dpc);
  962. }
  963. return status;
  964. }
  965. acpi_status acpi_os_execute(acpi_execute_type type,
  966. acpi_osd_exec_callback function, void *context)
  967. {
  968. return __acpi_os_execute(type, function, context, 0);
  969. }
  970. EXPORT_SYMBOL(acpi_os_execute);
  971. acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
  972. void *context)
  973. {
  974. return __acpi_os_execute(0, function, context, 1);
  975. }
  976. EXPORT_SYMBOL(acpi_os_hotplug_execute);
  977. void acpi_os_wait_events_complete(void)
  978. {
  979. flush_workqueue(kacpid_wq);
  980. flush_workqueue(kacpi_notify_wq);
  981. }
  982. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  983. acpi_status
  984. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  985. {
  986. struct semaphore *sem = NULL;
  987. sem = acpi_os_allocate(sizeof(struct semaphore));
  988. if (!sem)
  989. return AE_NO_MEMORY;
  990. memset(sem, 0, sizeof(struct semaphore));
  991. sema_init(sem, initial_units);
  992. *handle = (acpi_handle *) sem;
  993. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  994. *handle, initial_units));
  995. return AE_OK;
  996. }
  997. /*
  998. * TODO: A better way to delete semaphores? Linux doesn't have a
  999. * 'delete_semaphore()' function -- may result in an invalid
  1000. * pointer dereference for non-synchronized consumers. Should
  1001. * we at least check for blocked threads and signal/cancel them?
  1002. */
  1003. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  1004. {
  1005. struct semaphore *sem = (struct semaphore *)handle;
  1006. if (!sem)
  1007. return AE_BAD_PARAMETER;
  1008. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  1009. BUG_ON(!list_empty(&sem->wait_list));
  1010. kfree(sem);
  1011. sem = NULL;
  1012. return AE_OK;
  1013. }
  1014. /*
  1015. * TODO: Support for units > 1?
  1016. */
  1017. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  1018. {
  1019. acpi_status status = AE_OK;
  1020. struct semaphore *sem = (struct semaphore *)handle;
  1021. long jiffies;
  1022. int ret = 0;
  1023. if (!sem || (units < 1))
  1024. return AE_BAD_PARAMETER;
  1025. if (units > 1)
  1026. return AE_SUPPORT;
  1027. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  1028. handle, units, timeout));
  1029. if (timeout == ACPI_WAIT_FOREVER)
  1030. jiffies = MAX_SCHEDULE_TIMEOUT;
  1031. else
  1032. jiffies = msecs_to_jiffies(timeout);
  1033. ret = down_timeout(sem, jiffies);
  1034. if (ret)
  1035. status = AE_TIME;
  1036. if (ACPI_FAILURE(status)) {
  1037. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1038. "Failed to acquire semaphore[%p|%d|%d], %s",
  1039. handle, units, timeout,
  1040. acpi_format_exception(status)));
  1041. } else {
  1042. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1043. "Acquired semaphore[%p|%d|%d]", handle,
  1044. units, timeout));
  1045. }
  1046. return status;
  1047. }
  1048. /*
  1049. * TODO: Support for units > 1?
  1050. */
  1051. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  1052. {
  1053. struct semaphore *sem = (struct semaphore *)handle;
  1054. if (!sem || (units < 1))
  1055. return AE_BAD_PARAMETER;
  1056. if (units > 1)
  1057. return AE_SUPPORT;
  1058. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  1059. units));
  1060. up(sem);
  1061. return AE_OK;
  1062. }
  1063. #ifdef ACPI_FUTURE_USAGE
  1064. u32 acpi_os_get_line(char *buffer)
  1065. {
  1066. #ifdef ENABLE_DEBUGGER
  1067. if (acpi_in_debugger) {
  1068. u32 chars;
  1069. kdb_read(buffer, sizeof(line_buf));
  1070. /* remove the CR kdb includes */
  1071. chars = strlen(buffer) - 1;
  1072. buffer[chars] = '\0';
  1073. }
  1074. #endif
  1075. return 0;
  1076. }
  1077. #endif /* ACPI_FUTURE_USAGE */
  1078. acpi_status acpi_os_signal(u32 function, void *info)
  1079. {
  1080. switch (function) {
  1081. case ACPI_SIGNAL_FATAL:
  1082. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  1083. break;
  1084. case ACPI_SIGNAL_BREAKPOINT:
  1085. /*
  1086. * AML Breakpoint
  1087. * ACPI spec. says to treat it as a NOP unless
  1088. * you are debugging. So if/when we integrate
  1089. * AML debugger into the kernel debugger its
  1090. * hook will go here. But until then it is
  1091. * not useful to print anything on breakpoints.
  1092. */
  1093. break;
  1094. default:
  1095. break;
  1096. }
  1097. return AE_OK;
  1098. }
  1099. static int __init acpi_os_name_setup(char *str)
  1100. {
  1101. char *p = acpi_os_name;
  1102. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  1103. if (!str || !*str)
  1104. return 0;
  1105. for (; count-- && str && *str; str++) {
  1106. if (isalnum(*str) || *str == ' ' || *str == ':')
  1107. *p++ = *str;
  1108. else if (*str == '\'' || *str == '"')
  1109. continue;
  1110. else
  1111. break;
  1112. }
  1113. *p = 0;
  1114. return 1;
  1115. }
  1116. __setup("acpi_os_name=", acpi_os_name_setup);
  1117. #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
  1118. #define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
  1119. struct osi_setup_entry {
  1120. char string[OSI_STRING_LENGTH_MAX];
  1121. bool enable;
  1122. };
  1123. static struct osi_setup_entry __initdata
  1124. osi_setup_entries[OSI_STRING_ENTRIES_MAX] = {
  1125. {"Module Device", true},
  1126. {"Processor Device", true},
  1127. {"3.0 _SCP Extensions", true},
  1128. {"Processor Aggregator Device", true},
  1129. };
  1130. void __init acpi_osi_setup(char *str)
  1131. {
  1132. struct osi_setup_entry *osi;
  1133. bool enable = true;
  1134. int i;
  1135. if (!acpi_gbl_create_osi_method)
  1136. return;
  1137. if (str == NULL || *str == '\0') {
  1138. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  1139. acpi_gbl_create_osi_method = FALSE;
  1140. return;
  1141. }
  1142. if (*str == '!') {
  1143. str++;
  1144. enable = false;
  1145. }
  1146. for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
  1147. osi = &osi_setup_entries[i];
  1148. if (!strcmp(osi->string, str)) {
  1149. osi->enable = enable;
  1150. break;
  1151. } else if (osi->string[0] == '\0') {
  1152. osi->enable = enable;
  1153. strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. static void __init set_osi_linux(unsigned int enable)
  1159. {
  1160. if (osi_linux.enable != enable)
  1161. osi_linux.enable = enable;
  1162. if (osi_linux.enable)
  1163. acpi_osi_setup("Linux");
  1164. else
  1165. acpi_osi_setup("!Linux");
  1166. return;
  1167. }
  1168. static void __init acpi_cmdline_osi_linux(unsigned int enable)
  1169. {
  1170. osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
  1171. osi_linux.dmi = 0;
  1172. set_osi_linux(enable);
  1173. return;
  1174. }
  1175. void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
  1176. {
  1177. printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
  1178. if (enable == -1)
  1179. return;
  1180. osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
  1181. set_osi_linux(enable);
  1182. return;
  1183. }
  1184. /*
  1185. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  1186. *
  1187. * empty string disables _OSI
  1188. * string starting with '!' disables that string
  1189. * otherwise string is added to list, augmenting built-in strings
  1190. */
  1191. static void __init acpi_osi_setup_late(void)
  1192. {
  1193. struct osi_setup_entry *osi;
  1194. char *str;
  1195. int i;
  1196. acpi_status status;
  1197. for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
  1198. osi = &osi_setup_entries[i];
  1199. str = osi->string;
  1200. if (*str == '\0')
  1201. break;
  1202. if (osi->enable) {
  1203. status = acpi_install_interface(str);
  1204. if (ACPI_SUCCESS(status))
  1205. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  1206. } else {
  1207. status = acpi_remove_interface(str);
  1208. if (ACPI_SUCCESS(status))
  1209. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  1210. }
  1211. }
  1212. }
  1213. static int __init osi_setup(char *str)
  1214. {
  1215. if (str && !strcmp("Linux", str))
  1216. acpi_cmdline_osi_linux(1);
  1217. else if (str && !strcmp("!Linux", str))
  1218. acpi_cmdline_osi_linux(0);
  1219. else
  1220. acpi_osi_setup(str);
  1221. return 1;
  1222. }
  1223. __setup("acpi_osi=", osi_setup);
  1224. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  1225. static int __init acpi_serialize_setup(char *str)
  1226. {
  1227. printk(KERN_INFO PREFIX "serialize enabled\n");
  1228. acpi_gbl_all_methods_serialized = TRUE;
  1229. return 1;
  1230. }
  1231. __setup("acpi_serialize", acpi_serialize_setup);
  1232. /* Check of resource interference between native drivers and ACPI
  1233. * OperationRegions (SystemIO and System Memory only).
  1234. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  1235. * in arbitrary AML code and can interfere with legacy drivers.
  1236. * acpi_enforce_resources= can be set to:
  1237. *
  1238. * - strict (default) (2)
  1239. * -> further driver trying to access the resources will not load
  1240. * - lax (1)
  1241. * -> further driver trying to access the resources will load, but you
  1242. * get a system message that something might go wrong...
  1243. *
  1244. * - no (0)
  1245. * -> ACPI Operation Region resources will not be registered
  1246. *
  1247. */
  1248. #define ENFORCE_RESOURCES_STRICT 2
  1249. #define ENFORCE_RESOURCES_LAX 1
  1250. #define ENFORCE_RESOURCES_NO 0
  1251. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1252. static int __init acpi_enforce_resources_setup(char *str)
  1253. {
  1254. if (str == NULL || *str == '\0')
  1255. return 0;
  1256. if (!strcmp("strict", str))
  1257. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1258. else if (!strcmp("lax", str))
  1259. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  1260. else if (!strcmp("no", str))
  1261. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  1262. return 1;
  1263. }
  1264. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  1265. /* Check for resource conflicts between ACPI OperationRegions and native
  1266. * drivers */
  1267. int acpi_check_resource_conflict(const struct resource *res)
  1268. {
  1269. acpi_adr_space_type space_id;
  1270. acpi_size length;
  1271. u8 warn = 0;
  1272. int clash = 0;
  1273. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1274. return 0;
  1275. if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
  1276. return 0;
  1277. if (res->flags & IORESOURCE_IO)
  1278. space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  1279. else
  1280. space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
  1281. length = resource_size(res);
  1282. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO)
  1283. warn = 1;
  1284. clash = acpi_check_address_range(space_id, res->start, length, warn);
  1285. if (clash) {
  1286. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
  1287. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  1288. printk(KERN_NOTICE "ACPI: This conflict may"
  1289. " cause random problems and system"
  1290. " instability\n");
  1291. printk(KERN_INFO "ACPI: If an ACPI driver is available"
  1292. " for this device, you should use it instead of"
  1293. " the native driver\n");
  1294. }
  1295. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  1296. return -EBUSY;
  1297. }
  1298. return 0;
  1299. }
  1300. EXPORT_SYMBOL(acpi_check_resource_conflict);
  1301. int acpi_check_region(resource_size_t start, resource_size_t n,
  1302. const char *name)
  1303. {
  1304. struct resource res = {
  1305. .start = start,
  1306. .end = start + n - 1,
  1307. .name = name,
  1308. .flags = IORESOURCE_IO,
  1309. };
  1310. return acpi_check_resource_conflict(&res);
  1311. }
  1312. EXPORT_SYMBOL(acpi_check_region);
  1313. /*
  1314. * Let drivers know whether the resource checks are effective
  1315. */
  1316. int acpi_resources_are_enforced(void)
  1317. {
  1318. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  1319. }
  1320. EXPORT_SYMBOL(acpi_resources_are_enforced);
  1321. /*
  1322. * Deallocate the memory for a spinlock.
  1323. */
  1324. void acpi_os_delete_lock(acpi_spinlock handle)
  1325. {
  1326. ACPI_FREE(handle);
  1327. }
  1328. /*
  1329. * Acquire a spinlock.
  1330. *
  1331. * handle is a pointer to the spinlock_t.
  1332. */
  1333. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1334. {
  1335. acpi_cpu_flags flags;
  1336. spin_lock_irqsave(lockp, flags);
  1337. return flags;
  1338. }
  1339. /*
  1340. * Release a spinlock. See above.
  1341. */
  1342. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  1343. {
  1344. spin_unlock_irqrestore(lockp, flags);
  1345. }
  1346. #ifndef ACPI_USE_LOCAL_CACHE
  1347. /*******************************************************************************
  1348. *
  1349. * FUNCTION: acpi_os_create_cache
  1350. *
  1351. * PARAMETERS: name - Ascii name for the cache
  1352. * size - Size of each cached object
  1353. * depth - Maximum depth of the cache (in objects) <ignored>
  1354. * cache - Where the new cache object is returned
  1355. *
  1356. * RETURN: status
  1357. *
  1358. * DESCRIPTION: Create a cache object
  1359. *
  1360. ******************************************************************************/
  1361. acpi_status
  1362. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  1363. {
  1364. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1365. if (*cache == NULL)
  1366. return AE_ERROR;
  1367. else
  1368. return AE_OK;
  1369. }
  1370. /*******************************************************************************
  1371. *
  1372. * FUNCTION: acpi_os_purge_cache
  1373. *
  1374. * PARAMETERS: Cache - Handle to cache object
  1375. *
  1376. * RETURN: Status
  1377. *
  1378. * DESCRIPTION: Free all objects within the requested cache.
  1379. *
  1380. ******************************************************************************/
  1381. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  1382. {
  1383. kmem_cache_shrink(cache);
  1384. return (AE_OK);
  1385. }
  1386. /*******************************************************************************
  1387. *
  1388. * FUNCTION: acpi_os_delete_cache
  1389. *
  1390. * PARAMETERS: Cache - Handle to cache object
  1391. *
  1392. * RETURN: Status
  1393. *
  1394. * DESCRIPTION: Free all objects within the requested cache and delete the
  1395. * cache object.
  1396. *
  1397. ******************************************************************************/
  1398. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  1399. {
  1400. kmem_cache_destroy(cache);
  1401. return (AE_OK);
  1402. }
  1403. /*******************************************************************************
  1404. *
  1405. * FUNCTION: acpi_os_release_object
  1406. *
  1407. * PARAMETERS: Cache - Handle to cache object
  1408. * Object - The object to be released
  1409. *
  1410. * RETURN: None
  1411. *
  1412. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1413. * the object is deleted.
  1414. *
  1415. ******************************************************************************/
  1416. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1417. {
  1418. kmem_cache_free(cache, object);
  1419. return (AE_OK);
  1420. }
  1421. #endif
  1422. static int __init acpi_no_auto_ssdt_setup(char *s)
  1423. {
  1424. printk(KERN_NOTICE PREFIX "SSDT auto-load disabled\n");
  1425. acpi_gbl_disable_ssdt_table_load = TRUE;
  1426. return 1;
  1427. }
  1428. __setup("acpi_no_auto_ssdt", acpi_no_auto_ssdt_setup);
  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);