Lukas Auer | fa33f08 | 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 | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <asm/barrier.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 12 | #include <asm/smp.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 13 | #include <linux/printk.h> |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Lukas Auer | 90ae281 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 17 | static int send_ipi_many(struct ipi_data *ipi, int wait) |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 18 | { |
| 19 | ofnode node, cpus; |
| 20 | u32 reg; |
Lukas Auer | 90ae281 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 21 | int ret, pending; |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 22 | |
| 23 | cpus = ofnode_path("/cpus"); |
| 24 | if (!ofnode_valid(cpus)) { |
| 25 | pr_err("Can't find cpus node!\n"); |
| 26 | return -EINVAL; |
| 27 | } |
| 28 | |
| 29 | ofnode_for_each_subnode(node, cpus) { |
| 30 | /* skip if hart is marked as not available in the device tree */ |
Simon Glass | 8909066 | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 31 | if (!ofnode_is_enabled(node)) |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 32 | continue; |
| 33 | |
| 34 | /* read hart ID of CPU */ |
| 35 | ret = ofnode_read_u32(node, "reg", ®); |
| 36 | if (ret) |
| 37 | continue; |
| 38 | |
| 39 | /* skip if it is the hart we are running on */ |
| 40 | if (reg == gd->arch.boot_hart) |
| 41 | continue; |
| 42 | |
| 43 | if (reg >= CONFIG_NR_CPUS) { |
| 44 | pr_err("Hart ID %d is out of range, increase CONFIG_NR_CPUS\n", |
| 45 | reg); |
| 46 | continue; |
| 47 | } |
| 48 | |
Nikita Shubin | c2bdf02 | 2022-09-02 11:47:39 +0300 | [diff] [blame] | 49 | #if !CONFIG_IS_ENABLED(XIP) |
Rick Chen | e0465f8 | 2022-09-21 14:34:54 +0800 | [diff] [blame] | 50 | #ifdef CONFIG_AVAILABLE_HARTS |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 51 | /* skip if hart is not available */ |
| 52 | if (!(gd->arch.available_harts & (1 << reg))) |
| 53 | continue; |
Rick Chen | bdce389 | 2019-04-30 13:49:33 +0800 | [diff] [blame] | 54 | #endif |
Rick Chen | e0465f8 | 2022-09-21 14:34:54 +0800 | [diff] [blame] | 55 | #endif |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 56 | |
| 57 | gd->arch.ipi[reg].addr = ipi->addr; |
| 58 | gd->arch.ipi[reg].arg0 = ipi->arg0; |
| 59 | gd->arch.ipi[reg].arg1 = ipi->arg1; |
| 60 | |
Sean Anderson | f760c9a | 2020-09-21 07:51:37 -0400 | [diff] [blame] | 61 | /* |
| 62 | * Ensure valid only becomes set when the IPI parameters are |
| 63 | * set. An IPI may already be pending on other harts, so we |
| 64 | * need a way to signal that the IPI device has been |
| 65 | * initialized, and that it is ok to call the function. |
| 66 | */ |
| 67 | __smp_store_release(&gd->arch.ipi[reg].valid, 1); |
Sean Anderson | d4990a4 | 2020-09-21 07:51:36 -0400 | [diff] [blame] | 68 | |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 69 | ret = riscv_send_ipi(reg); |
| 70 | if (ret) { |
| 71 | pr_err("Cannot send IPI to hart %d\n", reg); |
| 72 | return ret; |
| 73 | } |
Lukas Auer | 90ae281 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 74 | |
| 75 | if (wait) { |
| 76 | pending = 1; |
| 77 | while (pending) { |
| 78 | ret = riscv_get_ipi(reg, &pending); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | } |
| 82 | } |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | void handle_ipi(ulong hart) |
| 89 | { |
| 90 | int ret; |
| 91 | void (*smp_function)(ulong hart, ulong arg0, ulong arg1); |
| 92 | |
| 93 | if (hart >= CONFIG_NR_CPUS) |
| 94 | return; |
| 95 | |
Sean Anderson | f760c9a | 2020-09-21 07:51:37 -0400 | [diff] [blame] | 96 | /* |
| 97 | * If valid is not set, then U-Boot has not requested the IPI. The |
| 98 | * IPI device may not be initialized, so all we can do is wait for |
| 99 | * U-Boot to initialize it and send an IPI |
| 100 | */ |
| 101 | if (!__smp_load_acquire(&gd->arch.ipi[hart].valid)) |
| 102 | return; |
Lukas Auer | 90ae281 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 103 | |
| 104 | smp_function = (void (*)(ulong, ulong, ulong))gd->arch.ipi[hart].addr; |
| 105 | invalidate_icache_all(); |
| 106 | |
| 107 | /* |
| 108 | * Clear the IPI to acknowledge the request before jumping to the |
| 109 | * requested function. |
| 110 | */ |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 111 | ret = riscv_clear_ipi(hart); |
| 112 | if (ret) { |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 113 | pr_err("Cannot clear IPI of hart %ld (error %d)\n", hart, ret); |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 117 | smp_function(hart, gd->arch.ipi[hart].arg0, gd->arch.ipi[hart].arg1); |
| 118 | } |
| 119 | |
Lukas Auer | 90ae281 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 120 | int smp_call_function(ulong addr, ulong arg0, ulong arg1, int wait) |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 121 | { |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 122 | struct ipi_data ipi = { |
| 123 | .addr = addr, |
| 124 | .arg0 = arg0, |
| 125 | .arg1 = arg1, |
| 126 | }; |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 127 | |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 128 | return send_ipi_many(&ipi, wait); |
Lukas Auer | fa33f08 | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 129 | } |