public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jesse Barnes <jbarnes@virtuousgeek.org>
To: Robert Hancock <hancockr@shaw.ca>
Cc: Olivier Galibert <galibert@pobox.com>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	Andi Kleen <ak@suse.de>, Chuck Ebbert <cebbert@redhat.com>,
	Len Brown <lenb@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [RFC PATCH] PCI MMCONFIG: add validation against ACPI motherboard resources
Date: Wed, 23 May 2007 11:52:56 -0700	[thread overview]
Message-ID: <200705231152.57796.jbarnes@virtuousgeek.org> (raw)
In-Reply-To: <46539378.6000508@shaw.ca>

On Tuesday, May 22, 2007 6:06 pm Robert Hancock wrote:
> There was a big discussion about this back in 2002, in which Linus
> wasn't overly enthused about disabling the decode during probing due
> to risk of causing problems with some devices:
>
> http://lkml.org/lkml/2002/12/19/145
>
> In this particular case (64-bit BAR) we might be able to avoid the
> problem by changing the order in which we probe the two halves of the
> address, i.e. change the top half to 0xffffffff before messing with
> the bottom half and then change it back last. That way, we end up
> mapping it way to the top of 64-bit address space, which hopefully is
> less likely to conflict..

Fixed it (finally).  I don't think moving the 64 bit probing around 
would make a difference, since we'd restore its original value anyway 
before moving on to the 32 bit probe which is where I think the problem 
is.

I think what's happening is the probe is writing 0xffffffff to the video 
device, which is in the GMCH, and without memory decoding disabled, 
it'll start claiming PCI config access cycles causing the problems I 
saw.  So my code to disable I/O and memory decode was actually working 
but I had a bug in the re-enable path so all my devices were staying 
disabled. :)

So here's the patch I used (along with your ACPI patch and my 965 MCFG 
enable patch of course).  The probing code could probably use a bit 
more cleanup, but this patch limits itself to implementing PCI_COMMAND 
based I/O and memory space decode disabling during size probing.  We 
might want to do this unconditionally if we're using mmconfig based 
configuration access, since I imagine other machines might end up 
having similar address space layouts that would cause problems.

Linus, since you were the one concerned about breaking working setups, 
what do you think?  Should we use this approach, or specifically quirk 
out cases where mmconfig space might conflict with BAR probing?

Thanks,
Jesse

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index e48fcf0..69dfe0c 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -170,6 +170,48 @@ static inline int is_64bit_memory(u32 mask)
 	return 0;
 }
 
+#define BAR_IS_MEMORY(bar) (((bar) & PCI_BASE_ADDRESS_SPACE) ==	\
+			    PCI_BASE_ADDRESS_SPACE_MEMORY)
+
+/**
+ * pci_bar_size - get raw PCI BAR size
+ * @dev: PCI device
+ * @reg: BAR to probe
+ *
+ * Use basic PCI probing:
+ *   - save original BAR value
+ *   - disable MEM or IO decode as appropriate in PCI_COMMAND reg
+ *   - write all 1s to the BAR
+ *   - read back value
+ *   - reenble MEM or IO decode as necessary
+ *   - write original value back
+ *
+ * Returns raw BAR size to caller.
+ */
+static u32 pci_bar_size(struct pci_dev *dev, unsigned int reg)
+{
+	u32 orig_reg, sz;
+	u16 orig_cmd;
+
+	pci_read_config_dword(dev, reg, &orig_reg);
+	pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
+
+	if (BAR_IS_MEMORY(orig_reg))
+		pci_write_config_word(dev, PCI_COMMAND,
+				      orig_cmd & ~PCI_COMMAND_MEMORY);
+	else
+		pci_write_config_word(dev, PCI_COMMAND, 
+				      orig_cmd & ~PCI_COMMAND_IO);
+
+	pci_write_config_dword(dev, reg, 0xffffffff);
+	pci_read_config_dword(dev, reg, &sz);
+	pci_write_config_dword(dev, reg, orig_reg);
+
+	pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
+
+	return sz;
+}
+
 static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, 
