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