1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2016 Broadcom
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <errno.h>
25bf215546Sopenharmony_ci#include <stdio.h>
26bf215546Sopenharmony_ci#include <string.h>
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "common/v3d_device_info.h"
29bf215546Sopenharmony_ci#include "drm-uapi/v3d_drm.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cibool
32bf215546Sopenharmony_civ3d_get_device_info(int fd, struct v3d_device_info* devinfo, v3d_ioctl_fun drm_ioctl) {
33bf215546Sopenharmony_ci    struct drm_v3d_get_param ident0 = {
34bf215546Sopenharmony_ci            .param = DRM_V3D_PARAM_V3D_CORE0_IDENT0,
35bf215546Sopenharmony_ci    };
36bf215546Sopenharmony_ci    struct drm_v3d_get_param ident1 = {
37bf215546Sopenharmony_ci            .param = DRM_V3D_PARAM_V3D_CORE0_IDENT1,
38bf215546Sopenharmony_ci    };
39bf215546Sopenharmony_ci    int ret;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci    ret = drm_ioctl(fd, DRM_IOCTL_V3D_GET_PARAM, &ident0);
42bf215546Sopenharmony_ci    if (ret != 0) {
43bf215546Sopenharmony_ci            fprintf(stderr, "Couldn't get V3D core IDENT0: %s\n",
44bf215546Sopenharmony_ci                    strerror(errno));
45bf215546Sopenharmony_ci            return false;
46bf215546Sopenharmony_ci    }
47bf215546Sopenharmony_ci    ret = drm_ioctl(fd, DRM_IOCTL_V3D_GET_PARAM, &ident1);
48bf215546Sopenharmony_ci    if (ret != 0) {
49bf215546Sopenharmony_ci            fprintf(stderr, "Couldn't get V3D core IDENT1: %s\n",
50bf215546Sopenharmony_ci                    strerror(errno));
51bf215546Sopenharmony_ci            return false;
52bf215546Sopenharmony_ci    }
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci    uint32_t major = (ident0.value >> 24) & 0xff;
55bf215546Sopenharmony_ci    uint32_t minor = (ident1.value >> 0) & 0xf;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci    devinfo->ver = major * 10 + minor;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci    devinfo->vpm_size = (ident1.value >> 28 & 0xf) * 8192;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci    int nslc = (ident1.value >> 4) & 0xf;
62bf215546Sopenharmony_ci    int qups = (ident1.value >> 8) & 0xf;
63bf215546Sopenharmony_ci    devinfo->qpu_count = nslc * qups;
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci    switch (devinfo->ver) {
66bf215546Sopenharmony_ci        case 33:
67bf215546Sopenharmony_ci        case 41:
68bf215546Sopenharmony_ci        case 42:
69bf215546Sopenharmony_ci                break;
70bf215546Sopenharmony_ci        default:
71bf215546Sopenharmony_ci                fprintf(stderr,
72bf215546Sopenharmony_ci                        "V3D %d.%d not supported by this version of Mesa.\n",
73bf215546Sopenharmony_ci                        devinfo->ver / 10,
74bf215546Sopenharmony_ci                        devinfo->ver % 10);
75bf215546Sopenharmony_ci                return false;
76bf215546Sopenharmony_ci    }
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci    return true;
79bf215546Sopenharmony_ci}
80