drm_pci.c revision 1.1.2.7 1 /* $NetBSD: drm_pci.c,v 1.1.2.7 2013/07/24 03:58:51 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 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: drm_pci.c,v 1.1.2.7 2013/07/24 03:58:51 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/systm.h>
38
39 #include <dev/pci/pcivar.h>
40
41 #include <drm/drmP.h>
42
43 static int drm_pci_get_irq(struct drm_device *);
44 static int drm_pci_irq_install(struct drm_device *,
45 irqreturn_t (*)(void *), int, const char *, void *,
46 struct drm_bus_irq_cookie **);
47 static void drm_pci_irq_uninstall(struct drm_device *,
48 struct drm_bus_irq_cookie *);
49 static const char *
50 drm_pci_get_name(struct drm_device *);
51 static int drm_pci_set_busid(struct drm_device *, struct drm_master *);
52 static int drm_pci_set_unique(struct drm_device *, struct drm_master *,
53 struct drm_unique *);
54 static int drm_pci_irq_by_busid(struct drm_device *,
55 struct drm_irq_busid *);
56 static int drm_pci_agp_init(struct drm_device *);
57
58 const struct drm_bus drm_pci_bus = {
59 .bus_type = DRIVER_BUS_PCI,
60 .get_irq = drm_pci_get_irq,
61 .irq_install = drm_pci_irq_install,
62 .irq_uninstall = drm_pci_irq_uninstall,
63 .get_name = drm_pci_get_name,
64 .set_busid = drm_pci_set_busid,
65 .set_unique = drm_pci_set_unique,
66 .irq_by_busid = drm_pci_irq_by_busid,
67 .agp_init = drm_pci_agp_init,
68 };
69
70 static const struct pci_attach_args *
71 drm_pci_attach_args(struct drm_device *dev)
72 {
73 return &dev->pdev->pd_pa;
74 }
75
76 int
77 drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver __unused)
78 {
79
80 driver->bus = &drm_pci_bus;
81 return 0;
82 }
83
84 void
85 drm_pci_exit(struct drm_driver *driver __unused,
86 struct pci_driver *pdriver __unused)
87 {
88 }
89
90 /* XXX Should go elsewhere. */
91 #define PCI_NMAPREGS ((PCI_MAPREG_END - PCI_MAPREG_START) / 4)
92
93 void
94 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
95 struct pci_dev *pdev, struct drm_device *dev)
96 {
97 unsigned int unit;
98
99 linux_pci_dev_init(pdev, self, pa, 0);
100
101 dev->pdev = pdev;
102 dev->pci_vendor = pdev->vendor;
103 dev->pci_device = pdev->device;
104
105 /* XXX Set the power state to D0? */
106
107 dev->bst = pa->pa_memt;
108 dev->bus_dmat = pa->pa_dmat; /* XXX dmat64? */
109 dev->dmat_subregion_p = false;
110
111 CTASSERT(PCI_NMAPREGS < (SIZE_MAX / sizeof(dev->bus_maps[0])));
112 dev->bus_nmaps = PCI_NMAPREGS;
113 dev->bus_maps = kmem_zalloc((PCI_NMAPREGS * sizeof(dev->bus_maps[0])),
114 KM_SLEEP);
115 for (unit = 0; unit < dev->bus_nmaps; unit++) {
116 struct drm_bus_map *const bm = &dev->bus_maps[unit];
117 const int reg = (PCI_MAPREG_START + (unit*4));
118 const pcireg_t type =
119 pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
120
121 /* Reject non-memory mappings. */
122 if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
123 aprint_debug_dev(self, "map %u has non-memory type:"
124 " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
125 continue;
126 }
127
128 /* Inquire about it. We'll map it in drm_ioremap. */
129 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
130 &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
131 aprint_debug_dev(self, "map %u failed\n", unit);
132 continue;
133 }
134
135 aprint_debug_dev(self, "map %u at %"PRIxMAX" size %"PRIxMAX
136 " flags 0x%"PRIxMAX"\n",
137 unit, (uintmax_t)bm->bm_base, (uintmax_t)bm->bm_size,
138 (uintmax_t)bm->bm_flags);
139
140 /* Assume since it is a memory mapping it can be linear. */
141 bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
142 }
143
144 /* All `agp' maps are just initialized to zero. */
145 CTASSERT(PCI_NMAPREGS < (SIZE_MAX / sizeof(dev->agp_maps[0])));
146 dev->agp_nmaps = PCI_NMAPREGS;
147 dev->agp_maps = kmem_zalloc((PCI_NMAPREGS * sizeof(dev->agp_maps[0])),
148 KM_SLEEP);
149 }
150
151 int
152 drm_pci_detach(struct drm_device *dev __unused, int flags __unused)
153 {
154
155 kmem_free(dev->bus_maps, (PCI_NMAPREGS * sizeof(dev->bus_maps[0])));
156 kmem_free(dev->agp_maps, (PCI_NMAPREGS * sizeof(dev->agp_maps[0])));
157
158 /*
159 * XXX This is the wrong place! Should be a routine in
160 * drm_memory.c.
161 */
162 if (dev->dmat_subregion_p)
163 bus_dmatag_destroy(dev->dmat);
164
165 /* XXX Disestablish irqs or anything? */
166 return 0;
167 }
168
169 static int
170 drm_pci_get_irq(struct drm_device *dev)
171 {
172 pci_intr_handle_t ih_pih;
173 int ih_int;
174
175 /*
176 * This is a compile-time assertion that the types match. If
177 * this fails, we have to change a bunch of drm code that uses
178 * int for intr handles.
179 */
180 KASSERT(&ih_pih != &ih_int);
181
182 if (pci_intr_map(drm_pci_attach_args(dev), &ih_pih))
183 return -1; /* XXX Hope -1 is an invalid intr handle. */
184
185 ih_int = ih_pih;
186 return ih_int;
187 }
188
189 static int
190 drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
191 int flags, const char *name, void *arg,
192 struct drm_bus_irq_cookie **cookiep)
193 {
194 const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
195 pci_intr_handle_t ih;
196 const char *intrstr;
197 void *ih_cookie;
198
199 if (pci_intr_map(pa, &ih))
200 return -ENOENT;
201
202 intrstr = pci_intr_string(pa->pa_pc, ih);
203 ih_cookie = pci_intr_establish(pa->pa_pc, ih, IPL_DRM, handler, arg);
204 if (ih_cookie == NULL) {
205 aprint_error_dev(dev->dev,
206 "couldn't establish interrupt at %s (%s)\n",
207 intrstr, name);
208 return -ENOENT;
209 }
210
211 aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n",
212 intrstr, name);
213 *cookiep = (struct drm_bus_irq_cookie *)ih_cookie;
214 return 0;
215 }
216
217 static void
218 drm_pci_irq_uninstall(struct drm_device *dev,
219 struct drm_bus_irq_cookie *cookie)
220 {
221 const struct pci_attach_args *pa = drm_pci_attach_args(dev);
222
223 pci_intr_disestablish(pa->pa_pc, (void *)cookie);
224 }
225
226 static const char *
227 drm_pci_get_name(struct drm_device *dev)
228 {
229 return "pci"; /* XXX PCI bus names? */
230 }
231
232 static int
233 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
234 {
235 return -ENOSYS; /* XXX PCI bus ids? */
236 }
237
238 static int
239 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
240 struct drm_unique *unique)
241 {
242 return -ENOSYS; /* XXX */
243 }
244
245 static int
246 drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
247 {
248 return -ENOSYS; /* XXX */
249 }
250
251 static int
252 drm_pci_agp_init(struct drm_device *dev)
253 {
254 return 0; /* XXX */
255 }
256