Home | History | Annotate | Line # | Download | only in radeon
radeondrmkmsfb.c revision 1.7.14.2
      1  1.7.14.2    martin /*	$NetBSD: radeondrmkmsfb.c,v 1.7.14.2 2020/04/13 08:05:00 martin Exp $	*/
      2       1.1  riastrad 
      3       1.1  riastrad /*-
      4       1.1  riastrad  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5       1.1  riastrad  * All rights reserved.
      6       1.1  riastrad  *
      7       1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  riastrad  * by Taylor R. Campbell.
      9       1.1  riastrad  *
     10       1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11       1.1  riastrad  * modification, are permitted provided that the following conditions
     12       1.1  riastrad  * are met:
     13       1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14       1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15       1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18       1.1  riastrad  *
     19       1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  riastrad  */
     31       1.1  riastrad 
     32       1.1  riastrad 
     33       1.1  riastrad #include <sys/cdefs.h>
     34  1.7.14.2    martin __KERNEL_RCSID(0, "$NetBSD: radeondrmkmsfb.c,v 1.7.14.2 2020/04/13 08:05:00 martin Exp $");
     35       1.1  riastrad 
     36       1.1  riastrad #include <sys/types.h>
     37       1.1  riastrad #include <sys/device.h>
     38       1.1  riastrad 
     39       1.1  riastrad #include <drm/drmP.h>
     40       1.1  riastrad #include <drm/drm_fb_helper.h>
     41  1.7.14.1  christos #include <drm/drmfb.h>
     42  1.7.14.1  christos #include <drm/drmfb_pci.h>
     43       1.1  riastrad 
     44       1.1  riastrad #include <radeon.h>
     45       1.1  riastrad #include "radeon_drv.h"
     46       1.1  riastrad #include "radeon_task.h"
     47       1.1  riastrad #include "radeondrmkmsfb.h"
     48       1.1  riastrad 
     49       1.1  riastrad struct radeonfb_softc {
     50  1.7.14.1  christos 	struct drmfb_softc		sc_drmfb; /* XXX Must be first.  */
     51       1.1  riastrad 	device_t			sc_dev;
     52       1.1  riastrad 	struct radeonfb_attach_args	sc_rfa;
     53  1.7.14.1  christos 	struct radeon_task		sc_attach_task;
     54       1.1  riastrad 	bool				sc_scheduled:1;
     55       1.1  riastrad 	bool				sc_attached:1;
     56       1.1  riastrad };
     57       1.1  riastrad 
     58       1.1  riastrad static int	radeonfb_match(device_t, cfdata_t, void *);
     59       1.1  riastrad static void	radeonfb_attach(device_t, device_t, void *);
     60       1.1  riastrad static int	radeonfb_detach(device_t, int);
     61       1.1  riastrad 
     62  1.7.14.1  christos static void	radeonfb_attach_task(struct radeon_task *);
     63       1.1  riastrad 
     64  1.7.14.1  christos static paddr_t	radeonfb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
     65  1.7.14.1  christos static bool	radeonfb_shutdown(device_t, int);
     66       1.1  riastrad 
     67       1.1  riastrad CFATTACH_DECL_NEW(radeondrmkmsfb, sizeof(struct radeonfb_softc),
     68       1.1  riastrad     radeonfb_match, radeonfb_attach, radeonfb_detach, NULL);
     69       1.1  riastrad 
     70  1.7.14.1  christos static const struct drmfb_params radeonfb_drmfb_params = {
     71  1.7.14.1  christos 	.dp_mmapfb = radeonfb_drmfb_mmapfb,
     72  1.7.14.1  christos 	.dp_mmap = drmfb_pci_mmap,
     73  1.7.14.1  christos 	.dp_ioctl = drmfb_pci_ioctl,
     74  1.7.14.1  christos 	.dp_is_vga_console = drmfb_pci_is_vga_console,
     75  1.7.14.1  christos };
     76  1.7.14.1  christos 
     77       1.1  riastrad static int
     78       1.1  riastrad radeonfb_match(device_t parent, cfdata_t match, void *aux)
     79       1.1  riastrad {
     80       1.1  riastrad 
     81       1.1  riastrad 	return 1;
     82       1.1  riastrad }
     83       1.1  riastrad 
     84       1.1  riastrad static void
     85       1.1  riastrad radeonfb_attach(device_t parent, device_t self, void *aux)
     86       1.1  riastrad {
     87       1.1  riastrad 	struct radeonfb_softc *const sc = device_private(self);
     88       1.1  riastrad 	const struct radeonfb_attach_args *const rfa = aux;
     89       1.1  riastrad 	int error;
     90       1.1  riastrad 
     91       1.1  riastrad 	sc->sc_dev = self;
     92       1.1  riastrad 	sc->sc_rfa = *rfa;
     93       1.1  riastrad 	sc->sc_scheduled = false;
     94       1.1  riastrad 	sc->sc_attached = false;
     95       1.1  riastrad 
     96       1.2  riastrad 	aprint_naive("\n");
     97       1.2  riastrad 	aprint_normal("\n");
     98       1.2  riastrad 
     99  1.7.14.1  christos 	radeon_task_init(&sc->sc_attach_task, &radeonfb_attach_task);
    100  1.7.14.1  christos 	error = radeon_task_schedule(parent, &sc->sc_attach_task);
    101       1.1  riastrad 	if (error) {
    102       1.1  riastrad 		aprint_error_dev(self, "failed to schedule mode set: %d\n",
    103       1.1  riastrad 		    error);
    104       1.1  riastrad 		goto fail0;
    105       1.1  riastrad 	}
    106       1.1  riastrad 	sc->sc_scheduled = true;
    107       1.1  riastrad 
    108       1.1  riastrad 	/* Success!  */
    109       1.1  riastrad 	return;
    110       1.1  riastrad 
    111       1.1  riastrad fail0:	return;
    112       1.1  riastrad }
    113       1.1  riastrad 
    114       1.1  riastrad static int
    115       1.1  riastrad radeonfb_detach(device_t self, int flags)
    116       1.1  riastrad {
    117       1.1  riastrad 	struct radeonfb_softc *const sc = device_private(self);
    118  1.7.14.1  christos 	int error;
    119       1.1  riastrad 
    120       1.1  riastrad 	if (sc->sc_scheduled)
    121       1.1  riastrad 		return EBUSY;
    122       1.1  riastrad 
    123       1.1  riastrad 	if (sc->sc_attached) {
    124  1.7.14.1  christos 		pmf_device_deregister(self);
    125  1.7.14.1  christos 		error = drmfb_detach(&sc->sc_drmfb, flags);
    126  1.7.14.1  christos 		if (error) {
    127  1.7.14.1  christos 			/* XXX Ugh.  */
    128  1.7.14.1  christos 			(void)pmf_device_register1(self, NULL, NULL,
    129  1.7.14.1  christos 			    &radeonfb_shutdown);
    130  1.7.14.1  christos 			return error;
    131  1.7.14.1  christos 		}
    132       1.1  riastrad 		sc->sc_attached = false;
    133       1.1  riastrad 	}
    134       1.1  riastrad 
    135       1.1  riastrad 	return 0;
    136       1.1  riastrad }
    137       1.1  riastrad 
    138       1.1  riastrad static void
    139  1.7.14.1  christos radeonfb_attach_task(struct radeon_task *task)
    140       1.1  riastrad {
    141       1.1  riastrad 	struct radeonfb_softc *const sc = container_of(task,
    142  1.7.14.1  christos 	    struct radeonfb_softc, sc_attach_task);
    143       1.1  riastrad 	const struct radeonfb_attach_args *const rfa = &sc->sc_rfa;
    144  1.7.14.1  christos 	const struct drmfb_attach_args da = {
    145  1.7.14.1  christos 		.da_dev = sc->sc_dev,
    146  1.7.14.1  christos 		.da_fb_helper = rfa->rfa_fb_helper,
    147  1.7.14.1  christos 		.da_fb_sizes = &rfa->rfa_fb_sizes,
    148  1.7.14.1  christos 		.da_fb_vaddr = __UNVOLATILE(rfa->rfa_fb_ptr),
    149  1.7.14.1  christos 		.da_fb_linebytes = rfa->rfa_fb_linebytes,
    150  1.7.14.1  christos 		.da_params = &radeonfb_drmfb_params,
    151  1.7.14.1  christos 	};
    152       1.1  riastrad 	int error;
    153       1.1  riastrad 
    154       1.1  riastrad 	KASSERT(sc->sc_scheduled);
    155       1.1  riastrad 
    156       1.1  riastrad 
    157  1.7.14.1  christos 	error = drmfb_attach(&sc->sc_drmfb, &da);
    158       1.1  riastrad 	if (error) {
    159  1.7.14.1  christos 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
    160       1.1  riastrad 		    error);
    161  1.7.14.1  christos 		return;
    162       1.1  riastrad 	}
    163  1.7.14.1  christos 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &radeonfb_shutdown))
    164       1.7      maya 		aprint_error_dev(sc->sc_dev,
    165  1.7.14.1  christos 		    "failed to register shutdown handler\n");
    166       1.1  riastrad 
    167  1.7.14.1  christos 	sc->sc_attached = true;
    168       1.1  riastrad }
    169       1.1  riastrad 
    170  1.7.14.1  christos static bool
    171  1.7.14.1  christos radeonfb_shutdown(device_t self, int flags)
    172       1.1  riastrad {
    173  1.7.14.1  christos 	struct radeonfb_softc *const sc = device_private(self);
    174       1.1  riastrad 
    175  1.7.14.1  christos 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    176       1.1  riastrad }
    177       1.1  riastrad 
    178       1.1  riastrad static paddr_t
    179  1.7.14.1  christos radeonfb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
    180       1.1  riastrad {
    181  1.7.14.1  christos 	struct radeonfb_softc *const sc = container_of(drmfb,
    182  1.7.14.1  christos 	    struct radeonfb_softc, sc_drmfb);
    183       1.1  riastrad 	struct drm_fb_helper *const helper = sc->sc_rfa.rfa_fb_helper;
    184       1.1  riastrad 	struct drm_framebuffer *const fb = helper->fb;
    185       1.1  riastrad 	struct radeon_framebuffer *const rfb = container_of(fb,
    186       1.1  riastrad 	    struct radeon_framebuffer, base);
    187       1.1  riastrad 	struct drm_gem_object *const gobj = rfb->obj;
    188       1.1  riastrad 	struct radeon_bo *const rbo = gem_to_radeon_bo(gobj);
    189  1.7.14.1  christos 	int flags = 0;
    190       1.1  riastrad 
    191       1.1  riastrad 	if (offset < 0)
    192       1.1  riastrad 		return -1;
    193       1.1  riastrad 
    194  1.7.14.1  christos 	const unsigned num_pages __diagused = rbo->tbo.num_pages;
    195       1.4       chs 
    196  1.7.14.1  christos 	KASSERT(offset < (num_pages << PAGE_SHIFT));
    197  1.7.14.1  christos 	KASSERT(rbo->tbo.mem.bus.is_iomem);
    198       1.4       chs 
    199  1.7.14.1  christos 	if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC))
    200  1.7.14.1  christos 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
    201       1.4       chs 
    202  1.7.14.1  christos 	return bus_space_mmap(rbo->tbo.bdev->memt,
    203  1.7.14.1  christos 	    rbo->tbo.mem.bus.base, rbo->tbo.mem.bus.offset + offset,
    204  1.7.14.1  christos 	    prot, flags);
    205       1.4       chs }
    206