1 /*
2  * Copyright (c) 2019 LK Trusty Authors. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <virtio-device.h>
25 #include <virtio-mmio.h>
26 #include <virtio.h>
27 
virtio_set_features(struct virtio_config * vio,uint64_t features)28 void virtio_set_features(struct virtio_config* vio, uint64_t features) {
29     struct virtio_mmio_config* vio_mmio = (struct virtio_mmio_config*)vio;
30 
31     io_write_32(&vio_mmio->guest_features_sel, 0);
32     io_write_32(&vio_mmio->guest_features, features & 0xFFFF);
33     io_write_32(&vio_mmio->guest_features_sel, 1);
34     io_write_32(&vio_mmio->guest_features, features >> 32);
35 }
36 
virtio_get_features(struct virtio_config * vio)37 uint64_t virtio_get_features(struct virtio_config* vio) {
38     struct virtio_mmio_config* vio_mmio = (struct virtio_mmio_config*)vio;
39     uint64_t features;
40 
41     io_write_32(&vio_mmio->host_features_sel, 1);
42     features = io_read_32(&vio_mmio->host_features);
43     features <<= 32;
44     io_write_32(&vio_mmio->host_features_sel, 0);
45     features |= io_read_32(&vio_mmio->host_features);
46     return features;
47 }
48 
vq_attach(struct virtq * vq,uint16_t idx)49 void vq_attach(struct virtq* vq, uint16_t idx) {
50     struct virtio_mmio_config* vio_mmio = (struct virtio_mmio_config*)vq->vio;
51 
52     vq->queue_id = idx;
53     io_write_32(&vio_mmio->queue_sel, idx);
54     io_write_32(&vio_mmio->queue_num, vq->num_bufs);
55     io_write_32(&vio_mmio->queue_align, PAGE_SIZE);
56     io_write_32(&vio_mmio->queue_pfn, (uintptr_t)vq->raw >> PAGE_SHIFT);
57 }
58 
vq_kick(struct virtq * vq)59 void vq_kick(struct virtq* vq) {
60     struct virtio_mmio_config* vio_mmio = (struct virtio_mmio_config*)vq->vio;
61 
62     io_write_32(&vio_mmio->queue_notify, vq->queue_id);
63 }
64 
virtio_or_status(struct virtio_config * vio,uint32_t flags)65 void virtio_or_status(struct virtio_config* vio, uint32_t flags) {
66     struct virtio_mmio_config* vio_mmio = (struct virtio_mmio_config*)vio;
67     uint32_t old_status = io_read_32(&vio_mmio->status);
68 
69     io_write_32(&vio_mmio->status, old_status | flags);
70 }
71 
virtio_get_status(struct virtio_config * vio)72 uint32_t virtio_get_status(struct virtio_config* vio) {
73     struct virtio_mmio_config* vio_mmio = (struct virtio_mmio_config*)vio;
74 
75     return io_read_32(&vio_mmio->status);
76 }
77 
virtio_reset_device(struct virtio_config * vio)78 void virtio_reset_device(struct virtio_config* vio) {
79     struct virtio_mmio_config* vio_mmio = (struct virtio_mmio_config*)vio;
80 
81     io_write_32(&vio_mmio->status, 0);
82 }
83 
virtio_set_guest_page_size(struct virtio_config * vio,uint32_t size)84 void virtio_set_guest_page_size(struct virtio_config* vio, uint32_t size) {
85     struct virtio_mmio_config* vio_mmio = (struct virtio_mmio_config*)vio;
86 
87     io_write_32(&vio_mmio->guest_page_size, size);
88 }
89 
virtio_probe_console(void)90 struct virtio_config* virtio_probe_console(void) {
91     struct virtio_mmio_config* cfg;
92 
93     for (cfg = (struct virtio_mmio_config*)VIRTIO_MMIO_BASE;
94          cfg->magic == VIRTIO_MMIO_MAGIC; cfg++) {
95         if (cfg->device_id == VIRTIO_DEVICE_ID_CONSOLE) {
96             return (struct virtio_config*)cfg;
97         }
98     }
99     return NULL;
100 }
101