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