Home | History | Annotate | Line # | Download | only in nouveau
nouveau_pci.c revision 1.10
      1 /*	$NetBSD: nouveau_pci.c,v 1.10 2018/05/31 09:18:31 mrg 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: nouveau_pci.c,v 1.10 2018/05/31 09:18:31 mrg Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/device.h>
     37 #include <sys/queue.h>
     38 #include <sys/workqueue.h>
     39 #include <sys/module.h>
     40 
     41 #include <drm/drmP.h>
     42 
     43 #include <engine/device.h>
     44 
     45 #include "nouveau_drm.h"
     46 #include "nouveau_pci.h"
     47 
     48 MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci");
     49 
     50 SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task);
     51 
     52 struct nouveau_pci_softc {
     53 	device_t		sc_dev;
     54 	enum {
     55 		NOUVEAU_TASK_ATTACH,
     56 		NOUVEAU_TASK_WORKQUEUE,
     57 	}			sc_task_state;
     58 	union {
     59 		struct workqueue		*workqueue;
     60 		struct nouveau_pci_task_head	attach;
     61 	}			sc_task_u;
     62 	struct drm_device	*sc_drm_dev;
     63 	struct pci_dev		sc_pci_dev;
     64 	struct nouveau_device	*sc_nv_dev;
     65 };
     66 
     67 static int	nouveau_pci_match(device_t, cfdata_t, void *);
     68 static void	nouveau_pci_attach(device_t, device_t, void *);
     69 static int	nouveau_pci_detach(device_t, int);
     70 
     71 static bool	nouveau_pci_suspend(device_t, const pmf_qual_t *);
     72 static bool	nouveau_pci_resume(device_t, const pmf_qual_t *);
     73 
     74 static void	nouveau_pci_task_work(struct work *, void *);
     75 
     76 CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc),
     77     nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL);
     78 
     79 /* Kludge to get this from nouveau_drm.c.  */
     80 extern struct drm_driver *const nouveau_drm_driver;
     81 
     82 static int
     83 nouveau_pci_match(device_t parent, cfdata_t match, void *aux)
     84 {
     85 	const struct pci_attach_args *const pa = aux;
     86 
     87 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
     88 	    PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
     89 		return 0;
     90 
     91 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
     92 		return 0;
     93 
     94 #define IS_BETWEEN(x,y) \
     95 	(PCI_PRODUCT(pa->pa_id) >= (x) && PCI_PRODUCT(pa->pa_id) <= (y))
     96 
     97 	/*
     98 	 * NetBSD drm2 doesn't support Pascal-based cards:
     99 	 *   0x1580-0x15ff 	GP100
    100 	 *   0x1b00-0x1b7f 	GP102
    101 	 *   0x1b80-0x1bff 	GP104
    102 	 *   0x1c00-0x1c7f 	GP106
    103 	 *   0x1c80-0x1cff 	GP107
    104 	 *   0x1d00-0x1d7f 	GP108
    105 	 *   0x1d80-0x1dff 	GV100
    106 	 */
    107 
    108 	if (IS_BETWEEN(0x1580, 0x15ff) ||
    109 	    IS_BETWEEN(0x1b00, 0x1b7f) ||
    110 	    IS_BETWEEN(0x1b80, 0x1bff) ||
    111 	    IS_BETWEEN(0x1c00, 0x1c7f) ||
    112 	    IS_BETWEEN(0x1c80, 0x1cff) ||
    113 	    IS_BETWEEN(0x1d00, 0x1d7f) ||
    114 	    IS_BETWEEN(0x1d80, 0x1dff))
    115 		return 0;
    116 #undef IS_BETWEEN
    117 
    118 #define IS_BETWEEN(x,y) \
    119 	(PCI_PRODUCT(pa->pa_id) >= (x) && PCI_PRODUCT(pa->pa_id) <= (y))
    120 	/*
    121 	 * NetBSD drm2 needs missing-so-far firmware for Maxwell-based cards:
    122 	 *   0x1380-0x13bf 	GM107
    123 	 */
    124 	if (IS_BETWEEN(0x1380, 0x13bf))
    125 		return 0;
    126 
    127 	/*
    128 	 * NetBSD drm2 doesn't support Pascal-based cards:
    129 	 *   0x1580-0x15ff 	GP100
    130 	 *   0x1b00-0x1b7f 	GP102
    131 	 *   0x1b80-0x1bff 	GP104
    132 	 *   0x1c00-0x1c7f 	GP106
    133 	 *   0x1c80-0x1cff 	GP107
    134 	 *   0x1d00-0x1d7f 	GP108
    135 	 *   0x1d80-0x1dff 	GV100
    136 	 */
    137 
    138 	if (IS_BETWEEN(0x1580, 0x15ff) ||
    139 	    IS_BETWEEN(0x1b00, 0x1b7f) ||
    140 	    IS_BETWEEN(0x1b80, 0x1bff) ||
    141 	    IS_BETWEEN(0x1c00, 0x1c7f) ||
    142 	    IS_BETWEEN(0x1c80, 0x1cff) ||
    143 	    IS_BETWEEN(0x1d00, 0x1d7f) ||
    144 	    IS_BETWEEN(0x1d80, 0x1dff))
    145 		return 0;
    146 #undef IS_BETWEEN
    147 
    148 	return 6;		/* XXX Beat genfb_pci...  */
    149 }
    150 
    151 extern char *nouveau_config;
    152 extern char *nouveau_debug;
    153 
    154 static void
    155 nouveau_pci_attach(device_t parent, device_t self, void *aux)
    156 {
    157 	struct nouveau_pci_softc *const sc = device_private(self);
    158 	const struct pci_attach_args *const pa = aux;
    159 	uint64_t devname;
    160 	int error;
    161 
    162 	pci_aprint_devinfo(pa, NULL);
    163 
    164 	sc->sc_dev = self;
    165 
    166 	if (!pmf_device_register(self, &nouveau_pci_suspend,
    167 		&nouveau_pci_resume))
    168 		aprint_error_dev(self, "unable to establish power handler\n");
    169 
    170 	sc->sc_task_state = NOUVEAU_TASK_ATTACH;
    171 	SIMPLEQ_INIT(&sc->sc_task_u.attach);
    172 
    173 	devname = (uint64_t)device_unit(device_parent(self)) << 32;
    174 	devname |= pa->pa_bus << 16;
    175 	/* XXX errno Linux->NetBSD */
    176 	error = -nouveau_device_create(&sc->sc_pci_dev, NOUVEAU_BUS_PCI,
    177 	    devname, device_xname(self), nouveau_config, nouveau_debug,
    178 	    &sc->sc_nv_dev);
    179 	if (error) {
    180 		aprint_error_dev(self, "unable to create nouveau device: %d\n",
    181 		    error);
    182 		return;
    183 	}
    184 
    185 	/* XXX errno Linux->NetBSD */
    186 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, nouveau_drm_driver,
    187 	    0, &sc->sc_drm_dev);
    188 	if (error) {
    189 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    190 		return;
    191 	}
    192 
    193 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    194 		struct nouveau_pci_task *const task =
    195 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    196 
    197 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue);
    198 		(*task->nt_fn)(task);
    199 	}
    200 
    201 	sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE;
    202 	error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci",
    203 	    &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    204 	if (error) {
    205 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    206 		    error);
    207 		sc->sc_task_u.workqueue = NULL;
    208 		return;
    209 	}
    210 }
    211 
    212 static int
    213 nouveau_pci_detach(device_t self, int flags)
    214 {
    215 	struct nouveau_pci_softc *const sc = device_private(self);
    216 	int error;
    217 
    218 	/* XXX Check for in-use before tearing it all down...  */
    219 	error = config_detach_children(self, flags);
    220 	if (error)
    221 		return error;
    222 
    223 	if (sc->sc_task_state == NOUVEAU_TASK_ATTACH)
    224 		goto out0;
    225 	if (sc->sc_task_u.workqueue != NULL) {
    226 		workqueue_destroy(sc->sc_task_u.workqueue);
    227 		sc->sc_task_u.workqueue = NULL;
    228 	}
    229 
    230 	if (sc->sc_nv_dev == NULL)
    231 		goto out0;
    232 
    233 	if (sc->sc_drm_dev == NULL)
    234 		goto out1;
    235 	/* XXX errno Linux->NetBSD */
    236 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    237 	if (error)
    238 		/* XXX Kinda too late to fail now...  */
    239 		return error;
    240 	sc->sc_drm_dev = NULL;
    241 
    242 out1:	nouveau_object_ref(NULL, (void *)&sc->sc_nv_dev);
    243 out0:	pmf_device_deregister(self);
    244 	return 0;
    245 }
    246 
    247 /*
    248  * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
    249  */
    250 static bool
    251 nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused)
    252 {
    253 	struct nouveau_pci_softc *const sc = device_private(self);
    254 
    255 	return nouveau_pmops_suspend(sc->sc_drm_dev) == 0;
    256 }
    257 
    258 static bool
    259 nouveau_pci_resume(device_t self, const pmf_qual_t *qual)
    260 {
    261 	struct nouveau_pci_softc *const sc = device_private(self);
    262 
    263 	return nouveau_pmops_resume(sc->sc_drm_dev) == 0;
    264 }
    265 
    266 static void
    267 nouveau_pci_task_work(struct work *work, void *cookie __unused)
    268 {
    269 	struct nouveau_pci_task *const task = container_of(work,
    270 	    struct nouveau_pci_task, nt_u.work);
    271 
    272 	(*task->nt_fn)(task);
    273 }
    274 
    275 int
    276 nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task)
    277 {
    278 	struct nouveau_pci_softc *const sc = device_private(self);
    279 
    280 	switch (sc->sc_task_state) {
    281 	case NOUVEAU_TASK_ATTACH:
    282 		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue);
    283 		return 0;
    284 	case NOUVEAU_TASK_WORKQUEUE:
    285 		if (sc->sc_task_u.workqueue == NULL) {
    286 			aprint_error_dev(self, "unable to schedule task\n");
    287 			return EIO;
    288 		}
    289 		workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work,
    290 		    NULL);
    291 		return 0;
    292 	default:
    293 		panic("nouveau in invalid task state: %d\n",
    294 		    (int)sc->sc_task_state);
    295 	}
    296 }
    297 
    298 static int
    299 nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused)
    300 {
    301 	int error;
    302 
    303 	switch (cmd) {
    304 	case MODULE_CMD_INIT:
    305 		error = drm_pci_init(nouveau_drm_driver, NULL);
    306 		if (error) {
    307 			aprint_error("nouveau_pci: failed to init: %d\n",
    308 			    error);
    309 			return error;
    310 		}
    311 #if 0		/* XXX nouveau acpi */
    312 		nouveau_register_dsm_handler();
    313 #endif
    314 		break;
    315 	case MODULE_CMD_FINI:
    316 #if 0		/* XXX nouveau acpi */
    317 		nouveau_unregister_dsm_handler();
    318 #endif
    319 		drm_pci_exit(nouveau_drm_driver, NULL);
    320 		break;
    321 	default:
    322 		return ENOTTY;
    323 	}
    324 
    325 	return 0;
    326 }
    327