osl.c 39 KB

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