1 /* $NetBSD: vmwgfx_pci.c,v 1.2 2022/07/18 23:34:03 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2022 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_pci.c,v 1.2 2022/07/18 23:34:03 riastradh Exp $");
31
32 #ifdef _KERNEL_OPT
33 #include "vga.h"
34 #endif
35
36 #include <sys/types.h>
37 #include <sys/atomic.h>
38 #include <sys/queue.h>
39 #include <sys/systm.h>
40 #include <sys/workqueue.h>
41
42 #include <dev/pci/pciio.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45
46 #include <dev/pci/wsdisplay_pci.h>
47 #include <dev/wsfb/genfbvar.h>
48
49 #include <drm/drm_device.h>
50 #include <drm/drm_drv.h>
51 #include <drm/drm_fb_helper.h>
52 #include <drm/drm_pci.h>
53
54 #include "vmwgfx_drv.h"
55 #include "vmwgfx_task.h"
56
57 SIMPLEQ_HEAD(vmwgfx_task_head, vmwgfx_task);
58
59 struct vmwgfx_softc {
60 device_t sc_dev;
61 struct pci_attach_args sc_pa;
62 struct lwp *sc_task_thread;
63 struct vmwgfx_task_head sc_tasks;
64 struct workqueue *sc_task_wq;
65 struct drm_device *sc_drm_dev;
66 struct pci_dev sc_pci_dev;
67 bool sc_pci_attached;
68 bool sc_dev_registered;
69 #if defined(__i386__)
70 #define VMWGFX_PCI_UGLY_MAP_HACK
71 /* XXX Used to claim the VGA device before attach_real */
72 bus_space_handle_t sc_temp_memh;
73 bool sc_temp_set;
74 #endif
75 };
76
77 static bool vmwgfx_pci_lookup(const struct pci_attach_args *,
78 unsigned long *);
79
80 static int vmwgfx_match(device_t, cfdata_t, void *);
81 static void vmwgfx_attach(device_t, device_t, void *);
82 static void vmwgfx_attach_real(device_t);
83 static int vmwgfx_detach(device_t, int);
84 static bool vmwgfx_do_suspend(device_t, const pmf_qual_t *);
85 static bool vmwgfx_do_resume(device_t, const pmf_qual_t *);
86
87 static void vmwgfx_task_work(struct work *, void *);
88
89 CFATTACH_DECL_NEW(vmwgfx, sizeof(struct vmwgfx_softc),
90 vmwgfx_match, vmwgfx_attach, vmwgfx_detach, NULL);
91
92 /* XXX Kludge to get these from vmwgfx_drv.c. */
93 extern struct drm_driver *const vmwgfx_drm_driver;
94 extern const struct pci_device_id *const vmwgfx_device_ids;
95 extern const size_t vmwgfx_n_device_ids;
96
97 static bool
vmwgfx_pci_lookup(const struct pci_attach_args * pa,unsigned long * flags)98 vmwgfx_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
99 {
100 size_t i;
101
102 for (i = 0; i < vmwgfx_n_device_ids; i++) {
103 if ((PCI_VENDOR(pa->pa_id) == vmwgfx_device_ids[i].vendor) &&
104 (PCI_PRODUCT(pa->pa_id) == vmwgfx_device_ids[i].device))
105 break;
106 }
107
108 /* Did we find it? */
109 if (i == vmwgfx_n_device_ids)
110 return false;
111
112 if (flags)
113 *flags = vmwgfx_device_ids[i].driver_data;
114 return true;
115 }
116
117 static int
vmwgfx_match(device_t parent,cfdata_t match,void * aux)118 vmwgfx_match(device_t parent, cfdata_t match, void *aux)
119 {
120 extern int vmwgfx_guarantee_initialized(void);
121 const struct pci_attach_args *const pa = aux;
122 int error;
123
124 error = vmwgfx_guarantee_initialized();
125 if (error) {
126 aprint_error("vmwgfx: failed to initialize: %d\n", error);
127 return 0;
128 }
129
130 if (!vmwgfx_pci_lookup(pa, NULL))
131 return 0;
132
133 return 6; /* XXX Beat genfb_pci... */
134 }
135
136 static void
vmwgfx_attach(device_t parent,device_t self,void * aux)137 vmwgfx_attach(device_t parent, device_t self, void *aux)
138 {
139 struct vmwgfx_softc *const sc = device_private(self);
140 const struct pci_attach_args *const pa = aux;
141 int error;
142
143 pci_aprint_devinfo(pa, NULL);
144
145 /* Initialize the Linux PCI device descriptor. */
146 linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
147
148 sc->sc_dev = self;
149 sc->sc_pa = *pa;
150 sc->sc_task_thread = NULL;
151 SIMPLEQ_INIT(&sc->sc_tasks);
152 error = workqueue_create(&sc->sc_task_wq, "vmwgfxfb",
153 &vmwgfx_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
154 if (error) {
155 aprint_error_dev(self, "unable to create workqueue: %d\n",
156 error);
157 sc->sc_task_wq = NULL;
158 return;
159 }
160
161 #ifdef VMWGFX_PCI_UGLY_MAP_HACK
162 /*
163 * XXX
164 * We try to map the VGA registers, in case we can prevent vga@isa or
165 * pcdisplay@isa attaching, and stealing wsdisplay0. This only works
166 * with serial console, as actual VGA console has already mapped them.
167 * The only way to handle that is for vga@isa to not attach.
168 */
169 int rv = bus_space_map(pa->pa_memt, 0xb0000, 0x10000, 0,
170 &sc->sc_temp_memh);
171 sc->sc_temp_set = rv == 0;
172 if (rv != 0)
173 aprint_error_dev(self, "unable to reserve VGA registers for "
174 "i386 vmwgfxdrmkms hack\n");
175 #endif
176
177 /*
178 * Defer the remainder of initialization until we have mounted
179 * the root file system and can load firmware images.
180 */
181 config_mountroot(self, &vmwgfx_attach_real);
182 }
183
184 static void
vmwgfx_attach_real(device_t self)185 vmwgfx_attach_real(device_t self)
186 {
187 struct vmwgfx_softc *const sc = device_private(self);
188 const struct pci_attach_args *const pa = &sc->sc_pa;
189 bool ok __diagused;
190 unsigned long flags = 0 /*XXXGCC*/;
191 int error;
192
193 ok = vmwgfx_pci_lookup(pa, &flags);
194 KASSERT(ok);
195
196 #ifdef VMWGFX_PCI_UGLY_MAP_HACK
197 /*
198 * XXX
199 * Unmap the VGA registers.
200 */
201 if (sc->sc_temp_set)
202 bus_space_unmap(pa->pa_memt, sc->sc_temp_memh, 0x10000);
203 #endif
204
205 /*
206 * Cause any tasks issued synchronously during attach to be
207 * processed at the end of this function.
208 */
209 sc->sc_task_thread = curlwp;
210
211 sc->sc_drm_dev = drm_dev_alloc(vmwgfx_drm_driver, self);
212 if (IS_ERR(sc->sc_drm_dev)) {
213 aprint_error_dev(self, "unable to create drm device: %ld\n",
214 PTR_ERR(sc->sc_drm_dev));
215 sc->sc_drm_dev = NULL;
216 goto out;
217 }
218
219 /* XXX errno Linux->NetBSD */
220 error = -drm_pci_attach(sc->sc_drm_dev, &sc->sc_pci_dev);
221 if (error) {
222 aprint_error_dev(self, "unable to attach drm: %d\n", error);
223 goto out;
224 }
225 sc->sc_pci_attached = true;
226
227 /* XXX errno Linux->NetBSD */
228 error = -drm_dev_register(sc->sc_drm_dev, flags);
229 if (error) {
230 aprint_error_dev(self, "unable to register drm: %d\n", error);
231 goto out;
232 }
233 sc->sc_dev_registered = true;
234
235 if (!pmf_device_register(self, &vmwgfx_do_suspend, &vmwgfx_do_resume))
236 aprint_error_dev(self, "unable to establish power handler\n");
237
238 /*
239 * Process asynchronous tasks queued synchronously during
240 * attach. This will be for display detection to attach a
241 * framebuffer, so we have the opportunity for a console device
242 * to attach before autoconf has completed, in time for init(8)
243 * to find that console without panicking.
244 */
245 while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
246 struct vmwgfx_task *const task = SIMPLEQ_FIRST(&sc->sc_tasks);
247
248 SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, vt_u.queue);
249 (*task->vt_fn)(task);
250 }
251
252 out: /* Cause any subesquent tasks to be processed by the workqueue. */
253 atomic_store_relaxed(&sc->sc_task_thread, NULL);
254 }
255
256 static int
vmwgfx_detach(device_t self,int flags)257 vmwgfx_detach(device_t self, int flags)
258 {
259 struct vmwgfx_softc *const sc = device_private(self);
260 int error;
261
262 /* XXX Check for in-use before tearing it all down... */
263 error = config_detach_children(self, flags);
264 if (error)
265 return error;
266
267 KASSERT(sc->sc_task_thread == NULL);
268 KASSERT(SIMPLEQ_EMPTY(&sc->sc_tasks));
269
270 pmf_device_deregister(self);
271 if (sc->sc_dev_registered)
272 drm_dev_unregister(sc->sc_drm_dev);
273 if (sc->sc_pci_attached)
274 drm_pci_detach(sc->sc_drm_dev);
275 if (sc->sc_drm_dev) {
276 drm_dev_put(sc->sc_drm_dev);
277 sc->sc_drm_dev = NULL;
278 }
279 if (sc->sc_task_wq) {
280 workqueue_destroy(sc->sc_task_wq);
281 sc->sc_task_wq = NULL;
282 }
283 linux_pci_dev_destroy(&sc->sc_pci_dev);
284
285 return 0;
286 }
287
288 static bool
vmwgfx_do_suspend(device_t self,const pmf_qual_t * qual)289 vmwgfx_do_suspend(device_t self, const pmf_qual_t *qual)
290 {
291 struct vmwgfx_softc *const sc = device_private(self);
292 struct drm_device *const dev = sc->sc_drm_dev;
293 int ret;
294
295 ret = vmw_kms_suspend(dev);
296 if (ret)
297 return false;
298
299 return true;
300 }
301
302 static bool
vmwgfx_do_resume(device_t self,const pmf_qual_t * qual)303 vmwgfx_do_resume(device_t self, const pmf_qual_t *qual)
304 {
305 struct vmwgfx_softc *const sc = device_private(self);
306 struct drm_device *const dev = sc->sc_drm_dev;
307 int ret;
308
309 ret = vmw_kms_resume(dev);
310 if (ret)
311 return false;
312
313 return true;
314 }
315
316 static void
vmwgfx_task_work(struct work * work,void * cookie __unused)317 vmwgfx_task_work(struct work *work, void *cookie __unused)
318 {
319 struct vmwgfx_task *const task = container_of(work, struct vmwgfx_task,
320 vt_u.work);
321
322 (*task->vt_fn)(task);
323 }
324
325 void
vmwgfx_task_schedule(device_t self,struct vmwgfx_task * task)326 vmwgfx_task_schedule(device_t self, struct vmwgfx_task *task)
327 {
328 struct vmwgfx_softc *const sc = device_private(self);
329
330 if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
331 SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, vt_u.queue);
332 else
333 workqueue_enqueue(sc->sc_task_wq, &task->vt_u.work, NULL);
334 }
335