blob: 4954355c1aa0411157c6ea93833ccec062bcb221 [file] [log] [blame]
Etienne Carriere240720e2020-09-09 18:44:01 +02001// 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
Patrick Delaunay9f5e4aa2021-02-24 11:19:44 +01007#define LOG_CATEGORY UCLASS_SCMI_AGENT
8
Etienne Carriere240720e2020-09-09 18:44:01 +02009#include <common.h>
10#include <cpu_func.h>
11#include <dm.h>
Sean Anderson66e73482020-10-04 21:39:44 -040012#include <dm/device_compat.h>
Etienne Carriere240720e2020-09-09 18:44:01 +020013#include <errno.h>
14#include <scmi_agent.h>
15#include <asm/cache.h>
16#include <asm/system.h>
17#include <dm/ofnode.h>
18#include <linux/compat.h>
19#include <linux/io.h>
20#include <linux/ioport.h>
21
22#include "smt.h"
23
24/**
25 * Get shared memory configuration defined by the referred DT phandle
26 * Return with a errno compliant value.
27 */
28int scmi_dt_get_smt_buffer(struct udevice *dev, struct scmi_smt *smt)
29{
30 int ret;
31 struct ofnode_phandle_args args;
32 struct resource resource;
33 fdt32_t faddr;
34 phys_addr_t paddr;
35
36 ret = dev_read_phandle_with_args(dev, "shmem", NULL, 0, 0, &args);
37 if (ret)
38 return ret;
39
40 ret = ofnode_read_resource(args.node, 0, &resource);
41 if (ret)
42 return ret;
43
44 faddr = cpu_to_fdt32(resource.start);
45 paddr = ofnode_translate_address(args.node, &faddr);
46
47 smt->size = resource_size(&resource);
48 if (smt->size < sizeof(struct scmi_smt_header)) {
49 dev_err(dev, "Shared memory buffer too small\n");
50 return -EINVAL;
51 }
52
53 smt->buf = devm_ioremap(dev, paddr, smt->size);
54 if (!smt->buf)
55 return -ENOMEM;
56
57#ifdef CONFIG_ARM
58 if (dcache_status())
Patrick Delaunay2a3f1612021-03-16 09:29:40 +010059 mmu_set_region_dcache_behaviour(ALIGN_DOWN((uintptr_t)smt->buf, MMU_SECTION_SIZE),
60 ALIGN(smt->size, MMU_SECTION_SIZE),
61 DCACHE_OFF);
62
Etienne Carriere240720e2020-09-09 18:44:01 +020063#endif
64
65 return 0;
66}
67
68/**
69 * Write SCMI message @msg into a SMT shared buffer @smt.
70 * Return 0 on success and with a negative errno in case of error.
71 */
72int scmi_write_msg_to_smt(struct udevice *dev, struct scmi_smt *smt,
73 struct scmi_msg *msg)
74{
75 struct scmi_smt_header *hdr = (void *)smt->buf;
76
77 if ((!msg->in_msg && msg->in_msg_sz) ||
78 (!msg->out_msg && msg->out_msg_sz))
79 return -EINVAL;
80
81 if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
82 dev_dbg(dev, "Channel busy\n");
83 return -EBUSY;
84 }
85
86 if (smt->size < (sizeof(*hdr) + msg->in_msg_sz) ||
87 smt->size < (sizeof(*hdr) + msg->out_msg_sz)) {
88 dev_dbg(dev, "Buffer too small\n");
89 return -ETOOSMALL;
90 }
91
92 /* Load message in shared memory */
93 hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
94 hdr->length = msg->in_msg_sz + sizeof(hdr->msg_header);
95 hdr->msg_header = SMT_HEADER_TOKEN(0) |
96 SMT_HEADER_MESSAGE_TYPE(0) |
97 SMT_HEADER_PROTOCOL_ID(msg->protocol_id) |
98 SMT_HEADER_MESSAGE_ID(msg->message_id);
99
100 memcpy_toio(hdr->msg_payload, msg->in_msg, msg->in_msg_sz);
101
102 return 0;
103}
104
105/**
106 * Read SCMI message from a SMT shared buffer @smt and copy it into @msg.
107 * Return 0 on success and with a negative errno in case of error.
108 */
109int scmi_read_resp_from_smt(struct udevice *dev, struct scmi_smt *smt,
110 struct scmi_msg *msg)
111{
112 struct scmi_smt_header *hdr = (void *)smt->buf;
113
114 if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
115 dev_err(dev, "Channel unexpectedly busy\n");
116 return -EBUSY;
117 }
118
119 if (hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR) {
120 dev_err(dev, "Channel error reported, reset channel\n");
121 return -ECOMM;
122 }
123
124 if (hdr->length > msg->out_msg_sz + sizeof(hdr->msg_header)) {
125 dev_err(dev, "Buffer to small\n");
126 return -ETOOSMALL;
127 }
128
129 /* Get the data */
130 msg->out_msg_sz = hdr->length - sizeof(hdr->msg_header);
131 memcpy_fromio(msg->out_msg, hdr->msg_payload, msg->out_msg_sz);
132
133 return 0;
134}
135
136/**
137 * Clear SMT flags in shared buffer to allow further message exchange
138 */
139void scmi_clear_smt_channel(struct scmi_smt *smt)
140{
141 struct scmi_smt_header *hdr = (void *)smt->buf;
142
143 hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR;
144}