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