xref: /freebsd/sys/arm/nvidia/drm2/tegra_host1x.c (revision b00ab754)
1 /*-
2  * Copyright (c) 2015 Michal Meloun
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/clock.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/lock.h>
37 
38 #include <sys/module.h>
39 #include <sys/resource.h>
40 #include <sys/sx.h>
41 #include <sys/rman.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 
46 #include <dev/extres/clk/clk.h>
47 #include <dev/extres/hwreset/hwreset.h>
48 #include <dev/drm2/drmP.h>
49 #include <dev/drm2/drm_crtc_helper.h>
50 #include <dev/drm2/drm_fb_helper.h>
51 #include <dev/fdt/simplebus.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 
55 #include <arm/nvidia/drm2/tegra_drm.h>
56 
57 #include "fb_if.h"
58 #include "tegra_drm_if.h"
59 
60 #define	WR4(_sc, _r, _v)	bus_rite_4((_sc)->mem_res, (_r), (_v))
61 #define	RD4(_sc, _r)		bus_read_4((_sc)->mem_res, (_r))
62 
63 #define	LOCK(_sc)		sx_xlock(&(_sc)->lock)
64 #define	UNLOCK(_sc)		sx_xunlock(&(_sc)->lock)
65 #define	SLEEP(_sc, timeout)	sx_sleep(sc, &sc->lock, 0, "host1x", timeout);
66 #define	LOCK_INIT(_sc)		sx_init(&_sc->lock, "host1x")
67 #define	LOCK_DESTROY(_sc)	sx_destroy(&_sc->lock)
68 #define	ASSERT_LOCKED(_sc)	sx_assert(&_sc->lock, SA_LOCKED)
69 #define	ASSERT_UNLOCKED(_sc)	sx_assert(&_sc->lock, SA_UNLOCKED)
70 
71 static struct ofw_compat_data compat_data[] = {
72 	{"nvidia,tegra124-host1x",	1},
73 	{NULL,				0}
74 };
75 
76 #define DRIVER_NAME "tegra"
77 #define DRIVER_DESC "NVIDIA Tegra TK1"
78 #define DRIVER_DATE "20151101"
79 #define DRIVER_MAJOR 0
80 #define DRIVER_MINOR 0
81 #define DRIVER_PATCHLEVEL 0
82 
83 struct client_info;
84 TAILQ_HEAD(client_list, client_info);
85 typedef struct client_list client_list_t;
86 
87 struct client_info {
88 	TAILQ_ENTRY(client_info) list_e;
89 	device_t client;
90 	int 	activated;
91 };
92 
93 struct host1x_softc {
94 	struct simplebus_softc	simplebus_sc;	/* must be first */
95 	device_t		dev;
96 	struct sx		lock;
97 	int 			attach_done;
98 
99 	struct resource		*mem_res;
100 	struct resource		*syncpt_irq_res;
101 	void			*syncpt_irq_h;
102 	struct resource		*gen_irq_res;
103 	void			*gen_irq_h;
104 
105 	clk_t			clk;
106 	hwreset_t			reset;
107 	struct intr_config_hook	irq_hook;
108 
109 	int			drm_inited;
110 	client_list_t		clients;
111 
112 	struct tegra_drm 	*tegra_drm;
113 };
114 
115 
116 static void
117 host1x_output_poll_changed(struct drm_device *drm_dev)
118 {
119 	struct tegra_drm *drm;
120 
121 	drm = container_of(drm_dev, struct tegra_drm, drm_dev);
122 	if (drm->fb != NULL)
123 		drm_fb_helper_hotplug_event(&drm->fb->fb_helper);
124 }
125 
126 static const struct drm_mode_config_funcs mode_config_funcs = {
127 	.fb_create = tegra_drm_fb_create,
128 	.output_poll_changed = host1x_output_poll_changed,
129 };
130 
131 
132 static int
133 host1x_drm_init(struct host1x_softc *sc)
134 {
135 	struct client_info *entry;
136 	int rv;
137 
138 	LOCK(sc);
139 
140 	TAILQ_FOREACH(entry, &sc->clients, list_e) {
141 		if (entry->activated)
142 			continue;
143 		rv = TEGRA_DRM_INIT_CLIENT(entry->client, sc->dev,
144 		    sc->tegra_drm);
145 		if (rv != 0) {
146 			device_printf(sc->dev,
147 			    "Cannot init DRM client %s: %d\n",
148 			    device_get_name(entry->client), rv);
149 			return (rv);
150 		}
151 		entry->activated = 1;
152 	}
153 	UNLOCK(sc);
154 
155 	return (0);
156 }
157 
158 static int
159 host1x_drm_exit(struct host1x_softc *sc)
160 {
161 	struct client_info *entry;
162 	int rv;
163 #ifdef FREEBSD_NOTYET
164 	struct drm_device *dev, *tmp;
165 #endif
166 	LOCK(sc);
167 	if (!sc->drm_inited) {
168 		UNLOCK(sc);
169 		return (0);
170 	}
171 	TAILQ_FOREACH_REVERSE(entry, &sc->clients, client_list, list_e) {
172 		if (!entry->activated)
173 			continue;
174 		rv = TEGRA_DRM_EXIT_CLIENT(entry->client, sc->dev,
175 		    sc->tegra_drm);
176 		if (rv != 0) {
177 			device_printf(sc->dev,
178 			    "Cannot exit DRM client %s: %d\n",
179 			    device_get_name(entry->client), rv);
180 		}
181 		entry->activated = 0;
182 	}
183 
184 #ifdef FREEBSD_NOTYET
185 	list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item)
186 		drm_put_dev(dev);
187 #endif
188 	sc->drm_inited = 0;
189 	UNLOCK(sc);
190 
191 	return (0);
192 }
193 
194 static int
195 host1x_drm_load(struct drm_device *drm_dev, unsigned long flags)
196 {
197 	struct host1x_softc *sc;
198 	int rv;
199 
200 	sc = device_get_softc(drm_dev->dev);
201 
202 	drm_mode_config_init(drm_dev);
203 	drm_dev->mode_config.min_width = 32;
204 	drm_dev->mode_config.min_height = 32;
205 	drm_dev->mode_config.max_width = 4096;
206 	drm_dev->mode_config.max_height = 4096;
207 	drm_dev->mode_config.funcs = &mode_config_funcs;
208 
209 	rv = host1x_drm_init(sc);
210 	if (rv != 0)
211 		goto fail_host1x;
212 
213 	drm_dev->irq_enabled = true;
214 	drm_dev->max_vblank_count = 0xffffffff;
215 	drm_dev->vblank_disable_allowed = true;
216 
217 	rv = drm_vblank_init(drm_dev, drm_dev->mode_config.num_crtc);
218 	if (rv != 0)
219 		goto fail_vblank;
220 
221 	drm_mode_config_reset(drm_dev);
222 
223 	rv = tegra_drm_fb_init(drm_dev);
224 	if (rv != 0)
225 		goto fail_fb;
226 	drm_kms_helper_poll_init(drm_dev);
227 
228 	return (0);
229 
230 fail_fb:
231 	tegra_drm_fb_destroy(drm_dev);
232 	drm_vblank_cleanup(drm_dev);
233 fail_vblank:
234 	host1x_drm_exit(sc);
235 fail_host1x:
236 	drm_mode_config_cleanup(drm_dev);
237 
238 	return (rv);
239 }
240 
241 static int
242 host1x_drm_unload(struct drm_device *drm_dev)
243 {
244 	struct host1x_softc *sc;
245 	int rv;
246 
247 	sc = device_get_softc(drm_dev->dev);
248 
249 	drm_kms_helper_poll_fini(drm_dev);
250 	tegra_drm_fb_destroy(drm_dev);
251 	drm_mode_config_cleanup(drm_dev);
252 
253 	rv = host1x_drm_exit(sc);
254 	if (rv < 0)
255 		return (rv);
256 	return (0);
257 }
258 
259 static int
260 host1x_drm_open(struct drm_device *drm_dev, struct drm_file *filp)
261 {
262 
263 	return (0);
264 }
265 
266 static void
267 tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
268 {
269 	struct drm_crtc *crtc;
270 
271 	list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
272 		tegra_dc_cancel_page_flip(crtc, file);
273 }
274 
275 static void
276 host1x_drm_lastclose(struct drm_device *drm_dev)
277 {
278 
279 	struct tegra_drm *drm;
280 
281 	drm = container_of(drm_dev, struct tegra_drm, drm_dev);
282 	if (drm->fb  != NULL)
283 		drm_fb_helper_restore_fbdev_mode(&drm->fb->fb_helper);
284 }
285 
286 static int
287 host1x_drm_enable_vblank(struct drm_device *drm_dev, int pipe)
288 {
289 	struct drm_crtc *crtc;
290 
291 	list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
292 		if (pipe == tegra_dc_get_pipe(crtc)) {
293 			tegra_dc_enable_vblank(crtc);
294 			return (0);
295 		}
296 	}
297 	return (-ENODEV);
298 }
299 
300 static void
301 host1x_drm_disable_vblank(struct drm_device *drm_dev, int pipe)
302 {
303 	struct drm_crtc *crtc;
304 
305 	list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
306 		if (pipe == tegra_dc_get_pipe(crtc)) {
307 			tegra_dc_disable_vblank(crtc);
308 			return;
309 		}
310 	}
311 }
312 
313 
314 static struct drm_ioctl_desc host1x_drm_ioctls[] = {
315 };
316 
317 
318 struct drm_driver tegra_drm_driver = {
319 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
320 	.load = host1x_drm_load,
321 	.unload = host1x_drm_unload,
322 	.open = host1x_drm_open,
323 	.preclose = tegra_drm_preclose,
324 	.lastclose = host1x_drm_lastclose,
325 
326 	.get_vblank_counter = drm_vblank_count,
327 	.enable_vblank = host1x_drm_enable_vblank,
328 	.disable_vblank = host1x_drm_disable_vblank,
329 
330 	/* Fields filled by tegra_bo_driver_register()
331 	.gem_free_object
332 	.gem_pager_ops
333 	.dumb_create
334 	.dumb_map_offset
335 	.dumb_destroy
336 	*/
337 	.ioctls = host1x_drm_ioctls,
338 	.num_ioctls = nitems(host1x_drm_ioctls),
339 
340 	.name = DRIVER_NAME,
341 	.desc = DRIVER_DESC,
342 	.date = DRIVER_DATE,
343 	.major = DRIVER_MAJOR,
344 	.minor = DRIVER_MINOR,
345 	.patchlevel = DRIVER_PATCHLEVEL,
346 };
347 
348 /*
349  * ----------------- Device methods -------------------------
350  */
351 static void
352 host1x_irq_hook(void *arg)
353 {
354 	struct host1x_softc *sc;
355 	int rv;
356 
357 	sc = arg;
358 	config_intrhook_disestablish(&sc->irq_hook);
359 
360 	tegra_bo_driver_register(&tegra_drm_driver);
361 	rv = drm_get_platform_dev(sc->dev, &sc->tegra_drm->drm_dev,
362 	    &tegra_drm_driver);
363 	if (rv != 0) {
364 		device_printf(sc->dev, "drm_get_platform_dev(): %d\n", rv);
365 		return;
366 	}
367 
368 	sc->drm_inited = 1;
369 }
370 
371 static struct fb_info *
372 host1x_fb_helper_getinfo(device_t dev)
373 {
374 	struct host1x_softc *sc;
375 
376 	sc = device_get_softc(dev);
377 	if (sc->tegra_drm == NULL)
378 		return (NULL);
379 	return (tegra_drm_fb_getinfo(&sc->tegra_drm->drm_dev));
380 }
381 
382 static int
383 host1x_register_client(device_t dev, device_t client)
384 {
385 	struct host1x_softc *sc;
386 	struct client_info *entry;
387 
388 	sc = device_get_softc(dev);
389 
390 	entry = malloc(sizeof(struct client_info), M_DEVBUF, M_WAITOK | M_ZERO);
391 	entry->client = client;
392 	entry->activated = 0;
393 
394 	LOCK(sc);
395 	TAILQ_INSERT_TAIL(&sc->clients, entry, list_e);
396 	UNLOCK(sc);
397 
398 	return (0);
399 }
400 
401 static int
402 host1x_deregister_client(device_t dev, device_t client)
403 {
404 	struct host1x_softc *sc;
405 	struct client_info *entry;
406 
407 	sc = device_get_softc(dev);
408 
409 	LOCK(sc);
410 	TAILQ_FOREACH(entry, &sc->clients, list_e) {
411 		if (entry->client == client) {
412 			if (entry->activated)
413 				panic("Tegra DRM: Attempt to deregister "
414 				    "activated client");
415 			TAILQ_REMOVE(&sc->clients, entry, list_e);
416 			free(entry, M_DEVBUF);
417 			UNLOCK(sc);
418 			return (0);
419 		}
420 	}
421 	UNLOCK(sc);
422 
423 	return (0);
424 }
425 
426 static void
427 host1x_gen_intr(void *arg)
428 {
429 	struct host1x_softc *sc;
430 
431 	sc = (struct host1x_softc *)arg;
432 	LOCK(sc);
433 	UNLOCK(sc);
434 }
435 
436 static void
437 host1x_syncpt_intr(void *arg)
438 {
439 	struct host1x_softc *sc;
440 
441 	sc = (struct host1x_softc *)arg;
442 	LOCK(sc);
443 	UNLOCK(sc);
444 }
445 
446 static void
447 host1x_new_pass(device_t dev)
448 {
449 	struct host1x_softc *sc;
450 	int rv, rid;
451 	phandle_t node;
452 
453 	/*
454 	 * We attach during BUS_PASS_BUS (because we must overcome simplebus),
455 	 * but some of our FDT resources are not ready until BUS_PASS_DEFAULT
456 	 */
457 	sc = device_get_softc(dev);
458 	if (sc->attach_done || bus_current_pass < BUS_PASS_DEFAULT) {
459 		bus_generic_new_pass(dev);
460 		return;
461 	}
462 
463 	sc->attach_done = 1;
464 	node = ofw_bus_get_node(dev);
465 
466 	/* Allocate our IRQ resource. */
467 	rid = 0;
468 	sc->syncpt_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
469 	    RF_ACTIVE);
470 	if (sc->syncpt_irq_res == NULL) {
471 		device_printf(dev, "Cannot allocate interrupt.\n");
472 		rv = ENXIO;
473 		goto fail;
474 	}
475 	rid = 1;
476 	sc->gen_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
477 	    RF_ACTIVE);
478 	if (sc->gen_irq_res == NULL) {
479 		device_printf(dev, "Cannot allocate interrupt.\n");
480 		rv = ENXIO;
481 		goto fail;
482 	}
483 
484 	/* FDT resources */
485 	rv = hwreset_get_by_ofw_name(sc->dev, 0, "host1x", &sc->reset);
486 	if (rv != 0) {
487 		device_printf(dev, "Cannot get fuse reset\n");
488 		goto fail;
489 	}
490 	rv = clk_get_by_ofw_index(sc->dev, 0, 0, &sc->clk);
491 	if (rv != 0) {
492 		device_printf(dev, "Cannot get i2c clock: %d\n", rv);
493 		goto fail;
494 	}
495 
496 	rv = clk_enable(sc->clk);
497 	if (rv != 0) {
498 		device_printf(dev, "Cannot enable clock: %d\n", rv);
499 		goto fail;
500 	}
501 	rv = hwreset_deassert(sc->reset);
502 	if (rv != 0) {
503 		device_printf(sc->dev, "Cannot clear reset\n");
504 		goto fail;
505 	}
506 
507 	/* Setup  interrupts */
508 	rv = bus_setup_intr(dev, sc->gen_irq_res,
509 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, host1x_gen_intr,
510 	    sc, &sc->gen_irq_h);
511 	if (rv) {
512 		device_printf(dev, "Cannot setup gen interrupt.\n");
513 		goto fail;
514 	}
515 
516 	rv = bus_setup_intr(dev, sc->syncpt_irq_res,
517 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, host1x_syncpt_intr,
518 	    sc, &sc->syncpt_irq_h);
519 	if (rv) {
520 		device_printf(dev, "Cannot setup syncpt interrupt.\n");
521 		goto fail;
522 	}
523 
524 	simplebus_init(dev, 0);
525 	for (node = OF_child(node); node > 0; node = OF_peer(node))
526 	    simplebus_add_device(dev, node, 0, NULL, -1, NULL);
527 
528 	sc->irq_hook.ich_func = host1x_irq_hook;
529 	sc->irq_hook.ich_arg = sc;
530 	config_intrhook_establish(&sc->irq_hook);
531 	bus_generic_new_pass(dev);
532 	return;
533 
534 fail:
535 	device_detach(dev);
536 	return;
537 }
538 
539 static int
540 host1x_probe(device_t dev)
541 {
542 
543 	if (!ofw_bus_status_okay(dev))
544 		return (ENXIO);
545 
546 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
547 		return (ENXIO);
548 
549 	return (BUS_PROBE_DEFAULT);
550 }
551 
552 static int
553 host1x_attach(device_t dev)
554 {
555 	int rv, rid;
556 	struct host1x_softc *sc;
557 
558 	sc = device_get_softc(dev);
559 	sc->tegra_drm = malloc(sizeof(struct tegra_drm), DRM_MEM_DRIVER,
560 	    M_WAITOK | M_ZERO);
561 
562 	/* crosslink together all worlds */
563 	sc->dev = dev;
564 	sc->tegra_drm->drm_dev.dev_private = &sc->tegra_drm;
565 	sc->tegra_drm->drm_dev.dev = dev;
566 
567 	TAILQ_INIT(&sc->clients);
568 
569 	LOCK_INIT(sc);
570 
571 	/* Get the memory resource for the register mapping. */
572 	rid = 0;
573 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
574 	    RF_ACTIVE);
575 	if (sc->mem_res == NULL) {
576 		device_printf(dev, "Cannot map registers.\n");
577 		rv = ENXIO;
578 		goto fail;
579 	}
580 
581 	return (bus_generic_attach(dev));
582 
583 fail:
584 	if (sc->tegra_drm != NULL)
585 		free(sc->tegra_drm, DRM_MEM_DRIVER);
586 	if (sc->mem_res != NULL)
587 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
588 	LOCK_DESTROY(sc);
589 	return (rv);
590 }
591 
592 static int
593 host1x_detach(device_t dev)
594 {
595 	struct host1x_softc *sc;
596 
597 	sc = device_get_softc(dev);
598 
599 	host1x_drm_exit(sc);
600 
601 	if (sc->gen_irq_h != NULL)
602 		bus_teardown_intr(dev, sc->gen_irq_res, sc->gen_irq_h);
603 	if (sc->tegra_drm != NULL)
604 		free(sc->tegra_drm, DRM_MEM_DRIVER);
605 	if (sc->clk != NULL)
606 		clk_release(sc->clk);
607 	if (sc->reset != NULL)
608 		hwreset_release(sc->reset);
609 	if (sc->syncpt_irq_h != NULL)
610 		bus_teardown_intr(dev, sc->syncpt_irq_res, sc->syncpt_irq_h);
611 	if (sc->gen_irq_res != NULL)
612 		bus_release_resource(dev, SYS_RES_IRQ, 1, sc->gen_irq_res);
613 	if (sc->syncpt_irq_res != NULL)
614 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->syncpt_irq_res);
615 	if (sc->mem_res != NULL)
616 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
617 	LOCK_DESTROY(sc);
618 	return (bus_generic_detach(dev));
619 }
620 
621 static device_method_t host1x_methods[] = {
622 	/* Device interface */
623 	DEVMETHOD(device_probe,		host1x_probe),
624 	DEVMETHOD(device_attach,	host1x_attach),
625 	DEVMETHOD(device_detach,	host1x_detach),
626 
627 	/* Bus interface */
628 	DEVMETHOD(bus_new_pass,		host1x_new_pass),
629 
630 	/* Framebuffer service methods */
631 	DEVMETHOD(fb_getinfo,           host1x_fb_helper_getinfo),
632 
633 	/* tegra drm interface */
634 	DEVMETHOD(tegra_drm_register_client,	host1x_register_client),
635 	DEVMETHOD(tegra_drm_deregister_client,	host1x_deregister_client),
636 
637 	DEVMETHOD_END
638 };
639 
640 static devclass_t host1x_devclass;
641 DEFINE_CLASS_1(host1x, host1x_driver, host1x_methods,
642     sizeof(struct host1x_softc), simplebus_driver);
643 EARLY_DRIVER_MODULE(host1x, simplebus, host1x_driver,
644     host1x_devclass, 0, 0,  BUS_PASS_BUS);
645 
646 /* Bindings for fbd device. */
647 extern devclass_t fbd_devclass;
648 extern driver_t fbd_driver;
649 DRIVER_MODULE(fbd, host1x, fbd_driver, fbd_devclass, 0, 0);
650 
651