amdgpufb.c revision 1.2 1 /* $NetBSD: amdgpufb.c,v 1.2 2021/12/19 10:32:59 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2018 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: amdgpufb.c,v 1.2 2021/12/19 10:32:59 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/errno.h>
39
40 #include <drm/drmfb.h>
41 #include <drm/drmfb_pci.h>
42
43 #include "amdgpu.h"
44 #include "amdgpu_task.h"
45 #include "amdgpufb.h"
46
47 static int amdgpufb_match(device_t, cfdata_t, void *);
48 static void amdgpufb_attach(device_t, device_t, void *);
49 static int amdgpufb_detach(device_t, int);
50
51 static void amdgpufb_attach_task(struct amdgpu_task *);
52
53 static bool amdgpufb_shutdown(device_t, int);
54 static paddr_t amdgpufb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
55
56 struct amdgpufb_softc {
57 struct drmfb_softc sc_drmfb; /* XXX Must be first. */
58 device_t sc_dev;
59 struct amdgpufb_attach_args sc_afa;
60 struct amdgpu_task sc_attach_task;
61 bool sc_scheduled:1;
62 bool sc_attached:1;
63 };
64
65 static const struct drmfb_params amdgpufb_drmfb_params = {
66 .dp_mmapfb = amdgpufb_drmfb_mmapfb,
67 .dp_mmap = drmfb_pci_mmap,
68 .dp_ioctl = drmfb_pci_ioctl,
69 .dp_is_vga_console = drmfb_pci_is_vga_console,
70 };
71
72 CFATTACH_DECL_NEW(amdgpufb, sizeof(struct amdgpufb_softc),
73 amdgpufb_match, amdgpufb_attach, amdgpufb_detach, NULL);
74
75 static int
76 amdgpufb_match(device_t parent, cfdata_t match, void *aux)
77 {
78
79 return 1;
80 }
81
82 static void
83 amdgpufb_attach(device_t parent, device_t self, void *aux)
84 {
85 struct amdgpufb_softc *const sc = device_private(self);
86 const struct amdgpufb_attach_args *const afa = aux;
87 int error;
88
89 sc->sc_dev = self;
90 sc->sc_afa = *afa;
91 sc->sc_scheduled = false;
92 sc->sc_attached = false;
93
94 aprint_naive("\n");
95 aprint_normal("\n");
96
97 amdgpu_task_init(&sc->sc_attach_task, &amdgpufb_attach_task);
98 error = amdgpu_task_schedule(parent, &sc->sc_attach_task);
99 if (error) {
100 aprint_error_dev(self, "failed to schedule mode set: %d\n",
101 error);
102 goto fail0;
103 }
104 sc->sc_scheduled = true;
105
106 /* Success! */
107 return;
108
109 fail0: return;
110 }
111
112 static int
113 amdgpufb_detach(device_t self, int flags)
114 {
115 struct amdgpufb_softc *const sc = device_private(self);
116 int error;
117
118 if (sc->sc_scheduled)
119 return EBUSY;
120
121 if (sc->sc_attached) {
122 pmf_device_deregister(self);
123 error = drmfb_detach(&sc->sc_drmfb, flags);
124 if (error) {
125 /* XXX Ugh. */
126 (void)pmf_device_register1(self, NULL, NULL,
127 &amdgpufb_shutdown);
128 return error;
129 }
130 sc->sc_attached = false;
131 }
132
133 return 0;
134 }
135
136 static void
137 amdgpufb_attach_task(struct amdgpu_task *task)
138 {
139 struct amdgpufb_softc *const sc = container_of(task,
140 struct amdgpufb_softc, sc_attach_task);
141 const struct amdgpufb_attach_args *const afa = &sc->sc_afa;
142 const struct drmfb_attach_args da = {
143 .da_dev = sc->sc_dev,
144 .da_fb_helper = afa->afa_fb_helper,
145 .da_fb_sizes = &afa->afa_fb_sizes,
146 .da_fb_vaddr = __UNVOLATILE(afa->afa_fb_ptr),
147 .da_fb_linebytes = afa->afa_fb_linebytes,
148 .da_params = &amdgpufb_drmfb_params,
149 };
150 int error;
151
152 error = drmfb_attach(&sc->sc_drmfb, &da);
153 if (error) {
154 aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
155 error);
156 return;
157 }
158
159 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &amdgpufb_shutdown))
160 aprint_error_dev(sc->sc_dev,
161 "failed to register shutdown handler\n");
162
163 sc->sc_attached = true;
164 }
165
166 static bool
167 amdgpufb_shutdown(device_t self, int flags)
168 {
169 struct amdgpufb_softc *const sc = device_private(self);
170
171 return drmfb_shutdown(&sc->sc_drmfb, flags);
172 }
173
174 static paddr_t
175 amdgpufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
176 {
177 struct amdgpufb_softc *const sc = container_of(drmfb,
178 struct amdgpufb_softc, sc_drmfb);
179 struct drm_fb_helper *const helper = sc->sc_afa.afa_fb_helper;
180 struct drm_framebuffer *const fb = helper->fb;
181 struct amdgpu_framebuffer *const rfb = container_of(fb,
182 struct amdgpu_framebuffer, base);
183 struct drm_gem_object *const gobj = rfb->obj;
184 struct amdgpu_bo *const rbo = gem_to_amdgpu_bo(gobj);
185 const unsigned num_pages __diagused = rbo->tbo.num_pages;
186 int flags = 0;
187
188 KASSERT(0 <= offset);
189 KASSERT(offset < ((uintmax_t)num_pages << PAGE_SHIFT));
190 KASSERT(rbo->tbo.mem.bus.is_iomem);
191
192 if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC))
193 flags |= BUS_SPACE_MAP_PREFETCHABLE;
194
195 return bus_space_mmap(rbo->tbo.bdev->memt, rbo->tbo.mem.bus.base,
196 rbo->tbo.mem.bus.offset + offset, prot, flags);
197 }
198