blob: a778231a85ffe1e057c893f712d0187131e86a8e [file] [log] [blame]
Eric Anholt8c741ed2008-04-23 13:06:58 -07001/*
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>
Eric Anholt3148c162008-04-29 13:45:43 -070036#include "drm.h"
Eric Anholt8c741ed2008-04-23 13:06:58 -070037
Eric Anholt3148c162008-04-29 13:45:43 -070038#define OBJECT_SIZE 16384
Eric Anholt8c741ed2008-04-23 13:06:58 -070039
40int do_read(int fd, int handle, void *buf, int offset, int size)
41{
Eric Anholt3148c162008-04-29 13:45:43 -070042 struct drm_mm_pread_args read;
Eric Anholt8c741ed2008-04-23 13:06:58 -070043
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 = buf;
52 read.size = size;
53 read.offset = offset;
54
Eric Anholt3148c162008-04-29 13:45:43 -070055 return ioctl(fd, DRM_IOCTL_MM_PREAD, &read);
Eric Anholt8c741ed2008-04-23 13:06:58 -070056}
57
58int do_write(int fd, int handle, void *buf, int offset, int size)
59{
Eric Anholt3148c162008-04-29 13:45:43 -070060 struct drm_mm_pwrite_args write;
Eric Anholt8c741ed2008-04-23 13:06:58 -070061
62 memset(&write, 0, sizeof(write));
63 write.handle = handle;
64 write.data = buf;
65 write.size = size;
66 write.offset = offset;
67
Eric Anholt3148c162008-04-29 13:45:43 -070068 return ioctl(fd, DRM_IOCTL_MM_PWRITE, &write);
Eric Anholt8c741ed2008-04-23 13:06:58 -070069}
70
71int main(int argc, char **argv)
72{
73 int fd;
Eric Anholt3148c162008-04-29 13:45:43 -070074 struct drm_mm_alloc_args alloc;
75 uint8_t expected[OBJECT_SIZE];
76 uint8_t buf[OBJECT_SIZE];
Eric Anholt8c741ed2008-04-23 13:06:58 -070077 int ret;
78 int handle;
79
Eric Anholt3148c162008-04-29 13:45:43 -070080 fd = drm_open_any();
Eric Anholt8c741ed2008-04-23 13:06:58 -070081
82 memset(&alloc, 0, sizeof(alloc));
Eric Anholt3148c162008-04-29 13:45:43 -070083 alloc.size = OBJECT_SIZE;
84 ret = ioctl(fd, DRM_IOCTL_MM_ALLOC, &alloc);
Eric Anholt8c741ed2008-04-23 13:06:58 -070085 assert(ret == 0);
86 handle = alloc.handle;
87
88 printf("Testing contents of newly allocated object.\n");
Eric Anholt3148c162008-04-29 13:45:43 -070089 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -070090 assert(ret == 0);
91 memset(&expected, 0, sizeof(expected));
92 assert(memcmp(expected, buf, sizeof(expected)) == 0);
93
94 printf("Testing read beyond end of buffer.\n");
Eric Anholt3148c162008-04-29 13:45:43 -070095 ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -070096 assert(ret == -1 && errno == EINVAL);
97
98 printf("Testing full write of buffer\n");
99 memset(buf, 0, sizeof(buf));
100 memset(buf + 1024, 0x01, 1024);
101 memset(expected + 1024, 0x01, 1024);
Eric Anholt3148c162008-04-29 13:45:43 -0700102 ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -0700103 assert(ret == 0);
Eric Anholt3148c162008-04-29 13:45:43 -0700104 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -0700105 assert(ret == 0);
106 assert(memcmp(buf, expected, sizeof(buf)) == 0);
107
108 printf("Testing partial write of buffer\n");
109 memset(buf + 4096, 0x02, 1024);
110 memset(expected + 4096, 0x02, 1024);
111 ret = do_write(fd, handle, buf + 4096, 4096, 1024);
112 assert(ret == 0);
Eric Anholt3148c162008-04-29 13:45:43 -0700113 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -0700114 assert(ret == 0);
115 assert(memcmp(buf, expected, sizeof(buf)) == 0);
116
117 printf("Testing partial read of buffer\n");
118 ret = do_read(fd, handle, buf, 512, 1024);
119 assert(ret == 0);
120 assert(memcmp(buf, expected + 512, 1024) == 0);
121
122 close(fd);
123
124 return 0;
125}