physmem.c 5.7 KB

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