1 // SPDX-License-Identifier: GPL-2.0-or-later
7 #include <drm/drm_drv.h>
8 #include <drm/drm_fourcc.h>
12 /* ---------------------------------------------------------------------- */
14 static void bochs_vga_writeb(struct bochs_device
*bochs
, u16 ioport
, u8 val
)
16 if (WARN_ON(ioport
< 0x3c0 || ioport
> 0x3df))
20 int offset
= ioport
- 0x3c0 + 0x400;
21 writeb(val
, bochs
->mmio
+ offset
);
27 static u16
bochs_dispi_read(struct bochs_device
*bochs
, u16 reg
)
32 int offset
= 0x500 + (reg
<< 1);
33 ret
= readw(bochs
->mmio
+ offset
);
35 outw(reg
, VBE_DISPI_IOPORT_INDEX
);
36 ret
= inw(VBE_DISPI_IOPORT_DATA
);
41 static void bochs_dispi_write(struct bochs_device
*bochs
, u16 reg
, u16 val
)
44 int offset
= 0x500 + (reg
<< 1);
45 writew(val
, bochs
->mmio
+ offset
);
47 outw(reg
, VBE_DISPI_IOPORT_INDEX
);
48 outw(val
, VBE_DISPI_IOPORT_DATA
);
52 static void bochs_hw_set_big_endian(struct bochs_device
*bochs
)
54 if (bochs
->qext_size
< 8)
57 writel(0xbebebebe, bochs
->mmio
+ 0x604);
60 static void bochs_hw_set_little_endian(struct bochs_device
*bochs
)
62 if (bochs
->qext_size
< 8)
65 writel(0x1e1e1e1e, bochs
->mmio
+ 0x604);
69 #define bochs_hw_set_native_endian(_b) bochs_hw_set_big_endian(_b)
71 #define bochs_hw_set_native_endian(_b) bochs_hw_set_little_endian(_b)
74 static int bochs_get_edid_block(void *data
, u8
*buf
,
75 unsigned int block
, size_t len
)
77 struct bochs_device
*bochs
= data
;
78 size_t i
, start
= block
* EDID_LENGTH
;
80 if (start
+ len
> 0x400 /* vga register offset */)
83 for (i
= 0; i
< len
; i
++) {
84 buf
[i
] = readb(bochs
->mmio
+ start
+ i
);
89 int bochs_hw_load_edid(struct bochs_device
*bochs
)
96 /* check header to detect whenever edid support is enabled in qemu */
97 bochs_get_edid_block(bochs
, header
, 0, ARRAY_SIZE(header
));
98 if (drm_edid_header_is_valid(header
) != 8)
102 bochs
->edid
= drm_do_get_edid(&bochs
->connector
,
103 bochs_get_edid_block
, bochs
);
104 if (bochs
->edid
== NULL
)
110 int bochs_hw_init(struct drm_device
*dev
)
112 struct bochs_device
*bochs
= dev
->dev_private
;
113 struct pci_dev
*pdev
= dev
->pdev
;
114 unsigned long addr
, size
, mem
, ioaddr
, iosize
;
117 if (pdev
->resource
[2].flags
& IORESOURCE_MEM
) {
118 /* mmio bar with vga and bochs registers present */
119 if (pci_request_region(pdev
, 2, "bochs-drm") != 0) {
120 DRM_ERROR("Cannot request mmio region\n");
123 ioaddr
= pci_resource_start(pdev
, 2);
124 iosize
= pci_resource_len(pdev
, 2);
125 bochs
->mmio
= ioremap(ioaddr
, iosize
);
126 if (bochs
->mmio
== NULL
) {
127 DRM_ERROR("Cannot map mmio region\n");
131 ioaddr
= VBE_DISPI_IOPORT_INDEX
;
133 if (!request_region(ioaddr
, iosize
, "bochs-drm")) {
134 DRM_ERROR("Cannot request ioports\n");
140 id
= bochs_dispi_read(bochs
, VBE_DISPI_INDEX_ID
);
141 mem
= bochs_dispi_read(bochs
, VBE_DISPI_INDEX_VIDEO_MEMORY_64K
)
143 if ((id
& 0xfff0) != VBE_DISPI_ID0
) {
144 DRM_ERROR("ID mismatch\n");
148 if ((pdev
->resource
[0].flags
& IORESOURCE_MEM
) == 0)
150 addr
= pci_resource_start(pdev
, 0);
151 size
= pci_resource_len(pdev
, 0);
155 DRM_ERROR("Size mismatch: pci=%ld, bochs=%ld\n",
157 size
= min(size
, mem
);
160 if (pci_request_region(pdev
, 0, "bochs-drm") != 0)
161 DRM_WARN("Cannot request framebuffer, boot fb still active?\n");
163 bochs
->fb_map
= ioremap(addr
, size
);
164 if (bochs
->fb_map
== NULL
) {
165 DRM_ERROR("Cannot map framebuffer\n");
168 bochs
->fb_base
= addr
;
169 bochs
->fb_size
= size
;
171 DRM_INFO("Found bochs VGA, ID 0x%x.\n", id
);
172 DRM_INFO("Framebuffer size %ld kB @ 0x%lx, %s @ 0x%lx.\n",
174 bochs
->ioports
? "ioports" : "mmio",
177 if (bochs
->mmio
&& pdev
->revision
>= 2) {
178 bochs
->qext_size
= readl(bochs
->mmio
+ 0x600);
179 if (bochs
->qext_size
< 4 || bochs
->qext_size
> iosize
) {
180 bochs
->qext_size
= 0;
183 DRM_DEBUG("Found qemu ext regs, size %ld\n",
185 bochs_hw_set_native_endian(bochs
);
192 void bochs_hw_fini(struct drm_device
*dev
)
194 struct bochs_device
*bochs
= dev
->dev_private
;
196 /* TODO: shot down existing vram mappings */
199 iounmap(bochs
->mmio
);
201 release_region(VBE_DISPI_IOPORT_INDEX
, 2);
203 iounmap(bochs
->fb_map
);
204 pci_release_regions(dev
->pdev
);
208 void bochs_hw_setmode(struct bochs_device
*bochs
,
209 struct drm_display_mode
*mode
)
213 if (!drm_dev_enter(bochs
->dev
, &idx
))
216 bochs
->xres
= mode
->hdisplay
;
217 bochs
->yres
= mode
->vdisplay
;
219 bochs
->stride
= mode
->hdisplay
* (bochs
->bpp
/ 8);
220 bochs
->yres_virtual
= bochs
->fb_size
/ bochs
->stride
;
222 DRM_DEBUG_DRIVER("%dx%d @ %d bpp, vy %d\n",
223 bochs
->xres
, bochs
->yres
, bochs
->bpp
,
224 bochs
->yres_virtual
);
226 bochs_vga_writeb(bochs
, 0x3c0, 0x20); /* unblank */
228 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_ENABLE
, 0);
229 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_BPP
, bochs
->bpp
);
230 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_XRES
, bochs
->xres
);
231 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_YRES
, bochs
->yres
);
232 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_BANK
, 0);
233 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_VIRT_WIDTH
, bochs
->xres
);
234 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_VIRT_HEIGHT
,
235 bochs
->yres_virtual
);
236 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_X_OFFSET
, 0);
237 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_Y_OFFSET
, 0);
239 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_ENABLE
,
240 VBE_DISPI_ENABLED
| VBE_DISPI_LFB_ENABLED
);
245 void bochs_hw_setformat(struct bochs_device
*bochs
,
246 const struct drm_format_info
*format
)
250 if (!drm_dev_enter(bochs
->dev
, &idx
))
253 DRM_DEBUG_DRIVER("format %c%c%c%c\n",
254 (format
->format
>> 0) & 0xff,
255 (format
->format
>> 8) & 0xff,
256 (format
->format
>> 16) & 0xff,
257 (format
->format
>> 24) & 0xff);
259 switch (format
->format
) {
260 case DRM_FORMAT_XRGB8888
:
261 bochs_hw_set_little_endian(bochs
);
263 case DRM_FORMAT_BGRX8888
:
264 bochs_hw_set_big_endian(bochs
);
267 /* should not happen */
268 DRM_ERROR("%s: Huh? Got framebuffer format 0x%x",
269 __func__
, format
->format
);
276 void bochs_hw_setbase(struct bochs_device
*bochs
,
277 int x
, int y
, int stride
, u64 addr
)
279 unsigned long offset
;
280 unsigned int vx
, vy
, vwidth
, idx
;
282 if (!drm_dev_enter(bochs
->dev
, &idx
))
285 bochs
->stride
= stride
;
286 offset
= (unsigned long)addr
+
288 x
* (bochs
->bpp
/ 8);
289 vy
= offset
/ bochs
->stride
;
290 vx
= (offset
% bochs
->stride
) * 8 / bochs
->bpp
;
291 vwidth
= stride
* 8 / bochs
->bpp
;
293 DRM_DEBUG_DRIVER("x %d, y %d, addr %llx -> offset %lx, vx %d, vy %d\n",
294 x
, y
, addr
, offset
, vx
, vy
);
295 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_VIRT_WIDTH
, vwidth
);
296 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_X_OFFSET
, vx
);
297 bochs_dispi_write(bochs
, VBE_DISPI_INDEX_Y_OFFSET
, vy
);