blob: 36f1c4adb3fa058d52347dda42e29f5fd39f55d9 [file] [log] [blame]
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001/*
2 * QEMU Bochs-style debug console ("port E9") emulation
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2008 Citrix Systems, Inc.
6 * Copyright (c) Intel Corporation; author: H. Peter Anvin
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010027#include "hw/hw.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020028#include "sysemu/char.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010029#include "hw/isa/isa.h"
30#include "hw/i386/pc.h"
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080031
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +010032#define TYPE_ISA_DEBUGCON_DEVICE "isa-debugcon"
33#define ISA_DEBUGCON_DEVICE(obj) \
34 OBJECT_CHECK(ISADebugconState, (obj), TYPE_ISA_DEBUGCON_DEVICE)
35
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080036//#define DEBUG_DEBUGCON
37
38typedef struct DebugconState {
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +010039 MemoryRegion io;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080040 CharDriverState *chr;
41 uint32_t readback;
42} DebugconState;
43
44typedef struct ISADebugconState {
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +010045 ISADevice parent_obj;
46
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080047 uint32_t iobase;
48 DebugconState state;
49} ISADebugconState;
50
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +010051static void debugcon_ioport_write(void *opaque, hwaddr addr, uint64_t val,
52 unsigned width)
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080053{
54 DebugconState *s = opaque;
55 unsigned char ch = val;
56
57#ifdef DEBUG_DEBUGCON
liguang668fca92013-05-24 10:47:33 +080058 printf(" [debugcon: write addr=0x%04" HWADDR_PRIx " val=0x%02" PRIx64 "]\n", addr, val);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080059#endif
60
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -050061 qemu_chr_fe_write(s->chr, &ch, 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080062}
63
64
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +010065static uint64_t debugcon_ioport_read(void *opaque, hwaddr addr, unsigned width)
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080066{
67 DebugconState *s = opaque;
68
69#ifdef DEBUG_DEBUGCON
liguang668fca92013-05-24 10:47:33 +080070 printf("debugcon: read addr=0x%04" HWADDR_PRIx "\n", addr);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080071#endif
72
73 return s->readback;
74}
75
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +010076static const MemoryRegionOps debugcon_ops = {
77 .read = debugcon_ioport_read,
78 .write = debugcon_ioport_write,
79 .valid.min_access_size = 1,
80 .valid.max_access_size = 1,
81 .endianness = DEVICE_LITTLE_ENDIAN,
82};
83
Andreas Färberdb895a12012-11-25 02:37:14 +010084static void debugcon_realize_core(DebugconState *s, Error **errp)
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080085{
86 if (!s->chr) {
Andreas Färberdb895a12012-11-25 02:37:14 +010087 error_setg(errp, "Can't create debugcon device, empty char device");
88 return;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080089 }
90
91 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, s);
92}
93
Andreas Färberdb895a12012-11-25 02:37:14 +010094static void debugcon_isa_realizefn(DeviceState *dev, Error **errp)
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080095{
Andreas Färberdb895a12012-11-25 02:37:14 +010096 ISADevice *d = ISA_DEVICE(dev);
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +010097 ISADebugconState *isa = ISA_DEBUGCON_DEVICE(dev);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -080098 DebugconState *s = &isa->state;
Andreas Färberdb895a12012-11-25 02:37:14 +010099 Error *err = NULL;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -0800100
Andreas Färberdb895a12012-11-25 02:37:14 +0100101 debugcon_realize_core(s, &err);
102 if (err != NULL) {
103 error_propagate(errp, err);
104 return;
105 }
Paolo Bonzini300b1fc2013-06-06 21:25:08 -0400106 memory_region_init_io(&s->io, OBJECT(dev), &debugcon_ops, s,
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +0100107 TYPE_ISA_DEBUGCON_DEVICE, 1);
Andreas Färberdb895a12012-11-25 02:37:14 +0100108 memory_region_add_subregion(isa_address_space_io(d),
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +0100109 isa->iobase, &s->io);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -0800110}
111
Anthony Liguori39bffca2011-12-07 21:34:16 -0600112static Property debugcon_isa_properties[] = {
Paolo Bonzinic7bcc852014-02-08 11:01:53 +0100113 DEFINE_PROP_UINT32("iobase", ISADebugconState, iobase, 0xe9),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600114 DEFINE_PROP_CHR("chardev", ISADebugconState, state.chr),
Paolo Bonzinic7bcc852014-02-08 11:01:53 +0100115 DEFINE_PROP_UINT32("readback", ISADebugconState, state.readback, 0xe9),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600116 DEFINE_PROP_END_OF_LIST(),
117};
118
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600119static void debugcon_isa_class_initfn(ObjectClass *klass, void *data)
120{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600121 DeviceClass *dc = DEVICE_CLASS(klass);
Andreas Färberdb895a12012-11-25 02:37:14 +0100122
123 dc->realize = debugcon_isa_realizefn;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600124 dc->props = debugcon_isa_properties;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300125 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600126}
127
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100128static const TypeInfo debugcon_isa_info = {
Gerd Hoffmanne8ba1ce2012-12-12 15:43:35 +0100129 .name = TYPE_ISA_DEBUGCON_DEVICE,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600130 .parent = TYPE_ISA_DEVICE,
131 .instance_size = sizeof(ISADebugconState),
132 .class_init = debugcon_isa_class_initfn,
H. Peter Anvinc9f398e2009-12-29 13:51:36 -0800133};
134
Andreas Färber83f7d432012-02-09 15:20:55 +0100135static void debugcon_register_types(void)
H. Peter Anvinc9f398e2009-12-29 13:51:36 -0800136{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600137 type_register_static(&debugcon_isa_info);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -0800138}
139
Andreas Färber83f7d432012-02-09 15:20:55 +0100140type_init(debugcon_register_types)