From 5d6c61ac404c5c7c463ea6ee216145f29a603f60 Mon Sep 17 00:00:00 2001 From: Mario Six Date: Mon, 6 Aug 2018 10:23:41 +0200 Subject: board_f: Use static print_cpuinfo if CONFIG_CPU is active When the DM CPU drivers are active, printing information about a CPU should be delegated to a matching driver. Hence, add a static print_cpuinfo that implements this delegation when DM CPU drivers are active. Reviewed-by: Simon Glass Signed-off-by: Mario Six Changed condition to CONFIG_IS_ENABLED(CPU): Signed-off-by: Simon Glass --- include/init.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'include') diff --git a/include/init.h b/include/init.h index a58d7a6917f..afc953d51e2 100644 --- a/include/init.h +++ b/include/init.h @@ -109,7 +109,14 @@ int arch_reserve_stacks(void); */ int init_cache_f_r(void); +#if !CONFIG_IS_ENABLED(CPU) +/** + * print_cpuinfo() - Display information about the CPU + * + * Return: 0 if OK, -ve on error + */ int print_cpuinfo(void); +#endif int timer_init(void); int reserve_mmu(void); int misc_init_f(void); -- cgit v1.3.1 From e601ab1bb69901420ba2c55d2f2d194a38739182 Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Wed, 10 Oct 2018 22:06:57 -0700 Subject: dm: util: Add a livetree equivalent API of dm_fdt_pre_reloc() This adds a new API dm_ofnode_pre_reloc(), a livetree equivalent API of dm_fdt_pre_reloc(). Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- drivers/core/util.c | 25 +++++++++++++++++++++++++ include/dm/util.h | 27 ++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/drivers/core/util.c b/drivers/core/util.c index 451d4766d08..27a68487034 100644 --- a/drivers/core/util.c +++ b/drivers/core/util.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -53,3 +54,27 @@ bool dm_fdt_pre_reloc(const void *blob, int offset) return false; } + +bool dm_ofnode_pre_reloc(ofnode node) +{ + if (ofnode_read_bool(node, "u-boot,dm-pre-reloc")) + return true; + +#ifdef CONFIG_TPL_BUILD + if (ofnode_read_bool(node, "u-boot,dm-tpl")) + return true; +#elif defined(CONFIG_SPL_BUILD) + if (ofnode_read_bool(node, "u-boot,dm-spl")) + return true; +#else + /* + * In regular builds individual spl and tpl handling both + * count as handled pre-relocation for later second init. + */ + if (ofnode_read_bool(node, "u-boot,dm-spl") || + ofnode_read_bool(node, "u-boot,dm-tpl")) + return true; +#endif + + return false; +} diff --git a/include/dm/util.h b/include/dm/util.h index 898822e4459..9ff6531d1b2 100644 --- a/include/dm/util.h +++ b/include/dm/util.h @@ -55,7 +55,7 @@ static inline void dm_dump_devres(void) * There are 3 settings currently in use * - * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL - * Existing platforms only use it to indicate nodes needee in + * Existing platforms only use it to indicate nodes needed in * SPL. Should probably be replaced by u-boot,dm-spl for * existing platforms. * @blob: devicetree @@ -65,4 +65,29 @@ static inline void dm_dump_devres(void) */ bool dm_fdt_pre_reloc(const void *blob, int offset); +/** + * Check if an of node should be or was bound before relocation. + * + * Devicetree nodes can be marked as needed to be bound + * in the loader stages via special devicetree properties. + * + * Before relocation this function can be used to check if nodes + * are required in either SPL or TPL stages. + * + * After relocation and jumping into the real U-Boot binary + * it is possible to determine if a node was bound in one of + * SPL/TPL stages. + * + * There are 3 settings currently in use + * - + * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL + * Existing platforms only use it to indicate nodes needed in + * SPL. Should probably be replaced by u-boot,dm-spl for + * existing platforms. + * @node: of node + * + * Returns true if node is needed in SPL/TL, false otherwise. + */ +bool dm_ofnode_pre_reloc(ofnode node); + #endif -- cgit v1.3.1 From 8d773c4ab3f77a5a6ceed974d00083a312454660 Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Wed, 10 Oct 2018 22:06:58 -0700 Subject: dm: core: Respect drivers with the DM_FLAG_PRE_RELOC flag in lists_bind_fdt() Currently the comments of several APIs (eg: dm_init_and_scan()) say: @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. The 'Pre-Relocation Support' chapter in doc/driver-model/README.txt documents the same that both device tree properties and driver flag are supported. However the implementation only checks these special device tree properties without checking the driver flag at all. This updates lists_bind_fdt() to consider both scenarios. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Squashed in http://patchwork.ozlabs.org/patch/996473/ : Signed-off-by: Simon Glass --- drivers/core/device.c | 2 +- drivers/core/lists.c | 9 ++++++++- drivers/core/root.c | 12 ++++-------- drivers/misc/imx8/scu.c | 4 ++-- drivers/serial/serial-uclass.c | 2 +- drivers/timer/timer-uclass.c | 2 +- include/dm/lists.h | 5 ++++- 7 files changed, 21 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/drivers/core/device.c b/drivers/core/device.c index 5176aa3f866..47a697f3e5c 100644 --- a/drivers/core/device.c +++ b/drivers/core/device.c @@ -834,5 +834,5 @@ int dev_enable_by_path(const char *path) if (ret) return ret; - return lists_bind_fdt(parent, node, NULL); + return lists_bind_fdt(parent, node, NULL, false); } diff --git a/drivers/core/lists.c b/drivers/core/lists.c index a1677269d8d..a1f828463ec 100644 --- a/drivers/core/lists.c +++ b/drivers/core/lists.c @@ -122,7 +122,8 @@ static int driver_check_compatible(const struct udevice_id *of_match, return -ENOENT; } -int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp) +int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp, + bool pre_reloc_only) { struct driver *driver = ll_entry_start(struct driver, driver); const int n_ents = ll_entry_count(struct driver, driver); @@ -171,6 +172,12 @@ int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp) if (entry == driver + n_ents) continue; + if (pre_reloc_only) { + if (!dm_ofnode_pre_reloc(node) && + !(entry->flags & DM_FLAG_PRE_RELOC)) + return 0; + } + pr_debug(" - found match at '%s'\n", entry->name); ret = device_bind_with_driver_data(parent, entry, name, id->data, node, &dev); diff --git a/drivers/core/root.c b/drivers/core/root.c index b54bf5bcdc5..cd6a5a0276a 100644 --- a/drivers/core/root.c +++ b/drivers/core/root.c @@ -222,14 +222,12 @@ static int dm_scan_fdt_live(struct udevice *parent, int ret = 0, err; for (np = node_parent->child; np; np = np->sibling) { - if (pre_reloc_only && - !of_find_property(np, "u-boot,dm-pre-reloc", NULL)) - continue; if (!of_device_is_available(np)) { pr_debug(" - ignoring disabled device\n"); continue; } - err = lists_bind_fdt(parent, np_to_ofnode(np), NULL); + err = lists_bind_fdt(parent, np_to_ofnode(np), NULL, + pre_reloc_only); if (err && !ret) { ret = err; debug("%s: ret=%d\n", np->name, ret); @@ -282,14 +280,12 @@ static int dm_scan_fdt_node(struct udevice *parent, const void *blob, continue; } - if (pre_reloc_only && - !dm_fdt_pre_reloc(blob, offset)) - continue; if (!fdtdec_get_is_enabled(blob, offset)) { pr_debug(" - ignoring disabled device\n"); continue; } - err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL); + err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL, + pre_reloc_only); if (err && !ret) { ret = err; debug("%s: ret=%d\n", node_name, ret); diff --git a/drivers/misc/imx8/scu.c b/drivers/misc/imx8/scu.c index 0647ddf1032..b824ac79e6d 100644 --- a/drivers/misc/imx8/scu.c +++ b/drivers/misc/imx8/scu.c @@ -223,7 +223,7 @@ static int imx8_scu_bind(struct udevice *dev) if (node < 0) panic("No clk node found\n"); - ret = lists_bind_fdt(dev, offset_to_ofnode(node), &child); + ret = lists_bind_fdt(dev, offset_to_ofnode(node), &child, true); if (ret) return ret; @@ -234,7 +234,7 @@ static int imx8_scu_bind(struct udevice *dev) if (node < 0) panic("No iomuxc node found\n"); - ret = lists_bind_fdt(dev, offset_to_ofnode(node), &child); + ret = lists_bind_fdt(dev, offset_to_ofnode(node), &child, true); if (ret) return ret; diff --git a/drivers/serial/serial-uclass.c b/drivers/serial/serial-uclass.c index 665cca85cb5..3ded62732d4 100644 --- a/drivers/serial/serial-uclass.c +++ b/drivers/serial/serial-uclass.c @@ -62,7 +62,7 @@ static int serial_check_stdout(const void *blob, struct udevice **devp) * anyway. */ if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node), - devp)) { + devp, false)) { if (!device_probe(*devp)) return 0; } diff --git a/drivers/timer/timer-uclass.c b/drivers/timer/timer-uclass.c index fe73f718199..12ee6eb8043 100644 --- a/drivers/timer/timer-uclass.c +++ b/drivers/timer/timer-uclass.c @@ -108,7 +108,7 @@ int notrace dm_timer_init(void) * If the timer is not marked to be bound before * relocation, bind it anyway. */ - if (!lists_bind_fdt(dm_root(), node, &dev)) { + if (!lists_bind_fdt(dm_root(), node, &dev, false)) { ret = device_probe(dev); if (ret) return ret; diff --git a/include/dm/lists.h b/include/dm/lists.h index 13d1516a121..59094d78fc3 100644 --- a/include/dm/lists.h +++ b/include/dm/lists.h @@ -53,10 +53,13 @@ int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only); * @parent: parent device (root) * @node: device tree node to bind * @devp: if non-NULL, returns a pointer to the bound device + * @pre_reloc_only: If true, bind only nodes with special devicetree properties, + * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. * @return 0 if device was bound, -EINVAL if the device tree is invalid, * other -ve value on error */ -int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp); +int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp, + bool pre_reloc_only); /** * device_bind_driver() - bind a device to a driver -- cgit v1.3.1 From 6244fc64ce9714373908111acf24f70f88c86b3e Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Wed, 10 Oct 2018 22:06:59 -0700 Subject: dm: Correct pre_reloc_only parameter description in several APIs' comments The pre_reloc_only parameter description currently only mentions drivers with the DM_FLAG_PRE_RELOC flag, but does not mention the special device tree properties. Correct them. Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- include/dm/device-internal.h | 4 ++-- include/dm/lists.h | 4 ++-- include/dm/root.h | 17 +++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/dm/device-internal.h b/include/dm/device-internal.h index 02ac4c79528..ee2b24a62a2 100644 --- a/include/dm/device-internal.h +++ b/include/dm/device-internal.h @@ -74,8 +74,8 @@ int device_bind_with_driver_data(struct udevice *parent, * tree. * * @parent: Pointer to device's parent - * @pre_reloc_only: If true, bind the driver only if its DM_INIT_F flag is set. - * If false bind the driver always. + * @pre_reloc_only: If true, bind the driver only if its DM_FLAG_PRE_RELOC flag + * is set. If false bind the driver always. * @info: Name and platdata for this device * @devp: if non-NULL, returns a pointer to the bound device * @return 0 if OK, -ve on error diff --git a/include/dm/lists.h b/include/dm/lists.h index 59094d78fc3..810e244d9ef 100644 --- a/include/dm/lists.h +++ b/include/dm/lists.h @@ -39,8 +39,8 @@ struct uclass_driver *lists_uclass_lookup(enum uclass_id id); * each one. The devices will have @parent as their parent. * * @parent: parent device (root) - * @early_only: If true, bind only drivers with the DM_INIT_F flag. If false - * bind all drivers. + * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC flag. + * If false bind all drivers. */ int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only); diff --git a/include/dm/root.h b/include/dm/root.h index 2b9c6da4165..c8d629ba9bf 100644 --- a/include/dm/root.h +++ b/include/dm/root.h @@ -48,8 +48,8 @@ int dm_scan_platdata(bool pre_reloc_only); * the top-level subnodes are examined. * * @blob: Pointer to device tree blob - * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC - * flag. If false bind all drivers. + * @pre_reloc_only: If true, bind only nodes with special devicetree properties, + * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. * @return 0 if OK, -ve on error */ int dm_scan_fdt(const void *blob, bool pre_reloc_only); @@ -62,8 +62,8 @@ int dm_scan_fdt(const void *blob, bool pre_reloc_only); * of "clocks" node. * * @blob: Pointer to device tree blob - * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC - * flag. If false bind all drivers. + * @pre_reloc_only: If true, bind only nodes with special devicetree properties, + * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. * @return 0 if OK, -ve on error */ int dm_extended_scan_fdt(const void *blob, bool pre_reloc_only); @@ -76,8 +76,9 @@ int dm_extended_scan_fdt(const void *blob, bool pre_reloc_only); * programmaticaly. They should do this by calling device_bind() on each * device. * - * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC - * flag. If false bind all drivers. + * @pre_reloc_only: If true, bind only nodes with special devicetree properties, + * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. + * @return 0 if OK, -ve on error */ int dm_scan_other(bool pre_reloc_only); @@ -88,8 +89,8 @@ int dm_scan_other(bool pre_reloc_only); * then scans and binds available devices from platform data and the FDT. * This calls dm_init() to set up Driver Model structures. * - * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC - * flag. If false bind all drivers. + * @pre_reloc_only: If true, bind only nodes with special devicetree properties, + * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. * @return 0 if OK, -ve on error */ int dm_init_and_scan(bool pre_reloc_only); -- cgit v1.3.1 From 604b6696ed147bbd0cd9c7b2f319e7dd5096ca14 Mon Sep 17 00:00:00 2001 From: Mario Six Date: Thu, 4 Oct 2018 09:00:41 +0200 Subject: regmap: Fix documentation The documentation in regmap.h is not in kernel-doc format. Correct this. Reviewed-by: Anatolij Gustschin Reviewed-by: Simon Glass Signed-off-by: Mario Six --- include/regmap.h | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/regmap.h b/include/regmap.h index 6a574eaa412..32f75e06f59 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -21,8 +21,8 @@ struct regmap_range { /** * struct regmap - a way of accessing hardware/bus registers * - * @range_count: Number of ranges available within the map - * @ranges: Array of ranges + * @range_count: Number of ranges available within the map + * @ranges: Array of ranges */ struct regmap { int range_count; @@ -33,7 +33,28 @@ struct regmap { * Interface to provide access to registers either through a direct memory * bus or through a peripheral bus like I2C, SPI. */ + +/** + * regmap_write() - Write a 32-bit value to a regmap + * + * @map: Regmap to write to + * @offset: Offset in the regmap to write to + * @val: Data to write to the regmap at the specified offset + * + * Return: 0 if OK, -ve on error + */ int regmap_write(struct regmap *map, uint offset, uint val); + +/** + * regmap_read() - Read a 32-bit value from a regmap + * + * @map: Regmap to read from + * @offset: Offset in the regmap to read from + * @valp: Pointer to the buffer to receive the data read from the regmap + * at the specified offset + * + * Return: 0 if OK, -ve on error + */ int regmap_read(struct regmap *map, uint offset, uint *valp); #define regmap_write32(map, ptr, member, val) \ @@ -49,31 +70,36 @@ int regmap_read(struct regmap *map, uint offset, uint *valp); * @offset: Offset of the memory * @mask: Mask to apply to the read value * @val: Value to apply to the value to write + * Return: 0 if OK, -ve on error */ int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val); /** * regmap_init_mem() - Set up a new register map that uses memory access * - * Use regmap_uninit() to free it. - * * @node: Device node that uses this map * @mapp: Returns allocated map + * Return: 0 if OK, -ve on error + * + * Use regmap_uninit() to free it. */ int regmap_init_mem(ofnode node, struct regmap **mapp); /** - * regmap_init_mem_platdata() - Set up a new memory register map for of-platdata + * regmap_init_mem_platdata() - Set up a new memory register map for + * of-platdata + * + * @dev: Device that uses this map + * @reg: List of address, size pairs + * @count: Number of pairs (e.g. 1 if the regmap has a single entry) + * @mapp: Returns allocated map + * Return: 0 if OK, -ve on error * * This creates a new regmap with a list of regions passed in, rather than * using the device tree. It only supports 32-bit machines. * * Use regmap_uninit() to free it. * - * @dev: Device that uses this map - * @reg: List of address, size pairs - * @count: Number of pairs (e.g. 1 if the regmap has a single entry) - * @mapp: Returns allocated map */ int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count, struct regmap **mapp); @@ -83,11 +109,15 @@ int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count, * * @map: Regmap to query * @range_num: Range to look up + * Return: Pointer to the range in question if OK, NULL on error */ void *regmap_get_range(struct regmap *map, unsigned int range_num); /** * regmap_uninit() - free a previously inited regmap + * + * @map: Regmap to free + * Return: 0 if OK, -ve on error */ int regmap_uninit(struct regmap *map); -- cgit v1.3.1 From 84ff8f622d2e2aeb67c1cec1c2c814895648fca8 Mon Sep 17 00:00:00 2001 From: Mario Six Date: Mon, 15 Oct 2018 09:24:10 +0200 Subject: regmap: Add raw read/write functions The regmap functions currently assume that all register map accesses have a data width of 32 bits, but there are maps that have different widths. To rectify this, implement the regmap_raw_read and regmap_raw_write functions from the Linux kernel API that specify the width of a desired read or write operation on a regmap. Implement the regmap_read and regmap_write functions using these raw functions in a backwards-compatible manner. Reviewed-by: Anatolij Gustschin Signed-off-by: Mario Six Reviewed-by: Simon Glass --- drivers/core/regmap.c | 64 +++++++++++++++++++++++++++++++++++++++++++++------ include/regmap.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c index 154426269d9..916d9272ea0 100644 --- a/drivers/core/regmap.c +++ b/drivers/core/regmap.c @@ -188,22 +188,72 @@ int regmap_uninit(struct regmap *map) return 0; } +int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len) +{ + void *ptr; + + ptr = map_physmem(map->ranges[0].start + offset, val_len, MAP_NOCACHE); + + switch (val_len) { + case REGMAP_SIZE_8: + *((u8 *)valp) = readb((u8 *)ptr); + break; + case REGMAP_SIZE_16: + *((u16 *)valp) = readw((u16 *)ptr); + break; + case REGMAP_SIZE_32: + *((u32 *)valp) = readl((u32 *)ptr); + break; +#if defined(readq) + case REGMAP_SIZE_64: + *((u64 *)valp) = readq((u64 *)ptr); + break; +#endif + default: + debug("%s: regmap size %zu unknown\n", __func__, val_len); + return -EINVAL; + } + return 0; +} + int regmap_read(struct regmap *map, uint offset, uint *valp) { - u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE); + return regmap_raw_read(map, offset, valp, REGMAP_SIZE_32); +} - *valp = le32_to_cpu(readl(ptr)); +int regmap_raw_write(struct regmap *map, uint offset, const void *val, + size_t val_len) +{ + void *ptr; + + ptr = map_physmem(map->ranges[0].start + offset, val_len, MAP_NOCACHE); + + switch (val_len) { + case REGMAP_SIZE_8: + writeb(*((u8 *)val), (u8 *)ptr); + break; + case REGMAP_SIZE_16: + writew(*((u16 *)val), (u16 *)ptr); + break; + case REGMAP_SIZE_32: + writel(*((u32 *)val), (u32 *)ptr); + break; +#if defined(writeq) + case REGMAP_SIZE_64: + writeq(*((u64 *)val), (u64 *)ptr); + break; +#endif + default: + debug("%s: regmap size %zu unknown\n", __func__, val_len); + return -EINVAL; + } return 0; } int regmap_write(struct regmap *map, uint offset, uint val) { - u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE); - - writel(cpu_to_le32(val), ptr); - - return 0; + return regmap_raw_write(map, offset, &val, REGMAP_SIZE_32); } int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val) diff --git a/include/regmap.h b/include/regmap.h index 32f75e06f59..f23664e8ba1 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -7,6 +7,21 @@ #ifndef __REGMAP_H #define __REGMAP_H +/** + * enum regmap_size_t - Access sizes for regmap reads and writes + * + * @REGMAP_SIZE_8: 8-bit read/write access size + * @REGMAP_SIZE_16: 16-bit read/write access size + * @REGMAP_SIZE_32: 32-bit read/write access size + * @REGMAP_SIZE_64: 64-bit read/write access size + */ +enum regmap_size_t { + REGMAP_SIZE_8 = 1, + REGMAP_SIZE_16 = 2, + REGMAP_SIZE_32 = 4, + REGMAP_SIZE_64 = 8, +}; + /** * struct regmap_range - a register map range * @@ -41,6 +56,10 @@ struct regmap { * @offset: Offset in the regmap to write to * @val: Data to write to the regmap at the specified offset * + * Note that this function will only write values of 32 bit width to the + * regmap; if the size of data to be read is different, the regmap_raw_write + * function can be used. + * * Return: 0 if OK, -ve on error */ int regmap_write(struct regmap *map, uint offset, uint val); @@ -53,10 +72,49 @@ int regmap_write(struct regmap *map, uint offset, uint val); * @valp: Pointer to the buffer to receive the data read from the regmap * at the specified offset * + * Note that this function will only read values of 32 bit width from the + * regmap; if the size of data to be read is different, the regmap_raw_read + * function can be used. + * * Return: 0 if OK, -ve on error */ int regmap_read(struct regmap *map, uint offset, uint *valp); +/** + * regmap_raw_write() - Write a value of specified length to a regmap + * + * @map: Regmap to write to + * @offset: Offset in the regmap to write to + * @val: Value to write to the regmap at the specified offset + * @val_len: Length of the data to be written to the regmap + * + * Note that this function will, as opposed to regmap_write, write data of + * arbitrary length to the regmap, and not just 32-bit values, and is thus a + * generalized version of regmap_write. + * + * Return: 0 if OK, -ve on error + */ +int regmap_raw_write(struct regmap *map, uint offset, const void *val, + size_t val_len); + +/** + * regmap_raw_read() - Read a value of specified length from a regmap + * + * @map: Regmap to read from + * @offset: Offset in the regmap to read from + * @valp: Pointer to the buffer to receive the data read from the regmap + * at the specified offset + * @val_len: Length of the data to be read from the regmap + * + * Note that this function will, as opposed to regmap_read, read data of + * arbitrary length from the regmap, and not just 32-bit values, and is thus a + * generalized version of regmap_read. + * + * Return: 0 if OK, -ve on error + */ +int regmap_raw_read(struct regmap *map, uint offset, void *valp, + size_t val_len); + #define regmap_write32(map, ptr, member, val) \ regmap_write(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), val) -- cgit v1.3.1 From d5c7bd985d759b7aade2700c11890821e6187e4b Mon Sep 17 00:00:00 2001 From: Mario Six Date: Mon, 15 Oct 2018 09:24:11 +0200 Subject: regmap: Support reading from specific range It is useful to be able to treat the different ranges of a regmap separately to be able to use distinct offset for them, but this is currently not implemented in the regmap API. To preserve backwards compatibility, add regmap_read_range and regmap_write_range functions that take an additional parameter 'range_num' that identifies the range to operate on. Reviewed-by: Anatolij Gustschin Reviewed-by: Simon Glass Signed-off-by: Mario Six --- drivers/core/regmap.c | 49 ++++++++++++++++++++++++++++++++++++++++++++----- include/regmap.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c index 916d9272ea0..9b2e02af2ec 100644 --- a/drivers/core/regmap.c +++ b/drivers/core/regmap.c @@ -188,11 +188,25 @@ int regmap_uninit(struct regmap *map) return 0; } -int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len) +int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, + void *valp, size_t val_len) { + struct regmap_range *range; void *ptr; - ptr = map_physmem(map->ranges[0].start + offset, val_len, MAP_NOCACHE); + if (range_num >= map->range_count) { + debug("%s: range index %d larger than range count\n", + __func__, range_num); + return -ERANGE; + } + range = &map->ranges[range_num]; + + ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); + + if (offset + val_len > range->size) { + debug("%s: offset/size combination invalid\n", __func__); + return -ERANGE; + } switch (val_len) { case REGMAP_SIZE_8: @@ -213,20 +227,39 @@ int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len) debug("%s: regmap size %zu unknown\n", __func__, val_len); return -EINVAL; } + return 0; } +int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len) +{ + return regmap_raw_read_range(map, 0, offset, valp, val_len); +} + int regmap_read(struct regmap *map, uint offset, uint *valp) { return regmap_raw_read(map, offset, valp, REGMAP_SIZE_32); } -int regmap_raw_write(struct regmap *map, uint offset, const void *val, - size_t val_len) +int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, + const void *val, size_t val_len) { + struct regmap_range *range; void *ptr; - ptr = map_physmem(map->ranges[0].start + offset, val_len, MAP_NOCACHE); + if (range_num >= map->range_count) { + debug("%s: range index %d larger than range count\n", + __func__, range_num); + return -ERANGE; + } + range = &map->ranges[range_num]; + + ptr = map_physmem(range->start + offset, val_len, MAP_NOCACHE); + + if (offset + val_len > range->size) { + debug("%s: offset/size combination invalid\n", __func__); + return -ERANGE; + } switch (val_len) { case REGMAP_SIZE_8: @@ -251,6 +284,12 @@ int regmap_raw_write(struct regmap *map, uint offset, const void *val, return 0; } +int regmap_raw_write(struct regmap *map, uint offset, const void *val, + size_t val_len) +{ + return regmap_raw_write_range(map, 0, offset, val, val_len); +} + int regmap_write(struct regmap *map, uint offset, uint val) { return regmap_raw_write(map, offset, &val, REGMAP_SIZE_32); diff --git a/include/regmap.h b/include/regmap.h index f23664e8ba1..eba300da29b 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -115,6 +115,37 @@ int regmap_raw_write(struct regmap *map, uint offset, const void *val, int regmap_raw_read(struct regmap *map, uint offset, void *valp, size_t val_len); +/** + * regmap_raw_write_range() - Write a value of specified length to a range of a + * regmap + * + * @map: Regmap to write to + * @range_num: Number of the range in the regmap to write to + * @offset: Offset in the regmap to write to + * @val: Value to write to the regmap at the specified offset + * @val_len: Length of the data to be written to the regmap + * + * Return: 0 if OK, -ve on error + */ +int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, + const void *val, size_t val_len); + +/** + * regmap_raw_read_range() - Read a value of specified length from a range of a + * regmap + * + * @map: Regmap to read from + * @range_num: Number of the range in the regmap to write to + * @offset: Offset in the regmap to read from + * @valp: Pointer to the buffer to receive the data read from the regmap + * at the specified offset + * @val_len: Length of the data to be read from the regmap + * + * Return: 0 if OK, -ve on error + */ +int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, + void *valp, size_t val_len); + #define regmap_write32(map, ptr, member, val) \ regmap_write(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), val) -- cgit v1.3.1 From e936397ae98b3a67ba7deaef0cc8a56f36e66b58 Mon Sep 17 00:00:00 2001 From: Mario Six Date: Mon, 15 Oct 2018 09:24:12 +0200 Subject: regmap: Define regmap_{get,set} It would be convenient if one could use the regmap API in conjunction with register maps defined as structs (i.e. structs that directly mirror the memory layout of the registers in question). A similar approach was planned with the regmap_write32/regmap_read32 macros, but was never used. Hence, implement regmap_set/regmap_range_set and regmap_get/regmap_range_get macros, which, given a register map, a struct describing the layout of the register map, and a member name automatically produce regmap_read/regmap_write calls that access the specified member in the register map. Reviewed-by: Anatolij Gustschin Reviewed-by: Simon Glass Signed-off-by: Mario Six --- include/regmap.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/regmap.h b/include/regmap.h index eba300da29b..3b7eea5f493 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -146,11 +146,57 @@ int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, void *valp, size_t val_len); -#define regmap_write32(map, ptr, member, val) \ - regmap_write(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), val) +/** + * regmap_range_set() - Set a value in a regmap range described by a struct + * @map: Regmap in which a value should be set + * @range: Range of the regmap in which a value should be set + * @type: Structure type that describes the memory layout of the regmap range + * @member: Member of the describing structure that should be set in the regmap + * range + * @val: Value which should be written to the regmap range + */ +#define regmap_range_set(map, range, type, member, val) \ + do { \ + typeof(((type *)0)->member) __tmp = val; \ + regmap_raw_write_range(map, range, offsetof(type, member), \ + &__tmp, sizeof(((type *)0)->member)); \ + } while (0) + +/** + * regmap_set() - Set a value in a regmap described by a struct + * @map: Regmap in which a value should be set + * @type: Structure type that describes the memory layout of the regmap + * @member: Member of the describing structure that should be set in the regmap + * @val: Value which should be written to the regmap + */ +#define regmap_set(map, type, member, val) \ + regmap_range_set(map, 0, type, member, val) -#define regmap_read32(map, ptr, member, valp) \ - regmap_read(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), valp) +/** + * regmap_range_get() - Get a value from a regmap range described by a struct + * @map: Regmap from which a value should be read + * @range: Range of the regmap from which a value should be read + * @type: Structure type that describes the memory layout of the regmap + * range + * @member: Member of the describing structure that should be read in the + * regmap range + * @valp: Variable that receives the value read from the regmap range + */ +#define regmap_range_get(map, range, type, member, valp) \ + regmap_raw_read_range(map, range, offsetof(type, member), \ + (void *)valp, sizeof(((type *)0)->member)) + +/** + * regmap_get() - Get a value from a regmap described by a struct + * @map: Regmap from which a value should be read + * @type: Structure type that describes the memory layout of the regmap + * range + * @member: Member of the describing structure that should be read in the + * regmap + * @valp: Variable that receives the value read from the regmap + */ +#define regmap_get(map, type, member, valp) \ + regmap_range_get(map, 0, type, member, valp) /** * regmap_update_bits() - Perform a read/modify/write using a mask -- cgit v1.3.1 From 9b77fe3b80b038af7114f7dae4934773bb026f8e Mon Sep 17 00:00:00 2001 From: Mario Six Date: Mon, 15 Oct 2018 09:24:14 +0200 Subject: regmap: Add endianness support Add support for switching the endianness of regmap accesses via the "little-endian", "big-endian", and "native-endian" boolean properties in the device tree. The default endianness is native endianness. Signed-off-by: Mario Six Reviewed-by: Simon Glass Reviewed-by: Daniel Schwierzeck --- drivers/core/regmap.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++---- include/regmap.h | 14 ++++++ 2 files changed, 138 insertions(+), 10 deletions(-) (limited to 'include') diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c index 9b2e02af2ec..5ef0f71c8b7 100644 --- a/drivers/core/regmap.c +++ b/drivers/core/regmap.c @@ -164,6 +164,15 @@ int regmap_init_mem(ofnode node, struct regmap **mapp) return ret; } + if (ofnode_read_bool(node, "little-endian")) + map->endianness = REGMAP_LITTLE_ENDIAN; + else if (ofnode_read_bool(node, "big-endian")) + map->endianness = REGMAP_BIG_ENDIAN; + else if (ofnode_read_bool(node, "native-endian")) + map->endianness = REGMAP_NATIVE_ENDIAN; + else /* Default: native endianness */ + map->endianness = REGMAP_NATIVE_ENDIAN; + *mapp = map; return 0; @@ -188,6 +197,55 @@ int regmap_uninit(struct regmap *map) return 0; } +static inline u8 __read_8(u8 *addr, enum regmap_endianness_t endianness) +{ + return readb(addr); +} + +static inline u16 __read_16(u16 *addr, enum regmap_endianness_t endianness) +{ + switch (endianness) { + case REGMAP_LITTLE_ENDIAN: + return in_le16(addr); + case REGMAP_BIG_ENDIAN: + return in_be16(addr); + case REGMAP_NATIVE_ENDIAN: + return readw(addr); + } + + return readw(addr); +} + +static inline u32 __read_32(u32 *addr, enum regmap_endianness_t endianness) +{ + switch (endianness) { + case REGMAP_LITTLE_ENDIAN: + return in_le32(addr); + case REGMAP_BIG_ENDIAN: + return in_be32(addr); + case REGMAP_NATIVE_ENDIAN: + return readl(addr); + } + + return readl(addr); +} + +#if defined(in_le64) && defined(in_be64) && defined(readq) +static inline u64 __read_64(u64 *addr, enum regmap_endianness_t endianness) +{ + switch (endianness) { + case REGMAP_LITTLE_ENDIAN: + return in_le64(addr); + case REGMAP_BIG_ENDIAN: + return in_be64(addr); + case REGMAP_NATIVE_ENDIAN: + return readq(addr); + } + + return readq(addr); +} +#endif + int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, void *valp, size_t val_len) { @@ -210,17 +268,17 @@ int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, switch (val_len) { case REGMAP_SIZE_8: - *((u8 *)valp) = readb((u8 *)ptr); + *((u8 *)valp) = __read_8(ptr, map->endianness); break; case REGMAP_SIZE_16: - *((u16 *)valp) = readw((u16 *)ptr); + *((u16 *)valp) = __read_16(ptr, map->endianness); break; case REGMAP_SIZE_32: - *((u32 *)valp) = readl((u32 *)ptr); + *((u32 *)valp) = __read_32(ptr, map->endianness); break; -#if defined(readq) +#if defined(in_le64) && defined(in_be64) && defined(readq) case REGMAP_SIZE_64: - *((u64 *)valp) = readq((u64 *)ptr); + *((u64 *)valp) = __read_64(ptr, map->endianness); break; #endif default: @@ -241,6 +299,62 @@ int regmap_read(struct regmap *map, uint offset, uint *valp) return regmap_raw_read(map, offset, valp, REGMAP_SIZE_32); } +static inline void __write_8(u8 *addr, const u8 *val, + enum regmap_endianness_t endianness) +{ + writeb(*val, addr); +} + +static inline void __write_16(u16 *addr, const u16 *val, + enum regmap_endianness_t endianness) +{ + switch (endianness) { + case REGMAP_NATIVE_ENDIAN: + writew(*val, addr); + break; + case REGMAP_LITTLE_ENDIAN: + out_le16(addr, *val); + break; + case REGMAP_BIG_ENDIAN: + out_be16(addr, *val); + break; + } +} + +static inline void __write_32(u32 *addr, const u32 *val, + enum regmap_endianness_t endianness) +{ + switch (endianness) { + case REGMAP_NATIVE_ENDIAN: + writel(*val, addr); + break; + case REGMAP_LITTLE_ENDIAN: + out_le32(addr, *val); + break; + case REGMAP_BIG_ENDIAN: + out_be32(addr, *val); + break; + } +} + +#if defined(out_le64) && defined(out_be64) && defined(writeq) +static inline void __write_64(u64 *addr, const u64 *val, + enum regmap_endianness_t endianness) +{ + switch (endianness) { + case REGMAP_NATIVE_ENDIAN: + writeq(*val, addr); + break; + case REGMAP_LITTLE_ENDIAN: + out_le64(addr, *val); + break; + case REGMAP_BIG_ENDIAN: + out_be64(addr, *val); + break; + } +} +#endif + int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, const void *val, size_t val_len) { @@ -263,17 +377,17 @@ int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, switch (val_len) { case REGMAP_SIZE_8: - writeb(*((u8 *)val), (u8 *)ptr); + __write_8(ptr, val, map->endianness); break; case REGMAP_SIZE_16: - writew(*((u16 *)val), (u16 *)ptr); + __write_16(ptr, val, map->endianness); break; case REGMAP_SIZE_32: - writel(*((u32 *)val), (u32 *)ptr); + __write_32(ptr, val, map->endianness); break; -#if defined(writeq) +#if defined(out_le64) && defined(out_be64) && defined(writeq) case REGMAP_SIZE_64: - writeq(*((u64 *)val), (u64 *)ptr); + __write_64(ptr, val, map->endianness); break; #endif default: diff --git a/include/regmap.h b/include/regmap.h index 3b7eea5f493..98860c27326 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -22,6 +22,19 @@ enum regmap_size_t { REGMAP_SIZE_64 = 8, }; +/** + * enum regmap_endianness_t - Endianness for regmap reads and writes + * + * @REGMAP_NATIVE_ENDIAN: Native endian read/write accesses + * @REGMAP_LITTLE_ENDIAN: Little endian read/write accesses + * @REGMAP_BIG_ENDIAN: Big endian read/write accesses + */ +enum regmap_endianness_t { + REGMAP_NATIVE_ENDIAN, + REGMAP_LITTLE_ENDIAN, + REGMAP_BIG_ENDIAN, +}; + /** * struct regmap_range - a register map range * @@ -40,6 +53,7 @@ struct regmap_range { * @ranges: Array of ranges */ struct regmap { + enum regmap_endianness_t endianness; int range_count; struct regmap_range ranges[0]; }; -- cgit v1.3.1 From a6d4b0608b79023c703ca2ad1cbdfa784160449b Mon Sep 17 00:00:00 2001 From: Mario Six Date: Mon, 15 Oct 2018 09:24:15 +0200 Subject: regmap: Add overview documentation Add some overview documentation that explains the purpose and some of the features and limitations of the regmap interface. Reviewed-by: Bin Meng Signed-off-by: Mario Six --- include/regmap.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'include') diff --git a/include/regmap.h b/include/regmap.h index 98860c27326..b2b733fda68 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -7,6 +7,33 @@ #ifndef __REGMAP_H #define __REGMAP_H +/** + * DOC: Overview + * + * Regmaps are an abstraction mechanism that allows device drivers to access + * register maps irrespective of the underlying bus architecture. This entails + * that for devices that support multiple busses (e.g. I2C and SPI for a GPIO + * expander chip) only one driver has to be written. This driver will + * instantiate a regmap with a backend depending on the bus the device is + * attached to, and use the regmap API to access the register map through that + * bus transparently. + * + * Read and write functions are supplied, which can read/write data of + * arbitrary length from/to the regmap. + * + * The endianness of regmap accesses is selectable for each map through device + * tree settings via the boolean "little-endian", "big-endian", and + * "native-endian" properties. + * + * Furthermore, the register map described by a regmap can be split into + * multiple disjoint areas called ranges. In this way, register maps with + * "holes", i.e. areas of addressable memory that are not part of the register + * map, can be accessed in a concise manner. + * + * Currently, only a bare "mem" backend for regmaps is supported, which + * accesses the register map as regular IO-mapped memory. + */ + /** * enum regmap_size_t - Access sizes for regmap reads and writes * -- cgit v1.3.1 From 651d0c019a428ca8b14352210c1ce7804dd51fae Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Mon, 15 Oct 2018 02:20:57 -0700 Subject: dm: core: Allow uclass to set up a device's child after it is probed Some buses need to set up their child devices after they are probed. Support a common child_post_probe() method for the uclass. With this change, the two APIs uclass_pre_probe_device() and uclass_post_probe_device() become symmetric. Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- drivers/core/uclass.c | 13 ++++++++++++- include/dm/uclass.h | 4 +++- 2 files changed, 15 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c index 3113d6a56ba..3c7b9cf0ad8 100644 --- a/drivers/core/uclass.c +++ b/drivers/core/uclass.c @@ -687,8 +687,19 @@ int uclass_pre_probe_device(struct udevice *dev) int uclass_post_probe_device(struct udevice *dev) { - struct uclass_driver *uc_drv = dev->uclass->uc_drv; + struct uclass_driver *uc_drv; + int ret; + + if (dev->parent) { + uc_drv = dev->parent->uclass->uc_drv; + if (uc_drv->child_post_probe) { + ret = uc_drv->child_post_probe(dev); + if (ret) + return ret; + } + } + uc_drv = dev->uclass->uc_drv; if (uc_drv->post_probe) return uc_drv->post_probe(dev); diff --git a/include/dm/uclass.h b/include/dm/uclass.h index eebf2d5614c..4ef0d0f0c01 100644 --- a/include/dm/uclass.h +++ b/include/dm/uclass.h @@ -61,7 +61,8 @@ struct udevice; * @post_probe: Called after a new device is probed * @pre_remove: Called before a device is removed * @child_post_bind: Called after a child is bound to a device in this uclass - * @child_pre_probe: Called before a child is probed in this uclass + * @child_pre_probe: Called before a child in this uclass is probed + * @child_post_probe: Called after a child in this uclass is probed * @init: Called to set up the uclass * @destroy: Called to destroy the uclass * @priv_auto_alloc_size: If non-zero this is the size of the private data @@ -94,6 +95,7 @@ struct uclass_driver { int (*pre_remove)(struct udevice *dev); int (*child_post_bind)(struct udevice *dev); int (*child_pre_probe)(struct udevice *dev); + int (*child_post_probe)(struct udevice *dev); int (*init)(struct uclass *class); int (*destroy)(struct uclass *class); int priv_auto_alloc_size; -- cgit v1.3.1 From d92878aa4089e1c91f8a6cb7b9655cd27e6ea23c Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Mon, 15 Oct 2018 02:20:58 -0700 Subject: test: dm: core: Add test case for uclass driver's child_post_probe() Add test case to cover uclass driver's child_post_probe() method. Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- include/dm/test.h | 1 + test/dm/bus.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/dm/test-fdt.c | 7 +++++-- 3 files changed, 51 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/dm/test.h b/include/dm/test.h index 83418eb4822..07385cd531f 100644 --- a/include/dm/test.h +++ b/include/dm/test.h @@ -69,6 +69,7 @@ struct dm_test_priv { int op_count[DM_TEST_OP_COUNT]; int uclass_flag; int uclass_total; + int uclass_postp; }; /** diff --git a/test/dm/bus.c b/test/dm/bus.c index d0cd5a009cd..93f3acd4308 100644 --- a/test/dm/bus.c +++ b/test/dm/bus.c @@ -63,6 +63,15 @@ static int testbus_child_pre_probe_uclass(struct udevice *dev) return 0; } +static int testbus_child_post_probe_uclass(struct udevice *dev) +{ + struct dm_test_priv *priv = dev_get_priv(dev); + + priv->uclass_postp++; + + return 0; +} + static int testbus_child_post_remove(struct udevice *dev) { struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev); @@ -102,6 +111,7 @@ UCLASS_DRIVER(testbus) = { .id = UCLASS_TEST_BUS, .flags = DM_UC_FLAG_SEQ_ALIAS, .child_pre_probe = testbus_child_pre_probe_uclass, + .child_post_probe = testbus_child_post_probe_uclass, }; /* Test that we can probe for children */ @@ -547,3 +557,38 @@ static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts) } DM_TEST(dm_test_bus_child_pre_probe_uclass, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* + * Test that the bus' uclass' child_post_probe() is called after the + * device's probe() method + */ +static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts) +{ + struct udevice *bus, *dev; + int child_count; + + /* + * See testfdt_drv_probe() which effectively initializes that + * the uclass postp flag is set to a value + */ + ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); + for (device_find_first_child(bus, &dev), child_count = 0; + dev; + device_find_next_child(&dev)) { + struct dm_test_priv *priv = dev_get_priv(dev); + + /* Check that things happened in the right order */ + ut_asserteq_ptr(NULL, priv); + ut_assertok(device_probe(dev)); + + priv = dev_get_priv(dev); + ut_assert(priv != NULL); + ut_asserteq(0, priv->uclass_postp); + child_count++; + } + ut_asserteq(3, child_count); + + return 0; +} +DM_TEST(dm_test_bus_child_post_probe_uclass, + DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c index 3da384f4c58..e43acb21d5e 100644 --- a/test/dm/test-fdt.c +++ b/test/dm/test-fdt.c @@ -55,10 +55,13 @@ static int testfdt_drv_probe(struct udevice *dev) /* * If this device is on a bus, the uclass_flag will be set before - * calling this function. This is used by - * dm_test_bus_child_pre_probe_uclass(). + * calling this function. In the meantime the uclass_postp is + * initlized to a value -1. These are used respectively by + * dm_test_bus_child_pre_probe_uclass() and + * dm_test_bus_child_post_probe_uclass(). */ priv->uclass_total += priv->uclass_flag; + priv->uclass_postp = -1; return 0; } -- cgit v1.3.1 From 8fb49b4c7a820461db7c11dce767f36fd6395cac Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Mon, 15 Oct 2018 02:21:00 -0700 Subject: dm: Add a new uclass driver for VirtIO transport devices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds a new virtio uclass driver for “virtio” [1] family of devices that are are found in virtual environments like QEMU, yet by design they look like physical devices to the guest. The uclass driver provides child_pre_probe() and child_post_probe() methods to do some common operations for virtio device drivers like device and driver supported feature negotiation, etc. [1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/virtio/Kconfig | 25 ++ drivers/virtio/Makefile | 6 + drivers/virtio/virtio-uclass.c | 369 +++++++++++++++++++++ include/dm/uclass-id.h | 1 + include/virtio.h | 707 +++++++++++++++++++++++++++++++++++++++++ include/virtio_types.h | 24 ++ 8 files changed, 1135 insertions(+) create mode 100644 drivers/virtio/Kconfig create mode 100644 drivers/virtio/Makefile create mode 100644 drivers/virtio/virtio-uclass.c create mode 100644 include/virtio.h create mode 100644 include/virtio_types.h (limited to 'include') diff --git a/drivers/Kconfig b/drivers/Kconfig index 927a2b87f6a..4ac823d962e 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -112,6 +112,8 @@ source "drivers/usb/Kconfig" source "drivers/video/Kconfig" +source "drivers/virtio/Kconfig" + source "drivers/w1/Kconfig" source "drivers/w1-eeprom/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index fb38b675413..4453c62ad33 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_$(SPL_TPL_)SERIAL_SUPPORT) += serial/ obj-$(CONFIG_$(SPL_TPL_)SPI_FLASH_SUPPORT) += mtd/spi/ obj-$(CONFIG_$(SPL_TPL_)SPI_SUPPORT) += spi/ obj-$(CONFIG_$(SPL_TPL_)TIMER) += timer/ +obj-$(CONFIG_$(SPL_TPL_)VIRTIO) += virtio/ obj-$(CONFIG_$(SPL_)DM_MAILBOX) += mailbox/ obj-$(CONFIG_$(SPL_)REMOTEPROC) += remoteproc/ diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig new file mode 100644 index 00000000000..82fc5368317 --- /dev/null +++ b/drivers/virtio/Kconfig @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (C) 2018, Tuomas Tynkkynen +# Copyright (C) 2018, Bin Meng +# +# VirtIO is a virtualization standard for network and disk device drivers +# where just the guest's device driver "knows" it is running in a virtual +# environment, and cooperates with the hypervisor. This enables guests to +# get high performance network and disk operations, and gives most of the +# performance benefits of paravirtualization. In the U-Boot case, the guest +# is U-Boot itself, while the virtual environment are normally QEMU targets +# like ARM, RISC-V and x86. +# +# See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for +# the VirtIO specification v1.0. + +menu "VirtIO Drivers" + +config VIRTIO + bool + help + This option is selected by any driver which implements the virtio + transport, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI. + +endmenu diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile new file mode 100644 index 00000000000..23e7be7165e --- /dev/null +++ b/drivers/virtio/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (C) 2018, Tuomas Tynkkynen +# Copyright (C) 2018, Bin Meng + +obj-y += virtio-uclass.o diff --git a/drivers/virtio/virtio-uclass.c b/drivers/virtio/virtio-uclass.c new file mode 100644 index 00000000000..34397d7dbb1 --- /dev/null +++ b/drivers/virtio/virtio-uclass.c @@ -0,0 +1,369 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * VirtIO is a virtualization standard for network and disk device drivers + * where just the guest's device driver "knows" it is running in a virtual + * environment, and cooperates with the hypervisor. This enables guests to + * get high performance network and disk operations, and gives most of the + * performance benefits of paravirtualization. In the U-Boot case, the guest + * is U-Boot itself, while the virtual environment are normally QEMU targets + * like ARM, RISC-V and x86. + * + * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for + * the VirtIO specification v1.0. + */ + +#include +#include +#include +#include +#include + +static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = { + [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME, + [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME, +}; + +int virtio_get_config(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->get_config(vdev->parent, offset, buf, len); +} + +int virtio_set_config(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->set_config(vdev->parent, offset, buf, len); +} + +int virtio_generation(struct udevice *vdev, u32 *counter) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + if (!ops->generation) + return -ENOSYS; + + return ops->generation(vdev->parent, counter); +} + +int virtio_get_status(struct udevice *vdev, u8 *status) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->get_status(vdev->parent, status); +} + +int virtio_set_status(struct udevice *vdev, u8 status) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->set_status(vdev->parent, status); +} + +int virtio_reset(struct udevice *vdev) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->reset(vdev->parent); +} + +int virtio_get_features(struct udevice *vdev, u64 *features) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->get_features(vdev->parent, features); +} + +int virtio_set_features(struct udevice *vdev) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->set_features(vdev->parent); +} + +int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs, + struct virtqueue *vqs[]) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->find_vqs(vdev->parent, nvqs, vqs); +} + +int virtio_del_vqs(struct udevice *vdev) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->del_vqs(vdev->parent); +} + +int virtio_notify(struct udevice *vdev, struct virtqueue *vq) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->notify(vdev->parent, vq); +} + +void virtio_add_status(struct udevice *vdev, u8 status) +{ + u8 old; + + if (!virtio_get_status(vdev, &old)) + virtio_set_status(vdev, old | status); +} + +int virtio_finalize_features(struct udevice *vdev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); + u8 status; + int ret; + + ret = virtio_set_features(vdev); + if (ret) + return ret; + + if (uc_priv->legacy) + return 0; + + virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK); + ret = virtio_get_status(vdev, &status); + if (ret) + return ret; + if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) { + debug("(%s): device refuses features %x\n", vdev->name, status); + return -ENODEV; + } + + return 0; +} + +void virtio_driver_features_init(struct virtio_dev_priv *priv, + const u32 *feature, + u32 feature_size, + const u32 *feature_legacy, + u32 feature_legacy_size) +{ + priv->feature_table = feature; + priv->feature_table_size = feature_size; + priv->feature_table_legacy = feature_legacy; + priv->feature_table_size_legacy = feature_legacy_size; +} + +int virtio_init(void) +{ + struct udevice *bus; + int ret; + + /* Enumerate all known virtio devices */ + ret = uclass_first_device(UCLASS_VIRTIO, &bus); + if (ret) + return ret; + + while (bus) { + ret = uclass_next_device(&bus); + if (ret) + break; + } + + return ret; +} + +static int virtio_uclass_pre_probe(struct udevice *udev) +{ + struct dm_virtio_ops *ops; + + ops = (struct dm_virtio_ops *)(udev->driver->ops); + + /* + * Check virtio transport driver ops here so that we don't need + * check these ops each time when the virtio_xxx APIs are called. + * + * Only generation op is optional. All other ops are must-have. + */ + if (!ops->get_config || !ops->set_config || + !ops->get_status || !ops->set_status || + !ops->get_features || !ops->set_features || + !ops->find_vqs || !ops->del_vqs || + !ops->reset || !ops->notify) + return -ENOENT; + + return 0; +} + +static int virtio_uclass_post_probe(struct udevice *udev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + char dev_name[30], *str; + struct udevice *vdev; + int ret; + + if (uc_priv->device > VIRTIO_ID_MAX_NUM) { + debug("(%s): virtio device ID %d exceeds maximum num\n", + udev->name, uc_priv->device); + return 0; + } + + if (!virtio_drv_name[uc_priv->device]) { + debug("(%s): underlying virtio device driver unavailable\n", + udev->name); + return 0; + } + + snprintf(dev_name, sizeof(dev_name), "%s#%d", + virtio_drv_name[uc_priv->device], udev->seq); + str = strdup(dev_name); + if (!str) + return -ENOMEM; + + ret = device_bind_driver(udev, virtio_drv_name[uc_priv->device], + str, &vdev); + if (ret == -ENOENT) { + debug("(%s): no driver configured\n", udev->name); + return 0; + } + if (ret) { + free(str); + return ret; + } + device_set_name_alloced(vdev); + + INIT_LIST_HEAD(&uc_priv->vqs); + + return 0; +} + +static int virtio_uclass_child_post_bind(struct udevice *vdev) +{ + /* Acknowledge that we've seen the device */ + virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE); + + return 0; +} + +static int virtio_uclass_child_pre_probe(struct udevice *vdev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); + u64 device_features; + u64 driver_features; + u64 driver_features_legacy; + int i; + int ret; + + /* + * Save the real virtio device (eg: virtio-net, virtio-blk) to + * the transport (parent) device's uclass priv for future use. + */ + uc_priv->vdev = vdev; + + /* + * We always start by resetting the device, in case a previous driver + * messed it up. This also tests that code path a little. + */ + ret = virtio_reset(vdev); + if (ret) + goto err; + + /* We have a driver! */ + virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER); + + /* Figure out what features the device supports */ + virtio_get_features(vdev, &device_features); + debug("(%s) plain device features supported %016llx\n", + vdev->name, device_features); + if (!(device_features & (1ULL << VIRTIO_F_VERSION_1))) + uc_priv->legacy = true; + + /* Figure out what features the driver supports */ + driver_features = 0; + for (i = 0; i < uc_priv->feature_table_size; i++) { + unsigned int f = uc_priv->feature_table[i]; + + WARN_ON(f >= 64); + driver_features |= (1ULL << f); + } + + /* Some drivers have a separate feature table for virtio v1.0 */ + if (uc_priv->feature_table_legacy) { + driver_features_legacy = 0; + for (i = 0; i < uc_priv->feature_table_size_legacy; i++) { + unsigned int f = uc_priv->feature_table_legacy[i]; + + WARN_ON(f >= 64); + driver_features_legacy |= (1ULL << f); + } + } else { + driver_features_legacy = driver_features; + } + + if (uc_priv->legacy) { + debug("(%s): legacy virtio device\n", vdev->name); + uc_priv->features = driver_features_legacy & device_features; + } else { + debug("(%s): v1.0 complaint virtio device\n", vdev->name); + uc_priv->features = driver_features & device_features; + } + + /* Transport features always preserved to pass to finalize_features */ + for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) + if ((device_features & (1ULL << i)) && + (i == VIRTIO_F_VERSION_1)) + __virtio_set_bit(vdev->parent, i); + + debug("(%s) final negotiated features supported %016llx\n", + vdev->name, uc_priv->features); + ret = virtio_finalize_features(vdev); + if (ret) + goto err; + + return 0; + +err: + virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED); + return ret; +} + +static int virtio_uclass_child_post_probe(struct udevice *vdev) +{ + /* Indicates that the driver is set up and ready to drive the device */ + virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK); + + return 0; +} + +UCLASS_DRIVER(virtio) = { + .name = "virtio", + .id = UCLASS_VIRTIO, + .flags = DM_UC_FLAG_SEQ_ALIAS, + .pre_probe = virtio_uclass_pre_probe, + .post_probe = virtio_uclass_post_probe, + .child_post_bind = virtio_uclass_child_post_bind, + .child_pre_probe = virtio_uclass_child_pre_probe, + .child_post_probe = virtio_uclass_child_post_probe, + .per_device_auto_alloc_size = sizeof(struct virtio_dev_priv), +}; diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h index 269a2c6e722..c91dca1f824 100644 --- a/include/dm/uclass-id.h +++ b/include/dm/uclass-id.h @@ -96,6 +96,7 @@ enum uclass_id { UCLASS_VIDEO_BRIDGE, /* Video bridge, e.g. DisplayPort to LVDS */ UCLASS_VIDEO_CONSOLE, /* Text console driver for video device */ UCLASS_VIDEO_OSD, /* On-screen display */ + UCLASS_VIRTIO, /* VirtIO transport device */ UCLASS_W1, /* Dallas 1-Wire bus */ UCLASS_W1_EEPROM, /* one-wire EEPROMs */ UCLASS_WDT, /* Watchdot Timer driver */ diff --git a/include/virtio.h b/include/virtio.h new file mode 100644 index 00000000000..654fdf154b6 --- /dev/null +++ b/include/virtio.h @@ -0,0 +1,707 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * VirtIO is a virtualization standard for network and disk device drivers + * where just the guest's device driver "knows" it is running in a virtual + * environment, and cooperates with the hypervisor. This enables guests to + * get high performance network and disk operations, and gives most of the + * performance benefits of paravirtualization. In the U-Boot case, the guest + * is U-Boot itself, while the virtual environment are normally QEMU targets + * like ARM, RISC-V and x86. + * + * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for + * the VirtIO specification v1.0. + * + * This file is largely based on Linux kernel virtio_*.h files + */ + +#ifndef __VIRTIO_H__ +#define __VIRTIO_H__ + +#define VIRTIO_ID_NET 1 /* virtio net */ +#define VIRTIO_ID_BLOCK 2 /* virtio block */ +#define VIRTIO_ID_MAX_NUM 3 + +#define VIRTIO_NET_DRV_NAME "virtio-net" +#define VIRTIO_BLK_DRV_NAME "virtio-blk" + +/* Status byte for guest to report progress, and synchronize features */ + +/* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */ +#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1 +/* We have found a driver for the device */ +#define VIRTIO_CONFIG_S_DRIVER 2 +/* Driver has used its parts of the config, and is happy */ +#define VIRTIO_CONFIG_S_DRIVER_OK 4 +/* Driver has finished configuring features */ +#define VIRTIO_CONFIG_S_FEATURES_OK 8 +/* Device entered invalid state, driver must reset it */ +#define VIRTIO_CONFIG_S_NEEDS_RESET 0x40 +/* We've given up on this device */ +#define VIRTIO_CONFIG_S_FAILED 0x80 + +/* + * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END + * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.), + * the rest are per-device feature bits. + */ +#define VIRTIO_TRANSPORT_F_START 28 +#define VIRTIO_TRANSPORT_F_END 38 + +#ifndef VIRTIO_CONFIG_NO_LEGACY +/* + * Do we get callbacks when the ring is completely used, + * even if we've suppressed them? + */ +#define VIRTIO_F_NOTIFY_ON_EMPTY 24 + +/* Can the device handle any descriptor layout? */ +#define VIRTIO_F_ANY_LAYOUT 27 +#endif /* VIRTIO_CONFIG_NO_LEGACY */ + +/* v1.0 compliant */ +#define VIRTIO_F_VERSION_1 32 + +/* + * If clear - device has the IOMMU bypass quirk feature. + * If set - use platform tools to detect the IOMMU. + * + * Note the reverse polarity (compared to most other features), + * this is for compatibility with legacy systems. + */ +#define VIRTIO_F_IOMMU_PLATFORM 33 + +/* Does the device support Single Root I/O Virtualization? */ +#define VIRTIO_F_SR_IOV 37 + +/** + * virtio scatter-gather struct + * + * @addr: sg buffer address + * @lengh: sg buffer length + */ +struct virtio_sg { + void *addr; + size_t length; +}; + +struct virtqueue; + +/* virtio bus operations */ +struct dm_virtio_ops { + /** + * get_config() - read the value of a configuration field + * + * @vdev: the real virtio device + * @offset: the offset of the configuration field + * @buf: the buffer to write the field value into + * @len: the length of the buffer + * @return 0 if OK, -ve on error + */ + int (*get_config)(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len); + /** + * set_config() - write the value of a configuration field + * + * @vdev: the real virtio device + * @offset: the offset of the configuration field + * @buf: the buffer to read the field value from + * @len: the length of the buffer + * @return 0 if OK, -ve on error + */ + int (*set_config)(struct udevice *vdev, unsigned int offset, + const void *buf, unsigned int len); + /** + * generation() - config generation counter + * + * @vdev: the real virtio device + * @counter: the returned config generation counter + * @return 0 if OK, -ve on error + */ + int (*generation)(struct udevice *vdev, u32 *counter); + /** + * get_status() - read the status byte + * + * @vdev: the real virtio device + * @status: the returned status byte + * @return 0 if OK, -ve on error + */ + int (*get_status)(struct udevice *vdev, u8 *status); + /** + * set_status() - write the status byte + * + * @vdev: the real virtio device + * @status: the new status byte + * @return 0 if OK, -ve on error + */ + int (*set_status)(struct udevice *vdev, u8 status); + /** + * reset() - reset the device + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ + int (*reset)(struct udevice *vdev); + /** + * get_features() - get the array of feature bits for this device + * + * @vdev: the real virtio device + * @features: the first 32 feature bits (all we currently need) + * @return 0 if OK, -ve on error + */ + int (*get_features)(struct udevice *vdev, u64 *features); + /** + * set_features() - confirm what device features we'll be using + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ + int (*set_features)(struct udevice *vdev); + /** + * find_vqs() - find virtqueues and instantiate them + * + * @vdev: the real virtio device + * @nvqs: the number of virtqueues to find + * @vqs: on success, includes new virtqueues + * @return 0 if OK, -ve on error + */ + int (*find_vqs)(struct udevice *vdev, unsigned int nvqs, + struct virtqueue *vqs[]); + /** + * del_vqs() - free virtqueues found by find_vqs() + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ + int (*del_vqs)(struct udevice *vdev); + /** + * notify() - notify the device to process the queue + * + * @vdev: the real virtio device + * @vq: virtqueue to process + * @return 0 if OK, -ve on error + */ + int (*notify)(struct udevice *vdev, struct virtqueue *vq); +}; + +/* Get access to a virtio bus' operations */ +#define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops) + +/** + * virtio uclass per device private data + * + * @vqs: virtualqueue for the virtio device + * @vdev: the real virtio device underneath + * @legacy: is it a legacy device? + * @device: virtio device ID + * @vendor: virtio vendor ID + * @features: negotiated supported features + * @feature_table: an array of feature supported by the driver + * @feature_table_size: number of entries in the feature table array + * @feature_table_legacy: same as feature_table but working in legacy mode + * @feature_table_size_legacy: number of entries in feature table legacy array + */ +struct virtio_dev_priv { + struct list_head vqs; + struct udevice *vdev; + bool legacy; + u32 device; + u32 vendor; + u64 features; + const u32 *feature_table; + u32 feature_table_size; + const u32 *feature_table_legacy; + u32 feature_table_size_legacy; +}; + +/** + * virtio_get_config() - read the value of a configuration field + * + * @vdev: the real virtio device + * @offset: the offset of the configuration field + * @buf: the buffer to write the field value into + * @len: the length of the buffer + * @return 0 if OK, -ve on error + */ +int virtio_get_config(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len); + +/** + * virtio_set_config() - write the value of a configuration field + * + * @vdev: the real virtio device + * @offset: the offset of the configuration field + * @buf: the buffer to read the field value from + * @len: the length of the buffer + * @return 0 if OK, -ve on error + */ +int virtio_set_config(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len); + +/** + * virtio_generation() - config generation counter + * + * @vdev: the real virtio device + * @counter: the returned config generation counter + * @return 0 if OK, -ve on error + */ +int virtio_generation(struct udevice *vdev, u32 *counter); + +/** + * virtio_get_status() - read the status byte + * + * @vdev: the real virtio device + * @status: the returned status byte + * @return 0 if OK, -ve on error + */ +int virtio_get_status(struct udevice *vdev, u8 *status); + +/** + * virtio_set_status() - write the status byte + * + * @vdev: the real virtio device + * @status: the new status byte + * @return 0 if OK, -ve on error + */ +int virtio_set_status(struct udevice *vdev, u8 status); + +/** + * virtio_reset() - reset the device + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ +int virtio_reset(struct udevice *vdev); + +/** + * virtio_get_features() - get the array of feature bits for this device + * + * @vdev: the real virtio device + * @features: the first 32 feature bits (all we currently need) + * @return 0 if OK, -ve on error + */ +int virtio_get_features(struct udevice *vdev, u64 *features); + +/** + * virtio_set_features() - confirm what device features we'll be using + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ +int virtio_set_features(struct udevice *vdev); + +/** + * virtio_find_vqs() - find virtqueues and instantiate them + * + * @vdev: the real virtio device + * @nvqs: the number of virtqueues to find + * @vqs: on success, includes new virtqueues + * @return 0 if OK, -ve on error + */ +int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs, + struct virtqueue *vqs[]); + +/** + * virtio_del_vqs() - free virtqueues found by find_vqs() + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ +int virtio_del_vqs(struct udevice *vdev); + +/** + * virtio_notify() - notify the device to process the queue + * + * @vdev: the real virtio device + * @vq: virtqueue to process + * @return 0 if OK, -ve on error + */ +int virtio_notify(struct udevice *vdev, struct virtqueue *vq); + +/** + * virtio_add_status() - helper to set a new status code to the device + * + * @vdev: the real virtio device + * @status: new status code to be added + */ +void virtio_add_status(struct udevice *vdev, u8 status); + +/** + * virtio_finalize_features() - helper to finalize features + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ +int virtio_finalize_features(struct udevice *vdev); + +/** + * virtio_driver_features_init() - initialize driver supported features + * + * This fills in the virtio device parent per child private data with the given + * information, which contains driver supported features and legacy features. + * + * This API should be called in the virtio device driver's bind method, so that + * later virtio transport uclass driver can utilize the driver supplied features + * to negotiate with the device on the final supported features. + * + * @priv: virtio uclass per device private data + * @feature: an array of feature supported by the driver + * @feature_size: number of entries in the feature table array + * @feature_legacy: same as feature_table but working in legacy mode + * @feature_legacy_size:number of entries in feature table legacy array + */ +void virtio_driver_features_init(struct virtio_dev_priv *priv, + const u32 *feature, + u32 feature_size, + const u32 *feature_legacy, + u32 feature_legacy_size); + +/** + * virtio_init() - helper to enumerate all known virtio devices + * + * @return 0 if OK, -ve on error + */ +int virtio_init(void); + +static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val) +{ + if (little_endian) + return le16_to_cpu((__force __le16)val); + else + return be16_to_cpu((__force __be16)val); +} + +static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val) +{ + if (little_endian) + return (__force __virtio16)cpu_to_le16(val); + else + return (__force __virtio16)cpu_to_be16(val); +} + +static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val) +{ + if (little_endian) + return le32_to_cpu((__force __le32)val); + else + return be32_to_cpu((__force __be32)val); +} + +static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val) +{ + if (little_endian) + return (__force __virtio32)cpu_to_le32(val); + else + return (__force __virtio32)cpu_to_be32(val); +} + +static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val) +{ + if (little_endian) + return le64_to_cpu((__force __le64)val); + else + return be64_to_cpu((__force __be64)val); +} + +static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val) +{ + if (little_endian) + return (__force __virtio64)cpu_to_le64(val); + else + return (__force __virtio64)cpu_to_be64(val); +} + +/** + * __virtio_test_bit - helper to test feature bits + * + * For use by transports. Devices should normally use virtio_has_feature, + * which includes more checks. + * + * @udev: the transport device + * @fbit: the feature bit + */ +static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* Did you forget to fix assumptions on max features? */ + if (__builtin_constant_p(fbit)) + BUILD_BUG_ON(fbit >= 64); + else + WARN_ON(fbit >= 64); + + return uc_priv->features & BIT_ULL(fbit); +} + +/** + * __virtio_set_bit - helper to set feature bits + * + * For use by transports. + * + * @udev: the transport device + * @fbit: the feature bit + */ +static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* Did you forget to fix assumptions on max features? */ + if (__builtin_constant_p(fbit)) + BUILD_BUG_ON(fbit >= 64); + else + WARN_ON(fbit >= 64); + + uc_priv->features |= BIT_ULL(fbit); +} + +/** + * __virtio_clear_bit - helper to clear feature bits + * + * For use by transports. + * + * @vdev: the transport device + * @fbit: the feature bit + */ +static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* Did you forget to fix assumptions on max features? */ + if (__builtin_constant_p(fbit)) + BUILD_BUG_ON(fbit >= 64); + else + WARN_ON(fbit >= 64); + + uc_priv->features &= ~BIT_ULL(fbit); +} + +/** + * virtio_has_feature - helper to determine if this device has this feature + * + * Note this API is only usable after the virtio device driver's bind phase, + * as the feature has been negotiated between the device and the driver. + * + * @vdev: the virtio device + * @fbit: the feature bit + */ +static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit) +{ + if (!(vdev->flags & DM_FLAG_BOUND)) + WARN_ON(true); + + return __virtio_test_bit(vdev->parent, fbit); +} + +static inline bool virtio_legacy_is_little_endian(void) +{ +#ifdef __LITTLE_ENDIAN + return true; +#else + return false; +#endif +} + +static inline bool virtio_is_little_endian(struct udevice *vdev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); + + return !uc_priv->legacy || virtio_legacy_is_little_endian(); +} + +/* Memory accessors */ +static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val) +{ + return __virtio16_to_cpu(virtio_is_little_endian(vdev), val); +} + +static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val) +{ + return __cpu_to_virtio16(virtio_is_little_endian(vdev), val); +} + +static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val) +{ + return __virtio32_to_cpu(virtio_is_little_endian(vdev), val); +} + +static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val) +{ + return __cpu_to_virtio32(virtio_is_little_endian(vdev), val); +} + +static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val) +{ + return __virtio64_to_cpu(virtio_is_little_endian(vdev), val); +} + +static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val) +{ + return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); +} + +/* Read @count fields, @bytes each */ +static inline void __virtio_cread_many(struct udevice *vdev, + unsigned int offset, + void *buf, size_t count, size_t bytes) +{ + u32 old, gen; + int i; + + /* no need to check return value as generation can be optional */ + virtio_generation(vdev, &gen); + do { + old = gen; + + for (i = 0; i < count; i++) + virtio_get_config(vdev, offset + bytes * i, + buf + i * bytes, bytes); + + virtio_generation(vdev, &gen); + } while (gen != old); +} + +static inline void virtio_cread_bytes(struct udevice *vdev, + unsigned int offset, + void *buf, size_t len) +{ + __virtio_cread_many(vdev, offset, buf, len, 1); +} + +static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset) +{ + u8 ret; + + virtio_get_config(vdev, offset, &ret, sizeof(ret)); + return ret; +} + +static inline void virtio_cwrite8(struct udevice *vdev, + unsigned int offset, u8 val) +{ + virtio_set_config(vdev, offset, &val, sizeof(val)); +} + +static inline u16 virtio_cread16(struct udevice *vdev, + unsigned int offset) +{ + u16 ret; + + virtio_get_config(vdev, offset, &ret, sizeof(ret)); + return virtio16_to_cpu(vdev, (__force __virtio16)ret); +} + +static inline void virtio_cwrite16(struct udevice *vdev, + unsigned int offset, u16 val) +{ + val = (__force u16)cpu_to_virtio16(vdev, val); + virtio_set_config(vdev, offset, &val, sizeof(val)); +} + +static inline u32 virtio_cread32(struct udevice *vdev, + unsigned int offset) +{ + u32 ret; + + virtio_get_config(vdev, offset, &ret, sizeof(ret)); + return virtio32_to_cpu(vdev, (__force __virtio32)ret); +} + +static inline void virtio_cwrite32(struct udevice *vdev, + unsigned int offset, u32 val) +{ + val = (__force u32)cpu_to_virtio32(vdev, val); + virtio_set_config(vdev, offset, &val, sizeof(val)); +} + +static inline u64 virtio_cread64(struct udevice *vdev, + unsigned int offset) +{ + u64 ret; + + __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret)); + return virtio64_to_cpu(vdev, (__force __virtio64)ret); +} + +static inline void virtio_cwrite64(struct udevice *vdev, + unsigned int offset, u64 val) +{ + val = (__force u64)cpu_to_virtio64(vdev, val); + virtio_set_config(vdev, offset, &val, sizeof(val)); +} + +/* Config space read accessor */ +#define virtio_cread(vdev, structname, member, ptr) \ + do { \ + /* Must match the member's type, and be integer */ \ + if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \ + (*ptr) = 1; \ + \ + switch (sizeof(*ptr)) { \ + case 1: \ + *(ptr) = virtio_cread8(vdev, \ + offsetof(structname, member)); \ + break; \ + case 2: \ + *(ptr) = virtio_cread16(vdev, \ + offsetof(structname, member)); \ + break; \ + case 4: \ + *(ptr) = virtio_cread32(vdev, \ + offsetof(structname, member)); \ + break; \ + case 8: \ + *(ptr) = virtio_cread64(vdev, \ + offsetof(structname, member)); \ + break; \ + default: \ + WARN_ON(true); \ + } \ + } while (0) + +/* Config space write accessor */ +#define virtio_cwrite(vdev, structname, member, ptr) \ + do { \ + /* Must match the member's type, and be integer */ \ + if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \ + WARN_ON((*ptr) == 1); \ + \ + switch (sizeof(*ptr)) { \ + case 1: \ + virtio_cwrite8(vdev, \ + offsetof(structname, member), \ + *(ptr)); \ + break; \ + case 2: \ + virtio_cwrite16(vdev, \ + offsetof(structname, member), \ + *(ptr)); \ + break; \ + case 4: \ + virtio_cwrite32(vdev, \ + offsetof(structname, member), \ + *(ptr)); \ + break; \ + case 8: \ + virtio_cwrite64(vdev, \ + offsetof(structname, member), \ + *(ptr)); \ + break; \ + default: \ + WARN_ON(true); \ + } \ + } while (0) + +/* Conditional config space accessors */ +#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \ + ({ \ + int _r = 0; \ + if (!virtio_has_feature(vdev, fbit)) \ + _r = -ENOENT; \ + else \ + virtio_cread(vdev, structname, member, ptr); \ + _r; \ + }) + +#endif /* __VIRTIO_H__ */ diff --git a/include/virtio_types.h b/include/virtio_types.h new file mode 100644 index 00000000000..d700d1936d7 --- /dev/null +++ b/include/virtio_types.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * From Linux kernel include/uapi/linux/virtio_types.h + */ + +#ifndef _LINUX_VIRTIO_TYPES_H +#define _LINUX_VIRTIO_TYPES_H + +#include + +/* + * __virtio{16,32,64} have the following meaning: + * - __u{16,32,64} for virtio devices in legacy mode, accessed in native endian + * - __le{16,32,64} for standard-compliant virtio devices + */ + +typedef __u16 __bitwise __virtio16; +typedef __u32 __bitwise __virtio32; +typedef __u64 __bitwise __virtio64; + +#endif /* _LINUX_VIRTIO_TYPES_H */ -- cgit v1.3.1 From c011641ec4fcb61d1335f61b413117c1b7d83e5e Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Mon, 15 Oct 2018 02:21:01 -0700 Subject: virtio: Add codes for virtual queue/ring management This adds support for managing virtual queue/ring, the channel for high performance I/O between host and guest. Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- drivers/virtio/Makefile | 2 +- drivers/virtio/virtio_ring.c | 358 +++++++++++++++++++++++++++++++++++++++++++ include/virtio_ring.h | 320 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 679 insertions(+), 1 deletion(-) create mode 100644 drivers/virtio/virtio_ring.c create mode 100644 include/virtio_ring.h (limited to 'include') diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 23e7be7165e..17d264a7718 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -3,4 +3,4 @@ # Copyright (C) 2018, Tuomas Tynkkynen # Copyright (C) 2018, Bin Meng -obj-y += virtio-uclass.o +obj-y += virtio-uclass.o virtio_ring.o diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c new file mode 100644 index 00000000000..0eeb3501c20 --- /dev/null +++ b/drivers/virtio/virtio_ring.c @@ -0,0 +1,358 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * virtio ring implementation + */ + +#include +#include +#include +#include +#include +#include + +int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[], + unsigned int out_sgs, unsigned int in_sgs) +{ + struct vring_desc *desc; + unsigned int total_sg = out_sgs + in_sgs; + unsigned int i, n, avail, descs_used, uninitialized_var(prev); + int head; + + WARN_ON(total_sg == 0); + + head = vq->free_head; + + desc = vq->vring.desc; + i = head; + descs_used = total_sg; + + if (vq->num_free < descs_used) { + debug("Can't add buf len %i - avail = %i\n", + descs_used, vq->num_free); + /* + * FIXME: for historical reasons, we force a notify here if + * there are outgoing parts to the buffer. Presumably the + * host should service the ring ASAP. + */ + if (out_sgs) + virtio_notify(vq->vdev, vq); + return -ENOSPC; + } + + for (n = 0; n < out_sgs; n++) { + struct virtio_sg *sg = sgs[n]; + + desc[i].flags = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT); + desc[i].addr = cpu_to_virtio64(vq->vdev, (u64)(size_t)sg->addr); + desc[i].len = cpu_to_virtio32(vq->vdev, sg->length); + + prev = i; + i = virtio16_to_cpu(vq->vdev, desc[i].next); + } + for (; n < (out_sgs + in_sgs); n++) { + struct virtio_sg *sg = sgs[n]; + + desc[i].flags = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT | + VRING_DESC_F_WRITE); + desc[i].addr = cpu_to_virtio64(vq->vdev, + (u64)(uintptr_t)sg->addr); + desc[i].len = cpu_to_virtio32(vq->vdev, sg->length); + + prev = i; + i = virtio16_to_cpu(vq->vdev, desc[i].next); + } + /* Last one doesn't continue */ + desc[prev].flags &= cpu_to_virtio16(vq->vdev, ~VRING_DESC_F_NEXT); + + /* We're using some buffers from the free list. */ + vq->num_free -= descs_used; + + /* Update free pointer */ + vq->free_head = i; + + /* + * Put entry in available array (but don't update avail->idx + * until they do sync). + */ + avail = vq->avail_idx_shadow & (vq->vring.num - 1); + vq->vring.avail->ring[avail] = cpu_to_virtio16(vq->vdev, head); + + /* + * Descriptors and available array need to be set before we expose the + * new available array entries. + */ + virtio_wmb(); + vq->avail_idx_shadow++; + vq->vring.avail->idx = cpu_to_virtio16(vq->vdev, vq->avail_idx_shadow); + vq->num_added++; + + /* + * This is very unlikely, but theoretically possible. + * Kick just in case. + */ + if (unlikely(vq->num_added == (1 << 16) - 1)) + virtqueue_kick(vq); + + return 0; +} + +static bool virtqueue_kick_prepare(struct virtqueue *vq) +{ + u16 new, old; + bool needs_kick; + + /* + * We need to expose available array entries before checking + * avail event. + */ + virtio_mb(); + + old = vq->avail_idx_shadow - vq->num_added; + new = vq->avail_idx_shadow; + vq->num_added = 0; + + if (vq->event) { + needs_kick = vring_need_event(virtio16_to_cpu(vq->vdev, + vring_avail_event(&vq->vring)), new, old); + } else { + needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(vq->vdev, + VRING_USED_F_NO_NOTIFY)); + } + + return needs_kick; +} + +void virtqueue_kick(struct virtqueue *vq) +{ + if (virtqueue_kick_prepare(vq)) + virtio_notify(vq->vdev, vq); +} + +static void detach_buf(struct virtqueue *vq, unsigned int head) +{ + unsigned int i; + __virtio16 nextflag = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT); + + /* Put back on free list: unmap first-level descriptors and find end */ + i = head; + + while (vq->vring.desc[i].flags & nextflag) { + i = virtio16_to_cpu(vq->vdev, vq->vring.desc[i].next); + vq->num_free++; + } + + vq->vring.desc[i].next = cpu_to_virtio16(vq->vdev, vq->free_head); + vq->free_head = head; + + /* Plus final descriptor */ + vq->num_free++; +} + +static inline bool more_used(const struct virtqueue *vq) +{ + return vq->last_used_idx != virtio16_to_cpu(vq->vdev, + vq->vring.used->idx); +} + +void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len) +{ + unsigned int i; + u16 last_used; + + if (!more_used(vq)) { + debug("(%s.%d): No more buffers in queue\n", + vq->vdev->name, vq->index); + return NULL; + } + + /* Only get used array entries after they have been exposed by host */ + virtio_rmb(); + + last_used = (vq->last_used_idx & (vq->vring.num - 1)); + i = virtio32_to_cpu(vq->vdev, vq->vring.used->ring[last_used].id); + if (len) { + *len = virtio32_to_cpu(vq->vdev, + vq->vring.used->ring[last_used].len); + debug("(%s.%d): last used idx %u with len %u\n", + vq->vdev->name, vq->index, i, *len); + } + + if (unlikely(i >= vq->vring.num)) { + printf("(%s.%d): id %u out of range\n", + vq->vdev->name, vq->index, i); + return NULL; + } + + detach_buf(vq, i); + vq->last_used_idx++; + /* + * If we expect an interrupt for the next entry, tell host + * by writing event index and flush out the write before + * the read in the next get_buf call. + */ + if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) + virtio_store_mb(&vring_used_event(&vq->vring), + cpu_to_virtio16(vq->vdev, vq->last_used_idx)); + + return (void *)(uintptr_t)virtio64_to_cpu(vq->vdev, + vq->vring.desc[i].addr); +} + +static struct virtqueue *__vring_new_virtqueue(unsigned int index, + struct vring vring, + struct udevice *udev) +{ + unsigned int i; + struct virtqueue *vq; + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct udevice *vdev = uc_priv->vdev; + + vq = malloc(sizeof(*vq)); + if (!vq) + return NULL; + + vq->vdev = vdev; + vq->index = index; + vq->num_free = vring.num; + vq->vring = vring; + vq->last_used_idx = 0; + vq->avail_flags_shadow = 0; + vq->avail_idx_shadow = 0; + vq->num_added = 0; + list_add_tail(&vq->list, &uc_priv->vqs); + + vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); + + /* Tell other side not to bother us */ + vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; + if (!vq->event) + vq->vring.avail->flags = cpu_to_virtio16(vdev, + vq->avail_flags_shadow); + + /* Put everything in free lists */ + vq->free_head = 0; + for (i = 0; i < vring.num - 1; i++) + vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); + + return vq; +} + +struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, + unsigned int vring_align, + struct udevice *udev) +{ + struct virtqueue *vq; + void *queue = NULL; + struct vring vring; + + /* We assume num is a power of 2 */ + if (num & (num - 1)) { + printf("Bad virtqueue length %u\n", num); + return NULL; + } + + /* TODO: allocate each queue chunk individually */ + for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { + queue = memalign(PAGE_SIZE, vring_size(num, vring_align)); + if (queue) + break; + } + + if (!num) + return NULL; + + if (!queue) { + /* Try to get a single page. You are my only hope! */ + queue = memalign(PAGE_SIZE, vring_size(num, vring_align)); + } + if (!queue) + return NULL; + + memset(queue, 0, vring_size(num, vring_align)); + vring_init(&vring, num, queue, vring_align); + + vq = __vring_new_virtqueue(index, vring, udev); + if (!vq) { + free(queue); + return NULL; + } + debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name, + queue, vq, num); + + return vq; +} + +void vring_del_virtqueue(struct virtqueue *vq) +{ + free(vq->vring.desc); + list_del(&vq->list); + free(vq); +} + +unsigned int virtqueue_get_vring_size(struct virtqueue *vq) +{ + return vq->vring.num; +} + +ulong virtqueue_get_desc_addr(struct virtqueue *vq) +{ + return (ulong)vq->vring.desc; +} + +ulong virtqueue_get_avail_addr(struct virtqueue *vq) +{ + return (ulong)vq->vring.desc + + ((char *)vq->vring.avail - (char *)vq->vring.desc); +} + +ulong virtqueue_get_used_addr(struct virtqueue *vq) +{ + return (ulong)vq->vring.desc + + ((char *)vq->vring.used - (char *)vq->vring.desc); +} + +bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx) +{ + virtio_mb(); + + return last_used_idx != virtio16_to_cpu(vq->vdev, vq->vring.used->idx); +} + +void virtqueue_dump(struct virtqueue *vq) +{ + unsigned int i; + + printf("virtqueue %p for dev %s:\n", vq, vq->vdev->name); + printf("\tindex %u, phys addr %p num %u\n", + vq->index, vq->vring.desc, vq->vring.num); + printf("\tfree_head %u, num_added %u, num_free %u\n", + vq->free_head, vq->num_added, vq->num_free); + printf("\tlast_used_idx %u, avail_flags_shadow %u, avail_idx_shadow %u\n", + vq->last_used_idx, vq->avail_flags_shadow, vq->avail_idx_shadow); + + printf("Descriptor dump:\n"); + for (i = 0; i < vq->vring.num; i++) { + printf("\tdesc[%u] = { 0x%llx, len %u, flags %u, next %u }\n", + i, vq->vring.desc[i].addr, vq->vring.desc[i].len, + vq->vring.desc[i].flags, vq->vring.desc[i].next); + } + + printf("Avail ring dump:\n"); + printf("\tflags %u, idx %u\n", + vq->vring.avail->flags, vq->vring.avail->idx); + for (i = 0; i < vq->vring.num; i++) { + printf("\tavail[%u] = %u\n", + i, vq->vring.avail->ring[i]); + } + + printf("Used ring dump:\n"); + printf("\tflags %u, idx %u\n", + vq->vring.used->flags, vq->vring.used->idx); + for (i = 0; i < vq->vring.num; i++) { + printf("\tused[%u] = { %u, %u }\n", i, + vq->vring.used->ring[i].id, vq->vring.used->ring[i].len); + } +} diff --git a/include/virtio_ring.h b/include/virtio_ring.h new file mode 100644 index 00000000000..6fc0593b14b --- /dev/null +++ b/include/virtio_ring.h @@ -0,0 +1,320 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * From Linux kernel include/uapi/linux/virtio_ring.h + */ + +#ifndef _LINUX_VIRTIO_RING_H +#define _LINUX_VIRTIO_RING_H + +#include + +/* This marks a buffer as continuing via the next field */ +#define VRING_DESC_F_NEXT 1 +/* This marks a buffer as write-only (otherwise read-only) */ +#define VRING_DESC_F_WRITE 2 +/* This means the buffer contains a list of buffer descriptors */ +#define VRING_DESC_F_INDIRECT 4 + +/* + * The Host uses this in used->flags to advise the Guest: don't kick me when + * you add a buffer. It's unreliable, so it's simply an optimization. Guest + * will still kick if it's out of buffers. + */ +#define VRING_USED_F_NO_NOTIFY 1 + +/* + * The Guest uses this in avail->flags to advise the Host: don't interrupt me + * when you consume a buffer. It's unreliable, so it's simply an optimization. + */ +#define VRING_AVAIL_F_NO_INTERRUPT 1 + +/* We support indirect buffer descriptors */ +#define VIRTIO_RING_F_INDIRECT_DESC 28 + +/* + * The Guest publishes the used index for which it expects an interrupt + * at the end of the avail ring. Host should ignore the avail->flags field. + * + * The Host publishes the avail index for which it expects a kick + * at the end of the used ring. Guest should ignore the used->flags field. + */ +#define VIRTIO_RING_F_EVENT_IDX 29 + +/* Virtio ring descriptors: 16 bytes. These can chain together via "next". */ +struct vring_desc { + /* Address (guest-physical) */ + __virtio64 addr; + /* Length */ + __virtio32 len; + /* The flags as indicated above */ + __virtio16 flags; + /* We chain unused descriptors via this, too */ + __virtio16 next; +}; + +struct vring_avail { + __virtio16 flags; + __virtio16 idx; + __virtio16 ring[]; +}; + +struct vring_used_elem { + /* Index of start of used descriptor chain */ + __virtio32 id; + /* Total length of the descriptor chain which was used (written to) */ + __virtio32 len; +}; + +struct vring_used { + __virtio16 flags; + __virtio16 idx; + struct vring_used_elem ring[]; +}; + +struct vring { + unsigned int num; + struct vring_desc *desc; + struct vring_avail *avail; + struct vring_used *used; +}; + +/** + * virtqueue - a queue to register buffers for sending or receiving. + * + * @list: the chain of virtqueues for this device + * @vdev: the virtio device this queue was created for + * @index: the zero-based ordinal number for this queue + * @num_free: number of elements we expect to be able to fit + * @vring: actual memory layout for this queue + * @event: host publishes avail event idx + * @free_head: head of free buffer list + * @num_added: number we've added since last sync + * @last_used_idx: last used index we've seen + * @avail_flags_shadow: last written value to avail->flags + * @avail_idx_shadow: last written value to avail->idx in guest byte order + */ +struct virtqueue { + struct list_head list; + struct udevice *vdev; + unsigned int index; + unsigned int num_free; + struct vring vring; + bool event; + unsigned int free_head; + unsigned int num_added; + u16 last_used_idx; + u16 avail_flags_shadow; + u16 avail_idx_shadow; +}; + +/* + * Alignment requirements for vring elements. + * When using pre-virtio 1.0 layout, these fall out naturally. + */ +#define VRING_AVAIL_ALIGN_SIZE 2 +#define VRING_USED_ALIGN_SIZE 4 +#define VRING_DESC_ALIGN_SIZE 16 + +/* + * We publish the used event index at the end of the available ring, + * and vice versa. They are at the end for backwards compatibility. + */ +#define vring_used_event(vr) ((vr)->avail->ring[(vr)->num]) +#define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num]) + +static inline void vring_init(struct vring *vr, unsigned int num, void *p, + unsigned long align) +{ + vr->num = num; + vr->desc = p; + vr->avail = p + num * sizeof(struct vring_desc); + vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + + sizeof(__virtio16) + align - 1) & ~(align - 1)); +} + +static inline unsigned int vring_size(unsigned int num, unsigned long align) +{ + return ((sizeof(struct vring_desc) * num + + sizeof(__virtio16) * (3 + num) + align - 1) & ~(align - 1)) + + sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num; +} + +/* + * The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX. + * Assuming a given event_idx value from the other side, if we have just + * incremented index from old to new_idx, should we trigger an event? + */ +static inline int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old) +{ + /* + * Note: Xen has similar logic for notification hold-off + * in include/xen/interface/io/ring.h with req_event and req_prod + * corresponding to event_idx + 1 and new_idx respectively. + * Note also that req_event and req_prod in Xen start at 1, + * event indexes in virtio start at 0. + */ + return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old); +} + +struct virtio_sg; + +/** + * virtqueue_add - expose buffers to other end + * + * @vq: the struct virtqueue we're talking about + * @sgs: array of terminated scatterlists + * @out_sgs: the number of scatterlists readable by other side + * @in_sgs: the number of scatterlists which are writable + * (after readable ones) + * + * Caller must ensure we don't call this with other virtqueue operations + * at the same time (except where noted). + * + * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). + */ +int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[], + unsigned int out_sgs, unsigned int in_sgs); + +/** + * virtqueue_kick - update after add_buf + * + * @vq: the struct virtqueue + * + * After one or more virtqueue_add() calls, invoke this to kick + * the other side. + * + * Caller must ensure we don't call this with other virtqueue + * operations at the same time (except where noted). + */ +void virtqueue_kick(struct virtqueue *vq); + +/** + * virtqueue_get_buf - get the next used buffer + * + * @vq: the struct virtqueue we're talking about + * @len: the length written into the buffer + * + * If the device wrote data into the buffer, @len will be set to the + * amount written. This means you don't need to clear the buffer + * beforehand to ensure there's no data leakage in the case of short + * writes. + * + * Caller must ensure we don't call this with other virtqueue + * operations at the same time (except where noted). + * + * Returns NULL if there are no used buffers, or the memory buffer + * handed to virtqueue_add_*(). + */ +void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len); + +/** + * vring_create_virtqueue - create a virtqueue for a virtio device + * + * @index: the index of the queue + * @num: number of elements of the queue + * @vring_align:the alignment requirement of the descriptor ring + * @udev: the virtio transport udevice + * @return: the virtqueue pointer or NULL if failed + * + * This creates a virtqueue and allocates the descriptor ring for a virtio + * device. The caller should query virtqueue_get_ring_size() to learn the + * actual size of the ring. + * + * This API is supposed to be called by the virtio transport driver in the + * virtio find_vqs() uclass method. + */ +struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, + unsigned int vring_align, + struct udevice *udev); + +/** + * vring_del_virtqueue - destroy a virtqueue + * + * @vq: the struct virtqueue we're talking about + * + * This destroys a virtqueue. If created with vring_create_virtqueue(), + * this also frees the descriptor ring. + * + * This API is supposed to be called by the virtio transport driver in the + * virtio del_vqs() uclass method. + */ +void vring_del_virtqueue(struct virtqueue *vq); + +/** + * virtqueue_get_vring_size - get the size of the virtqueue's vring + * + * @vq: the struct virtqueue containing the vring of interest + * @return: the size of the vring in a virtqueue. + */ +unsigned int virtqueue_get_vring_size(struct virtqueue *vq); + +/** + * virtqueue_get_desc_addr - get the vring descriptor table address + * + * @vq: the struct virtqueue containing the vring of interest + * @return: the descriptor table address of the vring in a virtqueue. + */ +ulong virtqueue_get_desc_addr(struct virtqueue *vq); + +/** + * virtqueue_get_avail_addr - get the vring available ring address + * + * @vq: the struct virtqueue containing the vring of interest + * @return: the available ring address of the vring in a virtqueue. + */ +ulong virtqueue_get_avail_addr(struct virtqueue *vq); + +/** + * virtqueue_get_used_addr - get the vring used ring address + * + * @vq: the struct virtqueue containing the vring of interest + * @return: the used ring address of the vring in a virtqueue. + */ +ulong virtqueue_get_used_addr(struct virtqueue *vq); + +/** + * virtqueue_poll - query pending used buffers + * + * @vq: the struct virtqueue we're talking about + * @last_used_idx: virtqueue last used index + * + * Returns "true" if there are pending used buffers in the queue. + */ +bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx); + +/** + * virtqueue_dump - dump the virtqueue for debugging + * + * @vq: the struct virtqueue we're talking about + * + * Caller must ensure we don't call this with other virtqueue operations + * at the same time (except where noted). + */ +void virtqueue_dump(struct virtqueue *vq); + +/* + * Barriers in virtio are tricky. Since we are not in a hyperviosr/guest + * scenario, having these as nops is enough to work as expected. + */ + +static inline void virtio_mb(void) +{ +} + +static inline void virtio_rmb(void) +{ +} + +static inline void virtio_wmb(void) +{ +} + +static inline void virtio_store_mb(__virtio16 *p, __virtio16 v) +{ + WRITE_ONCE(*p, v); +} + +#endif /* _LINUX_VIRTIO_RING_H */ -- cgit v1.3.1 From c515ee5c51618883df64ab7326fbee8911b99a97 Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Mon, 15 Oct 2018 02:21:08 -0700 Subject: blk: Drop blk_prepare_device() With the post_probe() changes, this API is no longer needed. Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- drivers/block/blk-uclass.c | 9 --------- include/blk.h | 10 ---------- 2 files changed, 19 deletions(-) (limited to 'include') diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index 95e7b540a58..3f00ff8ede0 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -471,15 +471,6 @@ unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, return ops->erase(dev, start, blkcnt); } -int blk_prepare_device(struct udevice *dev) -{ - struct blk_desc *desc = dev_get_uclass_platdata(dev); - - part_init(desc); - - return 0; -} - int blk_get_from_parent(struct udevice *parent, struct udevice **devp) { struct udevice *dev; diff --git a/include/blk.h b/include/blk.h index 6af219681cd..c243179ebbe 100644 --- a/include/blk.h +++ b/include/blk.h @@ -356,16 +356,6 @@ int blk_create_devicef(struct udevice *parent, const char *drv_name, const char *name, int if_type, int devnum, int blksz, lbaint_t lba, struct udevice **devp); -/** - * blk_prepare_device() - Prepare a block device for use - * - * This reads partition information from the device if supported. - * - * @dev: Device to prepare - * @return 0 if ok, -ve on error - */ -int blk_prepare_device(struct udevice *dev); - /** * blk_unbind_all() - Unbind all device of the given interface type * -- cgit v1.3.1 From c879eeb7aa95d753d4d9e39f43004943ed9d93b7 Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Mon, 15 Oct 2018 02:21:09 -0700 Subject: blk: Make blk_next_free_devnum() public blk_next_free_devnum() can be helpful in some cases. Make it a public API. Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- drivers/block/blk-uclass.c | 2 +- include/blk.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index 3f00ff8ede0..76ee3db80c4 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -517,7 +517,7 @@ int blk_find_max_devnum(enum if_type if_type) return max_devnum; } -static int blk_next_free_devnum(enum if_type if_type) +int blk_next_free_devnum(enum if_type if_type) { int ret; diff --git a/include/blk.h b/include/blk.h index c243179ebbe..5e94f0e096b 100644 --- a/include/blk.h +++ b/include/blk.h @@ -378,6 +378,17 @@ int blk_unbind_all(int if_type); */ int blk_find_max_devnum(enum if_type if_type); +/** + * blk_next_free_devnum() - get the next device number for an interface type + * + * Finds the next number that is safe to use for a newly allocated device for + * an interface type @if_type. + * + * @if_type: Interface type to scan + * @return next device number safe to use, or -ve on error + */ +int blk_next_free_devnum(enum if_type if_type); + /** * blk_select_hwpart() - select a hardware partition * -- cgit v1.3.1 From 4ad54ec4d5c98a297f8df2fe9a630a534557f717 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Mon, 15 Oct 2018 02:21:10 -0700 Subject: blk: Introduce IF_TYPE_VIRTIO This adds a new block interface type for VirtIO block devices. Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- disk/part.c | 6 ++++++ drivers/block/blk-uclass.c | 2 ++ include/blk.h | 1 + 3 files changed, 9 insertions(+) (limited to 'include') diff --git a/disk/part.c b/disk/part.c index 9e457a6e72e..f30f9e9187d 100644 --- a/disk/part.c +++ b/disk/part.c @@ -150,6 +150,9 @@ void dev_print (struct blk_desc *dev_desc) dev_desc->revision, dev_desc->product); break; + case IF_TYPE_VIRTIO: + printf("%s VirtIO Block Device\n", dev_desc->vendor); + break; case IF_TYPE_DOC: puts("device type DOC\n"); return; @@ -281,6 +284,9 @@ static void print_part_header(const char *type, struct blk_desc *dev_desc) case IF_TYPE_NVME: puts ("NVMe"); break; + case IF_TYPE_VIRTIO: + puts("VirtIO"); + break; default: puts ("UNKNOWN"); break; diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index 76ee3db80c4..65a766e586d 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -23,6 +23,7 @@ static const char *if_typename_str[IF_TYPE_COUNT] = { [IF_TYPE_HOST] = "host", [IF_TYPE_NVME] = "nvme", [IF_TYPE_EFI] = "efi", + [IF_TYPE_VIRTIO] = "virtio", }; static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = { @@ -37,6 +38,7 @@ static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = { [IF_TYPE_HOST] = UCLASS_ROOT, [IF_TYPE_NVME] = UCLASS_NVME, [IF_TYPE_EFI] = UCLASS_EFI, + [IF_TYPE_VIRTIO] = UCLASS_VIRTIO, }; static enum if_type if_typename_to_iftype(const char *if_typename) diff --git a/include/blk.h b/include/blk.h index 5e94f0e096b..d0c033aece0 100644 --- a/include/blk.h +++ b/include/blk.h @@ -33,6 +33,7 @@ enum if_type { IF_TYPE_HOST, IF_TYPE_NVME, IF_TYPE_EFI, + IF_TYPE_VIRTIO, IF_TYPE_COUNT, /* Number of interface types */ }; -- cgit v1.3.1 From 2895c4b7d65e1a65f7d8804126f91ee91e8e2481 Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Mon, 15 Oct 2018 02:21:15 -0700 Subject: kconfig: Introduce HAVE_ARCH_IOMAP Introduce a new Kconfig option for architecture codes to control whether it provides io{read,write}{8,16,32} I/O accessor functions. Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- include/linux/io.h | 4 ++++ lib/Kconfig | 6 ++++++ 2 files changed, 10 insertions(+) (limited to 'include') diff --git a/include/linux/io.h b/include/linux/io.h index d1b3efed9da..9badab49b0b 100644 --- a/include/linux/io.h +++ b/include/linux/io.h @@ -7,6 +7,7 @@ #include #include +#ifndef CONFIG_HAVE_ARCH_IOMAP static inline u8 ioread8(const volatile void __iomem *addr) { return readb(addr); @@ -21,6 +22,7 @@ static inline u32 ioread32(const volatile void __iomem *addr) { return readl(addr); } +#endif /* !CONFIG_HAVE_ARCH_IOMAP */ #ifdef CONFIG_64BIT static inline u64 ioread64(const volatile void __iomem *addr) @@ -29,6 +31,7 @@ static inline u64 ioread64(const volatile void __iomem *addr) } #endif /* CONFIG_64BIT */ +#ifndef CONFIG_HAVE_ARCH_IOMAP static inline void iowrite8(u8 value, volatile void __iomem *addr) { writeb(value, addr); @@ -43,6 +46,7 @@ static inline void iowrite32(u32 value, volatile void __iomem *addr) { writel(value, addr); } +#endif /* !CONFIG_HAVE_ARCH_IOMAP */ #ifdef CONFIG_64BIT static inline void iowrite64(u64 value, volatile void __iomem *addr) diff --git a/lib/Kconfig b/lib/Kconfig index ccab426e121..847e797a3a4 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -21,6 +21,12 @@ config DYNAMIC_CRC_TABLE Enable this option to calculate entries for CRC tables at runtime. This can be helpful when reducing the size of the build image +config HAVE_ARCH_IOMAP + bool + help + Enable this option if architecture provides io{read,write}{8,16,32} + I/O accessor functions. + config HAVE_PRIVATE_LIBGCC bool -- cgit v1.3.1 From a8c5f8d3d02807f72d048950d72b0c73d55bd7fb Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Mon, 15 Oct 2018 02:21:21 -0700 Subject: dm: pci: Add APIs to find next capability and extended capability This introduces two new APIs dm_pci_find_next_capability() and dm_pci_find_next_ext_capability() to get PCI capability address and PCI express extended capability address for a given PCI device starting from a given offset. Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- drivers/pci/pci-uclass.c | 51 ++++++++++++++++++++++++++++++++++-------------- include/pci.h | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/drivers/pci/pci-uclass.c b/drivers/pci/pci-uclass.c index da49c96ed5e..0c52337f33a 100644 --- a/drivers/pci/pci-uclass.c +++ b/drivers/pci/pci-uclass.c @@ -1344,26 +1344,14 @@ void *dm_pci_map_bar(struct udevice *dev, int bar, int flags) return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE); } -int dm_pci_find_capability(struct udevice *dev, int cap) +static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap) { - u16 status; - u8 header_type; int ttl = PCI_FIND_CAP_TTL; u8 id; u16 ent; - u8 pos; - - dm_pci_read_config16(dev, PCI_STATUS, &status); - if (!(status & PCI_STATUS_CAP_LIST)) - return 0; - - dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type); - if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS) - pos = PCI_CB_CAPABILITY_LIST; - else - pos = PCI_CAPABILITY_LIST; dm_pci_read_config8(dev, pos, &pos); + while (ttl--) { if (pos < PCI_STD_HEADER_SIZEOF) break; @@ -1381,7 +1369,32 @@ int dm_pci_find_capability(struct udevice *dev, int cap) return 0; } -int dm_pci_find_ext_capability(struct udevice *dev, int cap) +int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap) +{ + return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT, + cap); +} + +int dm_pci_find_capability(struct udevice *dev, int cap) +{ + u16 status; + u8 header_type; + u8 pos; + + dm_pci_read_config16(dev, PCI_STATUS, &status); + if (!(status & PCI_STATUS_CAP_LIST)) + return 0; + + dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type); + if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS) + pos = PCI_CB_CAPABILITY_LIST; + else + pos = PCI_CAPABILITY_LIST; + + return _dm_pci_find_next_capability(dev, pos, cap); +} + +int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap) { u32 header; int ttl; @@ -1390,6 +1403,9 @@ int dm_pci_find_ext_capability(struct udevice *dev, int cap) /* minimum 8 bytes per capability */ ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; + if (start) + pos = start; + dm_pci_read_config32(dev, pos, &header); /* * If we have no capabilities, this is indicated by cap ID, @@ -1412,6 +1428,11 @@ int dm_pci_find_ext_capability(struct udevice *dev, int cap) return 0; } +int dm_pci_find_ext_capability(struct udevice *dev, int cap) +{ + return dm_pci_find_next_ext_capability(dev, 0, cap); +} + UCLASS_DRIVER(pci) = { .id = UCLASS_PCI, .name = "pci", diff --git a/include/pci.h b/include/pci.h index 938a8390cbf..785d7d28b7e 100644 --- a/include/pci.h +++ b/include/pci.h @@ -1312,6 +1312,29 @@ pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t addr, */ void *dm_pci_map_bar(struct udevice *dev, int bar, int flags); +/** + * dm_pci_find_next_capability() - find a capability starting from an offset + * + * Tell if a device supports a given PCI capability. Returns the + * address of the requested capability structure within the device's + * PCI configuration space or 0 in case the device does not support it. + * + * Possible values for @cap: + * + * %PCI_CAP_ID_MSI Message Signalled Interrupts + * %PCI_CAP_ID_PCIX PCI-X + * %PCI_CAP_ID_EXP PCI Express + * %PCI_CAP_ID_MSIX MSI-X + * + * See PCI_CAP_ID_xxx for the complete capability ID codes. + * + * @dev: PCI device to query + * @start: offset to start from + * @cap: capability code + * @return: capability address or 0 if not supported + */ +int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap); + /** * dm_pci_find_capability() - find a capability * @@ -1334,6 +1357,31 @@ void *dm_pci_map_bar(struct udevice *dev, int bar, int flags); */ int dm_pci_find_capability(struct udevice *dev, int cap); +/** + * dm_pci_find_next_ext_capability() - find an extended capability + * starting from an offset + * + * Tell if a device supports a given PCI express extended capability. + * Returns the address of the requested extended capability structure + * within the device's PCI configuration space or 0 in case the device + * does not support it. + * + * Possible values for @cap: + * + * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting + * %PCI_EXT_CAP_ID_VC Virtual Channel + * %PCI_EXT_CAP_ID_DSN Device Serial Number + * %PCI_EXT_CAP_ID_PWR Power Budgeting + * + * See PCI_EXT_CAP_ID_xxx for the complete extended capability ID codes. + * + * @dev: PCI device to query + * @start: offset to start from + * @cap: extended capability code + * @return: extended capability address or 0 if not supported + */ +int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap); + /** * dm_pci_find_ext_capability() - find an extended capability * -- cgit v1.3.1