aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/amd/display/dc/core/dc_state.c
blob: 5cc7f8da209c599f7585e8f10e499ef2118f34ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
The alarmtimers code currently picks a rtc device to use at
late init time. However, if your rtc driver is loaded as a module,
it may be registered after the alarmtimers late init code, leaving
the alarmtimers nonfunctional.

This patch moves the the rtcdevice selection to when we actually try
to use it, allowing us to make use of rtc modules that may have been
loaded at any point since bootup.

CC: Thomas Gleixner <tglx@linutronix.de>
CC: Meelis Roos <mroos@ut.ee>
Reported-by: Meelis Roos <mroos@ut.ee>
Signed-off-by: John Stultz <john.stultz@linaro.org>
Diffstat (limited to 'kernel/time/alarmtimer.c')
-rw-r--r--kernel/time/alarmtimer.c137
1 files changed, 67 insertions, 70 deletions
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 2d966244ea60d0..98ecf4e36f2f22 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -42,15 +42,72 @@ static struct alarm_base {
clockid_t base_clockid;
} alarm_bases[ALARM_NUMTYPE];
+/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
+static ktime_t freezer_delta;
+static DEFINE_SPINLOCK(freezer_delta_lock);
+
#ifdef CONFIG_RTC_CLASS
/* rtc timer and device for setting alarm wakeups at suspend */
static struct rtc_timer rtctimer;
static struct rtc_device *rtcdev;
-#endif
+static DEFINE_SPINLOCK(rtcdev_lock);
-/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
-static ktime_t freezer_delta;
-static DEFINE_SPINLOCK(freezer_delta_lock);
+/**
+ * has_wakealarm - check rtc device has wakealarm ability
+ * @dev: current device
+ * @name_ptr: name to be returned
+ *
+ * This helper function checks to see if the rtc device can wake
+ * from suspend.
+ */
+static int has_wakealarm(struct device *dev, void *name_ptr)
+{
+ struct rtc_device *candidate = to_rtc_device(dev);
+
+ if (!candidate->ops->set_alarm)
+ return 0;
+ if (!device_may_wakeup(candidate->dev.parent))
+ return 0;
+
+ *(const char **)name_ptr = dev_name(dev);
+ return 1;
+}
+
+/**
+ * alarmtimer_get_rtcdev - Return selected rtcdevice
+ *
+ * This function returns the rtc device to use for wakealarms.
+ * If one has not already been chosen, it checks to see if a
+ * functional rtc device is available.
+ */
+static struct rtc_device *alarmtimer_get_rtcdev(void)
+{
+ struct device *dev;
+ char *str;
+ unsigned long flags;
+ struct rtc_device *ret;
+
+ spin_lock_irqsave(&rtcdev_lock, flags);
+ if (!rtcdev) {
+ /* Find an rtc device and init the rtc_timer */
+ dev = class_find_device(rtc_class, NULL, &str, has_wakealarm);
+ /* If we have a device then str is valid. See has_wakealarm() */
+ if (dev) {
+ rtcdev = rtc_class_open(str);
+ /*
+ * Drop the reference we got in class_find_device,
+ * rtc_open takes its own.
+ */
+ put_device(dev);
+ rtc_timer_init(&rtctimer, NULL, NULL);
+ }
+ }
+ ret = rtcdev;
+ spin_unlock_irqrestore(&rtcdev_lock, flags);
+
+ return ret;
+}
+#endif
/**
@@ -166,6 +223,7 @@ static int alarmtimer_suspend(struct device *dev)
struct rtc_time tm;
ktime_t min, now;
unsigned long flags;
+ struct rtc_device *rtc;
int i;
spin_lock_irqsave(&freezer_delta_lock, flags);
@@ -173,8 +231,9 @@ static int alarmtimer_suspend(struct device *dev)
freezer_delta = ktime_set(0, 0);
spin_unlock_irqrestore(&freezer_delta_lock, flags);
+ rtc = alarmtimer_get_rtcdev();
/* If we have no rtcdev, just return */
- if (!rtcdev)
+ if (!rtc)
return 0;
/* Find the soonest timer to expire*/
@@ -199,12 +258,12 @@ static int alarmtimer_suspend(struct device *dev)
WARN_ON(min.tv64 < NSEC_PER_SEC);
/* Setup an rtc timer to fire that far in the future */
- rtc_timer_cancel(rtcdev, &rtctimer);
- rtc_read_time(rtcdev, &tm);
+ rtc_timer_cancel(rtc, &rtctimer);
+ rtc_read_time(rtc, &tm);
now = rtc_tm_to_ktime(tm);
now = ktime_add(now, min);
- rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0));
+ rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
return 0;
}
@@ -638,65 +697,3 @@ static int __init alarmtimer_init(void)
}
device_initcall(alarmtimer_init);
-#ifdef CONFIG_RTC_CLASS
-/**
- * has_wakealarm - check rtc device has wakealarm ability
- * @dev: current device
- * @name_ptr: name to be returned
- *
- * This helper function checks to see if the rtc device can wake
- * from suspend.
- */
-static int __init has_wakealarm(struct device *dev, void *name_ptr)
-{
- struct rtc_device *candidate = to_rtc_device(dev);
-
- if (!candidate->ops->set_alarm)
- return 0;
- if (!device_may_wakeup(candidate->dev.parent))
- return 0;
-
- *(const char **)name_ptr = dev_name(dev);
- return 1;
-}
-
-/**
- * alarmtimer_init_late - Late initializing of alarmtimer code
- *
- * This function locates a rtc device to use for wakealarms.
- * Run as late_initcall to make sure rtc devices have been
- * registered.
- */
-static int __init alarmtimer_init_late(void)
-{
- struct device *dev;
- char *str;
-
- /* Find an rtc device and init the rtc_timer */
- dev = class_find_device(rtc_class, NULL, &str, has_wakealarm);
- /* If we have a device then str is valid. See has_wakealarm() */
- if (dev) {
- rtcdev = rtc_class_open(str);
- /*
- * Drop the reference we got in class_find_device,
- * rtc_open takes its own.
- */
- put_device(dev);
- }
- if (!rtcdev) {
- printk(KERN_WARNING "No RTC device found, ALARM timers will"
- " not wake from suspend");
- }
- rtc_timer_init(&rtctimer, NULL, NULL);
-
- return 0;
-}
-#else
-static int __init alarmtimer_init_late(void)
-{
- printk(KERN_WARNING "Kernel not built with RTC support, ALARM timers"
- " will not wake from suspend");
- return 0;
-}
-#endif
-late_initcall(alarmtimer_init_late);
href='#n390'>390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
/*
 * Copyright 2023 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: AMD
 *
 */
