Adrian Bunk | b00dc83 | 2008-05-19 16:52:27 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/swap.h> |
Paul Gortmaker | 7b64db6 | 2011-07-18 15:57:46 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/pagemap.h> |
| 13 | |
| 14 | #include <asm/pgalloc.h> |
| 15 | #include <asm/pgtable.h> |
| 16 | #include <asm/page.h> |
| 17 | #include <asm/tlbflush.h> |
| 18 | |
| 19 | /* Remap IO memory, the same way as remap_pfn_range(), but use |
| 20 | * the obio memory space. |
| 21 | * |
| 22 | * They use a pgprot that sets PAGE_IO and does not check the |
| 23 | * mem_map table as this is independent of normal memory. |
| 24 | */ |
| 25 | static inline void io_remap_pte_range(struct mm_struct *mm, pte_t * pte, |
| 26 | unsigned long address, |
| 27 | unsigned long size, |
| 28 | unsigned long offset, pgprot_t prot, |
| 29 | int space) |
| 30 | { |
| 31 | unsigned long end; |
| 32 | |
| 33 | /* clear hack bit that was used as a write_combine side-effect flag */ |
| 34 | offset &= ~0x1UL; |
| 35 | address &= ~PMD_MASK; |
| 36 | end = address + size; |
| 37 | if (end > PMD_SIZE) |
| 38 | end = PMD_SIZE; |
| 39 | do { |
| 40 | pte_t entry; |
| 41 | unsigned long curend = address + PAGE_SIZE; |
| 42 | |
David S. Miller | c4bce90 | 2006-02-11 21:57:54 -0800 | [diff] [blame] | 43 | entry = mk_pte_io(offset, prot, space, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | if (!(address & 0xffff)) { |
David S. Miller | c4bce90 | 2006-02-11 21:57:54 -0800 | [diff] [blame] | 45 | if (PAGE_SIZE < (4 * 1024 * 1024) && |
| 46 | !(address & 0x3fffff) && |
| 47 | !(offset & 0x3ffffe) && |
| 48 | end >= address + 0x400000) { |
| 49 | entry = mk_pte_io(offset, prot, space, |
| 50 | 4 * 1024 * 1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | curend = address + 0x400000; |
| 52 | offset += 0x400000; |
David S. Miller | c4bce90 | 2006-02-11 21:57:54 -0800 | [diff] [blame] | 53 | } else if (PAGE_SIZE < (512 * 1024) && |
| 54 | !(address & 0x7ffff) && |
| 55 | !(offset & 0x7fffe) && |
| 56 | end >= address + 0x80000) { |
| 57 | entry = mk_pte_io(offset, prot, space, |
| 58 | 512 * 1024 * 1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | curend = address + 0x80000; |
| 60 | offset += 0x80000; |
David S. Miller | c4bce90 | 2006-02-11 21:57:54 -0800 | [diff] [blame] | 61 | } else if (PAGE_SIZE < (64 * 1024) && |
| 62 | !(offset & 0xfffe) && |
| 63 | end >= address + 0x10000) { |
| 64 | entry = mk_pte_io(offset, prot, space, |
| 65 | 64 * 1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | curend = address + 0x10000; |
| 67 | offset += 0x10000; |
| 68 | } else |
| 69 | offset += PAGE_SIZE; |
| 70 | } else |
| 71 | offset += PAGE_SIZE; |
| 72 | |
David S. Miller | 47f2c36 | 2006-08-28 00:33:03 -0700 | [diff] [blame] | 73 | if (pte_write(entry)) |
| 74 | entry = pte_mkdirty(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | do { |
| 76 | BUG_ON(!pte_none(*pte)); |
| 77 | set_pte_at(mm, address, pte, entry); |
| 78 | address += PAGE_SIZE; |
David S. Miller | cab3f16 | 2005-11-29 13:59:03 -0800 | [diff] [blame] | 79 | pte_val(entry) += PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | pte++; |
| 81 | } while (address < curend); |
| 82 | } while (address < end); |
| 83 | } |
| 84 | |
| 85 | static inline int io_remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size, |
| 86 | unsigned long offset, pgprot_t prot, int space) |
| 87 | { |
| 88 | unsigned long end; |
| 89 | |
| 90 | address &= ~PGDIR_MASK; |
| 91 | end = address + size; |
| 92 | if (end > PGDIR_SIZE) |
| 93 | end = PGDIR_SIZE; |
| 94 | offset -= address; |
| 95 | do { |
Andrea Arcangeli | 8ac1f83 | 2011-01-13 15:46:43 -0800 | [diff] [blame] | 96 | pte_t *pte = pte_alloc_map(mm, NULL, pmd, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (!pte) |
| 98 | return -ENOMEM; |
| 99 | io_remap_pte_range(mm, pte, address, end - address, address + offset, prot, space); |
| 100 | pte_unmap(pte); |
| 101 | address = (address + PMD_SIZE) & PMD_MASK; |
| 102 | pmd++; |
| 103 | } while (address < end); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static inline int io_remap_pud_range(struct mm_struct *mm, pud_t * pud, unsigned long address, unsigned long size, |
| 108 | unsigned long offset, pgprot_t prot, int space) |
| 109 | { |
| 110 | unsigned long end; |
| 111 | |
| 112 | address &= ~PUD_MASK; |
| 113 | end = address + size; |
| 114 | if (end > PUD_SIZE) |
| 115 | end = PUD_SIZE; |
| 116 | offset -= address; |
| 117 | do { |
| 118 | pmd_t *pmd = pmd_alloc(mm, pud, address); |
| 119 | if (!pud) |
| 120 | return -ENOMEM; |
| 121 | io_remap_pmd_range(mm, pmd, address, end - address, address + offset, prot, space); |
| 122 | address = (address + PUD_SIZE) & PUD_MASK; |
| 123 | pud++; |
| 124 | } while (address < end); |
| 125 | return 0; |
| 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long from, |
| 129 | unsigned long pfn, unsigned long size, pgprot_t prot) |
| 130 | { |
| 131 | int error = 0; |
| 132 | pgd_t * dir; |
| 133 | unsigned long beg = from; |
| 134 | unsigned long end = from + size; |
| 135 | struct mm_struct *mm = vma->vm_mm; |
| 136 | int space = GET_IOSPACE(pfn); |
| 137 | unsigned long offset = GET_PFN(pfn) << PAGE_SHIFT; |
David S. Miller | 5cd9194 | 2005-11-28 14:02:10 -0800 | [diff] [blame] | 138 | unsigned long phys_base; |
| 139 | |
| 140 | phys_base = offset | (((unsigned long) space) << 32UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 142 | /* See comment in mm/memory.c remap_pfn_range */ |
David S. Miller | 5cd9194 | 2005-11-28 14:02:10 -0800 | [diff] [blame] | 143 | vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; |
| 144 | vma->vm_pgoff = phys_base >> PAGE_SHIFT; |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | offset -= from; |
| 147 | dir = pgd_offset(mm, from); |
| 148 | flush_cache_range(vma, beg, end); |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | while (from < end) { |
Hugh Dickins | b462705 | 2005-10-29 18:16:24 -0700 | [diff] [blame] | 151 | pud_t *pud = pud_alloc(mm, dir, from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | error = -ENOMEM; |
| 153 | if (!pud) |
| 154 | break; |
| 155 | error = io_remap_pud_range(mm, pud, from, end - from, offset + from, prot, space); |
| 156 | if (error) |
| 157 | break; |
| 158 | from = (from + PGDIR_SIZE) & PGDIR_MASK; |
| 159 | dir++; |
| 160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Hugh Dickins | b462705 | 2005-10-29 18:16:24 -0700 | [diff] [blame] | 162 | flush_tlb_range(vma, beg, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return error; |
| 164 | } |
Sam Ravnborg | 917c366 | 2009-01-08 16:58:20 -0800 | [diff] [blame] | 165 | EXPORT_SYMBOL(io_remap_pfn_range); |