LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Terry Bowman <Terry.Bowman@amd.com>
To: Guenter Roeck <linux@roeck-us.net>, linux-watchdog@vger.kernel.org
Cc: wim@linux-watchdog.org, linux-kernel@vger.kernel.org,
	rrichter@amd.com, thomas.lendacky@amd.com
Subject: Re: [PATCH] Watchdog: sp5100_tco: Replace watchdog cd6h/cd7h port I/O accesses with MMIO accesses
Date: Mon, 16 Aug 2021 16:29:20 -0500	[thread overview]
Message-ID: <396d4558-9ddf-55e8-75bf-cb1a15de393a@amd.com> (raw)
In-Reply-To: <416a67a7-646b-eb8d-b617-80cbbbc028c6@roeck-us.net>



On 8/13/21 5:37 PM, Guenter Roeck wrote:
> On 8/13/21 2:32 PM, Terry Bowman wrote:
>> Use MMIO instead of port I/O during SMBus controller address discovery.
>> Also, update how EFCH capability is determined by replacing a family 
>> check
>> with a PCI revision ID check.
>>
>> cd6h/cd7h port I/O can be disabled on recent AMD hardware. Read 
>> accesses to
>> disabled cd6h/cd7h port I/O will return F's and written data is dropped.
>> The recommended workaround to handle disabled cd6h/cd7h port I/O is
>> replacing port I/O with MMIO accesses. The MMIO access method has been
>> available since at least SMBus controllers using PCI revision 0x59.
>>
>> The sp5100_tco driver uses a CPU family match of 17h to determine
>> EFCH_PM_DECODEEN_WDT_TMREN register support. Using a family check 
>> requires
>> driver updates for each new AMD CPU family following 17h. This patch
>> replaces the family check with a check for SMBus PCI revision ID 0x59 and
>> later. Note: Family 17h processors use SMBus PCI revision ID 0x59. The
>> intent is to use the PCI revision ID check to support future AMD 
>> processors
>> while minimizing required driver changes. The caveat with this change is
>> the sp5100_tco driver must be updated if a new AMD processor family 
>> changes
>> the EFCH design or the SMBus PCI ID value doesn't follow this pattern.
>>
>> Tested with forced WDT reset using `cat >> /dev/watchdog`.
>>
> 
> I am sorry, I don't understand why the new code can not use devm functions,
> why the new data structure is necessary in the first place, and why it is
> not possible to improve alignment with the existing code. This will require
> a substantial amount of time to review to ensure that the changes are not
> excessive (at first glance it for sure looks like that to me).
> 
> Guenter
> 

Hi Guenter,

I can change the patch to use devm functions as you mentioned. My
understanding is the patch's reservation and mapping related functions
are the focus. I originally chose not to use devm functions because the
patch's MMIO reserved and mapped resources are not held for the driver
lifetime as is the case for most device managed resources. The
sp5100_tco driver must only hold these MMIO resources briefly because
other drivers use the same EFCH MMIO registers. An example of another
driver using the same registers is the piix4_smbus driver (drivers/i2c
/busses/i2c-piix4.c). This patch can be changed to use the devm
functions but the driver may not benefit from the device management.

The 'struct efch_cfg' addition is needed for MMIO reads/writes as well
as during cleanup when leaving sp5100_region_setup(). This structure was
chosen to contain the data instead of passing multiple parameters to
each EFCH function called.

Do you have any recommendations for how to best improve the alignment?

Regards,
Terry