#include "core_types.h"
#include "core_status.h"
#include "dc_state.h"
#include "dc_state_priv.h"
#include "dc_stream_priv.h"
#include "dc_plane_priv.h"

#include "dm_services.h"
#include "resource.h"
#include "link_enc_cfg.h"

#include "dml2/dml2_wrapper.h"
#include "dml2/dml2_internal_types.h"

#define DC_LOGGER \
	dc->ctx->logger
#define DC_LOGGER_INIT(logger)

/* Private dc_state helper functions */
static bool dc_state_track_phantom_stream(struct dc_state *state,
		struct dc_stream_state *phantom_stream)
{
	if (state->phantom_stream_count >= MAX_PHANTOM_PIPES)
		return false;

	state->phantom_streams[state->phantom_stream_count++] = phantom_stream;

	return true;
}

static bool dc_state_untrack_phantom_stream(struct dc_state *state, struct dc_stream_state *phantom_stream)
{
	bool res = false;
	int i;

	/* first find phantom stream in the dc_state */
	for (i = 0; i < state->phantom_stream_count; i++) {
		if (state->phantom_streams[i] == phantom_stream) {
			state->phantom_streams[i] = NULL;
			res = true;
			break;
		}
	}

	/* failed to find stream in state */
	if (!res)
		return res;

	/* trim back phantom streams */
	state->phantom_stream_count--;
	for (; i < state->phantom_stream_count; i++)
		state->phantom_streams[i] = state->phantom_streams[i + 1];

	return res;
}

