generic.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* $Id: generic.c,v 1.18 2001/12/21 04:56:15 davem Exp $
  2. * generic.c: Generic Sparc mm routines that are not dependent upon
  3. * MMU type but are Sparc specific.
  4. *
  5. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/mm.h>
  9. #include <linux/swap.h>
  10. #include <linux/pagemap.h>
  11. #include <asm/pgalloc.h>
  12. #include <asm/pgtable.h>
  13. #include <asm/page.h>
  14. #include <asm/tlbflush.h>
  15. static inline pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space)
  16. {
  17. pte_t pte;
  18. pte_val(pte) = (((page) | pgprot_val(prot) | _PAGE_E) &
  19. ~(unsigned long)_PAGE_CACHE);
  20. pte_val(pte) |= (((unsigned long)space) << 32);
  21. return pte;
  22. }
  23. /* Remap IO memory, the same way as remap_pfn_range(), but use
  24. * the obio memory space.
  25. *
  26. * They use a pgprot that sets PAGE_IO and does not check the
  27. * mem_map table as this is independent of normal memory.
  28. */
  29. static inline void io_remap_pte_range(struct mm_struct *mm, pte_t * pte,
  30. unsigned long address,
  31. unsigned long size,
  32. unsigned long offset, pgprot_t prot,
  33. int space)
  34. {
  35. unsigned long end;
  36. /* clear hack bit that was used as a write_combine side-effect flag */
  37. offset &= ~0x1UL;
  38. address &= ~PMD_MASK;
  39. end = address + size;
  40. if (end > PMD_SIZE)
  41. end = PMD_SIZE;
  42. do {
  43. pte_t entry;
  44. unsigned long curend = address + PAGE_SIZE;
  45. entry = mk_pte_io(offset, prot, space);
  46. if (!(address & 0xffff)) {
  47. if (!(address & 0x3fffff) && !(offset & 0x3ffffe) && end >= address + 0x400000) {
  48. entry = mk_pte_io(offset,
  49. __pgprot(pgprot_val (prot) | _PAGE_SZ4MB),
  50. space);
  51. curend = address + 0x400000;
  52. offset += 0x400000;
  53. } else if (!(address & 0x7ffff) && !(offset & 0x7fffe) && end >= address + 0x80000) {
  54. entry = mk_pte_io(offset,
  55. __pgprot(pgprot_val (prot) | _PAGE_SZ512K),
  56. space);
  57. curend = address + 0x80000;
  58. offset += 0x80000;
  59. } else if (!(offset & 0xfffe) && end >= address + 0x10000) {
  60. entry = mk_pte_io(offset,
  61. __pgprot(pgprot_val (prot) | _PAGE_SZ64K),
  62. space);
  63. curend = address + 0x10000;
  64. offset += 0x10000;
  65. } else
  66. offset += PAGE_SIZE;
  67. } else
  68. offset += PAGE_SIZE;
  69. do {
  70. BUG_ON(!pte_none(*pte));
  71. set_pte_at(mm, address, pte, entry);
  72. address += PAGE_SIZE;
  73. pte_val(entry) += PAGE_SIZE;
  74. pte++;
  75. } while (address < curend);
  76. } while (address < end);
  77. }
  78. static inline int io_remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
  79. unsigned long offset, pgprot_t prot, int space)
  80. {
  81. unsigned long end;
  82. address &= ~PGDIR_MASK;
  83. end = address + size;
  84. if (end > PGDIR_SIZE)
  85. end = PGDIR_SIZE;
  86. offset -= address;
  87. do {
  88. pte_t * pte = pte_alloc_map(mm, pmd, address);
  89. if (!pte)
  90. return -ENOMEM;
  91. io_remap_pte_range(mm, pte, address, end - address, address + offset, prot, space);
  92. pte_unmap(pte);
  93. address = (address + PMD_SIZE) & PMD_MASK;
  94. pmd++;
  95. } while (address < end);
  96. return 0;
  97. }
  98. static inline int io_remap_pud_range(struct mm_struct *mm, pud_t * pud, unsigned long address, unsigned long size,
  99. unsigned long offset, pgprot_t prot, int space)
  100. {
  101. unsigned long end;
  102. address &= ~PUD_MASK;
  103. end = address + size;
  104. if (end > PUD_SIZE)
  105. end = PUD_SIZE;
  106. offset -= address;
  107. do {
  108. pmd_t *pmd = pmd_alloc(mm, pud, address);
  109. if (!pud)
  110. return -ENOMEM;
  111. io_remap_pmd_range(mm, pmd, address, end - address, address + offset, prot, space);
  112. address = (address + PUD_SIZE) & PUD_MASK;
  113. pud++;
  114. } while (address < end);
  115. return 0;
  116. }
  117. int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  118. unsigned long pfn, unsigned long size, pgprot_t prot)
  119. {
  120. int error = 0;
  121. pgd_t * dir;
  122. unsigned long beg = from;
  123. unsigned long end = from + size;
  124. struct mm_struct *mm = vma->vm_mm;
  125. int space = GET_IOSPACE(pfn);
  126. unsigned long offset = GET_PFN(pfn) << PAGE_SHIFT;
  127. unsigned long phys_base;
  128. phys_base = offset | (((unsigned long) space) << 32UL);
  129. /* See comment in mm/memory.c remap_pfn_range */
  130. vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
  131. vma->vm_pgoff = phys_base >> PAGE_SHIFT;
  132. prot = __pgprot(pg_iobits);
  133. offset -= from;
  134. dir = pgd_offset(mm, from);
  135. flush_cache_range(vma, beg, end);
  136. while (from < end) {
  137. pud_t *pud = pud_alloc(mm, dir, from);
  138. error = -ENOMEM;
  139. if (!pud)
  140. break;
  141. error = io_remap_pud_range(mm, pud, from, end - from, offset + from, prot, space);
  142. if (error)
  143. break;
  144. from = (from + PGDIR_SIZE) & PGDIR_MASK;
  145. dir++;
  146. }
  147. flush_tlb_range(vma, beg, end);
  148. return error;
  149. }