physmem.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
  3. * Use of this source code is governed by a BSD-style license that can be
  4. * found in the LICENSE file.
  5. *
  6. * Alternatively, this software may be distributed under the terms of the
  7. * GNU General Public License ("GPL") version 2 as published by the Free
  8. * Software Foundation.
  9. */
  10. #include <common.h>
  11. #include <physmem.h>
  12. #include <asm/cpu.h>
  13. #include <linux/compiler.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /* Large pages are 2MB. */
  16. #define LARGE_PAGE_SIZE ((1 << 20) * 2)
  17. /*
  18. * Paging data structures.
  19. */
  20. struct pdpe {
  21. uint64_t p:1;
  22. uint64_t mbz_0:2;
  23. uint64_t pwt:1;
  24. uint64_t pcd:1;
  25. uint64_t mbz_1:4;
  26. uint64_t avl:3;
  27. uint64_t base:40;
  28. uint64_t mbz_2:12;
  29. };
  30. typedef struct pdpe pdpt_t[512];
  31. struct pde {
  32. uint64_t p:1; /* present */
  33. uint64_t rw:1; /* read/write */
  34. uint64_t us:1; /* user/supervisor */
  35. uint64_t pwt:1; /* page-level writethrough */
  36. uint64_t pcd:1; /* page-level cache disable */
  37. uint64_t a:1; /* accessed */
  38. uint64_t d:1; /* dirty */
  39. uint64_t ps:1; /* page size */
  40. uint64_t g:1; /* global page */
  41. uint64_t avl:3; /* available to software */
  42. uint64_t pat:1; /* page-attribute table */
  43. uint64_t mbz_0:8; /* must be zero */
  44. uint64_t base:31; /* base address */
  45. };
  46. typedef struct pde pdt_t[512];
  47. static pdpt_t pdpt __aligned(4096);
  48. static pdt_t pdts[4] __aligned(4096);
  49. /*
  50. * Map a virtual address to a physical address and optionally invalidate any
  51. * old mapping.
  52. *
  53. * @param virt The virtual address to use.
  54. * @param phys The physical address to use.
  55. * @param invlpg Whether to use invlpg to clear any old mappings.
  56. */
  57. static void x86_phys_map_page(uintptr_t virt, phys_addr_t phys, int invlpg)
  58. {
  59. /* Extract the two bit PDPT index and the 9 bit PDT index. */
  60. uintptr_t pdpt_idx = (virt >> 30) & 0x3;
  61. uintptr_t pdt_idx = (virt >> 21) & 0x1ff;
  62. /* Set up a handy pointer to the appropriate PDE. */
  63. struct pde *pde = &(pdts[pdpt_idx][pdt_idx]);
  64. memset(pde, 0, sizeof(struct pde));
  65. pde->p = 1;
  66. pde->rw = 1;
  67. pde->us = 1;
  68. pde->ps = 1;
  69. pde->base = phys >> 21;
  70. if (invlpg) {
  71. /* Flush any stale mapping out of the TLBs. */
  72. __asm__ __volatile__(
  73. "invlpg %0\n\t"
  74. :
  75. : "m" (*(uint8_t *)virt)
  76. );
  77. }
  78. }
  79. /* Identity map the lower 4GB and turn on paging with PAE. */
  80. static void x86_phys_enter_paging(void)
  81. {
  82. phys_addr_t page_addr;
  83. unsigned i;
  84. /* Zero out the page tables. */
  85. memset(pdpt, 0, sizeof(pdpt));
  86. memset(pdts, 0, sizeof(pdts));
  87. /* Set up the PDPT. */
  88. for (i = 0; i < ARRAY_SIZE(pdts); i++) {
  89. pdpt[i].p = 1;
  90. pdpt[i].base = ((uintptr_t)&pdts[i]) >> 12;
  91. }
  92. /* Identity map everything up to 4GB. */
  93. for (page_addr = 0; page_addr < (1ULL << 32);
  94. page_addr += LARGE_PAGE_SIZE) {
  95. /* There's no reason to invalidate the TLB with paging off. */
  96. x86_phys_map_page(page_addr, page_addr, 0);
  97. }
  98. cpu_enable_paging_pae((ulong)pdpt);
  99. }
  100. /* Disable paging and PAE mode. */
  101. static void x86_phys_exit_paging(void)
  102. {
  103. cpu_disable_paging_pae();
  104. }
  105. /*
  106. * Set physical memory to a particular value when the whole region fits on one
  107. * page.
  108. *
  109. * @param map_addr The address that starts the physical page.
  110. * @param offset How far into that page to start setting a value.
  111. * @param c The value to set memory to.
  112. * @param size The size in bytes of the area to set.
  113. */
  114. static void x86_phys_memset_page(phys_addr_t map_addr, uintptr_t offset, int c,
  115. unsigned size)
  116. {
  117. /*
  118. * U-Boot should be far away from the beginning of memory, so that's a
  119. * good place to map our window on top of.
  120. */
  121. const uintptr_t window = LARGE_PAGE_SIZE;
  122. /* Make sure the window is below U-Boot. */
  123. assert(window + LARGE_PAGE_SIZE <
  124. gd->relocaddr - CONFIG_SYS_MALLOC_LEN - CONFIG_SYS_STACK_SIZE);
  125. /* Map the page into the window and then memset the appropriate part. */
  126. x86_phys_map_page(window, map_addr, 1);
  127. memset((void *)(window + offset), c, size);
  128. }
  129. /*
  130. * A physical memory anologue to memset with matching parameters and return
  131. * value.
  132. */
  133. phys_addr_t arch_phys_memset(phys_addr_t start, int c, phys_size_t size)
  134. {
  135. const phys_addr_t max_addr = (phys_addr_t)~(uintptr_t)0;
  136. const phys_addr_t orig_start = start;
  137. if (!size)
  138. return orig_start;
  139. /* Handle memory below 4GB. */
  140. if (start <= max_addr) {
  141. phys_size_t low_size = min(max_addr + 1 - start, size);
  142. void *start_ptr = (void *)(uintptr_t)start;
  143. assert(((phys_addr_t)(uintptr_t)start) == start);
  144. memset(start_ptr, c, low_size);
  145. start += low_size;
  146. size -= low_size;
  147. }
  148. /* Use paging and PAE to handle memory above 4GB up to 64GB. */
  149. if (size) {
  150. phys_addr_t map_addr = start & ~(LARGE_PAGE_SIZE - 1);
  151. phys_addr_t offset = start - map_addr;
  152. x86_phys_enter_paging();
  153. /* Handle the first partial page. */
  154. if (offset) {
  155. phys_addr_t end =
  156. min(map_addr + LARGE_PAGE_SIZE, start + size);
  157. phys_size_t cur_size = end - start;
  158. x86_phys_memset_page(map_addr, offset, c, cur_size);
  159. size -= cur_size;
  160. map_addr += LARGE_PAGE_SIZE;
  161. }
  162. /* Handle the complete pages. */
  163. while (size > LARGE_PAGE_SIZE) {
  164. x86_phys_memset_page(map_addr, 0, c, LARGE_PAGE_SIZE);
  165. size -= LARGE_PAGE_SIZE;
  166. map_addr += LARGE_PAGE_SIZE;
  167. }
  168. /* Handle the last partial page. */
  169. if (size)
  170. x86_phys_memset_page(map_addr, 0, c, size);
  171. x86_phys_exit_paging();
  172. }
  173. return orig_start;
  174. }