nouveau_pci.c revision 1.1
1/* $NetBSD: nouveau_pci.c,v 1.1 2015/03/06 01:43:07 riastradh Exp $ */ 2 3/*- 4 * Copyright (c) 2015 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: nouveau_pci.c,v 1.1 2015/03/06 01:43:07 riastradh Exp $"); 34 35#include <sys/types.h> 36#include <sys/device.h> 37#include <sys/queue.h> 38#include <sys/workqueue.h> 39 40#include <drm/drmP.h> 41 42#include "nouveau_drm.h" 43#include "nouveau_pci.h" 44 45SIMPLEQ_HEAD(nouveau_task_head, nouveau_task); 46 47struct nouveau_softc { 48 device_t sc_dev; 49 enum { 50 NOUVEAU_TASK_ATTACH, 51 NOUVEAU_TASK_WORKQUEUE, 52 } sc_task_state; 53 union { 54 struct workqueue *workqueue; 55 struct nouveau_task_head attach; 56 } sc_task_u; 57 struct drm_device *sc_drm_dev; 58 struct pci_dev sc_pci_dev; 59}; 60 61static int nouveau_match(device_t, cfdata_t, void *); 62static void nouveau_attach(device_t, device_t, void *); 63static int nouveau_detach(device_t, int); 64 65static bool nouveau_suspend(device_t, const pmf_qual_t *); 66static bool nouveau_resume(device_t, const pmf_qual_t *); 67 68static void nouveau_task_work(struct work *, void *); 69 70CFATTACH_DECL_NEW(nouveau, sizeof(struct nouveau_softc), 71 nouveau_match, nouveau_attach, nouveau_detach, NULL); 72 73/* Kludge to get this from nouveau_drm.c. */ 74extern struct drm_driver *const nouveau_drm_driver; 75 76static int 77nouveau_match(device_t parent, cfdata_t match, void *aux) 78{ 79 extern int nouveau_guarantee_initialized(void); 80 const struct pci_attach_args *const pa = aux; 81 int error; 82 83 error = nouveau_guarantee_initialized(); 84 if (error) { 85 aprint_error("nouveau: failed to initialize: %d\n", error); 86 return 0; 87 } 88 89 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA && 90 PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS) 91 return 0; 92 93 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 94 return 0; 95 96 return 6; /* XXX Beat genfb_pci... */ 97} 98 99static void 100nouveau_attach(device_t parent, device_t self, void *aux) 101{ 102 struct nouveau_softc *const sc = device_private(self); 103 const struct pci_attach_args *const pa = aux; 104 int error; 105 106 pci_aprint_devinfo(pa, NULL); 107 108 sc->sc_dev = self; 109 110 if (!pmf_device_register(self, &nouveau_suspend, 111 &nouveau_resume)) 112 aprint_error_dev(self, "unable to establish power handler\n"); 113 114 sc->sc_task_state = NOUVEAU_TASK_ATTACH; 115 SIMPLEQ_INIT(&sc->sc_task_u.attach); 116 117 118 /* XXX errno Linux->NetBSD */ 119 error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, nouveau_drm_driver, 120 0, &sc->sc_drm_dev); 121 if (error) { 122 aprint_error_dev(self, "unable to attach drm: %d\n", error); 123 return; 124 } 125 126 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) { 127 struct nouveau_task *const task = 128 SIMPLEQ_FIRST(&sc->sc_task_u.attach); 129 130 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue); 131 (*task->nt_fn)(task); 132 } 133 134 sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE; 135 error = workqueue_create(&sc->sc_task_u.workqueue, "intelfb", 136 &nouveau_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE); 137 if (error) { 138 aprint_error_dev(self, "unable to create workqueue: %d\n", 139 error); 140 sc->sc_task_u.workqueue = NULL; 141 return; 142 } 143} 144 145static int 146nouveau_detach(device_t self, int flags) 147{ 148 struct nouveau_softc *const sc = device_private(self); 149 int error; 150 151 /* XXX Check for in-use before tearing it all down... */ 152 error = config_detach_children(self, flags); 153 if (error) 154 return error; 155 156 if (sc->sc_task_state == NOUVEAU_TASK_ATTACH) 157 goto out; 158 if (sc->sc_task_u.workqueue != NULL) { 159 workqueue_destroy(sc->sc_task_u.workqueue); 160 sc->sc_task_u.workqueue = NULL; 161 } 162 163 if (sc->sc_drm_dev == NULL) 164 goto out; 165 /* XXX errno Linux->NetBSD */ 166 error = -drm_pci_detach(sc->sc_drm_dev, flags); 167 if (error) 168 /* XXX Kinda too late to fail now... */ 169 return error; 170 sc->sc_drm_dev = NULL; 171 172out: pmf_device_deregister(self); 173 return 0; 174} 175 176/* 177 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c. 178 */ 179static bool 180nouveau_suspend(device_t self, const pmf_qual_t *qual __unused) 181{ 182 struct nouveau_softc *const sc = device_private(self); 183 struct device *const dev = &sc->sc_pci_dev.dev; /* XXX KLUDGE */ 184 185 return nouveau_pmops_suspend(dev) == 0; 186} 187 188static bool 189nouveau_resume(device_t self, const pmf_qual_t *qual) 190{ 191 struct nouveau_softc *const sc = device_private(self); 192 struct device *const dev = &sc->sc_pci_dev.dev; /* XXX KLUDGE */ 193 194 return nouveau_pmops_resume(dev) == 0; 195} 196 197static void 198nouveau_task_work(struct work *work, void *cookie __unused) 199{ 200 struct nouveau_task *const task = container_of(work, 201 struct nouveau_task, nt_u.work); 202 203 (*task->nt_fn)(task); 204} 205 206int 207nouveau_task_schedule(device_t self, struct nouveau_task *task) 208{ 209 struct nouveau_softc *const sc = device_private(self); 210 211 switch (sc->sc_task_state) { 212 case NOUVEAU_TASK_ATTACH: 213 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue); 214 return 0; 215 case NOUVEAU_TASK_WORKQUEUE: 216 if (sc->sc_task_u.workqueue == NULL) { 217 aprint_error_dev(self, "unable to schedule task\n"); 218 return EIO; 219 } 220 workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work, 221 NULL); 222 return 0; 223 default: 224 panic("nouveau in invalid task state: %d\n", 225 (int)sc->sc_task_state); 226 } 227} 228