summaryrefslogtreecommitdiff
path: root/backend/src/driver/cl_gen_device_id.cpp
blob: 049d555ed6be74973a07dbc06230a21fa1d65bb0 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
 * 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/>.
 *
 */

#ifdef __cplusplus
extern "C" {  // for the C header files
#endif /* __cplusplus */
#include <malloc.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/sysinfo.h>
#include "cl_platform_id.h"
#include "cl_device_id.h"
#include "cl_gen_driver.h"
#include "cl_gen_devices.h"
#ifdef __cplusplus
}
#endif /* __cplusplus */
#include "cl_gen_driver.hpp"
#include "sys/assert.hpp"
#include "sys/alloc.hpp"

extern _cl_device_id intel_ivb_gt2_device;
extern _cl_device_id intel_ivb_gt1_device;
extern _cl_device_id intel_baytrail_t_device;
extern _cl_device_id intel_hsw_gt1_device;
extern _cl_device_id intel_hsw_gt2_device;
extern _cl_device_id intel_hsw_gt3_device;
extern _cl_device_id intel_brw_gt1_device;
extern _cl_device_id intel_brw_gt2_device;
extern _cl_device_id intel_brw_gt3_device;
extern _cl_device_id intel_chv_device;
extern _cl_device_id intel_skl_gt1_device;
extern _cl_device_id intel_skl_gt2_device;
extern _cl_device_id intel_skl_gt3_device;
extern _cl_device_id intel_skl_gt4_device;
extern _cl_device_id intel_bxt_device;

static cl_device_id gen_device;
static _cl_extensions gen_device_extensions;
static char gen_ext_string[CL_MAX_EXTENSION_LENGTH];

static GenGPUDevice* getGPUDevice(cl_device_id device)
{
  return reinterpret_cast<GenGPUDevice*>(getGenDevicePrivate(device));
}

/* just used for maximum relocation number in drm_intel */
#define BATCH_SIZE 0x4000

/* set OCL_DUMP_AUB=1 to get aub file */
static void setGPUAubDump(GenGPUDevice *gpu)
{
  char *val;
  val = getenv("OCL_DUMP_AUB");
  if (!val)
    return;
  if (atoi(val) != 0) {
    drm_intel_bufmgr_gem_set_aub_filename(gpu->bufmgr, "beignet.aub");
    drm_intel_bufmgr_gem_set_aub_dump(gpu->bufmgr, 1);
  }
}

static int gpgpuDeviceInit(GenGPUDevice *gpu)
{
  gpu->bufmgr = drm_intel_bufmgr_gem_init(gpu->fd, BATCH_SIZE);
  if (!gpu->bufmgr)
    return 0;

  drm_intel_bufmgr_gem_enable_reuse(gpu->bufmgr);
  gpu->device_id = drm_intel_bufmgr_gem_get_devid(gpu->bufmgr);
  setGPUAubDump(gpu);

#if EMULATE_GEN
  gpu->gen_ver = EMULATE_GEN;
  if (EMULATE_GEN == 75)
    gpu->device_id = PCI_CHIP_HASWELL_L;       /* we pick L for HSW */
  else if (EMULATE_GEN == 7)
    gpu->device_id = PCI_CHIP_IVYBRIDGE_GT2; /* we pick GT2 for IVB */
  else
    FATAL ("Unsupported Gen for emulation");
#else
  if (IS_GEN9(gpu->device_id))
    gpu->gen_ver = 9;
  else if (IS_GEN8(gpu->device_id))
    gpu->gen_ver = 8;
  else if (IS_GEN75(gpu->device_id))
    gpu->gen_ver = 75;
  else if (IS_GEN7(gpu->device_id))
    gpu->gen_ver = 7;
  else if (IS_GEN6(gpu->device_id))
    gpu->gen_ver = 6;
  else if (IS_IGDNG(gpu->device_id))
    gpu->gen_ver = 5;
  else
    gpu->gen_ver = 4;
#endif /* EMULATE_GEN */

  return 1;
}

