Blame psm_help.h

Packit 961e70
/*
Packit 961e70
Packit 961e70
  This file is provided under a dual BSD/GPLv2 license.  When using or
Packit 961e70
  redistributing this file, you may do so under either license.
Packit 961e70
Packit 961e70
  GPL LICENSE SUMMARY
Packit 961e70
Packit 961e70
  Copyright(c) 2016 Intel Corporation.
Packit 961e70
Packit 961e70
  This program is free software; you can redistribute it and/or modify
Packit 961e70
  it under the terms of version 2 of the GNU General Public License as
Packit 961e70
  published by the Free Software Foundation.
Packit 961e70
Packit 961e70
  This program is distributed in the hope that it will be useful, but
Packit 961e70
  WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 961e70
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 961e70
  General Public License for more details.
Packit 961e70
Packit 961e70
  Contact Information:
Packit 961e70
  Intel Corporation, www.intel.com
Packit 961e70
Packit 961e70
  BSD LICENSE
Packit 961e70
Packit 961e70
  Copyright(c) 2016 Intel Corporation.
Packit 961e70
Packit 961e70
  Redistribution and use in source and binary forms, with or without
Packit 961e70
  modification, are permitted provided that the following conditions
Packit 961e70
  are met:
Packit 961e70
Packit 961e70
    * Redistributions of source code must retain the above copyright
Packit 961e70
      notice, this list of conditions and the following disclaimer.
Packit 961e70
    * Redistributions in binary form must reproduce the above copyright
Packit 961e70
      notice, this list of conditions and the following disclaimer in
Packit 961e70
      the documentation and/or other materials provided with the
Packit 961e70
      distribution.
Packit 961e70
    * Neither the name of Intel Corporation nor the names of its
Packit 961e70
      contributors may be used to endorse or promote products derived
Packit 961e70
      from this software without specific prior written permission.
Packit 961e70
Packit 961e70
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 961e70
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 961e70
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 961e70
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 961e70
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 961e70
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 961e70
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 961e70
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 961e70
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 961e70
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 961e70
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 961e70
Packit 961e70
*/
Packit 961e70
Packit 961e70
/* Copyright (c) 2003-2016 Intel Corporation. All rights reserved. */
Packit 961e70
Packit 961e70
#ifndef _PSMI_HELP_H
Packit 961e70
#define _PSMI_HELP_H
Packit 961e70
#include "psm_log.h"
Packit 961e70
Packit 961e70
/* XXX gcc only */
Packit 961e70
#define PSMI_INLINE(FN) \
Packit 961e70
	static inline FN
Packit 961e70
Packit 961e70
#define PSMI_ALWAYS_INLINE(FN) \
Packit 961e70
	static __inline__ FN __attribute__((always_inline));  \
Packit 961e70
	static __inline__ FN
Packit 961e70
Packit 961e70
#define PSMI_NEVER_INLINE(FN)             \
Packit 961e70
	static FN __attribute__((noinline));  \
Packit 961e70
	static FN
Packit 961e70
Packit 961e70
#define _PPragma(x) _Pragma(x)
Packit 961e70
Packit 961e70
#define STRINGIFY(s)	_STRINGIFY(s)
Packit 961e70
#define _STRINGIFY(s)	#s
Packit 961e70
#define PSMI_CURLOC	__FILE__ ":" STRINGIFY(__LINE__)
Packit 961e70
#define psmi_assert_always_loc(x, curloc)				\
Packit 961e70
	do {								\
Packit 961e70
	if_pf(!(x)) {							\
Packit 961e70
		psmi_handle_error(PSMI_EP_NORETURN, PSM2_INTERNAL_ERR,	\
Packit 961e70
				"Assertion failure at %s: %s", curloc,	\
Packit 961e70
				STRINGIFY(x));				\
Packit 961e70
	} } while (0)
Packit 961e70
Packit 961e70
#define psmi_assert_always(x)  psmi_assert_always_loc(x, PSMI_CURLOC)
Packit 961e70
Packit 961e70
#ifdef PSM_DEBUG
Packit 961e70
#  define psmi_assert(x)	psmi_assert_always(x)
Packit 961e70
#  define PSMI_ASSERT_INITIALIZED() psmi_assert_always(psmi_isinitialized())
Packit 961e70
#else
Packit 961e70
#  define psmi_assert(x)
Packit 961e70
#  define PSMI_ASSERT_INITIALIZED()
Packit 961e70
#endif
Packit 961e70
Packit 961e70
#define _PSMI_API_NAME(FN)  __ ## FN
Packit 961e70
#define _PSMI_API_STR(FN)   _STRINGIFY(__ ## FN)
Packit 961e70
#define PSMI_API_DECL(FN)							\
Packit 961e70
	typeof(_PSMI_API_NAME(FN)) FN __attribute__((weak, alias(_PSMI_API_STR(FN))));
