osl.c 38 KB

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