osl.c 39 KB

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