^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, 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 <linux/interconnect.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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "bcm-voter.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "icc-rpmh.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * qcom_icc_pre_aggregate - cleans up stale values from prior icc_set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * @node: icc node to operate on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void qcom_icc_pre_aggregate(struct icc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct qcom_icc_node *qn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) qn = node->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) for (i = 0; i < QCOM_ICC_NUM_BUCKETS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) qn->sum_avg[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) qn->max_peak[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) EXPORT_SYMBOL_GPL(qcom_icc_pre_aggregate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * qcom_icc_aggregate - aggregate bw for buckets indicated by tag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @node: node to aggregate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @tag: tag to indicate which buckets to aggregate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @avg_bw: new bw to sum aggregate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @peak_bw: new bw to max aggregate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @agg_avg: existing aggregate avg bw val
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @agg_peak: existing aggregate peak bw val
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int qcom_icc_aggregate(struct icc_node *node, u32 tag, u32 avg_bw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u32 peak_bw, u32 *agg_avg, u32 *agg_peak)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct qcom_icc_node *qn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct qcom_icc_provider *qp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) qn = node->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) qp = to_qcom_provider(node->provider);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) tag = QCOM_ICC_TAG_ALWAYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) for (i = 0; i < QCOM_ICC_NUM_BUCKETS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (tag & BIT(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) qn->sum_avg[i] += avg_bw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) qn->max_peak[i] = max_t(u32, qn->max_peak[i], peak_bw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (node->init_avg || node->init_peak) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) qn->sum_avg[i] = max_t(u64, qn->sum_avg[i], node->init_avg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) qn->max_peak[i] = max_t(u64, qn->max_peak[i], node->init_peak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *agg_avg += avg_bw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *agg_peak = max_t(u32, *agg_peak, peak_bw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) for (i = 0; i < qn->num_bcms; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) qcom_icc_bcm_voter_add(qp->voter, qn->bcms[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) EXPORT_SYMBOL_GPL(qcom_icc_aggregate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * qcom_icc_set - set the constraints based on path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @src: source node for the path to set constraints on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @dst: destination node for the path to set constraints on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Return: 0 on success, or an error code otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int qcom_icc_set(struct icc_node *src, struct icc_node *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct qcom_icc_provider *qp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct icc_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) node = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) node = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) qp = to_qcom_provider(node->provider);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) qcom_icc_bcm_voter_commit(qp->voter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) EXPORT_SYMBOL_GPL(qcom_icc_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct icc_node_data *qcom_icc_xlate_extended(struct of_phandle_args *spec, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct icc_node_data *ndata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct icc_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) node = of_icc_xlate_onecell(spec, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (IS_ERR(node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ERR_CAST(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ndata = kzalloc(sizeof(*ndata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!ndata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ndata->node = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (spec->args_count == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ndata->tag = spec->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (spec->args_count > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pr_warn("%pOF: Too many arguments, path tag is not parsed\n", spec->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return ndata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) EXPORT_SYMBOL_GPL(qcom_icc_xlate_extended);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * qcom_icc_bcm_init - populates bcm aux data and connect qnodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @bcm: bcm to be initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * @dev: associated provider device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Return: 0 on success, or an error code otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int qcom_icc_bcm_init(struct qcom_icc_bcm *bcm, struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct qcom_icc_node *qn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) const struct bcm_db *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) size_t data_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* BCM is already initialised*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (bcm->addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) bcm->addr = cmd_db_read_addr(bcm->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (!bcm->addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dev_err(dev, "%s could not find RPMh address\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) bcm->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) data = cmd_db_read_aux_data(bcm->name, &data_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (IS_ERR(data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) dev_err(dev, "%s command db read error (%ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) bcm->name, PTR_ERR(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!data_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) dev_err(dev, "%s command db missing or partial aux data\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) bcm->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) bcm->aux_data.unit = le32_to_cpu(data->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) bcm->aux_data.width = le16_to_cpu(data->width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) bcm->aux_data.vcd = data->vcd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) bcm->aux_data.reserved = data->reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) INIT_LIST_HEAD(&bcm->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) INIT_LIST_HEAD(&bcm->ws_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!bcm->vote_scale)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) bcm->vote_scale = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Link Qnodes to their respective BCMs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) for (i = 0; i < bcm->num_nodes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) qn = bcm->nodes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) qn->bcms[qn->num_bcms] = bcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) qn->num_bcms++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) EXPORT_SYMBOL_GPL(qcom_icc_bcm_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) MODULE_LICENSE("GPL v2");