1 1.5 riastrad /* $NetBSD: amdgpufb.c,v 1.5 2022/07/18 23:34:02 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2018 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.5 riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpufb.c,v 1.5 2022/07/18 23:34:02 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 #include <sys/errno.h> 39 1.1 riastrad 40 1.1 riastrad #include <drm/drmfb.h> 41 1.1 riastrad #include <drm/drmfb_pci.h> 42 1.1 riastrad 43 1.1 riastrad #include "amdgpu.h" 44 1.1 riastrad #include "amdgpu_task.h" 45 1.1 riastrad #include "amdgpufb.h" 46 1.1 riastrad 47 1.1 riastrad static int amdgpufb_match(device_t, cfdata_t, void *); 48 1.1 riastrad static void amdgpufb_attach(device_t, device_t, void *); 49 1.1 riastrad static int amdgpufb_detach(device_t, int); 50 1.1 riastrad 51 1.1 riastrad static void amdgpufb_attach_task(struct amdgpu_task *); 52 1.1 riastrad 53 1.1 riastrad static bool amdgpufb_shutdown(device_t, int); 54 1.1 riastrad static paddr_t amdgpufb_drmfb_mmapfb(struct drmfb_softc *, off_t, int); 55 1.1 riastrad 56 1.1 riastrad struct amdgpufb_softc { 57 1.1 riastrad struct drmfb_softc sc_drmfb; /* XXX Must be first. */ 58 1.1 riastrad device_t sc_dev; 59 1.1 riastrad struct amdgpufb_attach_args sc_afa; 60 1.1 riastrad struct amdgpu_task sc_attach_task; 61 1.1 riastrad bool sc_attached:1; 62 1.1 riastrad }; 63 1.1 riastrad 64 1.1 riastrad static const struct drmfb_params amdgpufb_drmfb_params = { 65 1.1 riastrad .dp_mmapfb = amdgpufb_drmfb_mmapfb, 66 1.1 riastrad .dp_mmap = drmfb_pci_mmap, 67 1.1 riastrad .dp_ioctl = drmfb_pci_ioctl, 68 1.1 riastrad .dp_is_vga_console = drmfb_pci_is_vga_console, 69 1.1 riastrad }; 70 1.1 riastrad 71 1.1 riastrad CFATTACH_DECL_NEW(amdgpufb, sizeof(struct amdgpufb_softc), 72 1.1 riastrad amdgpufb_match, amdgpufb_attach, amdgpufb_detach, NULL); 73 1.1 riastrad 74 1.1 riastrad static int 75 1.1 riastrad amdgpufb_match(device_t parent, cfdata_t match, void *aux) 76 1.1 riastrad { 77 1.1 riastrad 78 1.1 riastrad return 1; 79 1.1 riastrad } 80 1.1 riastrad 81 1.1 riastrad static void 82 1.1 riastrad amdgpufb_attach(device_t parent, device_t self, void *aux) 83 1.1 riastrad { 84 1.1 riastrad struct amdgpufb_softc *const sc = device_private(self); 85 1.1 riastrad const struct amdgpufb_attach_args *const afa = aux; 86 1.1 riastrad 87 1.1 riastrad sc->sc_dev = self; 88 1.1 riastrad sc->sc_afa = *afa; 89 1.1 riastrad sc->sc_attached = false; 90 1.1 riastrad 91 1.1 riastrad aprint_naive("\n"); 92 1.1 riastrad aprint_normal("\n"); 93 1.1 riastrad 94 1.1 riastrad amdgpu_task_init(&sc->sc_attach_task, &amdgpufb_attach_task); 95 1.5 riastrad amdgpu_task_schedule(parent, &sc->sc_attach_task); 96 1.4 riastrad config_pending_incr(self); 97 1.1 riastrad } 98 1.1 riastrad 99 1.1 riastrad static int 100 1.1 riastrad amdgpufb_detach(device_t self, int flags) 101 1.1 riastrad { 102 1.1 riastrad struct amdgpufb_softc *const sc = device_private(self); 103 1.1 riastrad int error; 104 1.1 riastrad 105 1.1 riastrad if (sc->sc_attached) { 106 1.1 riastrad pmf_device_deregister(self); 107 1.1 riastrad error = drmfb_detach(&sc->sc_drmfb, flags); 108 1.1 riastrad if (error) { 109 1.1 riastrad /* XXX Ugh. */ 110 1.1 riastrad (void)pmf_device_register1(self, NULL, NULL, 111 1.1 riastrad &amdgpufb_shutdown); 112 1.1 riastrad return error; 113 1.1 riastrad } 114 1.1 riastrad sc->sc_attached = false; 115 1.1 riastrad } 116 1.1 riastrad 117 1.1 riastrad return 0; 118 1.1 riastrad } 119 1.1 riastrad 120 1.1 riastrad static void 121 1.1 riastrad amdgpufb_attach_task(struct amdgpu_task *task) 122 1.1 riastrad { 123 1.1 riastrad struct amdgpufb_softc *const sc = container_of(task, 124 1.1 riastrad struct amdgpufb_softc, sc_attach_task); 125 1.1 riastrad const struct amdgpufb_attach_args *const afa = &sc->sc_afa; 126 1.1 riastrad const struct drmfb_attach_args da = { 127 1.1 riastrad .da_dev = sc->sc_dev, 128 1.1 riastrad .da_fb_helper = afa->afa_fb_helper, 129 1.1 riastrad .da_fb_sizes = &afa->afa_fb_sizes, 130 1.1 riastrad .da_fb_vaddr = __UNVOLATILE(afa->afa_fb_ptr), 131 1.1 riastrad .da_fb_linebytes = afa->afa_fb_linebytes, 132 1.1 riastrad .da_params = &amdgpufb_drmfb_params, 133 1.1 riastrad }; 134 1.1 riastrad int error; 135 1.1 riastrad 136 1.1 riastrad error = drmfb_attach(&sc->sc_drmfb, &da); 137 1.1 riastrad if (error) { 138 1.1 riastrad aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n", 139 1.1 riastrad error); 140 1.4 riastrad goto out; 141 1.1 riastrad } 142 1.1 riastrad 143 1.1 riastrad if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &amdgpufb_shutdown)) 144 1.1 riastrad aprint_error_dev(sc->sc_dev, 145 1.1 riastrad "failed to register shutdown handler\n"); 146 1.1 riastrad 147 1.1 riastrad sc->sc_attached = true; 148 1.4 riastrad out: 149 1.4 riastrad config_pending_decr(sc->sc_dev); 150 1.1 riastrad } 151 1.1 riastrad 152 1.1 riastrad static bool 153 1.1 riastrad amdgpufb_shutdown(device_t self, int flags) 154 1.1 riastrad { 155 1.1 riastrad struct amdgpufb_softc *const sc = device_private(self); 156 1.1 riastrad 157 1.1 riastrad return drmfb_shutdown(&sc->sc_drmfb, flags); 158 1.1 riastrad } 159 1.1 riastrad 160 1.1 riastrad static paddr_t 161 1.1 riastrad amdgpufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot) 162 1.1 riastrad { 163 1.1 riastrad struct amdgpufb_softc *const sc = container_of(drmfb, 164 1.1 riastrad struct amdgpufb_softc, sc_drmfb); 165 1.1 riastrad struct drm_fb_helper *const helper = sc->sc_afa.afa_fb_helper; 166 1.1 riastrad struct drm_framebuffer *const fb = helper->fb; 167 1.3 riastrad struct drm_gem_object *const gobj = fb->obj[0]; 168 1.1 riastrad struct amdgpu_bo *const rbo = gem_to_amdgpu_bo(gobj); 169 1.1 riastrad const unsigned num_pages __diagused = rbo->tbo.num_pages; 170 1.1 riastrad int flags = 0; 171 1.1 riastrad 172 1.1 riastrad KASSERT(0 <= offset); 173 1.1 riastrad KASSERT(offset < ((uintmax_t)num_pages << PAGE_SHIFT)); 174 1.1 riastrad KASSERT(rbo->tbo.mem.bus.is_iomem); 175 1.1 riastrad 176 1.1 riastrad if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC)) 177 1.1 riastrad flags |= BUS_SPACE_MAP_PREFETCHABLE; 178 1.1 riastrad 179 1.1 riastrad return bus_space_mmap(rbo->tbo.bdev->memt, rbo->tbo.mem.bus.base, 180 1.1 riastrad rbo->tbo.mem.bus.offset + offset, prot, flags); 181 1.1 riastrad } 182