ioremap.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/vmalloc.h>
  9. #include <linux/module.h>
  10. #include <asm/io.h>
  11. #include <asm/pgtable.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/tlbflush.h>
  14. #include <asm/addrspace.h>
  15. static inline int remap_area_pte(pte_t *pte, unsigned long address,
  16. unsigned long end, unsigned long phys_addr,
  17. pgprot_t prot)
  18. {
  19. unsigned long pfn;
  20. pfn = phys_addr >> PAGE_SHIFT;
  21. do {
  22. WARN_ON(!pte_none(*pte));
  23. set_pte(pte, pfn_pte(pfn, prot));
  24. address += PAGE_SIZE;
  25. pfn++;
  26. pte++;
  27. } while (address && (address < end));
  28. return 0;
  29. }
  30. static inline int remap_area_pmd(pmd_t *pmd, unsigned long address,
  31. unsigned long end, unsigned long phys_addr,
  32. pgprot_t prot)
  33. {
  34. unsigned long next;
  35. phys_addr -= address;
  36. do {
  37. pte_t *pte = pte_alloc_kernel(pmd, address);
  38. if (!pte)
  39. return -ENOMEM;
  40. next = (address + PMD_SIZE) & PMD_MASK;
  41. if (remap_area_pte(pte, address, next,
  42. address + phys_addr, prot))
  43. return -ENOMEM;
  44. address = next;
  45. pmd++;
  46. } while (address && (address < end));
  47. return 0;
  48. }
  49. static int remap_area_pud(pud_t *pud, unsigned long address,
  50. unsigned long end, unsigned long phys_addr,
  51. pgprot_t prot)
  52. {
  53. unsigned long next;
  54. phys_addr -= address;
  55. do {
  56. pmd_t *pmd = pmd_alloc(&init_mm, pud, address);
  57. if (!pmd)
  58. return -ENOMEM;
  59. next = (address + PUD_SIZE) & PUD_MASK;
  60. if (remap_area_pmd(pmd, address, next,
  61. phys_addr + address, prot))
  62. return -ENOMEM;
  63. address = next;
  64. pud++;
  65. } while (address && address < end);
  66. return 0;
  67. }
  68. static int remap_area_pages(unsigned long address, unsigned long phys_addr,
  69. size_t size, pgprot_t prot)
  70. {
  71. unsigned long end = address + size;
  72. unsigned long next;
  73. pgd_t *pgd;
  74. int err = 0;
  75. phys_addr -= address;
  76. pgd = pgd_offset_k(address);
  77. flush_cache_all();
  78. BUG_ON(address >= end);
  79. spin_lock(&init_mm.page_table_lock);
  80. do {
  81. pud_t *pud = pud_alloc(&init_mm, pgd, address);
  82. err = -ENOMEM;
  83. if (!pud)
  84. break;
  85. next = (address + PGDIR_SIZE) & PGDIR_MASK;
  86. if (next < address || next > end)
  87. next = end;
  88. err = remap_area_pud(pud, address, next,
  89. phys_addr + address, prot);
  90. if (err)
  91. break;
  92. address = next;
  93. pgd++;
  94. } while (address && (address < end));
  95. spin_unlock(&init_mm.page_table_lock);
  96. flush_tlb_all();
  97. return err;
  98. }
  99. /*
  100. * Re-map an arbitrary physical address space into the kernel virtual
  101. * address space. Needed when the kernel wants to access physical
  102. * memory directly.
  103. */
  104. void __iomem *__ioremap(unsigned long phys_addr, size_t size,
  105. unsigned long flags)
  106. {
  107. void *addr;
  108. struct vm_struct *area;
  109. unsigned long offset, last_addr;
  110. pgprot_t prot;
  111. /*
  112. * Check if we can simply use the P4 segment. This area is
  113. * uncacheable, so if caching/buffering is requested, we can't
  114. * use it.
  115. */
  116. if ((phys_addr >= P4SEG) && (flags == 0))
  117. return (void __iomem *)phys_addr;
  118. /* Don't allow wraparound or zero size */
  119. last_addr = phys_addr + size - 1;
  120. if (!size || last_addr < phys_addr)
  121. return NULL;
  122. /*
  123. * XXX: When mapping regular RAM, we'd better make damn sure
  124. * it's never used for anything else. But this is really the
  125. * caller's responsibility...
  126. */
  127. if (PHYSADDR(P2SEGADDR(phys_addr)) == phys_addr)
  128. return (void __iomem *)P2SEGADDR(phys_addr);
  129. /* Mappings have to be page-aligned */
  130. offset = phys_addr & ~PAGE_MASK;
  131. phys_addr &= PAGE_MASK;
  132. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  133. prot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY
  134. | _PAGE_ACCESSED | _PAGE_TYPE_SMALL | flags);
  135. /*
  136. * Ok, go for it..
  137. */
  138. area = get_vm_area(size, VM_IOREMAP);
  139. if (!area)
  140. return NULL;
  141. area->phys_addr = phys_addr;
  142. addr = area->addr;
  143. if (remap_area_pages((unsigned long)addr, phys_addr, size, prot)) {
  144. vunmap(addr);
  145. return NULL;
  146. }
  147. return (void __iomem *)(offset + (char *)addr);
  148. }
  149. EXPORT_SYMBOL(__ioremap);
  150. void __iounmap(void __iomem *addr)
  151. {
  152. struct vm_struct *p;
  153. if ((unsigned long)addr >= P4SEG)
  154. return;
  155. p = remove_vm_area((void *)(PAGE_MASK & (unsigned long __force)addr));
  156. if (unlikely(!p)) {
  157. printk (KERN_ERR "iounmap: bad address %p\n", addr);
  158. return;
  159. }
  160. kfree (p);
  161. }
  162. EXPORT_SYMBOL(__iounmap);