hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
/*
 * Copyright 2015 Rockchip Electronics Co. LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#define MODULE_TAG "mpp_platform"
 
#include <string.h>
 
#include "mpp_env.h"
#include "mpp_debug.h"
#include "mpp_common.h"
#include "mpp_platform.h"
#include "mpp_service.h"
 
static MppKernelVersion check_kernel_version(void)
{
    static const char *kernel_version_path = "/proc/version";
    MppKernelVersion version = KERNEL_UNKNOWN;
    FILE *fp = NULL;
    char buf[32];
 
    if (access(kernel_version_path, F_OK | R_OK))
        return version;
 
    fp = fopen(kernel_version_path, "rb");
    if (fp) {
        size_t len = fread(buf, 1, sizeof(buf) - 1, fp);
        char *pos = NULL;
 
        buf[len] = '\0';
        pos = strstr(buf, "Linux version ");
        if (pos) {
            RK_S32 major = 0;
            RK_S32 minor = 0;
            RK_S32 last = 0;
            RK_S32 count = 0;
 
            pos += 14;
            count = sscanf(pos, "%d.%d.%d ", &major, &minor, &last);
            if (count >= 2 && major > 0 && minor > 0) {
                if (major == 3)
                    version = KERNEL_3_10;
                else if (major == 4) {
                    version = KERNEL_4_4;
                    if (minor >= 19)
                        version = KERNEL_4_19;
                }
            }
        }
        fclose(fp);
    }
    return version;
}
 
class MppPlatformService
{
private:
    // avoid any unwanted function
    MppPlatformService();
    ~MppPlatformService() {};
    MppPlatformService(const MppPlatformService &);
    MppPlatformService &operator=(const MppPlatformService &);
 
    MppIoctlVersion     ioctl_version;
    MppKernelVersion    kernel_version;
    RK_U32              vcodec_type;
    RK_U32              hw_ids[32];
    MppServiceCmdCap    mpp_service_cmd_cap;
    const MppSocInfo    *soc_info;
    const char          *soc_name;
 
public:
    static MppPlatformService *get_instance() {
        static MppPlatformService instance;
        return &instance;
    }
 
    MppIoctlVersion     get_ioctl_version(void) { return ioctl_version; };
    MppKernelVersion    get_kernel_version(void) { return kernel_version; };
    const char          *get_soc_name() { return soc_name; };
    MppServiceCmdCap    *get_mpp_service_cmd_cap() { return &mpp_service_cmd_cap; };
    RK_U32              get_hw_id(RK_S32 client_type);
};
 
MppPlatformService::MppPlatformService()
    : ioctl_version(IOCTL_MPP_SERVICE_V1),
      kernel_version(KERNEL_UNKNOWN),
      vcodec_type(0),
      soc_info(NULL),
      soc_name(NULL)
{
    /* judge vdpu support version */
    MppServiceCmdCap *cap = &mpp_service_cmd_cap;
 
    /* default value */
    cap->support_cmd = 0;
    cap->query_cmd = MPP_CMD_QUERY_BASE + 1;
    cap->init_cmd = MPP_CMD_INIT_BASE + 1;
    cap->send_cmd = MPP_CMD_SEND_BASE + 1;
    cap->poll_cmd = MPP_CMD_POLL_BASE + 1;
    cap->ctrl_cmd = MPP_CMD_CONTROL_BASE + 0;
 
    mpp_env_get_u32("mpp_debug", &mpp_debug, 0);
 
    /* read soc name */
    soc_name = mpp_get_soc_name();
    soc_info = mpp_get_soc_info();
 
    if (soc_info->soc_type == ROCKCHIP_SOC_AUTO)
        mpp_log("can not found match soc name: %s\n", soc_name);
 
    ioctl_version = IOCTL_VCODEC_SERVICE;
    if (mpp_get_mpp_service_name()) {
        ioctl_version = IOCTL_MPP_SERVICE_V1;
        check_mpp_service_cap(&vcodec_type, hw_ids, cap);
    }
    kernel_version = check_kernel_version();
    vcodec_type = soc_info->vcodec_type;
}
 
RK_U32 MppPlatformService::get_hw_id(RK_S32 client_type)
{
    RK_U32 hw_id = 0;
 
    if (vcodec_type & (1 << client_type))
        hw_id = hw_ids[client_type];
 
    return hw_id;
}
 
MppIoctlVersion mpp_get_ioctl_version(void)
{
    return MppPlatformService::get_instance()->get_ioctl_version();
}
 
MppKernelVersion mpp_get_kernel_version(void)
{
    return MppPlatformService::get_instance()->get_kernel_version();
}
 
RK_U32 mpp_get_2d_hw_flag(void)
{
    RK_U32 flag = 0;
 
    if (!access("/dev/rga", F_OK))
        flag |= HAVE_RGA;
 
    if (!access("/dev/iep", F_OK))
        flag |= HAVE_IEP;
 
    return flag;
}
 
const MppServiceCmdCap *mpp_get_mpp_service_cmd_cap(void)
{
    return MppPlatformService::get_instance()->get_mpp_service_cmd_cap();
}
 
RK_U32 mpp_get_client_hw_id(RK_S32 client_type)
{
    return MppPlatformService::get_instance()->get_hw_id(client_type);
}