1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4 * Copyright (C) 2019-2020 Linaro Limited.
5 */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <scmi_agent.h>
11 #include <asm/cache.h>
12 #include <asm/system.h>
13 #include <dm/ofnode.h>
14 #include <linux/compat.h>
15 #include <linux/io.h>
16 #include <linux/ioport.h>
17
18 #include "smt.h"
19
20 /**
21 * Get shared memory configuration defined by the referred DT phandle
22 * Return with a errno compliant value.
23 */
scmi_dt_get_smt_buffer(struct udevice * dev,struct scmi_smt * smt)24 int scmi_dt_get_smt_buffer(struct udevice *dev, struct scmi_smt *smt)
25 {
26 int ret;
27 struct ofnode_phandle_args args;
28 struct resource resource;
29
30 ret = dev_read_phandle_with_args(dev, "shmem", NULL, 0, 0, &args);
31 if (ret)
32 return ret;
33
34 ret = ofnode_read_resource(args.node, 0, &resource);
35 if (ret)
36 return ret;
37
38 smt->size = resource_size(&resource);
39 if (smt->size < sizeof(struct scmi_smt_header)) {
40 dev_err(dev, "Shared memory buffer too small\n");
41 return -EINVAL;
42 }
43
44 smt->buf = devm_ioremap(dev, resource.start, smt->size);
45 if (!smt->buf)
46 return -ENOMEM;
47
48 #ifdef CONFIG_ARM
49 if (dcache_status())
50 mmu_set_region_dcache_behaviour(round_down((ulong)smt->buf, SZ_4K),
51 round_up((ulong)smt->size, SZ_4K),
52 DCACHE_OFF);
53 #endif
54
55 return 0;
56 }
57
58 /**
59 * Write SCMI message @msg into a SMT shared buffer @smt.
60 * Return 0 on success and with a negative errno in case of error.
61 */
scmi_write_msg_to_smt(struct udevice * dev,struct scmi_smt * smt,struct scmi_msg * msg)62 int scmi_write_msg_to_smt(struct udevice *dev, struct scmi_smt *smt,
63 struct scmi_msg *msg)
64 {
65 struct scmi_smt_header *hdr = (void *)smt->buf;
66
67 if ((!msg->in_msg && msg->in_msg_sz) ||
68 (!msg->out_msg && msg->out_msg_sz))
69 return -EINVAL;
70
71 if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
72 dev_dbg(dev, "Channel busy\n");
73 return -EBUSY;
74 }
75
76 if (smt->size < (sizeof(*hdr) + msg->in_msg_sz) ||
77 smt->size < (sizeof(*hdr) + msg->out_msg_sz)) {
78 dev_dbg(dev, "Buffer too small\n");
79 return -ETOOSMALL;
80 }
81
82 /* Load message in shared memory */
83 hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
84 hdr->length = msg->in_msg_sz + sizeof(hdr->msg_header);
85 hdr->msg_header = SMT_HEADER_TOKEN(0) |
86 SMT_HEADER_MESSAGE_TYPE(0) |
87 SMT_HEADER_PROTOCOL_ID(msg->protocol_id) |
88 SMT_HEADER_MESSAGE_ID(msg->message_id);
89
90 memcpy_toio(hdr->msg_payload, msg->in_msg, msg->in_msg_sz);
91
92 return 0;
93 }
94
95 /**
96 * Read SCMI message from a SMT shared buffer @smt and copy it into @msg.
97 * Return 0 on success and with a negative errno in case of error.
98 */
scmi_read_resp_from_smt(struct udevice * dev,struct scmi_smt * smt,struct scmi_msg * msg)99 int scmi_read_resp_from_smt(struct udevice *dev, struct scmi_smt *smt,
100 struct scmi_msg *msg)
101 {
102 struct scmi_smt_header *hdr = (void *)smt->buf;
103
104 if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
105 dev_err(dev, "Channel unexpectedly busy\n");
106 return -EBUSY;
107 }
108
109 if (hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR) {
110 dev_err(dev, "Channel error reported, reset channel\n");
111 return -ECOMM;
112 }
113
114 if (hdr->length > msg->out_msg_sz + sizeof(hdr->msg_header)) {
115 dev_err(dev, "Buffer to small\n");
116 return -ETOOSMALL;
117 }
118
119 /* Get the data */
120 msg->out_msg_sz = hdr->length - sizeof(hdr->msg_header);
121 memcpy_fromio(msg->out_msg, hdr->msg_payload, msg->out_msg_sz);
122
123 return 0;
124 }
125
126 /**
127 * Clear SMT flags in shared buffer to allow further message exchange
128 */
scmi_clear_smt_channel(struct scmi_smt * smt)129 void scmi_clear_smt_channel(struct scmi_smt *smt)
130 {
131 struct scmi_smt_header *hdr = (void *)smt->buf;
132
133 hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR;
134 }
135