Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved. |
Etienne Carriere | c624c97 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 4 | * Copyright (C) 2019-2022 Linaro Limited. |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 5dd84d4 | 2021-02-24 11:19:44 +0100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_SCMI_AGENT |
| 8 | |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 9 | #include <cpu_func.h> |
| 10 | #include <dm.h> |
Sean Anderson | 31786fe | 2020-10-04 21:39:44 -0400 | [diff] [blame] | 11 | #include <dm/device_compat.h> |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 12 | #include <errno.h> |
| 13 | #include <scmi_agent.h> |
| 14 | #include <asm/cache.h> |
| 15 | #include <asm/system.h> |
| 16 | #include <dm/ofnode.h> |
| 17 | #include <linux/compat.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/ioport.h> |
| 20 | |
| 21 | #include "smt.h" |
| 22 | |
| 23 | /** |
| 24 | * Get shared memory configuration defined by the referred DT phandle |
| 25 | * Return with a errno compliant value. |
| 26 | */ |
| 27 | int scmi_dt_get_smt_buffer(struct udevice *dev, struct scmi_smt *smt) |
| 28 | { |
| 29 | int ret; |
| 30 | struct ofnode_phandle_args args; |
| 31 | struct resource resource; |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 32 | |
| 33 | ret = dev_read_phandle_with_args(dev, "shmem", NULL, 0, 0, &args); |
| 34 | if (ret) |
| 35 | return ret; |
| 36 | |
| 37 | ret = ofnode_read_resource(args.node, 0, &resource); |
| 38 | if (ret) |
| 39 | return ret; |
| 40 | |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 41 | smt->size = resource_size(&resource); |
| 42 | if (smt->size < sizeof(struct scmi_smt_header)) { |
| 43 | dev_err(dev, "Shared memory buffer too small\n"); |
| 44 | return -EINVAL; |
| 45 | } |
| 46 | |
Patrick Delaunay | 2a28c6e | 2021-04-06 09:38:06 +0200 | [diff] [blame] | 47 | smt->buf = devm_ioremap(dev, resource.start, smt->size); |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 48 | if (!smt->buf) |
| 49 | return -ENOMEM; |
| 50 | |
| 51 | #ifdef CONFIG_ARM |
| 52 | if (dcache_status()) |
Patrick Delaunay | ab5154f | 2021-03-16 09:29:40 +0100 | [diff] [blame] | 53 | mmu_set_region_dcache_behaviour(ALIGN_DOWN((uintptr_t)smt->buf, MMU_SECTION_SIZE), |
| 54 | ALIGN(smt->size, MMU_SECTION_SIZE), |
| 55 | DCACHE_OFF); |
| 56 | |
Etienne Carriere | 2b41208 | 2020-09-09 18:44:01 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Write SCMI message @msg into a SMT shared buffer @smt. |
| 64 | * Return 0 on success and with a negative errno in case of error. |
| 65 | */ |
| 66 | int scmi_write_msg_to_smt(struct udevice *dev, struct scmi_smt *smt, |
| 67 | struct scmi_msg *msg) |
| 68 | { |
| 69 | struct scmi_smt_header *hdr = (void *)smt->buf; |
| 70 | |
| 71 | if ((!msg->in_msg && msg->in_msg_sz) || |
| 72 | (!msg->out_msg && msg->out_msg_sz)) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) { |
| 76 | dev_dbg(dev, "Channel busy\n"); |
| 77 | return -EBUSY; |
| 78 | } |
| 79 | |
| 80 | if (smt->size < (sizeof(*hdr) + msg->in_msg_sz) || |
| 81 | smt->size < (sizeof(*hdr) + msg->out_msg_sz)) { |
| 82 | dev_dbg(dev, "Buffer too small\n"); |
| 83 | return -ETOOSMALL; |
| 84 | } |
| 85 | |
| 86 | /* Load message in shared memory */ |
| 87 | hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE; |
| 88 | hdr->length = msg->in_msg_sz + sizeof(hdr->msg_header); |
| 89 | hdr->msg_header = SMT_HEADER_TOKEN(0) | |
| 90 | SMT_HEADER_MESSAGE_TYPE(0) | |
| 91 | SMT_HEADER_PROTOCOL_ID(msg->protocol_id) | |
| 92 | SMT_HEADER_MESSAGE_ID(msg->message_id); |
| 93 | |
| 94 | memcpy_toio(hdr->msg_payload, msg->in_msg, msg->in_msg_sz); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Read SCMI message from a SMT shared buffer @smt and copy it into @msg. |
| 101 | * Return 0 on success and with a negative errno in case of error. |
| 102 | */ |
| 103 | int scmi_read_resp_from_smt(struct udevice *dev, struct scmi_smt *smt, |
| 104 | struct scmi_msg *msg) |
| 105 | { |
| 106 | struct scmi_smt_header *hdr = (void *)smt->buf; |
| 107 | |
| 108 | if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) { |
| 109 | dev_err(dev, "Channel unexpectedly busy\n"); |
| 110 | return -EBUSY; |
| 111 | } |
| 112 | |
| 113 | if (hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR) { |
| 114 | dev_err(dev, "Channel error reported, reset channel\n"); |
| 115 | return -ECOMM; |
| 116 | } |
| 117 | |
| 118 | if (hdr->length > msg->out_msg_sz + sizeof(hdr->msg_header)) { |
| 119 | dev_err(dev, "Buffer to small\n"); |
| 120 | return -ETOOSMALL; |
| 121 | } |
| 122 | |
| 123 | /* Get the data */ |
| 124 | msg->out_msg_sz = hdr->length - sizeof(hdr->msg_header); |
| 125 | memcpy_fromio(msg->out_msg, hdr->msg_payload, msg->out_msg_sz); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Clear SMT flags in shared buffer to allow further message exchange |
| 132 | */ |
| 133 | void scmi_clear_smt_channel(struct scmi_smt *smt) |
| 134 | { |
| 135 | struct scmi_smt_header *hdr = (void *)smt->buf; |
| 136 | |
| 137 | hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR; |
| 138 | } |
Etienne Carriere | c624c97 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Write SCMI message @msg into a SMT_MSG shared buffer @smt. |
| 142 | * Return 0 on success and with a negative errno in case of error. |
| 143 | */ |
| 144 | int scmi_msg_to_smt_msg(struct udevice *dev, struct scmi_smt *smt, |
| 145 | struct scmi_msg *msg, size_t *buf_size) |
| 146 | { |
| 147 | struct scmi_smt_msg_header *hdr = (void *)smt->buf; |
| 148 | |
| 149 | if ((!msg->in_msg && msg->in_msg_sz) || |
| 150 | (!msg->out_msg && msg->out_msg_sz)) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | if (smt->size < (sizeof(*hdr) + msg->in_msg_sz) || |
| 154 | smt->size < (sizeof(*hdr) + msg->out_msg_sz)) { |
| 155 | dev_dbg(dev, "Buffer too small\n"); |
| 156 | return -ETOOSMALL; |
| 157 | } |
| 158 | |
| 159 | *buf_size = msg->in_msg_sz + sizeof(hdr->msg_header); |
| 160 | |
| 161 | hdr->msg_header = SMT_HEADER_TOKEN(0) | |
| 162 | SMT_HEADER_MESSAGE_TYPE(0) | |
| 163 | SMT_HEADER_PROTOCOL_ID(msg->protocol_id) | |
| 164 | SMT_HEADER_MESSAGE_ID(msg->message_id); |
| 165 | |
| 166 | memcpy(hdr->msg_payload, msg->in_msg, msg->in_msg_sz); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Read SCMI message from a SMT shared buffer @smt and copy it into @msg. |
| 173 | * Return 0 on success and with a negative errno in case of error. |
| 174 | */ |
| 175 | int scmi_msg_from_smt_msg(struct udevice *dev, struct scmi_smt *smt, |
| 176 | struct scmi_msg *msg, size_t buf_size) |
| 177 | { |
| 178 | struct scmi_smt_msg_header *hdr = (void *)smt->buf; |
| 179 | |
| 180 | if (buf_size > msg->out_msg_sz + sizeof(hdr->msg_header)) { |
| 181 | dev_err(dev, "Buffer to small\n"); |
| 182 | return -ETOOSMALL; |
| 183 | } |
| 184 | |
| 185 | msg->out_msg_sz = buf_size - sizeof(hdr->msg_header); |
| 186 | memcpy(msg->out_msg, hdr->msg_payload, msg->out_msg_sz); |
| 187 | |
| 188 | return 0; |
| 189 | } |