xref: /aosp_15_r20/external/mesa3d/src/gallium/winsys/kmsro/drm/kmsro_drm_winsys.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2016 Christian Gmeiner <[email protected]>
3  * Copyright (C) 2017 Broadcom
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include <fcntl.h>
26 #include <unistd.h>
27 
28 #include "kmsro_drm_public.h"
29 #include "v3d/drm/v3d_drm_public.h"
30 #include "vc4/drm/vc4_drm_public.h"
31 #include "etnaviv/drm/etnaviv_drm_public.h"
32 #include "freedreno/drm/freedreno_drm_public.h"
33 #include "panfrost/drm/panfrost_drm_public.h"
34 #include "lima/drm/lima_drm_public.h"
35 #include "asahi/drm/asahi_drm_public.h"
36 #include "xf86drm.h"
37 
38 #include "pipe/p_screen.h"
39 #include "pipe-loader/pipe_loader.h"
40 #include "renderonly/renderonly.h"
41 #include "util/u_memory.h"
42 
43 #include "loader.h"
44 
kmsro_ro_destroy(struct renderonly * ro)45 static void kmsro_ro_destroy(struct renderonly *ro)
46 {
47    if (ro->gpu_fd >= 0)
48       close(ro->gpu_fd);
49 
50    util_sparse_array_finish(&ro->bo_map);
51 
52    FREE(ro);
53 }
54 
kmsro_drm_screen_create(int kms_fd,const struct pipe_screen_config * config)55 struct pipe_screen *kmsro_drm_screen_create(int kms_fd,
56                                             const struct pipe_screen_config *config)
57 {
58    struct pipe_screen *screen = NULL;
59    struct renderonly *ro = CALLOC_STRUCT(renderonly);
60    char *render_dev_name = NULL;
61 
62    if (!ro)
63       return NULL;
64 
65    ro->kms_fd = kms_fd;
66    ro->gpu_fd = pipe_loader_get_compatible_render_capable_device_fd(kms_fd);
67    if (ro->gpu_fd < 0) {
68       FREE(ro);
69       return NULL;
70    }
71 
72    render_dev_name = loader_get_kernel_driver_name(ro->gpu_fd);
73    if (!render_dev_name) {
74       close(ro->gpu_fd);
75       FREE(ro);
76       return NULL;
77    }
78 
79    ro->destroy = kmsro_ro_destroy;
80    util_sparse_array_init(&ro->bo_map, sizeof(struct renderonly_scanout), 64);
81    simple_mtx_init(&ro->bo_map_lock, mtx_plain);
82 
83    if (strcmp(render_dev_name, "asahi") == 0) {
84 #if defined(GALLIUM_ASAHI)
85       ro->create_for_resource = renderonly_create_gpu_import_for_resource;
86       screen = asahi_drm_screen_create_renderonly(ro->gpu_fd, ro, config);
87 #endif
88    }
89    else if (strcmp(render_dev_name, "etnaviv") == 0) {
90 #if defined(GALLIUM_ETNAVIV)
91       ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource;
92       screen = etna_drm_screen_create_renderonly(ro->gpu_fd, ro, config);
93 #endif
94    } else if (strcmp(render_dev_name, "msm") == 0) {
95 #if defined(GALLIUM_FREEDRENO)
96       ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource;
97       screen = fd_drm_screen_create_renderonly(ro->gpu_fd, ro, config);
98 #endif
99    } else if (strcmp(render_dev_name, "lima") == 0) {
100 #if defined(GALLIUM_LIMA)
101       ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource;
102       screen = lima_drm_screen_create_renderonly(ro->gpu_fd, ro, config);
103 #endif
104    } else if (strcmp(render_dev_name, "panfrost") == 0 ||
105               strcmp(render_dev_name, "panthor") == 0) {
106 #if defined(GALLIUM_PANFROST)
107       ro->create_for_resource = panfrost_create_kms_dumb_buffer_for_resource;
108       screen = panfrost_drm_screen_create_renderonly(ro->gpu_fd, ro, config);
109 #endif
110    } else if (strcmp(render_dev_name, "v3d") == 0) {
111 #if defined(GALLIUM_V3D)
112       ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource;
113       screen = v3d_drm_screen_create_renderonly(ro->gpu_fd, ro, config);
114 #endif
115    } else if (strcmp(render_dev_name, "vc4") == 0) {
116 #if defined(GALLIUM_VC4)
117       /* Passes the vc4-allocated BO through to the KMS-only DRM device using
118        * PRIME buffer sharing.  The VC4 BO must be linear, which the SCANOUT
119        * flag on allocation will have ensured.
120        */
121       ro->create_for_resource = renderonly_create_gpu_import_for_resource;
122       screen = vc4_drm_screen_create_renderonly(ro->gpu_fd, ro, config);
123 #endif
124    }
125 
126    free(render_dev_name);
127 
128    return screen;
129 }
130