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