static int gpgpuDeviceOpenRender(GenGPUDevice *gpu)
{
  int cardi;
  int dev_fd;

  char card_name[20];
  for(cardi = 0; cardi < 16; cardi++) {
    sprintf(card_name, "/dev/dri/renderD%d", 128+cardi);
    if (access(card_name, R_OK) != 0)
      continue;

    dev_fd = open(card_name, O_RDWR);
    if (dev_fd == -1)
      continue;

    /* Try to init the device. */
    gpu->fd = dev_fd;
    if (!gpgpuDeviceInit(gpu)) {
      if (gpu->bufmgr)
        drm_intel_bufmgr_destroy(gpu->bufmgr);

      close(gpu->fd);
      gpu->fd = -1;
      continue;
    } else {
      /* We find the right one. */
      return 1;
    }
  }

  return 0;
}

static int gpgpuDeviceOpenMaster(GenGPUDevice *gpu)
{
  int cardi;
  int dev_fd, ret;
  drm_client_t client;

  char card_name[20];
  for(cardi = 0; cardi < 16; cardi++) {
    // usually dev_name = "/dev/dri/card%d"
    sprintf(card_name, "/dev/dri/card%d", cardi);
    if (access(card_name, R_OK) != 0)
      continue;

    dev_fd = open(card_name, O_RDWR);
    if (dev_fd == -1) {
      fprintf(stderr, "open(\"%s\", O_RDWR) failed: %s\n", card_name, strerror(errno));
      continue;
    }

    // Check that we're authenticated
    memset(&client, 0, sizeof(drm_client_t));
    ret = ioctl(dev_fd, DRM_IOCTL_GET_CLIENT, &client);
    if (ret == -1) {
      fprintf(stderr, "ioctl(dev_fd, DRM_IOCTL_GET_CLIENT, &client) failed: %s\n", strerror(errno));
      close(dev_fd);
      continue;
    }

    /* Try to init the device. */
    gpu->fd = dev_fd;
    if (!gpgpuDeviceInit(gpu)) {
      if (gpu->bufmgr)
        drm_intel_bufmgr_destroy(gpu->bufmgr);

      close(gpu->fd);
      gpu->fd = -1;
      continue;
    } else {
      /* We find the right one. */
      return 1;
    }
  }

  return 0;
}

static int gpgpuDeviceOpenX11(GenGPUDevice *gpu)
{
  gpu->fd = dri2OpenX11();
  if (gpu->fd >= 0) {
    if (!gpgpuDeviceInit(gpu)) {
      if (gpu->bufmgr)
        drm_intel_bufmgr_destroy(gpu->bufmgr);

      dri2CloseX11();
      gpu->fd = -1;
    } else {
      gpu->from_x11 = true;
      return 1;
    }
  }

  return 0;
}

static void gpgpuDeviceClose(GenGPUDevice *gpu)
{
  if (gpu->bufmgr)
    drm_intel_bufmgr_destroy(gpu->bufmgr);

  if (gpu->from_x11) {
    GBE_ASSERT(gpu->fd >= 0);
    dri2CloseX11();
    gpu->fd = -1;
  }

  if (gpu->fd >= 0) {
    close(gpu->fd);
    gpu->fd = -1;
  }
}

GenGPUDevice::GenGPUDevice(void) : bufmgr(NULL), fd(-1), from_x11(0), device_id(0),
  gen_ver(0), max_thread_per_unit(0), sub_slice_count(0), scratch_mem_size(0)
{
  if (!gpgpuDeviceOpenX11(this) && !gpgpuDeviceOpenRender(this)
      && !gpgpuDeviceOpenMaster(this)) {
    fprintf(stderr, "Device open failed, no support for Intel's GEN...\n");
    return;
  }

  if (this->gen_ver < 7) {
    fprintf(stderr, "GEN Version is %d, too old and no support...\n", this->gen_ver);
    return;
  }
}

GenGPUDevice::~GenGPUDevice(void)
{
  gpgpuDeviceClose(this);
}

