xref: /OK3568_Linux_fs/kernel/drivers/crypto/cavium/zip/zip_device.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /***********************license start************************************
2*4882a593Smuzhiyun  * Copyright (c) 2003-2017 Cavium, Inc.
3*4882a593Smuzhiyun  * All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * License: one of 'Cavium License' or 'GNU General Public License Version 2'
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This file is provided under the terms of the Cavium License (see below)
8*4882a593Smuzhiyun  * or under the terms of GNU General Public License, Version 2, as
9*4882a593Smuzhiyun  * published by the Free Software Foundation. When using or redistributing
10*4882a593Smuzhiyun  * this file, you may do so under either license.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Cavium License:  Redistribution and use in source and binary forms, with
13*4882a593Smuzhiyun  * or without modification, are permitted provided that the following
14*4882a593Smuzhiyun  * conditions are met:
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *  * Redistributions of source code must retain the above copyright
17*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *  * Redistributions in binary form must reproduce the above
20*4882a593Smuzhiyun  *    copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun  *    disclaimer in the documentation and/or other materials provided
22*4882a593Smuzhiyun  *    with the distribution.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *  * Neither the name of Cavium Inc. nor the names of its contributors may be
25*4882a593Smuzhiyun  *    used to endorse or promote products derived from this software without
26*4882a593Smuzhiyun  *    specific prior written permission.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * This Software, including technical data, may be subject to U.S. export
29*4882a593Smuzhiyun  * control laws, including the U.S. Export Administration Act and its
30*4882a593Smuzhiyun  * associated regulations, and may be subject to export or import
31*4882a593Smuzhiyun  * regulations in other countries.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
34*4882a593Smuzhiyun  * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
35*4882a593Smuzhiyun  * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
36*4882a593Smuzhiyun  * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
37*4882a593Smuzhiyun  * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
38*4882a593Smuzhiyun  * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
39*4882a593Smuzhiyun  * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
40*4882a593Smuzhiyun  * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
41*4882a593Smuzhiyun  * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
42*4882a593Smuzhiyun  * ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
43*4882a593Smuzhiyun  * WITH YOU.
44*4882a593Smuzhiyun  ***********************license end**************************************/
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #include "common.h"
47*4882a593Smuzhiyun #include "zip_deflate.h"
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /**
50*4882a593Smuzhiyun  * zip_cmd_queue_consumed - Calculates the space consumed in the command queue.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * @zip_dev: Pointer to zip device structure
53*4882a593Smuzhiyun  * @queue:   Queue number
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * Return: Bytes consumed in the command queue buffer.
56*4882a593Smuzhiyun  */
zip_cmd_queue_consumed(struct zip_device * zip_dev,int queue)57*4882a593Smuzhiyun static inline u32 zip_cmd_queue_consumed(struct zip_device *zip_dev, int queue)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	return ((zip_dev->iq[queue].sw_head - zip_dev->iq[queue].sw_tail) *
60*4882a593Smuzhiyun 		sizeof(u64 *));
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /**
64*4882a593Smuzhiyun  * zip_load_instr - Submits the instruction into the ZIP command queue
65*4882a593Smuzhiyun  * @instr:      Pointer to the instruction to be submitted
66*4882a593Smuzhiyun  * @zip_dev:    Pointer to ZIP device structure to which the instruction is to
67*4882a593Smuzhiyun  *              be submitted
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * This function copies the ZIP instruction to the command queue and rings the
70*4882a593Smuzhiyun  * doorbell to notify the engine of the instruction submission. The command
71*4882a593Smuzhiyun  * queue is maintained in a circular fashion. When there is space for exactly
72*4882a593Smuzhiyun  * one instruction in the queue, next chunk pointer of the queue is made to
73*4882a593Smuzhiyun  * point to the head of the queue, thus maintaining a circular queue.
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * Return: Queue number to which the instruction was submitted
76*4882a593Smuzhiyun  */
zip_load_instr(union zip_inst_s * instr,struct zip_device * zip_dev)77*4882a593Smuzhiyun u32 zip_load_instr(union zip_inst_s *instr,
78*4882a593Smuzhiyun 		   struct zip_device *zip_dev)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	union zip_quex_doorbell dbell;
81*4882a593Smuzhiyun 	u32 queue = 0;
82*4882a593Smuzhiyun 	u32 consumed = 0;
83*4882a593Smuzhiyun 	u64 *ncb_ptr = NULL;
84*4882a593Smuzhiyun 	union zip_nptr_s ncp;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/*
87*4882a593Smuzhiyun 	 * Distribute the instructions between the enabled queues based on
88*4882a593Smuzhiyun 	 * the CPU id.
89*4882a593Smuzhiyun 	 */
90*4882a593Smuzhiyun 	if (raw_smp_processor_id() % 2 == 0)
91*4882a593Smuzhiyun 		queue = 0;
92*4882a593Smuzhiyun 	else
93*4882a593Smuzhiyun 		queue = 1;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	zip_dbg("CPU Core: %d Queue number:%d", raw_smp_processor_id(), queue);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/* Take cmd buffer lock */
98*4882a593Smuzhiyun 	spin_lock(&zip_dev->iq[queue].lock);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	/*
101*4882a593Smuzhiyun 	 * Command Queue implementation
102*4882a593Smuzhiyun 	 * 1. If there is place for new instructions, push the cmd at sw_head.
103*4882a593Smuzhiyun 	 * 2. If there is place for exactly one instruction, push the new cmd
104*4882a593Smuzhiyun 	 *    at the sw_head. Make sw_head point to the sw_tail to make it
105*4882a593Smuzhiyun 	 *    circular. Write sw_head's physical address to the "Next-Chunk
106*4882a593Smuzhiyun 	 *    Buffer Ptr" to make it cmd_hw_tail.
107*4882a593Smuzhiyun 	 * 3. Ring the door bell.
108*4882a593Smuzhiyun 	 */
109*4882a593Smuzhiyun 	zip_dbg("sw_head : %lx", zip_dev->iq[queue].sw_head);
110*4882a593Smuzhiyun 	zip_dbg("sw_tail : %lx", zip_dev->iq[queue].sw_tail);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	consumed = zip_cmd_queue_consumed(zip_dev, queue);
113*4882a593Smuzhiyun 	/* Check if there is space to push just one cmd */
114*4882a593Smuzhiyun 	if ((consumed + 128) == (ZIP_CMD_QBUF_SIZE - 8)) {
115*4882a593Smuzhiyun 		zip_dbg("Cmd queue space available for single command");
116*4882a593Smuzhiyun 		/* Space for one cmd, pust it and make it circular queue */
117*4882a593Smuzhiyun 		memcpy((u8 *)zip_dev->iq[queue].sw_head, (u8 *)instr,
118*4882a593Smuzhiyun 		       sizeof(union zip_inst_s));
119*4882a593Smuzhiyun 		zip_dev->iq[queue].sw_head += 16; /* 16 64_bit words = 128B */
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 		/* Now, point the "Next-Chunk Buffer Ptr" to sw_head */
122*4882a593Smuzhiyun 		ncb_ptr = zip_dev->iq[queue].sw_head;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 		zip_dbg("ncb addr :0x%lx sw_head addr :0x%lx",
125*4882a593Smuzhiyun 			ncb_ptr, zip_dev->iq[queue].sw_head - 16);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		/* Using Circular command queue */
128*4882a593Smuzhiyun 		zip_dev->iq[queue].sw_head = zip_dev->iq[queue].sw_tail;
129*4882a593Smuzhiyun 		/* Mark this buffer for free */
130*4882a593Smuzhiyun 		zip_dev->iq[queue].free_flag = 1;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		/* Write new chunk buffer address at "Next-Chunk Buffer Ptr" */
133*4882a593Smuzhiyun 		ncp.u_reg64 = 0ull;
134*4882a593Smuzhiyun 		ncp.s.addr = __pa(zip_dev->iq[queue].sw_head);
135*4882a593Smuzhiyun 		*ncb_ptr = ncp.u_reg64;
136*4882a593Smuzhiyun 		zip_dbg("*ncb_ptr :0x%lx sw_head[phys] :0x%lx",
137*4882a593Smuzhiyun 			*ncb_ptr, __pa(zip_dev->iq[queue].sw_head));
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 		zip_dev->iq[queue].pend_cnt++;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	} else {
142*4882a593Smuzhiyun 		zip_dbg("Enough space is available for commands");
143*4882a593Smuzhiyun 		/* Push this cmd to cmd queue buffer */
144*4882a593Smuzhiyun 		memcpy((u8 *)zip_dev->iq[queue].sw_head, (u8 *)instr,
145*4882a593Smuzhiyun 		       sizeof(union zip_inst_s));
146*4882a593Smuzhiyun 		zip_dev->iq[queue].sw_head += 16; /* 16 64_bit words = 128B */
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 		zip_dev->iq[queue].pend_cnt++;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 	zip_dbg("sw_head :0x%lx sw_tail :0x%lx hw_tail :0x%lx",
151*4882a593Smuzhiyun 		zip_dev->iq[queue].sw_head, zip_dev->iq[queue].sw_tail,
152*4882a593Smuzhiyun 		zip_dev->iq[queue].hw_tail);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	zip_dbg(" Pushed the new cmd : pend_cnt : %d",
155*4882a593Smuzhiyun 		zip_dev->iq[queue].pend_cnt);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	/* Ring the doorbell */
158*4882a593Smuzhiyun 	dbell.u_reg64     = 0ull;
159*4882a593Smuzhiyun 	dbell.s.dbell_cnt = 1;
160*4882a593Smuzhiyun 	zip_reg_write(dbell.u_reg64,
161*4882a593Smuzhiyun 		      (zip_dev->reg_base + ZIP_QUEX_DOORBELL(queue)));
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/* Unlock cmd buffer lock */
164*4882a593Smuzhiyun 	spin_unlock(&zip_dev->iq[queue].lock);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	return queue;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /**
170*4882a593Smuzhiyun  * zip_update_cmd_bufs - Updates the queue statistics after posting the
171*4882a593Smuzhiyun  *                       instruction
172*4882a593Smuzhiyun  * @zip_dev: Pointer to zip device structure
173*4882a593Smuzhiyun  * @queue:   Queue number
174*4882a593Smuzhiyun  */
zip_update_cmd_bufs(struct zip_device * zip_dev,u32 queue)175*4882a593Smuzhiyun void zip_update_cmd_bufs(struct zip_device *zip_dev, u32 queue)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	/* Take cmd buffer lock */
178*4882a593Smuzhiyun 	spin_lock(&zip_dev->iq[queue].lock);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	/* Check if the previous buffer can be freed */
181*4882a593Smuzhiyun 	if (zip_dev->iq[queue].free_flag == 1) {
182*4882a593Smuzhiyun 		zip_dbg("Free flag. Free cmd buffer, adjust sw head and tail");
183*4882a593Smuzhiyun 		/* Reset the free flag */
184*4882a593Smuzhiyun 		zip_dev->iq[queue].free_flag = 0;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		/* Point the hw_tail to start of the new chunk buffer */
187*4882a593Smuzhiyun 		zip_dev->iq[queue].hw_tail = zip_dev->iq[queue].sw_head;
188*4882a593Smuzhiyun 	} else {
189*4882a593Smuzhiyun 		zip_dbg("Free flag not set. increment hw tail");
190*4882a593Smuzhiyun 		zip_dev->iq[queue].hw_tail += 16; /* 16 64_bit words = 128B */
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	zip_dev->iq[queue].done_cnt++;
194*4882a593Smuzhiyun 	zip_dev->iq[queue].pend_cnt--;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	zip_dbg("sw_head :0x%lx sw_tail :0x%lx hw_tail :0x%lx",
197*4882a593Smuzhiyun 		zip_dev->iq[queue].sw_head, zip_dev->iq[queue].sw_tail,
198*4882a593Smuzhiyun 		zip_dev->iq[queue].hw_tail);
199*4882a593Smuzhiyun 	zip_dbg(" Got CC : pend_cnt : %d\n", zip_dev->iq[queue].pend_cnt);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	spin_unlock(&zip_dev->iq[queue].lock);
202*4882a593Smuzhiyun }
203