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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
|
/* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* 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.
*
*/
#define pr_fmt(fmt) "%s: " fmt, __func__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/bug.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/of_address.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/device.h>
#include <linux/notifier.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/rbtree.h>
#include <soc/qcom/rpm-notifier.h>
#include <soc/qcom/rpm-smd.h>
#include <soc/qcom/smd.h>
#include <soc/qcom/glink_rpm_xprt.h>
#include <soc/qcom/glink.h>
#define CREATE_TRACE_POINTS
#include <trace/events/trace_rpm_smd.h>
/* Debug Definitions */
enum {
MSM_RPM_LOG_REQUEST_PRETTY = BIT(0),
MSM_RPM_LOG_REQUEST_RAW = BIT(1),
MSM_RPM_LOG_REQUEST_SHOW_MSG_ID = BIT(2),
};
static int msm_rpm_debug_mask;
module_param_named(
debug_mask, msm_rpm_debug_mask, int, S_IRUGO | S_IWUSR
);
struct msm_rpm_driver_data {
const char *ch_name;
uint32_t ch_type;
smd_channel_t *ch_info;
struct work_struct work;
spinlock_t smd_lock_write;
spinlock_t smd_lock_read;
struct completion smd_open;
};
struct glink_apps_rpm_data {
const char *name;
const char *edge;
const char *xprt;
void *glink_handle;
struct glink_link_info *link_info;
struct glink_open_config *open_cfg;
struct work_struct work;
};
static bool glink_enabled;
static struct glink_apps_rpm_data *glink_data;
#define DEFAULT_BUFFER_SIZE 256
#define DEBUG_PRINT_BUFFER_SIZE 512
#define MAX_SLEEP_BUFFER 128
#define GFP_FLAG(noirq) (noirq ? GFP_ATOMIC : GFP_NOIO)
#define INV_RSC "resource does not exist"
#define ERR "err\0"
#define MAX_ERR_BUFFER_SIZE 128
#define MAX_WAIT_ON_ACK 24
#define INIT_ERROR 1
#define V1_PROTOCOL_VERSION 0x31726576 /* rev1 */
#define V0_PROTOCOL_VERSION 0 /* rev0 */
#define RPM_MSG_TYPE_OFFSET 16
#define RPM_MSG_TYPE_SIZE 8
#define RPM_SET_TYPE_OFFSET 28
#define RPM_SET_TYPE_SIZE 4
#define RPM_REQ_LEN_OFFSET 0
#define RPM_REQ_LEN_SIZE 16
#define RPM_MSG_VERSION_OFFSET 24
#define RPM_MSG_VERSION_SIZE 8
#define RPM_MSG_VERSION 1
#define RPM_MSG_SET_OFFSET 28
#define RPM_MSG_SET_SIZE 4
#define RPM_RSC_ID_OFFSET 16
#define RPM_RSC_ID_SIZE 12
#define RPM_DATA_LEN_OFFSET 0
#define RPM_DATA_LEN_SIZE 16
#define RPM_HDR_SIZE ((rpm_msg_fmt_ver == RPM_MSG_V0_FMT) ?\
sizeof(struct rpm_v0_hdr) : sizeof(struct rpm_v1_hdr))
#define CLEAR_FIELD(offset, size) (~GENMASK(offset + size - 1, offset))
static ATOMIC_NOTIFIER_HEAD(msm_rpm_sleep_notifier);
static bool standalone;
static int probe_status = -EPROBE_DEFER;
static int msm_rpm_read_smd_data(char *buf);
static void msm_rpm_process_ack(uint32_t msg_id, int errno);
int msm_rpm_register_notifier(struct notifier_block *nb)
{
return atomic_notifier_chain_register(&msm_rpm_sleep_notifier, nb);
}
int msm_rpm_unregister_notifier(struct notifier_block *nb)
{
return atomic_notifier_chain_unregister(&msm_rpm_sleep_notifier, nb);
}
static struct workqueue_struct *msm_rpm_smd_wq;
enum {
MSM_RPM_MSG_REQUEST_TYPE = 0,
MSM_RPM_MSG_TYPE_NR,
};
static const uint32_t msm_rpm_request_service_v1[MSM_RPM_MSG_TYPE_NR] = {
0x716572, /* 'req\0' */
};
enum {
RPM_V1_REQUEST_SERVICE,
RPM_V1_SYSTEMDB_SERVICE,
RPM_V1_COMMAND_SERVICE,
RPM_V1_ACK_SERVICE,
RPM_V1_NACK_SERVICE,
} msm_rpm_request_service_v2;
struct rpm_v0_hdr {
uint32_t service_type;
uint32_t request_len;
};
struct rpm_v1_hdr {
uint32_t request_hdr;
};
struct rpm_message_header_v0 {
struct rpm_v0_hdr hdr;
uint32_t msg_id;
enum msm_rpm_set set;
uint32_t resource_type;
uint32_t resource_id;
uint32_t data_len;
};
struct rpm_message_header_v1 {
struct rpm_v1_hdr hdr;
uint32_t msg_id;
uint32_t resource_type;
uint32_t request_details;
};
struct msm_rpm_ack_msg_v0 {
uint32_t req;
uint32_t req_len;
uint32_t rsc_id;
uint32_t msg_len;
uint32_t id_ack;
};
struct msm_rpm_ack_msg_v1 {
uint32_t request_hdr;
uint32_t id_ack;
};
struct kvp {
unsigned int k;
unsigned int s;
};
struct msm_rpm_kvp_data {
uint32_t key;
uint32_t nbytes; /* number of bytes */
uint8_t *value;
bool valid;
};
struct slp_buf {
struct rb_node node;
char ubuf[MAX_SLEEP_BUFFER];
char *buf;
bool valid;
};
enum rpm_msg_fmts {
RPM_MSG_V0_FMT,
RPM_MSG_V1_FMT
};
static uint32_t rpm_msg_fmt_ver;
module_param_named(
rpm_msg_fmt_ver, rpm_msg_fmt_ver, uint, S_IRUGO
);
static struct rb_root tr_root = RB_ROOT;
static int (*msm_rpm_send_buffer)(char *buf, uint32_t size, bool noirq);
static int msm_rpm_send_smd_buffer(char *buf, uint32_t size, bool noirq);
static int msm_rpm_glink_send_buffer(char *buf, uint32_t size, bool noirq);
static uint32_t msm_rpm_get_next_msg_id(void);
static inline uint32_t get_offset_value(uint32_t val, uint32_t offset,
uint32_t size)
{
return (((val) & GENMASK(offset + size - 1, offset))
>> offset);
}
static inline void change_offset_value(uint32_t *val, uint32_t offset,
uint32_t size, int32_t val1)
{
uint32_t member = *val;
uint32_t offset_val = get_offset_value(member, offset, size);
uint32_t mask = (1 << size) - 1;
offset_val += val1;
*val &= CLEAR_FIELD(offset, size);
*val |= ((offset_val & mask) << offset);
}
static inline void set_offset_value(uint32_t *val, uint32_t offset,
uint32_t size, uint32_t val1)
{
uint32_t mask = (1 << size) - 1;
*val &= CLEAR_FIELD(offset, size);
*val |= ((val1 & mask) << offset);
}
static uint32_t get_msg_id(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct rpm_message_header_v0 *)buf)->msg_id;
return ((struct rpm_message_header_v1 *)buf)->msg_id;
}
static uint32_t get_ack_msg_id(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct msm_rpm_ack_msg_v0 *)buf)->id_ack;
return ((struct msm_rpm_ack_msg_v1 *)buf)->id_ack;
}
static uint32_t get_rsc_type(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct rpm_message_header_v0 *)buf)->resource_type;
return ((struct rpm_message_header_v1 *)buf)->resource_type;
}
static uint32_t get_set_type(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct rpm_message_header_v0 *)buf)->set;
return get_offset_value(((struct rpm_message_header_v1 *)buf)->
request_details, RPM_SET_TYPE_OFFSET,
RPM_SET_TYPE_SIZE);
}
static uint32_t get_data_len(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct rpm_message_header_v0 *)buf)->data_len;
return get_offset_value(((struct rpm_message_header_v1 *)buf)->
request_details, RPM_DATA_LEN_OFFSET,
RPM_DATA_LEN_SIZE);
}
static uint32_t get_rsc_id(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct rpm_message_header_v0 *)buf)->resource_id;
return get_offset_value(((struct rpm_message_header_v1 *)buf)->
request_details, RPM_RSC_ID_OFFSET,
RPM_RSC_ID_SIZE);
}
static uint32_t get_ack_req_len(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct msm_rpm_ack_msg_v0 *)buf)->req_len;
return get_offset_value(((struct msm_rpm_ack_msg_v1 *)buf)->
request_hdr, RPM_REQ_LEN_OFFSET,
RPM_REQ_LEN_SIZE);
}
static uint32_t get_ack_msg_type(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct msm_rpm_ack_msg_v0 *)buf)->req;
return get_offset_value(((struct msm_rpm_ack_msg_v1 *)buf)->
request_hdr, RPM_MSG_TYPE_OFFSET,
RPM_MSG_TYPE_SIZE);
}
static uint32_t get_req_len(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return ((struct rpm_message_header_v0 *)buf)->hdr.request_len;
return get_offset_value(((struct rpm_message_header_v1 *)buf)->
hdr.request_hdr, RPM_REQ_LEN_OFFSET,
RPM_REQ_LEN_SIZE);
}
static void set_msg_ver(char *buf, uint32_t val)
{
if (rpm_msg_fmt_ver) {
set_offset_value(&((struct rpm_message_header_v1 *)buf)->
hdr.request_hdr, RPM_MSG_VERSION_OFFSET,
RPM_MSG_VERSION_SIZE, val);
} else {
set_offset_value(&((struct rpm_message_header_v1 *)buf)->
hdr.request_hdr, RPM_MSG_VERSION_OFFSET,
RPM_MSG_VERSION_SIZE, 0);
}
}
static void set_req_len(char *buf, uint32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT) {
((struct rpm_message_header_v0 *)buf)->hdr.request_len = val;
} else {
set_offset_value(&((struct rpm_message_header_v1 *)buf)->
hdr.request_hdr, RPM_REQ_LEN_OFFSET,
RPM_REQ_LEN_SIZE, val);
}
}
static void change_req_len(char *buf, int32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT) {
((struct rpm_message_header_v0 *)buf)->hdr.request_len += val;
} else {
change_offset_value(&((struct rpm_message_header_v1 *)buf)->
hdr.request_hdr, RPM_REQ_LEN_OFFSET,
RPM_REQ_LEN_SIZE, val);
}
}
static void set_msg_type(char *buf, uint32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT) {
((struct rpm_message_header_v0 *)buf)->hdr.service_type =
msm_rpm_request_service_v1[val];
} else {
set_offset_value(&((struct rpm_message_header_v1 *)buf)->
hdr.request_hdr, RPM_MSG_TYPE_OFFSET,
RPM_MSG_TYPE_SIZE, RPM_V1_REQUEST_SERVICE);
}
}
static void set_rsc_id(char *buf, uint32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
((struct rpm_message_header_v0 *)buf)->resource_id = val;
else
set_offset_value(&((struct rpm_message_header_v1 *)buf)->
request_details, RPM_RSC_ID_OFFSET,
RPM_RSC_ID_SIZE, val);
}
static void set_data_len(char *buf, uint32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
((struct rpm_message_header_v0 *)buf)->data_len = val;
else
set_offset_value(&((struct rpm_message_header_v1 *)buf)->
request_details, RPM_DATA_LEN_OFFSET,
RPM_DATA_LEN_SIZE, val);
}
static void change_data_len(char *buf, int32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
((struct rpm_message_header_v0 *)buf)->data_len += val;
else
change_offset_value(&((struct rpm_message_header_v1 *)buf)->
request_details, RPM_DATA_LEN_OFFSET,
RPM_DATA_LEN_SIZE, val);
}
static void set_set_type(char *buf, uint32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
((struct rpm_message_header_v0 *)buf)->set = val;
else
set_offset_value(&((struct rpm_message_header_v1 *)buf)->
request_details, RPM_SET_TYPE_OFFSET,
RPM_SET_TYPE_SIZE, val);
}
static void set_msg_id(char *buf, uint32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
((struct rpm_message_header_v0 *)buf)->msg_id = val;
else
((struct rpm_message_header_v1 *)buf)->msg_id = val;
}
static void set_rsc_type(char *buf, uint32_t val)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
((struct rpm_message_header_v0 *)buf)->resource_type = val;
else
((struct rpm_message_header_v1 *)buf)->resource_type = val;
}
static inline int get_buf_len(char *buf)
{
return get_req_len(buf) + RPM_HDR_SIZE;
}
static inline struct kvp *get_first_kvp(char *buf)
{
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
return (struct kvp *)(buf +
sizeof(struct rpm_message_header_v0));
else
return (struct kvp *)(buf +
sizeof(struct rpm_message_header_v1));
}
static inline struct kvp *get_next_kvp(struct kvp *k)
{
return (struct kvp *)((void *)k + sizeof(*k) + k->s);
}
static inline void *get_data(struct kvp *k)
{
return (void *)k + sizeof(*k);
}
static void delete_kvp(char *buf, struct kvp *d)
{
struct kvp *n;
int dec;
uint32_t size;
n = get_next_kvp(d);
dec = (void *)n - (void *)d;
size = get_data_len(buf) -
((void *)n - (void *)get_first_kvp(buf));
memcpy((void *)d, (void *)n, size);
change_data_len(buf, -dec);
change_req_len(buf, -dec);
}
static inline void update_kvp_data(struct kvp *dest, struct kvp *src)
{
memcpy(get_data(dest), get_data(src), src->s);
}
static void add_kvp(char *buf, struct kvp *n)
{
int32_t inc = sizeof(*n) + n->s;
if (get_req_len(buf) + inc > MAX_SLEEP_BUFFER) {
WARN_ON(get_req_len(buf) + inc > MAX_SLEEP_BUFFER);
return;
}
memcpy(buf + get_buf_len(buf), n, inc);
change_data_len(buf, inc);
change_req_len(buf, inc);
}
static struct slp_buf *tr_search(struct rb_root *root, char *slp)
{
unsigned int type = get_rsc_type(slp);
unsigned int id = get_rsc_id(slp);
struct rb_node *node = root->rb_node;
while (node) {
struct slp_buf *cur = rb_entry(node, struct slp_buf, node);
unsigned int ctype = get_rsc_type(cur->buf);
unsigned int cid = get_rsc_id(cur->buf);
if (type < ctype)
node = node->rb_left;
else if (type > ctype)
node = node->rb_right;
else if (id < cid)
node = node->rb_left;
else if (id > cid)
node = node->rb_right;
else
return cur;
}
return NULL;
}
static int tr_insert(struct rb_root *root, struct slp_buf *slp)
{
unsigned int type = get_rsc_type(slp->buf);
unsigned int id = get_rsc_id(slp->buf);
struct rb_node **node = &(root->rb_node), *parent = NULL;
while (*node) {
struct slp_buf *curr = rb_entry(*node, struct slp_buf, node);
unsigned int ctype = get_rsc_type(curr->buf);
unsigned int cid = get_rsc_id(curr->buf);
parent = *node;
if (type < ctype)
node = &((*node)->rb_left);
else if (type > ctype)
node = &((*node)->rb_right);
else if (id < cid)
node = &((*node)->rb_left);
else if (id > cid)
node = &((*node)->rb_right);
else
return -EINVAL;
}
rb_link_node(&slp->node, parent, node);
rb_insert_color(&slp->node, root);
slp->valid = true;
return 0;
}
#define for_each_kvp(buf, k) \
for (k = (struct kvp *)get_first_kvp(buf); \
((void *)k - (void *)get_first_kvp(buf)) < \
get_data_len(buf);\
k = get_next_kvp(k))
static void tr_update(struct slp_buf *s, char *buf)
{
struct kvp *e, *n;
for_each_kvp(buf, n) {
bool found = false;
for_each_kvp(s->buf, e) {
if (n->k == e->k) {
found = true;
if (n->s == e->s) {
void *e_data = get_data(e);
void *n_data = get_data(n);
if (memcmp(e_data, n_data, n->s)) {
update_kvp_data(e, n);
s->valid = true;
}
} else {
delete_kvp(s->buf, e);
add_kvp(s->buf, n);
s->valid = true;
}
break;
}
}
if (!found) {
add_kvp(s->buf, n);
s->valid = true;
}
}
}
static atomic_t msm_rpm_msg_id = ATOMIC_INIT(0);
struct msm_rpm_request {
uint8_t *client_buf;
struct msm_rpm_kvp_data *kvp;
uint32_t num_elements;
uint32_t write_idx;
uint8_t *buf;
uint32_t numbytes;
};
/*
* Data related to message acknowledgment
*/
LIST_HEAD(msm_rpm_wait_list);
struct msm_rpm_wait_data {
struct list_head list;
uint32_t msg_id;
bool ack_recd;
int errno;
struct completion ack;
bool delete_on_ack;
};
DEFINE_SPINLOCK(msm_rpm_list_lock);
LIST_HEAD(msm_rpm_ack_list);
static struct tasklet_struct data_tasklet;
static inline uint32_t msm_rpm_get_msg_id_from_ack(uint8_t *buf)
{
return get_ack_msg_id(buf);
}
static inline int msm_rpm_get_error_from_ack(uint8_t *buf)
{
uint8_t *tmp;
uint32_t req_len = get_ack_req_len(buf);
uint32_t msg_type = get_ack_msg_type(buf);
int rc = -ENODEV;
uint32_t err;
uint32_t ack_msg_size = rpm_msg_fmt_ver ?
sizeof(struct msm_rpm_ack_msg_v1) :
sizeof(struct msm_rpm_ack_msg_v0);
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT &&
msg_type == RPM_V1_ACK_SERVICE) {
return 0;
} else if (rpm_msg_fmt_ver && msg_type == RPM_V1_NACK_SERVICE) {
err = *(uint32_t *)(buf + sizeof(struct msm_rpm_ack_msg_v1));
return err;
}
req_len -= ack_msg_size;
req_len += 2 * sizeof(uint32_t);
if (!req_len)
return 0;
pr_err("%s:rpm returned error or nack req_len: %d id_ack: %d\n",
__func__, req_len, get_ack_msg_id(buf));
tmp = buf + ack_msg_size;
if (memcmp(tmp, ERR, sizeof(uint32_t))) {
pr_err("%s rpm returned error\n", __func__);
BUG_ON(1);
}
tmp += 2 * sizeof(uint32_t);
if (!(memcmp(tmp, INV_RSC, min_t(uint32_t, req_len,
sizeof(INV_RSC))-1))) {
pr_err("%s(): RPM NACK Unsupported resource\n", __func__);
rc = -EINVAL;
} else {
pr_err("%s(): RPM NACK Invalid header\n", __func__);
}
return rc;
}
int msm_rpm_smd_buffer_request(struct msm_rpm_request *cdata,
uint32_t size, gfp_t flag)
{
struct slp_buf *slp;
static DEFINE_SPINLOCK(slp_buffer_lock);
unsigned long flags;
char *buf;
buf = cdata->buf;
if (size > MAX_SLEEP_BUFFER)
return -ENOMEM;
spin_lock_irqsave(&slp_buffer_lock, flags);
slp = tr_search(&tr_root, buf);
if (!slp) {
slp = kzalloc(sizeof(struct slp_buf), GFP_ATOMIC);
if (!slp) {
spin_unlock_irqrestore(&slp_buffer_lock, flags);
return -ENOMEM;
}
slp->buf = PTR_ALIGN(&slp->ubuf[0], sizeof(u32));
memcpy(slp->buf, buf, size);
if (tr_insert(&tr_root, slp))
pr_err("Error updating sleep request\n");
} else {
/* handle unsent requests */
tr_update(slp, buf);
}
trace_rpm_smd_sleep_set(get_msg_id(cdata->client_buf),
get_rsc_type(cdata->client_buf),
get_req_len(cdata->client_buf));
spin_unlock_irqrestore(&slp_buffer_lock, flags);
return 0;
}
static struct msm_rpm_driver_data msm_rpm_data = {
.smd_open = COMPLETION_INITIALIZER(msm_rpm_data.smd_open),
};
static int msm_rpm_glink_rx_poll(void *glink_handle)
{
int ret;
ret = glink_rpm_rx_poll(glink_handle);
if (ret >= 0)
/*
* Sleep for 50us at a time before checking
* for packet availability. The 50us is based
* on the the time rpm could take to process
* and send an ack for the sleep set request.
*/
udelay(50);
else
pr_err("Not receieve an ACK from RPM. ret = %d\n", ret);
return ret;
}
/*
* Returns
* = 0 on successful reads
* > 0 on successful reads with no further data
* standard Linux error codes on failure.
*/
static int msm_rpm_read_sleep_ack(void)
{
int ret;
char buf[MAX_ERR_BUFFER_SIZE] = {0};
if (glink_enabled)
ret = msm_rpm_glink_rx_poll(glink_data->glink_handle);
else {
ret = msm_rpm_read_smd_data(buf);
if (!ret)
ret = smd_is_pkt_avail(msm_rpm_data.ch_info);
}
return ret;
}
static int msm_rpm_flush_requests(bool print)
{
struct rb_node *t;
int ret;
int count = 0;
for (t = rb_first(&tr_root); t; t = rb_next(t)) {
struct slp_buf *s = rb_entry(t, struct slp_buf, node);
unsigned int type = get_rsc_type(s->buf);
unsigned int id = get_rsc_id(s->buf);
if (!s->valid)
continue;
set_msg_id(s->buf, msm_rpm_get_next_msg_id());
if (!glink_enabled)
ret = msm_rpm_send_smd_buffer(s->buf,
get_buf_len(s->buf), true);
else
ret = msm_rpm_glink_send_buffer(s->buf,
get_buf_len(s->buf), true);
WARN_ON(ret != get_buf_len(s->buf));
trace_rpm_smd_send_sleep_set(get_msg_id(s->buf), type, id);
s->valid = false;
count++;
/*
* RPM acks need to be handled here if we have sent 24
* messages such that we do not overrun SMD buffer. Since
* we expect only sleep sets at this point (RPM PC would be
* disallowed if we had pending active requests), we need not
* process these sleep set acks.
*/
if (count >= MAX_WAIT_ON_ACK) {
int ret = msm_rpm_read_sleep_ack();
if (ret >= 0)
count--;
else
return ret;
}
}
return 0;
}
static void msm_rpm_notify_sleep_chain(char *buf,
struct msm_rpm_kvp_data *kvp)
{
struct msm_rpm_notifier_data notif;
notif.rsc_type = get_rsc_type(buf);
notif.rsc_id = get_req_len(buf);
notif.key = kvp->key;
notif.size = kvp->nbytes;
notif.value = kvp->value;
atomic_notifier_call_chain(&msm_rpm_sleep_notifier, 0, ¬if);
}
static int msm_rpm_add_kvp_data_common(struct msm_rpm_request *handle,
uint32_t key, const uint8_t *data, int size, bool noirq)
{
uint32_t i;
uint32_t data_size, msg_size;
if (probe_status)
return probe_status;
if (!handle || !data) {
pr_err("%s(): Invalid handle/data\n", __func__);
return -EINVAL;
}
if (size < 0)
return -EINVAL;
data_size = ALIGN(size, SZ_4);
msg_size = data_size + 8;
for (i = 0; i < handle->write_idx; i++) {
if (handle->kvp[i].key != key)
continue;
if (handle->kvp[i].nbytes != data_size) {
kfree(handle->kvp[i].value);
handle->kvp[i].value = NULL;
} else {
if (!memcmp(handle->kvp[i].value, data, data_size))
return 0;
}
break;
}
if (i >= handle->num_elements) {
pr_err("Number of resources exceeds max allocated\n");
return -ENOMEM;
}
if (i == handle->write_idx)
handle->write_idx++;
if (!handle->kvp[i].value) {
handle->kvp[i].value = kzalloc(data_size, GFP_FLAG(noirq));
if (!handle->kvp[i].value) {
pr_err("Failed malloc\n");
return -ENOMEM;
}
} else {
/* We enter the else case, if a key already exists but the
* data doesn't match. In which case, we should zero the data
* out.
*/
memset(handle->kvp[i].value, 0, data_size);
}
if (!handle->kvp[i].valid)
change_data_len(handle->client_buf, msg_size);
else
change_data_len(handle->client_buf,
(data_size - handle->kvp[i].nbytes));
handle->kvp[i].nbytes = data_size;
handle->kvp[i].key = key;
memcpy(handle->kvp[i].value, data, size);
handle->kvp[i].valid = true;
return 0;
}
static struct msm_rpm_request *msm_rpm_create_request_common(
enum msm_rpm_set set, uint32_t rsc_type, uint32_t rsc_id,
int num_elements, bool noirq)
{
struct msm_rpm_request *cdata;
uint32_t buf_size;
if (probe_status)
return ERR_PTR(probe_status);
cdata = kzalloc(sizeof(struct msm_rpm_request),
GFP_FLAG(noirq));
if (!cdata) {
pr_err("Cannot allocate memory for client data\n");
goto cdata_alloc_fail;
}
if (rpm_msg_fmt_ver == RPM_MSG_V0_FMT)
buf_size = sizeof(struct rpm_message_header_v0);
else
buf_size = sizeof(struct rpm_message_header_v1);
cdata->client_buf = kzalloc(buf_size, GFP_FLAG(noirq));
if (!cdata->client_buf) {
pr_warn("Cannot allocate memory for client_buf\n");
goto client_buf_alloc_fail;
}
set_set_type(cdata->client_buf, set);
set_rsc_type(cdata->client_buf, rsc_type);
set_rsc_id(cdata->client_buf, rsc_id);
cdata->num_elements = num_elements;
cdata->write_idx = 0;
cdata->kvp = kzalloc(sizeof(struct msm_rpm_kvp_data) * num_elements,
GFP_FLAG(noirq));
if (!cdata->kvp) {
pr_warn("%s(): Cannot allocate memory for key value data\n",
__func__);
goto kvp_alloc_fail;
}
cdata->buf = kzalloc(DEFAULT_BUFFER_SIZE, GFP_FLAG(noirq));
if (!cdata->buf)
goto buf_alloc_fail;
cdata->numbytes = DEFAULT_BUFFER_SIZE;
return cdata;
buf_alloc_fail:
kfree(cdata->kvp);
kvp_alloc_fail:
kfree(cdata->client_buf);
client_buf_alloc_fail:
kfree(cdata);
cdata_alloc_fail:
return NULL;
}
void msm_rpm_free_request(struct msm_rpm_request *handle)
{
int i;
if (!handle)
return;
for (i = 0; i < handle->num_elements; i++)
kfree(handle->kvp[i].value);
kfree(handle->kvp);
kfree(handle->client_buf);
kfree(handle->buf);
kfree(handle);
}
EXPORT_SYMBOL(msm_rpm_free_request);
struct msm_rpm_request *msm_rpm_create_request(
enum msm_rpm_set set, uint32_t rsc_type,
uint32_t rsc_id, int num_elements)
{
return msm_rpm_create_request_common(set, rsc_type, rsc_id,
num_elements, false);
}
EXPORT_SYMBOL(msm_rpm_create_request);
struct msm_rpm_request *msm_rpm_create_request_noirq(
enum msm_rpm_set set, uint32_t rsc_type,
uint32_t rsc_id, int num_elements)
{
return msm_rpm_create_request_common(set, rsc_type, rsc_id,
num_elements, true);
}
EXPORT_SYMBOL(msm_rpm_create_request_noirq);
int msm_rpm_add_kvp_data(struct msm_rpm_request *handle,
uint32_t key, const uint8_t *data, int size)
{
return msm_rpm_add_kvp_data_common(handle, key, data, size, false);
}
EXPORT_SYMBOL(msm_rpm_add_kvp_data);
int msm_rpm_add_kvp_data_noirq(struct msm_rpm_request *handle,
uint32_t key, const uint8_t *data, int size)
{
return msm_rpm_add_kvp_data_common(handle, key, data, size, true);
}
EXPORT_SYMBOL(msm_rpm_add_kvp_data_noirq);
/* Runs in interrupt context */
static void msm_rpm_notify(void *data, unsigned event)
{
struct msm_rpm_driver_data *pdata = (struct msm_rpm_driver_data *)data;
BUG_ON(!pdata);
if (!(pdata->ch_info))
return;
switch (event) {
case SMD_EVENT_DATA:
tasklet_schedule(&data_tasklet);
trace_rpm_smd_interrupt_notify("interrupt notification");
break;
case SMD_EVENT_OPEN:
complete(&pdata->smd_open);
break;
case SMD_EVENT_CLOSE:
case SMD_EVENT_STATUS:
case SMD_EVENT_REOPEN_READY:
break;
default:
pr_info("Unknown SMD event\n");
}
}
bool msm_rpm_waiting_for_ack(void)
{
bool ret;
unsigned long flags;
spin_lock_irqsave(&msm_rpm_list_lock, flags);
ret = list_empty(&msm_rpm_wait_list);
spin_unlock_irqrestore(&msm_rpm_list_lock, flags);
return !ret;
}
static struct msm_rpm_wait_data *msm_rpm_get_entry_from_msg_id(uint32_t msg_id)
{
struct list_head *ptr;
struct msm_rpm_wait_data *elem = NULL;
unsigned long flags;
spin_lock_irqsave(&msm_rpm_list_lock, flags);
list_for_each(ptr, &msm_rpm_wait_list) {
elem = list_entry(ptr, struct msm_rpm_wait_data, list);
if (elem && (elem->msg_id == msg_id))
break;
elem = NULL;
}
spin_unlock_irqrestore(&msm_rpm_list_lock, flags);
return elem;
}
static uint32_t msm_rpm_get_next_msg_id(void)
{
uint32_t id;
/*
* A message id of 0 is used by the driver to indicate a error
* condition. The RPM driver uses a id of 1 to indicate unsent data
* when the data sent over hasn't been modified. This isn't a error
* scenario and wait for ack returns a success when the message id is 1.
*/
do {
id = atomic_inc_return(&msm_rpm_msg_id);
} while ((id == 0) || (id == 1) || msm_rpm_get_entry_from_msg_id(id));
return id;
}
static int msm_rpm_add_wait_list(uint32_t msg_id, bool delete_on_ack)
{
unsigned long flags;
struct msm_rpm_wait_data *data =
kzalloc(sizeof(struct msm_rpm_wait_data), GFP_ATOMIC);
if (!data)
return -ENOMEM;
init_completion(&data->ack);
data->ack_recd = false;
data->msg_id = msg_id;
data->errno = INIT_ERROR;
data->delete_on_ack = delete_on_ack;
spin_lock_irqsave(&msm_rpm_list_lock, flags);
if (delete_on_ack)
list_add_tail(&data->list, &msm_rpm_wait_list);
else
list_add(&data->list, &msm_rpm_wait_list);
spin_unlock_irqrestore(&msm_rpm_list_lock, flags);
return 0;
}
static void msm_rpm_free_list_entry(struct msm_rpm_wait_data *elem)
{
unsigned long flags;
spin_lock_irqsave(&msm_rpm_list_lock, flags);
list_del(&elem->list);
spin_unlock_irqrestore(&msm_rpm_list_lock, flags);
kfree(elem);
}
static void msm_rpm_process_ack(uint32_t msg_id, int errno)
{
struct list_head *ptr, *next;
struct msm_rpm_wait_data *elem = NULL;
unsigned long flags;
spin_lock_irqsave(&msm_rpm_list_lock, flags);
list_for_each_safe(ptr, next, &msm_rpm_wait_list) {
elem = list_entry(ptr, struct msm_rpm_wait_data, list);
if (elem->msg_id == msg_id) {
elem->errno = errno;
elem->ack_recd = true;
complete(&elem->ack);
if (elem->delete_on_ack) {
list_del(&elem->list);
kfree(elem);
}
break;
}
}
/* Special case where the sleep driver doesn't
* wait for ACKs. This would decrease the latency involved with
* entering RPM assisted power collapse.
*/
if (!elem)
trace_rpm_smd_ack_recvd(0, msg_id, 0xDEADBEEF);
spin_unlock_irqrestore(&msm_rpm_list_lock, flags);
}
struct msm_rpm_kvp_packet {
uint32_t id;
uint32_t len;
uint32_t val;
};
static int msm_rpm_read_smd_data(char *buf)
{
int pkt_sz;
int bytes_read = 0;
pkt_sz = smd_cur_packet_size(msm_rpm_data.ch_info);
if (!pkt_sz)
return -EAGAIN;
if (pkt_sz > MAX_ERR_BUFFER_SIZE) {
pr_err("rpm_smd pkt_sz is greater than max size\n");
goto error;
}
if (pkt_sz != smd_read_avail(msm_rpm_data.ch_info))
return -EAGAIN;
do {
int len;
len = smd_read(msm_rpm_data.ch_info, buf + bytes_read, pkt_sz);
pkt_sz -= len;
bytes_read += len;
} while (pkt_sz > 0);
if (pkt_sz < 0) {
pr_err("rpm_smd pkt_sz is less than zero\n");
goto error;
}
return 0;
error:
BUG_ON(1);
return 0;
}
static void data_fn_tasklet(unsigned long data)
{
uint32_t msg_id;
int errno;
char buf[MAX_ERR_BUFFER_SIZE] = {0};
spin_lock(&msm_rpm_data.smd_lock_read);
while (smd_is_pkt_avail(msm_rpm_data.ch_info)) {
if (msm_rpm_read_smd_data(buf))
break;
msg_id = msm_rpm_get_msg_id_from_ack(buf);
errno = msm_rpm_get_error_from_ack(buf);
trace_rpm_smd_ack_recvd(0, msg_id, errno);
msm_rpm_process_ack(msg_id, errno);
}
spin_unlock(&msm_rpm_data.smd_lock_read);
}
static void msm_rpm_log_request(struct msm_rpm_request *cdata)
{
char buf[DEBUG_PRINT_BUFFER_SIZE];
size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
char name[5];
u32 value;
uint32_t i;
int j, prev_valid;
int valid_count = 0;
int pos = 0;
uint32_t res_type, rsc_id;
name[4] = 0;
for (i = 0; i < cdata->write_idx; i++)
if (cdata->kvp[i].valid)
valid_count++;
pos += scnprintf(buf + pos, buflen - pos, "%sRPM req: ", KERN_INFO);
if (msm_rpm_debug_mask & MSM_RPM_LOG_REQUEST_SHOW_MSG_ID)
pos += scnprintf(buf + pos, buflen - pos, "msg_id=%u, ",
get_msg_id(cdata->client_buf));
pos += scnprintf(buf + pos, buflen - pos, "s=%s",
(get_set_type(cdata->client_buf) ==
MSM_RPM_CTX_ACTIVE_SET ? "act" : "slp"));
res_type = get_rsc_type(cdata->client_buf);
rsc_id = get_rsc_id(cdata->client_buf);
if ((msm_rpm_debug_mask & MSM_RPM_LOG_REQUEST_PRETTY)
&& (msm_rpm_debug_mask & MSM_RPM_LOG_REQUEST_RAW)) {
/* Both pretty and raw formatting */
memcpy(name, &res_type, sizeof(uint32_t));
pos += scnprintf(buf + pos, buflen - pos,
", rsc_type=0x%08X (%s), rsc_id=%u; ",
res_type, name, rsc_id);
for (i = 0, prev_valid = 0; i < cdata->write_idx; i++) {
if (!cdata->kvp[i].valid)
continue;
memcpy(name, &cdata->kvp[i].key, sizeof(uint32_t));
pos += scnprintf(buf + pos, buflen - pos,
"[key=0x%08X (%s), value=%s",
cdata->kvp[i].key, name,
(cdata->kvp[i].nbytes ? "0x" : "null"));
for (j = 0; j < cdata->kvp[i].nbytes; j++)
pos += scnprintf(buf + pos, buflen - pos,
"%02X ",
cdata->kvp[i].value[j]);
if (cdata->kvp[i].nbytes)
pos += scnprintf(buf + pos, buflen - pos, "(");
for (j = 0; j < cdata->kvp[i].nbytes; j += 4) {
value = 0;
memcpy(&value, &cdata->kvp[i].value[j],
min_t(uint32_t, sizeof(uint32_t),
cdata->kvp[i].nbytes - j));
pos += scnprintf(buf + pos, buflen - pos, "%u",
value);
if (j + 4 < cdata->kvp[i].nbytes)
pos += scnprintf(buf + pos,
buflen - pos, " ");
}
if (cdata->kvp[i].nbytes)
pos += scnprintf(buf + pos, buflen - pos, ")");
pos += scnprintf(buf + pos, buflen - pos, "]");
if (prev_valid + 1 < valid_count)
pos += scnprintf(buf + pos, buflen - pos, ", ");
prev_valid++;
}
} else if (msm_rpm_debug_mask & MSM_RPM_LOG_REQUEST_PRETTY) {
/* Pretty formatting only */
memcpy(name, &res_type, sizeof(uint32_t));
pos += scnprintf(buf + pos, buflen - pos, " %s %u; ", name,
rsc_id);
for (i = 0, prev_valid = 0; i < cdata->write_idx; i++) {
if (!cdata->kvp[i].valid)
continue;
memcpy(name, &cdata->kvp[i].key, sizeof(uint32_t));
pos += scnprintf(buf + pos, buflen - pos, "%s=%s",
name, (cdata->kvp[i].nbytes ? "" : "null"));
for (j = 0; j < cdata->kvp[i].nbytes; j += 4) {
value = 0;
memcpy(&value, &cdata->kvp[i].value[j],
min_t(uint32_t, sizeof(uint32_t),
cdata->kvp[i].nbytes - j));
pos += scnprintf(buf + pos, buflen - pos, "%u",
value);
if (j + 4 < cdata->kvp[i].nbytes)
pos += scnprintf(buf + pos,
buflen - pos, " ");
}
if (prev_valid + 1 < valid_count)
pos += scnprintf(buf + pos, buflen - pos, ", ");
prev_valid++;
}
} else {
/* Raw formatting only */
pos += scnprintf(buf + pos, buflen - pos,
", rsc_type=0x%08X, rsc_id=%u; ", res_type, rsc_id);
for (i = 0, prev_valid = 0; i < cdata->write_idx; i++) {
if (!cdata->kvp[i].valid)
continue;
pos += scnprintf(buf + pos, buflen - pos,
"[key=0x%08X, value=%s",
cdata->kvp[i].key,
(cdata->kvp[i].nbytes ? "0x" : "null"));
for (j = 0; j < cdata->kvp[i].nbytes; j++) {
pos += scnprintf(buf + pos, buflen - pos,
"%02X",
cdata->kvp[i].value[j]);
if (j + 1 < cdata->kvp[i].nbytes)
pos += scnprintf(buf + pos,
buflen - pos, " ");
}
pos += scnprintf(buf + pos, buflen - pos, "]");
if (prev_valid + 1 < valid_count)
pos += scnprintf(buf + pos, buflen - pos, ", ");
prev_valid++;
}
}
pos += scnprintf(buf + pos, buflen - pos, "\n");
printk(buf);
}
static int msm_rpm_send_smd_buffer(char *buf, uint32_t size, bool noirq)
{
unsigned long flags;
int ret;
spin_lock_irqsave(&msm_rpm_data.smd_lock_write, flags);
ret = smd_write_avail(msm_rpm_data.ch_info);
while ((ret = smd_write_avail(msm_rpm_data.ch_info)) < size) {
if (ret < 0)
break;
if (!noirq) {
spin_unlock_irqrestore(
&msm_rpm_data.smd_lock_write, flags);
cpu_relax();
spin_lock_irqsave(
&msm_rpm_data.smd_lock_write, flags);
} else
udelay(5);
}
if (ret < 0) {
pr_err("SMD not initialized\n");
spin_unlock_irqrestore(
&msm_rpm_data.smd_lock_write, flags);
return ret;
}
ret = smd_write(msm_rpm_data.ch_info, buf, size);
spin_unlock_irqrestore(&msm_rpm_data.smd_lock_write, flags);
return ret;
}
static int msm_rpm_glink_send_buffer(char *buf, uint32_t size, bool noirq)
{
int ret;
unsigned long flags;
int timeout = 50;
spin_lock_irqsave(&msm_rpm_data.smd_lock_write, flags);
do {
ret = glink_tx(glink_data->glink_handle, buf, buf,
size, GLINK_TX_SINGLE_THREADED);
if (ret == -EBUSY || ret == -ENOSPC) {
if (!noirq) {
spin_unlock_irqrestore(
&msm_rpm_data.smd_lock_write, flags);
cpu_relax();
spin_lock_irqsave(
&msm_rpm_data.smd_lock_write, flags);
} else {
udelay(5);
}
timeout--;
} else {
ret = 0;
}
} while (ret && timeout);
spin_unlock_irqrestore(&msm_rpm_data.smd_lock_write, flags);
if (!timeout)
return 0;
else
return size;
}
static int msm_rpm_send_data(struct msm_rpm_request *cdata,
int msg_type, bool noirq, bool noack)
{
uint8_t *tmpbuff;
int ret;
uint32_t i;
uint32_t msg_size;
int msg_hdr_sz, req_hdr_sz;
uint32_t data_len = get_data_len(cdata->client_buf);
uint32_t set = get_set_type(cdata->client_buf);
uint32_t msg_id;
if (probe_status)
return probe_status;
if (!data_len)
return 1;
msg_hdr_sz = rpm_msg_fmt_ver ? sizeof(struct rpm_message_header_v1) :
sizeof(struct rpm_message_header_v0);
req_hdr_sz = RPM_HDR_SIZE;
set_msg_type(cdata->client_buf, msg_type);
set_req_len(cdata->client_buf, data_len + msg_hdr_sz - req_hdr_sz);
msg_size = get_req_len(cdata->client_buf) + req_hdr_sz;
/* populate data_len */
if (msg_size > cdata->numbytes) {
kfree(cdata->buf);
cdata->numbytes = msg_size;
cdata->buf = kzalloc(msg_size, GFP_FLAG(noirq));
}
if (!cdata->buf) {
pr_err("Failed malloc\n");
return 0;
}
tmpbuff = cdata->buf;
tmpbuff += msg_hdr_sz;
for (i = 0; (i < cdata->write_idx); i++) {
/* Sanity check */
BUG_ON((tmpbuff - cdata->buf) > cdata->numbytes);
if (!cdata->kvp[i].valid)
continue;
memcpy(tmpbuff, &cdata->kvp[i].key, sizeof(uint32_t));
tmpbuff += sizeof(uint32_t);
memcpy(tmpbuff, &cdata->kvp[i].nbytes, sizeof(uint32_t));
tmpbuff += sizeof(uint32_t);
memcpy(tmpbuff, cdata->kvp[i].value, cdata->kvp[i].nbytes);
tmpbuff += cdata->kvp[i].nbytes;
if (set == MSM_RPM_CTX_SLEEP_SET)
msm_rpm_notify_sleep_chain(cdata->client_buf,
&cdata->kvp[i]);
}
memcpy(cdata->buf, cdata->client_buf, msg_hdr_sz);
if ((set == MSM_RPM_CTX_SLEEP_SET) &&
!msm_rpm_smd_buffer_request(cdata, msg_size,
GFP_FLAG(noirq)))
return 1;
msg_id = msm_rpm_get_next_msg_id();
/* Set the version bit for new protocol */
set_msg_ver(cdata->buf, rpm_msg_fmt_ver);
set_msg_id(cdata->buf, msg_id);
set_msg_id(cdata->client_buf, msg_id);
if (msm_rpm_debug_mask
& (MSM_RPM_LOG_REQUEST_PRETTY | MSM_RPM_LOG_REQUEST_RAW))
msm_rpm_log_request(cdata);
if (standalone) {
for (i = 0; (i < cdata->write_idx); i++)
cdata->kvp[i].valid = false;
set_data_len(cdata->client_buf, 0);
ret = msg_id;
return ret;
}
msm_rpm_add_wait_list(msg_id, noack);
ret = msm_rpm_send_buffer(&cdata->buf[0], msg_size, noirq);
if (ret == msg_size) {
for (i = 0; (i < cdata->write_idx); i++)
cdata->kvp[i].valid = false;
set_data_len(cdata->client_buf, 0);
ret = msg_id;
trace_rpm_smd_send_active_set(msg_id,
get_rsc_type(cdata->client_buf),
get_rsc_id(cdata->client_buf));
} else if (ret < msg_size) {
struct msm_rpm_wait_data *rc;
ret = 0;
pr_err("Failed to write data msg_size:%d ret:%d msg_id:%d\n",
msg_size, ret, msg_id);
rc = msm_rpm_get_entry_from_msg_id(msg_id);
if (rc)
msm_rpm_free_list_entry(rc);
}
return ret;
}
static int _msm_rpm_send_request(struct msm_rpm_request *handle, bool noack)
{
int ret;
static DEFINE_MUTEX(send_mtx);
mutex_lock(&send_mtx);
ret = msm_rpm_send_data(handle, MSM_RPM_MSG_REQUEST_TYPE, false, noack);
mutex_unlock(&send_mtx);
return ret;
}
int msm_rpm_send_request(struct msm_rpm_request *handle)
{
return _msm_rpm_send_request(handle, false);
}
EXPORT_SYMBOL(msm_rpm_send_request);
int msm_rpm_send_request_noirq(struct msm_rpm_request *handle)
{
return msm_rpm_send_data(handle, MSM_RPM_MSG_REQUEST_TYPE, true, false);
}
EXPORT_SYMBOL(msm_rpm_send_request_noirq);
void *msm_rpm_send_request_noack(struct msm_rpm_request *handle)
{
int ret;
ret = _msm_rpm_send_request(handle, true);
return ret < 0 ? ERR_PTR(ret) : NULL;
}
EXPORT_SYMBOL(msm_rpm_send_request_noack);
int msm_rpm_wait_for_ack(uint32_t msg_id)
{
struct msm_rpm_wait_data *elem;
int rc = 0;
if (!msg_id) {
pr_err("Invalid msg id\n");
return -ENOMEM;
}
if (msg_id == 1)
return rc;
if (standalone)
return rc;
elem = msm_rpm_get_entry_from_msg_id(msg_id);
if (!elem)
return rc;
wait_for_completion(&elem->ack);
trace_rpm_smd_ack_recvd(0, msg_id, 0xDEADFEED);
rc = elem->errno;
msm_rpm_free_list_entry(elem);
return rc;
}
EXPORT_SYMBOL(msm_rpm_wait_for_ack);
static void msm_rpm_smd_read_data_noirq(uint32_t msg_id)
{
uint32_t id = 0;
while (id != msg_id) {
if (smd_is_pkt_avail(msm_rpm_data.ch_info)) {
int errno;
char buf[MAX_ERR_BUFFER_SIZE] = {};
msm_rpm_read_smd_data(buf);
id = msm_rpm_get_msg_id_from_ack(buf);
errno = msm_rpm_get_error_from_ack(buf);
trace_rpm_smd_ack_recvd(1, msg_id, errno);
msm_rpm_process_ack(id, errno);
}
}
}
static void msm_rpm_glink_read_data_noirq(struct msm_rpm_wait_data *elem)
{
int ret;
/* Use rx_poll method to read the message from RPM */
while (elem->errno) {
ret = glink_rpm_rx_poll(glink_data->glink_handle);
if (ret >= 0) {
/*
* We might have receieve the notification.
* Now we have to check whether the notification
* received is what we are interested?
* Wait for few usec to get the notification
* before re-trying the poll again.
*/
udelay(50);
} else {
pr_err("rx poll return error = %d\n", ret);
}
}
}
int msm_rpm_wait_for_ack_noirq(uint32_t msg_id)
{
struct msm_rpm_wait_data *elem;
unsigned long flags;
int rc = 0;
if (!msg_id) {
pr_err("Invalid msg id\n");
return -ENOMEM;
}
if (msg_id == 1)
return 0;
if (standalone)
return 0;
spin_lock_irqsave(&msm_rpm_data.smd_lock_read, flags);
elem = msm_rpm_get_entry_from_msg_id(msg_id);
if (!elem)
/* Should this be a bug
* Is it ok for another thread to read the msg?
*/
goto wait_ack_cleanup;
if (elem->errno != INIT_ERROR) {
rc = elem->errno;
msm_rpm_free_list_entry(elem);
goto wait_ack_cleanup;
}
if (!glink_enabled)
msm_rpm_smd_read_data_noirq(msg_id);
else
msm_rpm_glink_read_data_noirq(elem);
rc = elem->errno;
msm_rpm_free_list_entry(elem);
wait_ack_cleanup:
spin_unlock_irqrestore(&msm_rpm_data.smd_lock_read, flags);
if (!glink_enabled)
if (smd_is_pkt_avail(msm_rpm_data.ch_info))
tasklet_schedule(&data_tasklet);
return rc;
}
EXPORT_SYMBOL(msm_rpm_wait_for_ack_noirq);
void *msm_rpm_send_message_noack(enum msm_rpm_set set, uint32_t rsc_type,
uint32_t rsc_id, struct msm_rpm_kvp *kvp, int nelems)
{
int i, rc;
struct msm_rpm_request *req =
msm_rpm_create_request_common(set, rsc_type, rsc_id, nelems,
false);
if (IS_ERR(req))
return req;
if (!req)
return ERR_PTR(ENOMEM);
for (i = 0; i < nelems; i++) {
rc = msm_rpm_add_kvp_data(req, kvp[i].key,
kvp[i].data, kvp[i].length);
if (rc)
goto bail;
}
rc = PTR_ERR(msm_rpm_send_request_noack(req));
bail:
msm_rpm_free_request(req);
return rc < 0 ? ERR_PTR(rc) : NULL;
}
EXPORT_SYMBOL(msm_rpm_send_message_noack);
int msm_rpm_send_message(enum msm_rpm_set set, uint32_t rsc_type,
uint32_t rsc_id, struct msm_rpm_kvp *kvp, int nelems)
{
int i, rc;
struct msm_rpm_request *req =
msm_rpm_create_request(set, rsc_type, rsc_id, nelems);
if (IS_ERR(req))
return PTR_ERR(req);
if (!req)
return -ENOMEM;
for (i = 0; i < nelems; i++) {
rc = msm_rpm_add_kvp_data(req, kvp[i].key,
kvp[i].data, kvp[i].length);
if (rc)
goto bail;
}
rc = msm_rpm_wait_for_ack(msm_rpm_send_request(req));
bail:
msm_rpm_free_request(req);
return rc;
}
EXPORT_SYMBOL(msm_rpm_send_message);
int msm_rpm_send_message_noirq(enum msm_rpm_set set, uint32_t rsc_type,
uint32_t rsc_id, struct msm_rpm_kvp *kvp, int nelems)
{
int i, rc;
struct msm_rpm_request *req =
msm_rpm_create_request_noirq(set, rsc_type, rsc_id, nelems);
if (IS_ERR(req))
return PTR_ERR(req);
if (!req)
return -ENOMEM;
for (i = 0; i < nelems; i++) {
rc = msm_rpm_add_kvp_data_noirq(req, kvp[i].key,
kvp[i].data, kvp[i].length);
if (rc)
goto bail;
}
rc = msm_rpm_wait_for_ack_noirq(msm_rpm_send_request_noirq(req));
bail:
msm_rpm_free_request(req);
return rc;
}
EXPORT_SYMBOL(msm_rpm_send_message_noirq);
/**
* During power collapse, the rpm driver disables the SMD interrupts to make
* sure that the interrupt doesn't wakes us from sleep.
*/
int msm_rpm_enter_sleep(bool print, const struct cpumask *cpumask)
{
int ret = 0;
if (standalone)
return 0;
if (!glink_enabled)
ret = smd_mask_receive_interrupt(msm_rpm_data.ch_info,
true, cpumask);
else
ret = glink_rpm_mask_rx_interrupt(glink_data->glink_handle,
true, (void *)cpumask);
if (!ret) {
ret = msm_rpm_flush_requests(print);
if (ret) {
if (!glink_enabled)
smd_mask_receive_interrupt(
msm_rpm_data.ch_info, false, NULL);
else
glink_rpm_mask_rx_interrupt(
glink_data->glink_handle, false, NULL);
}
}
return ret;
}
EXPORT_SYMBOL(msm_rpm_enter_sleep);
/**
* When the system resumes from power collapse, the SMD interrupt disabled by
* enter function has to reenabled to continue processing SMD message.
*/
void msm_rpm_exit_sleep(void)
{
int ret;
if (standalone)
return;
do {
ret = msm_rpm_read_sleep_ack();
} while (ret > 0);
if (!glink_enabled)
smd_mask_receive_interrupt(msm_rpm_data.ch_info, false, NULL);
else
glink_rpm_mask_rx_interrupt(glink_data->glink_handle,
false, NULL);
}
EXPORT_SYMBOL(msm_rpm_exit_sleep);
/*
* Whenever there is a data from RPM, notify_rx will be called.
* This function is invoked either interrupt OR polling context.
*/
static void msm_rpm_trans_notify_rx(void *handle, const void *priv,
const void *pkt_priv, const void *ptr, size_t size)
{
uint32_t msg_id;
int errno;
char buf[MAX_ERR_BUFFER_SIZE] = {0};
struct msm_rpm_wait_data *elem;
static DEFINE_SPINLOCK(rx_notify_lock);
unsigned long flags;
if (!size)
return;
BUG_ON(size > MAX_ERR_BUFFER_SIZE);
spin_lock_irqsave(&rx_notify_lock, flags);
memcpy(buf, ptr, size);
msg_id = msm_rpm_get_msg_id_from_ack(buf);
errno = msm_rpm_get_error_from_ack(buf);
elem = msm_rpm_get_entry_from_msg_id(msg_id);
/*
* It is applicable for sleep set requests
* Sleep set requests are not added to the
* wait queue list. Without this check we
* run into NULL pointer deferrence issue.
*/
if (!elem) {
spin_unlock_irqrestore(&rx_notify_lock, flags);
glink_rx_done(handle, ptr, 0);
return;
}
msm_rpm_process_ack(msg_id, errno);
spin_unlock_irqrestore(&rx_notify_lock, flags);
glink_rx_done(handle, ptr, 0);
}
static void msm_rpm_trans_notify_state(void *handle, const void *priv,
unsigned event)
{
switch (event) {
case GLINK_CONNECTED:
glink_data->glink_handle = handle;
if (IS_ERR_OR_NULL(glink_data->glink_handle)) {
pr_err("glink_handle %d\n",
(int)PTR_ERR(glink_data->glink_handle));
BUG_ON(1);
}
/*
* Do not allow clients to send data to RPM until glink
* is fully open.
*/
probe_status = 0;
pr_info("glink config params: transport=%s, edge=%s, name=%s\n",
glink_data->xprt,
glink_data->edge,
glink_data->name);
break;
default:
pr_err("Unrecognized event %d\n", event);
break;
};
}
static void msm_rpm_trans_notify_tx_done(void *handle, const void *priv,
const void *pkt_priv, const void *ptr)
{
return;
}
static void msm_rpm_glink_open_work(struct work_struct *work)
{
pr_debug("Opening glink channel\n");
glink_data->glink_handle = glink_open(glink_data->open_cfg);
if (IS_ERR_OR_NULL(glink_data->glink_handle)) {
pr_err("Error: glink_open failed %d\n",
(int)PTR_ERR(glink_data->glink_handle));
BUG_ON(1);
}
}
static void msm_rpm_glink_notifier_cb(struct glink_link_state_cb_info *cb_info,
void *priv)
{
struct glink_open_config *open_config;
static bool first = true;
if (!cb_info) {
pr_err("Missing callback data\n");
return;
}
switch (cb_info->link_state) {
case GLINK_LINK_STATE_UP:
if (first)
first = false;
else
break;
open_config = kzalloc(sizeof(*open_config), GFP_KERNEL);
if (!open_config) {
pr_err("Could not allocate memory\n");
break;
}
glink_data->open_cfg = open_config;
pr_debug("glink link state up cb receieved\n");
INIT_WORK(&glink_data->work, msm_rpm_glink_open_work);
open_config->priv = glink_data;
open_config->name = glink_data->name;
open_config->edge = glink_data->edge;
open_config->notify_rx = msm_rpm_trans_notify_rx;
open_config->notify_tx_done = msm_rpm_trans_notify_tx_done;
open_config->notify_state = msm_rpm_trans_notify_state;
schedule_work(&glink_data->work);
break;
default:
pr_err("Unrecognised state = %d\n", cb_info->link_state);
break;
};
}
static int msm_rpm_glink_dt_parse(struct platform_device *pdev,
struct glink_apps_rpm_data *glink_data)
{
char *key = NULL;
int ret;
if (of_device_is_compatible(pdev->dev.of_node, "qcom,rpm-glink")) {
glink_enabled = true;
} else {
pr_warn("qcom,rpm-glink compatible not matches\n");
ret = -EINVAL;
return ret;
}
key = "qcom,glink-edge";
ret = of_property_read_string(pdev->dev.of_node, key,
&glink_data->edge);
if (ret) {
pr_err("Failed to read node: %s, key=%s\n",
pdev->dev.of_node->full_name, key);
return ret;
}
key = "rpm-channel-name";
ret = of_property_read_string(pdev->dev.of_node, key,
&glink_data->name);
if (ret)
pr_err("%s(): Failed to read node: %s, key=%s\n", __func__,
pdev->dev.of_node->full_name, key);
return ret;
}
static int msm_rpm_glink_link_setup(struct glink_apps_rpm_data *glink_data,
struct platform_device *pdev)
{
struct glink_link_info *link_info;
void *link_state_cb_handle;
struct device *dev = &pdev->dev;
int ret = 0;
link_info = devm_kzalloc(dev, sizeof(struct glink_link_info),
GFP_KERNEL);
if (!link_info) {
pr_err("Could not allocate memory\n");
ret = -ENOMEM;
return ret;
}
glink_data->link_info = link_info;
/*
* Setup link info parameters
*/
link_info->edge = glink_data->edge;
link_info->glink_link_state_notif_cb =
msm_rpm_glink_notifier_cb;
link_state_cb_handle = glink_register_link_state_cb(link_info, NULL);
if (IS_ERR_OR_NULL(link_state_cb_handle)) {
pr_err("Could not register cb\n");
ret = PTR_ERR(link_state_cb_handle);
return ret;
}
spin_lock_init(&msm_rpm_data.smd_lock_read);
spin_lock_init(&msm_rpm_data.smd_lock_write);
return ret;
}
static int msm_rpm_dev_glink_probe(struct platform_device *pdev)
{
int ret = -ENOMEM;
struct device *dev = &pdev->dev;
glink_data = devm_kzalloc(dev, sizeof(*glink_data), GFP_KERNEL);
if (!glink_data) {
pr_err("Could not allocate memory\n");
return ret;
}
ret = msm_rpm_glink_dt_parse(pdev, glink_data);
if (ret < 0) {
devm_kfree(dev, glink_data);
return ret;
}
ret = msm_rpm_glink_link_setup(glink_data, pdev);
if (ret < 0) {
/*
* If the glink setup fails there is no
* fall back mechanism to SMD.
*/
pr_err("GLINK setup fail ret = %d\n", ret);
BUG_ON(1);
}
return ret;
}
static int msm_rpm_dev_probe(struct platform_device *pdev)
{
char *key = NULL;
int ret = 0;
void __iomem *reg_base;
uint32_t version = V0_PROTOCOL_VERSION; /* set to default v0 format */
/*
* Check for standalone support
*/
key = "rpm-standalone";
standalone = of_property_read_bool(pdev->dev.of_node, key);
if (standalone) {
probe_status = ret;
goto skip_init;
}
reg_base = of_iomap(pdev->dev.of_node, 0);
if (reg_base) {
version = readq_relaxed(reg_base);
iounmap(reg_base);
}
if (version == V1_PROTOCOL_VERSION)
rpm_msg_fmt_ver = RPM_MSG_V1_FMT;
pr_debug("RPM-SMD running version %d/n", rpm_msg_fmt_ver);
ret = msm_rpm_dev_glink_probe(pdev);
if (!ret) {
pr_info("APSS-RPM communication over GLINK\n");
msm_rpm_send_buffer = msm_rpm_glink_send_buffer;
of_platform_populate(pdev->dev.of_node, NULL, NULL,
&pdev->dev);
return ret;
} else {
msm_rpm_send_buffer = msm_rpm_send_smd_buffer;
pr_info("APSS-RPM communication over SMD\n");
}
key = "rpm-channel-name";
ret = of_property_read_string(pdev->dev.of_node, key,
&msm_rpm_data.ch_name);
if (ret) {
pr_err("%s(): Failed to read node: %s, key=%s\n", __func__,
pdev->dev.of_node->full_name, key);
goto fail;
}
key = "rpm-channel-type";
ret = of_property_read_u32(pdev->dev.of_node, key,
&msm_rpm_data.ch_type);
if (ret) {
pr_err("%s(): Failed to read node: %s, key=%s\n", __func__,
pdev->dev.of_node->full_name, key);
goto fail;
}
ret = smd_named_open_on_edge(msm_rpm_data.ch_name,
msm_rpm_data.ch_type,
&msm_rpm_data.ch_info,
&msm_rpm_data,
msm_rpm_notify);
if (ret) {
if (ret != -EPROBE_DEFER) {
pr_err("%s: Cannot open RPM channel %s %d\n",
__func__, msm_rpm_data.ch_name,
msm_rpm_data.ch_type);
}
goto fail;
}
spin_lock_init(&msm_rpm_data.smd_lock_write);
spin_lock_init(&msm_rpm_data.smd_lock_read);
tasklet_init(&data_tasklet, data_fn_tasklet, 0);
wait_for_completion(&msm_rpm_data.smd_open);
smd_disable_read_intr(msm_rpm_data.ch_info);
msm_rpm_smd_wq = alloc_workqueue("rpm-smd",
WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
if (!msm_rpm_smd_wq) {
pr_err("%s: Unable to alloc rpm-smd workqueue\n", __func__);
ret = -EINVAL;
goto fail;
}
queue_work(msm_rpm_smd_wq, &msm_rpm_data.work);
probe_status = ret;
skip_init:
of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
if (standalone)
pr_info("RPM running in standalone mode\n");
fail:
return probe_status;
}
static struct of_device_id msm_rpm_match_table[] = {
{.compatible = "qcom,rpm-smd"},
{.compatible = "qcom,rpm-glink"},
{},
};
static struct platform_driver msm_rpm_device_driver = {
.probe = msm_rpm_dev_probe,
.driver = {
.name = "rpm-smd",
.owner = THIS_MODULE,
.of_match_table = msm_rpm_match_table,
},
};
int __init msm_rpm_driver_init(void)
{
static bool registered;
if (registered)
return 0;
registered = true;
return platform_driver_register(&msm_rpm_device_driver);
}
EXPORT_SYMBOL(msm_rpm_driver_init);
arch_initcall(msm_rpm_driver_init);
|