tests: remove unused getopt header includes
[platform/upstream/intel-gpu-tools.git] / tests / gem_multi_bsd_sync_loop.c
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Daniel Vetter <daniel.vetter@ffwll.ch> (based on gem_ring_sync_loop_*.c)
25  *    Zhao Yakui <yakui.zhao@intel.com>
26  *
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include "drm.h"
38 #include "ioctl_wrappers.h"
39 #include "drmtest.h"
40 #include "intel_bufmgr.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_io.h"
43 #include "i830_reg.h"
44 #include "intel_chipset.h"
45
46 static drm_intel_bufmgr *bufmgr;
47 struct intel_batchbuffer *batch;
48 static drm_intel_bo *target_buffer;
49
50 #define NUM_FD  50
51
52 static int mfd[NUM_FD];
53 static drm_intel_bufmgr *mbufmgr[NUM_FD];
54 static struct intel_batchbuffer *mbatch[NUM_FD];
55 static drm_intel_bo *mbuffer[NUM_FD];
56
57
58 /*
59  * Testcase: Basic check of ring<->ring sync using a dummy reloc
60  *
61  * Extremely efficient at catching missed irqs with semaphores=0 ...
62  */
63
64 #define MI_COND_BATCH_BUFFER_END        (0x36<<23 | 1)
65 #define MI_DO_COMPARE                   (1<<21)
66
67 static void
68 store_dword_loop(int fd)
69 {
70         int i;
71         int num_rings = gem_get_num_rings(fd);
72
73         srandom(0xdeadbeef);
74
75         for (i = 0; i < SLOW_QUICK(0x100000, 10); i++) {
76                 int ring, mindex;
77                 ring = random() % num_rings + 1;
78                 mindex = random() % NUM_FD;
79                 batch = mbatch[mindex];
80                 if (ring == I915_EXEC_RENDER) {
81                         BEGIN_BATCH(4);
82                         OUT_BATCH(MI_COND_BATCH_BUFFER_END | MI_DO_COMPARE);
83                         OUT_BATCH(0xffffffff); /* compare dword */
84                         OUT_RELOC(mbuffer[mindex], I915_GEM_DOMAIN_RENDER,
85                                         I915_GEM_DOMAIN_RENDER, 0);
86                         OUT_BATCH(MI_NOOP);
87                         ADVANCE_BATCH();
88                 } else {
89                         BEGIN_BATCH(4);
90                         OUT_BATCH(MI_FLUSH_DW | 1);
91                         OUT_BATCH(0); /* reserved */
92                         OUT_RELOC(mbuffer[mindex], I915_GEM_DOMAIN_RENDER,
93                                         I915_GEM_DOMAIN_RENDER, 0);
94                         OUT_BATCH(MI_NOOP | (1<<22) | (0xf));
95                         ADVANCE_BATCH();
96                 }
97                 intel_batchbuffer_flush_on_ring(batch, ring);
98         }
99
100         drm_intel_bo_map(target_buffer, 0);
101         // map to force waiting on rendering
102         drm_intel_bo_unmap(target_buffer);
103 }
104
105 igt_simple_main
106 {
107         int fd;
108         int devid;
109         int i;
110
111         fd = drm_open_any();
112         devid = intel_get_drm_devid(fd);
113         gem_require_ring(fd, I915_EXEC_BLT);
114
115
116         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
117         igt_assert_f(bufmgr, "fail to initialize the buf manager\n");
118         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
119
120
121         target_buffer = drm_intel_bo_alloc(bufmgr, "target bo", 4096, 4096);
122         igt_assert_f(target_buffer, "fail to create the gem bo\n");
123
124         /* Create multiple drm_fd and map one gem_object among multi drm_fd */
125         {
126                 unsigned int target_flink;
127                 char buffer_name[32];
128                 igt_assert(dri_bo_flink(target_buffer, &target_flink) == 0);
129
130                 for (i = 0; i < NUM_FD; i++) {
131                         sprintf(buffer_name, "Target buffer %d\n", i);
132                         mfd[i] = drm_open_any();
133                         mbufmgr[i] = drm_intel_bufmgr_gem_init(mfd[i], 4096);
134                         igt_assert_f(mbufmgr[i],
135                                      "fail to initialize buf manager for drm_fd %d\n",
136                                      mfd[i]);
137                         drm_intel_bufmgr_gem_enable_reuse(mbufmgr[i]);
138                         mbatch[i] = intel_batchbuffer_alloc(mbufmgr[i], devid);
139                         igt_assert_f(mbatch[i],
140                                      "fail to create batchbuffer for drm_fd %d\n",
141                                      mfd[i]);
142                         mbuffer[i] = intel_bo_gem_create_from_name(mbufmgr[i], buffer_name, target_flink);
143                         igt_assert_f(mbuffer[i],
144                                      "fail to create buffer bo from global "
145                                      "gem handle %d for drm_fd %d\n",
146                                      target_flink, mfd[i]);
147                 }
148         }
149
150         store_dword_loop(fd);
151
152         {
153                 for (i = 0; i < NUM_FD; i++) {
154                         dri_bo_unreference(mbuffer[i]);
155                         intel_batchbuffer_free(mbatch[i]);
156                         drm_intel_bufmgr_destroy(mbufmgr[i]);
157                         close(mfd[i]);
158                 }
159         }
160         drm_intel_bo_unreference(target_buffer);
161         drm_intel_bufmgr_destroy(bufmgr);
162
163         close(fd);
164 }