/*
 * Copyright (C) 2018 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "runtime/built_ins/built_ins.h"
#include "runtime/command_stream/preemption.h"
#include "runtime/device/device.h"
#include "runtime/helpers/dispatch_info.h"
#include "runtime/helpers/string.h"
#include "runtime/kernel/kernel.h"

namespace OCLRT {

bool PreemptionHelper::allowThreadGroupPreemption(Kernel *kernel, const WorkaroundTable *waTable) {
    if (waTable->waDisablePerCtxtPreemptionGranularityControl) {
        return false;
    }

    if (kernel) {
        if (kernel->getKernelInfo().patchInfo.executionEnvironment &&
            kernel->getKernelInfo().patchInfo.executionEnvironment->UsesFencesForReadWriteImages &&
            waTable->waDisableLSQCROPERFforOCL) {
            return false;
        }
        if (kernel->isSchedulerKernel || kernel->isVmeKernel()) {
            return false;
        }
    }

    return true;
}

bool PreemptionHelper::allowMidThreadPreemption(Kernel *kernel, Device &device) {
    bool allowedByKernel = true;
    if (kernel) {
        allowedByKernel = (kernel->getKernelInfo().patchInfo.executionEnvironment->DisableMidThreadPreemption == 0) &&
                          !(kernel->isVmeKernel() && !device.getDeviceInfo().vmeAvcSupportsPreemption);
    }
    bool supportedByDevice = (device.getPreemptionMode() >= PreemptionMode::MidThread);
    return supportedByDevice && allowedByKernel;
}

PreemptionMode PreemptionHelper::taskPreemptionMode(Device &device, Kernel *kernel) {
    if (device.getPreemptionMode() == PreemptionMode::Disabled) {
        return PreemptionMode::Disabled;
    }

    if (device.getPreemptionMode() >= PreemptionMode::MidThread &&
        allowMidThreadPreemption(kernel, device)) {
        return PreemptionMode::MidThread;
    }

    if (device.getPreemptionMode() >= PreemptionMode::ThreadGroup &&
        allowThreadGroupPreemption(kernel, device.getWaTable())) {
        return PreemptionMode::ThreadGroup;
    }

    return PreemptionMode::MidBatch;
};

PreemptionMode PreemptionHelper::taskPreemptionMode(Device &device, const MultiDispatchInfo &multiDispatchInfo) {
    PreemptionMode devMode = device.getPreemptionMode();

    for (const auto &di : multiDispatchInfo) {
        PreemptionMode taskMode = taskPreemptionMode(device, di.getKernel());
        if (devMode > taskMode) {
            devMode = taskMode;
        }
    }

    return devMode;
}

void PreemptionHelper::adjustDefaultPreemptionMode(RuntimeCapabilityTable &deviceCapabilities, bool allowMidThread, bool allowThreadGroup, bool allowMidBatch) {
    if (deviceCapabilities.defaultPreemptionMode >= PreemptionMode::MidThread &&
        allowMidThread) {
        deviceCapabilities.defaultPreemptionMode = PreemptionMode::MidThread;
    } else if (deviceCapabilities.defaultPreemptionMode >= PreemptionMode::ThreadGroup &&
               allowThreadGroup) {
        deviceCapabilities.defaultPreemptionMode = PreemptionMode::ThreadGroup;
    } else if (deviceCapabilities.defaultPreemptionMode >= PreemptionMode::MidBatch &&
               allowMidBatch) {
        deviceCapabilities.defaultPreemptionMode = PreemptionMode::MidBatch;
    } else {
        deviceCapabilities.defaultPreemptionMode = PreemptionMode::Disabled;
    }
}

PreemptionMode PreemptionHelper::getDefaultPreemptionMode(const HardwareInfo &hwInfo) {
    return DebugManager.flags.ForcePreemptionMode.get() == -1
               ? hwInfo.capabilityTable.defaultPreemptionMode
               : static_cast<PreemptionMode>(DebugManager.flags.ForcePreemptionMode.get());
}

} // namespace OCLRT
