/*
 * Copyright (c) 2014 - 2016 MediaTek Inc.
 *
 * 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.
 */

#define TAG "[buffer]"
#include "mtk_log.h"

#include <unistd.h>
#include <tee_internal_api.h>
#include "mtk_buffer.h"

struct internal_buf {
    uint32_t pa;
    int size;
};

static  struct internal_buf disp_fb_buffer;
static  struct internal_buf disp_wk_buffer;
static  struct internal_buf touch_wk_buffer;

int set_disp_fb_buffer(uint32_t pa, int size)
{
    TUI_LOGD("%s\n", __func__);
    disp_fb_buffer.pa = pa;
    disp_fb_buffer.size = size;
    return 0;
}

int get_disp_fb_buffer(uint32_t *pa)
{
    *pa = disp_fb_buffer.pa;
    return disp_fb_buffer.size;
}

int clear_disp_fb_buffer(void)
{
    return set_disp_fb_buffer(0,0);
}

int set_disp_wk_buffer(uint32_t pa, int size)
{
    /* internal needs 1M buffer */
    static int requst = INTERNAL_WK_BUFFER_SIZE;

    TUI_LOGD("%s (0x%x, 0x%x)\n", __func__, pa, size);
    if (pa && size >=requst) {
        disp_wk_buffer.pa = pa;
        disp_wk_buffer.size = size;
        return 0;
    }
    TUI_LOGE("NO Enough Workinb buffer For display!\n");
    return -1;
}

int clear_disp_wk_buffer(void)
{
    TUI_LOGD("%s\n", __func__);
    disp_wk_buffer.pa = 0;
    disp_wk_buffer.size = 0;
    return 0;
}

int require_cmdq_buffer(uint32_t *pa)
{
    /*now only cmdq  extra buffer in display */
    *pa = disp_wk_buffer.pa;
    return disp_wk_buffer.size;
}

int set_touch_wk_buffer(uint32_t pa, int size)
{
    /* internal needs 1M buffer */
    static int requst = 0x100000;

    TUI_LOGD("%s (0x%x, 0x%x)\n", __func__, pa, size);
    if (pa && size >=requst) {
        touch_wk_buffer.pa = pa;
        touch_wk_buffer.size = size;
        return 0;
    }
    return -1;
}

int clear_touch_wk_buffer(void)
{
    TUI_LOGD("%s\n", __func__);
    touch_wk_buffer.pa = 0;
    touch_wk_buffer.size = 0;
    return 0;
}

int require_i2c_buffer(uint32_t *pa)
{
    /*now only i2c  extra buffer in touch */
    *pa = touch_wk_buffer.pa;
    return touch_wk_buffer.size;
}

int get_secure_buffer(uint32_t *pa)
{
    uint32_t secure_pa = 0;
    int secure_size = 0;
    uint32_t phys_addr = 0;
    int phys_size = 0;

    /* fb address is the firt physical address*/
    phys_size = get_disp_fb_buffer(&phys_addr);
    if (phys_size) {
        secure_pa = phys_addr;
        secure_size = phys_size;
    }

    /* i2c address is the last physical address*/
    phys_size = require_i2c_buffer(&phys_addr);

    if (secure_pa) {
        if (phys_size != 0) {
            /* i2c is used, protect all secure buffer*/
            secure_size = (phys_addr - secure_pa) + phys_size;
        }
    } else {
        /* only i2c address is used*/
        secure_pa = phys_addr;
        secure_size = phys_size;
    }

    *pa = secure_pa;
    return secure_size;
}
