Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Copyright 2019 Google LLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/remoteproc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/rpmsg/mtk_rpmsg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "rpmsg_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) struct mtk_rpmsg_rproc_subdev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	struct mtk_rpmsg_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct rpmsg_endpoint *ns_ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct rproc_subdev subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct work_struct register_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct list_head channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct mutex channels_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define to_mtk_subdev(d) container_of(d, struct mtk_rpmsg_rproc_subdev, subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) struct mtk_rpmsg_channel_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct rpmsg_channel_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	bool registered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * struct rpmsg_ns_msg - dynamic name service announcement message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * @name: name of remote service that is published
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * @addr: address of remote service that is published
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * This message is sent across to publish a new service. When we receive these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * messages, an appropriate rpmsg channel (i.e device) is created. In turn, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * ->probe() handler of the appropriate rpmsg driver will be invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *  (if/as-soon-as one is registered).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) struct rpmsg_ns_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	char name[RPMSG_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	u32 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) struct mtk_rpmsg_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct rpmsg_device rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) struct mtk_rpmsg_endpoint {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct rpmsg_endpoint ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define to_mtk_rpmsg_device(r) container_of(r, struct mtk_rpmsg_device, rpdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define to_mtk_rpmsg_endpoint(r) container_of(r, struct mtk_rpmsg_endpoint, ept)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static const struct rpmsg_endpoint_ops mtk_rpmsg_endpoint_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static void __mtk_ept_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 						  refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	kfree(to_mtk_rpmsg_endpoint(ept));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static void mtk_rpmsg_ipi_handler(void *data, unsigned int len, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct mtk_rpmsg_endpoint *mept = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct rpmsg_endpoint *ept = &mept->ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	ret = (*ept->cb)(ept->rpdev, data, len, ept->priv, ept->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		dev_warn(&ept->rpdev->dev, "rpmsg handler return error = %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			 ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static struct rpmsg_endpoint *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) __mtk_create_ept(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		 struct rpmsg_device *rpdev, rpmsg_rx_cb_t cb, void *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		 u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct mtk_rpmsg_endpoint *mept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct rpmsg_endpoint *ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct platform_device *pdev = mtk_subdev->pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	mept = kzalloc(sizeof(*mept), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!mept)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	mept->mtk_subdev = mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	ept = &mept->ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	kref_init(&ept->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	ept->rpdev = rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ept->cb = cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	ept->priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	ept->ops = &mtk_rpmsg_endpoint_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	ept->addr = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	ret = mtk_subdev->info->register_ipi(pdev, id, mtk_rpmsg_ipi_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 					     mept);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		dev_err(&pdev->dev, "IPI register failed, id = %d", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		kref_put(&ept->refcount, __mtk_ept_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return ept;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static struct rpmsg_endpoint *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) mtk_rpmsg_create_ept(struct rpmsg_device *rpdev, rpmsg_rx_cb_t cb, void *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		     struct rpmsg_channel_info chinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		to_mtk_rpmsg_device(rpdev)->mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return __mtk_create_ept(mtk_subdev, rpdev, cb, priv, chinfo.src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void mtk_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	mtk_subdev->info->unregister_ipi(mtk_subdev->pdev, ept->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	kref_put(&ept->refcount, __mtk_ept_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int mtk_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return mtk_subdev->info->send_ipi(mtk_subdev->pdev, ept->addr, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 					  len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int mtk_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		to_mtk_rpmsg_endpoint(ept)->mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 * TODO: This currently is same as mtk_rpmsg_send, and wait until SCP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * received the last command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return mtk_subdev->info->send_ipi(mtk_subdev->pdev, ept->addr, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 					  len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static const struct rpmsg_endpoint_ops mtk_rpmsg_endpoint_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	.destroy_ept = mtk_rpmsg_destroy_ept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	.send = mtk_rpmsg_send,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	.trysend = mtk_rpmsg_trysend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void mtk_rpmsg_release_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct mtk_rpmsg_device *mdev = to_mtk_rpmsg_device(rpdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	kfree(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static const struct rpmsg_device_ops mtk_rpmsg_device_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.create_ept = mtk_rpmsg_create_ept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static struct device_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) mtk_rpmsg_match_device_subnode(struct device_node *node, const char *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	for_each_available_child_of_node(node, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		ret = of_property_read_string(child, "mtk,rpmsg-name", &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		if (strcmp(name, channel) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			return child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int mtk_rpmsg_register_device(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				     struct rpmsg_channel_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct rpmsg_device *rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct mtk_rpmsg_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct platform_device *pdev = mtk_subdev->pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (!mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	mdev->mtk_subdev = mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	rpdev = &mdev->rpdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	rpdev->ops = &mtk_rpmsg_device_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	rpdev->src = info->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	rpdev->dst = info->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	strscpy(rpdev->id.name, info->name, RPMSG_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	rpdev->dev.of_node =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		mtk_rpmsg_match_device_subnode(pdev->dev.of_node, info->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	rpdev->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	rpdev->dev.release = mtk_rpmsg_release_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return rpmsg_register_device(rpdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static void mtk_register_device_work_function(struct work_struct *register_work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	struct mtk_rpmsg_rproc_subdev *subdev = container_of(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		register_work, struct mtk_rpmsg_rproc_subdev, register_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct platform_device *pdev = subdev->pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct mtk_rpmsg_channel_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	mutex_lock(&subdev->channels_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	list_for_each_entry(info, &subdev->channels, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		if (info->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		ret = mtk_rpmsg_register_device(subdev, &info->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			dev_err(&pdev->dev, "Can't create rpmsg_device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		info->registered = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	mutex_unlock(&subdev->channels_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static int mtk_rpmsg_create_device(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				   char *name, u32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct mtk_rpmsg_channel_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	info = kzalloc(sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	strscpy(info->info.name, name, RPMSG_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	info->info.src = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	info->info.dst = RPMSG_ADDR_ANY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	mutex_lock(&mtk_subdev->channels_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	list_add(&info->list, &mtk_subdev->channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	mutex_unlock(&mtk_subdev->channels_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	schedule_work(&mtk_subdev->register_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int mtk_rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			   void *priv, u32 src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct rpmsg_ns_msg *msg = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct device *dev = &mtk_subdev->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (len != sizeof(*msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		dev_err(dev, "malformed ns msg (%d)\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * the name service ept does _not_ belong to a real rpmsg channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * and is handled by the rpmsg bus itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 * in somehow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (rpdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	/* don't trust the remote processor for null terminating the name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	dev_info(dev, "creating channel %s addr 0x%x\n", msg->name, msg->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	ret = mtk_rpmsg_create_device(mtk_subdev, msg->name, msg->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		dev_err(dev, "create rpmsg device failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static int mtk_rpmsg_prepare(struct rproc_subdev *subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	/* a dedicated endpoint handles the name service msgs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (mtk_subdev->info->ns_ipi_id >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		mtk_subdev->ns_ept =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			__mtk_create_ept(mtk_subdev, NULL, mtk_rpmsg_ns_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 					 mtk_subdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 					 mtk_subdev->info->ns_ipi_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (!mtk_subdev->ns_ept) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			dev_err(&mtk_subdev->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				"failed to create name service endpoint\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static void mtk_rpmsg_unprepare(struct rproc_subdev *subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (mtk_subdev->ns_ept) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		mtk_rpmsg_destroy_ept(mtk_subdev->ns_ept);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		mtk_subdev->ns_ept = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static void mtk_rpmsg_stop(struct rproc_subdev *subdev, bool crashed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct mtk_rpmsg_channel_info *info, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	struct device *dev = &mtk_subdev->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	 * Destroy the name service endpoint here, to avoid new channel being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	 * created after the rpmsg_unregister_device loop below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (mtk_subdev->ns_ept) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		mtk_rpmsg_destroy_ept(mtk_subdev->ns_ept);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		mtk_subdev->ns_ept = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	cancel_work_sync(&mtk_subdev->register_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	mutex_lock(&mtk_subdev->channels_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	list_for_each_entry(info, &mtk_subdev->channels, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		if (!info->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		if (rpmsg_unregister_device(dev, &info->info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			dev_warn(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 				dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 				"rpmsg_unregister_device failed for %s.%d.%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 				info->info.name, info->info.src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 				info->info.dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	list_for_each_entry_safe(info, next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 				 &mtk_subdev->channels, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		list_del(&info->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	mutex_unlock(&mtk_subdev->channels_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct rproc_subdev *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) mtk_rpmsg_create_rproc_subdev(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			      struct mtk_rpmsg_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	mtk_subdev = kzalloc(sizeof(*mtk_subdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if (!mtk_subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	mtk_subdev->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	mtk_subdev->subdev.prepare = mtk_rpmsg_prepare;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	mtk_subdev->subdev.stop = mtk_rpmsg_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	mtk_subdev->subdev.unprepare = mtk_rpmsg_unprepare;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	mtk_subdev->info = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	INIT_LIST_HEAD(&mtk_subdev->channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	INIT_WORK(&mtk_subdev->register_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		  mtk_register_device_work_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	mutex_init(&mtk_subdev->channels_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	return &mtk_subdev->subdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) EXPORT_SYMBOL_GPL(mtk_rpmsg_create_rproc_subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) void mtk_rpmsg_destroy_rproc_subdev(struct rproc_subdev *subdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	struct mtk_rpmsg_rproc_subdev *mtk_subdev = to_mtk_subdev(subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	kfree(mtk_subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) EXPORT_SYMBOL_GPL(mtk_rpmsg_destroy_rproc_subdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) MODULE_DESCRIPTION("MediaTek scp rpmsg driver");