Packit 961e70
Packit 961e70
#define PSMI_ERR_UNLESS_INITIALIZED(ep)					\
Packit 961e70
	do {								\
Packit 961e70
		if (!psmi_isinitialized()) {				\
Packit 961e70
			PSM2_LOG_MSG("leaving");				\
Packit 961e70
			return psmi_handle_error(ep, PSM2_INIT_NOT_INIT,	\
Packit 961e70
				"PSM2 has not been initialized");	\
Packit 961e70
	  }								\
Packit 961e70
	} while (0)
Packit 961e70
Packit 961e70
#define PSMI_CHECKMEM(err, mem)			\
Packit 961e70
	do {					\
Packit 961e70
		if ((mem) == NULL) {		\
Packit 961e70
			(err) = PSM2_NO_MEMORY;	\
Packit 961e70
			goto fail;		\
Packit 961e70
		}				\
Packit 961e70
	} while (0)
Packit 961e70
Packit 961e70
#define PSMI_CACHEALIGN	__attribute__((aligned(64)))
Packit 961e70
Packit 961e70
/* Easy way to ignore the OK_NO_PROGRESS case */
Packit 961e70
PSMI_ALWAYS_INLINE(psm2_error_t psmi_err_only(psm2_error_t err))
Packit 961e70
{
Packit 961e70
	if (err > PSM2_OK_NO_PROGRESS)
Packit 961e70
		return err;
Packit 961e70
	else
Packit 961e70
		return PSM2_OK;
Packit 961e70
}
Packit 961e70
Packit 961e70
#ifdef min
Packit 961e70
#undef min
Packit 961e70
#endif
Packit 961e70
#define min(a, b) ((a) < (b) ? (a) : (b))
Packit 961e70
Packit 961e70
#ifdef max
Packit 961e70
#undef max
Packit 961e70
#endif
Packit 961e70
#define max(a, b) ((a) > (b) ? (a) : (b))
Packit 961e70
Packit 961e70
#define SEC_ULL	 1000000000ULL
Packit 961e70
#define MSEC_ULL 1000000ULL
Packit 961e70
#define USEC_ULL 1000ULL
Packit 961e70
#define NSEC_ULL 1ULL
Packit 961e70
Packit 961e70
#define PSMI_TRUE   1
Packit 961e70
#define PSMI_FALSE  0
Packit 961e70
Packit 961e70
#define PSMI_CYCLES_TO_SECSF(cycles)			\
Packit 961e70
		((double) cycles_to_nanosecs(cycles) / 1.0e9)
Packit 961e70
Packit 961e70
#define PSMI_PAGESIZE       psmi_getpagesize()
Packit 961e70
#define PSMI_POWEROFTWO(P)  (((P)&((P)-1)) == 0)
Packit 961e70
#define PSMI_ALIGNDOWN(p, P) (((uintptr_t)(p))&~((uintptr_t)((P)-1)))
Packit 961e70
#define PSMI_ALIGNUP(p, P)   (PSMI_ALIGNDOWN((uintptr_t)(p)+((uintptr_t)((P)-1)), (P)))
Packit 961e70
Packit 961e70
#define PSMI_MAKE_DRIVER_VERSION(major, minor) ((major)<<16 | ((minor) & 0xffff))
Packit 961e70
Packit 961e70
#ifdef PSM_DEBUG
Packit 961e70
Packit 961e70
/* The intent of the following two macros is to emit an internal error if a size of a
Packit 961e70
   'member' is not as expected, violating an assumption in the code. There are some
Packit 961e70
   problems with the implementation of this code:
Packit 961e70
Packit 961e70
   The first macro creates a static const variable with ABSOLUTELY NO references
Packit 961e70
   to them.  For example there are ABSOLUTELY NO uses of the second macro in the
Packit 961e70
   PSM code. This is not completely pure. GCC version 5, for example, emits a
Packit 961e70
   warning for defining a static const when it is not referenced.
Packit 961e70
Packit 961e70
   A better implementation of the intent of this code is to use static_assert()
Packit 961e70
   so that at compile time the violations can be caught and corrected - not at
Packit 961e70
   run time.  */
Packit 961e70
Packit 961e70
#define PSMI_STRICT_SIZE_DECL(member, sz) static const size_t __psm2_ss_ ## member = sz
Packit 961e70
#define PSMI_STRICT_SIZE_VERIFY(member, sz)				\
Packit 961e70
	do {								\
Packit 961e70
		if (__psm2_ss_ ## member != (sz)) {			\
Packit 961e70
			char errmsg[64];				\
Packit 961e70
			snprintf(errmsg, 32, "Internal error: %s "	\
Packit 961e70
					"size doesn't match expected %d bytes",	\
Packit 961e70
					STRINGIFY(member), (int) __psm2_ss_ ## member);	\
Packit 961e70
			exit(-1);					\
Packit 961e70
		}							\
Packit 961e70
	} while (0)
Packit 961e70
Packit 961e70
#else
Packit 961e70
Packit 961e70
#define PSMI_STRICT_SIZE_DECL(member, sz)   /* nothing */
Packit 961e70
#define PSMI_STRICT_SIZE_VERIFY(member, sz) /* nothing */
Packit 961e70
Packit 961e70
#endif /*  PSM_DEBUG */
Packit 961e70
Packit 961e70
#endif /* _PSMI_HELP_H */