osl.c 37 KB

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