via_pci.c revision 1.6 1 /* $NetBSD: via_pci.c,v 1.6 2021/12/19 10:33:00 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.6 2021/12/19 10:33:00 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_pciids.h>
43 #include <drm/via_drm.h>
44
45 #include "via_drv.h"
46
47 struct drm_device;
48
49 struct viadrm_softc {
50 device_t sc_dev;
51 struct pci_dev sc_pci_dev;
52 struct drm_device *sc_drm_dev;
53 };
54
55 static int viadrm_match(device_t, cfdata_t, void *);
56 static void viadrm_attach(device_t, device_t, void *);
57 static int viadrm_detach(device_t, int);
58
59 extern struct drm_driver *const via_drm_driver; /* XXX */
60
61 CFATTACH_DECL_NEW(viadrmums, sizeof(struct viadrm_softc),
62 viadrm_match, viadrm_attach, viadrm_detach, NULL);
63
64 static const struct pci_device_id viadrm_pci_ids[] = {
65 viadrv_PCI_IDS
66 };
67
68 static const unsigned long *
69 viadrm_lookup(const struct pci_attach_args *pa)
70 {
71 unsigned i;
72
73 for (i = 0; i < __arraycount(viadrm_pci_ids); i++) {
74 if (viadrm_pci_ids[i].vendor == 0 &&
75 viadrm_pci_ids[i].device == 0)
76 break;
77 KASSERT(viadrm_pci_ids[i].subvendor == PCI_ANY_ID);
78 KASSERT(viadrm_pci_ids[i].subdevice == PCI_ANY_ID);
79 KASSERT(viadrm_pci_ids[i].class == 0);
80 KASSERT(viadrm_pci_ids[i].class_mask == 0);
81 if (PCI_VENDOR(pa->pa_id) != viadrm_pci_ids[i].vendor)
82 continue;
83 if (PCI_PRODUCT(pa->pa_id) != viadrm_pci_ids[i].device)
84 continue;
85 return &viadrm_pci_ids[i].driver_data;
86 }
87
88 return NULL;
89 }
90
91 static int
92 viadrm_match(device_t parent, cfdata_t match, void *aux)
93 {
94 extern int viadrm_guarantee_initialized(void);
95 const struct pci_attach_args *const pa = aux;
96 int error;
97
98 error = viadrm_guarantee_initialized();
99 if (error) {
100 aprint_error("viadrm: failed to initialize: %d\n", error);
101 return 0;
102 }
103
104 if (viadrm_lookup(pa) == NULL)
105 return 0;
106
107 return 1;
108 }
109
110 static void
111 viadrm_attach(device_t parent, device_t self, void *aux)
112 {
113 struct viadrm_softc *const sc = device_private(self);
114 const struct pci_attach_args *const pa = aux;
115 const unsigned long *const cookiep = viadrm_lookup(pa);
116 int error;
117
118 KASSERT(cookiep != NULL);
119
120 aprint_naive("\n");
121 aprint_normal("\n");
122
123 if (!pmf_device_register(self, NULL, NULL))
124 aprint_error_dev(self, "couldn't establish power handler\n");
125
126 /* Initialize the Linux PCI device descriptor. */
127 linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
128
129 /* XXX errno Linux->NetBSD */
130 error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, via_drm_driver,
131 *cookiep, &sc->sc_drm_dev);
132 if (error) {
133 aprint_error_dev(self, "unable to attach drm: %d\n", error);
134 return;
135 }
136 }
137
138 static int
139 viadrm_detach(device_t self, int flags)
140 {
141 struct viadrm_softc *const sc = device_private(self);
142 int error;
143
144 error = config_detach_children(self, flags);
145 if (error)
146 return error;
147 if (sc->sc_drm_dev == NULL)
148 goto out;
149 /* XXX errno Linux->NetBSD */
150 error = -drm_pci_detach(sc->sc_drm_dev, flags);
151 if (error)
152 return error;
153 sc->sc_drm_dev = NULL;
154
155 out: linux_pci_dev_destroy(&sc->sc_pci_dev);
156 pmf_device_deregister(self);
157 return 0;
158 }
159