lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / gem_tiled_pread_pwrite.c
1 /*
2  * Copyright © 2009 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  *    Eric Anholt <eric@anholt.net>
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: Test swizzling by testing pwrite does the invers of pread
40  *
41  * Together with the explicit pread testcase, this should cover our swizzle
42  * handling.
43  *
44  * Note that this test will use swap in an effort to test all of ram.
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
57 #include <drm.h>
58
59 #include "ioctl_wrappers.h"
60 #include "drmtest.h"
61 #include "intel_io.h"
62 #include "igt_aux.h"
63
64 #define WIDTH 512
65 #define HEIGHT 512
66 static uint32_t linear[WIDTH * HEIGHT];
67 static uint32_t current_tiling_mode;
68
69 #define PAGE_SIZE 4096
70
71 static uint32_t
72 create_bo_and_fill(int fd)
73 {
74         uint32_t handle;
75         uint32_t *data;
76         int i;
77
78         handle = gem_create(fd, sizeof(linear));
79         gem_set_tiling(fd, handle, current_tiling_mode, WIDTH * sizeof(uint32_t));
80
81         /* Fill the BO with dwords starting at start_val */
82         data = gem_mmap(fd, handle, sizeof(linear), PROT_READ | PROT_WRITE);
83         for (i = 0; i < WIDTH*HEIGHT; i++)
84                 data[i] = i;
85         munmap(data, sizeof(linear));
86
87         return handle;
88 }
89
90 static uint32_t
91 create_bo(int fd)
92 {
93         uint32_t handle;
94
95         handle = gem_create(fd, sizeof(linear));
96         gem_set_tiling(fd, handle, current_tiling_mode, WIDTH * sizeof(uint32_t));
97
98         return handle;
99 }
100
101 igt_simple_main
102 {
103         int fd;
104         uint32_t *data;
105         int i, j;
106         uint32_t tiling, swizzle;
107         uint32_t handle, handle_target;
108         int count;
109         
110         fd = drm_open_any();
111         count = SLOW_QUICK(intel_get_total_ram_mb() * 9 / 10, 8) ;
112
113         for (i = 0; i < count/2; i++) {
114                 current_tiling_mode = I915_TILING_X;
115
116                 handle = create_bo_and_fill(fd);
117                 gem_get_tiling(fd, handle, &tiling, &swizzle);
118
119                 gem_read(fd, handle, 0, linear, sizeof(linear));
120
121                 handle_target = create_bo(fd);
122                 gem_write(fd, handle_target, 0, linear, sizeof(linear));
123
124                 /* Check the target bo's contents. */
125                 data = gem_mmap(fd, handle_target, sizeof(linear), PROT_READ | PROT_WRITE);
126                 for (j = 0; j < WIDTH*HEIGHT; j++)
127                         igt_assert_f(data[j] == j,
128                                      "mismatch at %i: %i\n",
129                                      j, data[j]);
130                 munmap(data, sizeof(linear));
131
132                 /* Leak both bos so that we use all of system mem! */
133                 gem_madvise(fd, handle_target, I915_MADV_DONTNEED);
134                 gem_madvise(fd, handle, I915_MADV_DONTNEED);
135
136                 igt_progress("gem_tiled_pread_pwrite: ", i, count/2);
137         }
138
139         close(fd);
140 }