amdgpu_pci.c revision 1.5.6.2 1 1.5.6.2 christos /* $NetBSD: amdgpu_pci.c,v 1.5.6.2 2019/06/10 22:07:56 christos Exp $ */
2 1.5.6.2 christos
3 1.5.6.2 christos /*-
4 1.5.6.2 christos * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.5.6.2 christos * All rights reserved.
6 1.5.6.2 christos *
7 1.5.6.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.5.6.2 christos * by Taylor R. Campbell.
9 1.5.6.2 christos *
10 1.5.6.2 christos * Redistribution and use in source and binary forms, with or without
11 1.5.6.2 christos * modification, are permitted provided that the following conditions
12 1.5.6.2 christos * are met:
13 1.5.6.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.5.6.2 christos * notice, this list of conditions and the following disclaimer.
15 1.5.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.5.6.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.5.6.2 christos * documentation and/or other materials provided with the distribution.
18 1.5.6.2 christos *
19 1.5.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.5.6.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.5.6.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.5.6.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.5.6.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.5.6.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.5.6.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.5.6.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.5.6.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.5.6.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.5.6.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.5.6.2 christos */
31 1.5.6.2 christos
32 1.5.6.2 christos #include <sys/cdefs.h>
33 1.5.6.2 christos __KERNEL_RCSID(0, "$NetBSD: amdgpu_pci.c,v 1.5.6.2 2019/06/10 22:07:56 christos Exp $");
34 1.5.6.2 christos
35 1.5.6.2 christos #include <sys/types.h>
36 1.5.6.2 christos #include <sys/queue.h>
37 1.5.6.2 christos #include <sys/systm.h>
38 1.5.6.2 christos #include <sys/workqueue.h>
39 1.5.6.2 christos
40 1.5.6.2 christos #include <drm/drmP.h>
41 1.5.6.2 christos
42 1.5.6.2 christos #include <amdgpu.h>
43 1.5.6.2 christos #include "amdgpu_drv.h"
44 1.5.6.2 christos #include "amdgpu_task.h"
45 1.5.6.2 christos
46 1.5.6.2 christos SIMPLEQ_HEAD(amdgpu_task_head, amdgpu_task);
47 1.5.6.2 christos
48 1.5.6.2 christos struct amdgpu_softc {
49 1.5.6.2 christos device_t sc_dev;
50 1.5.6.2 christos struct pci_attach_args sc_pa;
51 1.5.6.2 christos enum {
52 1.5.6.2 christos AMDGPU_TASK_ATTACH,
53 1.5.6.2 christos AMDGPU_TASK_WORKQUEUE,
54 1.5.6.2 christos } sc_task_state;
55 1.5.6.2 christos union {
56 1.5.6.2 christos struct workqueue *workqueue;
57 1.5.6.2 christos struct amdgpu_task_head attach;
58 1.5.6.2 christos } sc_task_u;
59 1.5.6.2 christos struct drm_device *sc_drm_dev;
60 1.5.6.2 christos struct pci_dev sc_pci_dev;
61 1.5.6.2 christos };
62 1.5.6.2 christos
63 1.5.6.2 christos static bool amdgpu_pci_lookup(const struct pci_attach_args *,
64 1.5.6.2 christos unsigned long *);
65 1.5.6.2 christos
66 1.5.6.2 christos static int amdgpu_match(device_t, cfdata_t, void *);
67 1.5.6.2 christos static void amdgpu_attach(device_t, device_t, void *);
68 1.5.6.2 christos static void amdgpu_attach_real(device_t);
69 1.5.6.2 christos static int amdgpu_detach(device_t, int);
70 1.5.6.2 christos static bool amdgpu_do_suspend(device_t, const pmf_qual_t *);
71 1.5.6.2 christos static bool amdgpu_do_resume(device_t, const pmf_qual_t *);
72 1.5.6.2 christos
73 1.5.6.2 christos static void amdgpu_task_work(struct work *, void *);
74 1.5.6.2 christos
75 1.5.6.2 christos CFATTACH_DECL_NEW(amdgpu, sizeof(struct amdgpu_softc),
76 1.5.6.2 christos amdgpu_match, amdgpu_attach, amdgpu_detach, NULL);
77 1.5.6.2 christos
78 1.5.6.2 christos /* XXX Kludge to get these from amdgpu_drv.c. */
79 1.5.6.2 christos extern struct drm_driver *const amdgpu_drm_driver;
80 1.5.6.2 christos extern const struct pci_device_id *const amdgpu_device_ids;
81 1.5.6.2 christos extern const size_t amdgpu_n_device_ids;
82 1.5.6.2 christos
83 1.5.6.2 christos static bool
84 1.5.6.2 christos amdgpu_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
85 1.5.6.2 christos {
86 1.5.6.2 christos size_t i;
87 1.5.6.2 christos
88 1.5.6.2 christos for (i = 0; i < amdgpu_n_device_ids; i++) {
89 1.5.6.2 christos if ((PCI_VENDOR(pa->pa_id) == amdgpu_device_ids[i].vendor) &&
90 1.5.6.2 christos (PCI_PRODUCT(pa->pa_id) == amdgpu_device_ids[i].device))
91 1.5.6.2 christos break;
92 1.5.6.2 christos }
93 1.5.6.2 christos
94 1.5.6.2 christos /* Did we find it? */
95 1.5.6.2 christos if (i == amdgpu_n_device_ids)
96 1.5.6.2 christos return false;
97 1.5.6.2 christos
98 1.5.6.2 christos if (flags)
99 1.5.6.2 christos *flags = amdgpu_device_ids[i].driver_data;
100 1.5.6.2 christos return true;
101 1.5.6.2 christos }
102 1.5.6.2 christos
103 1.5.6.2 christos static int
104 1.5.6.2 christos amdgpu_match(device_t parent, cfdata_t match, void *aux)
105 1.5.6.2 christos {
106 1.5.6.2 christos extern int amdgpu_guarantee_initialized(void);
107 1.5.6.2 christos const struct pci_attach_args *const pa = aux;
108 1.5.6.2 christos int error;
109 1.5.6.2 christos
110 1.5.6.2 christos error = amdgpu_guarantee_initialized();
111 1.5.6.2 christos if (error) {
112 1.5.6.2 christos aprint_error("amdgpu: failed to initialize: %d\n", error);
113 1.5.6.2 christos return 0;
114 1.5.6.2 christos }
115 1.5.6.2 christos
116 1.5.6.2 christos if (!amdgpu_pci_lookup(pa, NULL))
117 1.5.6.2 christos return 0;
118 1.5.6.2 christos
119 1.5.6.2 christos return 7; /* beat genfb_pci and radeon */
120 1.5.6.2 christos }
121 1.5.6.2 christos
122 1.5.6.2 christos static void
123 1.5.6.2 christos amdgpu_attach(device_t parent, device_t self, void *aux)
124 1.5.6.2 christos {
125 1.5.6.2 christos struct amdgpu_softc *const sc = device_private(self);
126 1.5.6.2 christos const struct pci_attach_args *const pa = aux;
127 1.5.6.2 christos
128 1.5.6.2 christos pci_aprint_devinfo(pa, NULL);
129 1.5.6.2 christos
130 1.5.6.2 christos if (!pmf_device_register(self, &amdgpu_do_suspend, &amdgpu_do_resume))
131 1.5.6.2 christos aprint_error_dev(self, "unable to establish power handler\n");
132 1.5.6.2 christos
133 1.5.6.2 christos /*
134 1.5.6.2 christos * Trivial initialization first; the rest will come after we
135 1.5.6.2 christos * have mounted the root file system and can load firmware
136 1.5.6.2 christos * images.
137 1.5.6.2 christos */
138 1.5.6.2 christos sc->sc_dev = NULL;
139 1.5.6.2 christos sc->sc_pa = *pa;
140 1.5.6.2 christos
141 1.5.6.2 christos config_mountroot(self, &amdgpu_attach_real);
142 1.5.6.2 christos }
143 1.5.6.2 christos
144 1.5.6.2 christos static void
145 1.5.6.2 christos amdgpu_attach_real(device_t self)
146 1.5.6.2 christos {
147 1.5.6.2 christos struct amdgpu_softc *const sc = device_private(self);
148 1.5.6.2 christos const struct pci_attach_args *const pa = &sc->sc_pa;
149 1.5.6.2 christos bool ok __diagused;
150 1.5.6.2 christos unsigned long flags = 0; /* XXXGCC */
151 1.5.6.2 christos int error;
152 1.5.6.2 christos
153 1.5.6.2 christos ok = amdgpu_pci_lookup(pa, &flags);
154 1.5.6.2 christos KASSERT(ok);
155 1.5.6.2 christos
156 1.5.6.2 christos sc->sc_task_state = AMDGPU_TASK_ATTACH;
157 1.5.6.2 christos SIMPLEQ_INIT(&sc->sc_task_u.attach);
158 1.5.6.2 christos
159 1.5.6.2 christos /* Initialize the Linux PCI device descriptor. */
160 1.5.6.2 christos linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
161 1.5.6.2 christos
162 1.5.6.2 christos /* XXX errno Linux->NetBSD */
163 1.5.6.2 christos error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, amdgpu_drm_driver,
164 1.5.6.2 christos flags, &sc->sc_drm_dev);
165 1.5.6.2 christos if (error) {
166 1.5.6.2 christos aprint_error_dev(self, "unable to attach drm: %d\n", error);
167 1.5.6.2 christos goto out;
168 1.5.6.2 christos }
169 1.5.6.2 christos
170 1.5.6.2 christos while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
171 1.5.6.2 christos struct amdgpu_task *const task =
172 1.5.6.2 christos SIMPLEQ_FIRST(&sc->sc_task_u.attach);
173 1.5.6.2 christos
174 1.5.6.2 christos SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, rt_u.queue);
175 1.5.6.2 christos (*task->rt_fn)(task);
176 1.5.6.2 christos }
177 1.5.6.2 christos
178 1.5.6.2 christos sc->sc_task_state = AMDGPU_TASK_WORKQUEUE;
179 1.5.6.2 christos error = workqueue_create(&sc->sc_task_u.workqueue, "amdgpufb",
180 1.5.6.2 christos &amdgpu_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
181 1.5.6.2 christos if (error) {
182 1.5.6.2 christos aprint_error_dev(self, "unable to create workqueue: %d\n",
183 1.5.6.2 christos error);
184 1.5.6.2 christos sc->sc_task_u.workqueue = NULL;
185 1.5.6.2 christos goto out;
186 1.5.6.2 christos }
187 1.5.6.2 christos
188 1.5.6.2 christos out: sc->sc_dev = self;
189 1.5.6.2 christos }
190 1.5.6.2 christos
191 1.5.6.2 christos static int
192 1.5.6.2 christos amdgpu_detach(device_t self, int flags)
193 1.5.6.2 christos {
194 1.5.6.2 christos struct amdgpu_softc *const sc = device_private(self);
195 1.5.6.2 christos int error;
196 1.5.6.2 christos
197 1.5.6.2 christos if (sc->sc_dev == NULL)
198 1.5.6.2 christos /* Not done attaching. */
199 1.5.6.2 christos return EBUSY;
200 1.5.6.2 christos
201 1.5.6.2 christos /* XXX Check for in-use before tearing it all down... */
202 1.5.6.2 christos error = config_detach_children(self, flags);
203 1.5.6.2 christos if (error)
204 1.5.6.2 christos return error;
205 1.5.6.2 christos
206 1.5.6.2 christos if (sc->sc_task_state == AMDGPU_TASK_ATTACH)
207 1.5.6.2 christos goto out;
208 1.5.6.2 christos if (sc->sc_task_u.workqueue != NULL) {
209 1.5.6.2 christos workqueue_destroy(sc->sc_task_u.workqueue);
210 1.5.6.2 christos sc->sc_task_u.workqueue = NULL;
211 1.5.6.2 christos }
212 1.5.6.2 christos
213 1.5.6.2 christos if (sc->sc_drm_dev == NULL)
214 1.5.6.2 christos goto out;
215 1.5.6.2 christos /* XXX errno Linux->NetBSD */
216 1.5.6.2 christos error = -drm_pci_detach(sc->sc_drm_dev, flags);
217 1.5.6.2 christos if (error)
218 1.5.6.2 christos /* XXX Kinda too late to fail now... */
219 1.5.6.2 christos return error;
220 1.5.6.2 christos sc->sc_drm_dev = NULL;
221 1.5.6.2 christos
222 1.5.6.2 christos out: linux_pci_dev_destroy(&sc->sc_pci_dev);
223 1.5.6.2 christos pmf_device_deregister(self);
224 1.5.6.2 christos
225 1.5.6.2 christos return 0;
226 1.5.6.2 christos }
227 1.5.6.2 christos
228 1.5.6.2 christos static bool
229 1.5.6.2 christos amdgpu_do_suspend(device_t self, const pmf_qual_t *qual)
230 1.5.6.2 christos {
231 1.5.6.2 christos struct amdgpu_softc *const sc = device_private(self);
232 1.5.6.2 christos struct drm_device *const dev = sc->sc_drm_dev;
233 1.5.6.2 christos int ret;
234 1.5.6.2 christos bool is_console = true; /* XXX */
235 1.5.6.2 christos
236 1.5.6.2 christos if (dev == NULL)
237 1.5.6.2 christos return true;
238 1.5.6.2 christos
239 1.5.6.2 christos ret = amdgpu_suspend_kms(dev, true, is_console);
240 1.5.6.2 christos if (ret)
241 1.5.6.2 christos return false;
242 1.5.6.2 christos
243 1.5.6.2 christos return true;
244 1.5.6.2 christos }
245 1.5.6.2 christos
246 1.5.6.2 christos static bool
247 1.5.6.2 christos amdgpu_do_resume(device_t self, const pmf_qual_t *qual)
248 1.5.6.2 christos {
249 1.5.6.2 christos struct amdgpu_softc *const sc = device_private(self);
250 1.5.6.2 christos struct drm_device *const dev = sc->sc_drm_dev;
251 1.5.6.2 christos int ret;
252 1.5.6.2 christos bool is_console = true; /* XXX */
253 1.5.6.2 christos
254 1.5.6.2 christos if (dev == NULL)
255 1.5.6.2 christos return true;
256 1.5.6.2 christos
257 1.5.6.2 christos ret = amdgpu_resume_kms(dev, true, is_console);
258 1.5.6.2 christos if (ret)
259 1.5.6.2 christos return false;
260 1.5.6.2 christos
261 1.5.6.2 christos return true;
262 1.5.6.2 christos }
263 1.5.6.2 christos
264 1.5.6.2 christos static void
265 1.5.6.2 christos amdgpu_task_work(struct work *work, void *cookie __unused)
266 1.5.6.2 christos {
267 1.5.6.2 christos struct amdgpu_task *const task = container_of(work, struct amdgpu_task,
268 1.5.6.2 christos rt_u.work);
269 1.5.6.2 christos
270 1.5.6.2 christos (*task->rt_fn)(task);
271 1.5.6.2 christos }
272 1.5.6.2 christos
273 1.5.6.2 christos int
274 1.5.6.2 christos amdgpu_task_schedule(device_t self, struct amdgpu_task *task)
275 1.5.6.2 christos {
276 1.5.6.2 christos struct amdgpu_softc *const sc = device_private(self);
277 1.5.6.2 christos
278 1.5.6.2 christos switch (sc->sc_task_state) {
279 1.5.6.2 christos case AMDGPU_TASK_ATTACH:
280 1.5.6.2 christos SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, rt_u.queue);
281 1.5.6.2 christos return 0;
282 1.5.6.2 christos case AMDGPU_TASK_WORKQUEUE:
283 1.5.6.2 christos if (sc->sc_task_u.workqueue == NULL) {
284 1.5.6.2 christos aprint_error_dev(self, "unable to schedule task\n");
285 1.5.6.2 christos return EIO;
286 1.5.6.2 christos }
287 1.5.6.2 christos workqueue_enqueue(sc->sc_task_u.workqueue, &task->rt_u.work,
288 1.5.6.2 christos NULL);
289 1.5.6.2 christos return 0;
290 1.5.6.2 christos default:
291 1.5.6.2 christos panic("amdgpu in invalid task state: %d\n",
292 1.5.6.2 christos (int)sc->sc_task_state);
293 1.5.6.2 christos }
294 1.5.6.2 christos }
295