From 6976b9a7c20dce349dccfb0eec658af5fa9af3c5 Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Mon, 2 Mar 2009 10:50:59 +0000 Subject: minios: add ioremap/iounmap Add ioremap and iounmap functions to minios. Also move some unmapping code from and clean up mem_test. Signed-off-by: Rolf Neugebauer Acked-by: Samuel Thibault --- extras/mini-os/arch/ia64/mm.c | 6 ++ extras/mini-os/arch/x86/ioremap.c | 88 +++++++++++++++++++++++++++ extras/mini-os/arch/x86/mm.c | 121 ++++++++++++++++++++++++++++++++++---- 3 files changed, 204 insertions(+), 11 deletions(-) create mode 100644 extras/mini-os/arch/x86/ioremap.c (limited to 'extras/mini-os/arch') diff --git a/extras/mini-os/arch/ia64/mm.c b/extras/mini-os/arch/ia64/mm.c index 189b39addc..802c29f0b5 100644 --- a/extras/mini-os/arch/ia64/mm.c +++ b/extras/mini-os/arch/ia64/mm.c @@ -156,6 +156,12 @@ map_frames_ex(unsigned long* frames, unsigned long n, unsigned long stride, return (void*) __va(frames[0] << PAGE_SHIFT); } +int unmap_frames(unsigned long virt_addr, unsigned long num_frames) +{ + /* TODO */ + ASSERT(0); +} + void arch_init_p2m(unsigned long max_pfn) { printk("Warn: p2m map not implemented.\n"); diff --git a/extras/mini-os/arch/x86/ioremap.c b/extras/mini-os/arch/x86/ioremap.c new file mode 100644 index 0000000000..d94f4e72a7 --- /dev/null +++ b/extras/mini-os/arch/x86/ioremap.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2009, Netronome Systems, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + + +#include +#include +#include +#include +#include + +/* Map a physical address range into virtual address space with provided + * flags. Return a virtual address range it is mapped to. */ +static void *__do_ioremap(unsigned long phys_addr, unsigned long size, + unsigned long prot) +{ + unsigned long va; + unsigned long mfns, mfn; + unsigned long num_pages, offset; + int i; + + /* allow non page aligned addresses but for mapping we need to align them */ + offset = (phys_addr & ~PAGE_MASK); + num_pages = (offset + size + PAGE_SIZE - 1) / PAGE_SIZE; + phys_addr &= PAGE_MASK; + mfns = mfn = phys_addr >> PAGE_SHIFT; + + /* sanity checks on list of MFNs */ + for ( i = 0; i < num_pages; i++, mfn++ ) + { + if ( mfn_is_ram(mfn) ) + { + printk("ioremap: mfn 0x%ulx is RAM\n", mfn); + goto mfn_invalid; + } + } + va = (unsigned long)map_frames_ex(&mfns, num_pages, 0, 1, 1, + DOMID_IO, 0, prot); + return (void *)(va + offset); + +mfn_invalid: + return NULL; +} + +void *ioremap(unsigned long phys_addr, unsigned long size) +{ + return __do_ioremap(phys_addr, size, IO_PROT); +} + +void *ioremap_nocache(unsigned long phys_addr, unsigned long size) +{ + return __do_ioremap(phys_addr, size, IO_PROT_NOCACHE); +} + +/* Un-map the io-remapped region. Currently no list of existing mappings is + * maintained, so the caller has to supply the size */ +void iounmap(void *virt_addr, unsigned long size) +{ + unsigned long num_pages; + unsigned long va = (unsigned long)virt_addr; + + /* work out number of frames to unmap */ + num_pages = ((va & ~PAGE_MASK) + size + PAGE_SIZE - 1) / PAGE_SIZE; + + unmap_frames(va & PAGE_MASK, num_pages); +} + + + +/* -*- Mode:C; c-basic-offset:4; tab-width:4 indent-tabs-mode:nil -*- */ diff --git a/extras/mini-os/arch/x86/mm.c b/extras/mini-os/arch/x86/mm.c index a0e2ae68bc..696c4662b6 100644 --- a/extras/mini-os/arch/x86/mm.c +++ b/extras/mini-os/arch/x86/mm.c @@ -316,32 +316,61 @@ static void set_readonly(void *text, void *etext) } } -void mem_test(unsigned long *start_add, unsigned long *end_add) +/* + * A useful mem testing function. Write the address to every address in the + * range provided and read back the value. If verbose, print page walk to + * some VA + * + * If we get MEM_TEST_MAX_ERRORS we might as well stop + */ +#define MEM_TEST_MAX_ERRORS 10 +int mem_test(unsigned long *start_va, unsigned long *end_va, int verbose) { unsigned long mask = 0x10000; unsigned long *pointer; - - for(pointer = start_add; pointer < end_add; pointer++) + int error_count = 0; + + /* write values and print page walks */ + if ( verbose && (((unsigned long)start_va) & 0xfffff) ) + { + printk("MemTest Start: 0x%lx\n", start_va); + page_walk((unsigned long)start_va); + } + for ( pointer = start_va; pointer < end_va; pointer++ ) { - if(!(((unsigned long)pointer) & 0xfffff)) + if ( verbose && !(((unsigned long)pointer) & 0xfffff) ) { printk("Writing to %lx\n", pointer); page_walk((unsigned long)pointer); } *pointer = (unsigned long)pointer & ~mask; } - - for(pointer = start_add; pointer < end_add; pointer++) + if ( verbose && (((unsigned long)end_va) & 0xfffff) ) { - if(((unsigned long)pointer & ~mask) != *pointer) + printk("MemTest End: %lx\n", end_va-1); + page_walk((unsigned long)end_va-1); + } + + /* verify values */ + for ( pointer = start_va; pointer < end_va; pointer++ ) + { + if ( ((unsigned long)pointer & ~mask) != *pointer ) + { printk("Read error at 0x%lx. Read: 0x%lx, should read 0x%lx\n", - (unsigned long)pointer, - *pointer, - ((unsigned long)pointer & ~mask)); + (unsigned long)pointer, *pointer, + ((unsigned long)pointer & ~mask)); + error_count++; + if ( error_count >= MEM_TEST_MAX_ERRORS ) + { + printk("mem_test: too many errors\n"); + return -1; + } + } } - + return 0; } + static pgentry_t *get_pgt(unsigned long addr) { unsigned long mfn; @@ -537,6 +566,72 @@ void *map_frames_ex(unsigned long *f, unsigned long n, unsigned long stride, return (void *)addr; } +/* + * Unmap nun_frames frames mapped at virtual address va. + */ +#define UNMAP_BATCH ((STACK_SIZE / 2) / sizeof(multicall_entry_t)) +int unmap_frames(unsigned long va, unsigned long num_frames) +{ + int n = UNMAP_BATCH; + multicall_entry_t call[n]; + int ret; + int i; + + ASSERT(!((unsigned long)va & ~PAGE_MASK)); + + DEBUG("va=%p, num=0x%lx\n", va, num_frames); + + while ( num_frames ) { + if ( n > num_frames ) + n = num_frames; + + for ( i = 0; i < n; i++ ) + { + int arg = 0; + /* simply update the PTE for the VA and invalidate TLB */ + call[i].op = __HYPERVISOR_update_va_mapping; + call[i].args[arg++] = va; + call[i].args[arg++] = 0; +#ifdef __i386__ + call[i].args[arg++] = 0; +#endif + call[i].args[arg++] = UVMF_INVLPG; + + va += PAGE_SIZE; + } + + ret = HYPERVISOR_multicall(call, n); + if ( ret ) + { + printk("update_va_mapping hypercall failed with rc=%d.\n", ret); + return -ret; + } + + for ( i = 0; i < n; i++ ) + { + if ( call[i].result ) + { + printk("update_va_mapping failed for with rc=%d.\n", ret); + return -(call[i].result); + } + } + num_frames -= n; + } + return 0; +} + +/* + * Check if a given MFN refers to real memory + */ +static long system_ram_end_mfn; +int mfn_is_ram(unsigned long mfn) +{ + /* very crude check if a given MFN is memory or not. Probably should + * make this a little more sophisticated ;) */ + return (mfn <= system_ram_end_mfn) ? 1 : 0; +} + + static void clear_bootstrap(void) { pte_t nullpte = { }; @@ -625,6 +720,10 @@ void arch_init_mm(unsigned long* start_pfn_p, unsigned long* max_pfn_p) clear_bootstrap(); set_readonly(&_text, &_erodata); + /* get the number of physical pages the system has. Used to check for + * system memory. */ + system_ram_end_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL); + *start_pfn_p = start_pfn; *max_pfn_p = max_pfn; } -- cgit v1.2.3