devtree.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * linux/arch/arm/kernel/devtree.c
  3. *
  4. * Copyright (C) 2009 Canonical Ltd. <jeremy.kerr@canonical.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/export.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/memblock.h>
  16. #include <linux/of.h>
  17. #include <linux/of_fdt.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <asm/setup.h>
  21. #include <asm/page.h>
  22. #include <asm/mach/arch.h>
  23. #include <asm/mach-types.h>
  24. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  25. {
  26. #ifndef CONFIG_ARM_LPAE
  27. if (base > ((phys_addr_t)~0)) {
  28. pr_crit("Ignoring memory at 0x%08llx due to lack of LPAE support\n",
  29. base);
  30. return;
  31. }
  32. if (size > ((phys_addr_t)~0))
  33. size = ((phys_addr_t)~0);
  34. /* arm_add_memory() already checks for the case of base + size > 4GB */
  35. #endif
  36. arm_add_memory(base, size);
  37. }
  38. void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
  39. {
  40. return alloc_bootmem_align(size, align);
  41. }
  42. void __init arm_dt_memblock_reserve(void)
  43. {
  44. u64 *reserve_map, base, size;
  45. if (!initial_boot_params)
  46. return;
  47. /* Reserve the dtb region */
  48. memblock_reserve(virt_to_phys(initial_boot_params),
  49. be32_to_cpu(initial_boot_params->totalsize));
  50. /*
  51. * Process the reserve map. This will probably overlap the initrd
  52. * and dtb locations which are already reserved, but overlaping
  53. * doesn't hurt anything
  54. */
  55. reserve_map = ((void*)initial_boot_params) +
  56. be32_to_cpu(initial_boot_params->off_mem_rsvmap);
  57. while (1) {
  58. base = be64_to_cpup(reserve_map++);
  59. size = be64_to_cpup(reserve_map++);
  60. if (!size)
  61. break;
  62. memblock_reserve(base, size);
  63. }
  64. }
  65. /**
  66. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  67. * @dt_phys: physical address of dt blob
  68. *
  69. * If a dtb was passed to the kernel in r2, then use it to choose the
  70. * correct machine_desc and to setup the system.
  71. */
  72. struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys)
  73. {
  74. struct boot_param_header *devtree;
  75. struct machine_desc *mdesc, *mdesc_best = NULL;
  76. unsigned int score, mdesc_score = ~1;
  77. unsigned long dt_root;
  78. const char *model;
  79. if (!dt_phys)
  80. return NULL;
  81. devtree = phys_to_virt(dt_phys);
  82. /* check device tree validity */
  83. if (be32_to_cpu(devtree->magic) != OF_DT_HEADER)
  84. return NULL;
  85. /* Search the mdescs for the 'best' compatible value match */
  86. initial_boot_params = devtree;
  87. dt_root = of_get_flat_dt_root();
  88. for_each_machine_desc(mdesc) {
  89. score = of_flat_dt_match(dt_root, mdesc->dt_compat);
  90. if (score > 0 && score < mdesc_score) {
  91. mdesc_best = mdesc;
  92. mdesc_score = score;
  93. }
  94. }
  95. if (!mdesc_best) {
  96. const char *prop;
  97. long size;
  98. early_print("\nError: unrecognized/unsupported "
  99. "device tree compatible list:\n[ ");
  100. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  101. while (size > 0) {
  102. early_print("'%s' ", prop);
  103. size -= strlen(prop) + 1;
  104. prop += strlen(prop) + 1;
  105. }
  106. early_print("]\n\n");
  107. dump_machine_table(); /* does not return */
  108. }
  109. model = of_get_flat_dt_prop(dt_root, "model", NULL);
  110. if (!model)
  111. model = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  112. if (!model)
  113. model = "<unknown>";
  114. pr_info("Machine: %s, model: %s\n", mdesc_best->name, model);
  115. /* Retrieve various information from the /chosen node */
  116. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  117. /* Initialize {size,address}-cells info */
  118. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  119. /* Setup memory, calling early_init_dt_add_memory_arch */
  120. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  121. /* Change machine number to match the mdesc we're using */
  122. __machine_arch_type = mdesc_best->nr;
  123. return mdesc_best;
  124. }