1 /* $NetBSD: amdgpufb.c,v 1.5 2022/07/18 23:34:02 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.5 2022/07/18 23:34:02 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_attached:1;
62 };
63
64 static const struct drmfb_params amdgpufb_drmfb_params = {
65 .dp_mmapfb = amdgpufb_drmfb_mmapfb,
66 .dp_mmap = drmfb_pci_mmap,
67 .dp_ioctl = drmfb_pci_ioctl,
68 .dp_is_vga_console = drmfb_pci_is_vga_console,
69 };
70
71 CFATTACH_DECL_NEW(amdgpufb, sizeof(struct amdgpufb_softc),
72 amdgpufb_match, amdgpufb_attach, amdgpufb_detach, NULL);
73
74 static int
amdgpufb_match(device_t parent,cfdata_t match,void * aux)75 amdgpufb_match(device_t parent, cfdata_t match, void *aux)
76 {
77
78 return 1;
79 }
80
81 static void
amdgpufb_attach(device_t parent,device_t self,void * aux)82 amdgpufb_attach(device_t parent, device_t self, void *aux)
83 {
84 struct amdgpufb_softc *const sc = device_private(self);
85 const struct amdgpufb_attach_args *const afa = aux;
86
87 sc->sc_dev = self;
88 sc->sc_afa = *afa;
89 sc->sc_attached = false;
90
91 aprint_naive("\n");
92 aprint_normal("\n");
93
94 amdgpu_task_init(&sc->sc_attach_task, &amdgpufb_attach_task);
95 amdgpu_task_schedule(parent, &sc->sc_attach_task);
96 config_pending_incr(self);
97 }
98
99 static int
amdgpufb_detach(device_t self,int flags)100 amdgpufb_detach(device_t self, int flags)
101 {
102 struct amdgpufb_softc *const sc = device_private(self);
103 int error;
104
105 if (sc->sc_attached) {
106 pmf_device_deregister(self);
107 error = drmfb_detach(&sc->sc_drmfb, flags);
108 if (error) {
109 /* XXX Ugh. */
110 (void)pmf_device_register1(self, NULL, NULL,
111 &amdgpufb_shutdown);
112 return error;
113 }
114 sc->sc_attached = false;
115 }
116
117 return 0;
118 }
119
120 static void
amdgpufb_attach_task(struct amdgpu_task * task)121 amdgpufb_attach_task(struct amdgpu_task *task)
122 {
123 struct amdgpufb_softc *const sc = container_of(task,
124 struct amdgpufb_softc, sc_attach_task);
125 const struct amdgpufb_attach_args *const afa = &sc->sc_afa;
126 const struct drmfb_attach_args da = {
127 .da_dev = sc->sc_dev,
128 .da_fb_helper = afa->afa_fb_helper,
129 .da_fb_sizes = &afa->afa_fb_sizes,
130 .da_fb_vaddr = __UNVOLATILE(afa->afa_fb_ptr),
131 .da_fb_linebytes = afa->afa_fb_linebytes,
132 .da_params = &amdgpufb_drmfb_params,
133 };
134 int error;
135
136 error = drmfb_attach(&sc->sc_drmfb, &da);
137 if (error) {
138 aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
139 error);
140 goto out;
141 }
142
143 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &amdgpufb_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
amdgpufb_shutdown(device_t self,int flags)153 amdgpufb_shutdown(device_t self, int flags)
154 {
155 struct amdgpufb_softc *const sc = device_private(self);
156
157 return drmfb_shutdown(&sc->sc_drmfb, flags);
158 }
159
160 static paddr_t
amdgpufb_drmfb_mmapfb(struct drmfb_softc * drmfb,off_t offset,int prot)161 amdgpufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
162 {
163 struct amdgpufb_softc *const sc = container_of(drmfb,
164 struct amdgpufb_softc, sc_drmfb);
165 struct drm_fb_helper *const helper = sc->sc_afa.afa_fb_helper;
166 struct drm_framebuffer *const fb = helper->fb;
167 struct drm_gem_object *const gobj = fb->obj[0];
168 struct amdgpu_bo *const rbo = gem_to_amdgpu_bo(gobj);
169 const unsigned num_pages __diagused = rbo->tbo.num_pages;
170 int flags = 0;
171
172 KASSERT(0 <= offset);
173 KASSERT(offset < ((uintmax_t)num_pages << PAGE_SHIFT));
174 KASSERT(rbo->tbo.mem.bus.is_iomem);
175
176 if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC))
177 flags |= BUS_SPACE_MAP_PREFETCHABLE;
178
179 return bus_space_mmap(rbo->tbo.bdev->memt, rbo->tbo.mem.bus.base,
180 rbo->tbo.mem.bus.offset + offset, prot, flags);
181 }
182