nouveaufb.c revision 1.2 1 1.2 jmcneill /* $NetBSD: nouveaufb.c,v 1.2 2015/04/04 15:12:39 jmcneill Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: nouveaufb.c,v 1.2 2015/04/04 15:12:39 jmcneill Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/bus.h>
37 1.1 riastrad #include <sys/device.h>
38 1.1 riastrad #include <sys/errno.h>
39 1.1 riastrad
40 1.1 riastrad #include <drm/drmP.h>
41 1.1 riastrad #include <drm/drmfb.h>
42 1.1 riastrad #include <drm/drmfb_pci.h>
43 1.1 riastrad
44 1.1 riastrad #include "nouveau_bo.h"
45 1.1 riastrad #include "nouveau_drm.h"
46 1.1 riastrad #include "nouveau_fbcon.h"
47 1.1 riastrad #include "nouveau_pci.h"
48 1.1 riastrad #include "nouveaufb.h"
49 1.1 riastrad
50 1.1 riastrad static int nouveaufb_match(device_t, cfdata_t, void *);
51 1.1 riastrad static void nouveaufb_attach(device_t, device_t, void *);
52 1.1 riastrad static int nouveaufb_detach(device_t, int);
53 1.1 riastrad
54 1.1 riastrad static void nouveaufb_attach_task(struct nouveau_task *);
55 1.1 riastrad
56 1.1 riastrad static bool nouveaufb_shutdown(device_t, int);
57 1.1 riastrad static paddr_t nouveaufb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
58 1.1 riastrad
59 1.1 riastrad struct nouveaufb_softc {
60 1.1 riastrad struct drmfb_softc sc_drmfb; /* XXX Must be first. */
61 1.1 riastrad device_t sc_dev;
62 1.1 riastrad struct nouveaufb_attach_args sc_nfa;
63 1.1 riastrad struct nouveau_task sc_attach_task;
64 1.1 riastrad bool sc_scheduled:1;
65 1.1 riastrad bool sc_attached:1;
66 1.1 riastrad };
67 1.1 riastrad
68 1.1 riastrad static const struct drmfb_params nouveaufb_drmfb_params = {
69 1.1 riastrad .dp_mmapfb = nouveaufb_drmfb_mmapfb,
70 1.1 riastrad .dp_mmap = drmfb_pci_mmap,
71 1.1 riastrad .dp_ioctl = drmfb_pci_ioctl,
72 1.1 riastrad .dp_is_vga_console = drmfb_pci_is_vga_console,
73 1.1 riastrad };
74 1.1 riastrad
75 1.1 riastrad CFATTACH_DECL_NEW(nouveaufb, sizeof(struct nouveaufb_softc),
76 1.1 riastrad nouveaufb_match, nouveaufb_attach, nouveaufb_detach, NULL);
77 1.1 riastrad
78 1.1 riastrad static int
79 1.1 riastrad nouveaufb_match(device_t parent, cfdata_t match, void *aux)
80 1.1 riastrad {
81 1.1 riastrad
82 1.1 riastrad return 1;
83 1.1 riastrad }
84 1.1 riastrad
85 1.1 riastrad static void
86 1.1 riastrad nouveaufb_attach(device_t parent, device_t self, void *aux)
87 1.1 riastrad {
88 1.1 riastrad struct nouveaufb_softc *const sc = device_private(self);
89 1.1 riastrad const struct nouveaufb_attach_args *const nfa = aux;
90 1.1 riastrad int error;
91 1.1 riastrad
92 1.1 riastrad sc->sc_dev = self;
93 1.1 riastrad sc->sc_nfa = *nfa;
94 1.1 riastrad sc->sc_scheduled = false;
95 1.1 riastrad sc->sc_attached = false;
96 1.1 riastrad
97 1.1 riastrad aprint_naive("\n");
98 1.1 riastrad aprint_normal("\n");
99 1.1 riastrad
100 1.1 riastrad nouveau_task_init(&sc->sc_attach_task, &nouveaufb_attach_task);
101 1.1 riastrad error = nouveau_task_schedule(parent, &sc->sc_attach_task);
102 1.1 riastrad if (error) {
103 1.1 riastrad aprint_error_dev(self, "failed to schedule mode set: %d\n",
104 1.1 riastrad error);
105 1.1 riastrad goto fail0;
106 1.1 riastrad }
107 1.1 riastrad sc->sc_scheduled = true;
108 1.1 riastrad
109 1.1 riastrad /* Success! */
110 1.1 riastrad return;
111 1.1 riastrad
112 1.1 riastrad fail0: return;
113 1.1 riastrad }
114 1.1 riastrad
115 1.1 riastrad static int
116 1.1 riastrad nouveaufb_detach(device_t self, int flags)
117 1.1 riastrad {
118 1.1 riastrad struct nouveaufb_softc *const sc = device_private(self);
119 1.1 riastrad int error;
120 1.1 riastrad
121 1.1 riastrad if (sc->sc_scheduled)
122 1.1 riastrad return EBUSY;
123 1.1 riastrad
124 1.1 riastrad if (sc->sc_attached) {
125 1.1 riastrad pmf_device_deregister(self);
126 1.1 riastrad error = drmfb_detach(&sc->sc_drmfb, flags);
127 1.1 riastrad if (error) {
128 1.1 riastrad /* XXX Ugh. */
129 1.1 riastrad (void)pmf_device_register1(self, NULL, NULL,
130 1.1 riastrad &nouveaufb_shutdown);
131 1.1 riastrad return error;
132 1.1 riastrad }
133 1.1 riastrad sc->sc_attached = false;
134 1.1 riastrad }
135 1.1 riastrad
136 1.1 riastrad return 0;
137 1.1 riastrad }
138 1.1 riastrad
139 1.1 riastrad static void
140 1.1 riastrad nouveaufb_attach_task(struct nouveau_task *task)
141 1.1 riastrad {
142 1.1 riastrad struct nouveaufb_softc *const sc = container_of(task,
143 1.1 riastrad struct nouveaufb_softc, sc_attach_task);
144 1.1 riastrad const struct nouveaufb_attach_args *const nfa = &sc->sc_nfa;
145 1.1 riastrad const struct drmfb_attach_args da = {
146 1.1 riastrad .da_dev = sc->sc_dev,
147 1.1 riastrad .da_fb_helper = nfa->nfa_fb_helper,
148 1.1 riastrad .da_fb_sizes = &nfa->nfa_fb_sizes,
149 1.1 riastrad .da_fb_vaddr = __UNVOLATILE(nfa->nfa_fb_ptr),
150 1.1 riastrad .da_params = &nouveaufb_drmfb_params,
151 1.1 riastrad };
152 1.1 riastrad int error;
153 1.1 riastrad
154 1.1 riastrad error = drmfb_attach(&sc->sc_drmfb, &da);
155 1.1 riastrad if (error) {
156 1.1 riastrad aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
157 1.1 riastrad error);
158 1.1 riastrad return;
159 1.1 riastrad }
160 1.1 riastrad
161 1.2 jmcneill if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &nouveaufb_shutdown))
162 1.1 riastrad aprint_error_dev(sc->sc_dev,
163 1.1 riastrad "failed to register shutdown handler\n");
164 1.1 riastrad
165 1.1 riastrad sc->sc_attached = true;
166 1.1 riastrad }
167 1.1 riastrad
168 1.1 riastrad static bool
169 1.1 riastrad nouveaufb_shutdown(device_t self, int flags)
170 1.1 riastrad {
171 1.1 riastrad struct nouveaufb_softc *const sc = device_private(self);
172 1.1 riastrad
173 1.1 riastrad return drmfb_shutdown(&sc->sc_drmfb, flags);
174 1.1 riastrad }
175 1.1 riastrad
176 1.1 riastrad static paddr_t
177 1.1 riastrad nouveaufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
178 1.1 riastrad {
179 1.1 riastrad struct nouveaufb_softc *const sc = container_of(drmfb,
180 1.1 riastrad struct nouveaufb_softc, sc_drmfb);
181 1.1 riastrad struct drm_fb_helper *const helper = sc->sc_nfa.nfa_fb_helper;
182 1.1 riastrad struct nouveau_fbdev *const fbdev = container_of(helper,
183 1.1 riastrad struct nouveau_fbdev, helper);
184 1.1 riastrad struct nouveau_bo *const nvbo = fbdev->nouveau_fb.nvbo;
185 1.1 riastrad const unsigned num_pages __diagused = nvbo->bo.num_pages;
186 1.1 riastrad int flags = 0;
187 1.1 riastrad
188 1.1 riastrad KASSERT(0 <= offset);
189 1.1 riastrad KASSERT(offset < (num_pages << PAGE_SHIFT));
190 1.1 riastrad
191 1.1 riastrad if (ISSET(nvbo->bo.mem.placement, TTM_PL_FLAG_WC))
192 1.1 riastrad flags |= BUS_SPACE_MAP_PREFETCHABLE;
193 1.1 riastrad
194 1.1 riastrad return bus_space_mmap(nvbo->bo.bdev->memt, nvbo->bo.mem.bus.base,
195 1.1 riastrad nvbo->bo.mem.bus.offset + offset, prot, flags);
196 1.1 riastrad }
197