~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to hw/char/serial.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
92
92
#define UART_FCR_RFR        0x02    /* RCVR Fifo Reset */
93
93
#define UART_FCR_FE         0x01    /* FIFO Enable */
94
94
 
95
 
#define XMIT_FIFO           0
96
 
#define RECV_FIFO           1
97
95
#define MAX_XMIT_RETRY      4
98
96
 
99
97
#ifdef DEBUG_SERIAL
106
104
 
107
105
static void serial_receive1(void *opaque, const uint8_t *buf, int size);
108
106
 
109
 
static void fifo_clear(SerialState *s, int fifo)
110
 
{
111
 
    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
112
 
    memset(f->data, 0, UART_FIFO_LENGTH);
113
 
    f->count = 0;
114
 
    f->head = 0;
115
 
    f->tail = 0;
116
 
}
117
 
 
118
 
static int fifo_put(SerialState *s, int fifo, uint8_t chr)
119
 
{
120
 
    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
121
 
 
 
107
static inline void recv_fifo_put(SerialState *s, uint8_t chr)
 
108
{
122
109
    /* Receive overruns do not overwrite FIFO contents. */
123
 
    if (fifo == XMIT_FIFO || f->count < UART_FIFO_LENGTH) {
124
 
 
125
 
        f->data[f->head++] = chr;
126
 
 
127
 
        if (f->head == UART_FIFO_LENGTH)
128
 
            f->head = 0;
129
 
    }
130
 
 
131
 
    if (f->count < UART_FIFO_LENGTH)
132
 
        f->count++;
133
 
    else if (fifo == RECV_FIFO)
 
110
    if (!fifo8_is_full(&s->recv_fifo)) {
 
111
        fifo8_push(&s->recv_fifo, chr);
 
112
    } else {
134
113
        s->lsr |= UART_LSR_OE;
135
 
 
136
 
    return 1;
137
 
}
138
 
 
139
 
static uint8_t fifo_get(SerialState *s, int fifo)
140
 
{
141
 
    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
142
 
    uint8_t c;
143
 
 
144
 
    if(f->count == 0)
145
 
        return 0;
146
 
 
147
 
    c = f->data[f->tail++];
148
 
    if (f->tail == UART_FIFO_LENGTH)
149
 
        f->tail = 0;
150
 
    f->count--;
151
 
 
152
 
    return c;
 
114
    }
153
115
}
154
116
 
155
117
static void serial_update_irq(SerialState *s)
165
127
        tmp_iir = UART_IIR_CTI;
166
128
    } else if ((s->ier & UART_IER_RDI) && (s->lsr & UART_LSR_DR) &&
167
129
               (!(s->fcr & UART_FCR_FE) ||
168
 
                s->recv_fifo.count >= s->recv_fifo.itl)) {
 
130
                s->recv_fifo.num >= s->recv_fifo_itl)) {
169
131
        tmp_iir = UART_IIR_RDI;
170
132
    } else if ((s->ier & UART_IER_THRI) && s->thr_ipending) {
171
133
        tmp_iir = UART_IIR_THRI;
262
224
 
263
225
    if (s->tsr_retry <= 0) {
264
226
        if (s->fcr & UART_FCR_FE) {
265
 
            s->tsr = fifo_get(s,XMIT_FIFO);
266
 
            if (!s->xmit_fifo.count)
 
227
            s->tsr = fifo8_is_full(&s->xmit_fifo) ?
 
228
                        0 : fifo8_pop(&s->xmit_fifo);
 
229
            if (!s->xmit_fifo.num) {
267
230
                s->lsr |= UART_LSR_THRE;
 
231
            }
268
232
        } else if ((s->lsr & UART_LSR_THRE)) {
269
233
            return FALSE;
270
234
        } else {
316
280
        } else {
317
281
            s->thr = (uint8_t) val;
318
282
            if(s->fcr & UART_FCR_FE) {
319
 
                fifo_put(s, XMIT_FIFO, s->thr);
320
 
                s->thr_ipending = 0;
 
283
                /* xmit overruns overwrite data, so make space if needed */
 
284
                if (fifo8_is_full(&s->xmit_fifo)) {
 
285
                    fifo8_pop(&s->xmit_fifo);
 
286
                }
 
287
                fifo8_push(&s->xmit_fifo, s->thr);
321
288
                s->lsr &= ~UART_LSR_TEMT;
322
 
                s->lsr &= ~UART_LSR_THRE;
323
 
                serial_update_irq(s);
324
 
            } else {
325
 
                s->thr_ipending = 0;
326
 
                s->lsr &= ~UART_LSR_THRE;
327
 
                serial_update_irq(s);
328
289
            }
 
290
            s->thr_ipending = 0;
 
291
            s->lsr &= ~UART_LSR_THRE;
 
292
            serial_update_irq(s);
329
293
            serial_xmit(NULL, G_IO_OUT, s);
330
294
        }
331
295
        break;
367
331
        if (val & UART_FCR_RFR) {
368
332
            qemu_del_timer(s->fifo_timeout_timer);
369
333
            s->timeout_ipending=0;
370
 
            fifo_clear(s,RECV_FIFO);
 
334
            fifo8_reset(&s->recv_fifo);
371
335
            if ((s->lsr & UART_LSR_DR)) {
372
336
                s->lsr &= ~(UART_LSR_DR | UART_LSR_BI | UART_LSR_OE);
373
337
                if (!(s->mcr & UART_MCR_LOOP)) {
377
341
        }
378
342
 
379
343
        if (val & UART_FCR_XFR) {
380
 
            fifo_clear(s,XMIT_FIFO);
 
344
            fifo8_reset(&s->xmit_fifo);
381
345
            s->lsr |= UART_LSR_THRE;
382
346
        }
383
347
 
384
348
        if (val & UART_FCR_FE) {
385
349
            s->iir |= UART_IIR_FE;
386
 
            /* Set RECV_FIFO trigger Level */
 
350
            /* Set recv_fifo trigger Level */
387
351
            switch (val & 0xC0) {
388
352
            case UART_FCR_ITL_1:
389
 
                s->recv_fifo.itl = 1;
 
353
                s->recv_fifo_itl = 1;
390
354
                break;
391
355
            case UART_FCR_ITL_2:
392
 
                s->recv_fifo.itl = 4;
 
356
                s->recv_fifo_itl = 4;
393
357
                break;
394
358
            case UART_FCR_ITL_3:
395
 
                s->recv_fifo.itl = 8;
 
359
                s->recv_fifo_itl = 8;
396
360
                break;
397
361
            case UART_FCR_ITL_4:
398
 
                s->recv_fifo.itl = 14;
 
362
                s->recv_fifo_itl = 14;
399
363
                break;
400
364
            }
401
365
        } else
467
431
            ret = s->divider & 0xff;
468
432
        } else {
469
433
            if(s->fcr & UART_FCR_FE) {
470
 
                ret = fifo_get(s,RECV_FIFO);
471
 
                if (s->recv_fifo.count == 0)
 
434
                ret = fifo8_is_empty(&s->recv_fifo) ?
 
435
                            0 : fifo8_pop(&s->recv_fifo);
 
436
                if (s->recv_fifo.num == 0) {
472
437
                    s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
473
 
                else
 
438
                } else {
474
439
                    qemu_mod_timer(s->fifo_timeout_timer, qemu_get_clock_ns (vm_clock) + s->char_transmit_time * 4);
 
440
                }
475
441
                s->timeout_ipending = 0;
476
442
            } else {
477
443
                ret = s->rbr;
541
507
static int serial_can_receive(SerialState *s)
542
508
{
543
509
    if(s->fcr & UART_FCR_FE) {
544
 
        if(s->recv_fifo.count < UART_FIFO_LENGTH)
545
 
        /* Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1 if above. If UART_FIFO_LENGTH - fifo.count is
546
 
        advertised the effect will be to almost always fill the fifo completely before the guest has a chance to respond,
547
 
        effectively overriding the ITL that the guest has set. */
548
 
             return (s->recv_fifo.count <= s->recv_fifo.itl) ? s->recv_fifo.itl - s->recv_fifo.count : 1;
549
 
        else
550
 
             return 0;
 
510
        if (s->recv_fifo.num < UART_FIFO_LENGTH) {
 
511
            /*
 
512
             * Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1
 
513
             * if above. If UART_FIFO_LENGTH - fifo.count is advertised the
 
514
             * effect will be to almost always fill the fifo completely before
 
515
             * the guest has a chance to respond, effectively overriding the ITL
 
516
             * that the guest has set.
 
517
             */
 
518
            return (s->recv_fifo.num <= s->recv_fifo_itl) ?
 
519
                        s->recv_fifo_itl - s->recv_fifo.num : 1;
 
520
        } else {
 
521
            return 0;
 
522
        }
551
523
    } else {
552
 
    return !(s->lsr & UART_LSR_DR);
 
524
        return !(s->lsr & UART_LSR_DR);
553
525
    }
554
526
}
555
527
 
557
529
{
558
530
    s->rbr = 0;
559
531
    /* When the LSR_DR is set a null byte is pushed into the fifo */
560
 
    fifo_put(s, RECV_FIFO, '\0');
 
532
    recv_fifo_put(s, '\0');
561
533
    s->lsr |= UART_LSR_BI | UART_LSR_DR;
562
534
    serial_update_irq(s);
563
535
}
565
537
/* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
566
538
static void fifo_timeout_int (void *opaque) {
567
539
    SerialState *s = opaque;
568
 
    if (s->recv_fifo.count) {
 
540
    if (s->recv_fifo.num) {
569
541
        s->timeout_ipending = 1;
570
542
        serial_update_irq(s);
571
543
    }
587
559
    if(s->fcr & UART_FCR_FE) {
588
560
        int i;
589
561
        for (i = 0; i < size; i++) {
590
 
            fifo_put(s, RECV_FIFO, buf[i]);
 
562
            recv_fifo_put(s, buf[i]);
591
563
        }
592
564
        s->lsr |= UART_LSR_DR;
593
565
        /* call the timeout receive callback in 4 char transmit time */
667
639
    s->char_transmit_time = (get_ticks_per_sec() / 9600) * 10;
668
640
    s->poll_msl = 0;
669
641
 
670
 
    fifo_clear(s,RECV_FIFO);
671
 
    fifo_clear(s,XMIT_FIFO);
 
642
    fifo8_reset(&s->recv_fifo);
 
643
    fifo8_reset(&s->xmit_fifo);
672
644
 
673
645
    s->last_xmit_ts = qemu_get_clock_ns(vm_clock);
674
646
 
677
649
    qemu_irq_lower(s->irq);
678
650
}
679
651
 
680
 
void serial_init_core(SerialState *s)
 
652
void serial_realize_core(SerialState *s, Error **errp)
681
653
{
682
654
    if (!s->chr) {
683
 
        fprintf(stderr, "Can't create serial device, empty char device\n");
684
 
        exit(1);
 
655
        error_setg(errp, "Can't create serial device, empty char device");
 
656
        return;
685
657
    }
686
658
 
687
659
    s->modem_status_poll = qemu_new_timer_ns(vm_clock, (QEMUTimerCB *) serial_update_msl, s);
691
663
 
692
664
    qemu_chr_add_handlers(s->chr, serial_can_receive1, serial_receive1,
693
665
                          serial_event, s);
 
666
    fifo8_create(&s->recv_fifo, UART_FIFO_LENGTH);
 
667
    fifo8_create(&s->xmit_fifo, UART_FIFO_LENGTH);
694
668
}
695
669
 
696
670
void serial_exit_core(SerialState *s)
702
676
/* Get number of stored bytes in receive fifo. */
703
677
unsigned serial_rx_fifo_count(SerialState *s)
704
678
{
705
 
    return s->recv_fifo.count;
 
679
    return fifo8_num(&s->recv_fifo);
706
680
}
707
681
 
708
682
/* Get number of stored bytes in transmit fifo. */
709
683
unsigned serial_tx_fifo_count(SerialState *s)
710
684
{
711
 
    return s->xmit_fifo.count;
 
685
    return fifo8_num(&s->xmit_fifo);
712
686
}
713
687
 
714
688
/* Change the main reference oscillator frequency. */
732
706
                         CharDriverState *chr, MemoryRegion *system_io)
733
707
{
734
708
    SerialState *s;
 
709
    Error *err = NULL;
735
710
 
736
711
    s = g_malloc0(sizeof(SerialState));
737
712
 
738
713
    s->irq = irq;
739
714
    s->baudbase = baudbase;
740
715
    s->chr = chr;
741
 
    serial_init_core(s);
 
716
    serial_realize_core(s, &err);
 
717
    if (err != NULL) {
 
718
        fprintf(stderr, "%s\n", error_get_pretty(err));
 
719
        error_free(err);
 
720
        exit(1);
 
721
    }
742
722
 
743
723
    vmstate_register(NULL, base, &vmstate_serial, s);
744
724
 
745
 
    memory_region_init_io(&s->io, &serial_io_ops, s, "serial", 8);
 
725
    memory_region_init_io(&s->io, NULL, &serial_io_ops, s, "serial", 8);
746
726
    memory_region_add_subregion(system_io, base, &s->io);
747
727
 
748
728
    return s;
788
768
                            CharDriverState *chr, enum device_endian end)
789
769
{
790
770
    SerialState *s;
 
771
    Error *err = NULL;
791
772
 
792
773
    s = g_malloc0(sizeof(SerialState));
793
774
 
796
777
    s->baudbase = baudbase;
797
778
    s->chr = chr;
798
779
 
799
 
    serial_init_core(s);
 
780
    serial_realize_core(s, &err);
 
781
    if (err != NULL) {
 
782
        fprintf(stderr, "%s\n", error_get_pretty(err));
 
783
        error_free(err);
 
784
        exit(1);
 
785
    }
800
786
    vmstate_register(NULL, base, &vmstate_serial, s);
801
787
 
802
788
    if (address_space) {
803
 
        memory_region_init_io(&s->io, &serial_mm_ops[end], s,
 
789
        memory_region_init_io(&s->io, NULL, &serial_mm_ops[end], s,
804
790
                              "serial", 8 << it_shift);
805
791
        memory_region_add_subregion(address_space, base, &s->io);
806
792
    }