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