osl.c 37 KB

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