spmi-pmic-arb: support configurable number of peripherals
Subsystems (3)
PATCH 1/1 spmi-pmic-arb: support configurable number of peripherals
The current driver implementation supports only 128 peripherals. Adding support for configurable number of peripherals since the spmi-pmic-arb v2 HW has sub-versions which support from 128 to 512 PMIC peripherals.
--- .../bindings/spmi/qcom,spmi-pmic-arb.txt | 34 ++++++++- drivers/spmi/spmi-pmic-arb.c | 88 ++++++++++++++-------- 2 files changed, 91 insertions(+), 31 deletions(-)
Documentation/devicetree/bindings/spmi/qcom,spmi-pmic-arb.txt +33 -1
@@ -42,7 +42,11 @@ Required properties: cell 4: interrupt flags indicating level-sense information, as defined in dt-bindings/interrupt-controller/irq.h -Example: +Optional properties: +- qcom,max-peripherals : number of PMIC peripherals (same as maximum APID) + supported by HW. Default (minimum supported) is 128. + +Example V1 PMIC-Arbiter: spmi { compatible = "qcom,spmi-pmic-arb";
@@ -62,4 +66,32 @@ Example: interrupt-controller; #interrupt-cells = <4>; + + qcom,max-peripherals = <256>; + }; + +Example V2 PMIC-Arbiter: + + spmi_bus: qcom,spmi@200f000 { + compatible = "qcom,spmi-pmic-arb"; + reg-names = "core", "chnls", "obsrvr", "intr", "cnfg"; + reg = <0x200f000 0x1000>, + <0x2400000 0x400000>, + <0x2c00000 0x400000>, + <0x3800000 0x200000>, + <0x200a000 0x2100>; + + interrupt-names = "periph_irq"; + interrupts = <0 190 0>; + + qcom,ee = <0>; + qcom,channel = <0>; + + #address-cells = <2>; + #size-cells = <0>; + + interrupt-controller; + #interrupt-cells = <4>; + + qcom,max-peripherals = <256>; };
drivers/spmi/spmi-pmic-arb.c +58 -30
173 lines not on this page · raw message
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
Documentation/devicetree/bindings/spmi/qcom,spmi-pmic-arb.txt Example: · line 70
Quoted 12 lines · Gilad Avidov
Documentation/devicetree/bindings/spmi/qcom,spmi-pmic-arb.txt +5
-0+ supported by HW. Default (minimum supported) is 128. + +Example V1 PMIC-Arbiter: spmi { compatible = "qcom,spmi-pmic-arb";@@ -62,4 +66,32 @@ Example: interrupt-controller; #interrupt-cells = <4>; + + qcom,max-peripherals = <256>;
If it's v1 isn't it always 128? So having 256 here is just confusing.
Quoted 7 lines
drivers/spmi/spmi-pmic-arb.c +2
-1+ }; +@@ -129,14 +131,15 @@ struct spmi_pmic_arb_dev {u8 channel;int irq;u8 ee; -u8 min_apid;
drivers/spmi/spmi-pmic-arb.c struct spmi_pmic_arb_dev { · line 140
Quoted 10 lines
drivers/spmi/spmi-pmic-arb.c +5
-3-u8 max_apid; -u32 mapping_table[SPMI_MAPPING_TABLE_LEN ]; +u16 min_irq_apid; +u16 max_irq_apid; +u16 max_apid; +u32 *mapping_table;struct irq_domain *domain;struct spmi_controller *spmic; -u16 apid_to_ppid[256 ]; +u16 *irq_apid_to_ppid;
Please drop all this renaming noise, or at the least, put it in a different patch. More than half the patch is just changing the names of these variables for what seems like no reason.
Quoted 31 lines
drivers/spmi/spmi-pmic-arb.c +1
-1const struct pmic_arb_ver_ops *ver_ops; -u8 *ppid_to_chan; +u16 *ppid_to_chan; };struct spmi_pmic_arb_dev *pa =irq_get_handler_data (irq);drivers/spmi/spmi-pmic-arb.c +11
-5struct irq_chip *chip =irq_get_chip (irq);void __iomem *intr = pa->intr; -int first = pa->min_apid >>5 ; -int last = pa->max_apid >>5 ; +int first = pa->min_irq_apid >>5 ; +int last = pa->max_irq_apid >>5 ;u32 status;int i, id;@@ -903,14 +915,30 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) pa->ee = ee; -for (i =0 ; i <ARRAY_SIZE (pa->mapping_table); ++i) - pa->mapping_table[i] =readl_relaxed ( - pa->cnfg +SPMI_MAPPING_TABLE_REG (i)); + pa->irq_apid_to_ppid =devm_kzalloc (&ctrl->dev, pa->max_apid * +sizeof (*pa->irq_apid_to_ppid), +GFP_KERNEL ); +if (!pa->irq_apid_to_ppid) { + err = -ENOMEM ; + goto err_put_ctrl; + } + + pa->mapping_table = devm_kzalloc(&ctrl->dev,
drivers/spmi/spmi-pmic-arb.c static int spmi_pmic_arb_probe(struct platform_device *pdev) · line 936
Quoted 10 lines
drivers/spmi/spmi-pmic-arb.c +10
-0+ (pa->max_apid -1 ) *sizeof (u32), + GFP_KERNEL); +if (!pa->mapping_table) { + err = -ENOMEM ; + goto err_put_ctrl; + } + +for (i =0 ; i < (pa->max_apid -1 ); ++i) + pa->mapping_table[i] =readl_relaxed (pa->cnfg + +SPMI_MAPPING_TABLE_REG (i));
Maybe we should stop doing this during probe and always allocate an empty cache of size 128 on v1 and 512 on v2 chips? So when we're searching through the mapping table we can cache the value from the register if the entry isn't 0. This delays the processing to when we're mapping irqs, hopefully speeding up probe for the case where you have a handful of irqs to map.
The DT property wouldn't be necessary then. Arguably it's being added there to optimize the size of the mapping table and isn't really necessary otherwise.
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
Quoted 9 lines · On Thu, 3 Sep 2015 17:16:30 -0700 Stephen Boyd…
Quoted 8 lines · Gilad Avidov
(quoted diff) +1
-0+ supported by HW. Default (minimumsupported) is 128. +
Documentation/devicetree/bindings/spmi/qcom,spmi-pmic-arb.txt +1
-0+Example V1 PMIC-Arbiter: spmi { compatible = "qcom,spmi-pmic-arb";@@ -62,4 +66,32 @@ Example:
Quoted 7 lines
Documentation/devicetree/bindings/spmi/qcom,spmi-pmic-arb.txt Example: · line 70
Quoted 4 lines
Documentation/devicetree/bindings/spmi/qcom,spmi-pmic-arb.txt +2
-0interrupt-controller; #interrupt-cells = <4>; + + qcom,max-peripherals = <256>;If it's v1 isn't it always 128? So having 256 here is just confusing.
Hi Stephen,
Actually some v1 chipsets, such as Dragonboard APQ8074, support 256 peripherals.
Quoted 15 lines
Quoted 15 lines
drivers/spmi/spmi-pmic-arb.c +6
-3+ }; +@@ -129,14 +131,15 @@ struct spmi_pmic_arb_dev {u8 channel;int irq;u8 ee; -u8 min_apid; -u8 max_apid; -u32 mapping_table[SPMI_MAPPING_TABLE_LEN ]; +u16 min_irq_apid; +u16 max_irq_apid; +u16 max_apid; +u32 *mapping_table;
Quoted 9 lines
drivers/spmi/spmi-pmic-arb.c struct spmi_pmic_arb_dev { · line 140
Quoted 4 lines
drivers/spmi/spmi-pmic-arb.c +1
-1struct irq_domain *domain;struct spmi_controller *spmic; -u16 apid_to_ppid[256 ]; +u16 *irq_apid_to_ppid;Please drop all this renaming noise, or at the least, put it in a different patch. More than half the patch is just changing the names of these variables for what seems like no reason.
I agree that changing apid_to_ppid to irq_apid_to_ppid is not required and I'll undo this change on next patch.
Regarding the change of max/min_apid to max/min_irq_apid:max_apid was already used but the name does not make good sense. Since really it is not the max_apid supported. Instead it is the largest apid which interrupt is currently requested for. But now we need a value that is actually the maximum supported apid. This is why repurposed max_apid and corrected the previous naming.
Quoted 41 lines
Quoted 41 lines
drivers/spmi/spmi-pmic-arb.c +1
-1const struct pmic_arb_ver_ops *ver_ops; -u8 *ppid_to_chan; +u16 *ppid_to_chan; };drivers/spmi/spmi-pmic-arb.c +3
-5struct spmi_pmic_arb_dev *pa =irq_get_handler_data (irq);struct irq_chip *chip =irq_get_chip (irq);void __iomem *intr = pa->intr; -int first = pa->min_apid >>5 ; -int last = pa->max_apid >>5 ; +int first = pa->min_irq_apid >>5 ; +int last = pa->max_irq_apid >>5 ;u32 status;int i, id;@@ -903,14 +915,30 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) pa->ee = ee; -for (i =0 ; i <ARRAY_SIZE (pa->mapping_table); ++i) - pa->mapping_table[i] =readl_relaxed ( - pa->cnfg + SPMI_MAPPING_TABLE_REG(i)); + pa->irq_apid_to_ppid = devm_kzalloc(&ctrl->dev,
pa->max_apid*
+sizeof(*pa->irq_apid_to_ppid),drivers/spmi/spmi-pmic-arb.c +8
-0+ GFP_KERNEL); +if (!pa->irq_apid_to_ppid) { + err = -ENOMEM ; + goto err_put_ctrl; + } + + pa->mapping_table = devm_kzalloc(&ctrl->dev, + (pa->max_apid - 1) *sizeof(u32),
drivers/spmi/spmi-pmic-arb.c +5
-0+ GFP_KERNEL); +if (!pa->mapping_table) { + err = -ENOMEM ; + goto err_put_ctrl; + }
Quoted 8 lines
Quoted 5 lines
drivers/spmi/spmi-pmic-arb.c +4
-0+ +for (i =0 ; i < (pa->max_apid -1 ); ++i) + pa->mapping_table[i] = readl_relaxed(pa->cnfg + +
SPMI_MAPPING_TABLE_REG(i));Maybe we should stop doing this during probe and always allocate an empty cache of size 128 on v1 and 512 on v2 chips? So when
As mentioned above, both v1 and v2 can have different number of peripherals.
Quoted 2 lines
we're searching through the mapping table we can cache the value from the register if the entry isn't 0. This delays the
Greedy approach (reading upto 512 registers from hw) have very small upfront penalty since sequential reads take place very fast and HW acceleration (prefetching) will improve it. A lazy approach will required much more complex algorithm (for example any value including zero is valid. So we will need to augment a marker for unassigned entries) and prefetching will not help.
Quoted 2 lines
processing to when we're mapping irqs, hopefully speeding up probe for the case where you have a handful of irqs to map.
Please note that we traverse this mapping table every time a PMIC driver registers for an interrupt. This means that even if we speed up the probe of pmic-arb driver we will not speed the system boot since the probing of the PMIC drivers will register for interrupts.
Quoted 5 lines
The DT property wouldn't be necessary then. Arguably it's being added there to optimize the size of the mapping table and isn't really necessary otherwise.
We need to know this value for both memory allocation
and for stopping before reading over the bounds of the mapping_table in HW.
Thank you,
Gilad
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 20 lines · Gilad Avidov
Quoted 9 lines · On Thu, 3 Sep 2015 17:16:30 -0700 Stephen Boyd <[email protected]> wrote:
Quoted 8 lines · Gilad Avidov
(quoted diff) +1
-0+ supported by HW. Default (minimumsupported) is 128. +
(quoted diff) +1
-0+Example V1 PMIC-Arbiter: spmi { compatible = "qcom,spmi-pmic-arb";@@ -62,4 +66,32 @@ Example:Quoted 7 lines
Review Example: · line 70
Quoted 4 lines
(quoted diff) +2
-0interrupt-controller; #interrupt-cells = <4>; + + qcom,max-peripherals = <256>;If it's v1 isn't it always 128? So having 256 here is just confusing.
Quoted 4 lines
Hi Stephen,
Actually some v1 chipsets, such as Dragonboard APQ8074, support 256 peripherals.
Then the commit text should be updated. It only mentions that v2 HW has sub-versions which support more than 128.
Quoted 26 lines
Quoted 16 lines
Quoted 16 lines
(quoted diff) +6
-3+ }; +@@ -129,14 +131,15 @@ struct spmi_pmic_arb_dev { u8 channel; int irq; u8 ee; - u8 min_apid; - u8 max_apid; - u32 mapping_table[SPMI_MAPPING_TABLE_LEN]; + u16 min_irq_apid; + u16 max_irq_apid; + u16 max_apid; + u32 *mapping_table;Quoted 9 lines
Review struct spmi_pmic_arb_dev { · line 141
Quoted 4 lines
(quoted diff) +1
-1struct irq_domain *domain; struct spmi_controller *spmic; - u16 apid_to_ppid[256]; + u16 *irq_apid_to_ppid;Please drop all this renaming noise, or at the least, put it in a different patch. More than half the patch is just changing the names of these variables for what seems like no reason.
Quoted 6 lines
Regarding the change of max/min_apid to max/min_irq_apid:
max_apidwas already used but the name does not make good sense. Since really it is not themax_apidsupported. Instead it is the largest apid which interrupt is currently requested for. But now we need a value that is actually the maximum supported apid. This is why repurposedmax_apidand corrected the previous naming.
max_apid in this patch is only used in probe, so there's no need for that variable to be a member of the structure. Meaning we can leave max_apid alone and call this new variable max_periphs or something like that and leave it local to the probe function.
Quoted 51 lines
Quoted 42 lines
Quoted 42 lines
(quoted diff) +1
-1const struct pmic_arb_ver_ops *ver_ops; - u8 *ppid_to_chan; + u16 *ppid_to_chan; };(quoted diff) +3
-5struct spmi_pmic_arb_dev *pa = irq_get_handler_data(irq); struct irq_chip *chip = irq_get_chip(irq); void __iomem *intr = pa->intr; - int first = pa->min_apid >> 5; - int last = pa->max_apid >> 5; + int first = pa->min_irq_apid >> 5; + int last = pa->max_irq_apid >> 5; u32 status; int i, id;@@ -903,14 +915,30 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) pa->ee = ee; - for (i = 0; i < ARRAY_SIZE(pa->mapping_table); ++i) - pa->mapping_table[i] = readl_relaxed( - pa->cnfg + SPMI_MAPPING_TABLE_REG(i)); + pa->irq_apid_to_ppid = devm_kzalloc(&ctrl->dev,
pa->max_apid*
+sizeof(*pa->irq_apid_to_ppid),(quoted diff) +8
-0+ GFP_KERNEL); + if (!pa->irq_apid_to_ppid) { + err = -ENOMEM; + goto err_put_ctrl; + } + + pa->mapping_table = devm_kzalloc(&ctrl->dev, + (pa->max_apid - 1) *sizeof(u32),
(quoted diff) +5
-0+ GFP_KERNEL); + if (!pa->mapping_table) { + err = -ENOMEM; + goto err_put_ctrl; + }Quoted 8 lines
Quoted 5 lines
(quoted diff) +4
-0+ + for (i = 0; i < (pa->max_apid - 1); ++i) + pa->mapping_table[i] = readl_relaxed(pa->cnfg + +
SPMI_MAPPING_TABLE_REG(i));we're searching through the mapping table we can cache the value from the register if the entry isn't 0. This delays the
Quoted 6 lines
Greedy approach (reading upto 512 registers from hw) have very small upfront penalty since sequential reads take place very fast and HW acceleration (prefetching) will improve it. A lazy approach will required much more complex algorithm (for example any value including zero is valid. So we will need to augment a marker for unassigned entries) and prefetching will not help.
That's solvable with a bitmap of 512 entries, not too much of a complicated algorithm.
We're sort of screwed now that we have to find the channel for a ppid by reading X number of PMIC_ARB_REG_CHNL registers. We could lazily do that too though, where we fill in the array linearly with what we read until we find the ppid we're looking for. We would need to do a bitmap trick again, but since we know ppid is at most going to use 12 bits, we can set the highest bit to indicate the ppid_to_chan entry is valid.
Quoted 8 lines
Quoted 3 lines
processing to when we're mapping irqs, hopefully speeding up probe for the case where you have a handful of irqs to map.
Please note that we traverse this mapping table every time a PMIC driver registers for an interrupt. This means that even if we speed up the probe of pmic-arb driver we will not speed the system boot since the probing of the PMIC drivers will register for interrupts.
Do we need to read all 512 entries for a typical system? If we do then I agree reading it all upfront is probably better, but if we only read a sparse amount of entries then we can defer it to when it's going to be used. On upstream 8974 dts files with all the devices we have in DT right now, I see 75 unique register reads during boot on apq8074 dragonboard.
Quoted 11 lines
Quoted 6 lines
The DT property wouldn't be necessary then. Arguably it's being added there to optimize the size of the mapping table and isn't really necessary otherwise.
We need to know this value for both memory allocation
and for stopping before reading over the bounds of themapping_tablein HW.
Presumably the hardware is correct and isn't telling us to read entries in the mapping table that are beyond the bounds, so stepping over the bounds of the mapping table cache isn't possible if we always make it the size of the biggest table.
Here's the untested patch. I hope we don't ever have more than 512 periphs.
BTW, we shouldn't make qcom,channel required on v2 hardware. That doesn't make any sense so it should be changed to required only on v1 devices and we should remove it from the 8916 dtsi file.
----8<-----
drivers/spmi/spmi-pmic-arb.c | 98 +++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 32 deletions(-)
drivers/spmi/spmi-pmic-arb.c +66 -32
@@ -10,6 +10,7 @@* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ +#include <linux/bitmap.h> #include <linux/delay.h> #include <linux/err.h> #include <linux/interrupt.h>
@@ -47,9 +48,9 @@#define SPMI_MAPPING_BIT_IS_1_FLAG (X ) (((X) >> 8) & 0x1)#define SPMI_MAPPING_BIT_IS_1_RESULT (X ) (((X) >> 0) & 0xFF) -#define SPMI_MAPPING_TABLE_LEN 255#define SPMI_MAPPING_TABLE_TREE_DEPTH 16/* Maximum of 16-bits */ -#define PPID_TO_CHAN_TABLE_SZ BIT(12)/* PPID is 12bit chan is 1byte*/ +#define PMIC_ARB_MAX_PPID BIT(12)/* PPID is 12bit */ +#define PMIC_ARB_PPID_VALID BIT(15)/* Ownership Table */ #define SPMI_OWNERSHIP_TABLE_REG (N ) (0x0700 + (4 * (N)))
@@ -85,9 +86,7 @@ enum pmic_arb_cmd_op_code { };/* Maximum number of support PMIC peripherals */ -#define PMIC_ARB_MAX_PERIPHS 256 -#define PMIC_ARB_MAX_CHNL 128 -#define PMIC_ARB_PERIPH_ID_VALID (1 << 15) +#define PMIC_ARB_MAX_PERIPHS 512#define PMIC_ARB_TIMEOUT_US 100#define PMIC_ARB_MAX_TRANS_BYTES (8)
@@ -125,18 +124,21 @@ struct spmi_pmic_arb_dev {void __iomem *wr_base;void __iomem *intr;void __iomem *cnfg; +void __iomem *core;raw_spinlock_t lock;u8 channel;int irq;u8 ee; -u8 min_apid; -u8 max_apid; -u32 mapping_table[SPMI_MAPPING_TABLE_LEN ]; +u16 min_apid; +u16 max_apid; +u32 *mapping_table; +DECLARE_BITMAP (mapping_table_valid,PMIC_ARB_MAX_PERIPHS );struct irq_domain *domain;struct spmi_controller *spmic; -u16 apid_to_ppid[256 ]; +u16 *apid_to_ppid;const struct pmic_arb_ver_ops *ver_ops; -u8 *ppid_to_chan; +u16 *ppid_to_chan; +u16 last_channel; };/**
@@ -614,6 +616,10 @@ static int search_mapping_table(struct spmi_pmic_arb_dev *pa,u32 data;for (i =0 ; i <SPMI_MAPPING_TABLE_TREE_DEPTH ; ++i) { +if (!test_and_set_bit (index, pa->mapping_table_valid)) + mapping_table[index] =readl_relaxed (pa->cnfg + +SPMI_MAPPING_TABLE_REG (index)); + data = mapping_table[index];if (ppid & (1 <<SPMI_MAPPING_BIT_INDEX (data))) {
@@ -706,11 +712,37 @@ static u32 pmic_arb_offset_v1(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr)return 0x800 +0x80 * pa->channel; } +static u16 find_chan (struct spmi_pmic_arb_dev *pa,u16 ppid) +{ +u32 regval; +u16 chan; +u16 _ppid; + +for (chan = pa->last_channel; ; chan++, pa->last_channel++) { + regval =readl_relaxed (pa->core +PMIC_ARB_REG_CHNL (chan)); +if (!regval) +continue ; + + _ppid = (regval >>8 ) &PMIC_ARB_PPID_MASK ; + pa->ppid_to_chan[_ppid] = chan |PMIC_ARB_PPID_VALID ; +if (_ppid == ppid) +break ; + } + +return chan; +} + +/* v2 offset per ppid (chan) and per ee */ static u32 pmic_arb_offset_v2 (struct spmi_pmic_arb_dev *pa,u8 sid,u16 addr) {u16 ppid = (sid <<8 ) | (addr >>8 ); -u8 chan = pa->ppid_to_chan[ppid]; +u16 chan; + + chan = pa->ppid_to_chan[ppid]; +if (!(chan &PMIC_ARB_PPID_VALID )) + chan =find_chan (pa, ppid); + chan &= ~PMIC_ARB_PPID_MASK ;return 0x1000 * pa->ee +0x8000 * chan; }
@@ -797,7 +829,7 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev)struct resource *res;void __iomem *core;u32 channel, ee, hw_ver; -int err, i; +int err;bool is_v1; ctrl =spmi_controller_alloc (&pdev->dev,sizeof (*pa));
@@ -825,10 +857,7 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) pa->wr_base = core; pa->rd_base = core; }else { -u8 chan; -u16 ppid; -u32 regval; - + pa->core = core; pa->ver_ops = &pmic_arb_v2; res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
@@ -847,23 +876,17 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) goto err_put_ctrl; } - pa->ppid_to_chan =devm_kzalloc (&ctrl->dev, -PPID_TO_CHAN_TABLE_SZ ,GFP_KERNEL ); -if (!pa->ppid_to_chan) { - err = -ENOMEM ; - goto err_put_ctrl; - }/* * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid. * ppid_to_chan is an in-memory invert of that table. */ -for (chan =0 ; chan <PMIC_ARB_MAX_CHNL ; ++chan) { - regval =readl_relaxed (core +PMIC_ARB_REG_CHNL (chan)); -if (!regval) -continue ; - - ppid = (regval >>8 ) &0xFFF ; - pa->ppid_to_chan[ppid] = chan; + pa->ppid_to_chan =devm_kcalloc (&ctrl->dev, +PMIC_ARB_MAX_PPID , +sizeof (*pa->ppid_to_chan), +GFP_KERNEL ); +if (!pa->ppid_to_chan) { + err = -ENOMEM ; + goto err_put_ctrl; } }
@@ -915,9 +938,20 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) pa->ee = ee; -for (i =0 ; i <ARRAY_SIZE (pa->mapping_table); ++i) - pa->mapping_table[i] =readl_relaxed ( - pa->cnfg +SPMI_MAPPING_TABLE_REG (i)); + pa->apid_to_ppid =devm_kcalloc (&ctrl->dev,PMIC_ARB_MAX_PERIPHS , +sizeof (*pa->apid_to_ppid), +GFP_KERNEL ); +if (!pa->apid_to_ppid) { + err = -ENOMEM ; + goto err_put_ctrl; + } + + pa->mapping_table =devm_kcalloc (&ctrl->dev,PMIC_ARB_MAX_PERIPHS -1 , +sizeof (*pa->mapping_table),GFP_KERNEL ); +if (!pa->mapping_table) { + err = -ENOMEM ; + goto err_put_ctrl; + }/* Initialize max_apid/min_apid to the opposite bounds, during * the irq domain translation, we are sure to update these */
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
Quoted 21 lines · On Fri, 4 Sep 2015 17:50:11 -0700 Stephen Boyd <[email protected]> wrote:
Quoted 20 lines · Gilad Avidov
Quoted 16 lines · On Thu, 3 Sep 2015 17:16:30 -0700 Stephen Boyd <[email protected]> wrote:
Review Example: · line 70
Quoted 12 lines · Gilad Avidov
(quoted diff) +1
-0+ supported by HW. Default (minimumsupported) is 128. +
(quoted diff) +3
-0+Example V1 PMIC-Arbiter: spmi { compatible = "qcom,spmi-pmic-arb";@@ -62,4 +66,32 @@ Example: interrupt-controller; #interrupt-cells = <4>; + + qcom,max-peripherals = <256>;If it's v1 isn't it always 128? So having 256 here is just confusing.
Quoted 8 lines
Quoted 4 lines
Hi Stephen,
Actually some v1 chipsets, such as Dragonboard APQ8074, support 256 peripherals.
Then the commit text should be updated. It only mentions that v2 HW has sub-versions which support more than 128.
Correct, I'll update the commit text.
Quoted 33 lines
Quoted 26 lines
Quoted 16 lines
Quoted 16 lines
drivers/spmi/spmi-pmic-arb.c +6
-3+ }; +@@ -129,14 +131,15 @@ struct spmi_pmic_arb_dev {u8 channel;int irq;u8 ee; -u8 min_apid; -u8 max_apid; -u32 mapping_table[SPMI_MAPPING_TABLE_LEN ]; +u16 min_irq_apid; +u16 max_irq_apid; +u16 max_apid; +u32 *mapping_table;Quoted 9 lines
drivers/spmi/spmi-pmic-arb.c struct spmi_pmic_arb_dev {
Quoted 4 lines
drivers/spmi/spmi-pmic-arb.c +1
-1struct irq_domain *domain;struct spmi_controller *spmic; -u16 apid_to_ppid[256 ]; +u16 *irq_apid_to_ppid;Please drop all this renaming noise, or at the least, put it in a different patch. More than half the patch is just changing the names of these variables for what seems like no reason.
Quoted 6 lines
Regarding the change of max/min_apid to max/min_irq_apid:
max_apidwas already used but the name does not make good sense. Since really it is not themax_apidsupported. Instead it is the largest apid which interrupt is currently requested for. But now we need a value that is actually the maximum supported apid. This is why repurposedmax_apidand corrected the previous naming.
Quoted 5 lines
max_apidin this patch is only used in probe, so there's no need for that variable to be a member of the structure. Meaning we can leavemax_apidalone and call this new variablemax_periphsor something like that and leave it local to the probe function.
I'll avoid the renaming altogether.
Quoted 61 lines
Quoted 51 lines
Quoted 42 lines
Quoted 42 lines
drivers/spmi/spmi-pmic-arb.c +1
-1const struct pmic_arb_ver_ops *ver_ops; -u8 *ppid_to_chan; +u16 *ppid_to_chan; };struct
spmi_pmic_arb_dev*pa =irq_get_handler_data(irq); structirq_chip*chip =irq_get_chip(irq); void__iomem*intr =pa->intr;drivers/spmi/spmi-pmic-arb.c +3
-5-int first = pa->min_apid >>5 ; -int last = pa->max_apid >>5 ; +int first = pa->min_irq_apid >>5 ; +int last = pa->max_irq_apid >>5 ;u32 status;int i, id;@@ -903,14 +915,30 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) pa->ee = ee; -for (i =0 ; i <ARRAY_SIZE (pa->mapping_table); ++i) - pa->mapping_table[i] =readl_relaxed ( - pa->cnfg + SPMI_MAPPING_TABLE_REG(i)); + pa->irq_apid_to_ppid = devm_kzalloc(&ctrl->dev,
pa->max_apid*
+sizeof(*pa->irq_apid_to_ppid),drivers/spmi/spmi-pmic-arb.c +8
-0+ GFP_KERNEL); +if (!pa->irq_apid_to_ppid) { + err = -ENOMEM ; + goto err_put_ctrl; + } + + pa->mapping_table = devm_kzalloc(&ctrl->dev, + (pa->max_apid - 1) *sizeof(u32),
drivers/spmi/spmi-pmic-arb.c +5
-0+ GFP_KERNEL); +if (!pa->mapping_table) { + err = -ENOMEM ; + goto err_put_ctrl; + }Quoted 8 lines
Quoted 5 lines
drivers/spmi/spmi-pmic-arb.c +4
-0+ +for (i =0 ; i < (pa->max_apid -1 ); ++i) + pa->mapping_table[i] = readl_relaxed(pa->cnfg + +
SPMI_MAPPING_TABLE_REG(i));we're searching through the mapping table we can cache the value from the register if the entry isn't 0. This delays the
Quoted 6 lines
Greedy approach (reading upto 512 registers from hw) have very small upfront penalty since sequential reads take place very fast and HW acceleration (prefetching) will improve it. A lazy approach will required much more complex algorithm (for example any value including zero is valid. So we will need to augment a marker for unassigned entries) and prefetching will not help.
That's solvable with a bitmap of 512 entries, not too much of a complicated algorithm.
Quoted 8 lines
We're sort of screwed now that we have to find the channel for a ppid by reading X number of
PMIC_ARB_REG_CHNLregisters. We could lazily do that too though, where we fill in the array linearly with what we read until we find the ppid we're looking for. We would need to do a bitmap trick again, but since we know ppid is at most going to use 12 bits, we can set the highest bit to indicate theppid_to_chaneI agree that the algorithm is not complicated. However, I still think
I agree that the algorithm is not complicated. However, I still think that just reading max of 512 registers is simpler and overhead is small.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
processing to when we're mapping irqs, hopefully speeding up probe for the case where you have a handful of irqs to map.
Quoted 8 lines
Quoted 4 lines
Please note that we traverse this mapping table every time a PMIC driver registers for an interrupt. This means that even if we speed up the probe of pmic-arb driver we will not speed the system boot since the probing of the PMIC drivers will register for interrupts.
Do we need to read all 512 entries for a typical system? If we do then I agree reading it all upfront is probably better, but if we only read a sparse amount of entries then we can defer it to when
We need a many of the 512 entries since for each request_irq() we traverse down the. Typically during boot pmic drivers request irqs.
Quoted 4 lines
it's going to be used. On upstream 8974 dts files with all the devices we have in DT right now, I see 75 unique register reads during boot on apq8074 dragonboard.
There are still very few of pmic driver upstreamed. However, many more of them are likely to get upstreamed soon. Once all of them will be active on upstream I am sure that the number of register access will dramatically increase (just as it is today on Codeaurora).
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The DT property wouldn't be necessary then. Arguably it's being added there to optimize the size of the mapping table and isn't really necessary otherwise.
Quoted 6 lines
Quoted 4 lines
We need to know this value for both memory allocation and for stopping before reading over the bounds of the
mapping_tablein HW.Presumably the hardware is correct and isn't telling us to read
HW tells us nothing about the size of the HW mapping table.
Quoted 4 lines
entries in the mapping table that are beyond the bounds, so stepping over the bounds of the mapping table cache isn't possible if we always make it the size of the biggest table.
The size of the table in RAM is the driver writer choice. It is wasteful to have it larger then needed but no harm to stability. On the flip side, reading registers beyond the HW bounds can have undefined behavior.
Quoted 103 lines
Here's the untested patch. I hope we don't ever have more than 512 periphs.
BTW, we shouldn't make qcom,channel required on v2 hardware. That doesn't make any sense so it should be changed to required only on v1 devices and we should remove it from the 8916 dtsi file.
----8<-----
Signed-off-by Stephen Boyd <[email protected]>drivers/spmi/spmi-pmic-arb.c | 98+++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 32 deletions(-)
(quoted diff) +0
-0
b/drivers/spmi/spmi-pmic-arb.cindexc7aa6f1a898e..cbe963f829d7100644drivers/spmi/spmi-pmic-arb.c +1
-1@@ -10,6 +10,7 @@* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ +#include <linux/bitmap.h> #include <linux/delay.h> #include <linux/err.h> #include <linux/interrupt.h> @@ -47,9 +48,9 @@#define SPMI_MAPPING_BIT_IS_1_FLAG (X ) (((X) >> 8) & 0x1)#define SPMI_MAPPING_BIT_IS_1_RESULT (X ) (((X) >> 0) & 0xFF) -#define SPMI_MAPPING_TABLE_LEN 255#define SPMI_MAPPING_TABLE_TREE_DEPTH 16/* Maximum of 16-bits */ -#define
PPID_TO_CHAN_TABLE_SZ
BIT(12) /* PPID is 12bit chan is 1byte*/ +#definePMIC_ARB_MAX_PPIDBIT(12) /* PPID is 12bit */ +#definePMIC_ARB_PPID_VALIDBIT(15)
/* Ownership Table */
#defineSPMI_OWNERSHIP_TABLE_REG(N)(0x0700+ (4 * (N)))drivers/spmi/spmi-pmic-arb.c +9
-8@@ -85,9 +86,7 @@ enum pmic_arb_cmd_op_code { };/* Maximum number of support PMIC peripherals */ -#define PMIC_ARB_MAX_PERIPHS 256 -#define PMIC_ARB_MAX_CHNL 128 -#define PMIC_ARB_PERIPH_ID_VALID (1 << 15) +#define PMIC_ARB_MAX_PERIPHS 512#define PMIC_ARB_TIMEOUT_US 100#define PMIC_ARB_MAX_TRANS_BYTES (8)@@ -125,18 +124,21 @@ struct spmi_pmic_arb_dev {void __iomem *wr_base;void __iomem *intr;void __iomem *cnfg; +void __iomem *core;raw_spinlock_t lock;u8 channel;int irq;u8 ee; -u8 min_apid; -u8 max_apid; -u32 mapping_table[SPMI_MAPPING_TABLE_LEN ]; +u16 min_apid; +u16 max_apid; +u32 *mapping_table; +DECLARE_BITMAP (mapping_table_valid,PMIC_ARB_MAX_PERIPHS );struct irq_domain *domain;struct spmi_controller *spmic; -u16 apid_to_ppid[256 ]; +u16 *apid_to_ppid;const struct pmic_arb_ver_ops *ver_ops; -u8 *ppid_to_chan; +u16 *ppid_to_chan; +u16 last_channel; };/**
drivers/spmi/spmi-pmic-arb.c +2
-0@@ -614,6 +616,10 @@ static int search_mapping_table(struct spmi_pmic_arb_dev *pa, u32 data;for (i =0 ; i <SPMI_MAPPING_TABLE_TREE_DEPTH ; ++i) { +if (!test_and_set_bit (index, pa->mapping_table_valid)) + mapping_table[index] =readl_relaxed(pa->cnfg +
+SPMI_MAPPING_TABLE_REG(index)); +drivers/spmi/spmi-pmic-arb.c +0
-0data = mapping_table[index];if (ppid & (1 <<SPMI_MAPPING_BIT_INDEX (data))) {@@ -706,11 +712,37 @@ static u32 pmic_arb_offset_v1(struct
spmi_pmic_arb_dev*pa, u8 sid, u16 addr) return0x800+0x80*pa->channel; }drivers/spmi/spmi-pmic-arb.c +8
-0+static u16 find_chan (struct spmi_pmic_arb_dev *pa,u16 ppid) +{ +u32 regval; +u16 chan; +u16 _ppid; + +for (chan = pa->last_channel; ; chan++, pa->last_channel++) { + regval = readl_relaxed(pa->core +
PMIC_ARB_REG_CHNL(chan));
drivers/spmi/spmi-pmic-arb.c static u32 pmic_arb_offset_v1(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr) · line 725
Quoted 3 lines
drivers/spmi/spmi-pmic-arb.c +3
-0+if (!regval) +continue ; +
I'm pretty sure that caching this table is way faster then searching it by reading from hw per any newly used apid. If we use 512 peripherals then we will read this table 512 times.
Quoted 13 lines
drivers/spmi/spmi-pmic-arb.c +10
-0+ _ppid = (regval >>8 ) &PMIC_ARB_PPID_MASK ; + pa->ppid_to_chan[_ppid] = chan |PMIC_ARB_PPID_VALID ; +if (_ppid == ppid) +break ; + } + +return chan; +} + +/* v2 offset per ppid (chan) and per ee */ static u32 pmic_arb_offset_v2(struct spmi_pmic_arb_dev *pa, u8 sid,u16 addr) {
drivers/spmi/spmi-pmic-arb.c static u32 pmic_arb_offset_v1(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr) · line 741
Quoted 4 lines
drivers/spmi/spmi-pmic-arb.c +2
-1u16 ppid = (sid <<8 ) | (addr >>8 ); -u8 chan = pa->ppid_to_chan[ppid]; +u16 chan; +
I would use PMIC_ARB_PPID_VALID here to reject access to invalid ppids.
Quoted 93 lines
drivers/spmi/spmi-pmic-arb.c +6
-5+ chan = pa->ppid_to_chan[ppid]; +if (!(chan &PMIC_ARB_PPID_VALID )) + chan =find_chan (pa, ppid); + chan &= ~PMIC_ARB_PPID_MASK ;return 0x1000 * pa->ee +0x8000 * chan; }@@ -797,7 +829,7 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) struct resource *res;void __iomem *core;u32 channel, ee, hw_ver; -int err, i; +int err;bool is_v1; ctrl =spmi_controller_alloc (&pdev->dev,sizeof (*pa));@@ -825,10 +857,7 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) pa->wr_base = core; pa->rd_base = core; }else { -u8 chan; -u16 ppid; -u32 regval; - + pa->core = core; pa->ver_ops = &pmic_arb_v2; res = platform_get_resource_byname(pdev,
IORESOURCE_MEM, @@ -847,23 +876,17 @@ static intspmi_pmic_arb_probe(struct platform_device *pdev)gotoerr_put_ctrl;drivers/spmi/spmi-pmic-arb.c +0
-2} - pa->ppid_to_chan = devm_kzalloc(&ctrl->dev, - PPID_TO_CHAN_TABLE_SZ,
GFP_KERNEL);drivers/spmi/spmi-pmic-arb.c +0
-4-if (!pa->ppid_to_chan) { - err = -ENOMEM ; - goto err_put_ctrl; - }/* * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
drivers/spmi/spmi-pmic-arb.c +0
-2* ppid_to_chan is an in-memory invert of that table. */ -for (chan =0 ; chan <PMIC_ARB_MAX_CHNL ; ++chan) { - regval = readl_relaxed(core +
PMIC_ARB_REG_CHNL(chan));drivers/spmi/spmi-pmic-arb.c +3
-5-if (!regval) -continue ; - - ppid = (regval >>8 ) &0xFFF ; - pa->ppid_to_chan[ppid] = chan; + pa->ppid_to_chan = devm_kcalloc(&ctrl->dev, + PMIC_ARB_MAX_PPID, +
sizeof(*pa->ppid_to_chan),drivers/spmi/spmi-pmic-arb.c +13
-3+ GFP_KERNEL); +if (!pa->ppid_to_chan) { + err = -ENOMEM ; + goto err_put_ctrl; } }@@ -915,9 +938,20 @@ static int spmi_pmic_arb_probe(struct platform_device *pdev) pa->ee = ee; -for (i =0 ; i <ARRAY_SIZE (pa->mapping_table); ++i) - pa->mapping_table[i] =readl_relaxed ( - pa->cnfg + SPMI_MAPPING_TABLE_REG(i)); + pa->apid_to_ppid =devm_kcalloc (&ctrl->dev,PMIC_ARB_MAX_PERIPHS , +sizeof (*pa->apid_to_ppid), +GFP_KERNEL ); +if (!pa->apid_to_ppid) { + err = -ENOMEM ; + goto err_put_ctrl; + } + + pa->mapping_table = devm_kcalloc(&ctrl->dev,
PMIC_ARB_MAX_PERIPHS- 1,drivers/spmi/spmi-pmic-arb.c +1
-0+ sizeof(*pa->mapping_table),
GFP_KERNEL);drivers/spmi/spmi-pmic-arb.c +4
-0+if (!pa->mapping_table) { + err = -ENOMEM ; + goto err_put_ctrl; + }/* Initialize max_apid/min_apid to the opposite bounds, during
* the irq domain translation, we are sure to update these */
Quoted 34 lines · Gilad Avidov
Quoted 5 lines · On Fri, 4 Sep 2015 17:50:11 -0700 Stephen Boyd <[email protected]> wrote:
Quoted 4 lines · Gilad Avidov
Quoted 1 line · On Thu, 3 Sep 2015 17:16:30 -0700 Stephen Boyd <[email protected]> wrote:
On 09/03, Gilad Avidov wrote:
I agree that the algorithm is not complicated. However, I still think that just reading max of 512 registers is simpler and overhead is small.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
processing to when we're mapping irqs, hopefully speeding up probe for the case where you have a handful of irqs to map.
Quoted 8 lines
Quoted 4 lines
Please note that we traverse this mapping table every time a PMIC driver registers for an interrupt. This means that even if we speed up the probe of pmic-arb driver we will not speed the system boot since the probing of the PMIC drivers will register for interrupts.
Do we need to read all 512 entries for a typical system? If we do then I agree reading it all upfront is probably better, but if we only read a sparse amount of entries then we can defer it to when
We need a many of the 512 entries since for each
request_irq()we traverse down the. Typically during boot pmic drivers request irqs.Quoted 4 lines
it's going to be used. On upstream 8974 dts files with all the devices we have in DT right now, I see 75 unique register reads during boot on apq8074 dragonboard.
Quoted 4 lines
There are still very few of pmic driver upstreamed. However, many more of them are likely to get upstreamed soon. Once all of them will be active on upstream I am sure that the number of register access will dramatically increase (just as it is today on Codeaurora).
So on the latest hardware I see 33 unique register reads. That's on the codeaurora android tree. It seems to be less than on 8974. I'll have to boot up the android tree on 8974 to see how much it is. The patch is simple:
drivers/spmi/spmi-pmic-arb.c +1 -0
@@ -603,6 +603,7 @@ static int search_mapping_table(struct spmi_pmic_arb_dev *pa,for (i =0 ; i <SPMI_MAPPING_TABLE_TREE_DEPTH ; ++i) { data = mapping_table[index]; +pr_info ("mapping table %d\n" , index);if (ppid & (1 <<SPMI_MAPPING_BIT_INDEX (data))) {if (SPMI_MAPPING_BIT_IS_1_FLAG (data)) {
And then
$ dmesg | grep "mapping table " | sort -u | wc -l
Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
The DT property wouldn't be necessary then. Arguably it's being added there to optimize the size of the mapping table and isn't really necessary otherwise.
Quoted 6 lines
Quoted 4 lines
We need to know this value for both memory allocation and for stopping before reading over the bounds of the
mapping_tablein HW.Presumably the hardware is correct and isn't telling us to read
HW tells us nothing about the size of the HW mapping table.
Quoted 4 lines
entries in the mapping table that are beyond the bounds, so stepping over the bounds of the mapping table cache isn't possible if we always make it the size of the biggest table.
Quoted 4 lines
The size of the table in RAM is the driver writer choice. It is wasteful to have it larger then needed but no harm to stability. On the flip side, reading registers beyond the HW bounds can have undefined behavior.
Sure. We could realloc the table at runtime if we really care about saving the space.
Quoted 5 lines
Quoted 5 lines
(quoted diff) +0
-0@@ -706,11 +712,37 @@ static u32 pmic_arb_offset_v1(struct
spmi_pmic_arb_dev*pa, u8 sid, u16 addr) return0x800+0x80*pa->channel; }
Quoted 16 lines
Review
Quoted 12 lines
(quoted diff) +8
-0+static u16 find_chan(struct spmi_pmic_arb_dev *pa, u16 ppid) +{ + u32 regval; + u16 chan; + u16 _ppid; + + for (chan = pa->last_channel; ; chan++, pa->last_channel++) { + regval = readl_relaxed(pa->core +
PMIC_ARB_REG_CHNL(chan));(quoted diff) +3
-0+ if (!regval) + continue; +I'm pretty sure that caching this table is way faster then searching it by reading from hw per any newly used apid. If we use 512 peripherals then we will read this table 512 times.
You missed the part where we don't read this table from hardware more than once. We read the hw table linearly until we find the ppid that we were looking for, caching other values along the way. If that happens to be the whole table then I guess we don't win anything by doing it here vs. in probe, but hopefully we don't have to search the entire table on the first try to find the ppid.
Quoted 12 lines
Quoted 12 lines
(quoted diff) +10
-0+ _ppid = (regval >> 8) & PMIC_ARB_PPID_MASK; + pa->ppid_to_chan[_ppid] = chan | PMIC_ARB_PPID_VALID; + if (_ppid == ppid) + break; + } + + return chan; +} + + /* v2 offset per ppid (chan) and per ee */
Quoted 8 lines
Review
Quoted 6 lines
static u32 pmic_arb_offset_v2(struct
spmi_pmic_arb_dev*pa, u8 sid,
u16 addr) {(quoted diff) +2
-1u16 ppid = (sid << 8) | (addr >> 8); - u8 chan = pa->ppid_to_chan[ppid]; + u16 chan; +I would use
PMIC_ARB_PPID_VALIDhere to reject access to invalid ppids.
Do you mean PMIC_ARB_MAX_PPID? We don't have that check now. I would guess that's because we assume the ppid is always valid at this point.
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project