1 1.6 riastrad /* $NetBSD: tegra_fb.c,v 1.6 2021/12/19 12:44:50 riastradh Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.6 riastrad __KERNEL_RCSID(0, "$NetBSD: tegra_fb.c,v 1.6 2021/12/19 12:44:50 riastradh Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/bus.h> 34 1.1 jmcneill #include <sys/device.h> 35 1.1 jmcneill 36 1.5 riastrad #include <drm/drm_drv.h> 37 1.1 jmcneill #include <drm/drmfb.h> 38 1.1 jmcneill 39 1.1 jmcneill #include <arm/nvidia/tegra_var.h> 40 1.1 jmcneill #include <arm/nvidia/tegra_drm.h> 41 1.1 jmcneill 42 1.1 jmcneill static int tegra_fb_match(device_t, cfdata_t, void *); 43 1.1 jmcneill static void tegra_fb_attach(device_t, device_t, void *); 44 1.1 jmcneill 45 1.6 riastrad static void tegra_fb_init(struct tegra_drm_task *); 46 1.6 riastrad 47 1.1 jmcneill static bool tegra_fb_shutdown(device_t, int); 48 1.1 jmcneill 49 1.1 jmcneill struct tegra_fb_softc { 50 1.1 jmcneill struct drmfb_softc sc_drmfb; 51 1.1 jmcneill device_t sc_dev; 52 1.1 jmcneill struct tegra_drm_softc *sc_drm; 53 1.1 jmcneill struct tegra_drmfb_attach_args sc_tfa; 54 1.2 jmcneill struct tegra_framebuffer *sc_fb; 55 1.6 riastrad struct tegra_drm_task sc_attach_task; 56 1.1 jmcneill }; 57 1.1 jmcneill 58 1.1 jmcneill static paddr_t tegra_fb_mmapfb(struct drmfb_softc *, off_t, int); 59 1.1 jmcneill static int tegra_fb_ioctl(struct drmfb_softc *, u_long, void *, int, 60 1.1 jmcneill lwp_t *); 61 1.1 jmcneill 62 1.1 jmcneill 63 1.1 jmcneill static const struct drmfb_params tegrafb_drmfb_params = { 64 1.1 jmcneill .dp_mmapfb = tegra_fb_mmapfb, 65 1.1 jmcneill .dp_ioctl = tegra_fb_ioctl, 66 1.1 jmcneill }; 67 1.1 jmcneill 68 1.1 jmcneill CFATTACH_DECL_NEW(tegra_fb, sizeof(struct tegra_fb_softc), 69 1.1 jmcneill tegra_fb_match, tegra_fb_attach, NULL, NULL); 70 1.1 jmcneill 71 1.1 jmcneill static int 72 1.1 jmcneill tegra_fb_match(device_t parent, cfdata_t cf, void *aux) 73 1.1 jmcneill { 74 1.1 jmcneill return 1; 75 1.1 jmcneill } 76 1.1 jmcneill 77 1.1 jmcneill static void 78 1.1 jmcneill tegra_fb_attach(device_t parent, device_t self, void *aux) 79 1.1 jmcneill { 80 1.1 jmcneill struct tegra_fb_softc * const sc = device_private(self); 81 1.1 jmcneill struct tegra_drm_softc * const drmsc = device_private(parent); 82 1.1 jmcneill struct tegra_drmfb_attach_args * const tfa = aux; 83 1.1 jmcneill 84 1.1 jmcneill sc->sc_dev = self; 85 1.1 jmcneill sc->sc_drm = drmsc; 86 1.1 jmcneill sc->sc_tfa = *tfa; 87 1.2 jmcneill sc->sc_fb = to_tegra_framebuffer(tfa->tfa_fb_helper->fb); 88 1.1 jmcneill 89 1.1 jmcneill aprint_naive("\n"); 90 1.1 jmcneill aprint_normal("\n"); 91 1.1 jmcneill 92 1.6 riastrad tegra_task_init(&sc->sc_attach_task, &tegra_fb_init); 93 1.6 riastrad tegra_task_schedule(parent, &sc->sc_attach_task); 94 1.6 riastrad } 95 1.6 riastrad 96 1.6 riastrad static void 97 1.6 riastrad tegra_fb_init(struct tegra_drm_task *task) 98 1.6 riastrad { 99 1.6 riastrad struct tegra_fb_softc *sc = container_of(task, struct tegra_fb_softc, 100 1.6 riastrad sc_attach_task); 101 1.6 riastrad device_t self = sc->sc_dev; 102 1.6 riastrad struct tegra_drmfb_attach_args * const tfa = &sc->sc_tfa; 103 1.3 maya const struct drmfb_attach_args da = { 104 1.3 maya .da_dev = self, 105 1.3 maya .da_fb_helper = tfa->tfa_fb_helper, 106 1.3 maya .da_fb_sizes = &tfa->tfa_fb_sizes, 107 1.4 jmcneill .da_fb_vaddr = sc->sc_fb->obj->vaddr, 108 1.3 maya .da_fb_linebytes = tfa->tfa_fb_linebytes, 109 1.3 maya .da_params = &tegrafb_drmfb_params, 110 1.3 maya }; 111 1.6 riastrad int error; 112 1.1 jmcneill 113 1.1 jmcneill error = drmfb_attach(&sc->sc_drmfb, &da); 114 1.1 jmcneill if (error) { 115 1.1 jmcneill aprint_error_dev(self, "failed to attach drmfb: %d\n", error); 116 1.1 jmcneill return; 117 1.1 jmcneill } 118 1.1 jmcneill 119 1.1 jmcneill pmf_device_register1(self, NULL, NULL, tegra_fb_shutdown); 120 1.1 jmcneill } 121 1.1 jmcneill 122 1.1 jmcneill static bool 123 1.1 jmcneill tegra_fb_shutdown(device_t self, int flags) 124 1.1 jmcneill { 125 1.1 jmcneill struct tegra_fb_softc * const sc = device_private(self); 126 1.1 jmcneill 127 1.1 jmcneill return drmfb_shutdown(&sc->sc_drmfb, flags); 128 1.1 jmcneill } 129 1.1 jmcneill 130 1.1 jmcneill static paddr_t 131 1.1 jmcneill tegra_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot) 132 1.1 jmcneill { 133 1.1 jmcneill struct tegra_fb_softc * const tfb_sc = (struct tegra_fb_softc *)sc; 134 1.4 jmcneill struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj; 135 1.1 jmcneill 136 1.1 jmcneill KASSERT(off >= 0); 137 1.2 jmcneill KASSERT(off < obj->dmasize); 138 1.1 jmcneill 139 1.2 jmcneill return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot, 140 1.2 jmcneill BUS_DMA_PREFETCHABLE); 141 1.1 jmcneill } 142 1.1 jmcneill 143 1.1 jmcneill static int 144 1.1 jmcneill tegra_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag, 145 1.1 jmcneill lwp_t *l) 146 1.1 jmcneill { 147 1.1 jmcneill struct wsdisplayio_bus_id *busid; 148 1.1 jmcneill struct wsdisplayio_fbinfo *fbi; 149 1.1 jmcneill struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri; 150 1.1 jmcneill int error; 151 1.1 jmcneill 152 1.1 jmcneill switch (cmd) { 153 1.1 jmcneill case WSDISPLAYIO_GET_BUSID: 154 1.1 jmcneill busid = data; 155 1.1 jmcneill busid->bus_type = WSDISPLAYIO_BUS_SOC; 156 1.1 jmcneill return 0; 157 1.1 jmcneill case WSDISPLAYIO_GTYPE: 158 1.1 jmcneill *(u_int *)data = WSDISPLAY_TYPE_TEGRA; 159 1.1 jmcneill return 0; 160 1.1 jmcneill case WSDISPLAYIO_GET_FBINFO: 161 1.1 jmcneill fbi = data; 162 1.1 jmcneill error = wsdisplayio_get_fbinfo(ri, fbi); 163 1.1 jmcneill fbi->fbi_flags |= WSFB_VRAM_IS_RAM; 164 1.1 jmcneill return error; 165 1.1 jmcneill default: 166 1.1 jmcneill return EPASSTHROUGH; 167 1.1 jmcneill } 168 1.1 jmcneill } 169