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