sunxi_drm.c revision 1.6 1 /* $NetBSD: sunxi_drm.c,v 1.6 2019/02/04 12:10:13 jmcneill 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.6 2019/02/04 12:10:13 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/intr.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/conf.h>
39
40 #include <uvm/uvm_extern.h>
41 #include <uvm/uvm_object.h>
42 #include <uvm/uvm_device.h>
43
44 #include <drm/drmP.h>
45 #include <drm/drm_crtc_helper.h>
46 #include <drm/drm_fb_helper.h>
47
48 #include <dev/fdt/fdtvar.h>
49 #include <dev/fdt/fdt_port.h>
50
51 #include <arm/sunxi/sunxi_drm.h>
52
53 static TAILQ_HEAD(, sunxi_drm_endpoint) sunxi_drm_endpoints =
54 TAILQ_HEAD_INITIALIZER(sunxi_drm_endpoints);
55
56 static const char * const compatible[] = {
57 "allwinner,sun8i-h3-display-engine",
58 "allwinner,sun50i-a64-display-engine",
59 NULL
60 };
61
62 static const char * fb_compatible[] = {
63 "allwinner,simple-framebuffer",
64 NULL
65 };
66
67 static int sunxi_drm_match(device_t, cfdata_t, void *);
68 static void sunxi_drm_attach(device_t, device_t, void *);
69
70 static void sunxi_drm_init(device_t);
71
72 static int sunxi_drm_set_busid(struct drm_device *, struct drm_master *);
73
74 static uint32_t sunxi_drm_get_vblank_counter(struct drm_device *, unsigned int);
75 static int sunxi_drm_enable_vblank(struct drm_device *, unsigned int);
76 static void sunxi_drm_disable_vblank(struct drm_device *, unsigned int);
77
78 static int sunxi_drm_load(struct drm_device *, unsigned long);
79 static int sunxi_drm_unload(struct drm_device *);
80
81 static struct drm_driver sunxi_drm_driver = {
82 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
83 .dev_priv_size = 0,
84 .load = sunxi_drm_load,
85 .unload = sunxi_drm_unload,
86
87 .gem_free_object = drm_gem_cma_free_object,
88 .mmap_object = drm_gem_or_legacy_mmap_object,
89 .gem_uvm_ops = &drm_gem_cma_uvm_ops,
90
91 .dumb_create = drm_gem_cma_dumb_create,
92 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
93 .dumb_destroy = drm_gem_dumb_destroy,
94
95 .get_vblank_counter = sunxi_drm_get_vblank_counter,
96 .enable_vblank = sunxi_drm_enable_vblank,
97 .disable_vblank = sunxi_drm_disable_vblank,
98
99 .name = DRIVER_NAME,
100 .desc = DRIVER_DESC,
101 .date = DRIVER_DATE,
102 .major = DRIVER_MAJOR,
103 .minor = DRIVER_MINOR,
104 .patchlevel = DRIVER_PATCHLEVEL,
105
106 .set_busid = sunxi_drm_set_busid,
107 };
108
109 CFATTACH_DECL_NEW(sunxi_drm, sizeof(struct sunxi_drm_softc),
110 sunxi_drm_match, sunxi_drm_attach, NULL, NULL);
111
112 static int
113 sunxi_drm_match(device_t parent, cfdata_t cf, void *aux)
114 {
115 struct fdt_attach_args * const faa = aux;
116
117 return of_match_compatible(faa->faa_phandle, compatible);
118 }
119
120 static void
121 sunxi_drm_attach(device_t parent, device_t self, void *aux)
122 {
123 struct sunxi_drm_softc * const sc = device_private(self);
124 struct fdt_attach_args * const faa = aux;
125 struct drm_driver * const driver = &sunxi_drm_driver;
126 prop_dictionary_t dict = device_properties(self);
127 bool is_disabled;
128
129 sc->sc_dev = self;
130 sc->sc_dmat = faa->faa_dmat;
131 sc->sc_bst = faa->faa_bst;
132 sc->sc_phandle = faa->faa_phandle;
133
134 aprint_naive("\n");
135
136 if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
137 aprint_normal(": Display Engine Pipeline (disabled)\n");
138 return;
139 }
140
141 aprint_normal(": Display Engine Pipeline\n");
142
143 sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
144 if (sc->sc_ddev == NULL) {
145 aprint_error_dev(self, "couldn't allocate DRM device\n");
146 return;
147 }
148 sc->sc_ddev->dev_private = sc;
149 sc->sc_ddev->bst = sc->sc_bst;
150 sc->sc_ddev->bus_dmat = sc->sc_dmat;
151 sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
152 sc->sc_ddev->dmat_subregion_p = false;
153
154 fdt_remove_bycompat(fb_compatible);
155
156 config_defer(self, sunxi_drm_init);
157 }
158
159 static void
160 sunxi_drm_init(device_t dev)
161 {
162 struct sunxi_drm_softc * const sc = device_private(dev);
163 struct drm_driver * const driver = &sunxi_drm_driver;
164 int error;
165
166 error = -drm_dev_register(sc->sc_ddev, 0);
167 if (error) {
168 drm_dev_unref(sc->sc_ddev);
169 aprint_error_dev(dev, "couldn't register DRM device: %d\n",
170 error);
171 return;
172 }
173
174 aprint_normal_dev(dev, "initialized %s %d.%d.%d %s on minor %d\n",
175 driver->name, driver->major, driver->minor, driver->patchlevel,
176 driver->date, sc->sc_ddev->primary->index);
177 }
178
179 static int
180 sunxi_drm_set_busid(struct drm_device *ddev, struct drm_master *master)
181 {
182 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
183 char id[32];
184
185 snprintf(id, sizeof(id), "platform:sunxi:%u", device_unit(sc->sc_dev));
186
187 master->unique = kzalloc(strlen(id) + 1, GFP_KERNEL);
188 if (master->unique == NULL)
189 return -ENOMEM;
190 strcpy(master->unique, id);
191 master->unique_len = strlen(master->unique);
192
193 return 0;
194 }
195
196 static int
197 sunxi_drm_fb_create_handle(struct drm_framebuffer *fb,
198 struct drm_file *file, unsigned int *handle)
199 {
200 struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
201
202 return drm_gem_handle_create(file, &sfb->obj->base, handle);
203 }
204
205 static void
206 sunxi_drm_fb_destroy(struct drm_framebuffer *fb)
207 {
208 struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
209
210 drm_framebuffer_cleanup(fb);
211 drm_gem_object_unreference_unlocked(&sfb->obj->base);
212 kmem_free(sfb, sizeof(*sfb));
213 }
214
215 static const struct drm_framebuffer_funcs sunxi_drm_framebuffer_funcs = {
216 .create_handle = sunxi_drm_fb_create_handle,
217 .destroy = sunxi_drm_fb_destroy,
218 };
219
220 static struct drm_framebuffer *
221 sunxi_drm_fb_create(struct drm_device *ddev, struct drm_file *file,
222 struct drm_mode_fb_cmd2 *cmd)
223 {
224 struct sunxi_drm_framebuffer *fb;
225 struct drm_gem_object *gem_obj;
226 int error;
227
228 if (cmd->flags)
229 return NULL;
230
231 gem_obj = drm_gem_object_lookup(ddev, file, cmd->handles[0]);
232 if (gem_obj == NULL)
233 return NULL;
234
235 fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
236 fb->obj = to_drm_gem_cma_obj(gem_obj);
237 fb->base.pitches[0] = cmd->pitches[0];
238 fb->base.pitches[1] = cmd->pitches[1];
239 fb->base.pitches[2] = cmd->pitches[2];
240 fb->base.offsets[0] = cmd->offsets[0];
241 fb->base.offsets[1] = cmd->offsets[2];
242 fb->base.offsets[2] = cmd->offsets[1];
243 fb->base.width = cmd->width;
244 fb->base.height = cmd->height;
245 fb->base.pixel_format = cmd->pixel_format;
246 fb->base.bits_per_pixel = drm_format_plane_cpp(fb->base.pixel_format, 0) * 8;
247
248 switch (fb->base.pixel_format) {
249 case DRM_FORMAT_XRGB8888:
250 fb->base.depth = 32;
251 break;
252 default:
253 break;
254 }
255
256 error = drm_framebuffer_init(ddev, &fb->base, &sunxi_drm_framebuffer_funcs);
257 if (error != 0)
258 goto dealloc;
259
260 return &fb->base;
261
262 dealloc:
263 drm_framebuffer_cleanup(&fb->base);
264 kmem_free(fb, sizeof(*fb));
265 drm_gem_object_unreference_unlocked(gem_obj);
266
267 return NULL;
268 }
269
270 static struct drm_mode_config_funcs sunxi_drm_mode_config_funcs = {
271 .fb_create = sunxi_drm_fb_create,
272 };
273
274 static int
275 sunxi_drm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
276 {
277 struct sunxi_drm_softc * const sc = sunxi_drm_private(helper->dev);
278 struct drm_device *ddev = helper->dev;
279 struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(helper->fb);
280 struct drm_framebuffer *fb = helper->fb;
281 struct sunxi_drmfb_attach_args sfa;
282 int error;
283
284 const u_int width = sizes->surface_width;
285 const u_int height = sizes->surface_height;
286 const u_int pitch = width * (32 / 8);
287
288 const size_t size = roundup(height * pitch, PAGE_SIZE);
289
290 sfb->obj = drm_gem_cma_create(ddev, size);
291 if (sfb->obj == NULL) {
292 DRM_ERROR("failed to allocate memory for framebuffer\n");
293 return -ENOMEM;
294 }
295
296 fb->pitches[0] = pitch;
297 fb->offsets[0] = 0;
298 fb->width = width;
299 fb->height = height;
300 fb->pixel_format = DRM_FORMAT_XRGB8888;
301 drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth, &fb->bits_per_pixel);
302
303 error = drm_framebuffer_init(ddev, fb, &sunxi_drm_framebuffer_funcs);
304 if (error != 0) {
305 DRM_ERROR("failed to initialize framebuffer\n");
306 return error;
307 }
308
309 memset(&sfa, 0, sizeof(sfa));
310 sfa.sfa_drm_dev = ddev;
311 sfa.sfa_fb_helper = helper;
312 sfa.sfa_fb_sizes = *sizes;
313 sfa.sfa_fb_bst = sc->sc_bst;
314 sfa.sfa_fb_dmat = sc->sc_dmat;
315 sfa.sfa_fb_linebytes = helper->fb->pitches[0];
316
317 helper->fbdev = config_found_ia(ddev->dev, "sunxifbbus", &sfa, NULL);
318 if (helper->fbdev == NULL) {
319 DRM_ERROR("unable to attach framebuffer\n");
320 return -ENXIO;
321 }
322
323 return 0;
324 }
325
326 static struct drm_fb_helper_funcs sunxi_drm_fb_helper_funcs = {
327 .fb_probe = sunxi_drm_fb_probe,
328 };
329
330 static int
331 sunxi_drm_load(struct drm_device *ddev, unsigned long flags)
332 {
333 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
334 struct sunxi_drm_endpoint *sep;
335 struct sunxi_drm_fbdev *fbdev;
336 const u_int *data;
337 int datalen, error, num_crtc;
338
339 drm_mode_config_init(ddev);
340 ddev->mode_config.min_width = 0;
341 ddev->mode_config.min_height = 0;
342 ddev->mode_config.max_width = 3840;
343 ddev->mode_config.max_height = 2160;
344 ddev->mode_config.funcs = &sunxi_drm_mode_config_funcs;
345
346 num_crtc = 0;
347 data = fdtbus_get_prop(sc->sc_phandle, "allwinner,pipelines", &datalen);
348 while (datalen >= 4) {
349 const int crtc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
350
351 TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
352 if (sep->phandle == crtc_phandle && sep->ddev == NULL) {
353 sep->ddev = ddev;
354 error = fdt_endpoint_activate_direct(sep->ep, true);
355 if (error != 0) {
356 aprint_error_dev(sc->sc_dev, "failed to activate endpoint: %d\n",
357 error);
358 }
359 if (fdt_endpoint_type(sep->ep) == EP_DRM_CRTC)
360 num_crtc++;
361 }
362
363 datalen -= 4;
364 data++;
365 }
366
367 if (num_crtc == 0) {
368 aprint_error_dev(sc->sc_dev, "no pipelines configured\n");
369 return ENXIO;
370 }
371
372 fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
373
374 drm_fb_helper_prepare(ddev, &fbdev->helper, &sunxi_drm_fb_helper_funcs);
375
376 error = drm_fb_helper_init(ddev, &fbdev->helper, num_crtc, num_crtc);
377 if (error)
378 goto drmerr;
379
380 fbdev->helper.fb = kmem_zalloc(sizeof(struct sunxi_drm_framebuffer), KM_SLEEP);
381
382 drm_fb_helper_single_add_all_connectors(&fbdev->helper);
383
384 drm_helper_disable_unused_functions(ddev);
385
386 drm_fb_helper_initial_config(&fbdev->helper, 32);
387
388 /* XXX */
389 ddev->irq_enabled = true;
390 drm_vblank_init(ddev, num_crtc);
391
392 return 0;
393
394 drmerr:
395 drm_mode_config_cleanup(ddev);
396 kmem_free(fbdev, sizeof(*fbdev));
397
398 return error;
399 }
400
401 static uint32_t
402 sunxi_drm_get_vblank_counter(struct drm_device *ddev, unsigned int crtc)
403 {
404 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
405
406 if (crtc >= __arraycount(sc->sc_vbl))
407 return 0;
408
409 if (sc->sc_vbl[crtc].get_vblank_counter == NULL)
410 return 0;
411
412 return sc->sc_vbl[crtc].get_vblank_counter(sc->sc_vbl[crtc].priv);
413 }
414
415 static int
416 sunxi_drm_enable_vblank(struct drm_device *ddev, unsigned int crtc)
417 {
418 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
419
420 if (crtc >= __arraycount(sc->sc_vbl))
421 return 0;
422
423 if (sc->sc_vbl[crtc].enable_vblank == NULL)
424 return 0;
425
426 sc->sc_vbl[crtc].enable_vblank(sc->sc_vbl[crtc].priv);
427
428 return 0;
429 }
430
431 static void
432 sunxi_drm_disable_vblank(struct drm_device *ddev, unsigned int crtc)
433 {
434 struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
435
436 if (crtc >= __arraycount(sc->sc_vbl))
437 return;
438
439 if (sc->sc_vbl[crtc].disable_vblank == NULL)
440 return;
441
442 sc->sc_vbl[crtc].disable_vblank(sc->sc_vbl[crtc].priv);
443 }
444
445 static int
446 sunxi_drm_unload(struct drm_device *ddev)
447 {
448 drm_mode_config_cleanup(ddev);
449
450 return 0;
451 }
452
453 int
454 sunxi_drm_register_endpoint(int phandle, struct fdt_endpoint *ep)
455 {
456 struct sunxi_drm_endpoint *sep;
457
458 sep = kmem_zalloc(sizeof(*sep), KM_SLEEP);
459 sep->phandle = phandle;
460 sep->ep = ep;
461 sep->ddev = NULL;
462 TAILQ_INSERT_TAIL(&sunxi_drm_endpoints, sep, entries);
463
464 return 0;
465 }
466
467 struct drm_device *
468 sunxi_drm_endpoint_device(struct fdt_endpoint *ep)
469 {
470 struct sunxi_drm_endpoint *sep;
471
472 TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
473 if (sep->ep == ep)
474 return sep->ddev;
475
476 return NULL;
477 }
478