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