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