1d722e3fbSopenharmony_ci/* 2d722e3fbSopenharmony_ci * Copyright © 2014 NVIDIA Corporation 3d722e3fbSopenharmony_ci * 4d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation 7d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10d722e3fbSopenharmony_ci * 11d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included in 12d722e3fbSopenharmony_ci * all copies or substantial portions of the Software. 13d722e3fbSopenharmony_ci * 14d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17d722e3fbSopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18d722e3fbSopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19d722e3fbSopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20d722e3fbSopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 21d722e3fbSopenharmony_ci */ 22d722e3fbSopenharmony_ci 23d722e3fbSopenharmony_ci#ifndef LIBDRM_LIBDRM_H 24d722e3fbSopenharmony_ci#define LIBDRM_LIBDRM_H 25d722e3fbSopenharmony_ci 26d722e3fbSopenharmony_ci#if HAVE_VISIBILITY 27d722e3fbSopenharmony_ci# define drm_private __attribute__((visibility("hidden"))) 28d722e3fbSopenharmony_ci# define drm_public __attribute__((visibility("default"))) 29d722e3fbSopenharmony_ci#else 30d722e3fbSopenharmony_ci# define drm_private 31d722e3fbSopenharmony_ci# define drm_public 32d722e3fbSopenharmony_ci#endif 33d722e3fbSopenharmony_ci 34d722e3fbSopenharmony_ci 35d722e3fbSopenharmony_ci/** 36d722e3fbSopenharmony_ci * Static (compile-time) assertion. 37d722e3fbSopenharmony_ci * Basically, use COND to dimension an array. If COND is false/zero the 38d722e3fbSopenharmony_ci * array size will be -1 and we'll get a compilation error. 39d722e3fbSopenharmony_ci */ 40d722e3fbSopenharmony_ci#define STATIC_ASSERT(COND) \ 41d722e3fbSopenharmony_ci do { \ 42d722e3fbSopenharmony_ci (void) sizeof(char [1 - 2*!(COND)]); \ 43d722e3fbSopenharmony_ci } while (0) 44d722e3fbSopenharmony_ci 45d722e3fbSopenharmony_ci 46d722e3fbSopenharmony_ci#include <sys/mman.h> 47d722e3fbSopenharmony_ci 48d722e3fbSopenharmony_ci#if defined(ANDROID) && !defined(__LP64__) 49d722e3fbSopenharmony_ci#include <errno.h> /* for EINVAL */ 50d722e3fbSopenharmony_ci 51d722e3fbSopenharmony_cistatic inline void *drm_mmap(void *addr, size_t length, int prot, int flags, 52d722e3fbSopenharmony_ci int fd, loff_t offset) 53d722e3fbSopenharmony_ci{ 54d722e3fbSopenharmony_ci /* offset must be aligned to 4096 (not necessarily the page size) */ 55d722e3fbSopenharmony_ci if (offset & 4095) { 56d722e3fbSopenharmony_ci errno = EINVAL; 57d722e3fbSopenharmony_ci return MAP_FAILED; 58d722e3fbSopenharmony_ci } 59d722e3fbSopenharmony_ci 60d722e3fbSopenharmony_ci return mmap64(addr, length, prot, flags, fd, offset); 61d722e3fbSopenharmony_ci} 62d722e3fbSopenharmony_ci 63d722e3fbSopenharmony_ci# define drm_munmap(addr, length) \ 64d722e3fbSopenharmony_ci munmap(addr, length) 65d722e3fbSopenharmony_ci 66d722e3fbSopenharmony_ci 67d722e3fbSopenharmony_ci#else 68d722e3fbSopenharmony_ci 69d722e3fbSopenharmony_ci/* assume large file support exists */ 70d722e3fbSopenharmony_ci# define drm_mmap(addr, length, prot, flags, fd, offset) \ 71d722e3fbSopenharmony_ci mmap(addr, length, prot, flags, fd, offset) 72d722e3fbSopenharmony_ci 73d722e3fbSopenharmony_ci 74d722e3fbSopenharmony_cistatic inline int drm_munmap(void *addr, size_t length) 75d722e3fbSopenharmony_ci{ 76d722e3fbSopenharmony_ci /* Copied from configure code generated by AC_SYS_LARGEFILE */ 77d722e3fbSopenharmony_ci#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + \ 78d722e3fbSopenharmony_ci (((off_t) 1 << 31) << 31)) 79d722e3fbSopenharmony_ci STATIC_ASSERT(LARGE_OFF_T % 2147483629 == 721 && 80d722e3fbSopenharmony_ci LARGE_OFF_T % 2147483647 == 1); 81d722e3fbSopenharmony_ci#undef LARGE_OFF_T 82d722e3fbSopenharmony_ci 83d722e3fbSopenharmony_ci return munmap(addr, length); 84d722e3fbSopenharmony_ci} 85d722e3fbSopenharmony_ci#endif 86d722e3fbSopenharmony_ci 87d722e3fbSopenharmony_ci#endif 88