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