Home | History | Annotate | Line # | Download | only in vmwgfx
vmwgfxfb.c revision 1.1
      1 /*	$NetBSD: vmwgfxfb.c,v 1.1 2022/02/17 01:21:03 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2022 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: vmwgfxfb.c,v 1.1 2022/02/17 01:21:03 riastradh Exp $");
     32 
     33 #include <sys/types.h>
     34 #include <sys/device.h>
     35 
     36 #include <drm/drm_fb_helper.h>
     37 #include <drm/drmfb.h>
     38 #include <drm/drmfb_pci.h>
     39 
     40 #include "vmwgfx_drv.h"
     41 #include "vmwgfx_task.h"
     42 #include "vmwgfxfb.h"
     43 
     44 struct vmwgfxfb_softc {
     45 	struct drmfb_softc		sc_drmfb; /* XXX Must be first.  */
     46 	device_t			sc_dev;
     47 	struct vmwgfxfb_attach_args	sc_vfa;
     48 	struct vmwgfx_task		sc_attach_task;
     49 	bool				sc_scheduled:1;
     50 	bool				sc_attached:1;
     51 };
     52 
     53 static int	vmwgfxfb_match(device_t, cfdata_t, void *);
     54 static void	vmwgfxfb_attach(device_t, device_t, void *);
     55 static int	vmwgfxfb_detach(device_t, int);
     56 
     57 static void	vmwgfxfb_attach_task(struct vmwgfx_task *);
     58 
     59 static paddr_t	vmwgfxfb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
     60 static bool	vmwgfxfb_shutdown(device_t, int);
     61 
     62 CFATTACH_DECL_NEW(vmwgfxfb, sizeof(struct vmwgfxfb_softc),
     63     vmwgfxfb_match, vmwgfxfb_attach, vmwgfxfb_detach, NULL);
     64 
     65 static const struct drmfb_params vmwgfxfb_drmfb_params = {
     66 	.dp_mmapfb = vmwgfxfb_drmfb_mmapfb,
     67 	.dp_mmap = drmfb_pci_mmap,
     68 	.dp_ioctl = drmfb_pci_ioctl,
     69 	.dp_is_vga_console = drmfb_pci_is_vga_console,
     70 };
     71 
     72 static int
     73 vmwgfxfb_match(device_t parent, cfdata_t match, void *aux)
     74 {
     75 
     76 	return 1;
     77 }
     78 
     79 static void
     80 vmwgfxfb_attach(device_t parent, device_t self, void *aux)
     81 {
     82 	struct vmwgfxfb_softc *const sc = device_private(self);
     83 	const struct vmwgfxfb_attach_args *const vfa = aux;
     84 	int error;
     85 
     86 	sc->sc_dev = self;
     87 	sc->sc_vfa = *vfa;
     88 	sc->sc_scheduled = false;
     89 	sc->sc_attached = false;
     90 
     91 	aprint_naive("\n");
     92 	aprint_normal("\n");
     93 
     94 	vmwgfx_task_init(&sc->sc_attach_task, &vmwgfxfb_attach_task);
     95 	error = vmwgfx_task_schedule(parent, &sc->sc_attach_task);
     96 	if (error) {
     97 		aprint_error_dev(self, "failed to schedule mode set: %d\n",
     98 		    error);
     99 		goto fail0;
    100 	}
    101 	sc->sc_scheduled = true;
    102 
    103 	/* Success!  */
    104 	return;
    105 
    106 fail0:	return;
    107 }
    108 
    109 static int
    110 vmwgfxfb_detach(device_t self, int flags)
    111 {
    112 	struct vmwgfxfb_softc *const sc = device_private(self);
    113 	int error;
    114 
    115 	if (sc->sc_scheduled)
    116 		return EBUSY;
    117 
    118 	if (sc->sc_attached) {
    119 		pmf_device_deregister(self);
    120 		error = drmfb_detach(&sc->sc_drmfb, flags);
    121 		if (error) {
    122 			/* XXX Ugh.  */
    123 			(void)pmf_device_register1(self, NULL, NULL,
    124 			    &vmwgfxfb_shutdown);
    125 			return error;
    126 		}
    127 		sc->sc_attached = false;
    128 	}
    129 
    130 	return 0;
    131 }
    132 
    133 static void
    134 vmwgfxfb_attach_task(struct vmwgfx_task *task)
    135 {
    136 	struct vmwgfxfb_softc *const sc = container_of(task,
    137 	    struct vmwgfxfb_softc, sc_attach_task);
    138 	const struct vmwgfxfb_attach_args *const vfa = &sc->sc_vfa;
    139 	const struct drmfb_attach_args da = {
    140 		.da_dev = sc->sc_dev,
    141 		.da_fb_helper = vfa->vfa_fb_helper,
    142 		.da_fb_sizes = &vfa->vfa_fb_sizes,
    143 		.da_fb_vaddr = __UNVOLATILE(vfa->vfa_fb_ptr),
    144 		.da_fb_linebytes = vfa->vfa_fb_linebytes,
    145 		.da_params = &vmwgfxfb_drmfb_params,
    146 	};
    147 	device_t parent = device_parent(sc->sc_dev);
    148 	bool is_console;
    149 	int error;
    150 
    151 	KASSERT(sc->sc_scheduled);
    152 
    153 	/*
    154 	 * MD device enumeration logic may choose the vmwgfxN PCI
    155 	 * device as the console.  If so, propagate that down to the
    156 	 * vmwgfxfbN device for genfb.
    157 	 */
    158 	if (prop_dictionary_get_bool(device_properties(parent),
    159 		"is_console", &is_console) &&
    160 	    !prop_dictionary_set_bool(device_properties(sc->sc_dev),
    161 		"is_console", is_console)) {
    162 		aprint_error_dev(sc->sc_dev, "failed to set is_console\n");
    163 	}
    164 
    165 	error = drmfb_attach(&sc->sc_drmfb, &da);
    166 	if (error) {
    167 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
    168 		    error);
    169 		return;
    170 	}
    171 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &vmwgfxfb_shutdown))
    172 		aprint_error_dev(sc->sc_dev,
    173 		    "failed to register shutdown handler\n");
    174 
    175 	sc->sc_attached = true;
    176 }
    177 
    178 static bool
    179 vmwgfxfb_shutdown(device_t self, int flags)
    180 {
    181 	struct vmwgfxfb_softc *const sc = device_private(self);
    182 
    183 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    184 }
    185 
    186 static paddr_t
    187 vmwgfxfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
    188 {
    189 	struct vmwgfxfb_softc *const sc = container_of(drmfb,
    190 	    struct vmwgfxfb_softc, sc_drmfb);
    191 	struct drm_fb_helper *const helper = sc->sc_vfa.vfa_fb_helper;
    192 	struct drm_framebuffer *const fb = helper->fb;
    193 	struct vmw_buffer_object *const vbo = /*XXX MAGIC HERE*/;
    194 	int flags = 0;
    195 
    196 	if (offset < 0)
    197 		return -1;
    198 
    199 	const unsigned num_pages __diagused = vbo->base.num_pages;
    200 
    201 	KASSERT(offset < (num_pages << PAGE_SHIFT));
    202 	KASSERT(vbo->base.mem.bus.is_iomem);
    203 
    204 	if (ISSET(vbo->base.mem.placement, TTM_PL_FLAG_WC))
    205 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
    206 
    207 	return bus_space_mmap(vbo->base.bdev->memt,
    208 	    vbo->base.mem.bus.base, vbo->base.mem.bus.offset + offset,
    209 	    prot, flags);
    210 }
    211