Skip to content

Commit 3cfc883

Browse files
ssqrewsakernel
authored andcommitted
i2c: virtio: add a virtio i2c frontend driver
Add an I2C bus driver for virtio para-virtualization. The controller can be emulated by the backend driver in any device model software by following the virtio protocol. The device specification can be found on https://lists.oasis-open.org/archives/virtio-comment/202101/msg00008.html. By following the specification, people may implement different backend drivers to emulate different controllers according to their needs. Co-developed-by: Conghui Chen <[email protected]> Signed-off-by: Conghui Chen <[email protected]> Signed-off-by: Jie Deng <[email protected]> Reviewed-by: Viresh Kumar <[email protected]> Tested-by: Viresh Kumar <[email protected]> Acked-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent f9b459c commit 3cfc883

File tree

6 files changed

+353
-0
lines changed

6 files changed

+353
-0
lines changed

MAINTAINERS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19708,6 +19708,15 @@ S: Maintained
1970819708
F: include/uapi/linux/virtio_snd.h
1970919709
F: sound/virtio/*
1971019710

19711+
VIRTIO I2C DRIVER
19712+
M: Jie Deng <[email protected]>
19713+
M: Viresh Kumar <[email protected]>
19714+
19715+
19716+
S: Maintained
19717+
F: drivers/i2c/busses/i2c-virtio.c
19718+
F: include/uapi/linux/virtio_i2c.h
19719+
1971119720
VIRTUAL BOX GUEST DEVICE DRIVER
1971219721
M: Hans de Goede <[email protected]>
1971319722
M: Arnd Bergmann <[email protected]>

drivers/i2c/busses/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,4 +1393,15 @@ config I2C_FSI
13931393
This driver can also be built as a module. If so, the module will be
13941394
called as i2c-fsi.
13951395

1396+
config I2C_VIRTIO
1397+
tristate "Virtio I2C Adapter"
1398+
select VIRTIO
1399+
help
1400+
If you say yes to this option, support will be included for the virtio
1401+
I2C adapter driver. The hardware can be emulated by any device model
1402+
software according to the virtio protocol.
1403+
1404+
This driver can also be built as a module. If so, the module
1405+
will be called i2c-virtio.
1406+
13961407
endmenu

drivers/i2c/busses/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,6 @@ obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o
145145
obj-$(CONFIG_I2C_XGENE_SLIMPRO) += i2c-xgene-slimpro.o
146146
obj-$(CONFIG_SCx200_ACB) += scx200_acb.o
147147
obj-$(CONFIG_I2C_FSI) += i2c-fsi.o
148+
obj-$(CONFIG_I2C_VIRTIO) += i2c-virtio.o
148149

149150
ccflags-$(CONFIG_I2C_DEBUG_BUS) := -DDEBUG

drivers/i2c/busses/i2c-virtio.c

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
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+
11+
#include <linux/acpi.h>
12+
#include <linux/completion.h>
13+
#include <linux/err.h>
14+
#include <linux/i2c.h>
15+
#include <linux/kernel.h>
16+
#include <linux/module.h>
17+
#include <linux/virtio.h>
18+
#include <linux/virtio_ids.h>
19+
#include <linux/virtio_config.h>
20+
#include <linux/virtio_i2c.h>
21+
22+
/**
23+
* struct virtio_i2c - virtio I2C data
24+
* @vdev: virtio device for this controller
25+
* @completion: completion of virtio I2C message
26+
* @adap: I2C adapter for this controller
27+
* @vq: the virtio virtqueue for communication
28+
*/
29+
struct virtio_i2c {
30+
struct virtio_device *vdev;
31+
struct completion completion;
32+
struct i2c_adapter adap;
33+
struct virtqueue *vq;
34+
};
35+
36+
/**
37+
* struct virtio_i2c_req - the virtio I2C request structure
38+
* @out_hdr: the OUT header of the virtio I2C message
39+
* @buf: the buffer into which data is read, or from which it's written
40+
* @in_hdr: the IN header of the virtio I2C message
41+
*/
42+
struct virtio_i2c_req {
43+
struct virtio_i2c_out_hdr out_hdr ____cacheline_aligned;
44+
uint8_t *buf ____cacheline_aligned;
45+
struct virtio_i2c_in_hdr in_hdr ____cacheline_aligned;
46+
};
47+
48+
static void virtio_i2c_msg_done(struct virtqueue *vq)
49+
{
50+
struct virtio_i2c *vi = vq->vdev->priv;
51+
52+
complete(&vi->completion);
53+
}
54+
55+
static int virtio_i2c_prepare_reqs(struct virtqueue *vq,
56+
struct virtio_i2c_req *reqs,
57+
struct i2c_msg *msgs, int num)
58+
{
59+
struct scatterlist *sgs[3], out_hdr, msg_buf, in_hdr;
60+
int i;
61+
62+
for (i = 0; i < num; i++) {
63+
int outcnt = 0, incnt = 0;
64+
65+
/*
66+
* We don't support 0 length messages and so filter out
67+
* 0 length transfers by using i2c_adapter_quirks.
68+
*/
69+
if (!msgs[i].len)
70+
break;
71+
72+
/*
73+
* Only 7-bit mode supported for this moment. For the address
74+
* format, Please check the Virtio I2C Specification.
75+
*/
76+
reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1);
77+
78+
if (i != num - 1)
79+
reqs[i].out_hdr.flags = cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT);
80+
81+
sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr));
82+
sgs[outcnt++] = &out_hdr;
83+
84+
reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1);
85+
if (!reqs[i].buf)
86+
break;
87+
88+
sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len);
89+
90+
if (msgs[i].flags & I2C_M_RD)
91+
sgs[outcnt + incnt++] = &msg_buf;
92+
else
93+
sgs[outcnt++] = &msg_buf;
94+
95+
sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr));
96+
sgs[outcnt + incnt++] = &in_hdr;
97+
98+
if (virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
99+
i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false);
100+
break;
101+
}
102+
}
103+
104+
return i;
105+
}
106+
107+
static int virtio_i2c_complete_reqs(struct virtqueue *vq,
108+
struct virtio_i2c_req *reqs,
109+
struct i2c_msg *msgs, int num,
110+
bool timedout)
111+
{
112+
struct virtio_i2c_req *req;
113+
bool failed = timedout;
114+
unsigned int len;
115+
int i, j = 0;
116+
117+
for (i = 0; i < num; i++) {
118+
/* Detach the ith request from the vq */
119+
req = virtqueue_get_buf(vq, &len);
120+
121+
/*
122+
* Condition req == &reqs[i] should always meet since we have
123+
* total num requests in the vq. reqs[i] can never be NULL here.
124+
*/
125+
if (!failed && (WARN_ON(req != &reqs[i]) ||
126+
req->in_hdr.status != VIRTIO_I2C_MSG_OK))
127+
failed = true;
128+
129+
i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], !failed);
130+
131+
if (!failed)
132+
j++;
133+
}
134+
135+
return timedout ? -ETIMEDOUT : j;
136+
}
137+
138+
static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
139+
int num)
140+
{
141+
struct virtio_i2c *vi = i2c_get_adapdata(adap);
142+
struct virtqueue *vq = vi->vq;
143+
struct virtio_i2c_req *reqs;
144+
unsigned long time_left;
145+
int count;
146+
147+
reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
148+
if (!reqs)
149+
return -ENOMEM;
150+
151+
count = virtio_i2c_prepare_reqs(vq, reqs, msgs, num);
152+
if (!count)
153+
goto err_free;
154+
155+
/*
156+
* For the case where count < num, i.e. we weren't able to queue all the
157+
* msgs, ideally we should abort right away and return early, but some
158+
* of the messages are already sent to the remote I2C controller and the
159+
* virtqueue will be left in undefined state in that case. We kick the
160+
* remote here to clear the virtqueue, so we can try another set of
161+
* messages later on.
162+
*/
163+
164+
reinit_completion(&vi->completion);
165+
virtqueue_kick(vq);
166+
167+
time_left = wait_for_completion_timeout(&vi->completion, adap->timeout);
168+
if (!time_left)
169+
dev_err(&adap->dev, "virtio i2c backend timeout.\n");
170+
171+
count = virtio_i2c_complete_reqs(vq, reqs, msgs, count, !time_left);
172+
173+
err_free:
174+
kfree(reqs);
175+
return count;
176+
}
177+
178+
static void virtio_i2c_del_vqs(struct virtio_device *vdev)
179+
{
180+
vdev->config->reset(vdev);
181+
vdev->config->del_vqs(vdev);
182+
}
183+
184+
static int virtio_i2c_setup_vqs(struct virtio_i2c *vi)
185+
{
186+
struct virtio_device *vdev = vi->vdev;
187+
188+
vi->vq = virtio_find_single_vq(vdev, virtio_i2c_msg_done, "msg");
189+
return PTR_ERR_OR_ZERO(vi->vq);
190+
}
191+
192+
static u32 virtio_i2c_func(struct i2c_adapter *adap)
193+
{
194+
return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
195+
}
196+
197+
static struct i2c_algorithm virtio_algorithm = {
198+
.master_xfer = virtio_i2c_xfer,
199+
.functionality = virtio_i2c_func,
200+
};
201+
202+
static const struct i2c_adapter_quirks virtio_i2c_quirks = {
203+
.flags = I2C_AQ_NO_ZERO_LEN,
204+
};
205+
206+
static int virtio_i2c_probe(struct virtio_device *vdev)
207+
{
208+
struct virtio_i2c *vi;
209+
int ret;
210+
211+
vi = devm_kzalloc(&vdev->dev, sizeof(*vi), GFP_KERNEL);
212+
if (!vi)
213+
return -ENOMEM;
214+
215+
vdev->priv = vi;
216+
vi->vdev = vdev;
217+
218+
init_completion(&vi->completion);
219+
220+
ret = virtio_i2c_setup_vqs(vi);
221+
if (ret)
222+
return ret;
223+
224+
vi->adap.owner = THIS_MODULE;
225+
snprintf(vi->adap.name, sizeof(vi->adap.name),
226+
"i2c_virtio at virtio bus %d", vdev->index);
227+
vi->adap.algo = &virtio_algorithm;
228+
vi->adap.quirks = &virtio_i2c_quirks;
229+
vi->adap.dev.parent = &vdev->dev;
230+
vi->adap.dev.of_node = vdev->dev.of_node;
231+
i2c_set_adapdata(&vi->adap, vi);
232+
233+
/*
234+
* Setup ACPI node for controlled devices which will be probed through
235+
* ACPI.
236+
*/
237+
ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(vdev->dev.parent));
238+
239+
ret = i2c_add_adapter(&vi->adap);
240+
if (ret)
241+
virtio_i2c_del_vqs(vdev);
242+
243+
return ret;
244+
}
245+
246+
static void virtio_i2c_remove(struct virtio_device *vdev)
247+
{
248+
struct virtio_i2c *vi = vdev->priv;
249+
250+
i2c_del_adapter(&vi->adap);
251+
virtio_i2c_del_vqs(vdev);
252+
}
253+
254+
static struct virtio_device_id id_table[] = {
255+
{ VIRTIO_ID_I2C_ADAPTER, VIRTIO_DEV_ANY_ID },
256+
{}
257+
};
258+
MODULE_DEVICE_TABLE(virtio, id_table);
259+
260+
#ifdef CONFIG_PM_SLEEP
261+
static int virtio_i2c_freeze(struct virtio_device *vdev)
262+
{
263+
virtio_i2c_del_vqs(vdev);
264+
return 0;
265+
}
266+
267+
static int virtio_i2c_restore(struct virtio_device *vdev)
268+
{
269+
return virtio_i2c_setup_vqs(vdev->priv);
270+
}
271+
#endif
272+
273+
static struct virtio_driver virtio_i2c_driver = {
274+
.id_table = id_table,
275+
.probe = virtio_i2c_probe,
276+
.remove = virtio_i2c_remove,
277+
.driver = {
278+
.name = "i2c_virtio",
279+
},
280+
#ifdef CONFIG_PM_SLEEP
281+
.freeze = virtio_i2c_freeze,
282+
.restore = virtio_i2c_restore,
283+
#endif
284+
};
285+
module_virtio_driver(virtio_i2c_driver);
286+
287+
MODULE_AUTHOR("Jie Deng <[email protected]>");
288+
MODULE_AUTHOR("Conghui Chen <[email protected]>");
289+
MODULE_DESCRIPTION("Virtio i2c bus driver");
290+
MODULE_LICENSE("GPL");

