ioremap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * linux/arch/m32r/mm/ioremap.c
  3. *
  4. * Copyright (c) 2001, 2002 Hiroyuki Kondo
  5. *
  6. * Taken from mips version.
  7. * (C) Copyright 1995 1996 Linus Torvalds
  8. * (C) Copyright 2001 Ralf Baechle
  9. */
  10. /*
  11. * This file is subject to the terms and conditions of the GNU General Public
  12. * License. See the file "COPYING" in the main directory of this archive
  13. * for more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <asm/addrspace.h>
  18. #include <asm/byteorder.h>
  19. #include <linux/vmalloc.h>
  20. #include <asm/io.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/tlbflush.h>
  24. static inline void
  25. remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
  26. unsigned long phys_addr, unsigned long flags)
  27. {
  28. unsigned long end;
  29. unsigned long pfn;
  30. pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | _PAGE_READ
  31. | _PAGE_WRITE | flags);
  32. address &= ~PMD_MASK;
  33. end = address + size;
  34. if (end > PMD_SIZE)
  35. end = PMD_SIZE;
  36. if (address >= end)
  37. BUG();
  38. pfn = phys_addr >> PAGE_SHIFT;
  39. do {
  40. if (!pte_none(*pte)) {
  41. printk("remap_area_pte: page already exists\n");
  42. BUG();
  43. }
  44. set_pte(pte, pfn_pte(pfn, pgprot));
  45. address += PAGE_SIZE;
  46. pfn++;
  47. pte++;
  48. } while (address && (address < end));
  49. }
  50. static inline int
  51. remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
  52. unsigned long phys_addr, unsigned long flags)
  53. {
  54. unsigned long end;
  55. address &= ~PGDIR_MASK;
  56. end = address + size;
  57. if (end > PGDIR_SIZE)
  58. end = PGDIR_SIZE;
  59. phys_addr -= address;
  60. if (address >= end)
  61. BUG();
  62. do {
  63. pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
  64. if (!pte)
  65. return -ENOMEM;
  66. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  67. address = (address + PMD_SIZE) & PMD_MASK;
  68. pmd++;
  69. } while (address && (address < end));
  70. return 0;
  71. }
  72. static int
  73. remap_area_pages(unsigned long address, unsigned long phys_addr,
  74. unsigned long size, unsigned long flags)
  75. {
  76. int error;
  77. pgd_t * dir;
  78. unsigned long end = address + size;
  79. phys_addr -= address;
  80. dir = pgd_offset(&init_mm, address);
  81. flush_cache_all();
  82. if (address >= end)
  83. BUG();
  84. spin_lock(&init_mm.page_table_lock);
  85. do {
  86. pmd_t *pmd;
  87. pmd = pmd_alloc(&init_mm, dir, address);
  88. error = -ENOMEM;
  89. if (!pmd)
  90. break;
  91. if (remap_area_pmd(pmd, address, end - address,
  92. phys_addr + address, flags))
  93. break;
  94. error = 0;
  95. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  96. dir++;
  97. } while (address && (address < end));
  98. spin_unlock(&init_mm.page_table_lock);
  99. flush_tlb_all();
  100. return error;
  101. }
  102. /*
  103. * Generic mapping function (not visible outside):
  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. */
  114. #define IS_LOW512(addr) (!((unsigned long)(addr) & ~0x1fffffffUL))
  115. void __iomem *
  116. __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  117. {
  118. void __iomem * addr;
  119. struct vm_struct * area;
  120. unsigned long offset, last_addr;
  121. /* Don't allow wraparound or zero size */
  122. last_addr = phys_addr + size - 1;
  123. if (!size || last_addr < phys_addr)
  124. return NULL;
  125. /*
  126. * Map objects in the low 512mb of address space using KSEG1, otherwise
  127. * map using page tables.
  128. */
  129. if (IS_LOW512(phys_addr) && IS_LOW512(phys_addr + size - 1))
  130. return (void *) KSEG1ADDR(phys_addr);
  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. t_addr = __va(phys_addr);
  138. t_end = t_addr + (size - 1);
  139. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  140. if(!PageReserved(page))
  141. return NULL;
  142. }
  143. /*
  144. * Mappings have to be page-aligned
  145. */
  146. offset = phys_addr & ~PAGE_MASK;
  147. phys_addr &= PAGE_MASK;
  148. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  149. /*
  150. * Ok, go for it..
  151. */
  152. area = get_vm_area(size, VM_IOREMAP);
  153. if (!area)
  154. return NULL;
  155. area->phys_addr = phys_addr;
  156. addr = (void __iomem *) area->addr;
  157. if (remap_area_pages((unsigned long)addr, phys_addr, size, flags)) {
  158. vunmap((void __force *) addr);
  159. return NULL;
  160. }
  161. return (void __iomem *) (offset + (char __iomem *)addr);
  162. }
  163. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == KSEG1)
  164. void iounmap(volatile void __iomem *addr)
  165. {
  166. if (!IS_KSEG1(addr))
  167. vfree((void *) (PAGE_MASK & (unsigned long) addr));
  168. }