2 * Copyright (c) 2015 Michal Meloun
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/clock.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
38 #include <sys/module.h>
39 #include <sys/resource.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
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>
55 #include <arm/nvidia/drm2/tegra_drm.h>
58 #include "tegra_drm_if.h"
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))
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)
71 static struct ofw_compat_data compat_data[] = {
72 {"nvidia,tegra124-host1x", 1},
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
84 TAILQ_HEAD(client_list, client_info);
85 typedef struct client_list client_list_t;
88 TAILQ_ENTRY(client_info) list_e;
94 struct simplebus_softc simplebus_sc; /* must be first */
99 struct resource *mem_res;
100 struct resource *syncpt_irq_res;
102 struct resource *gen_irq_res;
107 struct intr_config_hook irq_hook;
110 client_list_t clients;
112 struct tegra_drm *tegra_drm;
117 host1x_output_poll_changed(struct drm_device *drm_dev)
119 struct tegra_drm *drm;
121 drm = container_of(drm_dev, struct tegra_drm, drm_dev);
123 drm_fb_helper_hotplug_event(&drm->fb->fb_helper);
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,
133 host1x_drm_init(struct host1x_softc *sc)
135 struct client_info *entry;
140 TAILQ_FOREACH(entry, &sc->clients, list_e) {
141 if (entry->activated)
143 rv = TEGRA_DRM_INIT_CLIENT(entry->client, sc->dev,
146 device_printf(sc->dev,
147 "Cannot init DRM client %s: %d\n",
148 device_get_name(entry->client), rv);
151 entry->activated = 1;
159 host1x_drm_exit(struct host1x_softc *sc)
161 struct client_info *entry;
163 #ifdef FREEBSD_NOTYET
164 struct drm_device *dev, *tmp;
167 if (!sc->drm_inited) {
171 TAILQ_FOREACH_REVERSE(entry, &sc->clients, client_list, list_e) {
172 if (!entry->activated)
174 rv = TEGRA_DRM_EXIT_CLIENT(entry->client, sc->dev,
177 device_printf(sc->dev,
178 "Cannot exit DRM client %s: %d\n",
179 device_get_name(entry->client), rv);
181 entry->activated = 0;
184 #ifdef FREEBSD_NOTYET
185 list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item)
195 host1x_drm_load(struct drm_device *drm_dev, unsigned long flags)
197 struct host1x_softc *sc;
200 sc = device_get_softc(drm_dev->dev);
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;
209 rv = host1x_drm_init(sc);
213 drm_dev->irq_enabled = true;
214 drm_dev->max_vblank_count = 0xffffffff;
215 drm_dev->vblank_disable_allowed = true;
217 rv = drm_vblank_init(drm_dev, drm_dev->mode_config.num_crtc);
221 drm_mode_config_reset(drm_dev);
223 rv = tegra_drm_fb_init(drm_dev);
226 drm_kms_helper_poll_init(drm_dev);
231 tegra_drm_fb_destroy(drm_dev);
232 drm_vblank_cleanup(drm_dev);
236 drm_mode_config_cleanup(drm_dev);
242 host1x_drm_unload(struct drm_device *drm_dev)
244 struct host1x_softc *sc;
247 sc = device_get_softc(drm_dev->dev);
249 drm_kms_helper_poll_fini(drm_dev);
250 tegra_drm_fb_destroy(drm_dev);
251 drm_mode_config_cleanup(drm_dev);
253 rv = host1x_drm_exit(sc);
260 host1x_drm_open(struct drm_device *drm_dev, struct drm_file *filp)
267 tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
269 struct drm_crtc *crtc;
271 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
272 tegra_dc_cancel_page_flip(crtc, file);
276 host1x_drm_lastclose(struct drm_device *drm_dev)
279 struct tegra_drm *drm;
281 drm = container_of(drm_dev, struct tegra_drm, drm_dev);
283 drm_fb_helper_restore_fbdev_mode(&drm->fb->fb_helper);
287 host1x_drm_enable_vblank(struct drm_device *drm_dev, int pipe)
289 struct drm_crtc *crtc;
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);
301 host1x_drm_disable_vblank(struct drm_device *drm_dev, int pipe)
303 struct drm_crtc *crtc;
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);
314 static struct drm_ioctl_desc host1x_drm_ioctls[] = {
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,
326 .get_vblank_counter = drm_vblank_count,
327 .enable_vblank = host1x_drm_enable_vblank,
328 .disable_vblank = host1x_drm_disable_vblank,
330 /* Fields filled by tegra_bo_driver_register()
337 .ioctls = host1x_drm_ioctls,
338 .num_ioctls = nitems(host1x_drm_ioctls),
343 .major = DRIVER_MAJOR,
344 .minor = DRIVER_MINOR,
345 .patchlevel = DRIVER_PATCHLEVEL,
349 * ----------------- Device methods -------------------------
352 host1x_irq_hook(void *arg)
354 struct host1x_softc *sc;
358 config_intrhook_disestablish(&sc->irq_hook);
360 tegra_bo_driver_register(&tegra_drm_driver);
361 rv = drm_get_platform_dev(sc->dev, &sc->tegra_drm->drm_dev,
364 device_printf(sc->dev, "drm_get_platform_dev(): %d\n", rv);
371 static struct fb_info *
372 host1x_fb_helper_getinfo(device_t dev)
374 struct host1x_softc *sc;
376 sc = device_get_softc(dev);
377 if (sc->tegra_drm == NULL)
379 return (tegra_drm_fb_getinfo(&sc->tegra_drm->drm_dev));
383 host1x_register_client(device_t dev, device_t client)
385 struct host1x_softc *sc;
386 struct client_info *entry;
388 sc = device_get_softc(dev);
390 entry = malloc(sizeof(struct client_info), M_DEVBUF, M_WAITOK | M_ZERO);
391 entry->client = client;
392 entry->activated = 0;
395 TAILQ_INSERT_TAIL(&sc->clients, entry, list_e);
402 host1x_deregister_client(device_t dev, device_t client)
404 struct host1x_softc *sc;
405 struct client_info *entry;
407 sc = device_get_softc(dev);
410 TAILQ_FOREACH(entry, &sc->clients, list_e) {
411 if (entry->client == client) {
412 if (entry->activated)
413 panic("Tegra DRM: Attempt to deregister "
415 TAILQ_REMOVE(&sc->clients, entry, list_e);
416 free(entry, M_DEVBUF);
427 host1x_gen_intr(void *arg)
429 struct host1x_softc *sc;
431 sc = (struct host1x_softc *)arg;
437 host1x_syncpt_intr(void *arg)
439 struct host1x_softc *sc;
441 sc = (struct host1x_softc *)arg;
447 host1x_new_pass(device_t dev)
449 struct host1x_softc *sc;
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
457 sc = device_get_softc(dev);
458 if (sc->attach_done || bus_current_pass < BUS_PASS_DEFAULT) {
459 bus_generic_new_pass(dev);
464 node = ofw_bus_get_node(dev);
466 /* Allocate our IRQ resource. */
468 sc->syncpt_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
470 if (sc->syncpt_irq_res == NULL) {
471 device_printf(dev, "Cannot allocate interrupt.\n");
476 sc->gen_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
478 if (sc->gen_irq_res == NULL) {
479 device_printf(dev, "Cannot allocate interrupt.\n");
485 rv = hwreset_get_by_ofw_name(sc->dev, 0, "host1x", &sc->reset);
487 device_printf(dev, "Cannot get fuse reset\n");
490 rv = clk_get_by_ofw_index(sc->dev, 0, 0, &sc->clk);
492 device_printf(dev, "Cannot get i2c clock: %d\n", rv);
496 rv = clk_enable(sc->clk);
498 device_printf(dev, "Cannot enable clock: %d\n", rv);
501 rv = hwreset_deassert(sc->reset);
503 device_printf(sc->dev, "Cannot clear reset\n");
507 /* Setup interrupts */
508 rv = bus_setup_intr(dev, sc->gen_irq_res,
509 INTR_TYPE_MISC | INTR_MPSAFE, NULL, host1x_gen_intr,
512 device_printf(dev, "Cannot setup gen interrupt.\n");
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);
520 device_printf(dev, "Cannot setup syncpt interrupt.\n");
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);
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);
540 host1x_probe(device_t dev)
543 if (!ofw_bus_status_okay(dev))
546 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
549 return (BUS_PROBE_DEFAULT);
553 host1x_attach(device_t dev)
556 struct host1x_softc *sc;
558 sc = device_get_softc(dev);
559 sc->tegra_drm = malloc(sizeof(struct tegra_drm), DRM_MEM_DRIVER,
562 /* crosslink together all worlds */
564 sc->tegra_drm->drm_dev.dev_private = &sc->tegra_drm;
565 sc->tegra_drm->drm_dev.dev = dev;
567 TAILQ_INIT(&sc->clients);
571 /* Get the memory resource for the register mapping. */
573 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
575 if (sc->mem_res == NULL) {
576 device_printf(dev, "Cannot map registers.\n");
581 return (bus_generic_attach(dev));
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);
593 host1x_detach(device_t dev)
595 struct host1x_softc *sc;
597 sc = device_get_softc(dev);
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);
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);
618 return (bus_generic_detach(dev));
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),
628 DEVMETHOD(bus_new_pass, host1x_new_pass),
630 /* Framebuffer service methods */
631 DEVMETHOD(fb_getinfo, host1x_fb_helper_getinfo),
633 /* tegra drm interface */
634 DEVMETHOD(tegra_drm_register_client, host1x_register_client),
635 DEVMETHOD(tegra_drm_deregister_client, host1x_deregister_client),
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);
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);