static void initGenDevice(GenGPUDevice* gpu)
{
  int device_id = gpu->device_id;

#define DECL_INFO_STRING(STRUCT, FIELD, STRING) \
    STRUCT.FIELD = STRING; \
    STRUCT.JOIN(FIELD,_sz) = sizeof(STRING); \
    gen_device = &STRUCT; \
    break;

  switch (device_id) {
    case PCI_CHIP_HASWELL_D1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell GT1 Desktop");
    case PCI_CHIP_HASWELL_D2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell GT2 Desktop");
    case PCI_CHIP_HASWELL_D3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell GT3 Desktop");
    case PCI_CHIP_HASWELL_S1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell GT1 Server");
    case PCI_CHIP_HASWELL_S2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell GT2 Server");
    case PCI_CHIP_HASWELL_S3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell GT3 Server");
    case PCI_CHIP_HASWELL_M1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell GT1 Mobile");
    case PCI_CHIP_HASWELL_M2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell GT2 Mobile");
    case PCI_CHIP_HASWELL_M3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell GT3 Mobile");
    case PCI_CHIP_HASWELL_B1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell GT1 reserved");
    case PCI_CHIP_HASWELL_B2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell GT2 reserved");
    case PCI_CHIP_HASWELL_B3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell GT3 reserved");
    case PCI_CHIP_HASWELL_E1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell GT1 reserved");
    case PCI_CHIP_HASWELL_E2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell GT2 reserved");
    case PCI_CHIP_HASWELL_E3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell GT3 reserved");
    case PCI_CHIP_HASWELL_SDV_D1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT1 Desktop");
    case PCI_CHIP_HASWELL_SDV_D2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT2 Desktop");
    case PCI_CHIP_HASWELL_SDV_D3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT3 Desktop");
    case PCI_CHIP_HASWELL_SDV_S1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT1 Server");
    case PCI_CHIP_HASWELL_SDV_S2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT2 Server");
    case PCI_CHIP_HASWELL_SDV_S3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT3 Server");
    case PCI_CHIP_HASWELL_SDV_M1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT1 Mobile");
    case PCI_CHIP_HASWELL_SDV_M2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT2 Mobile");
    case PCI_CHIP_HASWELL_SDV_M3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT3 Mobile");
    case PCI_CHIP_HASWELL_SDV_B1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT1 reserved");
    case PCI_CHIP_HASWELL_SDV_B2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT2 reserved");
    case PCI_CHIP_HASWELL_SDV_B3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT3 reserved");
    case PCI_CHIP_HASWELL_SDV_E1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT1 reserved");
    case PCI_CHIP_HASWELL_SDV_E2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT2 reserved");
    case PCI_CHIP_HASWELL_SDV_E3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell"
                       " Software Development Vehicle device GT3 reserved");
    case PCI_CHIP_HASWELL_ULT_D1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT1 Desktop");
    case PCI_CHIP_HASWELL_ULT_D2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT2 Desktop");
    case PCI_CHIP_HASWELL_ULT_D3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT3 Desktop");
    case PCI_CHIP_HASWELL_ULT_S1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT1 Server");
    case PCI_CHIP_HASWELL_ULT_S2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT2 Server");
    case PCI_CHIP_HASWELL_ULT_S3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT3 Server");
    case PCI_CHIP_HASWELL_ULT_M1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT1 Mobile");
    case PCI_CHIP_HASWELL_ULT_M2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT2 Mobile");
    case PCI_CHIP_HASWELL_ULT_M3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT3 Mobile");
    case PCI_CHIP_HASWELL_ULT_B1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT1 reserved");
    case PCI_CHIP_HASWELL_ULT_B2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT2 reserved");
    case PCI_CHIP_HASWELL_ULT_B3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT3 reserved");
    case PCI_CHIP_HASWELL_ULT_E1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT1 reserved");
    case PCI_CHIP_HASWELL_ULT_E2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT2 reserved");
    case PCI_CHIP_HASWELL_ULT_E3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell Ultrabook GT3 reserved");

    /* CRW */
    case PCI_CHIP_HASWELL_CRW_D1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell CRW GT1 Desktop");
    case PCI_CHIP_HASWELL_CRW_D2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell CRW GT2 Desktop");
    case PCI_CHIP_HASWELL_CRW_D3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell CRW GT3 Desktop");
    case PCI_CHIP_HASWELL_CRW_S1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell CRW GT1 Server");
    case PCI_CHIP_HASWELL_CRW_S2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell CRW GT2 Server");
    case PCI_CHIP_HASWELL_CRW_S3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell CRW GT3 Server");
    case PCI_CHIP_HASWELL_CRW_M1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell CRW GT1 Mobile");
    case PCI_CHIP_HASWELL_CRW_M2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell CRW GT2 Mobile");
    case PCI_CHIP_HASWELL_CRW_M3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell CRW GT3 Mobile");
    case PCI_CHIP_HASWELL_CRW_B1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell CRW GT1 reserved");
    case PCI_CHIP_HASWELL_CRW_B2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell CRW GT2 reserved");
    case PCI_CHIP_HASWELL_CRW_B3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell CRW GT3 reserved");
    case PCI_CHIP_HASWELL_CRW_E1:
      DECL_INFO_STRING(intel_hsw_gt1_device, name, "Intel(R) HD Graphics Haswell CRW GT1 reserved");
    case PCI_CHIP_HASWELL_CRW_E2:
      DECL_INFO_STRING(intel_hsw_gt2_device, name, "Intel(R) HD Graphics Haswell CRW GT2 reserved");
    case PCI_CHIP_HASWELL_CRW_E3:
      DECL_INFO_STRING(intel_hsw_gt3_device, name, "Intel(R) HD Graphics Haswell CRW GT3 reserved");

    case PCI_CHIP_IVYBRIDGE_GT1:
      DECL_INFO_STRING(intel_ivb_gt1_device, name, "Intel(R) HD Graphics IvyBridge GT1");
    case PCI_CHIP_IVYBRIDGE_M_GT1:
      DECL_INFO_STRING(intel_ivb_gt1_device, name, "Intel(R) HD Graphics IvyBridge M GT1");
    case PCI_CHIP_IVYBRIDGE_S_GT1:
      DECL_INFO_STRING(intel_ivb_gt1_device, name, "Intel(R) HD Graphics IvyBridge S GT1");

    case PCI_CHIP_IVYBRIDGE_GT2:
      DECL_INFO_STRING(intel_ivb_gt2_device, name, "Intel(R) HD Graphics IvyBridge GT2");
    case PCI_CHIP_IVYBRIDGE_M_GT2:
      DECL_INFO_STRING(intel_ivb_gt2_device, name, "Intel(R) HD Graphics IvyBridge M GT2");
    case PCI_CHIP_IVYBRIDGE_S_GT2:
      DECL_INFO_STRING(intel_ivb_gt2_device, name, "Intel(R) HD Graphics IvyBridge S GT2");

    case PCI_CHIP_BAYTRAIL_T:
      DECL_INFO_STRING(intel_baytrail_t_device, name, "Intel(R) HD Graphics Bay Trail-T");

    case PCI_CHIP_BROADWLL_M_GT1:
      DECL_INFO_STRING(intel_brw_gt1_device, name, "Intel(R) HD Graphics BroadWell Mobile GT1");
    case PCI_CHIP_BROADWLL_D_GT1:
      DECL_INFO_STRING(intel_brw_gt1_device, name, "Intel(R) HD Graphics BroadWell U-Processor GT1");
    case PCI_CHIP_BROADWLL_S_GT1:
      DECL_INFO_STRING(intel_brw_gt1_device, name, "Intel(R) HD Graphics BroadWell Server GT1");
    case PCI_CHIP_BROADWLL_W_GT1:
      DECL_INFO_STRING(intel_brw_gt1_device, name, "Intel(R) HD Graphics BroadWell Workstation GT1");
    case PCI_CHIP_BROADWLL_U_GT1:
      DECL_INFO_STRING(intel_brw_gt1_device, name, "Intel(R) HD Graphics BroadWell ULX GT1");

    case PCI_CHIP_BROADWLL_M_GT2:
      DECL_INFO_STRING(intel_brw_gt2_device, name, "Intel(R) HD Graphics 5600 BroadWell Mobile GT2");
    case PCI_CHIP_BROADWLL_D_GT2:
      DECL_INFO_STRING(intel_brw_gt2_device, name, "Intel(R) HD Graphics 5500 BroadWell U-Processor GT2");
    case PCI_CHIP_BROADWLL_S_GT2:
      DECL_INFO_STRING(intel_brw_gt2_device, name, "Intel(R) HD Graphics BroadWell Server GT2");
    case PCI_CHIP_BROADWLL_W_GT2:
      DECL_INFO_STRING(intel_brw_gt2_device, name, "Intel(R) HD Graphics BroadWell Workstation GT2");
    case PCI_CHIP_BROADWLL_U_GT2:
      DECL_INFO_STRING(intel_brw_gt2_device, name, "Intel(R) HD Graphics 5300 BroadWell ULX GT2");

    case PCI_CHIP_BROADWLL_M_GT3:
      DECL_INFO_STRING(intel_brw_gt3_device, name, "Intel(R) Iris Pro Graphics 6200 BroadWell Mobile GT3");
    case PCI_CHIP_BROADWLL_D_GT3:
      DECL_INFO_STRING(intel_brw_gt3_device, name, "Intel(R) HD Graphics 6000 BroadWell U-Processor GT3");
    case PCI_CHIP_BROADWLL_UI_GT3:
      DECL_INFO_STRING(intel_brw_gt3_device, name, "Intel(R) Iris Graphics 6100 BroadWell U-Processor GT3");
    case PCI_CHIP_BROADWLL_S_GT3:
      DECL_INFO_STRING(intel_brw_gt3_device, name, "Intel(R) Iris Pro Graphics P6300 BroadWell Server GT3");
    case PCI_CHIP_BROADWLL_W_GT3:
      DECL_INFO_STRING(intel_brw_gt3_device, name, "Intel(R) HD Graphics BroadWell Workstation GT3");
    case PCI_CHIP_BROADWLL_U_GT3:
      DECL_INFO_STRING(intel_brw_gt3_device, name, "Intel(R) HD Graphics BroadWell ULX GT3");

    case PCI_CHIP_CHV_0:
    case PCI_CHIP_CHV_1:
    case PCI_CHIP_CHV_2:
    case PCI_CHIP_CHV_3:
      DECL_INFO_STRING(intel_chv_device, name, "Intel(R) HD Graphics Cherryview");

    case PCI_CHIP_SKYLAKE_ULT_GT1:
      DECL_INFO_STRING(intel_skl_gt1_device, name, "Intel(R) HD Graphics Skylake ULT GT1");
    case PCI_CHIP_SKYLAKE_ULX_GT1:
      DECL_INFO_STRING(intel_skl_gt1_device, name, "Intel(R) HD Graphics Skylake ULX GT1");
    case PCI_CHIP_SKYLAKE_DT_GT1:
      DECL_INFO_STRING(intel_skl_gt1_device, name, "Intel(R) HD Graphics Skylake Desktop GT1");
    case PCI_CHIP_SKYLAKE_HALO_GT1:
      DECL_INFO_STRING(intel_skl_gt1_device, name, "Intel(R) HD Graphics Skylake Halo GT1");
    case PCI_CHIP_SKYLAKE_SRV_GT1:
      DECL_INFO_STRING(intel_skl_gt1_device, name, "Intel(R) HD Graphics Skylake Server GT1");

    case PCI_CHIP_SKYLAKE_ULT_GT2:
      DECL_INFO_STRING(intel_skl_gt2_device, name, "Intel(R) HD Graphics Skylake ULT GT2");
    case PCI_CHIP_SKYLAKE_ULT_GT2F:
      DECL_INFO_STRING(intel_skl_gt2_device, name, "Intel(R) HD Graphics Skylake ULT GT2F");
    case PCI_CHIP_SKYLAKE_ULX_GT2:
      DECL_INFO_STRING(intel_skl_gt2_device, name, "Intel(R) HD Graphics Skylake ULX GT2");
    case PCI_CHIP_SKYLAKE_DT_GT2:
      DECL_INFO_STRING(intel_skl_gt2_device, name, "Intel(R) HD Graphics Skylake Desktop GT2");
    case PCI_CHIP_SKYLAKE_HALO_GT2:
      DECL_INFO_STRING(intel_skl_gt2_device, name, "Intel(R) HD Graphics Skylake Halo GT2");
    case PCI_CHIP_SKYLAKE_SRV_GT2:
      DECL_INFO_STRING(intel_skl_gt2_device, name, "Intel(R) HD Graphics Skylake Server GT2");

    case PCI_CHIP_SKYLAKE_ULT_GT3:
      DECL_INFO_STRING(intel_skl_gt3_device, name, "Intel(R) HD Graphics Skylake ULT GT3");
    case PCI_CHIP_SKYLAKE_HALO_GT3:
      DECL_INFO_STRING(intel_skl_gt3_device, name, "Intel(R) HD Graphics Skylake Halo GT3");
    case PCI_CHIP_SKYLAKE_SRV_GT3:
      DECL_INFO_STRING(intel_skl_gt3_device, name, "Intel(R) HD Graphics Skylake Server GT3");

    case PCI_CHIP_SKYLAKE_HALO_GT4:
      DECL_INFO_STRING(intel_skl_gt4_device, name, "Intel(R) HD Graphics Skylake Halo GT4");
    case PCI_CHIP_SKYLAKE_SRV_GT4:
      DECL_INFO_STRING(intel_skl_gt4_device, name, "Intel(R) HD Graphics Skylake Server GT4");

    case PCI_CHIP_BROXTON_P:
      DECL_INFO_STRING(intel_bxt_device, name, "Intel(R) HD Graphics Broxton-P");

    case PCI_CHIP_SANDYBRIDGE_BRIDGE:
    case PCI_CHIP_SANDYBRIDGE_GT1:
    case PCI_CHIP_SANDYBRIDGE_GT2:
    case PCI_CHIP_SANDYBRIDGE_GT2_PLUS:
    case PCI_CHIP_SANDYBRIDGE_BRIDGE_M:
    case PCI_CHIP_SANDYBRIDGE_M_GT1:
    case PCI_CHIP_SANDYBRIDGE_M_GT2:
    case PCI_CHIP_SANDYBRIDGE_M_GT2_PLUS:
    case PCI_CHIP_SANDYBRIDGE_BRIDGE_S:
    case PCI_CHIP_SANDYBRIDGE_S_GT:
    // Intel(R) HD Graphics SandyBridge not supported yet
    default:
      gen_device = NULL;
      printf("device id %x not support now\n", device_id);
  }

  if (gen_device == NULL)
    return;


