radeondrmkmsfb.c revision 1.1 1 1.1 riastrad /* $NetBSD: radeondrmkmsfb.c,v 1.1 2014/07/25 12:35:03 riastradh 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.1 riastrad __KERNEL_RCSID(0, "$NetBSD: radeondrmkmsfb.c,v 1.1 2014/07/25 12:35:03 riastradh Exp $");
35 1.1 riastrad
36 1.1 riastrad #ifdef _KERNEL_OPT
37 1.1 riastrad #include "vga.h"
38 1.1 riastrad #endif
39 1.1 riastrad
40 1.1 riastrad #include <sys/types.h>
41 1.1 riastrad #include <sys/device.h>
42 1.1 riastrad
43 1.1 riastrad #include <dev/pci/pciio.h>
44 1.1 riastrad #include <dev/pci/pcireg.h>
45 1.1 riastrad #include <dev/pci/pcivar.h>
46 1.1 riastrad
47 1.1 riastrad #include <dev/pci/wsdisplay_pci.h>
48 1.1 riastrad #include <dev/wsfb/genfbvar.h>
49 1.1 riastrad
50 1.1 riastrad #if NVGA > 0
51 1.1 riastrad /*
52 1.1 riastrad * XXX All we really need is vga_is_console from vgavar.h, but the
53 1.1 riastrad * header files are missing their own dependencies, so we need to
54 1.1 riastrad * explicitly drag in the other crap.
55 1.1 riastrad */
56 1.1 riastrad #include <dev/ic/mc6845reg.h>
57 1.1 riastrad #include <dev/ic/pcdisplayvar.h>
58 1.1 riastrad #include <dev/ic/vgareg.h>
59 1.1 riastrad #include <dev/ic/vgavar.h>
60 1.1 riastrad #endif
61 1.1 riastrad
62 1.1 riastrad #include <drm/drmP.h>
63 1.1 riastrad #include <drm/drm_fb_helper.h>
64 1.1 riastrad
65 1.1 riastrad #include <radeon.h>
66 1.1 riastrad #include "radeon_drv.h"
67 1.1 riastrad #include "radeon_task.h"
68 1.1 riastrad #include "radeondrmkmsfb.h"
69 1.1 riastrad
70 1.1 riastrad struct radeonfb_softc {
71 1.1 riastrad /* XXX genfb requires the genfb_softc to be first. */
72 1.1 riastrad struct genfb_softc sc_genfb;
73 1.1 riastrad device_t sc_dev;
74 1.1 riastrad struct radeonfb_attach_args sc_rfa;
75 1.1 riastrad struct radeon_task sc_setconfig_task;
76 1.1 riastrad bool sc_scheduled:1;
77 1.1 riastrad bool sc_attached:1;
78 1.1 riastrad };
79 1.1 riastrad
80 1.1 riastrad static int radeonfb_match(device_t, cfdata_t, void *);
81 1.1 riastrad static void radeonfb_attach(device_t, device_t, void *);
82 1.1 riastrad static int radeonfb_detach(device_t, int);
83 1.1 riastrad
84 1.1 riastrad static void radeonfb_setconfig_task(struct radeon_task *);
85 1.1 riastrad
86 1.1 riastrad static int radeonfb_genfb_ioctl(void *, void *, unsigned long, void *,
87 1.1 riastrad int, struct lwp *);
88 1.1 riastrad static paddr_t radeonfb_genfb_mmap(void *, void *, off_t, int);
89 1.1 riastrad static int radeonfb_genfb_enable_polling(void *);
90 1.1 riastrad static int radeonfb_genfb_disable_polling(void *);
91 1.1 riastrad
92 1.1 riastrad CFATTACH_DECL_NEW(radeondrmkmsfb, sizeof(struct radeonfb_softc),
93 1.1 riastrad radeonfb_match, radeonfb_attach, radeonfb_detach, NULL);
94 1.1 riastrad
95 1.1 riastrad static int
96 1.1 riastrad radeonfb_match(device_t parent, cfdata_t match, void *aux)
97 1.1 riastrad {
98 1.1 riastrad
99 1.1 riastrad return 1;
100 1.1 riastrad }
101 1.1 riastrad
102 1.1 riastrad static void
103 1.1 riastrad radeonfb_attach(device_t parent, device_t self, void *aux)
104 1.1 riastrad {
105 1.1 riastrad struct radeonfb_softc *const sc = device_private(self);
106 1.1 riastrad const struct radeonfb_attach_args *const rfa = aux;
107 1.1 riastrad int error;
108 1.1 riastrad
109 1.1 riastrad sc->sc_dev = self;
110 1.1 riastrad sc->sc_rfa = *rfa;
111 1.1 riastrad sc->sc_scheduled = false;
112 1.1 riastrad sc->sc_attached = false;
113 1.1 riastrad
114 1.1 riastrad radeon_task_init(&sc->sc_setconfig_task, &radeonfb_setconfig_task);
115 1.1 riastrad error = radeon_task_schedule(parent, &sc->sc_setconfig_task);
116 1.1 riastrad if (error) {
117 1.1 riastrad aprint_error_dev(self, "failed to schedule mode set: %d\n",
118 1.1 riastrad error);
119 1.1 riastrad goto fail0;
120 1.1 riastrad }
121 1.1 riastrad sc->sc_scheduled = true;
122 1.1 riastrad
123 1.1 riastrad /* Success! */
124 1.1 riastrad return;
125 1.1 riastrad
126 1.1 riastrad fail0: return;
127 1.1 riastrad }
128 1.1 riastrad
129 1.1 riastrad static int
130 1.1 riastrad radeonfb_detach(device_t self, int flags)
131 1.1 riastrad {
132 1.1 riastrad struct radeonfb_softc *const sc = device_private(self);
133 1.1 riastrad
134 1.1 riastrad if (sc->sc_scheduled)
135 1.1 riastrad return EBUSY;
136 1.1 riastrad
137 1.1 riastrad if (sc->sc_attached) {
138 1.1 riastrad /* XXX genfb detach? Help? */
139 1.1 riastrad sc->sc_attached = false;
140 1.1 riastrad }
141 1.1 riastrad
142 1.1 riastrad return 0;
143 1.1 riastrad }
144 1.1 riastrad
145 1.1 riastrad static void
146 1.1 riastrad radeonfb_setconfig_task(struct radeon_task *task)
147 1.1 riastrad {
148 1.1 riastrad struct radeonfb_softc *const sc = container_of(task,
149 1.1 riastrad struct radeonfb_softc, sc_setconfig_task);
150 1.1 riastrad const prop_dictionary_t dict = device_properties(sc->sc_dev);
151 1.1 riastrad const struct radeonfb_attach_args *const rfa = &sc->sc_rfa;
152 1.1 riastrad const struct drm_fb_helper_surface_size *const sizes =
153 1.1 riastrad &rfa->rfa_fb_sizes;
154 1.1 riastrad enum { CONS_VGA, CONS_GENFB, CONS_NONE } what_was_cons;
155 1.1 riastrad static const struct genfb_ops zero_genfb_ops;
156 1.1 riastrad struct genfb_ops genfb_ops = zero_genfb_ops;
157 1.1 riastrad int error;
158 1.1 riastrad
159 1.1 riastrad KASSERT(sc->sc_scheduled);
160 1.1 riastrad
161 1.1 riastrad /* XXX Ugh... Pass these parameters some other way! */
162 1.1 riastrad prop_dictionary_set_uint32(dict, "width", sizes->fb_width);
163 1.1 riastrad prop_dictionary_set_uint32(dict, "height", sizes->fb_height);
164 1.1 riastrad prop_dictionary_set_uint8(dict, "depth", sizes->surface_bpp);
165 1.1 riastrad prop_dictionary_set_uint16(dict, "linebytes",
166 1.1 riastrad roundup2((sizes->fb_width * howmany(sizes->surface_bpp, 8)), 64));
167 1.1 riastrad prop_dictionary_set_uint32(dict, "address", 0); /* XXX >32-bit */
168 1.1 riastrad CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
169 1.1 riastrad prop_dictionary_set_uint64(dict, "virtual_address",
170 1.1 riastrad (uint64_t)(uintptr_t)rfa->rfa_fb_ptr);
171 1.1 riastrad
172 1.1 riastrad /* XXX Whattakludge! */
173 1.1 riastrad #if NVGA > 0
174 1.1 riastrad if (vga_is_console(rfa->rfa_fb_helper->dev->pdev->pd_pa.pa_iot, -1)) {
175 1.1 riastrad what_was_cons = CONS_VGA;
176 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", true);
177 1.1 riastrad vga_cndetach();
178 1.1 riastrad } else
179 1.1 riastrad #endif
180 1.1 riastrad if (genfb_is_console() && genfb_is_enabled()) {
181 1.1 riastrad what_was_cons = CONS_GENFB;
182 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", true);
183 1.1 riastrad } else {
184 1.1 riastrad what_was_cons = CONS_NONE;
185 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", false);
186 1.1 riastrad }
187 1.1 riastrad
188 1.1 riastrad sc->sc_genfb.sc_dev = sc->sc_dev;
189 1.1 riastrad genfb_init(&sc->sc_genfb);
190 1.1 riastrad genfb_ops.genfb_ioctl = radeonfb_genfb_ioctl;
191 1.1 riastrad genfb_ops.genfb_mmap = radeonfb_genfb_mmap;
192 1.1 riastrad genfb_ops.genfb_enable_polling = radeonfb_genfb_enable_polling;
193 1.1 riastrad genfb_ops.genfb_disable_polling = radeonfb_genfb_disable_polling;
194 1.1 riastrad
195 1.1 riastrad error = genfb_attach(&sc->sc_genfb, &genfb_ops);
196 1.1 riastrad if (error) {
197 1.1 riastrad aprint_error_dev(sc->sc_dev, "failed to attach genfb: %d\n",
198 1.1 riastrad error);
199 1.1 riastrad goto fail0;
200 1.1 riastrad }
201 1.1 riastrad sc->sc_attached = true;
202 1.1 riastrad
203 1.1 riastrad drm_fb_helper_set_config(sc->sc_rfa.rfa_fb_helper);
204 1.1 riastrad
205 1.1 riastrad /* Success! */
206 1.1 riastrad sc->sc_scheduled = false;
207 1.1 riastrad return;
208 1.1 riastrad
209 1.1 riastrad fail0: /* XXX Restore console... */
210 1.1 riastrad switch (what_was_cons) {
211 1.1 riastrad case CONS_VGA:
212 1.1 riastrad break;
213 1.1 riastrad case CONS_GENFB:
214 1.1 riastrad break;
215 1.1 riastrad case CONS_NONE:
216 1.1 riastrad break;
217 1.1 riastrad default:
218 1.1 riastrad break;
219 1.1 riastrad }
220 1.1 riastrad }
221 1.1 riastrad
222 1.1 riastrad static int
223 1.1 riastrad radeonfb_genfb_ioctl(void *v, void *vs, unsigned long cmd, void *data, int flag,
224 1.1 riastrad struct lwp *l)
225 1.1 riastrad {
226 1.1 riastrad struct genfb_softc *const genfb = v;
227 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
228 1.1 riastrad struct radeonfb_softc, sc_genfb);
229 1.1 riastrad struct drm_device *const dev = sc->sc_rfa.rfa_fb_helper->dev;
230 1.1 riastrad const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
231 1.1 riastrad
232 1.1 riastrad switch (cmd) {
233 1.1 riastrad case WSDISPLAYIO_GTYPE:
234 1.1 riastrad *(unsigned int *)data = WSDISPLAY_TYPE_PCIVGA;
235 1.1 riastrad return 0;
236 1.1 riastrad
237 1.1 riastrad /* PCI config read/write passthrough. */
238 1.1 riastrad case PCI_IOC_CFGREAD:
239 1.1 riastrad case PCI_IOC_CFGWRITE:
240 1.1 riastrad return pci_devioctl(pa->pa_pc, pa->pa_tag, cmd, data, flag, l);
241 1.1 riastrad
242 1.1 riastrad case WSDISPLAYIO_GET_BUSID:
243 1.1 riastrad return wsdisplayio_busid_pci(dev->dev, pa->pa_pc, pa->pa_tag,
244 1.1 riastrad data);
245 1.1 riastrad
246 1.1 riastrad default:
247 1.1 riastrad return EPASSTHROUGH;
248 1.1 riastrad }
249 1.1 riastrad }
250 1.1 riastrad
251 1.1 riastrad static paddr_t
252 1.1 riastrad radeonfb_genfb_mmap(void *v, void *vs, off_t offset, int prot)
253 1.1 riastrad {
254 1.1 riastrad struct genfb_softc *const genfb = v;
255 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
256 1.1 riastrad struct radeonfb_softc, sc_genfb);
257 1.1 riastrad struct drm_fb_helper *const helper = sc->sc_rfa.rfa_fb_helper;
258 1.1 riastrad struct drm_framebuffer *const fb = helper->fb;
259 1.1 riastrad struct radeon_framebuffer *const rfb = container_of(fb,
260 1.1 riastrad struct radeon_framebuffer, base);
261 1.1 riastrad struct drm_gem_object *const gobj = rfb->obj;
262 1.1 riastrad struct radeon_bo *const rbo = gem_to_radeon_bo(gobj);
263 1.1 riastrad struct drm_device *const dev = helper->dev;
264 1.1 riastrad const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
265 1.1 riastrad unsigned int i;
266 1.1 riastrad
267 1.1 riastrad if (offset < 0)
268 1.1 riastrad return -1;
269 1.1 riastrad
270 1.1 riastrad /* Treat low memory as the framebuffer itself. */
271 1.1 riastrad if (offset < genfb->sc_fbsize) {
272 1.1 riastrad const unsigned num_pages __diagused = rbo->tbo.num_pages;
273 1.1 riastrad bus_addr_t addr;
274 1.1 riastrad int flags = 0;
275 1.1 riastrad
276 1.1 riastrad KASSERT(genfb->sc_fbsize == (num_pages << PAGE_SHIFT));
277 1.1 riastrad KASSERT(num_pages == rbo->tbo.ttm->num_pages);
278 1.1 riastrad addr = page_to_phys(rbo->tbo.ttm->pages[offset >> PAGE_SHIFT]);
279 1.1 riastrad /* XXX CACHEABLE/ PREFETCHABLE? WC? WB? */
280 1.1 riastrad if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_CACHED))
281 1.1 riastrad flags |= BUS_SPACE_MAP_PREFETCHABLE;
282 1.1 riastrad /*
283 1.1 riastrad * XXX Urk. We assume bus_space_mmap can cope with
284 1.1 riastrad * normal system RAM addresses.
285 1.1 riastrad */
286 1.1 riastrad return bus_space_mmap(rbo->tbo.bdev->memt, addr, 0, prot,
287 1.1 riastrad flags);
288 1.1 riastrad }
289 1.1 riastrad
290 1.1 riastrad /* XXX Cargo-culted from genfb_pci. */
291 1.1 riastrad if (kauth_authorize_machdep(kauth_cred_get(),
292 1.1 riastrad KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
293 1.1 riastrad aprint_normal_dev(dev->dev, "mmap at %"PRIxMAX" rejected\n",
294 1.1 riastrad (uintmax_t)offset);
295 1.1 riastrad return -1;
296 1.1 riastrad }
297 1.1 riastrad
298 1.1 riastrad for (i = 0; PCI_BAR(i) <= PCI_MAPREG_ROM; i++) {
299 1.1 riastrad pcireg_t type;
300 1.1 riastrad bus_addr_t addr;
301 1.1 riastrad bus_size_t size;
302 1.1 riastrad int flags;
303 1.1 riastrad
304 1.1 riastrad /* Interrogate the BAR. */
305 1.1 riastrad if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, PCI_BAR(i),
306 1.1 riastrad &type))
307 1.1 riastrad continue;
308 1.1 riastrad if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM)
309 1.1 riastrad continue;
310 1.1 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(i), type,
311 1.1 riastrad &addr, &size, &flags))
312 1.1 riastrad continue;
313 1.1 riastrad
314 1.1 riastrad /* Try to map it if it's in range. */
315 1.1 riastrad if ((addr <= offset) && (offset < (addr + size)))
316 1.1 riastrad return bus_space_mmap(pa->pa_memt, addr,
317 1.1 riastrad (offset - addr), prot, flags);
318 1.1 riastrad
319 1.1 riastrad /* Skip a slot if this was a 64-bit BAR. */
320 1.1 riastrad if ((PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM) &&
321 1.1 riastrad (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT))
322 1.1 riastrad i += 1;
323 1.1 riastrad }
324 1.1 riastrad
325 1.1 riastrad /* Failure! */
326 1.1 riastrad return -1;
327 1.1 riastrad }
328 1.1 riastrad
329 1.1 riastrad static int
330 1.1 riastrad radeonfb_genfb_enable_polling(void *cookie)
331 1.1 riastrad {
332 1.1 riastrad struct genfb_softc *const genfb = cookie;
333 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
334 1.1 riastrad struct radeonfb_softc, sc_genfb);
335 1.1 riastrad
336 1.1 riastrad return drm_fb_helper_debug_enter_fb(sc->sc_rfa.rfa_fb_helper);
337 1.1 riastrad }
338 1.1 riastrad
339 1.1 riastrad static int
340 1.1 riastrad radeonfb_genfb_disable_polling(void *cookie)
341 1.1 riastrad {
342 1.1 riastrad struct genfb_softc *const genfb = cookie;
343 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
344 1.1 riastrad struct radeonfb_softc, sc_genfb);
345 1.1 riastrad
346 1.1 riastrad return drm_fb_helper_debug_leave_fb(sc->sc_rfa.rfa_fb_helper);
347 1.1 riastrad }
348