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