Home | History | Annotate | Line # | Download | only in via
via_pci.c revision 1.5
      1 /*	$NetBSD: via_pci.c,v 1.5 2021/02/13 15:42:15 jakllsch Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2015 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: via_pci.c,v 1.5 2021/02/13 15:42:15 jakllsch Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/device.h>
     37 #include <sys/pmf.h>
     38 #include <sys/systm.h>
     39 
     40 #include <linux/pci.h>
     41 
     42 #include <drm/drmP.h>
     43 #include <drm/drm_pciids.h>
     44 #include <drm/via_drm.h>
     45 
     46 #include "via_drv.h"
     47 
     48 struct viadrm_softc {
     49 	device_t		sc_dev;
     50 	struct pci_dev		sc_pci_dev;
     51 	struct drm_device	*sc_drm_dev;
     52 };
     53 
     54 static int	viadrm_match(device_t, cfdata_t, void *);
     55 static void	viadrm_attach(device_t, device_t, void *);
     56 static int	viadrm_detach(device_t, int);
     57 
     58 extern struct drm_driver *const via_drm_driver; /* XXX */
     59 
     60 CFATTACH_DECL_NEW(viadrmums, sizeof(struct viadrm_softc),
     61     viadrm_match, viadrm_attach, viadrm_detach, NULL);
     62 
     63 static const struct pci_device_id viadrm_pci_ids[] = {
     64 	viadrv_PCI_IDS
     65 };
     66 
     67 static const unsigned long *
     68 viadrm_lookup(const struct pci_attach_args *pa)
     69 {
     70 	unsigned i;
     71 
     72 	for (i = 0; i < __arraycount(viadrm_pci_ids); i++) {
     73 		if (viadrm_pci_ids[i].vendor == 0 &&
     74 		    viadrm_pci_ids[i].device == 0)
     75 			break;
     76 		KASSERT(viadrm_pci_ids[i].subvendor == PCI_ANY_ID);
     77 		KASSERT(viadrm_pci_ids[i].subdevice == PCI_ANY_ID);
     78 		KASSERT(viadrm_pci_ids[i].class == 0);
     79 		KASSERT(viadrm_pci_ids[i].class_mask == 0);
     80 		if (PCI_VENDOR(pa->pa_id) != viadrm_pci_ids[i].vendor)
     81 			continue;
     82 		if (PCI_PRODUCT(pa->pa_id) != viadrm_pci_ids[i].device)
     83 			continue;
     84 		return &viadrm_pci_ids[i].driver_data;
     85 	}
     86 
     87 	return NULL;
     88 }
     89 
     90 static int
     91 viadrm_match(device_t parent, cfdata_t match, void *aux)
     92 {
     93 	extern int viadrm_guarantee_initialized(void);
     94 	const struct pci_attach_args *const pa = aux;
     95 	int error;
     96 
     97 	error = viadrm_guarantee_initialized();
     98 	if (error) {
     99 		aprint_error("viadrm: failed to initialize: %d\n", error);
    100 		return 0;
    101 	}
    102 
    103 	if (viadrm_lookup(pa) == NULL)
    104 		return 0;
    105 
    106 	return 1;
    107 }
    108 
    109 static void
    110 viadrm_attach(device_t parent, device_t self, void *aux)
    111 {
    112 	struct viadrm_softc *const sc = device_private(self);
    113 	const struct pci_attach_args *const pa = aux;
    114 	const unsigned long *const cookiep = viadrm_lookup(pa);
    115 	int error;
    116 
    117 	KASSERT(cookiep != NULL);
    118 
    119 	aprint_naive("\n");
    120 	aprint_normal("\n");
    121 
    122 	if (!pmf_device_register(self, NULL, NULL))
    123 		aprint_error_dev(self, "couldn't establish power handler\n");
    124 
    125 	/* Initialize the Linux PCI device descriptor.  */
    126 	linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
    127 
    128 	/* XXX errno Linux->NetBSD */
    129 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, via_drm_driver,
    130 	    *cookiep, &sc->sc_drm_dev);
    131 	if (error) {
    132 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    133 		return;
    134 	}
    135 }
    136 
    137 static int
    138 viadrm_detach(device_t self, int flags)
    139 {
    140 	struct viadrm_softc *const sc = device_private(self);
    141 	int error;
    142 
    143 	error = config_detach_children(self, flags);
    144 	if (error)
    145 		return error;
    146 	if (sc->sc_drm_dev == NULL)
    147 		goto out;
    148 	/* XXX errno Linux->NetBSD */
    149 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    150 	if (error)
    151 		return error;
    152 	sc->sc_drm_dev = NULL;
    153 
    154 out:	linux_pci_dev_destroy(&sc->sc_pci_dev);
    155 	pmf_device_deregister(self);
    156 	return 0;
    157 }
    158