static bool dc_state_is_phantom_stream_tracked(struct dc_state *state, struct dc_stream_state *phantom_stream)
{
	int i;

	for (i = 0; i < state->phantom_stream_count; i++) {
		if (state->phantom_streams[i] == phantom_stream)
			return true;
	}

	return false;
}

static bool dc_state_track_phantom_plane(struct dc_state *state,
		struct dc_plane_state *phantom_plane)
{
	if (state->phantom_plane_count >= MAX_PHANTOM_PIPES)
		return false;

	state->phantom_planes[state->phantom_plane_count++] = phantom_plane;

	return true;
}

static bool dc_state_untrack_phantom_plane(struct dc_state *state, struct dc_plane_state *phantom_plane)
{
	bool res = false;
	int i;

	/* first find phantom plane in the dc_state */
	for (i = 0; i < state->phantom_plane_count; i++) {
		if (state->phantom_planes[i] == phantom_plane) {
			state->phantom_planes[i] = NULL;
			res = true;
			break;
		}
	}

	/* failed to find plane in state */
	if (!res)
		return res;

	/* trim back phantom planes */
	state->phantom_plane_count--;
	for (; i < state->phantom_plane_count; i++)
		state->phantom_planes[i] = state->phantom_planes[i + 1];

	return res;
}

static bool dc_state_is_phantom_plane_tracked(struct dc_state *state, struct dc_plane_state *phantom_plane)
{
	int i;

	for (i = 0; i < state->phantom_plane_count; i++) {
		if (state->phantom_planes[i] == phantom_plane)
			return true;
	}

	return false;
}

static void dc_state_copy_internal(struct dc_state *dst_state, struct dc_state *src_state)
{
	int i, j;

	memcpy(dst_state, src_state, sizeof(struct dc_state));

	for (i = 0; i < MAX_PIPES; i++) {
		struct pipe_ctx *cur_pipe = &dst_state->res_ctx.pipe_ctx[i];

		if (cur_pipe->top_pipe)
			cur_pipe->top_pipe =  &dst_state->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];

		if (cur_pipe->bottom_pipe)
			cur_pipe->bottom_pipe = &dst_state->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];

		if (cur_pipe->prev_odm_pipe)
			cur_pipe->prev_odm_pipe =  &dst_state->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];

		if (cur_pipe->next_odm_pipe)
			cur_pipe->next_odm_pipe = &dst_state->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
	}

	/* retain phantoms */
	for (i = 0; i < dst_state->phantom_stream_count; i++)
		dc_stream_retain(dst_state->phantom_streams[i]);

	for (i = 0; i < dst_state->phantom_plane_count; i++)
		dc_plane_state_retain(dst_state->phantom_planes[i]);

	/* retain streams and planes */
	for (i = 0; i < dst_state->stream_count; i++) {
		dc_stream_retain(dst_state->streams[i]);
		for (j = 0; j < dst_state->stream_status[i].plane_count; j++)
			dc_plane_state_retain(
					dst_state->stream_status[i].plane_states[j]);
	}

}

static void init_state(struct dc *dc, struct dc_state *state)
{
	/* Each context must have their own instance of VBA and in order to
	 * initialize and obtain IP and SOC the base DML instance from DC is
	 * initially copied into every context
	 */
	memcpy(&state->bw_ctx.dml, &dc->dml, sizeof(struct display_mode_lib));
}

/* Public dc_state functions */
struct dc_state *dc_state_create(struct dc *dc)
{
	struct dc_state *state = kvzalloc(sizeof(struct dc_state),
			GFP_KERNEL);

	if (!state)
		return NULL;

	init_state(dc, state);
	dc_state_construct(dc, state);

#ifdef CONFIG_DRM_AMD_DC_FP
	if (dc->debug.using_dml2)
		dml2_create(dc, &dc->dml2_options, &state->bw_ctx.dml2);
#endif

	kref_init(&state->refcount);

	return state;
}

