xref: /netbsd-src/sys/arch/arm/nvidia/tegra_drm.c (revision bba6ca2d0d35add2de762c1e03f18e5ac1b1f92d)
1*bba6ca2dSandvar /* $NetBSD: tegra_drm.c,v 1.16 2022/04/21 21:22:25 andvar Exp $ */
2056d7a1bSjmcneill 
3056d7a1bSjmcneill /*-
4056d7a1bSjmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5056d7a1bSjmcneill  * All rights reserved.
6056d7a1bSjmcneill  *
7056d7a1bSjmcneill  * Redistribution and use in source and binary forms, with or without
8056d7a1bSjmcneill  * modification, are permitted provided that the following conditions
9056d7a1bSjmcneill  * are met:
10056d7a1bSjmcneill  * 1. Redistributions of source code must retain the above copyright
11056d7a1bSjmcneill  *    notice, this list of conditions and the following disclaimer.
12056d7a1bSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13056d7a1bSjmcneill  *    notice, this list of conditions and the following disclaimer in the
14056d7a1bSjmcneill  *    documentation and/or other materials provided with the distribution.
15056d7a1bSjmcneill  *
16056d7a1bSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17056d7a1bSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18056d7a1bSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19056d7a1bSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20056d7a1bSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21056d7a1bSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22056d7a1bSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23056d7a1bSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24056d7a1bSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25056d7a1bSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26056d7a1bSjmcneill  * SUCH DAMAGE.
27056d7a1bSjmcneill  */
28056d7a1bSjmcneill 
29056d7a1bSjmcneill #include <sys/cdefs.h>
30*bba6ca2dSandvar __KERNEL_RCSID(0, "$NetBSD: tegra_drm.c,v 1.16 2022/04/21 21:22:25 andvar Exp $");
31056d7a1bSjmcneill 
32056d7a1bSjmcneill #include <sys/param.h>
33056d7a1bSjmcneill #include <sys/bus.h>
34056d7a1bSjmcneill #include <sys/device.h>
35056d7a1bSjmcneill #include <sys/intr.h>
36056d7a1bSjmcneill #include <sys/systm.h>
37056d7a1bSjmcneill #include <sys/kernel.h>
38056d7a1bSjmcneill #include <sys/conf.h>
39056d7a1bSjmcneill 
40056d7a1bSjmcneill #include <uvm/uvm_extern.h>
41056d7a1bSjmcneill #include <uvm/uvm_device.h>
42056d7a1bSjmcneill 
43edfa016bSriastradh #include <drm/drm_drv.h>
44edfa016bSriastradh #include <drm/drm_encoder.h>
45056d7a1bSjmcneill 
46056d7a1bSjmcneill #include <arm/nvidia/tegra_reg.h>
47056d7a1bSjmcneill #include <arm/nvidia/tegra_var.h>
48056d7a1bSjmcneill #include <arm/nvidia/tegra_drm.h>
49056d7a1bSjmcneill 
50d59db8d0Sjmcneill #include <dev/fdt/fdtvar.h>
51d59db8d0Sjmcneill 
52056d7a1bSjmcneill static int	tegra_drm_match(device_t, cfdata_t, void *);
53056d7a1bSjmcneill static void	tegra_drm_attach(device_t, device_t, void *);
54056d7a1bSjmcneill 
55056d7a1bSjmcneill static int	tegra_drm_load(struct drm_device *, unsigned long);
56edfa016bSriastradh static void	tegra_drm_unload(struct drm_device *);
57056d7a1bSjmcneill 
58791424f4Sriastradh static void	tegra_drm_task_work(struct work *, void *);
59791424f4Sriastradh 
60056d7a1bSjmcneill static struct drm_driver tegra_drm_driver = {
618f23c6b1Sjmcneill 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
62056d7a1bSjmcneill 	.dev_priv_size = 0,
63056d7a1bSjmcneill 	.load = tegra_drm_load,
64056d7a1bSjmcneill 	.unload = tegra_drm_unload,
65056d7a1bSjmcneill 
66d584336aSjmcneill 	.gem_free_object = drm_gem_cma_free_object,
678f23c6b1Sjmcneill 	.mmap_object = drm_gem_or_legacy_mmap_object,
68d584336aSjmcneill 	.gem_uvm_ops = &drm_gem_cma_uvm_ops,
69056d7a1bSjmcneill 
70d584336aSjmcneill 	.dumb_create = drm_gem_cma_dumb_create,
71056d7a1bSjmcneill 
722245cb5cSjmcneill 	.get_vblank_counter = tegra_drm_get_vblank_counter,
732245cb5cSjmcneill 	.enable_vblank = tegra_drm_enable_vblank,
742245cb5cSjmcneill 	.disable_vblank = tegra_drm_disable_vblank,
752245cb5cSjmcneill 
76056d7a1bSjmcneill 	.name = DRIVER_NAME,
77056d7a1bSjmcneill 	.desc = DRIVER_DESC,
78056d7a1bSjmcneill 	.date = DRIVER_DATE,
79056d7a1bSjmcneill 	.major = DRIVER_MAJOR,
80056d7a1bSjmcneill 	.minor = DRIVER_MINOR,
81a21d5411Sriastradh 	.patchlevel = DRIVER_PATCHLEVEL,
82056d7a1bSjmcneill };
83056d7a1bSjmcneill 
84056d7a1bSjmcneill CFATTACH_DECL_NEW(tegra_drm, sizeof(struct tegra_drm_softc),
85056d7a1bSjmcneill 	tegra_drm_match, tegra_drm_attach, NULL, NULL);
86056d7a1bSjmcneill 
876e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
886e54367aSthorpej 	{ .compat = "nvidia,tegra124-host1x" },
896e54367aSthorpej 	DEVICE_COMPAT_EOL
906e54367aSthorpej };
916e54367aSthorpej 
92056d7a1bSjmcneill static int
tegra_drm_match(device_t parent,cfdata_t cf,void * aux)93056d7a1bSjmcneill tegra_drm_match(device_t parent, cfdata_t cf, void *aux)
94056d7a1bSjmcneill {
95d59db8d0Sjmcneill 	struct fdt_attach_args * const faa = aux;
96d59db8d0Sjmcneill 
976e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
98056d7a1bSjmcneill }
99056d7a1bSjmcneill 
1006e54367aSthorpej static const struct device_compatible_entry hdmi_compat[] = {
1016e54367aSthorpej 	{ .compat = "nvidia,tegra124-hdmi" },
1026e54367aSthorpej 	DEVICE_COMPAT_EOL
1036e54367aSthorpej };
1046e54367aSthorpej 
1056e54367aSthorpej static const struct device_compatible_entry dc_compat[] = {
1066e54367aSthorpej 	{ .compat = "nvidia,tegra124-dc" },
1076e54367aSthorpej 	DEVICE_COMPAT_EOL
1086e54367aSthorpej };
1096e54367aSthorpej 
110056d7a1bSjmcneill static void
tegra_drm_attach(device_t parent,device_t self,void * aux)111056d7a1bSjmcneill tegra_drm_attach(device_t parent, device_t self, void *aux)
112056d7a1bSjmcneill {
113056d7a1bSjmcneill 	struct tegra_drm_softc * const sc = device_private(self);
114d59db8d0Sjmcneill 	struct fdt_attach_args * const faa = aux;
115056d7a1bSjmcneill 	struct drm_driver * const driver = &tegra_drm_driver;
116d59db8d0Sjmcneill 	prop_dictionary_t prop = device_properties(self);
117d59db8d0Sjmcneill 	int error, node, hdmi_phandle, ddc_phandle;
1186e54367aSthorpej 	static const char * const hdmi_supplies[] = {
119d59db8d0Sjmcneill 		"hdmi-supply", "pll-supply", "vdd-supply"
120d59db8d0Sjmcneill 	};
121d59db8d0Sjmcneill 	struct fdtbus_regulator *reg;
12293e0bfebSjmcneill 	u_int n, ndc;
123056d7a1bSjmcneill 
124056d7a1bSjmcneill 	sc->sc_dev = self;
125d59db8d0Sjmcneill 	sc->sc_dmat = faa->faa_dmat;
126d59db8d0Sjmcneill 	sc->sc_bst = faa->faa_bst;
127d59db8d0Sjmcneill 	sc->sc_phandle = faa->faa_phandle;
128791424f4Sriastradh 	sc->sc_task_thread = NULL;
129791424f4Sriastradh 	SIMPLEQ_INIT(&sc->sc_tasks);
130791424f4Sriastradh 	if (workqueue_create(&sc->sc_task_wq, "tegradrm",
131791424f4Sriastradh 	    &tegra_drm_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE)) {
132791424f4Sriastradh 		aprint_error_dev(self, "unable to create workqueue\n");
133791424f4Sriastradh 		sc->sc_task_wq = NULL;
134791424f4Sriastradh 		return;
135791424f4Sriastradh 	}
136056d7a1bSjmcneill 
137056d7a1bSjmcneill 	aprint_naive("\n");
138056d7a1bSjmcneill 	aprint_normal("\n");
139056d7a1bSjmcneill 
14093e0bfebSjmcneill 	sc->sc_clk_host1x = fdtbus_clock_get_index(faa->faa_phandle, 0);
14193e0bfebSjmcneill 	if (sc->sc_clk_host1x == NULL) {
14293e0bfebSjmcneill 		aprint_error_dev(self, "couldn't get clock host1x\n");
14393e0bfebSjmcneill 		return;
14493e0bfebSjmcneill 	}
14593e0bfebSjmcneill 	sc->sc_rst_host1x = fdtbus_reset_get(faa->faa_phandle, "host1x");
14693e0bfebSjmcneill 	if (sc->sc_clk_host1x == NULL || sc->sc_rst_host1x == NULL) {
14793e0bfebSjmcneill 		aprint_error_dev(self, "couldn't get reset host1x\n");
14893e0bfebSjmcneill 		return;
14993e0bfebSjmcneill 	}
150d59db8d0Sjmcneill 
15193e0bfebSjmcneill 	ndc = 0;
152d59db8d0Sjmcneill 	hdmi_phandle = -1;
153d59db8d0Sjmcneill 	for (node = OF_child(faa->faa_phandle); node; node = OF_peer(node)) {
1546e54367aSthorpej 		if (of_compatible_match(node, hdmi_compat)) {
15593e0bfebSjmcneill 			sc->sc_clk_hdmi = fdtbus_clock_get(node, "hdmi");
15693e0bfebSjmcneill 			sc->sc_clk_hdmi_parent = fdtbus_clock_get(node,
15793e0bfebSjmcneill 			    "parent");
15893e0bfebSjmcneill 			sc->sc_rst_hdmi = fdtbus_reset_get(node, "hdmi");
159d59db8d0Sjmcneill 			hdmi_phandle = node;
1606e54367aSthorpej 		} else if (of_compatible_match(node, dc_compat) &&
16193e0bfebSjmcneill 			   ndc < __arraycount(sc->sc_clk_dc)) {
16293e0bfebSjmcneill 			sc->sc_clk_dc[ndc] = fdtbus_clock_get(node, "dc");
16393e0bfebSjmcneill 			sc->sc_clk_dc_parent[ndc] = fdtbus_clock_get(node,
16493e0bfebSjmcneill 			    "parent");
16593e0bfebSjmcneill 			sc->sc_rst_dc[ndc] = fdtbus_reset_get(node, "dc");
16693e0bfebSjmcneill 			++ndc;
167d59db8d0Sjmcneill 		}
168d59db8d0Sjmcneill 	}
169d59db8d0Sjmcneill 	if (hdmi_phandle >= 0) {
170d59db8d0Sjmcneill 		ddc_phandle = fdtbus_get_phandle(hdmi_phandle,
171d59db8d0Sjmcneill 		    "nvidia,ddc-i2c-bus");
172d59db8d0Sjmcneill 		if (ddc_phandle >= 0) {
173eb3ed4d4Sskrll 			sc->sc_ddc = fdtbus_i2c_get_tag(ddc_phandle);
174d59db8d0Sjmcneill 		}
175d59db8d0Sjmcneill 
176d59db8d0Sjmcneill 		sc->sc_pin_hpd = fdtbus_gpio_acquire(hdmi_phandle,
177d59db8d0Sjmcneill 		    "nvidia,hpd-gpio", GPIO_PIN_INPUT);
178d59db8d0Sjmcneill 
179d59db8d0Sjmcneill 		for (n = 0; n < __arraycount(hdmi_supplies); n++) {
180d59db8d0Sjmcneill 			const char *supply = hdmi_supplies[n];
181d59db8d0Sjmcneill 			reg = fdtbus_regulator_acquire(hdmi_phandle, supply);
182d59db8d0Sjmcneill 			if (reg == NULL) {
183d59db8d0Sjmcneill 				aprint_error_dev(self, "couldn't acquire %s\n",
184d59db8d0Sjmcneill 				    supply);
185d59db8d0Sjmcneill 				continue;
186d59db8d0Sjmcneill 			}
187d59db8d0Sjmcneill 			if (fdtbus_regulator_enable(reg) != 0) {
188d59db8d0Sjmcneill 				aprint_error_dev(self, "couldn't enable %s\n",
189d59db8d0Sjmcneill 				    supply);
190d59db8d0Sjmcneill 			}
191d59db8d0Sjmcneill 			fdtbus_regulator_release(reg);
192d59db8d0Sjmcneill 		}
193d59db8d0Sjmcneill 	}
194d59db8d0Sjmcneill 
19593e0bfebSjmcneill 	fdtbus_reset_assert(sc->sc_rst_host1x);
19693e0bfebSjmcneill 	error = clk_enable(sc->sc_clk_host1x);
19793e0bfebSjmcneill 	if (error) {
19893e0bfebSjmcneill 		aprint_error_dev(self, "couldn't enable clock host1x: %d\n",
19993e0bfebSjmcneill 		    error);
20093e0bfebSjmcneill 		return;
20193e0bfebSjmcneill 	}
20293e0bfebSjmcneill 	fdtbus_reset_deassert(sc->sc_rst_host1x);
20393e0bfebSjmcneill 
204d59db8d0Sjmcneill 	prop_dictionary_get_bool(prop, "force-dvi", &sc->sc_force_dvi);
205d59db8d0Sjmcneill 
206056d7a1bSjmcneill 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
2075fd19f0eSriastradh 	if (IS_ERR(sc->sc_ddev)) {
208056d7a1bSjmcneill 		aprint_error_dev(self, "couldn't allocate DRM device\n");
209056d7a1bSjmcneill 		return;
210056d7a1bSjmcneill 	}
211056d7a1bSjmcneill 	sc->sc_ddev->dev_private = sc;
212b0225f7dSjmcneill 	sc->sc_ddev->bst = sc->sc_bst;
213b0225f7dSjmcneill 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
214b0225f7dSjmcneill 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
215b0225f7dSjmcneill 	sc->sc_ddev->dmat_subregion_p = false;
216056d7a1bSjmcneill 
217791424f4Sriastradh 	/*
218791424f4Sriastradh 	 * Cause any tasks issued synchronously during attach to be
219791424f4Sriastradh 	 * processed at the end of this function.
220791424f4Sriastradh 	 */
221791424f4Sriastradh 	sc->sc_task_thread = curlwp;
222791424f4Sriastradh 
223056d7a1bSjmcneill 	error = -drm_dev_register(sc->sc_ddev, 0);
224056d7a1bSjmcneill 	if (error) {
225edfa016bSriastradh 		drm_dev_put(sc->sc_ddev);
226791424f4Sriastradh 		sc->sc_ddev = NULL;
227056d7a1bSjmcneill 		aprint_error_dev(self, "couldn't register DRM device: %d\n",
228056d7a1bSjmcneill 		    error);
229791424f4Sriastradh 		goto out;
230056d7a1bSjmcneill 	}
231791424f4Sriastradh 	sc->sc_dev_registered = true;
232056d7a1bSjmcneill 
233056d7a1bSjmcneill 	aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n",
234056d7a1bSjmcneill 	    driver->name, driver->major, driver->minor, driver->patchlevel,
235056d7a1bSjmcneill 	    driver->date, sc->sc_ddev->primary->index);
236056d7a1bSjmcneill 
237791424f4Sriastradh 	/*
238791424f4Sriastradh 	 * Process asynchronous tasks queued synchronously during
239791424f4Sriastradh 	 * attach.  This will be for display detection to attach a
240791424f4Sriastradh 	 * framebuffer, so we have the opportunity for a console device
241791424f4Sriastradh 	 * to attach before autoconf has completed, in time for init(8)
242791424f4Sriastradh 	 * to find that console without panicking.
243791424f4Sriastradh 	 */
244791424f4Sriastradh 	while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
245791424f4Sriastradh 		struct tegra_drm_task *const task =
246791424f4Sriastradh 		    SIMPLEQ_FIRST(&sc->sc_tasks);
247791424f4Sriastradh 
248791424f4Sriastradh 		SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, tdt_u.queue);
249791424f4Sriastradh 		(*task->tdt_fn)(task);
250791424f4Sriastradh 	}
251791424f4Sriastradh 
252*bba6ca2dSandvar out:	/* Cause any subsequent tasks to be processed by the workqueue.  */
253791424f4Sriastradh 	atomic_store_relaxed(&sc->sc_task_thread, NULL);
254056d7a1bSjmcneill }
255056d7a1bSjmcneill 
256056d7a1bSjmcneill static int
tegra_drm_load(struct drm_device * ddev,unsigned long flags)257056d7a1bSjmcneill tegra_drm_load(struct drm_device *ddev, unsigned long flags)
258056d7a1bSjmcneill {
259056d7a1bSjmcneill 	int error;
260056d7a1bSjmcneill 
261056d7a1bSjmcneill 	error = tegra_drm_mode_init(ddev);
262056d7a1bSjmcneill 	if (error)
263056d7a1bSjmcneill 		goto drmerr;
264056d7a1bSjmcneill 
265056d7a1bSjmcneill 	error = tegra_drm_fb_init(ddev);
266056d7a1bSjmcneill 	if (error)
267056d7a1bSjmcneill 		goto drmerr;
268056d7a1bSjmcneill 
269056d7a1bSjmcneill 	return 0;
270056d7a1bSjmcneill 
271056d7a1bSjmcneill drmerr:
272056d7a1bSjmcneill 	drm_mode_config_cleanup(ddev);
273056d7a1bSjmcneill 
274056d7a1bSjmcneill 	return error;
275056d7a1bSjmcneill }
276056d7a1bSjmcneill 
277edfa016bSriastradh static void
tegra_drm_unload(struct drm_device * ddev)278056d7a1bSjmcneill tegra_drm_unload(struct drm_device *ddev)
279056d7a1bSjmcneill {
280056d7a1bSjmcneill 
281edfa016bSriastradh 	drm_mode_config_cleanup(ddev);
282056d7a1bSjmcneill }
283791424f4Sriastradh 
284791424f4Sriastradh static void
tegra_drm_task_work(struct work * work,void * cookie)285791424f4Sriastradh tegra_drm_task_work(struct work *work, void *cookie)
286791424f4Sriastradh {
287791424f4Sriastradh 	struct tegra_drm_task *task = container_of(work, struct tegra_drm_task,
288791424f4Sriastradh 	    tdt_u.work);
289791424f4Sriastradh 
290791424f4Sriastradh 	(*task->tdt_fn)(task);
291791424f4Sriastradh }
292791424f4Sriastradh 
293791424f4Sriastradh void
tegra_task_init(struct tegra_drm_task * task,void (* fn)(struct tegra_drm_task *))294791424f4Sriastradh tegra_task_init(struct tegra_drm_task *task,
295791424f4Sriastradh     void (*fn)(struct tegra_drm_task *))
296791424f4Sriastradh {
297791424f4Sriastradh 
298791424f4Sriastradh 	task->tdt_fn = fn;
299791424f4Sriastradh }
300791424f4Sriastradh 
301791424f4Sriastradh void
tegra_task_schedule(device_t self,struct tegra_drm_task * task)302791424f4Sriastradh tegra_task_schedule(device_t self, struct tegra_drm_task *task)
303791424f4Sriastradh {
304791424f4Sriastradh 	struct tegra_drm_softc *sc = device_private(self);
305791424f4Sriastradh 
306791424f4Sriastradh 	if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
307791424f4Sriastradh 		SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, tdt_u.queue);
308791424f4Sriastradh 	else
309791424f4Sriastradh 		workqueue_enqueue(sc->sc_task_wq, &task->tdt_u.work, NULL);
310791424f4Sriastradh }
311