clk: Update req_rate on __clk_recalc_rates()

Commit cb1b1dd96241 ("clk: Set req_rate on reparenting") introduced a
new function, clk_core_update_orphan_child_rates(), that updates the
req_rate field on reparenting.

It turns out that that function will interfere with the clock notifying
done by __clk_recalc_rates(). This ends up reporting the new rate in
both the old_rate and new_rate fields of struct clk_notifier_data.

Since clk_core_update_orphan_child_rates() is basically
__clk_recalc_rates() without the notifiers, and with the req_rate field
update, we can drop clk_core_update_orphan_child_rates() entirely, and
make __clk_recalc_rates() update req_rate.

However, __clk_recalc_rates() is being called in several code paths:
when retrieving a rate (most likely through clk_get_rate()), when changing
parents (through clk_set_rate() or clk_hw_reparent()), or when updating
the orphan status (through clk_core_reparent_orphans_nolock(), called at
registration).

Updating req_rate on reparenting or initialisation makes sense, but we
shouldn't do it on clk_get_rate(). Thus an extra flag has been added to
update or not req_rate depending on the context.

Fixes: cb1b1dd96241 ("clk: Set req_rate on reparenting")
Link: https://lore.kernel.org/linux-clk/0acc7217-762c-7c0d-45a0-55c384824ce4@samsung.com/
Link: https://lore.kernel.org/linux-clk/Y0QNSx+ZgqKSvPOC@sirena.org.uk/
Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Reported-by: Mark Brown <broonie@kernel.org>
Suggested-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
Link: https://lore.kernel.org/r/20221010-rpi-clk-fixes-again-v1-1-d87ba82ac404@cerno.tech
Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>

authored by Maxime Ripard and committed by Stephen Boyd 096f2a0c 433fb8a6