void dc_state_copy(struct dc_state *dst_state, struct dc_state *src_state)
{
	struct kref refcount = dst_state->refcount;
#ifdef CONFIG_DRM_AMD_DC_FP
	struct dml2_context *dst_dml2 = dst_state->bw_ctx.dml2;
#endif

	dc_state_copy_internal(dst_state, src_state);

#ifdef CONFIG_DRM_AMD_DC_FP
	dst_state->bw_ctx.dml2 = dst_dml2;
	if (src_state->bw_ctx.dml2)
		dml2_copy(dst_state->bw_ctx.dml2, src_state->bw_ctx.dml2);
#endif

	/* context refcount should not be overridden */
	dst_state->refcount = refcount;
}

struct dc_state *dc_state_create_copy(struct dc_state *src_state)
{
	struct dc_state *new_state;

	new_state = kvmalloc(sizeof(struct dc_state),
			GFP_KERNEL);
	if (!new_state)
		return NULL;

	dc_state_copy_internal(new_state, src_state);

#ifdef CONFIG_DRM_AMD_DC_FP
	if (src_state->bw_ctx.dml2 &&
			!dml2_create_copy(&new_state->bw_ctx.dml2, src_state->bw_ctx.dml2)) {
		dc_state_release(new_state);
		return NULL;
	}
#endif

	kref_init(&new_state->refcount);

	return new_state;
}

void dc_state_copy_current(struct dc *dc, struct dc_state *dst_state)
{
	dc_state_copy(dst_state, dc->current_state);
}

struct dc_state *dc_state_create_current_copy(struct dc *dc)
{
	return dc_state_create_copy(dc->current_state);
}

void dc_state_construct(struct dc *dc, struct dc_state *state)
{
	state->clk_mgr = dc->clk_mgr;

	/* Initialise DIG link encoder resource tracking variables. */
	if (dc->res_pool)
		link_enc_cfg_init(dc, state);
}

void dc_state_destruct(struct dc_state *state)
{
	int i, j;

	for (i = 0; i < state->stream_count; i++) {
		for (j = 0; j < state->stream_status[i].plane_count; j++)
			dc_plane_state_release(
					state->stream_status[i].plane_states[j]);

		state->stream_status[i].plane_count = 0;
		dc_stream_release(state->streams[i]);
		state->streams[i] = NULL;
	}
	state->stream_count = 0;

	/* release tracked phantoms */
	for (i = 0; i < state->phantom_stream_count; i++) {
		dc_stream_release(state->phantom_streams[i]);
		state->phantom_streams[i] = NULL;
	}
	state->phantom_stream_count = 0;

	for (i = 0; i < state->phantom_plane_count; i++) {
		dc_plane_state_release(state->phantom_planes[i]);
		state->phantom_planes[i] = NULL;
	}
	state->phantom_plane_count = 0;

	state->stream_mask = 0;
	memset(&state->res_ctx, 0, sizeof(state->res_ctx));
	memset(&state->pp_display_cfg, 0, sizeof(state->pp_display_cfg));
	memset(&state->dcn_bw_vars, 0, sizeof(state->dcn_bw_vars));
	state->clk_mgr = NULL;
	memset(&state->bw_ctx.bw, 0, sizeof(state->bw_ctx.bw));
	memset(state->block_sequence, 0, sizeof(state->block_sequence));
	state->block_sequence_steps = 0;
	memset(state->dc_dmub_cmd, 0, sizeof(state->dc_dmub_cmd));
	state->dmub_cmd_count = 0;
	memset(&state->perf_params, 0, sizeof(state->perf_params));
	memset(&state->scratch, 0, sizeof(state->scratch));
}

void dc_state_retain(struct dc_state *state)
{
	kref_get(&state->refcount);
}

static void dc_state_free(struct kref *kref)
{
	struct dc_state *state = container_of(kref, struct dc_state, refcount);

	dc_state_destruct(state);

#ifdef CONFIG_DRM_AMD_DC_FP
	dml2_destroy(state->bw_ctx.dml2);
	state->bw_ctx.dml2 = 0;
#endif

	kvfree(state);
}

void dc_state_release(struct dc_state *state)
{
	if (state != NULL)
		kref_put(&state->refcount, dc_state_free);
}
/*
 * dc_state_add_stream() - Add a new dc_stream_state to a dc_state.
 */
