osl.c 45 KB

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