motorola.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * linux/arch/m68k/mm/motorola.c
  3. *
  4. * Routines specific to the Motorola MMU, originally from:
  5. * linux/arch/m68k/init.c
  6. * which are Copyright (C) 1995 Hamish Macdonald
  7. *
  8. * Moved 8/20/1999 Sam Creasey
  9. */
  10. #include <linux/module.h>
  11. #include <linux/signal.h>
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/swap.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/init.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/gfp.h>
  21. #include <asm/setup.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/page.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/machdep.h>
  26. #include <asm/io.h>
  27. #include <asm/dma.h>
  28. #ifdef CONFIG_ATARI
  29. #include <asm/atari_stram.h>
  30. #endif
  31. #include <asm/sections.h>
  32. #undef DEBUG
  33. #ifndef mm_cachebits
  34. /*
  35. * Bits to add to page descriptors for "normal" caching mode.
  36. * For 68020/030 this is 0.
  37. * For 68040, this is _PAGE_CACHE040 (cachable, copyback)
  38. */
  39. unsigned long mm_cachebits;
  40. EXPORT_SYMBOL(mm_cachebits);
  41. #endif
  42. /* size of memory already mapped in head.S */
  43. extern __initdata unsigned long m68k_init_mapped_size;
  44. extern unsigned long availmem;
  45. static pte_t * __init kernel_page_table(void)
  46. {
  47. pte_t *ptablep;
  48. ptablep = (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  49. clear_page(ptablep);
  50. __flush_page_to_ram(ptablep);
  51. flush_tlb_kernel_page(ptablep);
  52. nocache_page(ptablep);
  53. return ptablep;
  54. }
  55. static pmd_t *last_pgtable __initdata = NULL;
  56. pmd_t *zero_pgtable __initdata = NULL;
  57. static pmd_t * __init kernel_ptr_table(void)
  58. {
  59. if (!last_pgtable) {
  60. unsigned long pmd, last;
  61. int i;
  62. /* Find the last ptr table that was used in head.S and
  63. * reuse the remaining space in that page for further
  64. * ptr tables.
  65. */
  66. last = (unsigned long)kernel_pg_dir;
  67. for (i = 0; i < PTRS_PER_PGD; i++) {
  68. if (!pgd_present(kernel_pg_dir[i]))
  69. continue;
  70. pmd = __pgd_page(kernel_pg_dir[i]);
  71. if (pmd > last)
  72. last = pmd;
  73. }
  74. last_pgtable = (pmd_t *)last;
  75. #ifdef DEBUG
  76. printk("kernel_ptr_init: %p\n", last_pgtable);
  77. #endif
  78. }
  79. last_pgtable += PTRS_PER_PMD;
  80. if (((unsigned long)last_pgtable & ~PAGE_MASK) == 0) {
  81. last_pgtable = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  82. clear_page(last_pgtable);
  83. __flush_page_to_ram(last_pgtable);
  84. flush_tlb_kernel_page(last_pgtable);
  85. nocache_page(last_pgtable);
  86. }
  87. return last_pgtable;
  88. }
  89. static void __init map_node(int node)
  90. {
  91. #define PTRTREESIZE (256*1024)
  92. #define ROOTTREESIZE (32*1024*1024)
  93. unsigned long physaddr, virtaddr, size;
  94. pgd_t *pgd_dir;
  95. pmd_t *pmd_dir;
  96. pte_t *pte_dir;
  97. size = m68k_memory[node].size;
  98. physaddr = m68k_memory[node].addr;
  99. virtaddr = (unsigned long)phys_to_virt(physaddr);
  100. physaddr |= m68k_supervisor_cachemode |
  101. _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY;
  102. if (CPU_IS_040_OR_060)
  103. physaddr |= _PAGE_GLOBAL040;
  104. while (size > 0) {
  105. #ifdef DEBUG
  106. if (!(virtaddr & (PTRTREESIZE-1)))
  107. printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
  108. virtaddr);
  109. #endif
  110. pgd_dir = pgd_offset_k(virtaddr);
  111. if (virtaddr && CPU_IS_020_OR_030) {
  112. if (!(virtaddr & (ROOTTREESIZE-1)) &&
  113. size >= ROOTTREESIZE) {
  114. #ifdef DEBUG
  115. printk ("[very early term]");
  116. #endif
  117. pgd_val(*pgd_dir) = physaddr;
  118. size -= ROOTTREESIZE;
  119. virtaddr += ROOTTREESIZE;
  120. physaddr += ROOTTREESIZE;
  121. continue;
  122. }
  123. }
  124. if (!pgd_present(*pgd_dir)) {
  125. pmd_dir = kernel_ptr_table();
  126. #ifdef DEBUG
  127. printk ("[new pointer %p]", pmd_dir);
  128. #endif
  129. pgd_set(pgd_dir, pmd_dir);
  130. } else
  131. pmd_dir = pmd_offset(pgd_dir, virtaddr);
  132. if (CPU_IS_020_OR_030) {
  133. if (virtaddr) {
  134. #ifdef DEBUG
  135. printk ("[early term]");
  136. #endif
  137. pmd_dir->pmd[(virtaddr/PTRTREESIZE) & 15] = physaddr;
  138. physaddr += PTRTREESIZE;
  139. } else {
  140. int i;
  141. #ifdef DEBUG
  142. printk ("[zero map]");
  143. #endif
  144. zero_pgtable = kernel_ptr_table();
  145. pte_dir = (pte_t *)zero_pgtable;
  146. pmd_dir->pmd[0] = virt_to_phys(pte_dir) |
  147. _PAGE_TABLE | _PAGE_ACCESSED;
  148. pte_val(*pte_dir++) = 0;
  149. physaddr += PAGE_SIZE;
  150. for (i = 1; i < 64; physaddr += PAGE_SIZE, i++)
  151. pte_val(*pte_dir++) = physaddr;
  152. }
  153. size -= PTRTREESIZE;
  154. virtaddr += PTRTREESIZE;
  155. } else {
  156. if (!pmd_present(*pmd_dir)) {
  157. #ifdef DEBUG
  158. printk ("[new table]");
  159. #endif
  160. pte_dir = kernel_page_table();
  161. pmd_set(pmd_dir, pte_dir);
  162. }
  163. pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
  164. if (virtaddr) {
  165. if (!pte_present(*pte_dir))
  166. pte_val(*pte_dir) = physaddr;
  167. } else
  168. pte_val(*pte_dir) = 0;
  169. size -= PAGE_SIZE;
  170. virtaddr += PAGE_SIZE;
  171. physaddr += PAGE_SIZE;
  172. }
  173. }
  174. #ifdef DEBUG
  175. printk("\n");
  176. #endif
  177. }
  178. /*
  179. * paging_init() continues the virtual memory environment setup which
  180. * was begun by the code in arch/head.S.
  181. */
  182. void __init paging_init(void)
  183. {
  184. unsigned long zones_size[MAX_NR_ZONES] = { 0, };
  185. unsigned long min_addr, max_addr;
  186. unsigned long addr, size, end;
  187. int i;
  188. #ifdef DEBUG
  189. printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
  190. #endif
  191. /* Fix the cache mode in the page descriptors for the 680[46]0. */
  192. if (CPU_IS_040_OR_060) {
  193. int i;
  194. #ifndef mm_cachebits
  195. mm_cachebits = _PAGE_CACHE040;
  196. #endif
  197. for (i = 0; i < 16; i++)
  198. pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
  199. }
  200. min_addr = m68k_memory[0].addr;
  201. max_addr = min_addr + m68k_memory[0].size;
  202. for (i = 1; i < m68k_num_memory;) {
  203. if (m68k_memory[i].addr < min_addr) {
  204. printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
  205. m68k_memory[i].addr, m68k_memory[i].size);
  206. printk("Fix your bootloader or use a memfile to make use of this area!\n");
  207. m68k_num_memory--;
  208. memmove(m68k_memory + i, m68k_memory + i + 1,
  209. (m68k_num_memory - i) * sizeof(struct m68k_mem_info));
  210. continue;
  211. }
  212. addr = m68k_memory[i].addr + m68k_memory[i].size;
  213. if (addr > max_addr)
  214. max_addr = addr;
  215. i++;
  216. }
  217. m68k_memoffset = min_addr - PAGE_OFFSET;
  218. m68k_virt_to_node_shift = fls(max_addr - min_addr - 1) - 6;
  219. module_fixup(NULL, __start_fixup, __stop_fixup);
  220. flush_icache();
  221. high_memory = phys_to_virt(max_addr);
  222. min_low_pfn = availmem >> PAGE_SHIFT;
  223. max_pfn = max_low_pfn = max_addr >> PAGE_SHIFT;
  224. for (i = 0; i < m68k_num_memory; i++) {
  225. addr = m68k_memory[i].addr;
  226. end = addr + m68k_memory[i].size;
  227. m68k_setup_node(i);
  228. availmem = PAGE_ALIGN(availmem);
  229. availmem += init_bootmem_node(NODE_DATA(i),
  230. availmem >> PAGE_SHIFT,
  231. addr >> PAGE_SHIFT,
  232. end >> PAGE_SHIFT);
  233. }
  234. /*
  235. * Map the physical memory available into the kernel virtual
  236. * address space. First initialize the bootmem allocator with
  237. * the memory we already mapped, so map_node() has something
  238. * to allocate.
  239. */
  240. addr = m68k_memory[0].addr;
  241. size = m68k_memory[0].size;
  242. free_bootmem_node(NODE_DATA(0), availmem,
  243. min(m68k_init_mapped_size, size) - (availmem - addr));
  244. map_node(0);
  245. if (size > m68k_init_mapped_size)
  246. free_bootmem_node(NODE_DATA(0), addr + m68k_init_mapped_size,
  247. size - m68k_init_mapped_size);
  248. for (i = 1; i < m68k_num_memory; i++)
  249. map_node(i);
  250. flush_tlb_all();
  251. /*
  252. * initialize the bad page table and bad page to point
  253. * to a couple of allocated pages
  254. */
  255. empty_zero_page = alloc_bootmem_pages(PAGE_SIZE);
  256. /*
  257. * Set up SFC/DFC registers
  258. */
  259. set_fs(KERNEL_DS);
  260. #ifdef DEBUG
  261. printk ("before free_area_init\n");
  262. #endif
  263. for (i = 0; i < m68k_num_memory; i++) {
  264. zones_size[ZONE_DMA] = m68k_memory[i].size >> PAGE_SHIFT;
  265. free_area_init_node(i, zones_size,
  266. m68k_memory[i].addr >> PAGE_SHIFT, NULL);
  267. if (node_present_pages(i))
  268. node_set_state(i, N_NORMAL_MEMORY);
  269. }
  270. }