Home | History | Annotate | Line # | Download | only in i915drm
i915_pci_autoconf.c revision 1.8
      1 /*	$NetBSD: i915_pci_autoconf.c,v 1.8 2021/12/19 11:54:03 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.8 2021/12/19 11:54:03 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_drv.c.  */
     84 extern struct drm_driver *const i915_drm_driver;
     85 extern const struct pci_device_id *const i915_device_ids;
     86 extern const size_t i915_n_device_ids;
     87 
     88 static const struct pci_device_id *
     89 i915drmkms_pci_lookup(const struct pci_attach_args *pa)
     90 {
     91 	size_t i;
     92 
     93 	/* Attach only at function 0 to work around Intel lossage.  */
     94 	if (pa->pa_function != 0)
     95 		return NULL;
     96 
     97 	/* We're interested only in Intel products.  */
     98 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
     99 		return NULL;
    100 
    101 	/* We're interested only in Intel display devices.  */
    102 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    103 		return NULL;
    104 
    105 	for (i = 0; i < i915_n_device_ids; i++)
    106 		if (PCI_PRODUCT(pa->pa_id) == i915_device_ids[i].device)
    107 			break;
    108 
    109 	/* Did we find it?  */
    110 	if (i == i915_n_device_ids)
    111 		return NULL;
    112 
    113 	const struct pci_device_id *ent = &i915_device_ids[i];
    114 	const struct intel_device_info *const info = (struct intel_device_info *) ent->driver_data;
    115 
    116 	if (info->require_force_probe) {
    117 		printf("i915drmkms: preliminary hardware support disabled\n");
    118 		return NULL;
    119 	}
    120 
    121 	return ent;
    122 }
    123 
    124 static int
    125 i915drmkms_match(device_t parent, cfdata_t match, void *aux)
    126 {
    127 	extern int i915drmkms_guarantee_initialized(void);
    128 	const struct pci_attach_args *const pa = aux;
    129 	int error;
    130 
    131 	error = i915drmkms_guarantee_initialized();
    132 	if (error) {
    133 		aprint_error("i915drmkms: failed to initialize: %d\n", error);
    134 		return 0;
    135 	}
    136 
    137 	if (i915drmkms_pci_lookup(pa) == NULL)
    138 		return 0;
    139 
    140 	return 6;		/* XXX Beat genfb_pci...  */
    141 }
    142 
    143 static void
    144 i915drmkms_attach(device_t parent, device_t self, void *aux)
    145 {
    146 	struct i915drmkms_softc *const sc = device_private(self);
    147 	const struct pci_attach_args *const pa = aux;
    148 
    149 	pci_aprint_devinfo(pa, NULL);
    150 
    151 	if (!pmf_device_register(self, &i915drmkms_suspend,
    152 		&i915drmkms_resume))
    153 		aprint_error_dev(self, "unable to establish power handler\n");
    154 
    155 	/*
    156 	 * Trivial initialization first; the rest will come after we
    157 	 * have mounted the root file system and can load firmware
    158 	 * images.
    159 	 */
    160 	sc->sc_dev = self;
    161 	sc->sc_pa = *pa;
    162 
    163 	config_mountroot(self, &i915drmkms_attach_real);
    164 }
    165 
    166 static void
    167 i915drmkms_attach_real(device_t self)
    168 {
    169 	struct i915drmkms_softc *const sc = device_private(self);
    170 	struct pci_attach_args *const pa = &sc->sc_pa;
    171 	const struct pci_device_id *ent = i915drmkms_pci_lookup(pa);
    172 	const struct intel_device_info *const info = (struct intel_device_info *) ent->driver_data;
    173 	int error;
    174 
    175 	KASSERT(info != NULL);
    176 
    177 	sc->sc_task_state = I915DRMKMS_TASK_ATTACH;
    178 	SIMPLEQ_INIT(&sc->sc_task_u.attach);
    179 
    180 	/* Initialize the Linux PCI device descriptor.  */
    181 	linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
    182 
    183 	/* XXX errno Linux->NetBSD */
    184 	error = -i915_driver_probe(&sc->sc_pci_dev, ent);
    185 	if (error) {
    186 		aprint_error_dev(self, "unable to register drm: %d\n", error);
    187 		return;
    188 	}
    189 	sc->sc_dev_registered = true;
    190 	sc->sc_drm_dev = pci_get_drvdata(&sc->sc_pci_dev);
    191 
    192 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    193 		struct i915drmkms_task *const task =
    194 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    195 
    196 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, ift_u.queue);
    197 		(*task->ift_fn)(task);
    198 	}
    199 
    200 	sc->sc_task_state = I915DRMKMS_TASK_WORKQUEUE;
    201 	error = workqueue_create(&sc->sc_task_u.workqueue, "intelfb",
    202 	    &i915drmkms_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    203 	if (error) {
    204 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    205 		    error);
    206 		sc->sc_task_u.workqueue = NULL;
    207 		return;
    208 	}
    209 }
    210 
    211 static int
    212 i915drmkms_detach(device_t self, int flags)
    213 {
    214 	struct i915drmkms_softc *const sc = device_private(self);
    215 	int error;
    216 
    217 	/* XXX Check for in-use before tearing it all down...  */
    218 	error = config_detach_children(self, flags);
    219 	if (error)
    220 		return error;
    221 
    222 	if (sc->sc_task_state == I915DRMKMS_TASK_ATTACH)
    223 		goto out0;
    224 	if (sc->sc_task_u.workqueue != NULL) {
    225 		workqueue_destroy(sc->sc_task_u.workqueue);
    226 		sc->sc_task_u.workqueue = NULL;
    227 	}
    228 
    229 	if (sc->sc_drm_dev == NULL)
    230 		goto out0;
    231 
    232 	i915_driver_remove(sc->sc_drm_dev->dev_private);
    233 	sc->sc_drm_dev = NULL;
    234 out0:	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