kmap.c 8.1 KB

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