drm_pci.c revision 1.4 1 /* $NetBSD: drm_pci.c,v 1.4 2014/07/16 20:56:25 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.4 2014/07/16 20:56:25 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
57 const struct drm_bus drm_pci_bus = {
58 .bus_type = DRIVER_BUS_PCI,
59 .get_irq = drm_pci_get_irq,
60 .irq_install = drm_pci_irq_install,
61 .irq_uninstall = drm_pci_irq_uninstall,
62 .get_name = drm_pci_get_name,
63 .set_busid = drm_pci_set_busid,
64 .set_unique = drm_pci_set_unique,
65 .irq_by_busid = drm_pci_irq_by_busid,
66 };
67
68 static const struct pci_attach_args *
69 drm_pci_attach_args(struct drm_device *dev)
70 {
71 return &dev->pdev->pd_pa;
72 }
73
74 int
75 drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver __unused)
76 {
77
78 driver->bus = &drm_pci_bus;
79 return 0;
80 }
81
82 void
83 drm_pci_exit(struct drm_driver *driver __unused,
84 struct pci_driver *pdriver __unused)
85 {
86 }
87
88 int
89 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
90 struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
91 struct drm_device **devp)
92 {
93 struct drm_device *dev;
94 unsigned int unit;
95 int ret;
96
97 /* Initialize the Linux PCI device descriptor. */
98 linux_pci_dev_init(pdev, self, pa, 0);
99
100 /* Create a DRM device. */
101 dev = drm_dev_alloc(driver, self);
102 if (dev == NULL) {
103 ret = -ENOMEM;
104 goto fail0;
105 }
106
107 dev->pdev = pdev;
108
109 /* XXX Set the power state to D0? */
110
111 /* Set up the bus space and bus DMA tags. */
112 dev->bst = pa->pa_memt;
113 /* XXX Let the driver say something about 32-bit vs 64-bit DMA? */
114 dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
115 dev->dmat = dev->bus_dmat;
116 dev->dmat_subregion_p = false;
117
118 /* Find all the memory maps. */
119 CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
120 dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
121 sizeof(dev->bus_maps[0]), KM_SLEEP);
122 dev->bus_nmaps = PCI_NUM_RESOURCES;
123 for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
124 struct drm_bus_map *const bm = &dev->bus_maps[unit];
125 const int reg = PCI_BAR(unit);
126 const pcireg_t type =
127 pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
128
129 /* Reject non-memory mappings. */
130 if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
131 aprint_debug_dev(self, "map %u has non-memory type:"
132 " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
133 continue;
134 }
135
136 /* Inquire about it. We'll map it in drm_ioremap. */
137 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
138 &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
139 aprint_debug_dev(self, "map %u failed\n", unit);
140 continue;
141 }
142
143 /* Assume since it is a memory mapping it can be linear. */
144 bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
145 }
146
147 /* Set up AGP stuff if requested. */
148 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
149 if (drm_pci_device_is_agp(dev))
150 dev->agp = drm_agp_init(dev);
151 if (dev->agp)
152 dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
153 dev->agp->agp_info.aki_info.ai_aperture_size);
154 }
155
156 /* Register the DRM device and do driver-specific initialization. */
157 ret = drm_dev_register(dev, cookie);
158 if (ret)
159 goto fail1;
160
161 /* Success! */
162 *devp = dev;
163 return 0;
164
165 fail2: __unused
166 drm_dev_unregister(dev);
167 fail1: drm_pci_agp_destroy(dev);
168 dev->bus_nmaps = 0;
169 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
170 KASSERT(!dev->dmat_subregion_p);
171 drm_dev_unref(dev);
172 fail0: return ret;
173 }
174
175 int
176 drm_pci_detach(struct drm_device *dev, int flags __unused)
177 {
178
179 /* Do driver-specific detachment and unregister the device. */
180 drm_dev_unregister(dev);
181
182 /* Tear down AGP stuff if necessary. */
183 if (dev->agp) {
184 arch_phys_wc_del(dev->agp->agp_mtrr);
185 drm_agp_clear(dev);
186 kfree(dev->agp); /* XXX Should go in drm_agp_clear... */
187 dev->agp = NULL;
188 }
189
190 /* Free the record of available bus space mappings. */
191 dev->bus_nmaps = 0;
192 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
193
194 /* Tear down bus space and bus DMA tags. */
195 if (dev->dmat_subregion_p)
196 bus_dmatag_destroy(dev->dmat);
197
198 drm_dev_unref(dev);
199
200 return 0;
201 }
202
203 void
204 drm_pci_agp_destroy(struct drm_device *dev)
205 {
206
207 if (dev->agp) {
208 arch_phys_wc_del(dev->agp->agp_mtrr);
209 drm_agp_clear(dev);
210 kfree(dev->agp); /* XXX Should go in drm_agp_clear... */
211 dev->agp = NULL;
212 }
213 }
214
215 static int
216 drm_pci_get_irq(struct drm_device *dev)
217 {
218 pci_intr_handle_t ih_pih;
219 int ih_int;
220
221 /*
222 * This is a compile-time assertion that the types match. If
223 * this fails, we have to change a bunch of drm code that uses
224 * int for intr handles.
225 */
226 KASSERT(&ih_pih != &ih_int);
227
228 if (pci_intr_map(drm_pci_attach_args(dev), &ih_pih))
229 return -1; /* XXX Hope -1 is an invalid intr handle. */
230
231 ih_int = ih_pih;
232 return ih_int;
233 }
234
235 static int
236 drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
237 int flags, const char *name, void *arg,
238 struct drm_bus_irq_cookie **cookiep)
239 {
240 const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
241 pci_intr_handle_t ih;
242 const char *intrstr;
243 void *ih_cookie;
244 char intrbuf[PCI_INTRSTR_LEN];
245
246 if (pci_intr_map(pa, &ih))
247 return -ENOENT;
248
249 intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
250 ih_cookie = pci_intr_establish(pa->pa_pc, ih, IPL_DRM, handler, arg);
251 if (ih_cookie == NULL) {
252 aprint_error_dev(dev->dev,
253 "couldn't establish interrupt at %s (%s)\n",
254 intrstr, name);
255 return -ENOENT;
256 }
257
258 aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n",
259 intrstr, name);
260 *cookiep = (struct drm_bus_irq_cookie *)ih_cookie;
261 return 0;
262 }
263
264 static void
265 drm_pci_irq_uninstall(struct drm_device *dev,
266 struct drm_bus_irq_cookie *cookie)
267 {
268 const struct pci_attach_args *pa = drm_pci_attach_args(dev);
269
270 pci_intr_disestablish(pa->pa_pc, (void *)cookie);
271 }
272
273 static const char *
274 drm_pci_get_name(struct drm_device *dev)
275 {
276 return "pci"; /* XXX PCI bus names? */
277 }
278
279 static int
280 drm_pci_format_unique(struct drm_device *dev, char *buf, size_t size)
281 {
282 const unsigned int domain = 0; /* XXX PCI domains? */
283 const unsigned int bus = dev->pdev->pd_pa.pa_bus;
284 const unsigned int device = dev->pdev->pd_pa.pa_device;
285 const unsigned int function = dev->pdev->pd_pa.pa_function;
286
287 return snprintf(buf, size, "pci:%04x:%02x:%02x.%d",
288 domain, bus, device, function);
289 }
290
291 static int
292 drm_pci_format_devname(struct drm_device *dev, const char *unique,
293 char *buf, size_t size)
294 {
295
296 return snprintf(buf, size, "%s@%s",
297 device_xname(device_parent(dev->dev)),
298 unique);
299 }
300
301 static int
302 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
303 {
304 int n;
305 char *buf;
306
307 n = drm_pci_format_unique(dev, NULL, 0);
308 if (n < 0)
309 return -ENOSPC; /* XXX */
310 if (0xff < n)
311 n = 0xff;
312
313 buf = kzalloc(n + 1, GFP_KERNEL);
314 (void)drm_pci_format_unique(dev, buf, n + 1);
315
316 if (master->unique)
317 kfree(master->unique);
318 master->unique = buf;
319 master->unique_len = n;
320 master->unique_size = n + 1;
321
322 n = drm_pci_format_devname(dev, master->unique, NULL, 0);
323 if (n < 0)
324 return -ENOSPC; /* XXX back out? */
325 if (0xff < n)
326 n = 0xff;
327
328 buf = kzalloc(n + 1, GFP_KERNEL);
329 (void)drm_pci_format_devname(dev, master->unique, buf, n + 1);
330
331 if (dev->devname)
332 kfree(dev->devname);
333 dev->devname = buf;
334
335 return 0;
336 }
337
338 static int
339 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
340 struct drm_unique *unique __unused)
341 {
342
343 /*
344 * XXX This is silly. We're supposed to reject unique names
345 * that don't match the ones we would generate anyway. For
346 * expedience, we'll just generate the one we would and ignore
347 * whatever userland threw at us...
348 */
349 return drm_pci_set_busid(dev, master);
350 }
351
352 static int
353 drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
354 {
355 return -ENOSYS; /* XXX */
356 }
357