Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/interconnect-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/list_sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <soc/qcom/rpmh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <soc/qcom/tcs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "bcm-voter.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "icc-rpmh.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static LIST_HEAD(bcm_voters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static DEFINE_MUTEX(bcm_voter_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * struct bcm_voter - Bus Clock Manager voter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @dev: reference to the device that communicates with the BCM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * @np: reference to the device node to match bcm voters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * @lock: mutex to protect commit and wake/sleep lists in the voter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * @commit_list: list containing bcms to be committed to hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * @ws_list: list containing bcms that have different wake/sleep votes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * @voter_node: list of bcm voters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * @tcs_wait: mask for which buckets require TCS completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct bcm_voter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct list_head commit_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct list_head ws_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct list_head voter_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	u32 tcs_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int cmp_vcd(void *priv, struct list_head *a, struct list_head *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	const struct qcom_icc_bcm *bcm_a =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 			list_entry(a, struct qcom_icc_bcm, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	const struct qcom_icc_bcm *bcm_b =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			list_entry(b, struct qcom_icc_bcm, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (bcm_a->aux_data.vcd < bcm_b->aux_data.vcd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	else if (bcm_a->aux_data.vcd == bcm_b->aux_data.vcd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static u64 bcm_div(u64 num, u32 base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	/* Ensure that small votes aren't lost. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (num && num < base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	do_div(num, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void bcm_aggregate(struct qcom_icc_bcm *bcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct qcom_icc_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	size_t i, bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	u64 agg_avg[QCOM_ICC_NUM_BUCKETS] = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	u64 agg_peak[QCOM_ICC_NUM_BUCKETS] = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u64 temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	for (bucket = 0; bucket < QCOM_ICC_NUM_BUCKETS; bucket++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		for (i = 0; i < bcm->num_nodes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			node = bcm->nodes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			temp = bcm_div(node->sum_avg[bucket] * bcm->aux_data.width,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				       node->buswidth * node->channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			agg_avg[bucket] = max(agg_avg[bucket], temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			temp = bcm_div(node->max_peak[bucket] * bcm->aux_data.width,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				       node->buswidth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			agg_peak[bucket] = max(agg_peak[bucket], temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		temp = agg_avg[bucket] * bcm->vote_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		bcm->vote_x[bucket] = bcm_div(temp, bcm->aux_data.unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		temp = agg_peak[bucket] * bcm->vote_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		bcm->vote_y[bucket] = bcm_div(temp, bcm->aux_data.unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (bcm->keepalive && bcm->vote_x[QCOM_ICC_BUCKET_AMC] == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	    bcm->vote_y[QCOM_ICC_BUCKET_AMC] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		bcm->vote_x[QCOM_ICC_BUCKET_AMC] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		bcm->vote_x[QCOM_ICC_BUCKET_WAKE] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		bcm->vote_y[QCOM_ICC_BUCKET_AMC] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		bcm->vote_y[QCOM_ICC_BUCKET_WAKE] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static inline void tcs_cmd_gen(struct tcs_cmd *cmd, u64 vote_x, u64 vote_y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			       u32 addr, bool commit, bool wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	bool valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	memset(cmd, 0, sizeof(*cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (vote_x == 0 && vote_y == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (vote_x > BCM_TCS_CMD_VOTE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		vote_x = BCM_TCS_CMD_VOTE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (vote_y > BCM_TCS_CMD_VOTE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		vote_y = BCM_TCS_CMD_VOTE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	cmd->addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	cmd->data = BCM_TCS_CMD(commit, valid, vote_x, vote_y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 * Set the wait for completion flag on command that need to be completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	 * before the next command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	cmd->wait = wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static void tcs_list_gen(struct bcm_voter *voter, int bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			 struct tcs_cmd tcs_list[MAX_VCD],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			 int n[MAX_VCD + 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct list_head *bcm_list = &voter->commit_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct qcom_icc_bcm *bcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	bool commit, wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	size_t idx = 0, batch = 0, cur_vcd_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	memset(n, 0, sizeof(int) * (MAX_VCD + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	list_for_each_entry(bcm, bcm_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		commit = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		cur_vcd_size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if ((list_is_last(&bcm->list, bcm_list)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		    bcm->aux_data.vcd != list_next_entry(bcm, list)->aux_data.vcd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			commit = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			cur_vcd_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		wait = commit && (voter->tcs_wait & BIT(bucket));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		tcs_cmd_gen(&tcs_list[idx], bcm->vote_x[bucket],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			    bcm->vote_y[bucket], bcm->addr, commit, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		n[batch]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		 * Batch the BCMs in such a way that we do not split them in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		 * multiple payloads when they are under the same VCD. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		 * to ensure that every BCM is committed since we only set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		 * commit bit on the last BCM request of every VCD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		if (n[batch] >= MAX_RPMH_PAYLOAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			if (!commit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				n[batch] -= cur_vcd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 				n[batch + 1] = cur_vcd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			batch++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * of_bcm_voter_get - gets a bcm voter handle from DT node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * @dev: device pointer for the consumer device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * @name: name for the bcm voter device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * This function will match a device_node pointer for the phandle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * specified in the device DT and return a bcm_voter handle on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * Returns bcm_voter pointer or ERR_PTR() on error. EPROBE_DEFER is returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * when matching bcm voter is yet to be found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct bcm_voter *of_bcm_voter_get(struct device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct bcm_voter *voter = ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	struct bcm_voter *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	struct device_node *np, *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	int idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (!dev || !dev->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		idx = of_property_match_string(np, "qcom,bcm-voter-names", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			return ERR_PTR(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	node = of_parse_phandle(np, "qcom,bcm-voters", idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	mutex_lock(&bcm_voter_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	list_for_each_entry(temp, &bcm_voters, voter_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		if (temp->np == node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			voter = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	mutex_unlock(&bcm_voter_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return voter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) EXPORT_SYMBOL_GPL(of_bcm_voter_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * qcom_icc_bcm_voter_add - queues up the bcm nodes that require updates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  * @voter: voter that the bcms are being added to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * @bcm: bcm to add to the commit and wake sleep list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) void qcom_icc_bcm_voter_add(struct bcm_voter *voter, struct qcom_icc_bcm *bcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (!voter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	mutex_lock(&voter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (list_empty(&bcm->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		list_add_tail(&bcm->list, &voter->commit_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (list_empty(&bcm->ws_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		list_add_tail(&bcm->ws_list, &voter->ws_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	mutex_unlock(&voter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) EXPORT_SYMBOL_GPL(qcom_icc_bcm_voter_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * qcom_icc_bcm_voter_commit - generates and commits tcs cmds based on bcms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * @voter: voter that needs flushing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  * This function generates a set of AMC commands and flushes to the BCM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  * associated with the voter. It conditionally generate WAKE and SLEEP commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  * based on deltas between WAKE/SLEEP requirements. The ws_list persists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  * through multiple commit requests and bcm nodes are removed only when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  * requirements for WAKE matches SLEEP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * Returns 0 on success, or an appropriate error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int qcom_icc_bcm_voter_commit(struct bcm_voter *voter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct qcom_icc_bcm *bcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct qcom_icc_bcm *bcm_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	int commit_idx[MAX_VCD + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	struct tcs_cmd cmds[MAX_BCMS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (!voter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	mutex_lock(&voter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	list_for_each_entry(bcm, &voter->commit_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		bcm_aggregate(bcm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 * Pre sort the BCMs based on VCD for ease of generating a command list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 * that groups the BCMs with the same VCD together. VCDs are numbered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 * with lowest being the most expensive time wise, ensuring that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	 * those commands are being sent the earliest in the queue. This needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 * to be sorted every commit since we can't guarantee the order in which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * the BCMs are added to the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	list_sort(NULL, &voter->commit_list, cmp_vcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	 * Construct the command list based on a pre ordered list of BCMs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	 * based on VCD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	tcs_list_gen(voter, QCOM_ICC_BUCKET_AMC, cmds, commit_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (!commit_idx[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	rpmh_invalidate(voter->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	ret = rpmh_write_batch(voter->dev, RPMH_ACTIVE_ONLY_STATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			       cmds, commit_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		pr_err("Error sending AMC RPMH requests (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	list_for_each_entry_safe(bcm, bcm_tmp, &voter->commit_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		list_del_init(&bcm->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	list_for_each_entry_safe(bcm, bcm_tmp, &voter->ws_list, ws_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		 * Only generate WAKE and SLEEP commands if a resource's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		 * requirements change as the execution environment transitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		 * between different power states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (bcm->vote_x[QCOM_ICC_BUCKET_WAKE] !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		    bcm->vote_x[QCOM_ICC_BUCKET_SLEEP] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		    bcm->vote_y[QCOM_ICC_BUCKET_WAKE] !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		    bcm->vote_y[QCOM_ICC_BUCKET_SLEEP])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			list_add_tail(&bcm->list, &voter->commit_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			list_del_init(&bcm->ws_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (list_empty(&voter->commit_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	list_sort(NULL, &voter->commit_list, cmp_vcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	tcs_list_gen(voter, QCOM_ICC_BUCKET_WAKE, cmds, commit_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	ret = rpmh_write_batch(voter->dev, RPMH_WAKE_ONLY_STATE, cmds, commit_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		pr_err("Error sending WAKE RPMH requests (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	tcs_list_gen(voter, QCOM_ICC_BUCKET_SLEEP, cmds, commit_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	ret = rpmh_write_batch(voter->dev, RPMH_SLEEP_STATE, cmds, commit_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		pr_err("Error sending SLEEP RPMH requests (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	list_for_each_entry_safe(bcm, bcm_tmp, &voter->commit_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		list_del_init(&bcm->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	mutex_unlock(&voter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) EXPORT_SYMBOL_GPL(qcom_icc_bcm_voter_commit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static int qcom_icc_bcm_voter_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	struct bcm_voter *voter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	voter = devm_kzalloc(&pdev->dev, sizeof(*voter), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (!voter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	voter->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	voter->np = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (of_property_read_u32(np, "qcom,tcs-wait", &voter->tcs_wait))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		voter->tcs_wait = QCOM_ICC_TAG_ACTIVE_ONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	mutex_init(&voter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	INIT_LIST_HEAD(&voter->commit_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	INIT_LIST_HEAD(&voter->ws_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	mutex_lock(&bcm_voter_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	list_add_tail(&voter->voter_node, &bcm_voters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	mutex_unlock(&bcm_voter_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static const struct of_device_id bcm_voter_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	{ .compatible = "qcom,bcm-voter" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) MODULE_DEVICE_TABLE(of, bcm_voter_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static struct platform_driver qcom_icc_bcm_voter_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	.probe = qcom_icc_bcm_voter_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		.name		= "bcm_voter",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		.of_match_table = bcm_voter_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) module_platform_driver(qcom_icc_bcm_voter_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) MODULE_AUTHOR("David Dai <daidavid1@codeaurora.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) MODULE_DESCRIPTION("Qualcomm BCM Voter interconnect driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) MODULE_LICENSE("GPL v2");