osl.c 44 KB

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