xref: /netbsd-src/sys/external/bsd/drm2/amdgpu/amdgpufb.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: amdgpufb.c,v 1.1 2018/08/27 14:02:32 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2018 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: amdgpufb.c,v 1.1 2018/08/27 14:02:32 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/drmP.h>
41 #include <drm/drmfb.h>
42 #include <drm/drmfb_pci.h>
43 
44 #include "amdgpu.h"
45 #include "amdgpu_task.h"
46 #include "amdgpufb.h"
47 
48 static int	amdgpufb_match(device_t, cfdata_t, void *);
49 static void	amdgpufb_attach(device_t, device_t, void *);
50 static int	amdgpufb_detach(device_t, int);
51 
52 static void	amdgpufb_attach_task(struct amdgpu_task *);
53 
54 static bool	amdgpufb_shutdown(device_t, int);
55 static paddr_t	amdgpufb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
56 
57 struct amdgpufb_softc {
58 	struct drmfb_softc		sc_drmfb; /* XXX Must be first.  */
59 	device_t			sc_dev;
60 	struct amdgpufb_attach_args	sc_afa;
61 	struct amdgpu_task		sc_attach_task;
62 	bool				sc_scheduled:1;
63 	bool				sc_attached:1;
64 };
65 
66 static const struct drmfb_params amdgpufb_drmfb_params = {
67 	.dp_mmapfb = amdgpufb_drmfb_mmapfb,
68 	.dp_mmap = drmfb_pci_mmap,
69 	.dp_ioctl = drmfb_pci_ioctl,
70 	.dp_is_vga_console = drmfb_pci_is_vga_console,
71 };
72 
73 CFATTACH_DECL_NEW(amdgpufb, sizeof(struct amdgpufb_softc),
74     amdgpufb_match, amdgpufb_attach, amdgpufb_detach, NULL);
75 
76 static int
77 amdgpufb_match(device_t parent, cfdata_t match, void *aux)
78 {
79 
80 	return 1;
81 }
82 
83 static void
84 amdgpufb_attach(device_t parent, device_t self, void *aux)
85 {
86 	struct amdgpufb_softc *const sc = device_private(self);
87 	const struct amdgpufb_attach_args *const afa = aux;
88 	int error;
89 
90 	sc->sc_dev = self;
91 	sc->sc_afa = *afa;
92 	sc->sc_scheduled = false;
93 	sc->sc_attached = false;
94 
95 	aprint_naive("\n");
96 	aprint_normal("\n");
97 
98 	amdgpu_task_init(&sc->sc_attach_task, &amdgpufb_attach_task);
99 	error = amdgpu_task_schedule(parent, &sc->sc_attach_task);
100 	if (error) {
101 		aprint_error_dev(self, "failed to schedule mode set: %d\n",
102 		    error);
103 		goto fail0;
104 	}
105 	sc->sc_scheduled = true;
106 
107 	/* Success!  */
108 	return;
109 
110 fail0:	return;
111 }
112 
113 static int
114 amdgpufb_detach(device_t self, int flags)
115 {
116 	struct amdgpufb_softc *const sc = device_private(self);
117 	int error;
118 
119 	if (sc->sc_scheduled)
120 		return EBUSY;
121 
122 	if (sc->sc_attached) {
123 		pmf_device_deregister(self);
124 		error = drmfb_detach(&sc->sc_drmfb, flags);
125 		if (error) {
126 			/* XXX Ugh.  */
127 			(void)pmf_device_register1(self, NULL, NULL,
128 			    &amdgpufb_shutdown);
129 			return error;
130 		}
131 		sc->sc_attached = false;
132 	}
133 
134 	return 0;
135 }
136 
137 static void
138 amdgpufb_attach_task(struct amdgpu_task *task)
139 {
140 	struct amdgpufb_softc *const sc = container_of(task,
141 	    struct amdgpufb_softc, sc_attach_task);
142 	const struct amdgpufb_attach_args *const afa = &sc->sc_afa;
143 	const struct drmfb_attach_args da = {
144 		.da_dev = sc->sc_dev,
145 		.da_fb_helper = afa->afa_fb_helper,
146 		.da_fb_sizes = &afa->afa_fb_sizes,
147 		.da_fb_vaddr = __UNVOLATILE(afa->afa_fb_ptr),
148 		.da_fb_linebytes = afa->afa_fb_linebytes,
149 		.da_params = &amdgpufb_drmfb_params,
150 	};
151 	int error;
152 
153 	error = drmfb_attach(&sc->sc_drmfb, &da);
154 	if (error) {
155 		aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
156 		    error);
157 		return;
158 	}
159 
160 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &amdgpufb_shutdown))
161 		aprint_error_dev(sc->sc_dev,
162 		    "failed to register shutdown handler\n");
163 
164 	sc->sc_attached = true;
165 }
166 
167 static bool
168 amdgpufb_shutdown(device_t self, int flags)
169 {
170 	struct amdgpufb_softc *const sc = device_private(self);
171 
172 	return drmfb_shutdown(&sc->sc_drmfb, flags);
173 }
174 
175 static paddr_t
176 amdgpufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
177 {
178 	struct amdgpufb_softc *const sc = container_of(drmfb,
179 	    struct amdgpufb_softc, sc_drmfb);
180 	struct drm_fb_helper *const helper = sc->sc_afa.afa_fb_helper;
181 	struct drm_framebuffer *const fb = helper->fb;
182 	struct amdgpu_framebuffer *const rfb = container_of(fb,
183 	    struct amdgpu_framebuffer, base);
184 	struct drm_gem_object *const gobj = rfb->obj;
185 	struct amdgpu_bo *const rbo = gem_to_amdgpu_bo(gobj);
186 	const unsigned num_pages __diagused = rbo->tbo.num_pages;
187 	int flags = 0;
188 
189 	KASSERT(0 <= offset);
190 	KASSERT(offset < ((uintmax_t)num_pages << PAGE_SHIFT));
191 	KASSERT(rbo->tbo.mem.bus.is_iomem);
192 
193 	if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC))
194 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
195 
196 	return bus_space_mmap(rbo->tbo.bdev->memt, rbo->tbo.mem.bus.base,
197 	    rbo->tbo.mem.bus.offset + offset, prot, flags);
198 }
199