From 4201f58e1e9fb9d075f2c060187b1f6c589faba9 Mon Sep 17 00:00:00 2001 From: Vinod G Date: Tue, 4 Jun 2019 10:16:35 -0700 Subject: [PATCH] gpu: nvgpu: Add utils header for posix Move all definitions and functions other than type defines from types.h to new header utils.h for posix. Update files that use functions and defintions from utils.h DIV_ROUND_UP macro is updated to use safe_ops.h calls to handle the CERT-C wrap issues. Jira NVGPU-3411 Change-Id: I9da3e9f255f39949287c615519f062fd8816aa04 Signed-off-by: Vinod G Reviewed-on: https://git-master.nvidia.com/r/2130453 Reviewed-by: svc-mobile-coverity Reviewed-by: svc-mobile-misra Reviewed-by: Philip Elcan GVS: Gerrit_Virtual_Submit Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/common/vgpu/ivc/comm_vgpu.c | 1 + drivers/gpu/nvgpu/include/nvgpu/allocator.h | 1 + drivers/gpu/nvgpu/include/nvgpu/bios.h | 1 + drivers/gpu/nvgpu/include/nvgpu/bitops.h | 1 + .../nvgpu/include/nvgpu/channel_sync_syncpt.h | 1 + drivers/gpu/nvgpu/include/nvgpu/gk20a.h | 1 + drivers/gpu/nvgpu/include/nvgpu/kref.h | 1 + drivers/gpu/nvgpu/include/nvgpu/nvlink_bios.h | 1 + drivers/gpu/nvgpu/include/nvgpu/posix/types.h | 214 --------------- drivers/gpu/nvgpu/include/nvgpu/posix/utils.h | 258 ++++++++++++++++++ drivers/gpu/nvgpu/include/nvgpu/thread.h | 1 + drivers/gpu/nvgpu/include/nvgpu/utils.h | 8 +- drivers/gpu/nvgpu/include/nvgpu/vgpu/vgpu.h | 1 + drivers/gpu/nvgpu/os/posix/bitmap.c | 1 + 14 files changed, 276 insertions(+), 215 deletions(-) create mode 100644 drivers/gpu/nvgpu/include/nvgpu/posix/utils.h diff --git a/drivers/gpu/nvgpu/common/vgpu/ivc/comm_vgpu.c b/drivers/gpu/nvgpu/common/vgpu/ivc/comm_vgpu.c index 0fb4f3084..c2863a00c 100644 --- a/drivers/gpu/nvgpu/common/vgpu/ivc/comm_vgpu.c +++ b/drivers/gpu/nvgpu/common/vgpu/ivc/comm_vgpu.c @@ -21,6 +21,7 @@ */ #include +#include #include #include #include diff --git a/drivers/gpu/nvgpu/include/nvgpu/allocator.h b/drivers/gpu/nvgpu/include/nvgpu/allocator.h index 810c912da..202284046 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/allocator.h +++ b/drivers/gpu/nvgpu/include/nvgpu/allocator.h @@ -36,6 +36,7 @@ #include #include #include +#include /* #define ALLOCATOR_DEBUG_FINE */ diff --git a/drivers/gpu/nvgpu/include/nvgpu/bios.h b/drivers/gpu/nvgpu/include/nvgpu/bios.h index dc51d51d0..e6365014d 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/bios.h +++ b/drivers/gpu/nvgpu/include/nvgpu/bios.h @@ -24,6 +24,7 @@ #define NVGPU_BIOS_H #include +#include struct gk20a; diff --git a/drivers/gpu/nvgpu/include/nvgpu/bitops.h b/drivers/gpu/nvgpu/include/nvgpu/bitops.h index d289c3ca9..b1606cee1 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/bitops.h +++ b/drivers/gpu/nvgpu/include/nvgpu/bitops.h @@ -24,6 +24,7 @@ #include #include +#include /* * Explicit sizes for bit definitions. Please use these instead of BIT(). diff --git a/drivers/gpu/nvgpu/include/nvgpu/channel_sync_syncpt.h b/drivers/gpu/nvgpu/include/nvgpu/channel_sync_syncpt.h index 140ea9a26..0e46de3e0 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/channel_sync_syncpt.h +++ b/drivers/gpu/nvgpu/include/nvgpu/channel_sync_syncpt.h @@ -27,6 +27,7 @@ #define NVGPU_CHANNEL_SYNC_SYNCPT_H #include +#include #include #include diff --git a/drivers/gpu/nvgpu/include/nvgpu/gk20a.h b/drivers/gpu/nvgpu/include/nvgpu/gk20a.h index 6066c28e4..257808893 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/gk20a.h +++ b/drivers/gpu/nvgpu/include/nvgpu/gk20a.h @@ -100,6 +100,7 @@ enum ctxsw_addr_type; #include #include +#include #include #include diff --git a/drivers/gpu/nvgpu/include/nvgpu/kref.h b/drivers/gpu/nvgpu/include/nvgpu/kref.h index 0e096deed..e2cf22000 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/kref.h +++ b/drivers/gpu/nvgpu/include/nvgpu/kref.h @@ -28,6 +28,7 @@ #include #include +#include struct nvgpu_ref { nvgpu_atomic_t refcount; diff --git a/drivers/gpu/nvgpu/include/nvgpu/nvlink_bios.h b/drivers/gpu/nvgpu/include/nvgpu/nvlink_bios.h index be84324a1..e9327acad 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/nvlink_bios.h +++ b/drivers/gpu/nvgpu/include/nvgpu/nvlink_bios.h @@ -24,6 +24,7 @@ #define NVGPU_NVLINK_BIOS_H #include +#include #define NVLINK_CONFIG_DATA_HDR_VER_10 0x1U #define NVLINK_CONFIG_DATA_HDR_10_SIZE 16U diff --git a/drivers/gpu/nvgpu/include/nvgpu/posix/types.h b/drivers/gpu/nvgpu/include/nvgpu/posix/types.h index e3c18f50b..800200e0a 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/posix/types.h +++ b/drivers/gpu/nvgpu/include/nvgpu/posix/types.h @@ -58,218 +58,4 @@ typedef signed short s16; typedef signed int s32; typedef signed long long s64; -#define min_t(type, a, b) \ - ({ \ - type __a = (a); \ - type __b = (b); \ - __a < __b ? __a : __b; \ - }) - -#if defined(min) -#undef min -#endif -#if defined(max) -#undef max -#endif - -#define min(a, b) \ - ({ \ - (a) < (b) ? (a) : (b); \ - }) -#define max(a, b) \ - ({ \ - (a) > (b) ? (a) : (b); \ - }) -#define min3(a, b, c) min(min(a, b), c) - -#define PAGE_SIZE 4096U - -#define ARRAY_SIZE(array) \ - (sizeof(array) / sizeof((array)[0])) - -#define MAX_SCHEDULE_TIMEOUT LONG_MAX - -#define DIV_ROUND_UP(n, d) (((n) + (d) - 1U) / (d)) -#define DIV_ROUND_UP_ULL DIV_ROUND_UP - -#define DIV_ROUND_CLOSEST(a, divisor)( \ -{ \ - typeof(a) val = a; \ - typeof(divisor) div = divisor; \ - (((typeof(a))-1) > 0 || \ - ((typeof(divisor))-1) > 0 || (val) > 0) ? \ - (((val) + ((div) / 2)) / (div)) : \ - (((val) - ((div) / 2)) / (div)); \ -} \ -) -/* - * Joys of userspace: usually division just works since the compiler can link - * against external division functions implicitly. - */ -#define do_div(a, b) ((a) /= (b)) - -#define div64_u64(a, b) ((a) / (b)) - -#define round_mask(x, y) ((__typeof__(x))((y) - 1U)) -#define round_up(x, y) ((((x) - 1U) | round_mask(x, y)) + 1U) -#define roundup(x, y) round_up(x, y) -#define round_down(x, y) ((x) & ~round_mask(x, y)) - -#define ALIGN_MASK(x, mask) \ - ({ \ - typeof(x) ret; \ - typeof(x) sum = (x) + (mask); \ - \ - if ((sum >= (x)) && (sum >= (mask))) { \ - ret = sum & ~(mask); \ - } else { \ - ret = (typeof(x))~(typeof(x))0 & ~(mask); \ - } \ - ret; \ - }) - -#define ALIGN(x, a) ALIGN_MASK(x, \ - (a) > (typeof(a))0 ? \ - (typeof(x))(a) - 1U : \ - (typeof(x))0) -#define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE) - -#define HZ_TO_KHZ(x) ((x) / KHZ) -#define HZ_TO_MHZ(a) (u16)(a/MHZ) -#define HZ_TO_MHZ_ULL(a) (((a) > 0xF414F9CD7ULL) ? (u16) 0xffffU :\ - (((a) >> 32) > 0U) ? (u16) (((a) * 0x10C8ULL) >> 32) :\ - (u16) ((u32) (a)/MHZ)) -#define KHZ_TO_HZ(x) ((x) * KHZ) -#define MHZ_TO_KHZ(x) ((x) * KHZ) -#define KHZ_TO_MHZ(a) (u16)(a/KHZ) -#define MHZ_TO_HZ_ULL(a) ((u64)(a) * MHZ) - -/* - * Caps return at the size of the buffer not what would have been written if buf - * were arbitrarily sized. - */ -static inline int scnprintf(char *buf, size_t size, const char *format, ...) -{ - size_t ret; - va_list args; - - va_start(args, format); - ret = (size_t)vsnprintf(buf, size, format, args); - va_end(args); - - return ret <= size ? (int)ret : (int)size; -} - -static inline u32 be32_to_cpu(u32 x) -{ - /* - * Conveniently big-endian happens to be network byte order as well so - * we can use ntohl() for this. - */ - return ntohl(x); -} - -/* - * Hamming weights. - */ -static inline unsigned int nvgpu_posix_hweight8(uint8_t x) -{ - unsigned int ret; - uint8_t result = x - ((x >> 1) & 0x55U); - - result = (result & 0x33U) + ((result >> 2) & 0x33U); - result = (result + (result >> 4)) & 0x0FU; - ret = (unsigned int)result; - - return ret; -} -static inline unsigned int nvgpu_posix_hweight16(uint16_t x) -{ - unsigned int ret; - - ret = nvgpu_posix_hweight8((uint8_t)x); - ret += nvgpu_posix_hweight8((uint8_t)((x & 0xff00U) >> 8)); - - return ret; -} - -static inline unsigned int nvgpu_posix_hweight32(uint32_t x) -{ - unsigned int ret; - - ret = nvgpu_posix_hweight16((uint16_t)x); - ret += nvgpu_posix_hweight16((uint16_t)((x & 0xffff0000U) >> 16)); - - return ret; -} - -static inline unsigned int nvgpu_posix_hweight64(uint64_t x) -{ - unsigned int ret; - - ret = nvgpu_posix_hweight32((uint32_t)x); - ret += nvgpu_posix_hweight32((uint32_t)((x & - 0xffffffff00000000U) >> 32)); - - return ret; -} - -#define hweight32 nvgpu_posix_hweight32 -#define hweight_long nvgpu_posix_hweight64 - -/* - * Better suited under a compiler.h type header file, but for now these can live - * here. - */ -#define __must_check __attribute__((warn_unused_result)) -#define __maybe_unused __attribute__((unused)) -#define __iomem -#define __user -#define unlikely(x) (x) -#define likely(x) (x) - -#define __stringify(x) #x - -/* - * Prevent compiler optimizations from mangling writes. But likely most uses of - * this in nvgpu are incorrect (i.e unnecessary). - */ -#define WRITE_ONCE(p, v) \ - ({ \ - volatile typeof(p) *__p__ = &(p); \ - *__p__ = (v); \ - }) - -#define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) - -#define __packed __attribute__((packed)) - -#define MAX_ERRNO 4095 - -#define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO) - -static inline void *ERR_PTR(long error) -{ - return (void *) error; -} - -static inline long PTR_ERR(void *error) -{ - return (long)(uintptr_t)error; -} - -static inline bool IS_ERR(const void *ptr) -{ - return IS_ERR_VALUE((unsigned long)ptr); -} - -static inline bool IS_ERR_OR_NULL(const void *ptr) -{ - return (ptr == NULL) || IS_ERR_VALUE((unsigned long)ptr); -} - -#define ERESTARTSYS ERESTART - #endif /* NVGPU_POSIX_TYPES_H */ diff --git a/drivers/gpu/nvgpu/include/nvgpu/posix/utils.h b/drivers/gpu/nvgpu/include/nvgpu/posix/utils.h new file mode 100644 index 000000000..817bc81cc --- /dev/null +++ b/drivers/gpu/nvgpu/include/nvgpu/posix/utils.h @@ -0,0 +1,258 @@ +/* + * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef NVGPU_POSIX_UTILS_H +#define NVGPU_POSIX_UTILS_H + +#include + +#define min_t(type, a, b) \ + ({ \ + type __a = (a); \ + type __b = (b); \ + __a < __b ? __a : __b; \ + }) + +#if defined(min) +#undef min +#endif +#if defined(max) +#undef max +#endif + +#define min(a, b) \ + ({ \ + (a) < (b) ? (a) : (b); \ + }) +#define max(a, b) \ + ({ \ + (a) > (b) ? (a) : (b); \ + }) +#define min3(a, b, c) min(min(a, b), c) + +#define PAGE_SIZE 4096U + +#define ARRAY_SIZE(array) \ + (sizeof(array) / sizeof((array)[0])) + +#define MAX_SCHEDULE_TIMEOUT LONG_MAX + +#define DIV_ROUND_UP_U64(n, d) \ +({ \ + u64 divr = (u64)(d); \ + u64 divm = nvgpu_safe_sub_u64((divr), U64(1)); \ + u64 rup = nvgpu_safe_add_u64((u64)(n), (divm)); \ + u64 ret_val = ((rup) / (divr)); \ + ret_val ; \ +}) + +#define DIV_ROUND_UP(n, d) \ +({ \ + typeof(n) val = ((sizeof(typeof(n))) == (sizeof(u64))) ? \ + (typeof(n))(DIV_ROUND_UP_U64(n, d)) : \ + nvgpu_safe_cast_u64_to_u32(DIV_ROUND_UP_U64(n, d)); \ + val; \ +}) + +#define DIV_ROUND_UP_ULL DIV_ROUND_UP + +#define DIV_ROUND_CLOSEST(a, divisor)( \ +{ \ + typeof(a) val = (a); \ + typeof(divisor) div = (divisor); \ + (((typeof(a))-1) > 0 || \ + ((typeof(divisor))-1) > 0 || (val) > 0) ? \ + (((val) + ((div) / 2)) / (div)) : \ + (((val) - ((div) / 2)) / (div)); \ +} \ +) +/* + * Joys of userspace: usually division just works since the compiler can link + * against external division functions implicitly. + */ +#define do_div(a, b) ((a) /= (b)) + +#define div64_u64(a, b) ((a) / (b)) + +#define round_mask(x, y) ((__typeof__(x))((y) - 1U)) +#define round_up(x, y) ((((x) - 1U) | round_mask(x, y)) + 1U) +#define roundup(x, y) round_up(x, y) +#define round_down(x, y) ((x) & ~round_mask(x, y)) + +#define ALIGN_MASK(x, mask) \ + ({ \ + typeof(x) ret; \ + typeof(x) sum = (x) + (mask); \ + \ + if ((sum >= (x)) && (sum >= (mask))) { \ + ret = sum & ~(mask); \ + } else { \ + ret = (typeof(x))~(typeof(x))0 & ~(mask); \ + } \ + ret; \ + }) + +#define ALIGN(x, a) ALIGN_MASK(x, \ + (a) > (typeof(a))0 ? \ + (typeof(x))(a) - 1U : \ + (typeof(x))0) +#define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE) + +#define HZ_TO_KHZ(x) ((x) / KHZ) +#define HZ_TO_MHZ(a) (u16)(a/MHZ) +#define HZ_TO_MHZ_ULL(a) (((a) > 0xF414F9CD7ULL) ? (u16) 0xffffU :\ + (((a) >> 32) > 0U) ? (u16) (((a) * 0x10C8ULL) >> 32) :\ + (u16) ((u32) (a)/MHZ)) +#define KHZ_TO_HZ(x) ((x) * KHZ) +#define MHZ_TO_KHZ(x) ((x) * KHZ) +#define KHZ_TO_MHZ(a) (u16)(a/KHZ) +#define MHZ_TO_HZ_ULL(a) ((u64)(a) * MHZ) + +/* + * Caps return at the size of the buffer not what would have been written if buf + * were arbitrarily sized. + */ +static inline int scnprintf(char *buf, size_t size, const char *format, ...) +{ + size_t ret; + va_list args; + + va_start(args, format); + ret = (size_t)vsnprintf(buf, size, format, args); + va_end(args); + + return ret <= size ? (int)ret : (int)size; +} + +static inline u32 be32_to_cpu(u32 x) +{ + /* + * Conveniently big-endian happens to be network byte order as well so + * we can use ntohl() for this. + */ + return ntohl(x); +} + +/* + * Hamming weights. + */ +static inline unsigned int nvgpu_posix_hweight8(uint8_t x) +{ + unsigned int ret; + uint8_t result = x - ((x >> 1) & 0x55U); + + result = (result & 0x33U) + ((result >> 2) & 0x33U); + result = (result + (result >> 4)) & 0x0FU; + ret = (unsigned int)result; + + return ret; +} +static inline unsigned int nvgpu_posix_hweight16(uint16_t x) +{ + unsigned int ret; + + ret = nvgpu_posix_hweight8((uint8_t)x); + ret += nvgpu_posix_hweight8((uint8_t)((x & 0xff00U) >> 8)); + + return ret; +} + +static inline unsigned int nvgpu_posix_hweight32(uint32_t x) +{ + unsigned int ret; + + ret = nvgpu_posix_hweight16((uint16_t)x); + ret += nvgpu_posix_hweight16((uint16_t)((x & 0xffff0000U) >> 16)); + + return ret; +} + +static inline unsigned int nvgpu_posix_hweight64(uint64_t x) +{ + unsigned int ret; + + ret = nvgpu_posix_hweight32((uint32_t)x); + ret += nvgpu_posix_hweight32((uint32_t)((x & + 0xffffffff00000000U) >> 32)); + + return ret; +} + +#define hweight32 nvgpu_posix_hweight32 +#define hweight_long nvgpu_posix_hweight64 + +/* + * Better suited under a compiler.h type header file, but for now these can live + * here. + */ +#define __must_check __attribute__((warn_unused_result)) +#define __maybe_unused __attribute__((unused)) +#define __iomem +#define __user +#define unlikely(x) (x) +#define likely(x) (x) + +#define __stringify(x) #x + +/* + * Prevent compiler optimizations from mangling writes. But likely most uses of + * this in nvgpu are incorrect (i.e unnecessary). + */ +#define WRITE_ONCE(p, v) \ + ({ \ + volatile typeof(p) *__p__ = &(p); \ + *__p__ = (v); \ + }) + +#define container_of(ptr, type, member) ({ \ + const typeof(((type *)0)->member) *__mptr = (ptr); \ + (type *)((char *)__mptr - offsetof(type, member)); }) + +#define __packed __attribute__((packed)) + +#define MAX_ERRNO 4095 + +#define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO) + +static inline void *ERR_PTR(long error) +{ + return (void *) error; +} + +static inline long PTR_ERR(void *error) +{ + return (long)(uintptr_t)error; +} + +static inline bool IS_ERR(const void *ptr) +{ + return IS_ERR_VALUE((unsigned long)ptr); +} + +static inline bool IS_ERR_OR_NULL(const void *ptr) +{ + return (ptr == NULL) || IS_ERR_VALUE((unsigned long)ptr); +} + +#define ERESTARTSYS ERESTART + +#endif /* NVGPU_POSIX_UTILS_H */ diff --git a/drivers/gpu/nvgpu/include/nvgpu/thread.h b/drivers/gpu/nvgpu/include/nvgpu/thread.h index 102422c8e..7605dbac0 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/thread.h +++ b/drivers/gpu/nvgpu/include/nvgpu/thread.h @@ -30,6 +30,7 @@ #endif #include +#include /** * nvgpu_thread_create - Create and run a new thread. diff --git a/drivers/gpu/nvgpu/include/nvgpu/utils.h b/drivers/gpu/nvgpu/include/nvgpu/utils.h index 2e96e29d1..8962337ee 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/utils.h +++ b/drivers/gpu/nvgpu/include/nvgpu/utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -25,6 +25,12 @@ #include +#ifdef __KERNEL__ +#include +#else +#include +#endif + static inline u32 u64_hi32(u64 n) { return (u32)((n >> 32) & ~(u32)0); diff --git a/drivers/gpu/nvgpu/include/nvgpu/vgpu/vgpu.h b/drivers/gpu/nvgpu/include/nvgpu/vgpu/vgpu.h index 743e530d3..144504c35 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/vgpu/vgpu.h +++ b/drivers/gpu/nvgpu/include/nvgpu/vgpu/vgpu.h @@ -24,6 +24,7 @@ #define NVGPU_VGPU_H #include +#include #include #include #include diff --git a/drivers/gpu/nvgpu/os/posix/bitmap.c b/drivers/gpu/nvgpu/os/posix/bitmap.c index c5d2f15b4..4bf629dac 100644 --- a/drivers/gpu/nvgpu/os/posix/bitmap.c +++ b/drivers/gpu/nvgpu/os/posix/bitmap.c @@ -24,6 +24,7 @@ #include #include +#include #include #include