init.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * arch/xtensa/mm/init.c
  3. *
  4. * Derived from MIPS, PPC.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica Inc.
  11. *
  12. * Chris Zankel <chris@zankel.net>
  13. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  14. * Marc Gauthier
  15. * Kevin Chea
  16. */
  17. #include <linux/config.h>
  18. #include <linux/init.h>
  19. #include <linux/signal.h>
  20. #include <linux/sched.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/string.h>
  24. #include <linux/types.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/bootmem.h>
  27. #include <linux/swap.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/bootparam.h>
  30. #include <asm/mmu_context.h>
  31. #include <asm/tlb.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/page.h>
  34. #include <asm/pgalloc.h>
  35. #include <asm/pgtable.h>
  36. #define DEBUG 0
  37. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  38. //static DEFINE_SPINLOCK(tlb_lock);
  39. /*
  40. * This flag is used to indicate that the page was mapped and modified in
  41. * kernel space, so the cache is probably dirty at that address.
  42. * If cache aliasing is enabled and the page color mismatches, update_mmu_cache
  43. * synchronizes the caches if this bit is set.
  44. */
  45. #define PG_cache_clean PG_arch_1
  46. /* References to section boundaries */
  47. extern char _ftext, _etext, _fdata, _edata, _rodata_end;
  48. extern char __init_begin, __init_end;
  49. /*
  50. * mem_reserve(start, end, must_exist)
  51. *
  52. * Reserve some memory from the memory pool.
  53. *
  54. * Parameters:
  55. * start Start of region,
  56. * end End of region,
  57. * must_exist Must exist in memory pool.
  58. *
  59. * Returns:
  60. * 0 (memory area couldn't be mapped)
  61. * -1 (success)
  62. */
  63. int __init mem_reserve(unsigned long start, unsigned long end, int must_exist)
  64. {
  65. int i;
  66. if (start == end)
  67. return 0;
  68. start = start & PAGE_MASK;
  69. end = PAGE_ALIGN(end);
  70. for (i = 0; i < sysmem.nr_banks; i++)
  71. if (start < sysmem.bank[i].end
  72. && end >= sysmem.bank[i].start)
  73. break;
  74. if (i == sysmem.nr_banks) {
  75. if (must_exist)
  76. printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) "
  77. "not in any region!\n", start, end);
  78. return 0;
  79. }
  80. if (start > sysmem.bank[i].start) {
  81. if (end < sysmem.bank[i].end) {
  82. /* split entry */
  83. if (sysmem.nr_banks >= SYSMEM_BANKS_MAX)
  84. panic("meminfo overflow\n");
  85. sysmem.bank[sysmem.nr_banks].start = end;
  86. sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end;
  87. sysmem.nr_banks++;
  88. }
  89. sysmem.bank[i].end = start;
  90. } else {
  91. if (end < sysmem.bank[i].end)
  92. sysmem.bank[i].start = end;
  93. else {
  94. /* remove entry */
  95. sysmem.nr_banks--;
  96. sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start;
  97. sysmem.bank[i].end = sysmem.bank[sysmem.nr_banks].end;
  98. }
  99. }
  100. return -1;
  101. }
  102. /*
  103. * Initialize the bootmem system and give it all the memory we have available.
  104. */
  105. void __init bootmem_init(void)
  106. {
  107. unsigned long pfn;
  108. unsigned long bootmap_start, bootmap_size;
  109. int i;
  110. max_low_pfn = max_pfn = 0;
  111. min_low_pfn = ~0;
  112. for (i=0; i < sysmem.nr_banks; i++) {
  113. pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT;
  114. if (pfn < min_low_pfn)
  115. min_low_pfn = pfn;
  116. pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT;
  117. if (pfn > max_pfn)
  118. max_pfn = pfn;
  119. }
  120. if (min_low_pfn > max_pfn)
  121. panic("No memory found!\n");
  122. max_low_pfn = max_pfn < MAX_LOW_MEMORY >> PAGE_SHIFT ?
  123. max_pfn : MAX_LOW_MEMORY >> PAGE_SHIFT;
  124. /* Find an area to use for the bootmem bitmap. */
  125. bootmap_size = bootmem_bootmap_pages(max_low_pfn) << PAGE_SHIFT;
  126. bootmap_start = ~0;
  127. for (i=0; i<sysmem.nr_banks; i++)
  128. if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) {
  129. bootmap_start = sysmem.bank[i].start;
  130. break;
  131. }
  132. if (bootmap_start == ~0UL)
  133. panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
  134. /* Reserve the bootmem bitmap area */
  135. mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1);
  136. bootmap_size = init_bootmem_node(NODE_DATA(0), min_low_pfn,
  137. bootmap_start >> PAGE_SHIFT,
  138. max_low_pfn);
  139. /* Add all remaining memory pieces into the bootmem map */
  140. for (i=0; i<sysmem.nr_banks; i++)
  141. free_bootmem(sysmem.bank[i].start,
  142. sysmem.bank[i].end - sysmem.bank[i].start);
  143. }
  144. void __init paging_init(void)
  145. {
  146. unsigned long zones_size[MAX_NR_ZONES];
  147. int i;
  148. /* All pages are DMA-able, so we put them all in the DMA zone. */
  149. zones_size[ZONE_DMA] = max_low_pfn;
  150. for (i = 1; i < MAX_NR_ZONES; i++)
  151. zones_size[i] = 0;
  152. #ifdef CONFIG_HIGHMEM
  153. zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn;
  154. #endif
  155. /* Initialize the kernel's page tables. */
  156. memset(swapper_pg_dir, 0, PAGE_SIZE);
  157. free_area_init(zones_size);
  158. }
  159. /*
  160. * Flush the mmu and reset associated register to default values.
  161. */
  162. void __init init_mmu (void)
  163. {
  164. /* Writing zeros to the <t>TLBCFG special registers ensure
  165. * that valid values exist in the register. For existing
  166. * PGSZID<w> fields, zero selects the first element of the
  167. * page-size array. For nonexistant PGSZID<w> fields, zero is
  168. * the best value to write. Also, when changing PGSZID<w>
  169. * fields, the corresponding TLB must be flushed.
  170. */
  171. set_itlbcfg_register (0);
  172. set_dtlbcfg_register (0);
  173. flush_tlb_all ();
  174. /* Set rasid register to a known value. */
  175. set_rasid_register (ASID_ALL_RESERVED);
  176. /* Set PTEVADDR special register to the start of the page
  177. * table, which is in kernel mappable space (ie. not
  178. * statically mapped). This register's value is undefined on
  179. * reset.
  180. */
  181. set_ptevaddr_register (PGTABLE_START);
  182. }
  183. /*
  184. * Initialize memory pages.
  185. */
  186. void __init mem_init(void)
  187. {
  188. unsigned long codesize, reservedpages, datasize, initsize;
  189. unsigned long highmemsize, tmp, ram;
  190. max_mapnr = num_physpages = max_low_pfn;
  191. high_memory = (void *) __va(max_mapnr << PAGE_SHIFT);
  192. highmemsize = 0;
  193. #if CONFIG_HIGHMEM
  194. #error HIGHGMEM not implemented in init.c
  195. #endif
  196. totalram_pages += free_all_bootmem();
  197. reservedpages = ram = 0;
  198. for (tmp = 0; tmp < max_low_pfn; tmp++) {
  199. ram++;
  200. if (PageReserved(mem_map+tmp))
  201. reservedpages++;
  202. }
  203. codesize = (unsigned long) &_etext - (unsigned long) &_ftext;
  204. datasize = (unsigned long) &_edata - (unsigned long) &_fdata;
  205. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  206. printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, "
  207. "%ldk data, %ldk init %ldk highmem)\n",
  208. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  209. ram << (PAGE_SHIFT-10),
  210. codesize >> 10,
  211. reservedpages << (PAGE_SHIFT-10),
  212. datasize >> 10,
  213. initsize >> 10,
  214. highmemsize >> 10);
  215. }
  216. void
  217. free_reserved_mem(void *start, void *end)
  218. {
  219. for (; start < end; start += PAGE_SIZE) {
  220. ClearPageReserved(virt_to_page(start));
  221. set_page_count(virt_to_page(start), 1);
  222. free_page((unsigned long)start);
  223. totalram_pages++;
  224. }
  225. }
  226. #ifdef CONFIG_BLK_DEV_INITRD
  227. extern int initrd_is_mapped;
  228. void free_initrd_mem(unsigned long start, unsigned long end)
  229. {
  230. if (initrd_is_mapped) {
  231. free_reserved_mem((void*)start, (void*)end);
  232. printk ("Freeing initrd memory: %ldk freed\n",(end-start)>>10);
  233. }
  234. }
  235. #endif
  236. void free_initmem(void)
  237. {
  238. free_reserved_mem(&__init_begin, &__init_end);
  239. printk("Freeing unused kernel memory: %dk freed\n",
  240. (&__init_end - &__init_begin) >> 10);
  241. }
  242. void show_mem(void)
  243. {
  244. int i, free = 0, total = 0, reserved = 0;
  245. int shared = 0, cached = 0;
  246. printk("Mem-info:\n");
  247. show_free_areas();
  248. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  249. i = max_mapnr;
  250. while (i-- > 0) {
  251. total++;
  252. if (PageReserved(mem_map+i))
  253. reserved++;
  254. else if (PageSwapCache(mem_map+i))
  255. cached++;
  256. else if (!page_count(mem_map + i))
  257. free++;
  258. else
  259. shared += page_count(mem_map + i) - 1;
  260. }
  261. printk("%d pages of RAM\n", total);
  262. printk("%d reserved pages\n", reserved);
  263. printk("%d pages shared\n", shared);
  264. printk("%d pages swap cached\n",cached);
  265. printk("%d free pages\n", free);
  266. }
  267. /* ------------------------------------------------------------------------- */
  268. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  269. /*
  270. * With cache aliasing, the page color of the page in kernel space and user
  271. * space might mismatch. We temporarily map the page to a different virtual
  272. * address with the same color and clear the page there.
  273. */
  274. void clear_user_page(void *kaddr, unsigned long vaddr, struct page* page)
  275. {
  276. /* There shouldn't be any entries for this page. */
  277. __flush_invalidate_dcache_page_phys(__pa(page_address(page)));
  278. if (!PAGE_COLOR_EQ(vaddr, kaddr)) {
  279. unsigned long v, p;
  280. /* Temporarily map page to DTLB_WAY_DCACHE_ALIAS0. */
  281. spin_lock(&tlb_lock);
  282. p = (unsigned long)pte_val((mk_pte(page,PAGE_KERNEL)));
  283. kaddr = (void*)PAGE_COLOR_MAP0(vaddr);
  284. v = (unsigned long)kaddr | DTLB_WAY_DCACHE_ALIAS0;
  285. __asm__ __volatile__("wdtlb %0,%1; dsync" : :"a" (p), "a" (v));
  286. clear_page(kaddr);
  287. spin_unlock(&tlb_lock);
  288. } else {
  289. clear_page(kaddr);
  290. }
  291. /* We need to make sure that i$ and d$ are coherent. */
  292. clear_bit(PG_cache_clean, &page->flags);
  293. }
  294. /*
  295. * With cache aliasing, we have to make sure that the page color of the page
  296. * in kernel space matches that of the virtual user address before we read
  297. * the page. If the page color differ, we create a temporary DTLB entry with
  298. * the corrent page color and use this 'temporary' address as the source.
  299. * We then use the same approach as in clear_user_page and copy the data
  300. * to the kernel space and clear the PG_cache_clean bit to synchronize caches
  301. * later.
  302. *
  303. * Note:
  304. * Instead of using another 'way' for the temporary DTLB entry, we could
  305. * probably use the same entry that points to the kernel address (after
  306. * saving the original value and restoring it when we are done).
  307. */
  308. void copy_user_page(void* to, void* from, unsigned long vaddr,
  309. struct page* to_page)
  310. {
  311. /* There shouldn't be any entries for the new page. */
  312. __flush_invalidate_dcache_page_phys(__pa(page_address(to_page)));
  313. spin_lock(&tlb_lock);
  314. if (!PAGE_COLOR_EQ(vaddr, from)) {
  315. unsigned long v, p, t;
  316. __asm__ __volatile__ ("pdtlb %1,%2; rdtlb1 %0,%1"
  317. : "=a"(p), "=a"(t) : "a"(from));
  318. from = (void*)PAGE_COLOR_MAP0(vaddr);
  319. v = (unsigned long)from | DTLB_WAY_DCACHE_ALIAS0;
  320. __asm__ __volatile__ ("wdtlb %0,%1; dsync" ::"a" (p), "a" (v));
  321. }
  322. if (!PAGE_COLOR_EQ(vaddr, to)) {
  323. unsigned long v, p;
  324. p = (unsigned long)pte_val((mk_pte(to_page,PAGE_KERNEL)));
  325. to = (void*)PAGE_COLOR_MAP1(vaddr);
  326. v = (unsigned long)to | DTLB_WAY_DCACHE_ALIAS1;
  327. __asm__ __volatile__ ("wdtlb %0,%1; dsync" ::"a" (p), "a" (v));
  328. }
  329. copy_page(to, from);
  330. spin_unlock(&tlb_lock);
  331. /* We need to make sure that i$ and d$ are coherent. */
  332. clear_bit(PG_cache_clean, &to_page->flags);
  333. }
  334. /*
  335. * Any time the kernel writes to a user page cache page, or it is about to
  336. * read from a page cache page this routine is called.
  337. *
  338. * Note:
  339. * The kernel currently only provides one architecture bit in the page
  340. * flags that we use for I$/D$ coherency. Maybe, in future, we can
  341. * use a sepearte bit for deferred dcache aliasing:
  342. * If the page is not mapped yet, we only need to set a flag,
  343. * if mapped, we need to invalidate the page.
  344. */
  345. // FIXME: we probably need this for WB caches not only for Page Coloring..
  346. void flush_dcache_page(struct page *page)
  347. {
  348. unsigned long addr = __pa(page_address(page));
  349. struct address_space *mapping = page_mapping(page);
  350. __flush_invalidate_dcache_page_phys(addr);
  351. if (!test_bit(PG_cache_clean, &page->flags))
  352. return;
  353. /* If this page hasn't been mapped, yet, handle I$/D$ coherency later.*/
  354. #if 0
  355. if (mapping && !mapping_mapped(mapping))
  356. clear_bit(PG_cache_clean, &page->flags);
  357. else
  358. #endif
  359. __invalidate_icache_page_phys(addr);
  360. }
  361. void flush_cache_range(struct vm_area_struct* vma, unsigned long s,
  362. unsigned long e)
  363. {
  364. __flush_invalidate_cache_all();
  365. }
  366. void flush_cache_page(struct vm_area_struct* vma, unsigned long address,
  367. unsigned long pfn)
  368. {
  369. struct page *page = pfn_to_page(pfn);
  370. /* Remove any entry for the old mapping. */
  371. if (current->active_mm == vma->vm_mm) {
  372. unsigned long addr = __pa(page_address(page));
  373. __flush_invalidate_dcache_page_phys(addr);
  374. if ((vma->vm_flags & VM_EXEC) != 0)
  375. __invalidate_icache_page_phys(addr);
  376. } else {
  377. BUG();
  378. }
  379. }
  380. #endif /* (DCACHE_WAY_SIZE > PAGE_SIZE) */
  381. pte_t* pte_alloc_one_kernel (struct mm_struct* mm, unsigned long addr)
  382. {
  383. pte_t* pte = (pte_t*)__get_free_pages(GFP_KERNEL|__GFP_REPEAT, 0);
  384. if (likely(pte)) {
  385. pte_t* ptep = (pte_t*)(pte_val(*pte) + PAGE_OFFSET);
  386. int i;
  387. for (i = 0; i < 1024; i++, ptep++)
  388. pte_clear(mm, addr, ptep);
  389. }
  390. return pte;
  391. }
  392. struct page* pte_alloc_one(struct mm_struct *mm, unsigned long addr)
  393. {
  394. struct page *page;
  395. page = alloc_pages(GFP_KERNEL | __GFP_REPEAT, 0);
  396. if (likely(page)) {
  397. pte_t* ptep = kmap_atomic(page, KM_USER0);
  398. int i;
  399. for (i = 0; i < 1024; i++, ptep++)
  400. pte_clear(mm, addr, ptep);
  401. kunmap_atomic(ptep, KM_USER0);
  402. }
  403. return page;
  404. }
  405. /*
  406. * Handle D$/I$ coherency.
  407. *
  408. * Note:
  409. * We only have one architecture bit for the page flags, so we cannot handle
  410. * cache aliasing, yet.
  411. */
  412. void
  413. update_mmu_cache(struct vm_area_struct * vma, unsigned long addr, pte_t pte)
  414. {
  415. unsigned long pfn = pte_pfn(pte);
  416. struct page *page;
  417. unsigned long vaddr = addr & PAGE_MASK;
  418. if (!pfn_valid(pfn))
  419. return;
  420. page = pfn_to_page(pfn);
  421. invalidate_itlb_mapping(addr);
  422. invalidate_dtlb_mapping(addr);
  423. /* We have a new mapping. Use it. */
  424. write_dtlb_entry(pte, dtlb_probe(addr));
  425. /* If the processor can execute from this page, synchronize D$/I$. */
  426. if ((vma->vm_flags & VM_EXEC) != 0) {
  427. write_itlb_entry(pte, itlb_probe(addr));
  428. /* Synchronize caches, if not clean. */
  429. if (!test_and_set_bit(PG_cache_clean, &page->flags)) {
  430. __flush_dcache_page(vaddr);
  431. __invalidate_icache_page(vaddr);
  432. }
  433. }
  434. }