Home | History | Annotate | Line # | Download | only in i915drm
i915_pci_autoconf.c revision 1.2
      1 /*	$NetBSD: i915_pci_autoconf.c,v 1.2 2021/12/19 01:46:16 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 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: i915_pci_autoconf.c,v 1.2 2021/12/19 01:46:16 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/queue.h>
     37 #include <sys/systm.h>
     38 #include <sys/queue.h>
     39 #include <sys/workqueue.h>
     40 
     41 #include <drm/drmP.h>
     42 
     43 #include "i915_drv.h"
     44 #include "i915_pci.h"
     45 
     46 SIMPLEQ_HEAD(i915drmkms_task_head, i915drmkms_task);
     47 
     48 struct i915drmkms_softc {
     49 	device_t			sc_dev;
     50 	struct pci_attach_args		sc_pa;
     51 	enum {
     52 		I915DRMKMS_TASK_ATTACH,
     53 		I915DRMKMS_TASK_WORKQUEUE,
     54 	}				sc_task_state;
     55 	union {
     56 		struct workqueue		*workqueue;
     57 		struct i915drmkms_task_head	attach;
     58 	}				sc_task_u;
     59 	struct drm_device		*sc_drm_dev;
     60 	struct pci_dev			sc_pci_dev;
     61 };
     62 
     63 static const struct intel_device_info *
     64 		i915drmkms_pci_lookup(const struct pci_attach_args *);
     65 
     66 static int	i915drmkms_match(device_t, cfdata_t, void *);
     67 static void	i915drmkms_attach(device_t, device_t, void *);
     68 static void	i915drmkms_attach_real(device_t);
     69 static int	i915drmkms_detach(device_t, int);
     70 
     71 static bool	i915drmkms_suspend(device_t, const pmf_qual_t *);
     72 static bool	i915drmkms_resume(device_t, const pmf_qual_t *);
     73 
     74 static void	i915drmkms_task_work(struct work *, void *);
     75 
     76 CFATTACH_DECL_NEW(i915drmkms, sizeof(struct i915drmkms_softc),
     77     i915drmkms_match, i915drmkms_attach, i915drmkms_detach, NULL);
     78 
     79 /* XXX Kludge to get these from i915_drv.c.  */
     80 extern struct drm_driver *const i915_drm_driver;
     81 extern const struct pci_device_id *const i915_device_ids;
     82 extern const size_t i915_n_device_ids;
     83 
     84 static const struct intel_device_info *
     85 i915drmkms_pci_lookup(const struct pci_attach_args *pa)
     86 {
     87 	size_t i;
     88 
     89 	/* Attach only at function 0 to work around Intel lossage.  */
     90 	if (pa->pa_function != 0)
     91 		return NULL;
     92 
     93 	/* We're interested only in Intel products.  */
     94 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
     95 		return NULL;
     96 
     97 	/* We're interested only in Intel display devices.  */
     98 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
     99 		return NULL;
    100 
    101 	for (i = 0; i < i915_n_device_ids; i++)
    102 		if (PCI_PRODUCT(pa->pa_id) == i915_device_ids[i].device)
    103 			break;
    104 
    105 	/* Did we find it?  */
    106 	if (i == i915_n_device_ids)
    107 		return NULL;
    108 
    109 	const struct intel_device_info *const info =
    110 	    (const void *)(uintptr_t)i915_device_ids[i].driver_data;
    111 
    112 	if (IS_ALPHA_SUPPORT(info)) {
    113 		printf("i915drmkms: preliminary hardware support disabled\n");
    114 		return NULL;
    115 	}
    116 
    117 	return info;
    118 }
    119 
    120 static int
    121 i915drmkms_match(device_t parent, cfdata_t match, void *aux)
    122 {
    123 	extern int i915drmkms_guarantee_initialized(void);
    124 	const struct pci_attach_args *const pa = aux;
    125 	int error;
    126 
    127 	error = i915drmkms_guarantee_initialized();
    128 	if (error) {
    129 		aprint_error("i915drmkms: failed to initialize: %d\n", error);
    130 		return 0;
    131 	}
    132 
    133 	if (i915drmkms_pci_lookup(pa) == NULL)
    134 		return 0;
    135 
    136 	return 6;		/* XXX Beat genfb_pci...  */
    137 }
    138 
    139 static void
    140 i915drmkms_attach(device_t parent, device_t self, void *aux)
    141 {
    142 	struct i915drmkms_softc *const sc = device_private(self);
    143 	const struct pci_attach_args *const pa = aux;
    144 
    145 	pci_aprint_devinfo(pa, NULL);
    146 
    147 	if (!pmf_device_register(self, &i915drmkms_suspend,
    148 		&i915drmkms_resume))
    149 		aprint_error_dev(self, "unable to establish power handler\n");
    150 
    151 	/*
    152 	 * Trivial initialization first; the rest will come after we
    153 	 * have mounted the root file system and can load firmware
    154 	 * images.
    155 	 */
    156 	sc->sc_dev = self;
    157 	sc->sc_pa = *pa;
    158 
    159 	config_mountroot(self, &i915drmkms_attach_real);
    160 }
    161 
    162 static void
    163 i915drmkms_attach_real(device_t self)
    164 {
    165 	struct i915drmkms_softc *const sc = device_private(self);
    166 	struct pci_attach_args *const pa = &sc->sc_pa;
    167 	const struct intel_device_info *const info = i915drmkms_pci_lookup(pa);
    168 	const unsigned long cookie =
    169 	    (unsigned long)(uintptr_t)(const void *)info;
    170 	int error;
    171 
    172 	KASSERT(info != NULL);
    173 
    174 	sc->sc_task_state = I915DRMKMS_TASK_ATTACH;
    175 	SIMPLEQ_INIT(&sc->sc_task_u.attach);
    176 
    177 	/* Initialize the Linux PCI device descriptor.  */
    178 	linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
    179 
    180 	/* XXX errno Linux->NetBSD */
    181 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, i915_drm_driver,
    182 	    cookie, &sc->sc_drm_dev);
    183 	if (error) {
    184 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    185 		return;
    186 	}
    187 
    188 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    189 		struct i915drmkms_task *const task =
    190 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    191 
    192 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, ift_u.queue);
    193 		(*task->ift_fn)(task);
    194 	}
    195 
    196 	sc->sc_task_state = I915DRMKMS_TASK_WORKQUEUE;
    197 	error = workqueue_create(&sc->sc_task_u.workqueue, "intelfb",
    198 	    &i915drmkms_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    199 	if (error) {
    200 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    201 		    error);
    202 		sc->sc_task_u.workqueue = NULL;
    203 		return;
    204 	}
    205 }
    206 
    207 static int
    208 i915drmkms_detach(device_t self, int flags)
    209 {
    210 	struct i915drmkms_softc *const sc = device_private(self);
    211 	int error;
    212 
    213 	/* XXX Check for in-use before tearing it all down...  */
    214 	error = config_detach_children(self, flags);
    215 	if (error)
    216 		return error;
    217 
    218 	if (sc->sc_task_state == I915DRMKMS_TASK_ATTACH)
    219 		goto out;
    220 	if (sc->sc_task_u.workqueue != NULL) {
    221 		workqueue_destroy(sc->sc_task_u.workqueue);
    222 		sc->sc_task_u.workqueue = NULL;
    223 	}
    224 
    225 	if (sc->sc_drm_dev == NULL)
    226 		goto out;
    227 	/* XXX errno Linux->NetBSD */
    228 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    229 	if (error)
    230 		/* XXX Kinda too late to fail now...  */
    231 		return error;
    232 	sc->sc_drm_dev = NULL;
    233 
    234 out:	linux_pci_dev_destroy(&sc->sc_pci_dev);
    235 	pmf_device_deregister(self);
    236 	return 0;
    237 }
    238 
    239 static bool
    240 i915drmkms_suspend(device_t self, const pmf_qual_t *qual)
    241 {
    242 	struct i915drmkms_softc *const sc = device_private(self);
    243 	struct drm_device *const dev = sc->sc_drm_dev;
    244 	int ret;
    245 
    246 	if (dev == NULL)
    247 		return true;
    248 
    249 	ret = i915_drm_suspend(dev);
    250 	if (ret)
    251 		return false;
    252 	ret = i915_drm_suspend_late(dev, false);
    253 	if (ret)
    254 		return false;
    255 
    256 	return true;
    257 }
    258 
    259 static bool
    260 i915drmkms_resume(device_t self, const pmf_qual_t *qual)
    261 {
    262 	struct i915drmkms_softc *const sc = device_private(self);
    263 	struct drm_device *const dev = sc->sc_drm_dev;
    264 	int ret;
    265 
    266 	if (dev == NULL)
    267 		return true;
    268 
    269 	ret = i915_drm_resume_early(dev);
    270 	if (ret)
    271 		return false;
    272 	ret = i915_drm_resume(dev);
    273 	if (ret)
    274 		return false;
    275 
    276 	return true;
    277 }
    278 
    279 static void
    280 i915drmkms_task_work(struct work *work, void *cookie __unused)
    281 {
    282 	struct i915drmkms_task *const task = container_of(work,
    283 	    struct i915drmkms_task, ift_u.work);
    284 
    285 	(*task->ift_fn)(task);
    286 }
    287 
    288 int
    289 i915drmkms_task_schedule(device_t self, struct i915drmkms_task *task)
    290 {
    291 	struct i915drmkms_softc *const sc = device_private(self);
    292 
    293 	switch (sc->sc_task_state) {
    294 	case I915DRMKMS_TASK_ATTACH:
    295 		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, ift_u.queue);
    296 		return 0;
    297 	case I915DRMKMS_TASK_WORKQUEUE:
    298 		if (sc->sc_task_u.workqueue == NULL) {
    299 			aprint_error_dev(self, "unable to schedule task\n");
    300 			return EIO;
    301 		}
    302 		workqueue_enqueue(sc->sc_task_u.workqueue, &task->ift_u.work,
    303 		    NULL);
    304 		return 0;
    305 	default:
    306 		panic("i915drmkms in invalid task state: %d\n",
    307 		    (int)sc->sc_task_state);
    308 	}
    309 }
    310