2 * atomicio.c - ACPI IO memory pre-mapping/post-unmapping, then
3 * accessing in atomic context.
5 * This is used for NMI handler to access IO memory area, because
6 * ioremap/iounmap can not be used in NMI handler. The IO memory area
7 * is pre-mapped in process context and accessed in NMI handler.
9 * Copyright (C) 2009-2010, Intel Corp.
10 * Author: Huang Ying <ying.huang@intel.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version
14 * 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/acpi.h>
31 #include <linux/kref.h>
32 #include <linux/rculist.h>
33 #include <linux/interrupt.h>
34 #include <acpi/atomicio.h>
36 #define ACPI_PFX "ACPI: "
38 static LIST_HEAD(acpi_iomaps);
40 * Used for mutual exclusion between writers of acpi_iomaps list, for
41 * synchronization between readers and writer, RCU is used.
43 static DEFINE_SPINLOCK(acpi_iomaps_lock);
46 struct list_head list;
53 /* acpi_iomaps_lock or RCU read lock must be held before calling */
54 static struct acpi_iomap *__acpi_find_iomap(phys_addr_t paddr,
57 struct acpi_iomap *map;
59 list_for_each_entry_rcu(map, &acpi_iomaps, list) {
60 if (map->paddr + map->size >= paddr + size &&
68 * Atomic "ioremap" used by NMI handler, if the specified IO memory
69 * area is not pre-mapped, NULL will be returned.
71 * acpi_iomaps_lock or RCU read lock must be held before calling
73 static void __iomem *__acpi_ioremap_fast(phys_addr_t paddr,
76 struct acpi_iomap *map;
78 map = __acpi_find_iomap(paddr, size);
80 return map->vaddr + (paddr - map->paddr);
85 /* acpi_iomaps_lock must be held before calling */
86 static void __iomem *__acpi_try_ioremap(phys_addr_t paddr,
89 struct acpi_iomap *map;
91 map = __acpi_find_iomap(paddr, size);
94 return map->vaddr + (paddr - map->paddr);
100 * Used to pre-map the specified IO memory area. First try to find
101 * whether the area is already pre-mapped, if it is, increase the
102 * reference count (in __acpi_try_ioremap) and return; otherwise, do
103 * the real ioremap, and add the mapping into acpi_iomaps list.
105 static void __iomem *acpi_pre_map(phys_addr_t paddr,
109 struct acpi_iomap *map;
110 unsigned long pg_sz, flags;
113 spin_lock_irqsave(&acpi_iomaps_lock, flags);
114 vaddr = __acpi_try_ioremap(paddr, size);
115 spin_unlock_irqrestore(&acpi_iomaps_lock, flags);
119 pg_off = paddr & PAGE_MASK;
120 pg_sz = ((paddr + size + PAGE_SIZE - 1) & PAGE_MASK) - pg_off;
121 vaddr = ioremap(pg_off, pg_sz);
124 map = kmalloc(sizeof(*map), GFP_KERNEL);
127 INIT_LIST_HEAD(&map->list);
131 kref_init(&map->ref);
133 spin_lock_irqsave(&acpi_iomaps_lock, flags);
134 vaddr = __acpi_try_ioremap(paddr, size);
136 spin_unlock_irqrestore(&acpi_iomaps_lock, flags);
141 list_add_tail_rcu(&map->list, &acpi_iomaps);
142 spin_unlock_irqrestore(&acpi_iomaps_lock, flags);
144 return vaddr + (paddr - pg_off);
150 /* acpi_iomaps_lock must be held before calling */
151 static void __acpi_kref_del_iomap(struct kref *ref)
153 struct acpi_iomap *map;
155 map = container_of(ref, struct acpi_iomap, ref);
156 list_del_rcu(&map->list);
160 * Used to post-unmap the specified IO memory area. The iounmap is
161 * done only if the reference count goes zero.
163 static void acpi_post_unmap(phys_addr_t paddr, unsigned long size)
165 struct acpi_iomap *map;
169 spin_lock_irqsave(&acpi_iomaps_lock, flags);
170 map = __acpi_find_iomap(paddr, size);
172 del = kref_put(&map->ref, __acpi_kref_del_iomap);
173 spin_unlock_irqrestore(&acpi_iomaps_lock, flags);
183 /* In NMI handler, should set silent = 1 */
184 static int acpi_check_gar(struct acpi_generic_address *reg,
185 u64 *paddr, int silent)
189 width = reg->bit_width;
190 space_id = reg->space_id;
191 /* Handle possible alignment issues */
192 memcpy(paddr, ®->address, sizeof(*paddr));
195 pr_warning(FW_BUG ACPI_PFX
196 "Invalid physical address in GAR [0x%llx/%u/%u]\n",
197 *paddr, width, space_id);
201 if ((width != 8) && (width != 16) && (width != 32) && (width != 64)) {
203 pr_warning(FW_BUG ACPI_PFX
204 "Invalid bit width in GAR [0x%llx/%u/%u]\n",
205 *paddr, width, space_id);
209 if (space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY &&
210 space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
212 pr_warning(FW_BUG ACPI_PFX
213 "Invalid address space type in GAR [0x%llx/%u/%u]\n",
214 *paddr, width, space_id);
221 /* Pre-map, working on GAR */
222 int acpi_pre_map_gar(struct acpi_generic_address *reg)
228 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
231 rc = acpi_check_gar(reg, &paddr, 0);
235 vaddr = acpi_pre_map(paddr, reg->bit_width / 8);
241 EXPORT_SYMBOL_GPL(acpi_pre_map_gar);
243 /* Post-unmap, working on GAR */
244 int acpi_post_unmap_gar(struct acpi_generic_address *reg)
249 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
252 rc = acpi_check_gar(reg, &paddr, 0);
256 acpi_post_unmap(paddr, reg->bit_width / 8);
260 EXPORT_SYMBOL_GPL(acpi_post_unmap_gar);
263 * Can be used in atomic (including NMI) or process context. RCU read
264 * lock can only be released after the IO memory area accessing.
266 static int acpi_atomic_read_mem(u64 paddr, u64 *val, u32 width)
271 addr = __acpi_ioremap_fast(paddr, width);
293 static int acpi_atomic_write_mem(u64 paddr, u64 val, u32 width)
298 addr = __acpi_ioremap_fast(paddr, width);
320 /* GAR accessing in atomic (including NMI) or process context */
321 int acpi_atomic_read(u64 *val, struct acpi_generic_address *reg)
326 rc = acpi_check_gar(reg, &paddr, 1);
331 switch (reg->space_id) {
332 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
333 return acpi_atomic_read_mem(paddr, val, reg->bit_width);
334 case ACPI_ADR_SPACE_SYSTEM_IO:
335 return acpi_os_read_port(paddr, (u32 *)val, reg->bit_width);
340 EXPORT_SYMBOL_GPL(acpi_atomic_read);
342 int acpi_atomic_write(u64 val, struct acpi_generic_address *reg)
347 rc = acpi_check_gar(reg, &paddr, 1);
351 switch (reg->space_id) {
352 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
353 return acpi_atomic_write_mem(paddr, val, reg->bit_width);
354 case ACPI_ADR_SPACE_SYSTEM_IO:
355 return acpi_os_write_port(paddr, val, reg->bit_width);
360 EXPORT_SYMBOL_GPL(acpi_atomic_write);