osl.c 45 KB

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