xref: /netbsd-src/sys/arch/arm/nvidia/tegra_fb.c (revision 791424f4661927db92e291f5c62874bd37103c9d)
1*791424f4Sriastradh /* $NetBSD: tegra_fb.c,v 1.6 2021/12/19 12:44:50 riastradh 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*791424f4Sriastradh __KERNEL_RCSID(0, "$NetBSD: tegra_fb.c,v 1.6 2021/12/19 12:44:50 riastradh Exp $");
31056d7a1bSjmcneill 
32056d7a1bSjmcneill #include <sys/param.h>
33056d7a1bSjmcneill #include <sys/bus.h>
34056d7a1bSjmcneill #include <sys/device.h>
35056d7a1bSjmcneill 
36edfa016bSriastradh #include <drm/drm_drv.h>
37056d7a1bSjmcneill #include <drm/drmfb.h>
38056d7a1bSjmcneill 
39056d7a1bSjmcneill #include <arm/nvidia/tegra_var.h>
40056d7a1bSjmcneill #include <arm/nvidia/tegra_drm.h>
41056d7a1bSjmcneill 
42056d7a1bSjmcneill static int	tegra_fb_match(device_t, cfdata_t, void *);
43056d7a1bSjmcneill static void	tegra_fb_attach(device_t, device_t, void *);
44056d7a1bSjmcneill 
45*791424f4Sriastradh static void	tegra_fb_init(struct tegra_drm_task *);
46*791424f4Sriastradh 
47056d7a1bSjmcneill static bool	tegra_fb_shutdown(device_t, int);
48056d7a1bSjmcneill 
49056d7a1bSjmcneill struct tegra_fb_softc {
50056d7a1bSjmcneill 	struct drmfb_softc	sc_drmfb;
51056d7a1bSjmcneill 	device_t		sc_dev;
52056d7a1bSjmcneill 	struct tegra_drm_softc	*sc_drm;
53056d7a1bSjmcneill 	struct tegra_drmfb_attach_args sc_tfa;
548f23c6b1Sjmcneill 	struct tegra_framebuffer *sc_fb;
55*791424f4Sriastradh 	struct tegra_drm_task	sc_attach_task;
56056d7a1bSjmcneill };
57056d7a1bSjmcneill 
58056d7a1bSjmcneill static paddr_t	tegra_fb_mmapfb(struct drmfb_softc *, off_t, int);
59056d7a1bSjmcneill static int	tegra_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
60056d7a1bSjmcneill 			       lwp_t *);
61056d7a1bSjmcneill 
62056d7a1bSjmcneill 
63056d7a1bSjmcneill static const struct drmfb_params tegrafb_drmfb_params = {
64056d7a1bSjmcneill 	.dp_mmapfb = tegra_fb_mmapfb,
65056d7a1bSjmcneill 	.dp_ioctl = tegra_fb_ioctl,
66056d7a1bSjmcneill };
67056d7a1bSjmcneill 
68056d7a1bSjmcneill CFATTACH_DECL_NEW(tegra_fb, sizeof(struct tegra_fb_softc),
69056d7a1bSjmcneill 	tegra_fb_match, tegra_fb_attach, NULL, NULL);
70056d7a1bSjmcneill 
71056d7a1bSjmcneill static int
tegra_fb_match(device_t parent,cfdata_t cf,void * aux)72056d7a1bSjmcneill tegra_fb_match(device_t parent, cfdata_t cf, void *aux)
73056d7a1bSjmcneill {
74056d7a1bSjmcneill 	return 1;
75056d7a1bSjmcneill }
76056d7a1bSjmcneill 
77056d7a1bSjmcneill static void
tegra_fb_attach(device_t parent,device_t self,void * aux)78056d7a1bSjmcneill tegra_fb_attach(device_t parent, device_t self, void *aux)
79056d7a1bSjmcneill {
80056d7a1bSjmcneill 	struct tegra_fb_softc * const sc = device_private(self);
81056d7a1bSjmcneill 	struct tegra_drm_softc * const drmsc = device_private(parent);
82056d7a1bSjmcneill 	struct tegra_drmfb_attach_args * const tfa = aux;
83056d7a1bSjmcneill 
84056d7a1bSjmcneill 	sc->sc_dev = self;
85056d7a1bSjmcneill 	sc->sc_drm = drmsc;
86056d7a1bSjmcneill 	sc->sc_tfa = *tfa;
878f23c6b1Sjmcneill 	sc->sc_fb = to_tegra_framebuffer(tfa->tfa_fb_helper->fb);
88056d7a1bSjmcneill 
89056d7a1bSjmcneill 	aprint_naive("\n");
90056d7a1bSjmcneill 	aprint_normal("\n");
91056d7a1bSjmcneill 
92*791424f4Sriastradh 	tegra_task_init(&sc->sc_attach_task, &tegra_fb_init);
93*791424f4Sriastradh 	tegra_task_schedule(parent, &sc->sc_attach_task);
94*791424f4Sriastradh }
95*791424f4Sriastradh 
96*791424f4Sriastradh static void
tegra_fb_init(struct tegra_drm_task * task)97*791424f4Sriastradh tegra_fb_init(struct tegra_drm_task *task)
98*791424f4Sriastradh {
99*791424f4Sriastradh 	struct tegra_fb_softc *sc = container_of(task, struct tegra_fb_softc,
100*791424f4Sriastradh 	    sc_attach_task);
101*791424f4Sriastradh 	device_t self = sc->sc_dev;
102*791424f4Sriastradh 	struct tegra_drmfb_attach_args * const tfa = &sc->sc_tfa;
1031b6aaa82Smaya 	const struct drmfb_attach_args da = {
1041b6aaa82Smaya 		.da_dev = self,
1051b6aaa82Smaya 		.da_fb_helper = tfa->tfa_fb_helper,
1061b6aaa82Smaya 		.da_fb_sizes = &tfa->tfa_fb_sizes,
107d584336aSjmcneill 		.da_fb_vaddr = sc->sc_fb->obj->vaddr,
1081b6aaa82Smaya 		.da_fb_linebytes = tfa->tfa_fb_linebytes,
1091b6aaa82Smaya 		.da_params = &tegrafb_drmfb_params,
1101b6aaa82Smaya 	};
111*791424f4Sriastradh 	int error;
112056d7a1bSjmcneill 
113056d7a1bSjmcneill 	error = drmfb_attach(&sc->sc_drmfb, &da);
114056d7a1bSjmcneill 	if (error) {
115056d7a1bSjmcneill 		aprint_error_dev(self, "failed to attach drmfb: %d\n", error);
116056d7a1bSjmcneill 		return;
117056d7a1bSjmcneill 	}
118056d7a1bSjmcneill 
119056d7a1bSjmcneill 	pmf_device_register1(self, NULL, NULL, tegra_fb_shutdown);
120056d7a1bSjmcneill }
121056d7a1bSjmcneill 
122056d7a1bSjmcneill static bool
tegra_fb_shutdown(device_t self,int flags)123056d7a1bSjmcneill tegra_fb_shutdown(device_t self, int flags)
124056d7a1bSjmcneill {
125056d7a1bSjmcneill 	struct tegra_fb_softc * const sc = device_private(self);
126056d7a1bSjmcneill 
127056d7a1bSjmcneill 	return drmfb_shutdown(&sc->sc_drmfb, flags);
128056d7a1bSjmcneill }
129056d7a1bSjmcneill 
130056d7a1bSjmcneill static paddr_t
tegra_fb_mmapfb(struct drmfb_softc * sc,off_t off,int prot)131056d7a1bSjmcneill tegra_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
132056d7a1bSjmcneill {
133056d7a1bSjmcneill 	struct tegra_fb_softc * const tfb_sc = (struct tegra_fb_softc *)sc;
134d584336aSjmcneill 	struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
135056d7a1bSjmcneill 
136056d7a1bSjmcneill 	KASSERT(off >= 0);
1378f23c6b1Sjmcneill 	KASSERT(off < obj->dmasize);
138056d7a1bSjmcneill 
1398f23c6b1Sjmcneill 	return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
1408f23c6b1Sjmcneill 	    BUS_DMA_PREFETCHABLE);
141056d7a1bSjmcneill }
142056d7a1bSjmcneill 
143056d7a1bSjmcneill static int
tegra_fb_ioctl(struct drmfb_softc * sc,u_long cmd,void * data,int flag,lwp_t * l)144056d7a1bSjmcneill tegra_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
145056d7a1bSjmcneill     lwp_t *l)
146056d7a1bSjmcneill {
147056d7a1bSjmcneill 	struct wsdisplayio_bus_id *busid;
148056d7a1bSjmcneill 	struct wsdisplayio_fbinfo *fbi;
149056d7a1bSjmcneill 	struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
150056d7a1bSjmcneill 	int error;
151056d7a1bSjmcneill 
152056d7a1bSjmcneill 	switch (cmd) {
153056d7a1bSjmcneill 	case WSDISPLAYIO_GET_BUSID:
154056d7a1bSjmcneill 		busid = data;
155056d7a1bSjmcneill 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
156056d7a1bSjmcneill 		return 0;
157056d7a1bSjmcneill 	case WSDISPLAYIO_GTYPE:
158056d7a1bSjmcneill 		*(u_int *)data = WSDISPLAY_TYPE_TEGRA;
159056d7a1bSjmcneill 		return 0;
160056d7a1bSjmcneill 	case WSDISPLAYIO_GET_FBINFO:
161056d7a1bSjmcneill 		fbi = data;
162056d7a1bSjmcneill 		error = wsdisplayio_get_fbinfo(ri, fbi);
163056d7a1bSjmcneill 		fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
164056d7a1bSjmcneill 		return error;
165056d7a1bSjmcneill 	default:
166056d7a1bSjmcneill 		return EPASSTHROUGH;
167056d7a1bSjmcneill 	}
168056d7a1bSjmcneill }
169