Home | History | Annotate | Line # | Download | only in i915drm
i915_pci_autoconf.c revision 1.6
      1 /*	$NetBSD: i915_pci_autoconf.c,v 1.6 2021/12/19 11:53:02 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.6 2021/12/19 11:53:02 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 	sc->sc_drm_dev = drm_dev_alloc(i915_drm_driver, self);
    184 	if (IS_ERR(sc->sc_drm_dev)) {
    185 		aprint_error_dev(self, "unable to create drm device: %ld\n",
    186 		    PTR_ERR(sc->sc_drm_dev));
    187 		sc->sc_drm_dev = NULL;
    188 		return;
    189 	}
    190 
    191 	/* XXX errno Linux->NetBSD */
    192 	error = -drm_pci_attach(sc->sc_drm_dev, pa, &sc->sc_pci_dev);
    193 	if (error) {
    194 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    195 		return;
    196 	}
    197 	sc->sc_pci_attached = true;
    198 
    199 	/* XXX errno Linux->NetBSD */
    200 	error = -i915_driver_probe(&sc->sc_pci_dev, ent);
    201 	if (error) {
    202 		aprint_error_dev(self, "unable to register drm: %d\n", error);
    203 		return;
    204 	}
    205 	sc->sc_dev_registered = true;
    206 
    207 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    208 		struct i915drmkms_task *const task =
    209 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    210 
    211 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, ift_u.queue);
    212 		(*task->ift_fn)(task);
    213 	}
    214 
    215 	sc->sc_task_state = I915DRMKMS_TASK_WORKQUEUE;
    216 	error = workqueue_create(&sc->sc_task_u.workqueue, "intelfb",
    217 	    &i915drmkms_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    218 	if (error) {
    219 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    220 		    error);
    221 		sc->sc_task_u.workqueue = NULL;
    222 		return;
    223 	}
    224 }
    225 
    226 static int
    227 i915drmkms_detach(device_t self, int flags)
    228 {
    229 	struct i915drmkms_softc *const sc = device_private(self);
    230 	int error;
    231 
    232 	/* XXX Check for in-use before tearing it all down...  */
    233 	error = config_detach_children(self, flags);
    234 	if (error)
    235 		return error;
    236 
    237 	if (sc->sc_task_state == I915DRMKMS_TASK_ATTACH)
    238 		goto out0;
    239 	if (sc->sc_task_u.workqueue != NULL) {
    240 		workqueue_destroy(sc->sc_task_u.workqueue);
    241 		sc->sc_task_u.workqueue = NULL;
    242 	}
    243 
    244 	if (sc->sc_drm_dev == NULL)
    245 		goto out0;
    246 	if (!sc->sc_pci_attached)
    247 		goto out1;
    248 	if (!sc->sc_dev_registered)
    249 		goto out2;
    250 
    251 	drm_dev_unregister(sc->sc_drm_dev);
    252 out2:	drm_pci_detach(sc->sc_drm_dev);
    253 out1:	drm_dev_put(sc->sc_drm_dev);
    254 	sc->sc_drm_dev = NULL;
    255 out0:	linux_pci_dev_destroy(&sc->sc_pci_dev);
    256 	pmf_device_deregister(self);
    257 	return 0;
    258 }
    259 
    260 static bool
    261 i915drmkms_suspend(device_t self, const pmf_qual_t *qual)
    262 {
    263 	struct i915drmkms_softc *const sc = device_private(self);
    264 	struct drm_device *const dev = sc->sc_drm_dev;
    265 	int ret;
    266 
    267 	if (dev == NULL)
    268 		return true;
    269 
    270 	ret = i915_drm_suspend(dev);
    271 	if (ret)
    272 		return false;
    273 	ret = i915_drm_suspend_late(dev, false);
    274 	if (ret)
    275 		return false;
    276 
    277 	return true;
    278 }
    279 
    280 static bool
    281 i915drmkms_resume(device_t self, const pmf_qual_t *qual)
    282 {
    283 	struct i915drmkms_softc *const sc = device_private(self);
    284 	struct drm_device *const dev = sc->sc_drm_dev;
    285 	int ret;
    286 
    287 	if (dev == NULL)
    288 		return true;
    289 
    290 	ret = i915_drm_resume_early(dev);
    291 	if (ret)
    292 		return false;
    293 	ret = i915_drm_resume(dev);
    294 	if (ret)
    295 		return false;
    296 
    297 	return true;
    298 }
    299 
    300 static void
    301 i915drmkms_task_work(struct work *work, void *cookie __unused)
    302 {
    303 	struct i915drmkms_task *const task = container_of(work,
    304 	    struct i915drmkms_task, ift_u.work);
    305 
    306 	(*task->ift_fn)(task);
    307 }
    308 
    309 int
    310 i915drmkms_task_schedule(device_t self, struct i915drmkms_task *task)
    311 {
    312 	struct i915drmkms_softc *const sc = device_private(self);
    313 
    314 	switch (sc->sc_task_state) {
    315 	case I915DRMKMS_TASK_ATTACH:
    316 		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, ift_u.queue);
    317 		return 0;
    318 	case I915DRMKMS_TASK_WORKQUEUE:
    319 		if (sc->sc_task_u.workqueue == NULL) {
    320 			aprint_error_dev(self, "unable to schedule task\n");
    321 			return EIO;
    322 		}
    323 		workqueue_enqueue(sc->sc_task_u.workqueue, &task->ift_u.work,
    324 		    NULL);
    325 		return 0;
    326 	default:
    327 		panic("i915drmkms in invalid task state: %d\n",
    328 		    (int)sc->sc_task_state);
    329 	}
    330 }
    331