include/uapi/linux/virtio_i2c.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later WITH Linux-syscall-note */
2+
/*
3+
* Definitions for virtio I2C Adpter
4+
*
5+
* Copyright (c) 2021 Intel Corporation. All rights reserved.
6+
*/
7+
8+
#ifndef _UAPI_LINUX_VIRTIO_I2C_H
9+
#define _UAPI_LINUX_VIRTIO_I2C_H
10+
11+
#include <linux/const.h>
12+
#include <linux/types.h>
13+
14+
/* The bit 0 of the @virtio_i2c_out_hdr.@flags, used to group the requests */
15+
#define VIRTIO_I2C_FLAGS_FAIL_NEXT _BITUL(0)
16+
17+
/**
18+
* struct virtio_i2c_out_hdr - the virtio I2C message OUT header
19+
* @addr: the controlled device address
20+
* @padding: used to pad to full dword
21+
* @flags: used for feature extensibility
22+
*/
23+
struct virtio_i2c_out_hdr {
24+
__le16 addr;
25+
__le16 padding;
26+
__le32 flags;
27+
};
28+
29+
/**
30+
* struct virtio_i2c_in_hdr - the virtio I2C message IN header
31+
* @status: the processing result from the backend
32+
*/
33+
struct virtio_i2c_in_hdr {
34+
__u8 status;
35+
};
36+
37+
/* The final status written by the device */
38+
#define VIRTIO_I2C_MSG_OK 0
39+
#define VIRTIO_I2C_MSG_ERR 1
40+
41+
#endif /* _UAPI_LINUX_VIRTIO_I2C_H */

include/uapi/linux/virtio_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#define VIRTIO_ID_FS 26 /* virtio filesystem */
5656
#define VIRTIO_ID_PMEM 27 /* virtio pmem */
5757
#define VIRTIO_ID_MAC80211_HWSIM 29 /* virtio mac80211-hwsim */
58+
#define VIRTIO_ID_I2C_ADAPTER 34 /* virtio i2c adapter */
5859
#define VIRTIO_ID_BT 40 /* virtio bluetooth */
5960

6061
/*

0 commit comments

Comments
 (0)