1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
// SPDX-License-Identifier: GPL-2.0 or MIT
/*
* Copyright (c) 2024 Red Hat.
* Author: Jocelyn Falempe <jfalempe@redhat.com>
*/
#include <linux/console.h>
#include <linux/font.h>
#include <linux/init.h>
#include <linux/iosys-map.h>
#include <linux/module.h>
#include <linux/types.h>
#include <drm/drm_client.h>
#include <drm/drm_drv.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_print.h>
#include "drm_client_internal.h"
#include "drm_draw_internal.h"
#include "drm_internal.h"
MODULE_AUTHOR("Jocelyn Falempe");
MODULE_DESCRIPTION("DRM boot logger");
MODULE_LICENSE("GPL");
static unsigned int scale = 1;
module_param(scale, uint, 0444);
MODULE_PARM_DESC(scale, "Integer scaling factor for drm_log, default is 1");
/**
* DOC: overview
*
* This is a simple graphic logger, to print the kernel message on screen, until
* a userspace application is able to take over.
* It is only for debugging purpose.
*/
struct drm_log_scanout {
struct drm_client_buffer *buffer;
const struct font_desc *font;
u32 rows;
u32 columns;
u32 scaled_font_h;
u32 scaled_font_w;
u32 line;
u32 format;
u32 px_width;
u32 front_color;
u32 prefix_color;
};
struct drm_log {
struct mutex lock;
struct drm_client_dev client;
struct console con;
bool probed;
u32 n_scanout;
struct drm_log_scanout *scanout;
};
static struct drm_log *client_to_drm_log(struct drm_client_dev *client)
{
return container_of(client, struct drm_log, client);
}
static struct drm_log *console_to_drm_log(struct console *con)
{
return container_of(con, struct drm_log, con);
}
static void drm_log_blit(struct iosys_map *dst, unsigned int dst_pitch,
const u8 *src, unsigned int src_pitch,
u32 height, u32 width, u32 px_width, u32 color)
{
switch (px_width) {
case 2:
drm_draw_blit16(dst, dst_pitch, src, src_pitch, height, width, scale, color);
break;
case 3:
drm_draw_blit24(dst, dst_pitch, src, src_pitch, height, width, scale, color);
break;
case 4:
drm_draw_blit32(dst, dst_pitch, src, src_pitch, height, width, scale, color);
break;
default:
WARN_ONCE(1, "Can't blit with pixel width %d\n", px_width);
}
}
static void drm_log_clear_line(struct drm_log_scanout *scanout, u32 line)
{
struct drm_framebuffer *fb = scanout->buffer->fb;
unsigned long height = scanout->scaled_font_h;
struct iosys_map map;
struct drm_rect r = DRM_RECT_INIT(0, line * height, fb->width, height);
if (drm_client_buffer_vmap_local(scanout->buffer, &map))
return;
iosys_map_memset(&map, r.y1 * fb->pitches[0], 0, height * fb->pitches[0]);
drm_client_buffer_vunmap_local(scanout->buffer);
drm_client_framebuffer_flush(scanout->buffer, &r);
}
static void drm_log_draw_line(struct drm_log_scanout *scanout, const char *s,
unsigned int len, unsigned int prefix_len)
{
struct drm_framebuffer *fb = scanout->buffer->fb;
struct iosys_map map;
const struct font_desc *font = scanout->font;
size_t font_pitch = DIV_ROUND_UP(font->width, 8);
const u8 *src;
u32 px_width = fb->format->cpp[0];
struct drm_rect r = DRM_RECT_INIT(0, scanout->line * scanout->scaled_font_h,
fb->width, (scanout->line + 1) * scanout->scaled_font_h);
u32 i;
if (drm_client_buffer_vmap_local(scanout->buffer, &map))
return;
iosys_map_incr(&map, r.y1 * fb->pitches[0]);
for (i = 0; i < len && i < scanout->columns; i++) {
u32 color = (i < prefix_len) ? scanout->prefix_color : scanout->front_color;
src = drm_draw_get_char_bitmap(font, s[i], font_pitch);
drm_log_blit(&map, fb->pitches[0], src, font_pitch,
scanout->scaled_font_h, scanout->scaled_font_w,
px_width, color);
iosys_map_incr(&map, scanout->scaled_font_w * px_width);
}
scanout->line++;
if (scanout->line >= scanout->rows)
scanout->line = 0;
drm_client_buffer_vunmap_local(scanout->buffer);
drm_client_framebuffer_flush(scanout->buffer, &r);
}
static void drm_log_draw_new_line(struct drm_log_scanout *scanout,
const char *s, unsigned int len, unsigned int prefix_len)
{
if (scanout->line == 0) {
drm_log_clear_line(scanout, 0);
drm_log_clear_line(scanout, 1);
drm_log_clear_line(scanout, 2);
} else if (scanout->line + 2 < scanout->rows)
drm_log_clear_line(scanout, scanout->line + 2);
drm_log_draw_line(scanout, s, len, prefix_len);
}
/*
* Depends on print_time() in printk.c
* Timestamp is written with "[%5lu.%06lu]"
*/
#define TS_PREFIX_LEN 13
static void drm_log_draw_kmsg_record(struct drm_log_scanout *scanout,
const char *s, unsigned int len)
{
u32 prefix_len = 0;
if (len > TS_PREFIX_LEN && s[0] == '[' && s[6] == '.' && s[TS_PREFIX_LEN] == ']')
prefix_len = TS_PREFIX_LEN + 1;
/* do not print the ending \n character */
if (s[len - 1] == '\n')
len--;
while (len > scanout->columns) {
drm_log_draw_new_line(scanout, s, scanout->columns, prefix_len);
s += scanout->columns;
len -= scanout->columns;
prefix_len = 0;
}
if (len)
drm_log_draw_new_line(scanout, s, len, prefix_len);
}
static u32 drm_log_find_usable_format(struct drm_plane *plane)
{
int i;
for (i = 0; i < plane->format_count; i++)
if (drm_draw_color_from_xrgb8888(0xffffff, plane->format_types[i]) != 0)
return plane->format_types[i];
return DRM_FORMAT_INVALID;
}
static int drm_log_setup_modeset(struct drm_client_dev *client,
struct drm_mode_set *mode_set,
struct drm_log_scanout *scanout)
{
struct drm_crtc *crtc = mode_set->crtc;
u32 width = mode_set->mode->hdisplay;
u32 height = mode_set->mode->vdisplay;
u32 format;
scanout->font = get_default_font(width, height, NULL, NULL);
if (!scanout->font)
return -ENOENT;
format = drm_log_find_usable_format(crtc->primary);
if (format == DRM_FORMAT_INVALID)
return -EINVAL;
scanout->buffer = drm_client_framebuffer_create(client, width, height, format);
if (IS_ERR(scanout->buffer)) {
drm_warn(client->dev, "drm_log can't create framebuffer %d %d %p4cc\n",
width, height, &format);
return -ENOMEM;
}
mode_set->fb = scanout->buffer->fb;
scanout->scaled_font_h = scanout->font->height * scale;
scanout->scaled_font_w = scanout->font->width * scale;
scanout->rows = height / scanout->scaled_font_h;
scanout->columns = width / scanout->scaled_font_w;
scanout->front_color = drm_draw_color_from_xrgb8888(0xffffff, format);
scanout->prefix_color = drm_draw_color_from_xrgb8888(0x4e9a06, format);
return 0;
}
static int drm_log_count_modeset(struct drm_client_dev *client)
{
struct drm_mode_set *mode_set;
int count = 0;
mutex_lock(&client->modeset_mutex);
drm_client_for_each_modeset(mode_set, client)
count++;
mutex_unlock(&client->modeset_mutex);
return count;
}
static void drm_log_init_client(struct drm_log *dlog)
{
struct drm_client_dev *client = &dlog->client;
struct drm_mode_set *mode_set;
int i, max_modeset;
int n_modeset = 0;
dlog->probed = true;
if (drm_client_modeset_probe(client, 0, 0))
return;
max_modeset = drm_log_count_modeset(client);
if (!max_modeset)
return;
dlog->scanout = kcalloc(max_modeset, sizeof(*dlog->scanout), GFP_KERNEL);
if (!dlog->scanout)
return;
mutex_lock(&client->modeset_mutex);
drm_client_for_each_modeset(mode_set, client) {
if (!mode_set->mode)
continue;
if (drm_log_setup_modeset(client, mode_set, &dlog->scanout[n_modeset]))
continue;
n_modeset++;
}
mutex_unlock(&client->modeset_mutex);
if (n_modeset == 0)
goto err_nomodeset;
if (drm_client_modeset_commit(client))
goto err_failed_commit;
dlog->n_scanout = n_modeset;
return;
err_failed_commit:
for (i = 0; i < n_modeset; i++)
drm_client_framebuffer_delete(dlog->scanout[i].buffer);
err_nomodeset:
kfree(dlog->scanout);
dlog->scanout = NULL;
}
static void drm_log_free_scanout(struct drm_client_dev *client)
{
struct drm_log *dlog = client_to_drm_log(client);
int i;
if (dlog->n_scanout) {
for (i = 0; i < dlog->n_scanout; i++)
drm_client_framebuffer_delete(dlog->scanout[i].buffer);
dlog->n_scanout = 0;
kfree(dlog->scanout);
dlog->scanout = NULL;
}
}
static void drm_log_client_unregister(struct drm_client_dev *client)
{
struct drm_log *dlog = client_to_drm_log(client);
struct drm_device *dev = client->dev;
unregister_console(&dlog->con);
mutex_lock(&dlog->lock);
drm_log_free_scanout(client);
drm_client_release(client);
mutex_unlock(&dlog->lock);
kfree(dlog);
drm_dbg(dev, "Unregistered with drm log\n");
}
static int drm_log_client_hotplug(struct drm_client_dev *client)
{
struct drm_log *dlog = client_to_drm_log(client);
mutex_lock(&dlog->lock);
drm_log_free_scanout(client);
dlog->probed = false;
mutex_unlock(&dlog->lock);
return 0;
}
static int drm_log_client_suspend(struct drm_client_dev *client, bool _console_lock)
{
struct drm_log *dlog = client_to_drm_log(client);
console_suspend(&dlog->con);
return 0;
}
static int drm_log_client_resume(struct drm_client_dev *client, bool _console_lock)
{
struct drm_log *dlog = client_to_drm_log(client);
console_resume(&dlog->con);
return 0;
}
static const struct drm_client_funcs drm_log_client_funcs = {
.owner = THIS_MODULE,
.unregister = drm_log_client_unregister,
.hotplug = drm_log_client_hotplug,
.suspend = drm_log_client_suspend,
.resume = drm_log_client_resume,
};
static void drm_log_write_thread(struct console *con, struct nbcon_write_context *wctxt)
{
struct drm_log *dlog = console_to_drm_log(con);
int i;
if (!dlog->probed)
drm_log_init_client(dlog);
/* Check that we are still the master before drawing */
if (drm_master_internal_acquire(dlog->client.dev)) {
drm_master_internal_release(dlog->client.dev);
for (i = 0; i < dlog->n_scanout; i++)
drm_log_draw_kmsg_record(&dlog->scanout[i], wctxt->outbuf, wctxt->len);
}
}
static void drm_log_lock(struct console *con, unsigned long *flags)
{
struct drm_log *dlog = console_to_drm_log(con);
mutex_lock(&dlog->lock);
migrate_disable();
}
static void drm_log_unlock(struct console *con, unsigned long flags)
{
struct drm_log *dlog = console_to_drm_log(con);
migrate_enable();
mutex_unlock(&dlog->lock);
}
static void drm_log_register_console(struct console *con)
{
strscpy(con->name, "drm_log");
con->write_thread = drm_log_write_thread;
con->device_lock = drm_log_lock;
con->device_unlock = drm_log_unlock;
con->flags = CON_PRINTBUFFER | CON_NBCON;
con->index = -1;
register_console(con);
}
/**
* drm_log_register() - Register a drm device to drm_log
* @dev: the drm device to register.
*/
void drm_log_register(struct drm_device *dev)
{
struct drm_log *new;
new = kzalloc(sizeof(*new), GFP_KERNEL);
if (!new)
goto err_warn;
mutex_init(&new->lock);
if (drm_client_init(dev, &new->client, "drm_log", &drm_log_client_funcs))
goto err_free;
drm_client_register(&new->client);
drm_log_register_console(&new->con);
drm_dbg(dev, "Registered with drm log as %s\n", new->con.name);
return;
err_free:
kfree(new);
err_warn:
drm_warn(dev, "Failed to register with drm log\n");
}
|