osl.c 38 KB

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