enum dc_status dc_state_add_stream(
		struct dc *dc,
		struct dc_state *state,
		struct dc_stream_state *stream)
{
	enum dc_status res;

	DC_LOGGER_INIT(dc->ctx->logger);

	if (state->stream_count >= dc->res_pool->timing_generator_count) {
		DC_LOG_WARNING("Max streams reached, can't add stream %p !\n", stream);
		return DC_ERROR_UNEXPECTED;
	}

	state->streams[state->stream_count] = stream;
	dc_stream_retain(stream);
	state->stream_count++;

	res = resource_add_otg_master_for_stream_output(
			state, dc->res_pool, stream);
	if (res != DC_OK)
		DC_LOG_WARNING("Adding stream %p to context failed with err %d!\n", stream, res);

	return res;
}

/*
 * dc_state_remove_stream() - Remove a stream from a dc_state.
 */
enum dc_status dc_state_remove_stream(
		struct dc *dc,
		struct dc_state *state,
		struct dc_stream_state *stream)
{
	int i;
	struct pipe_ctx *del_pipe = resource_get_otg_master_for_stream(
			&state->res_ctx, stream);

	if (!del_pipe) {
		dm_error("Pipe not found for stream %p !\n", stream);
		return DC_ERROR_UNEXPECTED;
	}

	resource_update_pipes_for_stream_with_slice_count(state,
			dc->current_state, dc->res_pool, stream, 1);
	resource_remove_otg_master_for_stream_output(
			state, dc->res_pool, stream);

	for (i = 0; i < state->stream_count; i++)
		if (state->streams[i] == stream)
			break;

	if (state->streams[i] != stream) {
		dm_error("Context doesn't have stream %p !\n", stream);
		return DC_ERROR_UNEXPECTED;
	}

	dc_stream_release(state->streams[i]);
	state->stream_count--;

	/* Trim back arrays */
	for (; i < state->stream_count; i++) {
		state->streams[i] = state->streams[i + 1];
		state->stream_status[i] = state->stream_status[i + 1];
	}

	state->streams[state->stream_count] = NULL;
	memset(
			&state->stream_status[state->stream_count],
			0,
			sizeof(state->stream_status[0]));

	return DC_OK;
}

bool dc_state_add_plane(
		const struct dc *dc,
		struct dc_stream_state *stream,
		struct dc_plane_state *plane_state,
		struct dc_state *state)
{
	struct resource_pool *pool = dc->res_pool;
	struct pipe_ctx *otg_master_pipe;
	struct dc_stream_status *stream_status = NULL;
	bool added = false;

	stream_status = dc_state_get_stream_status(state, stream);
	if (stream_status == NULL) {
		dm_error("Existing stream not found; failed to attach surface!\n");
		goto out;
	} else if (stream_status->plane_count == MAX_SURFACE_NUM) {
		dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
				plane_state, MAX_SURFACE_NUM);
		goto out;
	}

	otg_master_pipe = resource_get_otg_master_for_stream(
			&state->res_ctx, stream);
	if (otg_master_pipe)
		added = resource_append_dpp_pipes_for_plane_composition(state,
				dc->current_state, pool, otg_master_pipe, plane_state);

	if (added) {
		stream_status->plane_states[stream_status->plane_count] =
				plane_state;
		stream_status->plane_count++;
		dc_plane_state_retain(plane_state);
	}

out:
	return added;
}

bool dc_state_remove_plane(
		const struct dc *dc,
		struct dc_stream_state *stream,
		struct dc_plane_state *plane_state,
		struct dc_state *state)
{
	int i;
	struct dc_stream_status *stream_status = NULL;
	struct resource_pool *pool = dc->res_pool;

	if (!plane_state)
		return true;

	for (i = 0; i < state->stream_count; i++)
		if (state->streams[i] == stream) {
			stream_status = &state->stream_status[i];
			break;
		}

	if (stream_status == NULL) {
		dm_error("Existing stream not found; failed to remove plane.\n");
		return false;
	}

