i2c-virtio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio I2C Bus Driver
  4. *
  5. * The Virtio I2C Specification:
  6. * https://raw.githubusercontent.com/oasis-tcs/virtio-spec/master/virtio-i2c.tex
  7. *
  8. * Copyright (c) 2021 Intel Corporation. All rights reserved.
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/completion.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_ids.h>
  18. #include <linux/virtio_config.h>
  19. #include <linux/virtio_i2c.h>
  20. /**
  21. * struct virtio_i2c - virtio I2C data
  22. * @vdev: virtio device for this controller
  23. * @adap: I2C adapter for this controller
  24. * @vq: the virtio virtqueue for communication
  25. */
  26. struct virtio_i2c {
  27. struct virtio_device *vdev;
  28. struct i2c_adapter adap;
  29. struct virtqueue *vq;
  30. };
  31. /**
  32. * struct virtio_i2c_req - the virtio I2C request structure
  33. * @completion: completion of virtio I2C message
  34. * @out_hdr: the OUT header of the virtio I2C message
  35. * @buf: the buffer into which data is read, or from which it's written
  36. * @in_hdr: the IN header of the virtio I2C message
  37. */
  38. struct virtio_i2c_req {
  39. struct completion completion;
  40. struct virtio_i2c_out_hdr out_hdr ____cacheline_aligned;
  41. uint8_t *buf ____cacheline_aligned;
  42. struct virtio_i2c_in_hdr in_hdr ____cacheline_aligned;
  43. };
  44. static void virtio_i2c_msg_done(struct virtqueue *vq)
  45. {
  46. struct virtio_i2c_req *req;
  47. unsigned int len;
  48. while ((req = virtqueue_get_buf(vq, &len)))
  49. complete(&req->completion);
  50. }
  51. static int virtio_i2c_prepare_reqs(struct virtqueue *vq,
  52. struct virtio_i2c_req *reqs,
  53. struct i2c_msg *msgs, int num)
  54. {
  55. struct scatterlist *sgs[3], out_hdr, msg_buf, in_hdr;
  56. int i;
  57. for (i = 0; i < num; i++) {
  58. int outcnt = 0, incnt = 0;
  59. init_completion(&reqs[i].completion);
  60. /*
  61. * Only 7-bit mode supported for this moment. For the address
  62. * format, Please check the Virtio I2C Specification.
  63. */
  64. reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1);
  65. if (msgs[i].flags & I2C_M_RD)
  66. reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_M_RD);
  67. if (i != num - 1)
  68. reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT);
  69. sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr));
  70. sgs[outcnt++] = &out_hdr;
  71. if (msgs[i].len) {
  72. reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1);
  73. if (!reqs[i].buf)
  74. break;
  75. sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len);
  76. if (msgs[i].flags & I2C_M_RD)
  77. sgs[outcnt + incnt++] = &msg_buf;
  78. else
  79. sgs[outcnt++] = &msg_buf;
  80. }
  81. sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr));
  82. sgs[outcnt + incnt++] = &in_hdr;
  83. if (virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
  84. i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false);
  85. break;
  86. }
  87. }
  88. return i;
  89. }
  90. static int virtio_i2c_complete_reqs(struct virtqueue *vq,
  91. struct virtio_i2c_req *reqs,
  92. struct i2c_msg *msgs, int num)
  93. {
  94. bool failed = false;
  95. int i, j = 0;
  96. for (i = 0; i < num; i++) {
  97. struct virtio_i2c_req *req = &reqs[i];
  98. if (!failed) {
  99. if (wait_for_completion_interruptible(&req->completion))
  100. failed = true;
  101. else if (req->in_hdr.status != VIRTIO_I2C_MSG_OK)
  102. failed = true;
  103. else
  104. j++;
  105. }
  106. i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], !failed);
  107. }
  108. return j;
  109. }
  110. static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  111. int num)
  112. {
  113. struct virtio_i2c *vi = i2c_get_adapdata(adap);
  114. struct virtqueue *vq = vi->vq;
  115. struct virtio_i2c_req *reqs;
  116. int count;
  117. reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
  118. if (!reqs)
  119. return -ENOMEM;
  120. count = virtio_i2c_prepare_reqs(vq, reqs, msgs, num);
  121. if (!count)
  122. goto err_free;
  123. /*
  124. * For the case where count < num, i.e. we weren't able to queue all the
  125. * msgs, ideally we should abort right away and return early, but some
  126. * of the messages are already sent to the remote I2C controller and the
  127. * virtqueue will be left in undefined state in that case. We kick the
  128. * remote here to clear the virtqueue, so we can try another set of
  129. * messages later on.
  130. */
  131. virtqueue_kick(vq);
  132. count = virtio_i2c_complete_reqs(vq, reqs, msgs, count);
  133. err_free:
  134. kfree(reqs);
  135. return count;
  136. }
  137. static void virtio_i2c_del_vqs(struct virtio_device *vdev)
  138. {
  139. virtio_reset_device(vdev);
  140. vdev->config->del_vqs(vdev);
  141. }
  142. static int virtio_i2c_setup_vqs(struct virtio_i2c *vi)
  143. {
  144. struct virtio_device *vdev = vi->vdev;
  145. vi->vq = virtio_find_single_vq(vdev, virtio_i2c_msg_done, "msg");
  146. return PTR_ERR_OR_ZERO(vi->vq);
  147. }
  148. static u32 virtio_i2c_func(struct i2c_adapter *adap)
  149. {
  150. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  151. }
  152. static const struct i2c_algorithm virtio_algorithm = {
  153. .xfer = virtio_i2c_xfer,
  154. .functionality = virtio_i2c_func,
  155. };
  156. static int virtio_i2c_probe(struct virtio_device *vdev)
  157. {
  158. struct virtio_i2c *vi;
  159. int ret;
  160. if (!virtio_has_feature(vdev, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST)) {
  161. dev_err(&vdev->dev, "Zero-length request feature is mandatory\n");
  162. return -EINVAL;
  163. }
  164. vi = devm_kzalloc(&vdev->dev, sizeof(*vi), GFP_KERNEL);
  165. if (!vi)
  166. return -ENOMEM;
  167. vdev->priv = vi;
  168. vi->vdev = vdev;
  169. ret = virtio_i2c_setup_vqs(vi);
  170. if (ret)
  171. return ret;
  172. vi->adap.owner = THIS_MODULE;
  173. snprintf(vi->adap.name, sizeof(vi->adap.name),
  174. "i2c_virtio at virtio bus %d", vdev->index);
  175. vi->adap.algo = &virtio_algorithm;
  176. vi->adap.dev.parent = &vdev->dev;
  177. vi->adap.dev.of_node = vdev->dev.of_node;
  178. i2c_set_adapdata(&vi->adap, vi);
  179. /*
  180. * Setup ACPI node for controlled devices which will be probed through
  181. * ACPI.
  182. */
  183. ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(vdev->dev.parent));
  184. ret = i2c_add_adapter(&vi->adap);
  185. if (ret)
  186. virtio_i2c_del_vqs(vdev);
  187. return ret;
  188. }
  189. static void virtio_i2c_remove(struct virtio_device *vdev)
  190. {
  191. struct virtio_i2c *vi = vdev->priv;
  192. i2c_del_adapter(&vi->adap);
  193. virtio_i2c_del_vqs(vdev);
  194. }
  195. static const struct virtio_device_id id_table[] = {
  196. { VIRTIO_ID_I2C_ADAPTER, VIRTIO_DEV_ANY_ID },
  197. {}
  198. };
  199. MODULE_DEVICE_TABLE(virtio, id_table);
  200. static int virtio_i2c_freeze(struct virtio_device *vdev)
  201. {
  202. virtio_i2c_del_vqs(vdev);
  203. return 0;
  204. }
  205. static int virtio_i2c_restore(struct virtio_device *vdev)
  206. {
  207. return virtio_i2c_setup_vqs(vdev->priv);
  208. }
  209. static const unsigned int features[] = {
  210. VIRTIO_I2C_F_ZERO_LENGTH_REQUEST,
  211. };
  212. static struct virtio_driver virtio_i2c_driver = {
  213. .feature_table = features,
  214. .feature_table_size = ARRAY_SIZE(features),
  215. .id_table = id_table,
  216. .probe = virtio_i2c_probe,
  217. .remove = virtio_i2c_remove,
  218. .driver = {
  219. .name = "i2c_virtio",
  220. },
  221. .freeze = pm_sleep_ptr(virtio_i2c_freeze),
  222. .restore = pm_sleep_ptr(virtio_i2c_restore),
  223. };
  224. module_virtio_driver(virtio_i2c_driver);
  225. MODULE_AUTHOR("Jie Deng <jie.deng@intel.com>");
  226. MODULE_AUTHOR("Conghui Chen <conghui.chen@intel.com>");
  227. MODULE_DESCRIPTION("Virtio i2c bus driver");
  228. MODULE_LICENSE("GPL");