cboot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2016-2018, NVIDIA CORPORATION.
  4. */
  5. #include <common.h>
  6. #include <env.h>
  7. #include <fdt_support.h>
  8. #include <fdtdec.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <linux/ctype.h>
  12. #include <linux/sizes.h>
  13. #include <asm/arch/tegra.h>
  14. #include <asm/arch-tegra/cboot.h>
  15. #include <asm/armv8/mmu.h>
  16. /*
  17. * Size of a region that's large enough to hold the relocated U-Boot and all
  18. * other allocations made around it (stack, heap, page tables, etc.)
  19. * In practice, running "bdinfo" at the shell prompt, the stack reaches about
  20. * 5MB from the address selected for ram_top as of the time of writing,
  21. * so a 16MB region should be plenty.
  22. */
  23. #define MIN_USABLE_RAM_SIZE SZ_16M
  24. /*
  25. * The amount of space we expect to require for stack usage. Used to validate
  26. * that all reservations fit into the region selected for the relocation target
  27. */
  28. #define MIN_USABLE_STACK_SIZE SZ_1M
  29. DECLARE_GLOBAL_DATA_PTR;
  30. extern struct mm_region tegra_mem_map[];
  31. /*
  32. * These variables are written to before relocation, and hence cannot be
  33. * in.bss, since .bss overlaps the DTB that's appended to the U-Boot binary.
  34. * The section attribute forces this into .data and avoids this issue. This
  35. * also has the nice side-effect of the content being valid after relocation.
  36. */
  37. /* The number of valid entries in ram_banks[] */
  38. static int ram_bank_count __attribute__((section(".data")));
  39. /*
  40. * The usable top-of-RAM for U-Boot. This is both:
  41. * a) Below 4GB to avoid issues with peripherals that use 32-bit addressing.
  42. * b) At the end of a region that has enough space to hold the relocated U-Boot
  43. * and all other allocations made around it (stack, heap, page tables, etc.)
  44. */
  45. static u64 ram_top __attribute__((section(".data")));
  46. /* The base address of the region of RAM that ends at ram_top */
  47. static u64 region_base __attribute__((section(".data")));
  48. /*
  49. * Explicitly put this in the .data section because it is written before the
  50. * .bss section is zeroed out but it needs to persist.
  51. */
  52. unsigned long cboot_boot_x0 __attribute__((section(".data")));
  53. void cboot_save_boot_params(unsigned long x0, unsigned long x1,
  54. unsigned long x2, unsigned long x3)
  55. {
  56. cboot_boot_x0 = x0;
  57. }
  58. int cboot_dram_init(void)
  59. {
  60. unsigned int na, ns;
  61. const void *cboot_blob = (void *)cboot_boot_x0;
  62. int node, len, i;
  63. const u32 *prop;
  64. if (!cboot_blob)
  65. return -EINVAL;
  66. na = fdtdec_get_uint(cboot_blob, 0, "#address-cells", 2);
  67. ns = fdtdec_get_uint(cboot_blob, 0, "#size-cells", 2);
  68. node = fdt_path_offset(cboot_blob, "/memory");
  69. if (node < 0) {
  70. pr_err("Can't find /memory node in cboot DTB");
  71. hang();
  72. }
  73. prop = fdt_getprop(cboot_blob, node, "reg", &len);
  74. if (!prop) {
  75. pr_err("Can't find /memory/reg property in cboot DTB");
  76. hang();
  77. }
  78. /* Calculate the true # of base/size pairs to read */
  79. len /= 4; /* Convert bytes to number of cells */
  80. len /= (na + ns); /* Convert cells to number of banks */
  81. if (len > CONFIG_NR_DRAM_BANKS)
  82. len = CONFIG_NR_DRAM_BANKS;
  83. /* Parse the /memory node, and save useful entries */
  84. gd->ram_size = 0;
  85. ram_bank_count = 0;
  86. for (i = 0; i < len; i++) {
  87. u64 bank_start, bank_end, bank_size, usable_bank_size;
  88. /* Extract raw memory region data from DTB */
  89. bank_start = fdt_read_number(prop, na);
  90. prop += na;
  91. bank_size = fdt_read_number(prop, ns);
  92. prop += ns;
  93. gd->ram_size += bank_size;
  94. bank_end = bank_start + bank_size;
  95. debug("Bank %d: %llx..%llx (+%llx)\n", i,
  96. bank_start, bank_end, bank_size);
  97. /*
  98. * Align the bank to MMU section size. This is not strictly
  99. * necessary, since the translation table construction code
  100. * handles page granularity without issue. However, aligning
  101. * the MMU entries reduces the size and number of levels in the
  102. * page table, so is worth it.
  103. */
  104. bank_start = ROUND(bank_start, SZ_2M);
  105. bank_end = bank_end & ~(SZ_2M - 1);
  106. bank_size = bank_end - bank_start;
  107. debug(" aligned: %llx..%llx (+%llx)\n",
  108. bank_start, bank_end, bank_size);
  109. if (bank_end <= bank_start)
  110. continue;
  111. /* Record data used to create MMU translation tables */
  112. ram_bank_count++;
  113. /* Index below is deliberately 1-based to skip MMIO entry */
  114. tegra_mem_map[ram_bank_count].virt = bank_start;
  115. tegra_mem_map[ram_bank_count].phys = bank_start;
  116. tegra_mem_map[ram_bank_count].size = bank_size;
  117. tegra_mem_map[ram_bank_count].attrs =
  118. PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_INNER_SHARE;
  119. /* Determine best bank to relocate U-Boot into */
  120. if (bank_end > SZ_4G)
  121. bank_end = SZ_4G;
  122. debug(" end %llx (usable)\n", bank_end);
  123. usable_bank_size = bank_end - bank_start;
  124. debug(" size %llx (usable)\n", usable_bank_size);
  125. if ((usable_bank_size >= MIN_USABLE_RAM_SIZE) &&
  126. (bank_end > ram_top)) {
  127. ram_top = bank_end;
  128. region_base = bank_start;
  129. debug("ram top now %llx\n", ram_top);
  130. }
  131. }
  132. /* Ensure memory map contains the desired sentinel entry */
  133. tegra_mem_map[ram_bank_count + 1].virt = 0;
  134. tegra_mem_map[ram_bank_count + 1].phys = 0;
  135. tegra_mem_map[ram_bank_count + 1].size = 0;
  136. tegra_mem_map[ram_bank_count + 1].attrs = 0;
  137. /* Error out if a relocation target couldn't be found */
  138. if (!ram_top) {
  139. pr_err("Can't find a usable RAM top");
  140. hang();
  141. }
  142. return 0;
  143. }
  144. int cboot_dram_init_banksize(void)
  145. {
  146. int i;
  147. if (ram_bank_count == 0)
  148. return -EINVAL;
  149. if ((gd->start_addr_sp - region_base) < MIN_USABLE_STACK_SIZE) {
  150. pr_err("Reservations exceed chosen region size");
  151. hang();
  152. }
  153. for (i = 0; i < ram_bank_count; i++) {
  154. gd->bd->bi_dram[i].start = tegra_mem_map[1 + i].virt;
  155. gd->bd->bi_dram[i].size = tegra_mem_map[1 + i].size;
  156. }
  157. #ifdef CONFIG_PCI
  158. gd->pci_ram_top = ram_top;
  159. #endif
  160. return 0;
  161. }
  162. ulong cboot_get_usable_ram_top(ulong total_size)
  163. {
  164. return ram_top;
  165. }
  166. /*
  167. * The following few functions run late during the boot process and dynamically
  168. * calculate the load address of various binaries. To keep track of multiple
  169. * allocations, some writable list of RAM banks must be used. tegra_mem_map[]
  170. * is used for this purpose to avoid making yet another copy of the list of RAM
  171. * banks. This is safe because tegra_mem_map[] is only used once during very
  172. * early boot to create U-Boot's page tables, long before this code runs. If
  173. * this assumption becomes invalid later, we can just fix the code to copy the
  174. * list of RAM banks into some private data structure before running.
  175. */
  176. static char *gen_varname(const char *var, const char *ext)
  177. {
  178. size_t len_var = strlen(var);
  179. size_t len_ext = strlen(ext);
  180. size_t len = len_var + len_ext + 1;
  181. char *varext = malloc(len);
  182. if (!varext)
  183. return 0;
  184. strcpy(varext, var);
  185. strcpy(varext + len_var, ext);
  186. return varext;
  187. }
  188. static void mark_ram_allocated(int bank, u64 allocated_start, u64 allocated_end)
  189. {
  190. u64 bank_start = tegra_mem_map[bank].virt;
  191. u64 bank_size = tegra_mem_map[bank].size;
  192. u64 bank_end = bank_start + bank_size;
  193. bool keep_front = allocated_start != bank_start;
  194. bool keep_tail = allocated_end != bank_end;
  195. if (keep_front && keep_tail) {
  196. /*
  197. * There are CONFIG_NR_DRAM_BANKS DRAM entries in the array,
  198. * starting at index 1 (index 0 is MMIO). So, we are at DRAM
  199. * entry "bank" not "bank - 1" as for a typical 0-base array.
  200. * The number of remaining DRAM entries is therefore
  201. * "CONFIG_NR_DRAM_BANKS - bank". We want to duplicate the
  202. * current entry and shift up the remaining entries, dropping
  203. * the last one. Thus, we must copy one fewer entry than the
  204. * number remaining.
  205. */
  206. memmove(&tegra_mem_map[bank + 1], &tegra_mem_map[bank],
  207. CONFIG_NR_DRAM_BANKS - bank - 1);
  208. tegra_mem_map[bank].size = allocated_start - bank_start;
  209. bank++;
  210. tegra_mem_map[bank].virt = allocated_end;
  211. tegra_mem_map[bank].phys = allocated_end;
  212. tegra_mem_map[bank].size = bank_end - allocated_end;
  213. } else if (keep_front) {
  214. tegra_mem_map[bank].size = allocated_start - bank_start;
  215. } else if (keep_tail) {
  216. tegra_mem_map[bank].virt = allocated_end;
  217. tegra_mem_map[bank].phys = allocated_end;
  218. tegra_mem_map[bank].size = bank_end - allocated_end;
  219. } else {
  220. /*
  221. * We could move all subsequent banks down in the array but
  222. * that's not necessary for subsequent allocations to work, so
  223. * we skip doing so.
  224. */
  225. tegra_mem_map[bank].size = 0;
  226. }
  227. }
  228. static void reserve_ram(u64 start, u64 size)
  229. {
  230. int bank;
  231. u64 end = start + size;
  232. for (bank = 1; bank <= CONFIG_NR_DRAM_BANKS; bank++) {
  233. u64 bank_start = tegra_mem_map[bank].virt;
  234. u64 bank_size = tegra_mem_map[bank].size;
  235. u64 bank_end = bank_start + bank_size;
  236. if (end <= bank_start || start > bank_end)
  237. continue;
  238. mark_ram_allocated(bank, start, end);
  239. break;
  240. }
  241. }
  242. static u64 alloc_ram(u64 size, u64 align, u64 offset)
  243. {
  244. int bank;
  245. for (bank = 1; bank <= CONFIG_NR_DRAM_BANKS; bank++) {
  246. u64 bank_start = tegra_mem_map[bank].virt;
  247. u64 bank_size = tegra_mem_map[bank].size;
  248. u64 bank_end = bank_start + bank_size;
  249. u64 allocated = ROUND(bank_start, align) + offset;
  250. u64 allocated_end = allocated + size;
  251. if (allocated_end > bank_end)
  252. continue;
  253. mark_ram_allocated(bank, allocated, allocated_end);
  254. return allocated;
  255. }
  256. return 0;
  257. }
  258. static void set_calculated_aliases(char *aliases, u64 address)
  259. {
  260. char *tmp, *alias;
  261. int err;
  262. aliases = strdup(aliases);
  263. if (!aliases) {
  264. pr_err("strdup(aliases) failed");
  265. return;
  266. }
  267. tmp = aliases;
  268. while (true) {
  269. alias = strsep(&tmp, " ");
  270. if (!alias)
  271. break;
  272. debug("%s: alias: %s\n", __func__, alias);
  273. err = env_set_hex(alias, address);
  274. if (err)
  275. pr_err("Could not set %s\n", alias);
  276. }
  277. free(aliases);
  278. }
  279. static void set_calculated_env_var(const char *var)
  280. {
  281. char *var_size;
  282. char *var_align;
  283. char *var_offset;
  284. char *var_aliases;
  285. u64 size;
  286. u64 align;
  287. u64 offset;
  288. char *aliases;
  289. u64 address;
  290. int err;
  291. var_size = gen_varname(var, "_size");
  292. if (!var_size)
  293. return;
  294. var_align = gen_varname(var, "_align");
  295. if (!var_align)
  296. goto out_free_var_size;
  297. var_offset = gen_varname(var, "_offset");
  298. if (!var_offset)
  299. goto out_free_var_align;
  300. var_aliases = gen_varname(var, "_aliases");
  301. if (!var_aliases)
  302. goto out_free_var_offset;
  303. size = env_get_hex(var_size, 0);
  304. if (!size) {
  305. pr_err("%s not set or zero\n", var_size);
  306. goto out_free_var_aliases;
  307. }
  308. align = env_get_hex(var_align, 1);
  309. /* Handle extant variables, but with a value of 0 */
  310. if (!align)
  311. align = 1;
  312. offset = env_get_hex(var_offset, 0);
  313. aliases = env_get(var_aliases);
  314. debug("%s: Calc var %s; size=%llx, align=%llx, offset=%llx\n",
  315. __func__, var, size, align, offset);
  316. if (aliases)
  317. debug("%s: Aliases: %s\n", __func__, aliases);
  318. address = alloc_ram(size, align, offset);
  319. if (!address) {
  320. pr_err("Could not allocate %s\n", var);
  321. goto out_free_var_aliases;
  322. }
  323. debug("%s: Address %llx\n", __func__, address);
  324. err = env_set_hex(var, address);
  325. if (err)
  326. pr_err("Could not set %s\n", var);
  327. if (aliases)
  328. set_calculated_aliases(aliases, address);
  329. out_free_var_aliases:
  330. free(var_aliases);
  331. out_free_var_offset:
  332. free(var_offset);
  333. out_free_var_align:
  334. free(var_align);
  335. out_free_var_size:
  336. free(var_size);
  337. }
  338. #ifdef DEBUG
  339. static void dump_ram_banks(void)
  340. {
  341. int bank;
  342. for (bank = 1; bank <= CONFIG_NR_DRAM_BANKS; bank++) {
  343. u64 bank_start = tegra_mem_map[bank].virt;
  344. u64 bank_size = tegra_mem_map[bank].size;
  345. u64 bank_end = bank_start + bank_size;
  346. if (!bank_size)
  347. continue;
  348. printf("%d: %010llx..%010llx (+%010llx)\n", bank - 1,
  349. bank_start, bank_end, bank_size);
  350. }
  351. }
  352. #endif
  353. static void set_calculated_env_vars(void)
  354. {
  355. char *vars, *tmp, *var;
  356. #ifdef DEBUG
  357. printf("RAM banks before any calculated env. var.s:\n");
  358. dump_ram_banks();
  359. #endif
  360. reserve_ram(cboot_boot_x0, fdt_totalsize(cboot_boot_x0));
  361. #ifdef DEBUG
  362. printf("RAM after reserving cboot DTB:\n");
  363. dump_ram_banks();
  364. #endif
  365. vars = env_get("calculated_vars");
  366. if (!vars) {
  367. debug("%s: No env var calculated_vars\n", __func__);
  368. return;
  369. }
  370. vars = strdup(vars);
  371. if (!vars) {
  372. pr_err("strdup(calculated_vars) failed");
  373. return;
  374. }
  375. tmp = vars;
  376. while (true) {
  377. var = strsep(&tmp, " ");
  378. if (!var)
  379. break;
  380. debug("%s: var: %s\n", __func__, var);
  381. set_calculated_env_var(var);
  382. #ifdef DEBUG
  383. printf("RAM banks after allocating %s:\n", var);
  384. dump_ram_banks();
  385. #endif
  386. }
  387. free(vars);
  388. }
  389. static int set_fdt_addr(void)
  390. {
  391. int ret;
  392. ret = env_set_hex("fdt_addr", cboot_boot_x0);
  393. if (ret) {
  394. printf("Failed to set fdt_addr to point at DTB: %d\n", ret);
  395. return ret;
  396. }
  397. return 0;
  398. }
  399. /*
  400. * Attempt to use /chosen/nvidia,ether-mac in the cboot DTB to U-Boot's
  401. * ethaddr environment variable if possible.
  402. */
  403. static int cboot_get_ethaddr_legacy(const void *fdt, uint8_t mac[ETH_ALEN])
  404. {
  405. const char *const properties[] = {
  406. "nvidia,ethernet-mac",
  407. "nvidia,ether-mac",
  408. };
  409. const char *prop;
  410. unsigned int i;
  411. int node, len;
  412. node = fdt_path_offset(fdt, "/chosen");
  413. if (node < 0) {
  414. printf("Can't find /chosen node in cboot DTB\n");
  415. return node;
  416. }
  417. for (i = 0; i < ARRAY_SIZE(properties); i++) {
  418. prop = fdt_getprop(fdt, node, properties[i], &len);
  419. if (prop)
  420. break;
  421. }
  422. if (!prop) {
  423. printf("Can't find Ethernet MAC address in cboot DTB\n");
  424. return -ENOENT;
  425. }
  426. string_to_enetaddr(prop, mac);
  427. if (!is_valid_ethaddr(mac)) {
  428. printf("Invalid MAC address: %s\n", prop);
  429. return -EINVAL;
  430. }
  431. debug("Legacy MAC address: %pM\n", mac);
  432. return 0;
  433. }
  434. int cboot_get_ethaddr(const void *fdt, uint8_t mac[ETH_ALEN])
  435. {
  436. int node, len, err = 0;
  437. const uchar *prop;
  438. const char *path;
  439. path = fdt_get_alias(fdt, "ethernet");
  440. if (!path) {
  441. err = -ENOENT;
  442. goto out;
  443. }
  444. debug("ethernet alias found: %s\n", path);
  445. node = fdt_path_offset(fdt, path);
  446. if (node < 0) {
  447. err = -ENOENT;
  448. goto out;
  449. }
  450. prop = fdt_getprop(fdt, node, "local-mac-address", &len);
  451. if (!prop) {
  452. err = -ENOENT;
  453. goto out;
  454. }
  455. if (len != ETH_ALEN) {
  456. err = -EINVAL;
  457. goto out;
  458. }
  459. debug("MAC address: %pM\n", prop);
  460. memcpy(mac, prop, ETH_ALEN);
  461. out:
  462. if (err < 0)
  463. err = cboot_get_ethaddr_legacy(fdt, mac);
  464. return err;
  465. }
  466. static char *strip(const char *ptr)
  467. {
  468. const char *end;
  469. while (*ptr && isblank(*ptr))
  470. ptr++;
  471. /* empty string */
  472. if (*ptr == '\0')
  473. return strdup(ptr);
  474. end = ptr;
  475. while (end[1])
  476. end++;
  477. while (isblank(*end))
  478. end--;
  479. return strndup(ptr, end - ptr + 1);
  480. }
  481. static char *cboot_get_bootargs(const void *fdt)
  482. {
  483. const char *args;
  484. int offset, len;
  485. offset = fdt_path_offset(fdt, "/chosen");
  486. if (offset < 0)
  487. return NULL;
  488. args = fdt_getprop(fdt, offset, "bootargs", &len);
  489. if (!args)
  490. return NULL;
  491. return strip(args);
  492. }
  493. int cboot_late_init(void)
  494. {
  495. const void *fdt = (const void *)cboot_boot_x0;
  496. uint8_t mac[ETH_ALEN];
  497. char *bootargs;
  498. int err;
  499. set_calculated_env_vars();
  500. /*
  501. * Ignore errors here; the value may not be used depending on
  502. * extlinux.conf or boot script content.
  503. */
  504. set_fdt_addr();
  505. /* Ignore errors here; not all cases care about Ethernet addresses */
  506. err = cboot_get_ethaddr(fdt, mac);
  507. if (!err) {
  508. void *blob = (void *)gd->fdt_blob;
  509. err = fdtdec_set_ethernet_mac_address(blob, mac, sizeof(mac));
  510. if (err < 0)
  511. printf("failed to set MAC address %pM: %d\n", mac, err);
  512. }
  513. bootargs = cboot_get_bootargs(fdt);
  514. if (bootargs) {
  515. env_set("cbootargs", bootargs);
  516. free(bootargs);
  517. }
  518. return 0;
  519. }