#ifdef HAS_USERPTR
  const size_t sz = 4096;
  void *host_ptr = memalign(4096, sz);
  if (host_ptr != NULL) {
    drm_intel_bo *bo;
    bo = drm_intel_bo_alloc_userptr(gpu->bufmgr, "CL memory object",
                                    host_ptr, I915_TILING_NONE, 0, sz, 0);
    /* Fallback to unsynchronized userptr allocation if kernel has no MMU notifier enabled. */
    if (bo == NULL)
      bo = drm_intel_bo_alloc_userptr(gpu->bufmgr, "CL memory object", host_ptr, I915_TILING_NONE,
                                      0, sz, flags | I915_USERPTR_UNSYNCHRONIZED);

    if (bo == NULL)
      gen_device->host_unified_memory = CL_FALSE;
    else
      drm_intel_bo_unreference(bo);

    free(host_ptr);
  } else
    gen_device->host_unified_memory = CL_FALSE;
#endif


#define CHV_CONFIG_WARNING \
  "Warning: can't get GPU's configurations, will use the minimal one. Please update your drm to 2.4.59+ and linux kernel to 4.0.0+.\n"
#ifdef HAS_EU_TOTAL
  unsigned int eu_total;

  /* Prefer driver-queried max compute units if supported */
  if (!drm_intel_get_eu_total(gpu->fd, &eu_total))
    gen_device->max_compute_unit = eu_total;
  else if (IS_CHERRYVIEW(device_id))
    printf(CHV_CONFIG_WARNING);