>> Signed-off-by: Terry Bowman <Terry.Bowman@amd.com>
>> Reviewed-by: Robert Richter <rrichter@amd.com>
>> Cc: Wim Van Sebroeck <wim@linux-watchdog.org>
>> Cc: Guenter Roeck <linux@roeck-us.net>
>> Cc: linux-kernel@vger.kernel.org
>> ---
>>   drivers/watchdog/sp5100_tco.c | 180 +++++++++++++++++++++++++++-------
>>   drivers/watchdog/sp5100_tco.h |   5 +
>>   2 files changed, 148 insertions(+), 37 deletions(-)
>>
>> diff --git a/drivers/watchdog/sp5100_tco.c 
>> b/drivers/watchdog/sp5100_tco.c
>> index a730ecbf78cd..cf1d0d96a731 100644
>> --- a/drivers/watchdog/sp5100_tco.c
>> +++ b/drivers/watchdog/sp5100_tco.c
>> @@ -44,6 +44,7 @@
>>   #include "sp5100_tco.h"
>>   #define TCO_DRIVER_NAME    "sp5100-tco"
>> +#define AMD_PCI_SMBUS_REVISION_MMIO             0x59
>>   /* internal variables */
>> @@ -51,6 +52,11 @@ enum tco_reg_layout {
>>       sp5100, sb800, efch
>>   };
>> +struct efch_cfg {
>> +    void __iomem *addr;
>> +    struct resource *res;
>> +};
>> +
>>   struct sp5100_tco {
>>       struct watchdog_device wdd;
>>       void __iomem *tcobase;
>> @@ -161,7 +167,133 @@ static void sp5100_tco_update_pm_reg8(u8 index, 
>> u8 reset, u8 set)
>>       outb(val, SP5100_IO_PM_DATA_REG);
>>   }
>> -static void tco_timer_enable(struct sp5100_tco *tco)
>> +static bool efch_use_mmio(void)
>> +{
>> +    return (sp5100_tco_pci->vendor == PCI_VENDOR_ID_AMD &&
>> +        sp5100_tco_pci->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
>> +        sp5100_tco_pci->revision >= AMD_PCI_SMBUS_REVISION_MMIO);
>> +}
>> +
>> +static u8 efch_read_pm_reg8(struct efch_cfg *cfg, u8 index)
>> +{
>> +    if (!cfg->addr)
>> +        return sp5100_tco_read_pm_reg8(index);
>> +
>> +    return readb(cfg->addr + index);
>> +}
>> +
>> +static void efch_update_pm_reg8(struct efch_cfg *cfg,
>> +                u8 index, u8 reset, u8 set)
>> +{
>> +    u8 val;
>> +
>> +    if (!cfg->addr) {
>> +        sp5100_tco_update_pm_reg8(index, reset, set);
>> +        return;
>> +    }
>> +
>> +    val = readb(cfg->addr + index);
>> +    val &= reset;
>> +    val |= set;
>> +    writeb(val, cfg->addr + index);
>> +}
>> +
>> +/* Return SMBus controller's MMIO address on success and 0 on error. */
>> +static u32 efch_setup_mmio_addr(struct efch_cfg *cfg)
>> +{
>> +    /*
>> +     * On EFCH devices, the EFCH_PM_DECODEEN_WDT_TMREN bit of
>> +     * EFCH_PM_DECODEEN not only enables the EFCH_PM_WDT_ADDR memory
>> +     * region, it also enables the watchdog itself.
>> +     * If mmio is enbaled then the WDT needs to be started if not
>> +     * already started.
>> +     */
>> +    if (cfg->addr) {
>> +        if (!(efch_read_pm_reg8(cfg, EFCH_PM_DECODEEN) &
>> +              EFCH_PM_DECODEEN_WDT_TMREN)) {
>> +            efch_update_pm_reg8(cfg, EFCH_PM_DECODEEN,
>> +                        0xff,
>> +                        EFCH_PM_DECODEEN_WDT_TMREN);
>> +        }
>> +    }
>> +
>> +    if (!(efch_read_pm_reg8(cfg, EFCH_PM_DECODEEN) &
>> +          EFCH_PM_DECODEEN_WDT_TMREN))
>> +        return 0;
>> +
>> +    return EFCH_PM_WDT_ADDR;
>> +}
>> +
>> +/*
>> + * Return SMBus controller's alternate MMIO address on success and 0 on
>> + * error.
>> + *
>> + * The alternate SMBus MMIO address is necessary if the address
>> + * returned from efch_setup_mmio_addr() is unreadable or the space
>> + * reservation fails.
>> + */
>> +static u32 efch_setup_alt_mmio_addr(struct efch_cfg *cfg)
>> +{
>> +    if (!(efch_read_pm_reg8(cfg, EFCH_PM_ISACONTROL) &
>> +          EFCH_PM_ISACONTROL_MMIOEN))
>> +        return 0;
>> +
>> +    return EFCH_PM_ACPI_MMIO_ADDR + EFCH_PM_ACPI_MMIO_WDT_OFFSET;
>> +}
>> +
>> +static int sp5100_region_setup(struct device *dev, struct efch_cfg *cfg)
>> +{
>> +    if (efch_use_mmio()) {
>> +        struct resource *res;
>> +        void __iomem *addr;
>> +
>> +        res = request_mem_region(EFCH_PM_ACPI_MMIO_PM_ADDR,
>> +                     EFCH_PM_ACPI_MMIO_PM_SIZE,
>> +                     "sp5100_tco");
>> +        if (!res) {
>> +            dev_err(dev,
>> +                "SMB base address memory region 0x%x already in use.\n",
>> +                EFCH_PM_ACPI_MMIO_PM_ADDR);
>> +            return -EBUSY;
>> +        }
>> +
>> +        addr = ioremap(EFCH_PM_ACPI_MMIO_PM_ADDR,
>> +                   EFCH_PM_ACPI_MMIO_PM_SIZE);
>> +        if (!addr) {
>> +            release_resource(res);
>> +            dev_err(dev, "SMB base address mapping failed.\n");
>> +            return -ENOMEM;
>> +        }
>> +
>> +        cfg->res = res;
>> +        cfg->addr = addr;
>> +        return 0;
>> +    }
>> +
>> +    /* Request the IO ports used by this driver */
>> +    if (!request_muxed_region(SP5100_IO_PM_INDEX_REG,
>> +                  SP5100_PM_IOPORTS_SIZE, "sp5100_tco")) {
>> +        dev_err(dev, "I/O address 0x%04x already in use\n",
>> +            SP5100_IO_PM_INDEX_REG);
>> +        return -EBUSY;
>> +    }
>> +
>> +    return 0;
>> +}
>> +
>> +static void sp5100_region_release(struct efch_cfg *cfg)
>> +{
>> +    if (!cfg->addr) {
>> +        release_region(SP5100_IO_PM_INDEX_REG,
>> +                   SP5100_PM_IOPORTS_SIZE);
>> +        return;
>> +    }
>> +
>> +    iounmap(cfg->addr);
>> +    release_resource(cfg->res);
>> +}
>> +
>> +static void tco_timer_enable(struct sp5100_tco *tco, struct efch_cfg 
>> *cfg)
>>   {
>>       u32 val;
>> @@ -197,9 +326,9 @@ static void tco_timer_enable(struct sp5100_tco *tco)
>>           break;
>>       case efch:
>>           /* Set the Watchdog timer resolution to 1 sec and enable */
>> -        sp5100_tco_update_pm_reg8(EFCH_PM_DECODEEN3,
>> -                      ~EFCH_PM_WATCHDOG_DISABLE,
>> -                      EFCH_PM_DECODEEN_SECOND_RES);
>> +        efch_update_pm_reg8(cfg, EFCH_PM_DECODEEN3,
>> +                    ~EFCH_PM_WATCHDOG_DISABLE,
>> +                    EFCH_PM_DECODEEN_SECOND_RES);
>>           break;
>>       }
>>   }
>> @@ -219,17 +348,14 @@ static int sp5100_tco_setupdevice(struct device 
>> *dev,
>>                     struct watchdog_device *wdd)
>>   {
>>       struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
>> +    struct efch_cfg cfg = {0};
>>       const char *dev_name;
>> -    u32 mmio_addr = 0, val;
>> +    u32 mmio_addr, val;
>>       int ret;
>> -    /* Request the IO ports used by this driver */
>> -    if (!request_muxed_region(SP5100_IO_PM_INDEX_REG,
>> -                  SP5100_PM_IOPORTS_SIZE, "sp5100_tco")) {
>> -        dev_err(dev, "I/O address 0x%04x already in use\n",
>> -            SP5100_IO_PM_INDEX_REG);
>> -        return -EBUSY;
>> -    }
>> +    ret = sp5100_region_setup(dev, &cfg);
>> +    if (ret)
>> +        return ret;
>>       /*
>>        * Determine type of southbridge chipset.
>> @@ -247,21 +373,7 @@ static int sp5100_tco_setupdevice(struct device 
>> *dev,
>>           break;
>>       case efch:
>>           dev_name = SB800_DEVNAME;
>> -        /*
>> -         * On Family 17h devices, the EFCH_PM_DECODEEN_WDT_TMREN bit of
>> -         * EFCH_PM_DECODEEN not only enables the EFCH_PM_WDT_ADDR memory
>> -         * region, it also enables the watchdog itself.
>> -         */
>> -        if (boot_cpu_data.x86 == 0x17) {
>> -            val = sp5100_tco_read_pm_reg8(EFCH_PM_DECODEEN);
>> -            if (!(val & EFCH_PM_DECODEEN_WDT_TMREN)) {
>> -                sp5100_tco_update_pm_reg8(EFCH_PM_DECODEEN, 0xff,
>> -                              EFCH_PM_DECODEEN_WDT_TMREN);
>> -            }
>> -        }
>> -        val = sp5100_tco_read_pm_reg8(EFCH_PM_DECODEEN);
>> -        if (val & EFCH_PM_DECODEEN_WDT_TMREN)
>> -            mmio_addr = EFCH_PM_WDT_ADDR;
>> +        mmio_addr = efch_setup_mmio_addr(&cfg);
>>           break;
>>       default:
>>           return -ENODEV;
>> @@ -307,13 +419,11 @@ static int sp5100_tco_setupdevice(struct device 
>> *dev,
>>               mmio_addr += SB800_PM_WDT_MMIO_OFFSET;
>>               break;
>>           case efch:
>> -            val = sp5100_tco_read_pm_reg8(EFCH_PM_ISACONTROL);
>> -            if (!(val & EFCH_PM_ISACONTROL_MMIOEN)) {
>> +            mmio_addr = efch_setup_alt_mmio_addr(&cfg);
>> +            if (!mmio_addr) {
>>                   ret = -ENODEV;
>>                   goto unreg_region;
>>               }
>> -            mmio_addr = EFCH_PM_ACPI_MMIO_ADDR +
>> -                    EFCH_PM_ACPI_MMIO_WDT_OFFSET;
>>               break;
>>           }
>>           dev_dbg(dev, "Got 0x%08x from SBResource_MMIO register\n",
>> @@ -338,7 +448,7 @@ static int sp5100_tco_setupdevice(struct device *dev,
>>       dev_info(dev, "Using 0x%08x for watchdog MMIO address\n", 
>> mmio_addr);
>>       /* Setup the watchdog timer */
>> -    tco_timer_enable(tco);
>> +    tco_timer_enable(tco, &cfg);
>>       val = readl(SP5100_WDT_CONTROL(tco->tcobase));
>>       if (val & SP5100_WDT_DISABLED) {
>> @@ -366,12 +476,8 @@ static int sp5100_tco_setupdevice(struct device 
>> *dev,
>>        */
>>       tco_timer_stop(wdd);
>> -    release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
>> -
>> -    return 0;
>> -
>>   unreg_region:
>> -    release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
>> +    sp5100_region_release(&cfg);
>>       return ret;
>>   }
>> diff --git a/drivers/watchdog/sp5100_tco.h 
>> b/drivers/watchdog/sp5100_tco.h
>> index adf015aa4126..2df8f8b2c55b 100644
>> --- a/drivers/watchdog/sp5100_tco.h
>> +++ b/drivers/watchdog/sp5100_tco.h
>> @@ -83,3 +83,8 @@
>>   #define EFCH_PM_ACPI_MMIO_ADDR        0xfed80000
>>   #define EFCH_PM_ACPI_MMIO_WDT_OFFSET    0x00000b00
>> +#define EFCH_PM_ACPI_MMIO_PM_OFFSET    0x00000300
>> +
>> +#define EFCH_PM_ACPI_MMIO_PM_ADDR    (EFCH_PM_ACPI_MMIO_ADDR +    \
>> +                     EFCH_PM_ACPI_MMIO_PM_OFFSET)
>> +#define EFCH_PM_ACPI_MMIO_PM_SIZE       8
>>
> 

  reply	other threads:[~2021-08-16 21:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-13 21:32 Terry Bowman
2021-08-13 22:37 ` Guenter Roeck
2021-08-16 21:29   ` Terry Bowman [this message]
2021-08-18 20:34     ` Guenter Roeck
2021-08-23 17:58       ` Terry Bowman
2021-09-06 12:05   ` Robert Richter

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=396d4558-9ddf-55e8-75bf-cb1a15de393a@amd.com \
    --to=terry.bowman@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=rrichter@amd.com \
    --cc=thomas.lendacky@amd.com \
    --cc=wim@linux-watchdog.org \
    --subject='Re: [PATCH] Watchdog: sp5100_tco: Replace watchdog cd6h/cd7h port I/O accesses with MMIO accesses' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).