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