+11 -28
+11 -28
drivers/clk/clk.c
··· 1760 /** 1761 * __clk_recalc_rates 1762 * @core: first clk in the subtree 1763 * @msg: notification type (see include/linux/clk.h) 1764 * 1765 * Walks the subtree of clks starting with clk and recalculates rates as it ··· 1770 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, 1771 * if necessary. 1772 */ 1773 - static void __clk_recalc_rates(struct clk_core *core, unsigned long msg) 1774 { 1775 unsigned long old_rate; 1776 unsigned long parent_rate = 0; ··· 1785 parent_rate = core->parent->rate; 1786 1787 core->rate = clk_recalc(core, parent_rate); 1788 1789 /* 1790 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE ··· 1796 __clk_notify(core, msg, old_rate, core->rate); 1797 1798 hlist_for_each_entry(child, &core->children, child_node) 1799 - __clk_recalc_rates(child, msg); 1800 } 1801 1802 static unsigned long clk_core_get_rate_recalc(struct clk_core *core) 1803 { 1804 if (core && (core->flags & CLK_GET_RATE_NOCACHE)) 1805 - __clk_recalc_rates(core, 0); 1806 1807 return clk_core_get_rate_nolock(core); 1808 } ··· 1905 clk_core_update_orphan_status(child, is_orphan); 1906 } 1907 1908 - /* 1909 - * Update the orphan rate and req_rate of @core and all its children. 1910 - */ 1911 - static void clk_core_update_orphan_child_rates(struct clk_core *core) 1912 - { 1913 - struct clk_core *child; 1914 - unsigned long parent_rate = 0; 1915 - 1916 - if (core->parent) 1917 - parent_rate = core->parent->rate; 1918 - 1919 - core->rate = core->req_rate = clk_recalc(core, parent_rate); 1920 - 1921 - hlist_for_each_entry(child, &core->children, child_node) 1922 - clk_core_update_orphan_child_rates(child); 1923 - } 1924 - 1925 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent) 1926 { 1927 bool was_orphan = core->orphan; ··· 1974 clk_reparent(core, parent); 1975 clk_enable_unlock(flags); 1976 1977 - clk_core_update_orphan_child_rates(core); 1978 - 1979 return old_parent; 1980 } 1981 ··· 2019 clk_reparent(core, old_parent); 2020 clk_enable_unlock(flags); 2021 2022 - clk_core_update_orphan_child_rates(core); 2023 __clk_set_parent_after(core, old_parent, parent); 2024 2025 return ret; ··· 2642 struct clk_core *new_parent) 2643 { 2644 clk_reparent(core, new_parent); 2645 - clk_core_update_orphan_child_rates(core); 2646 __clk_recalc_accuracies(core); 2647 - __clk_recalc_rates(core, POST_RATE_CHANGE); 2648 } 2649 2650 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent) ··· 2727 2728 /* propagate rate an accuracy recalculation accordingly */ 2729 if (ret) { 2730 - __clk_recalc_rates(core, ABORT_RATE_CHANGE); 2731 } else { 2732 - __clk_recalc_rates(core, POST_RATE_CHANGE); 2733 __clk_recalc_accuracies(core); 2734 } 2735 ··· 3626 __clk_set_parent_before(orphan, parent); 3627 __clk_set_parent_after(orphan, parent, NULL); 3628 __clk_recalc_accuracies(orphan); 3629 - __clk_recalc_rates(orphan, 0); 3630 3631 /* 3632 * __clk_init_parent() will set the initial req_rate to
··· 1760 /** 1761 * __clk_recalc_rates 1762 * @core: first clk in the subtree 1763 + * @update_req: Whether req_rate should be updated with the new rate 1764 * @msg: notification type (see include/linux/clk.h) 1765 * 1766 * Walks the subtree of clks starting with clk and recalculates rates as it ··· 1769 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, 1770 * if necessary. 1771 */ 1772 + static void __clk_recalc_rates(struct clk_core *core, bool update_req, 1773 + unsigned long msg) 1774 { 1775 unsigned long old_rate; 1776 unsigned long parent_rate = 0; ··· 1783 parent_rate = core->parent->rate; 1784 1785 core->rate = clk_recalc(core, parent_rate); 1786 + if (update_req) 1787 + core->req_rate = core->rate; 1788 1789 /* 1790 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE ··· 1792 __clk_notify(core, msg, old_rate, core->rate); 1793 1794 hlist_for_each_entry(child, &core->children, child_node) 1795 + __clk_recalc_rates(child, update_req, msg); 1796 } 1797 1798 static unsigned long clk_core_get_rate_recalc(struct clk_core *core) 1799 { 1800 if (core && (core->flags & CLK_GET_RATE_NOCACHE)) 1801 + __clk_recalc_rates(core, false, 0); 1802 1803 return clk_core_get_rate_nolock(core); 1804 } ··· 1901 clk_core_update_orphan_status(child, is_orphan); 1902 } 1903 1904 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent) 1905 { 1906 bool was_orphan = core->orphan; ··· 1987 clk_reparent(core, parent); 1988 clk_enable_unlock(flags); 1989 1990 return old_parent; 1991 } 1992 ··· 2034 clk_reparent(core, old_parent); 2035 clk_enable_unlock(flags); 2036 2037 __clk_set_parent_after(core, old_parent, parent); 2038 2039 return ret; ··· 2658 struct clk_core *new_parent) 2659 { 2660 clk_reparent(core, new_parent); 2661 __clk_recalc_accuracies(core); 2662 + __clk_recalc_rates(core, true, POST_RATE_CHANGE); 2663 } 2664 2665 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent) ··· 2744 2745 /* propagate rate an accuracy recalculation accordingly */ 2746 if (ret) { 2747 + __clk_recalc_rates(core, true, ABORT_RATE_CHANGE); 2748 } else { 2749 + __clk_recalc_rates(core, true, POST_RATE_CHANGE); 2750 __clk_recalc_accuracies(core); 2751 } 2752 ··· 3643 __clk_set_parent_before(orphan, parent); 3644 __clk_set_parent_after(orphan, parent, NULL); 3645 __clk_recalc_accuracies(orphan); 3646 + __clk_recalc_rates(orphan, true, 0); 3647 3648 /* 3649 * __clk_init_parent() will set the initial req_rate to