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/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. #include "internal.h"
  53. #define _COMPONENT ACPI_OS_SERVICES
  54. ACPI_MODULE_NAME("osl");
  55. #define PREFIX "ACPI: "
  56. struct acpi_os_dpc {
  57. acpi_osd_exec_callback function;
  58. void *context;
  59. struct work_struct work;
  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. #define ACPI_HEADER_SIZE sizeof(struct acpi_table_header)
  475. #define ACPI_OVERRIDE_TABLES 64
  476. static struct cpio_data __initdata acpi_initrd_files[ACPI_OVERRIDE_TABLES];
  477. #define MAP_CHUNK_SIZE (NR_FIX_BTMAPS << PAGE_SHIFT)
  478. void __init acpi_initrd_override(void *data, size_t size)
  479. {
  480. int sig, no, table_nr = 0, total_offset = 0;
  481. long offset = 0;
  482. struct acpi_table_header *table;
  483. char cpio_path[32] = "kernel/firmware/acpi/";
  484. struct cpio_data file;
  485. if (data == NULL || size == 0)
  486. return;
  487. for (no = 0; no < ACPI_OVERRIDE_TABLES; no++) {
  488. file = find_cpio_data(cpio_path, data, size, &offset);
  489. if (!file.data)
  490. break;
  491. data += offset;
  492. size -= offset;
  493. if (file.size < sizeof(struct acpi_table_header)) {
  494. pr_err("ACPI OVERRIDE: Table smaller than ACPI header [%s%s]\n",
  495. cpio_path, file.name);
  496. continue;
  497. }
  498. table = file.data;
  499. for (sig = 0; table_sigs[sig]; sig++)
  500. if (!memcmp(table->signature, table_sigs[sig], 4))
  501. break;
  502. if (!table_sigs[sig]) {
  503. pr_err("ACPI OVERRIDE: Unknown signature [%s%s]\n",
  504. cpio_path, file.name);
  505. continue;
  506. }
  507. if (file.size != table->length) {
  508. pr_err("ACPI OVERRIDE: File length does not match table length [%s%s]\n",
  509. cpio_path, file.name);
  510. continue;
  511. }
  512. if (acpi_table_checksum(file.data, table->length)) {
  513. pr_err("ACPI OVERRIDE: Bad table checksum [%s%s]\n",
  514. cpio_path, file.name);
  515. continue;
  516. }
  517. pr_info("%4.4s ACPI table found in initrd [%s%s][0x%x]\n",
  518. table->signature, cpio_path, file.name, table->length);
  519. all_tables_size += table->length;
  520. acpi_initrd_files[table_nr].data = file.data;
  521. acpi_initrd_files[table_nr].size = file.size;
  522. table_nr++;
  523. }
  524. if (table_nr == 0)
  525. return;
  526. acpi_tables_addr =
  527. memblock_find_in_range(0, max_low_pfn_mapped << PAGE_SHIFT,
  528. all_tables_size, PAGE_SIZE);
  529. if (!acpi_tables_addr) {
  530. WARN_ON(1);
  531. return;
  532. }
  533. /*
  534. * Only calling e820_add_reserve does not work and the
  535. * tables are invalid (memory got used) later.
  536. * memblock_reserve works as expected and the tables won't get modified.
  537. * But it's not enough on X86 because ioremap will
  538. * complain later (used by acpi_os_map_memory) that the pages
  539. * that should get mapped are not marked "reserved".
  540. * Both memblock_reserve and e820_add_region (via arch_reserve_mem_area)
  541. * works fine.
  542. */
  543. memblock_reserve(acpi_tables_addr, all_tables_size);
  544. arch_reserve_mem_area(acpi_tables_addr, all_tables_size);
  545. /*
  546. * early_ioremap only can remap 256k one time. If we map all
  547. * tables one time, we will hit the limit. Need to map chunks
  548. * one by one during copying the same as that in relocate_initrd().
  549. */
  550. for (no = 0; no < table_nr; no++) {
  551. unsigned char *src_p = acpi_initrd_files[no].data;
  552. phys_addr_t size = acpi_initrd_files[no].size;
  553. phys_addr_t dest_addr = acpi_tables_addr + total_offset;
  554. phys_addr_t slop, clen;
  555. char *dest_p;
  556. total_offset += size;
  557. while (size) {
  558. slop = dest_addr & ~PAGE_MASK;
  559. clen = size;
  560. if (clen > MAP_CHUNK_SIZE - slop)
  561. clen = MAP_CHUNK_SIZE - slop;
  562. dest_p = early_ioremap(dest_addr & PAGE_MASK,
  563. clen + slop);
  564. memcpy(dest_p + slop, src_p, clen);
  565. early_iounmap(dest_p, clen + slop);
  566. src_p += clen;
  567. dest_addr += clen;
  568. size -= clen;
  569. }
  570. }
  571. }
  572. #endif /* CONFIG_ACPI_INITRD_TABLE_OVERRIDE */
  573. static void acpi_table_taint(struct acpi_table_header *table)
  574. {
  575. pr_warn(PREFIX
  576. "Override [%4.4s-%8.8s], this is unsafe: tainting kernel\n",
  577. table->signature, table->oem_table_id);
  578. add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
  579. }
  580. acpi_status
  581. acpi_os_table_override(struct acpi_table_header * existing_table,
  582. struct acpi_table_header ** new_table)
  583. {
  584. if (!existing_table || !new_table)
  585. return AE_BAD_PARAMETER;
  586. *new_table = NULL;
  587. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  588. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  589. *new_table = (struct acpi_table_header *)AmlCode;
  590. #endif
  591. if (*new_table != NULL)
  592. acpi_table_taint(existing_table);
  593. return AE_OK;
  594. }
  595. acpi_status
  596. acpi_os_physical_table_override(struct acpi_table_header *existing_table,
  597. acpi_physical_address *address,
  598. u32 *table_length)
  599. {
  600. #ifndef CONFIG_ACPI_INITRD_TABLE_OVERRIDE
  601. *table_length = 0;
  602. *address = 0;
  603. return AE_OK;
  604. #else
  605. int table_offset = 0;
  606. struct acpi_table_header *table;
  607. *table_length = 0;
  608. *address = 0;
  609. if (!acpi_tables_addr)
  610. return AE_OK;
  611. do {
  612. if (table_offset + ACPI_HEADER_SIZE > all_tables_size) {
  613. WARN_ON(1);
  614. return AE_OK;
  615. }
  616. table = acpi_os_map_memory(acpi_tables_addr + table_offset,
  617. ACPI_HEADER_SIZE);
  618. if (table_offset + table->length > all_tables_size) {
  619. acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
  620. WARN_ON(1);
  621. return AE_OK;
  622. }
  623. table_offset += table->length;
  624. if (memcmp(existing_table->signature, table->signature, 4)) {
  625. acpi_os_unmap_memory(table,
  626. ACPI_HEADER_SIZE);
  627. continue;
  628. }
  629. /* Only override tables with matching oem id */
  630. if (memcmp(table->oem_table_id, existing_table->oem_table_id,
  631. ACPI_OEM_TABLE_ID_SIZE)) {
  632. acpi_os_unmap_memory(table,
  633. ACPI_HEADER_SIZE);
  634. continue;
  635. }
  636. table_offset -= table->length;
  637. *table_length = table->length;
  638. acpi_os_unmap_memory(table, ACPI_HEADER_SIZE);
  639. *address = acpi_tables_addr + table_offset;
  640. break;
  641. } while (table_offset + ACPI_HEADER_SIZE < all_tables_size);
  642. if (*address != 0)
  643. acpi_table_taint(existing_table);
  644. return AE_OK;
  645. #endif
  646. }
  647. static irqreturn_t acpi_irq(int irq, void *dev_id)
  648. {
  649. u32 handled;
  650. handled = (*acpi_irq_handler) (acpi_irq_context);
  651. if (handled) {
  652. acpi_irq_handled++;
  653. return IRQ_HANDLED;
  654. } else {
  655. acpi_irq_not_handled++;
  656. return IRQ_NONE;
  657. }
  658. }
  659. acpi_status
  660. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  661. void *context)
  662. {
  663. unsigned int irq;
  664. acpi_irq_stats_init();
  665. /*
  666. * ACPI interrupts different from the SCI in our copy of the FADT are
  667. * not supported.
  668. */
  669. if (gsi != acpi_gbl_FADT.sci_interrupt)
  670. return AE_BAD_PARAMETER;
  671. if (acpi_irq_handler)
  672. return AE_ALREADY_ACQUIRED;
  673. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  674. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  675. gsi);
  676. return AE_OK;
  677. }
  678. acpi_irq_handler = handler;
  679. acpi_irq_context = context;
  680. if (request_irq(irq, acpi_irq, IRQF_SHARED | IRQF_NO_SUSPEND, "acpi", acpi_irq)) {
  681. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  682. acpi_irq_handler = NULL;
  683. return AE_NOT_ACQUIRED;
  684. }
  685. return AE_OK;
  686. }
  687. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  688. {
  689. if (irq != acpi_gbl_FADT.sci_interrupt)
  690. return AE_BAD_PARAMETER;
  691. free_irq(irq, acpi_irq);
  692. acpi_irq_handler = NULL;
  693. return AE_OK;
  694. }
  695. /*
  696. * Running in interpreter thread context, safe to sleep
  697. */
  698. void acpi_os_sleep(u64 ms)
  699. {
  700. msleep(ms);
  701. }
  702. void acpi_os_stall(u32 us)
  703. {
  704. while (us) {
  705. u32 delay = 1000;
  706. if (delay > us)
  707. delay = us;
  708. udelay(delay);
  709. touch_nmi_watchdog();
  710. us -= delay;
  711. }
  712. }
  713. /*
  714. * Support ACPI 3.0 AML Timer operand
  715. * Returns 64-bit free-running, monotonically increasing timer
  716. * with 100ns granularity
  717. */
  718. u64 acpi_os_get_timer(void)
  719. {
  720. u64 time_ns = ktime_to_ns(ktime_get());
  721. do_div(time_ns, 100);
  722. return time_ns;
  723. }
  724. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  725. {
  726. u32 dummy;
  727. if (!value)
  728. value = &dummy;
  729. *value = 0;
  730. if (width <= 8) {
  731. *(u8 *) value = inb(port);
  732. } else if (width <= 16) {
  733. *(u16 *) value = inw(port);
  734. } else if (width <= 32) {
  735. *(u32 *) value = inl(port);
  736. } else {
  737. BUG();
  738. }
  739. return AE_OK;
  740. }
  741. EXPORT_SYMBOL(acpi_os_read_port);
  742. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  743. {
  744. if (width <= 8) {
  745. outb(value, port);
  746. } else if (width <= 16) {
  747. outw(value, port);
  748. } else if (width <= 32) {
  749. outl(value, port);
  750. } else {
  751. BUG();
  752. }
  753. return AE_OK;
  754. }
  755. EXPORT_SYMBOL(acpi_os_write_port);
  756. #ifdef readq
  757. static inline u64 read64(const volatile void __iomem *addr)
  758. {
  759. return readq(addr);
  760. }
  761. #else
  762. static inline u64 read64(const volatile void __iomem *addr)
  763. {
  764. u64 l, h;
  765. l = readl(addr);
  766. h = readl(addr+4);
  767. return l | (h << 32);
  768. }
  769. #endif
  770. acpi_status
  771. acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
  772. {
  773. void __iomem *virt_addr;
  774. unsigned int size = width / 8;
  775. bool unmap = false;
  776. u64 dummy;
  777. rcu_read_lock();
  778. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  779. if (!virt_addr) {
  780. rcu_read_unlock();
  781. virt_addr = acpi_os_ioremap(phys_addr, size);
  782. if (!virt_addr)
  783. return AE_BAD_ADDRESS;
  784. unmap = true;
  785. }
  786. if (!value)
  787. value = &dummy;
  788. switch (width) {
  789. case 8:
  790. *(u8 *) value = readb(virt_addr);
  791. break;
  792. case 16:
  793. *(u16 *) value = readw(virt_addr);
  794. break;
  795. case 32:
  796. *(u32 *) value = readl(virt_addr);
  797. break;
  798. case 64:
  799. *(u64 *) value = read64(virt_addr);
  800. break;
  801. default:
  802. BUG();
  803. }
  804. if (unmap)
  805. iounmap(virt_addr);
  806. else
  807. rcu_read_unlock();
  808. return AE_OK;
  809. }
  810. #ifdef writeq
  811. static inline void write64(u64 val, volatile void __iomem *addr)
  812. {
  813. writeq(val, addr);
  814. }
  815. #else
  816. static inline void write64(u64 val, volatile void __iomem *addr)
  817. {
  818. writel(val, addr);
  819. writel(val>>32, addr+4);
  820. }
  821. #endif
  822. acpi_status
  823. acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
  824. {
  825. void __iomem *virt_addr;
  826. unsigned int size = width / 8;
  827. bool unmap = false;
  828. rcu_read_lock();
  829. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  830. if (!virt_addr) {
  831. rcu_read_unlock();
  832. virt_addr = acpi_os_ioremap(phys_addr, size);
  833. if (!virt_addr)
  834. return AE_BAD_ADDRESS;
  835. unmap = true;
  836. }
  837. switch (width) {
  838. case 8:
  839. writeb(value, virt_addr);
  840. break;
  841. case 16:
  842. writew(value, virt_addr);
  843. break;
  844. case 32:
  845. writel(value, virt_addr);
  846. break;
  847. case 64:
  848. write64(value, virt_addr);
  849. break;
  850. default:
  851. BUG();
  852. }
  853. if (unmap)
  854. iounmap(virt_addr);
  855. else
  856. rcu_read_unlock();
  857. return AE_OK;
  858. }
  859. acpi_status
  860. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  861. u64 *value, u32 width)
  862. {
  863. int result, size;
  864. u32 value32;
  865. if (!value)
  866. return AE_BAD_PARAMETER;
  867. switch (width) {
  868. case 8:
  869. size = 1;
  870. break;
  871. case 16:
  872. size = 2;
  873. break;
  874. case 32:
  875. size = 4;
  876. break;
  877. default:
  878. return AE_ERROR;
  879. }
  880. result = raw_pci_read(pci_id->segment, pci_id->bus,
  881. PCI_DEVFN(pci_id->device, pci_id->function),
  882. reg, size, &value32);
  883. *value = value32;
  884. return (result ? AE_ERROR : AE_OK);
  885. }
  886. acpi_status
  887. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  888. u64 value, u32 width)
  889. {
  890. int result, size;
  891. switch (width) {
  892. case 8:
  893. size = 1;
  894. break;
  895. case 16:
  896. size = 2;
  897. break;
  898. case 32:
  899. size = 4;
  900. break;
  901. default:
  902. return AE_ERROR;
  903. }
  904. result = raw_pci_write(pci_id->segment, pci_id->bus,
  905. PCI_DEVFN(pci_id->device, pci_id->function),
  906. reg, size, value);
  907. return (result ? AE_ERROR : AE_OK);
  908. }
  909. static void acpi_os_execute_deferred(struct work_struct *work)
  910. {
  911. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  912. dpc->function(dpc->context);
  913. kfree(dpc);
  914. }
  915. /*******************************************************************************
  916. *
  917. * FUNCTION: acpi_os_execute
  918. *
  919. * PARAMETERS: Type - Type of the callback
  920. * Function - Function to be executed
  921. * Context - Function parameters
  922. *
  923. * RETURN: Status
  924. *
  925. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  926. * immediately executes function on a separate thread.
  927. *
  928. ******************************************************************************/
  929. acpi_status acpi_os_execute(acpi_execute_type type,
  930. acpi_osd_exec_callback function, void *context)
  931. {
  932. acpi_status status = AE_OK;
  933. struct acpi_os_dpc *dpc;
  934. struct workqueue_struct *queue;
  935. int ret;
  936. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  937. "Scheduling function [%p(%p)] for deferred execution.\n",
  938. function, context));
  939. /*
  940. * Allocate/initialize DPC structure. Note that this memory will be
  941. * freed by the callee. The kernel handles the work_struct list in a
  942. * way that allows us to also free its memory inside the callee.
  943. * Because we may want to schedule several tasks with different
  944. * parameters we can't use the approach some kernel code uses of
  945. * having a static work_struct.
  946. */
  947. dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  948. if (!dpc)
  949. return AE_NO_MEMORY;
  950. dpc->function = function;
  951. dpc->context = context;
  952. /*
  953. * To prevent lockdep from complaining unnecessarily, make sure that
  954. * there is a different static lockdep key for each workqueue by using
  955. * INIT_WORK() for each of them separately.
  956. */
  957. if (type == OSL_NOTIFY_HANDLER) {
  958. queue = kacpi_notify_wq;
  959. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  960. } else {
  961. queue = kacpid_wq;
  962. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  963. }
  964. /*
  965. * On some machines, a software-initiated SMI causes corruption unless
  966. * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
  967. * typically it's done in GPE-related methods that are run via
  968. * workqueues, so we can avoid the known corruption cases by always
  969. * queueing on CPU 0.
  970. */
  971. ret = queue_work_on(0, queue, &dpc->work);
  972. if (!ret) {
  973. printk(KERN_ERR PREFIX
  974. "Call to queue_work() failed.\n");
  975. status = AE_ERROR;
  976. kfree(dpc);
  977. }
  978. return status;
  979. }
  980. EXPORT_SYMBOL(acpi_os_execute);
  981. void acpi_os_wait_events_complete(void)
  982. {
  983. flush_workqueue(kacpid_wq);
  984. flush_workqueue(kacpi_notify_wq);
  985. }
  986. struct acpi_hp_work {
  987. struct work_struct work;
  988. acpi_hp_callback func;
  989. void *data;
  990. u32 src;
  991. };
  992. static void acpi_hotplug_work_fn(struct work_struct *work)
  993. {
  994. struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
  995. acpi_os_wait_events_complete();
  996. hpw->func(hpw->data, hpw->src);
  997. kfree(hpw);
  998. }
  999. acpi_status acpi_hotplug_execute(acpi_hp_callback func, void *data, u32 src)
  1000. {
  1001. struct acpi_hp_work *hpw;
  1002. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  1003. "Scheduling function [%p(%p, %u)] for deferred execution.\n",
  1004. func, data, src));
  1005. hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
  1006. if (!hpw)
  1007. return AE_NO_MEMORY;
  1008. INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
  1009. hpw->func = func;
  1010. hpw->data = data;
  1011. hpw->src = src;
  1012. /*
  1013. * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
  1014. * the hotplug code may call driver .remove() functions, which may
  1015. * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
  1016. * these workqueues.
  1017. */
  1018. if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
  1019. kfree(hpw);
  1020. return AE_ERROR;
  1021. }
  1022. return AE_OK;
  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_workqueue("kacpi_hotplug", 0, 1);
  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. }