	resource_remove_dpp_pipes_for_plane_composition(
			state, pool, plane_state);

	for (i = 0; i < stream_status->plane_count; i++) {
		if (stream_status->plane_states[i] == plane_state) {
			dc_plane_state_release(stream_status->plane_states[i]);
			break;
		}
	}

	if (i == stream_status->plane_count) {
		dm_error("Existing plane_state not found; failed to detach it!\n");
		return false;
	}

	stream_status->plane_count--;

	/* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
	for (; i < stream_status->plane_count; i++)
		stream_status->plane_states[i] = stream_status->plane_states[i + 1];

	stream_status->plane_states[stream_status->plane_count] = NULL;

	if (stream_status->plane_count == 0 && dc->config.enable_windowed_mpo_odm)
		/* ODM combine could prevent us from supporting more planes
		 * we will reset ODM slice count back to 1 when all planes have
		 * been removed to maximize the amount of planes supported when
		 * new planes are added.
		 */
		resource_update_pipes_for_stream_with_slice_count(
				state, dc->current_state, dc->res_pool, stream, 1);

	return true;
}

/**
 * dc_state_rem_all_planes_for_stream - Remove planes attached to the target stream.
 *
 * @dc: Current dc state.
 * @stream: Target stream, which we want to remove the attached plans.
 * @state: context from which the planes are to be removed.
 *
 * Return:
 * Return true if DC was able to remove all planes from the target
 * stream, otherwise, return false.
 */
bool dc_state_rem_all_planes_for_stream(
		const struct dc *dc,
		struct dc_stream_state *stream,
		struct dc_state *state)
{
	int i, old_plane_count;
	struct dc_stream_status *stream_status = NULL;
	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };

	for (i = 0; i < state->stream_count; i++)
		if (state->streams[i] == stream) {
			stream_status = &state->stream_status[i];
			break;
		}

	if (stream_status == NULL) {
		dm_error("Existing stream %p not found!\n", stream);
		return false;
	}

	old_plane_count = stream_status->plane_count;

	for (i = 0; i < old_plane_count; i++)
		del_planes[i] = stream_status->plane_states[i];

	for (i = 0; i < old_plane_count; i++)
		if (!dc_state_remove_plane(dc, stream, del_planes[i], state))
			return false;

	return true;
}

bool dc_state_add_all_planes_for_stream(
		const struct dc *dc,
		struct dc_stream_state *stream,
		struct dc_plane_state * const *plane_states,
		int plane_count,
		struct dc_state *state)
{
	int i;
	bool result = true;

	for (i = 0; i < plane_count; i++)
		if (!dc_state_add_plane(dc, stream, plane_states[i], state)) {
			result = false;
			break;
		}

	return result;
}

/* Private dc_state functions */

/**
 * dc_state_get_stream_status - Get stream status from given dc state
 * @state: DC state to find the stream status in
 * @stream: The stream to get the stream status for
 *
 * The given stream is expected to exist in the given dc state. Otherwise, NULL
 * will be returned.
 */
struct dc_stream_status *dc_state_get_stream_status(
		struct dc_state *state,
		struct dc_stream_state *stream)
{
	uint8_t i;

	if (state == NULL)
		return NULL;

	for (i = 0; i < state->stream_count; i++) {
		if (stream == state->streams[i])
			return &state->stream_status[i];
	}

	return NULL;
}

enum mall_stream_type dc_state_get_pipe_subvp_type(const struct dc_state *state,
		const struct pipe_ctx *pipe_ctx)
{
	return dc_state_get_stream_subvp_type(state, pipe_ctx->stream);
}

enum mall_stream_type dc_state_get_stream_subvp_type(const struct dc_state *state,
		const struct dc_stream_state *stream)
{
	int i;

	enum mall_stream_type type = SUBVP_NONE;

	for (i = 0; i < state->stream_count; i++) {
		if (state->streams[i] == stream) {
			type = state->stream_status[i].mall_stream_config.type;
			break;
		}
	}

	return type;
}

