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