int rom)
 {
 	unsigned int pos, reg, next;
@@ -185,17 +227,15 @@ static void pci_read_bases(struct pci_dev *dev, 
unsigned int howmany, int rom)
 		res = &dev->resource[pos];
 		res->name = pci_name(dev);
 		reg = PCI_BASE_ADDRESS_0 + (pos << 2);
+
 		pci_read_config_dword(dev, reg, &l);
-		pci_write_config_dword(dev, reg, ~0);
-		pci_read_config_dword(dev, reg, &sz);
-		pci_write_config_dword(dev, reg, l);
+		sz = pci_bar_size(dev, reg);
 		if (!sz || sz == 0xffffffff)
 			continue;
 		if (l == 0xffffffff)
 			l = 0;
 		raw_sz = sz;
-		if ((l & PCI_BASE_ADDRESS_SPACE) ==
-				PCI_BASE_ADDRESS_SPACE_MEMORY) {
+		if (BAR_IS_MEMORY(l)) {
 			sz = pci_size(l, sz, (u32)PCI_BASE_ADDRESS_MEM_MASK);
 			/*
 			 * For 64bit prefetchable memory sz could be 0, if the
@@ -219,9 +259,7 @@ static void pci_read_bases(struct pci_dev *dev, 
unsigned int howmany, int rom)
 			u32 szhi, lhi;
 
 			pci_read_config_dword(dev, reg+4, &lhi);
-			pci_write_config_dword(dev, reg+4, ~0);
-			pci_read_config_dword(dev, reg+4, &szhi);
-			pci_write_config_dword(dev, reg+4, lhi);
+			szhi = pci_bar_size(dev, reg+4);
 			sz64 = ((u64)szhi << 32) | raw_sz;
 			l64 = ((u64)lhi << 32) | l;
 			sz64 = pci_size64(l64, sz64, PCI_BASE_ADDRESS_MEM_MASK);


  reply	other threads:[~2007-05-23 18:53 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-30  2:14 [RFC PATCH] PCI MMCONFIG: add validation against ACPI motherboard resources Robert Hancock
2007-04-30  2:59 ` Randy Dunlap
2007-04-30 22:59 ` Olivier Galibert
2007-04-30 23:26   ` Robert Hancock
2007-05-01 16:48   ` Jesse Barnes
2007-05-02  2:41   ` Jesse Barnes
2007-05-02  2:56     ` Jesse Barnes
2007-05-02  5:27       ` Jesse Barnes
2007-05-02 14:34         ` Robert Hancock
2007-05-02 17:57           ` Jesse Barnes
2007-05-02 23:45             ` Robert Hancock
2007-05-02 23:54               ` Jesse Barnes
2007-05-04 21:06                 ` Jesse Barnes
2007-05-05  0:22                   ` Robert Hancock
2007-05-21 19:10                   ` Jesse Barnes
2007-05-21 19:26                     ` Robert Hancock
2007-05-21 20:07                       ` Jesse Barnes
2007-05-21 20:22                         ` Jesse Barnes
2007-05-23  0:31                           ` Robert Hancock
2007-05-23  0:38                             ` Jesse Barnes
2007-05-23  0:53                               ` Robert Hancock
2007-05-23  0:56                                 ` Jesse Barnes
2007-05-23  1:06                               ` Robert Hancock
2007-05-23 18:52                                 ` Jesse Barnes [this message]
2007-05-23 20:20                                   ` Linus Torvalds
2007-05-23 20:38                                     ` Alan Cox
2007-05-23 20:45                                       ` Linus Torvalds
2007-05-23 20:49                                     ` Jesse Barnes
2007-05-23 20:56                                       ` Linus Torvalds
2007-05-23 21:03                                         ` Jesse Barnes
2007-05-23 21:09                                           ` Jeff Garzik
2007-05-23 21:35                                             ` Alan Cox
2007-05-23 21:35                                               ` Jeff Garzik
2007-05-23 21:37                                               ` Jesse Barnes
2007-05-23 21:42                                                 ` Jeff Garzik
2007-05-23 23:07                                             ` Stephen Hemminger
2007-05-23 21:54                                           ` Linus Torvalds
2007-05-23 22:06                                             ` Jesse Barnes
2007-05-23 22:16                                               ` Linus Torvalds
2007-05-23 22:28                                                 ` Jesse Barnes
2007-05-23 23:04                                                 ` David Miller
2007-05-23 23:11                                                   ` Jesse Barnes
2007-05-23 23:15                                                     ` Robert Hancock
2007-05-23 23:21                                                       ` Jesse Barnes
2007-05-23 21:20                                         ` Jesse Barnes
2007-05-23 22:24                                           ` Olivier Galibert
2007-05-23 22:31                                             ` Jesse Barnes
2007-05-23 22:48                                             ` Linus Torvalds
2007-05-23 22:55                                               ` Jesse Barnes
2007-05-24  0:21                                                 ` Linus Torvalds
2007-05-24  2:59                                                   ` Jesse Barnes
2007-05-24  3:18                                                     ` Linus Torvalds
2007-05-24  3:20                                                       ` Linus Torvalds
2007-05-24  3:40                                                         ` Jesse Barnes
2007-05-24  5:19                                                           ` Robert Hancock
2007-05-24  6:18                                                   ` Jeff Garzik
2007-05-24 15:42                                                     ` Linus Torvalds
2007-05-23 23:04                                     ` Robert Hancock
2007-05-23 23:04                                   ` Robert Hancock
2007-05-23 23:06                                     ` Jesse Barnes
2007-05-24  0:02 ` Jesse Barnes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200705231152.57796.jbarnes@virtuousgeek.org \
    --to=jbarnes@virtuousgeek.org \
    --cc=ak@suse.de \
    --cc=cebbert@redhat.com \
    --cc=galibert@pobox.com \
    --cc=hancockr@shaw.ca \
    --cc=lenb@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox