blob: a0dc90db809cc6e68fb3fbedec8a39dd45d9ff5d [file] [log] [blame]
David 'Digit' Turnere3c71e12014-01-07 15:46:28 +01001Introduction
2============
3
4This file documents the 'goldfish' virtual hardware platform used to run some
5emulated Android systems under QEMU. It serves as a reference for implementers
6of virtual devices in QEMU, as well as Linux kernel developers who need to
7maintain the corresponding drivers.
8
9The following abbreviations will be used here:
10
11 $QEMU -> path to the Android AOSP directory, i.e. a git clone of
12 https://android.googlesource.com/platform/external/qemu.git
13
14 $KERNEL -> path to the Android goldfish kernel source tree, i.e. a git clone of
15 https://android.googlesource.com/kernel/goldfish.git
16
17 More specifically, to the android-goldfish-2.6.29 branch for now.
18
19'goldfish' is the name of a family of similar virtual hardware platforms, that
20mostly differ in the virtual CPU they support. 'goldfish' started as an
21ARM-specific platform, but has now been ported to x86 and MIPS virtual CPUs.
22
23Inside of QEMU, goldfish-specific virtual device implementation sources files
24are in $QEMU/hw/android/goldfish/*.c sources
25
26Inside the Linux kernel tree, they are under $KERNEL/arch/$ARCH/mach-goldfish,
27or $KERNEL/arch/$ARCH/goldfish/, as well as a few arch-independent drivers in
28different locations (detailed below).
29
30Goldfish devices appear to the Linux kernel as 'platform devices'. Read [1] and
31[2] for an introduction and reference documentation for these.
32
33Each device is identified by a name, and an optional unique id (an integer used
34to distinguish between several identical instances of similar devices, like
35serial ports, of block devices). When only one instance of a given device can
36be used, an ID of -1 is used instead.
37
38It also communicates with the kernel through:
39
40 - One or more 32-bit of I/O registers, mapped to physical addresses at
41 specific locations which depend on the architecture.
42
43 - Zero or more interrupt requests, used to signal to the kernel that an
44 important event occured.
45
46 Note that IRQ lines are numbered from 0 to 31, and are relative to the
47 goldfish interrupt controller, documented below.
48
49
50[1] http://lwn.net/Articles/448499/
51[2] https://www.kernel.org/doc/Documentation/driver-model/platform.txt
52
53
54I. Goldfish platform bus:
55=========================
56
57The 'platform bus', in Linux kernel speak, is a special device that is capable
58of enumerating other platform devices found on the system to the kernel. This
59flexibility allows to customize which virtual devices are available when running
60a given emulated system configuration.
61
62Relevant files:
63 $QEMU/hw/android/goldfish/device.c
64 $KERNEL/arch/arm/mach-goldfish/pdev_bus.c
65 $KERNEL/arch/x86/mach-goldfish/pdev_bus.c
66 $KERNEL/arch/mips/goldfish/pdev_bus.c
67
68Device properties:
69 Name: goldfish_device_bus
70 Id: -1
71 IrqCount: 1
72
73 32-bit I/O registers (offset, name, abstract)
74
75 0x00 BUS_OP R: Iterate to next device in enumeration.
76 W: Start device enumeration.
77
78 0x04 GET_NAME W: Copy device name to kernel memory.
79 0x08 NAME_LEN R: Read length of current device's name.
80 0x0c ID R: Read id of current device.
81 0x10 IO_BASE R: Read I/O base address of current device.
82 0x14 IO_SIZE R: Read I/O base size of current device.
83 0x18 IRQ_BASE R: Read base IRQ of current device.
84 0x1c IRQ_COUNT R: Read IRQ count of current device.
85
86The kernel iterates over the list of current devices with something like:
87
88 IO_WRITE(BUS_OP, 0); // Start iteration, any value other than 0 is invalid.
89 for (;;) {
90 int ret = IO_READ(BUS_OP);
91 if (ret == 0 /* OP_DONE */) {
92 // no more devices.
93 break;
94 }
95 else if (ret == 8 /* OP_ADD_DEV */) {
96 // Read device properties.
97 Device dev;
98 dev.name_len = IO_READ(NAME_LEN);
99 dev.id = IO_READ(ID);
100 dev.io_base = IO_READ(IO_BASE);
101 dev.io_size = IO_READ(IO_SIZE);
102 dev.irq_base = IO_READ(IRQ_BASE);
103 dev.irq_count = IO_READ(IRQ_COUNT);
104
105 dev.name = kalloc(dev.name_len + 1); // allocate room for device name.
106 IO_WRITE(GET_NAME, dev.name); // copy to kernel memory.
107 dev.name[dev.name_len] = 0;
108
109 .. add device to kernel's list.
110 }
111 else {
112 // Not returned by current goldfish implementation.
113 }
114 }
115
116The device also uses a single IRQ, which it will raise to indicate to the kernel
117that new devices are available, or that some of them have been removed. The
118kernel will then start a new enumeration. The IRQ is lowered by the device only
119when a IO_READ(BUS_OP) returns 0 (OP_DONE).
120
121NOTE: The kernel hard-codes a platform_device definition with the name
122 "goldfish_pdev_bus" for the platform bus (e.g. see
123 $KERNEL/arch/arm/mach-goldfish/board-goldfish.c), however, the bus itself
124 will appear during enumeration as a device named "goldfish_device_bus"
125
126 The kernel driver for the platform bus only matches the "goldfish_pdev_bus"
127 name, and will ignore any device named "goldfish_device_bus".
128
129
130II. Goldfish interrupt controller:
131==================================
132
133IMPORTANT: The controller IS NOT USED IN EMULATED X86 SYSTEMS.
134 TODO(digit): Indicate which virtual PIC is used on x86 systems.
135
136Relevant files:
137 $QEMU/hw/android/goldfish/interrupt.c
138 $KERNEL/arch/arm/mach-goldfish/board-goldfish.c
139 $KERNEL/arch/mips/goldfish/goldfish-interrupt.c
140
141Device properties:
142 Name: goldfish_interrupt_controller
143 Id: -1
144 IrqCount: 0 (uses parent CPU IRQ instead).
145
146 32-bit I/O registers (offset, name, abtract):
147 0x00 STATUS R: Read the number of pending interrupts (0 to 32).
148 0x04 NUMBER R: Read the lowest pending interrupt index, or 0 if none.
149 0x08 DISABLE_ALL W: Clear all pending interrupts (does not disable them!)
150 0x0c DISABLE W: Disable a given interrupt, value must be in [0..31].
151 0x10 ENABLE W: Enable a given interrupt, value must be in [0..31].
152
153Goldfish provides its own interrupt controller that can manage up to 32 distinct
154maskable interrupt request lines. The controller itself is cascaded from a
155parent CPU IRQ.
156
157What this means in practice:
158
159 - Each IRQ has a 'level' that is either 'high' (1) or 'low' (0).
160
161 - Each IRQ also has a binary 'enable' flag.
162
163 - Whenever (level == 1 && enabled == 1) is reached due to a state change, the
164 controller raises its parent IRQ. This typically interrupts the CPU and
165 forces the kernel to deal with the interrupt request.
166
167 - Raised/Enabled interrupts that have not been serviced yet are called
168 "pending". Raised/Disabled interrupts are called "masked" and are
169 essentially silent until enabled.
170
171When the interrupt controller triggers the parent IRQ, the kernel should do
172the following:
173
174 num_pending = IO_READ(STATUS); // Read number of pending interrupts.
175 for (int n = 0; n < num_pending; ++n) {
176 int irq_index = IO_READ(NUMBER); // Read n-th interrupt index.
177 .. service interrupt request with the proper driver.
178 }
179
180IO_WRITE(DISABLE, <num>) or IO_WRITE(ENABLE, <num>) can change the 'enable' flag
181of a given IRQ. <num> must be a value in the [0..31] range. Note that enabling
182an IRQ which has already been raised will make it active, i.e. it will raise
183the parent IRQ.
184
185IO_WRITE(DISABLE_ALL, 0) can be used to lower all interrupt levels at once (even
186disabled one). Note that this constant is probably mis-named since it does not
187change the 'enable' flag of any IRQ.
188
189Note that this is the only way for the kernel to lower an IRQ level through
190this device. Generally speaking, Goldfish devices are responsible for lowering
191their own IRQ, which is performed either when a specific condition is met, or
192when the kernel reads from or writes to a device-specific I/O register.
193
194
195III. Godlfish timer:
196====================
197
198NOTE: This is not used on x86 emulated platforms.
199
200Relevant files:
201 $QEMU/hw/android/goldfish/timer.c
202 $KERNEL/arch/arm/mach-goldfish/timer.c
203 $KERNEL/arch/mips/goldfish/goldfish-time.c
204
205Device properties:
206 Name: goldfish_timer
207 Id: -1
208 IrqCount: 1
209
210 32-bit I/O registers (offset, name, abstract)
211 0x00 TIME_LOW R: Get current time, then return low-order 32-bits.
212 0x04 TIME_HIGH R: Return high 32-bits from previous TIME_LOW read.
213 0x08 ALARM_LOW W: Set low 32-bit value of alarm, then arm it.
214 0x0c ALARM_HIGH W: Set high 32-bit value of alarm.
215 0x10 CLEAR_INTERRUPT W: Lower device's irq level.
216 0x14 CLEAR_ALARM
217
218This device is used to return the current host time to the kernel, as a
219high-precision signed 64-bit nanoseconds value, starting from a liberal point
220in time. This value should correspond to the QEMU "vm_clock", i.e. it should
221not be updated when the emulated system does _not_ run, and hence cannot be
222based directly on a host clock.
223
224To read the value, the kernel must perform an IO_READ(TIME_LOW), which returns
225an unsigned 32-bit value, before an IO_READ(TIME_HIGH), which returns a signed
22632-bit value, corresponding to the higher half of the full value.
227
228The device can also be used to program an alarm, with something like:
229
230 IO_WRITE(ALARM_HIGH, <high-value>) // Must happen first.
231 IO_WRITE(ALARM_LOW, <low-value>) // Must happen second.
232
233When the corresponding value is reached, the device will raise its IRQ. Note
234that the IRQ is raised as soon as the second IO_WRITE() if the alarm value is
235already older than the current time.
236
237IO_WRITE(CLEAR_INTERRUPT, <any>) can be used to lower the IRQ level once the
238alarm has been handled by the kernel.
239
240IO_WRITE(CLEAR_ALARM, <any>) can be used to disarm an existing alarm, if any.
241
242Note: At the moment, the alarm is only used on ARM-based system. MIPS based
243 systems only use TIME_LOW / TIME_HIGH on this device.
244
245
246III. Goldfish real-time clock (RTC):
247====================================
248
249Relevant files:
250 $QEMU/hw/android/goldfish/timer.c
251 $KERNEL/drivers/rtc/rtc-goldfish.c
252
253Device properties:
254 Name: goldfish_rtc
255 Id: -1
256 IrqCount: 1
257 I/O Registers:
258 0x00 TIME_LOW R: Get current time, then return low-order 32-bits.
259 0x04 TIME_HIGH R: Return high 32-bits, from previous TIME_LOW read.
260 0x08 ALARM_LOW W: Set low 32-bit value or alarm, then arm it.
261 0x0c ALARM_HIGH W: Set high 32-bit value of alarm.
262 0x10 CLEAR_INTERRUPT W: Lower device's irq level.
263
264This device is _very_ similar to the Goldfish timer one, with the following
265important differences:
266
267 - Values reported are still 64-bit nanoseconds, but they have a granularity
268 of 1 second, and represent host-specific values (really 'time() * 1e9')
269
270 - The alarm is non-functioning, i.e. writing to ALARM_LOW / ALARM_HIGH will
271 work, but will never arm any alarm.
272
273To support old Goldfish kernels, make sure to support writing to
274ALARM_LOW / ALARM_HIGH / CLEAR_INTERRUPT, even if the device never raises its
275IRQ.
276
277
278IV. Goldfish serial port (tty):
279===============================
280
281Relevant files:
282 $QEMU/hw/android/goldfish/tty.c
283 $KERNEL/drivers/char/goldfish_tty.c
284 $KERNEL/arch/arm/mach-goldfish/include/debug-macro.S
285
286Device properties:
287 Name: goldfish_tty
288 Id: 0 to N
289 IrqCount:
290 I/O Registers:
291 0x00 PUT_CHAR W: Write a single 8-bit value to the serial port.
292 0x04 BYTES_READY R: Read the number of available buffered input bytes.
293 0x08 CMD W: Send command (see below).
294 0x10 DATA_PTR W: Write kernel buffer address.
295 0x14 DATA_LEN W: Write kernel buffer size.
296
297This is the first case of a multi-instance goldfish device in this document.
298Each instance implements a virtual serial port that contains a small internal
299buffer where incoming data is stored until the kernel fetches it.
300
301The CMD I/O register is used to send various commands to the device, identified
302by the following values:
303
304 0x00 CMD_INT_DISABLE Disable device.
305 0x01 CMD_INT_ENABLE Enable device.
306 0x02 CMD_WRITE_BUFFER Write buffer from kernel to device.
307 0x03 CMD_READ_BUFFER Read buffer from device to kernel.
308
309Each device instance uses one IRQ that is raised to indicate that there is
310incoming/buffered data to read. To read such data, the kernel should do the
311following:
312
313 len = IO_READ(PUT_CHAR); // Read length of incoming data.
314 if (len == 0) return; // Nothing to do.
315
316 available = get_buffer(len, &buffer); // Get address of buffer and its size.
317 IO_WRITE(DATA_PTR, buffer); // Write buffer address to device.
318 IO_WRITE(DATA_LEN, available); // Write buffer length to device.
319 IO_WRITE(CMD, CMD_READ_BUFFER); // Read the data into kernel buffer.
320
321The device will automatically lower its IRQ when there is no more input data
322in its buffer. However, the kernel can also temporarily disable device interrupts
323with CMD_INT_DISABLE / CMD_INT_ENABLE.
324
325Note that disabling interrupts does not flush the buffer, nor prevent it from
326buffering further data from external inputs.
327
328To write to the serial port, the device can either send a single byte at a time
329with:
330
331 IO_WRITE(PUT_CHAR, <value>) // Send the lower 8 bits of <value>.
332
333Or use the mode efficient sequence:
334
335 IO_WRITE(DATA_PTR, buffer)
336 IO_WRITE(DATA_LEN, buffer_len)
337 IO_WRITE(CMD, CMD_WRITE_BUFFER)
338
339The former is less efficient but simpler, and is typically used by the kernel
340to send debug messages only.
341
342Note that the Android emulator always reserves the first two virtual serial
343ports:
344
345 - The first one is used to receive kernel messages, this is done by adding
346 the 'console=ttyS0' parameter to the kernel command line in
347 $QEMU/vl-android.c
348
349 - The second one is used to setup the legacy "qemud" channel, used on older
350 Android platform revisions. This is done by adding 'android.qemud=ttyS1'
351 on the kernel command line in $QEMU/vl-android.c
352
353 Read docs/ANDROID-QEMUD.TXT for more details about the data that passes
354 through this serial port. In a nutshell, this is required to emulate older
355 Android releases (e.g. cupcake). It provides a direct communication channel
356 between the guest system and the emulator.
357
358 More recent Android platforms do not use QEMUD anymore, but instead rely
359 on the much faster "QEMU pipe" device, described later in this document as
360 well as in docs/ANDROID-QEMU-PIPE.TXT.
361
362
363V. Goldfish framebuffer:
364========================
365
366Relevant files:
367 $QEMU/hw/android/goldfish/fb.c
368 $KERNEL/drivers/video/goldfish_fb.c
369
370Device properties:
371 Name: goldfish_fb
372 Id: 0 to N (only one used in practice).
373 IrqCount: 0
374 I/O Registers:
375 0x00 GET_WIDTH R: Read framebuffer width in pixels.
376 0x04 GET_HEIGHT R: Read framebuffer height in pixels.
377 0x08 INT_STATUS
378 0x0c INT_ENABLE
379 0x10 SET_BASE
380 0x14 SET_ROTATION
381 0x18 SET_BLANK W: Set 'blank' flag.
382 0x1c GET_PHYS_WIDTH R: Read framebuffer width in millimeters.
383 0x20 GET_PHYS_HEIGHT R: Read framebuffer height in millimeters.
384 0x24 GET_FORMAT R: Read framebuffer pixel format.
385
386The framebuffer device is a bit peculiar, because it uses, in addition to the
387typical I/O registers and IRQs, a large area of physical memory, allocated by
388the kernel, but visible to the emulator, to store a large pixel buffer.
389
390The emulator is responsible for displaying the framebuffer content in its UI
391window, which can be rotated, as instructed by the kernel.
392
393IMPORTANT NOTE: When GPU emulation is enabled, the framebuffer will typically
394only be used during boot. Note that GPU emulation doesn't rely on a specific
395virtual GPU device, however, it uses the "QEMU Pipe" device described below.
396For more information, please read:
397
398 https://android.googlesource.com/platform/sdk/+/master/emulator/opengl/DESIGN
399
400On boot, the kernel will read various properties of the framebuffer:
401
402 IO_READ(GET_WIDTH) and IO_READ(GET_HEIGHT) return the width and height of
403 the framebuffer in pixels. Note that a 'row' corresponds to consecutive bytes
404 in memory, but doesn't necessarily to an horizontal line on the final display,
405 due to possible rotation (see SET_ROTATION below).
406
407 IO_READ(GET_PHYS_WIDTH) and IO_READ(GET_PHYS_HEIGHT) return the emulated
408 physical width and height in millimeters, this is later used by the kernel
409 and the platform to determine the device's emulated density.
410
411 IO_READ(GET_FORMAT) returns a value matching the format of pixels in the
412 framebuffer. Note that these values are specified by the Android hardware
413 abstraction layer (HAL) and cannot change:
414
415 0x01 HAL_PIXEL_FORMAT_BRGA_8888
416 0x02 HAL_PIXEL_FORMAT_RGBX_8888
417 0x03 HAL_PIXEL_FORMAT_RGB_888
418 0x04 HAL_PIXEL_FORMAT_RGB_565
419 0x05 HAL_PIXEL_FORMAT_BGRA_8888
420 0x06 HAL_PIXEL_FORMAT_RGBA_5551
421 0x08 HAL_PIXEL_FORMAT_RGBA_4444
422
423 HOWEVER, the kernel driver only expects a value of HAL_PIXEL_FORMAT_RGB_565
424 at the moment. Until this is fixed, the virtual device should always return
425 the value 0x04 here. Rows are not padded, so the size in bytes of a single
426 framebuffer will always be exactly 'width * heigth * 2'.
427
428 Note that GPU emulation doesn't have this limitation and can use and display
429 32-bit surfaces properly, because it doesn't use the framebuffer.
430
431The device has a 'blank' flag. When set to 1, the UI should only display an
432empty/blank framebuffer, ignoring the content of the framebuffer memory.
433It is set with IO_WRITE(SET_BLANK, <value>), where value can be 1 or 0. This is
434used when simulating suspend/resume.
435
436IMPORTANT: The framebuffer memory is allocated by the kernel, which will send
437its physical address to the device by using IO_WRITE(SET_BASE, <address>).
438
439The kernel really allocates a memory buffer large enough to hold *two*
440framebuffers, in order to implement panning / double-buffering. This also means
441that calls to IO_WRITE(SET_BASE, <address>) will be frequent.
442
443The allocation happens with dma_alloc_writecombine() on ARM, which can only
444allocate a maximum of 4 MB, this limits the size of each framebuffer to 2 MB,
445which may not be enough to emulate high-density devices :-(
446
447For other architectures, dma_alloc_coherent() is used instead, and has the same
448upper limit / limitation.
449
450TODO(digit): Explain how it's possible to raise this limit by modifyinf
451 CONSISTENT_DMA_SIZE and/or MAX_ORDER in the kernel configuration.
452
453The device uses a single IRQ to notify the kernel of several events. When it
454is raised, the kernel IRQ handler must IO_READ(INT_STATUS), which will return
455a value containing the following bit flags:
456
457 bit 0: Set to 1 to indicate a VSYNC event.
458
459 bit 1: Set to 1 to indicate that the content of a previous SET_BASE has
460 been properly displayed.
461
462Note that reading this register also lowers the device's IRQ level.
463
464The second flag is essentially a way to notify the kernel that an
465IO_WRITE(SET_BASE, <address>) operation has been succesfully processed by
466the emulator, i.e. that the new content has been displayed to the user.
467
468The kernel can control which flags should raise an IRQ by using
469IO_WRITE(INT_ENABLE, <flags>), where <flags> has the same format as the
470result of IO_READ(INT_STATUS). If the corresponding bit is 0, the an IRQ
471for the corresponding event will never be generated,
472
473
474VI. Goldfish audio device:
475==========================
476
477Relevant files:
478 $QEMU/hw/android/goldfish/audio.c
479 $KERNEL/drivers/misc/goldfish_audio.c
480
481Device properties:
482 Name: goldfish_audio
483 Id: -1
484 IrqCount: 1
485 I/O Registers:
486 0x00 INT_STATUS
487 0x04 INT_ENABLE
488 0x08 SET_WRITE_BUFFER_1 W: Set address of first kernel output buffer.
489 0x0c SET_WRITE_BUFFER_2 W: Set address of second kernel output buffer.
490 0x10 WRITE_BUFFER_1 W: Send first kernel buffer samples to output.
491 0x14 WRITE_BUFFER_2 W: Send second kernel buffer samples to output.
492 0x18 READ_SUPPORTED R: Reads 1 if input is supported, 0 otherwise.
493 0x1c SET_READ_BUFFER
494 0x20 START_READ
495 0x24 READ_BUFFER_AVAILABLE
496
497This device implements a virtual sound card with the following properties:
498
499 - Stereo output at fixed 44.1 kHz frequency, using signed 16-bit samples.
500 Mandatory.
501
502 - Mono input at fixed 8 kHz frequency, using signed 16-bit samples.
503 Optional.
504
505For output, the kernel driver allocates two internal buffers to hold output
506samples, and passes their physical address with
507IO_WRITE(SET_WRITE_BUFFER_1, <buffer1>) and
508IO_WRITE(SET_WRITE_BUFFER_2, <buffer2>).
509
510After this, samples will be sent from the driver to the virtual device by
511using one of IO_WRITE(WRITE_BUFFER_1, <length1>) or
512IO_WRITE(WRITE_BUFFER_2, <length2>), depending on which sample buffer to use.
513NOTE: Each length is in bytes.
514
515Note however that the driver should wait, before doing this, until the device
516gives permission by raising its IRQ and setting the appropriate 'status' flags.
517
518The virtual device has an internal 'int_status' field made of 3 bit flags:
519
520 bit0: 1 iff the device is ready to receive data from the first buffer.
521 bit1: 1 iff the device is ready to receive data from the second buffer.
522 bit2: 1 iff the device has input samples for the kernel to read.
523
524Note that an IO_READ(INT_STATUS) also automatically lowers the IRQ level,
525except if the read value is 0 (which should not happen, since it should not
526raise the IRQ).
527
528The corresponding interrupts can be masked by using IO_WRITE(INT_ENABLE, <mask>),
529where <mask> has the same format as 'int_status'. A 1 bit in the mask enables the
530IRQ raise when the corresponding status bit is also set to 1.
531
532For input, the driver should first IO_READ(READ_SUPPORTED), which will return 1
533if the virtual device supports input, or 0 otherwise. If it does support it,
534the driver must allocate an internal buffer and send its physical address with
535IO_WRITE(SET_READ_BUFFER, <read-buffer>), then perform
536IO_WRITE(START_READ, <read-buffer-length>) to start recording and specify
537the kernel's buffer length.
538
539Later, the device will raise its IRQ and set bit2 of 'int_status' to indicate
540there are incoming samples to the driver. In its interrupt handler, the latter
541should IO_READ(READ_BUFFER_AVAILABLE), which triggers the transfer (from the
542device to the kernel), as well as return the size in bytes of the samples.
543
544
545VII. Goldfish battery:
546======================
547
548Relevant files:
549 $QEMU/hw/android/goldfish/battery.c
550 $QEMU/hw/power_supply.h
551 $KERNEL/drivers/power/goldfish_battery.c
552
553Device properties:
554 Name: goldfish_battery
555 Id: -1
556 IrqCount: 1
557 I/O Registers:
558 0x00 INT_STATUS R: Read battery and A/C status change bits.
559 0x04 INT_ENABLE W: Enable or disable IRQ on status change.
560 0x08 AC_ONLINE R: Read 0 if AC power disconnected, 1 otherwise.
561 0x0c STATUS R: Read battery status (charging/full/... see below).
562 0x10 HEALTH R: Read battery health (good/overheat/... see below).
563 0x14 PRESENT R: Read 1 if battery is present, 0 otherwise.
564 0x18 CAPACITY R: Read battery charge percentage in [0..100] range.
565
566A simple device used to report the state of the virtual device's battery, and
567whether the device is powered through a USB or A/C adapter.
568
569The device uses a single IRQ to notify the kernel that the battery or A/C status
570changed. When this happens, the kernel should perform an IO_READ(INT_STATUS)
571which returns a 2-bit value containing flags:
572
573 bit 0: Set to 1 to indicate a change in battery status.
574 bit 1: Set to 1 to indicate a change in A/C status.
575
576Note that reading this register also lowers the IRQ level.
577
578The A/C status can be read with IO_READ(AC_ONLINE), which returns 1 if the
579device is powered, or 0 otherwise.
580
581The battery status is spread over multiple I/O registers:
582
583 IO_READ(PRESENT) returns 1 if the battery is present in the virtual device,
584 or 0 otherwise.
585
586 IO_READ(CAPACITY) returns the battery's charge percentage, as an integer
587 between 0 and 100, inclusive. NOTE: This register is probably misnamed since
588 it does not represent the battery's capacity, but it's current charge level.
589
590 IO_READ(STATUS) returns one of the following values:
591
592 0x00 UNKNOWN Battery state is unknown.
593 0x01 CHARGING Battery is charging.
594 0x02 DISCHARGING Battery is discharging.
595 0x03 NOT_CHARGING Battery is not charging (e.g. full or dead).
596
597 IO_READ(HEALTH) returns one of the following values:
598
599 0x00 UNKNOWN Battery health unknown.
600 0x01 GOOD Battery is in good condition.
601 0x02 OVERHEATING Battery is over-heating.
602 0x03 DEAD Battery is dead.
603 0x04 OVERVOLTAGE Battery generates too much voltage.
604 0x05 UNSPEC_FAILURE Battery has unspecified failure.
605
606The kernel can use IO_WRITE(INT_ENABLE, <flags>) to select which condition
607changes should trigger an IRQ. <flags> is a 2-bit value using the same format
608as INT_STATUS.
609
610
611VIII. Goldfish events device (user input):
612==========================================
613
614Relevant files:
615 $QEMU/hw/android/goldfish/events_device.c
616 $KERNEL/drivers/input/keyboard/goldfish_events.c
617
618Device properties:
619 Name: goldfish_events
620 Id: -1
621 IrqCount: 1
622 I/O Registers:
623 0x00 READ R: Read next event type, code or value.
624 0x00 SET_PAGE W: Set page index.
625 0x04 LEN R: Read length of page data.
626 0x08 DATA R: Read page data.
627 .... R: Read additional page data (see below).
628
629This device is responsible for sending several kinds of user input events to
630the kernel, i.e. emulated device buttons, hardware keyboard, touch screen,
631trackball and lid events.
632
633NOTE: Android supports other input devices like mice or game controllers
634 through USB or Bluetooth, these are not supported by this virtual
635 Goldfish device.
636
637NOTE: The 'lid event' is useful for devices with a clamshell of foldable
638 keyboard design, and is used to report when it is opened or closed.
639
640As per Linux conventions, each 'emulated event' is sent to the kernel as a
641series of (<type>,<code>,<value>) triplets or 32-bit values. For more
642information, see:
643
644 https://www.kernel.org/doc/Documentation/input/input.txt
645
646As well as the <linux/input.h> kernel header.
647
648Note that in the context of goldfish:
649
650 - Button and keyboard events are reported with:
651 (EV_KEY, <code>, <press>)
652
653 Where <code> is a 9-bit keycode, as defined by <linux/input.h>, and
654 <press> is 1 for key/button presses, and 0 for releases.
655
656 - For touchscreen events, a single-touch event is reported with:
657 (EV_ABS, ABS_X, <x-position>) +
658 (EV_ABS, ABS_Y, <y-position>) +
659 (EV_ABS, ABS_Z, 0) +
660 (EV_KEY, BTN_TOUCH, <button-state>) +
661 (EV_SYN, 0, 0)
662
663 where <x-position> and <y-position> are the horizontal and vertical position
664 of the touch event, respectfully, and <button-state> is either 1 or 0 and
665 indicates the start/end of the touch gesture, respectively.
666
667 - For multi-touch events, things are much more complicated. In a nutshell,
668 these events are reported through (EV_ABS, ABS_MT_XXXXX, YYY) triplets,
669 as documented at:
670
671 https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
672
673 TODO(digit): There may be bugs in either the virtual device or driver code
674 when it comes to multi-touch. Iron out the situation and better
675 explain what's required to support all Android platforms.
676
677 - For trackball events:
678 (EV_REL, REL_X, <x-delta>) +
679 (EV_REL, REL_Y, <y-delta>) +
680 (EV_SYN, 0, 0)
681
682 Where <x-delta> and <y-delta> are the signed relative trackball displacement
683 in the horizontal and vertical directions, respectively.
684
685 - For lid events:
686 (EV_SW, 0, 1) + (EV_SYN, 0, 0) // When lid is closed.
687 (EV_SW, 0, 0) + (EV_SYN, 0, 0) // When lid is opened.
688
689When the kernel driver starts, it will probe the device to know what kind
690of events are supported by the emulated configuration. There are several
691categories of queries:
692
693 - Asking for the current physical keyboard 'charmap' name, used by the system
694 to translate keycodes in actual characters. In practice, this will nearly
695 always be 'goldfish' for emulated systems, but this out of spec for this
696 document.
697
698 - Asking which event codes are supported for a given event type
699 (e.g. all the possible KEY_XXX values generated for EV_KEY typed triplets).
700
701 - Asking for various minimum or maximum values for each supported EV_ABS
702 event code. For example the min/max values of (EV_ABS, ABS_X, ...) triplets,
703 to know the bounds of the input touch panel.
704
705The kernel driver first select which kind of query it wants by using
706IO_WRITE(SET_PAGE, <page>), where <page> is one of the following values:
707
708 PAGE_NAME 0x0000 Keyboard charmap name.
709 PAGE_EVBITS 0x10000 Event code supported sets.
710 PAGE_ABSDATA 0x20003 (really 0x20000 + EV_ABS) EV_ABS min/max values.
711
712Once a 'page' has been selected, it is possible to read from it with
713IO_READ(LEN) and IO_READ(DATA). In practice:
714
715 - To read the name of the keyboard charmap, the kernel will do:
716
717 IO_WRITE(SET_PAGE, PAGE_NAME); # Ind
718
719 charmap_name_len = IO_READ(LEN);
720 charmap_name = kalloc(charmap_name_len + 1);
721 for (int n = 0; n < charmap_name_len; ++n)
722 charmap_name[n] = (char) IO_READ(DATA);
723 charmap_name[n] = 0;
724
725 - To read which codes a given event type (here EV_KEY) supports:
726
727 IO_WRITE(SET_PAGE, PAGE_EVBITS + EV_KEY); // Or EV_REL, EV_ABS, etc...
728
729 bitmask_len = IO_READ(LEN);
730 for (int offset = 0; offset < bitmask_len; ++offset) {
731 uint8_t mask = (uint8_t) IO_READ(DATA):
732 for (int bit = 0; bit < 8; ++bit) {
733 int code = (offset * 8) + bit;
734 if ((mask & (1 << bit)) != 0) {
735 ... record that keycode |code| is supported.
736 }
737 }
738 }
739
740 - To read the range values of absolute event values:
741
742 IO_WRITE(SET_PAGE, PAGE_ABSDATA);
743 max_entries = IO_READ(LEN);
744 for (int n = 0; n < max_entries; n += 4) {
745 int32_t min = IO_READ(DATA + n);
746 int32_t max = IO_READ(DATA + n + 4);
747 int32_t fuzz = IO_READ(DATA + n + 8);
748 int32_t flat = IO_READ(DATA + n + 12);
749 int event_code = n/4;
750
751 // Record (min, max, fuzz, flat) values for EV_ABS 'event_code'.
752 }
753
754 Note that the 'fuzz' and 'flat' values reported by Goldfish are always 0,
755 refer to the source for more details.
756
757At runtime, the device implements a small buffer for incoming event triplets
758(each one is stored as three 32-bit integers in a circular buffer), and raises
759its IRQ to signal them to the kernel.
760
761When that happens, the kernel driver should use IO_READ(READ) to extract the
76232-bit values from the device. Note that three IO_READ() calls are required to
763extract a single event triplet.
764
765There are a few important notes here:
766
767 - The IRQ should not be raised _before_ the kernel driver is started
768 (otherwise the driver will be confused and ignore all events).
769
770 I.e. the emulator can buffer events before kernel initialization completes,
771 but should only raise the IRQ, if needed, lazily. Currently this is done
772 on the first IO_READ(LEN) following a IO_WRITE(SET_PAGE, PAGE_ABSDATA).
773
774 - The IRQ is lowered by the device once all event values have been read,
775 i.e. its buffer is empty.
776
777 However, on x86, if after an IO_READ(READ), there are still values in the
778 device's buffer, the IRQ should be lowered then re-raised immediately.
779
780
781IX. Goldfish NAND device:
782=========================
783
784Relevant files:
785 $QEMU/hw/android/goldfish/nand.c
786 $KERNEL/drivers/mtd/devices/goldfish_nand.c
787
788Device properties:
789 Name: goldfish_nand
790 Id: -1
791 IrqCount: 1
792 I/O Registers:
793
794This virtual device can provide access to one or more emulated NAND memory
795banks [3] (each one being backed by a different host file in the current
796implementation).
797
798These are used to back the following virtual partition files:
799
800 - system.img
801 - data.img
802 - cache.img
803
804TODO(digit): Complete this.
805
806
807[3] http://en.wikipedia.org/wiki/Flash_memory#NAND_memories
808
809
810X. Goldfish MMC device:
811=======================
812
813Relevant files:
814 $QEMU/hw/android/goldfish/mmc.c
815 $KERNEL/drivers/mmc/host/goldfish.c
816
817Device properties:
818 Name: goldfish_mmc
819 Id: -1
820 IrqCount: 1
821 I/O Registers:
822
823Similar to the NAND device, but uses a different, higher-level interface
824to access the emulated 'flash' memory. This is only used to access the
825virtual SDCard device with the Android emulator.
826
827TODO(digit): Complete this.
828
829
830XI. Goldfish memlog:
831=====================
832
833Relevant files:
834 $QEMU/hw/android/goldfish/memlog.c
835
836Device properties:
837 Name: goldfish_memlog
838 Id: -1
839 IrqCount: 0
840 I/O Registers:
841 ???
842
843Obsolete debugging device. Should be removed from both the kernel and the
844emulator sources.
845
846
847XII. Goldfish switch:
848=====================
849
850Relevant files:
851 $QEMU/hw/android/goldfish/switch.c
852 $KERNEL/arch/arm/mach-goldfish/switch.c
853 $KERNEL/arch/mips/goldfish/switch.c
854
855Device properties:
856 Name: goldfish_switch
857 Id: -1
858 IrqCount: 1
859 I/O Registers:
860
861Obsolete debugging device. Should be removed from both the kernel and the
862emulator sources.
863
864
865XIV. QEMU Pipe device:
866======================
867
868Relevant files:
869 $QEMU/hw/android/goldfish/pipe.c
870 $KERNEL/drivers/misc/qemupipe/qemu_pipe.c
871
872Device properties:
873 Name: qemu_pipe
874 Id: -1
875 IrqCount: 1
876 I/O Registers:
877 0x00 COMMAND W: Write to perform command (see below).
878 0x04 STATUS R: Read status
879 0x08 CHANNEL RW: Read or set current channel id.
880 0x0c SIZE RW: Read or set current buffer size.
881 0x10 ADDRESS RW: Read or set current buffer physical address.
882 0x14 WAKES R: Read wake flags.
883 0x18 PARAMS_ADDR_LOW RW: Read/set low bytes of parameters block address.
884 0x1c PARAMS_ADDR_HIGH RW: Read/set high bytes of parameters block address.
885 0x20 ACCESS_PARAMS W: Perform access with parameter block.
886
887This is a special device that is totally specific to QEMU, but allows guest
888processes to communicate directly with the emulator with extremely high
889performance. This is achieved by avoiding any in-kernel memory copies, relying
890on the fact that QEMU can access guest memory at runtime (under proper
891conditions controlled by the kernel).
892
893Please refer to $QEMU/docs/ANDROID-QEMU-PIPE.TXT for full details on the
894device's operations.
895
896
897XIII. QEMU Trace device:
898========================
899
900Relevant files:
901 $QEMU/hw/android/goldfish/trace.c
902 $KERNEL/drivers/misc/qemutrace/qemu_trace.c
903 $KERNEL/drivers/misc/qemutrace/qemu_trace_sysfs.c
904 $KERNEL/fs/exec.c
905 $KERNEL/exit.c
906 $KERNEL/fork.c
907 $KERNEL/sched/core.c
908 $KERNEL/mm/mmap.c
909
910Device properties:
911 Name: qemu_trace
912 Id: -1
913 IrqCount: 0
914 I/O Registers:
915
916TODO(digit)
917