cpu_ops_spinwait.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  4. */
  5. #include <linux/errno.h>
  6. #include <linux/of.h>
  7. #include <linux/string.h>
  8. #include <linux/sched/task_stack.h>
  9. #include <asm/cpu_ops.h>
  10. #include <asm/sbi.h>
  11. #include <asm/smp.h>
  12. #include "head.h"
  13. const struct cpu_operations cpu_ops_spinwait;
  14. void *__cpu_spinwait_stack_pointer[NR_CPUS] __section(".data");
  15. void *__cpu_spinwait_task_pointer[NR_CPUS] __section(".data");
  16. static void cpu_update_secondary_bootdata(unsigned int cpuid,
  17. struct task_struct *tidle)
  18. {
  19. unsigned long hartid = cpuid_to_hartid_map(cpuid);
  20. /*
  21. * The hartid must be less than NR_CPUS to avoid out-of-bound access
  22. * errors for __cpu_spinwait_stack/task_pointer. That is not always possible
  23. * for platforms with discontiguous hartid numbering scheme. That's why
  24. * spinwait booting is not the recommended approach for any platforms
  25. * booting Linux in S-mode and can be disabled in the future.
  26. */
  27. if (hartid == INVALID_HARTID || hartid >= (unsigned long) NR_CPUS)
  28. return;
  29. /* Make sure tidle is updated */
  30. smp_mb();
  31. WRITE_ONCE(__cpu_spinwait_stack_pointer[hartid],
  32. task_stack_page(tidle) + THREAD_SIZE);
  33. WRITE_ONCE(__cpu_spinwait_task_pointer[hartid], tidle);
  34. }
  35. static int spinwait_cpu_prepare(unsigned int cpuid)
  36. {
  37. if (!cpu_ops_spinwait.cpu_start) {
  38. pr_err("cpu start method not defined for CPU [%d]\n", cpuid);
  39. return -ENODEV;
  40. }
  41. return 0;
  42. }
  43. static int spinwait_cpu_start(unsigned int cpuid, struct task_struct *tidle)
  44. {
  45. /*
  46. * In this protocol, all cpus boot on their own accord. _start
  47. * selects the first cpu to boot the kernel and causes the remainder
  48. * of the cpus to spin in a loop waiting for their stack pointer to be
  49. * setup by that main cpu. Writing to bootdata
  50. * (i.e __cpu_spinwait_stack_pointer) signals to the spinning cpus that they
  51. * can continue the boot process.
  52. */
  53. cpu_update_secondary_bootdata(cpuid, tidle);
  54. return 0;
  55. }
  56. const struct cpu_operations cpu_ops_spinwait = {
  57. .name = "spinwait",
  58. .cpu_prepare = spinwait_cpu_prepare,
  59. .cpu_start = spinwait_cpu_start,
  60. };