struct dc_stream_state *dc_state_get_paired_subvp_stream(const struct dc_state *state,
		const struct dc_stream_state *stream)
{
	int i;

	struct dc_stream_state *paired_stream = NULL;

	for (i = 0; i < state->stream_count; i++) {
		if (state->streams[i] == stream) {
			paired_stream = state->stream_status[i].mall_stream_config.paired_stream;
			break;
		}
	}

	return paired_stream;
}

struct dc_stream_state *dc_state_create_phantom_stream(const struct dc *dc,
		struct dc_state *state,
		struct dc_stream_state *main_stream)
{
	struct dc_stream_state *phantom_stream;

	DC_LOGGER_INIT(dc->ctx->logger);

	phantom_stream = dc_create_stream_for_sink(main_stream->sink);

	if (!phantom_stream) {
		DC_LOG_ERROR("Failed to allocate phantom stream.\n");
		return NULL;
	}

	/* track phantom stream in dc_state */
	dc_state_track_phantom_stream(state, phantom_stream);

	phantom_stream->is_phantom = true;
	phantom_stream->signal = SIGNAL_TYPE_VIRTUAL;
	phantom_stream->dpms_off = true;

	return phantom_stream;
}

void dc_state_release_phantom_stream(const struct dc *dc,
		struct dc_state *state,
		struct dc_stream_state *phantom_stream)
{
	DC_LOGGER_INIT(dc->ctx->logger);

	if (!dc_state_untrack_phantom_stream(state, phantom_stream)) {
		DC_LOG_ERROR("Failed to free phantom stream %p in dc state %p.\n", phantom_stream, state);
		return;
	}

	dc_stream_release(phantom_stream);
}

struct dc_plane_state *dc_state_create_phantom_plane(struct dc *dc,
		struct dc_state *state,
		struct dc_plane_state *main_plane)
{
	struct dc_plane_state *phantom_plane = dc_create_plane_state(dc);

	DC_LOGGER_INIT(dc->ctx->logger);

	if (!phantom_plane) {
		DC_LOG_ERROR("Failed to allocate phantom plane.\n");
		return NULL;
	}

	/* track phantom inside dc_state */
	dc_state_track_phantom_plane(state, phantom_plane);

	phantom_plane->is_phantom = true;

	return phantom_plane;
}

void dc_state_release_phantom_plane(const struct dc *dc,
		struct dc_state *state,
		struct dc_plane_state *phantom_plane)
{
	DC_LOGGER_INIT(dc->ctx->logger);

	if (!dc_state_untrack_phantom_plane(state, phantom_plane)) {
		DC_LOG_ERROR("Failed to free phantom plane %p in dc state %p.\n", phantom_plane, state);
		return;
	}

	dc_plane_state_release(phantom_plane);
}

/* add phantom streams to context and generate correct meta inside dc_state */
enum dc_status dc_state_add_phantom_stream(struct dc *dc,
		struct dc_state *state,
		struct dc_stream_state *phantom_stream,
		struct dc_stream_state *main_stream)
{
	struct dc_stream_status *main_stream_status;
	struct dc_stream_status *phantom_stream_status;
	enum dc_status res = dc_state_add_stream(dc, state, phantom_stream);

	/* check if stream is tracked */
	if (res == DC_OK && !dc_state_is_phantom_stream_tracked(state, phantom_stream)) {
		/* stream must be tracked if added to state */
		dc_state_track_phantom_stream(state, phantom_stream);
	}

	/* setup subvp meta */
	main_stream_status = dc_state_get_stream_status(state, main_stream);
	phantom_stream_status = dc_state_get_stream_status(state, phantom_stream);
	phantom_stream_status->mall_stream_config.type = SUBVP_PHANTOM;
	phantom_stream_status->mall_stream_config.paired_stream = main_stream;
	main_stream_status->mall_stream_config.type = SUBVP_MAIN;
	main_stream_status->mall_stream_config.paired_stream = phantom_stream;

	return res;
}

enum dc_status dc_state_remove_phantom_stream(struct dc *dc,
		struct dc_state *state,
		struct dc_stream_state *phantom_stream)
{
	struct dc_stream_status *main_stream_status;
	struct dc_stream_status *phantom_stream_status;

