Home | History | Annotate | Line # | Download | only in i915drm
intelfb.c revision 1.21
      1  1.21  riastrad /*	$NetBSD: intelfb.c,v 1.21 2021/12/19 11:38:46 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.21  riastrad __KERNEL_RCSID(0, "$NetBSD: intelfb.c,v 1.21 2021/12/19 11:38:46 riastradh Exp $");
     34   1.1  riastrad 
     35   1.1  riastrad #include <sys/types.h>
     36   1.1  riastrad #include <sys/bus.h>
     37   1.1  riastrad #include <sys/device.h>
     38   1.1  riastrad 
     39  1.19  riastrad #include <drm/drm_device.h>
     40  1.11  riastrad #include <drm/drmfb.h>
     41  1.11  riastrad #include <drm/drmfb_pci.h>
     42   1.1  riastrad 
     43   1.1  riastrad #include "i915_drv.h"
     44   1.1  riastrad #include "i915_pci.h"
     45   1.1  riastrad #include "intelfb.h"
     46   1.1  riastrad 
     47  1.11  riastrad static int	intelfb_match(device_t, cfdata_t, void *);
     48  1.11  riastrad static void	intelfb_attach(device_t, device_t, void *);
     49  1.11  riastrad static int	intelfb_detach(device_t, int);
     50  1.11  riastrad 
     51  1.11  riastrad static void	intelfb_attach_task(struct i915drmkms_task *);
     52  1.11  riastrad 
     53  1.11  riastrad static bool	intelfb_shutdown(device_t, int);
     54  1.11  riastrad 
     55  1.11  riastrad static paddr_t	intelfb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
     56  1.11  riastrad 
     57   1.1  riastrad struct intelfb_softc {
     58  1.11  riastrad 	struct drmfb_softc		sc_drmfb; /* XXX Must be first.  */
     59   1.1  riastrad 	device_t			sc_dev;
     60   1.1  riastrad 	struct intelfb_attach_args	sc_ifa;
     61   1.1  riastrad 	bus_space_handle_t		sc_fb_bsh;
     62  1.11  riastrad 	struct i915drmkms_task		sc_attach_task;
     63   1.1  riastrad 	bool				sc_scheduled:1;
     64   1.1  riastrad 	bool				sc_attached:1;
     65   1.1  riastrad };
     66   1.1  riastrad 
     67  1.11  riastrad static const struct drmfb_params intelfb_drmfb_params = {
     68  1.11  riastrad 	.dp_mmapfb = intelfb_drmfb_mmapfb,
     69  1.11  riastrad 	.dp_mmap = drmfb_pci_mmap,
     70  1.11  riastrad 	.dp_ioctl = drmfb_pci_ioctl,
     71  1.12  riastrad 	.dp_is_vga_console = drmfb_pci_is_vga_console,
     72  1.11  riastrad 	.dp_disable_vga = i915_disable_vga,
     73   1.7  jmcneill };
     74   1.1  riastrad 
     75   1.1  riastrad CFATTACH_DECL_NEW(intelfb, sizeof(struct intelfb_softc),
     76   1.1  riastrad     intelfb_match, intelfb_attach, intelfb_detach, NULL);
     77   1.1  riastrad 
     78   1.1  riastrad static int
     79   1.1  riastrad intelfb_match(device_t parent, cfdata_t match, void *aux)
     80   1.1  riastrad {
     81   1.1  riastrad 
     82   1.1  riastrad 	return 1;
     83   1.1  riastrad }
     84   1.1  riastrad 
     85   1.1  riastrad static void
     86   1.1  riastrad intelfb_attach(device_t parent, device_t self, void *aux)
     87   1.1  riastrad {
     88   1.1  riastrad 	struct intelfb_softc *const sc = device_private(self);
     89   1.1  riastrad 	const struct intelfb_attach_args *const ifa = aux;
     90   1.1  riastrad 	int error;
     91   1.1  riastrad 
     92   1.1  riastrad 	sc->sc_dev = self;
     93   1.1  riastrad 	sc->sc_ifa = *ifa;
     94   1.1  riastrad 	sc->sc_scheduled = false;
     95   1.1  riastrad 	sc->sc_attached = false;
     96   1.1  riastrad 
     97   1.5  riastrad 	aprint_naive("\n");
     98   1.5  riastrad 	aprint_normal("\n");
     99   1.5  riastrad 
    100  1.11  riastrad 	i915drmkms_task_init(&sc->sc_attach_task, &intelfb_attach_task);
    101  1.11  riastrad 	error = i915drmkms_task_schedule(parent, &sc->sc_attach_task);
    102   1.1  riastrad 	if (error) {
    103   1.1  riastrad 		aprint_error_dev(self, "failed to schedule mode set: %d\n",
    104   1.1  riastrad 		    error);
    105   1.1  riastrad 		goto fail1;
    106   1.1  riastrad 	}
    107  1.18  riastrad 	config_pending_incr(self);
    108   1.1  riastrad 	sc->sc_scheduled = true;
    109   1.1  riastrad 
    110   1.1  riastrad 	/* Success!  */
    111   1.1  riastrad 	return;
    112   1.1  riastrad 
    113   1.1  riastrad fail0:	return;
    114   1.1  riastrad }
    115   1.1  riastrad 
    116   1.1  riastrad static int
    117   1.1  riastrad intelfb_detach(device_t self, int flags)
    118   1.1  riastrad {
    119   1.1  riastrad 	struct intelfb_softc *const sc = device_private(self);
    120  1.11  riastrad 	int error;
    121   1.1  riastrad 
    122   1.1  riastrad 	if (sc->sc_scheduled)
    123   1.1  riastrad 		return EBUSY;
    124   1.1  riastrad 
    125   1.1  riastrad 	if (sc->sc_attached) {
    126  1.11  riastrad 		pmf_device_deregister(self);
    127  1.11  riastrad 		error = drmfb_detach(&sc->sc_drmfb, flags);
    128  1.11  riastrad 		if (error) {
    129  1.11  riastrad 			/* XXX Ugh.  */
    130  1.11  riastrad 			(void)pmf_device_register1(self, NULL, NULL,
    131  1.11  riastrad 			    &intelfb_shutdown);
    132  1.11  riastrad 			return error;
    133  1.11  riastrad 		}
    134   1.1  riastrad 		sc->sc_attached = false;
    135   1.1  riastrad 	}
    136   1.1  riastrad 
    137   1.1  riastrad 	return 0;
    138   1.1  riastrad }
    139   1.1  riastrad 
    140   1.1  riastrad static void
    141  1.11  riastrad intelfb_attach_task(struct i915drmkms_task *task)
    142   1.1  riastrad {
    143   1.1  riastrad 	struct intelfb_softc *const sc = container_of(task,
    144  1.11  riastrad 	    struct intelfb_softc, sc_attach_task);
    145   1.1  riastrad 	const struct intelfb_attach_args *const ifa = &sc->sc_ifa;
    146  1.11  riastrad 	const struct drmfb_attach_args da = {
    147  1.11  riastrad 		.da_dev = sc->sc_dev,
    148  1.11  riastrad 		.da_fb_helper = ifa->ifa_fb_helper,
    149  1.11  riastrad 		.da_fb_sizes = &ifa->ifa_fb_sizes,
    150  1.21  riastrad 		.da_fb_vaddr = ifa->ifa_fb_vaddr,
    151  1.17       rin 		.da_fb_linebytes = ifa->ifa_fb_helper->fb->pitches[0],
    152  1.11  riastrad 		.da_params = &intelfb_drmfb_params,
    153  1.11  riastrad 	};
    154   1.1  riastrad 	int error;
    155   1.1  riastrad 
    156  1.11  riastrad 	error = drmfb_attach(&sc->sc_drmfb, &da);
    157   1.1  riastrad 	if (error) {
    158  1.11  riastrad 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
    159   1.1  riastrad 		    error);
    160  1.16   msaitoh 		goto out;
    161   1.1  riastrad 	}
    162   1.1  riastrad 
    163  1.13  jmcneill 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &intelfb_shutdown))
    164  1.11  riastrad 		aprint_error_dev(sc->sc_dev,
    165  1.11  riastrad 		    "failed to register shutdown handler\n");
    166   1.8  jmcneill 
    167  1.11  riastrad 	sc->sc_attached = true;
    168  1.16   msaitoh out:
    169  1.18  riastrad 	config_pending_decr(sc->sc_dev);
    170   1.1  riastrad }
    171   1.1  riastrad 
    172  1.11  riastrad static bool
    173  1.11  riastrad intelfb_shutdown(device_t self, int flags)
    174   1.1  riastrad {
    175  1.11  riastrad 	struct intelfb_softc *const sc = device_private(self);
    176   1.1  riastrad 
    177  1.11  riastrad 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    178   1.1  riastrad }
    179   1.1  riastrad 
    180   1.1  riastrad static paddr_t
    181  1.11  riastrad intelfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
    182   1.1  riastrad {
    183  1.11  riastrad 	struct intelfb_softc *const sc = container_of(drmfb,
    184  1.11  riastrad 	    struct intelfb_softc, sc_drmfb);
    185   1.1  riastrad 	struct drm_fb_helper *const helper = sc->sc_ifa.ifa_fb_helper;
    186   1.1  riastrad 	struct intel_fbdev *const fbdev = container_of(helper,
    187   1.1  riastrad 	    struct intel_fbdev, helper);
    188   1.1  riastrad 	struct drm_device *const dev = helper->dev;
    189   1.1  riastrad 	struct drm_i915_private *const dev_priv = dev->dev_private;
    190   1.1  riastrad 
    191  1.11  riastrad 	KASSERT(0 <= offset);
    192  1.11  riastrad 	KASSERT(offset < fbdev->fb->obj->base.size);
    193   1.7  jmcneill 
    194  1.11  riastrad 	return bus_space_mmap(dev->bst, dev_priv->gtt.mappable_base,
    195  1.11  riastrad 	    i915_gem_obj_ggtt_offset(fbdev->fb->obj) + offset,
    196  1.11  riastrad 	    prot, BUS_SPACE_MAP_PREFETCHABLE);
    197   1.7  jmcneill }
    198