Home | History | Annotate | Line # | Download | only in nouveau
nouveau_pci.c revision 1.25
      1  1.25       mrg /*	$NetBSD: nouveau_pci.c,v 1.25 2019/10/05 22:37:49 mrg Exp $	*/
      2   1.1  riastrad 
      3   1.1  riastrad /*-
      4   1.1  riastrad  * Copyright (c) 2015 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.25       mrg __KERNEL_RCSID(0, "$NetBSD: nouveau_pci.c,v 1.25 2019/10/05 22:37:49 mrg Exp $");
     34   1.1  riastrad 
     35   1.1  riastrad #include <sys/types.h>
     36   1.1  riastrad #include <sys/device.h>
     37   1.1  riastrad #include <sys/queue.h>
     38   1.1  riastrad #include <sys/workqueue.h>
     39   1.4  jmcneill #include <sys/module.h>
     40   1.1  riastrad 
     41   1.1  riastrad #include <drm/drmP.h>
     42   1.1  riastrad 
     43  1.12  riastrad #include <core/device.h>
     44  1.13  riastrad #include <core/pci.h>
     45   1.3  riastrad 
     46   1.1  riastrad #include "nouveau_drm.h"
     47   1.1  riastrad #include "nouveau_pci.h"
     48   1.1  riastrad 
     49   1.4  jmcneill MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci");
     50   1.1  riastrad 
     51   1.4  jmcneill SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task);
     52   1.4  jmcneill 
     53   1.4  jmcneill struct nouveau_pci_softc {
     54   1.1  riastrad 	device_t		sc_dev;
     55  1.24       wiz 	struct pci_attach_args	sc_pa;
     56   1.1  riastrad 	enum {
     57   1.1  riastrad 		NOUVEAU_TASK_ATTACH,
     58   1.1  riastrad 		NOUVEAU_TASK_WORKQUEUE,
     59   1.1  riastrad 	}			sc_task_state;
     60   1.1  riastrad 	union {
     61   1.1  riastrad 		struct workqueue		*workqueue;
     62   1.4  jmcneill 		struct nouveau_pci_task_head	attach;
     63   1.1  riastrad 	}			sc_task_u;
     64   1.1  riastrad 	struct drm_device	*sc_drm_dev;
     65   1.1  riastrad 	struct pci_dev		sc_pci_dev;
     66  1.12  riastrad 	struct nvkm_device	*sc_nv_dev;
     67   1.1  riastrad };
     68   1.1  riastrad 
     69   1.4  jmcneill static int	nouveau_pci_match(device_t, cfdata_t, void *);
     70   1.4  jmcneill static void	nouveau_pci_attach(device_t, device_t, void *);
     71  1.24       wiz static void	nouveau_pci_attach_real(device_t);
     72   1.4  jmcneill static int	nouveau_pci_detach(device_t, int);
     73   1.1  riastrad 
     74   1.4  jmcneill static bool	nouveau_pci_suspend(device_t, const pmf_qual_t *);
     75   1.4  jmcneill static bool	nouveau_pci_resume(device_t, const pmf_qual_t *);
     76   1.1  riastrad 
     77   1.4  jmcneill static void	nouveau_pci_task_work(struct work *, void *);
     78   1.1  riastrad 
     79   1.4  jmcneill CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc),
     80   1.4  jmcneill     nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL);
     81   1.1  riastrad 
     82   1.1  riastrad /* Kludge to get this from nouveau_drm.c.  */
     83  1.14  riastrad extern struct drm_driver *const nouveau_drm_driver_pci;
     84   1.1  riastrad 
     85   1.1  riastrad static int
     86   1.4  jmcneill nouveau_pci_match(device_t parent, cfdata_t match, void *aux)
     87   1.1  riastrad {
     88   1.1  riastrad 	const struct pci_attach_args *const pa = aux;
     89  1.19  riastrad 	struct pci_dev pdev;
     90  1.19  riastrad 	struct nvkm_device *device;
     91  1.19  riastrad 	int ret;
     92   1.1  riastrad 
     93   1.1  riastrad 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
     94   1.1  riastrad 	    PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
     95   1.1  riastrad 		return 0;
     96   1.1  riastrad 
     97   1.1  riastrad 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
     98   1.1  riastrad 		return 0;
     99   1.1  riastrad 
    100  1.10       mrg 	/*
    101  1.23       mrg 	 * NetBSD drm2 doesn't support Pascal, Volta or Turing based cards:
    102  1.10       mrg 	 *   0x1580-0x15ff 	GP100
    103  1.10       mrg 	 *   0x1b00-0x1b7f 	GP102
    104  1.10       mrg 	 *   0x1b80-0x1bff 	GP104
    105  1.10       mrg 	 *   0x1c00-0x1c7f 	GP106
    106  1.10       mrg 	 *   0x1c80-0x1cff 	GP107
    107  1.10       mrg 	 *   0x1d00-0x1d7f 	GP108
    108  1.10       mrg 	 *   0x1d80-0x1dff 	GV100
    109  1.23       mrg 	 *   0x1e00-0x1e7f 	TU102
    110  1.23       mrg 	 *   0x1e80-0x1eff 	TU104
    111  1.23       mrg 	 *   0x1f00-0x1f7f 	TU106
    112  1.25       mrg 	 *   0x1f80-0x1fff 	TU117
    113  1.25       mrg 	 *   0x2180-0x21ff 	TU116
    114  1.25       mrg 	 *
    115  1.25       mrg 	 * reduce this to >= 1580, so that new chipsets not explictly
    116  1.25       mrg 	 * listed above will be picked up.
    117  1.25       mrg 	 *
    118  1.25       mrg 	 * XXX perhaps switch this to explicitly match known list.
    119  1.10       mrg 	 */
    120  1.25       mrg 	if (PCI_PRODUCT(pa->pa_id) >= 0x1580)
    121  1.10       mrg 		return 0;
    122  1.10       mrg 
    123  1.20  riastrad 	linux_pci_dev_init(&pdev, parent /* XXX bogus */, parent, pa, 0);
    124  1.20  riastrad 	ret = nvkm_device_pci_new(&pdev, NULL, "error",
    125  1.20  riastrad 	    /* detect */ true, /* mmio */ false, /* subdev_mask */ 0, &device);
    126  1.19  riastrad 	if (ret == 0)		/* don't want to hang onto it */
    127  1.19  riastrad 		nvkm_device_del(&device);
    128  1.19  riastrad 	linux_pci_dev_destroy(&pdev);
    129  1.19  riastrad 	if (ret)		/* failure */
    130  1.19  riastrad 		return 0;
    131  1.19  riastrad 
    132   1.1  riastrad 	return 6;		/* XXX Beat genfb_pci...  */
    133   1.1  riastrad }
    134   1.1  riastrad 
    135   1.3  riastrad extern char *nouveau_config;
    136   1.3  riastrad extern char *nouveau_debug;
    137   1.3  riastrad 
    138   1.1  riastrad static void
    139   1.4  jmcneill nouveau_pci_attach(device_t parent, device_t self, void *aux)
    140   1.1  riastrad {
    141   1.4  jmcneill 	struct nouveau_pci_softc *const sc = device_private(self);
    142   1.1  riastrad 	const struct pci_attach_args *const pa = aux;
    143   1.1  riastrad 
    144   1.1  riastrad 	pci_aprint_devinfo(pa, NULL);
    145   1.1  riastrad 
    146  1.24       wiz 	if (!pmf_device_register(self, &nouveau_pci_suspend, &nouveau_pci_resume))
    147  1.24       wiz 		aprint_error_dev(self, "unable to establish power handler\n");
    148  1.24       wiz 
    149  1.24       wiz 	/*
    150  1.24       wiz 	 * Trivial initialization first; the rest will come after we
    151  1.24       wiz 	 * have mounted the root file system and can load firmware
    152  1.24       wiz 	 * images.
    153  1.24       wiz 	 */
    154  1.24       wiz 	sc->sc_dev = NULL;
    155  1.24       wiz 	sc->sc_pa = *pa;
    156   1.1  riastrad 
    157  1.24       wiz 	config_mountroot(self, &nouveau_pci_attach_real);
    158  1.24       wiz }
    159  1.16  riastrad 
    160  1.24       wiz static void
    161  1.24       wiz nouveau_pci_attach_real(device_t self)
    162  1.24       wiz {
    163  1.24       wiz 	struct nouveau_pci_softc *const sc = device_private(self);
    164  1.24       wiz 	const struct pci_attach_args *const pa = &sc->sc_pa;
    165  1.24       wiz 	int error;
    166   1.1  riastrad 
    167  1.24       wiz 	sc->sc_dev = self;
    168   1.1  riastrad 	sc->sc_task_state = NOUVEAU_TASK_ATTACH;
    169   1.1  riastrad 	SIMPLEQ_INIT(&sc->sc_task_u.attach);
    170   1.1  riastrad 
    171  1.24       wiz 	/* Initialize the Linux PCI device descriptor.  */
    172  1.24       wiz 	linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
    173  1.24       wiz 
    174   1.3  riastrad 	/* XXX errno Linux->NetBSD */
    175  1.12  riastrad 	error = -nvkm_device_pci_new(&sc->sc_pci_dev,
    176  1.20  riastrad 	    nouveau_config, nouveau_debug,
    177  1.20  riastrad 	    /* detect */ true, /* mmio */ true, /* subdev_mask */ ~0ULL,
    178   1.3  riastrad 	    &sc->sc_nv_dev);
    179   1.3  riastrad 	if (error) {
    180   1.3  riastrad 		aprint_error_dev(self, "unable to create nouveau device: %d\n",
    181   1.3  riastrad 		    error);
    182   1.3  riastrad 		return;
    183   1.3  riastrad 	}
    184   1.3  riastrad 
    185   1.1  riastrad 	/* XXX errno Linux->NetBSD */
    186  1.14  riastrad 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev,
    187  1.14  riastrad 	    nouveau_drm_driver_pci, 0, &sc->sc_drm_dev);
    188   1.1  riastrad 	if (error) {
    189   1.1  riastrad 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    190   1.1  riastrad 		return;
    191   1.1  riastrad 	}
    192   1.1  riastrad 
    193   1.1  riastrad 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    194   1.4  jmcneill 		struct nouveau_pci_task *const task =
    195   1.1  riastrad 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    196   1.1  riastrad 
    197   1.1  riastrad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue);
    198   1.1  riastrad 		(*task->nt_fn)(task);
    199   1.1  riastrad 	}
    200   1.1  riastrad 
    201   1.1  riastrad 	sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE;
    202   1.4  jmcneill 	error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci",
    203   1.4  jmcneill 	    &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    204   1.1  riastrad 	if (error) {
    205   1.1  riastrad 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    206   1.1  riastrad 		    error);
    207   1.1  riastrad 		sc->sc_task_u.workqueue = NULL;
    208   1.1  riastrad 		return;
    209   1.1  riastrad 	}
    210   1.1  riastrad }
    211   1.1  riastrad 
    212   1.1  riastrad static int
    213   1.4  jmcneill nouveau_pci_detach(device_t self, int flags)
    214   1.1  riastrad {
    215   1.4  jmcneill 	struct nouveau_pci_softc *const sc = device_private(self);
    216   1.1  riastrad 	int error;
    217   1.1  riastrad 
    218  1.24       wiz 	if (sc->sc_dev == NULL)
    219  1.24       wiz 		/* Not done attaching.  */
    220  1.24       wiz 		return EBUSY;
    221  1.24       wiz 
    222   1.1  riastrad 	/* XXX Check for in-use before tearing it all down...  */
    223   1.1  riastrad 	error = config_detach_children(self, flags);
    224   1.1  riastrad 	if (error)
    225   1.1  riastrad 		return error;
    226   1.1  riastrad 
    227   1.1  riastrad 	if (sc->sc_task_state == NOUVEAU_TASK_ATTACH)
    228   1.3  riastrad 		goto out0;
    229   1.1  riastrad 	if (sc->sc_task_u.workqueue != NULL) {
    230   1.1  riastrad 		workqueue_destroy(sc->sc_task_u.workqueue);
    231   1.1  riastrad 		sc->sc_task_u.workqueue = NULL;
    232   1.1  riastrad 	}
    233   1.1  riastrad 
    234   1.3  riastrad 	if (sc->sc_nv_dev == NULL)
    235   1.3  riastrad 		goto out0;
    236   1.3  riastrad 
    237   1.1  riastrad 	if (sc->sc_drm_dev == NULL)
    238   1.3  riastrad 		goto out1;
    239   1.1  riastrad 	/* XXX errno Linux->NetBSD */
    240   1.1  riastrad 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    241   1.1  riastrad 	if (error)
    242   1.1  riastrad 		/* XXX Kinda too late to fail now...  */
    243   1.1  riastrad 		return error;
    244   1.1  riastrad 	sc->sc_drm_dev = NULL;
    245   1.1  riastrad 
    246  1.13  riastrad out1:	nvkm_device_del(&sc->sc_nv_dev);
    247  1.18  riastrad out0:	linux_pci_dev_destroy(&sc->sc_pci_dev);
    248  1.18  riastrad 	pmf_device_deregister(self);
    249   1.1  riastrad 	return 0;
    250   1.1  riastrad }
    251   1.1  riastrad 
    252   1.1  riastrad /*
    253   1.1  riastrad  * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
    254   1.1  riastrad  */
    255   1.1  riastrad static bool
    256   1.4  jmcneill nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused)
    257   1.1  riastrad {
    258   1.7  riastrad 	struct nouveau_pci_softc *const sc = device_private(self);
    259   1.1  riastrad 
    260   1.7  riastrad 	return nouveau_pmops_suspend(sc->sc_drm_dev) == 0;
    261   1.1  riastrad }
    262   1.1  riastrad 
    263   1.1  riastrad static bool
    264   1.4  jmcneill nouveau_pci_resume(device_t self, const pmf_qual_t *qual)
    265   1.1  riastrad {
    266   1.7  riastrad 	struct nouveau_pci_softc *const sc = device_private(self);
    267   1.1  riastrad 
    268   1.7  riastrad 	return nouveau_pmops_resume(sc->sc_drm_dev) == 0;
    269   1.1  riastrad }
    270   1.1  riastrad 
    271   1.1  riastrad static void
    272   1.4  jmcneill nouveau_pci_task_work(struct work *work, void *cookie __unused)
    273   1.1  riastrad {
    274   1.4  jmcneill 	struct nouveau_pci_task *const task = container_of(work,
    275   1.4  jmcneill 	    struct nouveau_pci_task, nt_u.work);
    276   1.1  riastrad 
    277   1.1  riastrad 	(*task->nt_fn)(task);
    278   1.1  riastrad }
    279   1.1  riastrad 
    280   1.1  riastrad int
    281   1.4  jmcneill nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task)
    282   1.1  riastrad {
    283   1.4  jmcneill 	struct nouveau_pci_softc *const sc = device_private(self);
    284   1.1  riastrad 
    285   1.1  riastrad 	switch (sc->sc_task_state) {
    286   1.1  riastrad 	case NOUVEAU_TASK_ATTACH:
    287   1.1  riastrad 		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue);
    288   1.1  riastrad 		return 0;
    289   1.1  riastrad 	case NOUVEAU_TASK_WORKQUEUE:
    290   1.1  riastrad 		if (sc->sc_task_u.workqueue == NULL) {
    291   1.1  riastrad 			aprint_error_dev(self, "unable to schedule task\n");
    292   1.1  riastrad 			return EIO;
    293   1.1  riastrad 		}
    294   1.1  riastrad 		workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work,
    295   1.1  riastrad 		    NULL);
    296   1.1  riastrad 		return 0;
    297   1.1  riastrad 	default:
    298   1.1  riastrad 		panic("nouveau in invalid task state: %d\n",
    299   1.1  riastrad 		    (int)sc->sc_task_state);
    300   1.1  riastrad 	}
    301   1.1  riastrad }
    302   1.4  jmcneill 
    303  1.21  riastrad extern struct drm_driver *const nouveau_drm_driver_stub; /* XXX */
    304  1.21  riastrad extern struct drm_driver *const nouveau_drm_driver_pci;	 /* XXX */
    305  1.21  riastrad 
    306   1.4  jmcneill static int
    307   1.4  jmcneill nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused)
    308   1.4  jmcneill {
    309   1.4  jmcneill 
    310   1.4  jmcneill 	switch (cmd) {
    311   1.4  jmcneill 	case MODULE_CMD_INIT:
    312  1.21  riastrad 		*nouveau_drm_driver_pci = *nouveau_drm_driver_stub;
    313  1.21  riastrad 		nouveau_drm_driver_pci->set_busid = drm_pci_set_busid;
    314  1.21  riastrad 		nouveau_drm_driver_pci->request_irq = drm_pci_request_irq;
    315  1.21  riastrad 		nouveau_drm_driver_pci->free_irq = drm_pci_free_irq;
    316   1.4  jmcneill #if 0		/* XXX nouveau acpi */
    317   1.4  jmcneill 		nouveau_register_dsm_handler();
    318   1.4  jmcneill #endif
    319   1.4  jmcneill 		break;
    320   1.4  jmcneill 	case MODULE_CMD_FINI:
    321   1.4  jmcneill #if 0		/* XXX nouveau acpi */
    322   1.4  jmcneill 		nouveau_unregister_dsm_handler();
    323   1.4  jmcneill #endif
    324   1.4  jmcneill 		break;
    325   1.4  jmcneill 	default:
    326   1.4  jmcneill 		return ENOTTY;
    327   1.4  jmcneill 	}
    328   1.4  jmcneill 
    329   1.4  jmcneill 	return 0;
    330   1.4  jmcneill }
    331