1 /* $NetBSD: vmwgfxfb.c,v 1.4 2022/09/25 08:21:02 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.4 2022/09/25 08:21:02 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_attached:1; 50 }; 51 52 static int vmwgfxfb_match(device_t, cfdata_t, void *); 53 static void vmwgfxfb_attach(device_t, device_t, void *); 54 static int vmwgfxfb_detach(device_t, int); 55 56 static void vmwgfxfb_attach_task(struct vmwgfx_task *); 57 58 static paddr_t vmwgfxfb_drmfb_mmapfb(struct drmfb_softc *, off_t, int); 59 static bool vmwgfxfb_shutdown(device_t, int); 60 61 CFATTACH_DECL_NEW(vmwgfxfb, sizeof(struct vmwgfxfb_softc), 62 vmwgfxfb_match, vmwgfxfb_attach, vmwgfxfb_detach, NULL); 63 64 static const struct drmfb_params vmwgfxfb_drmfb_params = { 65 .dp_mmapfb = vmwgfxfb_drmfb_mmapfb, 66 .dp_mmap = drmfb_pci_mmap, 67 .dp_ioctl = drmfb_pci_ioctl, 68 .dp_is_vga_console = drmfb_pci_is_vga_console, 69 }; 70 71 static int 72 vmwgfxfb_match(device_t parent, cfdata_t match, void *aux) 73 { 74 75 return 1; 76 } 77 78 static void 79 vmwgfxfb_attach(device_t parent, device_t self, void *aux) 80 { 81 struct vmwgfxfb_softc *const sc = device_private(self); 82 const struct vmwgfxfb_attach_args *const vfa = aux; 83 84 sc->sc_dev = self; 85 sc->sc_vfa = *vfa; 86 sc->sc_attached = false; 87 88 aprint_naive("\n"); 89 aprint_normal("\n"); 90 91 vmwgfx_task_init(&sc->sc_attach_task, &vmwgfxfb_attach_task); 92 vmwgfx_task_schedule(parent, &sc->sc_attach_task); 93 config_pending_incr(self); 94 } 95 96 static int 97 vmwgfxfb_detach(device_t self, int flags) 98 { 99 struct vmwgfxfb_softc *const sc = device_private(self); 100 int error; 101 102 if (sc->sc_attached) { 103 pmf_device_deregister(self); 104 error = drmfb_detach(&sc->sc_drmfb, flags); 105 if (error) { 106 /* XXX Ugh. */ 107 (void)pmf_device_register1(self, NULL, NULL, 108 &vmwgfxfb_shutdown); 109 return error; 110 } 111 sc->sc_attached = false; 112 } 113 114 return 0; 115 } 116 117 static void 118 vmwgfxfb_attach_task(struct vmwgfx_task *task) 119 { 120 struct vmwgfxfb_softc *const sc = container_of(task, 121 struct vmwgfxfb_softc, sc_attach_task); 122 const struct vmwgfxfb_attach_args *const vfa = &sc->sc_vfa; 123 const struct drmfb_attach_args da = { 124 .da_dev = sc->sc_dev, 125 .da_fb_helper = vfa->vfa_fb_helper, 126 .da_fb_sizes = &vfa->vfa_fb_sizes, 127 .da_fb_vaddr = __UNVOLATILE(vfa->vfa_fb_ptr), 128 .da_fb_linebytes = vfa->vfa_fb_linebytes, 129 .da_params = &vmwgfxfb_drmfb_params, 130 }; 131 int error; 132 133 error = drmfb_attach(&sc->sc_drmfb, &da); 134 if (error) { 135 aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n", 136 error); 137 goto out; 138 } 139 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &vmwgfxfb_shutdown)) 140 aprint_error_dev(sc->sc_dev, 141 "failed to register shutdown handler\n"); 142 143 sc->sc_attached = true; 144 out: 145 config_pending_decr(sc->sc_dev); 146 } 147 148 static bool 149 vmwgfxfb_shutdown(device_t self, int flags) 150 { 151 struct vmwgfxfb_softc *const sc = device_private(self); 152 153 return drmfb_shutdown(&sc->sc_drmfb, flags); 154 } 155 156 static paddr_t 157 vmwgfxfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot) 158 { 159 struct vmwgfxfb_softc *const sc = container_of(drmfb, 160 struct vmwgfxfb_softc, sc_drmfb); 161 struct drm_fb_helper *const helper = sc->sc_vfa.vfa_fb_helper; 162 struct drm_framebuffer *const fb = helper->fb; 163 struct vmw_buffer_object *const vbo = /*XXX MAGIC HERE*/; 164 int flags = 0; 165 166 if (offset < 0) 167 return -1; 168 169 const unsigned num_pages __diagused = vbo->base.num_pages; 170 171 KASSERT(offset < (num_pages << PAGE_SHIFT)); 172 KASSERT(vbo->base.mem.bus.is_iomem); 173 174 if (ISSET(vbo->base.mem.placement, TTM_PL_FLAG_WC)) 175 flags |= BUS_SPACE_MAP_PREFETCHABLE; 176 177 return bus_space_mmap(vbo->base.bdev->memt, 178 vbo->base.mem.bus.base, vbo->base.mem.bus.offset + offset, 179 prot, flags); 180 } 181