OSDN Git Service

freedreno: add gpu-id property
[android-x86/external-libdrm.git] / freedreno / freedreno_pipe.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2012 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 #include "freedreno_drmif.h"
30 #include "freedreno_priv.h"
31
32
33 static int getprop(int fd, enum kgsl_property_type type,
34                 void *value, int sizebytes)
35 {
36         struct kgsl_device_getproperty req = {
37                         .type = type,
38                         .value = value,
39                         .sizebytes = sizebytes,
40         };
41         return ioctl(fd, IOCTL_KGSL_DEVICE_GETPROPERTY, &req);
42 }
43
44 #define GETPROP(fd, prop, x) do { \
45         if (getprop((fd), KGSL_PROP_##prop, &(x), sizeof(x))) {     \
46                 ERROR_MSG("failed to get property: " #prop);            \
47                 goto fail;                                              \
48         } } while (0)
49
50
51 struct fd_pipe * fd_pipe_new(struct fd_device *dev, enum fd_pipe_id id)
52 {
53         static const char *paths[] = {
54                         [FD_PIPE_3D] = "/dev/kgsl-3d0",
55                         [FD_PIPE_2D] = "/dev/kgsl-2d0",
56         };
57         struct kgsl_drawctxt_create req = {
58                         .flags = 0x2000, /* ??? */
59         };
60         struct fd_pipe *pipe = NULL;
61         int ret, fd;
62
63         if (id > FD_PIPE_MAX) {
64                 ERROR_MSG("invalid pipe id: %d", id);
65                 goto fail;
66         }
67
68         fd = open(paths[id], O_RDWR);
69         if (fd < 0) {
70                 ERROR_MSG("could not open %s device: %d (%s)",
71                                 paths[id], fd, strerror(errno));
72                 goto fail;
73         }
74
75         ret = ioctl(fd, IOCTL_KGSL_DRAWCTXT_CREATE, &req);
76         if (ret) {
77                 ERROR_MSG("failed to allocate context: %d (%s)",
78                                 ret, strerror(errno));
79                 goto fail;
80         }
81
82         pipe = calloc(1, sizeof(*pipe));
83         if (!pipe) {
84                 ERROR_MSG("allocation failed");
85                 goto fail;
86         }
87
88         pipe->dev = dev;
89         pipe->id = id;
90         pipe->fd = fd;
91         pipe->drawctxt_id = req.drawctxt_id;
92
93         list_inithead(&pipe->submit_list);
94         list_inithead(&pipe->pending_list);
95
96         GETPROP(fd, VERSION,     pipe->version);
97         GETPROP(fd, DEVICE_INFO, pipe->devinfo);
98
99         INFO_MSG("Pipe Info:");
100         INFO_MSG(" Device:          %s", paths[id]);
101         INFO_MSG(" Chip-id:         %d.%d.%d.%d",
102                         (pipe->devinfo.chip_id >> 24) & 0xff,
103                         (pipe->devinfo.chip_id >> 16) & 0xff,
104                         (pipe->devinfo.chip_id >>  8) & 0xff,
105                         (pipe->devinfo.chip_id >>  0) & 0xff);
106         INFO_MSG(" Device-id:       %d", pipe->devinfo.device_id);
107         INFO_MSG(" GPU-id:          %d", pipe->devinfo.gpu_id);
108         INFO_MSG(" MMU enabled:     %d", pipe->devinfo.mmu_enabled);
109         INFO_MSG(" GMEM Base addr:  0x%08x", pipe->devinfo.gmem_gpubaseaddr);
110         INFO_MSG(" GMEM size:       0x%08x", pipe->devinfo.gmem_sizebytes);
111         INFO_MSG(" Driver version:  %d.%d",
112                         pipe->version.drv_major, pipe->version.drv_minor);
113         INFO_MSG(" Device version:  %d.%d",
114                         pipe->version.dev_major, pipe->version.dev_minor);
115
116         return pipe;
117 fail:
118         if (pipe)
119                 fd_pipe_del(pipe);
120         return NULL;
121 }
122
123 void fd_pipe_del(struct fd_pipe *pipe)
124 {
125         struct kgsl_drawctxt_destroy req = {
126                         .drawctxt_id = pipe->drawctxt_id,
127         };
128
129         if (pipe->drawctxt_id)
130                 ioctl(pipe->fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &req);
131
132         if (pipe->fd)
133                 close(pipe->fd);
134
135         free(pipe);
136 }
137
138 int fd_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
139                 uint64_t *value)
140 {
141         switch (param) {
142         case FD_DEVICE_ID:
143                 *value = pipe->devinfo.device_id;
144                 return 0;
145         case FD_GPU_ID:
146                 *value = pipe->devinfo.gpu_id;
147                 return 0;
148         case FD_GMEM_SIZE:
149                 *value = pipe->devinfo.gmem_sizebytes;
150                 return 0;
151         default:
152                 ERROR_MSG("invalid param id: %d", param);
153                 return -1;
154         }
155 }
156
157 int fd_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp)
158 {
159         struct kgsl_device_waittimestamp req = {
160                         .timestamp = timestamp,
161                         .timeout   = 5000,
162         };
163         int ret;
164
165         do {
166                 ret = ioctl(pipe->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP, &req);
167         } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
168         if (ret)
169                 ERROR_MSG("waittimestamp failed! %d (%s)", ret, strerror(errno));
170         else
171                 fd_pipe_process_pending(pipe, timestamp);
172         return ret;
173 }
174
175 int fd_pipe_timestamp(struct fd_pipe *pipe, uint32_t *timestamp)
176 {
177         struct kgsl_cmdstream_readtimestamp req = {
178                         .type = KGSL_TIMESTAMP_RETIRED
179         };
180         int ret = ioctl(pipe->fd, IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, &req);
181         if (ret) {
182                 ERROR_MSG("readtimestamp failed! %d (%s)",
183                                 ret, strerror(errno));
184                 return ret;
185         }
186         *timestamp = req.timestamp;
187         return 0;
188 }
189
190 /* add buffer to submit list when it is referenced in cmdstream: */
191 void fd_pipe_add_submit(struct fd_pipe *pipe, struct fd_bo *bo)
192 {
193         struct list_head *list = &bo->list[pipe->id];
194         if (LIST_IS_EMPTY(list)) {
195                 fd_bo_ref(bo);
196         } else {
197                 list_del(list);
198         }
199         list_addtail(list, &pipe->submit_list);
200 }
201
202 /* process buffers on submit list after flush: */
203 void fd_pipe_process_submit(struct fd_pipe *pipe, uint32_t timestamp)
204 {
205         struct fd_bo *bo, *tmp;
206
207         LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &pipe->submit_list, list[pipe->id]) {
208                 struct list_head *list = &bo->list[pipe->id];
209                 list_del(list);
210                 bo->timestamp[pipe->id] = timestamp;
211                 list_addtail(list, &pipe->pending_list);
212         }
213
214         if (!fd_pipe_timestamp(pipe, &timestamp))
215                 fd_pipe_process_pending(pipe, timestamp);
216 }
217
218 void fd_pipe_process_pending(struct fd_pipe *pipe, uint32_t timestamp)
219 {
220         struct fd_bo *bo, *tmp;
221
222         LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &pipe->pending_list, list[pipe->id]) {
223                 struct list_head *list = &bo->list[pipe->id];
224                 if (bo->timestamp[pipe->id] > timestamp)
225                         return;
226                 list_delinit(list);
227                 bo->timestamp[pipe->id] = 0;
228                 fd_bo_del(bo);
229         }
230 }