blob: f1915c00741f0ed8a7f47911a57e63b2be63ba71 [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
Patrick Delaunay7062d4e2021-03-30 15:29:03 +020044 /* TEMP workaround for ofnode_read_resource translation issue */
45 if (of_live_active()) {
46 paddr = resource.start;
47 } else {
48 faddr = cpu_to_fdt32(resource.start);
49 paddr = ofnode_translate_address(args.node, &faddr);
50 }
Etienne Carriere240720e2020-09-09 18:44:01 +020051
52 smt->size = resource_size(&resource);
53 if (smt->size < sizeof(struct scmi_smt_header)) {
54 dev_err(dev, "Shared memory buffer too small\n");
55 return -EINVAL;
56 }
57
58 smt->buf = devm_ioremap(dev, paddr, smt->size);
59 if (!smt->buf)
60 return -ENOMEM;
61
62#ifdef CONFIG_ARM
63 if (dcache_status())
Patrick Delaunay2a3f1612021-03-16 09:29:40 +010064 mmu_set_region_dcache_behaviour(ALIGN_DOWN((uintptr_t)smt->buf, MMU_SECTION_SIZE),
65 ALIGN(smt->size, MMU_SECTION_SIZE),
66 DCACHE_OFF);
67
Etienne Carriere240720e2020-09-09 18:44:01 +020068#endif
69
70 return 0;
71}
72
73/**
74 * Write SCMI message @msg into a SMT shared buffer @smt.
75 * Return 0 on success and with a negative errno in case of error.
76 */
77int scmi_write_msg_to_smt(struct udevice *dev, struct scmi_smt *smt,
78 struct scmi_msg *msg)
79{
80 struct scmi_smt_header *hdr = (void *)smt->buf;
81
82 if ((!msg->in_msg && msg->in_msg_sz) ||
83 (!msg->out_msg && msg->out_msg_sz))
84 return -EINVAL;
85
86 if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
87 dev_dbg(dev, "Channel busy\n");
88 return -EBUSY;
89 }
90
91 if (smt->size < (sizeof(*hdr) + msg->in_msg_sz) ||
92 smt->size < (sizeof(*hdr) + msg->out_msg_sz)) {
93 dev_dbg(dev, "Buffer too small\n");
94 return -ETOOSMALL;
95 }
96
97 /* Load message in shared memory */
98 hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
99 hdr->length = msg->in_msg_sz + sizeof(hdr->msg_header);
100 hdr->msg_header = SMT_HEADER_TOKEN(0) |
101 SMT_HEADER_MESSAGE_TYPE(0) |
102 SMT_HEADER_PROTOCOL_ID(msg->protocol_id) |
103 SMT_HEADER_MESSAGE_ID(msg->message_id);
104
105 memcpy_toio(hdr->msg_payload, msg->in_msg, msg->in_msg_sz);
106
107 return 0;
108}
109
110/**
111 * Read SCMI message from a SMT shared buffer @smt and copy it into @msg.
112 * Return 0 on success and with a negative errno in case of error.
113 */
114int scmi_read_resp_from_smt(struct udevice *dev, struct scmi_smt *smt,
115 struct scmi_msg *msg)
116{
117 struct scmi_smt_header *hdr = (void *)smt->buf;
118
119 if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
120 dev_err(dev, "Channel unexpectedly busy\n");
121 return -EBUSY;
122 }
123
124 if (hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR) {
125 dev_err(dev, "Channel error reported, reset channel\n");
126 return -ECOMM;
127 }
128
129 if (hdr->length > msg->out_msg_sz + sizeof(hdr->msg_header)) {
130 dev_err(dev, "Buffer to small\n");
131 return -ETOOSMALL;
132 }
133
134 /* Get the data */
135 msg->out_msg_sz = hdr->length - sizeof(hdr->msg_header);
136 memcpy_fromio(msg->out_msg, hdr->msg_payload, msg->out_msg_sz);
137
138 return 0;
139}
140
141/**
142 * Clear SMT flags in shared buffer to allow further message exchange
143 */
144void scmi_clear_smt_channel(struct scmi_smt *smt)
145{
146 struct scmi_smt_header *hdr = (void *)smt->buf;
147
148 hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR;
149}