1 #include "kvm/8250-serial.h" 2 3 #include "kvm/read-write.h" 4 #include "kvm/ioport.h" 5 #include "kvm/mutex.h" 6 #include "kvm/util.h" 7 #include "kvm/term.h" 8 #include "kvm/kvm.h" 9 #include "kvm/fdt.h" 10 11 #include <linux/types.h> 12 #include <linux/serial_reg.h> 13 14 #include <pthread.h> 15 16 /* 17 * This fakes a U6_16550A. The fifo len needs to be 64 as the kernel 18 * expects that for autodetection. 19 */ 20 #define FIFO_LEN 64 21 #define FIFO_MASK (FIFO_LEN - 1) 22 23 #define UART_IIR_TYPE_BITS 0xc0 24 25 struct serial8250_device { 26 struct mutex mutex; 27 u8 id; 28 29 u16 iobase; 30 u8 irq; 31 u8 irq_state; 32 int txcnt; 33 int rxcnt; 34 int rxdone; 35 char txbuf[FIFO_LEN]; 36 char rxbuf[FIFO_LEN]; 37 38 u8 dll; 39 u8 dlm; 40 u8 iir; 41 u8 ier; 42 u8 fcr; 43 u8 lcr; 44 u8 mcr; 45 u8 lsr; 46 u8 msr; 47 u8 scr; 48 }; 49 50 #define SERIAL_REGS_SETTING \ 51 .iir = UART_IIR_NO_INT, \ 52 .lsr = UART_LSR_TEMT | UART_LSR_THRE, \ 53 .msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS, \ 54 .mcr = UART_MCR_OUT2, 55 56 static struct serial8250_device devices[] = { 57 /* ttyS0 */ 58 [0] = { 59 .mutex = MUTEX_INITIALIZER, 60 61 .id = 0, 62 .iobase = 0x3f8, 63 .irq = 4, 64 65 SERIAL_REGS_SETTING 66 }, 67 /* ttyS1 */ 68 [1] = { 69 .mutex = MUTEX_INITIALIZER, 70 71 .id = 1, 72 .iobase = 0x2f8, 73 .irq = 3, 74 75 SERIAL_REGS_SETTING 76 }, 77 /* ttyS2 */ 78 [2] = { 79 .mutex = MUTEX_INITIALIZER, 80 81 .id = 2, 82 .iobase = 0x3e8, 83 .irq = 4, 84 85 SERIAL_REGS_SETTING 86 }, 87 /* ttyS3 */ 88 [3] = { 89 .mutex = MUTEX_INITIALIZER, 90 91 .id = 3, 92 .iobase = 0x2e8, 93 .irq = 3, 94 95 SERIAL_REGS_SETTING 96 }, 97 }; 98 99 static void serial8250_flush_tx(struct kvm *kvm, struct serial8250_device *dev) 100 { 101 dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE; 102 103 if (dev->txcnt) { 104 term_putc(dev->txbuf, dev->txcnt, dev->id); 105 dev->txcnt = 0; 106 } 107 } 108 109 static void serial8250_update_irq(struct kvm *kvm, struct serial8250_device *dev) 110 { 111 u8 iir = 0; 112 113 /* Handle clear rx */ 114 if (dev->lcr & UART_FCR_CLEAR_RCVR) { 115 dev->lcr &= ~UART_FCR_CLEAR_RCVR; 116 dev->rxcnt = dev->rxdone = 0; 117 dev->lsr &= ~UART_LSR_DR; 118 } 119 120 /* Handle clear tx */ 121 if (dev->lcr & UART_FCR_CLEAR_XMIT) { 122 dev->lcr &= ~UART_FCR_CLEAR_XMIT; 123 dev->txcnt = 0; 124 dev->lsr |= UART_LSR_TEMT | UART_LSR_THRE; 125 } 126 127 /* Data ready and rcv interrupt enabled ? */ 128 if ((dev->ier & UART_IER_RDI) && (dev->lsr & UART_LSR_DR)) 129 iir |= UART_IIR_RDI; 130 131 /* Transmitter empty and interrupt enabled ? */ 132 if ((dev->ier & UART_IER_THRI) && (dev->lsr & UART_LSR_TEMT)) 133 iir |= UART_IIR_THRI; 134 135 /* Now update the irq line, if necessary */ 136 if (!iir) { 137 dev->iir = UART_IIR_NO_INT; 138 if (dev->irq_state) 139 kvm__irq_line(kvm, dev->irq, 0); 140 } else { 141 dev->iir = iir; 142 if (!dev->irq_state) 143 kvm__irq_line(kvm, dev->irq, 1); 144 } 145 dev->irq_state = iir; 146 147 /* 148 * If the kernel disabled the tx interrupt, we know that there 149 * is nothing more to transmit, so we can reset our tx logic 150 * here. 151 */ 152 if (!(dev->ier & UART_IER_THRI)) 153 serial8250_flush_tx(kvm, dev); 154 } 155 156 #define SYSRQ_PENDING_NONE 0 157 158 static int sysrq_pending; 159 160 static void serial8250__sysrq(struct kvm *kvm, struct serial8250_device *dev) 161 { 162 dev->lsr |= UART_LSR_DR | UART_LSR_BI; 163 dev->rxbuf[dev->rxcnt++] = sysrq_pending; 164 sysrq_pending = SYSRQ_PENDING_NONE; 165 } 166 167 static void serial8250__receive(struct kvm *kvm, struct serial8250_device *dev, 168 bool handle_sysrq) 169 { 170 int c; 171 172 /* 173 * If the guest transmitted a full fifo, we clear the 174 * TEMT/THRE bits to let the kernel escape from the 8250 175 * interrupt handler. We come here only once a ms, so that 176 * should give the kernel the desired pause. That also flushes 177 * the tx fifo to the terminal. 178 */ 179 serial8250_flush_tx(kvm, dev); 180 181 if (dev->mcr & UART_MCR_LOOP) 182 return; 183 184 if ((dev->lsr & UART_LSR_DR) || dev->rxcnt) 185 return; 186 187 if (handle_sysrq && sysrq_pending) { 188 serial8250__sysrq(kvm, dev); 189 return; 190 } 191 192 if (kvm->cfg.active_console != CONSOLE_8250) 193 return; 194 195 while (term_readable(dev->id) && 196 dev->rxcnt < FIFO_LEN) { 197 198 c = term_getc(kvm, dev->id); 199 200 if (c < 0) 201 break; 202 dev->rxbuf[dev->rxcnt++] = c; 203 dev->lsr |= UART_LSR_DR; 204 } 205 } 206 207 void serial8250__update_consoles(struct kvm *kvm) 208 { 209 unsigned int i; 210 211 for (i = 0; i < ARRAY_SIZE(devices); i++) { 212 struct serial8250_device *dev = &devices[i]; 213 214 mutex_lock(&dev->mutex); 215 216 /* Restrict sysrq injection to the first port */ 217 serial8250__receive(kvm, dev, i == 0); 218 219 serial8250_update_irq(kvm, dev); 220 221 mutex_unlock(&dev->mutex); 222 } 223 } 224 225 void serial8250__inject_sysrq(struct kvm *kvm, char sysrq) 226 { 227 sysrq_pending = sysrq; 228 } 229 230 static bool serial8250_out(struct ioport *ioport, struct kvm *kvm, u16 port, 231 void *data, int size) 232 { 233 struct serial8250_device *dev = ioport->priv; 234 u16 offset; 235 bool ret = true; 236 char *addr = data; 237 238 mutex_lock(&dev->mutex); 239 240 offset = port - dev->iobase; 241 242 switch (offset) { 243 case UART_TX: 244 if (dev->lcr & UART_LCR_DLAB) { 245 dev->dll = ioport__read8(data); 246 break; 247 } 248 249 /* Loopback mode */ 250 if (dev->mcr & UART_MCR_LOOP) { 251 if (dev->rxcnt < FIFO_LEN) { 252 dev->rxbuf[dev->rxcnt++] = *addr; 253 dev->lsr |= UART_LSR_DR; 254 } 255 break; 256 } 257 258 if (dev->txcnt < FIFO_LEN) { 259 dev->txbuf[dev->txcnt++] = *addr; 260 dev->lsr &= ~UART_LSR_TEMT; 261 if (dev->txcnt == FIFO_LEN / 2) 262 dev->lsr &= ~UART_LSR_THRE; 263 } else { 264 /* Should never happpen */ 265 dev->lsr &= ~(UART_LSR_TEMT | UART_LSR_THRE); 266 } 267 break; 268 case UART_IER: 269 if (!(dev->lcr & UART_LCR_DLAB)) 270 dev->ier = ioport__read8(data) & 0x0f; 271 else 272 dev->dlm = ioport__read8(data); 273 break; 274 case UART_FCR: 275 dev->fcr = ioport__read8(data); 276 break; 277 case UART_LCR: 278 dev->lcr = ioport__read8(data); 279 break; 280 case UART_MCR: 281 dev->mcr = ioport__read8(data); 282 break; 283 case UART_LSR: 284 /* Factory test */ 285 break; 286 case UART_MSR: 287 /* Not used */ 288 break; 289 case UART_SCR: 290 dev->scr = ioport__read8(data); 291 break; 292 default: 293 ret = false; 294 break; 295 } 296 297 serial8250_update_irq(kvm, dev); 298 299 mutex_unlock(&dev->mutex); 300 301 return ret; 302 } 303 304 static void serial8250_rx(struct serial8250_device *dev, void *data) 305 { 306 if (dev->rxdone == dev->rxcnt) 307 return; 308 309 /* Break issued ? */ 310 if (dev->lsr & UART_LSR_BI) { 311 dev->lsr &= ~UART_LSR_BI; 312 ioport__write8(data, 0); 313 return; 314 } 315 316 ioport__write8(data, dev->rxbuf[dev->rxdone++]); 317 if (dev->rxcnt == dev->rxdone) { 318 dev->lsr &= ~UART_LSR_DR; 319 dev->rxcnt = dev->rxdone = 0; 320 } 321 } 322 323 static bool serial8250_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size) 324 { 325 struct serial8250_device *dev = ioport->priv; 326 u16 offset; 327 bool ret = true; 328 329 mutex_lock(&dev->mutex); 330 331 offset = port - dev->iobase; 332 333 switch (offset) { 334 case UART_RX: 335 if (dev->lcr & UART_LCR_DLAB) 336 ioport__write8(data, dev->dll); 337 else 338 serial8250_rx(dev, data); 339 break; 340 case UART_IER: 341 if (dev->lcr & UART_LCR_DLAB) 342 ioport__write8(data, dev->dlm); 343 else 344 ioport__write8(data, dev->ier); 345 break; 346 case UART_IIR: 347 ioport__write8(data, dev->iir | UART_IIR_TYPE_BITS); 348 break; 349 case UART_LCR: 350 ioport__write8(data, dev->lcr); 351 break; 352 case UART_MCR: 353 ioport__write8(data, dev->mcr); 354 break; 355 case UART_LSR: 356 ioport__write8(data, dev->lsr); 357 break; 358 case UART_MSR: 359 ioport__write8(data, dev->msr); 360 break; 361 case UART_SCR: 362 ioport__write8(data, dev->scr); 363 break; 364 default: 365 ret = false; 366 break; 367 } 368 369 serial8250_update_irq(kvm, dev); 370 371 mutex_unlock(&dev->mutex); 372 373 return ret; 374 } 375 376 #ifdef CONFIG_HAS_LIBFDT 377 #define DEVICE_NAME_MAX_LEN 32 378 static void serial8250_generate_fdt_node(struct ioport *ioport, void *fdt, 379 void (*generate_irq_prop)(void *fdt, 380 u8 irq)) 381 { 382 char dev_name[DEVICE_NAME_MAX_LEN]; 383 struct serial8250_device *dev = ioport->priv; 384 u64 addr = KVM_IOPORT_AREA + dev->iobase; 385 u64 reg_prop[] = { 386 cpu_to_fdt64(addr), 387 cpu_to_fdt64(8), 388 }; 389 390 snprintf(dev_name, DEVICE_NAME_MAX_LEN, "U6_16550A@%llx", addr); 391 392 _FDT(fdt_begin_node(fdt, dev_name)); 393 _FDT(fdt_property_string(fdt, "compatible", "ns16550a")); 394 _FDT(fdt_property(fdt, "reg", reg_prop, sizeof(reg_prop))); 395 generate_irq_prop(fdt, dev->irq); 396 _FDT(fdt_property_cell(fdt, "clock-frequency", 1843200)); 397 _FDT(fdt_end_node(fdt)); 398 } 399 #else 400 #define serial8250_generate_fdt_node NULL 401 #endif 402 403 static struct ioport_operations serial8250_ops = { 404 .io_in = serial8250_in, 405 .io_out = serial8250_out, 406 .generate_fdt_node = serial8250_generate_fdt_node, 407 }; 408 409 static int serial8250__device_init(struct kvm *kvm, struct serial8250_device *dev) 410 { 411 int r; 412 413 ioport__map_irq(&dev->irq); 414 r = ioport__register(kvm, dev->iobase, &serial8250_ops, 8, dev); 415 kvm__irq_line(kvm, dev->irq, 0); 416 417 return r; 418 } 419 420 int serial8250__init(struct kvm *kvm) 421 { 422 unsigned int i, j; 423 int r = 0; 424 425 for (i = 0; i < ARRAY_SIZE(devices); i++) { 426 struct serial8250_device *dev = &devices[i]; 427 428 r = serial8250__device_init(kvm, dev); 429 if (r < 0) 430 goto cleanup; 431 } 432 433 return r; 434 cleanup: 435 for (j = 0; j <= i; j++) { 436 struct serial8250_device *dev = &devices[j]; 437 438 ioport__unregister(kvm, dev->iobase); 439 } 440 441 return r; 442 } 443 dev_init(serial8250__init); 444 445 int serial8250__exit(struct kvm *kvm) 446 { 447 unsigned int i; 448 int r; 449 450 for (i = 0; i < ARRAY_SIZE(devices); i++) { 451 struct serial8250_device *dev = &devices[i]; 452 453 r = ioport__unregister(kvm, dev->iobase); 454 if (r < 0) 455 return r; 456 } 457 458 return 0; 459 } 460 dev_exit(serial8250__exit); 461