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