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