1 /* $OpenBSD: rkdrm.c,v 1.23 2024/08/21 11:24:12 jsg Exp $ */ 2 /* $NetBSD: rk_drm.c,v 1.3 2019/12/15 01:00:58 mrg Exp $ */ 3 /*- 4 * Copyright (c) 2019 Jared D. McNeill <jmcneill@invisible.ca> 5 * Copyright (c) 2020 Patrick Wildt <patrick@blueri.se> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/device.h> 32 #include <sys/systm.h> 33 34 #include <machine/bus.h> 35 #include <machine/fdt.h> 36 37 #include <dev/ofw/openfirm.h> 38 #include <dev/ofw/ofw_misc.h> 39 40 #include <drm/drm_atomic_helper.h> 41 #include <drm/drm_drv.h> 42 #include <drm/drm_fb_helper.h> 43 #include <drm/drm_gem.h> 44 45 #include <dev/fdt/rkdrm.h> 46 47 #define RK_DRM_MAX_WIDTH 3840 48 #define RK_DRM_MAX_HEIGHT 2160 49 50 int rkdrm_match(struct device *, void *, void *); 51 void rkdrm_attach(struct device *, struct device *, void *); 52 void rkdrm_attachhook(struct device *); 53 54 int rkdrm_unload(struct drm_device *); 55 56 struct drm_driver rkdrm_driver = { 57 .driver_features = DRIVER_ATOMIC | DRIVER_MODESET | DRIVER_GEM, 58 59 .dumb_create = drm_gem_dma_dumb_create, 60 .dumb_map_offset = drm_gem_dumb_map_offset, 61 62 .gem_fault = drm_gem_dma_fault, 63 64 .name = DRIVER_NAME, 65 .desc = DRIVER_DESC, 66 .date = DRIVER_DATE, 67 .major = DRIVER_MAJOR, 68 .minor = DRIVER_MINOR, 69 .patchlevel = DRIVER_PATCHLEVEL, 70 }; 71 72 const struct drm_gem_object_funcs rkdrm_gem_object_funcs = { 73 .free = drm_gem_dma_free_object, 74 }; 75 76 const struct cfattach rkdrm_ca = { 77 sizeof (struct rkdrm_softc), rkdrm_match, rkdrm_attach 78 }; 79 80 struct cfdriver rkdrm_cd = { 81 NULL, "rkdrm", DV_DULL 82 }; 83 84 int 85 rkdrm_match(struct device *parent, void *match, void *aux) 86 { 87 struct fdt_attach_args *faa = aux; 88 89 return OF_is_compatible(faa->fa_node, "rockchip,display-subsystem"); 90 } 91 92 void 93 rkdrm_attach(struct device *parent, struct device *self, void *aux) 94 { 95 struct rkdrm_softc *sc = (struct rkdrm_softc *)self; 96 struct fdt_attach_args *faa = aux; 97 98 sc->sc_dmat = faa->fa_dmat; 99 sc->sc_iot = faa->fa_iot; 100 sc->sc_node = faa->fa_node; 101 102 printf("\n"); 103 104 /* 105 * Update our understanding of the console output node if 106 * we're using the framebuffer console. 107 */ 108 if (OF_is_compatible(stdout_node, "simple-framebuffer")) 109 stdout_node = sc->sc_node; 110 111 drm_attach_platform(&rkdrm_driver, faa->fa_iot, faa->fa_dmat, self, 112 &sc->sc_ddev); 113 config_mountroot(self, rkdrm_attachhook); 114 } 115 116 int 117 rkdrm_fb_create_handle(struct drm_framebuffer *fb, 118 struct drm_file *file, unsigned int *handle) 119 { 120 struct rkdrm_framebuffer *sfb = to_rkdrm_framebuffer(fb); 121 122 return drm_gem_handle_create(file, &sfb->obj->base, handle); 123 } 124 125 void 126 rkdrm_fb_destroy(struct drm_framebuffer *fb) 127 { 128 struct rkdrm_framebuffer *sfb = to_rkdrm_framebuffer(fb); 129 130 drm_framebuffer_cleanup(fb); 131 drm_gem_object_put(&sfb->obj->base); 132 free(sfb, M_DRM, sizeof(*sfb)); 133 } 134 135 struct drm_framebuffer_funcs rkdrm_framebuffer_funcs = { 136 .create_handle = rkdrm_fb_create_handle, 137 .destroy = rkdrm_fb_destroy, 138 }; 139 140 struct drm_framebuffer * 141 rkdrm_fb_create(struct drm_device *ddev, struct drm_file *file, 142 const struct drm_mode_fb_cmd2 *cmd) 143 { 144 struct rkdrm_framebuffer *fb; 145 struct drm_gem_object *gem_obj; 146 int error; 147 148 if (cmd->flags) 149 return NULL; 150 151 gem_obj = drm_gem_object_lookup(file, cmd->handles[0]); 152 if (gem_obj == NULL) 153 return NULL; 154 155 fb = malloc(sizeof(*fb), M_DRM, M_ZERO | M_WAITOK); 156 drm_helper_mode_fill_fb_struct(ddev, &fb->base, cmd); 157 fb->base.format = drm_format_info(DRM_FORMAT_ARGB8888); 158 fb->base.obj[0] = gem_obj; 159 fb->obj = to_drm_gem_dma_obj(gem_obj); 160 161 error = drm_framebuffer_init(ddev, &fb->base, &rkdrm_framebuffer_funcs); 162 if (error != 0) 163 goto dealloc; 164 165 return &fb->base; 166 167 dealloc: 168 drm_framebuffer_cleanup(&fb->base); 169 free(fb, M_DRM, sizeof(*fb)); 170 drm_gem_object_put(gem_obj); 171 172 return NULL; 173 } 174 175 struct drm_mode_config_helper_funcs rkdrm_mode_config_helper_funcs = 176 { 177 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, 178 }; 179 180 struct drm_mode_config_funcs rkdrm_mode_config_funcs = { 181 .fb_create = rkdrm_fb_create, 182 .atomic_check = drm_atomic_helper_check, 183 .atomic_commit = drm_atomic_helper_commit, 184 }; 185 186 int rkdrm_fb_probe(struct drm_fb_helper *, struct drm_fb_helper_surface_size *); 187 188 struct drm_fb_helper_funcs rkdrm_fb_helper_funcs = { 189 .fb_probe = rkdrm_fb_probe, 190 }; 191 192 int 193 rkdrm_unload(struct drm_device *ddev) 194 { 195 drm_mode_config_cleanup(ddev); 196 197 return 0; 198 } 199 200 void rkdrm_burner(void *, u_int, u_int); 201 int rkdrm_wsioctl(void *, u_long, caddr_t, int, struct proc *); 202 paddr_t rkdrm_wsmmap(void *, off_t, int); 203 int rkdrm_alloc_screen(void *, const struct wsscreen_descr *, 204 void **, int *, int *, uint32_t *); 205 void rkdrm_free_screen(void *, void *); 206 int rkdrm_show_screen(void *, void *, int, 207 void (*)(void *, int, int), void *); 208 void rkdrm_doswitch(void *); 209 void rkdrm_enter_ddb(void *, void *); 210 211 struct wsscreen_descr rkdrm_stdscreen = { 212 "std", 213 0, 0, 214 0, 215 0, 0, 216 WSSCREEN_UNDERLINE | WSSCREEN_HILIT | 217 WSSCREEN_REVERSE | WSSCREEN_WSCOLORS 218 }; 219 220 const struct wsscreen_descr *rkdrm_scrlist[] = { 221 &rkdrm_stdscreen, 222 }; 223 224 struct wsscreen_list rkdrm_screenlist = { 225 nitems(rkdrm_scrlist), rkdrm_scrlist 226 }; 227 228 struct wsdisplay_accessops rkdrm_accessops = { 229 .ioctl = rkdrm_wsioctl, 230 .mmap = rkdrm_wsmmap, 231 .alloc_screen = rkdrm_alloc_screen, 232 .free_screen = rkdrm_free_screen, 233 .show_screen = rkdrm_show_screen, 234 .enter_ddb = rkdrm_enter_ddb, 235 .getchar = rasops_getchar, 236 .load_font = rasops_load_font, 237 .list_font = rasops_list_font, 238 .scrollback = rasops_scrollback, 239 #ifdef notyet 240 .burn_screen = rkdrm_burner 241 #endif 242 }; 243 244 int 245 rkdrm_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 246 { 247 struct rasops_info *ri = v; 248 struct wsdisplay_param *dp = (struct wsdisplay_param *)data; 249 struct wsdisplay_fbinfo *wdf; 250 251 switch (cmd) { 252 case WSDISPLAYIO_GETPARAM: 253 if (ws_get_param) 254 return ws_get_param(dp); 255 return -1; 256 case WSDISPLAYIO_SETPARAM: 257 if (ws_set_param) 258 return ws_set_param(dp); 259 return -1; 260 case WSDISPLAYIO_GTYPE: 261 *(u_int *)data = WSDISPLAY_TYPE_KMS; 262 return 0; 263 case WSDISPLAYIO_GINFO: 264 wdf = (struct wsdisplay_fbinfo *)data; 265 wdf->width = ri->ri_width; 266 wdf->height = ri->ri_height; 267 wdf->depth = ri->ri_depth; 268 wdf->stride = ri->ri_stride; 269 wdf->offset = 0; 270 wdf->cmsize = 0; 271 return 0; 272 case WSDISPLAYIO_LINEBYTES: 273 *(u_int *)data = ri->ri_stride; 274 return 0; 275 case WSDISPLAYIO_SVIDEO: 276 case WSDISPLAYIO_GVIDEO: 277 return 0; 278 } 279 280 return (-1); 281 } 282 283 paddr_t 284 rkdrm_wsmmap(void *v, off_t off, int prot) 285 { 286 struct rasops_info *ri = v; 287 struct rkdrm_softc *sc = ri->ri_hw; 288 struct drm_fb_helper *helper = &sc->helper; 289 struct rkdrm_framebuffer *sfb = to_rkdrm_framebuffer(helper->fb); 290 uint64_t paddr = (uint64_t)sfb->obj->dmamap->dm_segs[0].ds_addr; 291 size_t size = sfb->obj->dmamap->dm_segs[0].ds_len; 292 293 if (off < 0 || off >= size) 294 return -1; 295 296 return ((paddr + off) | PMAP_NOCACHE); 297 } 298 299 int 300 rkdrm_alloc_screen(void *v, const struct wsscreen_descr *type, 301 void **cookiep, int *curxp, int *curyp, uint32_t *attrp) 302 { 303 return rasops_alloc_screen(v, cookiep, curxp, curyp, attrp); 304 } 305 306 void 307 rkdrm_free_screen(void *v, void *cookie) 308 { 309 return rasops_free_screen(v, cookie); 310 } 311 312 int 313 rkdrm_show_screen(void *v, void *cookie, int waitok, 314 void (*cb)(void *, int, int), void *cbarg) 315 { 316 struct rasops_info *ri = v; 317 struct rkdrm_softc *sc = ri->ri_hw; 318 319 if (cookie == ri->ri_active) 320 return (0); 321 322 sc->switchcb = cb; 323 sc->switchcbarg = cbarg; 324 sc->switchcookie = cookie; 325 if (cb) { 326 task_add(systq, &sc->switchtask); 327 return (EAGAIN); 328 } 329 330 rkdrm_doswitch(v); 331 332 return (0); 333 } 334 335 void 336 rkdrm_doswitch(void *v) 337 { 338 struct rasops_info *ri = v; 339 struct rkdrm_softc *sc = ri->ri_hw; 340 341 rasops_show_screen(ri, sc->switchcookie, 0, NULL, NULL); 342 drm_fb_helper_restore_fbdev_mode_unlocked(&sc->helper); 343 344 if (sc->switchcb) 345 (sc->switchcb)(sc->switchcbarg, 0, 0); 346 } 347 348 void 349 rkdrm_enter_ddb(void *v, void *cookie) 350 { 351 struct rasops_info *ri = v; 352 struct rkdrm_softc *sc = ri->ri_hw; 353 struct drm_fb_helper *fb_helper = &sc->helper; 354 355 if (cookie == ri->ri_active) 356 return; 357 358 rasops_show_screen(ri, cookie, 0, NULL, NULL); 359 drm_fb_helper_debug_enter(fb_helper->info); 360 } 361 362 void 363 rkdrm_attachhook(struct device *dev) 364 { 365 struct rkdrm_softc *sc = (struct rkdrm_softc *)dev; 366 struct wsemuldisplaydev_attach_args aa; 367 struct drm_fb_helper *helper = &sc->helper; 368 struct rasops_info *ri = &sc->ro; 369 struct rkdrm_framebuffer *sfb; 370 uint32_t *ports; 371 int i, portslen, nports; 372 int console = 0; 373 uint32_t defattr; 374 int error; 375 376 if (sc->sc_node == stdout_node) 377 console = 1; 378 379 portslen = OF_getproplen(sc->sc_node, "ports"); 380 if (portslen < 0) { 381 printf("%s: no display interface ports specified\n", 382 sc->sc_dev.dv_xname); 383 return; 384 } 385 386 drm_mode_config_init(&sc->sc_ddev); 387 sc->sc_ddev.mode_config.min_width = 0; 388 sc->sc_ddev.mode_config.min_height = 0; 389 sc->sc_ddev.mode_config.max_width = RK_DRM_MAX_WIDTH; 390 sc->sc_ddev.mode_config.max_height = RK_DRM_MAX_HEIGHT; 391 sc->sc_ddev.mode_config.funcs = &rkdrm_mode_config_funcs; 392 sc->sc_ddev.mode_config.helper_private = 393 &rkdrm_mode_config_helper_funcs; 394 395 nports = 0; 396 ports = malloc(portslen, M_TEMP, M_WAITOK); 397 OF_getpropintarray(sc->sc_node, "ports", ports, portslen); 398 for (i = 0; i < portslen / sizeof(uint32_t); i++) { 399 error = device_port_activate(ports[i], &sc->sc_ddev); 400 if (error == 0) 401 nports++; 402 } 403 free(ports, M_TEMP, portslen); 404 405 if (nports == 0) { 406 printf("%s: no display interface ports configured\n", 407 sc->sc_dev.dv_xname); 408 drm_mode_config_cleanup(&sc->sc_ddev); 409 return; 410 } 411 412 drm_mode_config_reset(&sc->sc_ddev); 413 414 drm_fb_helper_prepare(&sc->sc_ddev, &sc->helper, 32, 415 &rkdrm_fb_helper_funcs); 416 if (drm_fb_helper_init(&sc->sc_ddev, &sc->helper)) { 417 printf("%s: can't initialize framebuffer helper\n", 418 sc->sc_dev.dv_xname); 419 drm_mode_config_cleanup(&sc->sc_ddev); 420 return; 421 } 422 423 sc->helper.fb = malloc(sizeof(struct rkdrm_framebuffer), 424 M_DRM, M_WAITOK | M_ZERO); 425 426 drm_fb_helper_initial_config(&sc->helper); 427 428 task_set(&sc->switchtask, rkdrm_doswitch, ri); 429 430 drm_fb_helper_restore_fbdev_mode_unlocked(&sc->helper); 431 432 sfb = to_rkdrm_framebuffer(helper->fb); 433 ri->ri_bits = sfb->obj->vaddr; 434 ri->ri_flg = RI_CENTER | RI_VCONS; 435 ri->ri_depth = helper->fb->format->depth; 436 ri->ri_width = helper->fb->width; 437 ri->ri_height = helper->fb->height; 438 ri->ri_stride = ri->ri_width * ri->ri_depth / 8; 439 ri->ri_rnum = 8; /* ARGB8888 */ 440 ri->ri_rpos = 16; 441 ri->ri_gnum = 8; 442 ri->ri_gpos = 8; 443 ri->ri_bnum = 8; 444 ri->ri_bpos = 0; 445 rasops_init(ri, 160, 160); 446 ri->ri_hw = sc; 447 448 rkdrm_stdscreen.capabilities = ri->ri_caps; 449 rkdrm_stdscreen.nrows = ri->ri_rows; 450 rkdrm_stdscreen.ncols = ri->ri_cols; 451 rkdrm_stdscreen.textops = &ri->ri_ops; 452 rkdrm_stdscreen.fontwidth = ri->ri_font->fontwidth; 453 rkdrm_stdscreen.fontheight = ri->ri_font->fontheight; 454 455 if (console) { 456 ri->ri_ops.pack_attr(ri->ri_active, 0, 0, 0, &defattr); 457 wsdisplay_cnattach(&rkdrm_stdscreen, ri->ri_active, 458 ri->ri_ccol, ri->ri_crow, defattr); 459 } 460 461 memset(&aa, 0, sizeof(aa)); 462 aa.scrdata = &rkdrm_screenlist; 463 aa.accessops = &rkdrm_accessops; 464 aa.accesscookie = ri; 465 aa.console = console; 466 467 printf("%s: %dx%d, %dbpp\n", sc->sc_dev.dv_xname, 468 ri->ri_width, ri->ri_height, ri->ri_depth); 469 470 config_found_sm(&sc->sc_dev, &aa, wsemuldisplaydevprint, 471 wsemuldisplaydevsubmatch); 472 473 drm_dev_register(&sc->sc_ddev, 0); 474 } 475 476 int 477 rkdrm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes) 478 { 479 struct drm_device *ddev = helper->dev; 480 struct rkdrm_framebuffer *sfb = to_rkdrm_framebuffer(helper->fb); 481 struct drm_mode_fb_cmd2 mode_cmd = { 0 }; 482 struct drm_framebuffer *fb = helper->fb; 483 unsigned int bytes_per_pixel; 484 struct fb_info *info; 485 size_t size; 486 int error; 487 488 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8); 489 490 mode_cmd.width = sizes->surface_width; 491 mode_cmd.height = sizes->surface_height; 492 mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel; 493 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 494 sizes->surface_depth); 495 496 size = roundup(mode_cmd.pitches[0] * mode_cmd.height, PAGE_SIZE); 497 498 /* FIXME: CMA pool? */ 499 500 sfb->obj = drm_gem_dma_create(ddev, size); 501 if (sfb->obj == NULL) { 502 DRM_ERROR("failed to allocate memory for framebuffer\n"); 503 return -ENOMEM; 504 } 505 506 drm_helper_mode_fill_fb_struct(ddev, fb, &mode_cmd); 507 fb->format = drm_format_info(DRM_FORMAT_ARGB8888); 508 fb->obj[0] = &sfb->obj->base; 509 error = drm_framebuffer_init(ddev, fb, &rkdrm_framebuffer_funcs); 510 if (error != 0) { 511 DRM_ERROR("failed to initialize framebuffer\n"); 512 return error; 513 } 514 515 info = drm_fb_helper_alloc_info(helper); 516 if (IS_ERR(info)) { 517 DRM_ERROR("Failed to allocate fb_info\n"); 518 return PTR_ERR(info); 519 } 520 info->par = helper; 521 return 0; 522 } 523