ia64/xen-unstable

view linux-2.6.10-rc2-xen-sparse/arch/xen/i386/mm/ioremap.c @ 3254:d455acded006

bitkeeper revision 1.1159.170.60 (41b5f6f8C9PiyGZaHTSWyeJCoW8V_g)

ioremap.c:
whitespace
author cl349@arcadians.cl.cam.ac.uk
date Tue Dec 07 18:31:20 2004 +0000 (2004-12-07)
parents a169836882cb
children
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;
83 /* Don't allow wraparound or zero size */
84 last_addr = phys_addr + size - 1;
85 if (!size || last_addr < phys_addr)
86 return NULL;
88 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
89 /*
90 * Don't remap the low PCI/ISA area, it's always mapped..
91 */
92 if (phys_addr >= 0x0 && last_addr < 0x100000)
93 return isa_bus_to_virt(phys_addr);
94 #endif
96 /*
97 * Don't allow anybody to remap normal RAM that we're using..
98 */
99 if (is_local_lowmem(phys_addr)) {
100 char *t_addr, *t_end;
101 struct page *page;
103 t_addr = bus_to_virt(phys_addr);
104 t_end = t_addr + (size - 1);
106 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
107 if(!PageReserved(page))
108 return NULL;
109 }
111 /*
112 * Mappings have to be page-aligned
113 */
114 offset = phys_addr & ~PAGE_MASK;
115 phys_addr &= PAGE_MASK;
116 size = PAGE_ALIGN(last_addr+1) - phys_addr;
118 /*
119 * Ok, go for it..
120 */
121 area = get_vm_area(size, VM_IOREMAP);
122 if (!area)
123 return NULL;
124 area->phys_addr = phys_addr;
125 addr = (void __iomem *) area->addr;
126 if (direct_remap_area_pages(&init_mm, (unsigned long) addr, phys_addr,
127 size, __pgprot(_PAGE_PRESENT | _PAGE_RW |
128 _PAGE_DIRTY | _PAGE_ACCESSED
129 | flags), DOMID_IO)) {
130 vunmap((void __force *) addr);
131 return NULL;
132 }
133 return (void __iomem *) (offset + (char __iomem *)addr);
134 }
137 /**
138 * ioremap_nocache - map bus memory into CPU space
139 * @offset: bus address of the memory
140 * @size: size of the resource to map
141 *
142 * ioremap_nocache performs a platform specific sequence of operations to
143 * make bus memory CPU accessible via the readb/readw/readl/writeb/
144 * writew/writel functions and the other mmio helpers. The returned
145 * address is not guaranteed to be usable directly as a virtual
146 * address.
147 *
148 * This version of ioremap ensures that the memory is marked uncachable
149 * on the CPU as well as honouring existing caching rules from things like
150 * the PCI bus. Note that there are other caches and buffers on many
151 * busses. In particular driver authors should read up on PCI writes
152 *
153 * It's useful if some control registers are in such an area and
154 * write combining or read caching is not desirable:
155 *
156 * Must be freed with iounmap.
157 */
159 void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
160 {
161 unsigned long last_addr;
162 void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD);
163 if (!p)
164 return p;
166 /* Guaranteed to be > phys_addr, as per __ioremap() */
167 last_addr = phys_addr + size - 1;
169 if (is_local_lowmem(last_addr)) {
170 struct page *ppage = virt_to_page(bus_to_virt(phys_addr));
171 unsigned long npages;
173 phys_addr &= PAGE_MASK;
175 /* This might overflow and become zero.. */
176 last_addr = PAGE_ALIGN(last_addr);
178 /* .. but that's ok, because modulo-2**n arithmetic will make
179 * the page-aligned "last - first" come out right.
180 */
181 npages = (last_addr - phys_addr) >> PAGE_SHIFT;
183 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
184 iounmap(p);
185 p = NULL;
186 }
187 global_flush_tlb();
188 }
190 return p;
191 }
193 void iounmap(volatile void __iomem *addr)
194 {
195 struct vm_struct *p;
196 if ((void __force *) addr <= high_memory)
197 return;
198 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
199 if ((unsigned long) addr >= fix_to_virt(FIX_ISAMAP_BEGIN))
200 return;
201 #endif
202 p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
203 if (!p) {
204 printk("__iounmap: bad address %p\n", addr);
205 return;
206 }
208 if (p->flags && is_local_lowmem(p->phys_addr)) {
209 change_page_attr(virt_to_page(bus_to_virt(p->phys_addr)),
210 p->size >> PAGE_SHIFT,
211 PAGE_KERNEL);
212 global_flush_tlb();
213 }
214 kfree(p);
215 }
217 void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
218 {
219 unsigned long offset, last_addr;
220 unsigned int nrpages;
221 enum fixed_addresses idx;
223 /* Don't allow wraparound or zero size */
224 last_addr = phys_addr + size - 1;
225 if (!size || last_addr < phys_addr)
226 return NULL;
228 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
229 /*
230 * Don't remap the low PCI/ISA area, it's always mapped..
231 */
232 if (phys_addr >= 0x0 && last_addr < 0x100000)
233 return isa_bus_to_virt(phys_addr);
234 #endif
236 /*
237 * Mappings have to be page-aligned
238 */
239 offset = phys_addr & ~PAGE_MASK;
240 phys_addr &= PAGE_MASK;
241 size = PAGE_ALIGN(last_addr) - phys_addr;
243 /*
244 * Mappings have to fit in the FIX_BTMAP area.
245 */
246 nrpages = size >> PAGE_SHIFT;
247 if (nrpages > NR_FIX_BTMAPS)
248 return NULL;
250 /*
251 * Ok, go for it..
252 */
253 idx = FIX_BTMAP_BEGIN;
254 while (nrpages > 0) {
255 set_fixmap_ma(idx, phys_addr);
256 phys_addr += PAGE_SIZE;
257 --idx;
258 --nrpages;
259 }
260 return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
261 }
263 void __init bt_iounmap(void *addr, unsigned long size)
264 {
265 unsigned long virt_addr;
266 unsigned long offset;
267 unsigned int nrpages;
268 enum fixed_addresses idx;
270 virt_addr = (unsigned long)addr;
271 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
272 return;
273 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
274 if (virt_addr >= fix_to_virt(FIX_ISAMAP_BEGIN))
275 return;
276 #endif
277 offset = virt_addr & ~PAGE_MASK;
278 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
280 idx = FIX_BTMAP_BEGIN;
281 while (nrpages > 0) {
282 clear_fixmap(idx);
283 --idx;
284 --nrpages;
285 }
286 }
288 #endif /* CONFIG_XEN_PHYSDEV_ACCESS */
290 /* These hacky macros avoid phys->machine translations. */
291 #define __direct_pte(x) ((pte_t) { (x) } )
292 #define __direct_mk_pte(page_nr,pgprot) \
293 __direct_pte(((page_nr) << PAGE_SHIFT) | pgprot_val(pgprot))
294 #define direct_mk_pte_phys(physpage, pgprot) \
295 __direct_mk_pte((physpage) >> PAGE_SHIFT, pgprot)
297 static inline void direct_remap_area_pte(pte_t *pte,
298 unsigned long address,
299 unsigned long size,
300 mmu_update_t **v)
301 {
302 unsigned long end;
304 address &= ~PMD_MASK;
305 end = address + size;
306 if (end > PMD_SIZE)
307 end = PMD_SIZE;
308 if (address >= end)
309 BUG();
311 do {
312 (*v)->ptr = virt_to_machine(pte);
313 (*v)++;
314 address += PAGE_SIZE;
315 pte++;
316 } while (address && (address < end));
317 }
319 static inline int direct_remap_area_pmd(struct mm_struct *mm,
320 pmd_t *pmd,
321 unsigned long address,
322 unsigned long size,
323 mmu_update_t **v)
324 {
325 unsigned long end;
327 address &= ~PGDIR_MASK;
328 end = address + size;
329 if (end > PGDIR_SIZE)
330 end = PGDIR_SIZE;
331 if (address >= end)
332 BUG();
333 do {
334 pte_t *pte = (mm == &init_mm) ?
335 pte_alloc_kernel(mm, pmd, address) :
336 pte_alloc_map(mm, pmd, address);
337 if (!pte)
338 return -ENOMEM;
339 direct_remap_area_pte(pte, address, end - address, v);
340 pte_unmap(pte);
341 address = (address + PMD_SIZE) & PMD_MASK;
342 pmd++;
343 } while (address && (address < end));
344 return 0;
345 }
347 int __direct_remap_area_pages(struct mm_struct *mm,
348 unsigned long address,
349 unsigned long size,
350 mmu_update_t *v)
351 {
352 pgd_t * dir;
353 unsigned long end = address + size;
355 dir = pgd_offset(mm, address);
356 if (address >= end)
357 BUG();
358 spin_lock(&mm->page_table_lock);
359 do {
360 pmd_t *pmd = pmd_alloc(mm, dir, address);
361 if (!pmd)
362 return -ENOMEM;
363 direct_remap_area_pmd(mm, pmd, address, end - address, &v);
364 address = (address + PGDIR_SIZE) & PGDIR_MASK;
365 dir++;
367 } while (address && (address < end));
368 spin_unlock(&mm->page_table_lock);
369 return 0;
370 }
373 int direct_remap_area_pages(struct mm_struct *mm,
374 unsigned long address,
375 unsigned long machine_addr,
376 unsigned long size,
377 pgprot_t prot,
378 domid_t domid)
379 {
380 int i;
381 unsigned long start_address;
382 #define MAX_DIRECTMAP_MMU_QUEUE 130
383 mmu_update_t u[MAX_DIRECTMAP_MMU_QUEUE], *w, *v;
385 u[0].ptr = MMU_EXTENDED_COMMAND;
386 u[0].val = MMUEXT_SET_FOREIGNDOM;
387 u[0].val |= (unsigned long)domid << 16;
388 v = w = &u[1];
390 start_address = address;
392 flush_cache_all();
394 for (i = 0; i < size; i += PAGE_SIZE) {
395 if ((v - u) == MAX_DIRECTMAP_MMU_QUEUE) {
396 /* Fill in the PTE pointers. */
397 __direct_remap_area_pages(mm,
398 start_address,
399 address-start_address,
400 w);
402 if (HYPERVISOR_mmu_update(u, v - u, NULL) < 0)
403 return -EFAULT;
404 v = w;
405 start_address = address;
406 }
408 /*
409 * Fill in the machine address: PTE ptr is done later by
410 * __direct_remap_area_pages().
411 */
412 v->val = (machine_addr & PAGE_MASK) | pgprot_val(prot);
414 machine_addr += PAGE_SIZE;
415 address += PAGE_SIZE;
416 v++;
417 }
419 if (v != w) {
420 /* get the ptep's filled in */
421 __direct_remap_area_pages(mm,
422 start_address,
423 address-start_address,
424 w);
425 if (unlikely(HYPERVISOR_mmu_update(u, v - u, NULL) < 0))
426 return -EFAULT;
427 }
429 flush_tlb_all();
431 return 0;
432 }
434 EXPORT_SYMBOL(direct_remap_area_pages);