Back to index

libdrm  2.4.37
gem_mmap.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2008 Intel Corporation
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice (including the next
00012  * paragraph) shall be included in all copies or substantial portions of the
00013  * Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00021  * IN THE SOFTWARE.
00022  *
00023  * Authors:
00024  *    Eric Anholt <eric@anholt.net>
00025  *
00026  */
00027 
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <assert.h>
00032 #include <fcntl.h>
00033 #include <inttypes.h>
00034 #include <errno.h>
00035 #include <sys/stat.h>
00036 #include "drm.h"
00037 #include "i915_drm.h"
00038 
00039 #define OBJECT_SIZE 16384
00040 
00041 int do_read(int fd, int handle, void *buf, int offset, int size)
00042 {
00043        struct drm_i915_gem_pread read;
00044 
00045        /* Ensure that we don't have any convenient data in buf in case
00046         * we fail.
00047         */
00048        memset(buf, 0xd0, size);
00049 
00050        memset(&read, 0, sizeof(read));
00051        read.handle = handle;
00052        read.data_ptr = (uintptr_t)buf;
00053        read.size = size;
00054        read.offset = offset;
00055 
00056        return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &read);
00057 }
00058 
00059 int do_write(int fd, int handle, void *buf, int offset, int size)
00060 {
00061        struct drm_i915_gem_pwrite write;
00062 
00063        memset(&write, 0, sizeof(write));
00064        write.handle = handle;
00065        write.data_ptr = (uintptr_t)buf;
00066        write.size = size;
00067        write.offset = offset;
00068 
00069        return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &write);
00070 }
00071 
00072 int main(int argc, char **argv)
00073 {
00074        int fd;
00075        struct drm_i915_gem_create create;
00076        struct drm_i915_gem_mmap mmap;
00077        struct drm_gem_close unref;
00078        uint8_t expected[OBJECT_SIZE];
00079        uint8_t buf[OBJECT_SIZE];
00080        uint8_t *addr;
00081        int ret;
00082        int handle;
00083 
00084        fd = drm_open_matching("8086:*", 0);
00085        if (fd < 0) {
00086               fprintf(stderr, "failed to open intel drm device, skipping\n");
00087               return 0;
00088        }
00089 
00090        memset(&mmap, 0, sizeof(mmap));
00091        mmap.handle = 0x10101010;
00092        mmap.offset = 0;
00093        mmap.size = 4096;
00094        printf("Testing mmaping of bad object.\n");
00095        ret = ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap);
00096        assert(ret == -1 && errno == ENOENT);
00097 
00098        memset(&create, 0, sizeof(create));
00099        create.size = OBJECT_SIZE;
00100        ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
00101        assert(ret == 0);
00102        handle = create.handle;
00103 
00104        printf("Testing mmaping of newly created object.\n");
00105        mmap.handle = handle;
00106        mmap.offset = 0;
00107        mmap.size = OBJECT_SIZE;
00108        ret = ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap);
00109        assert(ret == 0);
00110        addr = (uint8_t *)(uintptr_t)mmap.addr_ptr;
00111 
00112        printf("Testing contents of newly created object.\n");
00113        memset(expected, 0, sizeof(expected));
00114        assert(memcmp(addr, expected, sizeof(expected)) == 0);
00115 
00116        printf("Testing coherency of writes and mmap reads.\n");
00117        memset(buf, 0, sizeof(buf));
00118        memset(buf + 1024, 0x01, 1024);
00119        memset(expected + 1024, 0x01, 1024);
00120        ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
00121        assert(ret == 0);
00122        assert(memcmp(buf, addr, sizeof(buf)) == 0);
00123 
00124        printf("Testing that mapping stays after close\n");
00125        unref.handle = handle;
00126        ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &unref);
00127        assert(ret == 0);
00128        assert(memcmp(buf, addr, sizeof(buf)) == 0);
00129 
00130        printf("Testing unmapping\n");
00131        munmap(addr, OBJECT_SIZE);
00132 
00133        close(fd);
00134 
00135        return 0;
00136 }