]> Git Repo - linux.git/blob - drivers/misc/mei/hw-virtio.c
KVM: SVM: Add required changes to support intercepts under SEV-ES
[linux.git] / drivers / misc / mei / hw-virtio.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2018-2020, Intel Corporation.
5  */
6 #include <linux/err.h>
7 #include <linux/module.h>
8 #include <linux/pm_runtime.h>
9 #include <linux/scatterlist.h>
10 #include <linux/spinlock.h>
11 #include <linux/slab.h>
12 #include <linux/virtio.h>
13 #include <linux/virtio_config.h>
14 #include <linux/virtio_ids.h>
15 #include <linux/atomic.h>
16
17 #include "mei_dev.h"
18 #include "hbm.h"
19 #include "client.h"
20
21 #define MEI_VIRTIO_RPM_TIMEOUT 500
22 /* ACRN virtio device types */
23 #ifndef VIRTIO_ID_MEI
24 #define VIRTIO_ID_MEI 0xFFFE /* virtio mei */
25 #endif
26
27 /**
28  * struct mei_virtio_cfg - settings passed from the virtio backend
29  * @buf_depth: read buffer depth in slots (4bytes)
30  * @hw_ready: hw is ready for operation
31  * @host_reset: synchronize reset with virtio backend
32  * @reserved: reserved for alignment
33  * @fw_status: FW status
34  */
35 struct mei_virtio_cfg {
36         u32 buf_depth;
37         u8 hw_ready;
38         u8 host_reset;
39         u8 reserved[2];
40         u32 fw_status[MEI_FW_STATUS_MAX];
41 } __packed;
42
43 struct mei_virtio_hw {
44         struct mei_device mdev;
45         char name[32];
46
47         struct virtqueue *in;
48         struct virtqueue *out;
49
50         bool host_ready;
51         struct work_struct intr_handler;
52
53         u32 *recv_buf;
54         u8 recv_rdy;
55         size_t recv_sz;
56         u32 recv_idx;
57         u32 recv_len;
58
59         /* send buffer */
60         atomic_t hbuf_ready;
61         const void *send_hdr;
62         const void *send_buf;
63
64         struct mei_virtio_cfg cfg;
65 };
66
67 #define to_virtio_hw(_dev) container_of(_dev, struct mei_virtio_hw, mdev)
68
69 /**
70  * mei_virtio_fw_status() - read status register of mei
71  * @dev: mei device
72  * @fw_status: fw status register values
73  *
74  * Return: always 0
75  */
76 static int mei_virtio_fw_status(struct mei_device *dev,
77                                 struct mei_fw_status *fw_status)
78 {
79         struct virtio_device *vdev = dev_to_virtio(dev->dev);
80
81         fw_status->count = MEI_FW_STATUS_MAX;
82         virtio_cread_bytes(vdev, offsetof(struct mei_virtio_cfg, fw_status),
83                            fw_status->status, sizeof(fw_status->status));
84         return 0;
85 }
86
87 /**
88  * mei_virtio_pg_state() - translate internal pg state
89  *   to the mei power gating state
90  *   There is no power management in ACRN mode always return OFF
91  * @dev: mei device
92  *
93  * Return:
94  * * MEI_PG_OFF - if aliveness is on (always)
95  * * MEI_PG_ON  - (never)
96  */
97 static inline enum mei_pg_state mei_virtio_pg_state(struct mei_device *dev)
98 {
99         return MEI_PG_OFF;
100 }
101
102 /**
103  * mei_virtio_hw_config() - configure hw dependent settings
104  *
105  * @dev: mei device
106  *
107  * Return: always 0
108  */
109 static int mei_virtio_hw_config(struct mei_device *dev)
110 {
111         return 0;
112 }
113
114 /**
115  * mei_virtio_hbuf_empty_slots() - counts write empty slots.
116  * @dev: the device structure
117  *
118  * Return: always return frontend buf size if buffer is ready, 0 otherwise
119  */
120 static int mei_virtio_hbuf_empty_slots(struct mei_device *dev)
121 {
122         struct mei_virtio_hw *hw = to_virtio_hw(dev);
123
124         return (atomic_read(&hw->hbuf_ready) == 1) ? hw->cfg.buf_depth : 0;
125 }
126
127 /**
128  * mei_virtio_hbuf_is_ready() - checks if write buffer is ready
129  * @dev: the device structure
130  *
131  * Return: true if hbuf is ready
132  */
133 static bool mei_virtio_hbuf_is_ready(struct mei_device *dev)
134 {
135         struct mei_virtio_hw *hw = to_virtio_hw(dev);
136
137         return atomic_read(&hw->hbuf_ready) == 1;
138 }
139
140 /**
141  * mei_virtio_hbuf_max_depth() - returns depth of FE write buffer.
142  * @dev: the device structure
143  *
144  * Return: size of frontend write buffer in bytes
145  */
146 static u32 mei_virtio_hbuf_depth(const struct mei_device *dev)
147 {
148         struct mei_virtio_hw *hw = to_virtio_hw(dev);
149
150         return hw->cfg.buf_depth;
151 }
152
153 /**
154  * mei_virtio_intr_clear() - clear and stop interrupts
155  * @dev: the device structure
156  */
157 static void mei_virtio_intr_clear(struct mei_device *dev)
158 {
159         /*
160          * In our virtio solution, there are two types of interrupts,
161          * vq interrupt and config change interrupt.
162          *   1) start/reset rely on virtio config changed interrupt;
163          *   2) send/recv rely on virtio virtqueue interrupts.
164          * They are all virtual interrupts. So, we don't have corresponding
165          * operation to do here.
166          */
167 }
168
169 /**
170  * mei_virtio_intr_enable() - enables mei BE virtqueues callbacks
171  * @dev: the device structure
172  */
173 static void mei_virtio_intr_enable(struct mei_device *dev)
174 {
175         struct mei_virtio_hw *hw = to_virtio_hw(dev);
176         struct virtio_device *vdev = dev_to_virtio(dev->dev);
177
178         virtio_config_enable(vdev);
179
180         virtqueue_enable_cb(hw->in);
181         virtqueue_enable_cb(hw->out);
182 }
183
184 /**
185  * mei_virtio_intr_disable() - disables mei BE virtqueues callbacks
186  *
187  * @dev: the device structure
188  */
189 static void mei_virtio_intr_disable(struct mei_device *dev)
190 {
191         struct mei_virtio_hw *hw = to_virtio_hw(dev);
192         struct virtio_device *vdev = dev_to_virtio(dev->dev);
193
194         virtio_config_disable(vdev);
195
196         virtqueue_disable_cb(hw->in);
197         virtqueue_disable_cb(hw->out);
198 }
199
200 /**
201  * mei_virtio_synchronize_irq() - wait for pending IRQ handlers for all
202  *     virtqueue
203  * @dev: the device structure
204  */
205 static void mei_virtio_synchronize_irq(struct mei_device *dev)
206 {
207         struct mei_virtio_hw *hw = to_virtio_hw(dev);
208
209         /*
210          * Now, all IRQ handlers are converted to workqueue.
211          * Change synchronize irq to flush this work.
212          */
213         flush_work(&hw->intr_handler);
214 }
215
216 static void mei_virtio_free_outbufs(struct mei_virtio_hw *hw)
217 {
218         kfree(hw->send_hdr);
219         kfree(hw->send_buf);
220         hw->send_hdr = NULL;
221         hw->send_buf = NULL;
222 }
223
224 /**
225  * mei_virtio_write_message() - writes a message to mei virtio back-end service.
226  * @dev: the device structure
227  * @hdr: mei header of message
228  * @hdr_len: header length
229  * @data: message payload will be written
230  * @data_len: message payload length
231  *
232  * Return:
233  * *  0: on success
234  * * -EIO: if write has failed
235  * * -ENOMEM: on memory allocation failure
236  */
237 static int mei_virtio_write_message(struct mei_device *dev,
238                                     const void *hdr, size_t hdr_len,
239                                     const void *data, size_t data_len)
240 {
241         struct mei_virtio_hw *hw = to_virtio_hw(dev);
242         struct scatterlist sg[2];
243         const void *hbuf, *dbuf;
244         int ret;
245
246         if (WARN_ON(!atomic_add_unless(&hw->hbuf_ready, -1, 0)))
247                 return -EIO;
248
249         hbuf = kmemdup(hdr, hdr_len, GFP_KERNEL);
250         hw->send_hdr = hbuf;
251
252         dbuf = kmemdup(data, data_len, GFP_KERNEL);
253         hw->send_buf = dbuf;
254
255         if (!hbuf || !dbuf) {
256                 ret = -ENOMEM;
257                 goto fail;
258         }
259
260         sg_init_table(sg, 2);
261         sg_set_buf(&sg[0], hbuf, hdr_len);
262         sg_set_buf(&sg[1], dbuf, data_len);
263
264         ret = virtqueue_add_outbuf(hw->out, sg, 2, hw, GFP_KERNEL);
265         if (ret) {
266                 dev_err(dev->dev, "failed to add outbuf\n");
267                 goto fail;
268         }
269
270         virtqueue_kick(hw->out);
271         return 0;
272 fail:
273
274         mei_virtio_free_outbufs(hw);
275
276         return ret;
277 }
278
279 /**
280  * mei_virtio_count_full_read_slots() - counts read full slots.
281  * @dev: the device structure
282  *
283  * Return: -EOVERFLOW if overflow, otherwise filled slots count
284  */
285 static int mei_virtio_count_full_read_slots(struct mei_device *dev)
286 {
287         struct mei_virtio_hw *hw = to_virtio_hw(dev);
288
289         if (hw->recv_idx > hw->recv_len)
290                 return -EOVERFLOW;
291
292         return hw->recv_len - hw->recv_idx;
293 }
294
295 /**
296  * mei_virtio_read_hdr() - Reads 32bit dword from mei virtio receive buffer
297  *
298  * @dev: the device structure
299  *
300  * Return: 32bit dword of receive buffer (u32)
301  */
302 static inline u32 mei_virtio_read_hdr(const struct mei_device *dev)
303 {
304         struct mei_virtio_hw *hw = to_virtio_hw(dev);
305
306         WARN_ON(hw->cfg.buf_depth < hw->recv_idx + 1);
307
308         return hw->recv_buf[hw->recv_idx++];
309 }
310
311 static int mei_virtio_read(struct mei_device *dev, unsigned char *buffer,
312                            unsigned long len)
313 {
314         struct mei_virtio_hw *hw = to_virtio_hw(dev);
315         u32 slots = mei_data2slots(len);
316
317         if (WARN_ON(hw->cfg.buf_depth < hw->recv_idx + slots))
318                 return -EOVERFLOW;
319
320         /*
321          * Assumption: There is only one MEI message in recv_buf each time.
322          * Backend service need follow this rule too.
323          */
324         memcpy(buffer, hw->recv_buf + hw->recv_idx, len);
325         hw->recv_idx += slots;
326
327         return 0;
328 }
329
330 static bool mei_virtio_pg_is_enabled(struct mei_device *dev)
331 {
332         return false;
333 }
334
335 static bool mei_virtio_pg_in_transition(struct mei_device *dev)
336 {
337         return false;
338 }
339
340 static void mei_virtio_add_recv_buf(struct mei_virtio_hw *hw)
341 {
342         struct scatterlist sg;
343
344         if (hw->recv_rdy) /* not needed */
345                 return;
346
347         /* refill the recv_buf to IN virtqueue to get next message */
348         sg_init_one(&sg, hw->recv_buf, mei_slots2data(hw->cfg.buf_depth));
349         hw->recv_len = 0;
350         hw->recv_idx = 0;
351         hw->recv_rdy = 1;
352         virtqueue_add_inbuf(hw->in, &sg, 1, hw->recv_buf, GFP_KERNEL);
353         virtqueue_kick(hw->in);
354 }
355
356 /**
357  * mei_virtio_hw_is_ready() - check whether the BE(hw) has turned ready
358  * @dev: mei device
359  * Return: bool
360  */
361 static bool mei_virtio_hw_is_ready(struct mei_device *dev)
362 {
363         struct mei_virtio_hw *hw = to_virtio_hw(dev);
364         struct virtio_device *vdev = dev_to_virtio(dev->dev);
365
366         virtio_cread(vdev, struct mei_virtio_cfg,
367                      hw_ready, &hw->cfg.hw_ready);
368
369         dev_dbg(dev->dev, "hw ready %d\n", hw->cfg.hw_ready);
370
371         return hw->cfg.hw_ready;
372 }
373
374 /**
375  * mei_virtio_hw_reset - resets virtio hw.
376  *
377  * @dev: the device structure
378  * @intr_enable: virtio use data/config callbacks
379  *
380  * Return: 0 on success an error code otherwise
381  */
382 static int mei_virtio_hw_reset(struct mei_device *dev, bool intr_enable)
383 {
384         struct mei_virtio_hw *hw = to_virtio_hw(dev);
385         struct virtio_device *vdev = dev_to_virtio(dev->dev);
386
387         dev_dbg(dev->dev, "hw reset\n");
388
389         dev->recvd_hw_ready = false;
390         hw->host_ready = false;
391         atomic_set(&hw->hbuf_ready, 0);
392         hw->recv_len = 0;
393         hw->recv_idx = 0;
394
395         hw->cfg.host_reset = 1;
396         virtio_cwrite(vdev, struct mei_virtio_cfg,
397                       host_reset, &hw->cfg.host_reset);
398
399         mei_virtio_hw_is_ready(dev);
400
401         if (intr_enable)
402                 mei_virtio_intr_enable(dev);
403
404         return 0;
405 }
406
407 /**
408  * mei_virtio_hw_reset_release() - release device from the reset
409  * @dev: the device structure
410  */
411 static void mei_virtio_hw_reset_release(struct mei_device *dev)
412 {
413         struct mei_virtio_hw *hw = to_virtio_hw(dev);
414         struct virtio_device *vdev = dev_to_virtio(dev->dev);
415
416         dev_dbg(dev->dev, "hw reset release\n");
417         hw->cfg.host_reset = 0;
418         virtio_cwrite(vdev, struct mei_virtio_cfg,
419                       host_reset, &hw->cfg.host_reset);
420 }
421
422 /**
423  * mei_virtio_hw_ready_wait() - wait until the virtio(hw) has turned ready
424  *  or timeout is reached
425  * @dev: mei device
426  *
427  * Return: 0 on success, error otherwise
428  */
429 static int mei_virtio_hw_ready_wait(struct mei_device *dev)
430 {
431         mutex_unlock(&dev->device_lock);
432         wait_event_timeout(dev->wait_hw_ready,
433                            dev->recvd_hw_ready,
434                            mei_secs_to_jiffies(MEI_HW_READY_TIMEOUT));
435         mutex_lock(&dev->device_lock);
436         if (!dev->recvd_hw_ready) {
437                 dev_err(dev->dev, "wait hw ready failed\n");
438                 return -ETIMEDOUT;
439         }
440
441         dev->recvd_hw_ready = false;
442         return 0;
443 }
444
445 /**
446  * mei_virtio_hw_start() - hw start routine
447  * @dev: mei device
448  *
449  * Return: 0 on success, error otherwise
450  */
451 static int mei_virtio_hw_start(struct mei_device *dev)
452 {
453         struct mei_virtio_hw *hw = to_virtio_hw(dev);
454         int ret;
455
456         dev_dbg(dev->dev, "hw start\n");
457         mei_virtio_hw_reset_release(dev);
458
459         ret = mei_virtio_hw_ready_wait(dev);
460         if (ret)
461                 return ret;
462
463         mei_virtio_add_recv_buf(hw);
464         atomic_set(&hw->hbuf_ready, 1);
465         dev_dbg(dev->dev, "hw is ready\n");
466         hw->host_ready = true;
467
468         return 0;
469 }
470
471 /**
472  * mei_virtio_host_is_ready() - check whether the FE has turned ready
473  * @dev: mei device
474  *
475  * Return: bool
476  */
477 static bool mei_virtio_host_is_ready(struct mei_device *dev)
478 {
479         struct mei_virtio_hw *hw = to_virtio_hw(dev);
480
481         dev_dbg(dev->dev, "host ready %d\n", hw->host_ready);
482
483         return hw->host_ready;
484 }
485
486 /**
487  * mei_virtio_data_in() - The callback of recv virtqueue of virtio mei
488  * @vq: receiving virtqueue
489  */
490 static void mei_virtio_data_in(struct virtqueue *vq)
491 {
492         struct mei_virtio_hw *hw = vq->vdev->priv;
493
494         /* disable interrupts (enabled again from in the interrupt worker) */
495         virtqueue_disable_cb(hw->in);
496
497         schedule_work(&hw->intr_handler);
498 }
499
500 /**
501  * mei_virtio_data_out() - The callback of send virtqueue of virtio mei
502  * @vq: transmitting virtqueue
503  */
504 static void mei_virtio_data_out(struct virtqueue *vq)
505 {
506         struct mei_virtio_hw *hw = vq->vdev->priv;
507
508         schedule_work(&hw->intr_handler);
509 }
510
511 static void mei_virtio_intr_handler(struct work_struct *work)
512 {
513         struct mei_virtio_hw *hw =
514                 container_of(work, struct mei_virtio_hw, intr_handler);
515         struct mei_device *dev = &hw->mdev;
516         LIST_HEAD(complete_list);
517         s32 slots;
518         int rets = 0;
519         void *data;
520         unsigned int len;
521
522         mutex_lock(&dev->device_lock);
523
524         if (dev->dev_state == MEI_DEV_DISABLED) {
525                 dev_warn(dev->dev, "Interrupt in disabled state.\n");
526                 mei_virtio_intr_disable(dev);
527                 goto end;
528         }
529
530         /* check if ME wants a reset */
531         if (!mei_hw_is_ready(dev) && dev->dev_state != MEI_DEV_RESETTING) {
532                 dev_warn(dev->dev, "BE service not ready: resetting.\n");
533                 schedule_work(&dev->reset_work);
534                 goto end;
535         }
536
537         /* check if we need to start the dev */
538         if (!mei_host_is_ready(dev)) {
539                 if (mei_hw_is_ready(dev)) {
540                         dev_dbg(dev->dev, "we need to start the dev.\n");
541                         dev->recvd_hw_ready = true;
542                         wake_up(&dev->wait_hw_ready);
543                 } else {
544                         dev_warn(dev->dev, "Spurious Interrupt\n");
545                 }
546                 goto end;
547         }
548
549         /* read */
550         if (hw->recv_rdy) {
551                 data = virtqueue_get_buf(hw->in, &len);
552                 if (!data || !len) {
553                         dev_dbg(dev->dev, "No data %d", len);
554                 } else {
555                         dev_dbg(dev->dev, "data_in %d\n", len);
556                         WARN_ON(data != hw->recv_buf);
557                         hw->recv_len = mei_data2slots(len);
558                         hw->recv_rdy = 0;
559                 }
560         }
561
562         /* write */
563         if (!atomic_read(&hw->hbuf_ready)) {
564                 if (!virtqueue_get_buf(hw->out, &len)) {
565                         dev_warn(dev->dev, "Failed to getbuf\n");
566                 } else {
567                         mei_virtio_free_outbufs(hw);
568                         atomic_inc(&hw->hbuf_ready);
569                 }
570         }
571
572         /* check slots available for reading */
573         slots = mei_count_full_read_slots(dev);
574         while (slots > 0) {
575                 dev_dbg(dev->dev, "slots to read = %08x\n", slots);
576                 rets = mei_irq_read_handler(dev, &complete_list, &slots);
577
578                 if (rets &&
579                     (dev->dev_state != MEI_DEV_RESETTING &&
580                      dev->dev_state != MEI_DEV_POWER_DOWN)) {
581                         dev_err(dev->dev, "mei_irq_read_handler ret = %d.\n",
582                                 rets);
583                         schedule_work(&dev->reset_work);
584                         goto end;
585                 }
586         }
587
588         dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
589
590         mei_irq_write_handler(dev, &complete_list);
591
592         dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
593
594         mei_irq_compl_handler(dev, &complete_list);
595
596         mei_virtio_add_recv_buf(hw);
597
598 end:
599         if (dev->dev_state != MEI_DEV_DISABLED) {
600                 if (!virtqueue_enable_cb(hw->in))
601                         schedule_work(&hw->intr_handler);
602         }
603
604         mutex_unlock(&dev->device_lock);
605 }
606
607 static void mei_virtio_config_changed(struct virtio_device *vdev)
608 {
609         struct mei_virtio_hw *hw = vdev->priv;
610         struct mei_device *dev = &hw->mdev;
611
612         virtio_cread(vdev, struct mei_virtio_cfg,
613                      hw_ready, &hw->cfg.hw_ready);
614
615         if (dev->dev_state == MEI_DEV_DISABLED) {
616                 dev_dbg(dev->dev, "disabled state don't start\n");
617                 return;
618         }
619
620         /* Run intr handler once to handle reset notify */
621         schedule_work(&hw->intr_handler);
622 }
623
624 static void mei_virtio_remove_vqs(struct virtio_device *vdev)
625 {
626         struct mei_virtio_hw *hw = vdev->priv;
627
628         virtqueue_detach_unused_buf(hw->in);
629         hw->recv_len = 0;
630         hw->recv_idx = 0;
631         hw->recv_rdy = 0;
632
633         virtqueue_detach_unused_buf(hw->out);
634
635         mei_virtio_free_outbufs(hw);
636
637         vdev->config->del_vqs(vdev);
638 }
639
640 /*
641  * There are two virtqueues, one is for send and another is for recv.
642  */
643 static int mei_virtio_init_vqs(struct mei_virtio_hw *hw,
644                                struct virtio_device *vdev)
645 {
646         struct virtqueue *vqs[2];
647
648         vq_callback_t *cbs[] = {
649                 mei_virtio_data_in,
650                 mei_virtio_data_out,
651         };
652         static const char * const names[] = {
653                 "in",
654                 "out",
655         };
656         int ret;
657
658         ret = virtio_find_vqs(vdev, 2, vqs, cbs, names, NULL);
659         if (ret)
660                 return ret;
661
662         hw->in = vqs[0];
663         hw->out = vqs[1];
664
665         return 0;
666 }
667
668 static const struct mei_hw_ops mei_virtio_ops = {
669         .fw_status = mei_virtio_fw_status,
670         .pg_state  = mei_virtio_pg_state,
671
672         .host_is_ready = mei_virtio_host_is_ready,
673
674         .hw_is_ready = mei_virtio_hw_is_ready,
675         .hw_reset = mei_virtio_hw_reset,
676         .hw_config = mei_virtio_hw_config,
677         .hw_start = mei_virtio_hw_start,
678
679         .pg_in_transition = mei_virtio_pg_in_transition,
680         .pg_is_enabled = mei_virtio_pg_is_enabled,
681
682         .intr_clear = mei_virtio_intr_clear,
683         .intr_enable = mei_virtio_intr_enable,
684         .intr_disable = mei_virtio_intr_disable,
685         .synchronize_irq = mei_virtio_synchronize_irq,
686
687         .hbuf_free_slots = mei_virtio_hbuf_empty_slots,
688         .hbuf_is_ready = mei_virtio_hbuf_is_ready,
689         .hbuf_depth = mei_virtio_hbuf_depth,
690
691         .write = mei_virtio_write_message,
692
693         .rdbuf_full_slots = mei_virtio_count_full_read_slots,
694         .read_hdr = mei_virtio_read_hdr,
695         .read = mei_virtio_read,
696 };
697
698 static int mei_virtio_probe(struct virtio_device *vdev)
699 {
700         struct mei_virtio_hw *hw;
701         int ret;
702
703         hw = devm_kzalloc(&vdev->dev, sizeof(*hw), GFP_KERNEL);
704         if (!hw)
705                 return -ENOMEM;
706
707         vdev->priv = hw;
708
709         INIT_WORK(&hw->intr_handler, mei_virtio_intr_handler);
710
711         ret = mei_virtio_init_vqs(hw, vdev);
712         if (ret)
713                 goto vqs_failed;
714
715         virtio_cread(vdev, struct mei_virtio_cfg,
716                      buf_depth, &hw->cfg.buf_depth);
717
718         hw->recv_buf = kzalloc(mei_slots2data(hw->cfg.buf_depth), GFP_KERNEL);
719         if (!hw->recv_buf) {
720                 ret = -ENOMEM;
721                 goto hbuf_failed;
722         }
723         atomic_set(&hw->hbuf_ready, 0);
724
725         virtio_device_ready(vdev);
726
727         mei_device_init(&hw->mdev, &vdev->dev, &mei_virtio_ops);
728
729         pm_runtime_get_noresume(&vdev->dev);
730         pm_runtime_set_active(&vdev->dev);
731         pm_runtime_enable(&vdev->dev);
732
733         ret = mei_start(&hw->mdev);
734         if (ret)
735                 goto mei_start_failed;
736
737         pm_runtime_set_autosuspend_delay(&vdev->dev, MEI_VIRTIO_RPM_TIMEOUT);
738         pm_runtime_use_autosuspend(&vdev->dev);
739
740         ret = mei_register(&hw->mdev, &vdev->dev);
741         if (ret)
742                 goto mei_failed;
743
744         pm_runtime_put(&vdev->dev);
745
746         return 0;
747
748 mei_failed:
749         mei_stop(&hw->mdev);
750 mei_start_failed:
751         mei_cancel_work(&hw->mdev);
752         mei_disable_interrupts(&hw->mdev);
753         kfree(hw->recv_buf);
754 hbuf_failed:
755         vdev->config->del_vqs(vdev);
756 vqs_failed:
757         return ret;
758 }
759
760 static int __maybe_unused mei_virtio_pm_runtime_idle(struct device *device)
761 {
762         struct virtio_device *vdev = dev_to_virtio(device);
763         struct mei_virtio_hw *hw = vdev->priv;
764
765         dev_dbg(&vdev->dev, "rpm: mei_virtio : runtime_idle\n");
766
767         if (!hw)
768                 return -ENODEV;
769
770         if (mei_write_is_idle(&hw->mdev))
771                 pm_runtime_autosuspend(device);
772
773         return -EBUSY;
774 }
775
776 static int __maybe_unused mei_virtio_pm_runtime_suspend(struct device *device)
777 {
778         return 0;
779 }
780
781 static int __maybe_unused mei_virtio_pm_runtime_resume(struct device *device)
782 {
783         return 0;
784 }
785
786 static int __maybe_unused mei_virtio_freeze(struct virtio_device *vdev)
787 {
788         struct mei_virtio_hw *hw = vdev->priv;
789
790         dev_dbg(&vdev->dev, "freeze\n");
791
792         if (!hw)
793                 return -ENODEV;
794
795         mei_stop(&hw->mdev);
796         mei_disable_interrupts(&hw->mdev);
797         cancel_work_sync(&hw->intr_handler);
798         vdev->config->reset(vdev);
799         mei_virtio_remove_vqs(vdev);
800
801         return 0;
802 }
803
804 static int __maybe_unused mei_virtio_restore(struct virtio_device *vdev)
805 {
806         struct mei_virtio_hw *hw = vdev->priv;
807         int ret;
808
809         dev_dbg(&vdev->dev, "restore\n");
810
811         if (!hw)
812                 return -ENODEV;
813
814         ret = mei_virtio_init_vqs(hw, vdev);
815         if (ret)
816                 return ret;
817
818         virtio_device_ready(vdev);
819
820         ret = mei_restart(&hw->mdev);
821         if (ret)
822                 return ret;
823
824         /* Start timer if stopped in suspend */
825         schedule_delayed_work(&hw->mdev.timer_work, HZ);
826
827         return 0;
828 }
829
830 static const struct dev_pm_ops mei_virtio_pm_ops = {
831         SET_RUNTIME_PM_OPS(mei_virtio_pm_runtime_suspend,
832                            mei_virtio_pm_runtime_resume,
833                            mei_virtio_pm_runtime_idle)
834 };
835
836 static void mei_virtio_remove(struct virtio_device *vdev)
837 {
838         struct mei_virtio_hw *hw = vdev->priv;
839
840         mei_stop(&hw->mdev);
841         mei_disable_interrupts(&hw->mdev);
842         cancel_work_sync(&hw->intr_handler);
843         mei_deregister(&hw->mdev);
844         vdev->config->reset(vdev);
845         mei_virtio_remove_vqs(vdev);
846         kfree(hw->recv_buf);
847         pm_runtime_disable(&vdev->dev);
848 }
849
850 static struct virtio_device_id id_table[] = {
851         { VIRTIO_ID_MEI, VIRTIO_DEV_ANY_ID },
852         { }
853 };
854
855 static struct virtio_driver mei_virtio_driver = {
856         .id_table = id_table,
857         .probe = mei_virtio_probe,
858         .remove = mei_virtio_remove,
859         .config_changed = mei_virtio_config_changed,
860         .driver = {
861                 .name = KBUILD_MODNAME,
862                 .owner = THIS_MODULE,
863                 .pm = &mei_virtio_pm_ops,
864         },
865 #ifdef CONFIG_PM_SLEEP
866         .freeze = mei_virtio_freeze,
867         .restore = mei_virtio_restore,
868 #endif
869 };
870
871 module_virtio_driver(mei_virtio_driver);
872 MODULE_DEVICE_TABLE(virtio, id_table);
873 MODULE_DESCRIPTION("Virtio MEI frontend driver");
874 MODULE_LICENSE("GPL v2");
This page took 0.082836 seconds and 4 git commands to generate.