Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 2 | #ifndef _LINUX_VIRTIO_CONFIG_H |
| 3 | #define _LINUX_VIRTIO_CONFIG_H |
Rusty Russell | 674bfc2 | 2008-07-25 12:06:03 -0500 | [diff] [blame] | 4 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 5 | #include <linux/err.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 6 | #include <linux/bug.h> |
Rusty Russell | 72e61eb | 2008-05-02 21:50:49 -0500 | [diff] [blame] | 7 | #include <linux/virtio.h> |
Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 8 | #include <linux/virtio_byteorder.h> |
Michael S. Tsirkin | a4235ec | 2020-07-10 03:20:21 -0400 | [diff] [blame] | 9 | #include <linux/compiler_types.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 10 | #include <uapi/linux/virtio_config.h> |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 11 | |
Christoph Hellwig | fb5e31d | 2017-02-05 18:15:22 +0100 | [diff] [blame] | 12 | struct irq_affinity; |
| 13 | |
Sebastien Boeuf | 5bfe37c | 2020-08-19 18:19:41 -0400 | [diff] [blame] | 14 | struct virtio_shm_region { |
| 15 | u64 addr; |
| 16 | u64 len; |
| 17 | }; |
| 18 | |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 19 | /** |
| 20 | * virtio_config_ops - operations for configuring a virtio device |
Cornelia Huck | d1c1dad | 2019-01-03 17:08:04 +0100 | [diff] [blame] | 21 | * Note: Do not assume that a transport implements all of the operations |
| 22 | * getting/setting a value as a simple read/write! Generally speaking, |
| 23 | * any of @get/@set, @get_status/@set_status, or @get_features/ |
| 24 | * @finalize_features are NOT safe to be called from an atomic |
| 25 | * context. |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 26 | * @get: read the value of a configuration field |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 27 | * vdev: the virtio_device |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 28 | * offset: the offset of the configuration field |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 29 | * buf: the buffer to write the field value into. |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 30 | * len: the length of the buffer |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 31 | * @set: write the value of a configuration field |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 32 | * vdev: the virtio_device |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 33 | * offset: the offset of the configuration field |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 34 | * buf: the buffer to read the field value from. |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 35 | * len: the length of the buffer |
Cornelia Huck | b89a07c | 2019-01-03 17:08:03 +0100 | [diff] [blame] | 36 | * @generation: config generation counter (optional) |
Michael S. Tsirkin | d71de9e | 2014-12-14 16:55:44 +0200 | [diff] [blame] | 37 | * vdev: the virtio_device |
| 38 | * Returns the config generation counter |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 39 | * @get_status: read the status byte |
| 40 | * vdev: the virtio_device |
| 41 | * Returns the status byte |
| 42 | * @set_status: write the status byte |
| 43 | * vdev: the virtio_device |
| 44 | * status: the new status byte |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 45 | * @reset: reset the device |
| 46 | * vdev: the virtio device |
| 47 | * After this, status and feature negotiation must be done again |
Michael S. Tsirkin | e6af578 | 2011-11-17 17:41:15 +0200 | [diff] [blame] | 48 | * Device must not be reset from its vq/config callbacks, or in |
| 49 | * parallel with being added/removed. |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 50 | * @find_vqs: find virtqueues and instantiate them. |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 51 | * vdev: the virtio_device |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 52 | * nvqs: the number of virtqueues to find |
| 53 | * vqs: on success, includes new virtqueues |
| 54 | * callbacks: array of callbacks, for each virtqueue |
Michael S. Tsirkin | 6457f12 | 2012-09-05 21:47:45 +0300 | [diff] [blame] | 55 | * include a NULL entry for vqs that do not need a callback |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 56 | * names: array of virtqueue names (mainly for debugging) |
Michael S. Tsirkin | 6457f12 | 2012-09-05 21:47:45 +0300 | [diff] [blame] | 57 | * include a NULL entry for vqs unused by driver |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 58 | * Returns 0 on success or error status |
| 59 | * @del_vqs: free virtqueues found by find_vqs(). |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 60 | * @get_features: get the array of feature bits for this device. |
| 61 | * vdev: the virtio_device |
Cornelia Huck | b89a07c | 2019-01-03 17:08:03 +0100 | [diff] [blame] | 62 | * Returns the first 64 feature bits (all we currently need). |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 63 | * @finalize_features: confirm what device features we'll be using. |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 64 | * vdev: the virtio_device |
Michael S. Tsirkin | cbb726e6 | 2022-01-14 14:58:41 -0500 | [diff] [blame] | 65 | * This sends the driver feature bits to the device: it can change |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 66 | * the dev->feature bits if it wants. |
Michael S. Tsirkin | cbb726e6 | 2022-01-14 14:58:41 -0500 | [diff] [blame] | 67 | * Note: despite the name this can be called any number of times. |
Michael S. Tsirkin | 5c609a5 | 2014-12-04 20:20:27 +0200 | [diff] [blame] | 68 | * Returns 0 on success or error status |
Cornelia Huck | b89a07c | 2019-01-03 17:08:03 +0100 | [diff] [blame] | 69 | * @bus_name: return the bus name associated with the device (optional) |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 70 | * vdev: the virtio_device |
| 71 | * This returns a pointer to the bus name a la pci_name from which |
| 72 | * the caller can then copy. |
Cornelia Huck | b89a07c | 2019-01-03 17:08:03 +0100 | [diff] [blame] | 73 | * @set_vq_affinity: set the affinity for a virtqueue (optional). |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 74 | * @get_vq_affinity: get the affinity for a virtqueue (optional). |
Sebastien Boeuf | 5bfe37c | 2020-08-19 18:19:41 -0400 | [diff] [blame] | 75 | * @get_shm_region: get a shared memory region based on the index. |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 76 | */ |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 77 | typedef void vq_callback_t(struct virtqueue *); |
Rusty Russell | 1842f23 | 2009-07-30 16:03:46 -0600 | [diff] [blame] | 78 | struct virtio_config_ops { |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 79 | void (*get)(struct virtio_device *vdev, unsigned offset, |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 80 | void *buf, unsigned len); |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 81 | void (*set)(struct virtio_device *vdev, unsigned offset, |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 82 | const void *buf, unsigned len); |
Michael S. Tsirkin | d71de9e | 2014-12-14 16:55:44 +0200 | [diff] [blame] | 83 | u32 (*generation)(struct virtio_device *vdev); |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 84 | u8 (*get_status)(struct virtio_device *vdev); |
| 85 | void (*set_status)(struct virtio_device *vdev, u8 status); |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 86 | void (*reset)(struct virtio_device *vdev); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 87 | int (*find_vqs)(struct virtio_device *, unsigned nvqs, |
Christoph Hellwig | fb5e31d | 2017-02-05 18:15:22 +0100 | [diff] [blame] | 88 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 89 | const char * const names[], const bool *ctx, |
| 90 | struct irq_affinity *desc); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 91 | void (*del_vqs)(struct virtio_device *); |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 92 | u64 (*get_features)(struct virtio_device *vdev); |
Michael S. Tsirkin | 5c609a5 | 2014-12-04 20:20:27 +0200 | [diff] [blame] | 93 | int (*finalize_features)(struct virtio_device *vdev); |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 94 | const char *(*bus_name)(struct virtio_device *vdev); |
Caleb Raitto | 19e226e | 2018-08-09 18:18:28 -0700 | [diff] [blame] | 95 | int (*set_vq_affinity)(struct virtqueue *vq, |
| 96 | const struct cpumask *cpu_mask); |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 97 | const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev, |
| 98 | int index); |
Sebastien Boeuf | 5bfe37c | 2020-08-19 18:19:41 -0400 | [diff] [blame] | 99 | bool (*get_shm_region)(struct virtio_device *vdev, |
| 100 | struct virtio_shm_region *region, u8 id); |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 101 | }; |
| 102 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 103 | /* If driver didn't advertise the feature, it will never appear. */ |
| 104 | void virtio_check_driver_offered_feature(const struct virtio_device *vdev, |
| 105 | unsigned int fbit); |
| 106 | |
| 107 | /** |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 108 | * __virtio_test_bit - helper to test feature bits. For use by transports. |
| 109 | * Devices should normally use virtio_has_feature, |
| 110 | * which includes more checks. |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 111 | * @vdev: the device |
| 112 | * @fbit: the feature bit |
| 113 | */ |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 114 | static inline bool __virtio_test_bit(const struct virtio_device *vdev, |
| 115 | unsigned int fbit) |
| 116 | { |
| 117 | /* Did you forget to fix assumptions on max features? */ |
| 118 | if (__builtin_constant_p(fbit)) |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 119 | BUILD_BUG_ON(fbit >= 64); |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 120 | else |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 121 | BUG_ON(fbit >= 64); |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 122 | |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 123 | return vdev->features & BIT_ULL(fbit); |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * __virtio_set_bit - helper to set feature bits. For use by transports. |
| 128 | * @vdev: the device |
| 129 | * @fbit: the feature bit |
| 130 | */ |
| 131 | static inline void __virtio_set_bit(struct virtio_device *vdev, |
| 132 | unsigned int fbit) |
| 133 | { |
| 134 | /* Did you forget to fix assumptions on max features? */ |
| 135 | if (__builtin_constant_p(fbit)) |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 136 | BUILD_BUG_ON(fbit >= 64); |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 137 | else |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 138 | BUG_ON(fbit >= 64); |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 139 | |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 140 | vdev->features |= BIT_ULL(fbit); |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * __virtio_clear_bit - helper to clear feature bits. For use by transports. |
| 145 | * @vdev: the device |
| 146 | * @fbit: the feature bit |
| 147 | */ |
| 148 | static inline void __virtio_clear_bit(struct virtio_device *vdev, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 149 | unsigned int fbit) |
| 150 | { |
| 151 | /* Did you forget to fix assumptions on max features? */ |
Rusty Russell | 1765e3a4 | 2011-01-24 14:45:10 -0600 | [diff] [blame] | 152 | if (__builtin_constant_p(fbit)) |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 153 | BUILD_BUG_ON(fbit >= 64); |
Rusty Russell | 1765e3a4 | 2011-01-24 14:45:10 -0600 | [diff] [blame] | 154 | else |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 155 | BUG_ON(fbit >= 64); |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 156 | |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 157 | vdev->features &= ~BIT_ULL(fbit); |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * virtio_has_feature - helper to determine if this device has this feature. |
| 162 | * @vdev: the device |
| 163 | * @fbit: the feature bit |
| 164 | */ |
| 165 | static inline bool virtio_has_feature(const struct virtio_device *vdev, |
| 166 | unsigned int fbit) |
| 167 | { |
Mark McLoughlin | ee006b35 | 2009-05-11 18:11:44 +0100 | [diff] [blame] | 168 | if (fbit < VIRTIO_TRANSPORT_F_START) |
| 169 | virtio_check_driver_offered_feature(vdev, fbit); |
| 170 | |
Michael S. Tsirkin | d4024af | 2014-11-27 21:19:02 +0200 | [diff] [blame] | 171 | return __virtio_test_bit(vdev, fbit); |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 174 | /** |
Michael S. Tsirkin | 24b6842 | 2020-06-24 19:17:04 -0400 | [diff] [blame] | 175 | * virtio_has_dma_quirk - determine whether this device has the DMA quirk |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 176 | * @vdev: the device |
| 177 | */ |
Michael S. Tsirkin | 24b6842 | 2020-06-24 19:17:04 -0400 | [diff] [blame] | 178 | static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev) |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 179 | { |
| 180 | /* |
| 181 | * Note the reverse polarity of the quirk feature (compared to most |
| 182 | * other features), this is for compatibility with legacy systems. |
| 183 | */ |
Michael S. Tsirkin | 321bd21 | 2020-06-24 18:24:33 -0400 | [diff] [blame] | 184 | return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM); |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 185 | } |
| 186 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 187 | static inline |
| 188 | struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev, |
| 189 | vq_callback_t *c, const char *n) |
| 190 | { |
| 191 | vq_callback_t *callbacks[] = { c }; |
| 192 | const char *names[] = { n }; |
| 193 | struct virtqueue *vq; |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 194 | int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL, |
| 195 | NULL); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 196 | if (err < 0) |
| 197 | return ERR_PTR(err); |
| 198 | return vq; |
| 199 | } |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 200 | |
Michael S. Tsirkin | 9b2bbdb | 2017-03-06 18:19:39 +0200 | [diff] [blame] | 201 | static inline |
| 202 | int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 203 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
| 204 | const char * const names[], |
| 205 | struct irq_affinity *desc) |
| 206 | { |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 207 | return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc); |
| 208 | } |
| 209 | |
| 210 | static inline |
| 211 | int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs, |
| 212 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
| 213 | const char * const names[], const bool *ctx, |
| 214 | struct irq_affinity *desc) |
| 215 | { |
| 216 | return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, |
| 217 | desc); |
Michael S. Tsirkin | 9b2bbdb | 2017-03-06 18:19:39 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Michael S. Tsirkin | 3569db5 | 2014-10-15 10:22:30 +1030 | [diff] [blame] | 220 | /** |
| 221 | * virtio_device_ready - enable vq use in probe function |
| 222 | * @vdev: the device |
| 223 | * |
| 224 | * Driver must call this to use vqs in the probe function. |
| 225 | * |
| 226 | * Note: vqs are enabled automatically after probe returns. |
| 227 | */ |
| 228 | static inline |
| 229 | void virtio_device_ready(struct virtio_device *dev) |
| 230 | { |
| 231 | unsigned status = dev->config->get_status(dev); |
| 232 | |
| 233 | BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK); |
| 234 | dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK); |
| 235 | } |
| 236 | |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 237 | static inline |
| 238 | const char *virtio_bus_name(struct virtio_device *vdev) |
| 239 | { |
| 240 | if (!vdev->config->bus_name) |
| 241 | return "virtio"; |
| 242 | return vdev->config->bus_name(vdev); |
| 243 | } |
| 244 | |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 245 | /** |
| 246 | * virtqueue_set_affinity - setting affinity for a virtqueue |
| 247 | * @vq: the virtqueue |
| 248 | * @cpu: the cpu no. |
| 249 | * |
| 250 | * Pay attention the function are best-effort: the affinity hint may not be set |
| 251 | * due to config support, irq type and sharing. |
| 252 | * |
| 253 | */ |
| 254 | static inline |
Caleb Raitto | 19e226e | 2018-08-09 18:18:28 -0700 | [diff] [blame] | 255 | int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask) |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 256 | { |
| 257 | struct virtio_device *vdev = vq->vdev; |
| 258 | if (vdev->config->set_vq_affinity) |
Caleb Raitto | 19e226e | 2018-08-09 18:18:28 -0700 | [diff] [blame] | 259 | return vdev->config->set_vq_affinity(vq, cpu_mask); |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Sebastien Boeuf | 5bfe37c | 2020-08-19 18:19:41 -0400 | [diff] [blame] | 263 | static inline |
| 264 | bool virtio_get_shm_region(struct virtio_device *vdev, |
| 265 | struct virtio_shm_region *region, u8 id) |
| 266 | { |
| 267 | if (!vdev->config->get_shm_region) |
| 268 | return false; |
| 269 | return vdev->config->get_shm_region(vdev, region, id); |
| 270 | } |
| 271 | |
Greg Kurz | cf561f0d | 2015-04-24 14:24:27 +0200 | [diff] [blame] | 272 | static inline bool virtio_is_little_endian(struct virtio_device *vdev) |
| 273 | { |
Greg Kurz | 7d82410 | 2015-04-24 14:26:24 +0200 | [diff] [blame] | 274 | return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) || |
| 275 | virtio_legacy_is_little_endian(); |
Greg Kurz | cf561f0d | 2015-04-24 14:24:27 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 278 | /* Memory accessors */ |
| 279 | static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val) |
| 280 | { |
Greg Kurz | cf561f0d | 2015-04-24 14:24:27 +0200 | [diff] [blame] | 281 | return __virtio16_to_cpu(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val) |
| 285 | { |
Greg Kurz | cf561f0d | 2015-04-24 14:24:27 +0200 | [diff] [blame] | 286 | return __cpu_to_virtio16(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val) |
| 290 | { |
Greg Kurz | cf561f0d | 2015-04-24 14:24:27 +0200 | [diff] [blame] | 291 | return __virtio32_to_cpu(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val) |
| 295 | { |
Greg Kurz | cf561f0d | 2015-04-24 14:24:27 +0200 | [diff] [blame] | 296 | return __cpu_to_virtio32(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val) |
| 300 | { |
Greg Kurz | cf561f0d | 2015-04-24 14:24:27 +0200 | [diff] [blame] | 301 | return __virtio64_to_cpu(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val) |
| 305 | { |
Greg Kurz | cf561f0d | 2015-04-24 14:24:27 +0200 | [diff] [blame] | 306 | return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); |
Michael S. Tsirkin | eef960a | 2014-10-22 15:35:56 +0300 | [diff] [blame] | 307 | } |
| 308 | |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 309 | #define virtio_to_cpu(vdev, x) \ |
| 310 | _Generic((x), \ |
| 311 | __u8: (x), \ |
| 312 | __virtio16: virtio16_to_cpu((vdev), (x)), \ |
| 313 | __virtio32: virtio32_to_cpu((vdev), (x)), \ |
Michael S. Tsirkin | 83eb9db9 | 2020-08-05 07:29:12 -0400 | [diff] [blame] | 314 | __virtio64: virtio64_to_cpu((vdev), (x)) \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 315 | ) |
Michael S. Tsirkin | a4235ec | 2020-07-10 03:20:21 -0400 | [diff] [blame] | 316 | |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 317 | #define cpu_to_virtio(vdev, x, m) \ |
| 318 | _Generic((m), \ |
| 319 | __u8: (x), \ |
| 320 | __virtio16: cpu_to_virtio16((vdev), (x)), \ |
| 321 | __virtio32: cpu_to_virtio32((vdev), (x)), \ |
Michael S. Tsirkin | 83eb9db9 | 2020-08-05 07:29:12 -0400 | [diff] [blame] | 322 | __virtio64: cpu_to_virtio64((vdev), (x)) \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 323 | ) |
Michael S. Tsirkin | a4235ec | 2020-07-10 03:20:21 -0400 | [diff] [blame] | 324 | |
| 325 | #define __virtio_native_type(structname, member) \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 326 | typeof(virtio_to_cpu(NULL, ((structname*)0)->member)) |
Michael S. Tsirkin | a4235ec | 2020-07-10 03:20:21 -0400 | [diff] [blame] | 327 | |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 328 | /* Config space accessors. */ |
| 329 | #define virtio_cread(vdev, structname, member, ptr) \ |
| 330 | do { \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 331 | typeof(((structname*)0)->member) virtio_cread_v; \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 332 | \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 333 | might_sleep(); \ |
| 334 | /* Sanity check: must match the member's type */ \ |
| 335 | typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \ |
| 336 | \ |
| 337 | switch (sizeof(virtio_cread_v)) { \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 338 | case 1: \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 339 | case 2: \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 340 | case 4: \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 341 | vdev->config->get((vdev), \ |
| 342 | offsetof(structname, member), \ |
| 343 | &virtio_cread_v, \ |
| 344 | sizeof(virtio_cread_v)); \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 345 | break; \ |
| 346 | default: \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 347 | __virtio_cread_many((vdev), \ |
| 348 | offsetof(structname, member), \ |
| 349 | &virtio_cread_v, \ |
| 350 | 1, \ |
| 351 | sizeof(virtio_cread_v)); \ |
| 352 | break; \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 353 | } \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 354 | *(ptr) = virtio_to_cpu(vdev, virtio_cread_v); \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 355 | } while(0) |
| 356 | |
| 357 | /* Config space accessors. */ |
| 358 | #define virtio_cwrite(vdev, structname, member, ptr) \ |
| 359 | do { \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 360 | typeof(((structname*)0)->member) virtio_cwrite_v = \ |
| 361 | cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 362 | \ |
Michael S. Tsirkin | a5b90f2 | 2020-08-03 16:08:11 -0400 | [diff] [blame] | 363 | might_sleep(); \ |
| 364 | /* Sanity check: must match the member's type */ \ |
| 365 | typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \ |
| 366 | \ |
| 367 | vdev->config->set((vdev), offsetof(structname, member), \ |
| 368 | &virtio_cwrite_v, \ |
| 369 | sizeof(virtio_cwrite_v)); \ |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 370 | } while(0) |
| 371 | |
Michael S. Tsirkin | e598960 | 2020-08-04 17:33:08 -0400 | [diff] [blame] | 372 | /* |
| 373 | * Nothing virtio-specific about these, but let's worry about generalizing |
| 374 | * these later. |
| 375 | */ |
| 376 | #define virtio_le_to_cpu(x) \ |
| 377 | _Generic((x), \ |
Michael S. Tsirkin | c84f91e | 2020-08-05 19:55:50 -0400 | [diff] [blame] | 378 | __u8: (u8)(x), \ |
| 379 | __le16: (u16)le16_to_cpu(x), \ |
| 380 | __le32: (u32)le32_to_cpu(x), \ |
| 381 | __le64: (u64)le64_to_cpu(x) \ |
Michael S. Tsirkin | e598960 | 2020-08-04 17:33:08 -0400 | [diff] [blame] | 382 | ) |
| 383 | |
| 384 | #define virtio_cpu_to_le(x, m) \ |
| 385 | _Generic((m), \ |
| 386 | __u8: (x), \ |
| 387 | __le16: cpu_to_le16(x), \ |
| 388 | __le32: cpu_to_le32(x), \ |
| 389 | __le64: cpu_to_le64(x) \ |
| 390 | ) |
| 391 | |
| 392 | /* LE (e.g. modern) Config space accessors. */ |
| 393 | #define virtio_cread_le(vdev, structname, member, ptr) \ |
| 394 | do { \ |
| 395 | typeof(((structname*)0)->member) virtio_cread_v; \ |
| 396 | \ |
| 397 | might_sleep(); \ |
| 398 | /* Sanity check: must match the member's type */ \ |
| 399 | typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \ |
| 400 | \ |
| 401 | switch (sizeof(virtio_cread_v)) { \ |
| 402 | case 1: \ |
| 403 | case 2: \ |
| 404 | case 4: \ |
| 405 | vdev->config->get((vdev), \ |
| 406 | offsetof(structname, member), \ |
| 407 | &virtio_cread_v, \ |
| 408 | sizeof(virtio_cread_v)); \ |
| 409 | break; \ |
| 410 | default: \ |
| 411 | __virtio_cread_many((vdev), \ |
| 412 | offsetof(structname, member), \ |
| 413 | &virtio_cread_v, \ |
| 414 | 1, \ |
| 415 | sizeof(virtio_cread_v)); \ |
| 416 | break; \ |
| 417 | } \ |
| 418 | *(ptr) = virtio_le_to_cpu(virtio_cread_v); \ |
| 419 | } while(0) |
| 420 | |
Michael S. Tsirkin | e598960 | 2020-08-04 17:33:08 -0400 | [diff] [blame] | 421 | #define virtio_cwrite_le(vdev, structname, member, ptr) \ |
| 422 | do { \ |
| 423 | typeof(((structname*)0)->member) virtio_cwrite_v = \ |
| 424 | virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \ |
| 425 | \ |
| 426 | might_sleep(); \ |
| 427 | /* Sanity check: must match the member's type */ \ |
| 428 | typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \ |
| 429 | \ |
| 430 | vdev->config->set((vdev), offsetof(structname, member), \ |
| 431 | &virtio_cwrite_v, \ |
| 432 | sizeof(virtio_cwrite_v)); \ |
| 433 | } while(0) |
| 434 | |
| 435 | |
Michael S. Tsirkin | d71de9e | 2014-12-14 16:55:44 +0200 | [diff] [blame] | 436 | /* Read @count fields, @bytes each. */ |
| 437 | static inline void __virtio_cread_many(struct virtio_device *vdev, |
| 438 | unsigned int offset, |
| 439 | void *buf, size_t count, size_t bytes) |
| 440 | { |
| 441 | u32 old, gen = vdev->config->generation ? |
| 442 | vdev->config->generation(vdev) : 0; |
| 443 | int i; |
| 444 | |
Cornelia Huck | ab7a237 | 2019-01-31 13:53:14 +0100 | [diff] [blame] | 445 | might_sleep(); |
Michael S. Tsirkin | d71de9e | 2014-12-14 16:55:44 +0200 | [diff] [blame] | 446 | do { |
| 447 | old = gen; |
| 448 | |
| 449 | for (i = 0; i < count; i++) |
| 450 | vdev->config->get(vdev, offset + bytes * i, |
| 451 | buf + i * bytes, bytes); |
| 452 | |
| 453 | gen = vdev->config->generation ? |
| 454 | vdev->config->generation(vdev) : 0; |
| 455 | } while (gen != old); |
| 456 | } |
| 457 | |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 458 | static inline void virtio_cread_bytes(struct virtio_device *vdev, |
| 459 | unsigned int offset, |
| 460 | void *buf, size_t len) |
| 461 | { |
Michael S. Tsirkin | d71de9e | 2014-12-14 16:55:44 +0200 | [diff] [blame] | 462 | __virtio_cread_many(vdev, offset, buf, len, 1); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 463 | } |
| 464 | |
Michael S. Tsirkin | caa0e2d | 2015-04-01 08:21:51 +1030 | [diff] [blame] | 465 | static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset) |
| 466 | { |
| 467 | u8 ret; |
Cornelia Huck | ab7a237 | 2019-01-31 13:53:14 +0100 | [diff] [blame] | 468 | |
| 469 | might_sleep(); |
Michael S. Tsirkin | caa0e2d | 2015-04-01 08:21:51 +1030 | [diff] [blame] | 470 | vdev->config->get(vdev, offset, &ret, sizeof(ret)); |
| 471 | return ret; |
| 472 | } |
| 473 | |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 474 | static inline void virtio_cwrite8(struct virtio_device *vdev, |
| 475 | unsigned int offset, u8 val) |
| 476 | { |
Cornelia Huck | ab7a237 | 2019-01-31 13:53:14 +0100 | [diff] [blame] | 477 | might_sleep(); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 478 | vdev->config->set(vdev, offset, &val, sizeof(val)); |
| 479 | } |
| 480 | |
| 481 | static inline u16 virtio_cread16(struct virtio_device *vdev, |
| 482 | unsigned int offset) |
| 483 | { |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 484 | __virtio16 ret; |
Cornelia Huck | ab7a237 | 2019-01-31 13:53:14 +0100 | [diff] [blame] | 485 | |
| 486 | might_sleep(); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 487 | vdev->config->get(vdev, offset, &ret, sizeof(ret)); |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 488 | return virtio16_to_cpu(vdev, ret); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static inline void virtio_cwrite16(struct virtio_device *vdev, |
| 492 | unsigned int offset, u16 val) |
| 493 | { |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 494 | __virtio16 v; |
| 495 | |
Cornelia Huck | ab7a237 | 2019-01-31 13:53:14 +0100 | [diff] [blame] | 496 | might_sleep(); |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 497 | v = cpu_to_virtio16(vdev, val); |
| 498 | vdev->config->set(vdev, offset, &v, sizeof(v)); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static inline u32 virtio_cread32(struct virtio_device *vdev, |
| 502 | unsigned int offset) |
| 503 | { |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 504 | __virtio32 ret; |
Cornelia Huck | ab7a237 | 2019-01-31 13:53:14 +0100 | [diff] [blame] | 505 | |
| 506 | might_sleep(); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 507 | vdev->config->get(vdev, offset, &ret, sizeof(ret)); |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 508 | return virtio32_to_cpu(vdev, ret); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static inline void virtio_cwrite32(struct virtio_device *vdev, |
| 512 | unsigned int offset, u32 val) |
| 513 | { |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 514 | __virtio32 v; |
| 515 | |
Cornelia Huck | ab7a237 | 2019-01-31 13:53:14 +0100 | [diff] [blame] | 516 | might_sleep(); |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 517 | v = cpu_to_virtio32(vdev, val); |
| 518 | vdev->config->set(vdev, offset, &v, sizeof(v)); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static inline u64 virtio_cread64(struct virtio_device *vdev, |
| 522 | unsigned int offset) |
| 523 | { |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 524 | __virtio64 ret; |
| 525 | |
Michael S. Tsirkin | d71de9e | 2014-12-14 16:55:44 +0200 | [diff] [blame] | 526 | __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret)); |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 527 | return virtio64_to_cpu(vdev, ret); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static inline void virtio_cwrite64(struct virtio_device *vdev, |
| 531 | unsigned int offset, u64 val) |
| 532 | { |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 533 | __virtio64 v; |
| 534 | |
Cornelia Huck | ab7a237 | 2019-01-31 13:53:14 +0100 | [diff] [blame] | 535 | might_sleep(); |
Michael S. Tsirkin | cacaf77 | 2020-07-30 16:12:40 -0400 | [diff] [blame] | 536 | v = cpu_to_virtio64(vdev, val); |
| 537 | vdev->config->set(vdev, offset, &v, sizeof(v)); |
Rusty Russell | 0b90d06 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* Conditional config space accessors. */ |
| 541 | #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \ |
| 542 | ({ \ |
| 543 | int _r = 0; \ |
| 544 | if (!virtio_has_feature(vdev, fbit)) \ |
| 545 | _r = -ENOENT; \ |
| 546 | else \ |
| 547 | virtio_cread((vdev), structname, member, ptr); \ |
| 548 | _r; \ |
| 549 | }) |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 550 | |
Michael S. Tsirkin | 035ce42 | 2020-08-05 09:17:38 -0400 | [diff] [blame] | 551 | /* Conditional config space accessors. */ |
| 552 | #define virtio_cread_le_feature(vdev, fbit, structname, member, ptr) \ |
| 553 | ({ \ |
| 554 | int _r = 0; \ |
| 555 | if (!virtio_has_feature(vdev, fbit)) \ |
| 556 | _r = -ENOENT; \ |
| 557 | else \ |
| 558 | virtio_cread_le((vdev), structname, member, ptr); \ |
| 559 | _r; \ |
| 560 | }) |
Pierre Morel | 0afa15e | 2020-09-10 10:53:49 +0200 | [diff] [blame] | 561 | |
| 562 | #ifdef CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS |
| 563 | int arch_has_restricted_virtio_memory_access(void); |
| 564 | #else |
| 565 | static inline int arch_has_restricted_virtio_memory_access(void) |
| 566 | { |
| 567 | return 0; |
| 568 | } |
| 569 | #endif /* CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS */ |
| 570 | |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 571 | #endif /* _LINUX_VIRTIO_CONFIG_H */ |