osl.c 39 KB

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