osl.c 41 KB

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