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 		pipe_ctx->plane_state->status.is_flip_pending = false;
147 
148 		break;
149 	}
150 
151 	dc_exit_ips_for_hw_access(dc);
152 
153 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
154 		struct pipe_ctx *pipe_ctx =
155 				&dc->current_state->res_ctx.pipe_ctx[i];
156 
157 		if (pipe_ctx->plane_state != plane_state)
158 			continue;
159 
160 		dc->hwss.update_pending_status(pipe_ctx);
161 	}
162 
163 	return plane_status;
164 }
165 
dc_plane_state_retain(struct dc_plane_state * plane_state)166 void dc_plane_state_retain(struct dc_plane_state *plane_state)
167 {
168 	kref_get(&plane_state->refcount);
169 }
170 
dc_plane_state_free(struct kref * kref)171 static void dc_plane_state_free(struct kref *kref)
172 {
173 	struct dc_plane_state *plane_state = container_of(kref, struct dc_plane_state, refcount);
174 	dc_plane_destruct(plane_state);
175 	kvfree(plane_state);
176 }
177 
dc_plane_state_release(struct dc_plane_state * plane_state)178 void dc_plane_state_release(struct dc_plane_state *plane_state)
179 {
180 	kref_put(&plane_state->refcount, dc_plane_state_free);
181 }
182 
dc_gamma_retain(struct dc_gamma * gamma)183 void dc_gamma_retain(struct dc_gamma *gamma)
184 {
185 	kref_get(&gamma->refcount);
186 }
187 
dc_gamma_free(struct kref * kref)188 static void dc_gamma_free(struct kref *kref)
189 {
190 	struct dc_gamma *gamma = container_of(kref, struct dc_gamma, refcount);
191 	kvfree(gamma);
192 }
193 
dc_gamma_release(struct dc_gamma ** gamma)194 void dc_gamma_release(struct dc_gamma **gamma)
195 {
196 	kref_put(&(*gamma)->refcount, dc_gamma_free);
197 	*gamma = NULL;
198 }
199 
dc_create_gamma(void)200 struct dc_gamma *dc_create_gamma(void)
201 {
202 	struct dc_gamma *gamma = kvzalloc(sizeof(*gamma), GFP_KERNEL);
203 
204 	if (gamma == NULL)
205 		goto alloc_fail;
206 
207 	kref_init(&gamma->refcount);
208 	return gamma;
209 
210 alloc_fail:
211 	return NULL;
212 }
213 
dc_transfer_func_retain(struct dc_transfer_func * tf)214 void dc_transfer_func_retain(struct dc_transfer_func *tf)
215 {
216 	kref_get(&tf->refcount);
217 }
218 
dc_transfer_func_free(struct kref * kref)219 static void dc_transfer_func_free(struct kref *kref)
220 {
221 	struct dc_transfer_func *tf = container_of(kref, struct dc_transfer_func, refcount);
222 	kvfree(tf);
223 }
224 
dc_transfer_func_release(struct dc_transfer_func * tf)225 void dc_transfer_func_release(struct dc_transfer_func *tf)
226 {
227 	kref_put(&tf->refcount, dc_transfer_func_free);
228 }
229 
dc_create_transfer_func(void)230 struct dc_transfer_func *dc_create_transfer_func(void)
231 {
232 	struct dc_transfer_func *tf = kvzalloc(sizeof(*tf), GFP_KERNEL);
233 
234 	if (tf == NULL)
235 		goto alloc_fail;
236 
237 	kref_init(&tf->refcount);
238 
239 	return tf;
240 
241 alloc_fail:
242 	return NULL;
243 }
244 
dc_3dlut_func_free(struct kref * kref)245 static void dc_3dlut_func_free(struct kref *kref)
246 {
247 	struct dc_3dlut *lut = container_of(kref, struct dc_3dlut, refcount);
248 
249 	kvfree(lut);
250 }
251 
dc_create_3dlut_func(void)252 struct dc_3dlut *dc_create_3dlut_func(void)
253 {
254 	struct dc_3dlut *lut = kvzalloc(sizeof(*lut), GFP_KERNEL);
255 
256 	if (lut == NULL)
257 		goto alloc_fail;
258 
259 	kref_init(&lut->refcount);
260 	lut->state.raw = 0;
261 
262 	return lut;
263 
264 alloc_fail:
265 	return NULL;
266 
267 }
268 
dc_3dlut_func_release(struct dc_3dlut * lut)269 void dc_3dlut_func_release(struct dc_3dlut *lut)
270 {
271 	kref_put(&lut->refcount, dc_3dlut_func_free);
272 }
273 
dc_3dlut_func_retain(struct dc_3dlut * lut)274 void dc_3dlut_func_retain(struct dc_3dlut *lut)
275 {
276 	kref_get(&lut->refcount);
277 }
278 
279 
280