Merge commit 'origin/drm-gem' into modesetting-gem
[profile/ivi/libdrm.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 <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include "drm.h"
37
38 #define OBJECT_SIZE 16384
39
40 int do_read(int fd, int handle, void *buf, int offset, int size)
41 {
42         struct drm_gem_pread read;
43
44         /* Ensure that we don't have any convenient data in buf in case
45          * we fail.
46          */
47         memset(buf, 0xd0, size);
48
49         memset(&read, 0, sizeof(read));
50         read.handle = handle;
51         read.data_ptr = (uintptr_t)buf;
52         read.size = size;
53         read.offset = offset;
54
55         return ioctl(fd, DRM_IOCTL_GEM_PREAD, &read);
56 }
57
58 int do_write(int fd, int handle, void *buf, int offset, int size)
59 {
60         struct drm_gem_pwrite write;
61
62         memset(&write, 0, sizeof(write));
63         write.handle = handle;
64         write.data_ptr = (uintptr_t)buf;
65         write.size = size;
66         write.offset = offset;
67
68         return ioctl(fd, DRM_IOCTL_GEM_PWRITE, &write);
69 }
70
71 int main(int argc, char **argv)
72 {
73         int fd;
74         struct drm_gem_create create;
75         struct drm_gem_mmap mmap;
76         struct drm_gem_close unref;
77         uint8_t expected[OBJECT_SIZE];
78         uint8_t buf[OBJECT_SIZE];
79         uint8_t *addr;
80         int ret;
81         int handle;
82
83         fd = drm_open_any();
84
85         memset(&mmap, 0, sizeof(mmap));
86         mmap.handle = 0x10101010;
87         mmap.offset = 0;
88         mmap.size = 4096;
89         printf("Testing mmaping of bad object.\n");
90         ret = ioctl(fd, DRM_IOCTL_GEM_MMAP, &mmap);
91         assert(ret == -1 && errno == EINVAL);
92
93         memset(&create, 0, sizeof(create));
94         create.size = OBJECT_SIZE;
95         ret = ioctl(fd, DRM_IOCTL_GEM_CREATE, &create);
96         assert(ret == 0);
97         handle = create.handle;
98
99         printf("Testing mmaping of newly created object.\n");
100         mmap.handle = handle;
101         mmap.offset = 0;
102         mmap.size = OBJECT_SIZE;
103         ret = ioctl(fd, DRM_IOCTL_GEM_MMAP, &mmap);
104         assert(ret == 0);
105         addr = (uint8_t *)(uintptr_t)mmap.addr_ptr;
106
107         printf("Testing contents of newly created object.\n");
108         memset(expected, 0, sizeof(expected));
109         assert(memcmp(addr, expected, sizeof(expected)) == 0);
110
111         printf("Testing coherency of writes and mmap reads.\n");
112         memset(buf, 0, sizeof(buf));
113         memset(buf + 1024, 0x01, 1024);
114         memset(expected + 1024, 0x01, 1024);
115         ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
116         assert(ret == 0);
117         assert(memcmp(buf, addr, sizeof(buf)) == 0);
118
119         printf("Testing that mapping stays after close\n");
120         unref.handle = handle;
121         ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &unref);
122         assert(ret == 0);
123         assert(memcmp(buf, addr, sizeof(buf)) == 0);
124
125         printf("Testing unmapping\n");
126         munmap(addr, OBJECT_SIZE);
127
128         close(fd);
129
130         return 0;
131 }