init.c 13 KB

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