/drivers/acpi/osl.c

http://github.com/mirrors/linux · C · 1841 lines · 1472 code · 226 blank · 143 comment · 169 complexity · 617a589a7da0f8182164acebd4b9e867 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
  4. *
  5. * Copyright (C) 2000 Andrew Henroid
  6. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  7. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  8. * Copyright (c) 2008 Intel Corporation
  9. * Author: Matthew Wilcox <willy@linux.intel.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/mm.h>
  15. #include <linux/highmem.h>
  16. #include <linux/lockdep.h>
  17. #include <linux/pci.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kmod.h>
  20. #include <linux/delay.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/nmi.h>
  23. #include <linux/acpi.h>
  24. #include <linux/efi.h>
  25. #include <linux/ioport.h>
  26. #include <linux/list.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/semaphore.h>
  29. #include <linux/security.h>
  30. #include <asm/io.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/io-64-nonatomic-lo-hi.h>
  33. #include "acpica/accommon.h"
  34. #include "acpica/acnamesp.h"
  35. #include "internal.h"
  36. #define _COMPONENT ACPI_OS_SERVICES
  37. ACPI_MODULE_NAME("osl");
  38. struct acpi_os_dpc {
  39. acpi_osd_exec_callback function;
  40. void *context;
  41. struct work_struct work;
  42. };
  43. #ifdef ENABLE_DEBUGGER
  44. #include <linux/kdb.h>
  45. /* stuff for debugger support */
  46. int acpi_in_debugger;
  47. EXPORT_SYMBOL(acpi_in_debugger);
  48. #endif /*ENABLE_DEBUGGER */
  49. static int (*__acpi_os_prepare_sleep)(u8 sleep_state, u32 pm1a_ctrl,
  50. u32 pm1b_ctrl);
  51. static int (*__acpi_os_prepare_extended_sleep)(u8 sleep_state, u32 val_a,
  52. u32 val_b);
  53. static acpi_osd_handler acpi_irq_handler;
  54. static void *acpi_irq_context;
  55. static struct workqueue_struct *kacpid_wq;
  56. static struct workqueue_struct *kacpi_notify_wq;
  57. static struct workqueue_struct *kacpi_hotplug_wq;
  58. static bool acpi_os_initialized;
  59. unsigned int acpi_sci_irq = INVALID_ACPI_IRQ;
  60. bool acpi_permanent_mmap = false;
  61. /*
  62. * This list of permanent mappings is for memory that may be accessed from
  63. * interrupt context, where we can't do the ioremap().
  64. */
  65. struct acpi_ioremap {
  66. struct list_head list;
  67. void __iomem *virt;
  68. acpi_physical_address phys;
  69. acpi_size size;
  70. unsigned long refcount;
  71. };
  72. static LIST_HEAD(acpi_ioremaps);
  73. static DEFINE_MUTEX(acpi_ioremap_lock);
  74. #define acpi_ioremap_lock_held() lock_is_held(&acpi_ioremap_lock.dep_map)
  75. static void __init acpi_request_region (struct acpi_generic_address *gas,
  76. unsigned int length, char *desc)
  77. {
  78. u64 addr;
  79. /* Handle possible alignment issues */
  80. memcpy(&addr, &gas->address, sizeof(addr));
  81. if (!addr || !length)
  82. return;
  83. /* Resources are never freed */
  84. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  85. request_region(addr, length, desc);
  86. else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  87. request_mem_region(addr, length, desc);
  88. }
  89. static int __init acpi_reserve_resources(void)
  90. {
  91. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  92. "ACPI PM1a_EVT_BLK");
  93. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  94. "ACPI PM1b_EVT_BLK");
  95. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  96. "ACPI PM1a_CNT_BLK");
  97. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  98. "ACPI PM1b_CNT_BLK");
  99. if (acpi_gbl_FADT.pm_timer_length == 4)
  100. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  101. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  102. "ACPI PM2_CNT_BLK");
  103. /* Length of GPE blocks must be a non-negative multiple of 2 */
  104. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  105. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  106. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  107. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  108. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  109. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  110. return 0;
  111. }
  112. fs_initcall_sync(acpi_reserve_resources);
  113. void acpi_os_printf(const char *fmt, ...)
  114. {
  115. va_list args;
  116. va_start(args, fmt);
  117. acpi_os_vprintf(fmt, args);
  118. va_end(args);
  119. }
  120. EXPORT_SYMBOL(acpi_os_printf);
  121. void acpi_os_vprintf(const char *fmt, va_list args)
  122. {
  123. static char buffer[512];
  124. vsprintf(buffer, fmt, args);
  125. #ifdef ENABLE_DEBUGGER
  126. if (acpi_in_debugger) {
  127. kdb_printf("%s", buffer);
  128. } else {
  129. if (printk_get_level(buffer))
  130. printk("%s", buffer);
  131. else
  132. printk(KERN_CONT "%s", buffer);
  133. }
  134. #else
  135. if (acpi_debugger_write_log(buffer) < 0) {
  136. if (printk_get_level(buffer))
  137. printk("%s", buffer);
  138. else
  139. printk(KERN_CONT "%s", buffer);
  140. }
  141. #endif
  142. }
  143. #ifdef CONFIG_KEXEC
  144. static unsigned long acpi_rsdp;
  145. static int __init setup_acpi_rsdp(char *arg)
  146. {
  147. return kstrtoul(arg, 16, &acpi_rsdp);
  148. }
  149. early_param("acpi_rsdp", setup_acpi_rsdp);
  150. #endif
  151. acpi_physical_address __init acpi_os_get_root_pointer(void)
  152. {
  153. acpi_physical_address pa;
  154. #ifdef CONFIG_KEXEC
  155. /*
  156. * We may have been provided with an RSDP on the command line,
  157. * but if a malicious user has done so they may be pointing us
  158. * at modified ACPI tables that could alter kernel behaviour -
  159. * so, we check the lockdown status before making use of
  160. * it. If we trust it then also stash it in an architecture
  161. * specific location (if appropriate) so it can be carried
  162. * over further kexec()s.
  163. */
  164. if (acpi_rsdp && !security_locked_down(LOCKDOWN_ACPI_TABLES)) {
  165. acpi_arch_set_root_pointer(acpi_rsdp);
  166. return acpi_rsdp;
  167. }
  168. #endif
  169. pa = acpi_arch_get_root_pointer();
  170. if (pa)
  171. return pa;
  172. if (efi_enabled(EFI_CONFIG_TABLES)) {
  173. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  174. return efi.acpi20;
  175. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  176. return efi.acpi;
  177. pr_err(PREFIX "System description tables not found\n");
  178. } else if (IS_ENABLED(CONFIG_ACPI_LEGACY_TABLES_LOOKUP)) {
  179. acpi_find_root_pointer(&pa);
  180. }
  181. return pa;
  182. }
  183. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  184. static struct acpi_ioremap *
  185. acpi_map_lookup(acpi_physical_address phys, acpi_size size)
  186. {
  187. struct acpi_ioremap *map;
  188. list_for_each_entry_rcu(map, &acpi_ioremaps, list, acpi_ioremap_lock_held())
  189. if (map->phys <= phys &&
  190. phys + size <= map->phys + map->size)
  191. return map;
  192. return NULL;
  193. }
  194. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  195. static void __iomem *
  196. acpi_map_vaddr_lookup(acpi_physical_address phys, unsigned int size)
  197. {
  198. struct acpi_ioremap *map;
  199. map = acpi_map_lookup(phys, size);
  200. if (map)
  201. return map->virt + (phys - map->phys);
  202. return NULL;
  203. }
  204. void __iomem *acpi_os_get_iomem(acpi_physical_address phys, unsigned int size)
  205. {
  206. struct acpi_ioremap *map;
  207. void __iomem *virt = NULL;
  208. mutex_lock(&acpi_ioremap_lock);
  209. map = acpi_map_lookup(phys, size);
  210. if (map) {
  211. virt = map->virt + (phys - map->phys);
  212. map->refcount++;
  213. }
  214. mutex_unlock(&acpi_ioremap_lock);
  215. return virt;
  216. }
  217. EXPORT_SYMBOL_GPL(acpi_os_get_iomem);
  218. /* Must be called with 'acpi_ioremap_lock' or RCU read lock held. */
  219. static struct acpi_ioremap *
  220. acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
  221. {
  222. struct acpi_ioremap *map;
  223. list_for_each_entry_rcu(map, &acpi_ioremaps, list, acpi_ioremap_lock_held())
  224. if (map->virt <= virt &&
  225. virt + size <= map->virt + map->size)
  226. return map;
  227. return NULL;
  228. }
  229. #if defined(CONFIG_IA64) || defined(CONFIG_ARM64)
  230. /* ioremap will take care of cache attributes */
  231. #define should_use_kmap(pfn) 0
  232. #else
  233. #define should_use_kmap(pfn) page_is_ram(pfn)
  234. #endif
  235. static void __iomem *acpi_map(acpi_physical_address pg_off, unsigned long pg_sz)
  236. {
  237. unsigned long pfn;
  238. pfn = pg_off >> PAGE_SHIFT;
  239. if (should_use_kmap(pfn)) {
  240. if (pg_sz > PAGE_SIZE)
  241. return NULL;
  242. return (void __iomem __force *)kmap(pfn_to_page(pfn));
  243. } else
  244. return acpi_os_ioremap(pg_off, pg_sz);
  245. }
  246. static void acpi_unmap(acpi_physical_address pg_off, void __iomem *vaddr)
  247. {
  248. unsigned long pfn;
  249. pfn = pg_off >> PAGE_SHIFT;
  250. if (should_use_kmap(pfn))
  251. kunmap(pfn_to_page(pfn));
  252. else
  253. iounmap(vaddr);
  254. }
  255. /**
  256. * acpi_os_map_iomem - Get a virtual address for a given physical address range.
  257. * @phys: Start of the physical address range to map.
  258. * @size: Size of the physical address range to map.
  259. *
  260. * Look up the given physical address range in the list of existing ACPI memory
  261. * mappings. If found, get a reference to it and return a pointer to it (its
  262. * virtual address). If not found, map it, add it to that list and return a
  263. * pointer to it.
  264. *
  265. * During early init (when acpi_permanent_mmap has not been set yet) this
  266. * routine simply calls __acpi_map_table() to get the job done.
  267. */
  268. void __iomem __ref
  269. *acpi_os_map_iomem(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_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_map(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_iomem);
  312. void *__ref acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  313. {
  314. return (void *)acpi_os_map_iomem(phys, size);
  315. }
  316. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  317. /* Must be called with mutex_lock(&acpi_ioremap_lock) */
  318. static unsigned long acpi_os_drop_map_ref(struct acpi_ioremap *map)
  319. {
  320. unsigned long refcount = --map->refcount;
  321. if (!refcount)
  322. list_del_rcu(&map->list);
  323. return refcount;
  324. }
  325. static void acpi_os_map_cleanup(struct acpi_ioremap *map)
  326. {
  327. synchronize_rcu_expedited();
  328. acpi_unmap(map->phys, map->virt);
  329. kfree(map);
  330. }
  331. /**
  332. * acpi_os_unmap_iomem - Drop a memory mapping reference.
  333. * @virt: Start of the address range to drop a reference to.
  334. * @size: Size of the address range to drop a reference to.
  335. *
  336. * Look up the given virtual address range in the list of existing ACPI memory
  337. * mappings, drop a reference to it and unmap it if there are no more active
  338. * references to it.
  339. *
  340. * During early init (when acpi_permanent_mmap has not been set yet) this
  341. * routine simply calls __acpi_unmap_table() to get the job done. Since
  342. * __acpi_unmap_table() is an __init function, the __ref annotation is needed
  343. * here.
  344. */
  345. void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
  346. {
  347. struct acpi_ioremap *map;
  348. unsigned long refcount;
  349. if (!acpi_permanent_mmap) {
  350. __acpi_unmap_table(virt, size);
  351. return;
  352. }
  353. mutex_lock(&acpi_ioremap_lock);
  354. map = acpi_map_lookup_virt(virt, size);
  355. if (!map) {
  356. mutex_unlock(&acpi_ioremap_lock);
  357. WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
  358. return;
  359. }
  360. refcount = acpi_os_drop_map_ref(map);
  361. mutex_unlock(&acpi_ioremap_lock);
  362. if (!refcount)
  363. acpi_os_map_cleanup(map);
  364. }
  365. EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
  366. void __ref acpi_os_unmap_memory(void *virt, acpi_size size)
  367. {
  368. return acpi_os_unmap_iomem((void __iomem *)virt, size);
  369. }
  370. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  371. int acpi_os_map_generic_address(struct acpi_generic_address *gas)
  372. {
  373. u64 addr;
  374. void __iomem *virt;
  375. if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  376. return 0;
  377. /* Handle possible alignment issues */
  378. memcpy(&addr, &gas->address, sizeof(addr));
  379. if (!addr || !gas->bit_width)
  380. return -EINVAL;
  381. virt = acpi_os_map_iomem(addr, gas->bit_width / 8);
  382. if (!virt)
  383. return -EIO;
  384. return 0;
  385. }
  386. EXPORT_SYMBOL(acpi_os_map_generic_address);
  387. void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
  388. {
  389. u64 addr;
  390. struct acpi_ioremap *map;
  391. unsigned long refcount;
  392. if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  393. return;
  394. /* Handle possible alignment issues */
  395. memcpy(&addr, &gas->address, sizeof(addr));
  396. if (!addr || !gas->bit_width)
  397. return;
  398. mutex_lock(&acpi_ioremap_lock);
  399. map = acpi_map_lookup(addr, gas->bit_width / 8);
  400. if (!map) {
  401. mutex_unlock(&acpi_ioremap_lock);
  402. return;
  403. }
  404. refcount = acpi_os_drop_map_ref(map);
  405. mutex_unlock(&acpi_ioremap_lock);
  406. if (!refcount)
  407. acpi_os_map_cleanup(map);
  408. }
  409. EXPORT_SYMBOL(acpi_os_unmap_generic_address);
  410. #ifdef ACPI_FUTURE_USAGE
  411. acpi_status
  412. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  413. {
  414. if (!phys || !virt)
  415. return AE_BAD_PARAMETER;
  416. *phys = virt_to_phys(virt);
  417. return AE_OK;
  418. }
  419. #endif
  420. #ifdef CONFIG_ACPI_REV_OVERRIDE_POSSIBLE
  421. static bool acpi_rev_override;
  422. int __init acpi_rev_override_setup(char *str)
  423. {
  424. acpi_rev_override = true;
  425. return 1;
  426. }
  427. __setup("acpi_rev_override", acpi_rev_override_setup);
  428. #else
  429. #define acpi_rev_override false
  430. #endif
  431. #define ACPI_MAX_OVERRIDE_LEN 100
  432. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  433. acpi_status
  434. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  435. acpi_string *new_val)
  436. {
  437. if (!init_val || !new_val)
  438. return AE_BAD_PARAMETER;
  439. *new_val = NULL;
  440. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  441. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  442. acpi_os_name);
  443. *new_val = acpi_os_name;
  444. }
  445. if (!memcmp(init_val->name, "_REV", 4) && acpi_rev_override) {
  446. printk(KERN_INFO PREFIX "Overriding _REV return value to 5\n");
  447. *new_val = (char *)5;
  448. }
  449. return AE_OK;
  450. }
  451. static irqreturn_t acpi_irq(int irq, void *dev_id)
  452. {
  453. u32 handled;
  454. handled = (*acpi_irq_handler) (acpi_irq_context);
  455. if (handled) {
  456. acpi_irq_handled++;
  457. return IRQ_HANDLED;
  458. } else {
  459. acpi_irq_not_handled++;
  460. return IRQ_NONE;
  461. }
  462. }
  463. acpi_status
  464. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  465. void *context)
  466. {
  467. unsigned int irq;
  468. acpi_irq_stats_init();
  469. /*
  470. * ACPI interrupts different from the SCI in our copy of the FADT are
  471. * not supported.
  472. */
  473. if (gsi != acpi_gbl_FADT.sci_interrupt)
  474. return AE_BAD_PARAMETER;
  475. if (acpi_irq_handler)
  476. return AE_ALREADY_ACQUIRED;
  477. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  478. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  479. gsi);
  480. return AE_OK;
  481. }
  482. acpi_irq_handler = handler;
  483. acpi_irq_context = context;
  484. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  485. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  486. acpi_irq_handler = NULL;
  487. return AE_NOT_ACQUIRED;
  488. }
  489. acpi_sci_irq = irq;
  490. return AE_OK;
  491. }
  492. acpi_status acpi_os_remove_interrupt_handler(u32 gsi, acpi_osd_handler handler)
  493. {
  494. if (gsi != acpi_gbl_FADT.sci_interrupt || !acpi_sci_irq_valid())
  495. return AE_BAD_PARAMETER;
  496. free_irq(acpi_sci_irq, acpi_irq);
  497. acpi_irq_handler = NULL;
  498. acpi_sci_irq = INVALID_ACPI_IRQ;
  499. return AE_OK;
  500. }
  501. /*
  502. * Running in interpreter thread context, safe to sleep
  503. */
  504. void acpi_os_sleep(u64 ms)
  505. {
  506. msleep(ms);
  507. }
  508. void acpi_os_stall(u32 us)
  509. {
  510. while (us) {
  511. u32 delay = 1000;
  512. if (delay > us)
  513. delay = us;
  514. udelay(delay);
  515. touch_nmi_watchdog();
  516. us -= delay;
  517. }
  518. }
  519. /*
  520. * Support ACPI 3.0 AML Timer operand. Returns a 64-bit free-running,
  521. * monotonically increasing timer with 100ns granularity. Do not use
  522. * ktime_get() to implement this function because this function may get
  523. * called after timekeeping has been suspended. Note: calling this function
  524. * after timekeeping has been suspended may lead to unexpected results
  525. * because when timekeeping is suspended the jiffies counter is not
  526. * incremented. See also timekeeping_suspend().
  527. */
  528. u64 acpi_os_get_timer(void)
  529. {
  530. return (get_jiffies_64() - INITIAL_JIFFIES) *
  531. (ACPI_100NSEC_PER_SEC / HZ);
  532. }
  533. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  534. {
  535. u32 dummy;
  536. if (!value)
  537. value = &dummy;
  538. *value = 0;
  539. if (width <= 8) {
  540. *(u8 *) value = inb(port);
  541. } else if (width <= 16) {
  542. *(u16 *) value = inw(port);
  543. } else if (width <= 32) {
  544. *(u32 *) value = inl(port);
  545. } else {
  546. BUG();
  547. }
  548. return AE_OK;
  549. }
  550. EXPORT_SYMBOL(acpi_os_read_port);
  551. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  552. {
  553. if (width <= 8) {
  554. outb(value, port);
  555. } else if (width <= 16) {
  556. outw(value, port);
  557. } else if (width <= 32) {
  558. outl(value, port);
  559. } else {
  560. BUG();
  561. }
  562. return AE_OK;
  563. }
  564. EXPORT_SYMBOL(acpi_os_write_port);
  565. int acpi_os_read_iomem(void __iomem *virt_addr, u64 *value, u32 width)
  566. {
  567. switch (width) {
  568. case 8:
  569. *(u8 *) value = readb(virt_addr);
  570. break;
  571. case 16:
  572. *(u16 *) value = readw(virt_addr);
  573. break;
  574. case 32:
  575. *(u32 *) value = readl(virt_addr);
  576. break;
  577. case 64:
  578. *(u64 *) value = readq(virt_addr);
  579. break;
  580. default:
  581. return -EINVAL;
  582. }
  583. return 0;
  584. }
  585. acpi_status
  586. acpi_os_read_memory(acpi_physical_address phys_addr, u64 *value, u32 width)
  587. {
  588. void __iomem *virt_addr;
  589. unsigned int size = width / 8;
  590. bool unmap = false;
  591. u64 dummy;
  592. int error;
  593. rcu_read_lock();
  594. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  595. if (!virt_addr) {
  596. rcu_read_unlock();
  597. virt_addr = acpi_os_ioremap(phys_addr, size);
  598. if (!virt_addr)
  599. return AE_BAD_ADDRESS;
  600. unmap = true;
  601. }
  602. if (!value)
  603. value = &dummy;
  604. error = acpi_os_read_iomem(virt_addr, value, width);
  605. BUG_ON(error);
  606. if (unmap)
  607. iounmap(virt_addr);
  608. else
  609. rcu_read_unlock();
  610. return AE_OK;
  611. }
  612. acpi_status
  613. acpi_os_write_memory(acpi_physical_address phys_addr, u64 value, u32 width)
  614. {
  615. void __iomem *virt_addr;
  616. unsigned int size = width / 8;
  617. bool unmap = false;
  618. rcu_read_lock();
  619. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  620. if (!virt_addr) {
  621. rcu_read_unlock();
  622. virt_addr = acpi_os_ioremap(phys_addr, size);
  623. if (!virt_addr)
  624. return AE_BAD_ADDRESS;
  625. unmap = true;
  626. }
  627. switch (width) {
  628. case 8:
  629. writeb(value, virt_addr);
  630. break;
  631. case 16:
  632. writew(value, virt_addr);
  633. break;
  634. case 32:
  635. writel(value, virt_addr);
  636. break;
  637. case 64:
  638. writeq(value, virt_addr);
  639. break;
  640. default:
  641. BUG();
  642. }
  643. if (unmap)
  644. iounmap(virt_addr);
  645. else
  646. rcu_read_unlock();
  647. return AE_OK;
  648. }
  649. #ifdef CONFIG_PCI
  650. acpi_status
  651. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  652. u64 *value, u32 width)
  653. {
  654. int result, size;
  655. u32 value32;
  656. if (!value)
  657. return AE_BAD_PARAMETER;
  658. switch (width) {
  659. case 8:
  660. size = 1;
  661. break;
  662. case 16:
  663. size = 2;
  664. break;
  665. case 32:
  666. size = 4;
  667. break;
  668. default:
  669. return AE_ERROR;
  670. }
  671. result = raw_pci_read(pci_id->segment, pci_id->bus,
  672. PCI_DEVFN(pci_id->device, pci_id->function),
  673. reg, size, &value32);
  674. *value = value32;
  675. return (result ? AE_ERROR : AE_OK);
  676. }
  677. acpi_status
  678. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  679. u64 value, u32 width)
  680. {
  681. int result, size;
  682. switch (width) {
  683. case 8:
  684. size = 1;
  685. break;
  686. case 16:
  687. size = 2;
  688. break;
  689. case 32:
  690. size = 4;
  691. break;
  692. default:
  693. return AE_ERROR;
  694. }
  695. result = raw_pci_write(pci_id->segment, pci_id->bus,
  696. PCI_DEVFN(pci_id->device, pci_id->function),
  697. reg, size, value);
  698. return (result ? AE_ERROR : AE_OK);
  699. }
  700. #endif
  701. static void acpi_os_execute_deferred(struct work_struct *work)
  702. {
  703. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  704. dpc->function(dpc->context);
  705. kfree(dpc);
  706. }
  707. #ifdef CONFIG_ACPI_DEBUGGER
  708. static struct acpi_debugger acpi_debugger;
  709. static bool acpi_debugger_initialized;
  710. int acpi_register_debugger(struct module *owner,
  711. const struct acpi_debugger_ops *ops)
  712. {
  713. int ret = 0;
  714. mutex_lock(&acpi_debugger.lock);
  715. if (acpi_debugger.ops) {
  716. ret = -EBUSY;
  717. goto err_lock;
  718. }
  719. acpi_debugger.owner = owner;
  720. acpi_debugger.ops = ops;
  721. err_lock:
  722. mutex_unlock(&acpi_debugger.lock);
  723. return ret;
  724. }
  725. EXPORT_SYMBOL(acpi_register_debugger);
  726. void acpi_unregister_debugger(const struct acpi_debugger_ops *ops)
  727. {
  728. mutex_lock(&acpi_debugger.lock);
  729. if (ops == acpi_debugger.ops) {
  730. acpi_debugger.ops = NULL;
  731. acpi_debugger.owner = NULL;
  732. }
  733. mutex_unlock(&acpi_debugger.lock);
  734. }
  735. EXPORT_SYMBOL(acpi_unregister_debugger);
  736. int acpi_debugger_create_thread(acpi_osd_exec_callback function, void *context)
  737. {
  738. int ret;
  739. int (*func)(acpi_osd_exec_callback, void *);
  740. struct module *owner;
  741. if (!acpi_debugger_initialized)
  742. return -ENODEV;
  743. mutex_lock(&acpi_debugger.lock);
  744. if (!acpi_debugger.ops) {
  745. ret = -ENODEV;
  746. goto err_lock;
  747. }
  748. if (!try_module_get(acpi_debugger.owner)) {
  749. ret = -ENODEV;
  750. goto err_lock;
  751. }
  752. func = acpi_debugger.ops->create_thread;
  753. owner = acpi_debugger.owner;
  754. mutex_unlock(&acpi_debugger.lock);
  755. ret = func(function, context);
  756. mutex_lock(&acpi_debugger.lock);
  757. module_put(owner);
  758. err_lock:
  759. mutex_unlock(&acpi_debugger.lock);
  760. return ret;
  761. }
  762. ssize_t acpi_debugger_write_log(const char *msg)
  763. {
  764. ssize_t ret;
  765. ssize_t (*func)(const char *);
  766. struct module *owner;
  767. if (!acpi_debugger_initialized)
  768. return -ENODEV;
  769. mutex_lock(&acpi_debugger.lock);
  770. if (!acpi_debugger.ops) {
  771. ret = -ENODEV;
  772. goto err_lock;
  773. }
  774. if (!try_module_get(acpi_debugger.owner)) {
  775. ret = -ENODEV;
  776. goto err_lock;
  777. }
  778. func = acpi_debugger.ops->write_log;
  779. owner = acpi_debugger.owner;
  780. mutex_unlock(&acpi_debugger.lock);
  781. ret = func(msg);
  782. mutex_lock(&acpi_debugger.lock);
  783. module_put(owner);
  784. err_lock:
  785. mutex_unlock(&acpi_debugger.lock);
  786. return ret;
  787. }
  788. ssize_t acpi_debugger_read_cmd(char *buffer, size_t buffer_length)
  789. {
  790. ssize_t ret;
  791. ssize_t (*func)(char *, size_t);
  792. struct module *owner;
  793. if (!acpi_debugger_initialized)
  794. return -ENODEV;
  795. mutex_lock(&acpi_debugger.lock);
  796. if (!acpi_debugger.ops) {
  797. ret = -ENODEV;
  798. goto err_lock;
  799. }
  800. if (!try_module_get(acpi_debugger.owner)) {
  801. ret = -ENODEV;
  802. goto err_lock;
  803. }
  804. func = acpi_debugger.ops->read_cmd;
  805. owner = acpi_debugger.owner;
  806. mutex_unlock(&acpi_debugger.lock);
  807. ret = func(buffer, buffer_length);
  808. mutex_lock(&acpi_debugger.lock);
  809. module_put(owner);
  810. err_lock:
  811. mutex_unlock(&acpi_debugger.lock);
  812. return ret;
  813. }
  814. int acpi_debugger_wait_command_ready(void)
  815. {
  816. int ret;
  817. int (*func)(bool, char *, size_t);
  818. struct module *owner;
  819. if (!acpi_debugger_initialized)
  820. return -ENODEV;
  821. mutex_lock(&acpi_debugger.lock);
  822. if (!acpi_debugger.ops) {
  823. ret = -ENODEV;
  824. goto err_lock;
  825. }
  826. if (!try_module_get(acpi_debugger.owner)) {
  827. ret = -ENODEV;
  828. goto err_lock;
  829. }
  830. func = acpi_debugger.ops->wait_command_ready;
  831. owner = acpi_debugger.owner;
  832. mutex_unlock(&acpi_debugger.lock);
  833. ret = func(acpi_gbl_method_executing,
  834. acpi_gbl_db_line_buf, ACPI_DB_LINE_BUFFER_SIZE);
  835. mutex_lock(&acpi_debugger.lock);
  836. module_put(owner);
  837. err_lock:
  838. mutex_unlock(&acpi_debugger.lock);
  839. return ret;
  840. }
  841. int acpi_debugger_notify_command_complete(void)
  842. {
  843. int ret;
  844. int (*func)(void);
  845. struct module *owner;
  846. if (!acpi_debugger_initialized)
  847. return -ENODEV;
  848. mutex_lock(&acpi_debugger.lock);
  849. if (!acpi_debugger.ops) {
  850. ret = -ENODEV;
  851. goto err_lock;
  852. }
  853. if (!try_module_get(acpi_debugger.owner)) {
  854. ret = -ENODEV;
  855. goto err_lock;
  856. }
  857. func = acpi_debugger.ops->notify_command_complete;
  858. owner = acpi_debugger.owner;
  859. mutex_unlock(&acpi_debugger.lock);
  860. ret = func();
  861. mutex_lock(&acpi_debugger.lock);
  862. module_put(owner);
  863. err_lock:
  864. mutex_unlock(&acpi_debugger.lock);
  865. return ret;
  866. }
  867. int __init acpi_debugger_init(void)
  868. {
  869. mutex_init(&acpi_debugger.lock);
  870. acpi_debugger_initialized = true;
  871. return 0;
  872. }
  873. #endif
  874. /*******************************************************************************
  875. *
  876. * FUNCTION: acpi_os_execute
  877. *
  878. * PARAMETERS: Type - Type of the callback
  879. * Function - Function to be executed
  880. * Context - Function parameters
  881. *
  882. * RETURN: Status
  883. *
  884. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  885. * immediately executes function on a separate thread.
  886. *
  887. ******************************************************************************/
  888. acpi_status acpi_os_execute(acpi_execute_type type,
  889. acpi_osd_exec_callback function, void *context)
  890. {
  891. acpi_status status = AE_OK;
  892. struct acpi_os_dpc *dpc;
  893. struct workqueue_struct *queue;
  894. int ret;
  895. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  896. "Scheduling function [%p(%p)] for deferred execution.\n",
  897. function, context));
  898. if (type == OSL_DEBUGGER_MAIN_THREAD) {
  899. ret = acpi_debugger_create_thread(function, context);
  900. if (ret) {
  901. pr_err("Call to kthread_create() failed.\n");
  902. status = AE_ERROR;
  903. }
  904. goto out_thread;
  905. }
  906. /*
  907. * Allocate/initialize DPC structure. Note that this memory will be
  908. * freed by the callee. The kernel handles the work_struct list in a
  909. * way that allows us to also free its memory inside the callee.
  910. * Because we may want to schedule several tasks with different
  911. * parameters we can't use the approach some kernel code uses of
  912. * having a static work_struct.
  913. */
  914. dpc = kzalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  915. if (!dpc)
  916. return AE_NO_MEMORY;
  917. dpc->function = function;
  918. dpc->context = context;
  919. /*
  920. * To prevent lockdep from complaining unnecessarily, make sure that
  921. * there is a different static lockdep key for each workqueue by using
  922. * INIT_WORK() for each of them separately.
  923. */
  924. if (type == OSL_NOTIFY_HANDLER) {
  925. queue = kacpi_notify_wq;
  926. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  927. } else if (type == OSL_GPE_HANDLER) {
  928. queue = kacpid_wq;
  929. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  930. } else {
  931. pr_err("Unsupported os_execute type %d.\n", type);
  932. status = AE_ERROR;
  933. }
  934. if (ACPI_FAILURE(status))
  935. goto err_workqueue;
  936. /*
  937. * On some machines, a software-initiated SMI causes corruption unless
  938. * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
  939. * typically it's done in GPE-related methods that are run via
  940. * workqueues, so we can avoid the known corruption cases by always
  941. * queueing on CPU 0.
  942. */
  943. ret = queue_work_on(0, queue, &dpc->work);
  944. if (!ret) {
  945. printk(KERN_ERR PREFIX
  946. "Call to queue_work() failed.\n");
  947. status = AE_ERROR;
  948. }
  949. err_workqueue:
  950. if (ACPI_FAILURE(status))
  951. kfree(dpc);
  952. out_thread:
  953. return status;
  954. }
  955. EXPORT_SYMBOL(acpi_os_execute);
  956. void acpi_os_wait_events_complete(void)
  957. {
  958. /*
  959. * Make sure the GPE handler or the fixed event handler is not used
  960. * on another CPU after removal.
  961. */
  962. if (acpi_sci_irq_valid())
  963. synchronize_hardirq(acpi_sci_irq);
  964. flush_workqueue(kacpid_wq);
  965. flush_workqueue(kacpi_notify_wq);
  966. }
  967. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  968. struct acpi_hp_work {
  969. struct work_struct work;
  970. struct acpi_device *adev;
  971. u32 src;
  972. };
  973. static void acpi_hotplug_work_fn(struct work_struct *work)
  974. {
  975. struct acpi_hp_work *hpw = container_of(work, struct acpi_hp_work, work);
  976. acpi_os_wait_events_complete();
  977. acpi_device_hotplug(hpw->adev, hpw->src);
  978. kfree(hpw);
  979. }
  980. acpi_status acpi_hotplug_schedule(struct acpi_device *adev, u32 src)
  981. {
  982. struct acpi_hp_work *hpw;
  983. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  984. "Scheduling hotplug event (%p, %u) for deferred execution.\n",
  985. adev, src));
  986. hpw = kmalloc(sizeof(*hpw), GFP_KERNEL);
  987. if (!hpw)
  988. return AE_NO_MEMORY;
  989. INIT_WORK(&hpw->work, acpi_hotplug_work_fn);
  990. hpw->adev = adev;
  991. hpw->src = src;
  992. /*
  993. * We can't run hotplug code in kacpid_wq/kacpid_notify_wq etc., because
  994. * the hotplug code may call driver .remove() functions, which may
  995. * invoke flush_scheduled_work()/acpi_os_wait_events_complete() to flush
  996. * these workqueues.
  997. */
  998. if (!queue_work(kacpi_hotplug_wq, &hpw->work)) {
  999. kfree(hpw);
  1000. return AE_ERROR;
  1001. }
  1002. return AE_OK;
  1003. }
  1004. bool acpi_queue_hotplug_work(struct work_struct *work)
  1005. {
  1006. return queue_work(kacpi_hotplug_wq, work);
  1007. }
  1008. acpi_status
  1009. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  1010. {
  1011. struct semaphore *sem = NULL;
  1012. sem = acpi_os_allocate_zeroed(sizeof(struct semaphore));
  1013. if (!sem)
  1014. return AE_NO_MEMORY;
  1015. sema_init(sem, initial_units);
  1016. *handle = (acpi_handle *) sem;
  1017. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  1018. *handle, initial_units));
  1019. return AE_OK;
  1020. }
  1021. /*
  1022. * TODO: A better way to delete semaphores? Linux doesn't have a
  1023. * 'delete_semaphore()' function -- may result in an invalid
  1024. * pointer dereference for non-synchronized consumers. Should
  1025. * we at least check for blocked threads and signal/cancel them?
  1026. */
  1027. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  1028. {
  1029. struct semaphore *sem = (struct semaphore *)handle;
  1030. if (!sem)
  1031. return AE_BAD_PARAMETER;
  1032. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  1033. BUG_ON(!list_empty(&sem->wait_list));
  1034. kfree(sem);
  1035. sem = NULL;
  1036. return AE_OK;
  1037. }
  1038. /*
  1039. * TODO: Support for units > 1?
  1040. */
  1041. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  1042. {
  1043. acpi_status status = AE_OK;
  1044. struct semaphore *sem = (struct semaphore *)handle;
  1045. long jiffies;
  1046. int ret = 0;
  1047. if (!acpi_os_initialized)
  1048. return AE_OK;
  1049. if (!sem || (units < 1))
  1050. return AE_BAD_PARAMETER;
  1051. if (units > 1)
  1052. return AE_SUPPORT;
  1053. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  1054. handle, units, timeout));
  1055. if (timeout == ACPI_WAIT_FOREVER)
  1056. jiffies = MAX_SCHEDULE_TIMEOUT;
  1057. else
  1058. jiffies = msecs_to_jiffies(timeout);
  1059. ret = down_timeout(sem, jiffies);
  1060. if (ret)
  1061. status = AE_TIME;
  1062. if (ACPI_FAILURE(status)) {
  1063. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1064. "Failed to acquire semaphore[%p|%d|%d], %s",
  1065. handle, units, timeout,
  1066. acpi_format_exception(status)));
  1067. } else {
  1068. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  1069. "Acquired semaphore[%p|%d|%d]", handle,
  1070. units, timeout));
  1071. }
  1072. return status;
  1073. }
  1074. /*
  1075. * TODO: Support for units > 1?
  1076. */
  1077. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  1078. {
  1079. struct semaphore *sem = (struct semaphore *)handle;
  1080. if (!acpi_os_initialized)
  1081. return AE_OK;
  1082. if (!sem || (units < 1))
  1083. return AE_BAD_PARAMETER;
  1084. if (units > 1)
  1085. return AE_SUPPORT;
  1086. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  1087. units));
  1088. up(sem);
  1089. return AE_OK;
  1090. }
  1091. acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
  1092. {
  1093. #ifdef ENABLE_DEBUGGER
  1094. if (acpi_in_debugger) {
  1095. u32 chars;
  1096. kdb_read(buffer, buffer_length);
  1097. /* remove the CR kdb includes */
  1098. chars = strlen(buffer) - 1;
  1099. buffer[chars] = '\0';
  1100. }
  1101. #else
  1102. int ret;
  1103. ret = acpi_debugger_read_cmd(buffer, buffer_length);
  1104. if (ret < 0)
  1105. return AE_ERROR;
  1106. if (bytes_read)
  1107. *bytes_read = ret;
  1108. #endif
  1109. return AE_OK;
  1110. }
  1111. EXPORT_SYMBOL(acpi_os_get_line);
  1112. acpi_status acpi_os_wait_command_ready(void)
  1113. {
  1114. int ret;
  1115. ret = acpi_debugger_wait_command_ready();
  1116. if (ret < 0)
  1117. return AE_ERROR;
  1118. return AE_OK;
  1119. }
  1120. acpi_status acpi_os_notify_command_complete(void)
  1121. {
  1122. int ret;
  1123. ret = acpi_debugger_notify_command_complete();
  1124. if (ret < 0)
  1125. return AE_ERROR;
  1126. return AE_OK;
  1127. }
  1128. acpi_status acpi_os_signal(u32 function, void *info)
  1129. {
  1130. switch (function) {
  1131. case ACPI_SIGNAL_FATAL:
  1132. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  1133. break;
  1134. case ACPI_SIGNAL_BREAKPOINT:
  1135. /*
  1136. * AML Breakpoint
  1137. * ACPI spec. says to treat it as a NOP unless
  1138. * you are debugging. So if/when we integrate
  1139. * AML debugger into the kernel debugger its
  1140. * hook will go here. But until then it is
  1141. * not useful to print anything on breakpoints.
  1142. */
  1143. break;
  1144. default:
  1145. break;
  1146. }
  1147. return AE_OK;
  1148. }
  1149. static int __init acpi_os_name_setup(char *str)
  1150. {
  1151. char *p = acpi_os_name;
  1152. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  1153. if (!str || !*str)
  1154. return 0;
  1155. for (; count-- && *str; str++) {
  1156. if (isalnum(*str) || *str == ' ' || *str == ':')
  1157. *p++ = *str;
  1158. else if (*str == '\'' || *str == '"')
  1159. continue;
  1160. else
  1161. break;
  1162. }
  1163. *p = 0;
  1164. return 1;
  1165. }
  1166. __setup("acpi_os_name=", acpi_os_name_setup);
  1167. /*
  1168. * Disable the auto-serialization of named objects creation methods.
  1169. *
  1170. * This feature is enabled by default. It marks the AML control methods
  1171. * that contain the opcodes to create named objects as "Serialized".
  1172. */
  1173. static int __init acpi_no_auto_serialize_setup(char *str)
  1174. {
  1175. acpi_gbl_auto_serialize_methods = FALSE;
  1176. pr_info("ACPI: auto-serialization disabled\n");
  1177. return 1;
  1178. }
  1179. __setup("acpi_no_auto_serialize", acpi_no_auto_serialize_setup);
  1180. /* Check of resource interference between native drivers and ACPI
  1181. * OperationRegions (SystemIO and System Memory only).
  1182. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  1183. * in arbitrary AML code and can interfere with legacy drivers.
  1184. * acpi_enforce_resources= can be set to:
  1185. *
  1186. * - strict (default) (2)
  1187. * -> further driver trying to access the resources will not load
  1188. * - lax (1)
  1189. * -> further driver trying to access the resources will load, but you
  1190. * get a system message that something might go wrong...
  1191. *
  1192. * - no (0)
  1193. * -> ACPI Operation Region resources will not be registered
  1194. *
  1195. */
  1196. #define ENFORCE_RESOURCES_STRICT 2
  1197. #define ENFORCE_RESOURCES_LAX 1
  1198. #define ENFORCE_RESOURCES_NO 0
  1199. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1200. static int __init acpi_enforce_resources_setup(char *str)
  1201. {
  1202. if (str == NULL || *str == '\0')
  1203. return 0;
  1204. if (!strcmp("strict", str))
  1205. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  1206. else if (!strcmp("lax", str))
  1207. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  1208. else if (!strcmp("no", str))
  1209. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  1210. return 1;
  1211. }
  1212. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  1213. /* Check for resource conflicts between ACPI OperationRegions and native
  1214. * drivers */
  1215. int acpi_check_resource_conflict(const struct resource *res)
  1216. {
  1217. acpi_adr_space_type space_id;
  1218. acpi_size length;
  1219. u8 warn = 0;
  1220. int clash = 0;
  1221. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1222. return 0;
  1223. if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
  1224. return 0;
  1225. if (res->flags & IORESOURCE_IO)
  1226. space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  1227. else
  1228. space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
  1229. length = resource_size(res);
  1230. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO)
  1231. warn = 1;
  1232. clash = acpi_check_address_range(space_id, res->start, length, warn);
  1233. if (clash) {
  1234. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
  1235. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  1236. printk(KERN_NOTICE "ACPI: This conflict may"
  1237. " cause random problems and system"
  1238. " instability\n");
  1239. printk(KERN_INFO "ACPI: If an ACPI driver is available"
  1240. " for this device, you should use it instead of"
  1241. " the native driver\n");
  1242. }
  1243. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  1244. return -EBUSY;
  1245. }
  1246. return 0;
  1247. }
  1248. EXPORT_SYMBOL(acpi_check_resource_conflict);
  1249. int acpi_check_region(resource_size_t start, resource_size_t n,
  1250. const char *name)
  1251. {
  1252. struct resource res = {
  1253. .start = start,
  1254. .end = start + n - 1,
  1255. .name = name,
  1256. .flags = IORESOURCE_IO,
  1257. };
  1258. return acpi_check_resource_conflict(&res);
  1259. }
  1260. EXPORT_SYMBOL(acpi_check_region);
  1261. static acpi_status acpi_deactivate_mem_region(acpi_handle handle, u32 level,
  1262. void *_res, void **return_value)
  1263. {
  1264. struct acpi_mem_space_context **mem_ctx;
  1265. union acpi_operand_object *handler_obj;
  1266. union acpi_operand_object *region_obj2;
  1267. union acpi_operand_object *region_obj;
  1268. struct resource *res = _res;
  1269. acpi_status status;
  1270. region_obj = acpi_ns_get_attached_object(handle);
  1271. if (!region_obj)
  1272. return AE_OK;
  1273. handler_obj = region_obj->region.handler;
  1274. if (!handler_obj)
  1275. return AE_OK;
  1276. if (region_obj->region.space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  1277. return AE_OK;
  1278. if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE))
  1279. return AE_OK;
  1280. region_obj2 = acpi_ns_get_secondary_object(region_obj);
  1281. if (!region_obj2)
  1282. return AE_OK;
  1283. mem_ctx = (void *)&region_obj2->extra.region_context;
  1284. if (!(mem_ctx[0]->address >= res->start &&
  1285. mem_ctx[0]->address < res->end))
  1286. return AE_OK;
  1287. status = handler_obj->address_space.setup(region_obj,
  1288. ACPI_REGION_DEACTIVATE,
  1289. NULL, (void **)mem_ctx);
  1290. if (ACPI_SUCCESS(status))
  1291. region_obj->region.flags &= ~(AOPOBJ_SETUP_COMPLETE);
  1292. return status;
  1293. }
  1294. /**
  1295. * acpi_release_memory - Release any mappings done to a memory region
  1296. * @handle: Handle to namespace node
  1297. * @res: Memory resource
  1298. * @level: A level that terminates the search
  1299. *
  1300. * Walks through @handle and unmaps all SystemMemory Operation Regions that
  1301. * overlap with @res and that have already been activated (mapped).
  1302. *
  1303. * This is a helper that allows drivers to place special requirements on memory
  1304. * region that may overlap with operation regions, primarily allowing them to
  1305. * safely map the region as non-cached memory.
  1306. *
  1307. * The unmapped Operation Regions will be automatically remapped next time they
  1308. * are called, so the drivers do not need to do anything else.
  1309. */
  1310. acpi_status acpi_release_memory(acpi_handle handle, struct resource *res,
  1311. u32 level)
  1312. {
  1313. if (!(res->flags & IORESOURCE_MEM))
  1314. return AE_TYPE;
  1315. return acpi_walk_namespace(ACPI_TYPE_REGION, handle, level,
  1316. acpi_deactivate_mem_region, NULL, res, NULL);
  1317. }
  1318. EXPORT_SYMBOL_GPL(acpi_release_memory);
  1319. /*
  1320. * Let drivers know whether the resource checks are effective
  1321. */
  1322. int acpi_resources_are_enforced(void)
  1323. {
  1324. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  1325. }
  1326. EXPORT_SYMBOL(acpi_resources_are_enforced);
  1327. /*
  1328. * Deallocate the memory for a spinlock.
  1329. */
  1330. void acpi_os_delete_lock(acpi_spinlock handle)
  1331. {
  1332. ACPI_FREE(handle);
  1333. }
  1334. /*
  1335. * Acquire a spinlock.
  1336. *
  1337. * handle is a pointer to the spinlock_t.
  1338. */
  1339. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1340. __acquires(lockp)
  1341. {
  1342. acpi_cpu_flags flags;
  1343. spin_lock_irqsave(lockp, flags);
  1344. return flags;
  1345. }
  1346. /*
  1347. * Release a spinlock. See above.
  1348. */
  1349. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  1350. __releases(lockp)
  1351. {
  1352. spin_unlock_irqrestore(lockp, flags);
  1353. }
  1354. #ifndef ACPI_USE_LOCAL_CACHE
  1355. /*******************************************************************************
  1356. *
  1357. * FUNCTION: acpi_os_create_cache
  1358. *
  1359. * PARAMETERS: name - Ascii name for the cache
  1360. * size - Size of each cached object
  1361. * depth - Maximum depth of the cache (in objects) <ignored>
  1362. * cache - Where the new cache object is returned
  1363. *
  1364. * RETURN: status
  1365. *
  1366. * DESCRIPTION: Create a cache object
  1367. *
  1368. ******************************************************************************/
  1369. acpi_status
  1370. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  1371. {
  1372. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1373. if (*cache == NULL)
  1374. return AE_ERROR;
  1375. else
  1376. return AE_OK;
  1377. }
  1378. /*******************************************************************************
  1379. *
  1380. * FUNCTION: acpi_os_purge_cache
  1381. *
  1382. * PARAMETERS: Cache - Handle to cache object
  1383. *
  1384. * RETURN: Status
  1385. *
  1386. * DESCRIPTION: Free all objects within the requested cache.
  1387. *
  1388. ******************************************************************************/
  1389. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  1390. {
  1391. kmem_cache_shrink(cache);
  1392. return (AE_OK);
  1393. }
  1394. /*******************************************************************************
  1395. *
  1396. * FUNCTION: acpi_os_delete_cache
  1397. *
  1398. * PARAMETERS: Cache - Handle to cache object
  1399. *
  1400. * RETURN: Status
  1401. *
  1402. * DESCRIPTION: Free all objects within the requested cache and delete the
  1403. * cache object.
  1404. *
  1405. ******************************************************************************/
  1406. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  1407. {
  1408. kmem_cache_destroy(cache);
  1409. return (AE_OK);
  1410. }
  1411. /*******************************************************************************
  1412. *
  1413. * FUNCTION: acpi_os_release_object
  1414. *
  1415. * PARAMETERS: Cache - Handle to cache object
  1416. * Object - The object to be released
  1417. *
  1418. * RETURN: None
  1419. *
  1420. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1421. * the object is deleted.
  1422. *
  1423. ******************************************************************************/
  1424. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1425. {
  1426. kmem_cache_free(cache, object);
  1427. return (AE_OK);
  1428. }
  1429. #endif
  1430. static int __init acpi_no_static_ssdt_setup(char *s)
  1431. {
  1432. acpi_gbl_disable_ssdt_table_install = TRUE;
  1433. pr_info("ACPI: static SSDT installation disabled\n");
  1434. return 0;
  1435. }
  1436. early_param("acpi_no_static_ssdt", acpi_no_static_ssdt_setup);
  1437. static int __init acpi_disable_return_repair(char *s)
  1438. {
  1439. printk(KERN_NOTICE PREFIX
  1440. "ACPI: Predefined validation mechanism disabled\n");
  1441. acpi_gbl_disable_auto_repair = TRUE;
  1442. return 1;
  1443. }
  1444. __setup("acpica_no_return_repair", acpi_disable_return_repair);
  1445. acpi_status __init acpi_os_initialize(void)
  1446. {
  1447. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1448. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1449. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1450. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1451. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) {
  1452. /*
  1453. * Use acpi_os_map_generic_address to pre-map the reset
  1454. * register if it's in system memory.
  1455. */
  1456. int rv;
  1457. rv = acpi_os_map_generic_address(&acpi_gbl_FADT.reset_register);
  1458. pr_debug(PREFIX "%s: map reset_reg status %d\n", __func__, rv);
  1459. }
  1460. acpi_os_initialized = true;
  1461. return AE_OK;
  1462. }
  1463. acpi_status __init acpi_os_initialize1(void)
  1464. {
  1465. kacpid_wq = alloc_workqueue("kacpid", 0, 1);
  1466. kacpi_notify_wq = alloc_workqueue("kacpi_notify", 0, 1);
  1467. kacpi_hotplug_wq = alloc_ordered_workqueue("kacpi_hotplug", 0);
  1468. BUG_ON(!kacpid_wq);
  1469. BUG_ON(!kacpi_notify_wq);
  1470. BUG_ON(!kacpi_hotplug_wq);
  1471. acpi_osi_init();
  1472. return AE_OK;
  1473. }
  1474. acpi_status acpi_os_terminate(void)
  1475. {
  1476. if (acpi_irq_handler) {
  1477. acpi_os_remove_interrupt_handler(acpi_gbl_FADT.sci_interrupt,
  1478. acpi_irq_handler);
  1479. }
  1480. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1481. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1482. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1483. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1484. if (acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)
  1485. acpi_os_unmap_generic_address(&acpi_gbl_FADT.reset_register);
  1486. destroy_workqueue(kacpid_wq);
  1487. destroy_workqueue(kacpi_notify_wq);
  1488. destroy_workqueue(kacpi_hotplug_wq);
  1489. return AE_OK;
  1490. }
  1491. acpi_status acpi_os_prepare_sleep(u8 sleep_state, u32 pm1a_control,
  1492. u32 pm1b_control)
  1493. {
  1494. int rc = 0;
  1495. if (__acpi_os_prepare_sleep)
  1496. rc = __acpi_os_prepare_sleep(sleep_state,
  1497. pm1a_control, pm1b_control);
  1498. if (rc < 0)
  1499. return AE_ERROR;
  1500. else if (rc > 0)
  1501. return AE_CTRL_TERMINATE;
  1502. return AE_OK;
  1503. }
  1504. void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
  1505. u32 pm1a_ctrl, u32 pm1b_ctrl))
  1506. {
  1507. __acpi_os_prepare_sleep = func;
  1508. }
  1509. #if (ACPI_REDUCED_HARDWARE)
  1510. acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
  1511. u32 val_b)
  1512. {
  1513. int rc = 0;
  1514. if (__acpi_os_prepare_extended_sleep)
  1515. rc = __acpi_os_prepare_extended_sleep(sleep_state,
  1516. val_a, val_b);
  1517. if (rc < 0)
  1518. return AE_ERROR;
  1519. else if (rc > 0)
  1520. return AE_CTRL_TERMINATE;
  1521. return AE_OK;
  1522. }
  1523. #else
  1524. acpi_status acpi_os_prepare_extended_sleep(u8 sleep_state, u32 val_a,
  1525. u32 val_b)
  1526. {
  1527. return AE_OK;
  1528. }
  1529. #endif
  1530. void acpi_os_set_prepare_extended_sleep(int (*func)(u8 sleep_state,
  1531. u32 val_a, u32 val_b))
  1532. {
  1533. __acpi_os_prepare_extended_sleep = func;
  1534. }
  1535. acpi_status acpi_os_enter_sleep(u8 sleep_state,
  1536. u32 reg_a_value, u32 reg_b_value)
  1537. {
  1538. acpi_status status;
  1539. if (acpi_gbl_reduced_hardware)
  1540. status = acpi_os_prepare_extended_sleep(sleep_state,
  1541. reg_a_value,
  1542. reg_b_value);
  1543. else
  1544. status = acpi_os_prepare_sleep(sleep_state,
  1545. reg_a_value, reg_b_value);
  1546. return status;
  1547. }