blob: bb4cc49107503c23c09ad3413189d5de29657a42 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Rusty Russellec3d41c2007-10-22 11:03:36 +10002#ifndef _LINUX_VIRTIO_CONFIG_H
3#define _LINUX_VIRTIO_CONFIG_H
Rusty Russell674bfc22008-07-25 12:06:03 -05004
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06005#include <linux/err.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05006#include <linux/bug.h>
Rusty Russell72e61eb2008-05-02 21:50:49 -05007#include <linux/virtio.h>
Michael S. Tsirkineef960a2014-10-22 15:35:56 +03008#include <linux/virtio_byteorder.h>
David Howells607ca462012-10-13 10:46:48 +01009#include <uapi/linux/virtio_config.h>
Rusty Russellec3d41c2007-10-22 11:03:36 +100010
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +010011struct irq_affinity;
12
Rusty Russellec3d41c2007-10-22 11:03:36 +100013/**
14 * virtio_config_ops - operations for configuring a virtio device
Cornelia Huckd1c1dad2019-01-03 17:08:04 +010015 * Note: Do not assume that a transport implements all of the operations
16 * getting/setting a value as a simple read/write! Generally speaking,
17 * any of @get/@set, @get_status/@set_status, or @get_features/
18 * @finalize_features are NOT safe to be called from an atomic
19 * context.
Rusty Russella586d4f2008-02-04 23:49:56 -050020 * @get: read the value of a configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100021 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050022 * offset: the offset of the configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100023 * buf: the buffer to write the field value into.
Rusty Russella586d4f2008-02-04 23:49:56 -050024 * len: the length of the buffer
Rusty Russella586d4f2008-02-04 23:49:56 -050025 * @set: write the value of a configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100026 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050027 * offset: the offset of the configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100028 * buf: the buffer to read the field value from.
Rusty Russella586d4f2008-02-04 23:49:56 -050029 * len: the length of the buffer
Cornelia Huckb89a07c2019-01-03 17:08:03 +010030 * @generation: config generation counter (optional)
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +020031 * vdev: the virtio_device
32 * Returns the config generation counter
Rusty Russellec3d41c2007-10-22 11:03:36 +100033 * @get_status: read the status byte
34 * vdev: the virtio_device
35 * Returns the status byte
36 * @set_status: write the status byte
37 * vdev: the virtio_device
38 * status: the new status byte
Rusty Russell6e5aa7e2008-02-04 23:50:03 -050039 * @reset: reset the device
40 * vdev: the virtio device
41 * After this, status and feature negotiation must be done again
Michael S. Tsirkine6af5782011-11-17 17:41:15 +020042 * Device must not be reset from its vq/config callbacks, or in
43 * parallel with being added/removed.
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060044 * @find_vqs: find virtqueues and instantiate them.
Rusty Russellec3d41c2007-10-22 11:03:36 +100045 * vdev: the virtio_device
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060046 * nvqs: the number of virtqueues to find
47 * vqs: on success, includes new virtqueues
48 * callbacks: array of callbacks, for each virtqueue
Michael S. Tsirkin6457f122012-09-05 21:47:45 +030049 * include a NULL entry for vqs that do not need a callback
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060050 * names: array of virtqueue names (mainly for debugging)
Michael S. Tsirkin6457f122012-09-05 21:47:45 +030051 * include a NULL entry for vqs unused by driver
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060052 * Returns 0 on success or error status
53 * @del_vqs: free virtqueues found by find_vqs().
Rusty Russellc45a6812008-05-02 21:50:50 -050054 * @get_features: get the array of feature bits for this device.
55 * vdev: the virtio_device
Cornelia Huckb89a07c2019-01-03 17:08:03 +010056 * Returns the first 64 feature bits (all we currently need).
Rusty Russellc6248962008-07-25 12:06:07 -050057 * @finalize_features: confirm what device features we'll be using.
Rusty Russellc45a6812008-05-02 21:50:50 -050058 * vdev: the virtio_device
Rusty Russellc6248962008-07-25 12:06:07 -050059 * This gives the final feature bits for the device: it can change
60 * the dev->feature bits if it wants.
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +020061 * Returns 0 on success or error status
Cornelia Huckb89a07c2019-01-03 17:08:03 +010062 * @bus_name: return the bus name associated with the device (optional)
Rick Jones66846042011-11-14 14:17:08 +000063 * vdev: the virtio_device
64 * This returns a pointer to the bus name a la pci_name from which
65 * the caller can then copy.
Cornelia Huckb89a07c2019-01-03 17:08:03 +010066 * @set_vq_affinity: set the affinity for a virtqueue (optional).
Christoph Hellwigbbaba472017-02-05 18:15:23 +010067 * @get_vq_affinity: get the affinity for a virtqueue (optional).
Rusty Russellec3d41c2007-10-22 11:03:36 +100068 */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060069typedef void vq_callback_t(struct virtqueue *);
Rusty Russell1842f232009-07-30 16:03:46 -060070struct virtio_config_ops {
Rusty Russella586d4f2008-02-04 23:49:56 -050071 void (*get)(struct virtio_device *vdev, unsigned offset,
Rusty Russellec3d41c2007-10-22 11:03:36 +100072 void *buf, unsigned len);
Rusty Russella586d4f2008-02-04 23:49:56 -050073 void (*set)(struct virtio_device *vdev, unsigned offset,
Rusty Russellec3d41c2007-10-22 11:03:36 +100074 const void *buf, unsigned len);
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +020075 u32 (*generation)(struct virtio_device *vdev);
Rusty Russellec3d41c2007-10-22 11:03:36 +100076 u8 (*get_status)(struct virtio_device *vdev);
77 void (*set_status)(struct virtio_device *vdev, u8 status);
Rusty Russell6e5aa7e2008-02-04 23:50:03 -050078 void (*reset)(struct virtio_device *vdev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060079 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +010080 struct virtqueue *vqs[], vq_callback_t *callbacks[],
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +020081 const char * const names[], const bool *ctx,
82 struct irq_affinity *desc);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060083 void (*del_vqs)(struct virtio_device *);
Michael S. Tsirkind0254772014-10-07 16:39:43 +020084 u64 (*get_features)(struct virtio_device *vdev);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +020085 int (*finalize_features)(struct virtio_device *vdev);
Rick Jones66846042011-11-14 14:17:08 +000086 const char *(*bus_name)(struct virtio_device *vdev);
Caleb Raitto19e226e2018-08-09 18:18:28 -070087 int (*set_vq_affinity)(struct virtqueue *vq,
88 const struct cpumask *cpu_mask);
Christoph Hellwigbbaba472017-02-05 18:15:23 +010089 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
90 int index);
Rusty Russellec3d41c2007-10-22 11:03:36 +100091};
92
Rusty Russellc45a6812008-05-02 21:50:50 -050093/* If driver didn't advertise the feature, it will never appear. */
94void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
95 unsigned int fbit);
96
97/**
Michael S. Tsirkind4024af2014-11-27 21:19:02 +020098 * __virtio_test_bit - helper to test feature bits. For use by transports.
99 * Devices should normally use virtio_has_feature,
100 * which includes more checks.
Rusty Russellc45a6812008-05-02 21:50:50 -0500101 * @vdev: the device
102 * @fbit: the feature bit
103 */
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200104static inline bool __virtio_test_bit(const struct virtio_device *vdev,
105 unsigned int fbit)
106{
107 /* Did you forget to fix assumptions on max features? */
108 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200109 BUILD_BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200110 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200111 BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200112
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200113 return vdev->features & BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200114}
115
116/**
117 * __virtio_set_bit - helper to set feature bits. For use by transports.
118 * @vdev: the device
119 * @fbit: the feature bit
120 */
121static inline void __virtio_set_bit(struct virtio_device *vdev,
122 unsigned int fbit)
123{
124 /* Did you forget to fix assumptions on max features? */
125 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200126 BUILD_BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200127 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200128 BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200129
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200130 vdev->features |= BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200131}
132
133/**
134 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
135 * @vdev: the device
136 * @fbit: the feature bit
137 */
138static inline void __virtio_clear_bit(struct virtio_device *vdev,
Rusty Russellc45a6812008-05-02 21:50:50 -0500139 unsigned int fbit)
140{
141 /* Did you forget to fix assumptions on max features? */
Rusty Russell1765e3a2011-01-24 14:45:10 -0600142 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200143 BUILD_BUG_ON(fbit >= 64);
Rusty Russell1765e3a2011-01-24 14:45:10 -0600144 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200145 BUG_ON(fbit >= 64);
Rusty Russellc45a6812008-05-02 21:50:50 -0500146
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200147 vdev->features &= ~BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200148}
149
150/**
151 * virtio_has_feature - helper to determine if this device has this feature.
152 * @vdev: the device
153 * @fbit: the feature bit
154 */
155static inline bool virtio_has_feature(const struct virtio_device *vdev,
156 unsigned int fbit)
157{
Mark McLoughlinee006b352009-05-11 18:11:44 +0100158 if (fbit < VIRTIO_TRANSPORT_F_START)
159 virtio_check_driver_offered_feature(vdev, fbit);
160
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200161 return __virtio_test_bit(vdev, fbit);
Rusty Russellc45a6812008-05-02 21:50:50 -0500162}
163
Michael S. Tsirkin1a937692016-04-18 12:58:14 +0300164/**
165 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
166 * @vdev: the device
167 */
168static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
169{
170 /*
171 * Note the reverse polarity of the quirk feature (compared to most
172 * other features), this is for compatibility with legacy systems.
173 */
174 return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
175}
176
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600177static inline
178struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
179 vq_callback_t *c, const char *n)
180{
181 vq_callback_t *callbacks[] = { c };
182 const char *names[] = { n };
183 struct virtqueue *vq;
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200184 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
185 NULL);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600186 if (err < 0)
187 return ERR_PTR(err);
188 return vq;
189}
Rick Jones66846042011-11-14 14:17:08 +0000190
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200191static inline
192int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
193 struct virtqueue *vqs[], vq_callback_t *callbacks[],
194 const char * const names[],
195 struct irq_affinity *desc)
196{
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200197 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
198}
199
200static inline
201int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
202 struct virtqueue *vqs[], vq_callback_t *callbacks[],
203 const char * const names[], const bool *ctx,
204 struct irq_affinity *desc)
205{
206 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
207 desc);
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200208}
209
Michael S. Tsirkin3569db52014-10-15 10:22:30 +1030210/**
211 * virtio_device_ready - enable vq use in probe function
212 * @vdev: the device
213 *
214 * Driver must call this to use vqs in the probe function.
215 *
216 * Note: vqs are enabled automatically after probe returns.
217 */
218static inline
219void virtio_device_ready(struct virtio_device *dev)
220{
221 unsigned status = dev->config->get_status(dev);
222
223 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
224 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
225}
226
Rick Jones66846042011-11-14 14:17:08 +0000227static inline
228const char *virtio_bus_name(struct virtio_device *vdev)
229{
230 if (!vdev->config->bus_name)
231 return "virtio";
232 return vdev->config->bus_name(vdev);
233}
234
Jason Wang75a0a522012-08-28 13:54:14 +0200235/**
236 * virtqueue_set_affinity - setting affinity for a virtqueue
237 * @vq: the virtqueue
238 * @cpu: the cpu no.
239 *
240 * Pay attention the function are best-effort: the affinity hint may not be set
241 * due to config support, irq type and sharing.
242 *
243 */
244static inline
Caleb Raitto19e226e2018-08-09 18:18:28 -0700245int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
Jason Wang75a0a522012-08-28 13:54:14 +0200246{
247 struct virtio_device *vdev = vq->vdev;
248 if (vdev->config->set_vq_affinity)
Caleb Raitto19e226e2018-08-09 18:18:28 -0700249 return vdev->config->set_vq_affinity(vq, cpu_mask);
Jason Wang75a0a522012-08-28 13:54:14 +0200250 return 0;
251}
252
Greg Kurzcf561f02015-04-24 14:24:27 +0200253static inline bool virtio_is_little_endian(struct virtio_device *vdev)
254{
Greg Kurz7d824102015-04-24 14:26:24 +0200255 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
256 virtio_legacy_is_little_endian();
Greg Kurzcf561f02015-04-24 14:24:27 +0200257}
258
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300259/* Memory accessors */
260static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
261{
Greg Kurzcf561f02015-04-24 14:24:27 +0200262 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300263}
264
265static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
266{
Greg Kurzcf561f02015-04-24 14:24:27 +0200267 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300268}
269
270static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
271{
Greg Kurzcf561f02015-04-24 14:24:27 +0200272 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300273}
274
275static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
276{
Greg Kurzcf561f02015-04-24 14:24:27 +0200277 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300278}
279
280static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
281{
Greg Kurzcf561f02015-04-24 14:24:27 +0200282 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300283}
284
285static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
286{
Greg Kurzcf561f02015-04-24 14:24:27 +0200287 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300288}
289
Rusty Russell0b90d062013-10-14 18:11:51 +1030290/* Config space accessors. */
291#define virtio_cread(vdev, structname, member, ptr) \
292 do { \
Cornelia Huckab7a2372019-01-31 13:53:14 +0100293 might_sleep(); \
Rusty Russell0b90d062013-10-14 18:11:51 +1030294 /* Must match the member's type, and be integer */ \
295 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
296 (*ptr) = 1; \
297 \
298 switch (sizeof(*ptr)) { \
299 case 1: \
300 *(ptr) = virtio_cread8(vdev, \
301 offsetof(structname, member)); \
302 break; \
303 case 2: \
304 *(ptr) = virtio_cread16(vdev, \
305 offsetof(structname, member)); \
306 break; \
307 case 4: \
308 *(ptr) = virtio_cread32(vdev, \
309 offsetof(structname, member)); \
310 break; \
311 case 8: \
312 *(ptr) = virtio_cread64(vdev, \
313 offsetof(structname, member)); \
314 break; \
315 default: \
316 BUG(); \
317 } \
318 } while(0)
319
320/* Config space accessors. */
321#define virtio_cwrite(vdev, structname, member, ptr) \
322 do { \
Cornelia Huckab7a2372019-01-31 13:53:14 +0100323 might_sleep(); \
Rusty Russell0b90d062013-10-14 18:11:51 +1030324 /* Must match the member's type, and be integer */ \
325 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
326 BUG_ON((*ptr) == 1); \
327 \
328 switch (sizeof(*ptr)) { \
329 case 1: \
330 virtio_cwrite8(vdev, \
331 offsetof(structname, member), \
332 *(ptr)); \
333 break; \
334 case 2: \
335 virtio_cwrite16(vdev, \
336 offsetof(structname, member), \
337 *(ptr)); \
338 break; \
339 case 4: \
340 virtio_cwrite32(vdev, \
341 offsetof(structname, member), \
342 *(ptr)); \
343 break; \
344 case 8: \
345 virtio_cwrite64(vdev, \
346 offsetof(structname, member), \
347 *(ptr)); \
348 break; \
349 default: \
350 BUG(); \
351 } \
352 } while(0)
353
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200354/* Read @count fields, @bytes each. */
355static inline void __virtio_cread_many(struct virtio_device *vdev,
356 unsigned int offset,
357 void *buf, size_t count, size_t bytes)
358{
359 u32 old, gen = vdev->config->generation ?
360 vdev->config->generation(vdev) : 0;
361 int i;
362
Cornelia Huckab7a2372019-01-31 13:53:14 +0100363 might_sleep();
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200364 do {
365 old = gen;
366
367 for (i = 0; i < count; i++)
368 vdev->config->get(vdev, offset + bytes * i,
369 buf + i * bytes, bytes);
370
371 gen = vdev->config->generation ?
372 vdev->config->generation(vdev) : 0;
373 } while (gen != old);
374}
375
Rusty Russell0b90d062013-10-14 18:11:51 +1030376static inline void virtio_cread_bytes(struct virtio_device *vdev,
377 unsigned int offset,
378 void *buf, size_t len)
379{
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200380 __virtio_cread_many(vdev, offset, buf, len, 1);
Rusty Russell0b90d062013-10-14 18:11:51 +1030381}
382
Michael S. Tsirkincaa0e2d2015-04-01 08:21:51 +1030383static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
384{
385 u8 ret;
Cornelia Huckab7a2372019-01-31 13:53:14 +0100386
387 might_sleep();
Michael S. Tsirkincaa0e2d2015-04-01 08:21:51 +1030388 vdev->config->get(vdev, offset, &ret, sizeof(ret));
389 return ret;
390}
391
Rusty Russell0b90d062013-10-14 18:11:51 +1030392static inline void virtio_cwrite8(struct virtio_device *vdev,
393 unsigned int offset, u8 val)
394{
Cornelia Huckab7a2372019-01-31 13:53:14 +0100395 might_sleep();
Rusty Russell0b90d062013-10-14 18:11:51 +1030396 vdev->config->set(vdev, offset, &val, sizeof(val));
397}
398
399static inline u16 virtio_cread16(struct virtio_device *vdev,
400 unsigned int offset)
401{
402 u16 ret;
Cornelia Huckab7a2372019-01-31 13:53:14 +0100403
404 might_sleep();
Rusty Russell0b90d062013-10-14 18:11:51 +1030405 vdev->config->get(vdev, offset, &ret, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300406 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030407}
408
409static inline void virtio_cwrite16(struct virtio_device *vdev,
410 unsigned int offset, u16 val)
411{
Cornelia Huckab7a2372019-01-31 13:53:14 +0100412 might_sleep();
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300413 val = (__force u16)cpu_to_virtio16(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030414 vdev->config->set(vdev, offset, &val, sizeof(val));
415}
416
417static inline u32 virtio_cread32(struct virtio_device *vdev,
418 unsigned int offset)
419{
420 u32 ret;
Cornelia Huckab7a2372019-01-31 13:53:14 +0100421
422 might_sleep();
Rusty Russell0b90d062013-10-14 18:11:51 +1030423 vdev->config->get(vdev, offset, &ret, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300424 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030425}
426
427static inline void virtio_cwrite32(struct virtio_device *vdev,
428 unsigned int offset, u32 val)
429{
Cornelia Huckab7a2372019-01-31 13:53:14 +0100430 might_sleep();
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300431 val = (__force u32)cpu_to_virtio32(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030432 vdev->config->set(vdev, offset, &val, sizeof(val));
433}
434
435static inline u64 virtio_cread64(struct virtio_device *vdev,
436 unsigned int offset)
437{
438 u64 ret;
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200439 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300440 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030441}
442
443static inline void virtio_cwrite64(struct virtio_device *vdev,
444 unsigned int offset, u64 val)
445{
Cornelia Huckab7a2372019-01-31 13:53:14 +0100446 might_sleep();
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300447 val = (__force u64)cpu_to_virtio64(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030448 vdev->config->set(vdev, offset, &val, sizeof(val));
449}
450
451/* Conditional config space accessors. */
452#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
453 ({ \
454 int _r = 0; \
455 if (!virtio_has_feature(vdev, fbit)) \
456 _r = -ENOENT; \
457 else \
458 virtio_cread((vdev), structname, member, ptr); \
459 _r; \
460 })
Jason Wang75a0a522012-08-28 13:54:14 +0200461
Rusty Russellec3d41c2007-10-22 11:03:36 +1000462#endif /* _LINUX_VIRTIO_CONFIG_H */