lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / gem_mmap.c
1 /*
2  * Copyright © 2008 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 #include <unistd.h>
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/ioctl.h>
37 #include "drm.h"
38 #include "ioctl_wrappers.h"
39 #include "drmtest.h"
40
41 #define OBJECT_SIZE 16384
42 int fd;
43 int handle;
44
45 igt_main
46 {
47         struct drm_i915_gem_mmap arg;
48         uint8_t expected[OBJECT_SIZE];
49         uint8_t buf[OBJECT_SIZE];
50         uint8_t *addr;
51         int ret;
52
53         igt_fixture
54                 fd = drm_open_any();
55
56         igt_subtest("bad-object") {
57                 memset(&arg, 0, sizeof(arg));
58                 arg.handle = 0x10101010;
59                 arg.offset = 0;
60                 arg.size = 4096;
61                 ret = ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &arg);
62                 igt_assert(ret == -1 && errno == ENOENT);
63         }
64
65         igt_fixture
66                 handle = gem_create(fd, OBJECT_SIZE);
67
68         igt_subtest("new-object") {
69                 arg.handle = handle;
70                 arg.offset = 0;
71                 arg.size = OBJECT_SIZE;
72                 ret = ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &arg);
73                 igt_assert(ret == 0);
74                 addr = (uint8_t *)(uintptr_t)arg.addr_ptr;
75
76                 igt_info("Testing contents of newly created object.\n");
77                 memset(expected, 0, sizeof(expected));
78                 igt_assert(memcmp(addr, expected, sizeof(expected)) == 0);
79
80                 igt_info("Testing coherency of writes and mmap reads.\n");
81                 memset(buf, 0, sizeof(buf));
82                 memset(buf + 1024, 0x01, 1024);
83                 memset(expected + 1024, 0x01, 1024);
84                 gem_write(fd, handle, 0, buf, OBJECT_SIZE);
85                 igt_assert(memcmp(buf, addr, sizeof(buf)) == 0);
86
87                 igt_info("Testing that mapping stays after close\n");
88                 gem_close(fd, handle);
89                 igt_assert(memcmp(buf, addr, sizeof(buf)) == 0);
90
91                 igt_info("Testing unmapping\n");
92                 munmap(addr, OBJECT_SIZE);
93         }
94
95         igt_subtest("short-mmap") {
96                 igt_assert(OBJECT_SIZE > 4096);
97                 addr = gem_mmap__cpu(fd, handle, 4096, PROT_WRITE);
98                 memset(addr, 0, 4096);
99                 munmap(addr, 4096);
100         }
101
102         igt_fixture
103                 close(fd);
104 }