i915_pci_autoconf.c revision 1.4 1 /* $NetBSD: i915_pci_autoconf.c,v 1.4 2021/12/19 11:05:12 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 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: i915_pci_autoconf.c,v 1.4 2021/12/19 11:05:12 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/queue.h>
37 #include <sys/systm.h>
38 #include <sys/queue.h>
39 #include <sys/workqueue.h>
40
41 #include "i915_drv.h"
42 #include "i915_pci.h"
43
44 struct drm_device;
45
46 SIMPLEQ_HEAD(i915drmkms_task_head, i915drmkms_task);
47
48 struct i915drmkms_softc {
49 device_t sc_dev;
50 struct pci_attach_args sc_pa;
51 enum {
52 I915DRMKMS_TASK_ATTACH,
53 I915DRMKMS_TASK_WORKQUEUE,
54 } sc_task_state;
55 union {
56 struct workqueue *workqueue;
57 struct i915drmkms_task_head attach;
58 } sc_task_u;
59 struct drm_device *sc_drm_dev;
60 struct pci_dev sc_pci_dev;
61 bool sc_pci_attached;
62 bool sc_dev_registered;
63 };
64
65 static const struct intel_device_info *
66 i915drmkms_pci_lookup(const struct pci_attach_args *);
67
68 static int i915drmkms_match(device_t, cfdata_t, void *);
69 static void i915drmkms_attach(device_t, device_t, void *);
70 static void i915drmkms_attach_real(device_t);
71 static int i915drmkms_detach(device_t, int);
72
73 static bool i915drmkms_suspend(device_t, const pmf_qual_t *);
74 static bool i915drmkms_resume(device_t, const pmf_qual_t *);
75
76 static void i915drmkms_task_work(struct work *, void *);
77
78 CFATTACH_DECL_NEW(i915drmkms, sizeof(struct i915drmkms_softc),
79 i915drmkms_match, i915drmkms_attach, i915drmkms_detach, NULL);
80
81 /* XXX Kludge to get these from i915_drv.c. */
82 extern struct drm_driver *const i915_drm_driver;
83 extern const struct pci_device_id *const i915_device_ids;
84 extern const size_t i915_n_device_ids;
85
86 static const struct intel_device_info *
87 i915drmkms_pci_lookup(const struct pci_attach_args *pa)
88 {
89 size_t i;
90
91 /* Attach only at function 0 to work around Intel lossage. */
92 if (pa->pa_function != 0)
93 return NULL;
94
95 /* We're interested only in Intel products. */
96 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
97 return NULL;
98
99 /* We're interested only in Intel display devices. */
100 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
101 return NULL;
102
103 for (i = 0; i < i915_n_device_ids; i++)
104 if (PCI_PRODUCT(pa->pa_id) == i915_device_ids[i].device)
105 break;
106
107 /* Did we find it? */
108 if (i == i915_n_device_ids)
109 return NULL;
110
111 const struct intel_device_info *const info =
112 (const void *)(uintptr_t)i915_device_ids[i].driver_data;
113
114 if (IS_ALPHA_SUPPORT(info)) {
115 printf("i915drmkms: preliminary hardware support disabled\n");
116 return NULL;
117 }
118
119 return info;
120 }
121
122 static int
123 i915drmkms_match(device_t parent, cfdata_t match, void *aux)
124 {
125 extern int i915drmkms_guarantee_initialized(void);
126 const struct pci_attach_args *const pa = aux;
127 int error;
128
129 error = i915drmkms_guarantee_initialized();
130 if (error) {
131 aprint_error("i915drmkms: failed to initialize: %d\n", error);
132 return 0;
133 }
134
135 if (i915drmkms_pci_lookup(pa) == NULL)
136 return 0;
137
138 return 6; /* XXX Beat genfb_pci... */
139 }
140
141 static void
142 i915drmkms_attach(device_t parent, device_t self, void *aux)
143 {
144 struct i915drmkms_softc *const sc = device_private(self);
145 const struct pci_attach_args *const pa = aux;
146
147 pci_aprint_devinfo(pa, NULL);
148
149 if (!pmf_device_register(self, &i915drmkms_suspend,
150 &i915drmkms_resume))
151 aprint_error_dev(self, "unable to establish power handler\n");
152
153 /*
154 * Trivial initialization first; the rest will come after we
155 * have mounted the root file system and can load firmware
156 * images.
157 */
158 sc->sc_dev = self;
159 sc->sc_pa = *pa;
160
161 config_mountroot(self, &i915drmkms_attach_real);
162 }
163
164 static void
165 i915drmkms_attach_real(device_t self)
166 {
167 struct i915drmkms_softc *const sc = device_private(self);
168 struct pci_attach_args *const pa = &sc->sc_pa;
169 const struct intel_device_info *const info = i915drmkms_pci_lookup(pa);
170 const unsigned long cookie =
171 (unsigned long)(uintptr_t)(const void *)info;
172 int error;
173
174 KASSERT(info != NULL);
175
176 sc->sc_task_state = I915DRMKMS_TASK_ATTACH;
177 SIMPLEQ_INIT(&sc->sc_task_u.attach);
178
179 /* Initialize the Linux PCI device descriptor. */
180 linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
181
182 sc->sc_drm_dev = drm_dev_alloc(i915_drm_driver, self);
183 if (IS_ERR(sc->sc_drm_dev)) {
184 aprint_error_dev(self, "unable to create drm device: %ld\n",
185 PTR_ERR(sc->sc_drm_dev));
186 sc->sc_drm_dev = NULL;
187 return;
188 }
189
190 /* XXX errno Linux->NetBSD */
191 error = -drm_pci_attach(sc->sc_drm_dev, pa, &sc->sc_pci_dev);
192 if (error) {
193 aprint_error_dev(self, "unable to attach drm: %d\n", error);
194 return;
195 }
196 sc->sc_pci_attached = true;
197
198 /* XXX errno Linux->NetBSD */
199 error = -drm_dev_register(sc->sc_drm_dev, cookie);
200 if (error) {
201 aprint_error_dev(self, "unable to register drm: %d\n", error);
202 return;
203 }
204 sc->sc_dev_registered = true;
205
206 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
207 struct i915drmkms_task *const task =
208 SIMPLEQ_FIRST(&sc->sc_task_u.attach);
209
210 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, ift_u.queue);
211 (*task->ift_fn)(task);
212 }
213
214 sc->sc_task_state = I915DRMKMS_TASK_WORKQUEUE;
215 error = workqueue_create(&sc->sc_task_u.workqueue, "intelfb",
216 &i915drmkms_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
217 if (error) {
218 aprint_error_dev(self, "unable to create workqueue: %d\n",
219 error);
220 sc->sc_task_u.workqueue = NULL;
221 return;
222 }
223 }
224
225 static int
226 i915drmkms_detach(device_t self, int flags)
227 {
228 struct i915drmkms_softc *const sc = device_private(self);
229 int error;
230
231 /* XXX Check for in-use before tearing it all down... */
232 error = config_detach_children(self, flags);
233 if (error)
234 return error;
235
236 if (sc->sc_task_state == I915DRMKMS_TASK_ATTACH)
237 goto out0;
238 if (sc->sc_task_u.workqueue != NULL) {
239 workqueue_destroy(sc->sc_task_u.workqueue);
240 sc->sc_task_u.workqueue = NULL;
241 }
242
243 if (sc->sc_drm_dev == NULL)
244 goto out0;
245 if (!sc->sc_pci_attached)
246 goto out1;
247 if (!sc->sc_dev_registered)
248 goto out2;
249
250 drm_dev_unregister(sc->sc_drm_dev);
251 out2: drm_pci_detach(sc->sc_drm_dev);
252 out1: drm_dev_put(sc->sc_drm_dev);
253 sc->sc_drm_dev = NULL;
254 out0: linux_pci_dev_destroy(&sc->sc_pci_dev);
255 pmf_device_deregister(self);
256 return 0;
257 }
258
259 static bool
260 i915drmkms_suspend(device_t self, const pmf_qual_t *qual)
261 {
262 struct i915drmkms_softc *const sc = device_private(self);
263 struct drm_device *const dev = sc->sc_drm_dev;
264 int ret;
265
266 if (dev == NULL)
267 return true;
268
269 ret = i915_drm_suspend(dev);
270 if (ret)
271 return false;
272 ret = i915_drm_suspend_late(dev, false);
273 if (ret)
274 return false;
275
276 return true;
277 }
278
279 static bool
280 i915drmkms_resume(device_t self, const pmf_qual_t *qual)
281 {
282 struct i915drmkms_softc *const sc = device_private(self);
283 struct drm_device *const dev = sc->sc_drm_dev;
284 int ret;
285
286 if (dev == NULL)
287 return true;
288
289 ret = i915_drm_resume_early(dev);
290 if (ret)
291 return false;
292 ret = i915_drm_resume(dev);
293 if (ret)
294 return false;
295
296 return true;
297 }
298
299 static void
300 i915drmkms_task_work(struct work *work, void *cookie __unused)
301 {
302 struct i915drmkms_task *const task = container_of(work,
303 struct i915drmkms_task, ift_u.work);
304
305 (*task->ift_fn)(task);
306 }
307
308 int
309 i915drmkms_task_schedule(device_t self, struct i915drmkms_task *task)
310 {
311 struct i915drmkms_softc *const sc = device_private(self);
312
313 switch (sc->sc_task_state) {
314 case I915DRMKMS_TASK_ATTACH:
315 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, ift_u.queue);
316 return 0;
317 case I915DRMKMS_TASK_WORKQUEUE:
318 if (sc->sc_task_u.workqueue == NULL) {
319 aprint_error_dev(self, "unable to schedule task\n");
320 return EIO;
321 }
322 workqueue_enqueue(sc->sc_task_u.workqueue, &task->ift_u.work,
323 NULL);
324 return 0;
325 default:
326 panic("i915drmkms in invalid task state: %d\n",
327 (int)sc->sc_task_state);
328 }
329 }
330