xref: /linux/drivers/crypto/cavium/zip/zip_mem.c (revision a976c2951d8f376112361830aa7762beff83a205)
1 /***********************license start************************************
2  * Copyright (c) 2003-2017 Cavium, Inc.
3  * All rights reserved.
4  *
5  * License: one of 'Cavium License' or 'GNU General Public License Version 2'
6  *
7  * This file is provided under the terms of the Cavium License (see below)
8  * or under the terms of GNU General Public License, Version 2, as
9  * published by the Free Software Foundation. When using or redistributing
10  * this file, you may do so under either license.
11  *
12  * Cavium License:  Redistribution and use in source and binary forms, with
13  * or without modification, are permitted provided that the following
14  * conditions are met:
15  *
16  *  * Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  *
19  *  * Redistributions in binary form must reproduce the above
20  *    copyright notice, this list of conditions and the following
21  *    disclaimer in the documentation and/or other materials provided
22  *    with the distribution.
23  *
24  *  * Neither the name of Cavium Inc. nor the names of its contributors may be
25  *    used to endorse or promote products derived from this software without
26  *    specific prior written permission.
27  *
28  * This Software, including technical data, may be subject to U.S. export
29  * control laws, including the U.S. Export Administration Act and its
30  * associated regulations, and may be subject to export or import
31  * regulations in other countries.
32  *
33  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
34  * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
35  * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
36  * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
37  * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
38  * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
39  * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
40  * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
41  * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
42  * ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
43  * WITH YOU.
44  ***********************license end**************************************/
45 
46 #include <linux/types.h>
47 #include <linux/vmalloc.h>
48 
49 #include "common.h"
50 
51 /**
52  * zip_cmd_qbuf_alloc - Allocates a cmd buffer for ZIP Instruction Queue
53  * @zip: Pointer to zip device structure
54  * @q:   Queue number to allocate bufffer to
55  * Return: 0 if successful, -ENOMEM otherwise
56  */
zip_cmd_qbuf_alloc(struct zip_device * zip,int q)57 int zip_cmd_qbuf_alloc(struct zip_device *zip, int q)
58 {
59 	zip->iq[q].sw_head = (u64 *)__get_free_pages((GFP_KERNEL | GFP_DMA),
60 						get_order(ZIP_CMD_QBUF_SIZE));
61 
62 	if (!zip->iq[q].sw_head)
63 		return -ENOMEM;
64 
65 	memset(zip->iq[q].sw_head, 0, ZIP_CMD_QBUF_SIZE);
66 
67 	zip_dbg("cmd_qbuf_alloc[%d] Success : %p\n", q, zip->iq[q].sw_head);
68 	return 0;
69 }
70 
71 /**
72  * zip_cmd_qbuf_free - Frees the cmd Queue buffer
73  * @zip: Pointer to zip device structure
74  * @q:   Queue number to free buffer of
75  */
zip_cmd_qbuf_free(struct zip_device * zip,int q)76 void zip_cmd_qbuf_free(struct zip_device *zip, int q)
77 {
78 	zip_dbg("Freeing cmd_qbuf 0x%lx\n", zip->iq[q].sw_tail);
79 
80 	free_pages((u64)zip->iq[q].sw_tail, get_order(ZIP_CMD_QBUF_SIZE));
81 }
82 
83 /**
84  * zip_data_buf_alloc - Allocates memory for a data bufffer
85  * @size:   Size of the buffer to allocate
86  * Returns: Pointer to the buffer allocated
87  */
zip_data_buf_alloc(u64 size)88 u8 *zip_data_buf_alloc(u64 size)
89 {
90 	u8 *ptr;
91 
92 	ptr = (u8 *)__get_free_pages((GFP_KERNEL | GFP_DMA),
93 					get_order(size));
94 
95 	if (!ptr)
96 		return NULL;
97 
98 	memset(ptr, 0, size);
99 
100 	zip_dbg("Data buffer allocation success\n");
101 	return ptr;
102 }
103 
104 /**
105  * zip_data_buf_free - Frees the memory of a data buffer
106  * @ptr:  Pointer to the buffer
107  * @size: Buffer size
108  */
zip_data_buf_free(u8 * ptr,u64 size)109 void zip_data_buf_free(u8 *ptr, u64 size)
110 {
111 	zip_dbg("Freeing data buffer 0x%lx\n", ptr);
112 
113 	free_pages((u64)ptr, get_order(size));
114 }
115