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