Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp565284pxa; Tue, 11 Aug 2020 09:32:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJVKW14q4MSKNKRMqU/bMVwZ/Uam6hWASDB8thV/7jXu8MeSeiAPJwXmhcLd7/CcTOHIp1 X-Received: by 2002:a17:906:2e51:: with SMTP id r17mr26548454eji.308.1597163533458; Tue, 11 Aug 2020 09:32:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597163533; cv=none; d=google.com; s=arc-20160816; b=MMm0Zmx/hjKPICAtUobSzoIas8OleJedqeuYiZkEzNxOF44zy68vsmVsZelTRzesp+ MEBpgYAYgJx/hpI/hLzJ+ykd1CrhGukWhQdJc1F+I/+MD11k0ZKZ7v5ID0Q9nz578vyC k8zSIEff6PrZr3BvX/aMsbgoPWGazsGVEWwzyuY3nXg18XpV7aFfgy4tWBOp9Cf0hlv7 4OybXkEnJVopLfEXfBO0AKL9+TRJ7lk2F/oIVawbJp/Q0dWe5J1cM5LR8FeekI8YENS/ d5JW5BTJAd50NjoxKm3jSJise0vmIHJF0dTBAJkKNIxz4IgBj5atqvGzPPupLKQ2dZNJ ttKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:ironport-sdr:ironport-sdr; bh=ptXId8R51im4xVDtHte9PdsbXXN8zIo5dzxr41G16gg=; b=0KIlT9nySZnbqeHR6m5VVHhFDFrCFg5khGhhlQJbnSM4lRZvdjVhnpp8RnaSYe5fOM q1Ka6BXD3h0NnPZFb/vJ4W4rgg4yEmHvsw6RlOyFnxNntGfvGYCD/lyRBU/po5831JO1 JYgA+JFZXtZcRBdoxns0k0E+BPTCmjqvk5ZaNQr57Hnt0L1hL2QQDSgEcJZh4atNmkOf UIgaSEpHJE2itwouJBU9BwtiuhAZipKFCDaei973SbDlieeCsz1wJoUFre0kD7cQu5Xi y2Jdt6p/1/kMs6jUAHfX6xOuMx2qbpRp7XHcYmVXMfAS81Lt6vgMtT+HWghhq1QKY8XK spTw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i7si15380081ejo.667.2020.08.11.09.31.49; Tue, 11 Aug 2020 09:32:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729273AbgHKQa4 (ORCPT + 99 others); Tue, 11 Aug 2020 12:30:56 -0400 Received: from mga09.intel.com ([134.134.136.24]:56171 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729188AbgHKQal (ORCPT ); Tue, 11 Aug 2020 12:30:41 -0400 IronPort-SDR: Sv4Ywq5vO/UnZiv8kLtHggreY/6JtjBzinmG3Zep++pX7Jd5Qc5p91MdDQFaNOjW5H7n3nU7m/ hHcseHZm6A0A== X-IronPort-AV: E=McAfee;i="6000,8403,9710"; a="154886264" X-IronPort-AV: E=Sophos;i="5.76,301,1592895600"; d="scan'208";a="154886264" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Aug 2020 09:30:39 -0700 IronPort-SDR: PCPNDyKvmuy02xp0/kBKQIsOKHrokyL8yLLW0rUJm3uzjEE8iFKl+qJCqi/Vj7GSnR1EelIdvM tQuspP5tEmEQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.76,301,1592895600"; d="scan'208";a="317786407" Received: from txasoft-yocto.an.intel.com ([10.123.72.192]) by fmsmga004.fm.intel.com with ESMTP; 11 Aug 2020 09:30:38 -0700 From: Gage Eads To: linux-kernel@vger.kernel.org, arnd@arndb.de, gregkh@linuxfoundation.org Cc: magnus.karlsson@intel.com, bjorn.topel@intel.com Subject: [PATCH v2 06/19] dlb2: add runtime power-management support Date: Tue, 11 Aug 2020 11:27:19 -0500 Message-Id: <20200811162732.1369-7-gage.eads@intel.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20200811162732.1369-1-gage.eads@intel.com> References: <20200811162732.1369-1-gage.eads@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The driver's power-management policy is to put the device in D0 when in use (when there are any open device files or memory mappings, or there are any virtual devices), and leave it in D3Hot otherwise. This includes resume/suspend callbacks; when the device resumes, the driver resets the hardware to a known good state. Signed-off-by: Gage Eads Reviewed-by: Magnus Karlsson --- drivers/misc/dlb2/dlb2_main.c | 72 +++++++++++++++++++++++++++++++++++++++++ drivers/misc/dlb2/dlb2_main.h | 2 ++ drivers/misc/dlb2/dlb2_pf_ops.c | 30 +++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/drivers/misc/dlb2/dlb2_main.c b/drivers/misc/dlb2/dlb2_main.c index 2e9b8c2d479c..17a26e6856d1 100644 --- a/drivers/misc/dlb2/dlb2_main.c +++ b/drivers/misc/dlb2/dlb2_main.c @@ -61,11 +61,19 @@ static int dlb2_open(struct inode *i, struct file *f) f->private_data = dev; + dev->ops->inc_pm_refcnt(dev->pdev, true); + return 0; } static int dlb2_close(struct inode *i, struct file *f) { + struct dlb2_dev *dev; + + dev = container_of(f->f_inode->i_cdev, struct dlb2_dev, cdev); + + dev->ops->dec_pm_refcnt(dev->pdev); + return 0; } @@ -94,6 +102,8 @@ int dlb2_init_domain(struct dlb2_dev *dlb2_dev, u32 domain_id) dlb2_dev->sched_domains[domain_id] = domain; + dlb2_dev->ops->inc_pm_refcnt(dlb2_dev->pdev, true); + return 0; } @@ -134,6 +144,8 @@ static int dlb2_domain_close(struct inode *i, struct file *f) kref_put(&domain->refcnt, dlb2_free_domain); + dev->ops->dec_pm_refcnt(dev->pdev); + mutex_unlock(&dev->resource_mutex); return ret; @@ -262,6 +274,15 @@ static int dlb2_probe(struct pci_dev *pdev, list_add(&dlb2_dev->list, &dlb2_dev_list); mutex_unlock(&dlb2_driver_lock); + /* + * The driver puts the device to sleep (D3hot) while there are no + * scheduling domains to service. The usage counter of a PCI device at + * probe time is 2, so decrement it twice here. (The PCI layer has + * already called pm_runtime_enable().) + */ + dlb2_dev->ops->dec_pm_refcnt(pdev); + dlb2_dev->ops->dec_pm_refcnt(pdev); + return 0; init_driver_state_fail: @@ -298,6 +319,10 @@ static void dlb2_remove(struct pci_dev *pdev) list_del(&dlb2_dev->list); mutex_unlock(&dlb2_driver_lock); + /* Undo the PM operations in dlb2_probe(). */ + dlb2_dev->ops->inc_pm_refcnt(pdev, false); + dlb2_dev->ops->inc_pm_refcnt(pdev, false); + dlb2_dev->ops->free_driver_state(dlb2_dev); dlb2_resource_free(&dlb2_dev->hw); @@ -319,17 +344,64 @@ static void dlb2_remove(struct pci_dev *pdev) devm_kfree(&pdev->dev, dlb2_dev); } +#ifdef CONFIG_PM +static void dlb2_reset_hardware_state(struct dlb2_dev *dev) +{ + dlb2_reset_device(dev->pdev); + + /* Reinitialize any other hardware state */ + dev->ops->init_hardware(dev); +} + +static int dlb2_runtime_suspend(struct device *dev) +{ + /* Return and let the PCI subsystem put the device in D3hot. */ + + return 0; +} + +static int dlb2_runtime_resume(struct device *dev) +{ + struct pci_dev *pdev = container_of(dev, struct pci_dev, dev); + struct dlb2_dev *dlb2_dev = pci_get_drvdata(pdev); + int ret; + + /* + * The PCI subsystem put the device in D0, but the device may not have + * completed powering up. Wait until the device is ready before + * proceeding. + */ + ret = dlb2_dev->ops->wait_for_device_ready(dlb2_dev, pdev); + if (ret) + return ret; + + /* Now reinitialize the device state. */ + dlb2_reset_hardware_state(dlb2_dev); + + return 0; +} +#endif + static struct pci_device_id dlb2_id_table[] = { { PCI_DEVICE_DATA(INTEL, DLB2_PF, DLB2_PF) }, { 0 } }; MODULE_DEVICE_TABLE(pci, dlb2_id_table); +#ifdef CONFIG_PM +static const struct dev_pm_ops dlb2_pm_ops = { + SET_RUNTIME_PM_OPS(dlb2_runtime_suspend, dlb2_runtime_resume, NULL) +}; +#endif + static struct pci_driver dlb2_pci_driver = { .name = (char *)dlb2_driver_name, .id_table = dlb2_id_table, .probe = dlb2_probe, .remove = dlb2_remove, +#ifdef CONFIG_PM + .driver.pm = &dlb2_pm_ops, +#endif }; static int __init dlb2_init_module(void) diff --git a/drivers/misc/dlb2/dlb2_main.h b/drivers/misc/dlb2/dlb2_main.h index 79378a47127f..86344f223649 100644 --- a/drivers/misc/dlb2/dlb2_main.h +++ b/drivers/misc/dlb2/dlb2_main.h @@ -41,6 +41,8 @@ struct dlb2_device_ops { int (*map_pci_bar_space)(struct dlb2_dev *dev, struct pci_dev *pdev); void (*unmap_pci_bar_space)(struct dlb2_dev *dev, struct pci_dev *pdev); + void (*inc_pm_refcnt)(struct pci_dev *pdev, bool resume); + void (*dec_pm_refcnt)(struct pci_dev *pdev); int (*init_driver_state)(struct dlb2_dev *dev); void (*free_driver_state)(struct dlb2_dev *dev); int (*device_create)(struct dlb2_dev *dlb2_dev, diff --git a/drivers/misc/dlb2/dlb2_pf_ops.c b/drivers/misc/dlb2/dlb2_pf_ops.c index d7bff677abda..6ca06406b0f2 100644 --- a/drivers/misc/dlb2/dlb2_pf_ops.c +++ b/drivers/misc/dlb2/dlb2_pf_ops.c @@ -2,11 +2,39 @@ /* Copyright(c) 2017-2020 Intel Corporation */ #include +#include #include "dlb2_main.h" #include "dlb2_regs.h" #include "dlb2_resource.h" +/***********************************/ +/****** Runtime PM management ******/ +/***********************************/ + +static void +dlb2_pf_pm_inc_refcnt(struct pci_dev *pdev, bool resume) +{ + if (resume) + /* + * Increment the device's usage count and immediately wake it + * if it was suspended. + */ + pm_runtime_get_sync(&pdev->dev); + else + pm_runtime_get_noresume(&pdev->dev); +} + +static void +dlb2_pf_pm_dec_refcnt(struct pci_dev *pdev) +{ + /* + * Decrement the device's usage count and suspend it if the + * count reaches zero. + */ + pm_runtime_put_sync_suspend(&pdev->dev); +} + /********************************/ /****** PCI BAR management ******/ /********************************/ @@ -207,6 +235,8 @@ dlb2_pf_reset_domain(struct dlb2_hw *hw, u32 id) struct dlb2_device_ops dlb2_pf_ops = { .map_pci_bar_space = dlb2_pf_map_pci_bar_space, .unmap_pci_bar_space = dlb2_pf_unmap_pci_bar_space, + .inc_pm_refcnt = dlb2_pf_pm_inc_refcnt, + .dec_pm_refcnt = dlb2_pf_pm_dec_refcnt, .init_driver_state = dlb2_pf_init_driver_state, .free_driver_state = dlb2_pf_free_driver_state, .device_create = dlb2_pf_device_create, -- 2.13.6