kmap.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * linux/arch/m68k/mm/kmap.c
  3. *
  4. * Copyright (C) 1997 Roman Hodek
  5. *
  6. * 10/01/99 cleaned up the code and changing to the same interface
  7. * used by other architectures /Roman Zippel
  8. */
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <asm/setup.h>
  17. #include <asm/segment.h>
  18. #include <asm/page.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/io.h>
  21. #undef DEBUG
  22. #define PTRTREESIZE (256*1024)
  23. /*
  24. * For 040/060 we can use the virtual memory area like other architectures,
  25. * but for 020/030 we want to use early termination page descriptors and we
  26. * can't mix this with normal page descriptors, so we have to copy that code
  27. * (mm/vmalloc.c) and return appropriately aligned addresses.
  28. */
  29. #ifdef CPU_M68040_OR_M68060_ONLY
  30. #define IO_SIZE PAGE_SIZE
  31. static inline struct vm_struct *get_io_area(unsigned long size)
  32. {
  33. return get_vm_area(size, VM_IOREMAP);
  34. }
  35. static inline void free_io_area(void *addr)
  36. {
  37. vfree((void *)(PAGE_MASK & (unsigned long)addr));
  38. }
  39. #else
  40. #define IO_SIZE (256*1024)
  41. static struct vm_struct *iolist;
  42. static struct vm_struct *get_io_area(unsigned long size)
  43. {
  44. unsigned long addr;
  45. struct vm_struct **p, *tmp, *area;
  46. area = kmalloc(sizeof(*area), GFP_KERNEL);
  47. if (!area)
  48. return NULL;
  49. addr = KMAP_START;
  50. for (p = &iolist; (tmp = *p) ; p = &tmp->next) {
  51. if (size + addr < (unsigned long)tmp->addr)
  52. break;
  53. if (addr > KMAP_END-size) {
  54. kfree(area);
  55. return NULL;
  56. }
  57. addr = tmp->size + (unsigned long)tmp->addr;
  58. }
  59. area->addr = (void *)addr;
  60. area->size = size + IO_SIZE;
  61. area->next = *p;
  62. *p = area;
  63. return area;
  64. }
  65. static inline void free_io_area(void *addr)
  66. {
  67. struct vm_struct **p, *tmp;
  68. if (!addr)
  69. return;
  70. addr = (void *)((unsigned long)addr & -IO_SIZE);
  71. for (p = &iolist ; (tmp = *p) ; p = &tmp->next) {
  72. if (tmp->addr == addr) {
  73. *p = tmp->next;
  74. /* remove gap added in get_io_area() */
  75. __iounmap(tmp->addr, tmp->size - IO_SIZE);
  76. kfree(tmp);
  77. return;
  78. }
  79. }
  80. }
  81. #endif
  82. /*
  83. * Map some physical address range into the kernel address space.
  84. */
  85. /* Rewritten by Andreas Schwab to remove all races. */
  86. void __iomem *__ioremap(unsigned long physaddr, unsigned long size, int cacheflag)
  87. {
  88. struct vm_struct *area;
  89. unsigned long virtaddr, retaddr;
  90. long offset;
  91. pgd_t *pgd_dir;
  92. pmd_t *pmd_dir;
  93. pte_t *pte_dir;
  94. /*
  95. * Don't allow mappings that wrap..
  96. */
  97. if (!size || physaddr > (unsigned long)(-size))
  98. return NULL;
  99. #ifdef CONFIG_AMIGA
  100. if (MACH_IS_AMIGA) {
  101. if ((physaddr >= 0x40000000) && (physaddr + size < 0x60000000)
  102. && (cacheflag == IOMAP_NOCACHE_SER))
  103. return (void __iomem *)physaddr;
  104. }
  105. #endif
  106. #ifdef DEBUG
  107. printk("ioremap: 0x%lx,0x%lx(%d) - ", physaddr, size, cacheflag);
  108. #endif
  109. /*
  110. * Mappings have to be aligned
  111. */
  112. offset = physaddr & (IO_SIZE - 1);
  113. physaddr &= -IO_SIZE;
  114. size = (size + offset + IO_SIZE - 1) & -IO_SIZE;
  115. /*
  116. * Ok, go for it..
  117. */
  118. area = get_io_area(size);
  119. if (!area)
  120. return NULL;
  121. virtaddr = (unsigned long)area->addr;
  122. retaddr = virtaddr + offset;
  123. #ifdef DEBUG
  124. printk("0x%lx,0x%lx,0x%lx", physaddr, virtaddr, retaddr);
  125. #endif
  126. /*
  127. * add cache and table flags to physical address
  128. */
  129. if (CPU_IS_040_OR_060) {
  130. physaddr |= (_PAGE_PRESENT | _PAGE_GLOBAL040 |
  131. _PAGE_ACCESSED | _PAGE_DIRTY);
  132. switch (cacheflag) {
  133. case IOMAP_FULL_CACHING:
  134. physaddr |= _PAGE_CACHE040;
  135. break;
  136. case IOMAP_NOCACHE_SER:
  137. default:
  138. physaddr |= _PAGE_NOCACHE_S;
  139. break;
  140. case IOMAP_NOCACHE_NONSER:
  141. physaddr |= _PAGE_NOCACHE;
  142. break;
  143. case IOMAP_WRITETHROUGH:
  144. physaddr |= _PAGE_CACHE040W;
  145. break;
  146. }
  147. } else {
  148. physaddr |= (_PAGE_PRESENT | _PAGE_ACCESSED |
  149. _PAGE_DIRTY | _PAGE_READWRITE);
  150. switch (cacheflag) {
  151. case IOMAP_NOCACHE_SER:
  152. case IOMAP_NOCACHE_NONSER:
  153. default:
  154. physaddr |= _PAGE_NOCACHE030;
  155. break;
  156. case IOMAP_FULL_CACHING:
  157. case IOMAP_WRITETHROUGH:
  158. break;
  159. }
  160. }
  161. while ((long)size > 0) {
  162. #ifdef DEBUG
  163. if (!(virtaddr & (PTRTREESIZE-1)))
  164. printk ("\npa=%#lx va=%#lx ", physaddr, virtaddr);
  165. #endif
  166. pgd_dir = pgd_offset_k(virtaddr);
  167. pmd_dir = pmd_alloc(&init_mm, pgd_dir, virtaddr);
  168. if (!pmd_dir) {
  169. printk("ioremap: no mem for pmd_dir\n");
  170. return NULL;
  171. }
  172. if (CPU_IS_020_OR_030) {
  173. pmd_dir->pmd[(virtaddr/PTRTREESIZE) & 15] = physaddr;
  174. physaddr += PTRTREESIZE;
  175. virtaddr += PTRTREESIZE;
  176. size -= PTRTREESIZE;
  177. } else {
  178. pte_dir = pte_alloc_kernel(pmd_dir, virtaddr);
  179. if (!pte_dir) {
  180. printk("ioremap: no mem for pte_dir\n");
  181. return NULL;
  182. }
  183. pte_val(*pte_dir) = physaddr;
  184. virtaddr += PAGE_SIZE;
  185. physaddr += PAGE_SIZE;
  186. size -= PAGE_SIZE;
  187. }
  188. }
  189. #ifdef DEBUG
  190. printk("\n");
  191. #endif
  192. flush_tlb_all();
  193. return (void __iomem *)retaddr;
  194. }
  195. EXPORT_SYMBOL(__ioremap);
  196. /*
  197. * Unmap an ioremap()ed region again
  198. */
  199. void iounmap(void __iomem *addr)
  200. {
  201. #ifdef CONFIG_AMIGA
  202. if ((!MACH_IS_AMIGA) ||
  203. (((unsigned long)addr < 0x40000000) ||
  204. ((unsigned long)addr > 0x60000000)))
  205. free_io_area((__force void *)addr);
  206. #else
  207. free_io_area((__force void *)addr);
  208. #endif
  209. }
  210. EXPORT_SYMBOL(iounmap);
  211. /*
  212. * __iounmap unmaps nearly everything, so be careful
  213. * Currently it doesn't free pointer/page tables anymore but this
  214. * wasn't used anyway and might be added later.
  215. */
  216. void __iounmap(void *addr, unsigned long size)
  217. {
  218. unsigned long virtaddr = (unsigned long)addr;
  219. pgd_t *pgd_dir;
  220. pmd_t *pmd_dir;
  221. pte_t *pte_dir;
  222. while ((long)size > 0) {
  223. pgd_dir = pgd_offset_k(virtaddr);
  224. if (pgd_bad(*pgd_dir)) {
  225. printk("iounmap: bad pgd(%08lx)\n", pgd_val(*pgd_dir));
  226. pgd_clear(pgd_dir);
  227. return;
  228. }
  229. pmd_dir = pmd_offset(pgd_dir, virtaddr);
  230. if (CPU_IS_020_OR_030) {
  231. int pmd_off = (virtaddr/PTRTREESIZE) & 15;
  232. int pmd_type = pmd_dir->pmd[pmd_off] & _DESCTYPE_MASK;
  233. if (pmd_type == _PAGE_PRESENT) {
  234. pmd_dir->pmd[pmd_off] = 0;
  235. virtaddr += PTRTREESIZE;
  236. size -= PTRTREESIZE;
  237. continue;
  238. } else if (pmd_type == 0)
  239. continue;
  240. }
  241. if (pmd_bad(*pmd_dir)) {
  242. printk("iounmap: bad pmd (%08lx)\n", pmd_val(*pmd_dir));
  243. pmd_clear(pmd_dir);
  244. return;
  245. }
  246. pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
  247. pte_val(*pte_dir) = 0;
  248. virtaddr += PAGE_SIZE;
  249. size -= PAGE_SIZE;
  250. }
  251. flush_tlb_all();
  252. }
  253. /*
  254. * Set new cache mode for some kernel address space.
  255. * The caller must push data for that range itself, if such data may already
  256. * be in the cache.
  257. */
  258. void kernel_set_cachemode(void *addr, unsigned long size, int cmode)
  259. {
  260. unsigned long virtaddr = (unsigned long)addr;
  261. pgd_t *pgd_dir;
  262. pmd_t *pmd_dir;
  263. pte_t *pte_dir;
  264. if (CPU_IS_040_OR_060) {
  265. switch (cmode) {
  266. case IOMAP_FULL_CACHING:
  267. cmode = _PAGE_CACHE040;
  268. break;
  269. case IOMAP_NOCACHE_SER:
  270. default:
  271. cmode = _PAGE_NOCACHE_S;
  272. break;
  273. case IOMAP_NOCACHE_NONSER:
  274. cmode = _PAGE_NOCACHE;
  275. break;
  276. case IOMAP_WRITETHROUGH:
  277. cmode = _PAGE_CACHE040W;
  278. break;
  279. }
  280. } else {
  281. switch (cmode) {
  282. case IOMAP_NOCACHE_SER:
  283. case IOMAP_NOCACHE_NONSER:
  284. default:
  285. cmode = _PAGE_NOCACHE030;
  286. break;
  287. case IOMAP_FULL_CACHING:
  288. case IOMAP_WRITETHROUGH:
  289. cmode = 0;
  290. }
  291. }
  292. while ((long)size > 0) {
  293. pgd_dir = pgd_offset_k(virtaddr);
  294. if (pgd_bad(*pgd_dir)) {
  295. printk("iocachemode: bad pgd(%08lx)\n", pgd_val(*pgd_dir));
  296. pgd_clear(pgd_dir);
  297. return;
  298. }
  299. pmd_dir = pmd_offset(pgd_dir, virtaddr);
  300. if (CPU_IS_020_OR_030) {
  301. int pmd_off = (virtaddr/PTRTREESIZE) & 15;
  302. if ((pmd_dir->pmd[pmd_off] & _DESCTYPE_MASK) == _PAGE_PRESENT) {
  303. pmd_dir->pmd[pmd_off] = (pmd_dir->pmd[pmd_off] &
  304. _CACHEMASK040) | cmode;
  305. virtaddr += PTRTREESIZE;
  306. size -= PTRTREESIZE;
  307. continue;
  308. }
  309. }
  310. if (pmd_bad(*pmd_dir)) {
  311. printk("iocachemode: bad pmd (%08lx)\n", pmd_val(*pmd_dir));
  312. pmd_clear(pmd_dir);
  313. return;
  314. }
  315. pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
  316. pte_val(*pte_dir) = (pte_val(*pte_dir) & _CACHEMASK040) | cmode;
  317. virtaddr += PAGE_SIZE;
  318. size -= PAGE_SIZE;
  319. }
  320. flush_tlb_all();
  321. }
  322. EXPORT_SYMBOL(kernel_set_cachemode);