tests: use igt_assert/igt_require more
[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 "drm.h"
57 #include "i915_drm.h"
58 #include "drmtest.h"
59 #include "intel_gpu_tools.h"
60
61 #define WIDTH 512
62 #define HEIGHT 512
63 #define LINEAR_DWORDS (4 * WIDTH * HEIGHT)
64 static uint32_t current_tiling_mode;
65
66 #define PAGE_SIZE 4096
67
68 static uint32_t
69 create_bo_and_fill(int fd)
70 {
71         uint32_t handle;
72         uint32_t *data;
73         int i;
74
75         handle = gem_create(fd, LINEAR_DWORDS);
76         gem_set_tiling(fd, handle, current_tiling_mode, WIDTH * sizeof(uint32_t));
77
78         /* Fill the BO with dwords starting at start_val */
79         data = gem_mmap(fd, handle, LINEAR_DWORDS, PROT_READ | PROT_WRITE);
80         if (data == NULL && errno == ENOSPC)
81                 return 0;
82
83         for (i = 0; i < WIDTH*HEIGHT; i++)
84                 data[i] = i;
85         munmap(data, LINEAR_DWORDS);
86
87         return handle;
88 }
89
90 uint32_t *bo_handles;
91 int *idx_arr;
92
93 int
94 main(int argc, char **argv)
95 {
96         int fd;
97         uint32_t *data;
98         int i, j;
99         int count;
100         current_tiling_mode = I915_TILING_X;
101
102         igt_skip_on_simulation();
103
104         fd = drm_open_any();
105         /* need slightly more than total ram */
106         count = intel_get_total_ram_mb() * 11 / 10;
107         bo_handles = calloc(count, sizeof(uint32_t));
108         igt_assert(bo_handles);
109
110         idx_arr = calloc(count, sizeof(int));
111         igt_assert(idx_arr);
112
113         igt_require(intel_get_total_ram_mb() / 4 < intel_get_total_swap_mb());
114
115         for (i = 0; i < count; i++) {
116                 bo_handles[i] = create_bo_and_fill(fd);
117                 /* Not enough mmap address space possible. */
118                 igt_require(bo_handles[i]);
119         }
120
121         for (i = 0; i < count; i++)
122                 idx_arr[i] = i;
123
124         igt_permute_array(idx_arr, count,
125                               igt_exchange_int);
126
127         for (i = 0; i < count/2; i++) {
128                 /* Check the target bo's contents. */
129                 data = gem_mmap(fd, bo_handles[idx_arr[i]],
130                                 LINEAR_DWORDS, PROT_READ | PROT_WRITE);
131                 for (j = 0; j < WIDTH*HEIGHT; j++)
132                         igt_assert_f(data[j] == j,
133                                      "mismatch at %i: %i\n",
134                                      j, data[j]);
135                 munmap(data, LINEAR_DWORDS);
136         }
137
138         close(fd);
139
140         return 0;
141 }