linux-bk/arch/i386/mm/ioremap.c
<<
>>
Prefs
   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 */
  10
  11#include <linux/vmalloc.h>
  12#include <linux/init.h>
  13#include <linux/slab.h>
  14#include <asm/io.h>
  15#include <asm/pgalloc.h>
  16#include <asm/fixmap.h>
  17#include <asm/cacheflush.h>
  18#include <asm/tlbflush.h>
  19#include <asm/pgtable.h>
  20
  21static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
  22        unsigned long phys_addr, unsigned long flags)
  23{
  24        unsigned long end;
  25        unsigned long pfn;
  26
  27        address &= ~PMD_MASK;
  28        end = address + size;
  29        if (end > PMD_SIZE)
  30                end = PMD_SIZE;
  31        if (address >= end)
  32                BUG();
  33        pfn = phys_addr >> PAGE_SHIFT;
  34        do {
  35                if (!pte_none(*pte)) {
  36                        printk("remap_area_pte: page already exists\n");
  37                        BUG();
  38                }
  39                set_pte(pte, pfn_pte(pfn, __pgprot(_PAGE_PRESENT | _PAGE_RW | 
  40                                        _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
  41                address += PAGE_SIZE;
  42                pfn++;
  43                pte++;
  44        } while (address && (address < end));
  45}
  46
  47static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
  48        unsigned long phys_addr, unsigned long flags)
  49{
  50        unsigned long end;
  51
  52        address &= ~PGDIR_MASK;
  53        end = address + size;
  54        if (end > PGDIR_SIZE)
  55                end = PGDIR_SIZE;
  56        phys_addr -= address;
  57        if (address >= end)
  58                BUG();
  59        do {
  60                pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
  61                if (!pte)
  62                        return -ENOMEM;
  63                remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  64                address = (address + PMD_SIZE) & PMD_MASK;
  65                pmd++;
  66        } while (address && (address < end));
  67        return 0;
  68}
  69
  70static int remap_area_pages(unsigned long address, unsigned long phys_addr,
  71                                 unsigned long size, unsigned long flags)
  72{
  73        int error;
  74        pgd_t * dir;
  75        unsigned long end = address + size;
  76
  77        phys_addr -= address;
  78        dir = pgd_offset(&init_mm, address);
  79        flush_cache_all();
  80        if (address >= end)
  81                BUG();
  82        spin_lock(&init_mm.page_table_lock);
  83        do {
  84                pmd_t *pmd;
  85                pmd = pmd_alloc(&init_mm, dir, address);
  86                error = -ENOMEM;
  87                if (!pmd)
  88                        break;
  89                if (remap_area_pmd(pmd, address, end - address,
  90                                         phys_addr + address, flags))
  91                        break;
  92                error = 0;
  93                address = (address + PGDIR_SIZE) & PGDIR_MASK;
  94                dir++;
  95        } while (address && (address < end));
  96        spin_unlock(&init_mm.page_table_lock);
  97        flush_tlb_all();
  98        return error;
  99}
 100
 101/*
 102 * Generic mapping function (not visible outside):
 103 */
 104
 105/*
 106 * Remap an arbitrary physical address space into the kernel virtual
 107 * address space. Needed when the kernel wants to access high addresses
 108 * directly.
 109 *
 110 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
 111 * have to convert them into an offset in a page-aligned mapping, but the
 112 * caller shouldn't need to know that small detail.
 113 */
 114void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
 115{
 116        void * addr;
 117        struct vm_struct * area;
 118        unsigned long offset, last_addr;
 119
 120        /* Don't allow wraparound or zero size */
 121        last_addr = phys_addr + size - 1;
 122        if (!size || last_addr < phys_addr)
 123                return NULL;
 124
 125        /*
 126         * Don't remap the low PCI/ISA area, it's always mapped..
 127         */
 128        if (phys_addr >= 0xA0000 && last_addr < 0x100000)
 129                return phys_to_virt(phys_addr);
 130
 131        /*
 132         * Don't allow anybody to remap normal RAM that we're using..
 133         */
 134        if (phys_addr < virt_to_phys(high_memory)) {
 135                char *t_addr, *t_end;
 136                struct page *page;
 137
 138                t_addr = __va(phys_addr);
 139                t_end = t_addr + (size - 1);
 140           
 141                for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
 142                        if(!PageReserved(page))
 143                                return NULL;
 144        }
 145
 146        /*
 147         * Mappings have to be page-aligned
 148         */
 149        offset = phys_addr & ~PAGE_MASK;
 150        phys_addr &= PAGE_MASK;
 151        size = PAGE_ALIGN(last_addr) - phys_addr;
 152
 153        /*
 154         * Ok, go for it..
 155         */
 156        area = get_vm_area(size, VM_IOREMAP);
 157        if (!area)
 158                return NULL;
 159        area->phys_addr = phys_addr;
 160        addr = area->addr;
 161        if (remap_area_pages(VMALLOC_VMADDR(addr), phys_addr, size, flags)) {
 162                vfree(addr);
 163                return NULL;
 164        }
 165        return (void *) (offset + (char *)addr);
 166}
 167
 168
 169/**
 170 * ioremap_nocache     -   map bus memory into CPU space
 171 * @offset:    bus address of the memory
 172 * @size:      size of the resource to map
 173 *
 174 * ioremap_nocache performs a platform specific sequence of operations to
 175 * make bus memory CPU accessible via the readb/readw/readl/writeb/
 176 * writew/writel functions and the other mmio helpers. The returned
 177 * address is not guaranteed to be usable directly as a virtual
 178 * address. 
 179 *
 180 * This version of ioremap ensures that the memory is marked uncachable
 181 * on the CPU as well as honouring existing caching rules from things like
 182 * the PCI bus. Note that there are other caches and buffers on many 
 183 * busses. In particular driver authors should read up on PCI writes
 184 *
 185 * It's useful if some control registers are in such an area and
 186 * write combining or read caching is not desirable:
 187 * 
 188 * Must be freed with iounmap.
 189 */
 190
 191void *ioremap_nocache (unsigned long phys_addr, unsigned long size)
 192{
 193        void *p = __ioremap(phys_addr, size, _PAGE_PCD);
 194        if (!p) 
 195                return p; 
 196
 197        if (phys_addr + size < virt_to_phys(high_memory)) { 
 198                struct page *ppage = virt_to_page(__va(phys_addr));             
 199                unsigned long npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
 200
 201                BUG_ON(phys_addr+size > (unsigned long)high_memory);
 202                BUG_ON(phys_addr + size < phys_addr);
 203
 204                if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) { 
 205                        iounmap(p); 
 206                        p = NULL;
 207                }
 208        } 
 209
 210        return p;                                       
 211}
 212
 213void iounmap(void *addr)
 214{
 215        struct vm_struct *p;
 216        if (addr <= high_memory) 
 217                return; 
 218        p = remove_kernel_area((void *) (PAGE_MASK & (unsigned long) addr));
 219        if (!p) { 
 220                printk("__iounmap: bad address %p\n", addr);
 221                return;
 222        } 
 223
 224        vmfree_area_pages(VMALLOC_VMADDR(p->addr), p->size);    
 225        if (p->flags && p->phys_addr < virt_to_phys(high_memory)) { 
 226                change_page_attr(virt_to_page(__va(p->phys_addr)),
 227                                 p->size >> PAGE_SHIFT,
 228                                 PAGE_KERNEL);                           
 229        } 
 230        kfree(p); 
 231}
 232
 233void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
 234{
 235        unsigned long offset, last_addr;
 236        unsigned int nrpages;
 237        enum fixed_addresses idx;
 238
 239        /* Don't allow wraparound or zero size */
 240        last_addr = phys_addr + size - 1;
 241        if (!size || last_addr < phys_addr)
 242                return NULL;
 243
 244        /*
 245         * Don't remap the low PCI/ISA area, it's always mapped..
 246         */
 247        if (phys_addr >= 0xA0000 && last_addr < 0x100000)
 248                return phys_to_virt(phys_addr);
 249
 250        /*
 251         * Mappings have to be page-aligned
 252         */
 253        offset = phys_addr & ~PAGE_MASK;
 254        phys_addr &= PAGE_MASK;
 255        size = PAGE_ALIGN(last_addr) - phys_addr;
 256
 257        /*
 258         * Mappings have to fit in the FIX_BTMAP area.
 259         */
 260        nrpages = size >> PAGE_SHIFT;
 261        if (nrpages > NR_FIX_BTMAPS)
 262                return NULL;
 263
 264        /*
 265         * Ok, go for it..
 266         */
 267        idx = FIX_BTMAP_BEGIN;
 268        while (nrpages > 0) {
 269                set_fixmap(idx, phys_addr);
 270                phys_addr += PAGE_SIZE;
 271                --idx;
 272                --nrpages;
 273        }
 274        return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
 275}
 276
 277void __init bt_iounmap(void *addr, unsigned long size)
 278{
 279        unsigned long virt_addr;
 280        unsigned long offset;
 281        unsigned int nrpages;
 282        enum fixed_addresses idx;
 283
 284        virt_addr = (unsigned long)addr;
 285        if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
 286                return;
 287        offset = virt_addr & ~PAGE_MASK;
 288        nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
 289
 290        idx = FIX_BTMAP_BEGIN;
 291        while (nrpages > 0) {
 292                __set_fixmap(idx, 0, __pgprot(0));
 293                --idx;
 294                --nrpages;
 295        }
 296}
 297
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.