summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2018-04-06 15:39:32 +0800
committerJeremy Kerr <jk@ozlabs.org>2018-04-17 15:32:41 +0800
commit9441b52214861a7faa53130e33e45eebb75a2397 (patch)
treedde3bac9352b22e5e4643d8b910e3ffc0e75dfd8
downloaduart-render-controller-9441b52214861a7faa53130e33e45eebb75a2397.tar.gz
uart-render-controller-9441b52214861a7faa53130e33e45eebb75a2397.zip
Initial commit
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
-rw-r--r--LICENSE202
-rw-r--r--Makefile.am7
-rw-r--r--configure.ac17
-rw-r--r--uart-render-controller.c434
4 files changed, 660 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..8f71f43
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "{}"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright {yyyy} {name of copyright owner}
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..3335e01
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,7 @@
+sbin_PROGRAMS = obmc-uart-render-controller
+
+obmc_uart_render_controller_SOURCES = \
+ uart-render-controller.c
+
+obmc_uart_render_controller_CPPFLAGS = $(LIBSYSTEMD_CFLAGS)
+obmc_uart_render_controller_LDADD = $(LIBSYSTEMD_LIBS)
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..8a7ced3
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,17 @@
+AC_PREREQ([2.69])
+AC_INIT([obmc-uart-render-controller], 0.1, [jk@ozlabs.org])
+AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz])
+AM_SILENT_RULES([yes])
+
+AC_PROG_CC
+AM_PROG_AR
+AC_PROG_INSTALL
+AC_PROG_MAKE_SET
+
+AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CFLAGS])
+
+PKG_CHECK_MODULES([LIBSYSTEMD], [libsystemd], [],
+ [AC_MSG_ERROR(["libsystemd required and not found."])])
+
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/uart-render-controller.c b/uart-render-controller.c
new file mode 100644
index 0000000..dde6e63
--- /dev/null
+++ b/uart-render-controller.c
@@ -0,0 +1,434 @@
+/**
+ * UART-to-video rendering controller.
+ *
+ * Copyright © 2018 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <systemd/sd-bus.h>
+
+/*
+ * We monitor two things: host state, and the VGA password register.
+ * State diagram:
+ *
+ * ┌──────────┐
+ * │host idle │←──┐
+ * └──────────┘ │
+ * │ │
+ * host host
+ * on !on
+ * ⭣ │
+ * ┌──────────┐ │
+ * │ GPU idle │───┤
+ * └──────────┘ │
+ * │ │
+ * vga pw │
+ * set │
+ * ⭣ │
+ * ┌──────────┐ │
+ * │ running │───┘
+ * └──────────┘
+ *
+ * We only want the renderer running while in GPU idle state (host may be
+ * outputting UART data, but isn't yet outputting video). That maps the GPU
+ * state transitions to renderer state transitions as follows:
+ *
+ * host on: start renderer
+ * host !on: stop renderer
+ * vga pw set: stop renderer
+ *
+ * Even though there are only two states for the renderer, we keep track of the
+ * three host states, so we know when to monitor the VGA pw register.
+ */
+
+enum host_state {
+ STATE_HOST_IDLE,
+ STATE_HOST_GPU_IDLE,
+ STATE_HOST_GPU_RUNNING,
+};
+
+struct render_ctx {
+ enum host_state state;
+ int vga_pw_fd;
+ sd_bus *bus;
+
+ const char *service_name;
+};
+
+static const int scratch_poll_ms = 100;
+
+static const char *host_bus_name = "xyz.openbmc_project.State.Host";
+static const char *host_iface_name = "xyz.openbmc_project.State.Host";
+static const char *host_obj_path = "/xyz/openbmc_project/state/host0";
+static const char *host_state_prop = "CurrentHostState";
+static const char *host_state_prop_running =
+ "xyz.openbmc_project.State.Host.HostState.Running";
+
+static const char *dbus_iface_props = "org.freedesktop.DBus.Properties";
+
+static const char *systemd_bus_name = "org.freedesktop.systemd1";
+static const char *systemd_obj = "/org/freedesktop/systemd1";
+static const char *systemd_iface = "org.freedesktop.systemd1.Manager";
+
+static void set_renderer_running(struct render_ctx *ctx, bool running)
+{
+ sd_bus_error e = {0};
+ int rc;
+
+ fprintf(stderr, "setting render state to %s\n",
+ running ? "running" : "stopped");
+
+ rc = sd_bus_call_method(ctx->bus,
+ systemd_bus_name,
+ systemd_obj,
+ systemd_iface,
+ running ? "StartUnit" : "StopUnit",
+ &e,
+ NULL,
+ "ss",
+ ctx->service_name,
+ "replace");
+
+ if (rc < 0)
+ warnx("failed to %s %s: %s\n",
+ running ? "start" : "stop",
+ ctx->service_name,
+ sd_bus_error_is_set(&e) ?
+ e.name : strerror(rc));
+}
+
+static int read_vga_pw_reg(struct render_ctx *ctx, uint8_t *regp)
+{
+ unsigned long tmp;
+ char buf[8];
+ int rc;
+
+ rc = lseek(ctx->vga_pw_fd, 0, SEEK_SET);
+ if (rc == -1) {
+ warn("Can't seek VGA scratch reg interface");
+ return -1;
+ }
+
+ rc = read(ctx->vga_pw_fd, buf, sizeof(buf));
+ if (rc < 1) {
+ warn("Can't read VGA scratch register");
+ return -1;
+ }
+
+ errno = 0;
+ tmp = strtoul(buf, NULL, 0);
+ if (errno || (tmp & ~0xfful)) {
+ warn("Can't parse VGA scratch register value");
+ return -1;
+ }
+
+ *regp = tmp & 0xff;
+
+ return 0;
+}
+
+static void handle_gpu_state(struct render_ctx *ctx, bool running)
+{
+ switch (ctx->state) {
+ case STATE_HOST_GPU_IDLE:
+ if (running) {
+ set_renderer_running(ctx, false);
+ ctx->state = STATE_HOST_GPU_RUNNING;
+ }
+ break;
+ case STATE_HOST_GPU_RUNNING:
+ /* we don't transition here, as the host will never cede
+ * control of the GPU while running */
+ break;
+ case STATE_HOST_IDLE:
+ /* can't have the GPU running with the host off */
+ break;
+ }
+}
+
+static void query_scratch_reg_state(struct render_ctx *ctx)
+{
+ bool gpu_running;
+ uint8_t reg;
+ int rc;
+
+ rc = read_vga_pw_reg(ctx, &reg);
+ if (rc) {
+ warnx("error reading VGA scratch register, assuming active");
+ gpu_running = true;
+ } else {
+ gpu_running = reg == 0xa8;
+ }
+
+ handle_gpu_state(ctx, gpu_running);
+}
+
+static void handle_host_state(struct render_ctx *ctx, bool running)
+{
+ switch (ctx->state) {
+ case STATE_HOST_IDLE:
+ if (running) {
+ ctx->state = STATE_HOST_GPU_IDLE;
+ query_scratch_reg_state(ctx);
+ if (ctx->state == STATE_HOST_GPU_IDLE)
+ set_renderer_running(ctx, true);
+ }
+ break;
+
+ case STATE_HOST_GPU_IDLE:
+ case STATE_HOST_GPU_RUNNING:
+ if (!running) {
+ ctx->state = STATE_HOST_IDLE;
+ set_renderer_running(ctx, false);
+ }
+ break;
+ }
+}
+
+static bool host_state_is_running(const char *state)
+{
+ return !strcmp(state, host_state_prop_running);
+}
+
+/* returns:
+ * <0 on message error
+ * 0 if the property isn't applicable
+ * 1 if the property was processed
+ */
+static int handle_host_propchange_entry(struct render_ctx *ctx,
+ sd_bus_message *m)
+{
+ const char *name, *type, *val;
+ char c;
+ int rc;
+
+ rc = sd_bus_message_enter_container(m,
+ SD_BUS_TYPE_DICT_ENTRY, "sv");
+ if (rc <= 0)
+ return -1;
+
+ rc = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name);
+ if (rc < 0)
+ goto out_exit;
+
+ /* is this a host state change? */
+ if (strcmp(name, host_state_prop)) {
+ rc = 0;
+ goto out_exit;
+ }
+
+ rc = sd_bus_message_peek_type(m, &c, &type);
+ if (rc < 0)
+ goto out_exit;
+
+ /* host state change should be a string, but be paranoid and check
+ * anyway */
+ if (c != SD_BUS_TYPE_VARIANT || *type != SD_BUS_TYPE_STRING) {
+ char skip_type[] = { c, '\0' };
+ sd_bus_message_skip(m, skip_type);
+ rc = 0;
+ goto out_exit;
+ }
+
+ rc = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, type);
+ if (rc < 0)
+ goto out_exit;
+
+ rc = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &val);
+ if (rc >= 0) {
+ handle_host_state(ctx, host_state_is_running(val));
+ rc = 1;
+ }
+
+ sd_bus_message_exit_container(m);
+
+out_exit:
+ sd_bus_message_exit_container(m);
+ return rc;
+}
+static int handle_dbus_signal_host_propchange(sd_bus_message *m,
+ void *data, sd_bus_error *err)
+{
+ struct render_ctx *ctx = data;
+ const char *s;
+ int rc;
+
+ /* check that the member and signature indicate a property
+ * change event
+ */
+ if (strcmp(sd_bus_message_get_interface(m), dbus_iface_props))
+ return 0;
+
+ if (strcmp(sd_bus_message_get_member(m), "PropertiesChanged"))
+ return 0;
+
+ /* is the property change part of the state interface? */
+ rc = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &s);
+ if (rc < 0 || strcmp(s, host_iface_name))
+ return 0;
+
+ rc = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
+ if (rc < 0)
+ return -1;
+
+ for (;;) {
+ rc = handle_host_propchange_entry(ctx, m);
+ if (rc)
+ break;
+ }
+
+ return 0;
+}
+
+static int handle_dbus_event(struct render_ctx *ctx)
+{
+ int rc;
+
+ for (;;) {
+ rc = sd_bus_process(ctx->bus, NULL);
+
+ if (rc <= 0)
+ return rc;
+ }
+}
+
+static int run_daemon(struct render_ctx *ctx)
+{
+ struct pollfd pollfds[1];
+ int rc, timeout;
+
+ pollfds[0].fd = sd_bus_get_fd(ctx->bus);
+ pollfds[0].events = POLLIN;
+
+ for (;;) {
+ if (ctx->state == STATE_HOST_GPU_IDLE)
+ timeout = scratch_poll_ms;
+ else
+ timeout = -1;
+
+ rc = poll(pollfds, 1, timeout);
+
+ if (rc < 0) {
+ warn("poll failure");
+ return -1;
+ }
+
+ if (pollfds[0].revents)
+ handle_dbus_event(ctx);
+
+ if (ctx->state == STATE_HOST_GPU_IDLE)
+ query_scratch_reg_state(ctx);
+ }
+
+ return 0;
+}
+
+static int query_initial_state(struct render_ctx *ctx)
+{
+ sd_bus_error sd_err = { 0 };
+ char *host_state;
+ int rc;
+
+ ctx->state = STATE_HOST_IDLE;
+
+ rc = sd_bus_get_property_string(ctx->bus,
+ host_bus_name,
+ host_obj_path,
+ host_iface_name,
+ host_state_prop,
+ &sd_err,
+ &host_state);
+
+ if (rc < 0) {
+ warnx("error querying host state: %d, %s\n",
+ rc, sd_err.name);
+ sd_bus_error_free(&sd_err);
+ return rc;
+ }
+
+ handle_host_state(ctx, host_state_is_running(host_state));
+ free(host_state);
+
+ return 0;
+}
+
+static void usage(const char *progname)
+{
+ fprintf(stderr, "usage: %s <VGA-reg> <renderer-service>\n",
+ progname);
+}
+
+
+int main(int argc, char **argv)
+{
+ struct render_ctx _ctx, *ctx;
+ const char *vga_pw_path;
+ char *match;
+ int rc;
+
+ ctx = &_ctx;
+
+ if (argc != 3) {
+ usage(argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ vga_pw_path = argv[1];
+ ctx->service_name = argv[2];
+
+ ctx->vga_pw_fd = open(vga_pw_path, O_RDONLY);
+ if (ctx->vga_pw_fd < 0)
+ err(EXIT_FAILURE, "can't open VGA scratch regs interface at %s",
+ vga_pw_path);
+
+
+ rc = sd_bus_default(&ctx->bus);
+ if (rc < 0)
+ err(EXIT_FAILURE, "can't connect to dbus");
+
+
+ rc = query_initial_state(ctx);
+ if (rc)
+ return EXIT_FAILURE;
+
+ rc = asprintf(&match,
+ "type='signal',path='%s',interface='%s',member='%s'",
+ host_obj_path,
+ dbus_iface_props,
+ "PropertiesChanged");
+
+ if (rc < 0)
+ return EXIT_FAILURE;
+
+ rc = sd_bus_add_match(ctx->bus, NULL, match,
+ handle_dbus_signal_host_propchange, ctx);
+ if (rc < 0)
+ errx(EXIT_FAILURE,
+ "can't establish host property change handler");
+
+ rc = run_daemon(ctx);
+
+ return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
OpenPOWER on IntegriCloud