ia64/xen-unstable

view linux-2.6.10-xen-sparse/arch/xen/i386/mm/ioremap.c @ 3678:87ad759770a2

bitkeeper revision 1.1159.212.101 (4205e445duHww3eKAPqc0vdxWD-fKQ)

trivial fix to the ioremap for local pages
author iap10@freefall.cl.cam.ac.uk
date Sun Feb 06 09:32:53 2005 +0000 (2005-02-06)
parents e922aa58971a
children 176610077cd4
line source
1 /*
2 * arch/i386/mm/ioremap.c
3 *
4 * Re-map IO memory to kernel address space so that we can access it.
5 * This is needed for high PCI addresses that aren't mapped in the
6 * 640k-1MB IO memory area on PC's
7 *
8 * (C) Copyright 1995 1996 Linus Torvalds
9 */
11 #include <linux/vmalloc.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <asm/io.h>
16 #include <asm/fixmap.h>
17 #include <asm/cacheflush.h>
18 #include <asm/tlbflush.h>
19 #include <asm/pgtable.h>
20 #include <asm/pgalloc.h>
22 #ifndef CONFIG_XEN_PHYSDEV_ACCESS
24 void * __ioremap(unsigned long phys_addr, unsigned long size,
25 unsigned long flags)
26 {
27 return NULL;
28 }
30 void *ioremap_nocache (unsigned long phys_addr, unsigned long size)
31 {
32 return NULL;
33 }
35 void iounmap(volatile void __iomem *addr)
36 {
37 }
39 void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
40 {
41 return NULL;
42 }
44 void __init bt_iounmap(void *addr, unsigned long size)
45 {
46 }
48 #else
50 /*
51 * Does @address reside within a non-highmem page that is local to this virtual
52 * machine (i.e., not an I/O page, nor a memory page belonging to another VM).
53 * See the comment that accompanies pte_pfn() in pgtable-2level.h to understand
54 * why this works.
55 */
56 static inline int is_local_lowmem(unsigned long address)
57 {
58 extern unsigned long max_low_pfn;
59 unsigned long mfn = address >> PAGE_SHIFT;
60 unsigned long pfn = mfn_to_pfn(mfn);
61 return ((pfn < max_low_pfn) && (pfn_to_mfn(pfn) == mfn));
62 }
64 /*
65 * Generic mapping function (not visible outside):
66 */
68 /*
69 * Remap an arbitrary physical address space into the kernel virtual
70 * address space. Needed when the kernel wants to access high addresses
71 * directly.
72 *
73 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
74 * have to convert them into an offset in a page-aligned mapping, but the
75 * caller shouldn't need to know that small detail.
76 */
77 void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
78 {
79 void __iomem * addr;
80 struct vm_struct * area;
81 unsigned long offset, last_addr;
82 domid_t domid = DOMID_IO;
84 /* Don't allow wraparound or zero size */
85 last_addr = phys_addr + size - 1;
86 if (!size || last_addr < phys_addr)
87 return NULL;
89 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
90 /*
91 * Don't remap the low PCI/ISA area, it's always mapped..
92 */
93 if (phys_addr >= 0x0 && last_addr < 0x100000)
94 return isa_bus_to_virt(phys_addr);
95 #endif
97 /*
98 * Don't allow anybody to remap normal RAM that we're using..
99 */
100 if (is_local_lowmem(phys_addr)) {
101 char *t_addr, *t_end;
102 struct page *page;
104 t_addr = bus_to_virt(phys_addr);
105 t_end = t_addr + (size - 1);
107 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
108 if(!PageReserved(page))
109 return NULL;
111 domid = DOMID_LOCAL;
112 }
114 /*
115 * Mappings have to be page-aligned
116 */
117 offset = phys_addr & ~PAGE_MASK;
118 phys_addr &= PAGE_MASK;
119 size = PAGE_ALIGN(last_addr+1) - phys_addr;
121 /*
122 * Ok, go for it..
123 */
124 area = get_vm_area(size, VM_IOREMAP);
125 if (!area)
126 return NULL;
127 area->phys_addr = phys_addr;
128 addr = (void __iomem *) area->addr;
129 if (direct_remap_area_pages(&init_mm, (unsigned long) addr, phys_addr,
130 size, __pgprot(_PAGE_PRESENT | _PAGE_RW |
131 _PAGE_DIRTY | _PAGE_ACCESSED
132 | flags), domid)) {
133 vunmap((void __force *) addr);
134 return NULL;
135 }
136 return (void __iomem *) (offset + (char __iomem *)addr);
137 }
140 /**
141 * ioremap_nocache - map bus memory into CPU space
142 * @offset: bus address of the memory
143 * @size: size of the resource to map
144 *
145 * ioremap_nocache performs a platform specific sequence of operations to
146 * make bus memory CPU accessible via the readb/readw/readl/writeb/
147 * writew/writel functions and the other mmio helpers. The returned
148 * address is not guaranteed to be usable directly as a virtual
149 * address.
150 *
151 * This version of ioremap ensures that the memory is marked uncachable
152 * on the CPU as well as honouring existing caching rules from things like
153 * the PCI bus. Note that there are other caches and buffers on many
154 * busses. In particular driver authors should read up on PCI writes
155 *
156 * It's useful if some control registers are in such an area and
157 * write combining or read caching is not desirable:
158 *
159 * Must be freed with iounmap.
160 */
162 void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
163 {
164 unsigned long last_addr;
165 void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD);
166 if (!p)
167 return p;
169 /* Guaranteed to be > phys_addr, as per __ioremap() */
170 last_addr = phys_addr + size - 1;
172 if (is_local_lowmem(last_addr)) {
173 struct page *ppage = virt_to_page(bus_to_virt(phys_addr));
174 unsigned long npages;
176 phys_addr &= PAGE_MASK;
178 /* This might overflow and become zero.. */
179 last_addr = PAGE_ALIGN(last_addr);
181 /* .. but that's ok, because modulo-2**n arithmetic will make
182 * the page-aligned "last - first" come out right.
183 */
184 npages = (last_addr - phys_addr) >> PAGE_SHIFT;
186 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
187 iounmap(p);
188 p = NULL;
189 }
190 global_flush_tlb();
191 }
193 return p;
194 }
196 void iounmap(volatile void __iomem *addr)
197 {
198 struct vm_struct *p;
199 if ((void __force *) addr <= high_memory)
200 return;
201 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
202 if ((unsigned long) addr >= fix_to_virt(FIX_ISAMAP_BEGIN))
203 return;
204 #endif
205 p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
206 if (!p) {
207 printk("__iounmap: bad address %p\n", addr);
208 return;
209 }
211 if (p->flags && is_local_lowmem(p->phys_addr)) {
212 change_page_attr(virt_to_page(bus_to_virt(p->phys_addr)),
213 p->size >> PAGE_SHIFT,
214 PAGE_KERNEL);
215 global_flush_tlb();
216 }
217 kfree(p);
218 }
220 void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
221 {
222 unsigned long offset, last_addr;
223 unsigned int nrpages;
224 enum fixed_addresses idx;
226 /* Don't allow wraparound or zero size */
227 last_addr = phys_addr + size - 1;
228 if (!size || last_addr < phys_addr)
229 return NULL;
231 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
232 /*
233 * Don't remap the low PCI/ISA area, it's always mapped..
234 */
235 if (phys_addr >= 0x0 && last_addr < 0x100000)
236 return isa_bus_to_virt(phys_addr);
237 #endif
239 /*
240 * Mappings have to be page-aligned
241 */
242 offset = phys_addr & ~PAGE_MASK;
243 phys_addr &= PAGE_MASK;
244 size = PAGE_ALIGN(last_addr) - phys_addr;
246 /*
247 * Mappings have to fit in the FIX_BTMAP area.
248 */
249 nrpages = size >> PAGE_SHIFT;
250 if (nrpages > NR_FIX_BTMAPS)
251 return NULL;
253 /*
254 * Ok, go for it..
255 */
256 idx = FIX_BTMAP_BEGIN;
257 while (nrpages > 0) {
258 set_fixmap_ma(idx, phys_addr);
259 phys_addr += PAGE_SIZE;
260 --idx;
261 --nrpages;
262 }
263 return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
264 }
266 void __init bt_iounmap(void *addr, unsigned long size)
267 {
268 unsigned long virt_addr;
269 unsigned long offset;
270 unsigned int nrpages;
271 enum fixed_addresses idx;
273 virt_addr = (unsigned long)addr;
274 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
275 return;
276 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
277 if (virt_addr >= fix_to_virt(FIX_ISAMAP_BEGIN))
278 return;
279 #endif
280 offset = virt_addr & ~PAGE_MASK;
281 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
283 idx = FIX_BTMAP_BEGIN;
284 while (nrpages > 0) {
285 clear_fixmap(idx);
286 --idx;
287 --nrpages;
288 }
289 }
291 #endif /* CONFIG_XEN_PHYSDEV_ACCESS */
293 /* These hacky macros avoid phys->machine translations. */
294 #define __direct_pte(x) ((pte_t) { (x) } )
295 #define __direct_mk_pte(page_nr,pgprot) \
296 __direct_pte(((page_nr) << PAGE_SHIFT) | pgprot_val(pgprot))
297 #define direct_mk_pte_phys(physpage, pgprot) \
298 __direct_mk_pte((physpage) >> PAGE_SHIFT, pgprot)
300 static inline void direct_remap_area_pte(pte_t *pte,
301 unsigned long address,
302 unsigned long size,
303 mmu_update_t **v)
304 {
305 unsigned long end;
307 address &= ~PMD_MASK;
308 end = address + size;
309 if (end > PMD_SIZE)
310 end = PMD_SIZE;
311 if (address >= end)
312 BUG();
314 do {
315 (*v)->ptr = virt_to_machine(pte);
316 (*v)++;
317 address += PAGE_SIZE;
318 pte++;
319 } while (address && (address < end));
320 }
322 static inline int direct_remap_area_pmd(struct mm_struct *mm,
323 pmd_t *pmd,
324 unsigned long address,
325 unsigned long size,
326 mmu_update_t **v)
327 {
328 unsigned long end;
330 address &= ~PGDIR_MASK;
331 end = address + size;
332 if (end > PGDIR_SIZE)
333 end = PGDIR_SIZE;
334 if (address >= end)
335 BUG();
336 do {
337 pte_t *pte = (mm == &init_mm) ?
338 pte_alloc_kernel(mm, pmd, address) :
339 pte_alloc_map(mm, pmd, address);
340 if (!pte)
341 return -ENOMEM;
342 direct_remap_area_pte(pte, address, end - address, v);
343 pte_unmap(pte);
344 address = (address + PMD_SIZE) & PMD_MASK;
345 pmd++;
346 } while (address && (address < end));
347 return 0;
348 }
350 int __direct_remap_area_pages(struct mm_struct *mm,
351 unsigned long address,
352 unsigned long size,
353 mmu_update_t *v)
354 {
355 pgd_t * dir;
356 unsigned long end = address + size;
358 dir = pgd_offset(mm, address);
359 if (address >= end)
360 BUG();
361 spin_lock(&mm->page_table_lock);
362 do {
363 pmd_t *pmd = pmd_alloc(mm, dir, address);
364 if (!pmd)
365 return -ENOMEM;
366 direct_remap_area_pmd(mm, pmd, address, end - address, &v);
367 address = (address + PGDIR_SIZE) & PGDIR_MASK;
368 dir++;
370 } while (address && (address < end));
371 spin_unlock(&mm->page_table_lock);
372 return 0;
373 }
376 int direct_remap_area_pages(struct mm_struct *mm,
377 unsigned long address,
378 unsigned long machine_addr,
379 unsigned long size,
380 pgprot_t prot,
381 domid_t domid)
382 {
383 int i;
384 unsigned long start_address;
385 #define MAX_DIRECTMAP_MMU_QUEUE 130
386 mmu_update_t u[MAX_DIRECTMAP_MMU_QUEUE], *w, *v;
388 v = w = &u[0];
389 if (domid != DOMID_LOCAL) {
390 u[0].ptr = MMU_EXTENDED_COMMAND;
391 u[0].val = MMUEXT_SET_FOREIGNDOM;
392 u[0].val |= (unsigned long)domid << 16;
393 v = w = &u[1];
394 }
396 start_address = address;
398 flush_cache_all();
400 for (i = 0; i < size; i += PAGE_SIZE) {
401 if ((v - u) == MAX_DIRECTMAP_MMU_QUEUE) {
402 /* Fill in the PTE pointers. */
403 __direct_remap_area_pages(mm,
404 start_address,
405 address-start_address,
406 w);
408 if (HYPERVISOR_mmu_update(u, v - u, NULL) < 0)
409 return -EFAULT;
410 v = w;
411 start_address = address;
412 }
414 /*
415 * Fill in the machine address: PTE ptr is done later by
416 * __direct_remap_area_pages().
417 */
418 v->val = (machine_addr & PAGE_MASK) | pgprot_val(prot);
420 machine_addr += PAGE_SIZE;
421 address += PAGE_SIZE;
422 v++;
423 }
425 if (v != w) {
426 /* get the ptep's filled in */
427 __direct_remap_area_pages(mm,
428 start_address,
429 address-start_address,
430 w);
431 if (unlikely(HYPERVISOR_mmu_update(u, v - u, NULL) < 0))
432 return -EFAULT;
433 }
435 flush_tlb_all();
437 return 0;
438 }
440 EXPORT_SYMBOL(direct_remap_area_pages);