sunxi_drm.c revision 1.22 1 /* $NetBSD: sunxi_drm.c,v 1.22 2021/12/19 12:28:20 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: sunxi_drm.c,v 1.22 2021/12/19 12:28:20 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/sunxi/sunxi_drm.h>
48
49 #include <drm/drm_auth.h>
50 #include <drm/drm_crtc_helper.h>
51 #include <drm/drm_drv.h>
52 #include <drm/drm_fb_helper.h>
53 #include <drm/drm_fourcc.h>
54 #include <drm/drm_vblank.h>
55
56 #define SUNXI_DRM_MAX_WIDTH 3840
57 #define SUNXI_DRM_MAX_HEIGHT 2160
58
59 /*
60 * The DRM headers break trunc_page/round_page macros with a redefinition
61 * of PAGE_MASK. Use our own macros instead.
62 */
63 #define SUNXI_PAGE_MASK (PAGE_SIZE - 1)
64 #define SUNXI_TRUNC_PAGE(x) ((x) & ~SUNXI_PAGE_MASK)
65 #define SUNXI_ROUND_PAGE(x) (((x) + SUNXI_PAGE_MASK) & ~SUNXI_PAGE_MASK)
66
67 static TAILQ_HEAD(, sunxi_drm_endpoint) sunxi_drm_endpoints =
68 TAILQ_HEAD_INITIALIZER(sunxi_drm_endpoints);
69
70 static const struct device_compatible_entry compat_data[] = {
71 { .compat = "allwinner,sun8i-h3-display-engine" },
72 { .compat = "allwinner,sun50i-a64-display-engine" },
73 DEVICE_COMPAT_EOL
74 };
75
76 static const char * fb_compatible[] = {
77 "allwinner,simple-framebuffer",
78 NULL
79 };
80
81 static int sunxi_drm_match(device_t, cfdata_t, void *);
82 static void sunxi_drm_attach(device_t, device_t, void *);
83
84 static void sunxi_drm_init(device_t);
85 static vmem_t *sunxi_drm_alloc_cma_pool(struct drm_device *, size_t);
86
87 static uint32_t sunxi_drm_get_vblank_counter(struct drm_device *, unsigned int);
88 static int sunxi_drm_enable_vblank(struct drm_device *, unsigned int);
89 static void sunxi_drm_disable_vblank(struct drm_device *, unsigned int);
90
91 static int sunxi_drm_load(struct drm_device *, unsigned long);
92 static void sunxi_drm_unload(struct drm_device *);
93
94 static void sunxi_drm_task_work(struct work *, void *);
95
96 static struct drm_driver sunxi_drm_driver = {
97 .driver_features = DRIVER_MODESET | DRIVER_GEM,
98 .dev_priv_size = 0,
99 .load = sunxi_drm_load,
100 .unload = sunxi_drm_unload,
101
102 .gem_free_object = drm_gem_cma_free_object,
103 .mmap_object = drm_gem_or_legacy_mmap_object,
104 .gem_uvm_ops = &drm_gem_cma_uvm_ops,
105
106 .dumb_create = drm_gem_cma_dumb_create,
107 .dumb_destroy = drm_gem_dumb_destroy,
108
109 .get_vblank_counter = sunxi_drm_get_vblank_counter,
110 .enable_vblank = sunxi_drm_enable_vblank,
111 .disable_vblank = sunxi_drm_disable_vblank,
112
113 .name = DRIVER_NAME,
114 .desc = DRIVER_DESC,
115 .date = DRIVER_DATE,
116 .major = DRIVER_MAJOR,
117 .minor = DRIVER_MINOR,
118 .patchlevel = DRIVER_PATCHLEVEL,
119 };
120
121 CFATTACH_DECL_NEW(sunxi_drm, sizeof(struct sunxi_drm_softc),
122 sunxi_drm_match, sunxi_drm_attach, NULL, NULL);
123
124 static int
125 sunxi_drm_match(device_t parent, cfdata_t cf, void *aux)
126 {
127 struct fdt_attach_args * const faa = aux;
128
129 return of_compatible_match(faa->faa_phandle, compat_data);
130 }
131
132 static void
133 sunxi_drm_attach(device_t parent, device_t self, void *aux)
134 {
135 struct sunxi_drm_softc * const sc = device_private(self);
136 struct fdt_attach_args * const faa = aux;
137 struct drm_driver * const driver = &sunxi_drm_driver;
138 prop_dictionary_t dict = device_properties(self);
139 bool is_disabled;
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, "sunxidrm",
148 &sunxi_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 aprint_naive("\n");
155
156 if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
157 aprint_normal(": Display Engine Pipeline (disabled)\n");
158 return;
159 }
160
161 aprint_normal(": Display Engine Pipeline\n");
162
163 sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
164 if (IS_ERR(sc->sc_ddev)) {
165 aprint_error_dev(self, "couldn't allocate DRM device\n");
166 return;
167 }
168 sc->sc_ddev->dev_private = sc;
169 sc->sc_ddev->bst = sc->sc_bst;
170 sc->sc_ddev->bus_dmat = sc->sc_dmat;
171 sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
172 sc->sc_ddev->dmat_subregion_p = false;
173
174 fdt_remove_bycompat(fb_compatible);
175
176 config_defer(self, sunxi_drm_init);
177 }
178
179 static void
180 sunxi_drm_init(device_t dev)
181 {
182 struct sunxi_drm_softc * const sc = device_private(dev);
183 struct drm_driver * const driver = &sunxi_drm_driver;
184 int error;
185
186 /*
187 * Cause any tasks issued synchronously during attach to be
188 * processed at the end of this function.
189 */
190 sc->sc_task_thread = curlwp;
191
192 error = -drm_dev_register(sc->sc_ddev, 0);
193 if (error) {
194 aprint_error_dev(dev, "couldn't register DRM device: %d\n",
195 error);
196 goto out;
197 }
198 sc->sc_dev_registered = true;
199
200 aprint_normal_dev(dev, "initialized %s %d.%d.%d %s on minor %d\n",
201 driver->name, driver->major, driver->minor, driver->patchlevel,
202 driver->date, sc->sc_ddev->primary->index);
203
204 /*
205 * Process asynchronous tasks queued synchronously during
206 * attach. This will be for display detection to attach a
207 * framebuffer, so we have the opportunity for a console device
208 * to attach before autoconf has completed, in time for init(8)
209 * to find that console without panicking.
210 */
211 while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
212 struct sunxi_drm_task *const task =
213 SIMPLEQ_FIRST(&sc->sc_tasks);
214
215 SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, sdt_u.queue);
216 (*task->sdt_fn)(task);
217 }
218
219 out: /* Cause any subesquent tasks to be processed by the workqueue. */
220 atomic_store_relaxed(&sc->sc_task_thread, NULL);
221 }
222
223 static vmem_t *
224 sunxi_drm_alloc_cma_pool(struct drm_device *ddev, size_t cma_size)
225 {
226 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
227 bus_dma_segment_t segs[1];
228 int nsegs;
229 int error;
230
231 error = bus_dmamem_alloc(sc->sc_dmat, cma_size, PAGE_SIZE, 0,
232 segs, 1, &nsegs, BUS_DMA_NOWAIT);
233 if (error) {
234 aprint_error_dev(sc->sc_dev, "couldn't allocate CMA pool\n");
235 return NULL;
236 }
237
238 return vmem_create("sunxidrm", segs[0].ds_addr, segs[0].ds_len,
239 PAGE_SIZE, NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE);
240 }
241
242 static int
243 sunxi_drm_fb_create_handle(struct drm_framebuffer *fb,
244 struct drm_file *file, unsigned int *handle)
245 {
246 struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
247
248 return drm_gem_handle_create(file, &sfb->obj->base, handle);
249 }
250
251 static void
252 sunxi_drm_fb_destroy(struct drm_framebuffer *fb)
253 {
254 struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
255
256 drm_framebuffer_cleanup(fb);
257 drm_gem_object_put_unlocked(&sfb->obj->base);
258 kmem_free(sfb, sizeof(*sfb));
259 }
260
261 static const struct drm_framebuffer_funcs sunxi_drm_framebuffer_funcs = {
262 .create_handle = sunxi_drm_fb_create_handle,
263 .destroy = sunxi_drm_fb_destroy,
264 };
265
266 static struct drm_framebuffer *
267 sunxi_drm_fb_create(struct drm_device *ddev, struct drm_file *file,
268 const struct drm_mode_fb_cmd2 *cmd)
269 {
270 struct sunxi_drm_framebuffer *fb;
271 struct drm_gem_object *gem_obj;
272 int error;
273
274 if (cmd->flags)
275 return NULL;
276
277 gem_obj = drm_gem_object_lookup(file, cmd->handles[0]);
278 if (gem_obj == NULL)
279 return NULL;
280
281 fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
282 fb->obj = to_drm_gem_cma_obj(gem_obj);
283 drm_helper_mode_fill_fb_struct(ddev, &fb->base, cmd);
284
285 error = drm_framebuffer_init(ddev, &fb->base, &sunxi_drm_framebuffer_funcs);
286 if (error != 0)
287 goto dealloc;
288
289 return &fb->base;
290
291 dealloc:
292 drm_framebuffer_cleanup(&fb->base);
293 kmem_free(fb, sizeof(*fb));
294 drm_gem_object_put_unlocked(gem_obj);
295
296 return NULL;
297 }
298
299 static struct drm_mode_config_funcs sunxi_drm_mode_config_funcs = {
300 .fb_create = sunxi_drm_fb_create,
301 };
302
303 static int
304 sunxi_drm_simplefb_lookup(bus_addr_t *paddr, bus_size_t *psize)
305 {
306 static const struct device_compatible_entry simplefb_compat[] = {
307 { .compat = "simple-framebuffer" },
308 DEVICE_COMPAT_EOL
309 };
310 int chosen, child, error;
311 bus_addr_t addr_end;
312
313 chosen = OF_finddevice("/chosen");
314 if (chosen == -1)
315 return ENOENT;
316
317 for (child = OF_child(chosen); child; child = OF_peer(child)) {
318 if (!fdtbus_status_okay(child))
319 continue;
320 if (!of_compatible_match(child, simplefb_compat))
321 continue;
322 error = fdtbus_get_reg(child, 0, paddr, psize);
323 if (error != 0)
324 return error;
325
326 /* Reclaim entire pages used by the simplefb */
327 addr_end = *paddr + *psize;
328 *paddr = SUNXI_TRUNC_PAGE(*paddr);
329 *psize = SUNXI_ROUND_PAGE(addr_end - *paddr);
330 return 0;
331 }
332
333 return ENOENT;
334 }
335
336 static int
337 sunxi_drm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
338 {
339 struct sunxi_drm_softc * const sc = sunxi_drm_private(helper->dev);
340 struct drm_device *ddev = helper->dev;
341 struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(helper->fb);
342 struct drm_framebuffer *fb = helper->fb;
343 struct sunxi_drmfb_attach_args sfa;
344 bus_addr_t sfb_addr;
345 bus_size_t sfb_size;
346 size_t cma_size;
347 int error;
348
349 const u_int width = sizes->surface_width;
350 const u_int height = sizes->surface_height;
351 const u_int pitch = width * (32 / 8);
352
353 const size_t size = roundup(height * pitch, PAGE_SIZE);
354
355 if (sunxi_drm_simplefb_lookup(&sfb_addr, &sfb_size) != 0)
356 sfb_size = 0;
357
358 /* Reserve enough memory for a 4K plane, rounded to 1MB */
359 cma_size = (SUNXI_DRM_MAX_WIDTH * SUNXI_DRM_MAX_HEIGHT * 4);
360 if (sfb_size == 0) {
361 /* Add memory for FB console if we cannot reclaim bootloader memory */
362 cma_size += size;
363 }
364 cma_size = roundup(cma_size, 1024 * 1024);
365 sc->sc_ddev->cma_pool = sunxi_drm_alloc_cma_pool(sc->sc_ddev, cma_size);
366 if (sc->sc_ddev->cma_pool != NULL) {
367 if (sfb_size != 0) {
368 error = vmem_add(sc->sc_ddev->cma_pool, sfb_addr,
369 sfb_size, VM_SLEEP);
370 if (error != 0)
371 sfb_size = 0;
372 }
373 aprint_normal_dev(sc->sc_dev, "reserved %u MB DRAM for CMA",
374 (u_int)((cma_size + sfb_size) / (1024 * 1024)));
375 if (sfb_size != 0)
376 aprint_normal(" (%u MB reclaimed from bootloader)",
377 (u_int)(sfb_size / (1024 * 1024)));
378 aprint_normal("\n");
379 }
380
381 sfb->obj = drm_gem_cma_create(ddev, size);
382 if (sfb->obj == NULL) {
383 DRM_ERROR("failed to allocate memory for framebuffer\n");
384 return -ENOMEM;
385 }
386
387 fb->pitches[0] = pitch;
388 fb->offsets[0] = 0;
389 fb->width = width;
390 fb->height = height;
391 fb->format = drm_format_info(DRM_FORMAT_XRGB8888);
392 fb->dev = ddev;
393
394 error = drm_framebuffer_init(ddev, fb, &sunxi_drm_framebuffer_funcs);
395 if (error != 0) {
396 DRM_ERROR("failed to initialize framebuffer\n");
397 return error;
398 }
399
400 memset(&sfa, 0, sizeof(sfa));
401 sfa.sfa_drm_dev = ddev;
402 sfa.sfa_fb_helper = helper;
403 sfa.sfa_fb_sizes = *sizes;
404 sfa.sfa_fb_bst = sc->sc_bst;
405 sfa.sfa_fb_dmat = sc->sc_dmat;
406 sfa.sfa_fb_linebytes = helper->fb->pitches[0];
407
408 helper->fbdev = config_found(ddev->dev, &sfa, NULL,
409 CFARGS(.iattr = "sunxifbbus"));
410 if (helper->fbdev == NULL) {
411 DRM_ERROR("unable to attach framebuffer\n");
412 return -ENXIO;
413 }
414
415 return 0;
416 }
417
418 static struct drm_fb_helper_funcs sunxi_drm_fb_helper_funcs = {
419 .fb_probe = sunxi_drm_fb_probe,
420 };
421
422 static int
423 sunxi_drm_load(struct drm_device *ddev, unsigned long flags)
424 {
425 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
426 struct sunxi_drm_endpoint *sep;
427 struct sunxi_drm_fbdev *fbdev;
428 const u_int *data;
429 int datalen, error, num_crtc;
430
431 drm_mode_config_init(ddev);
432 ddev->mode_config.min_width = 0;
433 ddev->mode_config.min_height = 0;
434 ddev->mode_config.max_width = SUNXI_DRM_MAX_WIDTH;
435 ddev->mode_config.max_height = SUNXI_DRM_MAX_HEIGHT;
436 ddev->mode_config.funcs = &sunxi_drm_mode_config_funcs;
437
438 num_crtc = 0;
439 data = fdtbus_get_prop(sc->sc_phandle, "allwinner,pipelines", &datalen);
440 while (datalen >= 4) {
441 const int crtc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
442
443 TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
444 if (sep->phandle == crtc_phandle && sep->ddev == NULL) {
445 sep->ddev = ddev;
446 error = fdt_endpoint_activate_direct(sep->ep, true);
447 if (error != 0) {
448 aprint_error_dev(sc->sc_dev, "failed to activate endpoint: %d\n",
449 error);
450 }
451 if (fdt_endpoint_type(sep->ep) == EP_DRM_CRTC)
452 num_crtc++;
453 }
454
455 datalen -= 4;
456 data++;
457 }
458
459 if (num_crtc == 0) {
460 aprint_error_dev(sc->sc_dev, "no pipelines configured\n");
461 error = ENXIO;
462 goto drmerr;
463 }
464
465 fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
466
467 drm_fb_helper_prepare(ddev, &fbdev->helper, &sunxi_drm_fb_helper_funcs);
468
469 error = drm_fb_helper_init(ddev, &fbdev->helper, num_crtc);
470 if (error)
471 goto allocerr;
472
473 fbdev->helper.fb = kmem_zalloc(sizeof(struct sunxi_drm_framebuffer), KM_SLEEP);
474
475 drm_fb_helper_single_add_all_connectors(&fbdev->helper);
476
477 drm_helper_disable_unused_functions(ddev);
478
479 drm_fb_helper_initial_config(&fbdev->helper, 32);
480
481 /* XXX */
482 ddev->irq_enabled = true;
483 drm_vblank_init(ddev, num_crtc);
484
485 return 0;
486
487 allocerr:
488 kmem_free(fbdev, sizeof(*fbdev));
489 drmerr:
490 drm_mode_config_cleanup(ddev);
491
492 return error;
493 }
494
495 static uint32_t
496 sunxi_drm_get_vblank_counter(struct drm_device *ddev, unsigned int crtc)
497 {
498 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
499
500 if (crtc >= __arraycount(sc->sc_vbl))
501 return 0;
502
503 if (sc->sc_vbl[crtc].get_vblank_counter == NULL)
504 return 0;
505
506 return sc->sc_vbl[crtc].get_vblank_counter(sc->sc_vbl[crtc].priv);
507 }
508
509 static int
510 sunxi_drm_enable_vblank(struct drm_device *ddev, unsigned int crtc)
511 {
512 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
513
514 if (crtc >= __arraycount(sc->sc_vbl))
515 return 0;
516
517 if (sc->sc_vbl[crtc].enable_vblank == NULL)
518 return 0;
519
520 sc->sc_vbl[crtc].enable_vblank(sc->sc_vbl[crtc].priv);
521
522 return 0;
523 }
524
525 static void
526 sunxi_drm_disable_vblank(struct drm_device *ddev, unsigned int crtc)
527 {
528 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
529
530 if (crtc >= __arraycount(sc->sc_vbl))
531 return;
532
533 if (sc->sc_vbl[crtc].disable_vblank == NULL)
534 return;
535
536 sc->sc_vbl[crtc].disable_vblank(sc->sc_vbl[crtc].priv);
537 }
538
539 static void
540 sunxi_drm_unload(struct drm_device *ddev)
541 {
542 drm_mode_config_cleanup(ddev);
543 }
544
545 int
546 sunxi_drm_register_endpoint(int phandle, struct fdt_endpoint *ep)
547 {
548 struct sunxi_drm_endpoint *sep;
549
550 sep = kmem_zalloc(sizeof(*sep), KM_SLEEP);
551 sep->phandle = phandle;
552 sep->ep = ep;
553 sep->ddev = NULL;
554 TAILQ_INSERT_TAIL(&sunxi_drm_endpoints, sep, entries);
555
556 return 0;
557 }
558
559 struct drm_device *
560 sunxi_drm_endpoint_device(struct fdt_endpoint *ep)
561 {
562 struct sunxi_drm_endpoint *sep;
563
564 TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
565 if (sep->ep == ep)
566 return sep->ddev;
567
568 return NULL;
569 }
570
571 static void
572 sunxi_drm_task_work(struct work *work, void *cookie)
573 {
574 struct sunxi_drm_task *task = container_of(work, struct sunxi_drm_task,
575 sdt_u.work);
576
577 (*task->sdt_fn)(task);
578 }
579
580 void
581 sunxi_task_init(struct sunxi_drm_task *task,
582 void (*fn)(struct sunxi_drm_task *))
583 {
584
585 task->sdt_fn = fn;
586 }
587
588 void
589 sunxi_task_schedule(device_t self, struct sunxi_drm_task *task)
590 {
591 struct sunxi_drm_softc *sc = device_private(self);
592
593 if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
594 SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, sdt_u.queue);
595 else
596 workqueue_enqueue(sc->sc_task_wq, &task->sdt_u.work, NULL);
597 }
598