Home | History | Annotate | Line # | Download | only in radeon
radeon_pci.c revision 1.16
      1 /*	$NetBSD: radeon_pci.c,v 1.16 2021/12/19 09:56:17 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 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: radeon_pci.c,v 1.16 2021/12/19 09:56:17 riastradh Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "vga.h"
     37 #if defined(__arm__) || defined(__aarch64__)
     38 #include "opt_fdt.h"
     39 #endif
     40 #endif
     41 
     42 #include <sys/types.h>
     43 #include <sys/queue.h>
     44 #include <sys/systm.h>
     45 #include <sys/workqueue.h>
     46 
     47 #include <dev/pci/pciio.h>
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcivar.h>
     50 
     51 #include <dev/pci/wsdisplay_pci.h>
     52 #include <dev/wsfb/genfbvar.h>
     53 
     54 #if NVGA > 0
     55 /*
     56  * XXX All we really need is vga_is_console from vgavar.h, but the
     57  * header files are missing their own dependencies, so we need to
     58  * explicitly drag in the other crap.
     59  */
     60 #include <dev/ic/mc6845reg.h>
     61 #include <dev/ic/pcdisplayvar.h>
     62 #include <dev/ic/vgareg.h>
     63 #include <dev/ic/vgavar.h>
     64 #endif
     65 
     66 #ifdef FDT
     67 #include <dev/fdt/fdtvar.h>
     68 #endif
     69 
     70 #include <drm/drm_pci.h>
     71 #include <drm/drm_fb_helper.h>
     72 
     73 #include <radeon.h>
     74 #include "radeon_drv.h"
     75 #include "radeon_task.h"
     76 
     77 SIMPLEQ_HEAD(radeon_task_head, radeon_task);
     78 
     79 struct radeon_softc {
     80 	device_t			sc_dev;
     81 	struct pci_attach_args		sc_pa;
     82 	enum {
     83 		RADEON_TASK_ATTACH,
     84 		RADEON_TASK_WORKQUEUE,
     85 	}				sc_task_state;
     86 	union {
     87 		struct workqueue		*workqueue;
     88 		struct radeon_task_head		attach;
     89 	}				sc_task_u;
     90 	struct drm_device		*sc_drm_dev;
     91 	struct pci_dev			sc_pci_dev;
     92 #if defined(__i386__)
     93 #define RADEON_PCI_UGLY_MAP_HACK
     94 	/* XXX Used to claim the VGA device before attach_real */
     95 	bus_space_handle_t		sc_temp_memh;
     96 	bool				sc_temp_set;
     97 #endif
     98 };
     99 
    100 struct radeon_device *
    101 radeon_device_private(device_t self)
    102 {
    103 	struct radeon_softc *const sc = device_private(self);
    104 
    105 	return sc->sc_drm_dev->dev_private;
    106 }
    107 
    108 static bool	radeon_pci_lookup(const struct pci_attach_args *,
    109 		    unsigned long *);
    110 
    111 static int	radeon_match(device_t, cfdata_t, void *);
    112 static void	radeon_attach(device_t, device_t, void *);
    113 static void	radeon_attach_real(device_t);
    114 static int	radeon_detach(device_t, int);
    115 static bool	radeon_do_suspend(device_t, const pmf_qual_t *);
    116 static bool	radeon_do_resume(device_t, const pmf_qual_t *);
    117 
    118 static void	radeon_task_work(struct work *, void *);
    119 
    120 CFATTACH_DECL_NEW(radeon, sizeof(struct radeon_softc),
    121     radeon_match, radeon_attach, radeon_detach, NULL);
    122 
    123 /* XXX Kludge to get these from radeon_drv.c.  */
    124 extern struct drm_driver *const radeon_drm_driver;
    125 extern const struct pci_device_id *const radeon_device_ids;
    126 extern const size_t radeon_n_device_ids;
    127 
    128 /* Set this to false if you want to match R100/R200 */
    129 bool radeon_pci_ignore_r100_r200 = true;
    130 
    131 static bool
    132 radeon_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
    133 {
    134 	size_t i;
    135 	enum radeon_family fam;
    136 
    137 	for (i = 0; i < radeon_n_device_ids; i++) {
    138 		if ((PCI_VENDOR(pa->pa_id) == radeon_device_ids[i].vendor) &&
    139 		    (PCI_PRODUCT(pa->pa_id) == radeon_device_ids[i].device))
    140 			break;
    141 	}
    142 
    143 	/* Did we find it?  */
    144 	if (i == radeon_n_device_ids)
    145 		return false;
    146 
    147 	/* NetBSD drm2 fails on R100 and many R200 chipsets, disable for now  */
    148 	fam = radeon_device_ids[i].driver_data & RADEON_FAMILY_MASK;
    149 	if (radeon_pci_ignore_r100_r200 && fam < CHIP_RV280)
    150 		return false;
    151 
    152 	if (flags)
    153 		*flags = radeon_device_ids[i].driver_data;
    154 	return true;
    155 }
    156 
    157 static int
    158 radeon_match(device_t parent, cfdata_t match, void *aux)
    159 {
    160 	extern int radeon_guarantee_initialized(void);
    161 	const struct pci_attach_args *const pa = aux;
    162 	int error;
    163 
    164 	error = radeon_guarantee_initialized();
    165 	if (error) {
    166 		aprint_error("radeon: failed to initialize: %d\n", error);
    167 		return 0;
    168 	}
    169 
    170 	if (!radeon_pci_lookup(pa, NULL))
    171 		return 0;
    172 
    173 	return 6;		/* XXX Beat genfb_pci...  */
    174 }
    175 
    176 static void
    177 radeon_attach(device_t parent, device_t self, void *aux)
    178 {
    179 	struct radeon_softc *const sc = device_private(self);
    180 	const struct pci_attach_args *const pa = aux;
    181 
    182 	pci_aprint_devinfo(pa, NULL);
    183 
    184 	if (!pmf_device_register(self, &radeon_do_suspend, &radeon_do_resume))
    185 		aprint_error_dev(self, "unable to establish power handler\n");
    186 
    187 	/*
    188 	 * Trivial initialization first; the rest will come after we
    189 	 * have mounted the root file system and can load firmware
    190 	 * images.
    191 	 */
    192 	sc->sc_dev = NULL;
    193 	sc->sc_pa = *pa;
    194 
    195 #ifdef RADEON_PCI_UGLY_MAP_HACK
    196 	/*
    197 	 * XXX
    198 	 * We try to map the VGA registers, in case we can prevent vga@isa or
    199 	 * pcdisplay@isa attaching, and stealing wsdisplay0.  This only works
    200 	 * with serial console, as actual VGA console has already mapped them.
    201 	 * The only way to handle that is for vga@isa to not attach.
    202 	 */
    203 	int rv = bus_space_map(pa->pa_memt, 0xb0000, 0x10000, 0,
    204 			       &sc->sc_temp_memh);
    205 	sc->sc_temp_set = rv == 0;
    206 	if (rv != 0)
    207 		aprint_error_dev(self, "unable to reserve VGA registers for "
    208 				       "i386 radeondrmkms hack\n");
    209 #endif
    210 
    211 #ifdef FDT
    212 	/*
    213 	 * XXX Remove the simple framebuffer, assuming that this device
    214 	 * will take over.
    215 	 */
    216 	const char *fb_compatible[] = { "simple-framebuffer", NULL };
    217 	fdt_remove_bycompat(fb_compatible);
    218 #endif
    219 
    220 	config_mountroot(self, &radeon_attach_real);
    221 }
    222 
    223 static void
    224 radeon_attach_real(device_t self)
    225 {
    226 	struct radeon_softc *const sc = device_private(self);
    227 	const struct pci_attach_args *const pa = &sc->sc_pa;
    228 	bool ok __diagused;
    229 	unsigned long flags;
    230 	int error;
    231 
    232 	ok = radeon_pci_lookup(pa, &flags);
    233 	KASSERT(ok);
    234 
    235 #ifdef RADEON_PCI_UGLY_MAP_HACK
    236 	/*
    237 	 * XXX
    238 	 * Unmap the VGA registers.
    239 	 */
    240 	if (sc->sc_temp_set)
    241 		bus_space_unmap(pa->pa_memt, sc->sc_temp_memh, 0x10000);
    242 #endif
    243 
    244 	sc->sc_task_state = RADEON_TASK_ATTACH;
    245 	SIMPLEQ_INIT(&sc->sc_task_u.attach);
    246 
    247 	/* Initialize the Linux PCI device descriptor.  */
    248 	linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
    249 
    250 	/* XXX errno Linux->NetBSD */
    251 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, radeon_drm_driver,
    252 	    flags, &sc->sc_drm_dev);
    253 	if (error) {
    254 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    255 		goto out;
    256 	}
    257 
    258 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    259 		struct radeon_task *const task =
    260 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    261 
    262 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, rt_u.queue);
    263 		(*task->rt_fn)(task);
    264 	}
    265 
    266 	sc->sc_task_state = RADEON_TASK_WORKQUEUE;
    267 	error = workqueue_create(&sc->sc_task_u.workqueue, "radeonfb",
    268 	    &radeon_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    269 	if (error) {
    270 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    271 		    error);
    272 		sc->sc_task_u.workqueue = NULL;
    273 		goto out;
    274 	}
    275 
    276 out:	sc->sc_dev = self;
    277 }
    278 
    279 static int
    280 radeon_detach(device_t self, int flags)
    281 {
    282 	struct radeon_softc *const sc = device_private(self);
    283 	int error;
    284 
    285 	if (sc->sc_dev == NULL)
    286 		/* Not done attaching.  */
    287 		return EBUSY;
    288 
    289 	/* XXX Check for in-use before tearing it all down...  */
    290 	error = config_detach_children(self, flags);
    291 	if (error)
    292 		return error;
    293 
    294 	if (sc->sc_task_state == RADEON_TASK_ATTACH)
    295 		goto out;
    296 	if (sc->sc_task_u.workqueue != NULL) {
    297 		workqueue_destroy(sc->sc_task_u.workqueue);
    298 		sc->sc_task_u.workqueue = NULL;
    299 	}
    300 
    301 	if (sc->sc_drm_dev == NULL)
    302 		goto out;
    303 	/* XXX errno Linux->NetBSD */
    304 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    305 	if (error)
    306 		/* XXX Kinda too late to fail now...  */
    307 		return error;
    308 	sc->sc_drm_dev = NULL;
    309 
    310 out:	linux_pci_dev_destroy(&sc->sc_pci_dev);
    311 	pmf_device_deregister(self);
    312 
    313 	return 0;
    314 }
    315 
    316 static bool
    317 radeon_do_suspend(device_t self, const pmf_qual_t *qual)
    318 {
    319 	struct radeon_softc *const sc = device_private(self);
    320 	struct drm_device *const dev = sc->sc_drm_dev;
    321 	int ret;
    322 	bool is_console = true; /* XXX */
    323 
    324 	if (dev == NULL)
    325 		return true;
    326 
    327 	ret = radeon_suspend_kms(dev, true, is_console, false);
    328 	if (ret)
    329 		return false;
    330 
    331 	return true;
    332 }
    333 
    334 static bool
    335 radeon_do_resume(device_t self, const pmf_qual_t *qual)
    336 {
    337 	struct radeon_softc *const sc = device_private(self);
    338 	struct drm_device *const dev = sc->sc_drm_dev;
    339 	int ret;
    340 	bool is_console = true; /* XXX */
    341 
    342 	if (dev == NULL)
    343 		return true;
    344 
    345 	ret = radeon_resume_kms(dev, true, is_console);
    346 	if (ret)
    347 		return false;
    348 
    349 	return true;
    350 }
    351 
    352 static void
    353 radeon_task_work(struct work *work, void *cookie __unused)
    354 {
    355 	struct radeon_task *const task = container_of(work, struct radeon_task,
    356 	    rt_u.work);
    357 
    358 	(*task->rt_fn)(task);
    359 }
    360 
    361 int
    362 radeon_task_schedule(device_t self, struct radeon_task *task)
    363 {
    364 	struct radeon_softc *const sc = device_private(self);
    365 
    366 	switch (sc->sc_task_state) {
    367 	case RADEON_TASK_ATTACH:
    368 		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, rt_u.queue);
    369 		return 0;
    370 	case RADEON_TASK_WORKQUEUE:
    371 		if (sc->sc_task_u.workqueue == NULL) {
    372 			aprint_error_dev(self, "unable to schedule task\n");
    373 			return EIO;
    374 		}
    375 		workqueue_enqueue(sc->sc_task_u.workqueue, &task->rt_u.work,
    376 		    NULL);
    377 		return 0;
    378 	default:
    379 		panic("radeon in invalid task state: %d\n",
    380 		    (int)sc->sc_task_state);
    381 	}
    382 }
    383