turnip: add .clang-format
[mesa.git] / src / freedreno / vulkan / tu_drm.c
1 /*
2 * Copyright © 2018 Google, Inc.
3 * Copyright © 2015 Intel Corporation
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
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "tu_private.h"
26
27 #include "xf86drm.h"
28 #include <errno.h>
29 #include <msm_drm.h>
30 #include <stdint.h>
31 #include <sys/ioctl.h>
32
33 /**
34 * Return gem handle on success. Return 0 on failure.
35 */
36 uint32_t
37 tu_gem_new(struct tu_device *dev, uint64_t size, uint32_t flags)
38 {
39 struct drm_msm_gem_new req = {
40 .size = size,
41 .flags = flags,
42 };
43
44 int ret = drmCommandWriteRead(dev->physical_device->local_fd,
45 DRM_MSM_GEM_NEW, &req, sizeof(req));
46 if (ret)
47 return 0;
48
49 return req.handle;
50 }
51
52 void
53 tu_gem_close(struct tu_device *dev, uint32_t gem_handle)
54 {
55 struct drm_gem_close req = {
56 .handle = gem_handle,
57 };
58
59 drmIoctl(dev->physical_device->local_fd, DRM_IOCTL_GEM_CLOSE, &req);
60 }
61
62 /** Return UINT64_MAX on error. */
63 static uint64_t
64 tu_gem_info(struct tu_device *dev, uint32_t gem_handle, uint32_t flags)
65 {
66 struct drm_msm_gem_info req = {
67 .handle = gem_handle,
68 .flags = flags,
69 };
70
71 int ret = drmCommandWriteRead(dev->physical_device->local_fd,
72 DRM_MSM_GEM_INFO, &req, sizeof(req));
73 if (ret == -1)
74 return UINT64_MAX;
75
76 return req.offset;
77 }
78
79 /** Return UINT64_MAX on error. */
80 uint64_t
81 tu_gem_info_offset(struct tu_device *dev, uint32_t gem_handle)
82 {
83 return tu_gem_info(dev, gem_handle, 0);
84 }
85
86 /** Return UINT64_MAX on error. */
87 uint64_t
88 tu_gem_info_iova(struct tu_device *dev, uint32_t gem_handle)
89 {
90 return tu_gem_info(dev, gem_handle, MSM_INFO_IOVA);
91 }
92
93 int
94 tu_drm_query_param(struct tu_physical_device *dev,
95 uint32_t param,
96 uint64_t *value)
97 {
98 /* Technically this requires a pipe, but the kernel only supports one pipe
99 * anyway at the time of writing and most of these are clearly pipe
100 * independent. */
101 struct drm_msm_param req = {
102 .pipe = MSM_PIPE_3D0,
103 .param = param,
104 };
105
106 int ret = drmCommandWriteRead(dev->local_fd, DRM_MSM_GET_PARAM, &req,
107 sizeof(req));
108 if (ret)
109 return ret;
110
111 *value = req.value;
112
113 return 0;
114 }