blob: d759340d18f6d7224194ee0c752b5b715e9a33a0 [file] [log] [blame]
Chris Wilson9de433a2011-06-20 13:53:12 +01001/*
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 * Chris Wilson <chris@chris-wilson.co.uk>
25 *
26 */
27
28#include <unistd.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <assert.h>
33#include <fcntl.h>
34#include <inttypes.h>
35#include <errno.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
38#include <sys/mman.h>
39#include "drm.h"
40#include "i915_drm.h"
41#include "drmtest.h"
42
43#define OBJECT_SIZE (16*1024*1024)
44
Daniel Vetter733a1d92011-09-15 18:47:21 +020045static void set_domain(int fd, uint32_t handle)
46{
Daniel Vetter673e6b22012-01-10 16:05:34 +010047 gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
Daniel Vetter733a1d92011-09-15 18:47:21 +020048}
49
Chris Wilson9de433a2011-06-20 13:53:12 +010050static void *
Daniel Vetter733a1d92011-09-15 18:47:21 +020051mmap_bo(int fd, uint32_t handle)
Chris Wilson9de433a2011-06-20 13:53:12 +010052{
Chris Wilson9de433a2011-06-20 13:53:12 +010053 void *ptr;
Chris Wilson9de433a2011-06-20 13:53:12 +010054
Daniel Vetter527cad12012-01-10 18:41:46 +010055 ptr = gem_mmap(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE);
Chris Wilson9de433a2011-06-20 13:53:12 +010056 assert(ptr != MAP_FAILED);
57
Daniel Vetter733a1d92011-09-15 18:47:21 +020058 return ptr;
59}
60
61static void *
62create_pointer(int fd)
63{
64 uint32_t handle;
65 void *ptr;
66
67 handle = gem_create(fd, OBJECT_SIZE);
68
69 ptr = mmap_bo(fd, handle);
70
Chris Wilson9de433a2011-06-20 13:53:12 +010071 gem_close(fd, handle);
72
73 return ptr;
74}
75
76static void
77test_copy(int fd)
78{
79 void *src, *dst;
80
81 /* copy from a fresh src to fresh dst to force pagefault on both */
82 src = create_pointer(fd);
83 dst = create_pointer(fd);
84
85 memcpy(dst, src, OBJECT_SIZE);
86 memcpy(src, dst, OBJECT_SIZE);
87
88 munmap(dst, OBJECT_SIZE);
89 munmap(src, OBJECT_SIZE);
90}
91
92static void
93test_write(int fd)
94{
95 void *src;
96 uint32_t dst;
97
98 /* copy from a fresh src to fresh dst to force pagefault on both */
99 src = create_pointer(fd);
100 dst = gem_create(fd, OBJECT_SIZE);
101
102 gem_write(fd, dst, 0, src, OBJECT_SIZE);
103
104 gem_close(fd, dst);
105 munmap(src, OBJECT_SIZE);
106}
107
108static void
Daniel Vetter733a1d92011-09-15 18:47:21 +0200109test_write_gtt(int fd)
110{
111 uint32_t dst;
112 char *dst_gtt;
113 void *src;
114
115 dst = gem_create(fd, OBJECT_SIZE);
116
117 /* prefault object into gtt */
118 dst_gtt = mmap_bo(fd, dst);
119 set_domain(fd, dst);
120 memset(dst_gtt, 0, OBJECT_SIZE);
121 munmap(dst_gtt, OBJECT_SIZE);
122
123 src = create_pointer(fd);
124
125 gem_write(fd, dst, 0, src, OBJECT_SIZE);
126
127 gem_close(fd, dst);
128 munmap(src, OBJECT_SIZE);
129}
130
131static void
Chris Wilson9de433a2011-06-20 13:53:12 +0100132test_read(int fd)
133{
134 void *dst;
135 uint32_t src;
136
137 /* copy from a fresh src to fresh dst to force pagefault on both */
138 dst = create_pointer(fd);
139 src = gem_create(fd, OBJECT_SIZE);
140
141 gem_read(fd, src, 0, dst, OBJECT_SIZE);
142
143 gem_close(fd, src);
144 munmap(dst, OBJECT_SIZE);
145}
146
147int main(int argc, char **argv)
148{
149 int fd;
150
Daniel Vetteraf228d42012-11-28 12:40:23 +0100151 drmtest_subtest_init(argc, argv);
152
Chris Wilson9de433a2011-06-20 13:53:12 +0100153 fd = drm_open_any();
154
Daniel Vetteraf228d42012-11-28 12:40:23 +0100155 if (drmtest_run_subtest("copy"))
156 test_copy(fd);
157 if (drmtest_run_subtest("read"))
158 test_read(fd);
159 if (drmtest_run_subtest("write"))
160 test_write(fd);
161 if (drmtest_run_subtest("write-gtt"))
162 test_write_gtt(fd);
Chris Wilson9de433a2011-06-20 13:53:12 +0100163
164 close(fd);
165
166 return 0;
167}