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