On Tue, Feb 22, 2022 at 05:31:56PM +0100, Pali Rohár wrote: > Add function of_pci_get_slot_power_limit(), which parses the > 'slot-power-limit-milliwatt' DT property, returning the value in > milliwatts and in format ready for the PCIe Slot Capabilities Register. > > Signed-off-by: Pali Rohár <pali@xxxxxxxxxx> > Signed-off-by: Marek Behún <kabel@xxxxxxxxxx> > Reviewed-by: Rob Herring <robh@xxxxxxxxxx> > Acked-by: Bjorn Helgaas <bhelgaas@xxxxxxxxxx> > --- > drivers/pci/of.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++ > drivers/pci/pci.h | 15 +++++++++++ > 2 files changed, 79 insertions(+) > > diff --git a/drivers/pci/of.c b/drivers/pci/of.c > index cb2e8351c2cc..2b0c0a3641a8 100644 > --- a/drivers/pci/of.c > +++ b/drivers/pci/of.c > @@ -633,3 +633,67 @@ int of_pci_get_max_link_speed(struct device_node *node) > return max_link_speed; > } > EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed); > + > +/** > + * of_pci_get_slot_power_limit - Parses the "slot-power-limit-milliwatt" > + * property. > + * > + * @node: device tree node with the slot power limit information > + * @slot_power_limit_value: pointer where the value should be stored in PCIe > + * Slot Capabilities Register format > + * @slot_power_limit_scale: pointer where the scale should be stored in PCIe > + * Slot Capabilities Register format > + * > + * Returns the slot power limit in milliwatts and if @slot_power_limit_value > + * and @slot_power_limit_scale pointers are non-NULL, fills in the value and > + * scale in format used by PCIe Slot Capabilities Register. > + * > + * If the property is not found or is invalid, returns 0. > + */ > +u32 of_pci_get_slot_power_limit(struct device_node *node, > + u8 *slot_power_limit_value, > + u8 *slot_power_limit_scale) > +{ > + u32 slot_power_limit; Including "mw" or similar reference to the units would give a hint of how to relate the code to the spec. > + u8 value, scale; > + > + if (of_property_read_u32(node, "slot-power-limit-milliwatt", > + &slot_power_limit)) > + slot_power_limit = 0; > + > + /* Calculate Slot Power Limit Value and Slot Power Limit Scale */ Add a spec reference to PCIe r6.0, sec 7.5.3.9. IIUC, this supports up to 300W, which was what r5.0 defined, but r6.0 added values up to 0xfe (600W). > + if (slot_power_limit == 0) { > + value = 0x00; > + scale = 0; > + } else if (slot_power_limit <= 255) { > + value = slot_power_limit; > + scale = 3; > + } else if (slot_power_limit <= 255*10) { > + value = slot_power_limit / 10; > + scale = 2; > + } else if (slot_power_limit <= 255*100) { > + value = slot_power_limit / 100; > + scale = 1; > + } else if (slot_power_limit <= 239*1000) { > + value = slot_power_limit / 1000; > + scale = 0; > + } else if (slot_power_limit <= 250*1000) { > + value = 0xF0; > + scale = 0; > + } else if (slot_power_limit <= 275*1000) { > + value = 0xF1; > + scale = 0; > + } else { > + value = 0xF2; > + scale = 0; > + } > + > + if (slot_power_limit_value) > + *slot_power_limit_value = value; > + > + if (slot_power_limit_scale) > + *slot_power_limit_scale = scale; > + > + return slot_power_limit; If "slot-power-limit-milliwatt" contains a value larger than can be represented in "value" and "scale", the return value will not agree with value/scale, will it? Currently you only use the return value for a log message, so no real harm yet, other than the fact that we might print "Slot power limit 1000.0W" when the hardware will only advertise 600W available. Also, if "slot-power-limit-milliwatt" contains something like 260000 mW (260 W), we'll return 0xF1/0, so the hardware will advertise 275 W available. > +} > +EXPORT_SYMBOL_GPL(of_pci_get_slot_power_limit); > diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h > index 3d60cabde1a1..e10cdec6c56e 100644 > --- a/drivers/pci/pci.h > +++ b/drivers/pci/pci.h > @@ -627,6 +627,9 @@ struct device_node; > int of_pci_parse_bus_range(struct device_node *node, struct resource *res); > int of_get_pci_domain_nr(struct device_node *node); > int of_pci_get_max_link_speed(struct device_node *node); > +u32 of_pci_get_slot_power_limit(struct device_node *node, > + u8 *slot_power_limit_value, > + u8 *slot_power_limit_scale); > void pci_set_of_node(struct pci_dev *dev); > void pci_release_of_node(struct pci_dev *dev); > void pci_set_bus_of_node(struct pci_bus *bus); > @@ -653,6 +656,18 @@ of_pci_get_max_link_speed(struct device_node *node) > return -EINVAL; > } > > +static inline u32 > +of_pci_get_slot_power_limit(struct device_node *node, > + u8 *slot_power_limit_value, > + u8 *slot_power_limit_scale) > +{ > + if (slot_power_limit_value) > + *slot_power_limit_value = 0; > + if (slot_power_limit_scale) > + *slot_power_limit_scale = 0; > + return 0; > +} > + > static inline void pci_set_of_node(struct pci_dev *dev) { } > static inline void pci_release_of_node(struct pci_dev *dev) { } > static inline void pci_set_bus_of_node(struct pci_bus *bus) { } > -- > 2.20.1 >