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