Home | History | Annotate | Line # | Download | only in nouveau
nouveaufb.c revision 1.7
      1 /*	$NetBSD: nouveaufb.c,v 1.7 2021/12/19 10:51:59 riastradh 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.7 2021/12/19 10:51:59 riastradh 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/drm_drv.h>
     41 #include <drm/drmfb.h>
     42 #include <drm/drmfb_pci.h>
     43 
     44 #include <ttm/ttm_placement.h>
     45 
     46 #include "nouveau_bo.h"
     47 #include "nouveau_drv.h"
     48 #include "nouveau_fbcon.h"
     49 #include "nouveau_pci.h"
     50 #include "nouveaufb.h"
     51 
     52 static int	nouveaufb_match(device_t, cfdata_t, void *);
     53 static void	nouveaufb_attach(device_t, device_t, void *);
     54 static int	nouveaufb_detach(device_t, int);
     55 
     56 static void	nouveaufb_attach_task(struct nouveau_pci_task *);
     57 
     58 static bool	nouveaufb_shutdown(device_t, int);
     59 static paddr_t	nouveaufb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
     60 
     61 struct nouveaufb_softc {
     62 	struct drmfb_softc		sc_drmfb; /* XXX Must be first.  */
     63 	device_t			sc_dev;
     64 	struct nouveaufb_attach_args	sc_nfa;
     65 	struct nouveau_pci_task		sc_attach_task;
     66 	bool				sc_scheduled:1;
     67 	bool				sc_attached:1;
     68 };
     69 
     70 static const struct drmfb_params nouveaufb_drmfb_params = {
     71 	.dp_mmapfb = nouveaufb_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 CFATTACH_DECL_NEW(nouveaufb, sizeof(struct nouveaufb_softc),
     78     nouveaufb_match, nouveaufb_attach, nouveaufb_detach, NULL);
     79 
     80 static int
     81 nouveaufb_match(device_t parent, cfdata_t match, void *aux)
     82 {
     83 
     84 	return 1;
     85 }
     86 
     87 static void
     88 nouveaufb_attach(device_t parent, device_t self, void *aux)
     89 {
     90 	struct nouveaufb_softc *const sc = device_private(self);
     91 	const struct nouveaufb_attach_args *const nfa = aux;
     92 	int error;
     93 
     94 	sc->sc_dev = self;
     95 	sc->sc_nfa = *nfa;
     96 	sc->sc_scheduled = false;
     97 	sc->sc_attached = false;
     98 
     99 	aprint_naive("\n");
    100 	aprint_normal("\n");
    101 
    102 	nouveau_pci_task_init(&sc->sc_attach_task, &nouveaufb_attach_task);
    103 	error = nouveau_pci_task_schedule(parent, &sc->sc_attach_task);
    104 	if (error) {
    105 		aprint_error_dev(self, "failed to schedule mode set: %d\n",
    106 		    error);
    107 		goto fail0;
    108 	}
    109 	sc->sc_scheduled = true;
    110 
    111 	/* Success!  */
    112 	return;
    113 
    114 fail0:	return;
    115 }
    116 
    117 static int
    118 nouveaufb_detach(device_t self, int flags)
    119 {
    120 	struct nouveaufb_softc *const sc = device_private(self);
    121 	int error;
    122 
    123 	if (sc->sc_scheduled)
    124 		return EBUSY;
    125 
    126 	if (sc->sc_attached) {
    127 		pmf_device_deregister(self);
    128 		error = drmfb_detach(&sc->sc_drmfb, flags);
    129 		if (error) {
    130 			/* XXX Ugh.  */
    131 			(void)pmf_device_register1(self, NULL, NULL,
    132 			    &nouveaufb_shutdown);
    133 			return error;
    134 		}
    135 		sc->sc_attached = false;
    136 	}
    137 
    138 	return 0;
    139 }
    140 
    141 static void
    142 nouveaufb_attach_task(struct nouveau_pci_task *task)
    143 {
    144 	struct nouveaufb_softc *const sc = container_of(task,
    145 	    struct nouveaufb_softc, sc_attach_task);
    146 	const struct nouveaufb_attach_args *const nfa = &sc->sc_nfa;
    147 	const struct drmfb_attach_args da = {
    148 		.da_dev = sc->sc_dev,
    149 		.da_fb_helper = nfa->nfa_fb_helper,
    150 		.da_fb_sizes = &nfa->nfa_fb_sizes,
    151 		.da_fb_vaddr = __UNVOLATILE(nfa->nfa_fb_ptr),
    152 		.da_fb_linebytes = nfa->nfa_fb_linebytes,
    153 		.da_params = &nouveaufb_drmfb_params,
    154 	};
    155 	int error;
    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 
    164 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &nouveaufb_shutdown))
    165 		aprint_error_dev(sc->sc_dev,
    166 		    "failed to register shutdown handler\n");
    167 
    168 	sc->sc_attached = true;
    169 }
    170 
    171 static bool
    172 nouveaufb_shutdown(device_t self, int flags)
    173 {
    174 	struct nouveaufb_softc *const sc = device_private(self);
    175 
    176 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    177 }
    178 
    179 static paddr_t
    180 nouveaufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
    181 {
    182 	struct nouveaufb_softc *const sc = container_of(drmfb,
    183 	    struct nouveaufb_softc, sc_drmfb);
    184 	struct drm_fb_helper *const helper = sc->sc_nfa.nfa_fb_helper;
    185 	struct drm_framebuffer *const fb = helper->fb;
    186 	struct nouveau_framebuffer *const nvfb = container_of(fb,
    187 	    struct nouveau_framebuffer, base);
    188 	struct nouveau_bo *const nvbo = nvfb->nvbo;
    189 	const unsigned num_pages __diagused = nvbo->bo.num_pages;
    190 	int flags = 0;
    191 
    192 	KASSERT(0 <= offset);
    193 	KASSERT(offset < (num_pages << PAGE_SHIFT));
    194 
    195 	if (ISSET(nvbo->bo.mem.placement, TTM_PL_FLAG_WC))
    196 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
    197 
    198 	return bus_space_mmap(nvbo->bo.bdev->memt, nvbo->bo.mem.bus.base,
    199 	    nvbo->bo.mem.bus.offset + offset, prot, flags);
    200 }
    201