osl.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /*
  2. * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
  3. *
  4. * Copyright (C) 2000 Andrew Henroid
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. * Copyright (c) 2008 Intel Corporation
  8. * Author: Matthew Wilcox <willy@linux.intel.com>
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/mm.h>
  33. #include <linux/pci.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/kmod.h>
  36. #include <linux/delay.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/nmi.h>
  39. #include <linux/acpi.h>
  40. #include <linux/acpi_io.h>
  41. #include <linux/efi.h>
  42. #include <linux/ioport.h>
  43. #include <linux/list.h>
  44. #include <linux/jiffies.h>
  45. #include <linux/semaphore.h>
  46. #include <asm/io.h>
  47. #include <asm/uaccess.h>
  48. #include <acpi/acpi.h>
  49. #include <acpi/acpi_bus.h>
  50. #include <acpi/processor.h>
  51. #define _COMPONENT ACPI_OS_SERVICES
  52. ACPI_MODULE_NAME("osl");
  53. #define PREFIX "ACPI: "
  54. struct acpi_os_dpc {
  55. acpi_osd_exec_callback function;
  56. void *context;
  57. struct work_struct work;
  58. int wait;
  59. };
  60. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  61. #include CONFIG_ACPI_CUSTOM_DSDT_FILE
  62. #endif
  63. #ifdef ENABLE_DEBUGGER
  64. #include <linux/kdb.h>
  65. /* stuff for debugger support */
  66. int acpi_in_debugger;
  67. EXPORT_SYMBOL(acpi_in_debugger);
  68. extern char line_buf[80];
  69. #endif /*ENABLE_DEBUGGER */
  70. static unsigned int acpi_irq_irq;
  71. static acpi_osd_handler acpi_irq_handler;
  72. static void *acpi_irq_context;
  73. static struct workqueue_struct *kacpid_wq;
  74. static struct workqueue_struct *kacpi_notify_wq;
  75. static struct workqueue_struct *kacpi_hotplug_wq;
  76. struct acpi_res_list {
  77. resource_size_t start;
  78. resource_size_t end;
  79. acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
  80. char name[5]; /* only can have a length of 4 chars, make use of this
  81. one instead of res->name, no need to kalloc then */
  82. struct list_head resource_list;
  83. int count;
  84. };
  85. static LIST_HEAD(resource_list_head);
  86. static DEFINE_SPINLOCK(acpi_res_lock);
  87. /*
  88. * This list of permanent mappings is for memory that may be accessed from
  89. * interrupt context, where we can't do the ioremap().
  90. */
  91. struct acpi_ioremap {
  92. struct list_head list;
  93. void __iomem *virt;
  94. acpi_physical_address phys;
  95. acpi_size size;
  96. struct kref ref;
  97. };
  98. static LIST_HEAD(acpi_ioremaps);
  99. static DEFINE_SPINLOCK(acpi_ioremap_lock);
  100. static void __init acpi_osi_setup_late(void);
  101. /*
  102. * The story of _OSI(Linux)
  103. *
  104. * From pre-history through Linux-2.6.22,
  105. * Linux responded TRUE upon a BIOS OSI(Linux) query.
  106. *
  107. * Unfortunately, reference BIOS writers got wind of this
  108. * and put OSI(Linux) in their example code, quickly exposing
  109. * this string as ill-conceived and opening the door to
  110. * an un-bounded number of BIOS incompatibilities.
  111. *
  112. * For example, OSI(Linux) was used on resume to re-POST a
  113. * video card on one system, because Linux at that time
  114. * could not do a speedy restore in its native driver.
  115. * But then upon gaining quick native restore capability,
  116. * Linux has no way to tell the BIOS to skip the time-consuming
  117. * POST -- putting Linux at a permanent performance disadvantage.
  118. * On another system, the BIOS writer used OSI(Linux)
  119. * to infer native OS support for IPMI! On other systems,
  120. * OSI(Linux) simply got in the way of Linux claiming to
  121. * be compatible with other operating systems, exposing
  122. * BIOS issues such as skipped device initialization.
  123. *
  124. * So "Linux" turned out to be a really poor chose of
  125. * OSI string, and from Linux-2.6.23 onward we respond FALSE.
  126. *
  127. * BIOS writers should NOT query _OSI(Linux) on future systems.
  128. * Linux will complain on the console when it sees it, and return FALSE.
  129. * To get Linux to return TRUE for your system will require
  130. * a kernel source update to add a DMI entry,
  131. * or boot with "acpi_osi=Linux"
  132. */
  133. static struct osi_linux {
  134. unsigned int enable:1;
  135. unsigned int dmi:1;
  136. unsigned int cmdline:1;
  137. } osi_linux = {0, 0, 0};
  138. static u32 acpi_osi_handler(acpi_string interface, u32 supported)
  139. {
  140. if (!strcmp("Linux", interface)) {
  141. printk(KERN_NOTICE FW_BUG PREFIX
  142. "BIOS _OSI(Linux) query %s%s\n",
  143. osi_linux.enable ? "honored" : "ignored",
  144. osi_linux.cmdline ? " via cmdline" :
  145. osi_linux.dmi ? " via DMI" : "");
  146. }
  147. return supported;
  148. }
  149. static void __init acpi_request_region (struct acpi_generic_address *addr,
  150. unsigned int length, char *desc)
  151. {
  152. if (!addr->address || !length)
  153. return;
  154. /* Resources are never freed */
  155. if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  156. request_region(addr->address, length, desc);
  157. else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  158. request_mem_region(addr->address, length, desc);
  159. }
  160. static int __init acpi_reserve_resources(void)
  161. {
  162. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  163. "ACPI PM1a_EVT_BLK");
  164. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  165. "ACPI PM1b_EVT_BLK");
  166. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  167. "ACPI PM1a_CNT_BLK");
  168. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  169. "ACPI PM1b_CNT_BLK");
  170. if (acpi_gbl_FADT.pm_timer_length == 4)
  171. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  172. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  173. "ACPI PM2_CNT_BLK");
  174. /* Length of GPE blocks must be a non-negative multiple of 2 */
  175. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  176. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  177. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  178. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  179. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  180. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  181. return 0;
  182. }
  183. device_initcall(acpi_reserve_resources);
  184. void acpi_os_printf(const char *fmt, ...)
  185. {
  186. va_list args;
  187. va_start(args, fmt);
  188. acpi_os_vprintf(fmt, args);
  189. va_end(args);
  190. }
  191. void acpi_os_vprintf(const char *fmt, va_list args)
  192. {
  193. static char buffer[512];
  194. vsprintf(buffer, fmt, args);
  195. #ifdef ENABLE_DEBUGGER
  196. if (acpi_in_debugger) {
  197. kdb_printf("%s", buffer);
  198. } else {
  199. printk(KERN_CONT "%s", buffer);
  200. }
  201. #else
  202. printk(KERN_CONT "%s", buffer);
  203. #endif
  204. }
  205. acpi_physical_address __init acpi_os_get_root_pointer(void)
  206. {
  207. if (efi_enabled) {
  208. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  209. return efi.acpi20;
  210. else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  211. return efi.acpi;
  212. else {
  213. printk(KERN_ERR PREFIX
  214. "System description tables not found\n");
  215. return 0;
  216. }
  217. } else {
  218. acpi_physical_address pa = 0;
  219. acpi_find_root_pointer(&pa);
  220. return pa;
  221. }
  222. }
  223. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  224. static struct acpi_ioremap *
  225. acpi_map_lookup(acpi_physical_address phys, acpi_size size)
  226. {
  227. struct acpi_ioremap *map;
  228. list_for_each_entry_rcu(map, &acpi_ioremaps, list)
  229. if (map->phys <= phys &&
  230. phys + size <= map->phys + map->size)
  231. return map;
  232. return NULL;
  233. }
  234. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  235. static void __iomem *
  236. acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
  237. {
  238. struct acpi_ioremap *map;
  239. map = acpi_map_lookup(phys, size);
  240. if (map)
  241. return map->virt + (phys - map->phys);
  242. return NULL;
  243. }
  244. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  245. static struct acpi_ioremap *
  246. acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
  247. {
  248. struct acpi_ioremap *map;
  249. list_for_each_entry_rcu(map, &acpi_ioremaps, list)
  250. if (map->virt <= virt &&
  251. virt + size <= map->virt + map->size)
  252. return map;
  253. return NULL;
  254. }
  255. void __iomem *__init_refok
  256. acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  257. {
  258. struct acpi_ioremap *map, *tmp_map;
  259. unsigned long flags;
  260. void __iomem *virt;
  261. acpi_physical_address pg_off;
  262. acpi_size pg_sz;
  263. if (phys > ULONG_MAX) {
  264. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  265. return NULL;
  266. }
  267. if (!acpi_gbl_permanent_mmap)
  268. return __acpi_map_table((unsigned long)phys, size);
  269. map = kzalloc(sizeof(*map), GFP_KERNEL);
  270. if (!map)
  271. return NULL;
  272. pg_off = round_down(phys, PAGE_SIZE);
  273. pg_sz = round_up(phys + size, PAGE_SIZE) - pg_off;
  274. virt = acpi_os_ioremap(pg_off, pg_sz);
  275. if (!virt) {
  276. kfree(map);
  277. return NULL;
  278. }
  279. INIT_LIST_HEAD(&map->list);
  280. map->virt = virt;
  281. map->phys = pg_off;
  282. map->size = pg_sz;
  283. kref_init(&map->ref);
  284. spin_lock_irqsave(&acpi_ioremap_lock, flags);
  285. /* Check if page has already been mapped. */
  286. tmp_map = acpi_map_lookup(phys, size);
  287. if (tmp_map) {
  288. kref_get(&tmp_map->ref);
  289. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  290. iounmap(map->virt);
  291. kfree(map);
  292. return tmp_map->virt + (phys - tmp_map->phys);
  293. }
  294. list_add_tail_rcu(&map->list, &acpi_ioremaps);
  295. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  296. return map->virt + (phys - map->phys);
  297. }
  298. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  299. static void acpi_kref_del_iomap(struct kref *ref)
  300. {
  301. struct acpi_ioremap *map;
  302. map = container_of(ref, struct acpi_ioremap, ref);
  303. list_del_rcu(&map->list);
  304. }
  305. void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
  306. {
  307. struct acpi_ioremap *map;
  308. unsigned long flags;
  309. int del;
  310. if (!acpi_gbl_permanent_mmap) {
  311. __acpi_unmap_table(virt, size);
  312. return;
  313. }
  314. spin_lock_irqsave(&acpi_ioremap_lock, flags);
  315. map = acpi_map_lookup_virt(virt, size);
  316. if (!map) {
  317. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  318. printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
  319. dump_stack();
  320. return;
  321. }
  322. del = kref_put(&map->ref, acpi_kref_del_iomap);
  323. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  324. if (!del)
  325. return;
  326. synchronize_rcu();
  327. iounmap(map->virt);
  328. kfree(map);
  329. }
  330. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  331. void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
  332. {
  333. if (!acpi_gbl_permanent_mmap)
  334. __acpi_unmap_table(virt, size);
  335. }
  336. int acpi_os_map_generic_address(struct acpi_generic_address *addr)
  337. {
  338. void __iomem *virt;
  339. if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  340. return 0;
  341. if (!addr->address || !addr->bit_width)
  342. return -EINVAL;
  343. virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
  344. if (!virt)
  345. return -EIO;
  346. return 0;
  347. }
  348. EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
  349. void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
  350. {
  351. void __iomem *virt;
  352. unsigned long flags;
  353. acpi_size size = addr->bit_width / 8;
  354. if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  355. return;
  356. if (!addr->address || !addr->bit_width)
  357. return;
  358. spin_lock_irqsave(&acpi_ioremap_lock, flags);
  359. virt = acpi_map_vaddr_lookup(addr->address, size);
  360. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  361. acpi_os_unmap_memory(virt, size);
  362. }
  363. EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
  364. #ifdef ACPI_FUTURE_USAGE
  365. acpi_status
  366. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  367. {
  368. if (!phys || !virt)
  369. return AE_BAD_PARAMETER;
  370. *phys = virt_to_phys(virt);
  371. return AE_OK;
  372. }
  373. #endif
  374. #define ACPI_MAX_OVERRIDE_LEN 100
  375. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  376. acpi_status
  377. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  378. acpi_string * new_val)
  379. {
  380. if (!init_val || !new_val)
  381. return AE_BAD_PARAMETER;
  382. *new_val = NULL;
  383. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  384. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  385. acpi_os_name);
  386. *new_val = acpi_os_name;
  387. }
  388. return AE_OK;
  389. }
  390. acpi_status
  391. acpi_os_table_override(struct acpi_table_header * existing_table,
  392. struct acpi_table_header ** new_table)
  393. {
  394. if (!existing_table || !new_table)
  395. return AE_BAD_PARAMETER;
  396. *new_table = NULL;
  397. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  398. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  399. *new_table = (struct acpi_table_header *)AmlCode;
  400. #endif
  401. if (*new_table != NULL) {
  402. printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
  403. "this is unsafe: tainting kernel\n",
  404. existing_table->signature,
  405. existing_table->oem_table_id);
  406. add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
  407. }
  408. return AE_OK;
  409. }
  410. static irqreturn_t acpi_irq(int irq, void *dev_id)
  411. {
  412. u32 handled;
  413. handled = (*acpi_irq_handler) (acpi_irq_context);
  414. if (handled) {
  415. acpi_irq_handled++;
  416. return IRQ_HANDLED;
  417. } else {
  418. acpi_irq_not_handled++;
  419. return IRQ_NONE;
  420. }
  421. }
  422. acpi_status
  423. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  424. void *context)
  425. {
  426. unsigned int irq;
  427. acpi_irq_stats_init();
  428. /*
  429. * Ignore the GSI from the core, and use the value in our copy of the
  430. * FADT. It may not be the same if an interrupt source override exists
  431. * for the SCI.
  432. */
  433. gsi = acpi_gbl_FADT.sci_interrupt;
  434. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  435. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  436. gsi);
  437. return AE_OK;
  438. }
  439. acpi_irq_handler = handler;
  440. acpi_irq_context = context;
  441. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  442. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  443. return AE_NOT_ACQUIRED;
  444. }
  445. acpi_irq_irq = irq;
  446. return AE_OK;
  447. }
  448. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  449. {
  450. if (irq) {
  451. free_irq(irq, acpi_irq);
  452. acpi_irq_handler = NULL;
  453. acpi_irq_irq = 0;
  454. }
  455. return AE_OK;
  456. }
  457. /*
  458. * Running in interpreter thread context, safe to sleep
  459. */
  460. void acpi_os_sleep(u64 ms)
  461. {
  462. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  463. }
  464. void acpi_os_stall(u32 us)
  465. {
  466. while (us) {
  467. u32 delay = 1000;
  468. if (delay > us)
  469. delay = us;
  470. udelay(delay);
  471. touch_nmi_watchdog();
  472. us -= delay;
  473. }
  474. }
  475. /*
  476. * Support ACPI 3.0 AML Timer operand
  477. * Returns 64-bit free-running, monotonically increasing timer
  478. * with 100ns granularity
  479. */
  480. u64 acpi_os_get_timer(void)
  481. {
  482. static u64 t;
  483. #ifdef CONFIG_HPET
  484. /* TBD: use HPET if available */
  485. #endif
  486. #ifdef CONFIG_X86_PM_TIMER
  487. /* TBD: default to PM timer if HPET was not available */
  488. #endif
  489. if (!t)
  490. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  491. return ++t;
  492. }
  493. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  494. {
  495. u32 dummy;
  496. if (!value)
  497. value = &dummy;
  498. *value = 0;
  499. if (width <= 8) {
  500. *(u8 *) value = inb(port);
  501. } else if (width <= 16) {
  502. *(u16 *) value = inw(port);
  503. } else if (width <= 32) {
  504. *(u32 *) value = inl(port);
  505. } else {
  506. BUG();
  507. }
  508. return AE_OK;
  509. }
  510. EXPORT_SYMBOL(acpi_os_read_port);
  511. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  512. {
  513. if (width <= 8) {
  514. outb(value, port);
  515. } else if (width <= 16) {
  516. outw(value, port);
  517. } else if (width <= 32) {
  518. outl(value, port);
  519. } else {
  520. BUG();
  521. }
  522. return AE_OK;
  523. }
  524. EXPORT_SYMBOL(acpi_os_write_port);
  525. acpi_status
  526. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  527. {
  528. 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 = acpi_os_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 = acpi_os_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. #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
  871. #define OSI_STRING_ENTRIES_MAX 16 /* arbitrary */
  872. struct osi_setup_entry {
  873. char string[OSI_STRING_LENGTH_MAX];
  874. bool enable;
  875. };
  876. static struct osi_setup_entry __initdata osi_setup_entries[OSI_STRING_ENTRIES_MAX];
  877. void __init acpi_osi_setup(char *str)
  878. {
  879. struct osi_setup_entry *osi;
  880. bool enable = true;
  881. int i;
  882. if (!acpi_gbl_create_osi_method)
  883. return;
  884. if (str == NULL || *str == '\0') {
  885. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  886. acpi_gbl_create_osi_method = FALSE;
  887. return;
  888. }
  889. if (*str == '!') {
  890. str++;
  891. enable = false;
  892. }
  893. for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
  894. osi = &osi_setup_entries[i];
  895. if (!strcmp(osi->string, str)) {
  896. osi->enable = enable;
  897. break;
  898. } else if (osi->string[0] == '\0') {
  899. osi->enable = enable;
  900. strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
  901. break;
  902. }
  903. }
  904. }
  905. static void __init set_osi_linux(unsigned int enable)
  906. {
  907. if (osi_linux.enable != enable)
  908. osi_linux.enable = enable;
  909. if (osi_linux.enable)
  910. acpi_osi_setup("Linux");
  911. else
  912. acpi_osi_setup("!Linux");
  913. return;
  914. }
  915. static void __init acpi_cmdline_osi_linux(unsigned int enable)
  916. {
  917. osi_linux.cmdline = 1; /* cmdline set the default and override DMI */
  918. osi_linux.dmi = 0;
  919. set_osi_linux(enable);
  920. return;
  921. }
  922. void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
  923. {
  924. printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
  925. if (enable == -1)
  926. return;
  927. osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
  928. set_osi_linux(enable);
  929. return;
  930. }
  931. /*
  932. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  933. *
  934. * empty string disables _OSI
  935. * string starting with '!' disables that string
  936. * otherwise string is added to list, augmenting built-in strings
  937. */
  938. static void __init acpi_osi_setup_late(void)
  939. {
  940. struct osi_setup_entry *osi;
  941. char *str;
  942. int i;
  943. acpi_status status;
  944. for (i = 0; i < OSI_STRING_ENTRIES_MAX; i++) {
  945. osi = &osi_setup_entries[i];
  946. str = osi->string;
  947. if (*str == '\0')
  948. break;
  949. if (osi->enable) {
  950. status = acpi_install_interface(str);
  951. if (ACPI_SUCCESS(status))
  952. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  953. } else {
  954. status = acpi_remove_interface(str);
  955. if (ACPI_SUCCESS(status))
  956. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  957. }
  958. }
  959. }
  960. static int __init osi_setup(char *str)
  961. {
  962. if (str && !strcmp("Linux", str))
  963. acpi_cmdline_osi_linux(1);
  964. else if (str && !strcmp("!Linux", str))
  965. acpi_cmdline_osi_linux(0);
  966. else
  967. acpi_osi_setup(str);
  968. return 1;
  969. }
  970. __setup("acpi_osi=", osi_setup);
  971. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  972. static int __init acpi_serialize_setup(char *str)
  973. {
  974. printk(KERN_INFO PREFIX "serialize enabled\n");
  975. acpi_gbl_all_methods_serialized = TRUE;
  976. return 1;
  977. }
  978. __setup("acpi_serialize", acpi_serialize_setup);
  979. /* Check of resource interference between native drivers and ACPI
  980. * OperationRegions (SystemIO and System Memory only).
  981. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  982. * in arbitrary AML code and can interfere with legacy drivers.
  983. * acpi_enforce_resources= can be set to:
  984. *
  985. * - strict (default) (2)
  986. * -> further driver trying to access the resources will not load
  987. * - lax (1)
  988. * -> further driver trying to access the resources will load, but you
  989. * get a system message that something might go wrong...
  990. *
  991. * - no (0)
  992. * -> ACPI Operation Region resources will not be registered
  993. *
  994. */
  995. #define ENFORCE_RESOURCES_STRICT 2
  996. #define ENFORCE_RESOURCES_LAX 1
  997. #define ENFORCE_RESOURCES_NO 0
  998. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  999. static int __init acpi_enforce_resources_setup(char *str)
  1000. {
  1001. if (str == NULL || *str == '\0')
  1002. return 0;
  1003. if (!strcmp("strict", str))
  1004. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1005. else if (!strcmp("lax", str))
  1006. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  1007. else if (!strcmp("no", str))
  1008. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  1009. return 1;
  1010. }
  1011. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  1012. /* Check for resource conflicts between ACPI OperationRegions and native
  1013. * drivers */
  1014. int acpi_check_resource_conflict(const struct resource *res)
  1015. {
  1016. struct acpi_res_list *res_list_elem;
  1017. int ioport = 0, clash = 0;
  1018. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1019. return 0;
  1020. if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
  1021. return 0;
  1022. ioport = res->flags & IORESOURCE_IO;
  1023. spin_lock(&acpi_res_lock);
  1024. list_for_each_entry(res_list_elem, &resource_list_head,
  1025. resource_list) {
  1026. if (ioport && (res_list_elem->resource_type
  1027. != ACPI_ADR_SPACE_SYSTEM_IO))
  1028. continue;
  1029. if (!ioport && (res_list_elem->resource_type
  1030. != ACPI_ADR_SPACE_SYSTEM_MEMORY))
  1031. continue;
  1032. if (res->end < res_list_elem->start
  1033. || res_list_elem->end < res->start)
  1034. continue;
  1035. clash = 1;
  1036. break;
  1037. }
  1038. spin_unlock(&acpi_res_lock);
  1039. if (clash) {
  1040. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
  1041. printk(KERN_WARNING "ACPI: resource %s %pR"
  1042. " conflicts with ACPI region %s "
  1043. "[%s 0x%zx-0x%zx]\n",
  1044. res->name, res, res_list_elem->name,
  1045. (res_list_elem->resource_type ==
  1046. ACPI_ADR_SPACE_SYSTEM_IO) ? "io" : "mem",
  1047. (size_t) res_list_elem->start,
  1048. (size_t) res_list_elem->end);
  1049. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  1050. printk(KERN_NOTICE "ACPI: This conflict may"
  1051. " cause random problems and system"
  1052. " instability\n");
  1053. printk(KERN_INFO "ACPI: If an ACPI driver is available"
  1054. " for this device, you should use it instead of"
  1055. " the native driver\n");
  1056. }
  1057. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  1058. return -EBUSY;
  1059. }
  1060. return 0;
  1061. }
  1062. EXPORT_SYMBOL(acpi_check_resource_conflict);
  1063. int acpi_check_region(resource_size_t start, resource_size_t n,
  1064. const char *name)
  1065. {
  1066. struct resource res = {
  1067. .start = start,
  1068. .end = start + n - 1,
  1069. .name = name,
  1070. .flags = IORESOURCE_IO,
  1071. };
  1072. return acpi_check_resource_conflict(&res);
  1073. }
  1074. EXPORT_SYMBOL(acpi_check_region);
  1075. /*
  1076. * Let drivers know whether the resource checks are effective
  1077. */
  1078. int acpi_resources_are_enforced(void)
  1079. {
  1080. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  1081. }
  1082. EXPORT_SYMBOL(acpi_resources_are_enforced);
  1083. /*
  1084. * Acquire a spinlock.
  1085. *
  1086. * handle is a pointer to the spinlock_t.
  1087. */
  1088. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1089. {
  1090. acpi_cpu_flags flags;
  1091. spin_lock_irqsave(lockp, flags);
  1092. return flags;
  1093. }
  1094. /*
  1095. * Release a spinlock. See above.
  1096. */
  1097. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  1098. {
  1099. spin_unlock_irqrestore(lockp, flags);
  1100. }
  1101. #ifndef ACPI_USE_LOCAL_CACHE
  1102. /*******************************************************************************
  1103. *
  1104. * FUNCTION: acpi_os_create_cache
  1105. *
  1106. * PARAMETERS: name - Ascii name for the cache
  1107. * size - Size of each cached object
  1108. * depth - Maximum depth of the cache (in objects) <ignored>
  1109. * cache - Where the new cache object is returned
  1110. *
  1111. * RETURN: status
  1112. *
  1113. * DESCRIPTION: Create a cache object
  1114. *
  1115. ******************************************************************************/
  1116. acpi_status
  1117. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  1118. {
  1119. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1120. if (*cache == NULL)
  1121. return AE_ERROR;
  1122. else
  1123. return AE_OK;
  1124. }
  1125. /*******************************************************************************
  1126. *
  1127. * FUNCTION: acpi_os_purge_cache
  1128. *
  1129. * PARAMETERS: Cache - Handle to cache object
  1130. *
  1131. * RETURN: Status
  1132. *
  1133. * DESCRIPTION: Free all objects within the requested cache.
  1134. *
  1135. ******************************************************************************/
  1136. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  1137. {
  1138. kmem_cache_shrink(cache);
  1139. return (AE_OK);
  1140. }
  1141. /*******************************************************************************
  1142. *
  1143. * FUNCTION: acpi_os_delete_cache
  1144. *
  1145. * PARAMETERS: Cache - Handle to cache object
  1146. *
  1147. * RETURN: Status
  1148. *
  1149. * DESCRIPTION: Free all objects within the requested cache and delete the
  1150. * cache object.
  1151. *
  1152. ******************************************************************************/
  1153. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  1154. {
  1155. kmem_cache_destroy(cache);
  1156. return (AE_OK);
  1157. }
  1158. /*******************************************************************************
  1159. *
  1160. * FUNCTION: acpi_os_release_object
  1161. *
  1162. * PARAMETERS: Cache - Handle to cache object
  1163. * Object - The object to be released
  1164. *
  1165. * RETURN: None
  1166. *
  1167. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1168. * the object is deleted.
  1169. *
  1170. ******************************************************************************/
  1171. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1172. {
  1173. kmem_cache_free(cache, object);
  1174. return (AE_OK);
  1175. }
  1176. static inline int acpi_res_list_add(struct acpi_res_list *res)
  1177. {
  1178. struct acpi_res_list *res_list_elem;
  1179. list_for_each_entry(res_list_elem, &resource_list_head,
  1180. resource_list) {
  1181. if (res->resource_type == res_list_elem->resource_type &&
  1182. res->start == res_list_elem->start &&
  1183. res->end == res_list_elem->end) {
  1184. /*
  1185. * The Region(addr,len) already exist in the list,
  1186. * just increase the count
  1187. */
  1188. res_list_elem->count++;
  1189. return 0;
  1190. }
  1191. }
  1192. res->count = 1;
  1193. list_add(&res->resource_list, &resource_list_head);
  1194. return 1;
  1195. }
  1196. static inline void acpi_res_list_del(struct acpi_res_list *res)
  1197. {
  1198. struct acpi_res_list *res_list_elem;
  1199. list_for_each_entry(res_list_elem, &resource_list_head,
  1200. resource_list) {
  1201. if (res->resource_type == res_list_elem->resource_type &&
  1202. res->start == res_list_elem->start &&
  1203. res->end == res_list_elem->end) {
  1204. /*
  1205. * If the res count is decreased to 0,
  1206. * remove and free it
  1207. */
  1208. if (--res_list_elem->count == 0) {
  1209. list_del(&res_list_elem->resource_list);
  1210. kfree(res_list_elem);
  1211. }
  1212. return;
  1213. }
  1214. }
  1215. }
  1216. acpi_status
  1217. acpi_os_invalidate_address(
  1218. u8 space_id,
  1219. acpi_physical_address address,
  1220. acpi_size length)
  1221. {
  1222. struct acpi_res_list res;
  1223. switch (space_id) {
  1224. case ACPI_ADR_SPACE_SYSTEM_IO:
  1225. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  1226. /* Only interference checks against SystemIO and SystemMemory
  1227. are needed */
  1228. res.start = address;
  1229. res.end = address + length - 1;
  1230. res.resource_type = space_id;
  1231. spin_lock(&acpi_res_lock);
  1232. acpi_res_list_del(&res);
  1233. spin_unlock(&acpi_res_lock);
  1234. break;
  1235. case ACPI_ADR_SPACE_PCI_CONFIG:
  1236. case ACPI_ADR_SPACE_EC:
  1237. case ACPI_ADR_SPACE_SMBUS:
  1238. case ACPI_ADR_SPACE_CMOS:
  1239. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  1240. case ACPI_ADR_SPACE_DATA_TABLE:
  1241. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  1242. break;
  1243. }
  1244. return AE_OK;
  1245. }
  1246. /******************************************************************************
  1247. *
  1248. * FUNCTION: acpi_os_validate_address
  1249. *
  1250. * PARAMETERS: space_id - ACPI space ID
  1251. * address - Physical address
  1252. * length - Address length
  1253. *
  1254. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  1255. * should return AE_AML_ILLEGAL_ADDRESS.
  1256. *
  1257. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  1258. * the addresses accessed by AML operation regions.
  1259. *
  1260. *****************************************************************************/
  1261. acpi_status
  1262. acpi_os_validate_address (
  1263. u8 space_id,
  1264. acpi_physical_address address,
  1265. acpi_size length,
  1266. char *name)
  1267. {
  1268. struct acpi_res_list *res;
  1269. int added;
  1270. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1271. return AE_OK;
  1272. switch (space_id) {
  1273. case ACPI_ADR_SPACE_SYSTEM_IO:
  1274. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  1275. /* Only interference checks against SystemIO and SystemMemory
  1276. are needed */
  1277. res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
  1278. if (!res)
  1279. return AE_OK;
  1280. /* ACPI names are fixed to 4 bytes, still better use strlcpy */
  1281. strlcpy(res->name, name, 5);
  1282. res->start = address;
  1283. res->end = address + length - 1;
  1284. res->resource_type = space_id;
  1285. spin_lock(&acpi_res_lock);
  1286. added = acpi_res_list_add(res);
  1287. spin_unlock(&acpi_res_lock);
  1288. pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
  1289. "name: %s\n", added ? "Added" : "Already exist",
  1290. (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  1291. ? "SystemIO" : "System Memory",
  1292. (unsigned long long)res->start,
  1293. (unsigned long long)res->end,
  1294. res->name);
  1295. if (!added)
  1296. kfree(res);
  1297. break;
  1298. case ACPI_ADR_SPACE_PCI_CONFIG:
  1299. case ACPI_ADR_SPACE_EC:
  1300. case ACPI_ADR_SPACE_SMBUS:
  1301. case ACPI_ADR_SPACE_CMOS:
  1302. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  1303. case ACPI_ADR_SPACE_DATA_TABLE:
  1304. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  1305. break;
  1306. }
  1307. return AE_OK;
  1308. }
  1309. #endif
  1310. acpi_status __init acpi_os_initialize(void)
  1311. {
  1312. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1313. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1314. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1315. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1316. return AE_OK;
  1317. }
  1318. acpi_status __init acpi_os_initialize1(void)
  1319. {
  1320. kacpid_wq = create_workqueue("kacpid");
  1321. kacpi_notify_wq = create_workqueue("kacpi_notify");
  1322. kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
  1323. BUG_ON(!kacpid_wq);
  1324. BUG_ON(!kacpi_notify_wq);
  1325. BUG_ON(!kacpi_hotplug_wq);
  1326. acpi_install_interface_handler(acpi_osi_handler);
  1327. acpi_osi_setup_late();
  1328. return AE_OK;
  1329. }
  1330. acpi_status acpi_os_terminate(void)
  1331. {
  1332. if (acpi_irq_handler) {
  1333. acpi_os_remove_interrupt_handler(acpi_irq_irq,
  1334. acpi_irq_handler);
  1335. }
  1336. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1337. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1338. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1339. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1340. destroy_workqueue(kacpid_wq);
  1341. destroy_workqueue(kacpi_notify_wq);
  1342. destroy_workqueue(kacpi_hotplug_wq);
  1343. return AE_OK;
  1344. }