From 4d6e19e457ac4d60c8c8a6b9e1df4d36828a5fe5 Mon Sep 17 00:00:00 2001 From: Sepherosa Ziehau Date: Tue, 27 Sep 2016 06:00:10 +0000 Subject: [PATCH] pci: Clear the MEM/PORT_EN bit when updating PCI BAR It's unsafe to update the BAR when the related EN bit is set. Submitted by: Dexuan Cui Reviewed by: jhb MFC after: 1 week Sponsored by: Microsoft Differential Revision: https://reviews.freebsd.org/D7914 --- sys/dev/pci/pci.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sys/dev/pci/pci.c b/sys/dev/pci/pci.c index 665f62e0911..8de548f0e2c 100644 --- a/sys/dev/pci/pci.c +++ b/sys/dev/pci/pci.c @@ -5000,6 +5000,7 @@ pci_reserve_map(device_t dev, device_t child, int type, int *rid, struct resource_list *rl = &dinfo->resources; struct resource *res; struct pci_map *pm; + uint16_t cmd; pci_addr_t map, testval; int mapsize; @@ -5089,8 +5090,17 @@ pci_reserve_map(device_t dev, device_t child, int type, int *rid, device_printf(child, "Lazy allocation of %#jx bytes rid %#x type %d at %#jx\n", count, *rid, type, rman_get_start(res)); + + /* Disable decoding via the CMD register before updating the BAR */ + cmd = pci_read_config(child, PCIR_COMMAND, 2); + pci_write_config(child, PCIR_COMMAND, + cmd & ~(PCI_BAR_MEM(map) ? PCIM_CMD_MEMEN : PCIM_CMD_PORTEN), 2); + map = rman_get_start(res); pci_write_bar(child, pm, map); + + /* Restore the original value of the CMD register */ + pci_write_config(child, PCIR_COMMAND, cmd, 2); out: return (res); }