Re: [PATCH] mmc: add quirk to disable eMMC cache for Micron eMMC v5.0 cards

2021-04-13 Thread Scott Branden
+Travis Dirkes (with corrected email),
 
 Could you provide more details on the Micron data error.
 
> On 2021-04-12 12:44 a.m., Ulf Hansson wrote:
>> + Bean Huo, Luca Porzio
>>
>> On Thu, 8 Apr 2021 at 19:59, Scott Branden  
>> wrote:
>>>
>>> From: Vladimir Olovyannikov 
>>>
>>> In certain rare combination of operations, Micron eMMC v5.0 cards
>>> may experience data errors if internal cache is enabled.
>>> This may lead to eMMC related data errors.
>>> Introduce a quirk to disable cache on these eMMC cards.
>>
>> Can you please elaborate on this, what combinations of operations are
>> you referring to - and what kind of data errors?
>>
>> I have also looped in some of the Micron guys, to let them chim in.
>>
>>>
>>> Signed-off-by: Vladimir Olovyannikov 
>>> Signed-off-by: Scott Branden 
>>
>> Kind regards
>> Uffe
>>
>>> ---
>>>  drivers/mmc/core/card.h   | 5 +
>>>  drivers/mmc/core/mmc.c| 4 ++--
>>>  drivers/mmc/core/quirks.h | 8 
>>>  include/linux/mmc/card.h  | 1 +
>>>  4 files changed, 16 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/mmc/core/card.h b/drivers/mmc/core/card.h
>>> index 7bd392d55cfa..22cea63ac359 100644
>>> --- a/drivers/mmc/core/card.h
>>> +++ b/drivers/mmc/core/card.h
>>> @@ -222,4 +222,9 @@ static inline int mmc_card_broken_hpi(const struct 
>>> mmc_card *c)
>>> return c->quirks & MMC_QUIRK_BROKEN_HPI;
>>>  }
>>>
>>> +static inline int mmc_card_broken_cache(const struct mmc_card *c)
>>> +{
>>> +   return c->quirks & MMC_QUIRK_BROKEN_CACHE;
>>> +}
>>> +
>>>  #endif
>>> diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
>>> index 8741271d3971..cd83b7f0e59c 100644
>>> --- a/drivers/mmc/core/mmc.c
>>> +++ b/drivers/mmc/core/mmc.c
>>> @@ -1820,12 +1820,12 @@ static int mmc_init_card(struct mmc_host *host, u32 
>>> ocr,
>>>  * sudden power failure tests. Let's extend the timeout to a 
>>> minimum of
>>>  * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards.
>>>  */
>>> -   if (card->ext_csd.cache_size > 0) {
>>> +   if (!mmc_card_broken_cache(card) && card->ext_csd.cache_size > 0) {
>>> unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS;
>>>
>>> timeout_ms = max(card->ext_csd.generic_cmd6_time, 
>>> timeout_ms);
>>> err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
>>> -   EXT_CSD_CACHE_CTRL, 1, timeout_ms);
>>> +EXT_CSD_CACHE_CTRL, 1, timeout_ms);
>>> if (err && err != -EBADMSG)
>>> goto free_card;
>>>
>>> diff --git a/drivers/mmc/core/quirks.h b/drivers/mmc/core/quirks.h
>>> index d68e6e513a4f..23972d87c82a 100644
>>> --- a/drivers/mmc/core/quirks.h
>>> +++ b/drivers/mmc/core/quirks.h
>>> @@ -116,6 +116,14 @@ static const struct mmc_fixup __maybe_unused 
>>> mmc_ext_csd_fixups[] = {
>>> MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_NUMONYX,
>>>   0x014e, add_quirk, MMC_QUIRK_BROKEN_HPI, 6),
>>>
>>> +   /*
>>> +* In certain rare combination of operations, Micron eMMC v5.0 cards
>>> +* may experience data errors if internal cache is enabled.
>>> +* Disabling cache for these cards eliminates the issue.
>>> +*/
>>> +   MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_MICRON,
>>> + 0x014e, add_quirk, MMC_QUIRK_BROKEN_CACHE, 7),
>>> +
>>> END_FIXUP
>>>  };
>>>
>>> diff --git a/include/linux/mmc/card.h b/include/linux/mmc/card.h
>>> index f9ad35dd6012..22f256a4e54e 100644
>>> --- a/include/linux/mmc/card.h
>>> +++ b/include/linux/mmc/card.h
>>> @@ -270,6 +270,7 @@ struct mmc_card {
>>>  #define MMC_QUIRK_BROKEN_IRQ_POLLING   (1<<11) /* Polling SDIO_CCCR_INTx 
>>> could create a fake interrupt */
>>>  #define MMC_QUIRK_TRIM_BROKEN  (1<<12) /* Skip trim */
>>>  #define MMC_QUIRK_BROKEN_HPI   (1<<13) /* Disable broken HPI 
>>> support */
>>> +#define MMC_QUIRK_BROKEN_CACHE (1<<14) /* Disable broken cache */
>>>
>>> boolreenable_cmdq;  /* Re-enable Command Queue 
>>> */
>>>
>>> --
>>> 2.17.1
>>>
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: [PATCH] mmc: add quirk to disable eMMC cache for Micron eMMC v5.0 cards

2021-04-13 Thread Scott Branden
+Travis Dirkes,

Could you provide more details on the Micron data error.

On 2021-04-12 12:44 a.m., Ulf Hansson wrote:
> + Bean Huo, Luca Porzio
> 
> On Thu, 8 Apr 2021 at 19:59, Scott Branden  wrote:
>>
>> From: Vladimir Olovyannikov 
>>
>> In certain rare combination of operations, Micron eMMC v5.0 cards
>> may experience data errors if internal cache is enabled.
>> This may lead to eMMC related data errors.
>> Introduce a quirk to disable cache on these eMMC cards.
> 
> Can you please elaborate on this, what combinations of operations are
> you referring to - and what kind of data errors?
> 
> I have also looped in some of the Micron guys, to let them chim in.
> 
>>
>> Signed-off-by: Vladimir Olovyannikov 
>> Signed-off-by: Scott Branden 
> 
> Kind regards
> Uffe
> 
>> ---
>>  drivers/mmc/core/card.h   | 5 +
>>  drivers/mmc/core/mmc.c| 4 ++--
>>  drivers/mmc/core/quirks.h | 8 
>>  include/linux/mmc/card.h  | 1 +
>>  4 files changed, 16 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/mmc/core/card.h b/drivers/mmc/core/card.h
>> index 7bd392d55cfa..22cea63ac359 100644
>> --- a/drivers/mmc/core/card.h
>> +++ b/drivers/mmc/core/card.h
>> @@ -222,4 +222,9 @@ static inline int mmc_card_broken_hpi(const struct 
>> mmc_card *c)
>> return c->quirks & MMC_QUIRK_BROKEN_HPI;
>>  }
>>
>> +static inline int mmc_card_broken_cache(const struct mmc_card *c)
>> +{
>> +   return c->quirks & MMC_QUIRK_BROKEN_CACHE;
>> +}
>> +
>>  #endif
>> diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
>> index 8741271d3971..cd83b7f0e59c 100644
>> --- a/drivers/mmc/core/mmc.c
>> +++ b/drivers/mmc/core/mmc.c
>> @@ -1820,12 +1820,12 @@ static int mmc_init_card(struct mmc_host *host, u32 
>> ocr,
>>  * sudden power failure tests. Let's extend the timeout to a minimum 
>> of
>>  * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards.
>>  */
>> -   if (card->ext_csd.cache_size > 0) {
>> +   if (!mmc_card_broken_cache(card) && card->ext_csd.cache_size > 0) {
>> unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS;
>>
>> timeout_ms = max(card->ext_csd.generic_cmd6_time, 
>> timeout_ms);
>> err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
>> -   EXT_CSD_CACHE_CTRL, 1, timeout_ms);
>> +EXT_CSD_CACHE_CTRL, 1, timeout_ms);
>> if (err && err != -EBADMSG)
>> goto free_card;
>>
>> diff --git a/drivers/mmc/core/quirks.h b/drivers/mmc/core/quirks.h
>> index d68e6e513a4f..23972d87c82a 100644
>> --- a/drivers/mmc/core/quirks.h
>> +++ b/drivers/mmc/core/quirks.h
>> @@ -116,6 +116,14 @@ static const struct mmc_fixup __maybe_unused 
>> mmc_ext_csd_fixups[] = {
>> MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_NUMONYX,
>>   0x014e, add_quirk, MMC_QUIRK_BROKEN_HPI, 6),
>>
>> +   /*
>> +* In certain rare combination of operations, Micron eMMC v5.0 cards
>> +* may experience data errors if internal cache is enabled.
>> +* Disabling cache for these cards eliminates the issue.
>> +*/
>> +   MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_MICRON,
>> + 0x014e, add_quirk, MMC_QUIRK_BROKEN_CACHE, 7),
>> +
>> END_FIXUP
>>  };
>>
>> diff --git a/include/linux/mmc/card.h b/include/linux/mmc/card.h
>> index f9ad35dd6012..22f256a4e54e 100644
>> --- a/include/linux/mmc/card.h
>> +++ b/include/linux/mmc/card.h
>> @@ -270,6 +270,7 @@ struct mmc_card {
>>  #define MMC_QUIRK_BROKEN_IRQ_POLLING   (1<<11) /* Polling SDIO_CCCR_INTx 
>> could create a fake interrupt */
>>  #define MMC_QUIRK_TRIM_BROKEN  (1<<12) /* Skip trim */
>>  #define MMC_QUIRK_BROKEN_HPI   (1<<13) /* Disable broken HPI 
>> support */
>> +#define MMC_QUIRK_BROKEN_CACHE (1<<14) /* Disable broken cache */
>>
>> boolreenable_cmdq;  /* Re-enable Command Queue */
>>
>> --
>> 2.17.1
>>



smime.p7s
Description: S/MIME Cryptographic Signature


[PATCH] mmc: add quirk to disable eMMC cache for Micron eMMC v5.0 cards

2021-04-08 Thread Scott Branden
From: Vladimir Olovyannikov 

In certain rare combination of operations, Micron eMMC v5.0 cards
may experience data errors if internal cache is enabled.
This may lead to eMMC related data errors.
Introduce a quirk to disable cache on these eMMC cards.

Signed-off-by: Vladimir Olovyannikov 
Signed-off-by: Scott Branden 
---
 drivers/mmc/core/card.h   | 5 +
 drivers/mmc/core/mmc.c| 4 ++--
 drivers/mmc/core/quirks.h | 8 
 include/linux/mmc/card.h  | 1 +
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/drivers/mmc/core/card.h b/drivers/mmc/core/card.h
index 7bd392d55cfa..22cea63ac359 100644
--- a/drivers/mmc/core/card.h
+++ b/drivers/mmc/core/card.h
@@ -222,4 +222,9 @@ static inline int mmc_card_broken_hpi(const struct mmc_card 
*c)
return c->quirks & MMC_QUIRK_BROKEN_HPI;
 }
 
+static inline int mmc_card_broken_cache(const struct mmc_card *c)
+{
+   return c->quirks & MMC_QUIRK_BROKEN_CACHE;
+}
+
 #endif
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index 8741271d3971..cd83b7f0e59c 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -1820,12 +1820,12 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
 * sudden power failure tests. Let's extend the timeout to a minimum of
 * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards.
 */
-   if (card->ext_csd.cache_size > 0) {
+   if (!mmc_card_broken_cache(card) && card->ext_csd.cache_size > 0) {
unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS;
 
timeout_ms = max(card->ext_csd.generic_cmd6_time, timeout_ms);
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
-   EXT_CSD_CACHE_CTRL, 1, timeout_ms);
+EXT_CSD_CACHE_CTRL, 1, timeout_ms);
if (err && err != -EBADMSG)
goto free_card;
 
diff --git a/drivers/mmc/core/quirks.h b/drivers/mmc/core/quirks.h
index d68e6e513a4f..23972d87c82a 100644
--- a/drivers/mmc/core/quirks.h
+++ b/drivers/mmc/core/quirks.h
@@ -116,6 +116,14 @@ static const struct mmc_fixup __maybe_unused 
mmc_ext_csd_fixups[] = {
MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_NUMONYX,
  0x014e, add_quirk, MMC_QUIRK_BROKEN_HPI, 6),
 
+   /*
+* In certain rare combination of operations, Micron eMMC v5.0 cards
+* may experience data errors if internal cache is enabled.
+* Disabling cache for these cards eliminates the issue.
+*/
+   MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_MICRON,
+ 0x014e, add_quirk, MMC_QUIRK_BROKEN_CACHE, 7),
+
END_FIXUP
 };
 
diff --git a/include/linux/mmc/card.h b/include/linux/mmc/card.h
index f9ad35dd6012..22f256a4e54e 100644
--- a/include/linux/mmc/card.h
+++ b/include/linux/mmc/card.h
@@ -270,6 +270,7 @@ struct mmc_card {
 #define MMC_QUIRK_BROKEN_IRQ_POLLING   (1<<11) /* Polling SDIO_CCCR_INTx could 
create a fake interrupt */
 #define MMC_QUIRK_TRIM_BROKEN  (1<<12) /* Skip trim */
 #define MMC_QUIRK_BROKEN_HPI   (1<<13) /* Disable broken HPI support */
+#define MMC_QUIRK_BROKEN_CACHE (1<<14) /* Disable broken cache */
 
boolreenable_cmdq;  /* Re-enable Command Queue */
 
-- 
2.17.1



Re: [PATCH 1/4] dt-bindings: mmc: iproc-sdhci: Convert to json-schema

2021-03-24 Thread Scott Branden
On 2021-03-24 9:35 a.m., Nicolas Saenz Julienne wrote:
> On Wed, 2021-03-24 at 09:27 -0700, Scott Branden wrote:
>> On 2021-03-23 2:24 p.m., Nicolas Saenz Julienne wrote:
>>> On Tue, 2021-03-23 at 15:08 -0600, Rob Herring wrote:
>>>> On Mon, Mar 22, 2021 at 12:11:29PM -0700, Scott Branden wrote:
>>>>> On 2021-03-22 11:58 a.m., Nicolas Saenz Julienne wrote:
>>>>>> Convert the brcm,iproc-sdhci binding to DT schema format using 
>>>>>> json-schema
>>>>>>
>>>>>> Signed-off-by: Nicolas Saenz Julienne 
>>>>>> ---
>>>>>>  .../bindings/mmc/brcm,iproc-sdhci.yaml| 58 +++
>>>>>>  .../bindings/mmc/brcm,sdhci-iproc.txt | 37 
>>>>>>  2 files changed, 58 insertions(+), 37 deletions(-)
>>>>>>  create mode 100644 
>>>>>> Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
>>>>>>  delete mode 100644 
>>>>>> Documentation/devicetree/bindings/mmc/brcm,sdhci-iproc.txt
>>>>>>
>>>>>> diff --git a/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml 
>>>>>> b/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
>>>>>> new file mode 100644
>>>>>> index ..19d84f3ef9e6
>>>>>> --- /dev/null
>>>>>> +++ b/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
>>>>>> @@ -0,0 +1,58 @@
>>>>>> +# SPDX-License-Identifier: GPL-2.0
>>>>>> +%YAML 1.2
>>>>>> +---
>>>>>> +$id: http://devicetree.org/schemas/mmc/brcm,iproc-sdhci.yaml#
>>>>>> +$schema: http://devicetree.org/meta-schemas/core.yaml#
>>>>>> +
>>>>>> +title: Broadcom IPROC SDHCI controller
>>>>>> +
>>>>>> +maintainers:
>>>>>> +  - Nicolas Saenz Julienne 
>>>>> This is already covered in the MAINTAINERS section via "N:iproc".
>>>>> M:Ray Jui 
>>>>>
>>>>> M:Scott Branden 
>>>>>
>>>>> M:bcm-kernel-feedback-l...@broadcom.com
>>>>
>>>> Maybe so, but still required here. The problem is there is no 
>>>> MAINTAINERS file in the DT only tree[1].
>>>
>>> Well in that case, if Scott and Ray are OK with it I'll add them.
>> I do not know what the "maintainers" section in the yaml file is used to 
>> indicate.
>> If it is maintainer for the driver then please add the duplicate of what is
>> already in the MAINTAINERS file.  If it is for maintainer of devicetrees that
>> use this driver then no need to add us.
> 
> From the dt bindings documentation:
> 
> maintainers
>   A DT specific property. Contains a list of email address(es)
>   for maintainers of this binding.
> 
> That's the maintainers for the bindings, not the devicetrees consuming them. I
> belive it makes sense for you guys to maintain it as it has a strong
> relationship to driver changes. But if you're not interested I'll do it 
> myself.
Sure, you can add us as well.
> 
> Regards,
> Nicolas
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: [PATCH 1/4] dt-bindings: mmc: iproc-sdhci: Convert to json-schema

2021-03-24 Thread Scott Branden
On 2021-03-23 2:24 p.m., Nicolas Saenz Julienne wrote:
> On Tue, 2021-03-23 at 15:08 -0600, Rob Herring wrote:
>> On Mon, Mar 22, 2021 at 12:11:29PM -0700, Scott Branden wrote:
>>> On 2021-03-22 11:58 a.m., Nicolas Saenz Julienne wrote:
>>>> Convert the brcm,iproc-sdhci binding to DT schema format using json-schema
>>>>
>>>> Signed-off-by: Nicolas Saenz Julienne 
>>>> ---
>>>>  .../bindings/mmc/brcm,iproc-sdhci.yaml| 58 +++
>>>>  .../bindings/mmc/brcm,sdhci-iproc.txt | 37 
>>>>  2 files changed, 58 insertions(+), 37 deletions(-)
>>>>  create mode 100644 
>>>> Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
>>>>  delete mode 100644 
>>>> Documentation/devicetree/bindings/mmc/brcm,sdhci-iproc.txt
>>>>
>>>> diff --git a/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml 
>>>> b/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
>>>> new file mode 100644
>>>> index ..19d84f3ef9e6
>>>> --- /dev/null
>>>> +++ b/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
>>>> @@ -0,0 +1,58 @@
>>>> +# SPDX-License-Identifier: GPL-2.0
>>>> +%YAML 1.2
>>>> +---
>>>> +$id: http://devicetree.org/schemas/mmc/brcm,iproc-sdhci.yaml#
>>>> +$schema: http://devicetree.org/meta-schemas/core.yaml#
>>>> +
>>>> +title: Broadcom IPROC SDHCI controller
>>>> +
>>>> +maintainers:
>>>> +  - Nicolas Saenz Julienne 
>>> This is already covered in the MAINTAINERS section via "N:  iproc".
>>> M:  Ray Jui 
>>>
>>> M:  Scott Branden 
>>>
>>> M:  bcm-kernel-feedback-l...@broadcom.com
>>
>> Maybe so, but still required here. The problem is there is no 
>> MAINTAINERS file in the DT only tree[1].
> 
> Well in that case, if Scott and Ray are OK with it I'll add them.
I do not know what the "maintainers" section in the yaml file is used to 
indicate.
If it is maintainer for the driver then please add the duplicate of what is 
already in the MAINTAINERS file.
If it is for maintainer of devicetrees that use this driver then no need to add 
us.
> 
> Regards,
> Nicolas
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: [PATCH 1/4] dt-bindings: mmc: iproc-sdhci: Convert to json-schema

2021-03-22 Thread Scott Branden
On 2021-03-22 11:58 a.m., Nicolas Saenz Julienne wrote:
> Convert the brcm,iproc-sdhci binding to DT schema format using json-schema
> 
> Signed-off-by: Nicolas Saenz Julienne 
> ---
>  .../bindings/mmc/brcm,iproc-sdhci.yaml| 58 +++
>  .../bindings/mmc/brcm,sdhci-iproc.txt | 37 
>  2 files changed, 58 insertions(+), 37 deletions(-)
>  create mode 100644 
> Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
>  delete mode 100644 Documentation/devicetree/bindings/mmc/brcm,sdhci-iproc.txt
> 
> diff --git a/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml 
> b/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
> new file mode 100644
> index ..19d84f3ef9e6
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/mmc/brcm,iproc-sdhci.yaml
> @@ -0,0 +1,58 @@
> +# SPDX-License-Identifier: GPL-2.0
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/mmc/brcm,iproc-sdhci.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Broadcom IPROC SDHCI controller
> +
> +maintainers:
> +  - Nicolas Saenz Julienne 
This is already covered in the MAINTAINERS section via "N:  iproc".
M:  Ray Jui 

M:  Scott Branden 

M:  bcm-kernel-feedback-l...@broadcom.com
> +
> +allOf:
> +  - $ref: mmc-controller.yaml#
> +
> +properties:
> +  compatible:
> +enum:
> +  - brcm,bcm2835-sdhci
> +  - brcm,bcm2711-emmc2
> +  - brcm,sdhci-iproc-cygnus
> +  - brcm,sdhci-iproc
> +
> +  reg:
> +minItems: 1
> +
> +  interrupts:
> +maxItems: 1
> +
> +  clocks:
> +maxItems: 1
> +description:
> +  Handle to core clock for the sdhci controller.
> +
> +  sdhci,auto-cmd12:
> +type: boolean
> +description: Specifies that controller should use auto CMD12
> +
> +required:
> +  - compatible
> +  - reg
> +  - interrupts
> +  - clocks
> +
> +unevaluatedProperties: false
> +
> +examples:
> +  - |
> +sdhci0: sdhci@18041000 {
> +  compatible = "brcm,sdhci-iproc-cygnus";
> +  reg = <0x18041000 0x100>;
> +  interrupts = ;
> +  clocks = <_clks BCM_CYGNUS_LCPLL0_SDIO_CLK>;
> +  bus-width = <4>;
> +  sdhci,auto-cmd12;
> +  no-1-8-v;
> +};
> +
> +...
> diff --git a/Documentation/devicetree/bindings/mmc/brcm,sdhci-iproc.txt 
> b/Documentation/devicetree/bindings/mmc/brcm,sdhci-iproc.txt
> deleted file mode 100644
> index 09d87cc1182a..
> --- a/Documentation/devicetree/bindings/mmc/brcm,sdhci-iproc.txt
> +++ /dev/null
> @@ -1,37 +0,0 @@
> -Broadcom IPROC SDHCI controller
> -
> -This file documents differences between the core properties described
> -by mmc.txt and the properties that represent the IPROC SDHCI controller.
> -
> -Required properties:
> -- compatible : Should be one of the following
> -"brcm,bcm2835-sdhci"
> -"brcm,bcm2711-emmc2"
> -"brcm,sdhci-iproc-cygnus"
> -"brcm,sdhci-iproc"
> -
> -Use brcm2835-sdhci for the eMMC controller on the BCM2835 (Raspberry Pi) and
> -bcm2711-emmc2 for the additional eMMC2 controller on BCM2711.
> -
> -Use sdhci-iproc-cygnus for Broadcom SDHCI Controllers
> -restricted to 32bit host accesses to SDHCI registers.
> -
> -Use sdhci-iproc for Broadcom SDHCI Controllers that allow standard
> -8, 16, 32-bit host access to SDHCI register.
> -
> -- clocks : The clock feeding the SDHCI controller.
> -
> -Optional properties:
> -  - sdhci,auto-cmd12: specifies that controller should use auto CMD12.
> -
> -Example:
> -
> -sdhci0: sdhci@18041000 {
> - compatible = "brcm,sdhci-iproc-cygnus";
> - reg = <0x18041000 0x100>;
> - interrupts = ;
> - clocks = <_clks BCM_CYGNUS_LCPLL0_SDIO_CLK>;
> - bus-width = <4>;
> - sdhci,auto-cmd12;
> - no-1-8-v;
> -};
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: [PATCH 4/4] ARM: dts: Fix-up EMMC2 controller's frequency

2021-03-22 Thread Scott Branden
1st line of commit should be ARM: dts: bcm2711

On 2021-03-22 11:58 a.m., Nicolas Saenz Julienne wrote:
> From: Nicolas Saenz Julienne 
> 
> Force emmc2's frequency to 150MHz as the default 100MHz (set by FW)
> seems to interfere with the VPU clock when setup at frequencies bigger
> than 500MHz (a pretty common case). This ends up causing unwarranted
> SDHCI CMD hangs  when no SD card is present.
> 
> Signed-off-by: Nicolas Saenz Julienne 
> ---
>  arch/arm/boot/dts/bcm2711-rpi-4-b.dts | 6 ++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts 
> b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
> index 3b4ab947492a..9aa8408d9960 100644
> --- a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
> +++ b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
> @@ -257,6 +257,12 @@  {
>   vqmmc-supply = <_io_1v8_reg>;
>   vmmc-supply = <_vcc_reg>;
>   broken-cd;
> + /*
> +  * Force the frequency to 150MHz as the default 100MHz seems to
> +  * interfere with the VPU clock when setup at frequencies bigger than
> +  * 500MHz, causing unwarranted CMD hangs.
> +  */
> + clock-frequency = <15000>;
>   status = "okay";
>  };
>  
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: [PATCH] ASoC: bcm: add missing call to of_node_put()

2021-03-08 Thread Scott Branden
Hi Yang,

On 2021-03-07 11:18 p.m., Yang Li wrote:
> In one of the error paths of the for_each_child_of_node() loop,
> add missing call to of_node_put().
> 
> Fix the following coccicheck warning:
> ./sound/soc/bcm/cygnus-ssp.c:1346:1-33: WARNING: Function
> "for_each_available_child_of_node" should have of_node_put() before
> return around line 1352.
> 
> Reported-by: Abaci Robot 
> Signed-off-by: Yang Li 
> ---
>  sound/soc/bcm/cygnus-ssp.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/sound/soc/bcm/cygnus-ssp.c b/sound/soc/bcm/cygnus-ssp.c
> index 6e634b4..aa16a23 100644
> --- a/sound/soc/bcm/cygnus-ssp.c
> +++ b/sound/soc/bcm/cygnus-ssp.c
> @@ -1348,8 +1348,10 @@ static int cygnus_ssp_probe(struct platform_device 
> *pdev)
>   _ssp_dai[active_port_count]);
>  
>   /* negative is err, 0 is active and good, 1 is disabled */
> - if (err < 0)
> + if (err < 0) {
> + of_node_put(child_node);
If such is needed in probe what about the other child_nodes in 
for_each_child_of_node loop that have already been parsed and succeeded.  Do 
they need additional cleanup as well?
>   return err;
> + }
>   else if (!err) {
>   dev_dbg(dev, "Activating DAI: %s\n",
>   cygnus_ssp_dai[active_port_count].name);
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: [PATCH 1/2] dt-bindings: rng: bcm2835: document reset support

2021-02-23 Thread Scott Branden
On 2021-02-23 8:36 a.m., Florian Fainelli wrote:
> 
> 
> On 2/23/2021 8:01 AM, Álvaro Fernández Rojas wrote:
>> Some devices may need to perform a reset before using the RNG, such as the
>> BCM6368.
>>
>> Signed-off-by: Álvaro Fernández Rojas 
> 
> Since the reset is unique to the 6368, you may want to make the property
> mandatory for the 6368 compatible string and optional otherwise.
> 
Perhaps the reset could be done at an earlier boot stage as well and then the
reset would even be optional on 6368?


smime.p7s
Description: S/MIME Cryptographic Signature


Re: [PATCH v2] hwrng: iproc: set quality to 1024

2021-02-22 Thread Scott Branden
Looks ok to me.

On 2021-02-20 12:01 p.m., Álvaro Fernández Rojas wrote:
> This allows khwrngd to make use of iproc-rng200.
> 
> Justification:
> cat /dev/hwrng | rngtest -c 1000
> rngtest 6.10
> Copyright (c) 2004 by Henrique de Moraes Holschuh
> This is free software; see the source for copying conditions.  There is NO 
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> 
> rngtest: starting FIPS tests...
> rngtest: bits received from input: 2032
> rngtest: FIPS 140-2 successes: 1000
> rngtest: FIPS 140-2 failures: 0
> rngtest: FIPS 140-2(2001-10-10) Monobit: 0
> rngtest: FIPS 140-2(2001-10-10) Poker: 0
> rngtest: FIPS 140-2(2001-10-10) Runs: 0
> rngtest: FIPS 140-2(2001-10-10) Long run: 0
> rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
> rngtest: input channel speed: (min=107.179; avg=200.770; 
> max=9765625.000)Kibits/s
> rngtest: FIPS tests speed: (min=34.742; avg=39.905; max=66.458)Mibits/s
> rngtest: Program run time: 97829648 microseconds
> 
> 1000 successes and 0 failures -> 100% success rate
> 
> Signed-off-by: Álvaro Fernández Rojas 
Acked-by: Scott Branden 
> ---
>  v2: set quality to 1024 and add justification
> 
>  drivers/char/hw_random/iproc-rng200.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/char/hw_random/iproc-rng200.c 
> b/drivers/char/hw_random/iproc-rng200.c
> index 01583faf9893..d22406528ac5 100644
> --- a/drivers/char/hw_random/iproc-rng200.c
> +++ b/drivers/char/hw_random/iproc-rng200.c
> @@ -199,6 +199,7 @@ static int iproc_rng200_probe(struct platform_device 
> *pdev)
>   priv->rng.read = iproc_rng200_read;
>   priv->rng.init = iproc_rng200_init;
>   priv->rng.cleanup = iproc_rng200_cleanup;
> + priv->rng.quality = 1024;
>  
>   /* Register driver */
>   ret = devm_hwrng_register(dev, >rng);
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: 5.10 LTS Kernel: 2 or 6 years?

2021-02-18 Thread Scott Branden
Hi Willy,

On 2021-02-18 1:00 p.m., Willy Tarreau wrote:
> On Thu, Feb 18, 2021 at 12:16:50PM -0800, Scott Branden wrote:
>> On 2021-02-18 10:36 a.m., Greg Kroah-Hartman wrote:
>>> On Thu, Feb 18, 2021 at 07:20:50PM +0100, Willy Tarreau wrote:
>>>> On Thu, Feb 18, 2021 at 06:53:56PM +0100, Greg Kroah-Hartman wrote:
>>>>> On Thu, Feb 18, 2021 at 09:21:13AM -0800, Florian Fainelli wrote:
>>>>>> As a company, we are most likely shooting ourselves in the foot by not
>>>>>> having a point of coordination with the Linux Foundation and key people
>>>>>> like you, Greg and other participants in the stable kernel.
>>>>>
>>>>> What does the LF have to do with this?
>>>>>
>>>>> We are here, on the mailing lists, working with everyone.  Just test the
>>>>> -rc releases we make and let us know if they work or not for you, it's
>>>>> not a lot of "coordination" needed at all.
>>>>>
>>>>> Otherwise, if no one is saying that they are going to need these for 6
>>>>> years and are willing to use it in their project (i.e. and test it),
>>>>> there's no need for us to maintain it for that long, right?
>>>>
>>>> Greg, please remember I expressed I really need them for slightly more than
>>>> 3 years (say 3.5-4) :-) I'm fine with helping a bit more as time permits if
>>>> this saves me from having to take over these kernels after you, like in the
>>>> past, but I cannot engage on the regularity of my availability.
>>>
>>> Ok, great!
>>>
>>> That's one person/company saying they can help out (along with what CIP
>>> has been stating.)
>>>
>>> What about others?  Broadcom started this conversation, odd that they
>>> don't seem to want to help out :)
>> Greg, I'm sorry but I'm not in a position to provide such a commitment.
> 
> Are you at least in a position to defend that ? There are necessarily
> some people in your company who understand the benefits of using open
> source provdided for free by others and who understand that devoting
> a few people's time to this task is extremely cheap compared to the
> amount of work required by having to do it entirely yourself for a
> lower quality.
> 
>> My original question arose because the 5.10 kernel is declared as 2 years LTS
>> while older LTS kernels are now 6 years.
> (...)
>> If all LTS kernels were declared as 3.5-4 years as Willy commented this would
>> solve a few issues. 6 year LTS kernels would only have a maximum 1 year
>> lifespan over the latest declared LTS kernel. Also, many products take a year
>> or more to develop, there isn't any life left in an LTS kernel if it is only
>> 2 years.
> 
> We all have the same problem regarding this but how do you want Greg to
> engage into such a task by himself if he's not certain he can count on
> others to help ? The few of us having worked on extended kernels know
> that there's a limit around 2.5 years beyond which backports become much
> harder to perform and to test. Doing it every year would result in 6 LTS
> kernels to maintain in addition to the last 1-2 stable ones. That becomes
> a huge amount of work! I even think that having one LTS kernel every 2
> years but maintained one extra year (e.g. 5 vs 4 in my case) would reduce
> the effort.
> 
>> After 1-3 years of kernel age the relevant parties that want to invest and
>> care about supporting specific kernel versions longer should become apparent
>> and could commit to longer support.
> 
> But that's exactly what's currently being done. Greg initially commits
> to 2 years hoping to get some help to pursue this longer, and this causes
> trouble to some of us not being certain upfront whether or not we're choosing
> the right kernel. So only the solution I'm seeing is for Greg to know
> early who jumps in so that those of us without the power or skill to
> entirely maintain a kernel by themselves know early which version to
> choose. Quite frankly if we ship an LTS kernel in a product, the least
> we can do is to give back a little bit to make sure the situation remains
> durable.
> 
> As such even if you are not in a position to provide such a commitment,
> I'd appreciate it if you would bring these arguments to those who are in
> such a position, so that I don't end up as one of the too few ones having
> to share a significant part of that task to make sure this valuable kernel
> continues to exist.
Thanks - will forward such info as necessary.
> 
> Thanks,
> Willy
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: 5.10 LTS Kernel: 2 or 6 years?

2021-02-18 Thread Scott Branden
On 2021-02-18 1:39 p.m., Sasha Levin wrote:
> On Thu, Feb 18, 2021 at 12:16:50PM -0800, Scott Branden wrote:
>> On 2021-02-18 10:36 a.m., Greg Kroah-Hartman wrote:
>>> On Thu, Feb 18, 2021 at 07:20:50PM +0100, Willy Tarreau wrote:
>>>> On Thu, Feb 18, 2021 at 06:53:56PM +0100, Greg Kroah-Hartman wrote:
>>>>> On Thu, Feb 18, 2021 at 09:21:13AM -0800, Florian Fainelli wrote:
>>>>>> As a company, we are most likely shooting ourselves in the foot by not
>>>>>> having a point of coordination with the Linux Foundation and key people
>>>>>> like you, Greg and other participants in the stable kernel.
>>>>>
>>>>> What does the LF have to do with this?
>>>>>
>>>>> We are here, on the mailing lists, working with everyone.  Just test the
>>>>> -rc releases we make and let us know if they work or not for you, it's
>>>>> not a lot of "coordination" needed at all.
>>>>>
>>>>> Otherwise, if no one is saying that they are going to need these for 6
>>>>> years and are willing to use it in their project (i.e. and test it),
>>>>> there's no need for us to maintain it for that long, right?
>>>>
>>>> Greg, please remember I expressed I really need them for slightly more than
>>>> 3 years (say 3.5-4) :-) I'm fine with helping a bit more as time permits if
>>>> this saves me from having to take over these kernels after you, like in the
>>>> past, but I cannot engage on the regularity of my availability.
>>>
>>> Ok, great!
>>>
>>> That's one person/company saying they can help out (along with what CIP
>>> has been stating.)
>>>
>>> What about others?  Broadcom started this conversation, odd that they
>>> don't seem to want to help out :)
>> Greg, I'm sorry but I'm not in a position to provide such a commitment.
>>
>> My original question arose because the 5.10 kernel is declared as 2 years 
>> LTS while older LTS kernels are now 6 years.
>> One problem this has created is requests to provide silicon support in an 
>> older kernel version (for a new project) rather than starting from a newer 
>> kernel version that more properly supports the (silicon and non-silicon) 
>> features.
> 
> So this sounds like you have boatloads of out-of-tree code and need a
> stable kernel to avoid having to rebase that code. This is not why the
> LTS trees are around.
My question centered around the inconsistency of a 2 year LTS kernel and some 
of the problems
of having older LTS kernels declared as 6 years.

The discussion has gone sideways from there with many other assumptions.

There is not always control over what kernel version a customer uses,
how many hacks they have in it, when they upgrade it, what other vendor changes 
are in it, etc.

But, will keep this email chain to forward to others. It may help influence 
some decisions.
> 
> For new projects, the easiest route is to upstream your stuff and ship
> the latest kernel.
> 
>> If all LTS kernels were declared as 3.5-4 years as Willy commented this 
>> would solve a few issues.
>> 6 year LTS kernels would only have a maximum 1 year lifespan over the latest 
>> declared LTS kernel.
>> Also, many products take a year or more to develop, there isn't any life 
>> left in an LTS kernel if it is only 2 years.
> 
> Products are supposed to upgrade their kernel. If you released something
> with, for example, a 5.4 kernel, doesn't mean you're forever stuck on a
> 5.4 kernel for that product.
> 
>> After 1-3 years of kernel age the relevant parties that want to invest and 
>> care about supporting specific kernel versions longer should become apparent 
>> and could commit to longer support.  Perhaps you move the burden of 6 years 
>> LTS elsewhere to longer term projects.  But, I'm sure many are happy because 
>> you continue doing such a great job in a central location, especially those 
>> whose product lifespan is around 6 years.
> 
> But this is exactly what's happening now: we support LTS kernels for two
> years, and after that interested parties can figure it out on their own
> if it's worth it for them to keep going.
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: 5.10 LTS Kernel: 2 or 6 years?

2021-02-18 Thread Scott Branden
On 2021-02-18 10:36 a.m., Greg Kroah-Hartman wrote:
> On Thu, Feb 18, 2021 at 07:20:50PM +0100, Willy Tarreau wrote:
>> On Thu, Feb 18, 2021 at 06:53:56PM +0100, Greg Kroah-Hartman wrote:
>>> On Thu, Feb 18, 2021 at 09:21:13AM -0800, Florian Fainelli wrote:
 As a company, we are most likely shooting ourselves in the foot by not
 having a point of coordination with the Linux Foundation and key people
 like you, Greg and other participants in the stable kernel.
>>>
>>> What does the LF have to do with this?
>>>
>>> We are here, on the mailing lists, working with everyone.  Just test the
>>> -rc releases we make and let us know if they work or not for you, it's
>>> not a lot of "coordination" needed at all.
>>>
>>> Otherwise, if no one is saying that they are going to need these for 6
>>> years and are willing to use it in their project (i.e. and test it),
>>> there's no need for us to maintain it for that long, right?
>>
>> Greg, please remember I expressed I really need them for slightly more than
>> 3 years (say 3.5-4) :-) I'm fine with helping a bit more as time permits if
>> this saves me from having to take over these kernels after you, like in the
>> past, but I cannot engage on the regularity of my availability.
> 
> Ok, great!
> 
> That's one person/company saying they can help out (along with what CIP
> has been stating.)
> 
> What about others?  Broadcom started this conversation, odd that they
> don't seem to want to help out :)
Greg, I'm sorry but I'm not in a position to provide such a commitment.

My original question arose because the 5.10 kernel is declared as 2 years LTS 
while older LTS kernels are now 6 years.
One problem this has created is requests to provide silicon support in an older 
kernel version (for a new project) rather than starting from a newer kernel 
version that more properly supports the (silicon and non-silicon) features.  

If all LTS kernels were declared as 3.5-4 years as Willy commented this would 
solve a few issues.
6 year LTS kernels would only have a maximum 1 year lifespan over the latest 
declared LTS kernel.
Also, many products take a year or more to develop, there isn't any life left 
in an LTS kernel if it is only 2 years.

After 1-3 years of kernel age the relevant parties that want to invest and care 
about supporting specific kernel versions longer should become apparent and 
could commit to longer support.  Perhaps you move the burden of 6 years LTS 
elsewhere to longer term projects.  But, I'm sure many are happy because you 
continue doing such a great job in a central location, especially those whose 
product lifespan is around 6 years.
> 
> thanks,
> 
> greg k-h
> 



smime.p7s
Description: S/MIME Cryptographic Signature


Re: 5.10 LTS Kernel: 2 or 6 years?

2021-02-17 Thread Scott Branden
Hi Greg,

On 2021-02-17 1:40 a.m., Greg Kroah-Hartman wrote:
> On Tue, Jan 26, 2021 at 07:51:18PM +0100, Greg Kroah-Hartman wrote:
>> On Tue, Jan 26, 2021 at 10:30:16AM -0800, Scott Branden wrote:
>>> Hi Greg,
>>>
>>>
>>> On 2021-01-25 11:29 p.m., Greg Kroah-Hartman wrote:
>>>> On Mon, Jan 25, 2021 at 11:55:11AM -0800, Scott Branden wrote:
>>>>> Hi All,
>>>>>
>>>>> The 5.10 LTS kernel being officially LTS supported for 2 years presents a 
>>>>> problem:
>>>>> why would anyone select a 5.10 kernel with 2 year LTS when 5.4 kernel has 
>>>>> a 6 year LTS.
>>>> Because they want to use all of the latest stuff that 5.10 provides
>>>> them.  Don't you want faster and more secure kernels for your devices?
>>> Yes, 5.10 is a more secure and less buggy kernel than 5.4.
>>
>> Great, use it, ship it to your customers and we are all happy.  What do
>> you need me for any of this?  :)
>>
>>>>>   And AOSP has already declared the use
>>>>> of 5.10 kernel in their Android S and T releases.
>>>> Publically?  Where?  And is that really the name of the new Android
>>>> releases, I thought they switched to numbers now (hence the naming of
>>>> the current android-common kernel branches, marketing is fun...)
>>> https://source.android.com/devices/architecture/kernel/android-common
>>> Feature and launch kernels provides kernels supported per version.
>>
>> Oh nice, didn't know that.
>>
>> But note, Android kernels do not reflect the lifespan of LTS kernels.
>> If that were the case, I would still be supporting 3.18 as they are
>> doing that at the moment for their devices and customers, and will be
>> doing so for I think another full year.
>>
>> So while Android is nice to see here, remember that is what Google is
>> promising to support for their users.  You can do the same thing for
>> your users, what do you need me here for this?  You can do the same
>> thing that Google is doing for 3.18 right now, pick the stable fixes
>> from upstream, backport them, test them, and push them out to their
>> users.
>>
>> While Google is a great help to me in the LTS effort, providing huge
>> amounts of resources to enable my life easier with this (i.e. funding
>> Linaro's testing efforts), their promise to their customers/users does
>> not depend on me keeping LTS kernels alive, if I stopped tomorrow their
>> contracts are still in place and they know how to do this work
>> themselves (as is proof with 3.18).
>>
>> So you can provide the same kind of guarantee to support any kernel
>> version for any amount of time to any customer you like, it shouldn't
>> require me to do that work for you, right?
>>
>>>>> Is there some way we could make the LTS support more clear.
>>>>> A 2 year declaration is not LTS any more.
>>>> Not true at all, a "normal" stable kernel is dropped after the next
>>>> release happens, making their lifespan about 4 months long.  2 years is
>>>> much longer than 4 months, so it still is a "long term supported" kernel
>>>> in contrast, correct?
>>> Perhaps a new name needs to be made for "LTS" for 6 years to distinguish it 
>>> from 2 years.
>>> The timeframes are very different.
>>
>> At this point in time, anyone wanting a kernel longer than 2 years
>> should know how this all works.
>>
>> If not, please do some basic research, I have written whitepapers on
>> this and given numerous talks.  The information is out there...
>>
>>>>> If 5.10 is "actually" going to be supported for 6 years it would be quite 
>>>>> valuable to make such a declaration.
>>>>> https://www.kernel.org/category/releases.html
>>>> Why?  What would that change?
>>>>
>>>> Ok, seriously, this happens every year, and every year we go through the
>>>> same thing, it's not like this is somehow new, right?
>>> No, but why do we need to keep playing the same game every year now.
>>
>> Because, 5.4 almost did not become "6 years" of support from me.  That
>> was because in the beginning, no one said they were going to use it in
>> their devices and offer me help in testing and backporting.  Only when I
>> knew for sure that we had people helping this out did I change the date
>> on kernel.org.
>>
>> So far the jury is still out for 5.10, are you willing to help with
>>

Re: [PATCH] misc: bcm-vk: only support ttyVK if CONFIG_TTY is set

2021-02-03 Thread Scott Branden



On 2021-02-03 2:17 p.m., Greg Kroah-Hartman wrote:
> On Wed, Feb 03, 2021 at 01:04:51PM -0800, Scott Branden wrote:
>> Correct compile issue if CONFIG_TTY is not set by
>> only adding ttyVK devices if CONFIG_BCM_VK_TTY is set.
>>
>> Reported-by: Randy Dunlap 
>> Signed-off-by: Scott Branden 
>>
>> ---
>> Changes since v3:
> Is this "v4"?  Your subject line doesn't say so :(
Yes, a mistake.  Sent same patch with PATCH v4 on subject line now.
>



[PATCH v4] misc: bcm-vk: only support ttyVK if CONFIG_TTY is set

2021-02-03 Thread Scott Branden
Correct compile issue if CONFIG_TTY is not set by
only adding ttyVK devices if CONFIG_BCM_VK_TTY is set.

Reported-by: Randy Dunlap 
Signed-off-by: Scott Branden 

---
Changes since v3:
- Kconfig cleanups below as per Greg's comments
- changed if BCM_VK to depends on BCM_VK
- removed default y in CONFIG_BCM_VK_TTY
- expanded decription of CONFIG_BCM_VK_TTY
Changes since v2:
- add CONFIG_BCM_VK_TTY
- add function and stub for bcm_vk_tty_set_irq_enabled
Changes since v1:
- add function stubs rather than compiling out code
---
 drivers/misc/bcm-vk/Kconfig  | 12 +
 drivers/misc/bcm-vk/Makefile |  4 +--
 drivers/misc/bcm-vk/bcm_vk.h | 42 +---
 drivers/misc/bcm-vk/bcm_vk_dev.c |  5 ++--
 drivers/misc/bcm-vk/bcm_vk_tty.c |  6 +
 5 files changed, 61 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/bcm-vk/Kconfig b/drivers/misc/bcm-vk/Kconfig
index 052f6f28b540..68a972772b99 100644
--- a/drivers/misc/bcm-vk/Kconfig
+++ b/drivers/misc/bcm-vk/Kconfig
@@ -15,3 +15,15 @@ config BCM_VK
  accelerators via /dev/bcm-vk.N devices.
 
  If unsure, say N.
+
+config BCM_VK_TTY
+   bool "Enable tty ports on a Broadcom VK Accelerator device"
+   depends on TTY
+   depends on BCM_VK
+   help
+ Select this option to enable tty support to allow console
+ access to Broadcom VK Accelerator cards from host.
+
+ Device node will in the form /dev/bcm-vk.x_ttyVKy where:
+ x is the instance of the VK card
+ y is the tty device number on the VK card.
diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index e4a1486f7209..1df2ebe851ca 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -7,6 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
bcm_vk_msg.o \
-   bcm_vk_sg.o \
-   bcm_vk_tty.o
+   bcm_vk_sg.o
 
+bcm_vk-$(CONFIG_BCM_VK_TTY) += bcm_vk_tty.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 3f37c640a814..a1338f375589 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -258,7 +258,11 @@ enum pci_barno {
BAR_2
 };
 
+#ifdef CONFIG_BCM_VK_TTY
 #define BCM_VK_NUM_TTY 2
+#else
+#define BCM_VK_NUM_TTY 0
+#endif
 
 struct bcm_vk_tty {
struct tty_port port;
@@ -366,11 +370,13 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+#ifdef CONFIG_BCM_VK_TTY
struct tty_driver *tty_drv;
struct timer_list serial_timer;
struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
struct workqueue_struct *tty_wq_thread;
struct work_struct tty_wq_work;
+#endif
 
/* Reference-counting to handle file operations */
struct kref kref;
@@ -501,13 +507,43 @@ int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 
shut_type,
 const pid_t pid, const u32 q_num);
 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
-int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
-void bcm_vk_tty_exit(struct bcm_vk *vk);
-void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
 void bcm_vk_hb_init(struct bcm_vk *vk);
 void bcm_vk_hb_deinit(struct bcm_vk *vk);
 void bcm_vk_handle_notf(struct bcm_vk *vk);
 bool bcm_vk_drv_access_ok(struct bcm_vk *vk);
 void bcm_vk_set_host_alert(struct bcm_vk *vk, u32 bit_mask);
 
+#ifdef CONFIG_BCM_VK_TTY
+int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
+void bcm_vk_tty_exit(struct bcm_vk *vk);
+void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
+void bcm_vk_tty_wq_exit(struct bcm_vk *vk);
+
+static inline void bcm_vk_tty_set_irq_enabled(struct bcm_vk *vk, int index)
+{
+   vk->tty[index].irq_enabled = true;
+}
+#else
+static inline int bcm_vk_tty_init(struct bcm_vk *vk, char *name)
+{
+   return 0;
+}
+
+static inline void bcm_vk_tty_exit(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_wq_exit(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_set_irq_enabled(struct bcm_vk *vk, int index)
+{
+}
+#endif /* CONFIG_BCM_VK_TTY */
+
 #endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index c3d2bba68ef1..59b4859d68d2 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -1396,7 +1396,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
pdev->irq + vk->num_irqs, vk->num_irqs + 1);
goto err_irq;
}
-   vk->tty[i].irq_enabled = true;
+   bcm_vk_tty_set_irq_enabled(vk, i);
}
 
id = ida_simple_get(_vk_ida, 0, 0, GFP_KERNEL);
@@ -1580,8 +1580,7 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 
cancel_work_sync(>wq_work);

[PATCH] misc: bcm-vk: only support ttyVK if CONFIG_TTY is set

2021-02-03 Thread Scott Branden
Correct compile issue if CONFIG_TTY is not set by
only adding ttyVK devices if CONFIG_BCM_VK_TTY is set.

Reported-by: Randy Dunlap 
Signed-off-by: Scott Branden 

---
Changes since v3:
- Kconfig cleanups below as per Greg's comments
- changed if BCM_VK to depends on BCM_VK
- removed default y in CONFIG_BCM_VK_TTY
- expanded decription of CONFIG_BCM_VK_TTY
Changes since v2:
- add CONFIG_BCM_VK_TTY
- add function and stub for bcm_vk_tty_set_irq_enabled
Changes since v1:
- add function stubs rather than compiling out code
---
 drivers/misc/bcm-vk/Kconfig  | 12 +
 drivers/misc/bcm-vk/Makefile |  4 +--
 drivers/misc/bcm-vk/bcm_vk.h | 42 +---
 drivers/misc/bcm-vk/bcm_vk_dev.c |  5 ++--
 drivers/misc/bcm-vk/bcm_vk_tty.c |  6 +
 5 files changed, 61 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/bcm-vk/Kconfig b/drivers/misc/bcm-vk/Kconfig
index 052f6f28b540..68a972772b99 100644
--- a/drivers/misc/bcm-vk/Kconfig
+++ b/drivers/misc/bcm-vk/Kconfig
@@ -15,3 +15,15 @@ config BCM_VK
  accelerators via /dev/bcm-vk.N devices.
 
  If unsure, say N.
+
+config BCM_VK_TTY
+   bool "Enable tty ports on a Broadcom VK Accelerator device"
+   depends on TTY
+   depends on BCM_VK
+   help
+ Select this option to enable tty support to allow console
+ access to Broadcom VK Accelerator cards from host.
+
+ Device node will in the form /dev/bcm-vk.x_ttyVKy where:
+ x is the instance of the VK card
+ y is the tty device number on the VK card.
diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index e4a1486f7209..1df2ebe851ca 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -7,6 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
bcm_vk_msg.o \
-   bcm_vk_sg.o \
-   bcm_vk_tty.o
+   bcm_vk_sg.o
 
+bcm_vk-$(CONFIG_BCM_VK_TTY) += bcm_vk_tty.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 3f37c640a814..a1338f375589 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -258,7 +258,11 @@ enum pci_barno {
BAR_2
 };
 
+#ifdef CONFIG_BCM_VK_TTY
 #define BCM_VK_NUM_TTY 2
+#else
+#define BCM_VK_NUM_TTY 0
+#endif
 
 struct bcm_vk_tty {
struct tty_port port;
@@ -366,11 +370,13 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+#ifdef CONFIG_BCM_VK_TTY
struct tty_driver *tty_drv;
struct timer_list serial_timer;
struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
struct workqueue_struct *tty_wq_thread;
struct work_struct tty_wq_work;
+#endif
 
/* Reference-counting to handle file operations */
struct kref kref;
@@ -501,13 +507,43 @@ int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 
shut_type,
 const pid_t pid, const u32 q_num);
 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
-int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
-void bcm_vk_tty_exit(struct bcm_vk *vk);
-void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
 void bcm_vk_hb_init(struct bcm_vk *vk);
 void bcm_vk_hb_deinit(struct bcm_vk *vk);
 void bcm_vk_handle_notf(struct bcm_vk *vk);
 bool bcm_vk_drv_access_ok(struct bcm_vk *vk);
 void bcm_vk_set_host_alert(struct bcm_vk *vk, u32 bit_mask);
 
+#ifdef CONFIG_BCM_VK_TTY
+int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
+void bcm_vk_tty_exit(struct bcm_vk *vk);
+void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
+void bcm_vk_tty_wq_exit(struct bcm_vk *vk);
+
+static inline void bcm_vk_tty_set_irq_enabled(struct bcm_vk *vk, int index)
+{
+   vk->tty[index].irq_enabled = true;
+}
+#else
+static inline int bcm_vk_tty_init(struct bcm_vk *vk, char *name)
+{
+   return 0;
+}
+
+static inline void bcm_vk_tty_exit(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_wq_exit(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_set_irq_enabled(struct bcm_vk *vk, int index)
+{
+}
+#endif /* CONFIG_BCM_VK_TTY */
+
 #endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index c3d2bba68ef1..59b4859d68d2 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -1396,7 +1396,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
pdev->irq + vk->num_irqs, vk->num_irqs + 1);
goto err_irq;
}
-   vk->tty[i].irq_enabled = true;
+   bcm_vk_tty_set_irq_enabled(vk, i);
}
 
id = ida_simple_get(_vk_ida, 0, 0, GFP_KERNEL);
@@ -1580,8 +1580,7 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 
cancel_work_sync(>wq_work);

Re: [PATCH] misc: bcm-vk: Fix a couple error codes in probe()

2021-02-03 Thread Scott Branden
Thanks Dan,

On 2021-02-03 6:42 a.m., Dan Carpenter wrote:
> These errors should return negative error codes instead of returning
> success.
Do you have a script running to report such issues or just manually reviewing
to find such paths?
>
> Fixes: 064ffc7c3939 ("misc: bcm-vk: add autoload support")
> Fixes: 522f692686a7 ("misc: bcm-vk: add Broadcom VK driver")
> Signed-off-by: Dan Carpenter 
Acked-by: Scott Branden 
> ---
>  drivers/misc/bcm-vk/bcm_vk_dev.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c 
> b/drivers/misc/bcm-vk/bcm_vk_dev.c
> index c3d2bba68ef1..a82a8927d92b 100644
> --- a/drivers/misc/bcm-vk/bcm_vk_dev.c
> +++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
> @@ -1358,6 +1358,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
> struct pci_device_id *ent)
>   vk->bar[i] = pci_ioremap_bar(pdev, i * 2);
>   if (!vk->bar[i]) {
>   dev_err(dev, "failed to remap BAR%d\n", i);
> + err = -ENOMEM;
>   goto err_iounmap;
>   }
>   }
> @@ -1463,7 +1464,8 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
> struct pci_device_id *ent)
>   boot_status = vkread32(vk, BAR_0, BAR_BOOT_STATUS);
>   if (auto_load) {
>   if ((boot_status & BOOT_STATE_MASK) == BROM_RUNNING) {
> - if (bcm_vk_trigger_autoload(vk))
> + err = bcm_vk_trigger_autoload(vk);
> + if (err)
>   goto err_bcm_vk_tty_exit;
>   } else {
>   dev_err(dev,



Re: [PATCH] misc: bcm-vk: unlock on error in bcm_to_h_msg_dequeue()

2021-02-01 Thread Scott Branden
Great, looks good then.

On 2021-02-01 10:32 a.m., Desmond Yan wrote:
> Acked-by: Desmond Yan 
Acked-by: Scott Branden 
>
> On Mon, Feb 1, 2021 at 4:24 AM Dan Carpenter  wrote:
>> Unlock before returning on this error path.
>>
>> Fixes: 111d746bb476 ("misc: bcm-vk: add VK messaging support")
>> Signed-off-by: Dan Carpenter 
>> ---
>>  drivers/misc/bcm-vk/bcm_vk_msg.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c 
>> b/drivers/misc/bcm-vk/bcm_vk_msg.c
>> index eec90494777d..fc972e43258a 100644
>> --- a/drivers/misc/bcm-vk/bcm_vk_msg.c
>> +++ b/drivers/misc/bcm-vk/bcm_vk_msg.c
>> @@ -849,7 +849,8 @@ s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk)
>>  * that is fatal.
>>  */
>> dev_crit(dev, "Kernel mem allocation 
>> failure.\n");
>> -   return -ENOMEM;
>> +   total = -ENOMEM;
>> +   goto idx_err;
>> }
>>
>> /* flush rd pointer after a message is dequeued */
>> --
>> 2.29.2
>>



Re: [PATCH v3] misc: bcm-vk: only support ttyVK if CONFIG_TTY is set

2021-02-01 Thread Scott Branden
Hi Greg,,

I need a few clarifications before sending (hopefully) final revisions to the 
patch.

On 2021-01-31 11:45 p.m., Greg Kroah-Hartman wrote:
> On Sun, Jan 31, 2021 at 03:30:49PM -0800, Scott Branden wrote:
>> Correct compile issue if CONFIG_TTY is not set by
>> only adding ttyVK devices if CONFIG_BCM_VK_TTY is set.
>>
>> Reported-by: Randy Dunlap 
>> Signed-off-by: Scott Branden 
>>
>> ---
>> Changes since v2:
>> - add CONFIG_BCM_VK_TTY
>> - add function and stub for bcm_vk_tty_set_irq_enabled
>> Changes since v1:
>> - add function stubs rather than compiling out code
>> ---
>>  drivers/misc/bcm-vk/Kconfig  | 16 
>>  drivers/misc/bcm-vk/Makefile |  4 +--
>>  drivers/misc/bcm-vk/bcm_vk.h | 42 +---
>>  drivers/misc/bcm-vk/bcm_vk_dev.c |  5 ++--
>>  drivers/misc/bcm-vk/bcm_vk_tty.c |  6 +
>>  5 files changed, 65 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/misc/bcm-vk/Kconfig b/drivers/misc/bcm-vk/Kconfig
>> index 052f6f28b540..16ce98c964b8 100644
>> --- a/drivers/misc/bcm-vk/Kconfig
>> +++ b/drivers/misc/bcm-vk/Kconfig
>> @@ -15,3 +15,19 @@ config BCM_VK
>>accelerators via /dev/bcm-vk.N devices.
>>  
>>If unsure, say N.
>> +
>> +if BCM_VK
> No need for this, just put it on the depends line, right?
If you prefer I can but it on the depends on line.
But, I actually prefer the if syntax in this case as it more clearly shows
BCM_VK_TTY is a suboption of BCM_VK.

Please let me know which method is "right"?
>
>> +
>> +config BCM_VK_TTY
>> +bool "Enable ttyVK"
> Better config help text to explain what this is?
I'll change it to the following?

"Enable tty's on VK devices"
>
>> +depends on TTY
>> +default y
> Default y is only there if your system can not boot without it, please
> remove it.
I can remove if really needed but I'd like to learn more about such convention.
Is there a document I can learn from describing such?

We actually want a full featured driver by default.  Otherwise we'll end up 
asking people to enable this
feature and recompile the driver to get missing features such as this.
>
>> +help
>> +  Select this option to enable ttyVK support to allow console
>> +  access to VK cards from host.
> Again, more help text, what is a "VK"?
VK is already described in BCM_VK.  Why would I need to add the same 
information again to a suboption?
Perhaps you would like "config BCM_VK" changed to "menuconfig BCM_VK"
>
> thanks,
>
> greg k-h
Thanks,
 Scott


Re: [PATCH] misc: bcm-vk: unlock on error in bcm_to_h_msg_dequeue()

2021-02-01 Thread Scott Branden



On 2021-02-01 4:22 a.m., Dan Carpenter wrote:
> Unlock before returning on this error path.
>
> Fixes: 111d746bb476 ("misc: bcm-vk: add VK messaging support")
> Signed-off-by: Dan Carpenter 
> ---
>  drivers/misc/bcm-vk/bcm_vk_msg.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c 
> b/drivers/misc/bcm-vk/bcm_vk_msg.c
> index eec90494777d..fc972e43258a 100644
> --- a/drivers/misc/bcm-vk/bcm_vk_msg.c
> +++ b/drivers/misc/bcm-vk/bcm_vk_msg.c
> @@ -849,7 +849,8 @@ s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk)
>* that is fatal.
>*/
>   dev_crit(dev, "Kernel mem allocation 
> failure.\n");
> - return -ENOMEM;
> + total = -ENOMEM;
> + goto idx_err;
>   }
>  
This is a pretty fatal case if we fail to allocate memory here.
Will let Desmond respond if we wanted to keep the mutex locked forever in this
case or if we do want to return and keep mutex locked if it is fatal and there 
is
no real recovery path.
>   /* flush rd pointer after a message is dequeued */



[PATCH v3] misc: bcm-vk: only support ttyVK if CONFIG_TTY is set

2021-01-31 Thread Scott Branden
Correct compile issue if CONFIG_TTY is not set by
only adding ttyVK devices if CONFIG_BCM_VK_TTY is set.

Reported-by: Randy Dunlap 
Signed-off-by: Scott Branden 

---
Changes since v2:
- add CONFIG_BCM_VK_TTY
- add function and stub for bcm_vk_tty_set_irq_enabled
Changes since v1:
- add function stubs rather than compiling out code
---
 drivers/misc/bcm-vk/Kconfig  | 16 
 drivers/misc/bcm-vk/Makefile |  4 +--
 drivers/misc/bcm-vk/bcm_vk.h | 42 +---
 drivers/misc/bcm-vk/bcm_vk_dev.c |  5 ++--
 drivers/misc/bcm-vk/bcm_vk_tty.c |  6 +
 5 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/bcm-vk/Kconfig b/drivers/misc/bcm-vk/Kconfig
index 052f6f28b540..16ce98c964b8 100644
--- a/drivers/misc/bcm-vk/Kconfig
+++ b/drivers/misc/bcm-vk/Kconfig
@@ -15,3 +15,19 @@ config BCM_VK
  accelerators via /dev/bcm-vk.N devices.
 
  If unsure, say N.
+
+if BCM_VK
+
+config BCM_VK_TTY
+   bool "Enable ttyVK"
+   depends on TTY
+   default y
+   help
+ Select this option to enable ttyVK support to allow console
+ access to VK cards from host.
+
+ Device node will in the form /dev/bcm-vk.x_ttyVKy where:
+ x is the instance of the VK card
+ y is the tty device number on the VK card.
+
+endif # BCM_VK
diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index e4a1486f7209..1df2ebe851ca 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -7,6 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
bcm_vk_msg.o \
-   bcm_vk_sg.o \
-   bcm_vk_tty.o
+   bcm_vk_sg.o
 
+bcm_vk-$(CONFIG_BCM_VK_TTY) += bcm_vk_tty.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 3f37c640a814..a1338f375589 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -258,7 +258,11 @@ enum pci_barno {
BAR_2
 };
 
+#ifdef CONFIG_BCM_VK_TTY
 #define BCM_VK_NUM_TTY 2
+#else
+#define BCM_VK_NUM_TTY 0
+#endif
 
 struct bcm_vk_tty {
struct tty_port port;
@@ -366,11 +370,13 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+#ifdef CONFIG_BCM_VK_TTY
struct tty_driver *tty_drv;
struct timer_list serial_timer;
struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
struct workqueue_struct *tty_wq_thread;
struct work_struct tty_wq_work;
+#endif
 
/* Reference-counting to handle file operations */
struct kref kref;
@@ -501,13 +507,43 @@ int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 
shut_type,
 const pid_t pid, const u32 q_num);
 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
-int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
-void bcm_vk_tty_exit(struct bcm_vk *vk);
-void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
 void bcm_vk_hb_init(struct bcm_vk *vk);
 void bcm_vk_hb_deinit(struct bcm_vk *vk);
 void bcm_vk_handle_notf(struct bcm_vk *vk);
 bool bcm_vk_drv_access_ok(struct bcm_vk *vk);
 void bcm_vk_set_host_alert(struct bcm_vk *vk, u32 bit_mask);
 
+#ifdef CONFIG_BCM_VK_TTY
+int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
+void bcm_vk_tty_exit(struct bcm_vk *vk);
+void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
+void bcm_vk_tty_wq_exit(struct bcm_vk *vk);
+
+static inline void bcm_vk_tty_set_irq_enabled(struct bcm_vk *vk, int index)
+{
+   vk->tty[index].irq_enabled = true;
+}
+#else
+static inline int bcm_vk_tty_init(struct bcm_vk *vk, char *name)
+{
+   return 0;
+}
+
+static inline void bcm_vk_tty_exit(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_wq_exit(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_set_irq_enabled(struct bcm_vk *vk, int index)
+{
+}
+#endif /* CONFIG_BCM_VK_TTY */
+
 #endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index c3d2bba68ef1..59b4859d68d2 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -1396,7 +1396,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
pdev->irq + vk->num_irqs, vk->num_irqs + 1);
goto err_irq;
}
-   vk->tty[i].irq_enabled = true;
+   bcm_vk_tty_set_irq_enabled(vk, i);
}
 
id = ida_simple_get(_vk_ida, 0, 0, GFP_KERNEL);
@@ -1580,8 +1580,7 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 
cancel_work_sync(>wq_work);
destroy_workqueue(vk->wq_thread);
-   cancel_work_sync(>tty_wq_work);
-   destroy_workqueue(vk->tty_wq_thread);
+   bcm_vk_tty_wq_exit(vk);
 
for (i = 0; i < MAX_BAR; i++) {

Re: [PATCH v2] misc: bcm-vk: only support ttyVK if CONFIG_TTY is set

2021-01-30 Thread Scott Branden



On 2021-01-30 12:55 a.m., Greg Kroah-Hartman wrote:
> On Fri, Jan 29, 2021 at 02:06:27PM -0800, Scott Branden wrote:
>> Correct compile issue if CONFIG_TTY is not set by
>> only adding ttyVK devices if CONFIG_TTY is set.
>>
>> Reported-by: Randy Dunlap 
>> Signed-off-by: Scott Branden 
>>
>> ---
>> Changes since v1:
>> Add function stubs rather than compiling out code
>> ---
>>  drivers/misc/bcm-vk/Makefile |  4 ++--
>>  drivers/misc/bcm-vk/bcm_vk.h | 35 +---
>>  drivers/misc/bcm-vk/bcm_vk_dev.c |  3 +--
>>  drivers/misc/bcm-vk/bcm_vk_tty.c |  6 ++
>>  4 files changed, 41 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
>> index e4a1486f7209..8d81a734fcad 100644
>> --- a/drivers/misc/bcm-vk/Makefile
>> +++ b/drivers/misc/bcm-vk/Makefile
>> @@ -7,6 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
>>  bcm_vk-objs := \
>>  bcm_vk_dev.o \
>>  bcm_vk_msg.o \
>> -bcm_vk_sg.o \
>> -bcm_vk_tty.o
>> +bcm_vk_sg.o
>>  
>> +bcm_vk-$(CONFIG_TTY) += bcm_vk_tty.o
>> diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
>> index 3f37c640a814..4a1d515374c7 100644
>> --- a/drivers/misc/bcm-vk/bcm_vk.h
>> +++ b/drivers/misc/bcm-vk/bcm_vk.h
>> @@ -258,7 +258,11 @@ enum pci_barno {
>>  BAR_2
>>  };
>>  
>> +#ifdef CONFIG_TTY
>>  #define BCM_VK_NUM_TTY 2
>> +#else
>> +#define BCM_VK_NUM_TTY 0
>> +#endif
>>  
>>  struct bcm_vk_tty {
>>  struct tty_port port;
>> @@ -366,11 +370,15 @@ struct bcm_vk {
>>  struct miscdevice miscdev;
>>  int devid; /* dev id allocated */
>>  
>> +#ifdef CONFIG_TTY
>>  struct tty_driver *tty_drv;
>>  struct timer_list serial_timer;
>>  struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
>>  struct workqueue_struct *tty_wq_thread;
>>  struct work_struct tty_wq_work;
>> +#else
>> +struct bcm_vk_tty *tty;
> Why do you still need this pointer?
vk->tty is still in one location in bcm_vk_dev.c when installing the IRQ.
The loop is never executed as VK_MSIX_TTY_MAX = 0 when CONFIG_TTY is not 
defined.

I'll move setting vk-tty[i].irq_enabled into an inline function in the header 
file to clean this up.

    for (i = 0;
     (i < VK_MSIX_TTY_MAX) && (vk->num_irqs < irq);
     i++, vk->num_irqs++) {
        err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs),
                   bcm_vk_tty_irqhandler,
                   IRQF_SHARED, DRV_MODULE_NAME, vk);
        if (err) {
            dev_err(dev, "failed request tty IRQ %d for MSIX %d\n",
                pdev->irq + vk->num_irqs, vk->num_irqs + 1);
            goto err_irq;
        }
        vk->tty[i].irq_enabled = true;
    }


> And should you just have a separate config option for your tty driver
> instead that depends on CONFIG_TTY?  Would you ever want to run this
> driver without the tty portion?
Yes, an additional config option could be added.
Looking at the code, it would simplify (a non-upstreamable) patch that allows 
the driver to run on
an ancient kernel where we compile out some features that don't work due to 
kernel api changes since then.

I hadn't added such a config as some are of the opinion having a full featured 
driver without config options is better.
For example, someone builds the driver without the feature enabled,
then someone needs to use the feature and the driver would need to be rebuilt.

Since it sounds like you are for such CONFIG options I will add it as it 
simplifies some legacy kernel support used in manufacturing.
> Oh, and much better than the previous version, thanks for cleaning it
> up.
Thanks - your comments do highlight some issues and we are learning from them.
>
> thanks,
>
> greg k-h



[PATCH v2] misc: bcm-vk: only support ttyVK if CONFIG_TTY is set

2021-01-29 Thread Scott Branden
Correct compile issue if CONFIG_TTY is not set by
only adding ttyVK devices if CONFIG_TTY is set.

Reported-by: Randy Dunlap 
Signed-off-by: Scott Branden 

---
Changes since v1:
Add function stubs rather than compiling out code
---
 drivers/misc/bcm-vk/Makefile |  4 ++--
 drivers/misc/bcm-vk/bcm_vk.h | 35 +---
 drivers/misc/bcm-vk/bcm_vk_dev.c |  3 +--
 drivers/misc/bcm-vk/bcm_vk_tty.c |  6 ++
 4 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index e4a1486f7209..8d81a734fcad 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -7,6 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
bcm_vk_msg.o \
-   bcm_vk_sg.o \
-   bcm_vk_tty.o
+   bcm_vk_sg.o
 
+bcm_vk-$(CONFIG_TTY) += bcm_vk_tty.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 3f37c640a814..4a1d515374c7 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -258,7 +258,11 @@ enum pci_barno {
BAR_2
 };
 
+#ifdef CONFIG_TTY
 #define BCM_VK_NUM_TTY 2
+#else
+#define BCM_VK_NUM_TTY 0
+#endif
 
 struct bcm_vk_tty {
struct tty_port port;
@@ -366,11 +370,15 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+#ifdef CONFIG_TTY
struct tty_driver *tty_drv;
struct timer_list serial_timer;
struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
struct workqueue_struct *tty_wq_thread;
struct work_struct tty_wq_work;
+#else
+   struct bcm_vk_tty *tty;
+#endif
 
/* Reference-counting to handle file operations */
struct kref kref;
@@ -501,13 +509,34 @@ int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 
shut_type,
 const pid_t pid, const u32 q_num);
 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
-int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
-void bcm_vk_tty_exit(struct bcm_vk *vk);
-void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
 void bcm_vk_hb_init(struct bcm_vk *vk);
 void bcm_vk_hb_deinit(struct bcm_vk *vk);
 void bcm_vk_handle_notf(struct bcm_vk *vk);
 bool bcm_vk_drv_access_ok(struct bcm_vk *vk);
 void bcm_vk_set_host_alert(struct bcm_vk *vk, u32 bit_mask);
 
+#ifdef CONFIG_TTY
+int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
+void bcm_vk_tty_exit(struct bcm_vk *vk);
+void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
+void bcm_vk_tty_wq_exit(struct bcm_vk *vk);
+#else
+static inline int bcm_vk_tty_init(struct bcm_vk *vk, char *name)
+{
+   return 0;
+}
+
+static inline void bcm_vk_tty_exit(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk)
+{
+}
+
+static inline void bcm_vk_tty_wq_exit(struct bcm_vk *vk)
+{
+}
+#endif /* CONFIG_TTY */
+
 #endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index c3d2bba68ef1..5d2030b67007 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -1580,8 +1580,7 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 
cancel_work_sync(>wq_work);
destroy_workqueue(vk->wq_thread);
-   cancel_work_sync(>tty_wq_work);
-   destroy_workqueue(vk->tty_wq_thread);
+   bcm_vk_tty_wq_exit(vk);
 
for (i = 0; i < MAX_BAR; i++) {
if (vk->bar[i])
diff --git a/drivers/misc/bcm-vk/bcm_vk_tty.c b/drivers/misc/bcm-vk/bcm_vk_tty.c
index be3964949b63..4d02692ecfc7 100644
--- a/drivers/misc/bcm-vk/bcm_vk_tty.c
+++ b/drivers/misc/bcm-vk/bcm_vk_tty.c
@@ -331,3 +331,9 @@ void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk)
kill_pid(find_vpid(vktty->pid), SIGKILL, 1);
}
 }
+
+void bcm_vk_tty_wq_exit(struct bcm_vk *vk)
+{
+   cancel_work_sync(>tty_wq_work);
+   destroy_workqueue(vk->tty_wq_thread);
+}
-- 
2.17.1



Re: [PATCH v10 00/13] Add Broadcom VK driver

2021-01-28 Thread Scott Branden



On 2021-01-28 8:02 p.m., Olof Johansson wrote:
> Scott,
>
> On Thu, Jan 28, 2021 at 4:30 PM Scott Branden
>  wrote:
>> This patch series drops previous patches in [1]
>> that were incorporated by Kees Cook into patch series
>> "Introduce partial kernel_read_file() support" [2].
>>
>> Remaining patches are contained in this series to add Broadcom VK driver.
>> (which depends on request_firmware_into_buf API addition which has
>> now been accepted into the upstream kernel as of v5.10-rc1).
>>
>> [1] 
>> https://lore.kernel.org/lkml/20200706232309.12010-1-scott.bran...@broadcom.com/
>> [2] 
>> https://lore.kernel.org/lkml/20201002173828.2099543-1-keesc...@chromium.org/
>>
>> Changes from v9:
>>  - fix compile if CONFIG_TTY not set
>>  - use if-else statement instead of conditional expression in set_q_num
>>  - generate bcm_vk_msg_remove call in proper patch
>>(was generated in tty patch when should be in msg patch)
> Did I miss something? Greg already applied v9, so any fixes on top
> needs to be new patches.
Sorry, was unfamiliar with the process of having changes in linux-next needing 
correcting.
I sent out new patches to correct these issues against linux-next now.
>
>
> -Olof
Thanks for providing correct process.

Scott


[PATCH] misc: bcm-vk: only support ttyVK if CONFIG_TTY is set

2021-01-28 Thread Scott Branden
Correct compile issue if CONFIG_TTY is not set by
only adding ttyVK devices if CONFIG_TTY is set.

Reported-by: Randy Dunlap 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/Makefile |  4 ++--
 drivers/misc/bcm-vk/bcm_vk_dev.c | 13 +
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index e4a1486f7209..8d81a734fcad 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -7,6 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
bcm_vk_msg.o \
-   bcm_vk_sg.o \
-   bcm_vk_tty.o
+   bcm_vk_sg.o
 
+bcm_vk-$(CONFIG_TTY) += bcm_vk_tty.o
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index c3d2bba68ef1..ae63fbdf14bc 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -525,7 +525,10 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
}
}
}
+
+#ifdef CONFIG_TTY
bcm_vk_tty_terminate_tty_user(vk);
+#endif
spin_unlock(>ctx_lock);
 }
 
@@ -1385,6 +1388,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
vk->num_irqs++;
 
+#ifdef CONFIG_TTY
for (i = 0;
 (i < VK_MSIX_TTY_MAX) && (vk->num_irqs < irq);
 i++, vk->num_irqs++) {
@@ -1398,6 +1402,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
vk->tty[i].irq_enabled = true;
}
+#endif
 
id = ida_simple_get(_vk_ida, 0, 0, GFP_KERNEL);
if (id < 0) {
@@ -1451,10 +1456,12 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
goto err_destroy_workqueue;
}
 
+#ifdef CONFIG_TTY
snprintf(name, sizeof(name), KBUILD_MODNAME ".%d_ttyVK", id);
err = bcm_vk_tty_init(vk, name);
if (err)
goto err_unregister_panic_notifier;
+#endif
 
/*
 * lets trigger an auto download.  We don't want to do it serially here
@@ -1480,9 +1487,11 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
return 0;
 
 err_bcm_vk_tty_exit:
+#ifdef CONFIG_TTY
bcm_vk_tty_exit(vk);
 
 err_unregister_panic_notifier:
+#endif
atomic_notifier_chain_unregister(_notifier_list,
 >panic_nb);
 
@@ -1560,7 +1569,9 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 >panic_nb);
 
bcm_vk_msg_remove(vk);
+#ifdef CONFIG_TTY
bcm_vk_tty_exit(vk);
+#endif
 
if (vk->tdma_vaddr)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
@@ -1580,8 +1591,10 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 
cancel_work_sync(>wq_work);
destroy_workqueue(vk->wq_thread);
+#ifdef CONFIG_TTY
cancel_work_sync(>tty_wq_work);
destroy_workqueue(vk->tty_wq_thread);
+#endif
 
for (i = 0; i < MAX_BAR; i++) {
if (vk->bar[i])
-- 
2.17.1



[PATCH] misc: bcm-vk: fix set_q_num API precedence issue

2021-01-28 Thread Scott Branden
From: Desmond Yan 

Change set_q_num API to use if-else to make it more explicit,
and avoid a precedence rule issue.

Reported-by: kernel test robot 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk_msg.c | 10 --
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c b/drivers/misc/bcm-vk/bcm_vk_msg.c
index eec90494777d..a363e2e4f7bc 100644
--- a/drivers/misc/bcm-vk/bcm_vk_msg.c
+++ b/drivers/misc/bcm-vk/bcm_vk_msg.c
@@ -52,8 +52,14 @@ static u32 get_q_num(const struct vk_msg_blk *msg)
 
 static void set_q_num(struct vk_msg_blk *msg, u32 q_num)
 {
-   msg->trans_id = (msg->trans_id & ~BCM_VK_MSG_Q_MASK) |
-   (q_num >= VK_MSGQ_PER_CHAN_MAX) ? VK_MSGQ_NUM_DEFAULT : q_num;
+   u32 trans_q;
+
+   if (q_num >= VK_MSGQ_PER_CHAN_MAX)
+   trans_q = VK_MSGQ_NUM_DEFAULT;
+   else
+   trans_q = q_num;
+
+   msg->trans_id = (msg->trans_id & ~BCM_VK_MSG_Q_MASK) | trans_q;
 }
 
 static u32 get_msg_id(const struct vk_msg_blk *msg)
-- 
2.17.1



[PATCH v10 11/13] misc: bcm-vk: add mmap function for exposing BAR2

2021-01-28 Thread Scott Branden
Add mmap function that allows host application to open up BAR2 memory
for remote spooling out messages from the VK logger.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk_dev.c | 24 
 1 file changed, 24 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 69a351fe3b38..ee926eb07542 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -1199,6 +1199,29 @@ static long bcm_vk_reset(struct bcm_vk *vk, struct 
vk_reset __user *arg)
return ret;
 }
 
+static int bcm_vk_mmap(struct file *file, struct vm_area_struct *vma)
+{
+   struct bcm_vk_ctx *ctx = file->private_data;
+   struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
+   unsigned long pg_size;
+
+   /* only BAR2 is mmap possible, which is bar num 4 due to 64bit */
+#define VK_MMAPABLE_BAR 4
+
+   pg_size = ((pci_resource_len(vk->pdev, VK_MMAPABLE_BAR) - 1)
+   >> PAGE_SHIFT) + 1;
+   if (vma->vm_pgoff + vma_pages(vma) > pg_size)
+   return -EINVAL;
+
+   vma->vm_pgoff += (pci_resource_start(vk->pdev, VK_MMAPABLE_BAR)
+ >> PAGE_SHIFT);
+   vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+   return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+ vma->vm_end - vma->vm_start,
+ vma->vm_page_prot);
+}
+
 static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
 {
long ret = -EINVAL;
@@ -1237,6 +1260,7 @@ static const struct file_operations bcm_vk_fops = {
.write = bcm_vk_write,
.poll = bcm_vk_poll,
.release = bcm_vk_release,
+   .mmap = bcm_vk_mmap,
.unlocked_ioctl = bcm_vk_ioctl,
 };
 
-- 
2.17.1



[PATCH v10 13/13] misc: bcm-vk: add ttyVK support

2021-01-28 Thread Scott Branden
Add ttyVK support to driver to allow console access to VK card from host.

Device node will be in the follow form /dev/bcm-vk.x_ttyVKy where:
x is the instance of the VK card
y is the tty device number on the VK card

Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/Makefile |   1 +
 drivers/misc/bcm-vk/bcm_vk.h |  28 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c |  41 +++-
 drivers/misc/bcm-vk/bcm_vk_tty.c | 333 +++
 4 files changed, 402 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index 79b4e365c9e6..8d81a734fcad 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -9,3 +9,4 @@ bcm_vk-objs := \
bcm_vk_msg.o \
bcm_vk_sg.o
 
+bcm_vk-$(CONFIG_TTY) += bcm_vk_tty.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index a1d0bf6e694c..3f37c640a814 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -8,12 +8,14 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -84,6 +86,9 @@
 #define CODEPUSH_BOOT2_ENTRY   0x6000
 
 #define BAR_CARD_STATUS0x410
+/* CARD_STATUS definitions */
+#define CARD_STATUS_TTYVK0_READY   BIT(0)
+#define CARD_STATUS_TTYVK1_READY   BIT(1)
 
 #define BAR_BOOT1_STDALONE_PROGRESS0x420
 #define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
@@ -255,6 +260,19 @@ enum pci_barno {
 
 #define BCM_VK_NUM_TTY 2
 
+struct bcm_vk_tty {
+   struct tty_port port;
+   u32 to_offset;  /* bar offset to use */
+   u32 to_size;/* to VK buffer size */
+   u32 wr; /* write offset shadow */
+   u32 from_offset;/* bar offset to use */
+   u32 from_size;  /* from VK buffer size */
+   u32 rd; /* read offset shadow */
+   pid_t pid;
+   bool irq_enabled;
+   bool is_opened; /* tracks tty open/close */
+};
+
 /* VK device max power state, supports 3, full, reduced and low */
 #define MAX_OPP 3
 #define MAX_CARD_INFO_TAG_SIZE 64
@@ -348,6 +366,12 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+   struct tty_driver *tty_drv;
+   struct timer_list serial_timer;
+   struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
+   struct workqueue_struct *tty_wq_thread;
+   struct work_struct tty_wq_work;
+
/* Reference-counting to handle file operations */
struct kref kref;
 
@@ -466,6 +490,7 @@ int bcm_vk_release(struct inode *inode, struct file 
*p_file);
 void bcm_vk_release_data(struct kref *kref);
 irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
+irqreturn_t bcm_vk_tty_irqhandler(int irq, void *dev_id);
 int bcm_vk_msg_init(struct bcm_vk *vk);
 void bcm_vk_msg_remove(struct bcm_vk *vk);
 void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
@@ -476,6 +501,9 @@ int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 
shut_type,
 const pid_t pid, const u32 q_num);
 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
+int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
+void bcm_vk_tty_exit(struct bcm_vk *vk);
+void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
 void bcm_vk_hb_init(struct bcm_vk *vk);
 void bcm_vk_hb_deinit(struct bcm_vk *vk);
 void bcm_vk_handle_notf(struct bcm_vk *vk);
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index ee926eb07542..ae63fbdf14bc 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -525,6 +525,10 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
}
}
}
+
+#ifdef CONFIG_TTY
+   bcm_vk_tty_terminate_tty_user(vk);
+#endif
spin_unlock(>ctx_lock);
 }
 
@@ -1384,6 +1388,22 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
vk->num_irqs++;
 
+#ifdef CONFIG_TTY
+   for (i = 0;
+(i < VK_MSIX_TTY_MAX) && (vk->num_irqs < irq);
+i++, vk->num_irqs++) {
+   err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs),
+  bcm_vk_tty_irqhandler,
+  IRQF_SHARED, DRV_MODULE_NAME, vk);
+   if (err) {
+   dev_err(dev, "failed request tty IRQ %d for MSIX %d\n",
+   pdev->irq + vk->num_irqs, vk->num_irqs + 1);
+   goto err_irq;
+   }
+   vk->tty[i].irq_enabled = true;
+   }
+#endif
+
id = ida_simple_get(_vk_ida, 0, 0, GFP_KERNEL);
if (id &

[PATCH v10 12/13] MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver

2021-01-28 Thread Scott Branden
Add maintainer entry for new Broadcom VK Driver

Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 MAINTAINERS | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 00836f6452f0..76f3817cf22c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3744,6 +3744,13 @@ L:   net...@vger.kernel.org
 S: Supported
 F: drivers/net/ethernet/broadcom/tg3.*
 
+BROADCOM VK DRIVER
+M: Scott Branden 
+L: bcm-kernel-feedback-l...@broadcom.com
+S: Supported
+F: drivers/misc/bcm-vk/
+F: include/uapi/linux/misc/bcm_vk.h
+
 BROCADE BFA FC SCSI DRIVER
 M: Anil Gurumurthy 
 M: Sudarsana Kalluru 
-- 
2.17.1



[PATCH v10 08/13] misc: bcm-vk: add get_card_info, peerlog_info, and proc_mon_info

2021-01-28 Thread Scott Branden
Add support to get card_info (details about card),
peerlog_info (to get details of peerlog on card),
and proc_mon_info (process monitoring on card).

This info is used for collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |  60 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 105 +++
 2 files changed, 165 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 726aab71bb6b..50f2a0cd6e13 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -205,6 +205,21 @@ enum pci_barno {
 
 #define BCM_VK_NUM_TTY 2
 
+/* VK device max power state, supports 3, full, reduced and low */
+#define MAX_OPP 3
+#define MAX_CARD_INFO_TAG_SIZE 64
+
+struct bcm_vk_card_info {
+   u32 version;
+   char os_tag[MAX_CARD_INFO_TAG_SIZE];
+   char cmpt_tag[MAX_CARD_INFO_TAG_SIZE];
+   u32 cpu_freq_mhz;
+   u32 cpu_scale[MAX_OPP];
+   u32 ddr_freq_mhz;
+   u32 ddr_size_MB;
+   u32 video_core_freq_mhz;
+};
+
 /* DAUTH related info */
 struct bcm_vk_dauth_key {
char store[VK_BAR1_DAUTH_STORE_SIZE];
@@ -215,10 +230,49 @@ struct bcm_vk_dauth_info {
struct bcm_vk_dauth_key keys[VK_BAR1_DAUTH_MAX];
 };
 
+/*
+ * Control structure of logging messages from the card.  This
+ * buffer is for logmsg that comes from vk
+ */
+struct bcm_vk_peer_log {
+   u32 rd_idx;
+   u32 wr_idx;
+   u32 buf_size;
+   u32 mask;
+   char data[0];
+};
+
+/* max buf size allowed */
+#define BCM_VK_PEER_LOG_BUF_MAX SZ_16K
+/* max size per line of peer log */
+#define BCM_VK_PEER_LOG_LINE_MAX  256
+
+/*
+ * single entry for processing type + utilization
+ */
+#define BCM_VK_PROC_TYPE_TAG_LEN 8
+struct bcm_vk_proc_mon_entry_t {
+   char tag[BCM_VK_PROC_TYPE_TAG_LEN];
+   u32 used;
+   u32 max; /**< max capacity */
+};
+
+/**
+ * Structure for run time utilization
+ */
+#define BCM_VK_PROC_MON_MAX 8 /* max entries supported */
+struct bcm_vk_proc_mon_info {
+   u32 num; /**< no of entries */
+   u32 entry_size; /**< per entry size */
+   struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
+};
+
 struct bcm_vk {
struct pci_dev *pdev;
void __iomem *bar[MAX_BAR];
 
+   struct bcm_vk_card_info card_info;
+   struct bcm_vk_proc_mon_info proc_mon_info;
struct bcm_vk_dauth_info dauth_info;
 
/* mutex to protect the ioctls */
@@ -240,6 +294,12 @@ struct bcm_vk {
dma_addr_t tdma_addr; /* test dma segment bus addr */
 
struct notifier_block panic_nb;
+
+   /* offset of the peer log control in BAR2 */
+   u32 peerlog_off;
+   struct bcm_vk_peer_log peerlog_info; /* record of peer log info */
+   /* offset of processing monitoring info in BAR2 */
+   u32 proc_mon_off;
 };
 
 /* wq offload work items bits definitions */
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 203a1cf2bae3..a63208513c64 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -172,6 +172,104 @@ static inline int bcm_vk_wait(struct bcm_vk *vk, enum 
pci_barno bar,
return 0;
 }
 
+static void bcm_vk_get_card_info(struct bcm_vk *vk)
+{
+   struct device *dev = >pdev->dev;
+   u32 offset;
+   int i;
+   u8 *dst;
+   struct bcm_vk_card_info *info = >card_info;
+
+   /* first read the offset from spare register */
+   offset = vkread32(vk, BAR_0, BAR_CARD_STATIC_INFO);
+   offset &= (pci_resource_len(vk->pdev, BAR_2 * 2) - 1);
+
+   /* based on the offset, read info to internal card info structure */
+   dst = (u8 *)info;
+   for (i = 0; i < sizeof(*info); i++)
+   *dst++ = vkread8(vk, BAR_2, offset++);
+
+#define CARD_INFO_LOG_FMT "version   : %x\n" \
+ "os_tag: %s\n" \
+ "cmpt_tag  : %s\n" \
+ "cpu_freq  : %d MHz\n" \
+ "cpu_scale : %d full, %d lowest\n" \
+ "ddr_freq  : %d MHz\n" \
+ "ddr_size  : %d MB\n" \
+ "video_freq: %d MHz\n"
+   dev_dbg(dev, CARD_INFO_LOG_FMT, info->version, info->os_tag,
+   info->cmpt_tag, info->cpu_freq_mhz, info->cpu_scale[0],
+   info->cpu_scale[MAX_OPP - 1], info->ddr_freq_mhz,
+   info->ddr_size_MB, info->video_core_freq_mhz);
+
+   /*
+* get the peer log pointer, only need the offset, and get record
+* of the log buffer information which would be used for checking
+* before dump, in case the BAR2 memory 

[PATCH v10 09/13] misc: bcm-vk: add VK messaging support

2021-01-28 Thread Scott Branden
Add message support in order to be able to communicate
to VK card via message queues.

This info is used for debug purposes via collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/Makefile |3 +-
 drivers/misc/bcm-vk/bcm_vk.h |  123 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c |  311 +++-
 drivers/misc/bcm-vk/bcm_vk_msg.c | 1193 ++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  132 
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  275 +++
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   61 ++
 7 files changed, 2095 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index a2ae79858409..79b4e365c9e6 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -6,5 +6,6 @@
 obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
-   bcm_vk_msg.o
+   bcm_vk_msg.o \
+   bcm_vk_sg.o
 
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 50f2a0cd6e13..d847a512d0ed 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -6,11 +6,13 @@
 #ifndef BCM_VK_H
 #define BCM_VK_H
 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -93,14 +95,53 @@
 #define MAJOR_SOC_REV(_chip_id)(((_chip_id) >> 20) & 0xf)
 
 #define BAR_CARD_TEMPERATURE   0x45c
+/* defines for all temperature sensor */
+#define BCM_VK_TEMP_FIELD_MASK 0xff
+#define BCM_VK_CPU_TEMP_SHIFT  0
+#define BCM_VK_DDR0_TEMP_SHIFT 8
+#define BCM_VK_DDR1_TEMP_SHIFT 16
 
 #define BAR_CARD_VOLTAGE   0x460
+/* defines for voltage rail conversion */
+#define BCM_VK_VOLT_RAIL_MASK  0x
+#define BCM_VK_3P3_VOLT_REG_SHIFT  16
 
 #define BAR_CARD_ERR_LOG   0x464
+/* Error log register bit definition - register for error alerts */
+#define ERR_LOG_UECC   BIT(0)
+#define ERR_LOG_SSIM_BUSY  BIT(1)
+#define ERR_LOG_AFBC_BUSY  BIT(2)
+#define ERR_LOG_HIGH_TEMP_ERR  BIT(3)
+#define ERR_LOG_WDOG_TIMEOUT   BIT(4)
+#define ERR_LOG_SYS_FAULT  BIT(5)
+#define ERR_LOG_RAMDUMPBIT(6)
+#define ERR_LOG_COP_WDOG_TIMEOUT   BIT(7)
+/* warnings */
+#define ERR_LOG_MEM_ALLOC_FAIL BIT(8)
+#define ERR_LOG_LOW_TEMP_WARN  BIT(9)
+#define ERR_LOG_ECCBIT(10)
+#define ERR_LOG_IPC_DWNBIT(11)
+
+/* Alert bit definitions detectd on host */
+#define ERR_LOG_HOST_INTF_V_FAIL   BIT(13)
+#define ERR_LOG_HOST_HB_FAIL   BIT(14)
+#define ERR_LOG_HOST_PCIE_DWN  BIT(15)
 
 #define BAR_CARD_ERR_MEM   0x468
+/* defines for mem err, all fields have same width */
+#define BCM_VK_MEM_ERR_FIELD_MASK  0xff
+#define BCM_VK_ECC_MEM_ERR_SHIFT   0
+#define BCM_VK_UECC_MEM_ERR_SHIFT  8
+/* threshold of event occurrence and logs start to come out */
+#define BCM_VK_ECC_THRESHOLD   10
+#define BCM_VK_UECC_THRESHOLD  1
 
 #define BAR_CARD_PWR_AND_THRE  0x46c
+/* defines for power and temp threshold, all fields have same width */
+#define BCM_VK_PWR_AND_THRE_FIELD_MASK 0xff
+#define BCM_VK_LOW_TEMP_THRE_SHIFT 0
+#define BCM_VK_HIGH_TEMP_THRE_SHIFT8
+#define BCM_VK_PWR_STATE_SHIFT 16
 
 #define BAR_CARD_STATIC_INFO   0x470
 
@@ -143,6 +184,11 @@
 #define BAR_FIRMWARE_TAG_SIZE  50
 #define FIRMWARE_STATUS_PRE_INIT_DONE  0x1f
 
+/* VK MSG_ID defines */
+#define VK_MSG_ID_BITMAP_SIZE  4096
+#define VK_MSG_ID_BITMAP_MASK  (VK_MSG_ID_BITMAP_SIZE - 1)
+#define VK_MSG_ID_OVERFLOW 0x
+
 /*
  * BAR1
  */
@@ -197,6 +243,10 @@
 /* VK device supports a maximum of 3 bars */
 #define MAX_BAR3
 
+/* default number of msg blk for inband SGL */
+#define BCM_VK_DEF_IB_SGL_BLK_LEN   16
+#define BCM_VK_IB_SGL_BLK_MAX   24
+
 enum pci_barno {
BAR_0 = 0,
BAR_1,
@@ -267,9 +317,27 @@ struct bcm_vk_proc_mon_info {
struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
 };
 
+struct bcm_vk_hb_ctrl {
+   struct timer_list timer;
+   u32 last_uptime;
+   u32 lost_cnt;
+};
+
+struct bcm_vk_alert {
+   u16 flags;
+   u16 notfs;
+};
+
+/* some alert counters that the driver will keep track */
+struct bcm_vk_alert_cnts {
+   u16 ecc;
+   u16 uecc;
+};
+
 struct bcm_vk {
struct pci_dev *pdev;
void __iomem *bar[MAX_BAR];
+   int num_irqs;
 
struct bcm_vk_card_info card_info;
struct bcm_vk_proc_mon_info proc_mon_info;
@@ -283,9 +351,17 @@ struct bcm_vk {
/*

[PATCH v10 07/13] misc: bcm-vk: add ioctl load_image

2021-01-28 Thread Scott Branden
Add ioctl support to issue load_image operation to VK card.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Co-developed-by: James Hu 
Signed-off-by: James Hu 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |  3 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 95 
 2 files changed, 98 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 5f0fcfdaf265..726aab71bb6b 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include "bcm_vk_msg.h"
@@ -220,6 +221,8 @@ struct bcm_vk {
 
struct bcm_vk_dauth_info dauth_info;
 
+   /* mutex to protect the ioctls */
+   struct mutex mutex;
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 79fffb1e6f84..203a1cf2bae3 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -580,6 +581,71 @@ static void bcm_vk_wq_handler(struct work_struct *work)
}
 }
 
+static long bcm_vk_load_image(struct bcm_vk *vk,
+ const struct vk_image __user *arg)
+{
+   struct device *dev = >pdev->dev;
+   const char *image_name;
+   struct vk_image image;
+   u32 next_loadable;
+   enum soc_idx idx;
+   int image_idx;
+   int ret = -EPERM;
+
+   if (copy_from_user(, arg, sizeof(image)))
+   return -EACCES;
+
+   if ((image.type != VK_IMAGE_TYPE_BOOT1) &&
+   (image.type != VK_IMAGE_TYPE_BOOT2)) {
+   dev_err(dev, "invalid image.type %u\n", image.type);
+   return ret;
+   }
+
+   next_loadable = bcm_vk_next_boot_image(vk);
+   if (next_loadable != image.type) {
+   dev_err(dev, "Next expected image %u, Loading %u\n",
+   next_loadable, image.type);
+   return ret;
+   }
+
+   /*
+* if something is pending download already.  This could only happen
+* for now when the driver is being loaded, or if someone has issued
+* another download command in another shell.
+*/
+   if (test_and_set_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload) != 0) {
+   dev_err(dev, "Download operation already pending.\n");
+   return ret;
+   }
+
+   image_name = image.filename;
+   if (image_name[0] == '\0') {
+   /* Use default image name if NULL */
+   idx = get_soc_idx(vk);
+   if (idx == VK_IDX_INVALID)
+   goto err_idx;
+
+   /* Image idx starts with boot1 */
+   image_idx = image.type - VK_IMAGE_TYPE_BOOT1;
+   image_name = get_load_fw_name(vk, _tab[idx][image_idx]);
+   if (!image_name) {
+   dev_err(dev, "No suitable image found for type %d",
+   image.type);
+   ret = -ENOENT;
+   goto err_idx;
+   }
+   } else {
+   /* Ensure filename is NULL terminated */
+   image.filename[sizeof(image.filename) - 1] = '\0';
+   }
+   ret = bcm_vk_load_image_by_type(vk, image.type, image_name);
+   dev_info(dev, "Load %s, ret %d\n", image_name, ret);
+err_idx:
+   clear_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload);
+
+   return ret;
+}
+
 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val)
 {
vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE);
@@ -636,10 +702,38 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
+{
+   long ret = -EINVAL;
+   struct bcm_vk_ctx *ctx = file->private_data;
+   struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
+   void __user *argp = (void __user *)arg;
+
+   dev_dbg(>pdev->dev,
+   "ioctl, cmd=0x%02x, arg=0x%02lx\n",
+   cmd, arg);
+
+   mutex_lock(>mutex);
+
+   switch (cmd) {
+   case VK_IOCTL_LOAD_IMAGE:
+   ret = bcm_vk_load_image(vk, argp);
+   break;
+
+   default:
+   break;
+   }
+
+   mutex_unlock(>mutex);
+
+   return ret;
+}
+
 static const struct file_operations bcm_vk_fops = {
.owner = THIS_MODULE,
.open = bcm_vk_open,
.release = bcm_vk_release,
+   .unlocked_ioctl = bcm_vk_ioctl,
 };
 
 static int bcm_vk_on_panic(struct notifier_block *nb,
@@ -670,6 +764,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id

[PATCH v10 10/13] misc: bcm-vk: reset_pid support

2021-01-28 Thread Scott Branden
Add reset support via ioctl.
Kill user processes that are open when VK card is reset.
If a particular PID has issued the reset request do not kill that process
as it issued the ioctl.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |   1 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 158 +--
 drivers/misc/bcm-vk/bcm_vk_msg.c |  40 +++-
 3 files changed, 191 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index d847a512d0ed..a1d0bf6e694c 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -468,6 +468,7 @@ irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
 int bcm_vk_msg_init(struct bcm_vk *vk);
 void bcm_vk_msg_remove(struct bcm_vk *vk);
+void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
 int bcm_vk_sync_msgq(struct bcm_vk *vk, bool force_sync);
 void bcm_vk_blk_drv_access(struct bcm_vk *vk);
 s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk);
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 76acca9b06e6..69a351fe3b38 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -504,7 +504,9 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
int i;
 
/*
-* kill all the apps
+* kill all the apps except for the process that is resetting.
+* If not called during reset, reset_pid will be 0, and all will be
+* killed.
 */
spin_lock(>ctx_lock);
 
@@ -515,10 +517,12 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
struct bcm_vk_ctx *ctx;
 
list_for_each_entry(ctx, >pid_ht[i].head, node) {
-   dev_dbg(>pdev->dev,
-   "Send kill signal to pid %d\n",
-   ctx->pid);
-   kill_pid(find_vpid(ctx->pid), SIGKILL, 1);
+   if (ctx->pid != vk->reset_pid) {
+   dev_dbg(>pdev->dev,
+   "Send kill signal to pid %d\n",
+   ctx->pid);
+   kill_pid(find_vpid(ctx->pid), SIGKILL, 1);
+   }
}
}
spin_unlock(>ctx_lock);
@@ -1001,6 +1005,49 @@ static long bcm_vk_load_image(struct bcm_vk *vk,
return ret;
 }
 
+static int bcm_vk_reset_successful(struct bcm_vk *vk)
+{
+   struct device *dev = >pdev->dev;
+   u32 fw_status, reset_reason;
+   int ret = -EAGAIN;
+
+   /*
+* Reset could be triggered when the card in several state:
+*   i)   in bootROM
+*   ii)  after boot1
+*   iii) boot2 running
+*
+* i) & ii) - no status bits will be updated.  If vkboot1
+* runs automatically after reset, it  will update the reason
+* to be unknown reason
+* iii) - reboot reason match + deinit done.
+*/
+   fw_status = vkread32(vk, BAR_0, VK_BAR_FWSTS);
+   /* immediate exit if interface goes down */
+   if (BCM_VK_INTF_IS_DOWN(fw_status)) {
+   dev_err(dev, "PCIe Intf Down!\n");
+   goto reset_exit;
+   }
+
+   reset_reason = (fw_status & VK_FWSTS_RESET_REASON_MASK);
+   if ((reset_reason == VK_FWSTS_RESET_MBOX_DB) ||
+   (reset_reason == VK_FWSTS_RESET_UNKNOWN))
+   ret = 0;
+
+   /*
+* if some of the deinit bits are set, but done
+* bit is not, this is a failure if triggered while boot2 is running
+*/
+   if ((fw_status & VK_FWSTS_DEINIT_TRIGGERED) &&
+   !(fw_status & VK_FWSTS_RESET_DONE))
+   ret = -EAGAIN;
+
+reset_exit:
+   dev_dbg(dev, "FW status = 0x%x ret %d\n", fw_status, ret);
+
+   return ret;
+}
+
 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val)
 {
vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE);
@@ -1010,12 +1057,16 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
 {
u32 i;
u32 value, boot_status;
+   bool is_stdalone, is_boot2;
static const u32 bar0_reg_clr_list[] = { BAR_OS_UPTIME,
 BAR_INTF_VER,
 BAR_CARD_VOLTAGE,
 BAR_CARD_TEMPERATURE,
 BAR_CARD_PWR_AND_THRE };
 
+   /* clean up before pressing the door bell */
+   bcm_vk_drain_msg_on_reset(vk);
+   vkwrite32(vk, 0, BAR_1, VK_BAR1_MSGQ_DEF_RDY);
/* make tag '\0' terminated */
vkwrite32(vk, 0, BAR_1, VK_BAR1_BOOT1_VER_TAG);
 
@@ -1026,6 +10

[PATCH v10 06/13] misc: bcm-vk: add open/release

2021-01-28 Thread Scott Branden
Add open/release to replace private data with context for other methods
to use.  Reason for the context is because it is allowed for multiple
sessions to open sysfs.  For each file open, when upper layer queries the
response, only those that are tied to a specified open should be returned.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/Makefile |   4 +-
 drivers/misc/bcm-vk/bcm_vk.h |  15 
 drivers/misc/bcm-vk/bcm_vk_dev.c |  23 ++
 drivers/misc/bcm-vk/bcm_vk_msg.c | 127 +++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  31 
 5 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index f8a7ac4c242f..a2ae79858409 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -5,4 +5,6 @@
 
 obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
-   bcm_vk_dev.o
+   bcm_vk_dev.o \
+   bcm_vk_msg.o
+
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index f428ad9a0c3d..5f0fcfdaf265 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -7,9 +7,14 @@
 #define BCM_VK_H
 
 #include 
+#include 
 #include 
+#include 
 #include 
 #include 
+#include 
+
+#include "bcm_vk_msg.h"
 
 #define DRV_MODULE_NAME"bcm-vk"
 
@@ -218,6 +223,13 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+   /* Reference-counting to handle file operations */
+   struct kref kref;
+
+   spinlock_t ctx_lock; /* Spinlock for component context */
+   struct bcm_vk_ctx ctx[VK_CMPT_CTX_MAX];
+   struct bcm_vk_ht_entry pid_ht[VK_PID_HT_SZ];
+
struct workqueue_struct *wq_thread;
struct work_struct wq_work; /* work queue for deferred job */
unsigned long wq_offload[1]; /* various flags on wq requested */
@@ -278,6 +290,9 @@ static inline bool bcm_vk_msgq_marker_valid(struct bcm_vk 
*vk)
return (rdy_marker == VK_BAR1_MSGQ_RDY_MARKER);
 }
 
+int bcm_vk_open(struct inode *inode, struct file *p_file);
+int bcm_vk_release(struct inode *inode, struct file *p_file);
+void bcm_vk_release_data(struct kref *kref);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
 
 #endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 09d99bd36e8a..79fffb1e6f84 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -8,6 +8,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -635,6 +636,12 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static const struct file_operations bcm_vk_fops = {
+   .owner = THIS_MODULE,
+   .open = bcm_vk_open,
+   .release = bcm_vk_release,
+};
+
 static int bcm_vk_on_panic(struct notifier_block *nb,
   unsigned long e, void *p)
 {
@@ -657,10 +664,13 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
struct miscdevice *misc_device;
u32 boot_status;
 
+   /* allocate vk structure which is tied to kref for freeing */
vk = kzalloc(sizeof(*vk), GFP_KERNEL);
if (!vk)
return -ENOMEM;
 
+   kref_init(>kref);
+
err = pci_enable_device(pdev);
if (err) {
dev_err(dev, "Cannot enable PCI device\n");
@@ -738,6 +748,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
err = -ENOMEM;
goto err_ida_remove;
}
+   misc_device->fops = _vk_fops,
 
err = misc_register(misc_device);
if (err) {
@@ -826,6 +837,16 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
return err;
 }
 
+void bcm_vk_release_data(struct kref *kref)
+{
+   struct bcm_vk *vk = container_of(kref, struct bcm_vk, kref);
+   struct pci_dev *pdev = vk->pdev;
+
+   dev_dbg(>dev, "BCM-VK:%d release data 0x%p\n", vk->devid, vk);
+   pci_dev_put(pdev);
+   kfree(vk);
+}
+
 static void bcm_vk_remove(struct pci_dev *pdev)
 {
int i;
@@ -869,6 +890,8 @@ static void bcm_vk_remove(struct pci_dev *pdev)
pci_release_regions(pdev);
pci_free_irq_vectors(pdev);
pci_disable_device(pdev);
+
+   kref_put(>kref, bcm_vk_release_data);
 }
 
 static void bcm_vk_shutdown(struct pci_dev *pdev)
diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c b/drivers/misc/bcm-vk/bcm_vk_msg.c
new file mode 100644
index ..2d9a6b4e5f61
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk_msg.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#include "bcm_vk.h&quo

[PATCH v10 00/13] Add Broadcom VK driver

2021-01-28 Thread Scott Branden
This patch series drops previous patches in [1]
that were incorporated by Kees Cook into patch series
"Introduce partial kernel_read_file() support" [2].

Remaining patches are contained in this series to add Broadcom VK driver.
(which depends on request_firmware_into_buf API addition which has
now been accepted into the upstream kernel as of v5.10-rc1).

[1] 
https://lore.kernel.org/lkml/20200706232309.12010-1-scott.bran...@broadcom.com/
[2] https://lore.kernel.org/lkml/20201002173828.2099543-1-keesc...@chromium.org/

Changes from v9:
 - fix compile if CONFIG_TTY not set
 - use if-else statement instead of conditional expression in set_q_num
 - generate bcm_vk_msg_remove call in proper patch
   (was generated in tty patch when should be in msg patch) 
Changes from v8:
 - add ack's by Olof Johansson
Changes from v7:
 - add more information in Kconfig help description
Changes from v6:
 - drop QSTATS patch as it needs to be reviewed if trace_printk makes sense
 - add wdog and IPC interface alerts
 - add boundary check to msgq and peerlog
 - clear additional registers on reset
Changes from v5:
 - dropped sysfs patch from series for now as rework to use hwmon
 - tty patch still at end of series to drop if another solution available
 - updated cover letter commit to point to Kees' latest patch submission in [2]
 - specified --base with Kees' patches applied (kernel branches don't have 
these yet)
 - removed trivial comment
 - moved location of const to before the struct in two declarations
 - changed dev_info to dev_warn and only print when irq don't match expected
 - changed dev_info to dev_dbg when printing debug QSTATS
 - removed unnecessary %p print
Changes from v4:
 - fixed memory leak in probe function on failure
 - changed -1 to -EBUSY in bcm_vk_tty return code
 - move bcm_vk_tty patch to end of patch series so it
   can be dropped from current patch series if needed
   and rearchitected if needed.
Changes from v3:
 - split driver into more incremental commits for acceptance/review
 - lowered some dev_info to dev_dbg
 - remove ANSI stdint types and replace with linux u8, etc types
 - changed an EIO return to EPFNOSUPPORT
 - move vk_msg_cmd internal to driver to not expose to UAPI at this time
Changes from v2:
 - open code BIT macro in uapi header
 - A0/B0 boot improvements
Changes from v1:
 - declare bcm_vk_intf_ver_chk as static

Scott Branden (13):
  bcm-vk: add bcm_vk UAPI
  misc: bcm-vk: add Broadcom VK driver
  misc: bcm-vk: add autoload support
  misc: bcm-vk: add misc device to Broadcom VK driver
  misc: bcm-vk: add triggers when host panic or reboots to notify card
  misc: bcm-vk: add open/release
  misc: bcm-vk: add ioctl load_image
  misc: bcm-vk: add get_card_info, peerlog_info, and proc_mon_info
  misc: bcm-vk: add VK messaging support
  misc: bcm-vk: reset_pid support
  misc: bcm-vk: add mmap function for exposing BAR2
  MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver
  misc: bcm-vk: add ttyVK support

 MAINTAINERS  |7 +
 drivers/misc/Kconfig |1 +
 drivers/misc/Makefile|1 +
 drivers/misc/bcm-vk/Kconfig  |   17 +
 drivers/misc/bcm-vk/Makefile |   12 +
 drivers/misc/bcm-vk/bcm_vk.h |  513 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 1664 ++
 drivers/misc/bcm-vk/bcm_vk_msg.c | 1356 
 drivers/misc/bcm-vk/bcm_vk_msg.h |  163 +++
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  275 +
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   61 ++
 drivers/misc/bcm-vk/bcm_vk_tty.c |  333 ++
 include/uapi/linux/misc/bcm_vk.h |   84 ++
 13 files changed, 4487 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c
 create mode 100644 include/uapi/linux/misc/bcm_vk.h

-- 
2.17.1



[PATCH v10 05/13] misc: bcm-vk: add triggers when host panic or reboots to notify card

2021-01-28 Thread Scott Branden
Pass down an interrupt to card in case of panic or reboot so
that card can take appropriate action to perform a clean reset.
Uses kernel notifier block either directly (register on panic list),
or implicitly (add shutdown method for PCI device).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |  2 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 29 -
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 0a366db693c8..f428ad9a0c3d 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -223,6 +223,8 @@ struct bcm_vk {
unsigned long wq_offload[1]; /* various flags on wq requested */
void *tdma_vaddr; /* test dma segment virtual addr */
dma_addr_t tdma_addr; /* test dma segment bus addr */
+
+   struct notifier_block panic_nb;
 };
 
 /* wq offload work items bits definitions */
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 4ecd5b5f80d3..09d99bd36e8a 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -635,6 +635,16 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static int bcm_vk_on_panic(struct notifier_block *nb,
+  unsigned long e, void *p)
+{
+   struct bcm_vk *vk = container_of(nb, struct bcm_vk, panic_nb);
+
+   bcm_to_v_reset_doorbell(vk, VK_BAR0_RESET_DB_HARD);
+
+   return 0;
+}
+
 static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
int err;
@@ -748,6 +758,15 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
/* sync other info */
bcm_vk_sync_card_info(vk);
 
+   /* register for panic notifier */
+   vk->panic_nb.notifier_call = bcm_vk_on_panic;
+   err = atomic_notifier_chain_register(_notifier_list,
+>panic_nb);
+   if (err) {
+   dev_err(dev, "Fail to register panic notifier\n");
+   goto err_destroy_workqueue;
+   }
+
/*
 * lets trigger an auto download.  We don't want to do it serially here
 * because at probing time, it is not supposed to block for a long time.
@@ -756,7 +775,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
if (auto_load) {
if ((boot_status & BOOT_STATE_MASK) == BROM_RUNNING) {
if (bcm_vk_trigger_autoload(vk))
-   goto err_destroy_workqueue;
+   goto err_unregister_panic_notifier;
} else {
dev_err(dev,
"Auto-load skipped - BROM not in proper state 
(0x%x)\n",
@@ -768,6 +787,10 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
 
return 0;
 
+err_unregister_panic_notifier:
+   atomic_notifier_chain_unregister(_notifier_list,
+>panic_nb);
+
 err_destroy_workqueue:
destroy_workqueue(vk->wq_thread);
 
@@ -818,6 +841,10 @@ static void bcm_vk_remove(struct pci_dev *pdev)
bcm_vk_trigger_reset(vk);
usleep_range(BCM_VK_UCODE_BOOT_US, BCM_VK_UCODE_BOOT_MAX_US);
 
+   /* unregister panic notifier */
+   atomic_notifier_chain_unregister(_notifier_list,
+>panic_nb);
+
if (vk->tdma_vaddr)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
  vk->tdma_vaddr, vk->tdma_addr);
-- 
2.17.1



[PATCH v10 01/13] bcm-vk: add bcm_vk UAPI

2021-01-28 Thread Scott Branden
Add user space api for bcm-vk driver.

Provide ioctl api to load images and issue reset command to card.
FW status registers in PCI BAR space also defined as part
of API so that user space is able to interpret these memory locations
as needed via direct PCIe access.

Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 include/uapi/linux/misc/bcm_vk.h | 84 
 1 file changed, 84 insertions(+)
 create mode 100644 include/uapi/linux/misc/bcm_vk.h

diff --git a/include/uapi/linux/misc/bcm_vk.h b/include/uapi/linux/misc/bcm_vk.h
new file mode 100644
index ..ec28e0bd46a9
--- /dev/null
+++ b/include/uapi/linux/misc/bcm_vk.h
@@ -0,0 +1,84 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#ifndef __UAPI_LINUX_MISC_BCM_VK_H
+#define __UAPI_LINUX_MISC_BCM_VK_H
+
+#include 
+#include 
+
+#define BCM_VK_MAX_FILENAME 64
+
+struct vk_image {
+   __u32 type; /* Type of image */
+#define VK_IMAGE_TYPE_BOOT1 1 /* 1st stage (load to SRAM) */
+#define VK_IMAGE_TYPE_BOOT2 2 /* 2nd stage (load to DDR) */
+   __u8 filename[BCM_VK_MAX_FILENAME]; /* Filename of image */
+};
+
+struct vk_reset {
+   __u32 arg1;
+   __u32 arg2;
+};
+
+#define VK_MAGIC   0x5e
+
+/* Load image to Valkyrie */
+#define VK_IOCTL_LOAD_IMAGE_IOW(VK_MAGIC, 0x2, struct vk_image)
+
+/* Send Reset to Valkyrie */
+#define VK_IOCTL_RESET _IOW(VK_MAGIC, 0x4, struct vk_reset)
+
+/*
+ * Firmware Status accessed directly via BAR space
+ */
+#define VK_BAR_FWSTS   0x41c
+#define VK_BAR_COP_FWSTS   0x428
+/* VK_FWSTS definitions */
+#define VK_FWSTS_RELOCATION_ENTRY  (1UL << 0)
+#define VK_FWSTS_RELOCATION_EXIT   (1UL << 1)
+#define VK_FWSTS_INIT_START(1UL << 2)
+#define VK_FWSTS_ARCH_INIT_DONE(1UL << 3)
+#define VK_FWSTS_PRE_KNL1_INIT_DONE(1UL << 4)
+#define VK_FWSTS_PRE_KNL2_INIT_DONE(1UL << 5)
+#define VK_FWSTS_POST_KNL_INIT_DONE(1UL << 6)
+#define VK_FWSTS_INIT_DONE (1UL << 7)
+#define VK_FWSTS_APP_INIT_START(1UL << 8)
+#define VK_FWSTS_APP_INIT_DONE (1UL << 9)
+#define VK_FWSTS_MASK  0x
+#define VK_FWSTS_READY (VK_FWSTS_INIT_START | \
+VK_FWSTS_ARCH_INIT_DONE | \
+VK_FWSTS_PRE_KNL1_INIT_DONE | \
+VK_FWSTS_PRE_KNL2_INIT_DONE | \
+VK_FWSTS_POST_KNL_INIT_DONE | \
+VK_FWSTS_INIT_DONE | \
+VK_FWSTS_APP_INIT_START | \
+VK_FWSTS_APP_INIT_DONE)
+/* Deinit */
+#define VK_FWSTS_APP_DEINIT_START  (1UL << 23)
+#define VK_FWSTS_APP_DEINIT_DONE   (1UL << 24)
+#define VK_FWSTS_DRV_DEINIT_START  (1UL << 25)
+#define VK_FWSTS_DRV_DEINIT_DONE   (1UL << 26)
+#define VK_FWSTS_RESET_DONE(1UL << 27)
+#define VK_FWSTS_DEINIT_TRIGGERED  (VK_FWSTS_APP_DEINIT_START | \
+VK_FWSTS_APP_DEINIT_DONE  | \
+VK_FWSTS_DRV_DEINIT_START | \
+VK_FWSTS_DRV_DEINIT_DONE)
+/* Last nibble for reboot reason */
+#define VK_FWSTS_RESET_REASON_SHIFT28
+#define VK_FWSTS_RESET_REASON_MASK (0xf << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_SYS_PWRUP   (0x0 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_MBOX_DB (0x1 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_M7_WDOG (0x2 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_TEMP(0x3 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_FLR (0x4 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_HOT (0x5 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_WARM(0x6 << 
VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_COLD(0x7 << 
VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_L1  (0x8 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_L0  (0x9 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_UNKNOWN (0xf << VK_FWSTS_RESET_REASON_SHIFT)
+
+#endif /* __UAPI_LINUX_MISC_BCM_VK_H */
-- 
2.17.1



[PATCH v10 02/13] misc: bcm-vk: add Broadcom VK driver

2021-01-28 Thread Scott Branden
Add initial version of Broadcom VK driver to enumerate PCI device IDs
of Valkyrie and Viper device IDs.

VK based cards provide real-time high performance, high throughput,
low latency offload compute engine operations.
They are used for multiple parallel offload tasks as:
audio, video and image processing and crypto operations.

Further commits add additional features to driver beyond probe/remove.

Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/Kconfig |   1 +
 drivers/misc/Makefile|   1 +
 drivers/misc/bcm-vk/Kconfig  |  17 
 drivers/misc/bcm-vk/Makefile |   8 ++
 drivers/misc/bcm-vk/bcm_vk.h |  29 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 141 +++
 6 files changed, 197 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index fafa8b0d8099..591903773a6d 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -478,6 +478,7 @@ source "drivers/misc/genwqe/Kconfig"
 source "drivers/misc/echo/Kconfig"
 source "drivers/misc/cxl/Kconfig"
 source "drivers/misc/ocxl/Kconfig"
+source "drivers/misc/bcm-vk/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
 source "drivers/misc/habanalabs/Kconfig"
 source "drivers/misc/uacce/Kconfig"
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index d23231e73330..54f2fe2d9448 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_ECHO)+= echo/
 obj-$(CONFIG_CXL_BASE) += cxl/
 obj-$(CONFIG_PCI_ENDPOINT_TEST)+= pci_endpoint_test.o
 obj-$(CONFIG_OCXL) += ocxl/
+obj-$(CONFIG_BCM_VK)   += bcm-vk/
 obj-y  += cardreader/
 obj-$(CONFIG_PVPANIC)  += pvpanic.o
 obj-$(CONFIG_HABANA_AI)+= habanalabs/
diff --git a/drivers/misc/bcm-vk/Kconfig b/drivers/misc/bcm-vk/Kconfig
new file mode 100644
index ..052f6f28b540
--- /dev/null
+++ b/drivers/misc/bcm-vk/Kconfig
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Broadcom VK device
+#
+config BCM_VK
+   tristate "Support for Broadcom VK Accelerators"
+   depends on PCI_MSI
+   help
+ Select this option to enable support for Broadcom
+ VK Accelerators.  VK is used for performing
+ multiple specific offload processing tasks in parallel.
+ Such offload tasks assist in such operations as video
+ transcoding, compression, and crypto tasks.
+ This driver enables userspace programs to access these
+ accelerators via /dev/bcm-vk.N devices.
+
+ If unsure, say N.
diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
new file mode 100644
index ..f8a7ac4c242f
--- /dev/null
+++ b/drivers/misc/bcm-vk/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Broadcom VK driver
+#
+
+obj-$(CONFIG_BCM_VK) += bcm_vk.o
+bcm_vk-objs := \
+   bcm_vk_dev.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
new file mode 100644
index ..9152785199ab
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#ifndef BCM_VK_H
+#define BCM_VK_H
+
+#include 
+
+#define DRV_MODULE_NAME"bcm-vk"
+
+/* VK device supports a maximum of 3 bars */
+#define MAX_BAR3
+
+enum pci_barno {
+   BAR_0 = 0,
+   BAR_1,
+   BAR_2
+};
+
+#define BCM_VK_NUM_TTY 2
+
+struct bcm_vk {
+   struct pci_dev *pdev;
+   void __iomem *bar[MAX_BAR];
+};
+
+#endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
new file mode 100644
index ..14afe2477b97
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include "bcm_vk.h"
+
+#define PCI_DEVICE_ID_VALKYRIE 0x5e87
+#define PCI_DEVICE_ID_VIPER0x5e88
+
+/* MSIX usages */
+#define VK_MSIX_MSGQ_MAX   3
+#define VK_MSIX_NOTF_MAX   1
+#define VK_MSIX_TTY_MAXBCM_VK_NUM_TTY
+#define VK_MSIX_IRQ_MAX(VK_MSIX_MSGQ_MAX + 
VK_MSIX_NOTF_MAX + \
+VK_MSIX_TTY_MAX)
+#define VK_MSIX_IRQ_MIN_REQ (VK_MSIX_MSGQ_MAX + VK_MSIX_NOTF_MAX)
+
+/* Number of bits set in DMA mask*/
+#define BCM_VK_DMA_BITS64
+
+static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{
+   int err;
+   int i;
+   int irq

[PATCH v10 04/13] misc: bcm-vk: add misc device to Broadcom VK driver

2021-01-28 Thread Scott Branden
Add misc device base support to create and remove devnode.
Additional misc functions for open/read/write/release/ioctl/sysfs, etc
will be added in follow on commits to allow for individual review.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |  2 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 36 +++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index c4fb61a84e41..0a366db693c8 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -7,6 +7,7 @@
 #define BCM_VK_H
 
 #include 
+#include 
 #include 
 #include 
 
@@ -214,6 +215,7 @@ struct bcm_vk {
 
struct bcm_vk_dauth_info dauth_info;
 
+   struct miscdevice miscdev;
int devid; /* dev id allocated */
 
struct workqueue_struct *wq_thread;
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index adc3103c7012..4ecd5b5f80d3 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -7,6 +7,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -643,6 +644,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
char name[20];
struct bcm_vk *vk;
struct device *dev = >dev;
+   struct miscdevice *misc_device;
u32 boot_status;
 
vk = kzalloc(sizeof(*vk), GFP_KERNEL);
@@ -719,6 +721,19 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
 
vk->devid = id;
snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
+   misc_device = >miscdev;
+   misc_device->minor = MISC_DYNAMIC_MINOR;
+   misc_device->name = kstrdup(name, GFP_KERNEL);
+   if (!misc_device->name) {
+   err = -ENOMEM;
+   goto err_ida_remove;
+   }
+
+   err = misc_register(misc_device);
+   if (err) {
+   dev_err(dev, "failed to register device\n");
+   goto err_kfree_name;
+   }
 
INIT_WORK(>wq_work, bcm_vk_wq_handler);
 
@@ -727,7 +742,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
if (!vk->wq_thread) {
dev_err(dev, "Fail to create workqueue thread\n");
err = -ENOMEM;
-   goto err_ida_remove;
+   goto err_misc_deregister;
}
 
/* sync other info */
@@ -749,11 +764,20 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
}
 
+   dev_dbg(dev, "BCM-VK:%u created\n", id);
+
return 0;
 
 err_destroy_workqueue:
destroy_workqueue(vk->wq_thread);
 
+err_misc_deregister:
+   misc_deregister(misc_device);
+
+err_kfree_name:
+   kfree(misc_device->name);
+   misc_device->name = NULL;
+
 err_ida_remove:
ida_simple_remove(_vk_ida, id);
 
@@ -783,6 +807,7 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 {
int i;
struct bcm_vk *vk = pci_get_drvdata(pdev);
+   struct miscdevice *misc_device = >miscdev;
 
/*
 * Trigger a reset to card and wait enough time for UCODE to rerun,
@@ -797,6 +822,13 @@ static void bcm_vk_remove(struct pci_dev *pdev)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
  vk->tdma_vaddr, vk->tdma_addr);
 
+   /* remove if name is set which means misc dev registered */
+   if (misc_device->name) {
+   misc_deregister(misc_device);
+   kfree(misc_device->name);
+   ida_simple_remove(_vk_ida, vk->devid);
+   }
+
cancel_work_sync(>wq_work);
destroy_workqueue(vk->wq_thread);
 
@@ -805,6 +837,8 @@ static void bcm_vk_remove(struct pci_dev *pdev)
pci_iounmap(pdev, vk->bar[i]);
}
 
+   dev_dbg(>dev, "BCM-VK:%d released\n", vk->devid);
+
pci_release_regions(pdev);
pci_free_irq_vectors(pdev);
pci_disable_device(pdev);
-- 
2.17.1



[PATCH v10 03/13] misc: bcm-vk: add autoload support

2021-01-28 Thread Scott Branden
Add support to load and boot images on card automatically.
The kernel module parameter auto_load can be passed in as false to disable
such support on probe.
As well, nr_scratch_pages can be specified to allocate more or less scratch
memory on init as needed for desired card operation.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Co-developed-by: James Hu 
Signed-off-by: James Hu 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h | 250 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 723 +++
 2 files changed, 973 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 9152785199ab..c4fb61a84e41 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -6,10 +6,187 @@
 #ifndef BCM_VK_H
 #define BCM_VK_H
 
+#include 
 #include 
+#include 
 
 #define DRV_MODULE_NAME"bcm-vk"
 
+/*
+ * Load Image is completed in two stages:
+ *
+ * 1) When the VK device boot-up, M7 CPU runs and executes the BootROM.
+ * The Secure Boot Loader (SBL) as part of the BootROM will run
+ * to open up ITCM for host to push BOOT1 image.
+ * SBL will authenticate the image before jumping to BOOT1 image.
+ *
+ * 2) Because BOOT1 image is a secured image, we also called it the
+ * Secure Boot Image (SBI). At second stage, SBI will initialize DDR
+ * and wait for host to push BOOT2 image to DDR.
+ * SBI will authenticate the image before jumping to BOOT2 image.
+ *
+ */
+/* Location of registers of interest in BAR0 */
+
+/* Request register for Secure Boot Loader (SBL) download */
+#define BAR_CODEPUSH_SBL   0x400
+/* Start of ITCM */
+#define CODEPUSH_BOOT1_ENTRY   0x0040
+#define CODEPUSH_MASK  0xf000
+#define CODEPUSH_BOOTSTART BIT(0)
+
+/* Boot Status register */
+#define BAR_BOOT_STATUS0x404
+
+#define SRAM_OPEN  BIT(16)
+#define DDR_OPEN   BIT(17)
+
+/* Firmware loader progress status definitions */
+#define FW_LOADER_ACK_SEND_MORE_DATA   BIT(18)
+#define FW_LOADER_ACK_IN_PROGRESS  BIT(19)
+#define FW_LOADER_ACK_RCVD_ALL_DATABIT(20)
+
+/* Boot1/2 is running in standalone mode */
+#define BOOT_STDALONE_RUNNING  BIT(21)
+
+/* definitions for boot status register */
+#define BOOT_STATE_MASK(0x & \
+~(FW_LOADER_ACK_SEND_MORE_DATA | \
+  FW_LOADER_ACK_IN_PROGRESS | \
+  BOOT_STDALONE_RUNNING))
+
+#define BOOT_ERR_SHIFT 4
+#define BOOT_ERR_MASK  (0xf << BOOT_ERR_SHIFT)
+#define BOOT_PROG_MASK 0xf
+
+#define BROM_STATUS_NOT_RUN0x2
+#define BROM_NOT_RUN   (SRAM_OPEN | BROM_STATUS_NOT_RUN)
+#define BROM_STATUS_COMPLETE   0x6
+#define BROM_RUNNING   (SRAM_OPEN | BROM_STATUS_COMPLETE)
+#define BOOT1_STATUS_COMPLETE  0x6
+#define BOOT1_RUNNING  (DDR_OPEN | BOOT1_STATUS_COMPLETE)
+#define BOOT2_STATUS_COMPLETE  0x6
+#define BOOT2_RUNNING  (FW_LOADER_ACK_RCVD_ALL_DATA | \
+BOOT2_STATUS_COMPLETE)
+
+/* Boot request for Secure Boot Image (SBI) */
+#define BAR_CODEPUSH_SBI   0x408
+/* 64M mapped to BAR2 */
+#define CODEPUSH_BOOT2_ENTRY   0x6000
+
+#define BAR_CARD_STATUS0x410
+
+#define BAR_BOOT1_STDALONE_PROGRESS0x420
+#define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
+#define BOOT1_STDALONE_PROGRESS_MASK   BOOT1_STDALONE_SUCCESS
+
+#define BAR_METADATA_VERSION   0x440
+#define BAR_OS_UPTIME  0x444
+#define BAR_CHIP_ID0x448
+#define MAJOR_SOC_REV(_chip_id)(((_chip_id) >> 20) & 0xf)
+
+#define BAR_CARD_TEMPERATURE   0x45c
+
+#define BAR_CARD_VOLTAGE   0x460
+
+#define BAR_CARD_ERR_LOG   0x464
+
+#define BAR_CARD_ERR_MEM   0x468
+
+#define BAR_CARD_PWR_AND_THRE  0x46c
+
+#define BAR_CARD_STATIC_INFO   0x470
+
+#define BAR_INTF_VER   0x47c
+#define BAR_INTF_VER_MAJOR_SHIFT   16
+#define BAR_INTF_VER_MASK  0x
+/*
+ * major and minor semantic version numbers supported
+ * Please update as required on interface changes
+ */
+#define SEMANTIC_MAJOR 1
+#define SEMANTIC_MINOR 0
+
+/*
+ * first door bell reg, ie for queue = 0.  Only need the first one, as
+ * we will use the queue number to derive the others
+ */
+#define VK_BAR0_REGSEG_DB_BASE 0x484
+#define VK_BAR0_REGSEG_DB_REG_GAP  8 /*
+  * DB register gap,
+  *

Re: 5.10 LTS Kernel: 2 or 6 years?

2021-01-26 Thread Scott Branden
Hi Greg,


On 2021-01-25 11:29 p.m., Greg Kroah-Hartman wrote:
> On Mon, Jan 25, 2021 at 11:55:11AM -0800, Scott Branden wrote:
>> Hi All,
>>
>> The 5.10 LTS kernel being officially LTS supported for 2 years presents a 
>> problem:
>> why would anyone select a 5.10 kernel with 2 year LTS when 5.4 kernel has a 
>> 6 year LTS.
> Because they want to use all of the latest stuff that 5.10 provides
> them.  Don't you want faster and more secure kernels for your devices?
Yes, 5.10 is a more secure and less buggy kernel than 5.4.
You don't need to convince me which is the better kernel to start using.
The kernel improves every version and we are at a point where regressions 
rarely happen.
But, it is not our choice what version a customer chooses as they have many 
valid reasons to stabilize their code.
They do have extensive QA cycles to go through and cannot simply pick up a 
newer kernel version to get a security fix or bug fix.
As everyone, we also have testing and cannot test every kernel version 
extensively so must choose wisely what to test.
We also track the mainline a test as well to ensure functionality - but stress 
testing is not done at the same level as a full QA cycle.
Unfortunately some people think that 5.4 LTS is more stable than 5.10.  And it 
also has a longer lifetime.
So, people starting a new project today decide to use 5.4 (or even earlier) 
kernels as they deem these stable.
For these reasons we would be forced to backport (upstreamed code) to an old 
kernel for new features and processors, etc not supported in 5.4.
You can see how such a kernel would not be as stable as 5.10 if a single subtle 
fix is not backported.  But, try convincing people to go with 5.10 with only a 
2 year LTS lifespan vs. 5.4 with many more years.
Hopefully you understand the kernel is not the product - the end solution is 
what is tested and QA'd.
And, picking up small, safe LTS versions requires a much smaller QA cycle than 
a kernel upgrade with thousands of possible changes in the code every kernel 
version.
>
>> Yet, various unofficial reports indicate it will be supported for 6 years.
> Rumors are nice, aren't they :)
>
>>   And AOSP has already declared the use
>> of 5.10 kernel in their Android S and T releases.
> Publically?  Where?  And is that really the name of the new Android
> releases, I thought they switched to numbers now (hence the naming of
> the current android-common kernel branches, marketing is fun...)
https://source.android.com/devices/architecture/kernel/android-common
Feature and launch kernels provides kernels supported per version.
>
>> Is there some way we could make the LTS support more clear.
>> A 2 year declaration is not LTS any more.
> Not true at all, a "normal" stable kernel is dropped after the next
> release happens, making their lifespan about 4 months long.  2 years is
> much longer than 4 months, so it still is a "long term supported" kernel
> in contrast, correct?
Perhaps a new name needs to be made for "LTS" for 6 years to distinguish it 
from 2 years.
The timeframes are very different.
>
>> If 5.10 is "actually" going to be supported for 6 years it would be quite 
>> valuable to make such a declaration.
>> https://www.kernel.org/category/releases.html
> Why?  What would that change?
>
> Ok, seriously, this happens every year, and every year we go through the
> same thing, it's not like this is somehow new, right?
No, but why do we need to keep playing the same game every year now.
>
> I want to see companies _using_ the kernel, and most importantly,
> _updating_ their devices with it, to know if it is worth to keep around
> for longer than 2 years.  I also, hopefully, want to see how those
> companies will help me out in the testing and maintenance of that kernel
> version in order to make supporting it for 6 years actually possible.
>
> So, are you planning on using 5.10?  Will you will be willing to help
> out in testing the -rc releases I make to let me know if there are any
> problems, and to help in pointing out and backporting any specific
> patches that your platforms need for that kernel release?
> When I get this kind of promises and support from companies, then I am
> glad to bump up the length of the kernel support from 2 to 6 years, and
> I mark it on the web site.  Traditionally this happens in Febuary/March
> once I hear from enough companies.  Can I count on your support in this
> endeavor?
It is a chicken and egg problem and 2 year LTS declaration is not helping 
customers commit to 5.10.
We are using 5.10 right now and testing it.  But, we don't make the end 
products.
If customers decide to use 5.10 we will continue to test, support, and backport 
any specific patches to it.
If they choose 5.4 that would be the one we wo

5.10 LTS Kernel: 2 or 6 years?

2021-01-25 Thread Scott Branden
Hi All,

The 5.10 LTS kernel being officially LTS supported for 2 years presents a 
problem:
why would anyone select a 5.10 kernel with 2 year LTS when 5.4 kernel has a 6 
year LTS.

Yet, various unofficial reports indicate it will be supported for 6 years.  And 
AOSP has already declared the use
of 5.10 kernel in their Android S and T releases.

Is there some way we could make the LTS support more clear.
A 2 year declaration is not LTS any more.
If 5.10 is "actually" going to be supported for 6 years it would be quite 
valuable to make such a declaration.
https://www.kernel.org/category/releases.html

Regards,
 Scott








Re: [PATCH] diffconfig: use python3 instead of python in Shebang line

2021-01-22 Thread Scott Branden



On 2021-01-21 6:18 p.m., Joe Perches wrote:
> On Fri, 2021-01-22 at 05:25 +0900, Masahiro Yamada wrote:
>> On Fri, Jan 22, 2021 at 2:17 AM Scott Branden
>>  wrote:
>>> Use python3 instead of python in diffconfig Shebang line.
>>> python2 was sunset January 1, 2000 and environments do not need
>>> to support python any more.
> python2 was sunset Jan 1, 2020, not 20 years before that.
>
> https://www.python.org/doc/sunset-python-2/
With the past year: Jan 1, 2020 seems about 20 years ago.
>
>



Re: [PATCH] diffconfig: use python3 instead of python in Shebang line

2021-01-21 Thread Scott Branden
Hi Andy,

On 2021-01-21 12:35 p.m., Andy Shevchenko wrote:
> On Thu, Jan 21, 2021 at 10:31 PM Andy Shevchenko
>  wrote:
>> On Thu, Jan 21, 2021 at 10:28 PM Masahiro Yamada  
>> wrote:
>>> On Fri, Jan 22, 2021 at 2:17 AM Scott Branden
>>>  wrote:
>>>> Use python3 instead of python in diffconfig Shebang line.
>>>> python2 was sunset January 1, 2000 and environments do not need
>>>> to support python any more.
>>> Just from curiosity, what problem is this solving?
>>>
>>> Is there a distribution where 'python' does not exist,
>>> but 'python3' does ?
>> Yes. Called surprise surprise Debian
>> An it's a rare case when I agree with them.
> For the record, you seems haven't noticed:
> https://lkml.org/lkml/2020/12/9/446
It doesn't look like your change made it into 5.11-rc but I think it should be 
added?



Re: [PATCH] diffconfig: use python3 instead of python in Shebang line

2021-01-21 Thread Scott Branden
Hi Masahiro,

On 2021-01-21 12:25 p.m., Masahiro Yamada wrote:
> On Fri, Jan 22, 2021 at 2:17 AM Scott Branden
>  wrote:
>> Use python3 instead of python in diffconfig Shebang line.
>> python2 was sunset January 1, 2000 and environments do not need
>> to support python any more.
>>
>> Fixes: b24413180f56 ("tweewide: Fix most Shebang lines")
>> Signed-off-by: Scott Branden 
>> ---
>>  scripts/diffconfig | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/scripts/diffconfig b/scripts/diffconfig
>> index 627eba5849b5..d5da5fa05d1d 100755
>> --- a/scripts/diffconfig
>> +++ b/scripts/diffconfig
>> @@ -1,4 +1,4 @@
>> -#!/usr/bin/env python
>> +#!/usr/bin/env python3
>>  # SPDX-License-Identifier: GPL-2.0
>>  #
>>  # diffconfig - a tool to compare .config files.
>> --
>> 2.17.1
>>
> Just from curiosity, what problem is this solving?
It is solving the problem that python is long past its life:
https://www.python.org/doc/sunset-python-2/

The 5.11-rc kernel diffconfig doesn't work now due
to the /usr/bin/python change to /usr/bin/env python.

Could we please ensure this makes it into 5.11 and/or
maintains the FIxes tag if it does not?
>
> Is there a distribution where 'python' does not exist,
> but 'python3' does ?
Yes, Python is finally being removed from some distributions
and has already been done so from my yocto builds where
I detected the problem.
>
Thanks,
Scott


[PATCH] diffconfig: use python3 instead of python in Shebang line

2021-01-21 Thread Scott Branden
Use python3 instead of python in diffconfig Shebang line.
python2 was sunset January 1, 2000 and environments do not need
to support python any more.

Fixes: b24413180f56 ("tweewide: Fix most Shebang lines")
Signed-off-by: Scott Branden 
---
 scripts/diffconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/diffconfig b/scripts/diffconfig
index 627eba5849b5..d5da5fa05d1d 100755
--- a/scripts/diffconfig
+++ b/scripts/diffconfig
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # SPDX-License-Identifier: GPL-2.0
 #
 # diffconfig - a tool to compare .config files.
-- 
2.17.1



[PATCH] arm64: dts: stingray: remove sata

2021-01-20 Thread Scott Branden
Remove sata from stingray as it is unsupported.

Signed-off-by: Scott Branden 
---
 .../dts/broadcom/stingray/bcm958742-base.dtsi |  64 
 .../dts/broadcom/stingray/stingray-sata.dtsi  | 278 --
 .../boot/dts/broadcom/stingray/stingray.dtsi  |   7 -
 3 files changed, 349 deletions(-)
 delete mode 100644 arch/arm64/boot/dts/broadcom/stingray/stingray-sata.dtsi

diff --git a/arch/arm64/boot/dts/broadcom/stingray/bcm958742-base.dtsi 
b/arch/arm64/boot/dts/broadcom/stingray/bcm958742-base.dtsi
index 43aa5e9c0020..8fe7325cfbb2 100644
--- a/arch/arm64/boot/dts/broadcom/stingray/bcm958742-base.dtsi
+++ b/arch/arm64/boot/dts/broadcom/stingray/bcm958742-base.dtsi
@@ -56,70 +56,6 @@
};
 };
 
- {
-   status = "okay";
-};
-
-_phy0{
-   status = "okay";
-};
-
- {
-   status = "okay";
-};
-
-_phy1{
-   status = "okay";
-};
-
- {
-   status = "okay";
-};
-
-_phy2{
-   status = "okay";
-};
-
- {
-   status = "okay";
-};
-
-_phy3{
-   status = "okay";
-};
-
- {
-   status = "okay";
-};
-
-_phy4{
-   status = "okay";
-};
-
- {
-   status = "okay";
-};
-
-_phy5{
-   status = "okay";
-};
-
- {
-   status = "okay";
-};
-
-_phy6{
-   status = "okay";
-};
-
- {
-   status = "okay";
-};
-
-_phy7{
-   status = "okay";
-};
-
  {
status = "okay";
 };
diff --git a/arch/arm64/boot/dts/broadcom/stingray/stingray-sata.dtsi 
b/arch/arm64/boot/dts/broadcom/stingray/stingray-sata.dtsi
deleted file mode 100644
index 8c68e0c26f1b..
--- a/arch/arm64/boot/dts/broadcom/stingray/stingray-sata.dtsi
+++ /dev/null
@@ -1,278 +0,0 @@
-/*
- *  BSD LICENSE
- *
- *  Copyright(c) 2016-2017 Broadcom.  All rights reserved.
- *
- *  Redistribution and use in source and binary forms, with or without
- *  modification, are permitted provided that the following conditions
- *  are met:
- *
- ** Redistributions of source code must retain the above copyright
- *  notice, this list of conditions and the following disclaimer.
- ** Redistributions in binary form must reproduce the above copyright
- *  notice, this list of conditions and the following disclaimer in
- *  the documentation and/or other materials provided with the
- *  distribution.
- ** Neither the name of Broadcom nor the names of its
- *  contributors may be used to endorse or promote products derived
- *  from this software without specific prior written permission.
- *
- *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-   sata {
-   compatible = "simple-bus";
-   #address-cells = <1>;
-   #size-cells = <1>;
-   ranges = <0x0 0x0 0x67d0 0x0080>;
-
-   sata0: ahci@0 {
-   compatible = "brcm,iproc-ahci", "generic-ahci";
-   reg = <0x 0x1000>;
-   reg-names = "ahci";
-   interrupts = ;
-   #address-cells = <1>;
-   #size-cells = <0>;
-   status = "disabled";
-
-   sata0_port0: sata-port@0 {
-   reg = <0>;
-   phys = <_phy0>;
-   phy-names = "sata-phy";
-   };
-   };
-
-   sata_phy0: sata_phy@2100 {
-   compatible = "brcm,iproc-sr-sata-phy";
-   reg = <0x2100 0x1000>;
-   reg-names = "phy";
-   #address-cells = <1>;
-   #size-cells = <0>;
-   status = "disabled";
-
-   sata0_phy0: sata-phy@0 {
-   reg = <0>;
-   #phy-cells = <0>;
-   };
-   };
-
-   sata1: ah

[PATCH v9 10/13] misc: bcm-vk: reset_pid support

2021-01-20 Thread Scott Branden
Add reset support via ioctl.
Kill user processes that are open when VK card is reset.
If a particular PID has issued the reset request do not kill that process
as it issued the ioctl.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |   1 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 158 +--
 drivers/misc/bcm-vk/bcm_vk_msg.c |  40 +++-
 3 files changed, 191 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index d847a512d0ed..a1d0bf6e694c 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -468,6 +468,7 @@ irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
 int bcm_vk_msg_init(struct bcm_vk *vk);
 void bcm_vk_msg_remove(struct bcm_vk *vk);
+void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
 int bcm_vk_sync_msgq(struct bcm_vk *vk, bool force_sync);
 void bcm_vk_blk_drv_access(struct bcm_vk *vk);
 s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk);
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 5d82f02c0f27..e572a7b18fab 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -504,7 +504,9 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
int i;
 
/*
-* kill all the apps
+* kill all the apps except for the process that is resetting.
+* If not called during reset, reset_pid will be 0, and all will be
+* killed.
 */
spin_lock(>ctx_lock);
 
@@ -515,10 +517,12 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
struct bcm_vk_ctx *ctx;
 
list_for_each_entry(ctx, >pid_ht[i].head, node) {
-   dev_dbg(>pdev->dev,
-   "Send kill signal to pid %d\n",
-   ctx->pid);
-   kill_pid(find_vpid(ctx->pid), SIGKILL, 1);
+   if (ctx->pid != vk->reset_pid) {
+   dev_dbg(>pdev->dev,
+   "Send kill signal to pid %d\n",
+   ctx->pid);
+   kill_pid(find_vpid(ctx->pid), SIGKILL, 1);
+   }
}
}
spin_unlock(>ctx_lock);
@@ -1001,6 +1005,49 @@ static long bcm_vk_load_image(struct bcm_vk *vk,
return ret;
 }
 
+static int bcm_vk_reset_successful(struct bcm_vk *vk)
+{
+   struct device *dev = >pdev->dev;
+   u32 fw_status, reset_reason;
+   int ret = -EAGAIN;
+
+   /*
+* Reset could be triggered when the card in several state:
+*   i)   in bootROM
+*   ii)  after boot1
+*   iii) boot2 running
+*
+* i) & ii) - no status bits will be updated.  If vkboot1
+* runs automatically after reset, it  will update the reason
+* to be unknown reason
+* iii) - reboot reason match + deinit done.
+*/
+   fw_status = vkread32(vk, BAR_0, VK_BAR_FWSTS);
+   /* immediate exit if interface goes down */
+   if (BCM_VK_INTF_IS_DOWN(fw_status)) {
+   dev_err(dev, "PCIe Intf Down!\n");
+   goto reset_exit;
+   }
+
+   reset_reason = (fw_status & VK_FWSTS_RESET_REASON_MASK);
+   if ((reset_reason == VK_FWSTS_RESET_MBOX_DB) ||
+   (reset_reason == VK_FWSTS_RESET_UNKNOWN))
+   ret = 0;
+
+   /*
+* if some of the deinit bits are set, but done
+* bit is not, this is a failure if triggered while boot2 is running
+*/
+   if ((fw_status & VK_FWSTS_DEINIT_TRIGGERED) &&
+   !(fw_status & VK_FWSTS_RESET_DONE))
+   ret = -EAGAIN;
+
+reset_exit:
+   dev_dbg(dev, "FW status = 0x%x ret %d\n", fw_status, ret);
+
+   return ret;
+}
+
 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val)
 {
vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE);
@@ -1010,12 +1057,16 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
 {
u32 i;
u32 value, boot_status;
+   bool is_stdalone, is_boot2;
static const u32 bar0_reg_clr_list[] = { BAR_OS_UPTIME,
 BAR_INTF_VER,
 BAR_CARD_VOLTAGE,
 BAR_CARD_TEMPERATURE,
 BAR_CARD_PWR_AND_THRE };
 
+   /* clean up before pressing the door bell */
+   bcm_vk_drain_msg_on_reset(vk);
+   vkwrite32(vk, 0, BAR_1, VK_BAR1_MSGQ_DEF_RDY);
/* make tag '\0' terminated */
vkwrite32(vk, 0, BAR_1, VK_BAR1_BOOT1_VER_TAG);
 
@@ -1026,6 +10

[PATCH v9 03/13] misc: bcm-vk: add autoload support

2021-01-20 Thread Scott Branden
Add support to load and boot images on card automatically.
The kernel module parameter auto_load can be passed in as false to disable
such support on probe.
As well, nr_scratch_pages can be specified to allocate more or less scratch
memory on init as needed for desired card operation.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Co-developed-by: James Hu 
Signed-off-by: James Hu 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h | 250 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 723 +++
 2 files changed, 973 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 9152785199ab..c4fb61a84e41 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -6,10 +6,187 @@
 #ifndef BCM_VK_H
 #define BCM_VK_H
 
+#include 
 #include 
+#include 
 
 #define DRV_MODULE_NAME"bcm-vk"
 
+/*
+ * Load Image is completed in two stages:
+ *
+ * 1) When the VK device boot-up, M7 CPU runs and executes the BootROM.
+ * The Secure Boot Loader (SBL) as part of the BootROM will run
+ * to open up ITCM for host to push BOOT1 image.
+ * SBL will authenticate the image before jumping to BOOT1 image.
+ *
+ * 2) Because BOOT1 image is a secured image, we also called it the
+ * Secure Boot Image (SBI). At second stage, SBI will initialize DDR
+ * and wait for host to push BOOT2 image to DDR.
+ * SBI will authenticate the image before jumping to BOOT2 image.
+ *
+ */
+/* Location of registers of interest in BAR0 */
+
+/* Request register for Secure Boot Loader (SBL) download */
+#define BAR_CODEPUSH_SBL   0x400
+/* Start of ITCM */
+#define CODEPUSH_BOOT1_ENTRY   0x0040
+#define CODEPUSH_MASK  0xf000
+#define CODEPUSH_BOOTSTART BIT(0)
+
+/* Boot Status register */
+#define BAR_BOOT_STATUS0x404
+
+#define SRAM_OPEN  BIT(16)
+#define DDR_OPEN   BIT(17)
+
+/* Firmware loader progress status definitions */
+#define FW_LOADER_ACK_SEND_MORE_DATA   BIT(18)
+#define FW_LOADER_ACK_IN_PROGRESS  BIT(19)
+#define FW_LOADER_ACK_RCVD_ALL_DATABIT(20)
+
+/* Boot1/2 is running in standalone mode */
+#define BOOT_STDALONE_RUNNING  BIT(21)
+
+/* definitions for boot status register */
+#define BOOT_STATE_MASK(0x & \
+~(FW_LOADER_ACK_SEND_MORE_DATA | \
+  FW_LOADER_ACK_IN_PROGRESS | \
+  BOOT_STDALONE_RUNNING))
+
+#define BOOT_ERR_SHIFT 4
+#define BOOT_ERR_MASK  (0xf << BOOT_ERR_SHIFT)
+#define BOOT_PROG_MASK 0xf
+
+#define BROM_STATUS_NOT_RUN0x2
+#define BROM_NOT_RUN   (SRAM_OPEN | BROM_STATUS_NOT_RUN)
+#define BROM_STATUS_COMPLETE   0x6
+#define BROM_RUNNING   (SRAM_OPEN | BROM_STATUS_COMPLETE)
+#define BOOT1_STATUS_COMPLETE  0x6
+#define BOOT1_RUNNING  (DDR_OPEN | BOOT1_STATUS_COMPLETE)
+#define BOOT2_STATUS_COMPLETE  0x6
+#define BOOT2_RUNNING  (FW_LOADER_ACK_RCVD_ALL_DATA | \
+BOOT2_STATUS_COMPLETE)
+
+/* Boot request for Secure Boot Image (SBI) */
+#define BAR_CODEPUSH_SBI   0x408
+/* 64M mapped to BAR2 */
+#define CODEPUSH_BOOT2_ENTRY   0x6000
+
+#define BAR_CARD_STATUS0x410
+
+#define BAR_BOOT1_STDALONE_PROGRESS0x420
+#define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
+#define BOOT1_STDALONE_PROGRESS_MASK   BOOT1_STDALONE_SUCCESS
+
+#define BAR_METADATA_VERSION   0x440
+#define BAR_OS_UPTIME  0x444
+#define BAR_CHIP_ID0x448
+#define MAJOR_SOC_REV(_chip_id)(((_chip_id) >> 20) & 0xf)
+
+#define BAR_CARD_TEMPERATURE   0x45c
+
+#define BAR_CARD_VOLTAGE   0x460
+
+#define BAR_CARD_ERR_LOG   0x464
+
+#define BAR_CARD_ERR_MEM   0x468
+
+#define BAR_CARD_PWR_AND_THRE  0x46c
+
+#define BAR_CARD_STATIC_INFO   0x470
+
+#define BAR_INTF_VER   0x47c
+#define BAR_INTF_VER_MAJOR_SHIFT   16
+#define BAR_INTF_VER_MASK  0x
+/*
+ * major and minor semantic version numbers supported
+ * Please update as required on interface changes
+ */
+#define SEMANTIC_MAJOR 1
+#define SEMANTIC_MINOR 0
+
+/*
+ * first door bell reg, ie for queue = 0.  Only need the first one, as
+ * we will use the queue number to derive the others
+ */
+#define VK_BAR0_REGSEG_DB_BASE 0x484
+#define VK_BAR0_REGSEG_DB_REG_GAP  8 /*
+  * DB register gap,
+  *

[PATCH v9 13/13] misc: bcm-vk: add ttyVK support

2021-01-20 Thread Scott Branden
Add ttyVK support to driver to allow console access to VK card from host.

Device node will be in the follow form /dev/bcm-vk.x_ttyVKy where:
x is the instance of the VK card
y is the tty device number on the VK card

Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/Makefile |   3 +-
 drivers/misc/bcm-vk/bcm_vk.h |  28 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c |  30 ++-
 drivers/misc/bcm-vk/bcm_vk_tty.c | 333 +++
 4 files changed, 392 insertions(+), 2 deletions(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index 79b4e365c9e6..e4a1486f7209 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -7,5 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
bcm_vk_msg.o \
-   bcm_vk_sg.o
+   bcm_vk_sg.o \
+   bcm_vk_tty.o
 
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index a1d0bf6e694c..3f37c640a814 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -8,12 +8,14 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -84,6 +86,9 @@
 #define CODEPUSH_BOOT2_ENTRY   0x6000
 
 #define BAR_CARD_STATUS0x410
+/* CARD_STATUS definitions */
+#define CARD_STATUS_TTYVK0_READY   BIT(0)
+#define CARD_STATUS_TTYVK1_READY   BIT(1)
 
 #define BAR_BOOT1_STDALONE_PROGRESS0x420
 #define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
@@ -255,6 +260,19 @@ enum pci_barno {
 
 #define BCM_VK_NUM_TTY 2
 
+struct bcm_vk_tty {
+   struct tty_port port;
+   u32 to_offset;  /* bar offset to use */
+   u32 to_size;/* to VK buffer size */
+   u32 wr; /* write offset shadow */
+   u32 from_offset;/* bar offset to use */
+   u32 from_size;  /* from VK buffer size */
+   u32 rd; /* read offset shadow */
+   pid_t pid;
+   bool irq_enabled;
+   bool is_opened; /* tracks tty open/close */
+};
+
 /* VK device max power state, supports 3, full, reduced and low */
 #define MAX_OPP 3
 #define MAX_CARD_INFO_TAG_SIZE 64
@@ -348,6 +366,12 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+   struct tty_driver *tty_drv;
+   struct timer_list serial_timer;
+   struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
+   struct workqueue_struct *tty_wq_thread;
+   struct work_struct tty_wq_work;
+
/* Reference-counting to handle file operations */
struct kref kref;
 
@@ -466,6 +490,7 @@ int bcm_vk_release(struct inode *inode, struct file 
*p_file);
 void bcm_vk_release_data(struct kref *kref);
 irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
+irqreturn_t bcm_vk_tty_irqhandler(int irq, void *dev_id);
 int bcm_vk_msg_init(struct bcm_vk *vk);
 void bcm_vk_msg_remove(struct bcm_vk *vk);
 void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
@@ -476,6 +501,9 @@ int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 
shut_type,
 const pid_t pid, const u32 q_num);
 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
+int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
+void bcm_vk_tty_exit(struct bcm_vk *vk);
+void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
 void bcm_vk_hb_init(struct bcm_vk *vk);
 void bcm_vk_hb_deinit(struct bcm_vk *vk);
 void bcm_vk_handle_notf(struct bcm_vk *vk);
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index cac07419f041..c3d2bba68ef1 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -525,6 +525,7 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
}
}
}
+   bcm_vk_tty_terminate_tty_user(vk);
spin_unlock(>ctx_lock);
 }
 
@@ -1384,6 +1385,20 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
vk->num_irqs++;
 
+   for (i = 0;
+(i < VK_MSIX_TTY_MAX) && (vk->num_irqs < irq);
+i++, vk->num_irqs++) {
+   err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs),
+  bcm_vk_tty_irqhandler,
+  IRQF_SHARED, DRV_MODULE_NAME, vk);
+   if (err) {
+   dev_err(dev, "failed request tty IRQ %d for MSIX %d\n",
+   pdev->irq + vk->num_irqs, vk->num_irqs + 1);
+   goto err_irq;
+   }
+   vk->tty[i].irq_enabled = true;
+   }
+
id = ida_simple_get(_vk_ida, 0, 0, GFP_KERNEL);
   

[PATCH v9 01/13] bcm-vk: add bcm_vk UAPI

2021-01-20 Thread Scott Branden
Add user space api for bcm-vk driver.

Provide ioctl api to load images and issue reset command to card.
FW status registers in PCI BAR space also defined as part
of API so that user space is able to interpret these memory locations
as needed via direct PCIe access.

Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 include/uapi/linux/misc/bcm_vk.h | 84 
 1 file changed, 84 insertions(+)
 create mode 100644 include/uapi/linux/misc/bcm_vk.h

diff --git a/include/uapi/linux/misc/bcm_vk.h b/include/uapi/linux/misc/bcm_vk.h
new file mode 100644
index ..ec28e0bd46a9
--- /dev/null
+++ b/include/uapi/linux/misc/bcm_vk.h
@@ -0,0 +1,84 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#ifndef __UAPI_LINUX_MISC_BCM_VK_H
+#define __UAPI_LINUX_MISC_BCM_VK_H
+
+#include 
+#include 
+
+#define BCM_VK_MAX_FILENAME 64
+
+struct vk_image {
+   __u32 type; /* Type of image */
+#define VK_IMAGE_TYPE_BOOT1 1 /* 1st stage (load to SRAM) */
+#define VK_IMAGE_TYPE_BOOT2 2 /* 2nd stage (load to DDR) */
+   __u8 filename[BCM_VK_MAX_FILENAME]; /* Filename of image */
+};
+
+struct vk_reset {
+   __u32 arg1;
+   __u32 arg2;
+};
+
+#define VK_MAGIC   0x5e
+
+/* Load image to Valkyrie */
+#define VK_IOCTL_LOAD_IMAGE_IOW(VK_MAGIC, 0x2, struct vk_image)
+
+/* Send Reset to Valkyrie */
+#define VK_IOCTL_RESET _IOW(VK_MAGIC, 0x4, struct vk_reset)
+
+/*
+ * Firmware Status accessed directly via BAR space
+ */
+#define VK_BAR_FWSTS   0x41c
+#define VK_BAR_COP_FWSTS   0x428
+/* VK_FWSTS definitions */
+#define VK_FWSTS_RELOCATION_ENTRY  (1UL << 0)
+#define VK_FWSTS_RELOCATION_EXIT   (1UL << 1)
+#define VK_FWSTS_INIT_START(1UL << 2)
+#define VK_FWSTS_ARCH_INIT_DONE(1UL << 3)
+#define VK_FWSTS_PRE_KNL1_INIT_DONE(1UL << 4)
+#define VK_FWSTS_PRE_KNL2_INIT_DONE(1UL << 5)
+#define VK_FWSTS_POST_KNL_INIT_DONE(1UL << 6)
+#define VK_FWSTS_INIT_DONE (1UL << 7)
+#define VK_FWSTS_APP_INIT_START(1UL << 8)
+#define VK_FWSTS_APP_INIT_DONE (1UL << 9)
+#define VK_FWSTS_MASK  0x
+#define VK_FWSTS_READY (VK_FWSTS_INIT_START | \
+VK_FWSTS_ARCH_INIT_DONE | \
+VK_FWSTS_PRE_KNL1_INIT_DONE | \
+VK_FWSTS_PRE_KNL2_INIT_DONE | \
+VK_FWSTS_POST_KNL_INIT_DONE | \
+VK_FWSTS_INIT_DONE | \
+VK_FWSTS_APP_INIT_START | \
+VK_FWSTS_APP_INIT_DONE)
+/* Deinit */
+#define VK_FWSTS_APP_DEINIT_START  (1UL << 23)
+#define VK_FWSTS_APP_DEINIT_DONE   (1UL << 24)
+#define VK_FWSTS_DRV_DEINIT_START  (1UL << 25)
+#define VK_FWSTS_DRV_DEINIT_DONE   (1UL << 26)
+#define VK_FWSTS_RESET_DONE(1UL << 27)
+#define VK_FWSTS_DEINIT_TRIGGERED  (VK_FWSTS_APP_DEINIT_START | \
+VK_FWSTS_APP_DEINIT_DONE  | \
+VK_FWSTS_DRV_DEINIT_START | \
+VK_FWSTS_DRV_DEINIT_DONE)
+/* Last nibble for reboot reason */
+#define VK_FWSTS_RESET_REASON_SHIFT28
+#define VK_FWSTS_RESET_REASON_MASK (0xf << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_SYS_PWRUP   (0x0 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_MBOX_DB (0x1 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_M7_WDOG (0x2 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_TEMP(0x3 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_FLR (0x4 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_HOT (0x5 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_WARM(0x6 << 
VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_COLD(0x7 << 
VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_L1  (0x8 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_L0  (0x9 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_UNKNOWN (0xf << VK_FWSTS_RESET_REASON_SHIFT)
+
+#endif /* __UAPI_LINUX_MISC_BCM_VK_H */
-- 
2.17.1



[PATCH v9 05/13] misc: bcm-vk: add triggers when host panic or reboots to notify card

2021-01-20 Thread Scott Branden
Pass down an interrupt to card in case of panic or reboot so
that card can take appropriate action to perform a clean reset.
Uses kernel notifier block either directly (register on panic list),
or implicitly (add shutdown method for PCI device).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |  2 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 29 -
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 0a366db693c8..f428ad9a0c3d 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -223,6 +223,8 @@ struct bcm_vk {
unsigned long wq_offload[1]; /* various flags on wq requested */
void *tdma_vaddr; /* test dma segment virtual addr */
dma_addr_t tdma_addr; /* test dma segment bus addr */
+
+   struct notifier_block panic_nb;
 };
 
 /* wq offload work items bits definitions */
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 4ecd5b5f80d3..09d99bd36e8a 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -635,6 +635,16 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static int bcm_vk_on_panic(struct notifier_block *nb,
+  unsigned long e, void *p)
+{
+   struct bcm_vk *vk = container_of(nb, struct bcm_vk, panic_nb);
+
+   bcm_to_v_reset_doorbell(vk, VK_BAR0_RESET_DB_HARD);
+
+   return 0;
+}
+
 static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
int err;
@@ -748,6 +758,15 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
/* sync other info */
bcm_vk_sync_card_info(vk);
 
+   /* register for panic notifier */
+   vk->panic_nb.notifier_call = bcm_vk_on_panic;
+   err = atomic_notifier_chain_register(_notifier_list,
+>panic_nb);
+   if (err) {
+   dev_err(dev, "Fail to register panic notifier\n");
+   goto err_destroy_workqueue;
+   }
+
/*
 * lets trigger an auto download.  We don't want to do it serially here
 * because at probing time, it is not supposed to block for a long time.
@@ -756,7 +775,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
if (auto_load) {
if ((boot_status & BOOT_STATE_MASK) == BROM_RUNNING) {
if (bcm_vk_trigger_autoload(vk))
-   goto err_destroy_workqueue;
+   goto err_unregister_panic_notifier;
} else {
dev_err(dev,
"Auto-load skipped - BROM not in proper state 
(0x%x)\n",
@@ -768,6 +787,10 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
 
return 0;
 
+err_unregister_panic_notifier:
+   atomic_notifier_chain_unregister(_notifier_list,
+>panic_nb);
+
 err_destroy_workqueue:
destroy_workqueue(vk->wq_thread);
 
@@ -818,6 +841,10 @@ static void bcm_vk_remove(struct pci_dev *pdev)
bcm_vk_trigger_reset(vk);
usleep_range(BCM_VK_UCODE_BOOT_US, BCM_VK_UCODE_BOOT_MAX_US);
 
+   /* unregister panic notifier */
+   atomic_notifier_chain_unregister(_notifier_list,
+>panic_nb);
+
if (vk->tdma_vaddr)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
  vk->tdma_vaddr, vk->tdma_addr);
-- 
2.17.1



[PATCH v9 06/13] misc: bcm-vk: add open/release

2021-01-20 Thread Scott Branden
Add open/release to replace private data with context for other methods
to use.  Reason for the context is because it is allowed for multiple
sessions to open sysfs.  For each file open, when upper layer queries the
response, only those that are tied to a specified open should be returned.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/Makefile |   4 +-
 drivers/misc/bcm-vk/bcm_vk.h |  15 
 drivers/misc/bcm-vk/bcm_vk_dev.c |  23 ++
 drivers/misc/bcm-vk/bcm_vk_msg.c | 127 +++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  31 
 5 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index f8a7ac4c242f..a2ae79858409 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -5,4 +5,6 @@
 
 obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
-   bcm_vk_dev.o
+   bcm_vk_dev.o \
+   bcm_vk_msg.o
+
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index f428ad9a0c3d..5f0fcfdaf265 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -7,9 +7,14 @@
 #define BCM_VK_H
 
 #include 
+#include 
 #include 
+#include 
 #include 
 #include 
+#include 
+
+#include "bcm_vk_msg.h"
 
 #define DRV_MODULE_NAME"bcm-vk"
 
@@ -218,6 +223,13 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+   /* Reference-counting to handle file operations */
+   struct kref kref;
+
+   spinlock_t ctx_lock; /* Spinlock for component context */
+   struct bcm_vk_ctx ctx[VK_CMPT_CTX_MAX];
+   struct bcm_vk_ht_entry pid_ht[VK_PID_HT_SZ];
+
struct workqueue_struct *wq_thread;
struct work_struct wq_work; /* work queue for deferred job */
unsigned long wq_offload[1]; /* various flags on wq requested */
@@ -278,6 +290,9 @@ static inline bool bcm_vk_msgq_marker_valid(struct bcm_vk 
*vk)
return (rdy_marker == VK_BAR1_MSGQ_RDY_MARKER);
 }
 
+int bcm_vk_open(struct inode *inode, struct file *p_file);
+int bcm_vk_release(struct inode *inode, struct file *p_file);
+void bcm_vk_release_data(struct kref *kref);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
 
 #endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 09d99bd36e8a..79fffb1e6f84 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -8,6 +8,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -635,6 +636,12 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static const struct file_operations bcm_vk_fops = {
+   .owner = THIS_MODULE,
+   .open = bcm_vk_open,
+   .release = bcm_vk_release,
+};
+
 static int bcm_vk_on_panic(struct notifier_block *nb,
   unsigned long e, void *p)
 {
@@ -657,10 +664,13 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
struct miscdevice *misc_device;
u32 boot_status;
 
+   /* allocate vk structure which is tied to kref for freeing */
vk = kzalloc(sizeof(*vk), GFP_KERNEL);
if (!vk)
return -ENOMEM;
 
+   kref_init(>kref);
+
err = pci_enable_device(pdev);
if (err) {
dev_err(dev, "Cannot enable PCI device\n");
@@ -738,6 +748,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
err = -ENOMEM;
goto err_ida_remove;
}
+   misc_device->fops = _vk_fops,
 
err = misc_register(misc_device);
if (err) {
@@ -826,6 +837,16 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
return err;
 }
 
+void bcm_vk_release_data(struct kref *kref)
+{
+   struct bcm_vk *vk = container_of(kref, struct bcm_vk, kref);
+   struct pci_dev *pdev = vk->pdev;
+
+   dev_dbg(>dev, "BCM-VK:%d release data 0x%p\n", vk->devid, vk);
+   pci_dev_put(pdev);
+   kfree(vk);
+}
+
 static void bcm_vk_remove(struct pci_dev *pdev)
 {
int i;
@@ -869,6 +890,8 @@ static void bcm_vk_remove(struct pci_dev *pdev)
pci_release_regions(pdev);
pci_free_irq_vectors(pdev);
pci_disable_device(pdev);
+
+   kref_put(>kref, bcm_vk_release_data);
 }
 
 static void bcm_vk_shutdown(struct pci_dev *pdev)
diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c b/drivers/misc/bcm-vk/bcm_vk_msg.c
new file mode 100644
index ..2d9a6b4e5f61
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk_msg.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#include "bcm_vk.h&quo

[PATCH v9 08/13] misc: bcm-vk: add get_card_info, peerlog_info, and proc_mon_info

2021-01-20 Thread Scott Branden
Add support to get card_info (details about card),
peerlog_info (to get details of peerlog on card),
and proc_mon_info (process monitoring on card).

This info is used for collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |  60 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 105 +++
 2 files changed, 165 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 726aab71bb6b..50f2a0cd6e13 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -205,6 +205,21 @@ enum pci_barno {
 
 #define BCM_VK_NUM_TTY 2
 
+/* VK device max power state, supports 3, full, reduced and low */
+#define MAX_OPP 3
+#define MAX_CARD_INFO_TAG_SIZE 64
+
+struct bcm_vk_card_info {
+   u32 version;
+   char os_tag[MAX_CARD_INFO_TAG_SIZE];
+   char cmpt_tag[MAX_CARD_INFO_TAG_SIZE];
+   u32 cpu_freq_mhz;
+   u32 cpu_scale[MAX_OPP];
+   u32 ddr_freq_mhz;
+   u32 ddr_size_MB;
+   u32 video_core_freq_mhz;
+};
+
 /* DAUTH related info */
 struct bcm_vk_dauth_key {
char store[VK_BAR1_DAUTH_STORE_SIZE];
@@ -215,10 +230,49 @@ struct bcm_vk_dauth_info {
struct bcm_vk_dauth_key keys[VK_BAR1_DAUTH_MAX];
 };
 
+/*
+ * Control structure of logging messages from the card.  This
+ * buffer is for logmsg that comes from vk
+ */
+struct bcm_vk_peer_log {
+   u32 rd_idx;
+   u32 wr_idx;
+   u32 buf_size;
+   u32 mask;
+   char data[0];
+};
+
+/* max buf size allowed */
+#define BCM_VK_PEER_LOG_BUF_MAX SZ_16K
+/* max size per line of peer log */
+#define BCM_VK_PEER_LOG_LINE_MAX  256
+
+/*
+ * single entry for processing type + utilization
+ */
+#define BCM_VK_PROC_TYPE_TAG_LEN 8
+struct bcm_vk_proc_mon_entry_t {
+   char tag[BCM_VK_PROC_TYPE_TAG_LEN];
+   u32 used;
+   u32 max; /**< max capacity */
+};
+
+/**
+ * Structure for run time utilization
+ */
+#define BCM_VK_PROC_MON_MAX 8 /* max entries supported */
+struct bcm_vk_proc_mon_info {
+   u32 num; /**< no of entries */
+   u32 entry_size; /**< per entry size */
+   struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
+};
+
 struct bcm_vk {
struct pci_dev *pdev;
void __iomem *bar[MAX_BAR];
 
+   struct bcm_vk_card_info card_info;
+   struct bcm_vk_proc_mon_info proc_mon_info;
struct bcm_vk_dauth_info dauth_info;
 
/* mutex to protect the ioctls */
@@ -240,6 +294,12 @@ struct bcm_vk {
dma_addr_t tdma_addr; /* test dma segment bus addr */
 
struct notifier_block panic_nb;
+
+   /* offset of the peer log control in BAR2 */
+   u32 peerlog_off;
+   struct bcm_vk_peer_log peerlog_info; /* record of peer log info */
+   /* offset of processing monitoring info in BAR2 */
+   u32 proc_mon_off;
 };
 
 /* wq offload work items bits definitions */
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 203a1cf2bae3..a63208513c64 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -172,6 +172,104 @@ static inline int bcm_vk_wait(struct bcm_vk *vk, enum 
pci_barno bar,
return 0;
 }
 
+static void bcm_vk_get_card_info(struct bcm_vk *vk)
+{
+   struct device *dev = >pdev->dev;
+   u32 offset;
+   int i;
+   u8 *dst;
+   struct bcm_vk_card_info *info = >card_info;
+
+   /* first read the offset from spare register */
+   offset = vkread32(vk, BAR_0, BAR_CARD_STATIC_INFO);
+   offset &= (pci_resource_len(vk->pdev, BAR_2 * 2) - 1);
+
+   /* based on the offset, read info to internal card info structure */
+   dst = (u8 *)info;
+   for (i = 0; i < sizeof(*info); i++)
+   *dst++ = vkread8(vk, BAR_2, offset++);
+
+#define CARD_INFO_LOG_FMT "version   : %x\n" \
+ "os_tag: %s\n" \
+ "cmpt_tag  : %s\n" \
+ "cpu_freq  : %d MHz\n" \
+ "cpu_scale : %d full, %d lowest\n" \
+ "ddr_freq  : %d MHz\n" \
+ "ddr_size  : %d MB\n" \
+ "video_freq: %d MHz\n"
+   dev_dbg(dev, CARD_INFO_LOG_FMT, info->version, info->os_tag,
+   info->cmpt_tag, info->cpu_freq_mhz, info->cpu_scale[0],
+   info->cpu_scale[MAX_OPP - 1], info->ddr_freq_mhz,
+   info->ddr_size_MB, info->video_core_freq_mhz);
+
+   /*
+* get the peer log pointer, only need the offset, and get record
+* of the log buffer information which would be used for checking
+* before dump, in case the BAR2 memory 

[PATCH v9 00/13] Add Broadcom VK driver

2021-01-20 Thread Scott Branden
This patch series drops previous patches in [1]
that were incorporated by Kees Cook into patch series
"Introduce partial kernel_read_file() support" [2].

Remaining patches are contained in this series to add Broadcom VK driver.
(which depends on request_firmware_into_buf API addition which has
now been accepted into the upstream kernel as of v5.10-rc1).

[1] 
https://lore.kernel.org/lkml/20200706232309.12010-1-scott.bran...@broadcom.com/
[2] https://lore.kernel.org/lkml/20201002173828.2099543-1-keesc...@chromium.org/

Changes from v8:
 - add ack's by Olof Johansson
Changes from v7:
 - add more information in Kconfig help description
Changes from v6:
 - drop QSTATS patch as it needs to be reviewed if trace_printk makes sense
 - add wdog and IPC interface alerts
 - add boundary check to msgq and peerlog
 - clear additional registers on reset
Changes from v5:
 - dropped sysfs patch from series for now as rework to use hwmon
 - tty patch still at end of series to drop if another solution available
 - updated cover letter commit to point to Kees' latest patch submission in [2]
 - specified --base with Kees' patches applied (kernel branches don't have 
these yet)
 - removed trivial comment
 - moved location of const to before the struct in two declarations
 - changed dev_info to dev_warn and only print when irq don't match expected
 - changed dev_info to dev_dbg when printing debug QSTATS
 - removed unnecessary %p print
Changes from v4:
 - fixed memory leak in probe function on failure
 - changed -1 to -EBUSY in bcm_vk_tty return code
 - move bcm_vk_tty patch to end of patch series so it
   can be dropped from current patch series if needed
   and rearchitected if needed.
Changes from v3:
 - split driver into more incremental commits for acceptance/review
 - lowered some dev_info to dev_dbg
 - remove ANSI stdint types and replace with linux u8, etc types
 - changed an EIO return to EPFNOSUPPORT
 - move vk_msg_cmd internal to driver to not expose to UAPI at this time
Changes from v2:
 - open code BIT macro in uapi header
 - A0/B0 boot improvements
Changes from v1:
 - declare bcm_vk_intf_ver_chk as static


Scott Branden (13):
  bcm-vk: add bcm_vk UAPI
  misc: bcm-vk: add Broadcom VK driver
  misc: bcm-vk: add autoload support
  misc: bcm-vk: add misc device to Broadcom VK driver
  misc: bcm-vk: add triggers when host panic or reboots to notify card
  misc: bcm-vk: add open/release
  misc: bcm-vk: add ioctl load_image
  misc: bcm-vk: add get_card_info, peerlog_info, and proc_mon_info
  misc: bcm-vk: add VK messaging support
  misc: bcm-vk: reset_pid support
  misc: bcm-vk: add mmap function for exposing BAR2
  MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver
  misc: bcm-vk: add ttyVK support

 MAINTAINERS  |7 +
 drivers/misc/Kconfig |1 +
 drivers/misc/Makefile|1 +
 drivers/misc/bcm-vk/Kconfig  |   17 +
 drivers/misc/bcm-vk/Makefile |   12 +
 drivers/misc/bcm-vk/bcm_vk.h |  513 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 1651 ++
 drivers/misc/bcm-vk/bcm_vk_msg.c | 1350 
 drivers/misc/bcm-vk/bcm_vk_msg.h |  163 +++
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  275 +
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   61 ++
 drivers/misc/bcm-vk/bcm_vk_tty.c |  333 ++
 include/uapi/linux/misc/bcm_vk.h |   84 ++
 13 files changed, 4468 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c
 create mode 100644 include/uapi/linux/misc/bcm_vk.h

-- 
2.17.1



[PATCH v9 04/13] misc: bcm-vk: add misc device to Broadcom VK driver

2021-01-20 Thread Scott Branden
Add misc device base support to create and remove devnode.
Additional misc functions for open/read/write/release/ioctl/sysfs, etc
will be added in follow on commits to allow for individual review.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |  2 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 36 +++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index c4fb61a84e41..0a366db693c8 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -7,6 +7,7 @@
 #define BCM_VK_H
 
 #include 
+#include 
 #include 
 #include 
 
@@ -214,6 +215,7 @@ struct bcm_vk {
 
struct bcm_vk_dauth_info dauth_info;
 
+   struct miscdevice miscdev;
int devid; /* dev id allocated */
 
struct workqueue_struct *wq_thread;
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index adc3103c7012..4ecd5b5f80d3 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -7,6 +7,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -643,6 +644,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
char name[20];
struct bcm_vk *vk;
struct device *dev = >dev;
+   struct miscdevice *misc_device;
u32 boot_status;
 
vk = kzalloc(sizeof(*vk), GFP_KERNEL);
@@ -719,6 +721,19 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
 
vk->devid = id;
snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
+   misc_device = >miscdev;
+   misc_device->minor = MISC_DYNAMIC_MINOR;
+   misc_device->name = kstrdup(name, GFP_KERNEL);
+   if (!misc_device->name) {
+   err = -ENOMEM;
+   goto err_ida_remove;
+   }
+
+   err = misc_register(misc_device);
+   if (err) {
+   dev_err(dev, "failed to register device\n");
+   goto err_kfree_name;
+   }
 
INIT_WORK(>wq_work, bcm_vk_wq_handler);
 
@@ -727,7 +742,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
if (!vk->wq_thread) {
dev_err(dev, "Fail to create workqueue thread\n");
err = -ENOMEM;
-   goto err_ida_remove;
+   goto err_misc_deregister;
}
 
/* sync other info */
@@ -749,11 +764,20 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
}
 
+   dev_dbg(dev, "BCM-VK:%u created\n", id);
+
return 0;
 
 err_destroy_workqueue:
destroy_workqueue(vk->wq_thread);
 
+err_misc_deregister:
+   misc_deregister(misc_device);
+
+err_kfree_name:
+   kfree(misc_device->name);
+   misc_device->name = NULL;
+
 err_ida_remove:
ida_simple_remove(_vk_ida, id);
 
@@ -783,6 +807,7 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 {
int i;
struct bcm_vk *vk = pci_get_drvdata(pdev);
+   struct miscdevice *misc_device = >miscdev;
 
/*
 * Trigger a reset to card and wait enough time for UCODE to rerun,
@@ -797,6 +822,13 @@ static void bcm_vk_remove(struct pci_dev *pdev)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
  vk->tdma_vaddr, vk->tdma_addr);
 
+   /* remove if name is set which means misc dev registered */
+   if (misc_device->name) {
+   misc_deregister(misc_device);
+   kfree(misc_device->name);
+   ida_simple_remove(_vk_ida, vk->devid);
+   }
+
cancel_work_sync(>wq_work);
destroy_workqueue(vk->wq_thread);
 
@@ -805,6 +837,8 @@ static void bcm_vk_remove(struct pci_dev *pdev)
pci_iounmap(pdev, vk->bar[i]);
}
 
+   dev_dbg(>dev, "BCM-VK:%d released\n", vk->devid);
+
pci_release_regions(pdev);
pci_free_irq_vectors(pdev);
pci_disable_device(pdev);
-- 
2.17.1



[PATCH v9 11/13] misc: bcm-vk: add mmap function for exposing BAR2

2021-01-20 Thread Scott Branden
Add mmap function that allows host application to open up BAR2 memory
for remote spooling out messages from the VK logger.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk_dev.c | 24 
 1 file changed, 24 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index e572a7b18fab..cac07419f041 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -1199,6 +1199,29 @@ static long bcm_vk_reset(struct bcm_vk *vk, struct 
vk_reset __user *arg)
return ret;
 }
 
+static int bcm_vk_mmap(struct file *file, struct vm_area_struct *vma)
+{
+   struct bcm_vk_ctx *ctx = file->private_data;
+   struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
+   unsigned long pg_size;
+
+   /* only BAR2 is mmap possible, which is bar num 4 due to 64bit */
+#define VK_MMAPABLE_BAR 4
+
+   pg_size = ((pci_resource_len(vk->pdev, VK_MMAPABLE_BAR) - 1)
+   >> PAGE_SHIFT) + 1;
+   if (vma->vm_pgoff + vma_pages(vma) > pg_size)
+   return -EINVAL;
+
+   vma->vm_pgoff += (pci_resource_start(vk->pdev, VK_MMAPABLE_BAR)
+ >> PAGE_SHIFT);
+   vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+   return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+ vma->vm_end - vma->vm_start,
+ vma->vm_page_prot);
+}
+
 static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
 {
long ret = -EINVAL;
@@ -1237,6 +1260,7 @@ static const struct file_operations bcm_vk_fops = {
.write = bcm_vk_write,
.poll = bcm_vk_poll,
.release = bcm_vk_release,
+   .mmap = bcm_vk_mmap,
.unlocked_ioctl = bcm_vk_ioctl,
 };
 
-- 
2.17.1



[PATCH v9 09/13] misc: bcm-vk: add VK messaging support

2021-01-20 Thread Scott Branden
Add message support in order to be able to communicate
to VK card via message queues.

This info is used for debug purposes via collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/Makefile |3 +-
 drivers/misc/bcm-vk/bcm_vk.h |  123 
 drivers/misc/bcm-vk/bcm_vk_dev.c |  309 +++-
 drivers/misc/bcm-vk/bcm_vk_msg.c | 1187 ++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  132 
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  275 +++
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   61 ++
 7 files changed, 2087 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index a2ae79858409..79b4e365c9e6 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -6,5 +6,6 @@
 obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
-   bcm_vk_msg.o
+   bcm_vk_msg.o \
+   bcm_vk_sg.o
 
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 50f2a0cd6e13..d847a512d0ed 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -6,11 +6,13 @@
 #ifndef BCM_VK_H
 #define BCM_VK_H
 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -93,14 +95,53 @@
 #define MAJOR_SOC_REV(_chip_id)(((_chip_id) >> 20) & 0xf)
 
 #define BAR_CARD_TEMPERATURE   0x45c
+/* defines for all temperature sensor */
+#define BCM_VK_TEMP_FIELD_MASK 0xff
+#define BCM_VK_CPU_TEMP_SHIFT  0
+#define BCM_VK_DDR0_TEMP_SHIFT 8
+#define BCM_VK_DDR1_TEMP_SHIFT 16
 
 #define BAR_CARD_VOLTAGE   0x460
+/* defines for voltage rail conversion */
+#define BCM_VK_VOLT_RAIL_MASK  0x
+#define BCM_VK_3P3_VOLT_REG_SHIFT  16
 
 #define BAR_CARD_ERR_LOG   0x464
+/* Error log register bit definition - register for error alerts */
+#define ERR_LOG_UECC   BIT(0)
+#define ERR_LOG_SSIM_BUSY  BIT(1)
+#define ERR_LOG_AFBC_BUSY  BIT(2)
+#define ERR_LOG_HIGH_TEMP_ERR  BIT(3)
+#define ERR_LOG_WDOG_TIMEOUT   BIT(4)
+#define ERR_LOG_SYS_FAULT  BIT(5)
+#define ERR_LOG_RAMDUMPBIT(6)
+#define ERR_LOG_COP_WDOG_TIMEOUT   BIT(7)
+/* warnings */
+#define ERR_LOG_MEM_ALLOC_FAIL BIT(8)
+#define ERR_LOG_LOW_TEMP_WARN  BIT(9)
+#define ERR_LOG_ECCBIT(10)
+#define ERR_LOG_IPC_DWNBIT(11)
+
+/* Alert bit definitions detectd on host */
+#define ERR_LOG_HOST_INTF_V_FAIL   BIT(13)
+#define ERR_LOG_HOST_HB_FAIL   BIT(14)
+#define ERR_LOG_HOST_PCIE_DWN  BIT(15)
 
 #define BAR_CARD_ERR_MEM   0x468
+/* defines for mem err, all fields have same width */
+#define BCM_VK_MEM_ERR_FIELD_MASK  0xff
+#define BCM_VK_ECC_MEM_ERR_SHIFT   0
+#define BCM_VK_UECC_MEM_ERR_SHIFT  8
+/* threshold of event occurrence and logs start to come out */
+#define BCM_VK_ECC_THRESHOLD   10
+#define BCM_VK_UECC_THRESHOLD  1
 
 #define BAR_CARD_PWR_AND_THRE  0x46c
+/* defines for power and temp threshold, all fields have same width */
+#define BCM_VK_PWR_AND_THRE_FIELD_MASK 0xff
+#define BCM_VK_LOW_TEMP_THRE_SHIFT 0
+#define BCM_VK_HIGH_TEMP_THRE_SHIFT8
+#define BCM_VK_PWR_STATE_SHIFT 16
 
 #define BAR_CARD_STATIC_INFO   0x470
 
@@ -143,6 +184,11 @@
 #define BAR_FIRMWARE_TAG_SIZE  50
 #define FIRMWARE_STATUS_PRE_INIT_DONE  0x1f
 
+/* VK MSG_ID defines */
+#define VK_MSG_ID_BITMAP_SIZE  4096
+#define VK_MSG_ID_BITMAP_MASK  (VK_MSG_ID_BITMAP_SIZE - 1)
+#define VK_MSG_ID_OVERFLOW 0x
+
 /*
  * BAR1
  */
@@ -197,6 +243,10 @@
 /* VK device supports a maximum of 3 bars */
 #define MAX_BAR3
 
+/* default number of msg blk for inband SGL */
+#define BCM_VK_DEF_IB_SGL_BLK_LEN   16
+#define BCM_VK_IB_SGL_BLK_MAX   24
+
 enum pci_barno {
BAR_0 = 0,
BAR_1,
@@ -267,9 +317,27 @@ struct bcm_vk_proc_mon_info {
struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
 };
 
+struct bcm_vk_hb_ctrl {
+   struct timer_list timer;
+   u32 last_uptime;
+   u32 lost_cnt;
+};
+
+struct bcm_vk_alert {
+   u16 flags;
+   u16 notfs;
+};
+
+/* some alert counters that the driver will keep track */
+struct bcm_vk_alert_cnts {
+   u16 ecc;
+   u16 uecc;
+};
+
 struct bcm_vk {
struct pci_dev *pdev;
void __iomem *bar[MAX_BAR];
+   int num_irqs;
 
struct bcm_vk_card_info card_info;
struct bcm_vk_proc_mon_info proc_mon_info;
@@ -283,9 +351,17 @@ struct bcm_vk {
/*

[PATCH v9 07/13] misc: bcm-vk: add ioctl load_image

2021-01-20 Thread Scott Branden
Add ioctl support to issue load_image operation to VK card.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Co-developed-by: James Hu 
Signed-off-by: James Hu 
Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/bcm-vk/bcm_vk.h |  3 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 95 
 2 files changed, 98 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 5f0fcfdaf265..726aab71bb6b 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include "bcm_vk_msg.h"
@@ -220,6 +221,8 @@ struct bcm_vk {
 
struct bcm_vk_dauth_info dauth_info;
 
+   /* mutex to protect the ioctls */
+   struct mutex mutex;
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 79fffb1e6f84..203a1cf2bae3 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -580,6 +581,71 @@ static void bcm_vk_wq_handler(struct work_struct *work)
}
 }
 
+static long bcm_vk_load_image(struct bcm_vk *vk,
+ const struct vk_image __user *arg)
+{
+   struct device *dev = >pdev->dev;
+   const char *image_name;
+   struct vk_image image;
+   u32 next_loadable;
+   enum soc_idx idx;
+   int image_idx;
+   int ret = -EPERM;
+
+   if (copy_from_user(, arg, sizeof(image)))
+   return -EACCES;
+
+   if ((image.type != VK_IMAGE_TYPE_BOOT1) &&
+   (image.type != VK_IMAGE_TYPE_BOOT2)) {
+   dev_err(dev, "invalid image.type %u\n", image.type);
+   return ret;
+   }
+
+   next_loadable = bcm_vk_next_boot_image(vk);
+   if (next_loadable != image.type) {
+   dev_err(dev, "Next expected image %u, Loading %u\n",
+   next_loadable, image.type);
+   return ret;
+   }
+
+   /*
+* if something is pending download already.  This could only happen
+* for now when the driver is being loaded, or if someone has issued
+* another download command in another shell.
+*/
+   if (test_and_set_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload) != 0) {
+   dev_err(dev, "Download operation already pending.\n");
+   return ret;
+   }
+
+   image_name = image.filename;
+   if (image_name[0] == '\0') {
+   /* Use default image name if NULL */
+   idx = get_soc_idx(vk);
+   if (idx == VK_IDX_INVALID)
+   goto err_idx;
+
+   /* Image idx starts with boot1 */
+   image_idx = image.type - VK_IMAGE_TYPE_BOOT1;
+   image_name = get_load_fw_name(vk, _tab[idx][image_idx]);
+   if (!image_name) {
+   dev_err(dev, "No suitable image found for type %d",
+   image.type);
+   ret = -ENOENT;
+   goto err_idx;
+   }
+   } else {
+   /* Ensure filename is NULL terminated */
+   image.filename[sizeof(image.filename) - 1] = '\0';
+   }
+   ret = bcm_vk_load_image_by_type(vk, image.type, image_name);
+   dev_info(dev, "Load %s, ret %d\n", image_name, ret);
+err_idx:
+   clear_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload);
+
+   return ret;
+}
+
 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val)
 {
vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE);
@@ -636,10 +702,38 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
+{
+   long ret = -EINVAL;
+   struct bcm_vk_ctx *ctx = file->private_data;
+   struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
+   void __user *argp = (void __user *)arg;
+
+   dev_dbg(>pdev->dev,
+   "ioctl, cmd=0x%02x, arg=0x%02lx\n",
+   cmd, arg);
+
+   mutex_lock(>mutex);
+
+   switch (cmd) {
+   case VK_IOCTL_LOAD_IMAGE:
+   ret = bcm_vk_load_image(vk, argp);
+   break;
+
+   default:
+   break;
+   }
+
+   mutex_unlock(>mutex);
+
+   return ret;
+}
+
 static const struct file_operations bcm_vk_fops = {
.owner = THIS_MODULE,
.open = bcm_vk_open,
.release = bcm_vk_release,
+   .unlocked_ioctl = bcm_vk_ioctl,
 };
 
 static int bcm_vk_on_panic(struct notifier_block *nb,
@@ -670,6 +764,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id

[PATCH v9 12/13] MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver

2021-01-20 Thread Scott Branden
Add maintainer entry for new Broadcom VK Driver

Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 MAINTAINERS | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 00836f6452f0..76f3817cf22c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3744,6 +3744,13 @@ L:   net...@vger.kernel.org
 S: Supported
 F: drivers/net/ethernet/broadcom/tg3.*
 
+BROADCOM VK DRIVER
+M: Scott Branden 
+L: bcm-kernel-feedback-l...@broadcom.com
+S: Supported
+F: drivers/misc/bcm-vk/
+F: include/uapi/linux/misc/bcm_vk.h
+
 BROCADE BFA FC SCSI DRIVER
 M: Anil Gurumurthy 
 M: Sudarsana Kalluru 
-- 
2.17.1



[PATCH v9 02/13] misc: bcm-vk: add Broadcom VK driver

2021-01-20 Thread Scott Branden
Add initial version of Broadcom VK driver to enumerate PCI device IDs
of Valkyrie and Viper device IDs.

VK based cards provide real-time high performance, high throughput,
low latency offload compute engine operations.
They are used for multiple parallel offload tasks as:
audio, video and image processing and crypto operations.

Further commits add additional features to driver beyond probe/remove.

Signed-off-by: Scott Branden 
Acked-by: Olof Johansson 
---
 drivers/misc/Kconfig |   1 +
 drivers/misc/Makefile|   1 +
 drivers/misc/bcm-vk/Kconfig  |  17 
 drivers/misc/bcm-vk/Makefile |   8 ++
 drivers/misc/bcm-vk/bcm_vk.h |  29 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 141 +++
 6 files changed, 197 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index fafa8b0d8099..591903773a6d 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -478,6 +478,7 @@ source "drivers/misc/genwqe/Kconfig"
 source "drivers/misc/echo/Kconfig"
 source "drivers/misc/cxl/Kconfig"
 source "drivers/misc/ocxl/Kconfig"
+source "drivers/misc/bcm-vk/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
 source "drivers/misc/habanalabs/Kconfig"
 source "drivers/misc/uacce/Kconfig"
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index d23231e73330..54f2fe2d9448 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_ECHO)+= echo/
 obj-$(CONFIG_CXL_BASE) += cxl/
 obj-$(CONFIG_PCI_ENDPOINT_TEST)+= pci_endpoint_test.o
 obj-$(CONFIG_OCXL) += ocxl/
+obj-$(CONFIG_BCM_VK)   += bcm-vk/
 obj-y  += cardreader/
 obj-$(CONFIG_PVPANIC)  += pvpanic.o
 obj-$(CONFIG_HABANA_AI)+= habanalabs/
diff --git a/drivers/misc/bcm-vk/Kconfig b/drivers/misc/bcm-vk/Kconfig
new file mode 100644
index ..052f6f28b540
--- /dev/null
+++ b/drivers/misc/bcm-vk/Kconfig
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Broadcom VK device
+#
+config BCM_VK
+   tristate "Support for Broadcom VK Accelerators"
+   depends on PCI_MSI
+   help
+ Select this option to enable support for Broadcom
+ VK Accelerators.  VK is used for performing
+ multiple specific offload processing tasks in parallel.
+ Such offload tasks assist in such operations as video
+ transcoding, compression, and crypto tasks.
+ This driver enables userspace programs to access these
+ accelerators via /dev/bcm-vk.N devices.
+
+ If unsure, say N.
diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
new file mode 100644
index ..f8a7ac4c242f
--- /dev/null
+++ b/drivers/misc/bcm-vk/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Broadcom VK driver
+#
+
+obj-$(CONFIG_BCM_VK) += bcm_vk.o
+bcm_vk-objs := \
+   bcm_vk_dev.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
new file mode 100644
index ..9152785199ab
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#ifndef BCM_VK_H
+#define BCM_VK_H
+
+#include 
+
+#define DRV_MODULE_NAME"bcm-vk"
+
+/* VK device supports a maximum of 3 bars */
+#define MAX_BAR3
+
+enum pci_barno {
+   BAR_0 = 0,
+   BAR_1,
+   BAR_2
+};
+
+#define BCM_VK_NUM_TTY 2
+
+struct bcm_vk {
+   struct pci_dev *pdev;
+   void __iomem *bar[MAX_BAR];
+};
+
+#endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
new file mode 100644
index ..14afe2477b97
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include "bcm_vk.h"
+
+#define PCI_DEVICE_ID_VALKYRIE 0x5e87
+#define PCI_DEVICE_ID_VIPER0x5e88
+
+/* MSIX usages */
+#define VK_MSIX_MSGQ_MAX   3
+#define VK_MSIX_NOTF_MAX   1
+#define VK_MSIX_TTY_MAXBCM_VK_NUM_TTY
+#define VK_MSIX_IRQ_MAX(VK_MSIX_MSGQ_MAX + 
VK_MSIX_NOTF_MAX + \
+VK_MSIX_TTY_MAX)
+#define VK_MSIX_IRQ_MIN_REQ (VK_MSIX_MSGQ_MAX + VK_MSIX_NOTF_MAX)
+
+/* Number of bits set in DMA mask*/
+#define BCM_VK_DMA_BITS64
+
+static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{
+   int err;
+   int i;
+   int irq

Re: [PATCH v8 00/13] Add Broadcom VK driver

2021-01-19 Thread Scott Branden
Hi Greg,

On 2021-01-18 3:06 p.m., Olof Johansson wrote:
> On Sun, Jan 17, 2021 at 11:17 PM Greg Kroah-Hartman
>  wrote:
>> On Sun, Jan 17, 2021 at 10:47:59AM -0800, Olof Johansson wrote:
>>> Hi,
>>>
>>> On Mon, Nov 30, 2020 at 10:42 AM Scott Branden
>>>  wrote:
>>>> This patch series drops previous patches in [1]
>>>> that were incorporated by Kees Cook into patch series
>>>> "Introduce partial kernel_read_file() support" [2].
>>>>
>>>> Remaining patches are contained in this series to add Broadcom VK driver.
>>>> (which depends on request_firmware_into_buf API addition which has
>>>> now been accepted into the upstream kernel as of v5.10-rc1).
>>>>
>>>> [1] 
>>>> https://lore.kernel.org/lkml/20200706232309.12010-1-scott.bran...@broadcom.com/
>>>> [2] 
>>>> https://lore.kernel.org/lkml/20201002173828.2099543-1-keesc...@chromium.org/
>>>
>>> I've been following this series for some time, and I think the code is
>>> ready to go in.
>>>
>>> Greg, mind queuing this up in the misc tree?
>> I will need a new version, this is long gone from my queue.
> I'll let Scott repost then (with acks applied etc)
I can send another patch version with Olof's acks applied to each patch.
Please let me know if that is what you are looking for?
>
>> And hopefully the tty layer abuse is gone... :)
> There's a simple tty driver as the final patch in the series, but it's
> pretty straightforward.
>
> If you've still got concerns with it, the rest of the series should
> stand on its own and should be mergeable without that piece.
Yes, I placed the patch at the end of the series so it can be dropped
if that is what is required to accept the rest of the patches.

There has been no viable solution suggested for replacing this functionality.
We need a tty-like interface that works via access to the circular buffers
in PCIe BAR space and interrupts.

The vk tty devices are a direct replacement to attaching serial cables.
In real production environment it is not possible to attach such cables.

I can work on an alternative tty solution and send such patch later
but I don't know what is going to prevent the tty "abuse".
>
>
> -Olof
Regards,
Scott


Re: [PATCH v2 2/2] hwrng: iproc-rng200: Move enable/disable in separate function

2020-12-18 Thread Scott Branden



On 2020-12-18 2:57 a.m., matthias@kernel.org wrote:
> From: Matthias Brugger 
>
> We are calling the same code for enable and disable the block in various
> parts of the driver. Put that code into a new function to reduce code
> duplication.
>
> Signed-off-by: Matthias Brugger 
> Acked-by: Florian Fainelli 
Acked-by: Scott Branden 
>
> ---
>
> Changes in v2:
> - rename function to iproc_rng200_enable_set()
> - use u32 value instead of uint32_t
>
>  drivers/char/hw_random/iproc-rng200.c | 35 ---
>  1 file changed, 16 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/char/hw_random/iproc-rng200.c 
> b/drivers/char/hw_random/iproc-rng200.c
> index 70cd818a0f31..a43743887db1 100644
> --- a/drivers/char/hw_random/iproc-rng200.c
> +++ b/drivers/char/hw_random/iproc-rng200.c
> @@ -53,14 +53,24 @@ struct iproc_rng200_dev {
>  
>  #define to_rng_priv(rng) container_of(rng, struct iproc_rng200_dev, rng)
>  
> -static void iproc_rng200_restart(void __iomem *rng_base)
> +static void iproc_rng200_enable_set(void __iomem *rng_base, bool enable)
>  {
> - uint32_t val;
> + u32 val;
>  
> - /* Disable RBG */
>   val = ioread32(rng_base + RNG_CTRL_OFFSET);
>   val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> +
> + if (enable)
> + val |= RNG_CTRL_RNG_RBGEN_ENABLE;
> +
>   iowrite32(val, rng_base + RNG_CTRL_OFFSET);
> +}
> +
> +static void iproc_rng200_restart(void __iomem *rng_base)
> +{
> + uint32_t val;
> +
> + iproc_rng200_enable_set(rng_base, false);
>  
>   /* Clear all interrupt status */
>   iowrite32(0xUL, rng_base + RNG_INT_STATUS_OFFSET);
> @@ -82,11 +92,7 @@ static void iproc_rng200_restart(void __iomem *rng_base)
>   val &= ~RBG_SOFT_RESET;
>   iowrite32(val, rng_base + RBG_SOFT_RESET_OFFSET);
>  
> - /* Enable RBG */
> - val = ioread32(rng_base + RNG_CTRL_OFFSET);
> - val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val |= RNG_CTRL_RNG_RBGEN_ENABLE;
> - iowrite32(val, rng_base + RNG_CTRL_OFFSET);
> + iproc_rng200_enable_set(rng_base, true);
>  }
>  
>  static int iproc_rng200_read(struct hwrng *rng, void *buf, size_t max,
> @@ -153,13 +159,8 @@ static int iproc_rng200_read(struct hwrng *rng, void 
> *buf, size_t max,
>  static int iproc_rng200_init(struct hwrng *rng)
>  {
>   struct iproc_rng200_dev *priv = to_rng_priv(rng);
> - uint32_t val;
>  
> - /* Setup RNG. */
> - val = ioread32(priv->base + RNG_CTRL_OFFSET);
> - val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val |= RNG_CTRL_RNG_RBGEN_ENABLE;
> - iowrite32(val, priv->base + RNG_CTRL_OFFSET);
> + iproc_rng200_enable_set(priv->base, true);
>  
>   return 0;
>  }
> @@ -167,12 +168,8 @@ static int iproc_rng200_init(struct hwrng *rng)
>  static void iproc_rng200_cleanup(struct hwrng *rng)
>  {
>   struct iproc_rng200_dev *priv = to_rng_priv(rng);
> - uint32_t val;
>  
> - /* Disable RNG hardware */
> - val = ioread32(priv->base + RNG_CTRL_OFFSET);
> - val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - iowrite32(val, priv->base + RNG_CTRL_OFFSET);
> + iproc_rng200_enable_set(priv->base, false);
>  }
>  
>  static int iproc_rng200_probe(struct platform_device *pdev)



Re: [PATCH v2 1/2] hwrng: iproc-rng200: Fix disable of the block.

2020-12-18 Thread Scott Branden



On 2020-12-18 2:57 a.m., matthias@kernel.org wrote:
> From: Matthias Brugger 
>
> When trying to disable the block we bitwise or the control
> register with value zero. This is confusing as using bitwise or with
> value zero doesn't have any effect at all. Drop this as we already set
> the enable bit to zero by appling inverted RNG_RBGEN_MASK.
>
> Signed-off-by: Matthias Brugger 
Acked-by: Scott Branden 
>
> ---
>
> Changes in v2:
> - fix commit message, dropping Fixes tag
> - drop inverted RNT_RBGEN_ENABLE in disable case
>
>  drivers/char/hw_random/iproc-rng200.c | 3 ---
>  1 file changed, 3 deletions(-)
>
> diff --git a/drivers/char/hw_random/iproc-rng200.c 
> b/drivers/char/hw_random/iproc-rng200.c
> index 01583faf9893..70cd818a0f31 100644
> --- a/drivers/char/hw_random/iproc-rng200.c
> +++ b/drivers/char/hw_random/iproc-rng200.c
> @@ -28,7 +28,6 @@
>  #define RNG_CTRL_OFFSET  0x00
>  #define RNG_CTRL_RNG_RBGEN_MASK  0x1FFF
>  #define RNG_CTRL_RNG_RBGEN_ENABLE0x0001
> -#define RNG_CTRL_RNG_RBGEN_DISABLE   0x
>  
>  #define RNG_SOFT_RESET_OFFSET0x04
>  #define RNG_SOFT_RESET   0x0001
> @@ -61,7 +60,6 @@ static void iproc_rng200_restart(void __iomem *rng_base)
>   /* Disable RBG */
>   val = ioread32(rng_base + RNG_CTRL_OFFSET);
>   val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val |= RNG_CTRL_RNG_RBGEN_DISABLE;
>   iowrite32(val, rng_base + RNG_CTRL_OFFSET);
>  
>   /* Clear all interrupt status */
> @@ -174,7 +172,6 @@ static void iproc_rng200_cleanup(struct hwrng *rng)
>   /* Disable RNG hardware */
>   val = ioread32(priv->base + RNG_CTRL_OFFSET);
>   val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val |= RNG_CTRL_RNG_RBGEN_DISABLE;
>   iowrite32(val, priv->base + RNG_CTRL_OFFSET);
>  }
>  



Re: [PATCH 2/2] hwrng: iproc-rng200: Move enable/disable in separate function

2020-12-14 Thread Scott Branden



On 2020-12-14 8:04 a.m., matthias@kernel.org wrote:
> From: Matthias Brugger 
>
> We are calling the same code for enable and disable the block in various
> parts of the driver. Put that code into a new function to reduce code
> duplication.
Patch needs to be regenerated after most of PATCH 1 dropped.
>
> Signed-off-by: Matthias Brugger 
>
> ---
>
>  drivers/char/hw_random/iproc-rng200.c | 37 ---
>  1 file changed, 17 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/char/hw_random/iproc-rng200.c 
> b/drivers/char/hw_random/iproc-rng200.c
> index e106ce3c0146..3367b26085e8 100644
> --- a/drivers/char/hw_random/iproc-rng200.c
> +++ b/drivers/char/hw_random/iproc-rng200.c
> @@ -53,15 +53,26 @@ struct iproc_rng200_dev {
>  
>  #define to_rng_priv(rng) container_of(rng, struct iproc_rng200_dev, rng)
>  
> -static void iproc_rng200_restart(void __iomem *rng_base)
> +static void iproc_rng200_enable(void __iomem *rng_base, bool enable)
>  {
>   uint32_t val;
>  
> - /* Disable RBG */
>   val = ioread32(rng_base + RNG_CTRL_OFFSET);
>   val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val &= ~RNG_CTRL_RNG_RBGEN_ENABLE;
> +
> + if (enable)
> + val |= RNG_CTRL_RNG_RBGEN_ENABLE;
> + else
> + val &= ~RNG_CTRL_RNG_RBGEN_ENABLE;
> +
>   iowrite32(val, rng_base + RNG_CTRL_OFFSET);
> +}
> +
> +static void iproc_rng200_restart(void __iomem *rng_base)
> +{
> + uint32_t val;
> +
> + iproc_rng200_enable(rng_base, false);
>  
>   /* Clear all interrupt status */
>   iowrite32(0xUL, rng_base + RNG_INT_STATUS_OFFSET);
> @@ -83,11 +94,7 @@ static void iproc_rng200_restart(void __iomem *rng_base)
>   val &= ~RBG_SOFT_RESET;
>   iowrite32(val, rng_base + RBG_SOFT_RESET_OFFSET);
>  
> - /* Enable RBG */
> - val = ioread32(rng_base + RNG_CTRL_OFFSET);
> - val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val |= RNG_CTRL_RNG_RBGEN_ENABLE;
> - iowrite32(val, rng_base + RNG_CTRL_OFFSET);
> + iproc_rng200_enable(rng_base, true);
>  }
>  
>  static int iproc_rng200_read(struct hwrng *rng, void *buf, size_t max,
> @@ -154,13 +161,8 @@ static int iproc_rng200_read(struct hwrng *rng, void 
> *buf, size_t max,
>  static int iproc_rng200_init(struct hwrng *rng)
>  {
>   struct iproc_rng200_dev *priv = to_rng_priv(rng);
> - uint32_t val;
>  
> - /* Setup RNG. */
> - val = ioread32(priv->base + RNG_CTRL_OFFSET);
> - val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val |= RNG_CTRL_RNG_RBGEN_ENABLE;
> - iowrite32(val, priv->base + RNG_CTRL_OFFSET);
> + iproc_rng200_enable(priv->base, true);
>  
>   return 0;
>  }
> @@ -168,13 +170,8 @@ static int iproc_rng200_init(struct hwrng *rng)
>  static void iproc_rng200_cleanup(struct hwrng *rng)
>  {
>   struct iproc_rng200_dev *priv = to_rng_priv(rng);
> - uint32_t val;
>  
> - /* Disable RNG hardware */
> - val = ioread32(priv->base + RNG_CTRL_OFFSET);
> - val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val &= ~RNG_CTRL_RNG_RBGEN_ENABLE;
> - iowrite32(val, priv->base + RNG_CTRL_OFFSET);
> + iproc_rng200_enable(priv->base, false);
>  }
>  
>  static int iproc_rng200_probe(struct platform_device *pdev)



Re: [PATCH 1/2] hwrng: iproc-rng200: Fix disable of the block.

2020-12-14 Thread Scott Branden



On 2020-12-14 8:04 a.m., matthias@kernel.org wrote:
> From: Matthias Brugger 
>
> When trying to disable the block we bitwise or the control
> register with value zero. This will leave the block always turned on.
> Fix this by setting the corresponding bit to zero.
>
> Fixes: c83d45d5685f ("hwrng: iproc-rng200 - Add Broadcom IPROC RNG driver")
Commit message needs to be re-written.
I don't think this is an actual fix as the ~RNG_CTL_RNG_RBGEN_MASK already 
zeros the bit.  This is just a code change, which is fine because it makes 
things clearer
> Signed-off-by: Matthias Brugger 
> ---
>
>  drivers/char/hw_random/iproc-rng200.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/char/hw_random/iproc-rng200.c 
> b/drivers/char/hw_random/iproc-rng200.c
> index 01583faf9893..e106ce3c0146 100644
> --- a/drivers/char/hw_random/iproc-rng200.c
> +++ b/drivers/char/hw_random/iproc-rng200.c
> @@ -28,7 +28,6 @@
>  #define RNG_CTRL_OFFSET  0x00
>  #define RNG_CTRL_RNG_RBGEN_MASK  0x1FFF
>  #define RNG_CTRL_RNG_RBGEN_ENABLE0x0001
> -#define RNG_CTRL_RNG_RBGEN_DISABLE   0x
>  
>  #define RNG_SOFT_RESET_OFFSET0x04
>  #define RNG_SOFT_RESET   0x0001
> @@ -61,7 +60,7 @@ static void iproc_rng200_restart(void __iomem *rng_base)
>   /* Disable RBG */
>   val = ioread32(rng_base + RNG_CTRL_OFFSET);
>   val &= ~RNG_CTRL_RNG_RBGEN_MASK;
This mask will already zero the enable bit.
> - val |= RNG_CTRL_RNG_RBGEN_DISABLE;
> + val &= ~RNG_CTRL_RNG_RBGEN_ENABLE;
>   iowrite32(val, rng_base + RNG_CTRL_OFFSET);
>  
>   /* Clear all interrupt status */
> @@ -174,7 +173,7 @@ static void iproc_rng200_cleanup(struct hwrng *rng)
>   /* Disable RNG hardware */
>   val = ioread32(priv->base + RNG_CTRL_OFFSET);
>   val &= ~RNG_CTRL_RNG_RBGEN_MASK;
> - val |= RNG_CTRL_RNG_RBGEN_DISABLE;
> + val &= ~RNG_CTRL_RNG_RBGEN_ENABLE;
>   iowrite32(val, priv->base + RNG_CTRL_OFFSET);
>  }
>  



Re: [PATCH] menuconfig,mconf-cfg: Allow specification of ncurses location

2020-12-01 Thread Scott Branden
Hi Bruce,

On 2020-12-01 11:01 a.m., Bruce Ashfield wrote:
> On Tue, Dec 1, 2020 at 12:19 PM Scott Branden
>  wrote:
>> Hi Masahiro,
>>
>> On 2020-12-01 4:25 a.m., Masahiro Yamada wrote:
>>> On Sat, Nov 28, 2020 at 9:45 AM Scott Branden
>>>  wrote:
>>>> From: Bruce Ashfield 
>>>>
>>>> In some cross build environments such as the Yocto Project build
>>>> environment it provides an ncurses library that is compiled
>>>> differently than the host's version.  This causes display corruption
>>>> problems when the host's curses includes are used instead of the
>>>> includes from the provided compiler are overridden.  There is a second
>>>> case where there is no curses libraries at all on the host system and
>>>> menuconfig will just fail entirely.
>>>>
>>>> The solution is simply to allow an override variable in
>>>> check-lxdialog.sh for environments such as the Yocto Project.  Adding
>>>> a CROSS_CURSES_LIB and CROSS_CURSES_INC solves the issue and allowing
>>>> compiling and linking against the right headers and libraries.
>>>>
>>>> Signed-off-by: Jason Wessel 
>>>> cc: Michal Marek 
>>>> cc: linux-kbu...@vger.kernel.org
>>>> Signed-off-by: Bruce Ashfield 
>>>> Signed-off-by: Scott Branden 
>>>> ---
>>> Some people solve the cross-compiling in Yocto
>>> by using pkg-config.
>>>
>>>
>>> For example,
>>>
>>> commit 067c650c456e758f933aaf87a202f841d34be269
>>> Author: Pavel Modilaynen 
>>> Date:   Fri Jul 12 13:52:19 2019 +0200
>>>
>>> dtc: Use pkg-config to locate libyaml
>>>
>>> Using Makefile's wildcard with absolute path to detect
>>> the presence of libyaml results in false-positive
>>> detection when cross-compiling e.g. in yocto environment.
>>>
>>>
>>>
>>> mconf-cfg.sh already allows the path flexibility with pkg-config.
>>> Why do you want yet another hook?
>> I hope the yocto community can provide more details on this patch.
>> The yocto environment isolates the build environment from the host tools.
>> Running menuconfig with the upstream kernel does not work on the latest 
>> yocto without this patch.
> Sorry for not commenting on the origin patch, gmail buried it within
> some other threads, but luckily this one popped up.
>
> It is true that we've been carrying this for several years to deal with
> the fact that the native sysroot is not searched by the pkg-config
> called by mconf-cfg.sh (since it is separate from host and target
> pkg-config).
>
> As it turns out, in the past few weeks, we have come up with a way
> to inject those native sysroot components into pkg-config without
> the need for any changes to the scripts.
>
> Scott: if you try again the the latest oe-core, and are still seeing
> the problem with the mainline kernel, ping me, and we can see if
> the pkg-config fix isn't holding for you, at that point, yes, we may
> still need a hook like this to solve the problem.
Try reverting this kernel patch from linux-yocto and menuconfig will fail.

menuconfig actually did work with the upstream kernel until the yocto change 
below was introduced:
"cml1.bbclass: Handle ncurses-native being available via pkg-config"
https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?h=master-next=ce447d70df386ca55ce1672478b245851556374e

> Cheers,
>
> Bruce
>
>
>
>>>>  scripts/kconfig/mconf-cfg.sh | 8 
>>>>  1 file changed, 8 insertions(+)
>>>>  mode change 100755 => 100644 scripts/kconfig/mconf-cfg.sh
>>>>
>>>> diff --git a/scripts/kconfig/mconf-cfg.sh b/scripts/kconfig/mconf-cfg.sh
>>>> old mode 100755
>>>> new mode 100644
>>>> index aa68ec95620d..32448bc198a5
>>>> --- a/scripts/kconfig/mconf-cfg.sh
>>>> +++ b/scripts/kconfig/mconf-cfg.sh
>>>> @@ -4,6 +4,14 @@
>>>>  PKG="ncursesw"
>>>>  PKG2="ncurses"
>>>>
>>>> +if [ "$CROSS_CURSES_LIB" != "" ]; then
>>>> +echo libs=\'$CROSS_CURSES_LIB\'
>>>> +if [ x"$CROSS_CURSES_INC" != x ]; then
>>>> +   echo cflags=\'$CROSS_CURSES_INC\'
>>>> +fi
>>>> +exit 0
>>>> +fi
>>>> +
>>>>  if [ -n "$(command -v pkg-config)" ]; then
>>>> if pkg-config --exists $PKG; then
>>>> echo cflags=\"$(pkg-config --cflags $PKG)\"
>>>> --
>>>> 2.17.1
>>>>
>



smime.p7s
Description: S/MIME Cryptographic Signature


Re: [PATCH] menuconfig,mconf-cfg: Allow specification of ncurses location

2020-12-01 Thread Scott Branden
Hi Masahiro,

On 2020-12-01 4:25 a.m., Masahiro Yamada wrote:
> On Sat, Nov 28, 2020 at 9:45 AM Scott Branden
>  wrote:
>> From: Bruce Ashfield 
>>
>> In some cross build environments such as the Yocto Project build
>> environment it provides an ncurses library that is compiled
>> differently than the host's version.  This causes display corruption
>> problems when the host's curses includes are used instead of the
>> includes from the provided compiler are overridden.  There is a second
>> case where there is no curses libraries at all on the host system and
>> menuconfig will just fail entirely.
>>
>> The solution is simply to allow an override variable in
>> check-lxdialog.sh for environments such as the Yocto Project.  Adding
>> a CROSS_CURSES_LIB and CROSS_CURSES_INC solves the issue and allowing
>> compiling and linking against the right headers and libraries.
>>
>> Signed-off-by: Jason Wessel 
>> cc: Michal Marek 
>> cc: linux-kbu...@vger.kernel.org
>> Signed-off-by: Bruce Ashfield 
>> Signed-off-by: Scott Branden 
>> ---
>
> Some people solve the cross-compiling in Yocto
> by using pkg-config.
>
>
> For example,
>
> commit 067c650c456e758f933aaf87a202f841d34be269
> Author: Pavel Modilaynen 
> Date:   Fri Jul 12 13:52:19 2019 +0200
>
> dtc: Use pkg-config to locate libyaml
>
> Using Makefile's wildcard with absolute path to detect
> the presence of libyaml results in false-positive
> detection when cross-compiling e.g. in yocto environment.
>
>
>
> mconf-cfg.sh already allows the path flexibility with pkg-config.
> Why do you want yet another hook?
I hope the yocto community can provide more details on this patch.
The yocto environment isolates the build environment from the host tools.
Running menuconfig with the upstream kernel does not work on the latest yocto 
without this patch.
>>  scripts/kconfig/mconf-cfg.sh | 8 
>>  1 file changed, 8 insertions(+)
>>  mode change 100755 => 100644 scripts/kconfig/mconf-cfg.sh
>>
>> diff --git a/scripts/kconfig/mconf-cfg.sh b/scripts/kconfig/mconf-cfg.sh
>> old mode 100755
>> new mode 100644
>> index aa68ec95620d..32448bc198a5
>> --- a/scripts/kconfig/mconf-cfg.sh
>> +++ b/scripts/kconfig/mconf-cfg.sh
>> @@ -4,6 +4,14 @@
>>  PKG="ncursesw"
>>  PKG2="ncurses"
>>
>> +if [ "$CROSS_CURSES_LIB" != "" ]; then
>> +echo libs=\'$CROSS_CURSES_LIB\'
>> +if [ x"$CROSS_CURSES_INC" != x ]; then
>> +   echo cflags=\'$CROSS_CURSES_INC\'
>> +fi
>> +exit 0
>> +fi
>> +
>>  if [ -n "$(command -v pkg-config)" ]; then
>> if pkg-config --exists $PKG; then
>> echo cflags=\"$(pkg-config --cflags $PKG)\"
>> --
>> 2.17.1
>>
>



smime.p7s
Description: S/MIME Cryptographic Signature


[PATCH v8 11/13] misc: bcm-vk: add mmap function for exposing BAR2

2020-11-30 Thread Scott Branden
Add mmap function that allows host application to open up BAR2 memory
for remote spooling out messages from the VK logger.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk_dev.c | 24 
 1 file changed, 24 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index e572a7b18fab..cac07419f041 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -1199,6 +1199,29 @@ static long bcm_vk_reset(struct bcm_vk *vk, struct 
vk_reset __user *arg)
return ret;
 }
 
+static int bcm_vk_mmap(struct file *file, struct vm_area_struct *vma)
+{
+   struct bcm_vk_ctx *ctx = file->private_data;
+   struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
+   unsigned long pg_size;
+
+   /* only BAR2 is mmap possible, which is bar num 4 due to 64bit */
+#define VK_MMAPABLE_BAR 4
+
+   pg_size = ((pci_resource_len(vk->pdev, VK_MMAPABLE_BAR) - 1)
+   >> PAGE_SHIFT) + 1;
+   if (vma->vm_pgoff + vma_pages(vma) > pg_size)
+   return -EINVAL;
+
+   vma->vm_pgoff += (pci_resource_start(vk->pdev, VK_MMAPABLE_BAR)
+ >> PAGE_SHIFT);
+   vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+   return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+ vma->vm_end - vma->vm_start,
+ vma->vm_page_prot);
+}
+
 static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
 {
long ret = -EINVAL;
@@ -1237,6 +1260,7 @@ static const struct file_operations bcm_vk_fops = {
.write = bcm_vk_write,
.poll = bcm_vk_poll,
.release = bcm_vk_release,
+   .mmap = bcm_vk_mmap,
.unlocked_ioctl = bcm_vk_ioctl,
 };
 
-- 
2.17.1



[PATCH v8 12/13] MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver

2020-11-30 Thread Scott Branden
Add maintainer entry for new Broadcom VK Driver

Signed-off-by: Scott Branden 
---
 MAINTAINERS | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 4a34b25ecc1f..2ce6adcff41d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3716,6 +3716,13 @@ L:   net...@vger.kernel.org
 S: Supported
 F: drivers/net/ethernet/broadcom/tg3.*
 
+BROADCOM VK DRIVER
+M: Scott Branden 
+L: bcm-kernel-feedback-l...@broadcom.com
+S: Supported
+F: drivers/misc/bcm-vk/
+F: include/uapi/linux/misc/bcm_vk.h
+
 BROCADE BFA FC SCSI DRIVER
 M: Anil Gurumurthy 
 M: Sudarsana Kalluru 
-- 
2.17.1



[PATCH v8 13/13] misc: bcm-vk: add ttyVK support

2020-11-30 Thread Scott Branden
Add ttyVK support to driver to allow console access to VK card from host.

Device node will be in the follow form /dev/bcm-vk.x_ttyVKy where:
x is the instance of the VK card
y is the tty device number on the VK card

Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/Makefile |   3 +-
 drivers/misc/bcm-vk/bcm_vk.h |  28 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c |  30 ++-
 drivers/misc/bcm-vk/bcm_vk_tty.c | 333 +++
 4 files changed, 392 insertions(+), 2 deletions(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index 79b4e365c9e6..e4a1486f7209 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -7,5 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
bcm_vk_msg.o \
-   bcm_vk_sg.o
+   bcm_vk_sg.o \
+   bcm_vk_tty.o
 
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index a1d0bf6e694c..3f37c640a814 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -8,12 +8,14 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -84,6 +86,9 @@
 #define CODEPUSH_BOOT2_ENTRY   0x6000
 
 #define BAR_CARD_STATUS0x410
+/* CARD_STATUS definitions */
+#define CARD_STATUS_TTYVK0_READY   BIT(0)
+#define CARD_STATUS_TTYVK1_READY   BIT(1)
 
 #define BAR_BOOT1_STDALONE_PROGRESS0x420
 #define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
@@ -255,6 +260,19 @@ enum pci_barno {
 
 #define BCM_VK_NUM_TTY 2
 
+struct bcm_vk_tty {
+   struct tty_port port;
+   u32 to_offset;  /* bar offset to use */
+   u32 to_size;/* to VK buffer size */
+   u32 wr; /* write offset shadow */
+   u32 from_offset;/* bar offset to use */
+   u32 from_size;  /* from VK buffer size */
+   u32 rd; /* read offset shadow */
+   pid_t pid;
+   bool irq_enabled;
+   bool is_opened; /* tracks tty open/close */
+};
+
 /* VK device max power state, supports 3, full, reduced and low */
 #define MAX_OPP 3
 #define MAX_CARD_INFO_TAG_SIZE 64
@@ -348,6 +366,12 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+   struct tty_driver *tty_drv;
+   struct timer_list serial_timer;
+   struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
+   struct workqueue_struct *tty_wq_thread;
+   struct work_struct tty_wq_work;
+
/* Reference-counting to handle file operations */
struct kref kref;
 
@@ -466,6 +490,7 @@ int bcm_vk_release(struct inode *inode, struct file 
*p_file);
 void bcm_vk_release_data(struct kref *kref);
 irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
+irqreturn_t bcm_vk_tty_irqhandler(int irq, void *dev_id);
 int bcm_vk_msg_init(struct bcm_vk *vk);
 void bcm_vk_msg_remove(struct bcm_vk *vk);
 void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
@@ -476,6 +501,9 @@ int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 
shut_type,
 const pid_t pid, const u32 q_num);
 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
+int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
+void bcm_vk_tty_exit(struct bcm_vk *vk);
+void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
 void bcm_vk_hb_init(struct bcm_vk *vk);
 void bcm_vk_hb_deinit(struct bcm_vk *vk);
 void bcm_vk_handle_notf(struct bcm_vk *vk);
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index cac07419f041..c3d2bba68ef1 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -525,6 +525,7 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
}
}
}
+   bcm_vk_tty_terminate_tty_user(vk);
spin_unlock(>ctx_lock);
 }
 
@@ -1384,6 +1385,20 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
vk->num_irqs++;
 
+   for (i = 0;
+(i < VK_MSIX_TTY_MAX) && (vk->num_irqs < irq);
+i++, vk->num_irqs++) {
+   err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs),
+  bcm_vk_tty_irqhandler,
+  IRQF_SHARED, DRV_MODULE_NAME, vk);
+   if (err) {
+   dev_err(dev, "failed request tty IRQ %d for MSIX %d\n",
+   pdev->irq + vk->num_irqs, vk->num_irqs + 1);
+   goto err_irq;
+   }
+   vk->tty[i].irq_enabled = true;
+   }
+
id = ida_simple_get(_vk_ida, 0, 0, GFP_KERNEL);
if (id < 0) {
 

[PATCH v8 10/13] misc: bcm-vk: reset_pid support

2020-11-30 Thread Scott Branden
Add reset support via ioctl.
Kill user processes that are open when VK card is reset.
If a particular PID has issued the reset request do not kill that process
as it issued the ioctl.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |   1 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 158 +--
 drivers/misc/bcm-vk/bcm_vk_msg.c |  40 +++-
 3 files changed, 191 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index d847a512d0ed..a1d0bf6e694c 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -468,6 +468,7 @@ irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
 int bcm_vk_msg_init(struct bcm_vk *vk);
 void bcm_vk_msg_remove(struct bcm_vk *vk);
+void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
 int bcm_vk_sync_msgq(struct bcm_vk *vk, bool force_sync);
 void bcm_vk_blk_drv_access(struct bcm_vk *vk);
 s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk);
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 5d82f02c0f27..e572a7b18fab 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -504,7 +504,9 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
int i;
 
/*
-* kill all the apps
+* kill all the apps except for the process that is resetting.
+* If not called during reset, reset_pid will be 0, and all will be
+* killed.
 */
spin_lock(>ctx_lock);
 
@@ -515,10 +517,12 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
struct bcm_vk_ctx *ctx;
 
list_for_each_entry(ctx, >pid_ht[i].head, node) {
-   dev_dbg(>pdev->dev,
-   "Send kill signal to pid %d\n",
-   ctx->pid);
-   kill_pid(find_vpid(ctx->pid), SIGKILL, 1);
+   if (ctx->pid != vk->reset_pid) {
+   dev_dbg(>pdev->dev,
+   "Send kill signal to pid %d\n",
+   ctx->pid);
+   kill_pid(find_vpid(ctx->pid), SIGKILL, 1);
+   }
}
}
spin_unlock(>ctx_lock);
@@ -1001,6 +1005,49 @@ static long bcm_vk_load_image(struct bcm_vk *vk,
return ret;
 }
 
+static int bcm_vk_reset_successful(struct bcm_vk *vk)
+{
+   struct device *dev = >pdev->dev;
+   u32 fw_status, reset_reason;
+   int ret = -EAGAIN;
+
+   /*
+* Reset could be triggered when the card in several state:
+*   i)   in bootROM
+*   ii)  after boot1
+*   iii) boot2 running
+*
+* i) & ii) - no status bits will be updated.  If vkboot1
+* runs automatically after reset, it  will update the reason
+* to be unknown reason
+* iii) - reboot reason match + deinit done.
+*/
+   fw_status = vkread32(vk, BAR_0, VK_BAR_FWSTS);
+   /* immediate exit if interface goes down */
+   if (BCM_VK_INTF_IS_DOWN(fw_status)) {
+   dev_err(dev, "PCIe Intf Down!\n");
+   goto reset_exit;
+   }
+
+   reset_reason = (fw_status & VK_FWSTS_RESET_REASON_MASK);
+   if ((reset_reason == VK_FWSTS_RESET_MBOX_DB) ||
+   (reset_reason == VK_FWSTS_RESET_UNKNOWN))
+   ret = 0;
+
+   /*
+* if some of the deinit bits are set, but done
+* bit is not, this is a failure if triggered while boot2 is running
+*/
+   if ((fw_status & VK_FWSTS_DEINIT_TRIGGERED) &&
+   !(fw_status & VK_FWSTS_RESET_DONE))
+   ret = -EAGAIN;
+
+reset_exit:
+   dev_dbg(dev, "FW status = 0x%x ret %d\n", fw_status, ret);
+
+   return ret;
+}
+
 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val)
 {
vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE);
@@ -1010,12 +1057,16 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
 {
u32 i;
u32 value, boot_status;
+   bool is_stdalone, is_boot2;
static const u32 bar0_reg_clr_list[] = { BAR_OS_UPTIME,
 BAR_INTF_VER,
 BAR_CARD_VOLTAGE,
 BAR_CARD_TEMPERATURE,
 BAR_CARD_PWR_AND_THRE };
 
+   /* clean up before pressing the door bell */
+   bcm_vk_drain_msg_on_reset(vk);
+   vkwrite32(vk, 0, BAR_1, VK_BAR1_MSGQ_DEF_RDY);
/* make tag '\0' terminated */
vkwrite32(vk, 0, BAR_1, VK_BAR1_BOOT1_VER_TAG);
 
@@ -1026,6 +1077,11 @@ static int bcm_vk_trigger_reset(s

[PATCH v8 09/13] misc: bcm-vk: add VK messaging support

2020-11-30 Thread Scott Branden
Add message support in order to be able to communicate
to VK card via message queues.

This info is used for debug purposes via collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/Makefile |3 +-
 drivers/misc/bcm-vk/bcm_vk.h |  123 
 drivers/misc/bcm-vk/bcm_vk_dev.c |  309 +++-
 drivers/misc/bcm-vk/bcm_vk_msg.c | 1187 ++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  132 
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  275 +++
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   61 ++
 7 files changed, 2087 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index a2ae79858409..79b4e365c9e6 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -6,5 +6,6 @@
 obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
-   bcm_vk_msg.o
+   bcm_vk_msg.o \
+   bcm_vk_sg.o
 
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 50f2a0cd6e13..d847a512d0ed 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -6,11 +6,13 @@
 #ifndef BCM_VK_H
 #define BCM_VK_H
 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -93,14 +95,53 @@
 #define MAJOR_SOC_REV(_chip_id)(((_chip_id) >> 20) & 0xf)
 
 #define BAR_CARD_TEMPERATURE   0x45c
+/* defines for all temperature sensor */
+#define BCM_VK_TEMP_FIELD_MASK 0xff
+#define BCM_VK_CPU_TEMP_SHIFT  0
+#define BCM_VK_DDR0_TEMP_SHIFT 8
+#define BCM_VK_DDR1_TEMP_SHIFT 16
 
 #define BAR_CARD_VOLTAGE   0x460
+/* defines for voltage rail conversion */
+#define BCM_VK_VOLT_RAIL_MASK  0x
+#define BCM_VK_3P3_VOLT_REG_SHIFT  16
 
 #define BAR_CARD_ERR_LOG   0x464
+/* Error log register bit definition - register for error alerts */
+#define ERR_LOG_UECC   BIT(0)
+#define ERR_LOG_SSIM_BUSY  BIT(1)
+#define ERR_LOG_AFBC_BUSY  BIT(2)
+#define ERR_LOG_HIGH_TEMP_ERR  BIT(3)
+#define ERR_LOG_WDOG_TIMEOUT   BIT(4)
+#define ERR_LOG_SYS_FAULT  BIT(5)
+#define ERR_LOG_RAMDUMPBIT(6)
+#define ERR_LOG_COP_WDOG_TIMEOUT   BIT(7)
+/* warnings */
+#define ERR_LOG_MEM_ALLOC_FAIL BIT(8)
+#define ERR_LOG_LOW_TEMP_WARN  BIT(9)
+#define ERR_LOG_ECCBIT(10)
+#define ERR_LOG_IPC_DWNBIT(11)
+
+/* Alert bit definitions detectd on host */
+#define ERR_LOG_HOST_INTF_V_FAIL   BIT(13)
+#define ERR_LOG_HOST_HB_FAIL   BIT(14)
+#define ERR_LOG_HOST_PCIE_DWN  BIT(15)
 
 #define BAR_CARD_ERR_MEM   0x468
+/* defines for mem err, all fields have same width */
+#define BCM_VK_MEM_ERR_FIELD_MASK  0xff
+#define BCM_VK_ECC_MEM_ERR_SHIFT   0
+#define BCM_VK_UECC_MEM_ERR_SHIFT  8
+/* threshold of event occurrence and logs start to come out */
+#define BCM_VK_ECC_THRESHOLD   10
+#define BCM_VK_UECC_THRESHOLD  1
 
 #define BAR_CARD_PWR_AND_THRE  0x46c
+/* defines for power and temp threshold, all fields have same width */
+#define BCM_VK_PWR_AND_THRE_FIELD_MASK 0xff
+#define BCM_VK_LOW_TEMP_THRE_SHIFT 0
+#define BCM_VK_HIGH_TEMP_THRE_SHIFT8
+#define BCM_VK_PWR_STATE_SHIFT 16
 
 #define BAR_CARD_STATIC_INFO   0x470
 
@@ -143,6 +184,11 @@
 #define BAR_FIRMWARE_TAG_SIZE  50
 #define FIRMWARE_STATUS_PRE_INIT_DONE  0x1f
 
+/* VK MSG_ID defines */
+#define VK_MSG_ID_BITMAP_SIZE  4096
+#define VK_MSG_ID_BITMAP_MASK  (VK_MSG_ID_BITMAP_SIZE - 1)
+#define VK_MSG_ID_OVERFLOW 0x
+
 /*
  * BAR1
  */
@@ -197,6 +243,10 @@
 /* VK device supports a maximum of 3 bars */
 #define MAX_BAR3
 
+/* default number of msg blk for inband SGL */
+#define BCM_VK_DEF_IB_SGL_BLK_LEN   16
+#define BCM_VK_IB_SGL_BLK_MAX   24
+
 enum pci_barno {
BAR_0 = 0,
BAR_1,
@@ -267,9 +317,27 @@ struct bcm_vk_proc_mon_info {
struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
 };
 
+struct bcm_vk_hb_ctrl {
+   struct timer_list timer;
+   u32 last_uptime;
+   u32 lost_cnt;
+};
+
+struct bcm_vk_alert {
+   u16 flags;
+   u16 notfs;
+};
+
+/* some alert counters that the driver will keep track */
+struct bcm_vk_alert_cnts {
+   u16 ecc;
+   u16 uecc;
+};
+
 struct bcm_vk {
struct pci_dev *pdev;
void __iomem *bar[MAX_BAR];
+   int num_irqs;
 
struct bcm_vk_card_info card_info;
struct bcm_vk_proc_mon_info proc_mon_info;
@@ -283,9 +351,17 @@ struct bcm_vk {
/* Reference-counting to h

[PATCH v8 08/13] misc: bcm-vk: add get_card_info, peerlog_info, and proc_mon_info

2020-11-30 Thread Scott Branden
Add support to get card_info (details about card),
peerlog_info (to get details of peerlog on card),
and proc_mon_info (process monitoring on card).

This info is used for collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |  60 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 105 +++
 2 files changed, 165 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 726aab71bb6b..50f2a0cd6e13 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -205,6 +205,21 @@ enum pci_barno {
 
 #define BCM_VK_NUM_TTY 2
 
+/* VK device max power state, supports 3, full, reduced and low */
+#define MAX_OPP 3
+#define MAX_CARD_INFO_TAG_SIZE 64
+
+struct bcm_vk_card_info {
+   u32 version;
+   char os_tag[MAX_CARD_INFO_TAG_SIZE];
+   char cmpt_tag[MAX_CARD_INFO_TAG_SIZE];
+   u32 cpu_freq_mhz;
+   u32 cpu_scale[MAX_OPP];
+   u32 ddr_freq_mhz;
+   u32 ddr_size_MB;
+   u32 video_core_freq_mhz;
+};
+
 /* DAUTH related info */
 struct bcm_vk_dauth_key {
char store[VK_BAR1_DAUTH_STORE_SIZE];
@@ -215,10 +230,49 @@ struct bcm_vk_dauth_info {
struct bcm_vk_dauth_key keys[VK_BAR1_DAUTH_MAX];
 };
 
+/*
+ * Control structure of logging messages from the card.  This
+ * buffer is for logmsg that comes from vk
+ */
+struct bcm_vk_peer_log {
+   u32 rd_idx;
+   u32 wr_idx;
+   u32 buf_size;
+   u32 mask;
+   char data[0];
+};
+
+/* max buf size allowed */
+#define BCM_VK_PEER_LOG_BUF_MAX SZ_16K
+/* max size per line of peer log */
+#define BCM_VK_PEER_LOG_LINE_MAX  256
+
+/*
+ * single entry for processing type + utilization
+ */
+#define BCM_VK_PROC_TYPE_TAG_LEN 8
+struct bcm_vk_proc_mon_entry_t {
+   char tag[BCM_VK_PROC_TYPE_TAG_LEN];
+   u32 used;
+   u32 max; /**< max capacity */
+};
+
+/**
+ * Structure for run time utilization
+ */
+#define BCM_VK_PROC_MON_MAX 8 /* max entries supported */
+struct bcm_vk_proc_mon_info {
+   u32 num; /**< no of entries */
+   u32 entry_size; /**< per entry size */
+   struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
+};
+
 struct bcm_vk {
struct pci_dev *pdev;
void __iomem *bar[MAX_BAR];
 
+   struct bcm_vk_card_info card_info;
+   struct bcm_vk_proc_mon_info proc_mon_info;
struct bcm_vk_dauth_info dauth_info;
 
/* mutex to protect the ioctls */
@@ -240,6 +294,12 @@ struct bcm_vk {
dma_addr_t tdma_addr; /* test dma segment bus addr */
 
struct notifier_block panic_nb;
+
+   /* offset of the peer log control in BAR2 */
+   u32 peerlog_off;
+   struct bcm_vk_peer_log peerlog_info; /* record of peer log info */
+   /* offset of processing monitoring info in BAR2 */
+   u32 proc_mon_off;
 };
 
 /* wq offload work items bits definitions */
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 203a1cf2bae3..a63208513c64 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -172,6 +172,104 @@ static inline int bcm_vk_wait(struct bcm_vk *vk, enum 
pci_barno bar,
return 0;
 }
 
+static void bcm_vk_get_card_info(struct bcm_vk *vk)
+{
+   struct device *dev = >pdev->dev;
+   u32 offset;
+   int i;
+   u8 *dst;
+   struct bcm_vk_card_info *info = >card_info;
+
+   /* first read the offset from spare register */
+   offset = vkread32(vk, BAR_0, BAR_CARD_STATIC_INFO);
+   offset &= (pci_resource_len(vk->pdev, BAR_2 * 2) - 1);
+
+   /* based on the offset, read info to internal card info structure */
+   dst = (u8 *)info;
+   for (i = 0; i < sizeof(*info); i++)
+   *dst++ = vkread8(vk, BAR_2, offset++);
+
+#define CARD_INFO_LOG_FMT "version   : %x\n" \
+ "os_tag: %s\n" \
+ "cmpt_tag  : %s\n" \
+ "cpu_freq  : %d MHz\n" \
+ "cpu_scale : %d full, %d lowest\n" \
+ "ddr_freq  : %d MHz\n" \
+ "ddr_size  : %d MB\n" \
+ "video_freq: %d MHz\n"
+   dev_dbg(dev, CARD_INFO_LOG_FMT, info->version, info->os_tag,
+   info->cmpt_tag, info->cpu_freq_mhz, info->cpu_scale[0],
+   info->cpu_scale[MAX_OPP - 1], info->ddr_freq_mhz,
+   info->ddr_size_MB, info->video_core_freq_mhz);
+
+   /*
+* get the peer log pointer, only need the offset, and get record
+* of the log buffer information which would be used for checking
+* before dump, in case the BAR2 memory has been corrupted.
+*/
+

[PATCH v8 07/13] misc: bcm-vk: add ioctl load_image

2020-11-30 Thread Scott Branden
Add ioctl support to issue load_image operation to VK card.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Co-developed-by: James Hu 
Signed-off-by: James Hu 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |  3 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 95 
 2 files changed, 98 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 5f0fcfdaf265..726aab71bb6b 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include "bcm_vk_msg.h"
@@ -220,6 +221,8 @@ struct bcm_vk {
 
struct bcm_vk_dauth_info dauth_info;
 
+   /* mutex to protect the ioctls */
+   struct mutex mutex;
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 79fffb1e6f84..203a1cf2bae3 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -580,6 +581,71 @@ static void bcm_vk_wq_handler(struct work_struct *work)
}
 }
 
+static long bcm_vk_load_image(struct bcm_vk *vk,
+ const struct vk_image __user *arg)
+{
+   struct device *dev = >pdev->dev;
+   const char *image_name;
+   struct vk_image image;
+   u32 next_loadable;
+   enum soc_idx idx;
+   int image_idx;
+   int ret = -EPERM;
+
+   if (copy_from_user(, arg, sizeof(image)))
+   return -EACCES;
+
+   if ((image.type != VK_IMAGE_TYPE_BOOT1) &&
+   (image.type != VK_IMAGE_TYPE_BOOT2)) {
+   dev_err(dev, "invalid image.type %u\n", image.type);
+   return ret;
+   }
+
+   next_loadable = bcm_vk_next_boot_image(vk);
+   if (next_loadable != image.type) {
+   dev_err(dev, "Next expected image %u, Loading %u\n",
+   next_loadable, image.type);
+   return ret;
+   }
+
+   /*
+* if something is pending download already.  This could only happen
+* for now when the driver is being loaded, or if someone has issued
+* another download command in another shell.
+*/
+   if (test_and_set_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload) != 0) {
+   dev_err(dev, "Download operation already pending.\n");
+   return ret;
+   }
+
+   image_name = image.filename;
+   if (image_name[0] == '\0') {
+   /* Use default image name if NULL */
+   idx = get_soc_idx(vk);
+   if (idx == VK_IDX_INVALID)
+   goto err_idx;
+
+   /* Image idx starts with boot1 */
+   image_idx = image.type - VK_IMAGE_TYPE_BOOT1;
+   image_name = get_load_fw_name(vk, _tab[idx][image_idx]);
+   if (!image_name) {
+   dev_err(dev, "No suitable image found for type %d",
+   image.type);
+   ret = -ENOENT;
+   goto err_idx;
+   }
+   } else {
+   /* Ensure filename is NULL terminated */
+   image.filename[sizeof(image.filename) - 1] = '\0';
+   }
+   ret = bcm_vk_load_image_by_type(vk, image.type, image_name);
+   dev_info(dev, "Load %s, ret %d\n", image_name, ret);
+err_idx:
+   clear_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload);
+
+   return ret;
+}
+
 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val)
 {
vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE);
@@ -636,10 +702,38 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
+{
+   long ret = -EINVAL;
+   struct bcm_vk_ctx *ctx = file->private_data;
+   struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
+   void __user *argp = (void __user *)arg;
+
+   dev_dbg(>pdev->dev,
+   "ioctl, cmd=0x%02x, arg=0x%02lx\n",
+   cmd, arg);
+
+   mutex_lock(>mutex);
+
+   switch (cmd) {
+   case VK_IOCTL_LOAD_IMAGE:
+   ret = bcm_vk_load_image(vk, argp);
+   break;
+
+   default:
+   break;
+   }
+
+   mutex_unlock(>mutex);
+
+   return ret;
+}
+
 static const struct file_operations bcm_vk_fops = {
.owner = THIS_MODULE,
.open = bcm_vk_open,
.release = bcm_vk_release,
+   .unlocked_ioctl = bcm_vk_ioctl,
 };
 
 static int bcm_vk_on_panic(struct notifier_block *nb,
@@ -670,6 +764,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
re

[PATCH v8 06/13] misc: bcm-vk: add open/release

2020-11-30 Thread Scott Branden
Add open/release to replace private data with context for other methods
to use.  Reason for the context is because it is allowed for multiple
sessions to open sysfs.  For each file open, when upper layer queries the
response, only those that are tied to a specified open should be returned.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/Makefile |   4 +-
 drivers/misc/bcm-vk/bcm_vk.h |  15 
 drivers/misc/bcm-vk/bcm_vk_dev.c |  23 ++
 drivers/misc/bcm-vk/bcm_vk_msg.c | 127 +++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  31 
 5 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index f8a7ac4c242f..a2ae79858409 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -5,4 +5,6 @@
 
 obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
-   bcm_vk_dev.o
+   bcm_vk_dev.o \
+   bcm_vk_msg.o
+
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index f428ad9a0c3d..5f0fcfdaf265 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -7,9 +7,14 @@
 #define BCM_VK_H
 
 #include 
+#include 
 #include 
+#include 
 #include 
 #include 
+#include 
+
+#include "bcm_vk_msg.h"
 
 #define DRV_MODULE_NAME"bcm-vk"
 
@@ -218,6 +223,13 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+   /* Reference-counting to handle file operations */
+   struct kref kref;
+
+   spinlock_t ctx_lock; /* Spinlock for component context */
+   struct bcm_vk_ctx ctx[VK_CMPT_CTX_MAX];
+   struct bcm_vk_ht_entry pid_ht[VK_PID_HT_SZ];
+
struct workqueue_struct *wq_thread;
struct work_struct wq_work; /* work queue for deferred job */
unsigned long wq_offload[1]; /* various flags on wq requested */
@@ -278,6 +290,9 @@ static inline bool bcm_vk_msgq_marker_valid(struct bcm_vk 
*vk)
return (rdy_marker == VK_BAR1_MSGQ_RDY_MARKER);
 }
 
+int bcm_vk_open(struct inode *inode, struct file *p_file);
+int bcm_vk_release(struct inode *inode, struct file *p_file);
+void bcm_vk_release_data(struct kref *kref);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
 
 #endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 09d99bd36e8a..79fffb1e6f84 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -8,6 +8,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -635,6 +636,12 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static const struct file_operations bcm_vk_fops = {
+   .owner = THIS_MODULE,
+   .open = bcm_vk_open,
+   .release = bcm_vk_release,
+};
+
 static int bcm_vk_on_panic(struct notifier_block *nb,
   unsigned long e, void *p)
 {
@@ -657,10 +664,13 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
struct miscdevice *misc_device;
u32 boot_status;
 
+   /* allocate vk structure which is tied to kref for freeing */
vk = kzalloc(sizeof(*vk), GFP_KERNEL);
if (!vk)
return -ENOMEM;
 
+   kref_init(>kref);
+
err = pci_enable_device(pdev);
if (err) {
dev_err(dev, "Cannot enable PCI device\n");
@@ -738,6 +748,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
err = -ENOMEM;
goto err_ida_remove;
}
+   misc_device->fops = _vk_fops,
 
err = misc_register(misc_device);
if (err) {
@@ -826,6 +837,16 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
return err;
 }
 
+void bcm_vk_release_data(struct kref *kref)
+{
+   struct bcm_vk *vk = container_of(kref, struct bcm_vk, kref);
+   struct pci_dev *pdev = vk->pdev;
+
+   dev_dbg(>dev, "BCM-VK:%d release data 0x%p\n", vk->devid, vk);
+   pci_dev_put(pdev);
+   kfree(vk);
+}
+
 static void bcm_vk_remove(struct pci_dev *pdev)
 {
int i;
@@ -869,6 +890,8 @@ static void bcm_vk_remove(struct pci_dev *pdev)
pci_release_regions(pdev);
pci_free_irq_vectors(pdev);
pci_disable_device(pdev);
+
+   kref_put(>kref, bcm_vk_release_data);
 }
 
 static void bcm_vk_shutdown(struct pci_dev *pdev)
diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c b/drivers/misc/bcm-vk/bcm_vk_msg.c
new file mode 100644
index ..2d9a6b4e5f61
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk_msg.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#include "bcm_vk.h"
+#include "b

[PATCH v8 04/13] misc: bcm-vk: add misc device to Broadcom VK driver

2020-11-30 Thread Scott Branden
Add misc device base support to create and remove devnode.
Additional misc functions for open/read/write/release/ioctl/sysfs, etc
will be added in follow on commits to allow for individual review.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |  2 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 36 +++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index c4fb61a84e41..0a366db693c8 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -7,6 +7,7 @@
 #define BCM_VK_H
 
 #include 
+#include 
 #include 
 #include 
 
@@ -214,6 +215,7 @@ struct bcm_vk {
 
struct bcm_vk_dauth_info dauth_info;
 
+   struct miscdevice miscdev;
int devid; /* dev id allocated */
 
struct workqueue_struct *wq_thread;
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index adc3103c7012..4ecd5b5f80d3 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -7,6 +7,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -643,6 +644,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
char name[20];
struct bcm_vk *vk;
struct device *dev = >dev;
+   struct miscdevice *misc_device;
u32 boot_status;
 
vk = kzalloc(sizeof(*vk), GFP_KERNEL);
@@ -719,6 +721,19 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
 
vk->devid = id;
snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
+   misc_device = >miscdev;
+   misc_device->minor = MISC_DYNAMIC_MINOR;
+   misc_device->name = kstrdup(name, GFP_KERNEL);
+   if (!misc_device->name) {
+   err = -ENOMEM;
+   goto err_ida_remove;
+   }
+
+   err = misc_register(misc_device);
+   if (err) {
+   dev_err(dev, "failed to register device\n");
+   goto err_kfree_name;
+   }
 
INIT_WORK(>wq_work, bcm_vk_wq_handler);
 
@@ -727,7 +742,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
if (!vk->wq_thread) {
dev_err(dev, "Fail to create workqueue thread\n");
err = -ENOMEM;
-   goto err_ida_remove;
+   goto err_misc_deregister;
}
 
/* sync other info */
@@ -749,11 +764,20 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
}
 
+   dev_dbg(dev, "BCM-VK:%u created\n", id);
+
return 0;
 
 err_destroy_workqueue:
destroy_workqueue(vk->wq_thread);
 
+err_misc_deregister:
+   misc_deregister(misc_device);
+
+err_kfree_name:
+   kfree(misc_device->name);
+   misc_device->name = NULL;
+
 err_ida_remove:
ida_simple_remove(_vk_ida, id);
 
@@ -783,6 +807,7 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 {
int i;
struct bcm_vk *vk = pci_get_drvdata(pdev);
+   struct miscdevice *misc_device = >miscdev;
 
/*
 * Trigger a reset to card and wait enough time for UCODE to rerun,
@@ -797,6 +822,13 @@ static void bcm_vk_remove(struct pci_dev *pdev)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
  vk->tdma_vaddr, vk->tdma_addr);
 
+   /* remove if name is set which means misc dev registered */
+   if (misc_device->name) {
+   misc_deregister(misc_device);
+   kfree(misc_device->name);
+   ida_simple_remove(_vk_ida, vk->devid);
+   }
+
cancel_work_sync(>wq_work);
destroy_workqueue(vk->wq_thread);
 
@@ -805,6 +837,8 @@ static void bcm_vk_remove(struct pci_dev *pdev)
pci_iounmap(pdev, vk->bar[i]);
}
 
+   dev_dbg(>dev, "BCM-VK:%d released\n", vk->devid);
+
pci_release_regions(pdev);
pci_free_irq_vectors(pdev);
pci_disable_device(pdev);
-- 
2.17.1



[PATCH v8 05/13] misc: bcm-vk: add triggers when host panic or reboots to notify card

2020-11-30 Thread Scott Branden
Pass down an interrupt to card in case of panic or reboot so
that card can take appropriate action to perform a clean reset.
Uses kernel notifier block either directly (register on panic list),
or implicitly (add shutdown method for PCI device).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |  2 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 29 -
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 0a366db693c8..f428ad9a0c3d 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -223,6 +223,8 @@ struct bcm_vk {
unsigned long wq_offload[1]; /* various flags on wq requested */
void *tdma_vaddr; /* test dma segment virtual addr */
dma_addr_t tdma_addr; /* test dma segment bus addr */
+
+   struct notifier_block panic_nb;
 };
 
 /* wq offload work items bits definitions */
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 4ecd5b5f80d3..09d99bd36e8a 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -635,6 +635,16 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static int bcm_vk_on_panic(struct notifier_block *nb,
+  unsigned long e, void *p)
+{
+   struct bcm_vk *vk = container_of(nb, struct bcm_vk, panic_nb);
+
+   bcm_to_v_reset_doorbell(vk, VK_BAR0_RESET_DB_HARD);
+
+   return 0;
+}
+
 static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
int err;
@@ -748,6 +758,15 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
/* sync other info */
bcm_vk_sync_card_info(vk);
 
+   /* register for panic notifier */
+   vk->panic_nb.notifier_call = bcm_vk_on_panic;
+   err = atomic_notifier_chain_register(_notifier_list,
+>panic_nb);
+   if (err) {
+   dev_err(dev, "Fail to register panic notifier\n");
+   goto err_destroy_workqueue;
+   }
+
/*
 * lets trigger an auto download.  We don't want to do it serially here
 * because at probing time, it is not supposed to block for a long time.
@@ -756,7 +775,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
if (auto_load) {
if ((boot_status & BOOT_STATE_MASK) == BROM_RUNNING) {
if (bcm_vk_trigger_autoload(vk))
-   goto err_destroy_workqueue;
+   goto err_unregister_panic_notifier;
} else {
dev_err(dev,
"Auto-load skipped - BROM not in proper state 
(0x%x)\n",
@@ -768,6 +787,10 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
 
return 0;
 
+err_unregister_panic_notifier:
+   atomic_notifier_chain_unregister(_notifier_list,
+>panic_nb);
+
 err_destroy_workqueue:
destroy_workqueue(vk->wq_thread);
 
@@ -818,6 +841,10 @@ static void bcm_vk_remove(struct pci_dev *pdev)
bcm_vk_trigger_reset(vk);
usleep_range(BCM_VK_UCODE_BOOT_US, BCM_VK_UCODE_BOOT_MAX_US);
 
+   /* unregister panic notifier */
+   atomic_notifier_chain_unregister(_notifier_list,
+>panic_nb);
+
if (vk->tdma_vaddr)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
  vk->tdma_vaddr, vk->tdma_addr);
-- 
2.17.1



[PATCH v8 03/13] misc: bcm-vk: add autoload support

2020-11-30 Thread Scott Branden
Add support to load and boot images on card automatically.
The kernel module parameter auto_load can be passed in as false to disable
such support on probe.
As well, nr_scratch_pages can be specified to allocate more or less scratch
memory on init as needed for desired card operation.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Co-developed-by: James Hu 
Signed-off-by: James Hu 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h | 250 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 723 +++
 2 files changed, 973 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 9152785199ab..c4fb61a84e41 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -6,10 +6,187 @@
 #ifndef BCM_VK_H
 #define BCM_VK_H
 
+#include 
 #include 
+#include 
 
 #define DRV_MODULE_NAME"bcm-vk"
 
+/*
+ * Load Image is completed in two stages:
+ *
+ * 1) When the VK device boot-up, M7 CPU runs and executes the BootROM.
+ * The Secure Boot Loader (SBL) as part of the BootROM will run
+ * to open up ITCM for host to push BOOT1 image.
+ * SBL will authenticate the image before jumping to BOOT1 image.
+ *
+ * 2) Because BOOT1 image is a secured image, we also called it the
+ * Secure Boot Image (SBI). At second stage, SBI will initialize DDR
+ * and wait for host to push BOOT2 image to DDR.
+ * SBI will authenticate the image before jumping to BOOT2 image.
+ *
+ */
+/* Location of registers of interest in BAR0 */
+
+/* Request register for Secure Boot Loader (SBL) download */
+#define BAR_CODEPUSH_SBL   0x400
+/* Start of ITCM */
+#define CODEPUSH_BOOT1_ENTRY   0x0040
+#define CODEPUSH_MASK  0xf000
+#define CODEPUSH_BOOTSTART BIT(0)
+
+/* Boot Status register */
+#define BAR_BOOT_STATUS0x404
+
+#define SRAM_OPEN  BIT(16)
+#define DDR_OPEN   BIT(17)
+
+/* Firmware loader progress status definitions */
+#define FW_LOADER_ACK_SEND_MORE_DATA   BIT(18)
+#define FW_LOADER_ACK_IN_PROGRESS  BIT(19)
+#define FW_LOADER_ACK_RCVD_ALL_DATABIT(20)
+
+/* Boot1/2 is running in standalone mode */
+#define BOOT_STDALONE_RUNNING  BIT(21)
+
+/* definitions for boot status register */
+#define BOOT_STATE_MASK(0x & \
+~(FW_LOADER_ACK_SEND_MORE_DATA | \
+  FW_LOADER_ACK_IN_PROGRESS | \
+  BOOT_STDALONE_RUNNING))
+
+#define BOOT_ERR_SHIFT 4
+#define BOOT_ERR_MASK  (0xf << BOOT_ERR_SHIFT)
+#define BOOT_PROG_MASK 0xf
+
+#define BROM_STATUS_NOT_RUN0x2
+#define BROM_NOT_RUN   (SRAM_OPEN | BROM_STATUS_NOT_RUN)
+#define BROM_STATUS_COMPLETE   0x6
+#define BROM_RUNNING   (SRAM_OPEN | BROM_STATUS_COMPLETE)
+#define BOOT1_STATUS_COMPLETE  0x6
+#define BOOT1_RUNNING  (DDR_OPEN | BOOT1_STATUS_COMPLETE)
+#define BOOT2_STATUS_COMPLETE  0x6
+#define BOOT2_RUNNING  (FW_LOADER_ACK_RCVD_ALL_DATA | \
+BOOT2_STATUS_COMPLETE)
+
+/* Boot request for Secure Boot Image (SBI) */
+#define BAR_CODEPUSH_SBI   0x408
+/* 64M mapped to BAR2 */
+#define CODEPUSH_BOOT2_ENTRY   0x6000
+
+#define BAR_CARD_STATUS0x410
+
+#define BAR_BOOT1_STDALONE_PROGRESS0x420
+#define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
+#define BOOT1_STDALONE_PROGRESS_MASK   BOOT1_STDALONE_SUCCESS
+
+#define BAR_METADATA_VERSION   0x440
+#define BAR_OS_UPTIME  0x444
+#define BAR_CHIP_ID0x448
+#define MAJOR_SOC_REV(_chip_id)(((_chip_id) >> 20) & 0xf)
+
+#define BAR_CARD_TEMPERATURE   0x45c
+
+#define BAR_CARD_VOLTAGE   0x460
+
+#define BAR_CARD_ERR_LOG   0x464
+
+#define BAR_CARD_ERR_MEM   0x468
+
+#define BAR_CARD_PWR_AND_THRE  0x46c
+
+#define BAR_CARD_STATIC_INFO   0x470
+
+#define BAR_INTF_VER   0x47c
+#define BAR_INTF_VER_MAJOR_SHIFT   16
+#define BAR_INTF_VER_MASK  0x
+/*
+ * major and minor semantic version numbers supported
+ * Please update as required on interface changes
+ */
+#define SEMANTIC_MAJOR 1
+#define SEMANTIC_MINOR 0
+
+/*
+ * first door bell reg, ie for queue = 0.  Only need the first one, as
+ * we will use the queue number to derive the others
+ */
+#define VK_BAR0_REGSEG_DB_BASE 0x484
+#define VK_BAR0_REGSEG_DB_REG_GAP  8 /*
+  * DB register gap,
+  * DB1 at 0x48c and DB2 at 0x494
+  */
+
+

[PATCH v8 02/13] misc: bcm-vk: add Broadcom VK driver

2020-11-30 Thread Scott Branden
Add initial version of Broadcom VK driver to enumerate PCI device IDs
of Valkyrie and Viper device IDs.

VK based cards provide real-time high performance, high throughput,
low latency offload compute engine operations.
They are used for multiple parallel offload tasks as:
audio, video and image processing and crypto operations.

Further commits add additional features to driver beyond probe/remove.

Signed-off-by: Scott Branden 
---
 drivers/misc/Kconfig |   1 +
 drivers/misc/Makefile|   1 +
 drivers/misc/bcm-vk/Kconfig  |  17 
 drivers/misc/bcm-vk/Makefile |   8 ++
 drivers/misc/bcm-vk/bcm_vk.h |  29 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 141 +++
 6 files changed, 197 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index fafa8b0d8099..591903773a6d 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -478,6 +478,7 @@ source "drivers/misc/genwqe/Kconfig"
 source "drivers/misc/echo/Kconfig"
 source "drivers/misc/cxl/Kconfig"
 source "drivers/misc/ocxl/Kconfig"
+source "drivers/misc/bcm-vk/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
 source "drivers/misc/habanalabs/Kconfig"
 source "drivers/misc/uacce/Kconfig"
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index d23231e73330..54f2fe2d9448 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_ECHO)+= echo/
 obj-$(CONFIG_CXL_BASE) += cxl/
 obj-$(CONFIG_PCI_ENDPOINT_TEST)+= pci_endpoint_test.o
 obj-$(CONFIG_OCXL) += ocxl/
+obj-$(CONFIG_BCM_VK)   += bcm-vk/
 obj-y  += cardreader/
 obj-$(CONFIG_PVPANIC)  += pvpanic.o
 obj-$(CONFIG_HABANA_AI)+= habanalabs/
diff --git a/drivers/misc/bcm-vk/Kconfig b/drivers/misc/bcm-vk/Kconfig
new file mode 100644
index ..052f6f28b540
--- /dev/null
+++ b/drivers/misc/bcm-vk/Kconfig
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Broadcom VK device
+#
+config BCM_VK
+   tristate "Support for Broadcom VK Accelerators"
+   depends on PCI_MSI
+   help
+ Select this option to enable support for Broadcom
+ VK Accelerators.  VK is used for performing
+ multiple specific offload processing tasks in parallel.
+ Such offload tasks assist in such operations as video
+ transcoding, compression, and crypto tasks.
+ This driver enables userspace programs to access these
+ accelerators via /dev/bcm-vk.N devices.
+
+ If unsure, say N.
diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
new file mode 100644
index ..f8a7ac4c242f
--- /dev/null
+++ b/drivers/misc/bcm-vk/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Broadcom VK driver
+#
+
+obj-$(CONFIG_BCM_VK) += bcm_vk.o
+bcm_vk-objs := \
+   bcm_vk_dev.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
new file mode 100644
index ..9152785199ab
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#ifndef BCM_VK_H
+#define BCM_VK_H
+
+#include 
+
+#define DRV_MODULE_NAME"bcm-vk"
+
+/* VK device supports a maximum of 3 bars */
+#define MAX_BAR3
+
+enum pci_barno {
+   BAR_0 = 0,
+   BAR_1,
+   BAR_2
+};
+
+#define BCM_VK_NUM_TTY 2
+
+struct bcm_vk {
+   struct pci_dev *pdev;
+   void __iomem *bar[MAX_BAR];
+};
+
+#endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
new file mode 100644
index ..14afe2477b97
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include "bcm_vk.h"
+
+#define PCI_DEVICE_ID_VALKYRIE 0x5e87
+#define PCI_DEVICE_ID_VIPER0x5e88
+
+/* MSIX usages */
+#define VK_MSIX_MSGQ_MAX   3
+#define VK_MSIX_NOTF_MAX   1
+#define VK_MSIX_TTY_MAXBCM_VK_NUM_TTY
+#define VK_MSIX_IRQ_MAX(VK_MSIX_MSGQ_MAX + 
VK_MSIX_NOTF_MAX + \
+VK_MSIX_TTY_MAX)
+#define VK_MSIX_IRQ_MIN_REQ (VK_MSIX_MSGQ_MAX + VK_MSIX_NOTF_MAX)
+
+/* Number of bits set in DMA mask*/
+#define BCM_VK_DMA_BITS64
+
+static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{
+   int err;
+   int i;
+   int irq;
+   struct bcm_vk *vk;
+   struc

[PATCH v8 00/13] Add Broadcom VK driver

2020-11-30 Thread Scott Branden
This patch series drops previous patches in [1]
that were incorporated by Kees Cook into patch series
"Introduce partial kernel_read_file() support" [2].

Remaining patches are contained in this series to add Broadcom VK driver.
(which depends on request_firmware_into_buf API addition which has
now been accepted into the upstream kernel as of v5.10-rc1).

[1] 
https://lore.kernel.org/lkml/20200706232309.12010-1-scott.bran...@broadcom.com/
[2] https://lore.kernel.org/lkml/20201002173828.2099543-1-keesc...@chromium.org/

Changes from v7:
 - add more information in Kconfig help description
Changes from v6:
 - drop QSTATS patch as it needs to be reviewed if trace_printk makes sense
 - add wdog and IPC interface alerts
 - add boundary check to msgq and peerlog
 - clear additional registers on reset
Changes from v5:
 - dropped sysfs patch from series for now as rework to use hwmon
 - tty patch still at end of series to drop if another solution available
 - updated cover letter commit to point to Kees' latest patch submission in [2]
 - specified --base with Kees' patches applied (kernel branches don't have 
these yet)
 - removed trivial comment
 - moved location of const to before the struct in two declarations
 - changed dev_info to dev_warn and only print when irq don't match expected
 - changed dev_info to dev_dbg when printing debug QSTATS
 - removed unnecessary %p print
Changes from v4:
 - fixed memory leak in probe function on failure
 - changed -1 to -EBUSY in bcm_vk_tty return code
 - move bcm_vk_tty patch to end of patch series so it
   can be dropped from current patch series if needed
   and rearchitected if needed.
Changes from v3:
 - split driver into more incremental commits for acceptance/review
 - lowered some dev_info to dev_dbg
 - remove ANSI stdint types and replace with linux u8, etc types
 - changed an EIO return to EPFNOSUPPORT
 - move vk_msg_cmd internal to driver to not expose to UAPI at this time
Changes from v2:
 - open code BIT macro in uapi header
 - A0/B0 boot improvements
Changes from v1:
 - declare bcm_vk_intf_ver_chk as static

Scott Branden (13):
  bcm-vk: add bcm_vk UAPI
  misc: bcm-vk: add Broadcom VK driver
  misc: bcm-vk: add autoload support
  misc: bcm-vk: add misc device to Broadcom VK driver
  misc: bcm-vk: add triggers when host panic or reboots to notify card
  misc: bcm-vk: add open/release
  misc: bcm-vk: add ioctl load_image
  misc: bcm-vk: add get_card_info, peerlog_info, and proc_mon_info
  misc: bcm-vk: add VK messaging support
  misc: bcm-vk: reset_pid support
  misc: bcm-vk: add mmap function for exposing BAR2
  MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver
  misc: bcm-vk: add ttyVK support

 MAINTAINERS  |7 +
 drivers/misc/Kconfig |1 +
 drivers/misc/Makefile|1 +
 drivers/misc/bcm-vk/Kconfig  |   17 +
 drivers/misc/bcm-vk/Makefile |   12 +
 drivers/misc/bcm-vk/bcm_vk.h |  513 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 1651 ++
 drivers/misc/bcm-vk/bcm_vk_msg.c | 1350 
 drivers/misc/bcm-vk/bcm_vk_msg.h |  163 +++
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  275 +
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   61 ++
 drivers/misc/bcm-vk/bcm_vk_tty.c |  333 ++
 include/uapi/linux/misc/bcm_vk.h |   84 ++
 13 files changed, 4468 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c
 create mode 100644 include/uapi/linux/misc/bcm_vk.h

-- 
2.17.1



[PATCH v8 01/13] bcm-vk: add bcm_vk UAPI

2020-11-30 Thread Scott Branden
Add user space api for bcm-vk driver.

Provide ioctl api to load images and issue reset command to card.
FW status registers in PCI BAR space also defined as part
of API so that user space is able to interpret these memory locations
as needed via direct PCIe access.

Signed-off-by: Scott Branden 
---
 include/uapi/linux/misc/bcm_vk.h | 84 
 1 file changed, 84 insertions(+)
 create mode 100644 include/uapi/linux/misc/bcm_vk.h

diff --git a/include/uapi/linux/misc/bcm_vk.h b/include/uapi/linux/misc/bcm_vk.h
new file mode 100644
index ..ec28e0bd46a9
--- /dev/null
+++ b/include/uapi/linux/misc/bcm_vk.h
@@ -0,0 +1,84 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#ifndef __UAPI_LINUX_MISC_BCM_VK_H
+#define __UAPI_LINUX_MISC_BCM_VK_H
+
+#include 
+#include 
+
+#define BCM_VK_MAX_FILENAME 64
+
+struct vk_image {
+   __u32 type; /* Type of image */
+#define VK_IMAGE_TYPE_BOOT1 1 /* 1st stage (load to SRAM) */
+#define VK_IMAGE_TYPE_BOOT2 2 /* 2nd stage (load to DDR) */
+   __u8 filename[BCM_VK_MAX_FILENAME]; /* Filename of image */
+};
+
+struct vk_reset {
+   __u32 arg1;
+   __u32 arg2;
+};
+
+#define VK_MAGIC   0x5e
+
+/* Load image to Valkyrie */
+#define VK_IOCTL_LOAD_IMAGE_IOW(VK_MAGIC, 0x2, struct vk_image)
+
+/* Send Reset to Valkyrie */
+#define VK_IOCTL_RESET _IOW(VK_MAGIC, 0x4, struct vk_reset)
+
+/*
+ * Firmware Status accessed directly via BAR space
+ */
+#define VK_BAR_FWSTS   0x41c
+#define VK_BAR_COP_FWSTS   0x428
+/* VK_FWSTS definitions */
+#define VK_FWSTS_RELOCATION_ENTRY  (1UL << 0)
+#define VK_FWSTS_RELOCATION_EXIT   (1UL << 1)
+#define VK_FWSTS_INIT_START(1UL << 2)
+#define VK_FWSTS_ARCH_INIT_DONE(1UL << 3)
+#define VK_FWSTS_PRE_KNL1_INIT_DONE(1UL << 4)
+#define VK_FWSTS_PRE_KNL2_INIT_DONE(1UL << 5)
+#define VK_FWSTS_POST_KNL_INIT_DONE(1UL << 6)
+#define VK_FWSTS_INIT_DONE (1UL << 7)
+#define VK_FWSTS_APP_INIT_START(1UL << 8)
+#define VK_FWSTS_APP_INIT_DONE (1UL << 9)
+#define VK_FWSTS_MASK  0x
+#define VK_FWSTS_READY (VK_FWSTS_INIT_START | \
+VK_FWSTS_ARCH_INIT_DONE | \
+VK_FWSTS_PRE_KNL1_INIT_DONE | \
+VK_FWSTS_PRE_KNL2_INIT_DONE | \
+VK_FWSTS_POST_KNL_INIT_DONE | \
+VK_FWSTS_INIT_DONE | \
+VK_FWSTS_APP_INIT_START | \
+VK_FWSTS_APP_INIT_DONE)
+/* Deinit */
+#define VK_FWSTS_APP_DEINIT_START  (1UL << 23)
+#define VK_FWSTS_APP_DEINIT_DONE   (1UL << 24)
+#define VK_FWSTS_DRV_DEINIT_START  (1UL << 25)
+#define VK_FWSTS_DRV_DEINIT_DONE   (1UL << 26)
+#define VK_FWSTS_RESET_DONE(1UL << 27)
+#define VK_FWSTS_DEINIT_TRIGGERED  (VK_FWSTS_APP_DEINIT_START | \
+VK_FWSTS_APP_DEINIT_DONE  | \
+VK_FWSTS_DRV_DEINIT_START | \
+VK_FWSTS_DRV_DEINIT_DONE)
+/* Last nibble for reboot reason */
+#define VK_FWSTS_RESET_REASON_SHIFT28
+#define VK_FWSTS_RESET_REASON_MASK (0xf << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_SYS_PWRUP   (0x0 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_MBOX_DB (0x1 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_M7_WDOG (0x2 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_TEMP(0x3 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_FLR (0x4 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_HOT (0x5 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_WARM(0x6 << 
VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_COLD(0x7 << 
VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_L1  (0x8 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_L0  (0x9 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_UNKNOWN (0xf << VK_FWSTS_RESET_REASON_SHIFT)
+
+#endif /* __UAPI_LINUX_MISC_BCM_VK_H */
-- 
2.17.1



Re: [PATCH v3 0/3] PCI: iproc: Add fixes to pcie iproc

2020-11-30 Thread Scott Branden


On 2020-11-30 4:19 a.m., Lorenzo Pieralisi wrote:
> On Thu, Oct 01, 2020 at 11:30:51AM +0530, Srinath Mannam wrote:
>> This patch series contains fixes and improvements to pcie iproc driver.
>>
>> This patch set is based on Linux-5.9.0-rc2.
>>
>> Changes from v2:
>>   - Addressed Bjorn's review comments
>>  - Corrected subject line and commit message of Patches 1 and 2.
>>  
>> Changes from v1:
>>   - Addressed Bjorn's review comments
>>  - pcie_print_link_status is used to print Link information.
>>  - Added IARR1/IMAP1 window map definition.
>>
>> Bharat Gooty (1):
>>   PCI: iproc: Fix out-of-bound array accesses
>>
>> Roman Bacik (1):
>>   PCI: iproc: Invalidate correct PAXB inbound windows
>>
>> Srinath Mannam (1):
>>   PCI: iproc: Display PCIe Link information
>>
>>  drivers/pci/controller/pcie-iproc.c | 29 ++---
>>  1 file changed, 22 insertions(+), 7 deletions(-)
> I need Ray a/o Scott ACK to proceed.
Let's see if Ray has anything to add.

Acked-by: Scott Branden 
>
> Thanks,
> Lorenzo



smime.p7s
Description: S/MIME Cryptographic Signature


[PATCH] menuconfig,mconf-cfg: Allow specification of ncurses location

2020-11-27 Thread Scott Branden
From: Bruce Ashfield 

In some cross build environments such as the Yocto Project build
environment it provides an ncurses library that is compiled
differently than the host's version.  This causes display corruption
problems when the host's curses includes are used instead of the
includes from the provided compiler are overridden.  There is a second
case where there is no curses libraries at all on the host system and
menuconfig will just fail entirely.

The solution is simply to allow an override variable in
check-lxdialog.sh for environments such as the Yocto Project.  Adding
a CROSS_CURSES_LIB and CROSS_CURSES_INC solves the issue and allowing
compiling and linking against the right headers and libraries.

Signed-off-by: Jason Wessel 
cc: Michal Marek 
cc: linux-kbu...@vger.kernel.org
Signed-off-by: Bruce Ashfield 
Signed-off-by: Scott Branden 
---
 scripts/kconfig/mconf-cfg.sh | 8 
 1 file changed, 8 insertions(+)
 mode change 100755 => 100644 scripts/kconfig/mconf-cfg.sh

diff --git a/scripts/kconfig/mconf-cfg.sh b/scripts/kconfig/mconf-cfg.sh
old mode 100755
new mode 100644
index aa68ec95620d..32448bc198a5
--- a/scripts/kconfig/mconf-cfg.sh
+++ b/scripts/kconfig/mconf-cfg.sh
@@ -4,6 +4,14 @@
 PKG="ncursesw"
 PKG2="ncurses"
 
+if [ "$CROSS_CURSES_LIB" != "" ]; then
+echo libs=\'$CROSS_CURSES_LIB\'
+if [ x"$CROSS_CURSES_INC" != x ]; then
+   echo cflags=\'$CROSS_CURSES_INC\'
+fi
+exit 0
+fi
+
 if [ -n "$(command -v pkg-config)" ]; then
if pkg-config --exists $PKG; then
echo cflags=\"$(pkg-config --cflags $PKG)\"
-- 
2.17.1



Re: [PATCH v7 02/13] misc: bcm-vk: add Broadcom VK driver

2020-11-19 Thread Scott Branden
Hi Pavel,

On 2020-11-19 2:42 p.m., Pavel Machek wrote:
> Hi!
>
>> Add initial version of Broadcom VK driver to enumerate PCI device IDs
>> of Valkyrie and Viper device IDs.
>>
>> VK based cards provide real-time high performance, high throughput,
>> low latency offload compute engine operations.
>> They are used for multiple parallel offload tasks as:
>> audio, video and image processing and crypto operations.
>>
>> Further commits add additional features to driver beyond probe/remove.
>>
>> Signed-off-by: Scott Branden 
>> +++ b/drivers/misc/bcm-vk/Kconfig
>> @@ -0,0 +1,15 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +#
>> +# Broadcom VK device
>> +#
>> +config BCM_VK
>> +tristate "Support for Broadcom VK Accelerators"
>> +depends on PCI_MSI
>> +help
>> +  Select this option to enable support for Broadcom
>> +  VK Accelerators.  VK is used for performing
>> +  specific offload processing.
>> +  This driver enables userspace programs to access these
>> +  accelerators via /dev/bcm-vk.N devices.
>> +
>> +  If unsure, say N.
> Could we
>
> a) align the text the usual way
Please explain what "the usual way" is?
>
> b) explain what the VK is here? What kind of offloads does it do? What
> does "VK" mean?
Sure I can add some more details to the menu.
Offloads such as many parallel crypto, video, compression, and transcode 
operations are some such offloads.
VK has no real meaning, it is just short for Valkyrie, the first device in the 
"VK" family.
>
> c) explain where such accelerators might be found.
Such accelerators won't be found in any consumer devices but in high compute 
data center type locations.
>
> (Is remoteproc framework unusable for this?)
remoteproc has been discussed previously [1] and doesn't suite this application.

[1] 
https://lore.kernel.org/lkml/CAOesGMg6f2pK4ZZ8bz=0nmgcjg-8jhlze41przogsbhggs6...@mail.gmail.com/

>
> Thanks,
>   Pavel



smime.p7s
Description: S/MIME Cryptographic Signature


[PATCH v7 13/13] misc: bcm-vk: add ttyVK support

2020-11-17 Thread Scott Branden
Add ttyVK support to driver to allow console access to VK card from host.

Device node will be in the follow form /dev/bcm-vk.x_ttyVKy where:
x is the instance of the VK card
y is the tty device number on the VK card

Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/Makefile |   3 +-
 drivers/misc/bcm-vk/bcm_vk.h |  28 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c |  30 ++-
 drivers/misc/bcm-vk/bcm_vk_tty.c | 333 +++
 4 files changed, 392 insertions(+), 2 deletions(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index 79b4e365c9e6..e4a1486f7209 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -7,5 +7,6 @@ obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
bcm_vk_msg.o \
-   bcm_vk_sg.o
+   bcm_vk_sg.o \
+   bcm_vk_tty.o
 
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index a1d0bf6e694c..3f37c640a814 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -8,12 +8,14 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -84,6 +86,9 @@
 #define CODEPUSH_BOOT2_ENTRY   0x6000
 
 #define BAR_CARD_STATUS0x410
+/* CARD_STATUS definitions */
+#define CARD_STATUS_TTYVK0_READY   BIT(0)
+#define CARD_STATUS_TTYVK1_READY   BIT(1)
 
 #define BAR_BOOT1_STDALONE_PROGRESS0x420
 #define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
@@ -255,6 +260,19 @@ enum pci_barno {
 
 #define BCM_VK_NUM_TTY 2
 
+struct bcm_vk_tty {
+   struct tty_port port;
+   u32 to_offset;  /* bar offset to use */
+   u32 to_size;/* to VK buffer size */
+   u32 wr; /* write offset shadow */
+   u32 from_offset;/* bar offset to use */
+   u32 from_size;  /* from VK buffer size */
+   u32 rd; /* read offset shadow */
+   pid_t pid;
+   bool irq_enabled;
+   bool is_opened; /* tracks tty open/close */
+};
+
 /* VK device max power state, supports 3, full, reduced and low */
 #define MAX_OPP 3
 #define MAX_CARD_INFO_TAG_SIZE 64
@@ -348,6 +366,12 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+   struct tty_driver *tty_drv;
+   struct timer_list serial_timer;
+   struct bcm_vk_tty tty[BCM_VK_NUM_TTY];
+   struct workqueue_struct *tty_wq_thread;
+   struct work_struct tty_wq_work;
+
/* Reference-counting to handle file operations */
struct kref kref;
 
@@ -466,6 +490,7 @@ int bcm_vk_release(struct inode *inode, struct file 
*p_file);
 void bcm_vk_release_data(struct kref *kref);
 irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
+irqreturn_t bcm_vk_tty_irqhandler(int irq, void *dev_id);
 int bcm_vk_msg_init(struct bcm_vk *vk);
 void bcm_vk_msg_remove(struct bcm_vk *vk);
 void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
@@ -476,6 +501,9 @@ int bcm_vk_send_shutdown_msg(struct bcm_vk *vk, u32 
shut_type,
 const pid_t pid, const u32 q_num);
 void bcm_to_v_q_doorbell(struct bcm_vk *vk, u32 q_num, u32 db_val);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
+int bcm_vk_tty_init(struct bcm_vk *vk, char *name);
+void bcm_vk_tty_exit(struct bcm_vk *vk);
+void bcm_vk_tty_terminate_tty_user(struct bcm_vk *vk);
 void bcm_vk_hb_init(struct bcm_vk *vk);
 void bcm_vk_hb_deinit(struct bcm_vk *vk);
 void bcm_vk_handle_notf(struct bcm_vk *vk);
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index cac07419f041..c3d2bba68ef1 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -525,6 +525,7 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
}
}
}
+   bcm_vk_tty_terminate_tty_user(vk);
spin_unlock(>ctx_lock);
 }
 
@@ -1384,6 +1385,20 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
vk->num_irqs++;
 
+   for (i = 0;
+(i < VK_MSIX_TTY_MAX) && (vk->num_irqs < irq);
+i++, vk->num_irqs++) {
+   err = devm_request_irq(dev, pci_irq_vector(pdev, vk->num_irqs),
+  bcm_vk_tty_irqhandler,
+  IRQF_SHARED, DRV_MODULE_NAME, vk);
+   if (err) {
+   dev_err(dev, "failed request tty IRQ %d for MSIX %d\n",
+   pdev->irq + vk->num_irqs, vk->num_irqs + 1);
+   goto err_irq;
+   }
+   vk->tty[i].irq_enabled = true;
+   }
+
id = ida_simple_get(_vk_ida, 0, 0, GFP_KERNEL);
if (id < 0) {
 

[PATCH v7 12/13] MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver

2020-11-17 Thread Scott Branden
Add maintainer entry for new Broadcom VK Driver

Signed-off-by: Scott Branden 
---
 MAINTAINERS | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index e451dcce054f..f574946f2f56 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3716,6 +3716,13 @@ L:   net...@vger.kernel.org
 S: Supported
 F: drivers/net/ethernet/broadcom/tg3.*
 
+BROADCOM VK DRIVER
+M: Scott Branden 
+L: bcm-kernel-feedback-l...@broadcom.com
+S: Supported
+F: drivers/misc/bcm-vk/
+F: include/uapi/linux/misc/bcm_vk.h
+
 BROCADE BFA FC SCSI DRIVER
 M: Anil Gurumurthy 
 M: Sudarsana Kalluru 
-- 
2.17.1



[PATCH v7 10/13] misc: bcm-vk: reset_pid support

2020-11-17 Thread Scott Branden
Add reset support via ioctl.
Kill user processes that are open when VK card is reset.
If a particular PID has issued the reset request do not kill that process
as it issued the ioctl.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |   1 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 158 +--
 drivers/misc/bcm-vk/bcm_vk_msg.c |  40 +++-
 3 files changed, 191 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index d847a512d0ed..a1d0bf6e694c 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -468,6 +468,7 @@ irqreturn_t bcm_vk_msgq_irqhandler(int irq, void *dev_id);
 irqreturn_t bcm_vk_notf_irqhandler(int irq, void *dev_id);
 int bcm_vk_msg_init(struct bcm_vk *vk);
 void bcm_vk_msg_remove(struct bcm_vk *vk);
+void bcm_vk_drain_msg_on_reset(struct bcm_vk *vk);
 int bcm_vk_sync_msgq(struct bcm_vk *vk, bool force_sync);
 void bcm_vk_blk_drv_access(struct bcm_vk *vk);
 s32 bcm_to_h_msg_dequeue(struct bcm_vk *vk);
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 5d82f02c0f27..e572a7b18fab 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -504,7 +504,9 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
int i;
 
/*
-* kill all the apps
+* kill all the apps except for the process that is resetting.
+* If not called during reset, reset_pid will be 0, and all will be
+* killed.
 */
spin_lock(>ctx_lock);
 
@@ -515,10 +517,12 @@ void bcm_vk_blk_drv_access(struct bcm_vk *vk)
struct bcm_vk_ctx *ctx;
 
list_for_each_entry(ctx, >pid_ht[i].head, node) {
-   dev_dbg(>pdev->dev,
-   "Send kill signal to pid %d\n",
-   ctx->pid);
-   kill_pid(find_vpid(ctx->pid), SIGKILL, 1);
+   if (ctx->pid != vk->reset_pid) {
+   dev_dbg(>pdev->dev,
+   "Send kill signal to pid %d\n",
+   ctx->pid);
+   kill_pid(find_vpid(ctx->pid), SIGKILL, 1);
+   }
}
}
spin_unlock(>ctx_lock);
@@ -1001,6 +1005,49 @@ static long bcm_vk_load_image(struct bcm_vk *vk,
return ret;
 }
 
+static int bcm_vk_reset_successful(struct bcm_vk *vk)
+{
+   struct device *dev = >pdev->dev;
+   u32 fw_status, reset_reason;
+   int ret = -EAGAIN;
+
+   /*
+* Reset could be triggered when the card in several state:
+*   i)   in bootROM
+*   ii)  after boot1
+*   iii) boot2 running
+*
+* i) & ii) - no status bits will be updated.  If vkboot1
+* runs automatically after reset, it  will update the reason
+* to be unknown reason
+* iii) - reboot reason match + deinit done.
+*/
+   fw_status = vkread32(vk, BAR_0, VK_BAR_FWSTS);
+   /* immediate exit if interface goes down */
+   if (BCM_VK_INTF_IS_DOWN(fw_status)) {
+   dev_err(dev, "PCIe Intf Down!\n");
+   goto reset_exit;
+   }
+
+   reset_reason = (fw_status & VK_FWSTS_RESET_REASON_MASK);
+   if ((reset_reason == VK_FWSTS_RESET_MBOX_DB) ||
+   (reset_reason == VK_FWSTS_RESET_UNKNOWN))
+   ret = 0;
+
+   /*
+* if some of the deinit bits are set, but done
+* bit is not, this is a failure if triggered while boot2 is running
+*/
+   if ((fw_status & VK_FWSTS_DEINIT_TRIGGERED) &&
+   !(fw_status & VK_FWSTS_RESET_DONE))
+   ret = -EAGAIN;
+
+reset_exit:
+   dev_dbg(dev, "FW status = 0x%x ret %d\n", fw_status, ret);
+
+   return ret;
+}
+
 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val)
 {
vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE);
@@ -1010,12 +1057,16 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
 {
u32 i;
u32 value, boot_status;
+   bool is_stdalone, is_boot2;
static const u32 bar0_reg_clr_list[] = { BAR_OS_UPTIME,
 BAR_INTF_VER,
 BAR_CARD_VOLTAGE,
 BAR_CARD_TEMPERATURE,
 BAR_CARD_PWR_AND_THRE };
 
+   /* clean up before pressing the door bell */
+   bcm_vk_drain_msg_on_reset(vk);
+   vkwrite32(vk, 0, BAR_1, VK_BAR1_MSGQ_DEF_RDY);
/* make tag '\0' terminated */
vkwrite32(vk, 0, BAR_1, VK_BAR1_BOOT1_VER_TAG);
 
@@ -1026,6 +1077,11 @@ static int bcm_vk_trigger_reset(s

[PATCH v7 11/13] misc: bcm-vk: add mmap function for exposing BAR2

2020-11-17 Thread Scott Branden
Add mmap function that allows host application to open up BAR2 memory
for remote spooling out messages from the VK logger.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk_dev.c | 24 
 1 file changed, 24 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index e572a7b18fab..cac07419f041 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -1199,6 +1199,29 @@ static long bcm_vk_reset(struct bcm_vk *vk, struct 
vk_reset __user *arg)
return ret;
 }
 
+static int bcm_vk_mmap(struct file *file, struct vm_area_struct *vma)
+{
+   struct bcm_vk_ctx *ctx = file->private_data;
+   struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
+   unsigned long pg_size;
+
+   /* only BAR2 is mmap possible, which is bar num 4 due to 64bit */
+#define VK_MMAPABLE_BAR 4
+
+   pg_size = ((pci_resource_len(vk->pdev, VK_MMAPABLE_BAR) - 1)
+   >> PAGE_SHIFT) + 1;
+   if (vma->vm_pgoff + vma_pages(vma) > pg_size)
+   return -EINVAL;
+
+   vma->vm_pgoff += (pci_resource_start(vk->pdev, VK_MMAPABLE_BAR)
+ >> PAGE_SHIFT);
+   vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+   return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+ vma->vm_end - vma->vm_start,
+ vma->vm_page_prot);
+}
+
 static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
 {
long ret = -EINVAL;
@@ -1237,6 +1260,7 @@ static const struct file_operations bcm_vk_fops = {
.write = bcm_vk_write,
.poll = bcm_vk_poll,
.release = bcm_vk_release,
+   .mmap = bcm_vk_mmap,
.unlocked_ioctl = bcm_vk_ioctl,
 };
 
-- 
2.17.1



[PATCH v7 09/13] misc: bcm-vk: add VK messaging support

2020-11-17 Thread Scott Branden
Add message support in order to be able to communicate
to VK card via message queues.

This info is used for debug purposes via collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/Makefile |3 +-
 drivers/misc/bcm-vk/bcm_vk.h |  123 
 drivers/misc/bcm-vk/bcm_vk_dev.c |  309 +++-
 drivers/misc/bcm-vk/bcm_vk_msg.c | 1187 ++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  132 
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  275 +++
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   61 ++
 7 files changed, 2087 insertions(+), 3 deletions(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index a2ae79858409..79b4e365c9e6 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -6,5 +6,6 @@
 obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
bcm_vk_dev.o \
-   bcm_vk_msg.o
+   bcm_vk_msg.o \
+   bcm_vk_sg.o
 
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 50f2a0cd6e13..d847a512d0ed 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -6,11 +6,13 @@
 #ifndef BCM_VK_H
 #define BCM_VK_H
 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -93,14 +95,53 @@
 #define MAJOR_SOC_REV(_chip_id)(((_chip_id) >> 20) & 0xf)
 
 #define BAR_CARD_TEMPERATURE   0x45c
+/* defines for all temperature sensor */
+#define BCM_VK_TEMP_FIELD_MASK 0xff
+#define BCM_VK_CPU_TEMP_SHIFT  0
+#define BCM_VK_DDR0_TEMP_SHIFT 8
+#define BCM_VK_DDR1_TEMP_SHIFT 16
 
 #define BAR_CARD_VOLTAGE   0x460
+/* defines for voltage rail conversion */
+#define BCM_VK_VOLT_RAIL_MASK  0x
+#define BCM_VK_3P3_VOLT_REG_SHIFT  16
 
 #define BAR_CARD_ERR_LOG   0x464
+/* Error log register bit definition - register for error alerts */
+#define ERR_LOG_UECC   BIT(0)
+#define ERR_LOG_SSIM_BUSY  BIT(1)
+#define ERR_LOG_AFBC_BUSY  BIT(2)
+#define ERR_LOG_HIGH_TEMP_ERR  BIT(3)
+#define ERR_LOG_WDOG_TIMEOUT   BIT(4)
+#define ERR_LOG_SYS_FAULT  BIT(5)
+#define ERR_LOG_RAMDUMPBIT(6)
+#define ERR_LOG_COP_WDOG_TIMEOUT   BIT(7)
+/* warnings */
+#define ERR_LOG_MEM_ALLOC_FAIL BIT(8)
+#define ERR_LOG_LOW_TEMP_WARN  BIT(9)
+#define ERR_LOG_ECCBIT(10)
+#define ERR_LOG_IPC_DWNBIT(11)
+
+/* Alert bit definitions detectd on host */
+#define ERR_LOG_HOST_INTF_V_FAIL   BIT(13)
+#define ERR_LOG_HOST_HB_FAIL   BIT(14)
+#define ERR_LOG_HOST_PCIE_DWN  BIT(15)
 
 #define BAR_CARD_ERR_MEM   0x468
+/* defines for mem err, all fields have same width */
+#define BCM_VK_MEM_ERR_FIELD_MASK  0xff
+#define BCM_VK_ECC_MEM_ERR_SHIFT   0
+#define BCM_VK_UECC_MEM_ERR_SHIFT  8
+/* threshold of event occurrence and logs start to come out */
+#define BCM_VK_ECC_THRESHOLD   10
+#define BCM_VK_UECC_THRESHOLD  1
 
 #define BAR_CARD_PWR_AND_THRE  0x46c
+/* defines for power and temp threshold, all fields have same width */
+#define BCM_VK_PWR_AND_THRE_FIELD_MASK 0xff
+#define BCM_VK_LOW_TEMP_THRE_SHIFT 0
+#define BCM_VK_HIGH_TEMP_THRE_SHIFT8
+#define BCM_VK_PWR_STATE_SHIFT 16
 
 #define BAR_CARD_STATIC_INFO   0x470
 
@@ -143,6 +184,11 @@
 #define BAR_FIRMWARE_TAG_SIZE  50
 #define FIRMWARE_STATUS_PRE_INIT_DONE  0x1f
 
+/* VK MSG_ID defines */
+#define VK_MSG_ID_BITMAP_SIZE  4096
+#define VK_MSG_ID_BITMAP_MASK  (VK_MSG_ID_BITMAP_SIZE - 1)
+#define VK_MSG_ID_OVERFLOW 0x
+
 /*
  * BAR1
  */
@@ -197,6 +243,10 @@
 /* VK device supports a maximum of 3 bars */
 #define MAX_BAR3
 
+/* default number of msg blk for inband SGL */
+#define BCM_VK_DEF_IB_SGL_BLK_LEN   16
+#define BCM_VK_IB_SGL_BLK_MAX   24
+
 enum pci_barno {
BAR_0 = 0,
BAR_1,
@@ -267,9 +317,27 @@ struct bcm_vk_proc_mon_info {
struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
 };
 
+struct bcm_vk_hb_ctrl {
+   struct timer_list timer;
+   u32 last_uptime;
+   u32 lost_cnt;
+};
+
+struct bcm_vk_alert {
+   u16 flags;
+   u16 notfs;
+};
+
+/* some alert counters that the driver will keep track */
+struct bcm_vk_alert_cnts {
+   u16 ecc;
+   u16 uecc;
+};
+
 struct bcm_vk {
struct pci_dev *pdev;
void __iomem *bar[MAX_BAR];
+   int num_irqs;
 
struct bcm_vk_card_info card_info;
struct bcm_vk_proc_mon_info proc_mon_info;
@@ -283,9 +351,17 @@ struct bcm_vk {
/* Reference-counting to h

[PATCH v7 08/13] misc: bcm-vk: add get_card_info, peerlog_info, and proc_mon_info

2020-11-17 Thread Scott Branden
Add support to get card_info (details about card),
peerlog_info (to get details of peerlog on card),
and proc_mon_info (process monitoring on card).

This info is used for collection of logs via direct
read of BAR space and by sysfs access (in a follow on commit).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |  60 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 105 +++
 2 files changed, 165 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 726aab71bb6b..50f2a0cd6e13 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -205,6 +205,21 @@ enum pci_barno {
 
 #define BCM_VK_NUM_TTY 2
 
+/* VK device max power state, supports 3, full, reduced and low */
+#define MAX_OPP 3
+#define MAX_CARD_INFO_TAG_SIZE 64
+
+struct bcm_vk_card_info {
+   u32 version;
+   char os_tag[MAX_CARD_INFO_TAG_SIZE];
+   char cmpt_tag[MAX_CARD_INFO_TAG_SIZE];
+   u32 cpu_freq_mhz;
+   u32 cpu_scale[MAX_OPP];
+   u32 ddr_freq_mhz;
+   u32 ddr_size_MB;
+   u32 video_core_freq_mhz;
+};
+
 /* DAUTH related info */
 struct bcm_vk_dauth_key {
char store[VK_BAR1_DAUTH_STORE_SIZE];
@@ -215,10 +230,49 @@ struct bcm_vk_dauth_info {
struct bcm_vk_dauth_key keys[VK_BAR1_DAUTH_MAX];
 };
 
+/*
+ * Control structure of logging messages from the card.  This
+ * buffer is for logmsg that comes from vk
+ */
+struct bcm_vk_peer_log {
+   u32 rd_idx;
+   u32 wr_idx;
+   u32 buf_size;
+   u32 mask;
+   char data[0];
+};
+
+/* max buf size allowed */
+#define BCM_VK_PEER_LOG_BUF_MAX SZ_16K
+/* max size per line of peer log */
+#define BCM_VK_PEER_LOG_LINE_MAX  256
+
+/*
+ * single entry for processing type + utilization
+ */
+#define BCM_VK_PROC_TYPE_TAG_LEN 8
+struct bcm_vk_proc_mon_entry_t {
+   char tag[BCM_VK_PROC_TYPE_TAG_LEN];
+   u32 used;
+   u32 max; /**< max capacity */
+};
+
+/**
+ * Structure for run time utilization
+ */
+#define BCM_VK_PROC_MON_MAX 8 /* max entries supported */
+struct bcm_vk_proc_mon_info {
+   u32 num; /**< no of entries */
+   u32 entry_size; /**< per entry size */
+   struct bcm_vk_proc_mon_entry_t entries[BCM_VK_PROC_MON_MAX];
+};
+
 struct bcm_vk {
struct pci_dev *pdev;
void __iomem *bar[MAX_BAR];
 
+   struct bcm_vk_card_info card_info;
+   struct bcm_vk_proc_mon_info proc_mon_info;
struct bcm_vk_dauth_info dauth_info;
 
/* mutex to protect the ioctls */
@@ -240,6 +294,12 @@ struct bcm_vk {
dma_addr_t tdma_addr; /* test dma segment bus addr */
 
struct notifier_block panic_nb;
+
+   /* offset of the peer log control in BAR2 */
+   u32 peerlog_off;
+   struct bcm_vk_peer_log peerlog_info; /* record of peer log info */
+   /* offset of processing monitoring info in BAR2 */
+   u32 proc_mon_off;
 };
 
 /* wq offload work items bits definitions */
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 203a1cf2bae3..a63208513c64 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -172,6 +172,104 @@ static inline int bcm_vk_wait(struct bcm_vk *vk, enum 
pci_barno bar,
return 0;
 }
 
+static void bcm_vk_get_card_info(struct bcm_vk *vk)
+{
+   struct device *dev = >pdev->dev;
+   u32 offset;
+   int i;
+   u8 *dst;
+   struct bcm_vk_card_info *info = >card_info;
+
+   /* first read the offset from spare register */
+   offset = vkread32(vk, BAR_0, BAR_CARD_STATIC_INFO);
+   offset &= (pci_resource_len(vk->pdev, BAR_2 * 2) - 1);
+
+   /* based on the offset, read info to internal card info structure */
+   dst = (u8 *)info;
+   for (i = 0; i < sizeof(*info); i++)
+   *dst++ = vkread8(vk, BAR_2, offset++);
+
+#define CARD_INFO_LOG_FMT "version   : %x\n" \
+ "os_tag: %s\n" \
+ "cmpt_tag  : %s\n" \
+ "cpu_freq  : %d MHz\n" \
+ "cpu_scale : %d full, %d lowest\n" \
+ "ddr_freq  : %d MHz\n" \
+ "ddr_size  : %d MB\n" \
+ "video_freq: %d MHz\n"
+   dev_dbg(dev, CARD_INFO_LOG_FMT, info->version, info->os_tag,
+   info->cmpt_tag, info->cpu_freq_mhz, info->cpu_scale[0],
+   info->cpu_scale[MAX_OPP - 1], info->ddr_freq_mhz,
+   info->ddr_size_MB, info->video_core_freq_mhz);
+
+   /*
+* get the peer log pointer, only need the offset, and get record
+* of the log buffer information which would be used for checking
+* before dump, in case the BAR2 memory has been corrupted.
+*/
+

[PATCH v7 07/13] misc: bcm-vk: add ioctl load_image

2020-11-17 Thread Scott Branden
Add ioctl support to issue load_image operation to VK card.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Co-developed-by: James Hu 
Signed-off-by: James Hu 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |  3 +
 drivers/misc/bcm-vk/bcm_vk_dev.c | 95 
 2 files changed, 98 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 5f0fcfdaf265..726aab71bb6b 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include "bcm_vk_msg.h"
@@ -220,6 +221,8 @@ struct bcm_vk {
 
struct bcm_vk_dauth_info dauth_info;
 
+   /* mutex to protect the ioctls */
+   struct mutex mutex;
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 79fffb1e6f84..203a1cf2bae3 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -580,6 +581,71 @@ static void bcm_vk_wq_handler(struct work_struct *work)
}
 }
 
+static long bcm_vk_load_image(struct bcm_vk *vk,
+ const struct vk_image __user *arg)
+{
+   struct device *dev = >pdev->dev;
+   const char *image_name;
+   struct vk_image image;
+   u32 next_loadable;
+   enum soc_idx idx;
+   int image_idx;
+   int ret = -EPERM;
+
+   if (copy_from_user(, arg, sizeof(image)))
+   return -EACCES;
+
+   if ((image.type != VK_IMAGE_TYPE_BOOT1) &&
+   (image.type != VK_IMAGE_TYPE_BOOT2)) {
+   dev_err(dev, "invalid image.type %u\n", image.type);
+   return ret;
+   }
+
+   next_loadable = bcm_vk_next_boot_image(vk);
+   if (next_loadable != image.type) {
+   dev_err(dev, "Next expected image %u, Loading %u\n",
+   next_loadable, image.type);
+   return ret;
+   }
+
+   /*
+* if something is pending download already.  This could only happen
+* for now when the driver is being loaded, or if someone has issued
+* another download command in another shell.
+*/
+   if (test_and_set_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload) != 0) {
+   dev_err(dev, "Download operation already pending.\n");
+   return ret;
+   }
+
+   image_name = image.filename;
+   if (image_name[0] == '\0') {
+   /* Use default image name if NULL */
+   idx = get_soc_idx(vk);
+   if (idx == VK_IDX_INVALID)
+   goto err_idx;
+
+   /* Image idx starts with boot1 */
+   image_idx = image.type - VK_IMAGE_TYPE_BOOT1;
+   image_name = get_load_fw_name(vk, _tab[idx][image_idx]);
+   if (!image_name) {
+   dev_err(dev, "No suitable image found for type %d",
+   image.type);
+   ret = -ENOENT;
+   goto err_idx;
+   }
+   } else {
+   /* Ensure filename is NULL terminated */
+   image.filename[sizeof(image.filename) - 1] = '\0';
+   }
+   ret = bcm_vk_load_image_by_type(vk, image.type, image_name);
+   dev_info(dev, "Load %s, ret %d\n", image_name, ret);
+err_idx:
+   clear_bit(BCM_VK_WQ_DWNLD_PEND, vk->wq_offload);
+
+   return ret;
+}
+
 static void bcm_to_v_reset_doorbell(struct bcm_vk *vk, u32 db_val)
 {
vkwrite32(vk, db_val, BAR_0, VK_BAR0_RESET_DB_BASE);
@@ -636,10 +702,38 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static long bcm_vk_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
+{
+   long ret = -EINVAL;
+   struct bcm_vk_ctx *ctx = file->private_data;
+   struct bcm_vk *vk = container_of(ctx->miscdev, struct bcm_vk, miscdev);
+   void __user *argp = (void __user *)arg;
+
+   dev_dbg(>pdev->dev,
+   "ioctl, cmd=0x%02x, arg=0x%02lx\n",
+   cmd, arg);
+
+   mutex_lock(>mutex);
+
+   switch (cmd) {
+   case VK_IOCTL_LOAD_IMAGE:
+   ret = bcm_vk_load_image(vk, argp);
+   break;
+
+   default:
+   break;
+   }
+
+   mutex_unlock(>mutex);
+
+   return ret;
+}
+
 static const struct file_operations bcm_vk_fops = {
.owner = THIS_MODULE,
.open = bcm_vk_open,
.release = bcm_vk_release,
+   .unlocked_ioctl = bcm_vk_ioctl,
 };
 
 static int bcm_vk_on_panic(struct notifier_block *nb,
@@ -670,6 +764,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
re

[PATCH v7 06/13] misc: bcm-vk: add open/release

2020-11-17 Thread Scott Branden
Add open/release to replace private data with context for other methods
to use.  Reason for the context is because it is allowed for multiple
sessions to open sysfs.  For each file open, when upper layer queries the
response, only those that are tied to a specified open should be returned.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/Makefile |   4 +-
 drivers/misc/bcm-vk/bcm_vk.h |  15 
 drivers/misc/bcm-vk/bcm_vk_dev.c |  23 ++
 drivers/misc/bcm-vk/bcm_vk_msg.c | 127 +++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  31 
 5 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h

diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
index f8a7ac4c242f..a2ae79858409 100644
--- a/drivers/misc/bcm-vk/Makefile
+++ b/drivers/misc/bcm-vk/Makefile
@@ -5,4 +5,6 @@
 
 obj-$(CONFIG_BCM_VK) += bcm_vk.o
 bcm_vk-objs := \
-   bcm_vk_dev.o
+   bcm_vk_dev.o \
+   bcm_vk_msg.o
+
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index f428ad9a0c3d..5f0fcfdaf265 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -7,9 +7,14 @@
 #define BCM_VK_H
 
 #include 
+#include 
 #include 
+#include 
 #include 
 #include 
+#include 
+
+#include "bcm_vk_msg.h"
 
 #define DRV_MODULE_NAME"bcm-vk"
 
@@ -218,6 +223,13 @@ struct bcm_vk {
struct miscdevice miscdev;
int devid; /* dev id allocated */
 
+   /* Reference-counting to handle file operations */
+   struct kref kref;
+
+   spinlock_t ctx_lock; /* Spinlock for component context */
+   struct bcm_vk_ctx ctx[VK_CMPT_CTX_MAX];
+   struct bcm_vk_ht_entry pid_ht[VK_PID_HT_SZ];
+
struct workqueue_struct *wq_thread;
struct work_struct wq_work; /* work queue for deferred job */
unsigned long wq_offload[1]; /* various flags on wq requested */
@@ -278,6 +290,9 @@ static inline bool bcm_vk_msgq_marker_valid(struct bcm_vk 
*vk)
return (rdy_marker == VK_BAR1_MSGQ_RDY_MARKER);
 }
 
+int bcm_vk_open(struct inode *inode, struct file *p_file);
+int bcm_vk_release(struct inode *inode, struct file *p_file);
+void bcm_vk_release_data(struct kref *kref);
 int bcm_vk_auto_load_all_images(struct bcm_vk *vk);
 
 #endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 09d99bd36e8a..79fffb1e6f84 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -8,6 +8,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -635,6 +636,12 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static const struct file_operations bcm_vk_fops = {
+   .owner = THIS_MODULE,
+   .open = bcm_vk_open,
+   .release = bcm_vk_release,
+};
+
 static int bcm_vk_on_panic(struct notifier_block *nb,
   unsigned long e, void *p)
 {
@@ -657,10 +664,13 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
struct miscdevice *misc_device;
u32 boot_status;
 
+   /* allocate vk structure which is tied to kref for freeing */
vk = kzalloc(sizeof(*vk), GFP_KERNEL);
if (!vk)
return -ENOMEM;
 
+   kref_init(>kref);
+
err = pci_enable_device(pdev);
if (err) {
dev_err(dev, "Cannot enable PCI device\n");
@@ -738,6 +748,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
err = -ENOMEM;
goto err_ida_remove;
}
+   misc_device->fops = _vk_fops,
 
err = misc_register(misc_device);
if (err) {
@@ -826,6 +837,16 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
return err;
 }
 
+void bcm_vk_release_data(struct kref *kref)
+{
+   struct bcm_vk *vk = container_of(kref, struct bcm_vk, kref);
+   struct pci_dev *pdev = vk->pdev;
+
+   dev_dbg(>dev, "BCM-VK:%d release data 0x%p\n", vk->devid, vk);
+   pci_dev_put(pdev);
+   kfree(vk);
+}
+
 static void bcm_vk_remove(struct pci_dev *pdev)
 {
int i;
@@ -869,6 +890,8 @@ static void bcm_vk_remove(struct pci_dev *pdev)
pci_release_regions(pdev);
pci_free_irq_vectors(pdev);
pci_disable_device(pdev);
+
+   kref_put(>kref, bcm_vk_release_data);
 }
 
 static void bcm_vk_shutdown(struct pci_dev *pdev)
diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c b/drivers/misc/bcm-vk/bcm_vk_msg.c
new file mode 100644
index ..2d9a6b4e5f61
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk_msg.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#include "bcm_vk.h"
+#include "b

[PATCH v7 05/13] misc: bcm-vk: add triggers when host panic or reboots to notify card

2020-11-17 Thread Scott Branden
Pass down an interrupt to card in case of panic or reboot so
that card can take appropriate action to perform a clean reset.
Uses kernel notifier block either directly (register on panic list),
or implicitly (add shutdown method for PCI device).

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |  2 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 29 -
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 0a366db693c8..f428ad9a0c3d 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -223,6 +223,8 @@ struct bcm_vk {
unsigned long wq_offload[1]; /* various flags on wq requested */
void *tdma_vaddr; /* test dma segment virtual addr */
dma_addr_t tdma_addr; /* test dma segment bus addr */
+
+   struct notifier_block panic_nb;
 };
 
 /* wq offload work items bits definitions */
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index 4ecd5b5f80d3..09d99bd36e8a 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -635,6 +635,16 @@ static int bcm_vk_trigger_reset(struct bcm_vk *vk)
return 0;
 }
 
+static int bcm_vk_on_panic(struct notifier_block *nb,
+  unsigned long e, void *p)
+{
+   struct bcm_vk *vk = container_of(nb, struct bcm_vk, panic_nb);
+
+   bcm_to_v_reset_doorbell(vk, VK_BAR0_RESET_DB_HARD);
+
+   return 0;
+}
+
 static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
int err;
@@ -748,6 +758,15 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
/* sync other info */
bcm_vk_sync_card_info(vk);
 
+   /* register for panic notifier */
+   vk->panic_nb.notifier_call = bcm_vk_on_panic;
+   err = atomic_notifier_chain_register(_notifier_list,
+>panic_nb);
+   if (err) {
+   dev_err(dev, "Fail to register panic notifier\n");
+   goto err_destroy_workqueue;
+   }
+
/*
 * lets trigger an auto download.  We don't want to do it serially here
 * because at probing time, it is not supposed to block for a long time.
@@ -756,7 +775,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
if (auto_load) {
if ((boot_status & BOOT_STATE_MASK) == BROM_RUNNING) {
if (bcm_vk_trigger_autoload(vk))
-   goto err_destroy_workqueue;
+   goto err_unregister_panic_notifier;
} else {
dev_err(dev,
"Auto-load skipped - BROM not in proper state 
(0x%x)\n",
@@ -768,6 +787,10 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
 
return 0;
 
+err_unregister_panic_notifier:
+   atomic_notifier_chain_unregister(_notifier_list,
+>panic_nb);
+
 err_destroy_workqueue:
destroy_workqueue(vk->wq_thread);
 
@@ -818,6 +841,10 @@ static void bcm_vk_remove(struct pci_dev *pdev)
bcm_vk_trigger_reset(vk);
usleep_range(BCM_VK_UCODE_BOOT_US, BCM_VK_UCODE_BOOT_MAX_US);
 
+   /* unregister panic notifier */
+   atomic_notifier_chain_unregister(_notifier_list,
+>panic_nb);
+
if (vk->tdma_vaddr)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
  vk->tdma_vaddr, vk->tdma_addr);
-- 
2.17.1



[PATCH v7 04/13] misc: bcm-vk: add misc device to Broadcom VK driver

2020-11-17 Thread Scott Branden
Add misc device base support to create and remove devnode.
Additional misc functions for open/read/write/release/ioctl/sysfs, etc
will be added in follow on commits to allow for individual review.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h |  2 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 36 +++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index c4fb61a84e41..0a366db693c8 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -7,6 +7,7 @@
 #define BCM_VK_H
 
 #include 
+#include 
 #include 
 #include 
 
@@ -214,6 +215,7 @@ struct bcm_vk {
 
struct bcm_vk_dauth_info dauth_info;
 
+   struct miscdevice miscdev;
int devid; /* dev id allocated */
 
struct workqueue_struct *wq_thread;
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
index adc3103c7012..4ecd5b5f80d3 100644
--- a/drivers/misc/bcm-vk/bcm_vk_dev.c
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -7,6 +7,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -643,6 +644,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
char name[20];
struct bcm_vk *vk;
struct device *dev = >dev;
+   struct miscdevice *misc_device;
u32 boot_status;
 
vk = kzalloc(sizeof(*vk), GFP_KERNEL);
@@ -719,6 +721,19 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
 
vk->devid = id;
snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
+   misc_device = >miscdev;
+   misc_device->minor = MISC_DYNAMIC_MINOR;
+   misc_device->name = kstrdup(name, GFP_KERNEL);
+   if (!misc_device->name) {
+   err = -ENOMEM;
+   goto err_ida_remove;
+   }
+
+   err = misc_register(misc_device);
+   if (err) {
+   dev_err(dev, "failed to register device\n");
+   goto err_kfree_name;
+   }
 
INIT_WORK(>wq_work, bcm_vk_wq_handler);
 
@@ -727,7 +742,7 @@ static int bcm_vk_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
if (!vk->wq_thread) {
dev_err(dev, "Fail to create workqueue thread\n");
err = -ENOMEM;
-   goto err_ida_remove;
+   goto err_misc_deregister;
}
 
/* sync other info */
@@ -749,11 +764,20 @@ static int bcm_vk_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
}
}
 
+   dev_dbg(dev, "BCM-VK:%u created\n", id);
+
return 0;
 
 err_destroy_workqueue:
destroy_workqueue(vk->wq_thread);
 
+err_misc_deregister:
+   misc_deregister(misc_device);
+
+err_kfree_name:
+   kfree(misc_device->name);
+   misc_device->name = NULL;
+
 err_ida_remove:
ida_simple_remove(_vk_ida, id);
 
@@ -783,6 +807,7 @@ static void bcm_vk_remove(struct pci_dev *pdev)
 {
int i;
struct bcm_vk *vk = pci_get_drvdata(pdev);
+   struct miscdevice *misc_device = >miscdev;
 
/*
 * Trigger a reset to card and wait enough time for UCODE to rerun,
@@ -797,6 +822,13 @@ static void bcm_vk_remove(struct pci_dev *pdev)
dma_free_coherent(>dev, nr_scratch_pages * PAGE_SIZE,
  vk->tdma_vaddr, vk->tdma_addr);
 
+   /* remove if name is set which means misc dev registered */
+   if (misc_device->name) {
+   misc_deregister(misc_device);
+   kfree(misc_device->name);
+   ida_simple_remove(_vk_ida, vk->devid);
+   }
+
cancel_work_sync(>wq_work);
destroy_workqueue(vk->wq_thread);
 
@@ -805,6 +837,8 @@ static void bcm_vk_remove(struct pci_dev *pdev)
pci_iounmap(pdev, vk->bar[i]);
}
 
+   dev_dbg(>dev, "BCM-VK:%d released\n", vk->devid);
+
pci_release_regions(pdev);
pci_free_irq_vectors(pdev);
pci_disable_device(pdev);
-- 
2.17.1



[PATCH v7 03/13] misc: bcm-vk: add autoload support

2020-11-17 Thread Scott Branden
Add support to load and boot images on card automatically.
The kernel module parameter auto_load can be passed in as false to disable
such support on probe.
As well, nr_scratch_pages can be specified to allocate more or less scratch
memory on init as needed for desired card operation.

Co-developed-by: Desmond Yan 
Signed-off-by: Desmond Yan 
Co-developed-by: James Hu 
Signed-off-by: James Hu 
Signed-off-by: Scott Branden 
---
 drivers/misc/bcm-vk/bcm_vk.h | 250 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 723 +++
 2 files changed, 973 insertions(+)

diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
index 9152785199ab..c4fb61a84e41 100644
--- a/drivers/misc/bcm-vk/bcm_vk.h
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -6,10 +6,187 @@
 #ifndef BCM_VK_H
 #define BCM_VK_H
 
+#include 
 #include 
+#include 
 
 #define DRV_MODULE_NAME"bcm-vk"
 
+/*
+ * Load Image is completed in two stages:
+ *
+ * 1) When the VK device boot-up, M7 CPU runs and executes the BootROM.
+ * The Secure Boot Loader (SBL) as part of the BootROM will run
+ * to open up ITCM for host to push BOOT1 image.
+ * SBL will authenticate the image before jumping to BOOT1 image.
+ *
+ * 2) Because BOOT1 image is a secured image, we also called it the
+ * Secure Boot Image (SBI). At second stage, SBI will initialize DDR
+ * and wait for host to push BOOT2 image to DDR.
+ * SBI will authenticate the image before jumping to BOOT2 image.
+ *
+ */
+/* Location of registers of interest in BAR0 */
+
+/* Request register for Secure Boot Loader (SBL) download */
+#define BAR_CODEPUSH_SBL   0x400
+/* Start of ITCM */
+#define CODEPUSH_BOOT1_ENTRY   0x0040
+#define CODEPUSH_MASK  0xf000
+#define CODEPUSH_BOOTSTART BIT(0)
+
+/* Boot Status register */
+#define BAR_BOOT_STATUS0x404
+
+#define SRAM_OPEN  BIT(16)
+#define DDR_OPEN   BIT(17)
+
+/* Firmware loader progress status definitions */
+#define FW_LOADER_ACK_SEND_MORE_DATA   BIT(18)
+#define FW_LOADER_ACK_IN_PROGRESS  BIT(19)
+#define FW_LOADER_ACK_RCVD_ALL_DATABIT(20)
+
+/* Boot1/2 is running in standalone mode */
+#define BOOT_STDALONE_RUNNING  BIT(21)
+
+/* definitions for boot status register */
+#define BOOT_STATE_MASK(0x & \
+~(FW_LOADER_ACK_SEND_MORE_DATA | \
+  FW_LOADER_ACK_IN_PROGRESS | \
+  BOOT_STDALONE_RUNNING))
+
+#define BOOT_ERR_SHIFT 4
+#define BOOT_ERR_MASK  (0xf << BOOT_ERR_SHIFT)
+#define BOOT_PROG_MASK 0xf
+
+#define BROM_STATUS_NOT_RUN0x2
+#define BROM_NOT_RUN   (SRAM_OPEN | BROM_STATUS_NOT_RUN)
+#define BROM_STATUS_COMPLETE   0x6
+#define BROM_RUNNING   (SRAM_OPEN | BROM_STATUS_COMPLETE)
+#define BOOT1_STATUS_COMPLETE  0x6
+#define BOOT1_RUNNING  (DDR_OPEN | BOOT1_STATUS_COMPLETE)
+#define BOOT2_STATUS_COMPLETE  0x6
+#define BOOT2_RUNNING  (FW_LOADER_ACK_RCVD_ALL_DATA | \
+BOOT2_STATUS_COMPLETE)
+
+/* Boot request for Secure Boot Image (SBI) */
+#define BAR_CODEPUSH_SBI   0x408
+/* 64M mapped to BAR2 */
+#define CODEPUSH_BOOT2_ENTRY   0x6000
+
+#define BAR_CARD_STATUS0x410
+
+#define BAR_BOOT1_STDALONE_PROGRESS0x420
+#define BOOT1_STDALONE_SUCCESS (BIT(13) | BIT(14))
+#define BOOT1_STDALONE_PROGRESS_MASK   BOOT1_STDALONE_SUCCESS
+
+#define BAR_METADATA_VERSION   0x440
+#define BAR_OS_UPTIME  0x444
+#define BAR_CHIP_ID0x448
+#define MAJOR_SOC_REV(_chip_id)(((_chip_id) >> 20) & 0xf)
+
+#define BAR_CARD_TEMPERATURE   0x45c
+
+#define BAR_CARD_VOLTAGE   0x460
+
+#define BAR_CARD_ERR_LOG   0x464
+
+#define BAR_CARD_ERR_MEM   0x468
+
+#define BAR_CARD_PWR_AND_THRE  0x46c
+
+#define BAR_CARD_STATIC_INFO   0x470
+
+#define BAR_INTF_VER   0x47c
+#define BAR_INTF_VER_MAJOR_SHIFT   16
+#define BAR_INTF_VER_MASK  0x
+/*
+ * major and minor semantic version numbers supported
+ * Please update as required on interface changes
+ */
+#define SEMANTIC_MAJOR 1
+#define SEMANTIC_MINOR 0
+
+/*
+ * first door bell reg, ie for queue = 0.  Only need the first one, as
+ * we will use the queue number to derive the others
+ */
+#define VK_BAR0_REGSEG_DB_BASE 0x484
+#define VK_BAR0_REGSEG_DB_REG_GAP  8 /*
+  * DB register gap,
+  * DB1 at 0x48c and DB2 at 0x494
+  */
+
+

[PATCH v7 02/13] misc: bcm-vk: add Broadcom VK driver

2020-11-17 Thread Scott Branden
Add initial version of Broadcom VK driver to enumerate PCI device IDs
of Valkyrie and Viper device IDs.

VK based cards provide real-time high performance, high throughput,
low latency offload compute engine operations.
They are used for multiple parallel offload tasks as:
audio, video and image processing and crypto operations.

Further commits add additional features to driver beyond probe/remove.

Signed-off-by: Scott Branden 
---
 drivers/misc/Kconfig |   1 +
 drivers/misc/Makefile|   1 +
 drivers/misc/bcm-vk/Kconfig  |  15 
 drivers/misc/bcm-vk/Makefile |   8 ++
 drivers/misc/bcm-vk/bcm_vk.h |  29 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 141 +++
 6 files changed, 195 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index fafa8b0d8099..591903773a6d 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -478,6 +478,7 @@ source "drivers/misc/genwqe/Kconfig"
 source "drivers/misc/echo/Kconfig"
 source "drivers/misc/cxl/Kconfig"
 source "drivers/misc/ocxl/Kconfig"
+source "drivers/misc/bcm-vk/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
 source "drivers/misc/habanalabs/Kconfig"
 source "drivers/misc/uacce/Kconfig"
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index d23231e73330..54f2fe2d9448 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_ECHO)+= echo/
 obj-$(CONFIG_CXL_BASE) += cxl/
 obj-$(CONFIG_PCI_ENDPOINT_TEST)+= pci_endpoint_test.o
 obj-$(CONFIG_OCXL) += ocxl/
+obj-$(CONFIG_BCM_VK)   += bcm-vk/
 obj-y  += cardreader/
 obj-$(CONFIG_PVPANIC)  += pvpanic.o
 obj-$(CONFIG_HABANA_AI)+= habanalabs/
diff --git a/drivers/misc/bcm-vk/Kconfig b/drivers/misc/bcm-vk/Kconfig
new file mode 100644
index ..2272e47655ed
--- /dev/null
+++ b/drivers/misc/bcm-vk/Kconfig
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Broadcom VK device
+#
+config BCM_VK
+   tristate "Support for Broadcom VK Accelerators"
+   depends on PCI_MSI
+   help
+ Select this option to enable support for Broadcom
+ VK Accelerators.  VK is used for performing
+ specific offload processing.
+ This driver enables userspace programs to access these
+ accelerators via /dev/bcm-vk.N devices.
+
+ If unsure, say N.
diff --git a/drivers/misc/bcm-vk/Makefile b/drivers/misc/bcm-vk/Makefile
new file mode 100644
index ..f8a7ac4c242f
--- /dev/null
+++ b/drivers/misc/bcm-vk/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Broadcom VK driver
+#
+
+obj-$(CONFIG_BCM_VK) += bcm_vk.o
+bcm_vk-objs := \
+   bcm_vk_dev.o
diff --git a/drivers/misc/bcm-vk/bcm_vk.h b/drivers/misc/bcm-vk/bcm_vk.h
new file mode 100644
index ..9152785199ab
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#ifndef BCM_VK_H
+#define BCM_VK_H
+
+#include 
+
+#define DRV_MODULE_NAME"bcm-vk"
+
+/* VK device supports a maximum of 3 bars */
+#define MAX_BAR3
+
+enum pci_barno {
+   BAR_0 = 0,
+   BAR_1,
+   BAR_2
+};
+
+#define BCM_VK_NUM_TTY 2
+
+struct bcm_vk {
+   struct pci_dev *pdev;
+   void __iomem *bar[MAX_BAR];
+};
+
+#endif
diff --git a/drivers/misc/bcm-vk/bcm_vk_dev.c b/drivers/misc/bcm-vk/bcm_vk_dev.c
new file mode 100644
index ..14afe2477b97
--- /dev/null
+++ b/drivers/misc/bcm-vk/bcm_vk_dev.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include "bcm_vk.h"
+
+#define PCI_DEVICE_ID_VALKYRIE 0x5e87
+#define PCI_DEVICE_ID_VIPER0x5e88
+
+/* MSIX usages */
+#define VK_MSIX_MSGQ_MAX   3
+#define VK_MSIX_NOTF_MAX   1
+#define VK_MSIX_TTY_MAXBCM_VK_NUM_TTY
+#define VK_MSIX_IRQ_MAX(VK_MSIX_MSGQ_MAX + 
VK_MSIX_NOTF_MAX + \
+VK_MSIX_TTY_MAX)
+#define VK_MSIX_IRQ_MIN_REQ (VK_MSIX_MSGQ_MAX + VK_MSIX_NOTF_MAX)
+
+/* Number of bits set in DMA mask*/
+#define BCM_VK_DMA_BITS64
+
+static int bcm_vk_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{
+   int err;
+   int i;
+   int irq;
+   struct bcm_vk *vk;
+   struct device *dev = >dev;
+
+   vk = kzalloc(sizeof(*vk), GFP_KERNEL);
+   if (!vk)
+   return -ENOMEM;
+
+

[PATCH v7 01/13] bcm-vk: add bcm_vk UAPI

2020-11-17 Thread Scott Branden
Add user space api for bcm-vk driver.

Provide ioctl api to load images and issue reset command to card.
FW status registers in PCI BAR space also defined as part
of API so that user space is able to interpret these memory locations
as needed via direct PCIe access.

Signed-off-by: Scott Branden 
---
 include/uapi/linux/misc/bcm_vk.h | 84 
 1 file changed, 84 insertions(+)
 create mode 100644 include/uapi/linux/misc/bcm_vk.h

diff --git a/include/uapi/linux/misc/bcm_vk.h b/include/uapi/linux/misc/bcm_vk.h
new file mode 100644
index ..ec28e0bd46a9
--- /dev/null
+++ b/include/uapi/linux/misc/bcm_vk.h
@@ -0,0 +1,84 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright 2018-2020 Broadcom.
+ */
+
+#ifndef __UAPI_LINUX_MISC_BCM_VK_H
+#define __UAPI_LINUX_MISC_BCM_VK_H
+
+#include 
+#include 
+
+#define BCM_VK_MAX_FILENAME 64
+
+struct vk_image {
+   __u32 type; /* Type of image */
+#define VK_IMAGE_TYPE_BOOT1 1 /* 1st stage (load to SRAM) */
+#define VK_IMAGE_TYPE_BOOT2 2 /* 2nd stage (load to DDR) */
+   __u8 filename[BCM_VK_MAX_FILENAME]; /* Filename of image */
+};
+
+struct vk_reset {
+   __u32 arg1;
+   __u32 arg2;
+};
+
+#define VK_MAGIC   0x5e
+
+/* Load image to Valkyrie */
+#define VK_IOCTL_LOAD_IMAGE_IOW(VK_MAGIC, 0x2, struct vk_image)
+
+/* Send Reset to Valkyrie */
+#define VK_IOCTL_RESET _IOW(VK_MAGIC, 0x4, struct vk_reset)
+
+/*
+ * Firmware Status accessed directly via BAR space
+ */
+#define VK_BAR_FWSTS   0x41c
+#define VK_BAR_COP_FWSTS   0x428
+/* VK_FWSTS definitions */
+#define VK_FWSTS_RELOCATION_ENTRY  (1UL << 0)
+#define VK_FWSTS_RELOCATION_EXIT   (1UL << 1)
+#define VK_FWSTS_INIT_START(1UL << 2)
+#define VK_FWSTS_ARCH_INIT_DONE(1UL << 3)
+#define VK_FWSTS_PRE_KNL1_INIT_DONE(1UL << 4)
+#define VK_FWSTS_PRE_KNL2_INIT_DONE(1UL << 5)
+#define VK_FWSTS_POST_KNL_INIT_DONE(1UL << 6)
+#define VK_FWSTS_INIT_DONE (1UL << 7)
+#define VK_FWSTS_APP_INIT_START(1UL << 8)
+#define VK_FWSTS_APP_INIT_DONE (1UL << 9)
+#define VK_FWSTS_MASK  0x
+#define VK_FWSTS_READY (VK_FWSTS_INIT_START | \
+VK_FWSTS_ARCH_INIT_DONE | \
+VK_FWSTS_PRE_KNL1_INIT_DONE | \
+VK_FWSTS_PRE_KNL2_INIT_DONE | \
+VK_FWSTS_POST_KNL_INIT_DONE | \
+VK_FWSTS_INIT_DONE | \
+VK_FWSTS_APP_INIT_START | \
+VK_FWSTS_APP_INIT_DONE)
+/* Deinit */
+#define VK_FWSTS_APP_DEINIT_START  (1UL << 23)
+#define VK_FWSTS_APP_DEINIT_DONE   (1UL << 24)
+#define VK_FWSTS_DRV_DEINIT_START  (1UL << 25)
+#define VK_FWSTS_DRV_DEINIT_DONE   (1UL << 26)
+#define VK_FWSTS_RESET_DONE(1UL << 27)
+#define VK_FWSTS_DEINIT_TRIGGERED  (VK_FWSTS_APP_DEINIT_START | \
+VK_FWSTS_APP_DEINIT_DONE  | \
+VK_FWSTS_DRV_DEINIT_START | \
+VK_FWSTS_DRV_DEINIT_DONE)
+/* Last nibble for reboot reason */
+#define VK_FWSTS_RESET_REASON_SHIFT28
+#define VK_FWSTS_RESET_REASON_MASK (0xf << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_SYS_PWRUP   (0x0 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_MBOX_DB (0x1 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_M7_WDOG (0x2 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_TEMP(0x3 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_FLR (0x4 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_HOT (0x5 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_WARM(0x6 << 
VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_PCI_COLD(0x7 << 
VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_L1  (0x8 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_L0  (0x9 << VK_FWSTS_RESET_REASON_SHIFT)
+#define VK_FWSTS_RESET_UNKNOWN (0xf << VK_FWSTS_RESET_REASON_SHIFT)
+
+#endif /* __UAPI_LINUX_MISC_BCM_VK_H */
-- 
2.17.1



[PATCH v7 00/13] Add Broadcom VK driver

2020-11-17 Thread Scott Branden
This patch series drops previous patches in [1]
that were incorporated by Kees Cook into patch series
"Introduce partial kernel_read_file() support" [2].

Remaining patches are contained in this series to add Broadcom VK driver.
(which depends on request_firmware_into_buf API addition which has
now been accepted into the upstream kernel as of v5.10-rc1).

[1] 
https://lore.kernel.org/lkml/20200706232309.12010-1-scott.bran...@broadcom.com/
[2] https://lore.kernel.org/lkml/20201002173828.2099543-1-keesc...@chromium.org/

Changes from v6:
 - drop QSTATS patch as it needs to be reviewed if trace_printk makes sense
 - add wdog and IPC interface alerts
 - add boundary check to msgq and peerlog
 - clear additional registers on reset
Changes from v5:
 - dropped sysfs patch from series for now as rework to use hwmon
 - tty patch still at end of series to drop if another solution available
 - updated cover letter commit to point to Kees' latest patch submission in [2]
 - specified --base with Kees' patches applied (kernel branches don't have 
these yet)
 - removed trivial comment
 - moved location of const to before the struct in two declarations
 - changed dev_info to dev_warn and only print when irq don't match expected
 - changed dev_info to dev_dbg when printing debug QSTATS
 - removed unnecessary %p print
Changes from v4:
 - fixed memory leak in probe function on failure
 - changed -1 to -EBUSY in bcm_vk_tty return code
 - move bcm_vk_tty patch to end of patch series so it
   can be dropped from current patch series if needed
   and rearchitected if needed.
Changes from v3:
 - split driver into more incremental commits for acceptance/review
 - lowered some dev_info to dev_dbg
 - remove ANSI stdint types and replace with linux u8, etc types
 - changed an EIO return to EPFNOSUPPORT
 - move vk_msg_cmd internal to driver to not expose to UAPI at this time
Changes from v2:
 - open code BIT macro in uapi header
 - A0/B0 boot improvements
Changes from v1:
 - declare bcm_vk_intf_ver_chk as static

Scott Branden (13):
  bcm-vk: add bcm_vk UAPI
  misc: bcm-vk: add Broadcom VK driver
  misc: bcm-vk: add autoload support
  misc: bcm-vk: add misc device to Broadcom VK driver
  misc: bcm-vk: add triggers when host panic or reboots to notify card
  misc: bcm-vk: add open/release
  misc: bcm-vk: add ioctl load_image
  misc: bcm-vk: add get_card_info, peerlog_info, and proc_mon_info
  misc: bcm-vk: add VK messaging support
  misc: bcm-vk: reset_pid support
  misc: bcm-vk: add mmap function for exposing BAR2
  MAINTAINERS: bcm-vk: add maintainer for Broadcom VK Driver
  misc: bcm-vk: add ttyVK support

 MAINTAINERS  |7 +
 drivers/misc/Kconfig |1 +
 drivers/misc/Makefile|1 +
 drivers/misc/bcm-vk/Kconfig  |   15 +
 drivers/misc/bcm-vk/Makefile |   12 +
 drivers/misc/bcm-vk/bcm_vk.h |  513 ++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 1651 ++
 drivers/misc/bcm-vk/bcm_vk_msg.c | 1350 
 drivers/misc/bcm-vk/bcm_vk_msg.h |  163 +++
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  275 +
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   61 ++
 drivers/misc/bcm-vk/bcm_vk_tty.c |  333 ++
 include/uapi/linux/misc/bcm_vk.h |   84 ++
 13 files changed, 4466 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_tty.c
 create mode 100644 include/uapi/linux/misc/bcm_vk.h

-- 
2.17.1



  1   2   3   4   5   6   7   8   9   10   >