summaryrefslogtreecommitdiff
path: root/backend/src/driver/cl_gen_driver.hpp
blob: 39607c1660845ed8a75ec1800f2ccdbaff111ca7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
#ifndef __CL_GEN_DRIVER_HPP__
#define __CL_GEN_DRIVER_HPP__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <i915_drm.h>
#include <intel_bufmgr.h>
#include "cl_driver.h"
#include "cl_device_id.h"
#include "cl_context.h"
#include "cl_command_queue.h"
#include "cl_program.h"
#include "cl_kernel.h"
#include "cl_mem.h"
#include "cl_gen_gpu_defines.h"
#ifdef __cplusplus
}
#endif /* __cplusplus */
#include "cl_gen_gpu_state.hpp"
#include "sys/platform.hpp"
#include "sys/list.hpp"

using namespace gbe;

INLINE int findIndexByDevice(cl_context ctx, cl_device_id device)
{
  cl_uint i;
  for (i = 0; i < ctx->device_num; i++) {
    if (ctx->devices[i] == device)
      return (int)i;
  }

  return -1;
}

INLINE void* getGenDevicePrivate(cl_device_id device)
{
  return device->pdata;
}

INLINE void setGenDevicePrivate(cl_device_id device, void* pdata)
{
  device->pdata = pdata;
}

INLINE void* getGenContextPrivate(cl_context ctx, cl_device_id device)
{
  int index;

  if (ctx->device_num == 1) {
    return ctx->pdata;
  }

  index = findIndexByDevice(ctx, device);

  if (index >= 0)
    return ((void**)(ctx->pdata))[index];

  return NULL;
}

INLINE void setGenContextPrivate(cl_context ctx, cl_device_id device, void* pdata)
{
  int index;

  if (ctx->device_num == 1) {
    ctx->pdata = pdata;
    return;
  }

  index = findIndexByDevice(ctx, device);

  assert(index >= 0);
  if (index >= 0)
    ((void**)(ctx->pdata))[index] = pdata;
}

INLINE void* getGenCommandQueuePrivate(cl_command_queue queue)
{
  return queue->pdata;
}

INLINE void setGenCommandQueuePrivate(cl_command_queue queue, void* pdata)
{
  queue->pdata = pdata;
}

INLINE void* getGenProgramPrivate(cl_program prog, cl_device_id device)
{
  int index;

  if (prog->ctx->device_num == 1) {
    return prog->pdata;
  }

  index = findIndexByDevice(prog->ctx, device);

  if (index >= 0)
    return ((void**)(prog->pdata))[index];

  return NULL;
}

INLINE void setGenProgramPrivate(cl_program prog, cl_device_id device, void* pdata)
{
  int index;

  if (prog->ctx->device_num == 1) {
    prog->pdata = pdata;
    return;
  }

  index = findIndexByDevice(prog->ctx, device);

  assert(index >= 0);
  if (index >= 0)
    ((void**)(prog->pdata))[index] = pdata;
}

INLINE void* getGenKernelPrivate(cl_kernel kernel, cl_device_id device)
{
  int index;

  if (kernel->program->ctx->device_num == 1) {
    return kernel->pdata;
  }

  index = findIndexByDevice(kernel->program->ctx, device);

  if (index >= 0)
    return ((void**)(kernel->pdata))[index];

  return NULL;
}

INLINE void setGenKernelPrivate(cl_kernel kernel, cl_device_id device, void* pdata)
{
  int index;

  if (kernel->program->ctx->device_num == 1) {
    kernel->pdata = pdata;
    return;
  }

  index = findIndexByDevice(kernel->program->ctx, device);

  assert(index >= 0);
  if (index >= 0)
    ((void**)(kernel->pdata))[index] = pdata;
}

INLINE void* getGenMemPrivate(cl_mem mem, cl_device_id device)
{
  int index;

  if (mem->ctx->device_num == 1) {
    return mem->pdata;
  }

  index = findIndexByDevice(mem->ctx, device);

  if (index >= 0)
    return ((void**)(mem->pdata))[index];

  return NULL;
}

INLINE void setGenMemPrivate(cl_mem mem, cl_device_id device, void* pdata)
{
  int index;

  if (mem->ctx->device_num == 1) {
    mem->pdata = pdata;
    return;
  }

  index = findIndexByDevice(mem->ctx, device);

  assert(index >= 0);
  if (index >= 0)
    ((void**)(mem->pdata))[index] = pdata;
}

