osl.c 43 KB

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