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