struct GenGPUDevice {
  dri_bufmgr *bufmgr;
  int fd;
  bool from_x11;
  int device_id;
  int gen_ver;
  cl_uint max_thread_per_unit;
  cl_uint sub_slice_count;
  cl_ulong scratch_mem_size;
  GenGPUDevice();
  ~GenGPUDevice();
};

struct GenGPUContext {
  dri_bufmgr *bufmgr;
  drm_intel_context *ctx;
  GenGPUContext(dri_bufmgr *bufmgr);
  ~GenGPUContext(void);
};

struct GenGPUCommandQueue;
struct GenGPUWorkItem {       // Represent Some real work for GPU to do.
  list<cl_event> dependEvents;// The events we depend on.
  cl_event event;             // The event represent us.
  cl_int state;               // Exec state.
  GenGPUWorkItem(cl_event event, const cl_event* dependEvents, cl_uint num_events);
  virtual bool submit(void) = 0;
  virtual bool complete(void) = 0;
  cl_int isReady(void); // >0 means ready, =0 means not ready, <0 means error and cancel
  bool setStatus(cl_int status);
  virtual ~GenGPUWorkItem() { };
};

struct GenGPUCommandQueue {
  cl_command_queue queue;  // Pointer back to queue.
  dri_bufmgr *bufmgr;
  drm_intel_context *ctx;
  pthread_t worker;
  pthread_cond_t cond;
  pthread_mutex_t mutex;
  bool quit;
  bool inExec;
  list<GenGPUWorkItem*> workItems;
  cl_uint cookie;
  bool enqueueWorkItem(GenGPUWorkItem* item);
  void eventStateChanged(void);
  bool waitForEvents(const cl_event* events, int num); // True means ok, False means canceled.
  void waitForFlush(void);
  void waitForFinish(void);
  GenGPUCommandQueue(dri_bufmgr *bufmgr, drm_intel_context *ctx, cl_command_queue queue);
  ~GenGPUCommandQueue(void);
};

struct GenGPUMem {
  drm_intel_bo *bo;
  GenGPUContext* gpuCtx;
  bool bindUserPtr;
  void* alignedHostPtr;
  void* mappedAddr;
  bool writeMap;
  size_t realSize; // Maybe diff from the size in buffer, because align, etc.
  cl_gpgpu_tiling tiling;
  bool mappedGtt;
  volatile int mapRef;
  pthread_mutex_t mutex;
  GenGPUMem(GenGPUContext* ctx) : bo(NULL), gpuCtx(ctx), bindUserPtr(false), alignedHostPtr(NULL),
    mappedAddr(NULL), writeMap(false), realSize(0), tiling(GPGPU_NO_TILE), mappedGtt(false), mapRef(0) {
    pthread_mutex_init(&this->mutex, NULL);
  }
  ~GenGPUMem(void);
  bool genAllocMemBo(cl_mem mem);
  void* genMapBo(cl_mem mem, bool write);
  void genUnMapBo(cl_mem mem);
};

struct GenGPUWorkItemMapBuf : public GenGPUWorkItem {
  virtual ~GenGPUWorkItemMapBuf(void) { }
  GenGPUWorkItemMapBuf(cl_mem mem, GenGPUMem* genMem, void* addr, cl_map_flags flags,
    size_t offset, size_t size, cl_event event, const cl_event* dependEvents, cl_uint num_events);
  virtual bool submit(void);
  virtual bool complete(void);
  void* mappedAddr;
  cl_mem mem;
  GenGPUMem* genMem;
  size_t offset;
  size_t size;
  cl_map_flags flags;
};

struct GenGPUWorkItemUnMapBuf : public GenGPUWorkItem {
  virtual ~GenGPUWorkItemUnMapBuf(void) { }
  GenGPUWorkItemUnMapBuf(cl_mem mem, GenGPUMem* genMem, void* mappedAddr, size_t mappedSize,
    size_t mappedOffset, bool needToFree, cl_event event, const cl_event* dependEvents, cl_uint num_events);
  virtual bool submit(void);
  virtual bool complete(void);
  cl_mem mem;
  GenGPUMem* genMem;
  void* mappedAddr;
  size_t mappedSize;
  size_t mappedOffset;
  bool needToFree;
};

struct GenGPULockerHelper {
  pthread_mutex_t* mutex;
  GenGPULockerHelper(pthread_mutex_t* m) : mutex(m) {
    pthread_mutex_lock(this->mutex);
  }
  ~GenGPULockerHelper(void) {
    pthread_mutex_unlock(this->mutex);
  }
};

#endif /* __CL_GEN_DRIVER_HPP__ */