i915_pci_autoconf.c revision 1.4 1 1.4 riastrad /* $NetBSD: i915_pci_autoconf.c,v 1.4 2021/12/19 11:05:12 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.4 riastrad __KERNEL_RCSID(0, "$NetBSD: i915_pci_autoconf.c,v 1.4 2021/12/19 11:05:12 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/queue.h>
37 1.1 riastrad #include <sys/systm.h>
38 1.1 riastrad #include <sys/queue.h>
39 1.1 riastrad #include <sys/workqueue.h>
40 1.1 riastrad
41 1.1 riastrad #include "i915_drv.h"
42 1.1 riastrad #include "i915_pci.h"
43 1.1 riastrad
44 1.3 riastrad struct drm_device;
45 1.3 riastrad
46 1.1 riastrad SIMPLEQ_HEAD(i915drmkms_task_head, i915drmkms_task);
47 1.1 riastrad
48 1.1 riastrad struct i915drmkms_softc {
49 1.1 riastrad device_t sc_dev;
50 1.1 riastrad struct pci_attach_args sc_pa;
51 1.1 riastrad enum {
52 1.1 riastrad I915DRMKMS_TASK_ATTACH,
53 1.1 riastrad I915DRMKMS_TASK_WORKQUEUE,
54 1.1 riastrad } sc_task_state;
55 1.1 riastrad union {
56 1.1 riastrad struct workqueue *workqueue;
57 1.1 riastrad struct i915drmkms_task_head attach;
58 1.1 riastrad } sc_task_u;
59 1.1 riastrad struct drm_device *sc_drm_dev;
60 1.1 riastrad struct pci_dev sc_pci_dev;
61 1.4 riastrad bool sc_pci_attached;
62 1.4 riastrad bool sc_dev_registered;
63 1.1 riastrad };
64 1.1 riastrad
65 1.1 riastrad static const struct intel_device_info *
66 1.1 riastrad i915drmkms_pci_lookup(const struct pci_attach_args *);
67 1.1 riastrad
68 1.1 riastrad static int i915drmkms_match(device_t, cfdata_t, void *);
69 1.1 riastrad static void i915drmkms_attach(device_t, device_t, void *);
70 1.1 riastrad static void i915drmkms_attach_real(device_t);
71 1.1 riastrad static int i915drmkms_detach(device_t, int);
72 1.1 riastrad
73 1.1 riastrad static bool i915drmkms_suspend(device_t, const pmf_qual_t *);
74 1.1 riastrad static bool i915drmkms_resume(device_t, const pmf_qual_t *);
75 1.1 riastrad
76 1.1 riastrad static void i915drmkms_task_work(struct work *, void *);
77 1.1 riastrad
78 1.1 riastrad CFATTACH_DECL_NEW(i915drmkms, sizeof(struct i915drmkms_softc),
79 1.1 riastrad i915drmkms_match, i915drmkms_attach, i915drmkms_detach, NULL);
80 1.1 riastrad
81 1.1 riastrad /* XXX Kludge to get these from i915_drv.c. */
82 1.1 riastrad extern struct drm_driver *const i915_drm_driver;
83 1.1 riastrad extern const struct pci_device_id *const i915_device_ids;
84 1.1 riastrad extern const size_t i915_n_device_ids;
85 1.1 riastrad
86 1.1 riastrad static const struct intel_device_info *
87 1.1 riastrad i915drmkms_pci_lookup(const struct pci_attach_args *pa)
88 1.1 riastrad {
89 1.1 riastrad size_t i;
90 1.1 riastrad
91 1.1 riastrad /* Attach only at function 0 to work around Intel lossage. */
92 1.1 riastrad if (pa->pa_function != 0)
93 1.1 riastrad return NULL;
94 1.1 riastrad
95 1.1 riastrad /* We're interested only in Intel products. */
96 1.1 riastrad if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
97 1.1 riastrad return NULL;
98 1.1 riastrad
99 1.1 riastrad /* We're interested only in Intel display devices. */
100 1.1 riastrad if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
101 1.1 riastrad return NULL;
102 1.1 riastrad
103 1.1 riastrad for (i = 0; i < i915_n_device_ids; i++)
104 1.1 riastrad if (PCI_PRODUCT(pa->pa_id) == i915_device_ids[i].device)
105 1.1 riastrad break;
106 1.1 riastrad
107 1.1 riastrad /* Did we find it? */
108 1.1 riastrad if (i == i915_n_device_ids)
109 1.1 riastrad return NULL;
110 1.1 riastrad
111 1.1 riastrad const struct intel_device_info *const info =
112 1.1 riastrad (const void *)(uintptr_t)i915_device_ids[i].driver_data;
113 1.1 riastrad
114 1.2 riastrad if (IS_ALPHA_SUPPORT(info)) {
115 1.1 riastrad printf("i915drmkms: preliminary hardware support disabled\n");
116 1.1 riastrad return NULL;
117 1.1 riastrad }
118 1.1 riastrad
119 1.1 riastrad return info;
120 1.1 riastrad }
121 1.1 riastrad
122 1.1 riastrad static int
123 1.1 riastrad i915drmkms_match(device_t parent, cfdata_t match, void *aux)
124 1.1 riastrad {
125 1.1 riastrad extern int i915drmkms_guarantee_initialized(void);
126 1.1 riastrad const struct pci_attach_args *const pa = aux;
127 1.1 riastrad int error;
128 1.1 riastrad
129 1.1 riastrad error = i915drmkms_guarantee_initialized();
130 1.1 riastrad if (error) {
131 1.1 riastrad aprint_error("i915drmkms: failed to initialize: %d\n", error);
132 1.1 riastrad return 0;
133 1.1 riastrad }
134 1.1 riastrad
135 1.1 riastrad if (i915drmkms_pci_lookup(pa) == NULL)
136 1.1 riastrad return 0;
137 1.1 riastrad
138 1.1 riastrad return 6; /* XXX Beat genfb_pci... */
139 1.1 riastrad }
140 1.1 riastrad
141 1.1 riastrad static void
142 1.1 riastrad i915drmkms_attach(device_t parent, device_t self, void *aux)
143 1.1 riastrad {
144 1.1 riastrad struct i915drmkms_softc *const sc = device_private(self);
145 1.1 riastrad const struct pci_attach_args *const pa = aux;
146 1.1 riastrad
147 1.1 riastrad pci_aprint_devinfo(pa, NULL);
148 1.1 riastrad
149 1.1 riastrad if (!pmf_device_register(self, &i915drmkms_suspend,
150 1.1 riastrad &i915drmkms_resume))
151 1.1 riastrad aprint_error_dev(self, "unable to establish power handler\n");
152 1.1 riastrad
153 1.1 riastrad /*
154 1.1 riastrad * Trivial initialization first; the rest will come after we
155 1.1 riastrad * have mounted the root file system and can load firmware
156 1.1 riastrad * images.
157 1.1 riastrad */
158 1.1 riastrad sc->sc_dev = self;
159 1.1 riastrad sc->sc_pa = *pa;
160 1.1 riastrad
161 1.1 riastrad config_mountroot(self, &i915drmkms_attach_real);
162 1.1 riastrad }
163 1.1 riastrad
164 1.1 riastrad static void
165 1.1 riastrad i915drmkms_attach_real(device_t self)
166 1.1 riastrad {
167 1.1 riastrad struct i915drmkms_softc *const sc = device_private(self);
168 1.1 riastrad struct pci_attach_args *const pa = &sc->sc_pa;
169 1.1 riastrad const struct intel_device_info *const info = i915drmkms_pci_lookup(pa);
170 1.1 riastrad const unsigned long cookie =
171 1.1 riastrad (unsigned long)(uintptr_t)(const void *)info;
172 1.1 riastrad int error;
173 1.1 riastrad
174 1.1 riastrad KASSERT(info != NULL);
175 1.1 riastrad
176 1.1 riastrad sc->sc_task_state = I915DRMKMS_TASK_ATTACH;
177 1.1 riastrad SIMPLEQ_INIT(&sc->sc_task_u.attach);
178 1.1 riastrad
179 1.1 riastrad /* Initialize the Linux PCI device descriptor. */
180 1.1 riastrad linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
181 1.1 riastrad
182 1.4 riastrad sc->sc_drm_dev = drm_dev_alloc(i915_drm_driver, self);
183 1.4 riastrad if (IS_ERR(sc->sc_drm_dev)) {
184 1.4 riastrad aprint_error_dev(self, "unable to create drm device: %ld\n",
185 1.4 riastrad PTR_ERR(sc->sc_drm_dev));
186 1.4 riastrad sc->sc_drm_dev = NULL;
187 1.4 riastrad return;
188 1.4 riastrad }
189 1.4 riastrad
190 1.1 riastrad /* XXX errno Linux->NetBSD */
191 1.4 riastrad error = -drm_pci_attach(sc->sc_drm_dev, pa, &sc->sc_pci_dev);
192 1.1 riastrad if (error) {
193 1.1 riastrad aprint_error_dev(self, "unable to attach drm: %d\n", error);
194 1.1 riastrad return;
195 1.1 riastrad }
196 1.4 riastrad sc->sc_pci_attached = true;
197 1.4 riastrad
198 1.4 riastrad /* XXX errno Linux->NetBSD */
199 1.4 riastrad error = -drm_dev_register(sc->sc_drm_dev, cookie);
200 1.4 riastrad if (error) {
201 1.4 riastrad aprint_error_dev(self, "unable to register drm: %d\n", error);
202 1.4 riastrad return;
203 1.4 riastrad }
204 1.4 riastrad sc->sc_dev_registered = true;
205 1.1 riastrad
206 1.1 riastrad while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
207 1.1 riastrad struct i915drmkms_task *const task =
208 1.1 riastrad SIMPLEQ_FIRST(&sc->sc_task_u.attach);
209 1.1 riastrad
210 1.1 riastrad SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, ift_u.queue);
211 1.1 riastrad (*task->ift_fn)(task);
212 1.1 riastrad }
213 1.1 riastrad
214 1.1 riastrad sc->sc_task_state = I915DRMKMS_TASK_WORKQUEUE;
215 1.1 riastrad error = workqueue_create(&sc->sc_task_u.workqueue, "intelfb",
216 1.1 riastrad &i915drmkms_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
217 1.1 riastrad if (error) {
218 1.1 riastrad aprint_error_dev(self, "unable to create workqueue: %d\n",
219 1.1 riastrad error);
220 1.1 riastrad sc->sc_task_u.workqueue = NULL;
221 1.1 riastrad return;
222 1.1 riastrad }
223 1.1 riastrad }
224 1.1 riastrad
225 1.1 riastrad static int
226 1.1 riastrad i915drmkms_detach(device_t self, int flags)
227 1.1 riastrad {
228 1.1 riastrad struct i915drmkms_softc *const sc = device_private(self);
229 1.1 riastrad int error;
230 1.1 riastrad
231 1.1 riastrad /* XXX Check for in-use before tearing it all down... */
232 1.1 riastrad error = config_detach_children(self, flags);
233 1.1 riastrad if (error)
234 1.1 riastrad return error;
235 1.1 riastrad
236 1.1 riastrad if (sc->sc_task_state == I915DRMKMS_TASK_ATTACH)
237 1.4 riastrad goto out0;
238 1.1 riastrad if (sc->sc_task_u.workqueue != NULL) {
239 1.1 riastrad workqueue_destroy(sc->sc_task_u.workqueue);
240 1.1 riastrad sc->sc_task_u.workqueue = NULL;
241 1.1 riastrad }
242 1.1 riastrad
243 1.1 riastrad if (sc->sc_drm_dev == NULL)
244 1.4 riastrad goto out0;
245 1.4 riastrad if (!sc->sc_pci_attached)
246 1.4 riastrad goto out1;
247 1.4 riastrad if (!sc->sc_dev_registered)
248 1.4 riastrad goto out2;
249 1.4 riastrad
250 1.4 riastrad drm_dev_unregister(sc->sc_drm_dev);
251 1.4 riastrad out2: drm_pci_detach(sc->sc_drm_dev);
252 1.4 riastrad out1: drm_dev_put(sc->sc_drm_dev);
253 1.1 riastrad sc->sc_drm_dev = NULL;
254 1.4 riastrad out0: linux_pci_dev_destroy(&sc->sc_pci_dev);
255 1.1 riastrad pmf_device_deregister(self);
256 1.1 riastrad return 0;
257 1.1 riastrad }
258 1.1 riastrad
259 1.1 riastrad static bool
260 1.1 riastrad i915drmkms_suspend(device_t self, const pmf_qual_t *qual)
261 1.1 riastrad {
262 1.1 riastrad struct i915drmkms_softc *const sc = device_private(self);
263 1.1 riastrad struct drm_device *const dev = sc->sc_drm_dev;
264 1.1 riastrad int ret;
265 1.1 riastrad
266 1.1 riastrad if (dev == NULL)
267 1.1 riastrad return true;
268 1.1 riastrad
269 1.1 riastrad ret = i915_drm_suspend(dev);
270 1.1 riastrad if (ret)
271 1.1 riastrad return false;
272 1.1 riastrad ret = i915_drm_suspend_late(dev, false);
273 1.1 riastrad if (ret)
274 1.1 riastrad return false;
275 1.1 riastrad
276 1.1 riastrad return true;
277 1.1 riastrad }
278 1.1 riastrad
279 1.1 riastrad static bool
280 1.1 riastrad i915drmkms_resume(device_t self, const pmf_qual_t *qual)
281 1.1 riastrad {
282 1.1 riastrad struct i915drmkms_softc *const sc = device_private(self);
283 1.1 riastrad struct drm_device *const dev = sc->sc_drm_dev;
284 1.1 riastrad int ret;
285 1.1 riastrad
286 1.1 riastrad if (dev == NULL)
287 1.1 riastrad return true;
288 1.1 riastrad
289 1.1 riastrad ret = i915_drm_resume_early(dev);
290 1.1 riastrad if (ret)
291 1.1 riastrad return false;
292 1.1 riastrad ret = i915_drm_resume(dev);
293 1.1 riastrad if (ret)
294 1.1 riastrad return false;
295 1.1 riastrad
296 1.1 riastrad return true;
297 1.1 riastrad }
298 1.1 riastrad
299 1.1 riastrad static void
300 1.1 riastrad i915drmkms_task_work(struct work *work, void *cookie __unused)
301 1.1 riastrad {
302 1.1 riastrad struct i915drmkms_task *const task = container_of(work,
303 1.1 riastrad struct i915drmkms_task, ift_u.work);
304 1.1 riastrad
305 1.1 riastrad (*task->ift_fn)(task);
306 1.1 riastrad }
307 1.1 riastrad
308 1.1 riastrad int
309 1.1 riastrad i915drmkms_task_schedule(device_t self, struct i915drmkms_task *task)
310 1.1 riastrad {
311 1.1 riastrad struct i915drmkms_softc *const sc = device_private(self);
312 1.1 riastrad
313 1.1 riastrad switch (sc->sc_task_state) {
314 1.1 riastrad case I915DRMKMS_TASK_ATTACH:
315 1.1 riastrad SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, ift_u.queue);
316 1.1 riastrad return 0;
317 1.1 riastrad case I915DRMKMS_TASK_WORKQUEUE:
318 1.1 riastrad if (sc->sc_task_u.workqueue == NULL) {
319 1.1 riastrad aprint_error_dev(self, "unable to schedule task\n");
320 1.1 riastrad return EIO;
321 1.1 riastrad }
322 1.1 riastrad workqueue_enqueue(sc->sc_task_u.workqueue, &task->ift_u.work,
323 1.1 riastrad NULL);
324 1.1 riastrad return 0;
325 1.1 riastrad default:
326 1.1 riastrad panic("i915drmkms in invalid task state: %d\n",
327 1.1 riastrad (int)sc->sc_task_state);
328 1.1 riastrad }
329 1.1 riastrad }
330