rk_drm.c revision 1.20 1 /* $NetBSD: rk_drm.c,v 1.20 2022/09/25 07:50:15 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: rk_drm.c,v 1.20 2022/09/25 07:50:15 riastradh Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/device.h>
36 #include <sys/intr.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39
40 #include <uvm/uvm_device.h>
41 #include <uvm/uvm_extern.h>
42 #include <uvm/uvm_object.h>
43
44 #include <dev/fdt/fdt_port.h>
45 #include <dev/fdt/fdtvar.h>
46
47 #include <arm/rockchip/rk_drm.h>
48
49 #include <drm/drm_atomic_helper.h>
50 #include <drm/drm_auth.h>
51 #include <drm/drm_crtc_helper.h>
52 #include <drm/drm_damage_helper.h>
53 #include <drm/drm_drv.h>
54 #include <drm/drm_fb_helper.h>
55 #include <drm/drm_fourcc.h>
56 #include <drm/drm_vblank.h>
57
58 #define RK_DRM_MAX_WIDTH 3840
59 #define RK_DRM_MAX_HEIGHT 2160
60
61 static TAILQ_HEAD(, rk_drm_ports) rk_drm_ports =
62 TAILQ_HEAD_INITIALIZER(rk_drm_ports);
63
64 static const struct device_compatible_entry compat_data[] = {
65 { .compat = "rockchip,display-subsystem" },
66 DEVICE_COMPAT_EOL
67 };
68
69 static const char * fb_compatible[] = {
70 "simple-framebuffer",
71 NULL
72 };
73
74 static int rk_drm_match(device_t, cfdata_t, void *);
75 static void rk_drm_attach(device_t, device_t, void *);
76
77 static void rk_drm_init(device_t);
78 static vmem_t *rk_drm_alloc_cma_pool(struct drm_device *, size_t);
79
80 static int rk_drm_load(struct drm_device *, unsigned long);
81 static void rk_drm_unload(struct drm_device *);
82
83 static void rk_drm_task_work(struct work *, void *);
84
85 static struct drm_driver rk_drm_driver = {
86 .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
87 .dev_priv_size = 0,
88 .load = rk_drm_load,
89 .unload = rk_drm_unload,
90
91 .gem_free_object = drm_gem_cma_free_object,
92 .mmap_object = drm_gem_or_legacy_mmap_object,
93 .gem_uvm_ops = &drm_gem_cma_uvm_ops,
94
95 .dumb_create = drm_gem_cma_dumb_create,
96 .dumb_destroy = drm_gem_dumb_destroy,
97
98 .name = DRIVER_NAME,
99 .desc = DRIVER_DESC,
100 .date = DRIVER_DATE,
101 .major = DRIVER_MAJOR,
102 .minor = DRIVER_MINOR,
103 .patchlevel = DRIVER_PATCHLEVEL,
104 };
105
106 CFATTACH_DECL_NEW(rk_drm, sizeof(struct rk_drm_softc),
107 rk_drm_match, rk_drm_attach, NULL, NULL);
108
109 static int
110 rk_drm_match(device_t parent, cfdata_t cf, void *aux)
111 {
112 struct fdt_attach_args * const faa = aux;
113
114 return of_compatible_match(faa->faa_phandle, compat_data);
115 }
116
117 static void
118 rk_drm_attach(device_t parent, device_t self, void *aux)
119 {
120 struct rk_drm_softc * const sc = device_private(self);
121 struct fdt_attach_args * const faa = aux;
122 struct drm_driver * const driver = &rk_drm_driver;
123 prop_dictionary_t dict = device_properties(self);
124 bool is_disabled;
125
126 aprint_naive("\n");
127
128 if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) &&
129 is_disabled) {
130 aprint_normal(": (disabled)\n");
131 return;
132 }
133
134 aprint_normal("\n");
135
136 #ifdef WSDISPLAY_MULTICONS
137 const bool is_console = true;
138 prop_dictionary_set_bool(dict, "is_console", is_console);
139 #endif
140
141 sc->sc_dev = self;
142 sc->sc_dmat = faa->faa_dmat;
143 sc->sc_bst = faa->faa_bst;
144 sc->sc_phandle = faa->faa_phandle;
145 sc->sc_task_thread = NULL;
146 SIMPLEQ_INIT(&sc->sc_tasks);
147 if (workqueue_create(&sc->sc_task_wq, "rkdrm",
148 &rk_drm_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE)) {
149 aprint_error_dev(self, "unable to create workqueue\n");
150 sc->sc_task_wq = NULL;
151 return;
152 }
153
154 sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
155 if (IS_ERR(sc->sc_ddev)) {
156 aprint_error_dev(self, "couldn't allocate DRM device\n");
157 return;
158 }
159 sc->sc_ddev->dev_private = sc;
160 sc->sc_ddev->bst = sc->sc_bst;
161 sc->sc_ddev->bus_dmat = sc->sc_dmat;
162 sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
163 sc->sc_ddev->dmat_subregion_p = false;
164
165 fdt_remove_bycompat(fb_compatible);
166
167 /*
168 * Wait until rk_vop is attached as a sibling to this device --
169 * we need that to actually display our framebuffer.
170 */
171 config_defer(self, rk_drm_init);
172 }
173
174 static void
175 rk_drm_init(device_t dev)
176 {
177 struct rk_drm_softc * const sc = device_private(dev);
178 struct drm_driver * const driver = &rk_drm_driver;
179 int error;
180
181 /*
182 * Cause any tasks issued synchronously during attach to be
183 * processed at the end of this function.
184 */
185 sc->sc_task_thread = curlwp;
186
187 error = -drm_dev_register(sc->sc_ddev, 0);
188 if (error) {
189 aprint_error_dev(dev, "couldn't register DRM device: %d\n",
190 error);
191 goto out;
192 }
193 sc->sc_dev_registered = true;
194
195 aprint_normal_dev(dev, "initialized %s %d.%d.%d %s on minor %d\n",
196 driver->name, driver->major, driver->minor, driver->patchlevel,
197 driver->date, sc->sc_ddev->primary->index);
198
199 /*
200 * Process asynchronous tasks queued synchronously during
201 * attach. This will be for display detection to attach a
202 * framebuffer, so we have the opportunity for a console device
203 * to attach before autoconf has completed, in time for init(8)
204 * to find that console without panicking.
205 */
206 while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
207 struct rk_drm_task *const task = SIMPLEQ_FIRST(&sc->sc_tasks);
208
209 SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, rdt_u.queue);
210 (*task->rdt_fn)(task);
211 }
212
213 out: /* Cause any subsequent tasks to be processed by the workqueue. */
214 atomic_store_relaxed(&sc->sc_task_thread, NULL);
215 }
216
217 static vmem_t *
218 rk_drm_alloc_cma_pool(struct drm_device *ddev, size_t cma_size)
219 {
220 struct rk_drm_softc * const sc = rk_drm_private(ddev);
221 bus_dma_segment_t segs[1];
222 int nsegs;
223 int error;
224
225 error = bus_dmamem_alloc(sc->sc_dmat, cma_size, PAGE_SIZE, 0,
226 segs, 1, &nsegs, BUS_DMA_NOWAIT);
227 if (error) {
228 aprint_error_dev(sc->sc_dev, "couldn't allocate CMA pool\n");
229 return NULL;
230 }
231
232 return vmem_create("rkdrm", segs[0].ds_addr, segs[0].ds_len,
233 PAGE_SIZE, NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE);
234 }
235
236 static int
237 rk_drm_fb_create_handle(struct drm_framebuffer *fb,
238 struct drm_file *file, unsigned int *handle)
239 {
240 struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(fb);
241
242 return drm_gem_handle_create(file, &sfb->obj->base, handle);
243 }
244
245 static void
246 rk_drm_fb_destroy(struct drm_framebuffer *fb)
247 {
248 struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(fb);
249
250 drm_framebuffer_cleanup(fb);
251 drm_gem_object_put_unlocked(&sfb->obj->base);
252 kmem_free(sfb, sizeof(*sfb));
253 }
254
255 static const struct drm_framebuffer_funcs rk_drm_framebuffer_funcs = {
256 .create_handle = rk_drm_fb_create_handle,
257 .destroy = rk_drm_fb_destroy,
258 .dirty = drm_atomic_helper_dirtyfb,
259 };
260
261 static struct drm_framebuffer *
262 rk_drm_fb_create(struct drm_device *ddev, struct drm_file *file,
263 const struct drm_mode_fb_cmd2 *cmd)
264 {
265 struct rk_drm_framebuffer *fb;
266 struct drm_gem_object *gem_obj;
267 int error;
268
269 if (cmd->flags)
270 return NULL;
271
272 gem_obj = drm_gem_object_lookup(file, cmd->handles[0]);
273 if (gem_obj == NULL)
274 return NULL;
275
276 fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
277 drm_helper_mode_fill_fb_struct(ddev, &fb->base, cmd);
278 fb->obj = to_drm_gem_cma_obj(gem_obj);
279
280 error = drm_framebuffer_init(ddev, &fb->base, &rk_drm_framebuffer_funcs);
281 if (error != 0)
282 goto dealloc;
283
284 return &fb->base;
285
286 dealloc:
287 drm_framebuffer_cleanup(&fb->base);
288 kmem_free(fb, sizeof(*fb));
289 drm_gem_object_put_unlocked(gem_obj);
290
291 return NULL;
292 }
293
294 static struct drm_mode_config_funcs rk_drm_mode_config_funcs = {
295 .fb_create = rk_drm_fb_create,
296 .atomic_check = drm_atomic_helper_check,
297 .atomic_commit = drm_atomic_helper_commit,
298 };
299
300 static struct drm_mode_config_helper_funcs rk_drm_mode_config_helper_funcs = {
301 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
302 };
303
304 static int
305 rk_drm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
306 {
307 struct rk_drm_softc * const sc = rk_drm_private(helper->dev);
308 struct drm_device *ddev = helper->dev;
309 struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(helper->fb);
310 struct drm_framebuffer *fb = helper->fb;
311 struct rk_drmfb_attach_args sfa;
312 size_t cma_size;
313 int error;
314
315 const u_int width = sizes->surface_width;
316 const u_int height = sizes->surface_height;
317 const u_int pitch = width * (32 / 8);
318
319 const size_t size = roundup(height * pitch, PAGE_SIZE);
320
321 /* Reserve enough memory for the FB console plus a 4K plane, rounded to 1MB */
322 cma_size = size;
323 cma_size += (RK_DRM_MAX_WIDTH * RK_DRM_MAX_HEIGHT * 4);
324 cma_size = roundup(cma_size, 1024 * 1024);
325 sc->sc_ddev->cma_pool = rk_drm_alloc_cma_pool(sc->sc_ddev, cma_size);
326 if (sc->sc_ddev->cma_pool != NULL)
327 aprint_normal_dev(sc->sc_dev, "reserved %u MB DRAM for CMA\n",
328 (u_int)(cma_size / (1024 * 1024)));
329
330 sfb->obj = drm_gem_cma_create(ddev, size);
331 if (sfb->obj == NULL) {
332 DRM_ERROR("failed to allocate memory for framebuffer\n");
333 return -ENOMEM;
334 }
335
336 /* similar to drm_helper_mode_fill_fb_struct(), but we have no cmd */
337 fb->pitches[0] = pitch;
338 fb->offsets[0] = 0;
339 fb->width = width;
340 fb->height = height;
341 fb->modifier = 0;
342 fb->flags = 0;
343 #ifdef __ARM_BIG_ENDIAN
344 fb->format = drm_format_info(DRM_FORMAT_BGRX8888);
345 #else
346 fb->format = drm_format_info(DRM_FORMAT_XRGB8888);
347 #endif
348 fb->dev = ddev;
349
350 error = drm_framebuffer_init(ddev, fb, &rk_drm_framebuffer_funcs);
351 if (error != 0) {
352 DRM_ERROR("failed to initialize framebuffer\n");
353 return error;
354 }
355
356 memset(&sfa, 0, sizeof(sfa));
357 sfa.sfa_drm_dev = ddev;
358 sfa.sfa_fb_helper = helper;
359 sfa.sfa_fb_sizes = *sizes;
360 sfa.sfa_fb_bst = sc->sc_bst;
361 sfa.sfa_fb_dmat = sc->sc_dmat;
362 sfa.sfa_fb_linebytes = helper->fb->pitches[0];
363
364 helper->fbdev = config_found(ddev->dev, &sfa, NULL,
365 CFARGS(.iattr = "rkfbbus"));
366 if (helper->fbdev == NULL) {
367 DRM_ERROR("unable to attach framebuffer\n");
368 return -ENXIO;
369 }
370
371 return 0;
372 }
373
374 static struct drm_fb_helper_funcs rk_drm_fb_helper_funcs = {
375 .fb_probe = rk_drm_fb_probe,
376 };
377
378 static int
379 rk_drm_load(struct drm_device *ddev, unsigned long flags)
380 {
381 struct rk_drm_softc * const sc = rk_drm_private(ddev);
382 struct rk_drm_ports *sport;
383 struct rk_drm_fbdev *fbdev;
384 struct fdt_endpoint *ep;
385 const u_int *data;
386 int datalen, error, num_crtc, ep_index;
387
388 drm_mode_config_init(ddev);
389 ddev->mode_config.min_width = 0;
390 ddev->mode_config.min_height = 0;
391 ddev->mode_config.max_width = RK_DRM_MAX_WIDTH;
392 ddev->mode_config.max_height = RK_DRM_MAX_HEIGHT;
393 ddev->mode_config.funcs = &rk_drm_mode_config_funcs;
394 ddev->mode_config.helper_private = &rk_drm_mode_config_helper_funcs;
395
396 num_crtc = 0;
397 data = fdtbus_get_prop(sc->sc_phandle, "ports", &datalen);
398 while (datalen >= 4) {
399 const int crtc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
400
401 TAILQ_FOREACH(sport, &rk_drm_ports, entries)
402 if (sport->phandle == crtc_phandle && sport->ddev == NULL) {
403 sport->ddev = ddev;
404 for (ep_index = 0; (ep = fdt_endpoint_get_from_index(sport->port, 0, ep_index)) != NULL; ep_index++) {
405 error = fdt_endpoint_activate_direct(ep, true);
406 if (error != 0)
407 aprint_debug_dev(sc->sc_dev,
408 "failed to activate endpoint %d: %d\n",
409 ep_index, error);
410 }
411 num_crtc++;
412 }
413
414 datalen -= 4;
415 data++;
416 }
417
418 if (num_crtc == 0) {
419 aprint_error_dev(sc->sc_dev, "no display interface ports configured\n");
420 error = ENXIO;
421 goto drmerr;
422 }
423
424 drm_mode_config_reset(ddev);
425
426 fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
427
428 drm_fb_helper_prepare(ddev, &fbdev->helper, &rk_drm_fb_helper_funcs);
429
430 error = drm_fb_helper_init(ddev, &fbdev->helper, num_crtc);
431 if (error)
432 goto allocerr;
433
434 fbdev->helper.fb = kmem_zalloc(sizeof(struct rk_drm_framebuffer), KM_SLEEP);
435
436 drm_fb_helper_single_add_all_connectors(&fbdev->helper);
437
438 drm_fb_helper_initial_config(&fbdev->helper, 32);
439
440 /* XXX Delegate this to rk_vop.c? */
441 ddev->irq_enabled = true;
442 drm_vblank_init(ddev, num_crtc);
443
444 return 0;
445
446 allocerr:
447 kmem_free(fbdev, sizeof(*fbdev));
448 drmerr:
449 drm_mode_config_cleanup(ddev);
450
451 return error;
452 }
453
454 static void
455 rk_drm_unload(struct drm_device *ddev)
456 {
457 drm_mode_config_cleanup(ddev);
458 }
459
460 int
461 rk_drm_register_port(int phandle, struct fdt_device_ports *port)
462 {
463 struct rk_drm_ports *sport;
464
465 sport = kmem_zalloc(sizeof(*sport), KM_SLEEP);
466 sport->phandle = phandle;
467 sport->port = port;
468 sport->ddev = NULL;
469 TAILQ_INSERT_TAIL(&rk_drm_ports, sport, entries);
470
471 return 0;
472 }
473
474 struct drm_device *
475 rk_drm_port_device(struct fdt_device_ports *port)
476 {
477 struct rk_drm_ports *sport;
478
479 TAILQ_FOREACH(sport, &rk_drm_ports, entries)
480 if (sport->port == port)
481 return sport->ddev;
482
483 return NULL;
484 }
485
486 static void
487 rk_drm_task_work(struct work *work, void *cookie)
488 {
489 struct rk_drm_task *task = container_of(work, struct rk_drm_task,
490 rdt_u.work);
491
492 (*task->rdt_fn)(task);
493 }
494
495 void
496 rk_task_init(struct rk_drm_task *task,
497 void (*fn)(struct rk_drm_task *))
498 {
499
500 task->rdt_fn = fn;
501 }
502
503 void
504 rk_task_schedule(device_t self, struct rk_drm_task *task)
505 {
506 struct rk_drm_softc *sc = device_private(self);
507
508 if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
509 SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, rdt_u.queue);
510 else
511 workqueue_enqueue(sc->sc_task_wq, &task->rdt_u.work, NULL);
512 }
513