Home | History | Annotate | Line # | Download | only in radeon
radeon_pci.c revision 1.9
      1  1.9       mrg /*	$NetBSD: radeon_pci.c,v 1.9 2015/04/19 01:08:56 mrg Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/cdefs.h>
     33  1.9       mrg __KERNEL_RCSID(0, "$NetBSD: radeon_pci.c,v 1.9 2015/04/19 01:08:56 mrg Exp $");
     34  1.1  riastrad 
     35  1.1  riastrad #ifdef _KERNEL_OPT
     36  1.1  riastrad #include "vga.h"
     37  1.1  riastrad #endif
     38  1.1  riastrad 
     39  1.1  riastrad #include <sys/types.h>
     40  1.1  riastrad #include <sys/queue.h>
     41  1.1  riastrad #include <sys/systm.h>
     42  1.1  riastrad #include <sys/workqueue.h>
     43  1.1  riastrad 
     44  1.1  riastrad #include <dev/pci/pciio.h>
     45  1.1  riastrad #include <dev/pci/pcireg.h>
     46  1.1  riastrad #include <dev/pci/pcivar.h>
     47  1.1  riastrad 
     48  1.1  riastrad #include <dev/pci/wsdisplay_pci.h>
     49  1.1  riastrad #include <dev/wsfb/genfbvar.h>
     50  1.1  riastrad 
     51  1.1  riastrad #if NVGA > 0
     52  1.1  riastrad /*
     53  1.1  riastrad  * XXX All we really need is vga_is_console from vgavar.h, but the
     54  1.1  riastrad  * header files are missing their own dependencies, so we need to
     55  1.1  riastrad  * explicitly drag in the other crap.
     56  1.1  riastrad  */
     57  1.1  riastrad #include <dev/ic/mc6845reg.h>
     58  1.1  riastrad #include <dev/ic/pcdisplayvar.h>
     59  1.1  riastrad #include <dev/ic/vgareg.h>
     60  1.1  riastrad #include <dev/ic/vgavar.h>
     61  1.1  riastrad #endif
     62  1.1  riastrad 
     63  1.1  riastrad #include <drm/drmP.h>
     64  1.1  riastrad #include <drm/drm_fb_helper.h>
     65  1.1  riastrad 
     66  1.1  riastrad #include <radeon.h>
     67  1.1  riastrad #include "radeon_drv.h"
     68  1.2  riastrad #include "radeon_task.h"
     69  1.1  riastrad 
     70  1.2  riastrad SIMPLEQ_HEAD(radeon_task_head, radeon_task);
     71  1.1  riastrad 
     72  1.1  riastrad struct radeon_softc {
     73  1.1  riastrad 	device_t			sc_dev;
     74  1.3  riastrad 	struct pci_attach_args		sc_pa;
     75  1.2  riastrad 	enum {
     76  1.2  riastrad 		RADEON_TASK_ATTACH,
     77  1.2  riastrad 		RADEON_TASK_WORKQUEUE,
     78  1.2  riastrad 	}				sc_task_state;
     79  1.2  riastrad 	union {
     80  1.2  riastrad 		struct workqueue		*workqueue;
     81  1.2  riastrad 		struct radeon_task_head		attach;
     82  1.2  riastrad 	}				sc_task_u;
     83  1.1  riastrad 	struct drm_device		*sc_drm_dev;
     84  1.1  riastrad 	struct pci_dev			sc_pci_dev;
     85  1.7       mrg #if defined(__i386__)
     86  1.5       mrg #define RADEON_PCI_UGLY_MAP_HACK
     87  1.5       mrg 	/* XXX Used to claim the VGA device before attach_real */
     88  1.5       mrg 	bus_space_handle_t		sc_temp_memh;
     89  1.5       mrg 	bool				sc_temp_set;
     90  1.5       mrg #endif
     91  1.1  riastrad };
     92  1.1  riastrad 
     93  1.2  riastrad struct radeon_device *
     94  1.2  riastrad radeon_device_private(device_t self)
     95  1.2  riastrad {
     96  1.2  riastrad 	struct radeon_softc *const sc = device_private(self);
     97  1.2  riastrad 
     98  1.2  riastrad 	return sc->sc_drm_dev->dev_private;
     99  1.2  riastrad }
    100  1.1  riastrad 
    101  1.1  riastrad static bool	radeon_pci_lookup(const struct pci_attach_args *,
    102  1.1  riastrad 		    unsigned long *);
    103  1.1  riastrad 
    104  1.1  riastrad static int	radeon_match(device_t, cfdata_t, void *);
    105  1.1  riastrad static void	radeon_attach(device_t, device_t, void *);
    106  1.3  riastrad static void	radeon_attach_real(device_t);
    107  1.1  riastrad static int	radeon_detach(device_t, int);
    108  1.8       mrg static bool	radeon_do_suspend(device_t, const pmf_qual_t *);
    109  1.8       mrg static bool	radeon_do_resume(device_t, const pmf_qual_t *);
    110  1.1  riastrad 
    111  1.2  riastrad static void	radeon_task_work(struct work *, void *);
    112  1.1  riastrad 
    113  1.4  riastrad CFATTACH_DECL_NEW(radeon, sizeof(struct radeon_softc),
    114  1.1  riastrad     radeon_match, radeon_attach, radeon_detach, NULL);
    115  1.1  riastrad 
    116  1.1  riastrad /* XXX Kludge to get these from radeon_drv.c.  */
    117  1.1  riastrad extern struct drm_driver *const radeon_drm_driver;
    118  1.1  riastrad extern const struct pci_device_id *const radeon_device_ids;
    119  1.1  riastrad extern const size_t radeon_n_device_ids;
    120  1.1  riastrad 
    121  1.1  riastrad static bool
    122  1.1  riastrad radeon_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
    123  1.1  riastrad {
    124  1.1  riastrad 	size_t i;
    125  1.1  riastrad 
    126  1.1  riastrad 	for (i = 0; i < radeon_n_device_ids; i++) {
    127  1.1  riastrad 		if ((PCI_VENDOR(pa->pa_id) == radeon_device_ids[i].vendor) &&
    128  1.1  riastrad 		    (PCI_PRODUCT(pa->pa_id) == radeon_device_ids[i].device))
    129  1.1  riastrad 			break;
    130  1.1  riastrad 	}
    131  1.1  riastrad 
    132  1.1  riastrad 	/* Did we find it?  */
    133  1.1  riastrad 	if (i == radeon_n_device_ids)
    134  1.1  riastrad 		return false;
    135  1.1  riastrad 
    136  1.1  riastrad 	if (flags)
    137  1.1  riastrad 		*flags = radeon_device_ids[i].driver_data;
    138  1.1  riastrad 	return true;
    139  1.1  riastrad }
    140  1.1  riastrad 
    141  1.1  riastrad static int
    142  1.1  riastrad radeon_match(device_t parent, cfdata_t match, void *aux)
    143  1.1  riastrad {
    144  1.1  riastrad 	extern int radeon_guarantee_initialized(void);
    145  1.1  riastrad 	const struct pci_attach_args *const pa = aux;
    146  1.1  riastrad 	int error;
    147  1.1  riastrad 
    148  1.1  riastrad 	error = radeon_guarantee_initialized();
    149  1.1  riastrad 	if (error) {
    150  1.1  riastrad 		aprint_error("radeon: failed to initialize: %d\n", error);
    151  1.1  riastrad 		return 0;
    152  1.1  riastrad 	}
    153  1.1  riastrad 
    154  1.1  riastrad 	if (!radeon_pci_lookup(pa, NULL))
    155  1.1  riastrad 		return 0;
    156  1.1  riastrad 
    157  1.1  riastrad 	return 6;		/* XXX Beat genfb_pci...  */
    158  1.1  riastrad }
    159  1.1  riastrad 
    160  1.1  riastrad static void
    161  1.1  riastrad radeon_attach(device_t parent, device_t self, void *aux)
    162  1.1  riastrad {
    163  1.1  riastrad 	struct radeon_softc *const sc = device_private(self);
    164  1.1  riastrad 	const struct pci_attach_args *const pa = aux;
    165  1.3  riastrad 
    166  1.3  riastrad 	pci_aprint_devinfo(pa, NULL);
    167  1.3  riastrad 
    168  1.8       mrg 	if (!pmf_device_register(self, &radeon_do_suspend, &radeon_do_resume))
    169  1.8       mrg 		aprint_error_dev(self, "unable to establish power handler\n");
    170  1.8       mrg 
    171  1.3  riastrad 	/*
    172  1.3  riastrad 	 * Trivial initialization first; the rest will come after we
    173  1.3  riastrad 	 * have mounted the root file system and can load firmware
    174  1.3  riastrad 	 * images.
    175  1.3  riastrad 	 */
    176  1.3  riastrad 	sc->sc_dev = NULL;
    177  1.3  riastrad 	sc->sc_pa = *pa;
    178  1.3  riastrad 
    179  1.5       mrg #ifdef RADEON_PCI_UGLY_MAP_HACK
    180  1.5       mrg 	/*
    181  1.5       mrg 	 * XXX
    182  1.7       mrg 	 * We try to map the VGA registers, in case we can prevent vga@isa or
    183  1.7       mrg 	 * pcdisplay@isa attaching, and stealing wsdisplay0.  This only works
    184  1.7       mrg 	 * with serial console, as actual VGA console has already mapped them.
    185  1.7       mrg 	 * The only way to handle that is for vga@isa to not attach.
    186  1.5       mrg 	 */
    187  1.6       mrg 	int rv = bus_space_map(pa->pa_memt, 0xb0000, 0x10000, 0,
    188  1.6       mrg 			       &sc->sc_temp_memh);
    189  1.6       mrg 	sc->sc_temp_set = rv == 0;
    190  1.6       mrg 	if (rv != 0)
    191  1.6       mrg 		aprint_error_dev(self, "unable to reserve VGA registers for "
    192  1.6       mrg 				       "i386 radeondrmkms hack\n");
    193  1.5       mrg #endif
    194  1.5       mrg 
    195  1.3  riastrad 	config_mountroot(self, &radeon_attach_real);
    196  1.3  riastrad }
    197  1.3  riastrad 
    198  1.3  riastrad static void
    199  1.3  riastrad radeon_attach_real(device_t self)
    200  1.3  riastrad {
    201  1.3  riastrad 	struct radeon_softc *const sc = device_private(self);
    202  1.3  riastrad 	const struct pci_attach_args *const pa = &sc->sc_pa;
    203  1.1  riastrad 	bool ok __diagused;
    204  1.1  riastrad 	unsigned long flags;
    205  1.1  riastrad 	int error;
    206  1.1  riastrad 
    207  1.1  riastrad 	ok = radeon_pci_lookup(pa, &flags);
    208  1.1  riastrad 	KASSERT(ok);
    209  1.1  riastrad 
    210  1.5       mrg #ifdef RADEON_PCI_UGLY_MAP_HACK
    211  1.5       mrg 	/*
    212  1.5       mrg 	 * XXX
    213  1.7       mrg 	 * Unmap the VGA registers.
    214  1.5       mrg 	 */
    215  1.5       mrg 	if (sc->sc_temp_set)
    216  1.5       mrg 		bus_space_unmap(pa->pa_memt, sc->sc_temp_memh, 0x10000);
    217  1.5       mrg #endif
    218  1.5       mrg 
    219  1.2  riastrad 	sc->sc_task_state = RADEON_TASK_ATTACH;
    220  1.2  riastrad 	SIMPLEQ_INIT(&sc->sc_task_u.attach);
    221  1.1  riastrad 
    222  1.1  riastrad 	/* XXX errno Linux->NetBSD */
    223  1.1  riastrad 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, radeon_drm_driver,
    224  1.1  riastrad 	    flags, &sc->sc_drm_dev);
    225  1.1  riastrad 	if (error) {
    226  1.1  riastrad 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    227  1.3  riastrad 		goto out;
    228  1.1  riastrad 	}
    229  1.1  riastrad 
    230  1.2  riastrad 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    231  1.2  riastrad 		struct radeon_task *const task =
    232  1.2  riastrad 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    233  1.1  riastrad 
    234  1.2  riastrad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, rt_u.queue);
    235  1.2  riastrad 		(*task->rt_fn)(task);
    236  1.1  riastrad 	}
    237  1.1  riastrad 
    238  1.2  riastrad 	sc->sc_task_state = RADEON_TASK_WORKQUEUE;
    239  1.2  riastrad 	error = workqueue_create(&sc->sc_task_u.workqueue, "radeonfb",
    240  1.2  riastrad 	    &radeon_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    241  1.1  riastrad 	if (error) {
    242  1.1  riastrad 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    243  1.1  riastrad 		    error);
    244  1.2  riastrad 		sc->sc_task_u.workqueue = NULL;
    245  1.3  riastrad 		goto out;
    246  1.1  riastrad 	}
    247  1.3  riastrad 
    248  1.3  riastrad out:	sc->sc_dev = self;
    249  1.1  riastrad }
    250  1.1  riastrad 
    251  1.1  riastrad static int
    252  1.1  riastrad radeon_detach(device_t self, int flags)
    253  1.1  riastrad {
    254  1.1  riastrad 	struct radeon_softc *const sc = device_private(self);
    255  1.1  riastrad 	int error;
    256  1.1  riastrad 
    257  1.3  riastrad 	if (sc->sc_dev == NULL)
    258  1.3  riastrad 		/* Not done attaching.  */
    259  1.3  riastrad 		return EBUSY;
    260  1.3  riastrad 
    261  1.1  riastrad 	/* XXX Check for in-use before tearing it all down...  */
    262  1.1  riastrad 	error = config_detach_children(self, flags);
    263  1.1  riastrad 	if (error)
    264  1.1  riastrad 		return error;
    265  1.1  riastrad 
    266  1.2  riastrad 	if (sc->sc_task_state == RADEON_TASK_ATTACH)
    267  1.9       mrg 		goto out;
    268  1.2  riastrad 	if (sc->sc_task_u.workqueue != NULL) {
    269  1.2  riastrad 		workqueue_destroy(sc->sc_task_u.workqueue);
    270  1.2  riastrad 		sc->sc_task_u.workqueue = NULL;
    271  1.2  riastrad 	}
    272  1.1  riastrad 
    273  1.1  riastrad 	if (sc->sc_drm_dev == NULL)
    274  1.9       mrg 		goto out;
    275  1.1  riastrad 	/* XXX errno Linux->NetBSD */
    276  1.1  riastrad 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    277  1.1  riastrad 	if (error)
    278  1.2  riastrad 		/* XXX Kinda too late to fail now...  */
    279  1.1  riastrad 		return error;
    280  1.2  riastrad 	sc->sc_drm_dev = NULL;
    281  1.1  riastrad 
    282  1.9       mrg out:	pmf_device_deregister(self);
    283  1.9       mrg 
    284  1.1  riastrad 	return 0;
    285  1.1  riastrad }
    286  1.1  riastrad 
    287  1.8       mrg static bool
    288  1.8       mrg radeon_do_suspend(device_t self, const pmf_qual_t *qual)
    289  1.8       mrg {
    290  1.8       mrg 	struct radeon_softc *const sc = device_private(self);
    291  1.8       mrg 	struct drm_device *const dev = sc->sc_drm_dev;
    292  1.8       mrg 	int ret;
    293  1.9       mrg 	bool is_console = true; /* XXX */
    294  1.8       mrg 
    295  1.8       mrg 	if (dev == NULL)
    296  1.8       mrg 		return true;
    297  1.8       mrg 
    298  1.9       mrg 	ret = radeon_suspend_kms(dev, true, is_console);
    299  1.8       mrg 	if (ret)
    300  1.8       mrg 		return false;
    301  1.8       mrg 
    302  1.8       mrg 	return true;
    303  1.8       mrg }
    304  1.8       mrg 
    305  1.8       mrg static bool
    306  1.8       mrg radeon_do_resume(device_t self, const pmf_qual_t *qual)
    307  1.8       mrg {
    308  1.8       mrg 	struct radeon_softc *const sc = device_private(self);
    309  1.8       mrg 	struct drm_device *const dev = sc->sc_drm_dev;
    310  1.8       mrg 	int ret;
    311  1.9       mrg 	bool is_console = true; /* XXX */
    312  1.8       mrg 
    313  1.8       mrg 	if (dev == NULL)
    314  1.8       mrg 		return true;
    315  1.8       mrg 
    316  1.9       mrg 	ret = radeon_resume_kms(dev, true, is_console);
    317  1.8       mrg 	if (ret)
    318  1.8       mrg 		return false;
    319  1.8       mrg 
    320  1.8       mrg 	return true;
    321  1.8       mrg }
    322  1.8       mrg 
    323  1.1  riastrad static void
    324  1.2  riastrad radeon_task_work(struct work *work, void *cookie __unused)
    325  1.1  riastrad {
    326  1.2  riastrad 	struct radeon_task *const task = container_of(work, struct radeon_task,
    327  1.2  riastrad 	    rt_u.work);
    328  1.1  riastrad 
    329  1.2  riastrad 	(*task->rt_fn)(task);
    330  1.1  riastrad }
    331  1.1  riastrad 
    332  1.2  riastrad int
    333  1.2  riastrad radeon_task_schedule(device_t self, struct radeon_task *task)
    334  1.1  riastrad {
    335  1.2  riastrad 	struct radeon_softc *const sc = device_private(self);
    336  1.1  riastrad 
    337  1.2  riastrad 	switch (sc->sc_task_state) {
    338  1.2  riastrad 	case RADEON_TASK_ATTACH:
    339  1.2  riastrad 		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, rt_u.queue);
    340  1.2  riastrad 		return 0;
    341  1.2  riastrad 	case RADEON_TASK_WORKQUEUE:
    342  1.2  riastrad 		if (sc->sc_task_u.workqueue == NULL) {
    343  1.2  riastrad 			aprint_error_dev(self, "unable to schedule task\n");
    344  1.2  riastrad 			return EIO;
    345  1.2  riastrad 		}
    346  1.2  riastrad 		workqueue_enqueue(sc->sc_task_u.workqueue, &task->rt_u.work,
    347  1.2  riastrad 		    NULL);
    348  1.1  riastrad 		return 0;
    349  1.1  riastrad 	default:
    350  1.2  riastrad 		panic("radeon in invalid task state: %d\n",
    351  1.2  riastrad 		    (int)sc->sc_task_state);
    352  1.1  riastrad 	}
    353  1.1  riastrad }
    354