162306a36Sopenharmony_ci/***********************license start************************************
262306a36Sopenharmony_ci * Copyright (c) 2003-2017 Cavium, Inc.
362306a36Sopenharmony_ci * All rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * License: one of 'Cavium License' or 'GNU General Public License Version 2'
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * This file is provided under the terms of the Cavium License (see below)
862306a36Sopenharmony_ci * or under the terms of GNU General Public License, Version 2, as
962306a36Sopenharmony_ci * published by the Free Software Foundation. When using or redistributing
1062306a36Sopenharmony_ci * this file, you may do so under either license.
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Cavium License:  Redistribution and use in source and binary forms, with
1362306a36Sopenharmony_ci * or without modification, are permitted provided that the following
1462306a36Sopenharmony_ci * conditions are met:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *  * Redistributions of source code must retain the above copyright
1762306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *  * Redistributions in binary form must reproduce the above
2062306a36Sopenharmony_ci *    copyright notice, this list of conditions and the following
2162306a36Sopenharmony_ci *    disclaimer in the documentation and/or other materials provided
2262306a36Sopenharmony_ci *    with the distribution.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci *  * Neither the name of Cavium Inc. nor the names of its contributors may be
2562306a36Sopenharmony_ci *    used to endorse or promote products derived from this software without
2662306a36Sopenharmony_ci *    specific prior written permission.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * This Software, including technical data, may be subject to U.S. export
2962306a36Sopenharmony_ci * control laws, including the U.S. Export Administration Act and its
3062306a36Sopenharmony_ci * associated regulations, and may be subject to export or import
3162306a36Sopenharmony_ci * regulations in other countries.
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
3462306a36Sopenharmony_ci * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
3562306a36Sopenharmony_ci * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
3662306a36Sopenharmony_ci * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
3762306a36Sopenharmony_ci * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
3862306a36Sopenharmony_ci * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
3962306a36Sopenharmony_ci * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
4062306a36Sopenharmony_ci * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
4162306a36Sopenharmony_ci * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
4262306a36Sopenharmony_ci * ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
4362306a36Sopenharmony_ci * WITH YOU.
4462306a36Sopenharmony_ci ***********************license end**************************************/
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#include "common.h"
4762306a36Sopenharmony_ci#include "zip_deflate.h"
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/**
5062306a36Sopenharmony_ci * zip_cmd_queue_consumed - Calculates the space consumed in the command queue.
5162306a36Sopenharmony_ci *
5262306a36Sopenharmony_ci * @zip_dev: Pointer to zip device structure
5362306a36Sopenharmony_ci * @queue:   Queue number
5462306a36Sopenharmony_ci *
5562306a36Sopenharmony_ci * Return: Bytes consumed in the command queue buffer.
5662306a36Sopenharmony_ci */
5762306a36Sopenharmony_cistatic inline u32 zip_cmd_queue_consumed(struct zip_device *zip_dev, int queue)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	return ((zip_dev->iq[queue].sw_head - zip_dev->iq[queue].sw_tail) *
6062306a36Sopenharmony_ci		sizeof(u64 *));
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/**
6462306a36Sopenharmony_ci * zip_load_instr - Submits the instruction into the ZIP command queue
6562306a36Sopenharmony_ci * @instr:      Pointer to the instruction to be submitted
6662306a36Sopenharmony_ci * @zip_dev:    Pointer to ZIP device structure to which the instruction is to
6762306a36Sopenharmony_ci *              be submitted
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * This function copies the ZIP instruction to the command queue and rings the
7062306a36Sopenharmony_ci * doorbell to notify the engine of the instruction submission. The command
7162306a36Sopenharmony_ci * queue is maintained in a circular fashion. When there is space for exactly
7262306a36Sopenharmony_ci * one instruction in the queue, next chunk pointer of the queue is made to
7362306a36Sopenharmony_ci * point to the head of the queue, thus maintaining a circular queue.
7462306a36Sopenharmony_ci *
7562306a36Sopenharmony_ci * Return: Queue number to which the instruction was submitted
7662306a36Sopenharmony_ci */
7762306a36Sopenharmony_ciu32 zip_load_instr(union zip_inst_s *instr,
7862306a36Sopenharmony_ci		   struct zip_device *zip_dev)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	union zip_quex_doorbell dbell;
8162306a36Sopenharmony_ci	u32 queue = 0;
8262306a36Sopenharmony_ci	u32 consumed = 0;
8362306a36Sopenharmony_ci	u64 *ncb_ptr = NULL;
8462306a36Sopenharmony_ci	union zip_nptr_s ncp;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	/*
8762306a36Sopenharmony_ci	 * Distribute the instructions between the enabled queues based on
8862306a36Sopenharmony_ci	 * the CPU id.
8962306a36Sopenharmony_ci	 */
9062306a36Sopenharmony_ci	if (raw_smp_processor_id() % 2 == 0)
9162306a36Sopenharmony_ci		queue = 0;
9262306a36Sopenharmony_ci	else
9362306a36Sopenharmony_ci		queue = 1;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	zip_dbg("CPU Core: %d Queue number:%d", raw_smp_processor_id(), queue);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/* Take cmd buffer lock */
9862306a36Sopenharmony_ci	spin_lock(&zip_dev->iq[queue].lock);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	/*
10162306a36Sopenharmony_ci	 * Command Queue implementation
10262306a36Sopenharmony_ci	 * 1. If there is place for new instructions, push the cmd at sw_head.
10362306a36Sopenharmony_ci	 * 2. If there is place for exactly one instruction, push the new cmd
10462306a36Sopenharmony_ci	 *    at the sw_head. Make sw_head point to the sw_tail to make it
10562306a36Sopenharmony_ci	 *    circular. Write sw_head's physical address to the "Next-Chunk
10662306a36Sopenharmony_ci	 *    Buffer Ptr" to make it cmd_hw_tail.
10762306a36Sopenharmony_ci	 * 3. Ring the door bell.
10862306a36Sopenharmony_ci	 */
10962306a36Sopenharmony_ci	zip_dbg("sw_head : %lx", zip_dev->iq[queue].sw_head);
11062306a36Sopenharmony_ci	zip_dbg("sw_tail : %lx", zip_dev->iq[queue].sw_tail);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	consumed = zip_cmd_queue_consumed(zip_dev, queue);
11362306a36Sopenharmony_ci	/* Check if there is space to push just one cmd */
11462306a36Sopenharmony_ci	if ((consumed + 128) == (ZIP_CMD_QBUF_SIZE - 8)) {
11562306a36Sopenharmony_ci		zip_dbg("Cmd queue space available for single command");
11662306a36Sopenharmony_ci		/* Space for one cmd, pust it and make it circular queue */
11762306a36Sopenharmony_ci		memcpy((u8 *)zip_dev->iq[queue].sw_head, (u8 *)instr,
11862306a36Sopenharmony_ci		       sizeof(union zip_inst_s));
11962306a36Sopenharmony_ci		zip_dev->iq[queue].sw_head += 16; /* 16 64_bit words = 128B */
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci		/* Now, point the "Next-Chunk Buffer Ptr" to sw_head */
12262306a36Sopenharmony_ci		ncb_ptr = zip_dev->iq[queue].sw_head;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci		zip_dbg("ncb addr :0x%lx sw_head addr :0x%lx",
12562306a36Sopenharmony_ci			ncb_ptr, zip_dev->iq[queue].sw_head - 16);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci		/* Using Circular command queue */
12862306a36Sopenharmony_ci		zip_dev->iq[queue].sw_head = zip_dev->iq[queue].sw_tail;
12962306a36Sopenharmony_ci		/* Mark this buffer for free */
13062306a36Sopenharmony_ci		zip_dev->iq[queue].free_flag = 1;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci		/* Write new chunk buffer address at "Next-Chunk Buffer Ptr" */
13362306a36Sopenharmony_ci		ncp.u_reg64 = 0ull;
13462306a36Sopenharmony_ci		ncp.s.addr = __pa(zip_dev->iq[queue].sw_head);
13562306a36Sopenharmony_ci		*ncb_ptr = ncp.u_reg64;
13662306a36Sopenharmony_ci		zip_dbg("*ncb_ptr :0x%lx sw_head[phys] :0x%lx",
13762306a36Sopenharmony_ci			*ncb_ptr, __pa(zip_dev->iq[queue].sw_head));
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci		zip_dev->iq[queue].pend_cnt++;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	} else {
14262306a36Sopenharmony_ci		zip_dbg("Enough space is available for commands");
14362306a36Sopenharmony_ci		/* Push this cmd to cmd queue buffer */
14462306a36Sopenharmony_ci		memcpy((u8 *)zip_dev->iq[queue].sw_head, (u8 *)instr,
14562306a36Sopenharmony_ci		       sizeof(union zip_inst_s));
14662306a36Sopenharmony_ci		zip_dev->iq[queue].sw_head += 16; /* 16 64_bit words = 128B */
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci		zip_dev->iq[queue].pend_cnt++;
14962306a36Sopenharmony_ci	}
15062306a36Sopenharmony_ci	zip_dbg("sw_head :0x%lx sw_tail :0x%lx hw_tail :0x%lx",
15162306a36Sopenharmony_ci		zip_dev->iq[queue].sw_head, zip_dev->iq[queue].sw_tail,
15262306a36Sopenharmony_ci		zip_dev->iq[queue].hw_tail);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	zip_dbg(" Pushed the new cmd : pend_cnt : %d",
15562306a36Sopenharmony_ci		zip_dev->iq[queue].pend_cnt);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	/* Ring the doorbell */
15862306a36Sopenharmony_ci	dbell.u_reg64     = 0ull;
15962306a36Sopenharmony_ci	dbell.s.dbell_cnt = 1;
16062306a36Sopenharmony_ci	zip_reg_write(dbell.u_reg64,
16162306a36Sopenharmony_ci		      (zip_dev->reg_base + ZIP_QUEX_DOORBELL(queue)));
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	/* Unlock cmd buffer lock */
16462306a36Sopenharmony_ci	spin_unlock(&zip_dev->iq[queue].lock);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	return queue;
16762306a36Sopenharmony_ci}
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/**
17062306a36Sopenharmony_ci * zip_update_cmd_bufs - Updates the queue statistics after posting the
17162306a36Sopenharmony_ci *                       instruction
17262306a36Sopenharmony_ci * @zip_dev: Pointer to zip device structure
17362306a36Sopenharmony_ci * @queue:   Queue number
17462306a36Sopenharmony_ci */
17562306a36Sopenharmony_civoid zip_update_cmd_bufs(struct zip_device *zip_dev, u32 queue)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	/* Take cmd buffer lock */
17862306a36Sopenharmony_ci	spin_lock(&zip_dev->iq[queue].lock);
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	/* Check if the previous buffer can be freed */
18162306a36Sopenharmony_ci	if (zip_dev->iq[queue].free_flag == 1) {
18262306a36Sopenharmony_ci		zip_dbg("Free flag. Free cmd buffer, adjust sw head and tail");
18362306a36Sopenharmony_ci		/* Reset the free flag */
18462306a36Sopenharmony_ci		zip_dev->iq[queue].free_flag = 0;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci		/* Point the hw_tail to start of the new chunk buffer */
18762306a36Sopenharmony_ci		zip_dev->iq[queue].hw_tail = zip_dev->iq[queue].sw_head;
18862306a36Sopenharmony_ci	} else {
18962306a36Sopenharmony_ci		zip_dbg("Free flag not set. increment hw tail");
19062306a36Sopenharmony_ci		zip_dev->iq[queue].hw_tail += 16; /* 16 64_bit words = 128B */
19162306a36Sopenharmony_ci	}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	zip_dev->iq[queue].done_cnt++;
19462306a36Sopenharmony_ci	zip_dev->iq[queue].pend_cnt--;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	zip_dbg("sw_head :0x%lx sw_tail :0x%lx hw_tail :0x%lx",
19762306a36Sopenharmony_ci		zip_dev->iq[queue].sw_head, zip_dev->iq[queue].sw_tail,
19862306a36Sopenharmony_ci		zip_dev->iq[queue].hw_tail);
19962306a36Sopenharmony_ci	zip_dbg(" Got CC : pend_cnt : %d\n", zip_dev->iq[queue].pend_cnt);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	spin_unlock(&zip_dev->iq[queue].lock);
20262306a36Sopenharmony_ci}
203