smp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Fraunhofer AISEC,
  4. * Lukas Auer <lukas.auer@aisec.fraunhofer.de>
  5. */
  6. #include <common.h>
  7. #include <cpu_func.h>
  8. #include <dm.h>
  9. #include <asm/barrier.h>
  10. #include <asm/smp.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. static int send_ipi_many(struct ipi_data *ipi, int wait)
  13. {
  14. ofnode node, cpus;
  15. u32 reg;
  16. int ret, pending;
  17. cpus = ofnode_path("/cpus");
  18. if (!ofnode_valid(cpus)) {
  19. pr_err("Can't find cpus node!\n");
  20. return -EINVAL;
  21. }
  22. ofnode_for_each_subnode(node, cpus) {
  23. /* skip if hart is marked as not available in the device tree */
  24. if (!ofnode_is_available(node))
  25. continue;
  26. /* read hart ID of CPU */
  27. ret = ofnode_read_u32(node, "reg", &reg);
  28. if (ret)
  29. continue;
  30. /* skip if it is the hart we are running on */
  31. if (reg == gd->arch.boot_hart)
  32. continue;
  33. if (reg >= CONFIG_NR_CPUS) {
  34. pr_err("Hart ID %d is out of range, increase CONFIG_NR_CPUS\n",
  35. reg);
  36. continue;
  37. }
  38. #ifndef CONFIG_XIP
  39. /* skip if hart is not available */
  40. if (!(gd->arch.available_harts & (1 << reg)))
  41. continue;
  42. #endif
  43. gd->arch.ipi[reg].addr = ipi->addr;
  44. gd->arch.ipi[reg].arg0 = ipi->arg0;
  45. gd->arch.ipi[reg].arg1 = ipi->arg1;
  46. ret = riscv_send_ipi(reg);
  47. if (ret) {
  48. pr_err("Cannot send IPI to hart %d\n", reg);
  49. return ret;
  50. }
  51. if (wait) {
  52. pending = 1;
  53. while (pending) {
  54. ret = riscv_get_ipi(reg, &pending);
  55. if (ret)
  56. return ret;
  57. }
  58. }
  59. }
  60. return 0;
  61. }
  62. void handle_ipi(ulong hart)
  63. {
  64. int ret;
  65. void (*smp_function)(ulong hart, ulong arg0, ulong arg1);
  66. if (hart >= CONFIG_NR_CPUS)
  67. return;
  68. __smp_mb();
  69. smp_function = (void (*)(ulong, ulong, ulong))gd->arch.ipi[hart].addr;
  70. invalidate_icache_all();
  71. /*
  72. * Clear the IPI to acknowledge the request before jumping to the
  73. * requested function.
  74. */
  75. ret = riscv_clear_ipi(hart);
  76. if (ret) {
  77. pr_err("Cannot clear IPI of hart %ld (error %d)\n", hart, ret);
  78. return;
  79. }
  80. smp_function(hart, gd->arch.ipi[hart].arg0, gd->arch.ipi[hart].arg1);
  81. }
  82. int smp_call_function(ulong addr, ulong arg0, ulong arg1, int wait)
  83. {
  84. struct ipi_data ipi = {
  85. .addr = addr,
  86. .arg0 = arg0,
  87. .arg1 = arg1,
  88. };
  89. return send_ipi_many(&ipi, wait);
  90. }