Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp177035rwo; Fri, 21 Jul 2023 10:07:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlHEDptWPfZVdygDPBcNd0LLQXvlJffC+kr25CQzA9wjQ7OJcfEiMty0IFIWy/xdUDTiIQih X-Received: by 2002:a05:6a00:981:b0:67f:d4e2:3dd7 with SMTP id u1-20020a056a00098100b0067fd4e23dd7mr631864pfg.21.1689959230682; Fri, 21 Jul 2023 10:07:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689959230; cv=none; d=google.com; s=arc-20160816; b=nfDSfMQm90wAOMke3o2N+2a4geksnf3/3oqJm0bmtMVERpIUZjSvs+zeWYHIj+dbJw N1l33KnKchkzSsGVtkEGg/kk1tqtJdEBcPu9h22Adbm4vamSKccRyRxocRYWPo7vrGCG P6GN6NQ0QqQkpR05bUEPLrey9PycsscVMHtT6gHBkrufVOaPRaJor8mvJ9kkfW/WHVG+ 4+olyJfdMJTs2i2NxahKD7lHM04EcQohvehxHnhK8WR8lEGUv+XUDHkROiq2lr5xjFcW +9NDHyavmwCegzO6dhL1iYkmPiGZOISNj/k0QZ1w8MUI2wc5tCPQXcTpgB7+crnGWcN/ F3yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ogunO7gAAOqmWnDIoHO4tyEhO9cKCdlOg56H2eTKr/I=; fh=X59gKo2bTEq2VHcwIFizOsXoeGl1m0HuKLr4L9qvQjg=; b=RCiuMWoB/hQIKIwzdhIw58OUED/SCudgM5t/BzTjpgHBN2ud4O5VsUXevOoAdGc0LU zsK5W1cMz27480DW6lcZ8nmlNheLVEguVneNddQHKwk2PKW/mSiipWTBFHX+Y7oDnfg7 AlpwA1KEQuhtC6sphiL6BwZsDR8tiC2aUtAnkiejWIvK2YEpUpKhjYUAw0jJ2LuEjDnM Wh9bYo6ojmcFgrxISLS9f8J8OJaDs214X0hZtnGnMO1x0sXlUmmDF4NGhX9Sw3h4Inoz 6O1xYYF7VRcevfOda0/AazVPJ1jo9aVDbdpN2YrH39jhNute2Wp3LM5Or86sqjoeGmy9 j0UA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cm21-20020a056a00339500b0067f03c85d88si3424600pfb.177.2023.07.21.10.06.57; Fri, 21 Jul 2023 10:07:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232226AbjGUPue (ORCPT + 99 others); Fri, 21 Jul 2023 11:50:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232308AbjGUPu2 (ORCPT ); Fri, 21 Jul 2023 11:50:28 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7D1E63583; Fri, 21 Jul 2023 08:50:18 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CBEF1D75; Fri, 21 Jul 2023 08:51:00 -0700 (PDT) Received: from e129166.arm.com (unknown [10.57.0.79]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9FB213F738; Fri, 21 Jul 2023 08:50:09 -0700 (PDT) From: Lukasz Luba To: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, rafael@kernel.org Cc: lukasz.luba@arm.com, dietmar.eggemann@arm.com, rui.zhang@intel.com, amit.kucheria@verdurent.com, amit.kachhap@gmail.com, daniel.lezcano@linaro.org, viresh.kumar@linaro.org, len.brown@intel.com, pavel@ucw.cz, Pierre.Gondois@arm.com, ionela.voinescu@arm.com, mhiramat@kernel.org Subject: [PATCH v3 04/12] PM: EM: Refactor a new function em_compute_costs() Date: Fri, 21 Jul 2023 16:50:14 +0100 Message-Id: <20230721155022.2339982-5-lukasz.luba@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230721155022.2339982-1-lukasz.luba@arm.com> References: <20230721155022.2339982-1-lukasz.luba@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Refactor a dedicated function which will be easier to maintain and re-use in future. The upcoming changes for the modifiable EM perf_state table will use it (instead of duplicating the code). Signed-off-by: Lukasz Luba --- kernel/power/energy_model.c | 72 ++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c index 85a70b7da023..fd1066dcf38b 100644 --- a/kernel/power/energy_model.c +++ b/kernel/power/energy_model.c @@ -103,14 +103,52 @@ static void em_debug_create_pd(struct device *dev) {} static void em_debug_remove_pd(struct device *dev) {} #endif +static int em_compute_costs(struct device *dev, struct em_perf_state *table, + struct em_data_callback *cb, int nr_states, + unsigned long flags) +{ + unsigned long prev_cost = ULONG_MAX; + u64 fmax; + int i, ret; + + /* Compute the cost of each performance state. */ + fmax = (u64) table[nr_states - 1].frequency; + for (i = nr_states - 1; i >= 0; i--) { + unsigned long power_res, cost; + + if (flags & EM_PERF_DOMAIN_ARTIFICIAL) { + ret = cb->get_cost(dev, table[i].frequency, &cost); + if (ret || !cost || cost > EM_MAX_POWER) { + dev_err(dev, "EM: invalid cost %lu %d\n", + cost, ret); + return -EINVAL; + } + } else { + power_res = table[i].power; + cost = div64_u64(fmax * power_res, table[i].frequency); + } + + table[i].cost = cost; + + if (table[i].cost >= prev_cost) { + table[i].flags = EM_PERF_STATE_INEFFICIENT; + dev_dbg(dev, "EM: OPP:%lu is inefficient\n", + table[i].frequency); + } else { + prev_cost = table[i].cost; + } + } + + return 0; +} + static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd, int nr_states, struct em_data_callback *cb, unsigned long flags) { - unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX; + unsigned long power, freq, prev_freq = 0; struct em_perf_state *table; int i, ret; - u64 fmax; table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL); if (!table) @@ -154,33 +192,9 @@ static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd, table[i].frequency = prev_freq = freq; } - /* Compute the cost of each performance state. */ - fmax = (u64) table[nr_states - 1].frequency; - for (i = nr_states - 1; i >= 0; i--) { - unsigned long power_res, cost; - - if (flags & EM_PERF_DOMAIN_ARTIFICIAL) { - ret = cb->get_cost(dev, table[i].frequency, &cost); - if (ret || !cost || cost > EM_MAX_POWER) { - dev_err(dev, "EM: invalid cost %lu %d\n", - cost, ret); - goto free_ps_table; - } - } else { - power_res = table[i].power; - cost = div64_u64(fmax * power_res, table[i].frequency); - } - - table[i].cost = cost; - - if (table[i].cost >= prev_cost) { - table[i].flags = EM_PERF_STATE_INEFFICIENT; - dev_dbg(dev, "EM: OPP:%lu is inefficient\n", - table[i].frequency); - } else { - prev_cost = table[i].cost; - } - } + ret = em_compute_costs(dev, table, cb, nr_states, flags); + if (ret) + goto free_ps_table; pd->table = table; pd->nr_perf_states = nr_states; -- 2.25.1