Home | History | Annotate | Line # | Download | only in nouveau
nouveaufb.c revision 1.8
      1 /*	$NetBSD: nouveaufb.c,v 1.8 2022/07/18 23:33:53 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.8 2022/07/18 23:33:53 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_attached:1;
     67 };
     68 
     69 static const struct drmfb_params nouveaufb_drmfb_params = {
     70 	.dp_mmapfb = nouveaufb_drmfb_mmapfb,
     71 	.dp_mmap = drmfb_pci_mmap,
     72 	.dp_ioctl = drmfb_pci_ioctl,
     73 	.dp_is_vga_console = drmfb_pci_is_vga_console,
     74 };
     75 
     76 CFATTACH_DECL_NEW(nouveaufb, sizeof(struct nouveaufb_softc),
     77     nouveaufb_match, nouveaufb_attach, nouveaufb_detach, NULL);
     78 
     79 static int
     80 nouveaufb_match(device_t parent, cfdata_t match, void *aux)
     81 {
     82 
     83 	return 1;
     84 }
     85 
     86 static void
     87 nouveaufb_attach(device_t parent, device_t self, void *aux)
     88 {
     89 	struct nouveaufb_softc *const sc = device_private(self);
     90 	const struct nouveaufb_attach_args *const nfa = aux;
     91 	int error;
     92 
     93 	sc->sc_dev = self;
     94 	sc->sc_nfa = *nfa;
     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 		return;
    106 	}
    107 	config_pending_incr(self);
    108 }
    109 
    110 static int
    111 nouveaufb_detach(device_t self, int flags)
    112 {
    113 	struct nouveaufb_softc *const sc = device_private(self);
    114 	int error;
    115 
    116 	if (sc->sc_attached) {
    117 		pmf_device_deregister(self);
    118 		error = drmfb_detach(&sc->sc_drmfb, flags);
    119 		if (error) {
    120 			/* XXX Ugh.  */
    121 			(void)pmf_device_register1(self, NULL, NULL,
    122 			    &nouveaufb_shutdown);
    123 			return error;
    124 		}
    125 		sc->sc_attached = false;
    126 	}
    127 
    128 	return 0;
    129 }
    130 
    131 static void
    132 nouveaufb_attach_task(struct nouveau_pci_task *task)
    133 {
    134 	struct nouveaufb_softc *const sc = container_of(task,
    135 	    struct nouveaufb_softc, sc_attach_task);
    136 	const struct nouveaufb_attach_args *const nfa = &sc->sc_nfa;
    137 	const struct drmfb_attach_args da = {
    138 		.da_dev = sc->sc_dev,
    139 		.da_fb_helper = nfa->nfa_fb_helper,
    140 		.da_fb_sizes = &nfa->nfa_fb_sizes,
    141 		.da_fb_vaddr = __UNVOLATILE(nfa->nfa_fb_ptr),
    142 		.da_fb_linebytes = nfa->nfa_fb_linebytes,
    143 		.da_params = &nouveaufb_drmfb_params,
    144 	};
    145 	int error;
    146 
    147 	error = drmfb_attach(&sc->sc_drmfb, &da);
    148 	if (error) {
    149 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
    150 		    error);
    151 		goto out;
    152 	}
    153 
    154 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &nouveaufb_shutdown))
    155 		aprint_error_dev(sc->sc_dev,
    156 		    "failed to register shutdown handler\n");
    157 
    158 	sc->sc_attached = true;
    159 out:
    160 	config_pending_decr(sc->sc_dev);
    161 }
    162 
    163 static bool
    164 nouveaufb_shutdown(device_t self, int flags)
    165 {
    166 	struct nouveaufb_softc *const sc = device_private(self);
    167 
    168 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    169 }
    170 
    171 static paddr_t
    172 nouveaufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
    173 {
    174 	struct nouveaufb_softc *const sc = container_of(drmfb,
    175 	    struct nouveaufb_softc, sc_drmfb);
    176 	struct drm_fb_helper *const helper = sc->sc_nfa.nfa_fb_helper;
    177 	struct drm_framebuffer *const fb = helper->fb;
    178 	struct nouveau_framebuffer *const nvfb = container_of(fb,
    179 	    struct nouveau_framebuffer, base);
    180 	struct nouveau_bo *const nvbo = nvfb->nvbo;
    181 	const unsigned num_pages __diagused = nvbo->bo.num_pages;
    182 	int flags = 0;
    183 
    184 	KASSERT(0 <= offset);
    185 	KASSERT(offset < (num_pages << PAGE_SHIFT));
    186 
    187 	if (ISSET(nvbo->bo.mem.placement, TTM_PL_FLAG_WC))
    188 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
    189 
    190 	return bus_space_mmap(nvbo->bo.bdev->memt, nvbo->bo.mem.bus.base,
    191 	    nvbo->bo.mem.bus.offset + offset, prot, flags);
    192 }
    193