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->in_shaper_func.type = TF_TYPE_BYPASS;
49 
50 	plane_state->lut3d_func.state.raw = 0;
51 
52 	plane_state->blend_tf.type = TF_TYPE_BYPASS;
53 
54 	plane_state->pre_multiplied_alpha = true;
55 
56 }
57 
dc_plane_destruct(struct dc_plane_state * plane_state)58 void dc_plane_destruct(struct dc_plane_state *plane_state)
59 {
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 	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 |= 1 << pipe_ctx->plane_res.hubp->inst;
78 	}
79 
80 	return pipe_mask;
81 }
82 
83 /*******************************************************************************
84  * Public functions
85  ******************************************************************************/
enable_surface_flip_reporting(struct dc_plane_state * plane_state,uint32_t controller_id)86 void enable_surface_flip_reporting(struct dc_plane_state *plane_state,
87 		uint32_t controller_id)
88 {
89 	plane_state->irq_source = controller_id + DC_IRQ_SOURCE_PFLIP1 - 1;
90 	/*register_flip_interrupt(surface);*/
91 }
92 
dc_create_plane_state(const struct dc * dc)93 struct dc_plane_state *dc_create_plane_state(const struct dc *dc)
94 {
95 	struct dc_plane_state *plane_state = kvzalloc(sizeof(*plane_state),
96 							GFP_KERNEL);
97 
98 	if (NULL == plane_state)
99 		return NULL;
100 
101 	kref_init(&plane_state->refcount);
102 	dc_plane_construct(dc->ctx, plane_state);
103 
104 	return plane_state;
105 }
106 
107 /*
108  *****************************************************************************
109  *  Function: dc_plane_get_status
110  *
111  *  @brief
112  *     Looks up the pipe context of plane_state and updates the pending status
113  *     of the pipe context. Then returns plane_state->status
114  *
115  *  @param [in] plane_state: pointer to the plane_state to get the status of
116  *****************************************************************************
117  */
dc_plane_get_status(const struct dc_plane_state * plane_state)118 const struct dc_plane_status *dc_plane_get_status(
119 		const struct dc_plane_state *plane_state)
120 {
121 	const struct dc_plane_status *plane_status;
122 	struct dc  *dc;
123 	int i;
124 
125 	if (!plane_state ||
126 		!plane_state->ctx ||
127 		!plane_state->ctx->dc) {
128 		ASSERT(0);
129 		return NULL; /* remove this if above assert never hit */
130 	}
131 
132 	plane_status = &plane_state->status;
133 	dc = plane_state->ctx->dc;
134 
135 	if (dc->current_state == NULL)
136 		return NULL;
137 
138 	/* Find the current plane state and set its pending bit to false */
139 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
140 		struct pipe_ctx *pipe_ctx =
141 				&dc->current_state->res_ctx.pipe_ctx[i];
142 
143 		if (pipe_ctx->plane_state != plane_state)
144 			continue;
145 
146 		if (pipe_ctx->plane_state)
147 			pipe_ctx->plane_state->status.is_flip_pending = false;
148 
149 		break;
150 	}
151 
152 	dc_exit_ips_for_hw_access(dc);
153 
154 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
155 		struct pipe_ctx *pipe_ctx =
156 				&dc->current_state->res_ctx.pipe_ctx[i];
157 
158 		if (pipe_ctx->plane_state != plane_state)
159 			continue;
160 
161 		dc->hwss.update_pending_status(pipe_ctx);
162 	}
163 
164 	return plane_status;
165 }
166 
dc_plane_state_retain(struct dc_plane_state * plane_state)167 void dc_plane_state_retain(struct dc_plane_state *plane_state)
168 {
169 	kref_get(&plane_state->refcount);
170 }
171 
dc_plane_state_free(struct kref * kref)172 static void dc_plane_state_free(struct kref *kref)
173 {
174 	struct dc_plane_state *plane_state = container_of(kref, struct dc_plane_state, refcount);
175 	dc_plane_destruct(plane_state);
176 	kvfree(plane_state);
177 }
178 
dc_plane_state_release(struct dc_plane_state * plane_state)179 void dc_plane_state_release(struct dc_plane_state *plane_state)
180 {
181 	kref_put(&plane_state->refcount, dc_plane_state_free);
182 }
183 
dc_gamma_retain(struct dc_gamma * gamma)184 void dc_gamma_retain(struct dc_gamma *gamma)
185 {
186 	kref_get(&gamma->refcount);
187 }
188 
dc_gamma_free(struct kref * kref)189 static void dc_gamma_free(struct kref *kref)
190 {
191 	struct dc_gamma *gamma = container_of(kref, struct dc_gamma, refcount);
192 	kvfree(gamma);
193 }
194 
dc_gamma_release(struct dc_gamma ** gamma)195 void dc_gamma_release(struct dc_gamma **gamma)
196 {
197 	kref_put(&(*gamma)->refcount, dc_gamma_free);
198 	*gamma = NULL;
199 }
200 
dc_create_gamma(void)201 struct dc_gamma *dc_create_gamma(void)
202 {
203 	struct dc_gamma *gamma = kvzalloc(sizeof(*gamma), GFP_KERNEL);
204 
205 	if (gamma == NULL)
206 		goto alloc_fail;
207 
208 	kref_init(&gamma->refcount);
209 	return gamma;
210 
211 alloc_fail:
212 	return NULL;
213 }
214 
dc_transfer_func_retain(struct dc_transfer_func * tf)215 void dc_transfer_func_retain(struct dc_transfer_func *tf)
216 {
217 	kref_get(&tf->refcount);
218 }
219 
dc_transfer_func_free(struct kref * kref)220 static void dc_transfer_func_free(struct kref *kref)
221 {
222 	struct dc_transfer_func *tf = container_of(kref, struct dc_transfer_func, refcount);
223 	kvfree(tf);
224 }
225 
dc_transfer_func_release(struct dc_transfer_func * tf)226 void dc_transfer_func_release(struct dc_transfer_func *tf)
227 {
228 	kref_put(&tf->refcount, dc_transfer_func_free);
229 }
230 
dc_create_transfer_func(void)231 struct dc_transfer_func *dc_create_transfer_func(void)
232 {
233 	struct dc_transfer_func *tf = kvzalloc(sizeof(*tf), GFP_KERNEL);
234 
235 	if (tf == NULL)
236 		goto alloc_fail;
237 
238 	kref_init(&tf->refcount);
239 
240 	return tf;
241 
242 alloc_fail:
243 	return NULL;
244 }
245 
dc_3dlut_func_free(struct kref * kref)246 static void dc_3dlut_func_free(struct kref *kref)
247 {
248 	struct dc_3dlut *lut = container_of(kref, struct dc_3dlut, refcount);
249 
250 	kvfree(lut);
251 }
252 
dc_create_3dlut_func(void)253 struct dc_3dlut *dc_create_3dlut_func(void)
254 {
255 	struct dc_3dlut *lut = kvzalloc(sizeof(*lut), GFP_KERNEL);
256 
257 	if (lut == NULL)
258 		goto alloc_fail;
259 
260 	kref_init(&lut->refcount);
261 	lut->state.raw = 0;
262 
263 	return lut;
264 
265 alloc_fail:
266 	return NULL;
267 
268 }
269 
dc_3dlut_func_release(struct dc_3dlut * lut)270 void dc_3dlut_func_release(struct dc_3dlut *lut)
271 {
272 	kref_put(&lut->refcount, dc_3dlut_func_free);
273 }
274 
dc_3dlut_func_retain(struct dc_3dlut * lut)275 void dc_3dlut_func_retain(struct dc_3dlut *lut)
276 {
277 	kref_get(&lut->refcount);
278 }
279 
280 
281