intelfb.c revision 1.21 1 /* $NetBSD: intelfb.c,v 1.21 2021/12/19 11:38:46 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.21 2021/12/19 11:38:46 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 "i915_drv.h"
44 #include "i915_pci.h"
45 #include "intelfb.h"
46
47 static int intelfb_match(device_t, cfdata_t, void *);
48 static void intelfb_attach(device_t, device_t, void *);
49 static int intelfb_detach(device_t, int);
50
51 static void intelfb_attach_task(struct i915drmkms_task *);
52
53 static bool intelfb_shutdown(device_t, int);
54
55 static paddr_t intelfb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
56
57 struct intelfb_softc {
58 struct drmfb_softc sc_drmfb; /* XXX Must be first. */
59 device_t sc_dev;
60 struct intelfb_attach_args sc_ifa;
61 bus_space_handle_t sc_fb_bsh;
62 struct i915drmkms_task sc_attach_task;
63 bool sc_scheduled:1;
64 bool sc_attached:1;
65 };
66
67 static const struct drmfb_params intelfb_drmfb_params = {
68 .dp_mmapfb = intelfb_drmfb_mmapfb,
69 .dp_mmap = drmfb_pci_mmap,
70 .dp_ioctl = drmfb_pci_ioctl,
71 .dp_is_vga_console = drmfb_pci_is_vga_console,
72 .dp_disable_vga = i915_disable_vga,
73 };
74
75 CFATTACH_DECL_NEW(intelfb, sizeof(struct intelfb_softc),
76 intelfb_match, intelfb_attach, intelfb_detach, NULL);
77
78 static int
79 intelfb_match(device_t parent, cfdata_t match, void *aux)
80 {
81
82 return 1;
83 }
84
85 static void
86 intelfb_attach(device_t parent, device_t self, void *aux)
87 {
88 struct intelfb_softc *const sc = device_private(self);
89 const struct intelfb_attach_args *const ifa = aux;
90 int error;
91
92 sc->sc_dev = self;
93 sc->sc_ifa = *ifa;
94 sc->sc_scheduled = false;
95 sc->sc_attached = false;
96
97 aprint_naive("\n");
98 aprint_normal("\n");
99
100 i915drmkms_task_init(&sc->sc_attach_task, &intelfb_attach_task);
101 error = i915drmkms_task_schedule(parent, &sc->sc_attach_task);
102 if (error) {
103 aprint_error_dev(self, "failed to schedule mode set: %d\n",
104 error);
105 goto fail1;
106 }
107 config_pending_incr(self);
108 sc->sc_scheduled = true;
109
110 /* Success! */
111 return;
112
113 fail0: return;
114 }
115
116 static int
117 intelfb_detach(device_t self, int flags)
118 {
119 struct intelfb_softc *const sc = device_private(self);
120 int error;
121
122 if (sc->sc_scheduled)
123 return EBUSY;
124
125 if (sc->sc_attached) {
126 pmf_device_deregister(self);
127 error = drmfb_detach(&sc->sc_drmfb, flags);
128 if (error) {
129 /* XXX Ugh. */
130 (void)pmf_device_register1(self, NULL, NULL,
131 &intelfb_shutdown);
132 return error;
133 }
134 sc->sc_attached = false;
135 }
136
137 return 0;
138 }
139
140 static void
141 intelfb_attach_task(struct i915drmkms_task *task)
142 {
143 struct intelfb_softc *const sc = container_of(task,
144 struct intelfb_softc, sc_attach_task);
145 const struct intelfb_attach_args *const ifa = &sc->sc_ifa;
146 const struct drmfb_attach_args da = {
147 .da_dev = sc->sc_dev,
148 .da_fb_helper = ifa->ifa_fb_helper,
149 .da_fb_sizes = &ifa->ifa_fb_sizes,
150 .da_fb_vaddr = ifa->ifa_fb_vaddr,
151 .da_fb_linebytes = ifa->ifa_fb_helper->fb->pitches[0],
152 .da_params = &intelfb_drmfb_params,
153 };
154 int error;
155
156 error = drmfb_attach(&sc->sc_drmfb, &da);
157 if (error) {
158 aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
159 error);
160 goto out;
161 }
162
163 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &intelfb_shutdown))
164 aprint_error_dev(sc->sc_dev,
165 "failed to register shutdown handler\n");
166
167 sc->sc_attached = true;
168 out:
169 config_pending_decr(sc->sc_dev);
170 }
171
172 static bool
173 intelfb_shutdown(device_t self, int flags)
174 {
175 struct intelfb_softc *const sc = device_private(self);
176
177 return drmfb_shutdown(&sc->sc_drmfb, flags);
178 }
179
180 static paddr_t
181 intelfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
182 {
183 struct intelfb_softc *const sc = container_of(drmfb,
184 struct intelfb_softc, sc_drmfb);
185 struct drm_fb_helper *const helper = sc->sc_ifa.ifa_fb_helper;
186 struct intel_fbdev *const fbdev = container_of(helper,
187 struct intel_fbdev, helper);
188 struct drm_device *const dev = helper->dev;
189 struct drm_i915_private *const dev_priv = dev->dev_private;
190
191 KASSERT(0 <= offset);
192 KASSERT(offset < fbdev->fb->obj->base.size);
193
194 return bus_space_mmap(dev->bst, dev_priv->gtt.mappable_base,
195 i915_gem_obj_ggtt_offset(fbdev->fb->obj) + offset,
196 prot, BUS_SPACE_MAP_PREFETCHABLE);
197 }
198