xref: /openbmc/linux/drivers/firmware/arm_scmi/smc.c (revision 04b3c795)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Management Interface (SCMI) Message SMC/HVC
4  * Transport driver
5  *
6  * Copyright 2020 NXP
7  */
8 
9 #include <linux/arm-smccc.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/mutex.h>
13 #include <linux/of.h>
14 #include <linux/of_address.h>
15 #include <linux/slab.h>
16 
17 #include "common.h"
18 
19 /**
20  * struct scmi_smc - Structure representing a SCMI smc transport
21  *
22  * @cinfo: SCMI channel info
23  * @shmem: Transmit/Receive shared memory area
24  * @func_id: smc/hvc call function id
25  */
26 
27 struct scmi_smc {
28 	struct scmi_chan_info *cinfo;
29 	struct scmi_shared_mem __iomem *shmem;
30 	struct mutex shmem_lock;
31 	u32 func_id;
32 };
33 
34 static bool smc_chan_available(struct device *dev, int idx)
35 {
36 	struct device_node *np = of_parse_phandle(dev->of_node, "shmem", 0);
37 	if (!np)
38 		return false;
39 
40 	of_node_put(np);
41 	return true;
42 }
43 
44 static int smc_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
45 			  bool tx)
46 {
47 	struct device *cdev = cinfo->dev;
48 	struct scmi_smc *scmi_info;
49 	resource_size_t size;
50 	struct resource res;
51 	struct device_node *np;
52 	u32 func_id;
53 	int ret;
54 
55 	if (!tx)
56 		return -ENODEV;
57 
58 	scmi_info = devm_kzalloc(dev, sizeof(*scmi_info), GFP_KERNEL);
59 	if (!scmi_info)
60 		return -ENOMEM;
61 
62 	np = of_parse_phandle(cdev->of_node, "shmem", 0);
63 	ret = of_address_to_resource(np, 0, &res);
64 	of_node_put(np);
65 	if (ret) {
66 		dev_err(cdev, "failed to get SCMI Tx shared memory\n");
67 		return ret;
68 	}
69 
70 	size = resource_size(&res);
71 	scmi_info->shmem = devm_ioremap(dev, res.start, size);
72 	if (!scmi_info->shmem) {
73 		dev_err(dev, "failed to ioremap SCMI Tx shared memory\n");
74 		return -EADDRNOTAVAIL;
75 	}
76 
77 	ret = of_property_read_u32(dev->of_node, "arm,smc-id", &func_id);
78 	if (ret < 0)
79 		return ret;
80 
81 	scmi_info->func_id = func_id;
82 	scmi_info->cinfo = cinfo;
83 	mutex_init(&scmi_info->shmem_lock);
84 	cinfo->transport_info = scmi_info;
85 
86 	return 0;
87 }
88 
89 static int smc_chan_free(int id, void *p, void *data)
90 {
91 	struct scmi_chan_info *cinfo = p;
92 	struct scmi_smc *scmi_info = cinfo->transport_info;
93 
94 	cinfo->transport_info = NULL;
95 	scmi_info->cinfo = NULL;
96 
97 	scmi_free_channel(cinfo, data, id);
98 
99 	return 0;
100 }
101 
102 static int smc_send_message(struct scmi_chan_info *cinfo,
103 			    struct scmi_xfer *xfer)
104 {
105 	struct scmi_smc *scmi_info = cinfo->transport_info;
106 	struct arm_smccc_res res;
107 
108 	mutex_lock(&scmi_info->shmem_lock);
109 
110 	shmem_tx_prepare(scmi_info->shmem, xfer);
111 
112 	arm_smccc_1_1_invoke(scmi_info->func_id, 0, 0, 0, 0, 0, 0, 0, &res);
113 	scmi_rx_callback(scmi_info->cinfo, shmem_read_header(scmi_info->shmem));
114 
115 	mutex_unlock(&scmi_info->shmem_lock);
116 
117 	/* Only SMCCC_RET_NOT_SUPPORTED is valid error code */
118 	if (res.a0)
119 		return -EOPNOTSUPP;
120 	return 0;
121 }
122 
123 static void smc_fetch_response(struct scmi_chan_info *cinfo,
124 			       struct scmi_xfer *xfer)
125 {
126 	struct scmi_smc *scmi_info = cinfo->transport_info;
127 
128 	shmem_fetch_response(scmi_info->shmem, xfer);
129 }
130 
131 static bool
132 smc_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
133 {
134 	struct scmi_smc *scmi_info = cinfo->transport_info;
135 
136 	return shmem_poll_done(scmi_info->shmem, xfer);
137 }
138 
139 static struct scmi_transport_ops scmi_smc_ops = {
140 	.chan_available = smc_chan_available,
141 	.chan_setup = smc_chan_setup,
142 	.chan_free = smc_chan_free,
143 	.send_message = smc_send_message,
144 	.fetch_response = smc_fetch_response,
145 	.poll_done = smc_poll_done,
146 };
147 
148 const struct scmi_desc scmi_smc_desc = {
149 	.ops = &scmi_smc_ops,
150 	.max_rx_timeout_ms = 30,
151 	.max_msg = 1,
152 	.max_msg_size = 128,
153 };
154