diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/blk.h | 22 | ||||
-rw-r--r-- | include/dm/device-internal.h | 4 | ||||
-rw-r--r-- | include/dm/lists.h | 9 | ||||
-rw-r--r-- | include/dm/root.h | 17 | ||||
-rw-r--r-- | include/dm/test.h | 1 | ||||
-rw-r--r-- | include/dm/uclass-id.h | 1 | ||||
-rw-r--r-- | include/dm/uclass.h | 4 | ||||
-rw-r--r-- | include/dm/util.h | 27 | ||||
-rw-r--r-- | include/init.h | 7 | ||||
-rw-r--r-- | include/linux/io.h | 4 | ||||
-rw-r--r-- | include/pci.h | 48 | ||||
-rw-r--r-- | include/regmap.h | 232 | ||||
-rw-r--r-- | include/virtio.h | 707 | ||||
-rw-r--r-- | include/virtio_ring.h | 320 | ||||
-rw-r--r-- | include/virtio_types.h | 24 |
15 files changed, 1389 insertions, 38 deletions
diff --git a/include/blk.h b/include/blk.h index 6af219681c..d0c033aece 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 */ }; @@ -357,16 +358,6 @@ int blk_create_devicef(struct udevice *parent, const char *drv_name, 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 * * The devices are removed and then unbound. @@ -389,6 +380,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 * * Select a hardware partition if the device supports it (typically MMC does) diff --git a/include/dm/device-internal.h b/include/dm/device-internal.h index 02ac4c7952..ee2b24a62a 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 13d1516a12..810e244d9e 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); @@ -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 diff --git a/include/dm/root.h b/include/dm/root.h index 2b9c6da416..c8d629ba9b 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); diff --git a/include/dm/test.h b/include/dm/test.h index 83418eb482..07385cd531 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/include/dm/uclass-id.h b/include/dm/uclass-id.h index 269a2c6e72..c91dca1f82 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/dm/uclass.h b/include/dm/uclass.h index eebf2d5614..4ef0d0f0c0 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; diff --git a/include/dm/util.h b/include/dm/util.h index 898822e445..9ff6531d1b 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 diff --git a/include/init.h b/include/init.h index a58d7a6917..afc953d51e 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); diff --git a/include/linux/io.h b/include/linux/io.h index d1b3efed9d..9badab49b0 100644 --- a/include/linux/io.h +++ b/include/linux/io.h @@ -7,6 +7,7 @@ #include <linux/types.h> #include <asm/io.h> +#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/include/pci.h b/include/pci.h index 938a8390cb..785d7d28b7 100644 --- a/include/pci.h +++ b/include/pci.h @@ -1313,6 +1313,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 * * Tell if a device supports a given PCI capability. Returns the @@ -1335,6 +1358,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 * * Tell if a device supports a given PCI express extended capability. diff --git a/include/regmap.h b/include/regmap.h index 6a574eaa41..b2b733fda6 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -8,6 +8,61 @@ #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 + * + * @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, +}; + +/** + * 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 * * @start: Start address @@ -21,10 +76,11 @@ 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 { + enum regmap_endianness_t endianness; int range_count; struct regmap_range ranges[0]; }; @@ -33,14 +89,155 @@ 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 + * + * 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); + +/** + * 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 + * + * 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); -#define regmap_write32(map, ptr, member, val) \ - regmap_write(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), val) +/** + * 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); + +/** + * 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); + +/** + * 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) + +/** + * 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)) -#define regmap_read32(map, ptr, member, valp) \ - regmap_read(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), valp) +/** + * 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 @@ -49,31 +246,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 +285,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); diff --git a/include/virtio.h b/include/virtio.h new file mode 100644 index 0000000000..654fdf154b --- /dev/null +++ b/include/virtio.h @@ -0,0 +1,707 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi> + * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> + * + * 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_ring.h b/include/virtio_ring.h new file mode 100644 index 0000000000..6fc0593b14 --- /dev/null +++ b/include/virtio_ring.h @@ -0,0 +1,320 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi> + * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> + * + * From Linux kernel include/uapi/linux/virtio_ring.h + */ + +#ifndef _LINUX_VIRTIO_RING_H +#define _LINUX_VIRTIO_RING_H + +#include <virtio_types.h> + +/* 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 */ diff --git a/include/virtio_types.h b/include/virtio_types.h new file mode 100644 index 0000000000..d700d1936d --- /dev/null +++ b/include/virtio_types.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi> + * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> + * + * From Linux kernel include/uapi/linux/virtio_types.h + */ + +#ifndef _LINUX_VIRTIO_TYPES_H +#define _LINUX_VIRTIO_TYPES_H + +#include <linux/types.h> + +/* + * __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 */ |