Home | History | Annotate | Line # | Download | only in i915drm
intelfb.c revision 1.11
      1  1.11  riastrad /*	$NetBSD: intelfb.c,v 1.11 2015/03/05 17:50:41 riastradh 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.11  riastrad __KERNEL_RCSID(0, "$NetBSD: intelfb.c,v 1.11 2015/03/05 17:50:41 riastradh 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/bus.h>
     41   1.1  riastrad #include <sys/device.h>
     42   1.1  riastrad 
     43   1.1  riastrad #if NVGA > 0
     44   1.1  riastrad /*
     45   1.1  riastrad  * XXX All we really need is vga_is_console from vgavar.h, but the
     46   1.1  riastrad  * header files are missing their own dependencies, so we need to
     47   1.1  riastrad  * explicitly drag in the other crap.
     48   1.1  riastrad  */
     49   1.1  riastrad #include <dev/ic/mc6845reg.h>
     50   1.1  riastrad #include <dev/ic/pcdisplayvar.h>
     51   1.1  riastrad #include <dev/ic/vgareg.h>
     52   1.1  riastrad #include <dev/ic/vgavar.h>
     53   1.1  riastrad #endif
     54   1.1  riastrad 
     55   1.1  riastrad #include <drm/drmP.h>
     56  1.11  riastrad #include <drm/drmfb.h>
     57  1.11  riastrad #include <drm/drmfb_pci.h>
     58   1.1  riastrad 
     59   1.1  riastrad #include "i915_drv.h"
     60   1.1  riastrad #include "i915_pci.h"
     61   1.1  riastrad #include "intelfb.h"
     62   1.1  riastrad 
     63  1.11  riastrad static int	intelfb_match(device_t, cfdata_t, void *);
     64  1.11  riastrad static void	intelfb_attach(device_t, device_t, void *);
     65  1.11  riastrad static int	intelfb_detach(device_t, int);
     66  1.11  riastrad 
     67  1.11  riastrad static void	intelfb_attach_task(struct i915drmkms_task *);
     68  1.11  riastrad 
     69  1.11  riastrad static bool	intel_is_vga_console(struct drm_device *);
     70  1.11  riastrad static bool	intelfb_shutdown(device_t, int);
     71  1.11  riastrad 
     72  1.11  riastrad static paddr_t	intelfb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
     73  1.11  riastrad 
     74   1.1  riastrad struct intelfb_softc {
     75  1.11  riastrad 	struct drmfb_softc		sc_drmfb; /* XXX Must be first.  */
     76   1.1  riastrad 	device_t			sc_dev;
     77   1.1  riastrad 	struct intelfb_attach_args	sc_ifa;
     78   1.1  riastrad 	bus_space_handle_t		sc_fb_bsh;
     79  1.11  riastrad 	struct i915drmkms_task		sc_attach_task;
     80   1.1  riastrad 	bool				sc_mapped:1;
     81   1.1  riastrad 	bool				sc_scheduled:1;
     82   1.1  riastrad 	bool				sc_attached:1;
     83   1.1  riastrad };
     84   1.1  riastrad 
     85  1.11  riastrad static const struct drmfb_params intelfb_drmfb_params = {
     86  1.11  riastrad 	.dp_mmapfb = intelfb_drmfb_mmapfb,
     87  1.11  riastrad 	.dp_mmap = drmfb_pci_mmap,
     88  1.11  riastrad 	.dp_ioctl = drmfb_pci_ioctl,
     89  1.11  riastrad 	.dp_is_vga_console = intel_is_vga_console,
     90  1.11  riastrad 	.dp_disable_vga = i915_disable_vga,
     91   1.7  jmcneill };
     92   1.1  riastrad 
     93   1.1  riastrad CFATTACH_DECL_NEW(intelfb, sizeof(struct intelfb_softc),
     94   1.1  riastrad     intelfb_match, intelfb_attach, intelfb_detach, NULL);
     95   1.1  riastrad 
     96   1.1  riastrad static int
     97   1.1  riastrad intelfb_match(device_t parent, cfdata_t match, void *aux)
     98   1.1  riastrad {
     99   1.1  riastrad 
    100   1.1  riastrad 	return 1;
    101   1.1  riastrad }
    102   1.1  riastrad 
    103   1.1  riastrad static void
    104   1.1  riastrad intelfb_attach(device_t parent, device_t self, void *aux)
    105   1.1  riastrad {
    106   1.1  riastrad 	struct intelfb_softc *const sc = device_private(self);
    107   1.1  riastrad 	const struct intelfb_attach_args *const ifa = aux;
    108   1.1  riastrad 	int error;
    109   1.1  riastrad 
    110   1.1  riastrad 	sc->sc_dev = self;
    111   1.1  riastrad 	sc->sc_ifa = *ifa;
    112   1.1  riastrad 	sc->sc_mapped = false;
    113   1.1  riastrad 	sc->sc_scheduled = false;
    114   1.1  riastrad 	sc->sc_attached = false;
    115   1.1  riastrad 
    116   1.5  riastrad 	aprint_naive("\n");
    117   1.5  riastrad 	aprint_normal("\n");
    118   1.5  riastrad 
    119   1.1  riastrad 	/* XXX Defer this too?  */
    120   1.1  riastrad 	error = bus_space_map(ifa->ifa_fb_bst, ifa->ifa_fb_addr,
    121   1.1  riastrad 	    ifa->ifa_fb_size,
    122  1.11  riastrad 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE,
    123   1.1  riastrad 	    &sc->sc_fb_bsh);
    124   1.1  riastrad 	if (error) {
    125   1.1  riastrad 		aprint_error_dev(self, "unable to map framebuffer: %d\n",
    126   1.1  riastrad 		    error);
    127   1.1  riastrad 		goto fail0;
    128   1.1  riastrad 	}
    129   1.1  riastrad 	sc->sc_mapped = true;
    130   1.1  riastrad 
    131  1.11  riastrad 	i915drmkms_task_init(&sc->sc_attach_task, &intelfb_attach_task);
    132  1.11  riastrad 	error = i915drmkms_task_schedule(parent, &sc->sc_attach_task);
    133   1.1  riastrad 	if (error) {
    134   1.1  riastrad 		aprint_error_dev(self, "failed to schedule mode set: %d\n",
    135   1.1  riastrad 		    error);
    136   1.1  riastrad 		goto fail1;
    137   1.1  riastrad 	}
    138   1.1  riastrad 	sc->sc_scheduled = true;
    139   1.1  riastrad 
    140   1.1  riastrad 	/* Success!  */
    141   1.1  riastrad 	return;
    142   1.1  riastrad 
    143   1.1  riastrad fail1:	bus_space_unmap(ifa->ifa_fb_bst, sc->sc_fb_bsh, ifa->ifa_fb_size);
    144   1.1  riastrad 	sc->sc_mapped = false;
    145   1.1  riastrad fail0:	return;
    146   1.1  riastrad }
    147   1.1  riastrad 
    148   1.1  riastrad static int
    149   1.1  riastrad intelfb_detach(device_t self, int flags)
    150   1.1  riastrad {
    151   1.1  riastrad 	struct intelfb_softc *const sc = device_private(self);
    152  1.11  riastrad 	int error;
    153   1.1  riastrad 
    154   1.1  riastrad 	if (sc->sc_scheduled)
    155   1.1  riastrad 		return EBUSY;
    156   1.1  riastrad 
    157   1.1  riastrad 	if (sc->sc_attached) {
    158  1.11  riastrad 		pmf_device_deregister(self);
    159  1.11  riastrad 		error = drmfb_detach(&sc->sc_drmfb, flags);
    160  1.11  riastrad 		if (error) {
    161  1.11  riastrad 			/* XXX Ugh.  */
    162  1.11  riastrad 			(void)pmf_device_register1(self, NULL, NULL,
    163  1.11  riastrad 			    &intelfb_shutdown);
    164  1.11  riastrad 			return error;
    165  1.11  riastrad 		}
    166   1.1  riastrad 		sc->sc_attached = false;
    167   1.1  riastrad 	}
    168   1.1  riastrad 
    169   1.1  riastrad 	if (sc->sc_mapped) {
    170   1.1  riastrad 		bus_space_unmap(sc->sc_ifa.ifa_fb_bst, sc->sc_fb_bsh,
    171   1.1  riastrad 		    sc->sc_ifa.ifa_fb_size);
    172   1.1  riastrad 		sc->sc_mapped = false;
    173   1.1  riastrad 	}
    174   1.1  riastrad 
    175   1.1  riastrad 	return 0;
    176   1.1  riastrad }
    177   1.1  riastrad 
    178   1.1  riastrad static void
    179  1.11  riastrad intelfb_attach_task(struct i915drmkms_task *task)
    180   1.1  riastrad {
    181   1.1  riastrad 	struct intelfb_softc *const sc = container_of(task,
    182  1.11  riastrad 	    struct intelfb_softc, sc_attach_task);
    183   1.1  riastrad 	const struct intelfb_attach_args *const ifa = &sc->sc_ifa;
    184  1.11  riastrad 	const struct drmfb_attach_args da = {
    185  1.11  riastrad 		.da_dev = sc->sc_dev,
    186  1.11  riastrad 		.da_fb_helper = ifa->ifa_fb_helper,
    187  1.11  riastrad 		.da_fb_sizes = &ifa->ifa_fb_sizes,
    188  1.11  riastrad 		.da_fb_vaddr = bus_space_vaddr(ifa->ifa_fb_bst, sc->sc_fb_bsh),
    189  1.11  riastrad 		.da_params = &intelfb_drmfb_params,
    190  1.11  riastrad 	};
    191   1.1  riastrad 	int error;
    192   1.1  riastrad 
    193  1.11  riastrad 	error = drmfb_attach(&sc->sc_drmfb, &da);
    194   1.1  riastrad 	if (error) {
    195  1.11  riastrad 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
    196   1.1  riastrad 		    error);
    197  1.11  riastrad 		return;
    198   1.1  riastrad 	}
    199   1.1  riastrad 
    200  1.11  riastrad 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, &intelfb_shutdown))
    201  1.11  riastrad 		aprint_error_dev(sc->sc_dev,
    202  1.11  riastrad 		    "failed to register shutdown handler\n");
    203   1.8  jmcneill 
    204  1.11  riastrad 	sc->sc_attached = true;
    205   1.1  riastrad }
    206   1.1  riastrad 
    207  1.11  riastrad static bool
    208  1.11  riastrad intel_is_vga_console(struct drm_device *dev)
    209   1.1  riastrad {
    210   1.1  riastrad 
    211  1.11  riastrad #if NVGA > 0
    212  1.11  riastrad 	return vga_is_console(dev->pdev->pd_pa.pa_iot, -1);
    213  1.11  riastrad #else
    214  1.11  riastrad 	return false;
    215  1.11  riastrad #endif
    216   1.1  riastrad }
    217   1.1  riastrad 
    218  1.11  riastrad static bool
    219  1.11  riastrad intelfb_shutdown(device_t self, int flags)
    220   1.1  riastrad {
    221  1.11  riastrad 	struct intelfb_softc *const sc = device_private(self);
    222   1.1  riastrad 
    223  1.11  riastrad 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    224   1.1  riastrad }
    225   1.1  riastrad 
    226   1.1  riastrad static paddr_t
    227  1.11  riastrad intelfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
    228   1.1  riastrad {
    229  1.11  riastrad 	struct intelfb_softc *const sc = container_of(drmfb,
    230  1.11  riastrad 	    struct intelfb_softc, sc_drmfb);
    231   1.1  riastrad 	struct drm_fb_helper *const helper = sc->sc_ifa.ifa_fb_helper;
    232   1.1  riastrad 	struct intel_fbdev *const fbdev = container_of(helper,
    233   1.1  riastrad 	    struct intel_fbdev, helper);
    234   1.1  riastrad 	struct drm_device *const dev = helper->dev;
    235   1.1  riastrad 	struct drm_i915_private *const dev_priv = dev->dev_private;
    236   1.1  riastrad 
    237  1.11  riastrad 	KASSERT(0 <= offset);
    238  1.11  riastrad 	KASSERT(offset < fbdev->fb->obj->base.size);
    239   1.7  jmcneill 
    240  1.11  riastrad 	return bus_space_mmap(dev->bst, dev_priv->gtt.mappable_base,
    241  1.11  riastrad 	    i915_gem_obj_ggtt_offset(fbdev->fb->obj) + offset,
    242  1.11  riastrad 	    prot, BUS_SPACE_MAP_PREFETCHABLE);
    243   1.7  jmcneill }
    244