cpu.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <cpu.h>
  7. #include <dm.h>
  8. #include <init.h>
  9. #include <log.h>
  10. #include <asm/encoding.h>
  11. #include <dm/uclass-internal.h>
  12. #include <linux/bitops.h>
  13. /*
  14. * The variables here must be stored in the data section since they are used
  15. * before the bss section is available.
  16. */
  17. #ifdef CONFIG_OF_PRIOR_STAGE
  18. phys_addr_t prior_stage_fdt_address __section(".data");
  19. #endif
  20. #ifndef CONFIG_XIP
  21. u32 hart_lottery __section(".data") = 0;
  22. /*
  23. * The main hart running U-Boot has acquired available_harts_lock until it has
  24. * finished initialization of global data.
  25. */
  26. u32 available_harts_lock = 1;
  27. #endif
  28. static inline bool supports_extension(char ext)
  29. {
  30. #ifdef CONFIG_CPU
  31. struct udevice *dev;
  32. char desc[32];
  33. uclass_find_first_device(UCLASS_CPU, &dev);
  34. if (!dev) {
  35. debug("unable to find the RISC-V cpu device\n");
  36. return false;
  37. }
  38. if (!cpu_get_desc(dev, desc, sizeof(desc))) {
  39. /* skip the first 4 characters (rv32|rv64) */
  40. if (strchr(desc + 4, ext))
  41. return true;
  42. }
  43. return false;
  44. #else /* !CONFIG_CPU */
  45. #if CONFIG_IS_ENABLED(RISCV_MMODE)
  46. return csr_read(CSR_MISA) & (1 << (ext - 'a'));
  47. #else /* !CONFIG_IS_ENABLED(RISCV_MMODE) */
  48. #warning "There is no way to determine the available extensions in S-mode."
  49. #warning "Please convert your board to use the RISC-V CPU driver."
  50. return false;
  51. #endif /* CONFIG_IS_ENABLED(RISCV_MMODE) */
  52. #endif /* CONFIG_CPU */
  53. }
  54. static int riscv_cpu_probe(void)
  55. {
  56. #ifdef CONFIG_CPU
  57. int ret;
  58. /* probe cpus so that RISC-V timer can be bound */
  59. ret = cpu_probe_all();
  60. if (ret)
  61. return log_msg_ret("RISC-V cpus probe failed\n", ret);
  62. #endif
  63. return 0;
  64. }
  65. /*
  66. * This is called on secondary harts just after the IPI is init'd. Currently
  67. * there's nothing to do, since we just need to clear any existing IPIs, and
  68. * that is handled by the sending of an ipi itself.
  69. */
  70. #if CONFIG_IS_ENABLED(SMP)
  71. static void dummy_pending_ipi_clear(ulong hart, ulong arg0, ulong arg1)
  72. {
  73. }
  74. #endif
  75. int arch_cpu_init_dm(void)
  76. {
  77. int ret;
  78. ret = riscv_cpu_probe();
  79. if (ret)
  80. return ret;
  81. /* Enable FPU */
  82. if (supports_extension('d') || supports_extension('f')) {
  83. csr_set(MODE_PREFIX(status), MSTATUS_FS);
  84. csr_write(CSR_FCSR, 0);
  85. }
  86. if (CONFIG_IS_ENABLED(RISCV_MMODE)) {
  87. /*
  88. * Enable perf counters for cycle, time,
  89. * and instret counters only
  90. */
  91. #ifdef CONFIG_RISCV_PRIV_1_9
  92. csr_write(CSR_MSCOUNTEREN, GENMASK(2, 0));
  93. csr_write(CSR_MUCOUNTEREN, GENMASK(2, 0));
  94. #else
  95. csr_write(CSR_MCOUNTEREN, GENMASK(2, 0));
  96. #endif
  97. /* Disable paging */
  98. if (supports_extension('s'))
  99. #ifdef CONFIG_RISCV_PRIV_1_9
  100. csr_read_clear(CSR_MSTATUS, SR_VM);
  101. #else
  102. csr_write(CSR_SATP, 0);
  103. #endif
  104. }
  105. #if CONFIG_IS_ENABLED(SMP)
  106. ret = riscv_init_ipi();
  107. if (ret)
  108. return ret;
  109. /*
  110. * Clear all pending IPIs on secondary harts. We don't do anything on
  111. * the boot hart, since we never send an IPI to ourselves, and no
  112. * interrupts are enabled
  113. */
  114. ret = smp_call_function((ulong)dummy_pending_ipi_clear, 0, 0, 0);
  115. if (ret)
  116. return ret;
  117. #endif
  118. return 0;
  119. }
  120. int arch_early_init_r(void)
  121. {
  122. return riscv_cpu_probe();
  123. }
  124. /**
  125. * harts_early_init() - A callback function called by start.S to configure
  126. * feature settings of each hart.
  127. *
  128. * In a multi-core system, memory access shall be careful here, it shall
  129. * take care of race conditions.
  130. */
  131. __weak void harts_early_init(void)
  132. {
  133. }