cpu_ops_sbi.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HSM extension and cpu_ops implementation.
  4. *
  5. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  6. */
  7. #include <linux/init.h>
  8. #include <linux/mm.h>
  9. #include <asm/cpu_ops.h>
  10. #include <asm/sbi.h>
  11. #include <asm/smp.h>
  12. extern char secondary_start_sbi[];
  13. const struct cpu_operations cpu_ops_sbi;
  14. static int sbi_hsm_hart_start(unsigned long hartid, unsigned long saddr,
  15. unsigned long priv)
  16. {
  17. struct sbiret ret;
  18. ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_START,
  19. hartid, saddr, priv, 0, 0, 0);
  20. if (ret.error)
  21. return sbi_err_map_linux_errno(ret.error);
  22. else
  23. return 0;
  24. }
  25. #ifdef CONFIG_HOTPLUG_CPU
  26. static int sbi_hsm_hart_stop(void)
  27. {
  28. struct sbiret ret;
  29. ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_STOP, 0, 0, 0, 0, 0, 0);
  30. if (ret.error)
  31. return sbi_err_map_linux_errno(ret.error);
  32. else
  33. return 0;
  34. }
  35. static int sbi_hsm_hart_get_status(unsigned long hartid)
  36. {
  37. struct sbiret ret;
  38. ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_STATUS,
  39. hartid, 0, 0, 0, 0, 0);
  40. if (ret.error)
  41. return sbi_err_map_linux_errno(ret.error);
  42. else
  43. return ret.value;
  44. }
  45. #endif
  46. static int sbi_cpu_start(unsigned int cpuid, struct task_struct *tidle)
  47. {
  48. int rc;
  49. unsigned long boot_addr = __pa_symbol(secondary_start_sbi);
  50. int hartid = cpuid_to_hartid_map(cpuid);
  51. cpu_update_secondary_bootdata(cpuid, tidle);
  52. rc = sbi_hsm_hart_start(hartid, boot_addr, 0);
  53. return rc;
  54. }
  55. static int sbi_cpu_prepare(unsigned int cpuid)
  56. {
  57. if (!cpu_ops_sbi.cpu_start) {
  58. pr_err("cpu start method not defined for CPU [%d]\n", cpuid);
  59. return -ENODEV;
  60. }
  61. return 0;
  62. }
  63. #ifdef CONFIG_HOTPLUG_CPU
  64. static int sbi_cpu_disable(unsigned int cpuid)
  65. {
  66. if (!cpu_ops_sbi.cpu_stop)
  67. return -EOPNOTSUPP;
  68. return 0;
  69. }
  70. static void sbi_cpu_stop(void)
  71. {
  72. int ret;
  73. ret = sbi_hsm_hart_stop();
  74. pr_crit("Unable to stop the cpu %u (%d)\n", smp_processor_id(), ret);
  75. }
  76. static int sbi_cpu_is_stopped(unsigned int cpuid)
  77. {
  78. int rc;
  79. int hartid = cpuid_to_hartid_map(cpuid);
  80. rc = sbi_hsm_hart_get_status(hartid);
  81. if (rc == SBI_HSM_HART_STATUS_STOPPED)
  82. return 0;
  83. return rc;
  84. }
  85. #endif
  86. const struct cpu_operations cpu_ops_sbi = {
  87. .name = "sbi",
  88. .cpu_prepare = sbi_cpu_prepare,
  89. .cpu_start = sbi_cpu_start,
  90. #ifdef CONFIG_HOTPLUG_CPU
  91. .cpu_disable = sbi_cpu_disable,
  92. .cpu_stop = sbi_cpu_stop,
  93. .cpu_is_stopped = sbi_cpu_is_stopped,
  94. #endif
  95. };