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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame^] | 11 | #include <asm/global_data.h> |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 12 | #include <asm/smp.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Lukas Auer | c308e01 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 16 | static int send_ipi_many(struct ipi_data *ipi, int wait) |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 17 | { |
| 18 | ofnode node, cpus; |
| 19 | u32 reg; |
Lukas Auer | c308e01 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 20 | int ret, pending; |
Lukas Auer | 83d573d | 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 */ |
| 30 | if (!ofnode_is_available(node)) |
| 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 | |
Rick Chen | e5e6c36 | 2019-04-30 13:49:33 +0800 | [diff] [blame] | 48 | #ifndef CONFIG_XIP |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 49 | /* skip if hart is not available */ |
| 50 | if (!(gd->arch.available_harts & (1 << reg))) |
| 51 | continue; |
Rick Chen | e5e6c36 | 2019-04-30 13:49:33 +0800 | [diff] [blame] | 52 | #endif |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 53 | |
| 54 | gd->arch.ipi[reg].addr = ipi->addr; |
| 55 | gd->arch.ipi[reg].arg0 = ipi->arg0; |
| 56 | gd->arch.ipi[reg].arg1 = ipi->arg1; |
| 57 | |
Sean Anderson | ff184fe | 2020-09-21 07:51:37 -0400 | [diff] [blame] | 58 | /* |
| 59 | * Ensure valid only becomes set when the IPI parameters are |
| 60 | * set. An IPI may already be pending on other harts, so we |
| 61 | * need a way to signal that the IPI device has been |
| 62 | * initialized, and that it is ok to call the function. |
| 63 | */ |
| 64 | __smp_store_release(&gd->arch.ipi[reg].valid, 1); |
Sean Anderson | cfb0809 | 2020-09-21 07:51:36 -0400 | [diff] [blame] | 65 | |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 66 | ret = riscv_send_ipi(reg); |
| 67 | if (ret) { |
| 68 | pr_err("Cannot send IPI to hart %d\n", reg); |
| 69 | return ret; |
| 70 | } |
Lukas Auer | c308e01 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 71 | |
| 72 | if (wait) { |
| 73 | pending = 1; |
| 74 | while (pending) { |
| 75 | ret = riscv_get_ipi(reg, &pending); |
| 76 | if (ret) |
| 77 | return ret; |
| 78 | } |
| 79 | } |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | void handle_ipi(ulong hart) |
| 86 | { |
| 87 | int ret; |
| 88 | void (*smp_function)(ulong hart, ulong arg0, ulong arg1); |
| 89 | |
| 90 | if (hart >= CONFIG_NR_CPUS) |
| 91 | return; |
| 92 | |
Sean Anderson | ff184fe | 2020-09-21 07:51:37 -0400 | [diff] [blame] | 93 | /* |
| 94 | * If valid is not set, then U-Boot has not requested the IPI. The |
| 95 | * IPI device may not be initialized, so all we can do is wait for |
| 96 | * U-Boot to initialize it and send an IPI |
| 97 | */ |
| 98 | if (!__smp_load_acquire(&gd->arch.ipi[hart].valid)) |
| 99 | return; |
Lukas Auer | c308e01 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 100 | |
| 101 | smp_function = (void (*)(ulong, ulong, ulong))gd->arch.ipi[hart].addr; |
| 102 | invalidate_icache_all(); |
| 103 | |
| 104 | /* |
| 105 | * Clear the IPI to acknowledge the request before jumping to the |
| 106 | * requested function. |
| 107 | */ |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 108 | ret = riscv_clear_ipi(hart); |
| 109 | if (ret) { |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 110 | 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] | 111 | return; |
| 112 | } |
| 113 | |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 114 | smp_function(hart, gd->arch.ipi[hart].arg0, gd->arch.ipi[hart].arg1); |
| 115 | } |
| 116 | |
Lukas Auer | c308e01 | 2019-12-08 23:28:51 +0100 | [diff] [blame] | 117 | int smp_call_function(ulong addr, ulong arg0, ulong arg1, int wait) |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 118 | { |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 119 | struct ipi_data ipi = { |
| 120 | .addr = addr, |
| 121 | .arg0 = arg0, |
| 122 | .arg1 = arg1, |
| 123 | }; |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 124 | |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 125 | return send_ipi_many(&ipi, wait); |
Lukas Auer | 83d573d | 2019-03-17 19:28:32 +0100 | [diff] [blame] | 126 | } |