	/* reset subvp meta */
	phantom_stream_status = dc_state_get_stream_status(state, phantom_stream);
	main_stream_status = dc_state_get_stream_status(state, phantom_stream_status->mall_stream_config.paired_stream);
	phantom_stream_status->mall_stream_config.type = SUBVP_NONE;
	phantom_stream_status->mall_stream_config.paired_stream = NULL;
	if (main_stream_status) {
		main_stream_status->mall_stream_config.type = SUBVP_NONE;
		main_stream_status->mall_stream_config.paired_stream = NULL;
	}

	/* remove stream from state */
	return dc_state_remove_stream(dc, state, phantom_stream);
}

bool dc_state_add_phantom_plane(
		const struct dc *dc,
		struct dc_stream_state *phantom_stream,
		struct dc_plane_state *phantom_plane,
		struct dc_state *state)
{
	bool res = dc_state_add_plane(dc, phantom_stream, phantom_plane, state);

	/* check if stream is tracked */
	if (res && !dc_state_is_phantom_plane_tracked(state, phantom_plane)) {
		/* stream must be tracked if added to state */
		dc_state_track_phantom_plane(state, phantom_plane);
	}

	return res;
}

bool dc_state_remove_phantom_plane(
		const struct dc *dc,
		struct dc_stream_state *phantom_stream,
		struct dc_plane_state *phantom_plane,
		struct dc_state *state)
{
	return dc_state_remove_plane(dc, phantom_stream, phantom_plane, state);
}

bool dc_state_rem_all_phantom_planes_for_stream(
		const struct dc *dc,
		struct dc_stream_state *phantom_stream,
		struct dc_state *state,
		bool should_release_planes)
{
	int i, old_plane_count;
	struct dc_stream_status *stream_status = NULL;
	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };

	for (i = 0; i < state->stream_count; i++)
		if (state->streams[i] == phantom_stream) {
			stream_status = &state->stream_status[i];
			break;
		}

	if (stream_status == NULL) {
		dm_error("Existing stream %p not found!\n", phantom_stream);
		return false;
	}

	old_plane_count = stream_status->plane_count;

	for (i = 0; i < old_plane_count; i++)
		del_planes[i] = stream_status->plane_states[i];

	for (i = 0; i < old_plane_count; i++) {
		if (!dc_state_remove_plane(dc, phantom_stream, del_planes[i], state))
			return false;
		if (should_release_planes)
			dc_state_release_phantom_plane(dc, state, del_planes[i]);
	}

	return true;
}

bool dc_state_add_all_phantom_planes_for_stream(
		const struct dc *dc,
		struct dc_stream_state *phantom_stream,
		struct dc_plane_state * const *phantom_planes,
		int plane_count,
		struct dc_state *state)
{
	return dc_state_add_all_planes_for_stream(dc, phantom_stream, phantom_planes, plane_count, state);
}

bool dc_state_remove_phantom_streams_and_planes(
	struct dc *dc,
	struct dc_state *state)
{
	int i;
	bool removed_phantom = false;
	struct dc_stream_state *phantom_stream = NULL;

	for (i = 0; i < dc->res_pool->pipe_count; i++) {
		struct pipe_ctx *pipe = &state->res_ctx.pipe_ctx[i];

		if (pipe->plane_state && pipe->stream && dc_state_get_pipe_subvp_type(state, pipe) == SUBVP_PHANTOM) {
			phantom_stream = pipe->stream;

			dc_state_rem_all_phantom_planes_for_stream(dc, phantom_stream, state, false);
			dc_state_remove_phantom_stream(dc, state, phantom_stream);
			removed_phantom = true;
		}
	}
	return removed_phantom;
}

void dc_state_release_phantom_streams_and_planes(
		struct dc *dc,
		struct dc_state *state)
{
	int i;

	for (i = 0; i < state->phantom_stream_count; i++)
		dc_state_release_phantom_stream(dc, state, state->phantom_streams[i]);

	for (i = 0; i < state->phantom_plane_count; i++)
		dc_state_release_phantom_plane(dc, state, state->phantom_planes[i]);
}