osl.c 38 KB

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