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