#else
  if (IS_CHERRYVIEW(device_id))
    printf(CHV_CONFIG_WARNING);
#endif

#ifdef HAS_SUBSLICE_TOTAL
  unsigned int subslice_total;

  /* Prefer driver-queried subslice count if supported */
  if (!drm_intel_get_subslice_total(gpu->fd, &subslice_total))
    gen_device->sub_slice_count = subslice_total;
  else if (IS_CHERRYVIEW(device->device_id))
    printf(CHV_CONFIG_WARNING);
#else
  if (IS_CHERRYVIEW(device_id))
    printf(CHV_CONFIG_WARNING);
#endif
#undef CHV_CONFIG_WARNING

  //We should get the device memory dynamically, but the
  //mapablce mem size usage is unknown. Just ignore it.
  size_t total_mem,map_mem;
  if(drm_intel_get_aperture_sizes(gpu->fd, &map_mem, &total_mem) == 0)
    gen_device->global_mem_size = (cl_ulong)total_mem;


#define toMB(size) (size)&(0xfffffffffffffff<<20)
  /* Get the global_mem_size and max_mem_alloc size from
   * driver, system ram and hardware*/
  struct sysinfo info;
  if (sysinfo(&info) == 0) {
    uint64_t totalgpumem = gen_device->global_mem_size;
    uint64_t maxallocmem = gen_device->max_mem_alloc_size;
    uint64_t totalram = info.totalram * info.mem_unit;
    /* In case to keep system stable we just use half
     * of the raw as global mem */
    gen_device->global_mem_size = toMB((totalram / 2 > totalgpumem) ?
                                       totalgpumem: totalram / 2);
    /* The hardware has some limit about the alloc size
     * and the excution of kernel need some global mem
     * so we now make sure single mem does not use much
     * than 3/4 global mem*/
    gen_device->max_mem_alloc_size = toMB((gen_device->global_mem_size * 3 / 4 > maxallocmem) ?
                                          maxallocmem: gen_device->global_mem_size * 3 / 4);
  }
}

