Home | History | Annotate | Line # | Download | only in nouveau
nouveau_pci.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: nouveau_pci.c,v 1.1 2015/03/06 01:43:07 riastradh 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.1  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_pci.c,v 1.1 2015/03/06 01:43:07 riastradh 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.1  riastrad 
     40  1.1  riastrad #include <drm/drmP.h>
     41  1.1  riastrad 
     42  1.1  riastrad #include "nouveau_drm.h"
     43  1.1  riastrad #include "nouveau_pci.h"
     44  1.1  riastrad 
     45  1.1  riastrad SIMPLEQ_HEAD(nouveau_task_head, nouveau_task);
     46  1.1  riastrad 
     47  1.1  riastrad struct nouveau_softc {
     48  1.1  riastrad 	device_t		sc_dev;
     49  1.1  riastrad 	enum {
     50  1.1  riastrad 		NOUVEAU_TASK_ATTACH,
     51  1.1  riastrad 		NOUVEAU_TASK_WORKQUEUE,
     52  1.1  riastrad 	}			sc_task_state;
     53  1.1  riastrad 	union {
     54  1.1  riastrad 		struct workqueue		*workqueue;
     55  1.1  riastrad 		struct nouveau_task_head	attach;
     56  1.1  riastrad 	}			sc_task_u;
     57  1.1  riastrad 	struct drm_device	*sc_drm_dev;
     58  1.1  riastrad 	struct pci_dev		sc_pci_dev;
     59  1.1  riastrad };
     60  1.1  riastrad 
     61  1.1  riastrad static int	nouveau_match(device_t, cfdata_t, void *);
     62  1.1  riastrad static void	nouveau_attach(device_t, device_t, void *);
     63  1.1  riastrad static int	nouveau_detach(device_t, int);
     64  1.1  riastrad 
     65  1.1  riastrad static bool	nouveau_suspend(device_t, const pmf_qual_t *);
     66  1.1  riastrad static bool	nouveau_resume(device_t, const pmf_qual_t *);
     67  1.1  riastrad 
     68  1.1  riastrad static void	nouveau_task_work(struct work *, void *);
     69  1.1  riastrad 
     70  1.1  riastrad CFATTACH_DECL_NEW(nouveau, sizeof(struct nouveau_softc),
     71  1.1  riastrad     nouveau_match, nouveau_attach, nouveau_detach, NULL);
     72  1.1  riastrad 
     73  1.1  riastrad /* Kludge to get this from nouveau_drm.c.  */
     74  1.1  riastrad extern struct drm_driver *const nouveau_drm_driver;
     75  1.1  riastrad 
     76  1.1  riastrad static int
     77  1.1  riastrad nouveau_match(device_t parent, cfdata_t match, void *aux)
     78  1.1  riastrad {
     79  1.1  riastrad 	extern int nouveau_guarantee_initialized(void);
     80  1.1  riastrad 	const struct pci_attach_args *const pa = aux;
     81  1.1  riastrad 	int error;
     82  1.1  riastrad 
     83  1.1  riastrad 	error = nouveau_guarantee_initialized();
     84  1.1  riastrad 	if (error) {
     85  1.1  riastrad 		aprint_error("nouveau: failed to initialize: %d\n", error);
     86  1.1  riastrad 		return 0;
     87  1.1  riastrad 	}
     88  1.1  riastrad 
     89  1.1  riastrad 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
     90  1.1  riastrad 	    PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
     91  1.1  riastrad 		return 0;
     92  1.1  riastrad 
     93  1.1  riastrad 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
     94  1.1  riastrad 		return 0;
     95  1.1  riastrad 
     96  1.1  riastrad 	return 6;		/* XXX Beat genfb_pci...  */
     97  1.1  riastrad }
     98  1.1  riastrad 
     99  1.1  riastrad static void
    100  1.1  riastrad nouveau_attach(device_t parent, device_t self, void *aux)
    101  1.1  riastrad {
    102  1.1  riastrad 	struct nouveau_softc *const sc = device_private(self);
    103  1.1  riastrad 	const struct pci_attach_args *const pa = aux;
    104  1.1  riastrad 	int error;
    105  1.1  riastrad 
    106  1.1  riastrad 	pci_aprint_devinfo(pa, NULL);
    107  1.1  riastrad 
    108  1.1  riastrad 	sc->sc_dev = self;
    109  1.1  riastrad 
    110  1.1  riastrad 	if (!pmf_device_register(self, &nouveau_suspend,
    111  1.1  riastrad 		&nouveau_resume))
    112  1.1  riastrad 		aprint_error_dev(self, "unable to establish power handler\n");
    113  1.1  riastrad 
    114  1.1  riastrad 	sc->sc_task_state = NOUVEAU_TASK_ATTACH;
    115  1.1  riastrad 	SIMPLEQ_INIT(&sc->sc_task_u.attach);
    116  1.1  riastrad 
    117  1.1  riastrad 
    118  1.1  riastrad 	/* XXX errno Linux->NetBSD */
    119  1.1  riastrad 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, nouveau_drm_driver,
    120  1.1  riastrad 	    0, &sc->sc_drm_dev);
    121  1.1  riastrad 	if (error) {
    122  1.1  riastrad 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    123  1.1  riastrad 		return;
    124  1.1  riastrad 	}
    125  1.1  riastrad 
    126  1.1  riastrad 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    127  1.1  riastrad 		struct nouveau_task *const task =
    128  1.1  riastrad 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    129  1.1  riastrad 
    130  1.1  riastrad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue);
    131  1.1  riastrad 		(*task->nt_fn)(task);
    132  1.1  riastrad 	}
    133  1.1  riastrad 
    134  1.1  riastrad 	sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE;
    135  1.1  riastrad 	error = workqueue_create(&sc->sc_task_u.workqueue, "intelfb",
    136  1.1  riastrad 	    &nouveau_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    137  1.1  riastrad 	if (error) {
    138  1.1  riastrad 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    139  1.1  riastrad 		    error);
    140  1.1  riastrad 		sc->sc_task_u.workqueue = NULL;
    141  1.1  riastrad 		return;
    142  1.1  riastrad 	}
    143  1.1  riastrad }
    144  1.1  riastrad 
    145  1.1  riastrad static int
    146  1.1  riastrad nouveau_detach(device_t self, int flags)
    147  1.1  riastrad {
    148  1.1  riastrad 	struct nouveau_softc *const sc = device_private(self);
    149  1.1  riastrad 	int error;
    150  1.1  riastrad 
    151  1.1  riastrad 	/* XXX Check for in-use before tearing it all down...  */
    152  1.1  riastrad 	error = config_detach_children(self, flags);
    153  1.1  riastrad 	if (error)
    154  1.1  riastrad 		return error;
    155  1.1  riastrad 
    156  1.1  riastrad 	if (sc->sc_task_state == NOUVEAU_TASK_ATTACH)
    157  1.1  riastrad 		goto out;
    158  1.1  riastrad 	if (sc->sc_task_u.workqueue != NULL) {
    159  1.1  riastrad 		workqueue_destroy(sc->sc_task_u.workqueue);
    160  1.1  riastrad 		sc->sc_task_u.workqueue = NULL;
    161  1.1  riastrad 	}
    162  1.1  riastrad 
    163  1.1  riastrad 	if (sc->sc_drm_dev == NULL)
    164  1.1  riastrad 		goto out;
    165  1.1  riastrad 	/* XXX errno Linux->NetBSD */
    166  1.1  riastrad 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    167  1.1  riastrad 	if (error)
    168  1.1  riastrad 		/* XXX Kinda too late to fail now...  */
    169  1.1  riastrad 		return error;
    170  1.1  riastrad 	sc->sc_drm_dev = NULL;
    171  1.1  riastrad 
    172  1.1  riastrad out:	pmf_device_deregister(self);
    173  1.1  riastrad 	return 0;
    174  1.1  riastrad }
    175  1.1  riastrad 
    176  1.1  riastrad /*
    177  1.1  riastrad  * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
    178  1.1  riastrad  */
    179  1.1  riastrad static bool
    180  1.1  riastrad nouveau_suspend(device_t self, const pmf_qual_t *qual __unused)
    181  1.1  riastrad {
    182  1.1  riastrad 	struct nouveau_softc *const sc = device_private(self);
    183  1.1  riastrad 	struct device *const dev = &sc->sc_pci_dev.dev; /* XXX KLUDGE */
    184  1.1  riastrad 
    185  1.1  riastrad 	return nouveau_pmops_suspend(dev) == 0;
    186  1.1  riastrad }
    187  1.1  riastrad 
    188  1.1  riastrad static bool
    189  1.1  riastrad nouveau_resume(device_t self, const pmf_qual_t *qual)
    190  1.1  riastrad {
    191  1.1  riastrad 	struct nouveau_softc *const sc = device_private(self);
    192  1.1  riastrad 	struct device *const dev = &sc->sc_pci_dev.dev; /* XXX KLUDGE */
    193  1.1  riastrad 
    194  1.1  riastrad 	return nouveau_pmops_resume(dev) == 0;
    195  1.1  riastrad }
    196  1.1  riastrad 
    197  1.1  riastrad static void
    198  1.1  riastrad nouveau_task_work(struct work *work, void *cookie __unused)
    199  1.1  riastrad {
    200  1.1  riastrad 	struct nouveau_task *const task = container_of(work,
    201  1.1  riastrad 	    struct nouveau_task, nt_u.work);
    202  1.1  riastrad 
    203  1.1  riastrad 	(*task->nt_fn)(task);
    204  1.1  riastrad }
    205  1.1  riastrad 
    206  1.1  riastrad int
    207  1.1  riastrad nouveau_task_schedule(device_t self, struct nouveau_task *task)
    208  1.1  riastrad {
    209  1.1  riastrad 	struct nouveau_softc *const sc = device_private(self);
    210  1.1  riastrad 
    211  1.1  riastrad 	switch (sc->sc_task_state) {
    212  1.1  riastrad 	case NOUVEAU_TASK_ATTACH:
    213  1.1  riastrad 		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue);
    214  1.1  riastrad 		return 0;
    215  1.1  riastrad 	case NOUVEAU_TASK_WORKQUEUE:
    216  1.1  riastrad 		if (sc->sc_task_u.workqueue == NULL) {
    217  1.1  riastrad 			aprint_error_dev(self, "unable to schedule task\n");
    218  1.1  riastrad 			return EIO;
    219  1.1  riastrad 		}
    220  1.1  riastrad 		workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work,
    221  1.1  riastrad 		    NULL);
    222  1.1  riastrad 		return 0;
    223  1.1  riastrad 	default:
    224  1.1  riastrad 		panic("nouveau in invalid task state: %d\n",
    225  1.1  riastrad 		    (int)sc->sc_task_state);
    226  1.1  riastrad 	}
    227  1.1  riastrad }
    228