Merge tag 'qcom-drivers-for-6.9-2' of https://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / drivers / gpu / drm / amd / display / dc / core / dc_state.c
1 /*
2  * Copyright 2023 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 #include "core_types.h"
26 #include "core_status.h"
27 #include "dc_state.h"
28 #include "dc_state_priv.h"
29 #include "dc_stream_priv.h"
30 #include "dc_plane_priv.h"
31
32 #include "dm_services.h"
33 #include "resource.h"
34 #include "link_enc_cfg.h"
35
36 #include "dml2/dml2_wrapper.h"
37 #include "dml2/dml2_internal_types.h"
38
39 #define DC_LOGGER \
40         dc->ctx->logger
41 #define DC_LOGGER_INIT(logger)
42
43 /* Private dc_state helper functions */
44 static bool dc_state_track_phantom_stream(struct dc_state *state,
45                 struct dc_stream_state *phantom_stream)
46 {
47         if (state->phantom_stream_count >= MAX_PHANTOM_PIPES)
48                 return false;
49
50         state->phantom_streams[state->phantom_stream_count++] = phantom_stream;
51
52         return true;
53 }
54
55 static bool dc_state_untrack_phantom_stream(struct dc_state *state, struct dc_stream_state *phantom_stream)
56 {
57         bool res = false;
58         int i;
59
60         /* first find phantom stream in the dc_state */
61         for (i = 0; i < state->phantom_stream_count; i++) {
62                 if (state->phantom_streams[i] == phantom_stream) {
63                         state->phantom_streams[i] = NULL;
64                         res = true;
65                         break;
66                 }
67         }
68
69         /* failed to find stream in state */
70         if (!res)
71                 return res;
72
73         /* trim back phantom streams */
74         state->phantom_stream_count--;
75         for (; i < state->phantom_stream_count; i++)
76                 state->phantom_streams[i] = state->phantom_streams[i + 1];
77
78         return res;
79 }
80
81 static bool dc_state_is_phantom_stream_tracked(struct dc_state *state, struct dc_stream_state *phantom_stream)
82 {
83         int i;
84
85         for (i = 0; i < state->phantom_stream_count; i++) {
86                 if (state->phantom_streams[i] == phantom_stream)
87                         return true;
88         }
89
90         return false;
91 }
92
93 static bool dc_state_track_phantom_plane(struct dc_state *state,
94                 struct dc_plane_state *phantom_plane)
95 {
96         if (state->phantom_plane_count >= MAX_PHANTOM_PIPES)
97                 return false;
98
99         state->phantom_planes[state->phantom_plane_count++] = phantom_plane;
100
101         return true;
102 }
103
104 static bool dc_state_untrack_phantom_plane(struct dc_state *state, struct dc_plane_state *phantom_plane)
105 {
106         bool res = false;
107         int i;
108
109         /* first find phantom plane in the dc_state */
110         for (i = 0; i < state->phantom_plane_count; i++) {
111                 if (state->phantom_planes[i] == phantom_plane) {
112                         state->phantom_planes[i] = NULL;
113                         res = true;
114                         break;
115                 }
116         }
117
118         /* failed to find plane in state */
119         if (!res)
120                 return res;
121
122         /* trim back phantom planes */
123         state->phantom_plane_count--;
124         for (; i < state->phantom_plane_count; i++)
125                 state->phantom_planes[i] = state->phantom_planes[i + 1];
126
127         return res;
128 }
129
130 static bool dc_state_is_phantom_plane_tracked(struct dc_state *state, struct dc_plane_state *phantom_plane)
131 {
132         int i;
133
134         for (i = 0; i < state->phantom_plane_count; i++) {
135                 if (state->phantom_planes[i] == phantom_plane)
136                         return true;
137         }
138
139         return false;
140 }
141
142 static void dc_state_copy_internal(struct dc_state *dst_state, struct dc_state *src_state)
143 {
144         int i, j;
145
146         memcpy(dst_state, src_state, sizeof(struct dc_state));
147
148         for (i = 0; i < MAX_PIPES; i++) {
149                 struct pipe_ctx *cur_pipe = &dst_state->res_ctx.pipe_ctx[i];
150
151                 if (cur_pipe->top_pipe)
152                         cur_pipe->top_pipe =  &dst_state->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
153
154                 if (cur_pipe->bottom_pipe)
155                         cur_pipe->bottom_pipe = &dst_state->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
156
157                 if (cur_pipe->prev_odm_pipe)
158                         cur_pipe->prev_odm_pipe =  &dst_state->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
159
160                 if (cur_pipe->next_odm_pipe)
161                         cur_pipe->next_odm_pipe = &dst_state->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
162         }
163
164         /* retain phantoms */
165         for (i = 0; i < dst_state->phantom_stream_count; i++)
166                 dc_stream_retain(dst_state->phantom_streams[i]);
167
168         for (i = 0; i < dst_state->phantom_plane_count; i++)
169                 dc_plane_state_retain(dst_state->phantom_planes[i]);
170
171         /* retain streams and planes */
172         for (i = 0; i < dst_state->stream_count; i++) {
173                 dc_stream_retain(dst_state->streams[i]);
174                 for (j = 0; j < dst_state->stream_status[i].plane_count; j++)
175                         dc_plane_state_retain(
176                                         dst_state->stream_status[i].plane_states[j]);
177         }
178
179 }
180
181 static void init_state(struct dc *dc, struct dc_state *state)
182 {
183         /* Each context must have their own instance of VBA and in order to
184          * initialize and obtain IP and SOC the base DML instance from DC is
185          * initially copied into every context
186          */
187         memcpy(&state->bw_ctx.dml, &dc->dml, sizeof(struct display_mode_lib));
188 }
189
190 /* Public dc_state functions */
191 struct dc_state *dc_state_create(struct dc *dc)
192 {
193         struct dc_state *state = kvzalloc(sizeof(struct dc_state),
194                         GFP_KERNEL);
195
196         if (!state)
197                 return NULL;
198
199         init_state(dc, state);
200         dc_state_construct(dc, state);
201
202 #ifdef CONFIG_DRM_AMD_DC_FP
203         if (dc->debug.using_dml2)
204                 dml2_create(dc, &dc->dml2_options, &state->bw_ctx.dml2);
205 #endif
206
207         kref_init(&state->refcount);
208
209         return state;
210 }
211
212 void dc_state_copy(struct dc_state *dst_state, struct dc_state *src_state)
213 {
214         struct kref refcount = dst_state->refcount;
215 #ifdef CONFIG_DRM_AMD_DC_FP
216         struct dml2_context *dst_dml2 = dst_state->bw_ctx.dml2;
217 #endif
218
219         dc_state_copy_internal(dst_state, src_state);
220
221 #ifdef CONFIG_DRM_AMD_DC_FP
222         dst_state->bw_ctx.dml2 = dst_dml2;
223         if (src_state->bw_ctx.dml2)
224                 dml2_copy(dst_state->bw_ctx.dml2, src_state->bw_ctx.dml2);
225 #endif
226
227         /* context refcount should not be overridden */
228         dst_state->refcount = refcount;
229 }
230
231 struct dc_state *dc_state_create_copy(struct dc_state *src_state)
232 {
233         struct dc_state *new_state;
234
235         new_state = kvmalloc(sizeof(struct dc_state),
236                         GFP_KERNEL);
237         if (!new_state)
238                 return NULL;
239
240         dc_state_copy_internal(new_state, src_state);
241
242 #ifdef CONFIG_DRM_AMD_DC_FP
243         if (src_state->bw_ctx.dml2 &&
244                         !dml2_create_copy(&new_state->bw_ctx.dml2, src_state->bw_ctx.dml2)) {
245                 dc_state_release(new_state);
246                 return NULL;
247         }
248 #endif
249
250         kref_init(&new_state->refcount);
251
252         return new_state;
253 }
254
255 void dc_state_copy_current(struct dc *dc, struct dc_state *dst_state)
256 {
257         dc_state_copy(dst_state, dc->current_state);
258 }
259
260 struct dc_state *dc_state_create_current_copy(struct dc *dc)
261 {
262         return dc_state_create_copy(dc->current_state);
263 }
264
265 void dc_state_construct(struct dc *dc, struct dc_state *state)
266 {
267         state->clk_mgr = dc->clk_mgr;
268
269         /* Initialise DIG link encoder resource tracking variables. */
270         if (dc->res_pool)
271                 link_enc_cfg_init(dc, state);
272 }
273
274 void dc_state_destruct(struct dc_state *state)
275 {
276         int i, j;
277
278         for (i = 0; i < state->stream_count; i++) {
279                 for (j = 0; j < state->stream_status[i].plane_count; j++)
280                         dc_plane_state_release(
281                                         state->stream_status[i].plane_states[j]);
282
283                 state->stream_status[i].plane_count = 0;
284                 dc_stream_release(state->streams[i]);
285                 state->streams[i] = NULL;
286         }
287         state->stream_count = 0;
288
289         /* release tracked phantoms */
290         for (i = 0; i < state->phantom_stream_count; i++) {
291                 dc_stream_release(state->phantom_streams[i]);
292                 state->phantom_streams[i] = NULL;
293         }
294
295         for (i = 0; i < state->phantom_plane_count; i++) {
296                 dc_plane_state_release(state->phantom_planes[i]);
297                 state->phantom_planes[i] = NULL;
298         }
299         state->stream_mask = 0;
300         memset(&state->res_ctx, 0, sizeof(state->res_ctx));
301         memset(&state->pp_display_cfg, 0, sizeof(state->pp_display_cfg));
302         memset(&state->dcn_bw_vars, 0, sizeof(state->dcn_bw_vars));
303         state->clk_mgr = NULL;
304         memset(&state->bw_ctx.bw, 0, sizeof(state->bw_ctx.bw));
305         memset(state->block_sequence, 0, sizeof(state->block_sequence));
306         state->block_sequence_steps = 0;
307         memset(state->dc_dmub_cmd, 0, sizeof(state->dc_dmub_cmd));
308         state->dmub_cmd_count = 0;
309         memset(&state->perf_params, 0, sizeof(state->perf_params));
310         memset(&state->scratch, 0, sizeof(state->scratch));
311 }
312
313 void dc_state_retain(struct dc_state *state)
314 {
315         kref_get(&state->refcount);
316 }
317
318 static void dc_state_free(struct kref *kref)
319 {
320         struct dc_state *state = container_of(kref, struct dc_state, refcount);
321
322         dc_state_destruct(state);
323
324 #ifdef CONFIG_DRM_AMD_DC_FP
325         dml2_destroy(state->bw_ctx.dml2);
326         state->bw_ctx.dml2 = 0;
327 #endif
328
329         kvfree(state);
330 }
331
332 void dc_state_release(struct dc_state *state)
333 {
334         kref_put(&state->refcount, dc_state_free);
335 }
336 /*
337  * dc_state_add_stream() - Add a new dc_stream_state to a dc_state.
338  */
339 enum dc_status dc_state_add_stream(
340                 struct dc *dc,
341                 struct dc_state *state,
342                 struct dc_stream_state *stream)
343 {
344         enum dc_status res;
345
346         DC_LOGGER_INIT(dc->ctx->logger);
347
348         if (state->stream_count >= dc->res_pool->timing_generator_count) {
349                 DC_LOG_WARNING("Max streams reached, can't add stream %p !\n", stream);
350                 return DC_ERROR_UNEXPECTED;
351         }
352
353         state->streams[state->stream_count] = stream;
354         dc_stream_retain(stream);
355         state->stream_count++;
356
357         res = resource_add_otg_master_for_stream_output(
358                         state, dc->res_pool, stream);
359         if (res != DC_OK)
360                 DC_LOG_WARNING("Adding stream %p to context failed with err %d!\n", stream, res);
361
362         return res;
363 }
364
365 /*
366  * dc_state_remove_stream() - Remove a stream from a dc_state.
367  */
368 enum dc_status dc_state_remove_stream(
369                 struct dc *dc,
370                 struct dc_state *state,
371                 struct dc_stream_state *stream)
372 {
373         int i;
374         struct pipe_ctx *del_pipe = resource_get_otg_master_for_stream(
375                         &state->res_ctx, stream);
376
377         if (!del_pipe) {
378                 dm_error("Pipe not found for stream %p !\n", stream);
379                 return DC_ERROR_UNEXPECTED;
380         }
381
382         resource_update_pipes_for_stream_with_slice_count(state,
383                         dc->current_state, dc->res_pool, stream, 1);
384         resource_remove_otg_master_for_stream_output(
385                         state, dc->res_pool, stream);
386
387         for (i = 0; i < state->stream_count; i++)
388                 if (state->streams[i] == stream)
389                         break;
390
391         if (state->streams[i] != stream) {
392                 dm_error("Context doesn't have stream %p !\n", stream);
393                 return DC_ERROR_UNEXPECTED;
394         }
395
396         dc_stream_release(state->streams[i]);
397         state->stream_count--;
398
399         /* Trim back arrays */
400         for (; i < state->stream_count; i++) {
401                 state->streams[i] = state->streams[i + 1];
402                 state->stream_status[i] = state->stream_status[i + 1];
403         }
404
405         state->streams[state->stream_count] = NULL;
406         memset(
407                         &state->stream_status[state->stream_count],
408                         0,
409                         sizeof(state->stream_status[0]));
410
411         return DC_OK;
412 }
413
414 bool dc_state_add_plane(
415                 const struct dc *dc,
416                 struct dc_stream_state *stream,
417                 struct dc_plane_state *plane_state,
418                 struct dc_state *state)
419 {
420         struct resource_pool *pool = dc->res_pool;
421         struct pipe_ctx *otg_master_pipe;
422         struct dc_stream_status *stream_status = NULL;
423         bool added = false;
424
425         stream_status = dc_state_get_stream_status(state, stream);
426         if (stream_status == NULL) {
427                 dm_error("Existing stream not found; failed to attach surface!\n");
428                 goto out;
429         } else if (stream_status->plane_count == MAX_SURFACE_NUM) {
430                 dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
431                                 plane_state, MAX_SURFACE_NUM);
432                 goto out;
433         }
434
435         otg_master_pipe = resource_get_otg_master_for_stream(
436                         &state->res_ctx, stream);
437         if (otg_master_pipe)
438                 added = resource_append_dpp_pipes_for_plane_composition(state,
439                                 dc->current_state, pool, otg_master_pipe, plane_state);
440
441         if (added) {
442                 stream_status->plane_states[stream_status->plane_count] =
443                                 plane_state;
444                 stream_status->plane_count++;
445                 dc_plane_state_retain(plane_state);
446         }
447
448 out:
449         return added;
450 }
451
452 bool dc_state_remove_plane(
453                 const struct dc *dc,
454                 struct dc_stream_state *stream,
455                 struct dc_plane_state *plane_state,
456                 struct dc_state *state)
457 {
458         int i;
459         struct dc_stream_status *stream_status = NULL;
460         struct resource_pool *pool = dc->res_pool;
461
462         if (!plane_state)
463                 return true;
464
465         for (i = 0; i < state->stream_count; i++)
466                 if (state->streams[i] == stream) {
467                         stream_status = &state->stream_status[i];
468                         break;
469                 }
470
471         if (stream_status == NULL) {
472                 dm_error("Existing stream not found; failed to remove plane.\n");
473                 return false;
474         }
475
476         resource_remove_dpp_pipes_for_plane_composition(
477                         state, pool, plane_state);
478
479         for (i = 0; i < stream_status->plane_count; i++) {
480                 if (stream_status->plane_states[i] == plane_state) {
481                         dc_plane_state_release(stream_status->plane_states[i]);
482                         break;
483                 }
484         }
485
486         if (i == stream_status->plane_count) {
487                 dm_error("Existing plane_state not found; failed to detach it!\n");
488                 return false;
489         }
490
491         stream_status->plane_count--;
492
493         /* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
494         for (; i < stream_status->plane_count; i++)
495                 stream_status->plane_states[i] = stream_status->plane_states[i + 1];
496
497         stream_status->plane_states[stream_status->plane_count] = NULL;
498
499         if (stream_status->plane_count == 0 && dc->config.enable_windowed_mpo_odm)
500                 /* ODM combine could prevent us from supporting more planes
501                  * we will reset ODM slice count back to 1 when all planes have
502                  * been removed to maximize the amount of planes supported when
503                  * new planes are added.
504                  */
505                 resource_update_pipes_for_stream_with_slice_count(
506                                 state, dc->current_state, dc->res_pool, stream, 1);
507
508         return true;
509 }
510
511 /**
512  * dc_state_rem_all_planes_for_stream - Remove planes attached to the target stream.
513  *
514  * @dc: Current dc state.
515  * @stream: Target stream, which we want to remove the attached plans.
516  * @state: context from which the planes are to be removed.
517  *
518  * Return:
519  * Return true if DC was able to remove all planes from the target
520  * stream, otherwise, return false.
521  */
522 bool dc_state_rem_all_planes_for_stream(
523                 const struct dc *dc,
524                 struct dc_stream_state *stream,
525                 struct dc_state *state)
526 {
527         int i, old_plane_count;
528         struct dc_stream_status *stream_status = NULL;
529         struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
530
531         for (i = 0; i < state->stream_count; i++)
532                 if (state->streams[i] == stream) {
533                         stream_status = &state->stream_status[i];
534                         break;
535                 }
536
537         if (stream_status == NULL) {
538                 dm_error("Existing stream %p not found!\n", stream);
539                 return false;
540         }
541
542         old_plane_count = stream_status->plane_count;
543
544         for (i = 0; i < old_plane_count; i++)
545                 del_planes[i] = stream_status->plane_states[i];
546
547         for (i = 0; i < old_plane_count; i++)
548                 if (!dc_state_remove_plane(dc, stream, del_planes[i], state))
549                         return false;
550
551         return true;
552 }
553
554 bool dc_state_add_all_planes_for_stream(
555                 const struct dc *dc,
556                 struct dc_stream_state *stream,
557                 struct dc_plane_state * const *plane_states,
558                 int plane_count,
559                 struct dc_state *state)
560 {
561         int i;
562         bool result = true;
563
564         for (i = 0; i < plane_count; i++)
565                 if (!dc_state_add_plane(dc, stream, plane_states[i], state)) {
566                         result = false;
567                         break;
568                 }
569
570         return result;
571 }
572
573 /* Private dc_state functions */
574
575 /**
576  * dc_state_get_stream_status - Get stream status from given dc state
577  * @state: DC state to find the stream status in
578  * @stream: The stream to get the stream status for
579  *
580  * The given stream is expected to exist in the given dc state. Otherwise, NULL
581  * will be returned.
582  */
583 struct dc_stream_status *dc_state_get_stream_status(
584                 struct dc_state *state,
585                 struct dc_stream_state *stream)
586 {
587         uint8_t i;
588
589         if (state == NULL)
590                 return NULL;
591
592         for (i = 0; i < state->stream_count; i++) {
593                 if (stream == state->streams[i])
594                         return &state->stream_status[i];
595         }
596
597         return NULL;
598 }
599
600 enum mall_stream_type dc_state_get_pipe_subvp_type(const struct dc_state *state,
601                 const struct pipe_ctx *pipe_ctx)
602 {
603         return dc_state_get_stream_subvp_type(state, pipe_ctx->stream);
604 }
605
606 enum mall_stream_type dc_state_get_stream_subvp_type(const struct dc_state *state,
607                 const struct dc_stream_state *stream)
608 {
609         int i;
610
611         enum mall_stream_type type = SUBVP_NONE;
612
613         for (i = 0; i < state->stream_count; i++) {
614                 if (state->streams[i] == stream) {
615                         type = state->stream_status[i].mall_stream_config.type;
616                         break;
617                 }
618         }
619
620         return type;
621 }
622
623 struct dc_stream_state *dc_state_get_paired_subvp_stream(const struct dc_state *state,
624                 const struct dc_stream_state *stream)
625 {
626         int i;
627
628         struct dc_stream_state *paired_stream = NULL;
629
630         for (i = 0; i < state->stream_count; i++) {
631                 if (state->streams[i] == stream) {
632                         paired_stream = state->stream_status[i].mall_stream_config.paired_stream;
633                         break;
634                 }
635         }
636
637         return paired_stream;
638 }
639
640 struct dc_stream_state *dc_state_create_phantom_stream(const struct dc *dc,
641                 struct dc_state *state,
642                 struct dc_stream_state *main_stream)
643 {
644         struct dc_stream_state *phantom_stream;
645
646         DC_LOGGER_INIT(dc->ctx->logger);
647
648         phantom_stream = dc_create_stream_for_sink(main_stream->sink);
649
650         if (!phantom_stream) {
651                 DC_LOG_ERROR("Failed to allocate phantom stream.\n");
652                 return NULL;
653         }
654
655         /* track phantom stream in dc_state */
656         dc_state_track_phantom_stream(state, phantom_stream);
657
658         phantom_stream->is_phantom = true;
659         phantom_stream->signal = SIGNAL_TYPE_VIRTUAL;
660         phantom_stream->dpms_off = true;
661
662         return phantom_stream;
663 }
664
665 void dc_state_release_phantom_stream(const struct dc *dc,
666                 struct dc_state *state,
667                 struct dc_stream_state *phantom_stream)
668 {
669         DC_LOGGER_INIT(dc->ctx->logger);
670
671         if (!dc_state_untrack_phantom_stream(state, phantom_stream)) {
672                 DC_LOG_ERROR("Failed to free phantom stream %p in dc state %p.\n", phantom_stream, state);
673                 return;
674         }
675
676         dc_stream_release(phantom_stream);
677 }
678
679 struct dc_plane_state *dc_state_create_phantom_plane(struct dc *dc,
680                 struct dc_state *state,
681                 struct dc_plane_state *main_plane)
682 {
683         struct dc_plane_state *phantom_plane = dc_create_plane_state(dc);
684
685         DC_LOGGER_INIT(dc->ctx->logger);
686
687         if (!phantom_plane) {
688                 DC_LOG_ERROR("Failed to allocate phantom plane.\n");
689                 return NULL;
690         }
691
692         /* track phantom inside dc_state */
693         dc_state_track_phantom_plane(state, phantom_plane);
694
695         phantom_plane->is_phantom = true;
696
697         return phantom_plane;
698 }
699
700 void dc_state_release_phantom_plane(const struct dc *dc,
701                 struct dc_state *state,
702                 struct dc_plane_state *phantom_plane)
703 {
704         DC_LOGGER_INIT(dc->ctx->logger);
705
706         if (!dc_state_untrack_phantom_plane(state, phantom_plane)) {
707                 DC_LOG_ERROR("Failed to free phantom plane %p in dc state %p.\n", phantom_plane, state);
708                 return;
709         }
710
711         dc_plane_state_release(phantom_plane);
712 }
713
714 /* add phantom streams to context and generate correct meta inside dc_state */
715 enum dc_status dc_state_add_phantom_stream(struct dc *dc,
716                 struct dc_state *state,
717                 struct dc_stream_state *phantom_stream,
718                 struct dc_stream_state *main_stream)
719 {
720         struct dc_stream_status *main_stream_status;
721         struct dc_stream_status *phantom_stream_status;
722         enum dc_status res = dc_state_add_stream(dc, state, phantom_stream);
723
724         /* check if stream is tracked */
725         if (res == DC_OK && !dc_state_is_phantom_stream_tracked(state, phantom_stream)) {
726                 /* stream must be tracked if added to state */
727                 dc_state_track_phantom_stream(state, phantom_stream);
728         }
729
730         /* setup subvp meta */
731         main_stream_status = dc_state_get_stream_status(state, main_stream);
732         phantom_stream_status = dc_state_get_stream_status(state, phantom_stream);
733         phantom_stream_status->mall_stream_config.type = SUBVP_PHANTOM;
734         phantom_stream_status->mall_stream_config.paired_stream = main_stream;
735         main_stream_status->mall_stream_config.type = SUBVP_MAIN;
736         main_stream_status->mall_stream_config.paired_stream = phantom_stream;
737
738         return res;
739 }
740
741 enum dc_status dc_state_remove_phantom_stream(struct dc *dc,
742                 struct dc_state *state,
743                 struct dc_stream_state *phantom_stream)
744 {
745         struct dc_stream_status *main_stream_status;
746         struct dc_stream_status *phantom_stream_status;
747
748         /* reset subvp meta */
749         phantom_stream_status = dc_state_get_stream_status(state, phantom_stream);
750         main_stream_status = dc_state_get_stream_status(state, phantom_stream_status->mall_stream_config.paired_stream);
751         phantom_stream_status->mall_stream_config.type = SUBVP_NONE;
752         phantom_stream_status->mall_stream_config.paired_stream = NULL;
753         if (main_stream_status) {
754                 main_stream_status->mall_stream_config.type = SUBVP_NONE;
755                 main_stream_status->mall_stream_config.paired_stream = NULL;
756         }
757
758         /* remove stream from state */
759         return dc_state_remove_stream(dc, state, phantom_stream);
760 }
761
762 bool dc_state_add_phantom_plane(
763                 const struct dc *dc,
764                 struct dc_stream_state *phantom_stream,
765                 struct dc_plane_state *phantom_plane,
766                 struct dc_state *state)
767 {
768         bool res = dc_state_add_plane(dc, phantom_stream, phantom_plane, state);
769
770         /* check if stream is tracked */
771         if (res && !dc_state_is_phantom_plane_tracked(state, phantom_plane)) {
772                 /* stream must be tracked if added to state */
773                 dc_state_track_phantom_plane(state, phantom_plane);
774         }
775
776         return res;
777 }
778
779 bool dc_state_remove_phantom_plane(
780                 const struct dc *dc,
781                 struct dc_stream_state *phantom_stream,
782                 struct dc_plane_state *phantom_plane,
783                 struct dc_state *state)
784 {
785         return dc_state_remove_plane(dc, phantom_stream, phantom_plane, state);
786 }
787
788 bool dc_state_rem_all_phantom_planes_for_stream(
789                 const struct dc *dc,
790                 struct dc_stream_state *phantom_stream,
791                 struct dc_state *state,
792                 bool should_release_planes)
793 {
794         int i, old_plane_count;
795         struct dc_stream_status *stream_status = NULL;
796         struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
797
798         for (i = 0; i < state->stream_count; i++)
799                 if (state->streams[i] == phantom_stream) {
800                         stream_status = &state->stream_status[i];
801                         break;
802                 }
803
804         if (stream_status == NULL) {
805                 dm_error("Existing stream %p not found!\n", phantom_stream);
806                 return false;
807         }
808
809         old_plane_count = stream_status->plane_count;
810
811         for (i = 0; i < old_plane_count; i++)
812                 del_planes[i] = stream_status->plane_states[i];
813
814         for (i = 0; i < old_plane_count; i++) {
815                 if (!dc_state_remove_plane(dc, phantom_stream, del_planes[i], state))
816                         return false;
817                 if (should_release_planes)
818                         dc_state_release_phantom_plane(dc, state, del_planes[i]);
819         }
820
821         return true;
822 }
823
824 bool dc_state_add_all_phantom_planes_for_stream(
825                 const struct dc *dc,
826                 struct dc_stream_state *phantom_stream,
827                 struct dc_plane_state * const *phantom_planes,
828                 int plane_count,
829                 struct dc_state *state)
830 {
831         return dc_state_add_all_planes_for_stream(dc, phantom_stream, phantom_planes, plane_count, state);
832 }
833
834 bool dc_state_remove_phantom_streams_and_planes(
835         struct dc *dc,
836         struct dc_state *state)
837 {
838         int i;
839         bool removed_phantom = false;
840         struct dc_stream_state *phantom_stream = NULL;
841
842         for (i = 0; i < dc->res_pool->pipe_count; i++) {
843                 struct pipe_ctx *pipe = &state->res_ctx.pipe_ctx[i];
844
845                 if (pipe->plane_state && pipe->stream && dc_state_get_pipe_subvp_type(state, pipe) == SUBVP_PHANTOM) {
846                         phantom_stream = pipe->stream;
847
848                         dc_state_rem_all_phantom_planes_for_stream(dc, phantom_stream, state, false);
849                         dc_state_remove_phantom_stream(dc, state, phantom_stream);
850                         removed_phantom = true;
851                 }
852         }
853         return removed_phantom;
854 }
855
856 void dc_state_release_phantom_streams_and_planes(
857                 struct dc *dc,
858                 struct dc_state *state)
859 {
860         int i;
861
862         for (i = 0; i < state->phantom_stream_count; i++)
863                 dc_state_release_phantom_stream(dc, state, state->phantom_streams[i]);
864
865         for (i = 0; i < state->phantom_plane_count; i++)
866                 dc_state_release_phantom_plane(dc, state, state->phantom_planes[i]);
867 }