xref: /netbsd-src/sys/external/bsd/drm2/i915drm/intelfb.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: intelfb.c,v 1.22 2021/12/19 11:49:12 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: intelfb.c,v 1.22 2021/12/19 11:49:12 riastradh Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 
39 #include <drm/drm_device.h>
40 #include <drm/drmfb.h>
41 #include <drm/drmfb_pci.h>
42 
43 #include "display/intel_display_types.h"
44 #include "display/intel_vga.h"
45 #include "i915_drv.h"
46 #include "i915_pci.h"
47 #include "intelfb.h"
48 
49 static int	intelfb_match(device_t, cfdata_t, void *);
50 static void	intelfb_attach(device_t, device_t, void *);
51 static int	intelfb_detach(device_t, int);
52 
53 static void	intelfb_attach_task(struct i915drmkms_task *);
54 
55 static bool	intelfb_shutdown(device_t, int);
56 
57 static paddr_t	intelfb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
58 
59 static void	intelfb_disable_vga(struct drm_device *);
60 
61 struct intelfb_softc {
62 	struct drmfb_softc		sc_drmfb; /* XXX Must be first.  */
63 	device_t			sc_dev;
64 	struct intelfb_attach_args	sc_ifa;
65 	bus_space_handle_t		sc_fb_bsh;
66 	struct i915drmkms_task		sc_attach_task;
67 	bool				sc_scheduled:1;
68 	bool				sc_attached:1;
69 };
70 
71 static const struct drmfb_params intelfb_drmfb_params = {
72 	.dp_mmapfb = intelfb_drmfb_mmapfb,
73 	.dp_mmap = drmfb_pci_mmap,
74 	.dp_ioctl = drmfb_pci_ioctl,
75 	.dp_is_vga_console = drmfb_pci_is_vga_console,
76 	.dp_disable_vga = intelfb_disable_vga,
77 };
78 
79 CFATTACH_DECL_NEW(intelfb, sizeof(struct intelfb_softc),
80     intelfb_match, intelfb_attach, intelfb_detach, NULL);
81 
82 static int
83 intelfb_match(device_t parent, cfdata_t match, void *aux)
84 {
85 
86 	return 1;
87 }
88 
89 static void
90 intelfb_attach(device_t parent, device_t self, void *aux)
91 {
92 	struct intelfb_softc *const sc = device_private(self);
93 	const struct intelfb_attach_args *const ifa = aux;
94 	int error;
95 
96 	sc->sc_dev = self;
97 	sc->sc_ifa = *ifa;
98 	sc->sc_scheduled = false;
99 	sc->sc_attached = false;
100 
101 	aprint_naive("\n");
102 	aprint_normal("\n");
103 
104 	i915drmkms_task_init(&sc->sc_attach_task, &intelfb_attach_task);
105 	error = i915drmkms_task_schedule(parent, &sc->sc_attach_task);
106 	if (error) {
107 		aprint_error_dev(self, "failed to schedule mode set: %d\n",
108 		    error);
109 		return;
110 	}
111 	config_pending_incr(self);
112 	sc->sc_scheduled = true;
113 
114 	/* Success!  */
115 	return;
116 }
117 
118 static int
119 intelfb_detach(device_t self, int flags)
120 {
121 	struct intelfb_softc *const sc = device_private(self);
122 	int error;
123 
124 	if (sc->sc_scheduled)
125 		return EBUSY;
126 
127 	if (sc->sc_attached) {
128 		pmf_device_deregister(self);
129 		error = drmfb_detach(&sc->sc_drmfb, flags);
130 		if (error) {
131 			/* XXX Ugh.  */
132 			(void)pmf_device_register1(self, NULL, NULL,
133 			    &intelfb_shutdown);
134 			return error;
135 		}
136 		sc->sc_attached = false;
137 	}
138 
139 	return 0;
140 }
141 
142 static void
143 intelfb_attach_task(struct i915drmkms_task *task)
144 {
145 	struct intelfb_softc *const sc = container_of(task,
146 	    struct intelfb_softc, sc_attach_task);
147 	const struct intelfb_attach_args *const ifa = &sc->sc_ifa;
148 	const struct drmfb_attach_args da = {
149 		.da_dev = sc->sc_dev,
150 		.da_fb_helper = ifa->ifa_fb_helper,
151 		.da_fb_sizes = &ifa->ifa_fb_sizes,
152 		.da_fb_vaddr = ifa->ifa_fb_vaddr,
153 		.da_fb_linebytes = ifa->ifa_fb_helper->fb->pitches[0],
154 		.da_params = &intelfb_drmfb_params,
155 	};
156 	int error;
157 
158 	error = drmfb_attach(&sc->sc_drmfb, &da);
159 	if (error) {
160 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
161 		    error);
162 		goto out;
163 	}
164 
165 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &intelfb_shutdown))
166 		aprint_error_dev(sc->sc_dev,
167 		    "failed to register shutdown handler\n");
168 
169 	sc->sc_attached = true;
170 out:
171 	config_pending_decr(sc->sc_dev);
172 }
173 
174 static bool
175 intelfb_shutdown(device_t self, int flags)
176 {
177 	struct intelfb_softc *const sc = device_private(self);
178 
179 	return drmfb_shutdown(&sc->sc_drmfb, flags);
180 }
181 
182 static paddr_t
183 intelfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
184 {
185 	struct intelfb_softc *const sc = container_of(drmfb,
186 	    struct intelfb_softc, sc_drmfb);
187 	struct drm_fb_helper *const helper = sc->sc_ifa.ifa_fb_helper;
188 	struct intel_fbdev *const fbdev = container_of(helper,
189 	    struct intel_fbdev, helper);
190 	struct drm_device *const dev = helper->dev;
191 	struct drm_i915_private *const i915 =
192 	    container_of(dev, struct drm_i915_private, drm);
193 	struct i915_ggtt *const ggtt = &i915->ggtt;
194 	struct i915_vma *const vma = fbdev->vma;
195 
196 	KASSERT(0 <= offset);
197 	KASSERT(offset < vma->node.size);
198 
199 	return bus_space_mmap(dev->bst, ggtt->gmadr.start, vma->node.start,
200 	    prot, BUS_SPACE_MAP_PREFETCHABLE);
201 }
202 
203 static void
204 intelfb_disable_vga(struct drm_device *dev)
205 {
206 	struct drm_i915_private *i915 =
207 	    container_of(dev, struct drm_i915_private, drm);
208 
209 	intel_vga_disable(i915);
210 }
211