virtio_config.h 16 KB

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