freedreno: add support for FD_MAX_FREQ
[platform/upstream/libdrm.git] / freedreno / kgsl / kgsl_pipe.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include "kgsl_priv.h"
34
35
36 static int kgsl_pipe_get_param(struct fd_pipe *pipe,
37                 enum fd_param_id param, uint64_t *value)
38 {
39         struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
40         switch (param) {
41         case FD_DEVICE_ID:
42                 *value = kgsl_pipe->devinfo.device_id;
43                 return 0;
44         case FD_GPU_ID:
45                 *value = kgsl_pipe->devinfo.gpu_id;
46                 return 0;
47         case FD_GMEM_SIZE:
48                 *value = kgsl_pipe->devinfo.gmem_sizebytes;
49                 return 0;
50         case FD_CHIP_ID:
51                 *value = kgsl_pipe->devinfo.chip_id;
52                 return 0;
53         case FD_MAX_FREQ:
54                 /* unsupported on kgsl */
55                 return -1;
56         default:
57                 ERROR_MSG("invalid param id: %d", param);
58                 return -1;
59         }
60 }
61
62 static int kgsl_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp,
63                 uint64_t timeout)
64 {
65         struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
66         struct kgsl_device_waittimestamp req = {
67                         .timestamp = timestamp,
68                         .timeout   = 5000,
69         };
70         int ret;
71
72         do {
73                 ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP, &req);
74         } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
75         if (ret)
76                 ERROR_MSG("waittimestamp failed! %d (%s)", ret, strerror(errno));
77         else
78                 kgsl_pipe_process_pending(kgsl_pipe, timestamp);
79         return ret;
80 }
81
82 drm_private int kgsl_pipe_timestamp(struct kgsl_pipe *kgsl_pipe,
83                 uint32_t *timestamp)
84 {
85         struct kgsl_cmdstream_readtimestamp req = {
86                         .type = KGSL_TIMESTAMP_RETIRED
87         };
88         int ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, &req);
89         if (ret) {
90                 ERROR_MSG("readtimestamp failed! %d (%s)",
91                                 ret, strerror(errno));
92                 return ret;
93         }
94         *timestamp = req.timestamp;
95         return 0;
96 }
97
98 static void kgsl_pipe_destroy(struct fd_pipe *pipe)
99 {
100         struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
101         struct kgsl_drawctxt_destroy req = {
102                         .drawctxt_id = kgsl_pipe->drawctxt_id,
103         };
104
105         if (kgsl_pipe->drawctxt_id)
106                 ioctl(kgsl_pipe->fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &req);
107
108         if (kgsl_pipe->fd >= 0)
109                 close(kgsl_pipe->fd);
110
111         free(kgsl_pipe);
112 }
113
114 static const struct fd_pipe_funcs funcs = {
115                 .ringbuffer_new = kgsl_ringbuffer_new,
116                 .get_param = kgsl_pipe_get_param,
117                 .wait = kgsl_pipe_wait,
118                 .destroy = kgsl_pipe_destroy,
119 };
120
121 drm_private int is_kgsl_pipe(struct fd_pipe *pipe)
122 {
123         return pipe->funcs == &funcs;
124 }
125
126 /* add buffer to submit list when it is referenced in cmdstream: */
127 drm_private void kgsl_pipe_add_submit(struct kgsl_pipe *kgsl_pipe,
128                 struct kgsl_bo *kgsl_bo)
129 {
130         struct fd_pipe *pipe = &kgsl_pipe->base;
131         struct fd_bo *bo = &kgsl_bo->base;
132         struct list_head *list = &kgsl_bo->list[pipe->id];
133         if (LIST_IS_EMPTY(list)) {
134                 fd_bo_ref(bo);
135         } else {
136                 list_del(list);
137         }
138         list_addtail(list, &kgsl_pipe->submit_list);
139 }
140
141 /* prepare buffers on submit list before flush: */
142 drm_private void kgsl_pipe_pre_submit(struct kgsl_pipe *kgsl_pipe)
143 {
144         struct fd_pipe *pipe = &kgsl_pipe->base;
145         struct kgsl_bo *kgsl_bo = NULL;
146
147         if (!kgsl_pipe->p3d)
148                 kgsl_pipe->p3d = fd_pipe_new(pipe->dev, FD_PIPE_3D);
149
150         LIST_FOR_EACH_ENTRY(kgsl_bo, &kgsl_pipe->submit_list, list[pipe->id]) {
151                 uint32_t timestamp = kgsl_bo_get_timestamp(kgsl_bo);
152                 if (timestamp)
153                         fd_pipe_wait(kgsl_pipe->p3d, timestamp);
154         }
155 }
156
157 /* process buffers on submit list after flush: */
158 drm_private void kgsl_pipe_post_submit(struct kgsl_pipe *kgsl_pipe,
159                 uint32_t timestamp)
160 {
161         struct fd_pipe *pipe = &kgsl_pipe->base;
162         struct kgsl_bo *kgsl_bo = NULL, *tmp;
163
164         LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->submit_list, list[pipe->id]) {
165                 struct list_head *list = &kgsl_bo->list[pipe->id];
166                 list_del(list);
167                 kgsl_bo->timestamp[pipe->id] = timestamp;
168                 list_addtail(list, &kgsl_pipe->pending_list);
169
170                 kgsl_bo_set_timestamp(kgsl_bo, timestamp);
171         }
172
173         if (!kgsl_pipe_timestamp(kgsl_pipe, &timestamp))
174                 kgsl_pipe_process_pending(kgsl_pipe, timestamp);
175 }
176
177 drm_private void kgsl_pipe_process_pending(struct kgsl_pipe *kgsl_pipe,
178                 uint32_t timestamp)
179 {
180         struct fd_pipe *pipe = &kgsl_pipe->base;
181         struct kgsl_bo *kgsl_bo = NULL, *tmp;
182
183         LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->pending_list, list[pipe->id]) {
184                 struct list_head *list = &kgsl_bo->list[pipe->id];
185                 if (kgsl_bo->timestamp[pipe->id] > timestamp)
186                         return;
187                 list_delinit(list);
188                 kgsl_bo->timestamp[pipe->id] = 0;
189                 fd_bo_del(&kgsl_bo->base);
190         }
191 }
192
193 static int getprop(int fd, enum kgsl_property_type type,
194                 void *value, int sizebytes)
195 {
196         struct kgsl_device_getproperty req = {
197                         .type = type,
198                         .value = value,
199                         .sizebytes = sizebytes,
200         };
201         return ioctl(fd, IOCTL_KGSL_DEVICE_GETPROPERTY, &req);
202 }
203
204 #define GETPROP(fd, prop, x) do { \
205         if (getprop((fd), KGSL_PROP_##prop, &(x), sizeof(x))) {     \
206                 ERROR_MSG("failed to get property: " #prop);            \
207                 goto fail;                                              \
208         } } while (0)
209
210
211 drm_private struct fd_pipe * kgsl_pipe_new(struct fd_device *dev,
212                 enum fd_pipe_id id)
213 {
214         static const char *paths[] = {
215                         [FD_PIPE_3D] = "/dev/kgsl-3d0",
216                         [FD_PIPE_2D] = "/dev/kgsl-2d0",
217         };
218         struct kgsl_drawctxt_create req = {
219                         .flags = 0x2000, /* ??? */
220         };
221         struct kgsl_pipe *kgsl_pipe = NULL;
222         struct fd_pipe *pipe = NULL;
223         int ret, fd;
224
225         fd = open(paths[id], O_RDWR);
226         if (fd < 0) {
227                 ERROR_MSG("could not open %s device: %d (%s)",
228                                 paths[id], fd, strerror(errno));
229                 goto fail;
230         }
231
232         ret = ioctl(fd, IOCTL_KGSL_DRAWCTXT_CREATE, &req);
233         if (ret) {
234                 ERROR_MSG("failed to allocate context: %d (%s)",
235                                 ret, strerror(errno));
236                 goto fail;
237         }
238
239         kgsl_pipe = calloc(1, sizeof(*kgsl_pipe));
240         if (!kgsl_pipe) {
241                 ERROR_MSG("allocation failed");
242                 goto fail;
243         }
244
245         pipe = &kgsl_pipe->base;
246         pipe->funcs = &funcs;
247
248         kgsl_pipe->fd = fd;
249         kgsl_pipe->drawctxt_id = req.drawctxt_id;
250
251         list_inithead(&kgsl_pipe->submit_list);
252         list_inithead(&kgsl_pipe->pending_list);
253
254         GETPROP(fd, VERSION,     kgsl_pipe->version);
255         GETPROP(fd, DEVICE_INFO, kgsl_pipe->devinfo);
256
257         INFO_MSG("Pipe Info:");
258         INFO_MSG(" Device:          %s", paths[id]);
259         INFO_MSG(" Chip-id:         %d.%d.%d.%d",
260                         (kgsl_pipe->devinfo.chip_id >> 24) & 0xff,
261                         (kgsl_pipe->devinfo.chip_id >> 16) & 0xff,
262                         (kgsl_pipe->devinfo.chip_id >>  8) & 0xff,
263                         (kgsl_pipe->devinfo.chip_id >>  0) & 0xff);
264         INFO_MSG(" Device-id:       %d", kgsl_pipe->devinfo.device_id);
265         INFO_MSG(" GPU-id:          %d", kgsl_pipe->devinfo.gpu_id);
266         INFO_MSG(" MMU enabled:     %d", kgsl_pipe->devinfo.mmu_enabled);
267         INFO_MSG(" GMEM Base addr:  0x%08x", kgsl_pipe->devinfo.gmem_gpubaseaddr);
268         INFO_MSG(" GMEM size:       0x%08x", kgsl_pipe->devinfo.gmem_sizebytes);
269         INFO_MSG(" Driver version:  %d.%d",
270                         kgsl_pipe->version.drv_major, kgsl_pipe->version.drv_minor);
271         INFO_MSG(" Device version:  %d.%d",
272                         kgsl_pipe->version.dev_major, kgsl_pipe->version.dev_minor);
273
274         return pipe;
275 fail:
276         if (pipe)
277                 fd_pipe_del(pipe);
278         return NULL;
279 }