xref: /netbsd-src/sys/external/bsd/drm2/via/via_pci.c (revision c185307488bbf6fc561ecae5b867f82bd8fc67eb)
1 /*	$NetBSD: via_pci.c,v 1.7 2021/12/19 12:30:23 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: via_pci.c,v 1.7 2021/12/19 12:30:23 riastradh Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/device.h>
37 #include <sys/pmf.h>
38 #include <sys/systm.h>
39 
40 #include <linux/pci.h>
41 
42 #include <drm/drm_device.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_pci.h>
45 #include <drm/drm_pciids.h>
46 #include <drm/via_drm.h>
47 
48 #include "via_drv.h"
49 
50 struct drm_device;
51 
52 struct viadrm_softc {
53 	device_t		sc_dev;
54 	struct pci_dev		sc_pci_dev;
55 	struct drm_device	*sc_drm_dev;
56 	bool			sc_pci_attached;
57 	bool			sc_dev_registered;
58 };
59 
60 static int	viadrm_match(device_t, cfdata_t, void *);
61 static void	viadrm_attach(device_t, device_t, void *);
62 static int	viadrm_detach(device_t, int);
63 
64 extern struct drm_driver *const via_drm_driver; /* XXX */
65 
66 CFATTACH_DECL_NEW(viadrmums, sizeof(struct viadrm_softc),
67     viadrm_match, viadrm_attach, viadrm_detach, NULL);
68 
69 static const struct pci_device_id viadrm_pci_ids[] = {
70 	viadrv_PCI_IDS
71 };
72 
73 static const unsigned long *
viadrm_lookup(const struct pci_attach_args * pa)74 viadrm_lookup(const struct pci_attach_args *pa)
75 {
76 	unsigned i;
77 
78 	for (i = 0; i < __arraycount(viadrm_pci_ids); i++) {
79 		if (viadrm_pci_ids[i].vendor == 0 &&
80 		    viadrm_pci_ids[i].device == 0)
81 			break;
82 		KASSERT(viadrm_pci_ids[i].subvendor == PCI_ANY_ID);
83 		KASSERT(viadrm_pci_ids[i].subdevice == PCI_ANY_ID);
84 		KASSERT(viadrm_pci_ids[i].class == 0);
85 		KASSERT(viadrm_pci_ids[i].class_mask == 0);
86 		if (PCI_VENDOR(pa->pa_id) != viadrm_pci_ids[i].vendor)
87 			continue;
88 		if (PCI_PRODUCT(pa->pa_id) != viadrm_pci_ids[i].device)
89 			continue;
90 		return &viadrm_pci_ids[i].driver_data;
91 	}
92 
93 	return NULL;
94 }
95 
96 static int
viadrm_match(device_t parent,cfdata_t match,void * aux)97 viadrm_match(device_t parent, cfdata_t match, void *aux)
98 {
99 	extern int viadrm_guarantee_initialized(void);
100 	const struct pci_attach_args *const pa = aux;
101 	int error;
102 
103 	error = viadrm_guarantee_initialized();
104 	if (error) {
105 		aprint_error("viadrm: failed to initialize: %d\n", error);
106 		return 0;
107 	}
108 
109 	if (viadrm_lookup(pa) == NULL)
110 		return 0;
111 
112 	return 1;
113 }
114 
115 static void
viadrm_attach(device_t parent,device_t self,void * aux)116 viadrm_attach(device_t parent, device_t self, void *aux)
117 {
118 	struct viadrm_softc *const sc = device_private(self);
119 	const struct pci_attach_args *const pa = aux;
120 	const unsigned long *const cookiep = viadrm_lookup(pa);
121 	int error;
122 
123 	KASSERT(cookiep != NULL);
124 
125 	aprint_naive("\n");
126 	aprint_normal("\n");
127 
128 	/* Initialize the Linux PCI device descriptor.  */
129 	linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
130 
131 	sc->sc_drm_dev = drm_dev_alloc(via_drm_driver, self);
132 	if (IS_ERR(sc->sc_drm_dev)) {
133 		aprint_error_dev(self, "unable to create drm device: %ld\n",
134 		    PTR_ERR(sc->sc_drm_dev));
135 		sc->sc_drm_dev = NULL;
136 		return;
137 	}
138 
139 	/* XXX errno Linux->NetBSD */
140 	error = -drm_pci_attach(sc->sc_drm_dev, &sc->sc_pci_dev);
141 	if (error) {
142 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
143 		return;
144 	}
145 	sc->sc_pci_attached = true;
146 
147 	/* XXX errno Linux->NetBSD */
148 	error = -drm_dev_register(sc->sc_drm_dev, *cookiep);
149 	if (error) {
150 		aprint_error_dev(self, "unable to register drm: %d\n", error);
151 		return;
152 	}
153 	sc->sc_dev_registered = true;
154 
155 	if (!pmf_device_register(self, NULL, NULL))
156 		aprint_error_dev(self, "couldn't establish power handler\n");
157 }
158 
159 static int
viadrm_detach(device_t self,int flags)160 viadrm_detach(device_t self, int flags)
161 {
162 	struct viadrm_softc *const sc = device_private(self);
163 	int error;
164 
165 	error = config_detach_children(self, flags);
166 	if (error)
167 		return error;
168 
169 	pmf_device_deregister(self);
170 	if (sc->sc_dev_registered) {
171 		drm_dev_unregister(sc->sc_drm_dev);
172 		sc->sc_dev_registered = false;
173 	}
174 	if (sc->sc_pci_attached) {
175 		drm_pci_detach(sc->sc_drm_dev);
176 		sc->sc_pci_attached = false;
177 	}
178 	if (sc->sc_drm_dev) {
179 		drm_dev_put(sc->sc_drm_dev);
180 		sc->sc_drm_dev = NULL;
181 	}
182 	linux_pci_dev_destroy(&sc->sc_pci_dev);
183 
184 	return 0;
185 }
186