Home | History | Annotate | Line # | Download | only in radeon
radeondrmkmsfb.c revision 1.16
      1 /*	$NetBSD: radeondrmkmsfb.c,v 1.16 2022/07/18 23:33:53 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 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 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: radeondrmkmsfb.c,v 1.16 2022/07/18 23:33:53 riastradh Exp $");
     35 
     36 #include <sys/types.h>
     37 #include <sys/device.h>
     38 
     39 #include <drm/drm_fb_helper.h>
     40 #include <drm/drmfb.h>
     41 #include <drm/drmfb_pci.h>
     42 
     43 #include <radeon.h>
     44 #include "radeon_drv.h"
     45 #include "radeon_task.h"
     46 #include "radeondrmkmsfb.h"
     47 
     48 struct radeonfb_softc {
     49 	struct drmfb_softc		sc_drmfb; /* XXX Must be first.  */
     50 	device_t			sc_dev;
     51 	struct radeonfb_attach_args	sc_rfa;
     52 	struct radeon_task		sc_attach_task;
     53 	bool				sc_attached:1;
     54 };
     55 
     56 static int	radeonfb_match(device_t, cfdata_t, void *);
     57 static void	radeonfb_attach(device_t, device_t, void *);
     58 static int	radeonfb_detach(device_t, int);
     59 
     60 static void	radeonfb_attach_task(struct radeon_task *);
     61 
     62 static paddr_t	radeonfb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
     63 static bool	radeonfb_shutdown(device_t, int);
     64 
     65 CFATTACH_DECL_NEW(radeondrmkmsfb, sizeof(struct radeonfb_softc),
     66     radeonfb_match, radeonfb_attach, radeonfb_detach, NULL);
     67 
     68 static const struct drmfb_params radeonfb_drmfb_params = {
     69 	.dp_mmapfb = radeonfb_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 static int
     76 radeonfb_match(device_t parent, cfdata_t match, void *aux)
     77 {
     78 
     79 	return 1;
     80 }
     81 
     82 static void
     83 radeonfb_attach(device_t parent, device_t self, void *aux)
     84 {
     85 	struct radeonfb_softc *const sc = device_private(self);
     86 	const struct radeonfb_attach_args *const rfa = aux;
     87 	int error;
     88 
     89 	sc->sc_dev = self;
     90 	sc->sc_rfa = *rfa;
     91 	sc->sc_attached = false;
     92 
     93 	aprint_naive("\n");
     94 	aprint_normal("\n");
     95 
     96 	radeon_task_init(&sc->sc_attach_task, &radeonfb_attach_task);
     97 	error = radeon_task_schedule(parent, &sc->sc_attach_task);
     98 	if (error) {
     99 		aprint_error_dev(self, "failed to schedule mode set: %d\n",
    100 		    error);
    101 		return;
    102 	}
    103 	config_pending_incr(self);
    104 }
    105 
    106 static int
    107 radeonfb_detach(device_t self, int flags)
    108 {
    109 	struct radeonfb_softc *const sc = device_private(self);
    110 	int error;
    111 
    112 	if (sc->sc_attached) {
    113 		pmf_device_deregister(self);
    114 		error = drmfb_detach(&sc->sc_drmfb, flags);
    115 		if (error) {
    116 			/* XXX Ugh.  */
    117 			(void)pmf_device_register1(self, NULL, NULL,
    118 			    &radeonfb_shutdown);
    119 			return error;
    120 		}
    121 		sc->sc_attached = false;
    122 	}
    123 
    124 	return 0;
    125 }
    126 
    127 static void
    128 radeonfb_attach_task(struct radeon_task *task)
    129 {
    130 	struct radeonfb_softc *const sc = container_of(task,
    131 	    struct radeonfb_softc, sc_attach_task);
    132 	const struct radeonfb_attach_args *const rfa = &sc->sc_rfa;
    133 	const struct drmfb_attach_args da = {
    134 		.da_dev = sc->sc_dev,
    135 		.da_fb_helper = rfa->rfa_fb_helper,
    136 		.da_fb_sizes = &rfa->rfa_fb_sizes,
    137 		.da_fb_vaddr = __UNVOLATILE(rfa->rfa_fb_ptr),
    138 		.da_fb_linebytes = rfa->rfa_fb_linebytes,
    139 		.da_params = &radeonfb_drmfb_params,
    140 	};
    141 	int error;
    142 
    143 	error = drmfb_attach(&sc->sc_drmfb, &da);
    144 	if (error) {
    145 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
    146 		    error);
    147 		goto out;
    148 	}
    149 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &radeonfb_shutdown))
    150 		aprint_error_dev(sc->sc_dev,
    151 		    "failed to register shutdown handler\n");
    152 
    153 	sc->sc_attached = true;
    154 out:
    155 	config_pending_decr(sc->sc_dev);
    156 }
    157 
    158 static bool
    159 radeonfb_shutdown(device_t self, int flags)
    160 {
    161 	struct radeonfb_softc *const sc = device_private(self);
    162 
    163 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    164 }
    165 
    166 static paddr_t
    167 radeonfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
    168 {
    169 	struct radeonfb_softc *const sc = container_of(drmfb,
    170 	    struct radeonfb_softc, sc_drmfb);
    171 	struct drm_fb_helper *const helper = sc->sc_rfa.rfa_fb_helper;
    172 	struct drm_framebuffer *const fb = helper->fb;
    173 	struct drm_gem_object *const gobj = fb->obj[0];
    174 	struct radeon_bo *const rbo = gem_to_radeon_bo(gobj);
    175 	int flags = 0;
    176 
    177 	if (offset < 0)
    178 		return -1;
    179 
    180 	const unsigned num_pages __diagused = rbo->tbo.num_pages;
    181 
    182 	KASSERT(offset < (num_pages << PAGE_SHIFT));
    183 	KASSERT(rbo->tbo.mem.bus.is_iomem);
    184 
    185 	if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC))
    186 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
    187 
    188 	return bus_space_mmap(rbo->tbo.bdev->memt,
    189 	    rbo->tbo.mem.bus.base, rbo->tbo.mem.bus.offset + offset,
    190 	    prot, flags);
    191 }
    192