Debian Codegraph @

src/imagination/vulkan/winsys/pvr_winsys.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <fcntl.h>
25 #include <stdbool.h>
26 #include <vulkan/vulkan.h>
27 #include <xf86drm.h>
28
29 #include "powervr/pvr_drm_public.h"
30 #include "pvr_winsys.h"
31 #include "vk_log.h"
32
33 #if defined(PVR_SUPPORT_SERVICES_DRIVER)
34 # include "pvrsrvkm/pvr_srv_public.h"
35 #endif
36
37 void pvr_winsys_destroy(struct pvr_winsys *ws)
38 {
39 const int display_fd = ws->display_fd;
40 const int render_fd = ws->render_fd;
41
42 ws->ops->destroy(ws);
43
44 if (display_fd >= 0)
45 close(display_fd);
46
47 if (render_fd >= 0)
48 close(render_fd);
49 }
50
51 VkResult pvr_winsys_create(const char *render_path,
52 const char *display_path,
53 bool keep_display_master,
54 const VkAllocationCallbacks *alloc,
55 struct pvr_winsys **const ws_out)
56 {
57 drmVersionPtr version;
58 VkResult result;
59 int display_fd;
60 int render_fd;
61
62 render_fd = open(render_path, O_RDWR | O_CLOEXEC);
63 if (render_fd < 0) {
64 result = vk_errorf(NULL,
65 VK_ERROR_INITIALIZATION_FAILED,
66 "Failed to open render device %s",
67 render_path);
68 goto err_out;
69 }
70
71 if (display_path) {
72 display_fd = open(display_path, O_RDWR | O_CLOEXEC);
73 if (display_fd < 0) {
74 result = vk_errorf(NULL,
75 VK_ERROR_INITIALIZATION_FAILED,
76 "Failed to open display device %s",
77 display_path);
78 goto err_close_render_fd;
79 }
80 if (!keep_display_master) {
81 /* Try to drop master for the display FD if it's not meant to be
82 * kept, but not to fail if it's not the master at opening time.
83 */
84 drmDropMaster(display_fd);
85 }
86 } else {
87 display_fd = -1;
88 }
89
90 version = drmGetVersion(render_fd);
91 if (!version) {
92 result = vk_errorf(NULL,
93 VK_ERROR_INCOMPATIBLE_DRIVER,
94 "Failed to query kernel driver version for device.");
95 goto err_close_display_fd;
96 }
97
98 if (strcmp(version->name, PVR_DRM_DRIVER_NAME) == 0) {
99 result = pvr_drm_winsys_create(render_fd, display_fd, alloc, ws_out);
100 #if defined(PVR_SUPPORT_SERVICES_DRIVER)
101 } else if (strcmp(version->name, PVR_SRV_DRIVER_NAME) == 0) {
102 result = pvr_srv_winsys_create(render_fd, display_fd, alloc, ws_out);
103 #endif
104 } else {
105 result = vk_errorf(
106 NULL,
107 VK_ERROR_INCOMPATIBLE_DRIVER,
108 "Device does not use any of the supported pvrsrvkm or powervr kernel driver.");
109 }
110
111 drmFreeVersion(version);
112
113 if (result != VK_SUCCESS)
114 goto err_close_display_fd;
115
116 return VK_SUCCESS;
117
118 err_close_display_fd:
119 if (display_fd >= 0)
120 close(display_fd);
121
122 err_close_render_fd:
123 close(render_fd);
124
125 err_out:
126 return result;
127 }