-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsbuf.c
1771 lines (1555 loc) · 44.2 KB
/
sbuf.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019 Spreadtrum Communications Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kthread.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <asm/pgtable.h>
#include <linux/sched.h>
#include <linux/version.h>
#if (LINUX_VERSION_CODE > KERNEL_VERSION( 4,4,60 ))
#include <uapi/linux/sched/types.h>
#include <linux/sched/task.h>
#endif
#include "../include/sipc.h"
#include "sipc_priv.h"
#include "sbuf.h"
#if defined(CONFIG_DEBUG_FS)
#include "sipc_debugfs.h"
#endif
#define VOLA_SBUF_SMEM volatile struct sbuf_smem_header
#define VOLA_SBUF_RING volatile struct sbuf_ring_header
struct name_node {
struct list_head list;
char comm[TASK_COMM_LEN];
pid_t pid;
u8 latest;
};
union sbuf_buf {
void *buf;
void __user *ubuf;
};
enum task_type {
TASK_RXWAIT = 0,
TASK_TXWAIT,
TASK_SELECT
};
static struct sbuf_mgr *sbufs[SIPC_ID_NR][SMSG_VALID_CH_NR];
static bool sbuf_has_data(struct sbuf_ring *ring, u8 dst, bool tx)
{
struct sbuf_ring_header_op *hd_op = &ring->header_op;
bool has_data;
unsigned long flags;
/*
* if it is local share memory,
* check the read and write point directly.
*/
if (smsg_ipcs[dst]->smem_type == SMEM_LOCAL) {
if (tx)
return (int)(BL_READL(hd_op->tx_wt_p) - BL_READL(hd_op->tx_rd_p)) <
hd_op->tx_size;
return BL_READL(hd_op->rx_wt_p) != BL_READL(hd_op->rx_rd_p);
}
/*
* if it is remote share memmory read the poll_mask,
* this situation requires that the poll_mask must be accurate enough.
*/
spin_lock_irqsave(&ring->poll_lock, flags);
if (tx)
has_data = ring->poll_mask & (POLLOUT | POLLWRNORM);
else
has_data = ring->poll_mask & (POLLIN | POLLRDNORM);
spin_unlock_irqrestore(&ring->poll_lock, flags);
return has_data;
}
static bool sbuf_is_task_pointer(const void *ptr)
{
struct task_struct *task;
struct thread_info *thread_info;
task = (struct task_struct *)ptr;
if (IS_ERR_OR_NULL(task) || !virt_addr_valid(task))
return false;
#ifndef CONFIG_THREAD_INFO_IN_TASK
/* in this case thread_info is in the same addres with stack thread_union*/
if (IS_ERR_OR_NULL(task->stack) || !virt_addr_valid(task->stack))
return false;
#endif
thread_info = task_thread_info(task);
if (IS_ERR_OR_NULL(thread_info) || !virt_addr_valid(thread_info))
return false;
return true;
}
static struct task_struct *sbuf_wait_get_task(
#if (LINUX_VERSION_CODE <= KERNEL_VERSION( 4,10,0 ))
wait_queue_t *pos,
#else
wait_queue_entry_t *pos,
#endif
u32 *b_select)
{
struct task_struct *task;
struct poll_wqueues *table;
if (!pos->private)
return NULL;
/* if the private is put into wait list by sbuf_read, the struct of
* pos->private is struct task_struct
* if the private is put into list by sbuf_poll, the struct of
* pos->private is struct poll_wqueues
*/
/* firstly, try struct poll_wqueues */
table = (struct poll_wqueues *)pos->private;
task = table->polling_task;
if (sbuf_is_task_pointer(task)) {
*b_select = 1;
return task;
}
/* firstly, try convert it with the struct task_struct */
task = (struct task_struct *)pos->private;
if (sbuf_is_task_pointer(task)) {
*b_select = 0;
return task;
}
return NULL;
}
#if defined(SIPC_DEBUG_SBUF_RDWT_OWNER)
static void sbuf_record_rdwt_owner(struct sbuf_ring *ring, int b_rx)
{
int b_add;
int cnt = 0;
struct name_node *pos = NULL;
struct name_node *temp = NULL;
struct list_head *owner_list;
unsigned long flags;
b_add = 1;
owner_list = b_rx ? (&ring->rx_list) : (&ring->tx_list);
spin_lock_irqsave(&ring->rxwait.lock, flags);
list_for_each_entry(pos, owner_list, list) {
cnt++;
if (pos->pid == current->pid) {
b_add = 0;
pos->latest = 1;
continue;
}
if (pos->latest)
pos->latest = 0;
}
spin_unlock_irqrestore(&ring->rxwait.lock, flags);
if (b_add) {
/* delete head next */
if (cnt == MAX_RECORD_CNT) {
temp = list_first_entry(owner_list,
struct name_node, list);
list_del(&temp->list);
kfree(temp);
}
pos = kzalloc(sizeof(*pos), GFP_KERNEL);
if (pos) {
memcpy(pos->comm, current->comm, TASK_COMM_LEN);
pos->pid = current->pid;
pos->latest = 1;
spin_lock_irqsave(&ring->rxwait.lock, flags);
list_add_tail(&pos->list, owner_list);
spin_unlock_irqrestore(&ring->rxwait.lock, flags);
}
}
}
static void sbuf_destroy_rdwt_owner(struct sbuf_ring *ring)
{
struct name_node *pos, *temp;
unsigned long flags;
spin_lock_irqsave(&ring->rxwait.lock, flags);
/* free task node */
list_for_each_entry_safe(pos,
temp,
&ring->rx_list,
list) {
list_del(&pos->list);
kfree(pos);
}
list_for_each_entry_safe(pos,
temp,
&ring->tx_list,
list) {
list_del(&pos->list);
kfree(pos);
}
spin_unlock_irqrestore(&ring->rxwait.lock, flags);
}
#endif
static void sbuf_skip_old_data(struct sbuf_mgr *sbuf)
{
struct sbuf_ring *ring = NULL;
struct sbuf_ring_header_op *hd_op = NULL;
u32 i, v;
unsigned long flags;
ring = &sbuf->rings[0];
/* must reques resource before read or write share memory */
if (sipc_smem_request_resource(ring->rx_pms, sbuf->dst, -1) < 0)
return;
for (i = 0; i < sbuf->ringnr; i++) {
ring = &sbuf->rings[i];
hd_op = &ring->header_op;
/* clean sbuf tx ring , sbuf tx ring no need to clear */
/* *(hd_op->tx_wt_p) = *(hd_op->tx_rd_p); */
/* clean sbuf rx ring */
v = BL_READL(hd_op->rx_wt_p);
BL_WRITEL(v, hd_op->rx_rd_p);
/* restore write mask. */
spin_lock_irqsave(&ring->poll_lock, flags);
ring->poll_mask = POLLOUT | POLLWRNORM;
spin_unlock_irqrestore(&ring->poll_lock, flags);
}
ring = &sbuf->rings[0];
/* release resource */
sipc_smem_release_resource(ring->rx_pms, sbuf->dst);
}
static void sbuf_pms_init(struct sbuf_ring *ring,
uint8_t dst, uint8_t ch, int index)
{
ring->need_wake_lock = true;
sprintf(ring->tx_pms_name, "sbuf-%d-%d-%d-tx", dst, ch, index);
ring->tx_pms = sprd_pms_create(dst, ring->tx_pms_name, false);
if (!ring->tx_pms)
pr_warn("create pms %s failed!\n", ring->tx_pms_name);
sprintf(ring->rx_pms_name, "sbuf-%d-%d-%d-rx", dst, ch, index);
ring->rx_pms = sprd_pms_create(dst, ring->rx_pms_name, false);
if (!ring->rx_pms)
pr_warn("create pms %s failed!\n", ring->rx_pms_name);
}
static void sbuf_comm_init(struct sbuf_mgr *sbuf)
{
u32 bufnum = sbuf->ringnr;
int i;
struct sbuf_ring *ring;
for (i = 0; i < bufnum; i++) {
ring = &sbuf->rings[i];
init_waitqueue_head(&ring->txwait);
init_waitqueue_head(&ring->rxwait);
#if defined(SIPC_DEBUG_SBUF_RDWT_OWNER)
INIT_LIST_HEAD(&ring->tx_list);
INIT_LIST_HEAD(&ring->rx_list);
#endif
mutex_init(&ring->txlock);
mutex_init(&ring->rxlock);
spin_lock_init(&ring->poll_lock);
spin_lock_init(&ring->rxwait.lock);
/* init, set write mask. */
ring->poll_mask = POLLOUT | POLLWRNORM;
sbuf_pms_init(ring, sbuf->dst, sbuf->channel, i);
}
}
static int sbuf_host_init(struct smsg_ipc *sipc, struct sbuf_mgr *sbuf,
u32 bufnum, u32 txbufsize, u32 rxbufsize)
{
VOLA_SBUF_SMEM *smem;
VOLA_SBUF_RING *ringhd;
struct sbuf_ring_header_op *hd_op;
int hsize, i, rval;
phys_addr_t offset = 0;
u8 dst = sbuf->dst;
struct sbuf_ring *ring;
u32 txbuf_addr;
sbuf->ringnr = bufnum;
/* allocate smem */
hsize = sizeof(struct sbuf_smem_header) +
sizeof(struct sbuf_ring_header) * bufnum;
sbuf->smem_size = hsize + (txbufsize + rxbufsize) * bufnum;
sbuf->smem_addr = smem_alloc(dst, sbuf->smem_size);
if (!sbuf->smem_addr) {
pr_err("%s: channel %d-%d, Failed to allocate smem for sbuf\n",
__func__, sbuf->dst, sbuf->channel);
return -ENOMEM;
}
sbuf->dst_smem_addr = sbuf->smem_addr - sipc->smem_base +
sipc->dst_smem_base;
pr_debug("%s: channel %d-%d, smem_addr=0x%x, smem_size=0x%x, dst_smem_addr=0x%x\n",
__func__,
sbuf->dst,
sbuf->channel,
sbuf->smem_addr,
sbuf->smem_size,
sbuf->dst_smem_addr);
#ifdef CONFIG_PHYS_ADDR_T_64BIT
offset = sipc->high_offset;
offset = offset << 32;
#endif
pr_info("%s: channel %d-%d, offset = 0x%lx!\n",
__func__, sbuf->dst, sbuf->channel, (unsigned long)offset);
sbuf->smem_virt = shmem_ram_vmap_nocache(dst,
sbuf->smem_addr + offset,
sbuf->smem_size);
if (!sbuf->smem_virt) {
pr_err("%s: channel %d-%d, Failed to map smem for sbuf\n",
__func__, sbuf->dst, sbuf->channel);
smem_free(dst, sbuf->smem_addr, sbuf->smem_size);
return -EFAULT;
}
/* allocate rings description */
sbuf->rings = kcalloc(bufnum, sizeof(struct sbuf_ring), GFP_KERNEL);
if (!sbuf->rings) {
smem_free(dst, sbuf->smem_addr, sbuf->smem_size);
shmem_ram_unmap(dst, sbuf->smem_virt);
return -ENOMEM;
}
/* must request resource before read or write share memory */
rval = sipc_smem_request_resource(sipc->sipc_pms, sipc->dst, -1);
if (rval < 0) {
smem_free(dst, sbuf->smem_addr, sbuf->smem_size);
shmem_ram_unmap(dst, sbuf->smem_virt);
kfree(sbuf->rings);
return rval;
}
/* initialize all ring bufs */
smem = (VOLA_SBUF_SMEM *)sbuf->smem_virt;
BL_SETL(smem->ringnr, bufnum);
for (i = 0; i < bufnum; i++) {
ringhd = (VOLA_SBUF_RING *)&smem->headers[i];
txbuf_addr = sbuf->dst_smem_addr + hsize +
(txbufsize + rxbufsize) * i;
BL_SETL(ringhd->txbuf_addr, txbuf_addr);
BL_SETL(ringhd->txbuf_size, txbufsize);
BL_SETL(ringhd->txbuf_rdptr, 0);
BL_SETL(ringhd->txbuf_wrptr, 0);
BL_SETL(ringhd->rxbuf_addr, txbuf_addr + txbufsize);
BL_SETL(ringhd->rxbuf_size, rxbufsize);
BL_SETL(ringhd->rxbuf_rdptr, 0);
BL_SETL(ringhd->rxbuf_wrptr, 0);
ring = &sbuf->rings[i];
ring->header = ringhd;
ring->txbuf_virt = sbuf->smem_virt + hsize +
(txbufsize + rxbufsize) * i;
ring->rxbuf_virt = ring->txbuf_virt + txbufsize;
/* init header op */
hd_op = &ring->header_op;
hd_op->rx_rd_p = &ringhd->rxbuf_rdptr;
hd_op->rx_wt_p = &ringhd->rxbuf_wrptr;
hd_op->rx_size = ringhd->rxbuf_size;
hd_op->tx_rd_p = &ringhd->txbuf_rdptr;
hd_op->tx_wt_p = &ringhd->txbuf_wrptr;
hd_op->tx_size = ringhd->txbuf_size;
}
/* release resource */
sipc_smem_release_resource(sipc->sipc_pms, sipc->dst);
sbuf_comm_init(sbuf);
return 0;
}
static int sbuf_client_init(struct smsg_ipc *sipc, struct sbuf_mgr *sbuf)
{
VOLA_SBUF_SMEM *smem;
VOLA_SBUF_RING *ringhd;
struct sbuf_ring_header_op *hd_op;
struct sbuf_ring *ring;
int hsize, i, rval;
u32 txbufsize, rxbufsize;
phys_addr_t offset = 0;
u32 bufnum;
u8 dst = sbuf->dst;
#ifdef CONFIG_PHYS_ADDR_T_64BIT
offset = sipc->high_offset;
offset = offset << 32;
pr_info("%s: channel %d-%d, offset = 0x%llx!\n",
__func__, sbuf->dst, sbuf->channel, offset);
#endif
/* get bufnum and bufsize */
hsize = sizeof(struct sbuf_smem_header) +
sizeof(struct sbuf_ring_header) * 1;
sbuf->smem_virt = shmem_ram_vmap_nocache(dst,
sbuf->smem_addr + offset,
hsize);
if (!sbuf->smem_virt) {
pr_err("%s: channel %d-%d, Failed to map smem for sbuf head\n",
__func__, sbuf->dst, sbuf->channel);
return -EFAULT;
}
smem = (VOLA_SBUF_SMEM *)sbuf->smem_virt;
sbuf->ringnr = smem->ringnr;
bufnum = sbuf->ringnr;
ringhd = (VOLA_SBUF_RING *)&smem->headers[0];
txbufsize = BL_GETL(ringhd->rxbuf_size);
rxbufsize = BL_GETL(ringhd->txbuf_size);
hsize = sizeof(struct sbuf_smem_header) +
sizeof(struct sbuf_ring_header) * bufnum;
sbuf->smem_size = hsize + (txbufsize + rxbufsize) * bufnum;
pr_debug("%s: channel %d-%d, txbufsize = 0x%x, rxbufsize = 0x%x!\n",
__func__, sbuf->dst, sbuf->channel, txbufsize, rxbufsize);
pr_debug("%s: channel %d-%d, smem_size = 0x%x, ringnr = %d!\n",
__func__, sbuf->dst, sbuf->channel, sbuf->smem_size, bufnum);
shmem_ram_unmap(dst, sbuf->smem_virt);
/* alloc debug smem */
sbuf->smem_addr_debug = smem_alloc(dst, sbuf->smem_size);
if (!sbuf->smem_addr_debug) {
pr_err("%s: channel %d-%d,Failed to allocate debug smem for sbuf\n",
__func__, sbuf->dst, sbuf->channel);
return -ENOMEM;
}
/* get smem virtual address */
sbuf->smem_virt = shmem_ram_vmap_nocache(dst,
sbuf->smem_addr + offset,
sbuf->smem_size);
if (!sbuf->smem_virt) {
pr_err("%s: channel %d-%d,Failed to map smem for sbuf\n",
__func__, sbuf->dst, sbuf->channel);
smem_free(dst, sbuf->smem_addr_debug, sbuf->smem_size);
return -EFAULT;
}
/* allocate rings description */
sbuf->rings = kcalloc(bufnum, sizeof(struct sbuf_ring), GFP_KERNEL);
if (!sbuf->rings) {
smem_free(dst, sbuf->smem_addr_debug, sbuf->smem_size);
shmem_ram_unmap(dst, sbuf->smem_virt);
return -ENOMEM;
}
pr_info("%s: channel %d-%d, ringns = 0x%p!\n",
__func__, sbuf->dst, sbuf->channel, sbuf->rings);
/* must request resource before read or write share memory */
rval = sipc_smem_request_resource(sipc->sipc_pms, sipc->dst, -1);
if (rval < 0) {
smem_free(dst, sbuf->smem_addr, sbuf->smem_size);
shmem_ram_unmap(dst, sbuf->smem_virt);
kfree(sbuf->rings);
return rval;
}
/* initialize all ring bufs */
smem = (VOLA_SBUF_SMEM *)sbuf->smem_virt;
for (i = 0; i < bufnum; i++) {
ringhd = (VOLA_SBUF_RING *)&smem->headers[i];
ring = &sbuf->rings[i];
ring->header = ringhd;
/* host txbuf_addr */
ring->rxbuf_virt = sbuf->smem_virt + hsize +
(txbufsize + rxbufsize) * i;
/* host rxbuf_addr */
ring->txbuf_virt = ring->rxbuf_virt + rxbufsize;
/* init header op , client mode, rx <==> tx */
hd_op = &ring->header_op;
hd_op->rx_rd_p = &ringhd->txbuf_rdptr;
hd_op->rx_wt_p = &ringhd->txbuf_wrptr;
hd_op->rx_size = ringhd->txbuf_size;
hd_op->tx_rd_p = &ringhd->rxbuf_rdptr;
hd_op->tx_wt_p = &ringhd->rxbuf_wrptr;
hd_op->tx_size = ringhd->rxbuf_size;
}
/* release resource */
sipc_smem_release_resource(sipc->sipc_pms, sipc->dst);
sbuf_comm_init(sbuf);
return 0;
}
static int sbuf_thread(void *data)
{
struct sbuf_mgr *sbuf = data;
struct sbuf_ring *ring;
struct smsg mcmd, mrecv;
int rval, bufid;
struct smsg_ipc *sipc;
unsigned long flags;
/* since the channel open may hang, we call it in the sbuf thread */
rval = smsg_ch_open(sbuf->dst, sbuf->channel, -1);
if (rval != 0) {
pr_err("Failed to open channel %d\n", sbuf->channel);
/* assign NULL to thread poniter as failed to open channel */
sbuf->thread = NULL;
return rval;
}
/* if client, send SMSG_CMD_SBUF_INIT, wait sbuf SMSG_DONE_SBUF_INIT */
sipc = smsg_ipcs[sbuf->dst];
if (sipc->client) {
smsg_set(&mcmd, sbuf->channel, SMSG_TYPE_CMD,
SMSG_CMD_SBUF_INIT, 0);
smsg_send(sbuf->dst, &mcmd, -1);
do {
smsg_set(&mrecv, sbuf->channel, 0, 0, 0);
rval = smsg_recv(sbuf->dst, &mrecv, -1);
if (rval != 0) {
sbuf->thread = NULL;
return rval;
}
} while (mrecv.type != SMSG_TYPE_DONE ||
mrecv.flag != SMSG_DONE_SBUF_INIT);
sbuf->smem_addr = mrecv.value;
pr_info("%s: channel %d-%d, done_sbuf_init, address = 0x%x!\n",
__func__, sbuf->dst, sbuf->channel, sbuf->smem_addr);
if (sbuf_client_init(sipc, sbuf)) {
sbuf->thread = NULL;
return 0;
}
sbuf->state = SBUF_STATE_READY;
}
/* sbuf init done, handle the ring rx events */
while (!kthread_should_stop()) {
/* monitor sbuf rdptr/wrptr update smsg */
smsg_set(&mrecv, sbuf->channel, 0, 0, 0);
rval = smsg_recv(sbuf->dst, &mrecv, -1);
if (rval == -EIO) {
/* channel state is free */
msleep(20);
continue;
}
pr_debug("sbuf thread recv msg: dst=%d, channel=%d, type=%d, flag=0x%04x, value=0x%08x\n",
sbuf->dst,
sbuf->channel,
mrecv.type,
mrecv.flag,
mrecv.value);
switch (mrecv.type) {
case SMSG_TYPE_OPEN:
pr_info("%s: channel %d-%d, state=%d, recv open msg!\n",
__func__, sbuf->dst,
sbuf->channel, sbuf->state);
if (sipc->client)
break;
/* if channel state is already reay, reopen it
* (such as modem reset), we must skip the old
* buf data , than give open ack and reset state
* to idle
*/
if (sbuf->state == SBUF_STATE_READY) {
sbuf_skip_old_data(sbuf);
sbuf->state = SBUF_STATE_IDLE;
}
/* handle channel open */
smsg_open_ack(sbuf->dst, sbuf->channel);
break;
case SMSG_TYPE_CLOSE:
/* handle channel close */
sbuf_skip_old_data(sbuf);
smsg_close_ack(sbuf->dst, sbuf->channel);
sbuf->state = SBUF_STATE_IDLE;
break;
case SMSG_TYPE_CMD:
pr_info("%s: channel %d-%d state = %d, recv cmd msg, flag = %d!\n",
__func__, sbuf->dst, sbuf->channel,
sbuf->state, mrecv.flag);
if (sipc->client)
break;
/* respond cmd done for sbuf init only state is idle */
if (sbuf->state == SBUF_STATE_IDLE &&
mrecv.flag == SMSG_CMD_SBUF_INIT) {
smsg_set(&mcmd,
sbuf->channel,
SMSG_TYPE_DONE,
SMSG_DONE_SBUF_INIT,
sbuf->dst_smem_addr);
smsg_send(sbuf->dst, &mcmd, -1);
sbuf->state = SBUF_STATE_READY;
for (bufid = 0; bufid < sbuf->ringnr; bufid++) {
ring = &sbuf->rings[bufid];
if (ring->handler)
ring->handler(SBUF_NOTIFY_READY,
ring->data);
}
}
break;
case SMSG_TYPE_EVENT:
bufid = mrecv.value;
WARN_ON(bufid >= sbuf->ringnr);
ring = &sbuf->rings[bufid];
switch (mrecv.flag) {
case SMSG_EVENT_SBUF_RDPTR:
if (ring->need_wake_lock)
sprd_pms_request_wakelock_period(ring->tx_pms,
500);
/* set write mask. */
spin_lock_irqsave(&ring->poll_lock, flags);
ring->poll_mask |= POLLOUT | POLLWRNORM;
spin_unlock_irqrestore(&ring->poll_lock, flags);
wake_up_interruptible_all(&ring->txwait);
if (ring->handler)
ring->handler(SBUF_NOTIFY_WRITE,
ring->data);
break;
case SMSG_EVENT_SBUF_WRPTR:
/* set read mask. */
spin_lock_irqsave(&ring->poll_lock, flags);
ring->poll_mask |= POLLIN | POLLRDNORM;
spin_unlock_irqrestore(&ring->poll_lock, flags);
if (ring->need_wake_lock)
sprd_pms_request_wakelock_period(ring->rx_pms,
500);
wake_up_interruptible_all(&ring->rxwait);
if (ring->handler)
ring->handler(SBUF_NOTIFY_READ,
ring->data);
break;
default:
rval = 1;
break;
}
break;
default:
rval = 1;
break;
};
if (rval) {
pr_info("non-handled sbuf msg: %d-%d, %d, %d, %d\n",
sbuf->dst,
sbuf->channel,
mrecv.type,
mrecv.flag,
mrecv.value);
rval = 0;
}
/* unlock sipc channel wake lock */
smsg_ch_wake_unlock(sbuf->dst, sbuf->channel);
}
return 0;
}
int sbuf_create(u8 dst, u8 channel, u32 bufnum, u32 txbufsize, u32 rxbufsize)
{
struct sbuf_mgr *sbuf;
u8 ch_index;
int ret;
struct smsg_ipc *sipc = NULL;
struct sched_param param = {.sched_priority = 10};
sipc = smsg_ipcs[dst];
ch_index = sipc_channel2index(channel);
if (ch_index == INVALID_CHANEL_INDEX) {
pr_err("%s:channel %d invalid!\n", __func__, channel);
return -EINVAL;
}
pr_debug("%s dst=%d, chanel=%d, bufnum=%d, txbufsize=0x%x, rxbufsize=0x%x\n",
__func__,
dst,
channel,
bufnum,
txbufsize,
rxbufsize);
if (dst >= SIPC_ID_NR || !sipc) {
pr_err("%s: dst = %d is invalid\n", __func__, dst);
return -EINVAL;
}
sbuf = kzalloc(sizeof(*sbuf), GFP_KERNEL);
if (!sbuf)
return -ENOMEM;
sbuf->state = SBUF_STATE_IDLE;
sbuf->dst = dst;
sbuf->channel = channel;
/* The dst smem type is pcie, need force send smsg in sbuf_write. */
if (sipc->type == SIPC_BASE_PCIE && sipc->smem_type == SMEM_LOCAL)
sbuf->force_send = true;
if (!sipc->client) {
ret = sbuf_host_init(sipc, sbuf, bufnum, txbufsize, rxbufsize);
if (ret) {
kfree(sbuf);
return ret;
}
}
sbuf->thread = kthread_create(sbuf_thread, sbuf,
"sbuf-%d-%d", dst, channel);
if (IS_ERR(sbuf->thread)) {
pr_err("Failed to create kthread: sbuf-%d-%d\n", dst, channel);
if (!sipc->client) {
kfree(sbuf->rings);
shmem_ram_unmap(dst, sbuf->smem_virt);
smem_free(dst, sbuf->smem_addr, sbuf->smem_size);
}
ret = PTR_ERR(sbuf->thread);
kfree(sbuf);
return ret;
}
sbufs[dst][ch_index] = sbuf;
/*set the thread as a real time thread, and its priority is 10*/
sched_setscheduler(sbuf->thread, SCHED_FIFO, ¶m);
wake_up_process(sbuf->thread);
return 0;
}
EXPORT_SYMBOL_GPL(sbuf_create);
void sbuf_set_no_need_wake_lock(u8 dst, u8 channel, u32 bufnum)
{
u8 ch_index;
struct sbuf_mgr *sbuf;
struct sbuf_ring *ring = NULL;
ch_index = sipc_channel2index(channel);
if (ch_index == INVALID_CHANEL_INDEX) {
pr_err("%s:channel %d invalid!\n", __func__, channel);
return;
}
sbuf = sbufs[dst][ch_index];
if (!sbuf || sbuf->ringnr <= bufnum)
return;
ring = &sbuf->rings[bufnum];
ring->need_wake_lock = false;
}
EXPORT_SYMBOL_GPL(sbuf_set_no_need_wake_lock);
void sbuf_down(u8 dst, u8 channel)
{
struct sbuf_mgr *sbuf;
u8 ch_index;
int i;
ch_index = sipc_channel2index(channel);
if (ch_index == INVALID_CHANEL_INDEX) {
pr_err("%s:channel %d invalid!\n", __func__, channel);
return;
}
sbuf = sbufs[dst][ch_index];
if (!sbuf)
return;
sbuf->state = SBUF_STATE_IDLE;
if (sbuf->rings) {
for (i = 0; i < sbuf->ringnr; i++) {
wake_up_interruptible_all(&sbuf->rings[i].txwait);
wake_up_interruptible_all(&sbuf->rings[i].rxwait);
}
}
pr_info("%s: channel=%d sbuf down success\n", __func__, channel);
}
void sbuf_destroy(u8 dst, u8 channel)
{
struct sbuf_mgr *sbuf;
int i;
u8 ch_index;
struct smsg_ipc *sipc;
ch_index = sipc_channel2index(channel);
if (ch_index == INVALID_CHANEL_INDEX) {
pr_err("%s:channel %d invalid!\n", __func__, channel);
return;
}
sbuf = sbufs[dst][ch_index];
if (!sbuf)
return;
sbuf->state = SBUF_STATE_IDLE;
smsg_ch_close(dst, channel, -1);
/* stop sbuf thread if it's created successfully and still alive */
if (!IS_ERR_OR_NULL(sbuf->thread))
kthread_stop(sbuf->thread);
if (sbuf->rings) {
for (i = 0; i < sbuf->ringnr; i++) {
wake_up_interruptible_all(&sbuf->rings[i].txwait);
wake_up_interruptible_all(&sbuf->rings[i].rxwait);
#if defined(SIPC_DEBUG_SBUF_RDWT_OWNER)
sbuf_destroy_rdwt_owner(&sbuf->rings[i]);
#endif
sprd_pms_destroy(sbuf->rings[i].tx_pms);
sprd_pms_destroy(sbuf->rings[i].rx_pms);
}
kfree(sbuf->rings);
}
if (sbuf->smem_virt)
shmem_ram_unmap(dst, sbuf->smem_virt);
sipc = smsg_ipcs[dst];
if (sipc->client)
smem_free(dst, sbuf->smem_addr_debug, sbuf->smem_size);
else
smem_free(dst, sbuf->smem_addr, sbuf->smem_size);
kfree(sbuf);
sbufs[dst][ch_index] = NULL;
}
EXPORT_SYMBOL_GPL(sbuf_destroy);
int sbuf_write(u8 dst, u8 channel, u32 bufid,
void *buf, u32 len, int timeout)
{
struct sbuf_mgr *sbuf;
struct sbuf_ring *ring = NULL;
struct sbuf_ring_header_op *hd_op;
struct smsg mevt;
void *txpos;
int rval, left, tail, txsize;
u8 ch_index;
union sbuf_buf u_buf;
bool no_data;
unsigned long flags;
u32 v;
u_buf.buf = buf;
ch_index = sipc_channel2index(channel);
if (ch_index == INVALID_CHANEL_INDEX) {
pr_err("%s:channel %d invalid!\n", __func__, channel);
return -EINVAL;
}
sbuf = sbufs[dst][ch_index];
if (!sbuf)
return -ENODEV;
ring = &sbuf->rings[bufid];
hd_op = &ring->header_op;
if (sbuf->state != SBUF_STATE_READY) {
pr_info("sbuf-%d-%d not ready to write!\n",
dst, channel);
return -ENODEV;
}
pr_debug("%s: dst=%d, channel=%d, bufid=%d, len=%d, timeout=%d\n",
__func__,
dst,
channel,
bufid,
len,
timeout);
rval = 0;
left = len;
if (timeout) {
mutex_lock_interruptible(&ring->txlock);
} else {
if (!mutex_trylock(&ring->txlock)) {
pr_debug("sbuf_read busy, dst=%d, channel=%d, bufid=%d\n",
dst, channel, bufid);
return -EBUSY;
}
}
#if defined(SIPC_DEBUG_SBUF_RDWT_OWNER)
sbuf_record_rdwt_owner(ring, 0);
#endif
/* must request resource before read or write share memory */
rval = sipc_smem_request_resource(ring->tx_pms, sbuf->dst, -1);
if (rval < 0) {
mutex_unlock(&ring->txlock);
return rval;
}
pr_debug("%s: channel=%d, wrptr=%d, rdptr=%d\n",
__func__,
channel,
BL_READL(hd_op->tx_wt_p),
BL_READL(hd_op->tx_rd_p));
no_data = ((int)(BL_READL(hd_op->tx_wt_p) - BL_READL(hd_op->tx_rd_p)) >=
hd_op->tx_size);
/* update write mask */
spin_lock_irqsave(&ring->poll_lock, flags);
if (no_data)
ring->poll_mask &= ~(POLLOUT | POLLWRNORM);
else
ring->poll_mask |= POLLOUT | POLLWRNORM;
spin_unlock_irqrestore(&ring->poll_lock, flags);
/* release resource */
sipc_smem_release_resource(ring->tx_pms, sbuf->dst);
if (no_data) {
if (timeout == 0) {
pr_info("%s: %d-%d ring %d txbuf is full!\n",
__func__, dst, channel, bufid);
rval = -EBUSY;
} else if (timeout < 0) {
/* wait forever */
rval = wait_event_interruptible(
ring->txwait,
sbuf_has_data(ring, dst, true) ||
sbuf->state == SBUF_STATE_IDLE);
if (rval < 0)
pr_debug("%s: wait interrupted!\n", __func__);
if (sbuf->state == SBUF_STATE_IDLE) {
pr_err("%s: sbuf state is idle!\n", __func__);
rval = -EIO;
}
} else {
/* wait timeout */
rval = wait_event_interruptible_timeout(
ring->txwait,
sbuf_has_data(ring, dst, true) ||
sbuf->state == SBUF_STATE_IDLE,
timeout);
if (rval < 0) {
pr_debug("%s: wait interrupted!\n", __func__);
} else if (rval == 0) {
pr_info("%s: wait timeout!\n", __func__);
rval = -ETIME;
}
if (sbuf->state == SBUF_STATE_IDLE) {
pr_err("%s: sbuf state is idle!\n", __func__);
rval = -EIO;
}
}
}
if (rval < 0) {
mutex_unlock(&ring->txlock);
return rval;
}
/* must request resource before read or write share memory */
rval = sipc_smem_request_resource(ring->tx_pms, sbuf->dst, -1);
if (rval < 0) {
mutex_unlock(&ring->txlock);
return rval;
}