1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2022 Ruslan Bukin <br@bsdpad.com> 5 * Copyright (c) 2023 Arm Ltd 6 * 7 * This work was supported by Innovate UK project 105694, "Digital Security 8 * by Design (DSbD) Technology Platform Prototype". 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bus.h> 37 #include <sys/cpu.h> 38 #include <sys/kernel.h> 39 #include <sys/module.h> 40 41 #include <dev/fdt/simplebus.h> 42 #include <dev/fdt/fdt_common.h> 43 #include <dev/ofw/ofw_bus_subr.h> 44 45 #include <dev/psci/smccc.h> 46 47 #include "scmi.h" 48 #include "scmi_protocols.h" 49 #include "scmi_shmem.h" 50 51 struct scmi_smc_softc { 52 struct scmi_softc base; 53 uint32_t smc_id; 54 device_t a2p_dev; 55 }; 56 57 static int scmi_smc_transport_init(device_t); 58 static int scmi_smc_xfer_msg(device_t, struct scmi_msg *); 59 static int scmi_smc_poll_msg(device_t, struct scmi_msg *, unsigned int); 60 static int scmi_smc_collect_reply(device_t, struct scmi_msg *); 61 static void scmi_smc_tx_complete(device_t, void *); 62 63 static int scmi_smc_probe(device_t); 64 65 static int 66 scmi_smc_transport_init(device_t dev) 67 { 68 struct scmi_smc_softc *sc; 69 phandle_t node; 70 ssize_t len; 71 72 sc = device_get_softc(dev); 73 74 node = ofw_bus_get_node(dev); 75 len = OF_getencprop(node, "arm,smc-id", &sc->smc_id, 76 sizeof(sc->smc_id)); 77 if (len <= 0) { 78 device_printf(dev, "No SMC ID found\n"); 79 return (EINVAL); 80 } 81 82 device_printf(dev, "smc id %x\n", sc->smc_id); 83 84 sc->a2p_dev = scmi_shmem_get(dev, node, SCMI_CHAN_A2P); 85 if (sc->a2p_dev == NULL) { 86 device_printf(dev, "A2P shmem dev not found.\n"); 87 return (ENXIO); 88 } 89 90 sc->base.trs_desc.no_completion_irq = true; 91 sc->base.trs_desc.reply_timo_ms = 30; 92 93 return (0); 94 } 95 96 static int 97 scmi_smc_xfer_msg(device_t dev, struct scmi_msg *msg) 98 { 99 struct scmi_smc_softc *sc; 100 int ret; 101 102 sc = device_get_softc(dev); 103 104 ret = scmi_shmem_prepare_msg(sc->a2p_dev, (uint8_t *)&msg->hdr, 105 msg->tx_len, msg->polling); 106 if (ret != 0) 107 return (ret); 108 109 arm_smccc_smc(sc->smc_id, 0, 0, 0, 0, 0, 0, 0, NULL); 110 111 return (0); 112 } 113 114 static int 115 scmi_smc_poll_msg(device_t dev, struct scmi_msg *msg, unsigned int tmo) 116 { 117 struct scmi_smc_softc *sc; 118 119 sc = device_get_softc(dev); 120 121 /* 122 * Nothing to poll since commands are completed as soon as smc 123 * returns ... but did we get back what we were poling for ? 124 */ 125 scmi_shmem_read_msg_header(sc->a2p_dev, &msg->hdr); 126 127 return (0); 128 } 129 130 static int 131 scmi_smc_collect_reply(device_t dev, struct scmi_msg *msg) 132 { 133 struct scmi_smc_softc *sc; 134 int ret; 135 136 sc = device_get_softc(dev); 137 138 ret = scmi_shmem_read_msg_payload(sc->a2p_dev, 139 msg->payld, msg->rx_len - SCMI_MSG_HDR_SIZE); 140 141 return (ret); 142 } 143 144 static void 145 scmi_smc_tx_complete(device_t dev, void *chan) 146 { 147 struct scmi_smc_softc *sc; 148 149 sc = device_get_softc(dev); 150 scmi_shmem_tx_complete(sc->a2p_dev); 151 } 152 153 static int 154 scmi_smc_probe(device_t dev) 155 { 156 157 if (!ofw_bus_is_compatible(dev, "arm,scmi-smc")) 158 return (ENXIO); 159 160 if (!ofw_bus_status_okay(dev)) 161 return (ENXIO); 162 163 device_set_desc(dev, "ARM SCMI SMC Transport driver"); 164 165 return (BUS_PROBE_DEFAULT); 166 } 167 168 static device_method_t scmi_smc_methods[] = { 169 DEVMETHOD(device_probe, scmi_smc_probe), 170 171 /* SCMI interface */ 172 DEVMETHOD(scmi_transport_init, scmi_smc_transport_init), 173 DEVMETHOD(scmi_xfer_msg, scmi_smc_xfer_msg), 174 DEVMETHOD(scmi_poll_msg, scmi_smc_poll_msg), 175 DEVMETHOD(scmi_collect_reply, scmi_smc_collect_reply), 176 DEVMETHOD(scmi_tx_complete, scmi_smc_tx_complete), 177 178 DEVMETHOD_END 179 }; 180 181 DEFINE_CLASS_1(scmi_smc, scmi_smc_driver, scmi_smc_methods, 182 sizeof(struct scmi_smc_softc), scmi_driver); 183 184 /* Needs to be after the mmio_sram driver */ 185 EARLY_DRIVER_MODULE(scmi_smc, simplebus, scmi_smc_driver, 0, 0, 186 BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_LATE); 187 MODULE_VERSION(scmi_smc, 1); 188