xref: /OK3568_Linux_fs/kernel/drivers/crypto/cavium/zip/zip_mem.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 <linux/types.h>
47*4882a593Smuzhiyun #include <linux/vmalloc.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include "common.h"
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /**
52*4882a593Smuzhiyun  * zip_cmd_qbuf_alloc - Allocates a cmd buffer for ZIP Instruction Queue
53*4882a593Smuzhiyun  * @zip: Pointer to zip device structure
54*4882a593Smuzhiyun  * @q:   Queue number to allocate bufffer to
55*4882a593Smuzhiyun  * Return: 0 if successful, -ENOMEM otherwise
56*4882a593Smuzhiyun  */
zip_cmd_qbuf_alloc(struct zip_device * zip,int q)57*4882a593Smuzhiyun int zip_cmd_qbuf_alloc(struct zip_device *zip, int q)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	zip->iq[q].sw_head = (u64 *)__get_free_pages((GFP_KERNEL | GFP_DMA),
60*4882a593Smuzhiyun 						get_order(ZIP_CMD_QBUF_SIZE));
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (!zip->iq[q].sw_head)
63*4882a593Smuzhiyun 		return -ENOMEM;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	memset(zip->iq[q].sw_head, 0, ZIP_CMD_QBUF_SIZE);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	zip_dbg("cmd_qbuf_alloc[%d] Success : %p\n", q, zip->iq[q].sw_head);
68*4882a593Smuzhiyun 	return 0;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun  * zip_cmd_qbuf_free - Frees the cmd Queue buffer
73*4882a593Smuzhiyun  * @zip: Pointer to zip device structure
74*4882a593Smuzhiyun  * @q:   Queue number to free buffer of
75*4882a593Smuzhiyun  */
zip_cmd_qbuf_free(struct zip_device * zip,int q)76*4882a593Smuzhiyun void zip_cmd_qbuf_free(struct zip_device *zip, int q)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	zip_dbg("Freeing cmd_qbuf 0x%lx\n", zip->iq[q].sw_tail);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	free_pages((u64)zip->iq[q].sw_tail, get_order(ZIP_CMD_QBUF_SIZE));
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun  * zip_data_buf_alloc - Allocates memory for a data bufffer
85*4882a593Smuzhiyun  * @size:   Size of the buffer to allocate
86*4882a593Smuzhiyun  * Returns: Pointer to the buffer allocated
87*4882a593Smuzhiyun  */
zip_data_buf_alloc(u64 size)88*4882a593Smuzhiyun u8 *zip_data_buf_alloc(u64 size)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	u8 *ptr;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	ptr = (u8 *)__get_free_pages((GFP_KERNEL | GFP_DMA),
93*4882a593Smuzhiyun 					get_order(size));
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (!ptr)
96*4882a593Smuzhiyun 		return NULL;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	memset(ptr, 0, size);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	zip_dbg("Data buffer allocation success\n");
101*4882a593Smuzhiyun 	return ptr;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun  * zip_data_buf_free - Frees the memory of a data buffer
106*4882a593Smuzhiyun  * @ptr:  Pointer to the buffer
107*4882a593Smuzhiyun  * @size: Buffer size
108*4882a593Smuzhiyun  */
zip_data_buf_free(u8 * ptr,u64 size)109*4882a593Smuzhiyun void zip_data_buf_free(u8 *ptr, u64 size)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	zip_dbg("Freeing data buffer 0x%lx\n", ptr);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	free_pages((u64)ptr, get_order(size));
114*4882a593Smuzhiyun }
115