osl.c 38 KB

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