blob: c5491bc050c367524872b03e2e9a8d038dd4e602 [file] [log] [blame]
lh758261d2023-07-13 05:52:04 -07001/*
2 * linux/ipc/cross_msg.c
3 * Copyright (C) 2023 Sanechips Technology Co., Ltd.
4 */
5#include <linux/kthread.h>
6#include <linux/mm.h>
7#include <linux/slab.h>
8#include <asm/uaccess.h>
9
10#include "cross_msg.h"
11
12extern int msg_chn_ready;
13extern struct mutex cross_msg_mutex;
14
15extern int sys_msgget(key_t key, int msgflg);
16extern long do_kmsgsnd(int msqid, struct msgbuf* msgp, size_t msgsz, int msgflg);
17
18static void msg_ap_icp_init(void)
19{
20 mutex_init(&cross_msg_mutex);
21
22 if (zDrvRpMsg_CreateChannel_Cap(CROSS_MSG_ACT, CROSS_MSG_CHN, CROSS_CHN_SIZE)) {
23 panic(CROSS_PRINT "Failed create ap->cap msg channel id %d!\n", CROSS_MSG_CHN);
24 } else {
25 printk(KERN_INFO CROSS_PRINT "create ap->cap msg channel success!\n");
26 msg_chn_ready = 1;
27 }
28
29 if (zDrvRpMsg_CreateChannel_Cap(CROSS_MSG_ACT, CROSS_MSG_CHN_CAP, CROSS_CHN_SIZE)) {
30 panic(CROSS_PRINT "Failed create cap->ap msg channel id %d\n", CROSS_MSG_CHN_CAP);
31 } else {
32 printk(KERN_INFO CROSS_PRINT "create cap->ap msg channel success!\n");
33 }
34}
35
36void cross_msg_recv()
37{
38 T_sc_msg_header *msgheader = NULL;
39 long *typeheader = NULL;
40 char *textheader = NULL;
41 T_ZDrvRpMsg_Msg Icp_Msg;
42 int ret;
43 int msqid, alen;
44
45 msgheader = (T_sc_msg_header *)kmalloc(CROSS_MSG_SIZE, GFP_KERNEL);
46 if (!msgheader) {
47 panic(CROSS_PRINT "Failed malloc send msgheader!\n");
48 }
49
50 while (1)
51 {
52 memset(msgheader, 0, sizeof(T_sc_msg_header));
53 Icp_Msg.actorID = CROSS_MSG_ACT;
54 Icp_Msg.chID = CROSS_MSG_CHN_CAP;
55 Icp_Msg.buf = msgheader;
56 Icp_Msg.len = CROSS_MSG_SIZE;
57 Icp_Msg.flag = 0;
58 ret = zDrvRpMsg_Read_Cap(&Icp_Msg);
59#if CROSS_DEBUG
60 printk(KERN_INFO CROSS_PRINT "cross message rpmsg recv header %x ops %x\n", msgheader->head, msgheader->ops);
61#endif
62 if(ret < 0) {
63 printk(KERN_ERR CROSS_PRINT "read rmpsg from cap error:(%d)\n", ret);
64 continue;
65 }
66 if (msgheader->head != CROSS_MSG_HEAD)
67 {
68 printk(KERN_ERR CROSS_PRINT "read rmpsg content error\n");
69 continue;
70 }
71 switch (msgheader->ops) {
72 case MSGGET_F: {
73 break;
74 }
75 case MSGCTL_F: {
76 break;
77 }
78 case MSGSND_F: {
79 typeheader = (long *)(msgheader + 1);
80 textheader = (char *)(typeheader + 1);
81#if CROSS_DEBUG
82 printk(KERN_INFO CROSS_PRINT "cross message msgget key:(%d) flag:(%d)\n", msgheader->sndp.getp.key, msgheader->sndp.getp.msgflg);
83#endif
84 msqid = sys_msgget(msgheader->sndp.getp.key, msgheader->sndp.getp.msgflg);
85 if (msqid < 0) {
86 printk(KERN_ERR CROSS_PRINT "msgget error:(%d)\n", msqid);
87 ret = msqid;
88 goto ack;
89 }
90#if CROSS_DEBUG
91 printk(KERN_INFO CROSS_PRINT "cross message msgsnd text:(%s) msgtyp:(%d)\n", textheader, *typeheader);
92 printk(KERN_INFO CROSS_PRINT "cross message msgsnd msgflg: %x, msgsz:(%d)\n", msgheader->sndp.msgflg, msgheader->sndp.msgsz);
93#endif
94 ret = do_kmsgsnd(msqid, typeheader, msgheader->sndp.msgsz, msgheader->sndp.msgflg);
95 if (ret < 0) {
96 printk(KERN_ERR CROSS_PRINT "msgsnd error:(%d)\n", ret);
97 goto ack;
98 }
99#if CROSS_DEBUG
100 printk(KERN_INFO CROSS_PRINT "cross message msgsnd ret:(%d)\n", ret);
101#endif
102ack:
103 msgheader->head = CROSS_MSG_HEAD;
104 msgheader->ret = ret;
105 Icp_Msg.actorID = CROSS_MSG_ACT;
106 Icp_Msg.chID = CROSS_MSG_CHN_CAP;
107 Icp_Msg.flag = RPMSG_WRITE_INT;
108 Icp_Msg.buf = msgheader;
109 Icp_Msg.len = sizeof(T_sc_msg_header);
110 ret = zDrvRpMsg_Write_Cap(&Icp_Msg);
111 if (ret < 0)
112 printk(KERN_INFO CROSS_PRINT "write rpmsg to cap error:(%d)\n", ret);
113 break;
114 }
115 case MSGRCV_F: {
116 break;
117 }
118 default: {
119 printk(KERN_INFO CROSS_PRINT "cross message msg options unknow:(%d)\n", ret);
120 break;
121 }
122 }
123 }
124
125}
126
127void __init cross_msg_init(void)
128{
129 struct task_struct *recv_msg_thread;
130 msg_ap_icp_init();
131
132 printk(KERN_INFO CROSS_PRINT "cross message init");
133 recv_msg_thread = kthread_run(cross_msg_recv, NULL, "cross_msg_recv");
134 if (IS_ERR(recv_msg_thread)) {
135 panic("create recv_msg_thread err");
136 }
137 else {
138 wake_up_process(recv_msg_thread);
139 }
140}
141late_initcall(cross_msg_init);
142