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