/*
 * Copyright (c) 2020-2022, NVIDIA CORPORATION.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of NVIDIA CORPORATION nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef DEBUG__NVRISCV__PRINT_H
#define DEBUG__NVRISCV__PRINT_H
#define FSP__DEBUG__NVRISCV__PRINT_H    1

/* Compiler headers */
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>

/* Early FSP headers */
#include <misc/gcc_attrs.h>

/* Hardware headers */

/* Late FSP headers */

/* Module-specific FSP headers */

// Logging levels for ucode

// Level not to be used (used to check for disabled prints)
#define LEVEL_NONE     0

// Levels below are printed if level passed is <= level from configuration

//
// Intened for messages that should be always printed. That may include
// bootup/shutdown (for every stage), fatal crash etc.
//
#define LEVEL_ALWAYS   1
// Critical (unrecoverable errors)
#define LEVEL_CRIT     3
// Ordinary errors, extra verbosity / information vs crit
#define LEVEL_ERROR    5
// Logs that may help understanding what's going on (but doesn't happen too often)
#define LEVEL_INFO     7
// Information useful for debugging
#define LEVEL_DEBUG   10
// Most verbose messages should be here (for example incoming RPC's)
#define LEVEL_TRACE   15

#define NVRISCV_DEBUG_PRINT_ENABLED (NVRISCV_CONFIG_DEBUG_PRINT_LEVEL > LEVEL_NONE)

#if NVRISCV_DEBUG_PRINT_ENABLED
int putchar(int c);
int puts(const char *pStr);
void putHex(unsigned count, unsigned long value);
GCC_ATTR_FORMAT_PRINTF(1, 2) int printf(const char *pFmt, ...);
#else
static inline int putchar(int c GCC_ATTR_UNUSED) { return 0; }
static inline int puts(const char *pStr GCC_ATTR_UNUSED) { return 0; }
static inline void putHex(unsigned count GCC_ATTR_UNUSED,
                          unsigned long value GCC_ATTR_UNUSED) { }

GCC_ATTR_FORMAT_PRINTF(1, 2)
static inline int printf(const char *pFmt GCC_ATTR_UNUSED, ...) { return 0; }
#endif

#define dbgPutchar(level, ch) do { if ((level) <= NVRISCV_CONFIG_DEBUG_PRINT_LEVEL) putchar(ch); } while(0)
#define dbgPuts(level, str) do { if ((level) <= NVRISCV_CONFIG_DEBUG_PRINT_LEVEL) puts(str); } while (0)
#define dbgPutHex(level, count, value) do { if ((level) <= NVRISCV_CONFIG_DEBUG_PRINT_LEVEL) putHex(count, value); } while (0)
#define dbgPrintf(level, fmt, ...) do { if ((level) <= NVRISCV_CONFIG_DEBUG_PRINT_LEVEL) printf((fmt), ##__VA_ARGS__); } while (0)

// String wrapper for non-standard print functions, string arrays etc.
#define dbgStr(level, str) ((level) <= NVRISCV_CONFIG_DEBUG_PRINT_LEVEL ? str : "")

#if NVRISCV_DEBUG_PRINT_ENABLED
bool printInit(void *pBuffer, uint16_t bufferSize, uint8_t queueNo, uint8_t swgenNo);
bool printInitEx(void *pBuffer, uint16_t bufferSize, uint32_t queueHeadPri, uint32_t queueTailPri, uint8_t swgenNo);
#else
static inline bool printInit(void *pBuffer GCC_ATTR_UNUSED,
                             uint16_t bufferSize GCC_ATTR_UNUSED,
                             uint8_t queueNo GCC_ATTR_UNUSED,
                             uint8_t swgenNo GCC_ATTR_UNUSED)
{
    return true;
}
static inline bool printInitEx(void *pBuffer GCC_ATTR_UNUSED,
                               uint16_t bufferSize GCC_ATTR_UNUSED,
                               uint32_t queueHeadPri GCC_ATTR_UNUSED,
                               uint32_t queueTailPri GCC_ATTR_UNUSED,
                               uint8_t swgenNo GCC_ATTR_UNUSED)
{
    return true;
}
#endif // NVRISCV_DEBUG_PRINT_ENABLED

#endif // DEBUG__NVRISCV__PRINT_H
