aboutsummaryrefslogtreecommitdiff
path: root/drivers/rpmsg/rpmsg_arm_mailbox.c
blob: 1cf7e20142e2c49e4608cc840738f7af6ed71ee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// SPDX-License-Identifier: GPL-2.0
/*
 * rpmsg client driver using mailbox client interface
 *
 * Copyright (C) 2019 ARM Ltd.
 *
 */

#include <linux/bitmap.h>
#include <linux/export.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/ktime.h>
#include <linux/mailbox_client.h>
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/processor.h>
#include <linux/semaphore.h>
#include <linux/slab.h>
#include <linux/rpmsg.h>
#include "rpmsg_internal.h"

#define RPMSG_NAME	"arm_rpmsg"
#define RPMSG_ADDR_ANY	0xFFFFFFFF
#define TX_TIMEOUT	500 /*by half second*/

struct arm_ept_info {
	struct rpmsg_channel_info info;
	struct rpmsg_endpoint *ept;
	struct mbox_client *cl;

	struct list_head node;
};

static LIST_HEAD(arm_ept_infos);

static void arm_msg_rx_handler(struct mbox_client *cl, void *mssg)
{
	struct arm_ept_info *it = NULL;

	list_for_each_entry(it, &arm_ept_infos, node) {
		if (it->cl == cl) {
			struct rpmsg_endpoint *ept = it->ept;

			ept->cb(ept->rpdev, mssg, 4, ept->priv, RPMSG_ADDR_ANY);
			return;
		}
	}
}


static void arm_destroy_ept(struct rpmsg_endpoint *ept)
{
	struct arm_ept_info *it = NULL;

	list_for_each_entry(it, &arm_ept_infos, node) {
		if (it->ept == ept) {
			list_del(&it->node);
			kfree(it);
			break;
		}
	}
	kfree(ept);
}

static int arm_send(struct rpmsg_endpoint *ept, void *data, int len)
{
	struct arm_ept_info *it = NULL;
	struct arm_ept_info *arm_ept = NULL;
	struct mbox_client *cl;
	struct mbox_chan *mbox;

	// Locate registered endpoint
	list_for_each_entry(it, &arm_ept_infos, node) {
		if (it->ept == ept) {
			arm_ept = it;
			break;
		}
	}

	if (arm_ept == NULL) {
		dev_printk(KERN_ERR, cl->dev,
			   "RPMsg ARM: Invalid endpoint\n");
		return -1;
	}

	cl = kzalloc(sizeof(struct mbox_client), GFP_KERNEL);
	cl->dev = ept->rpdev->dev.parent;
	cl->rx_callback = arm_msg_rx_handler;
	cl->tx_done = NULL; /* operate in blocking mode */
	cl->tx_block = true;
	cl->tx_tout = TX_TIMEOUT; /* by half a second */
	cl->knows_txdone = false; /* depending upon protocol */
	arm_ept->cl = cl;

	mbox = mbox_request_channel_byname(cl, arm_ept->info.name);
	if (IS_ERR_OR_NULL(mbox)) {
		dev_printk(KERN_ERR, cl->dev,
		 "RPMsg ARM: Cannot get channel by name: '%s'\n",
		 arm_ept->info.name);
		return -1;
	}
	mbox_send_message(mbox, data);
	mbox_free_channel(mbox);
	return 0;
}

static const struct rpmsg_endpoint_ops arm_endpoint_ops = {
	.destroy_ept = arm_destroy_ept,
	.send = arm_send,
};


static struct rpmsg_endpoint *arm_create_ept(struct rpmsg_device *rpdev,
		rpmsg_rx_cb_t cb, void *priv, struct rpmsg_channel_info chinfo)
{
	struct rpmsg_endpoint *ept;
	struct arm_ept_info *ept_mbox;

	ept = kzalloc(sizeof(*ept), GFP_KERNEL);
	if (!ept)
		return NULL;

	kref_init(&ept->refcount);
	ept->rpdev = rpdev;
	ept->cb = cb;
	ept->priv = priv;
	ept->ops = &arm_endpoint_ops;

	// store chinfo for determining destination mailbox when sending
	ept_mbox = kzalloc(sizeof(*ept_mbox), GFP_KERNEL);
	ept_mbox->info = chinfo;
	strncpy(ept_mbox->info.name, chinfo.name, RPMSG_NAME_SIZE);
	ept_mbox->ept = ept;
	list_add(&ept_mbox->node, &arm_ept_infos);
	return ept;
}

static const struct rpmsg_device_ops arm_device_ops = {
	.create_ept = arm_create_ept,
};


static void arm_release_device(struct device *dev)
{
	struct rpmsg_device *rpdev = to_rpmsg_device(dev);

	kfree(rpdev);
}


static int client_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct rpmsg_device *rpdev;

	rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
	if (!rpdev)
		return -ENOMEM;

	/* Assign callbacks for rpmsg_device */
	rpdev->ops = &arm_device_ops;

	/* Assign public information to the rpmsg_device */
	memcpy(rpdev->id.name, RPMSG_NAME, strlen(RPMSG_NAME));

	rpdev->dev.parent = dev;
	rpdev->dev.release = arm_release_device;

	return rpmsg_chrdev_register_device(rpdev);
}

static const struct of_device_id client_of_match[] = {
	{ .compatible = "arm,client", .data = NULL },
	{ /* Sentinel */ },
};

static struct platform_driver client_driver = {
	.driver = {
		.name = "arm-mhu-client",
		.of_match_table = client_of_match,
	},
	.probe = client_probe,
};

module_platform_driver(client_driver);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("ARM RPMSG Driver");
MODULE_AUTHOR("Tushar Khandelwal <tushar.khandelwal@arm.com>");