drm_pci.c revision 1.1.2.5 1 1.1.2.1 riastrad /* $NetBSD: drm_pci.c,v 1.1.2.5 2013/07/24 03:54:14 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.5 2013/07/24 03:54:14 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.2 riastrad void
77 1.1.2.2 riastrad drm_pci_attach(device_t self, const struct pci_attach_args *pa,
78 1.1.2.2 riastrad struct pci_dev *pdev, struct drm_device *dev)
79 1.1.2.2 riastrad {
80 1.1.2.2 riastrad
81 1.1.2.4 riastrad dev->driver->bus = &drm_pci_bus;
82 1.1.2.4 riastrad
83 1.1.2.3 riastrad linux_pci_dev_init(pdev, self, pa, 0);
84 1.1.2.2 riastrad
85 1.1.2.2 riastrad dev->pdev = pdev;
86 1.1.2.2 riastrad dev->pci_vendor = pdev->vendor;
87 1.1.2.2 riastrad dev->pci_device = pdev->device;
88 1.1.2.2 riastrad
89 1.1.2.2 riastrad /* XXX Set the power state to D0? */
90 1.1.2.2 riastrad }
91 1.1.2.2 riastrad
92 1.1.2.2 riastrad int
93 1.1.2.2 riastrad drm_pci_detach(struct drm_device *dev __unused, int flags __unused)
94 1.1.2.2 riastrad {
95 1.1.2.2 riastrad
96 1.1.2.2 riastrad /* XXX Disestablish irqs or anything? */
97 1.1.2.2 riastrad return 0;
98 1.1.2.2 riastrad }
99 1.1.2.2 riastrad
100 1.1.2.1 riastrad static int
101 1.1.2.1 riastrad drm_pci_get_irq(struct drm_device *dev)
102 1.1.2.1 riastrad {
103 1.1.2.1 riastrad pci_intr_handle_t ih_pih;
104 1.1.2.1 riastrad int ih_int;
105 1.1.2.1 riastrad
106 1.1.2.1 riastrad /*
107 1.1.2.1 riastrad * This is a compile-time assertion that the types match. If
108 1.1.2.1 riastrad * this fails, we have to change a bunch of drm code that uses
109 1.1.2.1 riastrad * int for intr handles.
110 1.1.2.1 riastrad */
111 1.1.2.1 riastrad KASSERT(&ih_pih != &ih_int);
112 1.1.2.1 riastrad
113 1.1.2.1 riastrad if (pci_intr_map(drm_pci_attach_args(dev), &ih_pih))
114 1.1.2.1 riastrad return -1; /* XXX Hope -1 is an invalid intr handle. */
115 1.1.2.1 riastrad
116 1.1.2.1 riastrad ih_int = ih_pih;
117 1.1.2.1 riastrad return ih_int;
118 1.1.2.1 riastrad }
119 1.1.2.1 riastrad
120 1.1.2.1 riastrad static int
121 1.1.2.1 riastrad drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
122 1.1.2.1 riastrad int flags, const char *name, void *arg,
123 1.1.2.1 riastrad struct drm_bus_irq_cookie **cookiep)
124 1.1.2.1 riastrad {
125 1.1.2.1 riastrad const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
126 1.1.2.1 riastrad pci_intr_handle_t ih;
127 1.1.2.1 riastrad const char *intrstr;
128 1.1.2.1 riastrad void *ih_cookie;
129 1.1.2.1 riastrad
130 1.1.2.1 riastrad if (pci_intr_map(pa, &ih))
131 1.1.2.1 riastrad return -ENOENT;
132 1.1.2.1 riastrad
133 1.1.2.1 riastrad intrstr = pci_intr_string(pa->pa_pc, ih);
134 1.1.2.1 riastrad ih_cookie = pci_intr_establish(pa->pa_pc, ih, IPL_DRM, handler, arg);
135 1.1.2.1 riastrad if (ih_cookie == NULL) {
136 1.1.2.1 riastrad aprint_error_dev(dev->dev,
137 1.1.2.1 riastrad "couldn't establish interrupt at %s (%s)\n",
138 1.1.2.1 riastrad intrstr, name);
139 1.1.2.1 riastrad return -ENOENT;
140 1.1.2.1 riastrad }
141 1.1.2.1 riastrad
142 1.1.2.1 riastrad aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n",
143 1.1.2.1 riastrad intrstr, name);
144 1.1.2.1 riastrad *cookiep = (struct drm_bus_irq_cookie *)ih_cookie;
145 1.1.2.1 riastrad return 0;
146 1.1.2.1 riastrad }
147 1.1.2.1 riastrad
148 1.1.2.1 riastrad static void
149 1.1.2.1 riastrad drm_pci_irq_uninstall(struct drm_device *dev,
150 1.1.2.1 riastrad struct drm_bus_irq_cookie *cookie)
151 1.1.2.1 riastrad {
152 1.1.2.1 riastrad const struct pci_attach_args *pa = drm_pci_attach_args(dev);
153 1.1.2.1 riastrad
154 1.1.2.1 riastrad pci_intr_disestablish(pa->pa_pc, (void *)cookie);
155 1.1.2.1 riastrad }
156 1.1.2.1 riastrad
157 1.1.2.1 riastrad static const char *
158 1.1.2.1 riastrad drm_pci_get_name(struct drm_device *dev)
159 1.1.2.1 riastrad {
160 1.1.2.1 riastrad return "pci"; /* XXX PCI bus names? */
161 1.1.2.1 riastrad }
162 1.1.2.1 riastrad
163 1.1.2.1 riastrad static int
164 1.1.2.1 riastrad drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
165 1.1.2.1 riastrad {
166 1.1.2.1 riastrad return -ENOSYS; /* XXX PCI bus ids? */
167 1.1.2.1 riastrad }
168 1.1.2.1 riastrad
169 1.1.2.1 riastrad static int
170 1.1.2.1 riastrad drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
171 1.1.2.1 riastrad struct drm_unique *unique)
172 1.1.2.1 riastrad {
173 1.1.2.1 riastrad return -ENOSYS; /* XXX */
174 1.1.2.1 riastrad }
175 1.1.2.1 riastrad
176 1.1.2.1 riastrad static int
177 1.1.2.1 riastrad drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
178 1.1.2.1 riastrad {
179 1.1.2.1 riastrad return -ENOSYS; /* XXX */
180 1.1.2.1 riastrad }
181 1.1.2.1 riastrad
182 1.1.2.1 riastrad static int
183 1.1.2.1 riastrad drm_pci_agp_init(struct drm_device *dev)
184 1.1.2.1 riastrad {
185 1.1.2.5 riastrad return 0; /* XXX */
186 1.1.2.1 riastrad }
187