Home | History | Annotate | Line # | Download | only in radeon
radeon_pci.c revision 1.5
      1  1.5       mrg /*	$NetBSD: radeon_pci.c,v 1.5 2015/02/14 06:58:12 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.5       mrg __KERNEL_RCSID(0, "$NetBSD: radeon_pci.c,v 1.5 2015/02/14 06:58:12 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.5       mrg #ifdef __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.1  riastrad 
    109  1.2  riastrad static void	radeon_task_work(struct work *, void *);
    110  1.1  riastrad 
    111  1.4  riastrad CFATTACH_DECL_NEW(radeon, sizeof(struct radeon_softc),
    112  1.1  riastrad     radeon_match, radeon_attach, radeon_detach, NULL);
    113  1.1  riastrad 
    114  1.1  riastrad /* XXX Kludge to get these from radeon_drv.c.  */
    115  1.1  riastrad extern struct drm_driver *const radeon_drm_driver;
    116  1.1  riastrad extern const struct pci_device_id *const radeon_device_ids;
    117  1.1  riastrad extern const size_t radeon_n_device_ids;
    118  1.1  riastrad 
    119  1.1  riastrad static bool
    120  1.1  riastrad radeon_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
    121  1.1  riastrad {
    122  1.1  riastrad 	size_t i;
    123  1.1  riastrad 
    124  1.1  riastrad 	for (i = 0; i < radeon_n_device_ids; i++) {
    125  1.1  riastrad 		if ((PCI_VENDOR(pa->pa_id) == radeon_device_ids[i].vendor) &&
    126  1.1  riastrad 		    (PCI_PRODUCT(pa->pa_id) == radeon_device_ids[i].device))
    127  1.1  riastrad 			break;
    128  1.1  riastrad 	}
    129  1.1  riastrad 
    130  1.1  riastrad 	/* Did we find it?  */
    131  1.1  riastrad 	if (i == radeon_n_device_ids)
    132  1.1  riastrad 		return false;
    133  1.1  riastrad 
    134  1.1  riastrad 	if (flags)
    135  1.1  riastrad 		*flags = radeon_device_ids[i].driver_data;
    136  1.1  riastrad 	return true;
    137  1.1  riastrad }
    138  1.1  riastrad 
    139  1.1  riastrad static int
    140  1.1  riastrad radeon_match(device_t parent, cfdata_t match, void *aux)
    141  1.1  riastrad {
    142  1.1  riastrad 	extern int radeon_guarantee_initialized(void);
    143  1.1  riastrad 	const struct pci_attach_args *const pa = aux;
    144  1.1  riastrad 	int error;
    145  1.1  riastrad 
    146  1.1  riastrad 	error = radeon_guarantee_initialized();
    147  1.1  riastrad 	if (error) {
    148  1.1  riastrad 		aprint_error("radeon: failed to initialize: %d\n", error);
    149  1.1  riastrad 		return 0;
    150  1.1  riastrad 	}
    151  1.1  riastrad 
    152  1.1  riastrad 	if (!radeon_pci_lookup(pa, NULL))
    153  1.1  riastrad 		return 0;
    154  1.1  riastrad 
    155  1.1  riastrad 	return 6;		/* XXX Beat genfb_pci...  */
    156  1.1  riastrad }
    157  1.1  riastrad 
    158  1.1  riastrad static void
    159  1.1  riastrad radeon_attach(device_t parent, device_t self, void *aux)
    160  1.1  riastrad {
    161  1.1  riastrad 	struct radeon_softc *const sc = device_private(self);
    162  1.1  riastrad 	const struct pci_attach_args *const pa = aux;
    163  1.3  riastrad 
    164  1.3  riastrad 	pci_aprint_devinfo(pa, NULL);
    165  1.3  riastrad 
    166  1.3  riastrad 	/*
    167  1.3  riastrad 	 * Trivial initialization first; the rest will come after we
    168  1.3  riastrad 	 * have mounted the root file system and can load firmware
    169  1.3  riastrad 	 * images.
    170  1.3  riastrad 	 */
    171  1.3  riastrad 	sc->sc_dev = NULL;
    172  1.3  riastrad 	sc->sc_pa = *pa;
    173  1.3  riastrad 
    174  1.5       mrg #ifdef RADEON_PCI_UGLY_MAP_HACK
    175  1.5       mrg 	/*
    176  1.5       mrg 	 * XXX
    177  1.5       mrg 	 * We map the VGA registers, so that other driver don't
    178  1.5       mrg 	 * think they can.  This stops vga@isa or pcdisplay@isa
    179  1.5       mrg 	 * attaching, and stealing wsdisplay0.  Yuck.
    180  1.5       mrg 	 */
    181  1.5       mrg 	sc->sc_temp_set = bus_space_map(pa->pa_memt, 0xb0000, 0x10000, 0,
    182  1.5       mrg 					&sc->sc_temp_memh);
    183  1.5       mrg #endif
    184  1.5       mrg 
    185  1.3  riastrad 	config_mountroot(self, &radeon_attach_real);
    186  1.3  riastrad }
    187  1.3  riastrad 
    188  1.3  riastrad static void
    189  1.3  riastrad radeon_attach_real(device_t self)
    190  1.3  riastrad {
    191  1.3  riastrad 	struct radeon_softc *const sc = device_private(self);
    192  1.3  riastrad 	const struct pci_attach_args *const pa = &sc->sc_pa;
    193  1.1  riastrad 	bool ok __diagused;
    194  1.1  riastrad 	unsigned long flags;
    195  1.1  riastrad 	int error;
    196  1.1  riastrad 
    197  1.1  riastrad 	ok = radeon_pci_lookup(pa, &flags);
    198  1.1  riastrad 	KASSERT(ok);
    199  1.1  riastrad 
    200  1.5       mrg #ifdef RADEON_PCI_UGLY_MAP_HACK
    201  1.5       mrg 	/*
    202  1.5       mrg 	 * XXX
    203  1.5       mrg 	 * Unmap the VGA registers so the DRM code can map them.
    204  1.5       mrg 	 */
    205  1.5       mrg 	if (sc->sc_temp_set)
    206  1.5       mrg 		bus_space_unmap(pa->pa_memt, sc->sc_temp_memh, 0x10000);
    207  1.5       mrg #endif
    208  1.5       mrg 
    209  1.2  riastrad 	sc->sc_task_state = RADEON_TASK_ATTACH;
    210  1.2  riastrad 	SIMPLEQ_INIT(&sc->sc_task_u.attach);
    211  1.1  riastrad 
    212  1.1  riastrad 	/* XXX errno Linux->NetBSD */
    213  1.1  riastrad 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, radeon_drm_driver,
    214  1.1  riastrad 	    flags, &sc->sc_drm_dev);
    215  1.1  riastrad 	if (error) {
    216  1.1  riastrad 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    217  1.3  riastrad 		goto out;
    218  1.1  riastrad 	}
    219  1.1  riastrad 
    220  1.2  riastrad 	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
    221  1.2  riastrad 		struct radeon_task *const task =
    222  1.2  riastrad 		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
    223  1.1  riastrad 
    224  1.2  riastrad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, rt_u.queue);
    225  1.2  riastrad 		(*task->rt_fn)(task);
    226  1.1  riastrad 	}
    227  1.1  riastrad 
    228  1.2  riastrad 	sc->sc_task_state = RADEON_TASK_WORKQUEUE;
    229  1.2  riastrad 	error = workqueue_create(&sc->sc_task_u.workqueue, "radeonfb",
    230  1.2  riastrad 	    &radeon_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
    231  1.1  riastrad 	if (error) {
    232  1.1  riastrad 		aprint_error_dev(self, "unable to create workqueue: %d\n",
    233  1.1  riastrad 		    error);
    234  1.2  riastrad 		sc->sc_task_u.workqueue = NULL;
    235  1.3  riastrad 		goto out;
    236  1.1  riastrad 	}
    237  1.3  riastrad 
    238  1.3  riastrad out:	sc->sc_dev = self;
    239  1.1  riastrad }
    240  1.1  riastrad 
    241  1.1  riastrad static int
    242  1.1  riastrad radeon_detach(device_t self, int flags)
    243  1.1  riastrad {
    244  1.1  riastrad 	struct radeon_softc *const sc = device_private(self);
    245  1.1  riastrad 	int error;
    246  1.1  riastrad 
    247  1.3  riastrad 	if (sc->sc_dev == NULL)
    248  1.3  riastrad 		/* Not done attaching.  */
    249  1.3  riastrad 		return EBUSY;
    250  1.3  riastrad 
    251  1.1  riastrad 	/* XXX Check for in-use before tearing it all down...  */
    252  1.1  riastrad 	error = config_detach_children(self, flags);
    253  1.1  riastrad 	if (error)
    254  1.1  riastrad 		return error;
    255  1.1  riastrad 
    256  1.2  riastrad 	if (sc->sc_task_state == RADEON_TASK_ATTACH)
    257  1.1  riastrad 		return 0;
    258  1.2  riastrad 	if (sc->sc_task_u.workqueue != NULL) {
    259  1.2  riastrad 		workqueue_destroy(sc->sc_task_u.workqueue);
    260  1.2  riastrad 		sc->sc_task_u.workqueue = NULL;
    261  1.2  riastrad 	}
    262  1.1  riastrad 
    263  1.1  riastrad 	if (sc->sc_drm_dev == NULL)
    264  1.1  riastrad 		return 0;
    265  1.1  riastrad 	/* XXX errno Linux->NetBSD */
    266  1.1  riastrad 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    267  1.1  riastrad 	if (error)
    268  1.2  riastrad 		/* XXX Kinda too late to fail now...  */
    269  1.1  riastrad 		return error;
    270  1.2  riastrad 	sc->sc_drm_dev = NULL;
    271  1.1  riastrad 
    272  1.1  riastrad 	return 0;
    273  1.1  riastrad }
    274  1.1  riastrad 
    275  1.1  riastrad static void
    276  1.2  riastrad radeon_task_work(struct work *work, void *cookie __unused)
    277  1.1  riastrad {
    278  1.2  riastrad 	struct radeon_task *const task = container_of(work, struct radeon_task,
    279  1.2  riastrad 	    rt_u.work);
    280  1.1  riastrad 
    281  1.2  riastrad 	(*task->rt_fn)(task);
    282  1.1  riastrad }
    283  1.1  riastrad 
    284  1.2  riastrad int
    285  1.2  riastrad radeon_task_schedule(device_t self, struct radeon_task *task)
    286  1.1  riastrad {
    287  1.2  riastrad 	struct radeon_softc *const sc = device_private(self);
    288  1.1  riastrad 
    289  1.2  riastrad 	switch (sc->sc_task_state) {
    290  1.2  riastrad 	case RADEON_TASK_ATTACH:
    291  1.2  riastrad 		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, rt_u.queue);
    292  1.2  riastrad 		return 0;
    293  1.2  riastrad 	case RADEON_TASK_WORKQUEUE:
    294  1.2  riastrad 		if (sc->sc_task_u.workqueue == NULL) {
    295  1.2  riastrad 			aprint_error_dev(self, "unable to schedule task\n");
    296  1.2  riastrad 			return EIO;
    297  1.2  riastrad 		}
    298  1.2  riastrad 		workqueue_enqueue(sc->sc_task_u.workqueue, &task->rt_u.work,
    299  1.2  riastrad 		    NULL);
    300  1.1  riastrad 		return 0;
    301  1.1  riastrad 	default:
    302  1.2  riastrad 		panic("radeon in invalid task state: %d\n",
    303  1.2  riastrad 		    (int)sc->sc_task_state);
    304  1.1  riastrad 	}
    305  1.1  riastrad }
    306