xref: /linux/drivers/block/zram/zcomp.h (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef _ZCOMP_H_
4 #define _ZCOMP_H_
5 
6 #include <linux/local_lock.h>
7 
8 #define ZCOMP_PARAM_NO_LEVEL	INT_MIN
9 
10 /*
11  * Immutable driver (backend) parameters. The driver may attach private
12  * data to it (e.g. driver representation of the dictionary, etc.).
13  *
14  * This data is kept per-comp and is shared among execution contexts.
15  */
16 struct zcomp_params {
17 	void *dict;
18 	size_t dict_sz;
19 	s32 level;
20 
21 	void *drv_data;
22 };
23 
24 /*
25  * Run-time driver context - scratch buffers, etc. It is modified during
26  * request execution (compression/decompression), cannot be shared, so
27  * it's in per-CPU area.
28  */
29 struct zcomp_ctx {
30 	void *context;
31 };
32 
33 struct zcomp_strm {
34 	local_lock_t lock;
35 	/* compression buffer */
36 	void *buffer;
37 	struct zcomp_ctx ctx;
38 };
39 
40 struct zcomp_req {
41 	const unsigned char *src;
42 	const size_t src_len;
43 
44 	unsigned char *dst;
45 	size_t dst_len;
46 };
47 
48 struct zcomp_ops {
49 	int (*compress)(struct zcomp_params *params, struct zcomp_ctx *ctx,
50 			struct zcomp_req *req);
51 	int (*decompress)(struct zcomp_params *params, struct zcomp_ctx *ctx,
52 			  struct zcomp_req *req);
53 
54 	int (*create_ctx)(struct zcomp_params *params, struct zcomp_ctx *ctx);
55 	void (*destroy_ctx)(struct zcomp_ctx *ctx);
56 
57 	int (*setup_params)(struct zcomp_params *params);
58 	void (*release_params)(struct zcomp_params *params);
59 
60 	const char *name;
61 };
62 
63 /* dynamic per-device compression frontend */
64 struct zcomp {
65 	struct zcomp_strm __percpu *stream;
66 	const struct zcomp_ops *ops;
67 	struct zcomp_params *params;
68 	struct hlist_node node;
69 };
70 
71 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node);
72 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node);
73 ssize_t zcomp_available_show(const char *comp, char *buf);
74 bool zcomp_available_algorithm(const char *comp);
75 
76 struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params);
77 void zcomp_destroy(struct zcomp *comp);
78 
79 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp);
80 void zcomp_stream_put(struct zcomp *comp);
81 
82 int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
83 		   const void *src, unsigned int *dst_len);
84 int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm,
85 		     const void *src, unsigned int src_len, void *dst);
86 
87 #endif /* _ZCOMP_H_ */
88