osl.c 43 KB

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