drm_pci.c revision 1.1.2.6 1 1.1.2.1 riastrad /* $NetBSD: drm_pci.c,v 1.1.2.6 2013/07/24 03:57:03 riastradh Exp $ */
2 1.1.2.1 riastrad
3 1.1.2.1 riastrad /*-
4 1.1.2.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1.2.1 riastrad * All rights reserved.
6 1.1.2.1 riastrad *
7 1.1.2.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 riastrad * by Taylor R. Campbell.
9 1.1.2.1 riastrad *
10 1.1.2.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 riastrad * modification, are permitted provided that the following conditions
12 1.1.2.1 riastrad * are met:
13 1.1.2.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 riastrad *
19 1.1.2.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 riastrad */
31 1.1.2.1 riastrad
32 1.1.2.1 riastrad #include <sys/cdefs.h>
33 1.1.2.1 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_pci.c,v 1.1.2.6 2013/07/24 03:57:03 riastradh Exp $");
34 1.1.2.1 riastrad
35 1.1.2.1 riastrad #include <sys/types.h>
36 1.1.2.1 riastrad #include <sys/errno.h>
37 1.1.2.1 riastrad #include <sys/systm.h>
38 1.1.2.1 riastrad
39 1.1.2.1 riastrad #include <dev/pci/pcivar.h>
40 1.1.2.1 riastrad
41 1.1.2.1 riastrad #include <drm/drmP.h>
42 1.1.2.1 riastrad
43 1.1.2.1 riastrad static int drm_pci_get_irq(struct drm_device *);
44 1.1.2.1 riastrad static int drm_pci_irq_install(struct drm_device *,
45 1.1.2.1 riastrad irqreturn_t (*)(void *), int, const char *, void *,
46 1.1.2.1 riastrad struct drm_bus_irq_cookie **);
47 1.1.2.1 riastrad static void drm_pci_irq_uninstall(struct drm_device *,
48 1.1.2.1 riastrad struct drm_bus_irq_cookie *);
49 1.1.2.1 riastrad static const char *
50 1.1.2.1 riastrad drm_pci_get_name(struct drm_device *);
51 1.1.2.1 riastrad static int drm_pci_set_busid(struct drm_device *, struct drm_master *);
52 1.1.2.1 riastrad static int drm_pci_set_unique(struct drm_device *, struct drm_master *,
53 1.1.2.1 riastrad struct drm_unique *);
54 1.1.2.1 riastrad static int drm_pci_irq_by_busid(struct drm_device *,
55 1.1.2.1 riastrad struct drm_irq_busid *);
56 1.1.2.1 riastrad static int drm_pci_agp_init(struct drm_device *);
57 1.1.2.1 riastrad
58 1.1.2.1 riastrad const struct drm_bus drm_pci_bus = {
59 1.1.2.1 riastrad .bus_type = DRIVER_BUS_PCI,
60 1.1.2.1 riastrad .get_irq = drm_pci_get_irq,
61 1.1.2.1 riastrad .irq_install = drm_pci_irq_install,
62 1.1.2.1 riastrad .irq_uninstall = drm_pci_irq_uninstall,
63 1.1.2.1 riastrad .get_name = drm_pci_get_name,
64 1.1.2.1 riastrad .set_busid = drm_pci_set_busid,
65 1.1.2.1 riastrad .set_unique = drm_pci_set_unique,
66 1.1.2.1 riastrad .irq_by_busid = drm_pci_irq_by_busid,
67 1.1.2.1 riastrad .agp_init = drm_pci_agp_init,
68 1.1.2.1 riastrad };
69 1.1.2.1 riastrad
70 1.1.2.1 riastrad static const struct pci_attach_args *
71 1.1.2.1 riastrad drm_pci_attach_args(struct drm_device *dev)
72 1.1.2.1 riastrad {
73 1.1.2.1 riastrad return &dev->pdev->pd_pa;
74 1.1.2.1 riastrad }
75 1.1.2.1 riastrad
76 1.1.2.6 riastrad int
77 1.1.2.6 riastrad drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver __unused)
78 1.1.2.6 riastrad {
79 1.1.2.6 riastrad
80 1.1.2.6 riastrad driver->bus = &drm_pci_bus;
81 1.1.2.6 riastrad return 0;
82 1.1.2.6 riastrad }
83 1.1.2.6 riastrad
84 1.1.2.6 riastrad void
85 1.1.2.6 riastrad drm_pci_exit(struct drm_driver *driver __unused,
86 1.1.2.6 riastrad struct pci_driver *pdriver __unused)
87 1.1.2.6 riastrad {
88 1.1.2.6 riastrad }
89 1.1.2.6 riastrad
90 1.1.2.2 riastrad void
91 1.1.2.2 riastrad drm_pci_attach(device_t self, const struct pci_attach_args *pa,
92 1.1.2.2 riastrad struct pci_dev *pdev, struct drm_device *dev)
93 1.1.2.2 riastrad {
94 1.1.2.2 riastrad
95 1.1.2.3 riastrad linux_pci_dev_init(pdev, self, pa, 0);
96 1.1.2.2 riastrad
97 1.1.2.2 riastrad dev->pdev = pdev;
98 1.1.2.2 riastrad dev->pci_vendor = pdev->vendor;
99 1.1.2.2 riastrad dev->pci_device = pdev->device;
100 1.1.2.2 riastrad
101 1.1.2.2 riastrad /* XXX Set the power state to D0? */
102 1.1.2.2 riastrad }
103 1.1.2.2 riastrad
104 1.1.2.2 riastrad int
105 1.1.2.2 riastrad drm_pci_detach(struct drm_device *dev __unused, int flags __unused)
106 1.1.2.2 riastrad {
107 1.1.2.2 riastrad
108 1.1.2.2 riastrad /* XXX Disestablish irqs or anything? */
109 1.1.2.2 riastrad return 0;
110 1.1.2.2 riastrad }
111 1.1.2.2 riastrad
112 1.1.2.1 riastrad static int
113 1.1.2.1 riastrad drm_pci_get_irq(struct drm_device *dev)
114 1.1.2.1 riastrad {
115 1.1.2.1 riastrad pci_intr_handle_t ih_pih;
116 1.1.2.1 riastrad int ih_int;
117 1.1.2.1 riastrad
118 1.1.2.1 riastrad /*
119 1.1.2.1 riastrad * This is a compile-time assertion that the types match. If
120 1.1.2.1 riastrad * this fails, we have to change a bunch of drm code that uses
121 1.1.2.1 riastrad * int for intr handles.
122 1.1.2.1 riastrad */
123 1.1.2.1 riastrad KASSERT(&ih_pih != &ih_int);
124 1.1.2.1 riastrad
125 1.1.2.1 riastrad if (pci_intr_map(drm_pci_attach_args(dev), &ih_pih))
126 1.1.2.1 riastrad return -1; /* XXX Hope -1 is an invalid intr handle. */
127 1.1.2.1 riastrad
128 1.1.2.1 riastrad ih_int = ih_pih;
129 1.1.2.1 riastrad return ih_int;
130 1.1.2.1 riastrad }
131 1.1.2.1 riastrad
132 1.1.2.1 riastrad static int
133 1.1.2.1 riastrad drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
134 1.1.2.1 riastrad int flags, const char *name, void *arg,
135 1.1.2.1 riastrad struct drm_bus_irq_cookie **cookiep)
136 1.1.2.1 riastrad {
137 1.1.2.1 riastrad const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
138 1.1.2.1 riastrad pci_intr_handle_t ih;
139 1.1.2.1 riastrad const char *intrstr;
140 1.1.2.1 riastrad void *ih_cookie;
141 1.1.2.1 riastrad
142 1.1.2.1 riastrad if (pci_intr_map(pa, &ih))
143 1.1.2.1 riastrad return -ENOENT;
144 1.1.2.1 riastrad
145 1.1.2.1 riastrad intrstr = pci_intr_string(pa->pa_pc, ih);
146 1.1.2.1 riastrad ih_cookie = pci_intr_establish(pa->pa_pc, ih, IPL_DRM, handler, arg);
147 1.1.2.1 riastrad if (ih_cookie == NULL) {
148 1.1.2.1 riastrad aprint_error_dev(dev->dev,
149 1.1.2.1 riastrad "couldn't establish interrupt at %s (%s)\n",
150 1.1.2.1 riastrad intrstr, name);
151 1.1.2.1 riastrad return -ENOENT;
152 1.1.2.1 riastrad }
153 1.1.2.1 riastrad
154 1.1.2.1 riastrad aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n",
155 1.1.2.1 riastrad intrstr, name);
156 1.1.2.1 riastrad *cookiep = (struct drm_bus_irq_cookie *)ih_cookie;
157 1.1.2.1 riastrad return 0;
158 1.1.2.1 riastrad }
159 1.1.2.1 riastrad
160 1.1.2.1 riastrad static void
161 1.1.2.1 riastrad drm_pci_irq_uninstall(struct drm_device *dev,
162 1.1.2.1 riastrad struct drm_bus_irq_cookie *cookie)
163 1.1.2.1 riastrad {
164 1.1.2.1 riastrad const struct pci_attach_args *pa = drm_pci_attach_args(dev);
165 1.1.2.1 riastrad
166 1.1.2.1 riastrad pci_intr_disestablish(pa->pa_pc, (void *)cookie);
167 1.1.2.1 riastrad }
168 1.1.2.1 riastrad
169 1.1.2.1 riastrad static const char *
170 1.1.2.1 riastrad drm_pci_get_name(struct drm_device *dev)
171 1.1.2.1 riastrad {
172 1.1.2.1 riastrad return "pci"; /* XXX PCI bus names? */
173 1.1.2.1 riastrad }
174 1.1.2.1 riastrad
175 1.1.2.1 riastrad static int
176 1.1.2.1 riastrad drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
177 1.1.2.1 riastrad {
178 1.1.2.1 riastrad return -ENOSYS; /* XXX PCI bus ids? */
179 1.1.2.1 riastrad }
180 1.1.2.1 riastrad
181 1.1.2.1 riastrad static int
182 1.1.2.1 riastrad drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
183 1.1.2.1 riastrad struct drm_unique *unique)
184 1.1.2.1 riastrad {
185 1.1.2.1 riastrad return -ENOSYS; /* XXX */
186 1.1.2.1 riastrad }
187 1.1.2.1 riastrad
188 1.1.2.1 riastrad static int
189 1.1.2.1 riastrad drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
190 1.1.2.1 riastrad {
191 1.1.2.1 riastrad return -ENOSYS; /* XXX */
192 1.1.2.1 riastrad }
193 1.1.2.1 riastrad
194 1.1.2.1 riastrad static int
195 1.1.2.1 riastrad drm_pci_agp_init(struct drm_device *dev)
196 1.1.2.1 riastrad {
197 1.1.2.5 riastrad return 0; /* XXX */
198 1.1.2.1 riastrad }
199