1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 /**
27  * This file defines external dependencies of Display Core.
28  */
29 
30 #ifndef __DM_SERVICES_H__
31 
32 #define __DM_SERVICES_H__
33 
34 #include "amdgpu_dm_trace.h"
35 
36 /* TODO: remove when DC is complete. */
37 #include "dm_services_types.h"
38 #include "logger_interface.h"
39 #include "link_service_types.h"
40 
41 #undef DEPRECATED
42 
43 irq_handler_idx dm_register_interrupt(
44 	struct dc_context *ctx,
45 	struct dc_interrupt_params *int_params,
46 	interrupt_handler ih,
47 	void *handler_args);
48 
49 
50 /*
51  *
52  * GPU registers access
53  *
54  */
55 
56 /* enable for debugging new code, this adds 50k to the driver size. */
57 /* #define DM_CHECK_ADDR_0 */
58 
59 #define dm_read_reg(ctx, address)	\
60 		dm_read_reg_func(ctx, address, __func__)
61 
62 static inline uint32_t dm_read_reg_func(
63 	const struct dc_context *ctx,
64 	uint32_t address,
65 	const char *func_name)
66 {
67 	uint32_t value;
68 #ifdef DM_CHECK_ADDR_0
69 	if (address == 0) {
70 		DC_ERR("invalid register read; address = 0\n");
71 		return 0;
72 	}
73 #endif
74 	value = cgs_read_register(ctx->cgs_device, address);
75 	trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
76 
77 	return value;
78 }
79 
80 #define dm_write_reg(ctx, address, value)	\
81 	dm_write_reg_func(ctx, address, value, __func__)
82 
83 static inline void dm_write_reg_func(
84 	const struct dc_context *ctx,
85 	uint32_t address,
86 	uint32_t value,
87 	const char *func_name)
88 {
89 #ifdef DM_CHECK_ADDR_0
90 	if (address == 0) {
91 		DC_ERR("invalid register write. address = 0");
92 		return;
93 	}
94 #endif
95 	cgs_write_register(ctx->cgs_device, address, value);
96 	trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
97 }
98 
99 static inline uint32_t dm_read_index_reg(
100 	const struct dc_context *ctx,
101 	enum cgs_ind_reg addr_space,
102 	uint32_t index)
103 {
104 	return cgs_read_ind_register(ctx->cgs_device, addr_space, index);
105 }
106 
107 static inline void dm_write_index_reg(
108 	const struct dc_context *ctx,
109 	enum cgs_ind_reg addr_space,
110 	uint32_t index,
111 	uint32_t value)
112 {
113 	cgs_write_ind_register(ctx->cgs_device, addr_space, index, value);
114 }
115 
116 static inline uint32_t get_reg_field_value_ex(
117 	uint32_t reg_value,
118 	uint32_t mask,
119 	uint8_t shift)
120 {
121 	return (mask & reg_value) >> shift;
122 }
123 
124 #define get_reg_field_value(reg_value, reg_name, reg_field)\
125 	get_reg_field_value_ex(\
126 		(reg_value),\
127 		reg_name ## __ ## reg_field ## _MASK,\
128 		reg_name ## __ ## reg_field ## __SHIFT)
129 
130 static inline uint32_t set_reg_field_value_ex(
131 	uint32_t reg_value,
132 	uint32_t value,
133 	uint32_t mask,
134 	uint8_t shift)
135 {
136 	ASSERT(mask != 0);
137 	return (reg_value & ~mask) | (mask & (value << shift));
138 }
139 
140 #define set_reg_field_value(reg_value, value, reg_name, reg_field)\
141 	(reg_value) = set_reg_field_value_ex(\
142 		(reg_value),\
143 		(value),\
144 		reg_name ## __ ## reg_field ## _MASK,\
145 		reg_name ## __ ## reg_field ## __SHIFT)
146 
147 uint32_t generic_reg_set_ex(const struct dc_context *ctx,
148 		uint32_t addr, uint32_t reg_val, int n,
149 		uint8_t shift1, uint32_t mask1, uint32_t field_value1, ...);
150 
151 uint32_t generic_reg_update_ex(const struct dc_context *ctx,
152 		uint32_t addr, int n,
153 		uint8_t shift1, uint32_t mask1, uint32_t field_value1, ...);
154 
155 #define FD(reg_field)	reg_field ## __SHIFT, \
156 						reg_field ## _MASK
157 
158 /*
159  * return number of poll before condition is met
160  * return 0 if condition is not meet after specified time out tries
161  */
162 unsigned int generic_reg_wait(const struct dc_context *ctx,
163 	uint32_t addr, uint32_t mask, uint32_t shift, uint32_t condition_value,
164 	unsigned int delay_between_poll_us, unsigned int time_out_num_tries,
165 	const char *func_name, int line);
166 
167 
168 /* These macros need to be used with soc15 registers in order to retrieve
169  * the actual offset.
170  */
171 #define dm_write_reg_soc15(ctx, reg, inst_offset, value)	\
172 		dm_write_reg_func(ctx, reg + DCE_BASE.instance[0].segment[reg##_BASE_IDX] + inst_offset, value, __func__)
173 
174 #define dm_read_reg_soc15(ctx, reg, inst_offset)	\
175 		dm_read_reg_func(ctx, reg + DCE_BASE.instance[0].segment[reg##_BASE_IDX] + inst_offset, __func__)
176 
177 #define generic_reg_update_soc15(ctx, inst_offset, reg_name, n, ...)\
178 		generic_reg_update_ex(ctx, DCE_BASE.instance[0].segment[mm##reg_name##_BASE_IDX] +  mm##reg_name + inst_offset, \
179 		n, __VA_ARGS__)
180 
181 #define generic_reg_set_soc15(ctx, inst_offset, reg_name, n, ...)\
182 		generic_reg_set_ex(ctx, DCE_BASE.instance[0].segment[mm##reg_name##_BASE_IDX] + mm##reg_name + inst_offset, 0, \
183 		n, __VA_ARGS__)
184 
185 #define get_reg_field_value_soc15(reg_value, block, reg_num, reg_name, reg_field)\
186 	get_reg_field_value_ex(\
187 		(reg_value),\
188 		block ## reg_num ## _ ## reg_name ## __ ## reg_field ## _MASK,\
189 		block ## reg_num ## _ ## reg_name ## __ ## reg_field ## __SHIFT)
190 
191 #define set_reg_field_value_soc15(reg_value, value, block, reg_num, reg_name, reg_field)\
192 	(reg_value) = set_reg_field_value_ex(\
193 		(reg_value),\
194 		(value),\
195 		block ## reg_num ## _ ## reg_name ## __ ## reg_field ## _MASK,\
196 		block ## reg_num ## _ ## reg_name ## __ ## reg_field ## __SHIFT)
197 
198 /**************************************
199  * Power Play (PP) interfaces
200  **************************************/
201 
202 /* Gets valid clocks levels from pplib
203  *
204  * input: clk_type - display clk / sclk / mem clk
205  *
206  * output: array of valid clock levels for given type in ascending order,
207  * with invalid levels filtered out
208  *
209  */
210 bool dm_pp_get_clock_levels_by_type(
211 	const struct dc_context *ctx,
212 	enum dm_pp_clock_type clk_type,
213 	struct dm_pp_clock_levels *clk_level_info);
214 
215 bool dm_pp_get_clock_levels_by_type_with_latency(
216 	const struct dc_context *ctx,
217 	enum dm_pp_clock_type clk_type,
218 	struct dm_pp_clock_levels_with_latency *clk_level_info);
219 
220 bool dm_pp_get_clock_levels_by_type_with_voltage(
221 	const struct dc_context *ctx,
222 	enum dm_pp_clock_type clk_type,
223 	struct dm_pp_clock_levels_with_voltage *clk_level_info);
224 
225 bool dm_pp_notify_wm_clock_changes(
226 	const struct dc_context *ctx,
227 	struct dm_pp_wm_sets_with_clock_ranges *wm_with_clock_ranges);
228 
229 void dm_pp_get_funcs(struct dc_context *ctx,
230 		struct pp_smu_funcs *funcs);
231 
232 /* DAL calls this function to notify PP about completion of Mode Set.
233  * For PP it means that current DCE clocks are those which were returned
234  * by dc_service_pp_pre_dce_clock_change(), in the 'output' parameter.
235  *
236  * If the clocks are higher than before, then PP does nothing.
237  *
238  * If the clocks are lower than before, then PP reduces the voltage.
239  *
240  * \returns	true - call is successful
241  *		false - call failed
242  */
243 bool dm_pp_apply_display_requirements(
244 	const struct dc_context *ctx,
245 	const struct dm_pp_display_configuration *pp_display_cfg);
246 
247 bool dm_pp_apply_power_level_change_request(
248 	const struct dc_context *ctx,
249 	struct dm_pp_power_level_change_request *level_change_req);
250 
251 bool dm_pp_apply_clock_for_voltage_request(
252 	const struct dc_context *ctx,
253 	struct dm_pp_clock_for_voltage_req *clock_for_voltage_req);
254 
255 bool dm_pp_get_static_clocks(
256 	const struct dc_context *ctx,
257 	struct dm_pp_static_clock_info *static_clk_info);
258 
259 /****** end of PP interfaces ******/
260 
261 struct persistent_data_flag {
262 	bool save_per_link;
263 	bool save_per_edid;
264 };
265 
266 /* Call to write data in registry editor for persistent data storage.
267  *
268  * \inputs      sink - identify edid/link for registry folder creation
269  *              module name - identify folders for registry
270  *              key name - identify keys within folders for registry
271  *              params - value to write in defined folder/key
272  *              size - size of the input params
273  *              flag - determine whether to save by link or edid
274  *
275  * \returns     true - call is successful
276  *              false - call failed
277  *
278  * sink         module         key
279  * -----------------------------------------------------------------------------
280  * NULL         NULL           NULL     - failure
281  * NULL         NULL           -        - create key with param value
282  *                                                      under base folder
283  * NULL         -              NULL     - create module folder under base folder
284  * -            NULL           NULL     - failure
285  * NULL         -              -        - create key under module folder
286  *                                            with no edid/link identification
287  * -            NULL           -        - create key with param value
288  *                                                       under base folder
289  * -            -              NULL     - create module folder under base folder
290  * -            -              -        - create key under module folder
291  *                                              with edid/link identification
292  */
293 bool dm_write_persistent_data(struct dc_context *ctx,
294 		const struct dc_sink *sink,
295 		const char *module_name,
296 		const char *key_name,
297 		void *params,
298 		unsigned int size,
299 		struct persistent_data_flag *flag);
300 
301 
302 /* Call to read data in registry editor for persistent data storage.
303  *
304  * \inputs      sink - identify edid/link for registry folder creation
305  *              module name - identify folders for registry
306  *              key name - identify keys within folders for registry
307  *              size - size of the output params
308  *              flag - determine whether it was save by link or edid
309  *
310  * \returns     params - value read from defined folder/key
311  *              true - call is successful
312  *              false - call failed
313  *
314  * sink         module         key
315  * -----------------------------------------------------------------------------
316  * NULL         NULL           NULL     - failure
317  * NULL         NULL           -        - read key under base folder
318  * NULL         -              NULL     - failure
319  * -            NULL           NULL     - failure
320  * NULL         -              -        - read key under module folder
321  *                                             with no edid/link identification
322  * -            NULL           -        - read key under base folder
323  * -            -              NULL     - failure
324  * -            -              -        - read key under module folder
325  *                                              with edid/link identification
326  */
327 bool dm_read_persistent_data(struct dc_context *ctx,
328 		const struct dc_sink *sink,
329 		const char *module_name,
330 		const char *key_name,
331 		void *params,
332 		unsigned int size,
333 		struct persistent_data_flag *flag);
334 
335 bool dm_query_extended_brightness_caps
336 	(struct dc_context *ctx, enum dm_acpi_display_type display,
337 			struct dm_acpi_atif_backlight_caps *pCaps);
338 
339 bool dm_dmcu_set_pipe(struct dc_context *ctx, unsigned int controller_id);
340 
341 /*
342  *
343  * print-out services
344  *
345  */
346 #define dm_log_to_buffer(buffer, size, fmt, args)\
347 	vsnprintf(buffer, size, fmt, args)
348 
349 static inline unsigned long long dm_get_timestamp(struct dc_context *ctx)
350 {
351 	return ktime_get_raw_ns();
352 }
353 
354 unsigned long long dm_get_elapse_time_in_ns(struct dc_context *ctx,
355 		unsigned long long current_time_stamp,
356 		unsigned long long last_time_stamp);
357 
358 /*
359  * performance tracing
360  */
361 #define PERF_TRACE()	trace_amdgpu_dc_performance(CTX->perf_trace->read_count,\
362 		CTX->perf_trace->write_count, &CTX->perf_trace->last_entry_read,\
363 		&CTX->perf_trace->last_entry_write, __func__, __LINE__)
364 #define PERF_TRACE_CTX(__CTX)	trace_amdgpu_dc_performance(__CTX->perf_trace->read_count,\
365 		__CTX->perf_trace->write_count, &__CTX->perf_trace->last_entry_read,\
366 		&__CTX->perf_trace->last_entry_write, __func__, __LINE__)
367 
368 
369 /*
370  * Debug and verification hooks
371  */
372 
373 void dm_dtn_log_begin(struct dc_context *ctx,
374 	struct dc_log_buffer_ctx *log_ctx);
375 void dm_dtn_log_append_v(struct dc_context *ctx,
376 	struct dc_log_buffer_ctx *log_ctx,
377 	const char *msg, ...);
378 void dm_dtn_log_end(struct dc_context *ctx,
379 	struct dc_log_buffer_ctx *log_ctx);
380 
381 #endif /* __DM_SERVICES_H__ */
382