baf505ce1105f9018cb410257c27cb6984baf674
[platform/upstream/libdrm.git] / freedreno / msm / msm_drm.h
1 /*
2  * Copyright (C) 2013 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
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 #ifndef __MSM_DRM_H__
26 #define __MSM_DRM_H__
27
28 #include <stddef.h>
29 #include "drm.h"
30
31 /* Please note that modifications to all structs defined here are
32  * subject to backwards-compatibility constraints:
33  *  1) Do not use pointers, use uint64_t instead for 32 bit / 64 bit
34  *     user/kernel compatibility
35  *  2) Keep fields aligned to their size
36  *  3) Because of how drm_ioctl() works, we can add new fields at
37  *     the end of an ioctl if some care is taken: drm_ioctl() will
38  *     zero out the new fields at the tail of the ioctl, so a zero
39  *     value should have a backwards compatible meaning.  And for
40  *     output params, userspace won't see the newly added output
41  *     fields.. so that has to be somehow ok.
42  */
43
44 #define MSM_PIPE_NONE        0x00
45 #define MSM_PIPE_2D0         0x01
46 #define MSM_PIPE_2D1         0x02
47 #define MSM_PIPE_3D0         0x10
48
49 /* timeouts are specified in clock-monotonic absolute times (to simplify
50  * restarting interrupted ioctls).  The following struct is logically the
51  * same as 'struct timespec' but 32/64b ABI safe.
52  */
53 struct drm_msm_timespec {
54         int64_t tv_sec;          /* seconds */
55         int64_t tv_nsec;         /* nanoseconds */
56 };
57
58 #define MSM_PARAM_GPU_ID     0x01
59 #define MSM_PARAM_GMEM_SIZE  0x02
60 #define MSM_PARAM_CHIP_ID    0x03
61 #define MSM_PARAM_MAX_FREQ   0x04
62
63 struct drm_msm_param {
64         uint32_t pipe;           /* in, MSM_PIPE_x */
65         uint32_t param;          /* in, MSM_PARAM_x */
66         uint64_t value;          /* out (get_param) or in (set_param) */
67 };
68
69 /*
70  * GEM buffers:
71  */
72
73 #define MSM_BO_SCANOUT       0x00000001     /* scanout capable */
74 #define MSM_BO_GPU_READONLY  0x00000002
75 #define MSM_BO_CACHE_MASK    0x000f0000
76 /* cache modes */
77 #define MSM_BO_CACHED        0x00010000
78 #define MSM_BO_WC            0x00020000
79 #define MSM_BO_UNCACHED      0x00040000
80
81 #define MSM_BO_FLAGS         (MSM_BO_SCANOUT | \
82                               MSM_BO_GPU_READONLY | \
83                               MSM_BO_CACHED | \
84                               MSM_BO_WC | \
85                               MSM_BO_UNCACHED)
86
87 struct drm_msm_gem_new {
88         uint64_t size;           /* in */
89         uint32_t flags;          /* in, mask of MSM_BO_x */
90         uint32_t handle;         /* out */
91 };
92
93 struct drm_msm_gem_info {
94         uint32_t handle;         /* in */
95         uint32_t pad;
96         uint64_t offset;         /* out, offset to pass to mmap() */
97 };
98
99 #define MSM_PREP_READ        0x01
100 #define MSM_PREP_WRITE       0x02
101 #define MSM_PREP_NOSYNC      0x04
102
103 #define MSM_PREP_FLAGS       (MSM_PREP_READ | MSM_PREP_WRITE | MSM_PREP_NOSYNC)
104
105 struct drm_msm_gem_cpu_prep {
106         uint32_t handle;         /* in */
107         uint32_t op;             /* in, mask of MSM_PREP_x */
108         struct drm_msm_timespec timeout;   /* in */
109 };
110
111 struct drm_msm_gem_cpu_fini {
112         uint32_t handle;         /* in */
113 };
114
115 /*
116  * Cmdstream Submission:
117  */
118
119 /* The value written into the cmdstream is logically:
120  *
121  *   ((relocbuf->gpuaddr + reloc_offset) << shift) | or
122  *
123  * When we have GPU's w/ >32bit ptrs, it should be possible to deal
124  * with this by emit'ing two reloc entries with appropriate shift
125  * values.  Or a new MSM_SUBMIT_CMD_x type would also be an option.
126  *
127  * NOTE that reloc's must be sorted by order of increasing submit_offset,
128  * otherwise EINVAL.
129  */
130 struct drm_msm_gem_submit_reloc {
131         uint32_t submit_offset;  /* in, offset from submit_bo */
132         uint32_t or;             /* in, value OR'd with result */
133         int32_t  shift;          /* in, amount of left shift (can be negative) */
134         uint32_t reloc_idx;      /* in, index of reloc_bo buffer */
135         uint64_t reloc_offset;   /* in, offset from start of reloc_bo */
136 };
137
138 /* submit-types:
139  *   BUF - this cmd buffer is executed normally.
140  *   IB_TARGET_BUF - this cmd buffer is an IB target.  Reloc's are
141  *      processed normally, but the kernel does not setup an IB to
142  *      this buffer in the first-level ringbuffer
143  *   CTX_RESTORE_BUF - only executed if there has been a GPU context
144  *      switch since the last SUBMIT ioctl
145  */
146 #define MSM_SUBMIT_CMD_BUF             0x0001
147 #define MSM_SUBMIT_CMD_IB_TARGET_BUF   0x0002
148 #define MSM_SUBMIT_CMD_CTX_RESTORE_BUF 0x0003
149 struct drm_msm_gem_submit_cmd {
150         uint32_t type;           /* in, one of MSM_SUBMIT_CMD_x */
151         uint32_t submit_idx;     /* in, index of submit_bo cmdstream buffer */
152         uint32_t submit_offset;  /* in, offset into submit_bo */
153         uint32_t size;           /* in, cmdstream size */
154         uint32_t pad;
155         uint32_t nr_relocs;      /* in, number of submit_reloc's */
156         uint64_t __user relocs;  /* in, ptr to array of submit_reloc's */
157 };
158
159 /* Each buffer referenced elsewhere in the cmdstream submit (ie. the
160  * cmdstream buffer(s) themselves or reloc entries) has one (and only
161  * one) entry in the submit->bos[] table.
162  *
163  * As a optimization, the current buffer (gpu virtual address) can be
164  * passed back through the 'presumed' field.  If on a subsequent reloc,
165  * userspace passes back a 'presumed' address that is still valid,
166  * then patching the cmdstream for this entry is skipped.  This can
167  * avoid kernel needing to map/access the cmdstream bo in the common
168  * case.
169  */
170 #define MSM_SUBMIT_BO_READ             0x0001
171 #define MSM_SUBMIT_BO_WRITE            0x0002
172
173 #define MSM_SUBMIT_BO_FLAGS            (MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_WRITE)
174
175 struct drm_msm_gem_submit_bo {
176         uint32_t flags;          /* in, mask of MSM_SUBMIT_BO_x */
177         uint32_t handle;         /* in, GEM handle */
178         uint64_t presumed;       /* in/out, presumed buffer address */
179 };
180
181 /* Each cmdstream submit consists of a table of buffers involved, and
182  * one or more cmdstream buffers.  This allows for conditional execution
183  * (context-restore), and IB buffers needed for per tile/bin draw cmds.
184  */
185 struct drm_msm_gem_submit {
186         uint32_t pipe;           /* in, MSM_PIPE_x */
187         uint32_t fence;          /* out */
188         uint32_t nr_bos;         /* in, number of submit_bo's */
189         uint32_t nr_cmds;        /* in, number of submit_cmd's */
190         uint64_t __user bos;     /* in, ptr to array of submit_bo's */
191         uint64_t __user cmds;    /* in, ptr to array of submit_cmd's */
192 };
193
194 /* The normal way to synchronize with the GPU is just to CPU_PREP on
195  * a buffer if you need to access it from the CPU (other cmdstream
196  * submission from same or other contexts, PAGE_FLIP ioctl, etc, all
197  * handle the required synchronization under the hood).  This ioctl
198  * mainly just exists as a way to implement the gallium pipe_fence
199  * APIs without requiring a dummy bo to synchronize on.
200  */
201 struct drm_msm_wait_fence {
202         uint32_t fence;          /* in */
203         uint32_t pad;
204         struct drm_msm_timespec timeout;   /* in */
205 };
206
207 #define DRM_MSM_GET_PARAM              0x00
208 /* placeholder:
209 #define DRM_MSM_SET_PARAM              0x01
210  */
211 #define DRM_MSM_GEM_NEW                0x02
212 #define DRM_MSM_GEM_INFO               0x03
213 #define DRM_MSM_GEM_CPU_PREP           0x04
214 #define DRM_MSM_GEM_CPU_FINI           0x05
215 #define DRM_MSM_GEM_SUBMIT             0x06
216 #define DRM_MSM_WAIT_FENCE             0x07
217 #define DRM_MSM_NUM_IOCTLS             0x08
218
219 #define DRM_IOCTL_MSM_GET_PARAM        DRM_IOWR(DRM_COMMAND_BASE + DRM_MSM_GET_PARAM, struct drm_msm_param)
220 #define DRM_IOCTL_MSM_GEM_NEW          DRM_IOWR(DRM_COMMAND_BASE + DRM_MSM_GEM_NEW, struct drm_msm_gem_new)
221 #define DRM_IOCTL_MSM_GEM_INFO         DRM_IOWR(DRM_COMMAND_BASE + DRM_MSM_GEM_INFO, struct drm_msm_gem_info)
222 #define DRM_IOCTL_MSM_GEM_CPU_PREP     DRM_IOW (DRM_COMMAND_BASE + DRM_MSM_GEM_CPU_PREP, struct drm_msm_gem_cpu_prep)
223 #define DRM_IOCTL_MSM_GEM_CPU_FINI     DRM_IOW (DRM_COMMAND_BASE + DRM_MSM_GEM_CPU_FINI, struct drm_msm_gem_cpu_fini)
224 #define DRM_IOCTL_MSM_GEM_SUBMIT       DRM_IOWR(DRM_COMMAND_BASE + DRM_MSM_GEM_SUBMIT, struct drm_msm_gem_submit)
225 #define DRM_IOCTL_MSM_WAIT_FENCE       DRM_IOW (DRM_COMMAND_BASE + DRM_MSM_WAIT_FENCE, struct drm_msm_wait_fence)
226
227 #endif /* __MSM_DRM_H__ */