radeondrmkmsfb.c revision 1.2 1 1.2 riastrad /* $NetBSD: radeondrmkmsfb.c,v 1.2 2014/07/25 16:35:43 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.2 riastrad __KERNEL_RCSID(0, "$NetBSD: radeondrmkmsfb.c,v 1.2 2014/07/25 16:35:43 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.2 riastrad aprint_naive("\n");
115 1.2 riastrad aprint_normal("\n");
116 1.2 riastrad
117 1.1 riastrad radeon_task_init(&sc->sc_setconfig_task, &radeonfb_setconfig_task);
118 1.1 riastrad error = radeon_task_schedule(parent, &sc->sc_setconfig_task);
119 1.1 riastrad if (error) {
120 1.1 riastrad aprint_error_dev(self, "failed to schedule mode set: %d\n",
121 1.1 riastrad error);
122 1.1 riastrad goto fail0;
123 1.1 riastrad }
124 1.1 riastrad sc->sc_scheduled = true;
125 1.1 riastrad
126 1.1 riastrad /* Success! */
127 1.1 riastrad return;
128 1.1 riastrad
129 1.1 riastrad fail0: return;
130 1.1 riastrad }
131 1.1 riastrad
132 1.1 riastrad static int
133 1.1 riastrad radeonfb_detach(device_t self, int flags)
134 1.1 riastrad {
135 1.1 riastrad struct radeonfb_softc *const sc = device_private(self);
136 1.1 riastrad
137 1.1 riastrad if (sc->sc_scheduled)
138 1.1 riastrad return EBUSY;
139 1.1 riastrad
140 1.1 riastrad if (sc->sc_attached) {
141 1.1 riastrad /* XXX genfb detach? Help? */
142 1.1 riastrad sc->sc_attached = false;
143 1.1 riastrad }
144 1.1 riastrad
145 1.1 riastrad return 0;
146 1.1 riastrad }
147 1.1 riastrad
148 1.1 riastrad static void
149 1.1 riastrad radeonfb_setconfig_task(struct radeon_task *task)
150 1.1 riastrad {
151 1.1 riastrad struct radeonfb_softc *const sc = container_of(task,
152 1.1 riastrad struct radeonfb_softc, sc_setconfig_task);
153 1.1 riastrad const prop_dictionary_t dict = device_properties(sc->sc_dev);
154 1.1 riastrad const struct radeonfb_attach_args *const rfa = &sc->sc_rfa;
155 1.1 riastrad const struct drm_fb_helper_surface_size *const sizes =
156 1.1 riastrad &rfa->rfa_fb_sizes;
157 1.1 riastrad enum { CONS_VGA, CONS_GENFB, CONS_NONE } what_was_cons;
158 1.1 riastrad static const struct genfb_ops zero_genfb_ops;
159 1.1 riastrad struct genfb_ops genfb_ops = zero_genfb_ops;
160 1.1 riastrad int error;
161 1.1 riastrad
162 1.1 riastrad KASSERT(sc->sc_scheduled);
163 1.1 riastrad
164 1.1 riastrad /* XXX Ugh... Pass these parameters some other way! */
165 1.1 riastrad prop_dictionary_set_uint32(dict, "width", sizes->fb_width);
166 1.1 riastrad prop_dictionary_set_uint32(dict, "height", sizes->fb_height);
167 1.1 riastrad prop_dictionary_set_uint8(dict, "depth", sizes->surface_bpp);
168 1.1 riastrad prop_dictionary_set_uint16(dict, "linebytes",
169 1.1 riastrad roundup2((sizes->fb_width * howmany(sizes->surface_bpp, 8)), 64));
170 1.1 riastrad prop_dictionary_set_uint32(dict, "address", 0); /* XXX >32-bit */
171 1.1 riastrad CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
172 1.1 riastrad prop_dictionary_set_uint64(dict, "virtual_address",
173 1.1 riastrad (uint64_t)(uintptr_t)rfa->rfa_fb_ptr);
174 1.1 riastrad
175 1.1 riastrad /* XXX Whattakludge! */
176 1.1 riastrad #if NVGA > 0
177 1.1 riastrad if (vga_is_console(rfa->rfa_fb_helper->dev->pdev->pd_pa.pa_iot, -1)) {
178 1.1 riastrad what_was_cons = CONS_VGA;
179 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", true);
180 1.1 riastrad vga_cndetach();
181 1.1 riastrad } else
182 1.1 riastrad #endif
183 1.1 riastrad if (genfb_is_console() && genfb_is_enabled()) {
184 1.1 riastrad what_was_cons = CONS_GENFB;
185 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", true);
186 1.1 riastrad } else {
187 1.1 riastrad what_was_cons = CONS_NONE;
188 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", false);
189 1.1 riastrad }
190 1.1 riastrad
191 1.1 riastrad sc->sc_genfb.sc_dev = sc->sc_dev;
192 1.1 riastrad genfb_init(&sc->sc_genfb);
193 1.1 riastrad genfb_ops.genfb_ioctl = radeonfb_genfb_ioctl;
194 1.1 riastrad genfb_ops.genfb_mmap = radeonfb_genfb_mmap;
195 1.1 riastrad genfb_ops.genfb_enable_polling = radeonfb_genfb_enable_polling;
196 1.1 riastrad genfb_ops.genfb_disable_polling = radeonfb_genfb_disable_polling;
197 1.1 riastrad
198 1.1 riastrad error = genfb_attach(&sc->sc_genfb, &genfb_ops);
199 1.1 riastrad if (error) {
200 1.1 riastrad aprint_error_dev(sc->sc_dev, "failed to attach genfb: %d\n",
201 1.1 riastrad error);
202 1.1 riastrad goto fail0;
203 1.1 riastrad }
204 1.1 riastrad sc->sc_attached = true;
205 1.1 riastrad
206 1.1 riastrad drm_fb_helper_set_config(sc->sc_rfa.rfa_fb_helper);
207 1.1 riastrad
208 1.1 riastrad /* Success! */
209 1.1 riastrad sc->sc_scheduled = false;
210 1.1 riastrad return;
211 1.1 riastrad
212 1.1 riastrad fail0: /* XXX Restore console... */
213 1.1 riastrad switch (what_was_cons) {
214 1.1 riastrad case CONS_VGA:
215 1.1 riastrad break;
216 1.1 riastrad case CONS_GENFB:
217 1.1 riastrad break;
218 1.1 riastrad case CONS_NONE:
219 1.1 riastrad break;
220 1.1 riastrad default:
221 1.1 riastrad break;
222 1.1 riastrad }
223 1.1 riastrad }
224 1.1 riastrad
225 1.1 riastrad static int
226 1.1 riastrad radeonfb_genfb_ioctl(void *v, void *vs, unsigned long cmd, void *data, int flag,
227 1.1 riastrad struct lwp *l)
228 1.1 riastrad {
229 1.1 riastrad struct genfb_softc *const genfb = v;
230 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
231 1.1 riastrad struct radeonfb_softc, sc_genfb);
232 1.1 riastrad struct drm_device *const dev = sc->sc_rfa.rfa_fb_helper->dev;
233 1.1 riastrad const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
234 1.1 riastrad
235 1.1 riastrad switch (cmd) {
236 1.1 riastrad case WSDISPLAYIO_GTYPE:
237 1.1 riastrad *(unsigned int *)data = WSDISPLAY_TYPE_PCIVGA;
238 1.1 riastrad return 0;
239 1.1 riastrad
240 1.1 riastrad /* PCI config read/write passthrough. */
241 1.1 riastrad case PCI_IOC_CFGREAD:
242 1.1 riastrad case PCI_IOC_CFGWRITE:
243 1.1 riastrad return pci_devioctl(pa->pa_pc, pa->pa_tag, cmd, data, flag, l);
244 1.1 riastrad
245 1.1 riastrad case WSDISPLAYIO_GET_BUSID:
246 1.1 riastrad return wsdisplayio_busid_pci(dev->dev, pa->pa_pc, pa->pa_tag,
247 1.1 riastrad data);
248 1.1 riastrad
249 1.1 riastrad default:
250 1.1 riastrad return EPASSTHROUGH;
251 1.1 riastrad }
252 1.1 riastrad }
253 1.1 riastrad
254 1.1 riastrad static paddr_t
255 1.1 riastrad radeonfb_genfb_mmap(void *v, void *vs, off_t offset, int prot)
256 1.1 riastrad {
257 1.1 riastrad struct genfb_softc *const genfb = v;
258 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
259 1.1 riastrad struct radeonfb_softc, sc_genfb);
260 1.1 riastrad struct drm_fb_helper *const helper = sc->sc_rfa.rfa_fb_helper;
261 1.1 riastrad struct drm_framebuffer *const fb = helper->fb;
262 1.1 riastrad struct radeon_framebuffer *const rfb = container_of(fb,
263 1.1 riastrad struct radeon_framebuffer, base);
264 1.1 riastrad struct drm_gem_object *const gobj = rfb->obj;
265 1.1 riastrad struct radeon_bo *const rbo = gem_to_radeon_bo(gobj);
266 1.1 riastrad struct drm_device *const dev = helper->dev;
267 1.1 riastrad const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
268 1.1 riastrad unsigned int i;
269 1.1 riastrad
270 1.1 riastrad if (offset < 0)
271 1.1 riastrad return -1;
272 1.1 riastrad
273 1.1 riastrad /* Treat low memory as the framebuffer itself. */
274 1.1 riastrad if (offset < genfb->sc_fbsize) {
275 1.1 riastrad const unsigned num_pages __diagused = rbo->tbo.num_pages;
276 1.1 riastrad bus_addr_t addr;
277 1.1 riastrad int flags = 0;
278 1.1 riastrad
279 1.1 riastrad KASSERT(genfb->sc_fbsize == (num_pages << PAGE_SHIFT));
280 1.1 riastrad KASSERT(num_pages == rbo->tbo.ttm->num_pages);
281 1.1 riastrad addr = page_to_phys(rbo->tbo.ttm->pages[offset >> PAGE_SHIFT]);
282 1.1 riastrad /* XXX CACHEABLE/ PREFETCHABLE? WC? WB? */
283 1.1 riastrad if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_CACHED))
284 1.1 riastrad flags |= BUS_SPACE_MAP_PREFETCHABLE;
285 1.1 riastrad /*
286 1.1 riastrad * XXX Urk. We assume bus_space_mmap can cope with
287 1.1 riastrad * normal system RAM addresses.
288 1.1 riastrad */
289 1.1 riastrad return bus_space_mmap(rbo->tbo.bdev->memt, addr, 0, prot,
290 1.1 riastrad flags);
291 1.1 riastrad }
292 1.1 riastrad
293 1.1 riastrad /* XXX Cargo-culted from genfb_pci. */
294 1.1 riastrad if (kauth_authorize_machdep(kauth_cred_get(),
295 1.1 riastrad KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
296 1.1 riastrad aprint_normal_dev(dev->dev, "mmap at %"PRIxMAX" rejected\n",
297 1.1 riastrad (uintmax_t)offset);
298 1.1 riastrad return -1;
299 1.1 riastrad }
300 1.1 riastrad
301 1.1 riastrad for (i = 0; PCI_BAR(i) <= PCI_MAPREG_ROM; i++) {
302 1.1 riastrad pcireg_t type;
303 1.1 riastrad bus_addr_t addr;
304 1.1 riastrad bus_size_t size;
305 1.1 riastrad int flags;
306 1.1 riastrad
307 1.1 riastrad /* Interrogate the BAR. */
308 1.1 riastrad if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, PCI_BAR(i),
309 1.1 riastrad &type))
310 1.1 riastrad continue;
311 1.1 riastrad if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM)
312 1.1 riastrad continue;
313 1.1 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(i), type,
314 1.1 riastrad &addr, &size, &flags))
315 1.1 riastrad continue;
316 1.1 riastrad
317 1.1 riastrad /* Try to map it if it's in range. */
318 1.1 riastrad if ((addr <= offset) && (offset < (addr + size)))
319 1.1 riastrad return bus_space_mmap(pa->pa_memt, addr,
320 1.1 riastrad (offset - addr), prot, flags);
321 1.1 riastrad
322 1.1 riastrad /* Skip a slot if this was a 64-bit BAR. */
323 1.1 riastrad if ((PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM) &&
324 1.1 riastrad (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT))
325 1.1 riastrad i += 1;
326 1.1 riastrad }
327 1.1 riastrad
328 1.1 riastrad /* Failure! */
329 1.1 riastrad return -1;
330 1.1 riastrad }
331 1.1 riastrad
332 1.1 riastrad static int
333 1.1 riastrad radeonfb_genfb_enable_polling(void *cookie)
334 1.1 riastrad {
335 1.1 riastrad struct genfb_softc *const genfb = cookie;
336 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
337 1.1 riastrad struct radeonfb_softc, sc_genfb);
338 1.1 riastrad
339 1.1 riastrad return drm_fb_helper_debug_enter_fb(sc->sc_rfa.rfa_fb_helper);
340 1.1 riastrad }
341 1.1 riastrad
342 1.1 riastrad static int
343 1.1 riastrad radeonfb_genfb_disable_polling(void *cookie)
344 1.1 riastrad {
345 1.1 riastrad struct genfb_softc *const genfb = cookie;
346 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
347 1.1 riastrad struct radeonfb_softc, sc_genfb);
348 1.1 riastrad
349 1.1 riastrad return drm_fb_helper_debug_leave_fb(sc->sc_rfa.rfa_fb_helper);
350 1.1 riastrad }
351