1 /* $NetBSD: nouveaufb.c,v 1.4 2016/12/12 19:45:56 maya Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 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 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: nouveaufb.c,v 1.4 2016/12/12 19:45:56 maya Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/bus.h> 37 #include <sys/device.h> 38 #include <sys/errno.h> 39 40 #include <drm/drmP.h> 41 #include <drm/drmfb.h> 42 #include <drm/drmfb_pci.h> 43 44 #include "nouveau_bo.h" 45 #include "nouveau_drm.h" 46 #include "nouveau_fbcon.h" 47 #include "nouveau_pci.h" 48 #include "nouveaufb.h" 49 50 static int nouveaufb_match(device_t, cfdata_t, void *); 51 static void nouveaufb_attach(device_t, device_t, void *); 52 static int nouveaufb_detach(device_t, int); 53 54 static void nouveaufb_attach_task(struct nouveau_pci_task *); 55 56 static bool nouveaufb_shutdown(device_t, int); 57 static paddr_t nouveaufb_drmfb_mmapfb(struct drmfb_softc *, off_t, int); 58 59 struct nouveaufb_softc { 60 struct drmfb_softc sc_drmfb; /* XXX Must be first. */ 61 device_t sc_dev; 62 struct nouveaufb_attach_args sc_nfa; 63 struct nouveau_pci_task sc_attach_task; 64 bool sc_scheduled:1; 65 bool sc_attached:1; 66 }; 67 68 static const struct drmfb_params nouveaufb_drmfb_params = { 69 .dp_mmapfb = nouveaufb_drmfb_mmapfb, 70 .dp_mmap = drmfb_pci_mmap, 71 .dp_ioctl = drmfb_pci_ioctl, 72 .dp_is_vga_console = drmfb_pci_is_vga_console, 73 }; 74 75 CFATTACH_DECL_NEW(nouveaufb, sizeof(struct nouveaufb_softc), 76 nouveaufb_match, nouveaufb_attach, nouveaufb_detach, NULL); 77 78 static int 79 nouveaufb_match(device_t parent, cfdata_t match, void *aux) 80 { 81 82 return 1; 83 } 84 85 static void 86 nouveaufb_attach(device_t parent, device_t self, void *aux) 87 { 88 struct nouveaufb_softc *const sc = device_private(self); 89 const struct nouveaufb_attach_args *const nfa = aux; 90 int error; 91 92 sc->sc_dev = self; 93 sc->sc_nfa = *nfa; 94 sc->sc_scheduled = false; 95 sc->sc_attached = false; 96 97 aprint_naive("\n"); 98 aprint_normal("\n"); 99 100 nouveau_pci_task_init(&sc->sc_attach_task, &nouveaufb_attach_task); 101 error = nouveau_pci_task_schedule(parent, &sc->sc_attach_task); 102 if (error) { 103 aprint_error_dev(self, "failed to schedule mode set: %d\n", 104 error); 105 goto fail0; 106 } 107 sc->sc_scheduled = true; 108 109 /* Success! */ 110 return; 111 112 fail0: return; 113 } 114 115 static int 116 nouveaufb_detach(device_t self, int flags) 117 { 118 struct nouveaufb_softc *const sc = device_private(self); 119 int error; 120 121 if (sc->sc_scheduled) 122 return EBUSY; 123 124 if (sc->sc_attached) { 125 pmf_device_deregister(self); 126 error = drmfb_detach(&sc->sc_drmfb, flags); 127 if (error) { 128 /* XXX Ugh. */ 129 (void)pmf_device_register1(self, NULL, NULL, 130 &nouveaufb_shutdown); 131 return error; 132 } 133 sc->sc_attached = false; 134 } 135 136 return 0; 137 } 138 139 static void 140 nouveaufb_attach_task(struct nouveau_pci_task *task) 141 { 142 struct nouveaufb_softc *const sc = container_of(task, 143 struct nouveaufb_softc, sc_attach_task); 144 const struct nouveaufb_attach_args *const nfa = &sc->sc_nfa; 145 const struct drmfb_attach_args da = { 146 .da_dev = sc->sc_dev, 147 .da_fb_helper = nfa->nfa_fb_helper, 148 .da_fb_sizes = &nfa->nfa_fb_sizes, 149 .da_fb_vaddr = __UNVOLATILE(nfa->nfa_fb_ptr), 150 .da_fb_linebytes = nfa->nfa_fb_linebytes, 151 .da_params = &nouveaufb_drmfb_params, 152 }; 153 int error; 154 155 error = drmfb_attach(&sc->sc_drmfb, &da); 156 if (error) { 157 aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n", 158 error); 159 return; 160 } 161 162 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &nouveaufb_shutdown)) 163 aprint_error_dev(sc->sc_dev, 164 "failed to register shutdown handler\n"); 165 166 sc->sc_attached = true; 167 } 168 169 static bool 170 nouveaufb_shutdown(device_t self, int flags) 171 { 172 struct nouveaufb_softc *const sc = device_private(self); 173 174 return drmfb_shutdown(&sc->sc_drmfb, flags); 175 } 176 177 static paddr_t 178 nouveaufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot) 179 { 180 struct nouveaufb_softc *const sc = container_of(drmfb, 181 struct nouveaufb_softc, sc_drmfb); 182 struct drm_fb_helper *const helper = sc->sc_nfa.nfa_fb_helper; 183 struct nouveau_fbdev *const fbdev = container_of(helper, 184 struct nouveau_fbdev, helper); 185 struct nouveau_bo *const nvbo = fbdev->nouveau_fb.nvbo; 186 const unsigned num_pages __diagused = nvbo->bo.num_pages; 187 int flags = 0; 188 189 KASSERT(0 <= offset); 190 KASSERT(offset < (num_pages << PAGE_SHIFT)); 191 192 if (ISSET(nvbo->bo.mem.placement, TTM_PL_FLAG_WC)) 193 flags |= BUS_SPACE_MAP_PREFETCHABLE; 194 195 return bus_space_mmap(nvbo->bo.bdev->memt, nvbo->bo.mem.bus.base, 196 nvbo->bo.mem.bus.offset + offset, prot, flags); 197 } 198