1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Support utilities for cs_dsp testing.
4  *
5  * Copyright (C) 2024 Cirrus Logic, Inc. and
6  *                    Cirrus Logic International Semiconductor Ltd.
7  */
8 
9 #include <linux/regmap.h>
10 #include <linux/firmware/cirrus/wmfw.h>
11 
12 struct kunit;
13 struct cs_dsp_test;
14 struct cs_dsp_test_local;
15 
16 /**
17  * struct cs_dsp_test - base class for test utilities
18  *
19  * @test:	Pointer to struct kunit instance.
20  * @dsp:	Pointer to struct cs_dsp instance.
21  * @local:	Private data for each test suite.
22  */
23 struct cs_dsp_test {
24 	struct kunit *test;
25 	struct cs_dsp *dsp;
26 
27 	struct cs_dsp_test_local *local;
28 
29 	/* Following members are private */
30 	bool saw_bus_write;
31 };
32 
33 /**
34  * struct cs_dsp_mock_alg_def - Info for creating a mock algorithm entry.
35  *
36  * @id		  Algorithm ID.
37  * @ver;	  Algorithm version.
38  * @xm_base_words XM base address in DSP words.
39  * @xm_size_words XM size in DSP words.
40  * @ym_base_words YM base address in DSP words.
41  * @ym_size_words YM size in DSP words.
42  * @zm_base_words ZM base address in DSP words.
43  * @zm_size_words ZM size in DSP words.
44  */
45 struct cs_dsp_mock_alg_def {
46 	unsigned int id;
47 	unsigned int ver;
48 	unsigned int xm_base_words;
49 	unsigned int xm_size_words;
50 	unsigned int ym_base_words;
51 	unsigned int ym_size_words;
52 	unsigned int zm_base_words;
53 	unsigned int zm_size_words;
54 };
55 
56 struct cs_dsp_mock_coeff_def {
57 	const char *shortname;
58 	const char *fullname;
59 	const char *description;
60 	u16 type;
61 	u16 flags;
62 	u16 mem_type;
63 	unsigned int offset_dsp_words;
64 	unsigned int length_bytes;
65 };
66 
67 /**
68  * struct cs_dsp_mock_xm_header - XM header builder
69  *
70  * @test_priv:	     Pointer to the struct cs_dsp_test.
71  * @blob_data:	     Pointer to the created blob data.
72  * @blob_size_bytes: Size of the data at blob_data.
73  */
74 struct cs_dsp_mock_xm_header {
75 	struct cs_dsp_test *test_priv;
76 	void *blob_data;
77 	size_t blob_size_bytes;
78 };
79 
80 struct cs_dsp_mock_wmfw_builder;
81 struct cs_dsp_mock_bin_builder;
82 
83 extern const unsigned int cs_dsp_mock_adsp2_32bit_sysbase;
84 extern const unsigned int cs_dsp_mock_adsp2_16bit_sysbase;
85 extern const unsigned int cs_dsp_mock_halo_core_base;
86 extern const unsigned int cs_dsp_mock_halo_sysinfo_base;
87 
88 extern const struct cs_dsp_region cs_dsp_mock_halo_dsp1_regions[];
89 extern const unsigned int cs_dsp_mock_halo_dsp1_region_sizes[];
90 extern const struct cs_dsp_region cs_dsp_mock_adsp2_32bit_dsp1_regions[];
91 extern const unsigned int cs_dsp_mock_adsp2_32bit_dsp1_region_sizes[];
92 extern const struct cs_dsp_region cs_dsp_mock_adsp2_16bit_dsp1_regions[];
93 extern const unsigned int cs_dsp_mock_adsp2_16bit_dsp1_region_sizes[];
94 int cs_dsp_mock_count_regions(const unsigned int *region_sizes);
95 unsigned int cs_dsp_mock_size_of_region(const struct cs_dsp *dsp, int mem_type);
96 unsigned int cs_dsp_mock_base_addr_for_mem(struct cs_dsp_test *priv, int mem_type);
97 unsigned int cs_dsp_mock_reg_addr_inc_per_unpacked_word(struct cs_dsp_test *priv);
98 unsigned int cs_dsp_mock_reg_block_length_bytes(struct cs_dsp_test *priv, int mem_type);
99 unsigned int cs_dsp_mock_reg_block_length_registers(struct cs_dsp_test *priv, int mem_type);
100 unsigned int cs_dsp_mock_reg_block_length_dsp_words(struct cs_dsp_test *priv, int mem_type);
101 bool cs_dsp_mock_has_zm(struct cs_dsp_test *priv);
102 int cs_dsp_mock_packed_to_unpacked_mem_type(int packed_mem_type);
103 unsigned int cs_dsp_mock_num_dsp_words_to_num_packed_regs(unsigned int num_dsp_words);
104 unsigned int cs_dsp_mock_xm_header_get_alg_base_in_words(struct cs_dsp_test *priv,
105 							 unsigned int alg_id,
106 							 int mem_type);
107 unsigned int cs_dsp_mock_xm_header_get_fw_version(struct cs_dsp_mock_xm_header *header);
108 void cs_dsp_mock_xm_header_drop_from_regmap_cache(struct cs_dsp_test *priv);
109 int cs_dsp_mock_xm_header_write_to_regmap(struct cs_dsp_mock_xm_header *header);
110 struct cs_dsp_mock_xm_header *cs_dsp_create_mock_xm_header(struct cs_dsp_test *priv,
111 							   const struct cs_dsp_mock_alg_def *algs,
112 							   size_t num_algs);
113 
114 int cs_dsp_mock_regmap_init(struct cs_dsp_test *priv);
115 void cs_dsp_mock_regmap_drop_range(struct cs_dsp_test *priv,
116 				   unsigned int first_reg, unsigned int last_reg);
117 void cs_dsp_mock_regmap_drop_regs(struct cs_dsp_test *priv,
118 				  unsigned int first_reg, size_t num_regs);
119 void cs_dsp_mock_regmap_drop_bytes(struct cs_dsp_test *priv,
120 				   unsigned int first_reg, size_t num_bytes);
121 void cs_dsp_mock_regmap_drop_system_regs(struct cs_dsp_test *priv);
122 bool cs_dsp_mock_regmap_is_dirty(struct cs_dsp_test *priv, bool drop_system_regs);
123 
124 struct cs_dsp_mock_bin_builder *cs_dsp_mock_bin_init(struct cs_dsp_test *priv,
125 						     int format_version,
126 						     unsigned int fw_version);
127 void cs_dsp_mock_bin_add_raw_block(struct cs_dsp_mock_bin_builder *builder,
128 				   unsigned int alg_id, unsigned int alg_ver,
129 				   int type, unsigned int offset,
130 				   const void *payload_data, size_t payload_len_bytes);
131 void cs_dsp_mock_bin_add_info(struct cs_dsp_mock_bin_builder *builder,
132 			      const char *info);
133 void cs_dsp_mock_bin_add_name(struct cs_dsp_mock_bin_builder *builder,
134 			      const char *name);
135 void cs_dsp_mock_bin_add_patch(struct cs_dsp_mock_bin_builder *builder,
136 			       unsigned int alg_id, unsigned int alg_ver,
137 			       int mem_region, unsigned int reg_addr_offset,
138 			       const void *payload_data, size_t payload_len_bytes);
139 struct firmware *cs_dsp_mock_bin_get_firmware(struct cs_dsp_mock_bin_builder *builder);
140 
141 struct cs_dsp_mock_wmfw_builder *cs_dsp_mock_wmfw_init(struct cs_dsp_test *priv,
142 						       int format_version);
143 void cs_dsp_mock_wmfw_add_raw_block(struct cs_dsp_mock_wmfw_builder *builder,
144 				    int mem_region, unsigned int mem_offset_dsp_words,
145 				    const void *payload_data, size_t payload_len_bytes);
146 void cs_dsp_mock_wmfw_add_info(struct cs_dsp_mock_wmfw_builder *builder,
147 			       const char *info);
148 void cs_dsp_mock_wmfw_add_data_block(struct cs_dsp_mock_wmfw_builder *builder,
149 				     int mem_region, unsigned int mem_offset_dsp_words,
150 				     const void *payload_data, size_t payload_len_bytes);
151 void cs_dsp_mock_wmfw_start_alg_info_block(struct cs_dsp_mock_wmfw_builder *builder,
152 					   unsigned int alg_id,
153 					   const char *name,
154 					   const char *description);
155 void cs_dsp_mock_wmfw_add_coeff_desc(struct cs_dsp_mock_wmfw_builder *builder,
156 				     const struct cs_dsp_mock_coeff_def *def);
157 void cs_dsp_mock_wmfw_end_alg_info_block(struct cs_dsp_mock_wmfw_builder *builder);
158 struct firmware *cs_dsp_mock_wmfw_get_firmware(struct cs_dsp_mock_wmfw_builder *builder);
159 int cs_dsp_mock_wmfw_format_version(struct cs_dsp_mock_wmfw_builder *builder);
160