osl.c 45 KB

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