xref: /netbsd-src/sys/external/bsd/drm2/i915drm/intelfb.c (revision 2f0ff0f4722e489354a204e530c8c8d3e0d930a2)
1 /*	$NetBSD: intelfb.c,v 1.25 2023/05/22 22:36:53 nat 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.25 2023/05/22 22:36:53 nat 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_attached:1;
68 };
69 
70 static const struct drmfb_params intelfb_drmfb_params = {
71 	.dp_mmapfb = intelfb_drmfb_mmapfb,
72 	.dp_mmap = drmfb_pci_mmap,
73 	.dp_ioctl = drmfb_pci_ioctl,
74 	.dp_is_vga_console = drmfb_pci_is_vga_console,
75 	.dp_disable_vga = intelfb_disable_vga,
76 };
77 
78 CFATTACH_DECL_NEW(intelfb, sizeof(struct intelfb_softc),
79     intelfb_match, intelfb_attach, intelfb_detach, NULL);
80 
81 static int
intelfb_match(device_t parent,cfdata_t match,void * aux)82 intelfb_match(device_t parent, cfdata_t match, void *aux)
83 {
84 
85 	return 1;
86 }
87 
88 static void
intelfb_attach(device_t parent,device_t self,void * aux)89 intelfb_attach(device_t parent, device_t self, void *aux)
90 {
91 	struct intelfb_softc *const sc = device_private(self);
92 	const struct intelfb_attach_args *const ifa = aux;
93 
94 	sc->sc_dev = self;
95 	sc->sc_ifa = *ifa;
96 	sc->sc_attached = false;
97 
98 	aprint_naive("\n");
99 	aprint_normal("\n");
100 
101 	i915drmkms_task_init(&sc->sc_attach_task, &intelfb_attach_task);
102 	i915drmkms_task_schedule(parent, &sc->sc_attach_task);
103 	config_pending_incr(self);
104 }
105 
106 static int
intelfb_detach(device_t self,int flags)107 intelfb_detach(device_t self, int flags)
108 {
109 	struct intelfb_softc *const sc = device_private(self);
110 	int error;
111 
112 	if (sc->sc_attached) {
113 		pmf_device_deregister(self);
114 		error = drmfb_detach(&sc->sc_drmfb, flags);
115 		if (error) {
116 			/* XXX Ugh.  */
117 			(void)pmf_device_register1(self, NULL, NULL,
118 			    &intelfb_shutdown);
119 			return error;
120 		}
121 		sc->sc_attached = false;
122 	}
123 
124 	return 0;
125 }
126 
127 static void
intelfb_attach_task(struct i915drmkms_task * task)128 intelfb_attach_task(struct i915drmkms_task *task)
129 {
130 	struct intelfb_softc *const sc = container_of(task,
131 	    struct intelfb_softc, sc_attach_task);
132 	const struct intelfb_attach_args *const ifa = &sc->sc_ifa;
133 	const struct drmfb_attach_args da = {
134 		.da_dev = sc->sc_dev,
135 		.da_fb_helper = ifa->ifa_fb_helper,
136 		.da_fb_sizes = &ifa->ifa_fb_sizes,
137 		.da_fb_vaddr = ifa->ifa_fb_vaddr,
138 		.da_fb_linebytes = ifa->ifa_fb_helper->fb->pitches[0],
139 		.da_params = &intelfb_drmfb_params,
140 	};
141 	int error;
142 
143 	error = drmfb_attach(&sc->sc_drmfb, &da);
144 	if (error) {
145 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
146 		    error);
147 		goto out;
148 	}
149 
150 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &intelfb_shutdown))
151 		aprint_error_dev(sc->sc_dev,
152 		    "failed to register shutdown handler\n");
153 
154 	sc->sc_attached = true;
155 out:
156 	config_pending_decr(sc->sc_dev);
157 }
158 
159 static bool
intelfb_shutdown(device_t self,int flags)160 intelfb_shutdown(device_t self, int flags)
161 {
162 	struct intelfb_softc *const sc = device_private(self);
163 
164 	return drmfb_shutdown(&sc->sc_drmfb, flags);
165 }
166 
167 static paddr_t
intelfb_drmfb_mmapfb(struct drmfb_softc * drmfb,off_t offset,int prot)168 intelfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
169 {
170 	struct intelfb_softc *const sc = container_of(drmfb,
171 	    struct intelfb_softc, sc_drmfb);
172 	struct drm_fb_helper *const helper = sc->sc_ifa.ifa_fb_helper;
173 	struct intel_fbdev *const fbdev = container_of(helper,
174 	    struct intel_fbdev, helper);
175 	struct drm_device *const dev = helper->dev;
176 	struct drm_i915_private *const i915 =
177 	    container_of(dev, struct drm_i915_private, drm);
178 	struct i915_ggtt *const ggtt = &i915->ggtt;
179 	struct i915_vma *const vma = fbdev->vma;
180 
181 	KASSERT(0 <= offset);
182 	KASSERT(offset < vma->node.size);
183 
184 	return bus_space_mmap(dev->bst, ggtt->gmadr.start,
185 	    vma->node.start + offset, prot, BUS_SPACE_MAP_PREFETCHABLE);
186 }
187 
188 static void
intelfb_disable_vga(struct drm_device * dev)189 intelfb_disable_vga(struct drm_device *dev)
190 {
191 	struct drm_i915_private *i915 =
192 	    container_of(dev, struct drm_i915_private, drm);
193 
194 	intel_vga_disable(i915);
195 }
196