tests: remove unused getopt header includes
[platform/upstream/intel-gpu-tools.git] / tests / gem_tiled_swapping.c
1 /*
2  * Copyright © 2011 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>
25  *
26  */
27
28 /** @file gem_tiled_pread_pwrite.c
29  *
30  * This is a test of pread's behavior on tiled objects with respect to the
31  * reported swizzling value.
32  *
33  * The goal is to exercise the slow_bit17_copy path for reading on bit17
34  * machines, but will also be useful for catching swizzling value bugs on
35  * other systems.
36  */
37
38 /*
39  * Testcase: Exercise swizzle code for swapping
40  *
41  * The swizzle checks in the swapin path are at a different place than the ones
42  * for pread/pwrite, so we need to check them separately.
43  *
44  * This test obviously needs swap present (and exits if none is detected).
45  */
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <fcntl.h>
51 #include <inttypes.h>
52 #include <errno.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 #include <sys/ioctl.h>
56 #include <pthread.h>
57
58 #include <drm.h>
59
60 #include "ioctl_wrappers.h"
61 #include "drmtest.h"
62 #include "intel_io.h"
63 #include "igt_aux.h"
64
65 #define WIDTH 512
66 #define HEIGHT 512
67 #define LINEAR_DWORDS (4 * WIDTH * HEIGHT)
68 static uint32_t current_tiling_mode;
69
70 #define PAGE_SIZE 4096
71
72 static uint32_t
73 create_bo_and_fill(int fd)
74 {
75         uint32_t handle;
76         uint32_t *data;
77         int i;
78
79         handle = gem_create(fd, LINEAR_DWORDS);
80         gem_set_tiling(fd, handle, current_tiling_mode, WIDTH * sizeof(uint32_t));
81
82         /* Fill the BO with dwords starting at start_val */
83         data = gem_mmap(fd, handle, LINEAR_DWORDS, PROT_READ | PROT_WRITE);
84         if (data == NULL && errno == ENOSPC)
85                 return 0;
86
87         for (i = 0; i < WIDTH*HEIGHT; i++)
88                 data[i] = i;
89         munmap(data, LINEAR_DWORDS);
90
91         return handle;
92 }
93
94 uint32_t *bo_handles;
95
96 struct thread {
97         pthread_t thread;
98         int *idx_arr;
99         int fd, count;
100 };
101
102 static void
103 check_bo(int fd, uint32_t handle)
104 {
105         uint32_t *data;
106         int j;
107
108         /* Check the target bo's contents. */
109         data = gem_mmap(fd, handle, LINEAR_DWORDS, PROT_READ | PROT_WRITE);
110         j = rand() % (WIDTH * HEIGHT);
111         igt_assert_f(data[j] == j, "mismatch at %i: %i\n", j, data[j]);
112         munmap(data, LINEAR_DWORDS);
113 }
114
115 static void *thread_run(void *data)
116 {
117         struct thread *t = data;
118         int i;
119
120         for (i = 0; i < t->count; i++)
121                 check_bo(t->fd, bo_handles[t->idx_arr[i]]);
122
123         return NULL;
124 }
125
126 static void thread_init(struct thread *t, int fd, int count)
127 {
128         int i;
129
130         t->fd = fd;
131         t->count = count;
132         t->idx_arr = calloc(count, sizeof(int));
133         igt_assert(t->idx_arr);
134
135         for (i = 0; i < count; i++)
136                 t->idx_arr[i] = i;
137
138         igt_permute_array(t->idx_arr, count, igt_exchange_int);
139 }
140
141 static void thread_fini(struct thread *t)
142 {
143         free(t->idx_arr);
144 }
145
146 igt_simple_main
147 {
148         struct thread *threads;
149         int fd, n, count, num_threads;
150
151         current_tiling_mode = I915_TILING_X;
152
153         igt_skip_on_simulation();
154         intel_purge_vm_caches();
155
156         fd = drm_open_any();
157         /* need slightly more than available memory */
158         count = intel_get_total_ram_mb() + intel_get_total_swap_mb() / 4;
159         bo_handles = calloc(count, sizeof(uint32_t));
160         igt_assert(bo_handles);
161
162         num_threads = gem_available_fences(fd);
163         threads = calloc(num_threads, sizeof(struct thread));
164         igt_assert(threads);
165
166         igt_log(IGT_LOG_INFO,
167                 "Using %d 1MiB objects (available RAM: %ld/%ld, swap: %ld)\n",
168                 count,
169                 (long)intel_get_avail_ram_mb(),
170                 (long)intel_get_total_ram_mb(),
171                 (long)intel_get_total_swap_mb());
172         igt_require(intel_check_memory(count, 1024*1024, CHECK_RAM | CHECK_SWAP));
173
174         for (n = 0; n < count; n++) {
175                 bo_handles[n] = create_bo_and_fill(fd);
176                 /* Not enough mmap address space possible. */
177                 igt_require(bo_handles[n]);
178         }
179
180         thread_init(&threads[0], fd, count);
181         thread_run(&threads[0]);
182         thread_fini(&threads[0]);
183
184         /* Once more with threads */
185         igt_subtest("threaded") {
186                 for (n = 0; n < num_threads; n++) {
187                         thread_init(&threads[n], fd, count);
188                         pthread_create(&threads[n].thread, NULL, thread_run, &threads[n]);
189                 }
190                 for (n = 0; n < num_threads; n++) {
191                         pthread_join(threads[n].thread, NULL);
192                         thread_fini(&threads[n]);
193                 }
194         }
195
196         close(fd);
197 }