generic.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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++;
  74. } while (address < curend);
  75. } while (address < end);
  76. }
  77. static inline int io_remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
  78. unsigned long offset, pgprot_t prot, int space)
  79. {
  80. unsigned long end;
  81. address &= ~PGDIR_MASK;
  82. end = address + size;
  83. if (end > PGDIR_SIZE)
  84. end = PGDIR_SIZE;
  85. offset -= address;
  86. do {
  87. pte_t * pte = pte_alloc_map(mm, pmd, address);
  88. if (!pte)
  89. return -ENOMEM;
  90. io_remap_pte_range(mm, pte, address, end - address, address + offset, prot, space);
  91. pte_unmap(pte);
  92. address = (address + PMD_SIZE) & PMD_MASK;
  93. pmd++;
  94. } while (address < end);
  95. return 0;
  96. }
  97. static inline int io_remap_pud_range(struct mm_struct *mm, pud_t * pud, unsigned long address, unsigned long size,
  98. unsigned long offset, pgprot_t prot, int space)
  99. {
  100. unsigned long end;
  101. address &= ~PUD_MASK;
  102. end = address + size;
  103. if (end > PUD_SIZE)
  104. end = PUD_SIZE;
  105. offset -= address;
  106. do {
  107. pmd_t *pmd = pmd_alloc(mm, pud, address);
  108. if (!pud)
  109. return -ENOMEM;
  110. io_remap_pmd_range(mm, pmd, address, end - address, address + offset, prot, space);
  111. address = (address + PUD_SIZE) & PUD_MASK;
  112. pud++;
  113. } while (address < end);
  114. return 0;
  115. }
  116. int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  117. unsigned long pfn, unsigned long size, pgprot_t prot)
  118. {
  119. int error = 0;
  120. pgd_t * dir;
  121. unsigned long beg = from;
  122. unsigned long end = from + size;
  123. struct mm_struct *mm = vma->vm_mm;
  124. int space = GET_IOSPACE(pfn);
  125. unsigned long offset = GET_PFN(pfn) << PAGE_SHIFT;
  126. unsigned long phys_base;
  127. phys_base = offset | (((unsigned long) space) << 32UL);
  128. /* See comment in mm/memory.c remap_pfn_range */
  129. vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
  130. vma->vm_pgoff = phys_base >> PAGE_SHIFT;
  131. prot = __pgprot(pg_iobits);
  132. offset -= from;
  133. dir = pgd_offset(mm, from);
  134. flush_cache_range(vma, beg, end);
  135. while (from < end) {
  136. pud_t *pud = pud_alloc(mm, dir, from);
  137. error = -ENOMEM;
  138. if (!pud)
  139. break;
  140. error = io_remap_pud_range(mm, pud, from, end - from, offset + from, prot, space);
  141. if (error)
  142. break;
  143. from = (from + PGDIR_SIZE) & PGDIR_MASK;
  144. dir++;
  145. }
  146. flush_tlb_range(vma, beg, end);
  147. return error;
  148. }