Home | History | Annotate | Line # | Download | only in nouveau
      1 /*	$NetBSD: nouveaufb.c,v 1.9 2022/07/18 23:34:02 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.9 2022/07/18 23:34:02 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 
     92 	sc->sc_dev = self;
     93 	sc->sc_nfa = *nfa;
     94 	sc->sc_attached = false;
     95 
     96 	aprint_naive("\n");
     97 	aprint_normal("\n");
     98 
     99 	nouveau_pci_task_init(&sc->sc_attach_task, &nouveaufb_attach_task);
    100 	nouveau_pci_task_schedule(parent, &sc->sc_attach_task);
    101 	config_pending_incr(self);
    102 }
    103 
    104 static int
    105 nouveaufb_detach(device_t self, int flags)
    106 {
    107 	struct nouveaufb_softc *const sc = device_private(self);
    108 	int error;
    109 
    110 	if (sc->sc_attached) {
    111 		pmf_device_deregister(self);
    112 		error = drmfb_detach(&sc->sc_drmfb, flags);
    113 		if (error) {
    114 			/* XXX Ugh.  */
    115 			(void)pmf_device_register1(self, NULL, NULL,
    116 			    &nouveaufb_shutdown);
    117 			return error;
    118 		}
    119 		sc->sc_attached = false;
    120 	}
    121 
    122 	return 0;
    123 }
    124 
    125 static void
    126 nouveaufb_attach_task(struct nouveau_pci_task *task)
    127 {
    128 	struct nouveaufb_softc *const sc = container_of(task,
    129 	    struct nouveaufb_softc, sc_attach_task);
    130 	const struct nouveaufb_attach_args *const nfa = &sc->sc_nfa;
    131 	const struct drmfb_attach_args da = {
    132 		.da_dev = sc->sc_dev,
    133 		.da_fb_helper = nfa->nfa_fb_helper,
    134 		.da_fb_sizes = &nfa->nfa_fb_sizes,
    135 		.da_fb_vaddr = __UNVOLATILE(nfa->nfa_fb_ptr),
    136 		.da_fb_linebytes = nfa->nfa_fb_linebytes,
    137 		.da_params = &nouveaufb_drmfb_params,
    138 	};
    139 	int error;
    140 
    141 	error = drmfb_attach(&sc->sc_drmfb, &da);
    142 	if (error) {
    143 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
    144 		    error);
    145 		goto out;
    146 	}
    147 
    148 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &nouveaufb_shutdown))
    149 		aprint_error_dev(sc->sc_dev,
    150 		    "failed to register shutdown handler\n");
    151 
    152 	sc->sc_attached = true;
    153 out:
    154 	config_pending_decr(sc->sc_dev);
    155 }
    156 
    157 static bool
    158 nouveaufb_shutdown(device_t self, int flags)
    159 {
    160 	struct nouveaufb_softc *const sc = device_private(self);
    161 
    162 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    163 }
    164 
    165 static paddr_t
    166 nouveaufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
    167 {
    168 	struct nouveaufb_softc *const sc = container_of(drmfb,
    169 	    struct nouveaufb_softc, sc_drmfb);
    170 	struct drm_fb_helper *const helper = sc->sc_nfa.nfa_fb_helper;
    171 	struct drm_framebuffer *const fb = helper->fb;
    172 	struct nouveau_framebuffer *const nvfb = container_of(fb,
    173 	    struct nouveau_framebuffer, base);
    174 	struct nouveau_bo *const nvbo = nvfb->nvbo;
    175 	const unsigned num_pages __diagused = nvbo->bo.num_pages;
    176 	int flags = 0;
    177 
    178 	KASSERT(0 <= offset);
    179 	KASSERT(offset < (num_pages << PAGE_SHIFT));
    180 
    181 	if (ISSET(nvbo->bo.mem.placement, TTM_PL_FLAG_WC))
    182 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
    183 
    184 	return bus_space_mmap(nvbo->bo.bdev->memt, nvbo->bo.mem.bus.base,
    185 	    nvbo->bo.mem.bus.offset + offset, prot, flags);
    186 }
    187