static void processExtensionStr(cl_extensions extensions, char* ext_str, int str_max)
{
  int str_offset = 0;
  int id;

  memset(ext_str, 0, sizeof(char)*str_max);

  for(id = 0; id < cl_khr_extension_id_max; id++) {
    if (extensions->extensions[id].base.ext_enabled) {
      int copy_len;
      char *ext_name = extensions->extensions[id].base.ext_name;
      if (str_offset + 1 >= str_max)
        return;

      if (str_offset != 0)
        ext_str[str_offset - 1] = ' ';
      copy_len = ((int)strlen(ext_name) + 1 + str_offset) < str_max
                 ? (strlen(ext_name) + 1) : (str_max - str_offset - 1);
      strncpy(&ext_str[str_offset],
              extensions->extensions[id].base.ext_name, copy_len);
      str_offset += copy_len;
    }
  }
}

static bool checkDeviceExtension(cl_platform_id platform)
{
  GBE_ASSERT(gen_device);
  gen_device_extensions = *platform->all_extensions;
  cl_extensions extensions = &gen_device_extensions;

  int id;
  for (id = BASE_EXT_START_ID; id <= BASE_EXT_END_ID; id++) {
    if (id != EXT_ID(khr_fp64)) {
      /* We support all extension of base ext. */
      extensions->extensions[id].base.ext_enabled = 1;
    } else {
      if (IS_GEN8(getGPUDevice(gen_device)->device_id) ||
          IS_GEN9(getGPUDevice(gen_device)->device_id)) {
        extensions->extensions[id].base.ext_enabled = 1;
      } else {
        /* Before GEN8, we do not support FP64. */
        if (extensions->extensions[id].base.ext_enabled == 1) {
          printf("In GEN driver, the extension %s must not be enabled\n",
                 extensions->extensions[id].base.ext_name);
          return false;
        }
      }
    }
  }

  for(id = OPT1_EXT_START_ID; id <= OPT1_EXT_END_ID; id++) {
    if (id == EXT_ID(khr_icd)) {
      extensions->extensions[id].base.ext_enabled = 1;
      continue;
    }

#if  LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5
    if (id == EXT_ID(khr_spir)) {
      extensions->extensions[id].base.ext_enabled = 1;
      continue;
    }
#endif

    if (id == EXT_ID(khr_image2d_from_buffer)) {
      extensions->extensions[id].base.ext_enabled = 1;
      continue;
    }

    if (id == EXT_ID(khr_fp16)) {
      if (IS_GEN8(getGPUDevice(gen_device)->device_id) ||
          IS_GEN9(getGPUDevice(gen_device)->device_id)) {
        extensions->extensions[id].base.ext_enabled = 1;
      } else {
        /* Before GEN8, we do not support FP16. */
        if (extensions->extensions[id].base.ext_enabled == 1) {
          printf("In GEN driver, the extension %s must not be enabled\n",
                 extensions->extensions[id].base.ext_name);
          return false;
        }
      }

      continue;
    }
  }

  processExtensionStr(extensions, gen_ext_string, sizeof(gen_ext_string));
  gen_device->all_extensions = &gen_device_extensions;
  gen_device->extensions = gen_ext_string;
  gen_device->extensions_sz = strlen(gen_ext_string);
  return true;
}

