amdgpu_pci.c revision 1.12 1 1.12 riastrad /* $NetBSD: amdgpu_pci.c,v 1.12 2023/08/07 16:34:47 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2018 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 #include <sys/cdefs.h>
33 1.12 riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_pci.c,v 1.12 2023/08/07 16:34:47 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.10 riastrad #include <sys/atomic.h>
37 1.1 riastrad #include <sys/queue.h>
38 1.1 riastrad #include <sys/systm.h>
39 1.1 riastrad #include <sys/workqueue.h>
40 1.1 riastrad
41 1.9 riastrad #include <dev/pci/pcivar.h>
42 1.9 riastrad
43 1.9 riastrad #include <linux/pci.h>
44 1.9 riastrad
45 1.9 riastrad #include <drm/drm_device.h>
46 1.9 riastrad #include <drm/drm_drv.h>
47 1.9 riastrad #include <drm/drm_fb_helper.h>
48 1.12 riastrad #include <drm/drm_ioctl.h>
49 1.9 riastrad #include <drm/drm_pci.h>
50 1.9 riastrad
51 1.1 riastrad #include <amdgpu.h>
52 1.1 riastrad #include "amdgpu_drv.h"
53 1.1 riastrad #include "amdgpu_task.h"
54 1.1 riastrad
55 1.6 riastrad struct drm_device;
56 1.6 riastrad
57 1.1 riastrad SIMPLEQ_HEAD(amdgpu_task_head, amdgpu_task);
58 1.1 riastrad
59 1.1 riastrad struct amdgpu_softc {
60 1.1 riastrad device_t sc_dev;
61 1.1 riastrad struct pci_attach_args sc_pa;
62 1.10 riastrad struct lwp *sc_task_thread;
63 1.10 riastrad struct amdgpu_task_head sc_tasks;
64 1.10 riastrad struct workqueue *sc_task_wq;
65 1.1 riastrad struct drm_device *sc_drm_dev;
66 1.1 riastrad struct pci_dev sc_pci_dev;
67 1.7 riastrad bool sc_pci_attached;
68 1.7 riastrad bool sc_dev_registered;
69 1.1 riastrad };
70 1.1 riastrad
71 1.1 riastrad static bool amdgpu_pci_lookup(const struct pci_attach_args *,
72 1.1 riastrad unsigned long *);
73 1.1 riastrad
74 1.1 riastrad static int amdgpu_match(device_t, cfdata_t, void *);
75 1.1 riastrad static void amdgpu_attach(device_t, device_t, void *);
76 1.1 riastrad static void amdgpu_attach_real(device_t);
77 1.1 riastrad static int amdgpu_detach(device_t, int);
78 1.1 riastrad static bool amdgpu_do_suspend(device_t, const pmf_qual_t *);
79 1.1 riastrad static bool amdgpu_do_resume(device_t, const pmf_qual_t *);
80 1.1 riastrad
81 1.1 riastrad static void amdgpu_task_work(struct work *, void *);
82 1.1 riastrad
83 1.1 riastrad CFATTACH_DECL_NEW(amdgpu, sizeof(struct amdgpu_softc),
84 1.1 riastrad amdgpu_match, amdgpu_attach, amdgpu_detach, NULL);
85 1.1 riastrad
86 1.1 riastrad /* XXX Kludge to get these from amdgpu_drv.c. */
87 1.1 riastrad extern struct drm_driver *const amdgpu_drm_driver;
88 1.1 riastrad extern const struct pci_device_id *const amdgpu_device_ids;
89 1.1 riastrad extern const size_t amdgpu_n_device_ids;
90 1.1 riastrad
91 1.1 riastrad static bool
92 1.1 riastrad amdgpu_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
93 1.1 riastrad {
94 1.1 riastrad size_t i;
95 1.1 riastrad
96 1.1 riastrad for (i = 0; i < amdgpu_n_device_ids; i++) {
97 1.1 riastrad if ((PCI_VENDOR(pa->pa_id) == amdgpu_device_ids[i].vendor) &&
98 1.1 riastrad (PCI_PRODUCT(pa->pa_id) == amdgpu_device_ids[i].device))
99 1.1 riastrad break;
100 1.1 riastrad }
101 1.1 riastrad
102 1.1 riastrad /* Did we find it? */
103 1.1 riastrad if (i == amdgpu_n_device_ids)
104 1.1 riastrad return false;
105 1.1 riastrad
106 1.1 riastrad if (flags)
107 1.1 riastrad *flags = amdgpu_device_ids[i].driver_data;
108 1.1 riastrad return true;
109 1.1 riastrad }
110 1.1 riastrad
111 1.1 riastrad static int
112 1.1 riastrad amdgpu_match(device_t parent, cfdata_t match, void *aux)
113 1.1 riastrad {
114 1.1 riastrad extern int amdgpu_guarantee_initialized(void);
115 1.1 riastrad const struct pci_attach_args *const pa = aux;
116 1.1 riastrad int error;
117 1.1 riastrad
118 1.1 riastrad error = amdgpu_guarantee_initialized();
119 1.1 riastrad if (error) {
120 1.1 riastrad aprint_error("amdgpu: failed to initialize: %d\n", error);
121 1.1 riastrad return 0;
122 1.1 riastrad }
123 1.1 riastrad
124 1.1 riastrad if (!amdgpu_pci_lookup(pa, NULL))
125 1.1 riastrad return 0;
126 1.1 riastrad
127 1.5 riastrad return 7; /* beat genfb_pci and radeon */
128 1.1 riastrad }
129 1.1 riastrad
130 1.1 riastrad static void
131 1.1 riastrad amdgpu_attach(device_t parent, device_t self, void *aux)
132 1.1 riastrad {
133 1.1 riastrad struct amdgpu_softc *const sc = device_private(self);
134 1.1 riastrad const struct pci_attach_args *const pa = aux;
135 1.10 riastrad int error;
136 1.1 riastrad
137 1.1 riastrad pci_aprint_devinfo(pa, NULL);
138 1.1 riastrad
139 1.10 riastrad /* Initialize the Linux PCI device descriptor. */
140 1.10 riastrad linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
141 1.10 riastrad
142 1.10 riastrad sc->sc_dev = self;
143 1.10 riastrad sc->sc_pa = *pa;
144 1.10 riastrad sc->sc_task_thread = NULL;
145 1.10 riastrad SIMPLEQ_INIT(&sc->sc_tasks);
146 1.10 riastrad error = workqueue_create(&sc->sc_task_wq, "amdgpufb",
147 1.10 riastrad &amdgpu_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
148 1.10 riastrad if (error) {
149 1.10 riastrad aprint_error_dev(self, "unable to create workqueue: %d\n",
150 1.10 riastrad error);
151 1.10 riastrad sc->sc_task_wq = NULL;
152 1.10 riastrad return;
153 1.10 riastrad }
154 1.1 riastrad
155 1.1 riastrad /*
156 1.10 riastrad * Defer the remainder of initialization until we have mounted
157 1.10 riastrad * the root file system and can load firmware images.
158 1.1 riastrad */
159 1.1 riastrad config_mountroot(self, &amdgpu_attach_real);
160 1.1 riastrad }
161 1.1 riastrad
162 1.1 riastrad static void
163 1.1 riastrad amdgpu_attach_real(device_t self)
164 1.1 riastrad {
165 1.1 riastrad struct amdgpu_softc *const sc = device_private(self);
166 1.1 riastrad const struct pci_attach_args *const pa = &sc->sc_pa;
167 1.1 riastrad bool ok __diagused;
168 1.1 riastrad unsigned long flags = 0; /* XXXGCC */
169 1.1 riastrad int error;
170 1.1 riastrad
171 1.1 riastrad ok = amdgpu_pci_lookup(pa, &flags);
172 1.1 riastrad KASSERT(ok);
173 1.1 riastrad
174 1.10 riastrad /*
175 1.10 riastrad * Cause any tasks issued synchronously during attach to be
176 1.10 riastrad * processed at the end of this function.
177 1.10 riastrad */
178 1.10 riastrad sc->sc_task_thread = curlwp;
179 1.2 riastrad
180 1.7 riastrad sc->sc_drm_dev = drm_dev_alloc(amdgpu_drm_driver, self);
181 1.7 riastrad if (IS_ERR(sc->sc_drm_dev)) {
182 1.7 riastrad aprint_error_dev(self, "unable to create drm device: %ld\n",
183 1.7 riastrad PTR_ERR(sc->sc_drm_dev));
184 1.7 riastrad sc->sc_drm_dev = NULL;
185 1.7 riastrad goto out;
186 1.7 riastrad }
187 1.7 riastrad
188 1.1 riastrad /* XXX errno Linux->NetBSD */
189 1.8 riastrad error = -drm_pci_attach(sc->sc_drm_dev, &sc->sc_pci_dev);
190 1.1 riastrad if (error) {
191 1.1 riastrad aprint_error_dev(self, "unable to attach drm: %d\n", error);
192 1.1 riastrad goto out;
193 1.1 riastrad }
194 1.7 riastrad sc->sc_pci_attached = true;
195 1.7 riastrad
196 1.7 riastrad /* XXX errno Linux->NetBSD */
197 1.7 riastrad error = -drm_dev_register(sc->sc_drm_dev, flags);
198 1.7 riastrad if (error) {
199 1.7 riastrad aprint_error_dev(self, "unable to register drm: %d\n", error);
200 1.10 riastrad goto out;
201 1.7 riastrad }
202 1.7 riastrad sc->sc_dev_registered = true;
203 1.1 riastrad
204 1.10 riastrad if (!pmf_device_register(self, &amdgpu_do_suspend, &amdgpu_do_resume))
205 1.10 riastrad aprint_error_dev(self, "unable to establish power handler\n");
206 1.10 riastrad
207 1.10 riastrad /*
208 1.10 riastrad * Process asynchronous tasks queued synchronously during
209 1.10 riastrad * attach. This will be for display detection to attach a
210 1.10 riastrad * framebuffer, so we have the opportunity for a console device
211 1.10 riastrad * to attach before autoconf has completed, in time for init(8)
212 1.10 riastrad * to find that console without panicking.
213 1.10 riastrad */
214 1.10 riastrad while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
215 1.10 riastrad struct amdgpu_task *const task = SIMPLEQ_FIRST(&sc->sc_tasks);
216 1.1 riastrad
217 1.10 riastrad SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, rt_u.queue);
218 1.1 riastrad (*task->rt_fn)(task);
219 1.1 riastrad }
220 1.1 riastrad
221 1.10 riastrad out: /* Cause any subesquent tasks to be processed by the workqueue. */
222 1.10 riastrad atomic_store_relaxed(&sc->sc_task_thread, NULL);
223 1.1 riastrad }
224 1.1 riastrad
225 1.1 riastrad static int
226 1.1 riastrad amdgpu_detach(device_t self, int flags)
227 1.1 riastrad {
228 1.1 riastrad struct amdgpu_softc *const sc = device_private(self);
229 1.1 riastrad int error;
230 1.1 riastrad
231 1.1 riastrad /* XXX Check for in-use before tearing it all down... */
232 1.1 riastrad error = config_detach_children(self, flags);
233 1.1 riastrad if (error)
234 1.1 riastrad return error;
235 1.1 riastrad
236 1.10 riastrad KASSERT(sc->sc_task_thread == NULL);
237 1.10 riastrad KASSERT(SIMPLEQ_EMPTY(&sc->sc_tasks));
238 1.10 riastrad
239 1.10 riastrad pmf_device_deregister(self);
240 1.10 riastrad if (sc->sc_dev_registered)
241 1.10 riastrad drm_dev_unregister(sc->sc_drm_dev);
242 1.10 riastrad if (sc->sc_pci_attached)
243 1.10 riastrad drm_pci_detach(sc->sc_drm_dev);
244 1.10 riastrad if (sc->sc_drm_dev) {
245 1.10 riastrad drm_dev_put(sc->sc_drm_dev);
246 1.10 riastrad sc->sc_drm_dev = NULL;
247 1.1 riastrad }
248 1.10 riastrad if (sc->sc_task_wq) {
249 1.10 riastrad workqueue_destroy(sc->sc_task_wq);
250 1.10 riastrad sc->sc_task_wq = NULL;
251 1.10 riastrad }
252 1.10 riastrad linux_pci_dev_destroy(&sc->sc_pci_dev);
253 1.1 riastrad
254 1.1 riastrad return 0;
255 1.1 riastrad }
256 1.1 riastrad
257 1.1 riastrad static bool
258 1.1 riastrad amdgpu_do_suspend(device_t self, const pmf_qual_t *qual)
259 1.1 riastrad {
260 1.1 riastrad struct amdgpu_softc *const sc = device_private(self);
261 1.1 riastrad struct drm_device *const dev = sc->sc_drm_dev;
262 1.1 riastrad int ret;
263 1.1 riastrad
264 1.12 riastrad drm_suspend_ioctl(dev);
265 1.12 riastrad
266 1.9 riastrad ret = amdgpu_device_suspend(dev, /*fbcon*/true);
267 1.1 riastrad if (ret)
268 1.1 riastrad return false;
269 1.1 riastrad
270 1.1 riastrad return true;
271 1.1 riastrad }
272 1.1 riastrad
273 1.1 riastrad static bool
274 1.1 riastrad amdgpu_do_resume(device_t self, const pmf_qual_t *qual)
275 1.1 riastrad {
276 1.1 riastrad struct amdgpu_softc *const sc = device_private(self);
277 1.1 riastrad struct drm_device *const dev = sc->sc_drm_dev;
278 1.1 riastrad int ret;
279 1.1 riastrad
280 1.9 riastrad ret = amdgpu_device_resume(dev, /*fbcon*/true);
281 1.1 riastrad if (ret)
282 1.12 riastrad goto out;
283 1.1 riastrad
284 1.12 riastrad out: drm_resume_ioctl(dev);
285 1.12 riastrad return ret == 0;
286 1.1 riastrad }
287 1.1 riastrad
288 1.1 riastrad static void
289 1.1 riastrad amdgpu_task_work(struct work *work, void *cookie __unused)
290 1.1 riastrad {
291 1.1 riastrad struct amdgpu_task *const task = container_of(work, struct amdgpu_task,
292 1.1 riastrad rt_u.work);
293 1.1 riastrad
294 1.1 riastrad (*task->rt_fn)(task);
295 1.1 riastrad }
296 1.1 riastrad
297 1.11 riastrad void
298 1.1 riastrad amdgpu_task_schedule(device_t self, struct amdgpu_task *task)
299 1.1 riastrad {
300 1.1 riastrad struct amdgpu_softc *const sc = device_private(self);
301 1.1 riastrad
302 1.10 riastrad if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
303 1.10 riastrad SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, rt_u.queue);
304 1.10 riastrad else
305 1.10 riastrad workqueue_enqueue(sc->sc_task_wq, &task->rt_u.work, NULL);
306 1.1 riastrad }
307