aboutsummaryrefslogtreecommitdiffstats
path: root/extras/mini-os/arch/x86/ioremap.c
blob: c7f818609c25680c172697f3a3a5b0db06a8b7c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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 <mini-os/types.h>
#include <mini-os/lib.h>
#include <mini-os/xmalloc.h>
#include <mini-os/mm.h>
#include <mini-os/ioremap.h>

/* 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, NULL, 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 -*- */