1 /*-
2 * Copyright (c) 2012 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29 #include <stdio.h>
30 #include <sys/errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <sys/user.h>
35
36 #include "pci_core.h"
37 #include "uart_core.h"
38 #include "dm_string.h"
39 #include "vmmapi.h"
40
41 /*
42 * Pick a PCI vid/did of a chip with a single uart at
43 * BAR0, that most versions of FreeBSD can understand:
44 * Siig CyberSerial 1-port.
45 */
46 #define COM_VENDOR 0x9710
47 #define COM_DEV 0x9900
48
49 #define VUART_IDX "vuart_idx"
50
51 static void
pci_uart_intr_assert(void * arg)52 pci_uart_intr_assert(void *arg)
53 {
54 struct pci_vdev *dev = arg;
55
56 pci_lintr_assert(dev);
57 }
58
59 static void
pci_uart_intr_deassert(void * arg)60 pci_uart_intr_deassert(void *arg)
61 {
62 struct pci_vdev *dev = arg;
63
64 pci_lintr_deassert(dev);
65 }
66
67 static void
pci_uart_write(struct vmctx * ctx,int vcpu,struct pci_vdev * dev,int baridx,uint64_t offset,int size,uint64_t value)68 pci_uart_write(struct vmctx *ctx, int vcpu, struct pci_vdev *dev,
69 int baridx, uint64_t offset, int size, uint64_t value)
70 {
71 if (baridx == 0 && size == 1)
72 uart_write(dev->arg, offset, value);
73 }
74
75 uint64_t
pci_uart_read(struct vmctx * ctx,int vcpu,struct pci_vdev * dev,int baridx,uint64_t offset,int size)76 pci_uart_read(struct vmctx *ctx, int vcpu, struct pci_vdev *dev,
77 int baridx, uint64_t offset, int size)
78 {
79 uint8_t val = 0xff;
80
81 if (baridx == 0 && size == 1)
82 val = uart_read(dev->arg, offset);
83 return val;
84 }
85
86 static int
pci_uart_init(struct vmctx * ctx,struct pci_vdev * dev,char * opts)87 pci_uart_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
88 {
89 char *tmp, *val = NULL;
90 bool is_hv_land = false;
91 uint32_t vuart_idx;
92 struct acrn_vdev vdev = {};
93 int32_t err = 0;
94
95 if (opts != NULL) {
96 tmp = val= strdup(opts);
97 if (!tmp) {
98 pr_err("No memory for strdup, can't parse uart args!!!!\n");
99 } else {
100 if (!strncmp(tmp, VUART_IDX, strlen(VUART_IDX))) {
101 tmp = strsep(&val, ":");
102 if (!val) {
103 pr_err("pci vuart miss vuart_idx value!!!!\n");
104 } else {
105 if (dm_strtoui(val, &val, 10, &vuart_idx)) {
106 pr_err("wrong vuart_idx value!!!!\n");
107 } else {
108 is_hv_land = true;
109 }
110 }
111 }
112 }
113 }
114
115 if (is_hv_land) {
116 pci_emul_alloc_bar(dev, 0, PCIBAR_MEM32, 256);
117 pci_emul_alloc_bar(dev, 1, PCIBAR_MEM32, PAGE_SIZE);
118 dev->arg = NULL;
119 vdev.id.fields.vendor = COM_VENDOR;
120 vdev.id.fields.device = COM_DEV;
121 vdev.slot = PCI_BDF(dev->bus, dev->slot, dev->func);
122 vdev.io_addr[0] = pci_get_cfgdata32(dev, PCIR_BAR(0));
123 vdev.io_addr[1] = pci_get_cfgdata32(dev, PCIR_BAR(1));
124 *((uint32_t *)vdev.args) = vuart_idx;
125 err = vm_add_hv_vdev(ctx, &vdev);
126 if (err) {
127 pr_err("HV can't create vuart with vuart_idx=%d\n", vuart_idx);
128 }
129 } else {
130 pci_emul_alloc_bar(dev, 0, PCIBAR_IO, UART_IO_BAR_SIZE);
131 pci_lintr_request(dev);
132
133 /* initialize config space */
134 pci_set_cfgdata16(dev, PCIR_DEVICE, COM_DEV);
135 pci_set_cfgdata16(dev, PCIR_VENDOR, COM_VENDOR);
136 pci_set_cfgdata8(dev, PCIR_CLASS, PCIC_SIMPLECOMM);
137
138 dev->arg = uart_set_backend(pci_uart_intr_assert, pci_uart_intr_deassert, dev, opts);
139 if (dev->arg == NULL) {
140 pr_err("Unable to initialize backend '%s' for "
141 "pci uart at %d:%d\n", opts, dev->slot, dev->func);
142 err = -1;
143 }
144 }
145
146 return err;
147 }
148
149 static void
pci_uart_deinit(struct vmctx * ctx,struct pci_vdev * dev,char * opts)150 pci_uart_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
151 {
152 struct uart_vdev *uart = (struct uart_vdev *)dev->arg;
153 struct acrn_vdev emul_dev = {};
154
155 if (uart == NULL) {
156 emul_dev.id.fields.vendor = COM_VENDOR;
157 emul_dev.id.fields.device = COM_DEV;
158 emul_dev.slot = PCI_BDF(dev->bus, dev->slot, dev->func);
159 vm_remove_hv_vdev(ctx, &emul_dev);
160 return;
161 }
162
163 uart_release_backend(uart, opts);
164 }
165
166 struct pci_vdev_ops pci_ops_com = {
167 .class_name = "uart",
168 .vdev_init = pci_uart_init,
169 .vdev_deinit = pci_uart_deinit,
170 .vdev_barwrite = pci_uart_write,
171 .vdev_barread = pci_uart_read
172 };
173 DEFINE_PCI_DEVTYPE(pci_ops_com);
174