Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4296352pxf; Tue, 16 Mar 2021 09:59:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzy8B1qFZO7MWKhuKndmhFhHWleysh+L9nzqdRDqJwUNlPdd0kgI+qhWu7Z+Xu0hCaDgto+ X-Received: by 2002:a05:6402:27d4:: with SMTP id c20mr37663754ede.271.1615913977920; Tue, 16 Mar 2021 09:59:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615913977; cv=none; d=google.com; s=arc-20160816; b=cg4FY+9cNzvnKXBgBtLjTixJ14XccLoui4vr6qZIEShd+pfGGRxbYnCFL3yfUOFdzT G9xVqnCd2thyK28D3QQztS/fDFMGtoqvP2QaTRT5QxHAlEq5kUmh2454BoYxm/QkYRU4 Hbb5FrXT7PQ22OQvIAG6lRHJKacykL0YZx4wDmVPNs2dqnHZ6ZYkJ6dNhXUb8tlos9IU CRL/TE9J+h0NUaBHCJcty19pziYxL4gJdula8a7NrQLi4SfflMZNNNV3ZN5h4OQwfPew P5g1wIEKcXYijhU4nrnCrTlFHKnITv7OTvfBOSJs6Udcz1djigfqLNYHS1r2kJ11SQvD 3srw== 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 :message-id:date:subject:cc:to:from; bh=V/KPPV7flK+22xj2VHEqUcolfCN9wgY6dpMNjsqrYZI=; b=VZB6moR7Ydlc03ZPreJgxm8vLOfd+Zicrnzc81cJOUgpy8oGsDG6W19Bc/yzbmtAc0 +SGXW8EFj29K43eC8qJ3q2hCInP6HakljPZNaNeGJBxFhzDVs3dmkjOMNYuE5i0wX3w4 HrtDnhnf7bH4G5Z3NgQhmg7vO/HPLl5RPGrzoZnpYR8s2BtxqKyjPfmyP5D+HfOY+STr 5hwqYz5H3fWWu9JNHjfFPacN4swPjX9695lf/mxxM8n4oHJurc//++k6VXLAtKu8ceqs FufV0s0gabjZfI4ms0/nb0gaQl3v4d8ZSW+tEOcYe4pr0Bnen4Y4aXsYdrPI+QAptebU +TMw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mh2si13710760ejb.107.2021.03.16.09.59.14; Tue, 16 Mar 2021 09:59:37 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238206AbhCPPwR (ORCPT + 99 others); Tue, 16 Mar 2021 11:52:17 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:51386 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232711AbhCPPvp (ORCPT ); Tue, 16 Mar 2021 11:51:45 -0400 Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_relay_lmtp) via UNIX with SMTP (IdeaSmtpServer 2.0.3) id c874691c4fc46822; Tue, 16 Mar 2021 16:51:42 +0100 Received: from kreacher.localnet (89-64-82-37.dynamic.chello.pl [89.64.82.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by v370.home.net.pl (Postfix) with ESMTPSA id 40065668CF6; Tue, 16 Mar 2021 16:51:41 +0100 (CET) From: "Rafael J. Wysocki" To: Bjorn Helgaas , Maximilian Luz Cc: LKML , Linux PCI , Linux PM , Mika Westerberg , Linux ACPI Subject: [PATCH] PCI: PM: Do not read power state in pci_enable_device_flags() Date: Tue, 16 Mar 2021 16:51:40 +0100 Message-ID: <3219454.74lMxhSOWB@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduledrudefvddgkedvucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecujffqoffgrffnpdggtffipffknecuuegrihhlohhuthemucduhedtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvffufffkggfgtgesthfuredttddtvdenucfhrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqeenucggtffrrghtthgvrhhnpeeiueevhfeigffhffevueekgedtleeitdfhffejleevtddvtdettedvfffffffhjeenucffohhmrghinhepkhgvrhhnvghlrdhorhhgnecukfhppeekledrieegrdekvddrfeejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepkeelrdeigedrkedvrdefjedphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedprhgtphhtthhopehhvghlghgrrghssehkvghrnhgvlhdrohhrghdprhgtphhtthhopehluhiimhgrgihimhhilhhirghnsehgmhgrihhlrdgtohhmpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqphgtihesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhig qdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehmihhkrgdrfigvshhtvghrsggvrhhgsehlihhnuhigrdhinhhtvghlrdgtohhmpdhrtghpthhtoheplhhinhhugidqrggtphhisehvghgvrhdrkhgvrhhnvghlrdhorhhg X-DCC--Metrics: v370.home.net.pl 1024; Body=7 Fuz1=7 Fuz2=7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki It should not be necessary to update the current_state field of struct pci_dev in pci_enable_device_flags() before calling do_pci_enable_device() for the device, because none of the code between that point and the pci_set_power_state() call in do_pci_enable_device() invoked later depends on it. Moreover, doing that is actively harmful in some cases. For example, if the given PCI device depends on an ACPI power resource whose _STA method initially returns 0 ("off"), but the config space of the PCI device is accessible and the power state retrieved from the PCI_PM_CTRL register is D0, the current_state field in the struct pci_dev representing that device will get out of sync with the power.state of its ACPI companion object and that will lead to power management issues going forward. To avoid such issues it is better to leave the current_state value as is until it is changed to PCI_D0 by do_pci_enable_device() as appropriate. However, the power state of the device is not changed to PCI_D0 if it is already enabled when pci_enable_device_flags() gets called for it, so update its current_state in that case, but use pci_update_current_state() covering platform PM too for that. Link: https://lore.kernel.org/lkml/20210314000439.3138941-1-luzmaximilian@gmail.com/ Reported-by: Maximilian Luz Tested-by: Maximilian Luz Signed-off-by: Rafael J. Wysocki --- Max, I've added a T-by from you even though the patch is slightly different from what you have tested, but the difference shouldn't matter for your case. --- drivers/pci/pci.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) Index: linux-pm/drivers/pci/pci.c =================================================================== --- linux-pm.orig/drivers/pci/pci.c +++ linux-pm/drivers/pci/pci.c @@ -1870,20 +1870,10 @@ static int pci_enable_device_flags(struc int err; int i, bars = 0; - /* - * Power state could be unknown at this point, either due to a fresh - * boot or a device removal call. So get the current power state - * so that things like MSI message writing will behave as expected - * (e.g. if the device really is in D0 at enable time). - */ - if (dev->pm_cap) { - u16 pmcsr; - pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); - dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK); - } - - if (atomic_inc_return(&dev->enable_cnt) > 1) + if (atomic_inc_return(&dev->enable_cnt) > 1) { + pci_update_current_state(dev, dev->current_state); return 0; /* already enabled */ + } bridge = pci_upstream_bridge(dev); if (bridge)