extern "C"
cl_int GenGetDeviceIDs(cl_platform_id platform, cl_device_type device_type,
                       cl_uint num_entries, cl_device_id *devices, cl_uint *num_devices)
{
  /* We are GPU device. */
  const cl_device_type valid =  CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_DEFAULT;
  if ((device_type & valid) == 0) {
    return CL_DEVICE_NOT_FOUND;
  }

  if (gen_device->platform != platform) {
    return CL_INVALID_PLATFORM;
  }

  if (devices) {
    GBE_ASSERT(num_entries >= 1);
    devices[0] = gen_device;
  }

  if (num_devices)
    *num_devices = 1;
  return CL_SUCCESS;
}

extern "C"
cl_int GenDriverInit(cl_platform_id platform)
{
  cl_int err = CL_SUCCESS;

  if (gen_device) {// already find one.
    return CL_SUCCESS;
  }

  GenGPUDevice* gpuDev = GBE_NEW(GenGPUDevice);
  if (gpuDev->gen_ver < 7) {
    gen_device = NULL;
    GBE_FREE(gpuDev);
    err = CL_DEVICE_NOT_FOUND;
    return err;
  }

  initGenDevice(gpuDev);
  if (!gen_device) {
    gen_device = NULL;
    GBE_FREE(gpuDev);
    err = CL_DEVICE_NOT_FOUND;
    return err;
  }

  gen_device->platform = platform;
  gen_device->driver = &clgenDriver;
  gen_device->magic = CL_MAGIC_DEVICE_HEADER;
  gen_device->root = 1;
  setGenDevicePrivate(gen_device, gpuDev);
  /* Check and set the extension for device. */
  if (!checkDeviceExtension(platform)) {
    GBE_FREE(gpuDev);
    setGenDevicePrivate(gen_device, NULL);
    gen_device = NULL;
    err = CL_DEVICE_NOT_FOUND;
    return err;
  }

  return CL_SUCCESS;
}