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