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;
116 host1x_output_poll_changed(struct drm_device *drm_dev)
118 struct tegra_drm *drm;
120 drm = container_of(drm_dev, struct tegra_drm, drm_dev);
122 drm_fb_helper_hotplug_event(&drm->fb->fb_helper);
125 static const struct drm_mode_config_funcs mode_config_funcs = {
126 .fb_create = tegra_drm_fb_create,
127 .output_poll_changed = host1x_output_poll_changed,
131 host1x_drm_init(struct host1x_softc *sc)
133 struct client_info *entry;
138 TAILQ_FOREACH(entry, &sc->clients, list_e) {
139 if (entry->activated)
141 rv = TEGRA_DRM_INIT_CLIENT(entry->client, sc->dev,
144 device_printf(sc->dev,
145 "Cannot init DRM client %s: %d\n",
146 device_get_name(entry->client), rv);
149 entry->activated = 1;
157 host1x_drm_exit(struct host1x_softc *sc)
159 struct client_info *entry;
161 #ifdef FREEBSD_NOTYET
162 struct drm_device *dev, *tmp;
165 if (!sc->drm_inited) {
169 TAILQ_FOREACH_REVERSE(entry, &sc->clients, client_list, list_e) {
170 if (!entry->activated)
172 rv = TEGRA_DRM_EXIT_CLIENT(entry->client, sc->dev,
175 device_printf(sc->dev,
176 "Cannot exit DRM client %s: %d\n",
177 device_get_name(entry->client), rv);
179 entry->activated = 0;
182 #ifdef FREEBSD_NOTYET
183 list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item)
193 host1x_drm_load(struct drm_device *drm_dev, unsigned long flags)
195 struct host1x_softc *sc;
198 sc = device_get_softc(drm_dev->dev);
200 drm_mode_config_init(drm_dev);
201 drm_dev->mode_config.min_width = 32;
202 drm_dev->mode_config.min_height = 32;
203 drm_dev->mode_config.max_width = 4096;
204 drm_dev->mode_config.max_height = 4096;
205 drm_dev->mode_config.funcs = &mode_config_funcs;
207 rv = host1x_drm_init(sc);
211 drm_dev->irq_enabled = true;
212 drm_dev->max_vblank_count = 0xffffffff;
213 drm_dev->vblank_disable_allowed = true;
215 rv = drm_vblank_init(drm_dev, drm_dev->mode_config.num_crtc);
219 drm_mode_config_reset(drm_dev);
221 rv = tegra_drm_fb_init(drm_dev);
224 drm_kms_helper_poll_init(drm_dev);
229 tegra_drm_fb_destroy(drm_dev);
230 drm_vblank_cleanup(drm_dev);
234 drm_mode_config_cleanup(drm_dev);
240 host1x_drm_unload(struct drm_device *drm_dev)
242 struct host1x_softc *sc;
245 sc = device_get_softc(drm_dev->dev);
247 drm_kms_helper_poll_fini(drm_dev);
248 tegra_drm_fb_destroy(drm_dev);
249 drm_mode_config_cleanup(drm_dev);
251 rv = host1x_drm_exit(sc);
258 host1x_drm_open(struct drm_device *drm_dev, struct drm_file *filp)
265 tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
267 struct drm_crtc *crtc;
269 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
270 tegra_dc_cancel_page_flip(crtc, file);
274 host1x_drm_lastclose(struct drm_device *drm_dev)
277 struct tegra_drm *drm;
279 drm = container_of(drm_dev, struct tegra_drm, drm_dev);
281 drm_fb_helper_restore_fbdev_mode(&drm->fb->fb_helper);
285 host1x_drm_enable_vblank(struct drm_device *drm_dev, int pipe)
287 struct drm_crtc *crtc;
289 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
290 if (pipe == tegra_dc_get_pipe(crtc)) {
291 tegra_dc_enable_vblank(crtc);
299 host1x_drm_disable_vblank(struct drm_device *drm_dev, int pipe)
301 struct drm_crtc *crtc;
303 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
304 if (pipe == tegra_dc_get_pipe(crtc)) {
305 tegra_dc_disable_vblank(crtc);
311 static struct drm_ioctl_desc host1x_drm_ioctls[] = {
314 struct drm_driver tegra_drm_driver = {
315 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
316 .load = host1x_drm_load,
317 .unload = host1x_drm_unload,
318 .open = host1x_drm_open,
319 .preclose = tegra_drm_preclose,
320 .lastclose = host1x_drm_lastclose,
322 .get_vblank_counter = drm_vblank_count,
323 .enable_vblank = host1x_drm_enable_vblank,
324 .disable_vblank = host1x_drm_disable_vblank,
326 /* Fields filled by tegra_bo_driver_register()
333 .ioctls = host1x_drm_ioctls,
334 .num_ioctls = nitems(host1x_drm_ioctls),
339 .major = DRIVER_MAJOR,
340 .minor = DRIVER_MINOR,
341 .patchlevel = DRIVER_PATCHLEVEL,
345 * ----------------- Device methods -------------------------
348 host1x_irq_hook(void *arg)
350 struct host1x_softc *sc;
354 config_intrhook_disestablish(&sc->irq_hook);
356 tegra_bo_driver_register(&tegra_drm_driver);
357 rv = drm_get_platform_dev(sc->dev, &sc->tegra_drm->drm_dev,
360 device_printf(sc->dev, "drm_get_platform_dev(): %d\n", rv);
367 static struct fb_info *
368 host1x_fb_helper_getinfo(device_t dev)
370 struct host1x_softc *sc;
372 sc = device_get_softc(dev);
373 if (sc->tegra_drm == NULL)
375 return (tegra_drm_fb_getinfo(&sc->tegra_drm->drm_dev));
379 host1x_register_client(device_t dev, device_t client)
381 struct host1x_softc *sc;
382 struct client_info *entry;
384 sc = device_get_softc(dev);
386 entry = malloc(sizeof(struct client_info), M_DEVBUF, M_WAITOK | M_ZERO);
387 entry->client = client;
388 entry->activated = 0;
391 TAILQ_INSERT_TAIL(&sc->clients, entry, list_e);
398 host1x_deregister_client(device_t dev, device_t client)
400 struct host1x_softc *sc;
401 struct client_info *entry;
403 sc = device_get_softc(dev);
406 TAILQ_FOREACH(entry, &sc->clients, list_e) {
407 if (entry->client == client) {
408 if (entry->activated)
409 panic("Tegra DRM: Attempt to deregister "
411 TAILQ_REMOVE(&sc->clients, entry, list_e);
412 free(entry, M_DEVBUF);
423 host1x_gen_intr(void *arg)
425 struct host1x_softc *sc;
427 sc = (struct host1x_softc *)arg;
433 host1x_syncpt_intr(void *arg)
435 struct host1x_softc *sc;
437 sc = (struct host1x_softc *)arg;
443 host1x_new_pass(device_t dev)
445 struct host1x_softc *sc;
450 * We attach during BUS_PASS_BUS (because we must overcome simplebus),
451 * but some of our FDT resources are not ready until BUS_PASS_DEFAULT
453 sc = device_get_softc(dev);
454 if (sc->attach_done || bus_current_pass < BUS_PASS_DEFAULT) {
455 bus_generic_new_pass(dev);
460 node = ofw_bus_get_node(dev);
462 /* Allocate our IRQ resource. */
464 sc->syncpt_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
466 if (sc->syncpt_irq_res == NULL) {
467 device_printf(dev, "Cannot allocate interrupt.\n");
472 sc->gen_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
474 if (sc->gen_irq_res == NULL) {
475 device_printf(dev, "Cannot allocate interrupt.\n");
481 rv = hwreset_get_by_ofw_name(sc->dev, 0, "host1x", &sc->reset);
483 device_printf(dev, "Cannot get fuse reset\n");
486 rv = clk_get_by_ofw_index(sc->dev, 0, 0, &sc->clk);
488 device_printf(dev, "Cannot get i2c clock: %d\n", rv);
492 rv = clk_enable(sc->clk);
494 device_printf(dev, "Cannot enable clock: %d\n", rv);
497 rv = hwreset_deassert(sc->reset);
499 device_printf(sc->dev, "Cannot clear reset\n");
503 /* Setup interrupts */
504 rv = bus_setup_intr(dev, sc->gen_irq_res,
505 INTR_TYPE_MISC | INTR_MPSAFE, NULL, host1x_gen_intr,
508 device_printf(dev, "Cannot setup gen interrupt.\n");
512 rv = bus_setup_intr(dev, sc->syncpt_irq_res,
513 INTR_TYPE_MISC | INTR_MPSAFE, NULL, host1x_syncpt_intr,
514 sc, &sc->syncpt_irq_h);
516 device_printf(dev, "Cannot setup syncpt interrupt.\n");
520 simplebus_init(dev, 0);
521 for (node = OF_child(node); node > 0; node = OF_peer(node))
522 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
524 sc->irq_hook.ich_func = host1x_irq_hook;
525 sc->irq_hook.ich_arg = sc;
526 config_intrhook_establish(&sc->irq_hook);
527 bus_generic_new_pass(dev);
536 host1x_probe(device_t dev)
539 if (!ofw_bus_status_okay(dev))
542 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
545 return (BUS_PROBE_DEFAULT);
549 host1x_attach(device_t dev)
552 struct host1x_softc *sc;
554 sc = device_get_softc(dev);
555 sc->tegra_drm = malloc(sizeof(struct tegra_drm), DRM_MEM_DRIVER,
558 /* crosslink together all worlds */
560 sc->tegra_drm->drm_dev.dev_private = &sc->tegra_drm;
561 sc->tegra_drm->drm_dev.dev = dev;
563 TAILQ_INIT(&sc->clients);
567 /* Get the memory resource for the register mapping. */
569 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
571 if (sc->mem_res == NULL) {
572 device_printf(dev, "Cannot map registers.\n");
577 return (bus_generic_attach(dev));
580 if (sc->tegra_drm != NULL)
581 free(sc->tegra_drm, DRM_MEM_DRIVER);
582 if (sc->mem_res != NULL)
583 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
589 host1x_detach(device_t dev)
591 struct host1x_softc *sc;
593 sc = device_get_softc(dev);
597 if (sc->gen_irq_h != NULL)
598 bus_teardown_intr(dev, sc->gen_irq_res, sc->gen_irq_h);
599 if (sc->tegra_drm != NULL)
600 free(sc->tegra_drm, DRM_MEM_DRIVER);
602 clk_release(sc->clk);
603 if (sc->reset != NULL)
604 hwreset_release(sc->reset);
605 if (sc->syncpt_irq_h != NULL)
606 bus_teardown_intr(dev, sc->syncpt_irq_res, sc->syncpt_irq_h);
607 if (sc->gen_irq_res != NULL)
608 bus_release_resource(dev, SYS_RES_IRQ, 1, sc->gen_irq_res);
609 if (sc->syncpt_irq_res != NULL)
610 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->syncpt_irq_res);
611 if (sc->mem_res != NULL)
612 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
614 return (bus_generic_detach(dev));
617 static device_method_t host1x_methods[] = {
618 /* Device interface */
619 DEVMETHOD(device_probe, host1x_probe),
620 DEVMETHOD(device_attach, host1x_attach),
621 DEVMETHOD(device_detach, host1x_detach),
624 DEVMETHOD(bus_new_pass, host1x_new_pass),
626 /* Framebuffer service methods */
627 DEVMETHOD(fb_getinfo, host1x_fb_helper_getinfo),
629 /* tegra drm interface */
630 DEVMETHOD(tegra_drm_register_client, host1x_register_client),
631 DEVMETHOD(tegra_drm_deregister_client, host1x_deregister_client),
636 DEFINE_CLASS_1(host1x, host1x_driver, host1x_methods,
637 sizeof(struct host1x_softc), simplebus_driver);
638 EARLY_DRIVER_MODULE(host1x, simplebus, host1x_driver, 0, 0, BUS_PASS_BUS);
640 /* Bindings for fbd device. */
641 extern driver_t fbd_driver;
642 DRIVER_MODULE(fbd, host1x, fbd_driver, 0, 0);