osl.c 39 KB

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