1 /* $NetBSD: radeon_pci.c,v 1.20 2021/12/19 12:28:12 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 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: radeon_pci.c,v 1.20 2021/12/19 12:28:12 riastradh Exp $"); 34 35 #ifdef _KERNEL_OPT 36 #include "vga.h" 37 #if defined(__arm__) || defined(__aarch64__) 38 #include "opt_fdt.h" 39 #endif 40 #endif 41 42 #include <sys/types.h> 43 #include <sys/atomic.h> 44 #include <sys/queue.h> 45 #include <sys/systm.h> 46 #include <sys/workqueue.h> 47 48 #include <dev/pci/pciio.h> 49 #include <dev/pci/pcireg.h> 50 #include <dev/pci/pcivar.h> 51 52 #include <dev/pci/wsdisplay_pci.h> 53 #include <dev/wsfb/genfbvar.h> 54 55 #if NVGA > 0 56 /* 57 * XXX All we really need is vga_is_console from vgavar.h, but the 58 * header files are missing their own dependencies, so we need to 59 * explicitly drag in the other crap. 60 */ 61 #include <dev/ic/mc6845reg.h> 62 #include <dev/ic/pcdisplayvar.h> 63 #include <dev/ic/vgareg.h> 64 #include <dev/ic/vgavar.h> 65 #endif 66 67 #ifdef FDT 68 #include <dev/fdt/fdtvar.h> 69 #endif 70 71 #include <drm/drm_device.h> 72 #include <drm/drm_drv.h> 73 #include <drm/drm_fb_helper.h> 74 #include <drm/drm_pci.h> 75 76 #include <radeon.h> 77 #include "radeon_drv.h" 78 #include "radeon_task.h" 79 80 SIMPLEQ_HEAD(radeon_task_head, radeon_task); 81 82 struct radeon_softc { 83 device_t sc_dev; 84 struct pci_attach_args sc_pa; 85 struct lwp *sc_task_thread; 86 struct radeon_task_head sc_tasks; 87 struct workqueue *sc_task_wq; 88 struct drm_device *sc_drm_dev; 89 struct pci_dev sc_pci_dev; 90 bool sc_pci_attached; 91 bool sc_dev_registered; 92 #if defined(__i386__) 93 #define RADEON_PCI_UGLY_MAP_HACK 94 /* XXX Used to claim the VGA device before attach_real */ 95 bus_space_handle_t sc_temp_memh; 96 bool sc_temp_set; 97 #endif 98 }; 99 100 struct radeon_device * 101 radeon_device_private(device_t self) 102 { 103 struct radeon_softc *const sc = device_private(self); 104 105 return sc->sc_drm_dev->dev_private; 106 } 107 108 static bool radeon_pci_lookup(const struct pci_attach_args *, 109 unsigned long *); 110 111 static int radeon_match(device_t, cfdata_t, void *); 112 static void radeon_attach(device_t, device_t, void *); 113 static void radeon_attach_real(device_t); 114 static int radeon_detach(device_t, int); 115 static bool radeon_do_suspend(device_t, const pmf_qual_t *); 116 static bool radeon_do_resume(device_t, const pmf_qual_t *); 117 118 static void radeon_task_work(struct work *, void *); 119 120 CFATTACH_DECL_NEW(radeon, sizeof(struct radeon_softc), 121 radeon_match, radeon_attach, radeon_detach, NULL); 122 123 /* XXX Kludge to get these from radeon_drv.c. */ 124 extern struct drm_driver *const radeon_drm_driver; 125 extern const struct pci_device_id *const radeon_device_ids; 126 extern const size_t radeon_n_device_ids; 127 128 /* Set this to false if you want to match R100/R200 */ 129 bool radeon_pci_ignore_r100_r200 = true; 130 131 static bool 132 radeon_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags) 133 { 134 size_t i; 135 enum radeon_family fam; 136 137 for (i = 0; i < radeon_n_device_ids; i++) { 138 if ((PCI_VENDOR(pa->pa_id) == radeon_device_ids[i].vendor) && 139 (PCI_PRODUCT(pa->pa_id) == radeon_device_ids[i].device)) 140 break; 141 } 142 143 /* Did we find it? */ 144 if (i == radeon_n_device_ids) 145 return false; 146 147 /* NetBSD drm2 fails on R100 and many R200 chipsets, disable for now */ 148 fam = radeon_device_ids[i].driver_data & RADEON_FAMILY_MASK; 149 if (radeon_pci_ignore_r100_r200 && fam < CHIP_RV280) 150 return false; 151 152 if (flags) 153 *flags = radeon_device_ids[i].driver_data; 154 return true; 155 } 156 157 static int 158 radeon_match(device_t parent, cfdata_t match, void *aux) 159 { 160 extern int radeon_guarantee_initialized(void); 161 const struct pci_attach_args *const pa = aux; 162 int error; 163 164 error = radeon_guarantee_initialized(); 165 if (error) { 166 aprint_error("radeon: failed to initialize: %d\n", error); 167 return 0; 168 } 169 170 if (!radeon_pci_lookup(pa, NULL)) 171 return 0; 172 173 return 6; /* XXX Beat genfb_pci... */ 174 } 175 176 static void 177 radeon_attach(device_t parent, device_t self, void *aux) 178 { 179 struct radeon_softc *const sc = device_private(self); 180 const struct pci_attach_args *const pa = aux; 181 int error; 182 183 pci_aprint_devinfo(pa, NULL); 184 185 /* Initialize the Linux PCI device descriptor. */ 186 linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0); 187 188 sc->sc_dev = self; 189 sc->sc_pa = *pa; 190 sc->sc_task_thread = NULL; 191 SIMPLEQ_INIT(&sc->sc_tasks); 192 error = workqueue_create(&sc->sc_task_wq, "radeonfb", 193 &radeon_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE); 194 if (error) { 195 aprint_error_dev(self, "unable to create workqueue: %d\n", 196 error); 197 sc->sc_task_wq = NULL; 198 return; 199 } 200 201 #ifdef RADEON_PCI_UGLY_MAP_HACK 202 /* 203 * XXX 204 * We try to map the VGA registers, in case we can prevent vga@isa or 205 * pcdisplay@isa attaching, and stealing wsdisplay0. This only works 206 * with serial console, as actual VGA console has already mapped them. 207 * The only way to handle that is for vga@isa to not attach. 208 */ 209 int rv = bus_space_map(pa->pa_memt, 0xb0000, 0x10000, 0, 210 &sc->sc_temp_memh); 211 sc->sc_temp_set = rv == 0; 212 if (rv != 0) 213 aprint_error_dev(self, "unable to reserve VGA registers for " 214 "i386 radeondrmkms hack\n"); 215 #endif 216 217 #ifdef FDT 218 /* 219 * XXX Remove the simple framebuffer, assuming that this device 220 * will take over. 221 */ 222 const char *fb_compatible[] = { "simple-framebuffer", NULL }; 223 fdt_remove_bycompat(fb_compatible); 224 #endif 225 226 /* 227 * Defer the remainder of initialization until we have mounted 228 * the root file system and can load firmware images. 229 */ 230 config_mountroot(self, &radeon_attach_real); 231 } 232 233 static void 234 radeon_attach_real(device_t self) 235 { 236 struct radeon_softc *const sc = device_private(self); 237 const struct pci_attach_args *const pa = &sc->sc_pa; 238 bool ok __diagused; 239 unsigned long flags; 240 int error; 241 242 ok = radeon_pci_lookup(pa, &flags); 243 KASSERT(ok); 244 245 #ifdef RADEON_PCI_UGLY_MAP_HACK 246 /* 247 * XXX 248 * Unmap the VGA registers. 249 */ 250 if (sc->sc_temp_set) 251 bus_space_unmap(pa->pa_memt, sc->sc_temp_memh, 0x10000); 252 #endif 253 254 /* 255 * Cause any tasks issued synchronously during attach to be 256 * processed at the end of this function. 257 */ 258 sc->sc_task_thread = curlwp; 259 260 sc->sc_drm_dev = drm_dev_alloc(radeon_drm_driver, self); 261 if (IS_ERR(sc->sc_drm_dev)) { 262 aprint_error_dev(self, "unable to create drm device: %ld\n", 263 PTR_ERR(sc->sc_drm_dev)); 264 sc->sc_drm_dev = NULL; 265 goto out; 266 } 267 268 /* XXX errno Linux->NetBSD */ 269 error = -drm_pci_attach(sc->sc_drm_dev, &sc->sc_pci_dev); 270 if (error) { 271 aprint_error_dev(self, "unable to attach drm: %d\n", error); 272 goto out; 273 } 274 sc->sc_pci_attached = true; 275 276 /* XXX errno Linux->NetBSD */ 277 error = -drm_dev_register(sc->sc_drm_dev, flags); 278 if (error) { 279 aprint_error_dev(self, "unable to register drm: %d\n", error); 280 goto out; 281 } 282 sc->sc_dev_registered = true; 283 284 if (!pmf_device_register(self, &radeon_do_suspend, &radeon_do_resume)) 285 aprint_error_dev(self, "unable to establish power handler\n"); 286 287 /* 288 * Process asynchronous tasks queued synchronously during 289 * attach. This will be for display detection to attach a 290 * framebuffer, so we have the opportunity for a console device 291 * to attach before autoconf has completed, in time for init(8) 292 * to find that console without panicking. 293 */ 294 while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) { 295 struct radeon_task *const task = SIMPLEQ_FIRST(&sc->sc_tasks); 296 297 SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, rt_u.queue); 298 (*task->rt_fn)(task); 299 } 300 301 out: /* Cause any subesquent tasks to be processed by the workqueue. */ 302 atomic_store_relaxed(&sc->sc_task_thread, NULL); 303 } 304 305 static int 306 radeon_detach(device_t self, int flags) 307 { 308 struct radeon_softc *const sc = device_private(self); 309 int error; 310 311 /* XXX Check for in-use before tearing it all down... */ 312 error = config_detach_children(self, flags); 313 if (error) 314 return error; 315 316 KASSERT(sc->sc_task_thread == NULL); 317 KASSERT(SIMPLEQ_EMPTY(&sc->sc_tasks)); 318 319 pmf_device_deregister(self); 320 if (sc->sc_dev_registered) 321 drm_dev_unregister(sc->sc_drm_dev); 322 if (sc->sc_pci_attached) 323 drm_pci_detach(sc->sc_drm_dev); 324 if (sc->sc_drm_dev) { 325 drm_dev_put(sc->sc_drm_dev); 326 sc->sc_drm_dev = NULL; 327 } 328 if (sc->sc_task_wq) { 329 workqueue_destroy(sc->sc_task_wq); 330 sc->sc_task_wq = NULL; 331 } 332 linux_pci_dev_destroy(&sc->sc_pci_dev); 333 334 return 0; 335 } 336 337 static bool 338 radeon_do_suspend(device_t self, const pmf_qual_t *qual) 339 { 340 struct radeon_softc *const sc = device_private(self); 341 struct drm_device *const dev = sc->sc_drm_dev; 342 int ret; 343 bool is_console = true; /* XXX */ 344 345 ret = radeon_suspend_kms(dev, true, is_console, false); 346 if (ret) 347 return false; 348 349 return true; 350 } 351 352 static bool 353 radeon_do_resume(device_t self, const pmf_qual_t *qual) 354 { 355 struct radeon_softc *const sc = device_private(self); 356 struct drm_device *const dev = sc->sc_drm_dev; 357 int ret; 358 bool is_console = true; /* XXX */ 359 360 ret = radeon_resume_kms(dev, true, is_console); 361 if (ret) 362 return false; 363 364 return true; 365 } 366 367 static void 368 radeon_task_work(struct work *work, void *cookie __unused) 369 { 370 struct radeon_task *const task = container_of(work, struct radeon_task, 371 rt_u.work); 372 373 (*task->rt_fn)(task); 374 } 375 376 int 377 radeon_task_schedule(device_t self, struct radeon_task *task) 378 { 379 struct radeon_softc *const sc = device_private(self); 380 381 if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp) 382 SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, rt_u.queue); 383 else 384 workqueue_enqueue(sc->sc_task_wq, &task->rt_u.work, NULL); 385 386 return 0; 387 } 388