2 * Copyright © 2018 Google, Inc.
3 * Copyright © 2015 Intel Corporation
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:
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
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.
25 #include "tu_private.h"
30 #include <sys/ioctl.h>
33 #include "drm/msm_drm.h"
36 tu_drm_get_param(const struct tu_physical_device
*dev
,
40 /* Technically this requires a pipe, but the kernel only supports one pipe
41 * anyway at the time of writing and most of these are clearly pipe
43 struct drm_msm_param req
= {
48 int ret
= drmCommandWriteRead(dev
->local_fd
, DRM_MSM_GET_PARAM
, &req
,
59 tu_drm_get_gpu_id(const struct tu_physical_device
*dev
, uint32_t *id
)
62 int ret
= tu_drm_get_param(dev
, MSM_PARAM_GPU_ID
, &value
);
71 tu_drm_get_gmem_size(const struct tu_physical_device
*dev
, uint32_t *size
)
74 int ret
= tu_drm_get_param(dev
, MSM_PARAM_GMEM_SIZE
, &value
);
83 tu_drm_submitqueue_new(const struct tu_device
*dev
,
87 struct drm_msm_submitqueue req
= {
92 int ret
= drmCommandWriteRead(dev
->physical_device
->local_fd
,
93 DRM_MSM_SUBMITQUEUE_NEW
, &req
, sizeof(req
));
102 tu_drm_submitqueue_close(const struct tu_device
*dev
, uint32_t queue_id
)
104 drmCommandWrite(dev
->physical_device
->local_fd
, DRM_MSM_SUBMITQUEUE_CLOSE
,
105 &queue_id
, sizeof(uint32_t));
109 * Return gem handle on success. Return 0 on failure.
112 tu_gem_new(const struct tu_device
*dev
, uint64_t size
, uint32_t flags
)
114 struct drm_msm_gem_new req
= {
119 int ret
= drmCommandWriteRead(dev
->physical_device
->local_fd
,
120 DRM_MSM_GEM_NEW
, &req
, sizeof(req
));
128 tu_gem_import_dmabuf(const struct tu_device
*dev
, int prime_fd
, uint64_t size
)
130 /* lseek() to get the real size */
131 off_t real_size
= lseek(prime_fd
, 0, SEEK_END
);
132 lseek(prime_fd
, 0, SEEK_SET
);
133 if (real_size
< 0 || (uint64_t) real_size
< size
)
137 int ret
= drmPrimeFDToHandle(dev
->physical_device
->local_fd
, prime_fd
,
146 tu_gem_export_dmabuf(const struct tu_device
*dev
, uint32_t gem_handle
)
149 int ret
= drmPrimeHandleToFD(dev
->physical_device
->local_fd
, gem_handle
,
150 DRM_CLOEXEC
, &prime_fd
);
152 return ret
== 0 ? prime_fd
: -1;
156 tu_gem_close(const struct tu_device
*dev
, uint32_t gem_handle
)
158 struct drm_gem_close req
= {
159 .handle
= gem_handle
,
162 drmIoctl(dev
->physical_device
->local_fd
, DRM_IOCTL_GEM_CLOSE
, &req
);
165 /** Return UINT64_MAX on error. */
167 tu_gem_info(const struct tu_device
*dev
, uint32_t gem_handle
, uint32_t info
)
169 struct drm_msm_gem_info req
= {
170 .handle
= gem_handle
,
174 int ret
= drmCommandWriteRead(dev
->physical_device
->local_fd
,
175 DRM_MSM_GEM_INFO
, &req
, sizeof(req
));
182 /** Return UINT64_MAX on error. */
184 tu_gem_info_offset(const struct tu_device
*dev
, uint32_t gem_handle
)
186 return tu_gem_info(dev
, gem_handle
, MSM_INFO_GET_OFFSET
);
189 /** Return UINT64_MAX on error. */
191 tu_gem_info_iova(const struct tu_device
*dev
, uint32_t gem_handle
)
193 return tu_gem_info(dev
, gem_handle
, MSM_INFO_GET_IOVA
);