1/* 2 * Copyright (C) 2009, Netronome Systems, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to 6 * deal in the Software without restriction, including without limitation the 7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 * sell copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 * DEALINGS IN THE SOFTWARE. 21 */ 22 23 24#include <mini-os/types.h> 25#include <mini-os/lib.h> 26#include <mini-os/mm.h> 27#include <mini-os/ioremap.h> 28 29/* Map a physical address range into virtual address space with provided 30 * flags. Return a virtual address range it is mapped to. */ 31static void *__do_ioremap(unsigned long phys_addr, unsigned long size, 32 unsigned long prot) 33{ 34 unsigned long va; 35 unsigned long mfns, mfn; 36 unsigned long num_pages, offset; 37 int i; 38 39 /* allow non page aligned addresses but for mapping we need to align them */ 40 offset = (phys_addr & ~PAGE_MASK); 41 num_pages = (offset + size + PAGE_SIZE - 1) / PAGE_SIZE; 42 phys_addr &= PAGE_MASK; 43 mfns = mfn = phys_addr >> PAGE_SHIFT; 44 45 /* sanity checks on list of MFNs */ 46 for ( i = 0; i < num_pages; i++, mfn++ ) 47 { 48 if ( mfn_is_ram(mfn) ) 49 { 50 minios_printk("ioremap: mfn 0x%ulx is RAM\n", mfn); 51 goto mfn_invalid; 52 } 53 } 54 va = (unsigned long)minios_map_frames_ex(&mfns, num_pages, 0, 1, 1, 55 DOMID_IO, NULL, prot); 56 return (void *)(va + offset); 57 58mfn_invalid: 59 return NULL; 60} 61 62void *minios_ioremap(unsigned long phys_addr, unsigned long size) 63{ 64 return __do_ioremap(phys_addr, size, IO_PROT); 65} 66 67void *minios_ioremap_nocache(unsigned long phys_addr, unsigned long size) 68{ 69 return __do_ioremap(phys_addr, size, IO_PROT_NOCACHE); 70} 71 72/* Un-map the io-remapped region. Currently no list of existing mappings is 73 * maintained, so the caller has to supply the size */ 74void minios_iounmap(void *virt_addr, unsigned long size) 75{ 76 unsigned long num_pages; 77 unsigned long va = (unsigned long)virt_addr; 78 79 /* work out number of frames to unmap */ 80 num_pages = ((va & ~PAGE_MASK) + size + PAGE_SIZE - 1) / PAGE_SIZE; 81 82 unmap_frames(va & PAGE_MASK, num_pages); 83} 84 85 86 87/* -*- Mode:C; c-basic-offset:4; tab-width:4 indent-tabs-mode:nil -*- */ 88