ioremap.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(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. do {
  85. pmd_t *pmd;
  86. pmd = pmd_alloc(&init_mm, dir, address);
  87. error = -ENOMEM;
  88. if (!pmd)
  89. break;
  90. if (remap_area_pmd(pmd, address, end - address,
  91. phys_addr + address, flags))
  92. break;
  93. error = 0;
  94. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  95. dir++;
  96. } while (address && (address < end));
  97. flush_tlb_all();
  98. return error;
  99. }
  100. /*
  101. * Generic mapping function (not visible outside):
  102. */
  103. /*
  104. * Remap an arbitrary physical address space into the kernel virtual
  105. * address space. Needed when the kernel wants to access high addresses
  106. * directly.
  107. *
  108. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  109. * have to convert them into an offset in a page-aligned mapping, but the
  110. * caller shouldn't need to know that small detail.
  111. */
  112. #define IS_LOW512(addr) (!((unsigned long)(addr) & ~0x1fffffffUL))
  113. void __iomem *
  114. __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  115. {
  116. void __iomem * addr;
  117. struct vm_struct * area;
  118. unsigned long offset, last_addr;
  119. /* Don't allow wraparound or zero size */
  120. last_addr = phys_addr + size - 1;
  121. if (!size || last_addr < phys_addr)
  122. return NULL;
  123. /*
  124. * Map objects in the low 512mb of address space using KSEG1, otherwise
  125. * map using page tables.
  126. */
  127. if (IS_LOW512(phys_addr) && IS_LOW512(phys_addr + size - 1))
  128. return (void *) KSEG1ADDR(phys_addr);
  129. /*
  130. * Don't allow anybody to remap normal RAM that we're using..
  131. */
  132. if (phys_addr < virt_to_phys(high_memory)) {
  133. char *t_addr, *t_end;
  134. struct page *page;
  135. t_addr = __va(phys_addr);
  136. t_end = t_addr + (size - 1);
  137. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  138. if(!PageReserved(page))
  139. return NULL;
  140. }
  141. /*
  142. * Mappings have to be page-aligned
  143. */
  144. offset = phys_addr & ~PAGE_MASK;
  145. phys_addr &= PAGE_MASK;
  146. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  147. /*
  148. * Ok, go for it..
  149. */
  150. area = get_vm_area(size, VM_IOREMAP);
  151. if (!area)
  152. return NULL;
  153. area->phys_addr = phys_addr;
  154. addr = (void __iomem *) area->addr;
  155. if (remap_area_pages((unsigned long)addr, phys_addr, size, flags)) {
  156. vunmap((void __force *) addr);
  157. return NULL;
  158. }
  159. return (void __iomem *) (offset + (char __iomem *)addr);
  160. }
  161. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == KSEG1)
  162. void iounmap(volatile void __iomem *addr)
  163. {
  164. if (!IS_KSEG1(addr))
  165. vfree((void *) (PAGE_MASK & (unsigned long) addr));
  166. }