xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc_surface.c (revision 1fc5a74b108fc90951890ec513ac81869f5eaff1)
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 /* DC interface (public) */
27 #include "dm_services.h"
28 #include "dc.h"
29 
30 /* DC core (private) */
31 #include "core_types.h"
32 #include "transform.h"
33 #include "dpp.h"
34 
35 #include "dc_plane_priv.h"
36 
37 /*******************************************************************************
38  * Private functions
39  ******************************************************************************/
dc_plane_construct(struct dc_context * ctx,struct dc_plane_state * plane_state)40 void dc_plane_construct(struct dc_context *ctx, struct dc_plane_state *plane_state)
41 {
42 	plane_state->ctx = ctx;
43 
44 	plane_state->gamma_correction.is_identity = true;
45 
46 	plane_state->in_transfer_func.type = TF_TYPE_BYPASS;
47 
48 	plane_state->pre_multiplied_alpha = true;
49 
50 	/* CM */
51 	plane_state->cm.shaper_func.type = TF_TYPE_BYPASS;
52 	plane_state->cm.blend_func.type = TF_TYPE_BYPASS;
53 	plane_state->cm.lut3d_func.state.raw = 0;
54 	plane_state->cm.flags.all = 0;
55 }
56 
dc_plane_destruct(struct dc_plane_state * plane_state)57 void dc_plane_destruct(struct dc_plane_state *plane_state)
58 {
59 	(void)plane_state;
60 	// no more pointers to free within dc_plane_state
61 }
62 
63 
64 /* dc_state is passed in separately since it may differ from the current dc state accessible from plane_state e.g.
65  * if the driver is doing an update from an old context to a new one and the caller wants the pipe mask for the new
66  * context rather than the existing one
67  */
dc_plane_get_pipe_mask(struct dc_state * dc_state,const struct dc_plane_state * plane_state)68 uint8_t  dc_plane_get_pipe_mask(struct dc_state *dc_state, const struct dc_plane_state *plane_state)
69 {
70 	uint8_t pipe_mask = 0;
71 	unsigned int i;
72 
73 	for (i = 0; i < plane_state->ctx->dc->res_pool->pipe_count; i++) {
74 		struct pipe_ctx *pipe_ctx = &dc_state->res_ctx.pipe_ctx[i];
75 
76 		if (pipe_ctx->plane_state == plane_state && pipe_ctx->plane_res.hubp)
77 			pipe_mask |= (uint8_t)(1 << pipe_ctx->plane_res.hubp->inst);
78 	}
79 
80 	return pipe_mask;
81 }
82 
83 /*******************************************************************************
84  * Public functions
85  ******************************************************************************/
dc_create_plane_state(const struct dc * dc)86 struct dc_plane_state *dc_create_plane_state(const struct dc *dc)
87 {
88 	struct dc_plane_state *plane_state = kvzalloc_obj(*plane_state,
89 							  GFP_ATOMIC);
90 
91 	if (NULL == plane_state)
92 		return NULL;
93 
94 	kref_init(&plane_state->refcount);
95 	dc_plane_construct(dc->ctx, plane_state);
96 
97 	return plane_state;
98 }
99 
100 /*
101  *****************************************************************************
102  *  Function: dc_plane_get_status
103  *
104  *  @brief
105  *     Looks up the pipe context of plane_state and updates the pending status
106  *     of the pipe context. Then returns plane_state->status
107  *
108  *  @param [in] plane_state: pointer to the plane_state to get the status of
109  *****************************************************************************
110  */
dc_plane_get_status(const struct dc_plane_state * plane_state,union dc_plane_status_update_flags flags)111 const struct dc_plane_status *dc_plane_get_status(
112 		const struct dc_plane_state *plane_state,
113 		union dc_plane_status_update_flags flags)
114 {
115 	const struct dc_plane_status *plane_status;
116 	struct dc  *dc;
117 	unsigned int i;
118 
119 	if (!plane_state ||
120 		!plane_state->ctx ||
121 		!plane_state->ctx->dc) {
122 		ASSERT(0);
123 		return NULL; /* remove this if above assert never hit */
124 	}
125 
126 	plane_status = &plane_state->status;
127 	dc = plane_state->ctx->dc;
128 
129 	if (dc->current_state == NULL)
130 		return NULL;
131 
132 	/* Find the current plane state and set its pending bit to false */
133 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
134 		struct pipe_ctx *pipe_ctx =
135 				&dc->current_state->res_ctx.pipe_ctx[i];
136 
137 		if (pipe_ctx->plane_state != plane_state)
138 			continue;
139 
140 		if (pipe_ctx->plane_state && flags.bits.address)
141 			pipe_ctx->plane_state->status.is_flip_pending = false;
142 		if (pipe_ctx->plane_state && flags.bits.histogram)
143 			memset(&pipe_ctx->plane_state->status.cm_hist, 0,
144 				sizeof(pipe_ctx->plane_state->status.cm_hist));
145 
146 		break;
147 	}
148 
149 	dc_exit_ips_for_hw_access(dc);
150 
151 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
152 		struct pipe_ctx *pipe_ctx =
153 				&dc->current_state->res_ctx.pipe_ctx[i];
154 
155 		if (pipe_ctx->plane_state != plane_state)
156 			continue;
157 
158 		if (flags.bits.address)
159 			dc->hwss.update_pending_status(pipe_ctx);
160 		if (flags.bits.histogram) {
161 			struct dpp *dpp = pipe_ctx->plane_res.dpp;
162 
163 			if (dpp && dpp->funcs->dpp_cm_hist_read)
164 				dpp->funcs->dpp_cm_hist_read(dpp, &pipe_ctx->plane_state->status.cm_hist);
165 		}
166 	}
167 
168 	return plane_status;
169 }
170 
dc_plane_state_retain(struct dc_plane_state * plane_state)171 void dc_plane_state_retain(struct dc_plane_state *plane_state)
172 {
173 	kref_get(&plane_state->refcount);
174 }
175 
dc_plane_state_free(struct kref * kref)176 static void dc_plane_state_free(struct kref *kref)
177 {
178 	struct dc_plane_state *plane_state = container_of(kref, struct dc_plane_state, refcount);
179 	dc_plane_destruct(plane_state);
180 	kvfree(plane_state);
181 }
182 
dc_plane_state_release(struct dc_plane_state * plane_state)183 void dc_plane_state_release(struct dc_plane_state *plane_state)
184 {
185 	kref_put(&plane_state->refcount, dc_plane_state_free);
186 }
187 
dc_gamma_retain(struct dc_gamma * gamma)188 void dc_gamma_retain(struct dc_gamma *gamma)
189 {
190 	kref_get(&gamma->refcount);
191 }
192 
dc_gamma_free(struct kref * kref)193 static void dc_gamma_free(struct kref *kref)
194 {
195 	struct dc_gamma *gamma = container_of(kref, struct dc_gamma, refcount);
196 	kvfree(gamma);
197 }
198 
dc_gamma_release(struct dc_gamma ** gamma)199 void dc_gamma_release(struct dc_gamma **gamma)
200 {
201 	kref_put(&(*gamma)->refcount, dc_gamma_free);
202 	*gamma = NULL;
203 }
204 
dc_create_gamma(void)205 struct dc_gamma *dc_create_gamma(void)
206 {
207 	struct dc_gamma *gamma = kvzalloc_obj(*gamma);
208 
209 	if (gamma == NULL)
210 		goto alloc_fail;
211 
212 	kref_init(&gamma->refcount);
213 	return gamma;
214 
215 alloc_fail:
216 	return NULL;
217 }
218 
dc_transfer_func_retain(struct dc_transfer_func * tf)219 void dc_transfer_func_retain(struct dc_transfer_func *tf)
220 {
221 	kref_get(&tf->refcount);
222 }
223 
dc_transfer_func_free(struct kref * kref)224 static void dc_transfer_func_free(struct kref *kref)
225 {
226 	struct dc_transfer_func *tf = container_of(kref, struct dc_transfer_func, refcount);
227 	kvfree(tf);
228 }
229 
dc_transfer_func_release(struct dc_transfer_func * tf)230 void dc_transfer_func_release(struct dc_transfer_func *tf)
231 {
232 	kref_put(&tf->refcount, dc_transfer_func_free);
233 }
234 
dc_create_transfer_func(void)235 struct dc_transfer_func *dc_create_transfer_func(void)
236 {
237 	struct dc_transfer_func *tf = kvzalloc_obj(*tf);
238 
239 	if (tf == NULL)
240 		goto alloc_fail;
241 
242 	kref_init(&tf->refcount);
243 
244 	return tf;
245 
246 alloc_fail:
247 	return NULL;
248 }
249 
dc_3dlut_func_free(struct kref * kref)250 static void dc_3dlut_func_free(struct kref *kref)
251 {
252 	struct dc_3dlut *lut = container_of(kref, struct dc_3dlut, refcount);
253 
254 	kvfree(lut);
255 }
256 
dc_create_3dlut_func(void)257 struct dc_3dlut *dc_create_3dlut_func(void)
258 {
259 	struct dc_3dlut *lut = kvzalloc_obj(*lut);
260 
261 	if (lut == NULL)
262 		goto alloc_fail;
263 
264 	kref_init(&lut->refcount);
265 	lut->state.raw = 0;
266 
267 	return lut;
268 
269 alloc_fail:
270 	return NULL;
271 
272 }
273 
dc_3dlut_func_release(struct dc_3dlut * lut)274 void dc_3dlut_func_release(struct dc_3dlut *lut)
275 {
276 	kref_put(&lut->refcount, dc_3dlut_func_free);
277 }
278 
dc_3dlut_func_retain(struct dc_3dlut * lut)279 void dc_3dlut_func_retain(struct dc_3dlut *lut)
280 {
281 	kref_get(&lut->refcount);
282 }
283 
dc_plane_cm_free(struct kref * kref)284 static void dc_plane_cm_free(struct kref *kref)
285 {
286 	struct dc_plane_cm *cm = container_of(kref, struct dc_plane_cm, refcount);
287 
288 	kvfree(cm);
289 }
290 
dc_plane_cm_create(void)291 struct dc_plane_cm *dc_plane_cm_create(void)
292 {
293 	struct dc_plane_cm *cm = kvzalloc_obj(*cm);
294 
295 	if (cm == NULL)
296 		goto alloc_fail;
297 
298 	kref_init(&cm->refcount);
299 
300 	return cm;
301 
302 alloc_fail:
303 	return NULL;
304 
305 }
306 
dc_plane_cm_release(struct dc_plane_cm * cm)307 void dc_plane_cm_release(struct dc_plane_cm *cm)
308 {
309 	kref_put(&cm->refcount, dc_plane_cm_free);
310 }
311 
dc_plane_cm_retain(struct dc_plane_cm * cm)312 void dc_plane_cm_retain(struct dc_plane_cm *cm)
313 {
314 	kref_get(&cm->refcount);
315 }
316 
dc_plane_force_dcc_and_tiling_disable(struct dc_plane_state * plane_state,bool clear_tiling)317 void dc_plane_force_dcc_and_tiling_disable(struct dc_plane_state *plane_state,
318 					   bool clear_tiling)
319 {
320 	struct dc *dc;
321 	unsigned int i;
322 
323 	if (!plane_state)
324 		return;
325 
326 	dc = plane_state->ctx->dc;
327 
328 	if (!dc || !dc->current_state)
329 		return;
330 
331 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
332 		struct pipe_ctx *pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
333 
334 		if (!pipe_ctx)
335 			continue;
336 
337 		if (dc->hwss.clear_surface_dcc_and_tiling)
338 			dc->hwss.clear_surface_dcc_and_tiling(pipe_ctx, plane_state, clear_tiling);
339 	}
340 }
341 
dc_plane_copy_config(struct dc_plane_state * dst,const struct dc_plane_state * src)342 void dc_plane_copy_config(struct dc_plane_state *dst, const struct dc_plane_state *src)
343 {
344 	struct kref temp_refcount;
345 
346 	/* backup persistent info */
347 	memcpy(&temp_refcount, &dst->refcount, sizeof(struct kref));
348 
349 	/* copy all configuration information */
350 	memcpy(dst, src, sizeof(struct dc_plane_state));
351 
352 	/* restore persistent info */
353 	memcpy(&dst->refcount, &temp_refcount, sizeof(struct kref));
354 }
355