bochs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/module.h>
  3. #include <linux/pci.h>
  4. #include <drm/drm_aperture.h>
  5. #include <drm/drm_atomic_helper.h>
  6. #include <drm/drm_drv.h>
  7. #include <drm/drm_edid.h>
  8. #include <drm/drm_fbdev_ttm.h>
  9. #include <drm/drm_fourcc.h>
  10. #include <drm/drm_framebuffer.h>
  11. #include <drm/drm_gem_framebuffer_helper.h>
  12. #include <drm/drm_gem_vram_helper.h>
  13. #include <drm/drm_managed.h>
  14. #include <drm/drm_module.h>
  15. #include <drm/drm_probe_helper.h>
  16. #include <drm/drm_simple_kms_helper.h>
  17. #include <video/vga.h>
  18. /* ---------------------------------------------------------------------- */
  19. #define VBE_DISPI_IOPORT_INDEX 0x01CE
  20. #define VBE_DISPI_IOPORT_DATA 0x01CF
  21. #define VBE_DISPI_INDEX_ID 0x0
  22. #define VBE_DISPI_INDEX_XRES 0x1
  23. #define VBE_DISPI_INDEX_YRES 0x2
  24. #define VBE_DISPI_INDEX_BPP 0x3
  25. #define VBE_DISPI_INDEX_ENABLE 0x4
  26. #define VBE_DISPI_INDEX_BANK 0x5
  27. #define VBE_DISPI_INDEX_VIRT_WIDTH 0x6
  28. #define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7
  29. #define VBE_DISPI_INDEX_X_OFFSET 0x8
  30. #define VBE_DISPI_INDEX_Y_OFFSET 0x9
  31. #define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa
  32. #define VBE_DISPI_ID0 0xB0C0
  33. #define VBE_DISPI_ID1 0xB0C1
  34. #define VBE_DISPI_ID2 0xB0C2
  35. #define VBE_DISPI_ID3 0xB0C3
  36. #define VBE_DISPI_ID4 0xB0C4
  37. #define VBE_DISPI_ID5 0xB0C5
  38. #define VBE_DISPI_DISABLED 0x00
  39. #define VBE_DISPI_ENABLED 0x01
  40. #define VBE_DISPI_GETCAPS 0x02
  41. #define VBE_DISPI_8BIT_DAC 0x20
  42. #define VBE_DISPI_LFB_ENABLED 0x40
  43. #define VBE_DISPI_NOCLEARMEM 0x80
  44. static int bochs_modeset = -1;
  45. static int defx = 1024;
  46. static int defy = 768;
  47. module_param_named(modeset, bochs_modeset, int, 0444);
  48. MODULE_PARM_DESC(modeset, "enable/disable kernel modesetting");
  49. module_param(defx, int, 0444);
  50. module_param(defy, int, 0444);
  51. MODULE_PARM_DESC(defx, "default x resolution");
  52. MODULE_PARM_DESC(defy, "default y resolution");
  53. /* ---------------------------------------------------------------------- */
  54. enum bochs_types {
  55. BOCHS_QEMU_STDVGA,
  56. BOCHS_SIMICS,
  57. BOCHS_UNKNOWN,
  58. };
  59. struct bochs_device {
  60. /* hw */
  61. void __iomem *mmio;
  62. int ioports;
  63. void __iomem *fb_map;
  64. unsigned long fb_base;
  65. unsigned long fb_size;
  66. unsigned long qext_size;
  67. /* mode */
  68. u16 xres;
  69. u16 yres;
  70. u16 yres_virtual;
  71. u32 stride;
  72. u32 bpp;
  73. const struct drm_edid *drm_edid;
  74. /* drm */
  75. struct drm_device *dev;
  76. struct drm_simple_display_pipe pipe;
  77. struct drm_connector connector;
  78. };
  79. /* ---------------------------------------------------------------------- */
  80. static void bochs_vga_writeb(struct bochs_device *bochs, u16 ioport, u8 val)
  81. {
  82. if (WARN_ON(ioport < 0x3c0 || ioport > 0x3df))
  83. return;
  84. if (bochs->mmio) {
  85. int offset = ioport - 0x3c0 + 0x400;
  86. writeb(val, bochs->mmio + offset);
  87. } else {
  88. outb(val, ioport);
  89. }
  90. }
  91. static u8 bochs_vga_readb(struct bochs_device *bochs, u16 ioport)
  92. {
  93. if (WARN_ON(ioport < 0x3c0 || ioport > 0x3df))
  94. return 0xff;
  95. if (bochs->mmio) {
  96. int offset = ioport - 0x3c0 + 0x400;
  97. return readb(bochs->mmio + offset);
  98. } else {
  99. return inb(ioport);
  100. }
  101. }
  102. static u16 bochs_dispi_read(struct bochs_device *bochs, u16 reg)
  103. {
  104. u16 ret = 0;
  105. if (bochs->mmio) {
  106. int offset = 0x500 + (reg << 1);
  107. ret = readw(bochs->mmio + offset);
  108. } else {
  109. outw(reg, VBE_DISPI_IOPORT_INDEX);
  110. ret = inw(VBE_DISPI_IOPORT_DATA);
  111. }
  112. return ret;
  113. }
  114. static void bochs_dispi_write(struct bochs_device *bochs, u16 reg, u16 val)
  115. {
  116. if (bochs->mmio) {
  117. int offset = 0x500 + (reg << 1);
  118. writew(val, bochs->mmio + offset);
  119. } else {
  120. outw(reg, VBE_DISPI_IOPORT_INDEX);
  121. outw(val, VBE_DISPI_IOPORT_DATA);
  122. }
  123. }
  124. static void bochs_hw_set_big_endian(struct bochs_device *bochs)
  125. {
  126. if (bochs->qext_size < 8)
  127. return;
  128. writel(0xbebebebe, bochs->mmio + 0x604);
  129. }
  130. static void bochs_hw_set_little_endian(struct bochs_device *bochs)
  131. {
  132. if (bochs->qext_size < 8)
  133. return;
  134. writel(0x1e1e1e1e, bochs->mmio + 0x604);
  135. }
  136. #ifdef __BIG_ENDIAN
  137. #define bochs_hw_set_native_endian(_b) bochs_hw_set_big_endian(_b)
  138. #else
  139. #define bochs_hw_set_native_endian(_b) bochs_hw_set_little_endian(_b)
  140. #endif
  141. static int bochs_get_edid_block(void *data, u8 *buf,
  142. unsigned int block, size_t len)
  143. {
  144. struct bochs_device *bochs = data;
  145. size_t i, start = block * EDID_LENGTH;
  146. if (start + len > 0x400 /* vga register offset */)
  147. return -1;
  148. for (i = 0; i < len; i++)
  149. buf[i] = readb(bochs->mmio + start + i);
  150. return 0;
  151. }
  152. static int bochs_hw_load_edid(struct bochs_device *bochs)
  153. {
  154. u8 header[8];
  155. if (!bochs->mmio)
  156. return -1;
  157. /* check header to detect whenever edid support is enabled in qemu */
  158. bochs_get_edid_block(bochs, header, 0, ARRAY_SIZE(header));
  159. if (drm_edid_header_is_valid(header) != 8)
  160. return -1;
  161. drm_edid_free(bochs->drm_edid);
  162. bochs->drm_edid = drm_edid_read_custom(&bochs->connector,
  163. bochs_get_edid_block, bochs);
  164. if (!bochs->drm_edid)
  165. return -1;
  166. return 0;
  167. }
  168. static int bochs_hw_init(struct drm_device *dev)
  169. {
  170. struct bochs_device *bochs = dev->dev_private;
  171. struct pci_dev *pdev = to_pci_dev(dev->dev);
  172. unsigned long addr, size, mem, ioaddr, iosize;
  173. u16 id;
  174. if (pdev->resource[2].flags & IORESOURCE_MEM) {
  175. /* mmio bar with vga and bochs registers present */
  176. if (pci_request_region(pdev, 2, "bochs-drm") != 0) {
  177. DRM_ERROR("Cannot request mmio region\n");
  178. return -EBUSY;
  179. }
  180. ioaddr = pci_resource_start(pdev, 2);
  181. iosize = pci_resource_len(pdev, 2);
  182. bochs->mmio = ioremap(ioaddr, iosize);
  183. if (bochs->mmio == NULL) {
  184. DRM_ERROR("Cannot map mmio region\n");
  185. return -ENOMEM;
  186. }
  187. } else {
  188. ioaddr = VBE_DISPI_IOPORT_INDEX;
  189. iosize = 2;
  190. if (!request_region(ioaddr, iosize, "bochs-drm")) {
  191. DRM_ERROR("Cannot request ioports\n");
  192. return -EBUSY;
  193. }
  194. bochs->ioports = 1;
  195. }
  196. id = bochs_dispi_read(bochs, VBE_DISPI_INDEX_ID);
  197. mem = bochs_dispi_read(bochs, VBE_DISPI_INDEX_VIDEO_MEMORY_64K)
  198. * 64 * 1024;
  199. if ((id & 0xfff0) != VBE_DISPI_ID0) {
  200. DRM_ERROR("ID mismatch\n");
  201. return -ENODEV;
  202. }
  203. if ((pdev->resource[0].flags & IORESOURCE_MEM) == 0)
  204. return -ENODEV;
  205. addr = pci_resource_start(pdev, 0);
  206. size = pci_resource_len(pdev, 0);
  207. if (addr == 0)
  208. return -ENODEV;
  209. if (size != mem) {
  210. DRM_ERROR("Size mismatch: pci=%ld, bochs=%ld\n",
  211. size, mem);
  212. size = min(size, mem);
  213. }
  214. if (pci_request_region(pdev, 0, "bochs-drm") != 0)
  215. DRM_WARN("Cannot request framebuffer, boot fb still active?\n");
  216. bochs->fb_map = ioremap(addr, size);
  217. if (bochs->fb_map == NULL) {
  218. DRM_ERROR("Cannot map framebuffer\n");
  219. return -ENOMEM;
  220. }
  221. bochs->fb_base = addr;
  222. bochs->fb_size = size;
  223. DRM_INFO("Found bochs VGA, ID 0x%x.\n", id);
  224. DRM_INFO("Framebuffer size %ld kB @ 0x%lx, %s @ 0x%lx.\n",
  225. size / 1024, addr,
  226. bochs->ioports ? "ioports" : "mmio",
  227. ioaddr);
  228. if (bochs->mmio && pdev->revision >= 2) {
  229. bochs->qext_size = readl(bochs->mmio + 0x600);
  230. if (bochs->qext_size < 4 || bochs->qext_size > iosize) {
  231. bochs->qext_size = 0;
  232. goto noext;
  233. }
  234. DRM_DEBUG("Found qemu ext regs, size %ld\n",
  235. bochs->qext_size);
  236. bochs_hw_set_native_endian(bochs);
  237. }
  238. noext:
  239. return 0;
  240. }
  241. static void bochs_hw_fini(struct drm_device *dev)
  242. {
  243. struct bochs_device *bochs = dev->dev_private;
  244. /* TODO: shot down existing vram mappings */
  245. if (bochs->mmio)
  246. iounmap(bochs->mmio);
  247. if (bochs->ioports)
  248. release_region(VBE_DISPI_IOPORT_INDEX, 2);
  249. if (bochs->fb_map)
  250. iounmap(bochs->fb_map);
  251. pci_release_regions(to_pci_dev(dev->dev));
  252. drm_edid_free(bochs->drm_edid);
  253. }
  254. static void bochs_hw_blank(struct bochs_device *bochs, bool blank)
  255. {
  256. DRM_DEBUG_DRIVER("hw_blank %d\n", blank);
  257. /* enable color bit (so VGA_IS1_RC access works) */
  258. bochs_vga_writeb(bochs, VGA_MIS_W, VGA_MIS_COLOR);
  259. /* discard ar_flip_flop */
  260. (void)bochs_vga_readb(bochs, VGA_IS1_RC);
  261. /* blank or unblank; we need only update index and set 0x20 */
  262. bochs_vga_writeb(bochs, VGA_ATT_W, blank ? 0 : 0x20);
  263. }
  264. static void bochs_hw_setmode(struct bochs_device *bochs, struct drm_display_mode *mode)
  265. {
  266. int idx;
  267. if (!drm_dev_enter(bochs->dev, &idx))
  268. return;
  269. bochs->xres = mode->hdisplay;
  270. bochs->yres = mode->vdisplay;
  271. bochs->bpp = 32;
  272. bochs->stride = mode->hdisplay * (bochs->bpp / 8);
  273. bochs->yres_virtual = bochs->fb_size / bochs->stride;
  274. DRM_DEBUG_DRIVER("%dx%d @ %d bpp, vy %d\n",
  275. bochs->xres, bochs->yres, bochs->bpp,
  276. bochs->yres_virtual);
  277. bochs_hw_blank(bochs, false);
  278. bochs_dispi_write(bochs, VBE_DISPI_INDEX_ENABLE, 0);
  279. bochs_dispi_write(bochs, VBE_DISPI_INDEX_BPP, bochs->bpp);
  280. bochs_dispi_write(bochs, VBE_DISPI_INDEX_XRES, bochs->xres);
  281. bochs_dispi_write(bochs, VBE_DISPI_INDEX_YRES, bochs->yres);
  282. bochs_dispi_write(bochs, VBE_DISPI_INDEX_BANK, 0);
  283. bochs_dispi_write(bochs, VBE_DISPI_INDEX_VIRT_WIDTH, bochs->xres);
  284. bochs_dispi_write(bochs, VBE_DISPI_INDEX_VIRT_HEIGHT,
  285. bochs->yres_virtual);
  286. bochs_dispi_write(bochs, VBE_DISPI_INDEX_X_OFFSET, 0);
  287. bochs_dispi_write(bochs, VBE_DISPI_INDEX_Y_OFFSET, 0);
  288. bochs_dispi_write(bochs, VBE_DISPI_INDEX_ENABLE,
  289. VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED);
  290. drm_dev_exit(idx);
  291. }
  292. static void bochs_hw_setformat(struct bochs_device *bochs, const struct drm_format_info *format)
  293. {
  294. int idx;
  295. if (!drm_dev_enter(bochs->dev, &idx))
  296. return;
  297. DRM_DEBUG_DRIVER("format %c%c%c%c\n",
  298. (format->format >> 0) & 0xff,
  299. (format->format >> 8) & 0xff,
  300. (format->format >> 16) & 0xff,
  301. (format->format >> 24) & 0xff);
  302. switch (format->format) {
  303. case DRM_FORMAT_XRGB8888:
  304. bochs_hw_set_little_endian(bochs);
  305. break;
  306. case DRM_FORMAT_BGRX8888:
  307. bochs_hw_set_big_endian(bochs);
  308. break;
  309. default:
  310. /* should not happen */
  311. DRM_ERROR("%s: Huh? Got framebuffer format 0x%x",
  312. __func__, format->format);
  313. break;
  314. }
  315. drm_dev_exit(idx);
  316. }
  317. static void bochs_hw_setbase(struct bochs_device *bochs, int x, int y, int stride, u64 addr)
  318. {
  319. unsigned long offset;
  320. unsigned int vx, vy, vwidth, idx;
  321. if (!drm_dev_enter(bochs->dev, &idx))
  322. return;
  323. bochs->stride = stride;
  324. offset = (unsigned long)addr +
  325. y * bochs->stride +
  326. x * (bochs->bpp / 8);
  327. vy = offset / bochs->stride;
  328. vx = (offset % bochs->stride) * 8 / bochs->bpp;
  329. vwidth = stride * 8 / bochs->bpp;
  330. DRM_DEBUG_DRIVER("x %d, y %d, addr %llx -> offset %lx, vx %d, vy %d\n",
  331. x, y, addr, offset, vx, vy);
  332. bochs_dispi_write(bochs, VBE_DISPI_INDEX_VIRT_WIDTH, vwidth);
  333. bochs_dispi_write(bochs, VBE_DISPI_INDEX_X_OFFSET, vx);
  334. bochs_dispi_write(bochs, VBE_DISPI_INDEX_Y_OFFSET, vy);
  335. drm_dev_exit(idx);
  336. }
  337. /* ---------------------------------------------------------------------- */
  338. static const uint32_t bochs_formats[] = {
  339. DRM_FORMAT_XRGB8888,
  340. DRM_FORMAT_BGRX8888,
  341. };
  342. static void bochs_plane_update(struct bochs_device *bochs, struct drm_plane_state *state)
  343. {
  344. struct drm_gem_vram_object *gbo;
  345. s64 gpu_addr;
  346. if (!state->fb || !bochs->stride)
  347. return;
  348. gbo = drm_gem_vram_of_gem(state->fb->obj[0]);
  349. gpu_addr = drm_gem_vram_offset(gbo);
  350. if (WARN_ON_ONCE(gpu_addr < 0))
  351. return; /* Bug: we didn't pin the BO to VRAM in prepare_fb. */
  352. bochs_hw_setbase(bochs,
  353. state->crtc_x,
  354. state->crtc_y,
  355. state->fb->pitches[0],
  356. state->fb->offsets[0] + gpu_addr);
  357. bochs_hw_setformat(bochs, state->fb->format);
  358. }
  359. static void bochs_pipe_enable(struct drm_simple_display_pipe *pipe,
  360. struct drm_crtc_state *crtc_state,
  361. struct drm_plane_state *plane_state)
  362. {
  363. struct bochs_device *bochs = pipe->crtc.dev->dev_private;
  364. bochs_hw_setmode(bochs, &crtc_state->mode);
  365. bochs_plane_update(bochs, plane_state);
  366. }
  367. static void bochs_pipe_disable(struct drm_simple_display_pipe *pipe)
  368. {
  369. struct bochs_device *bochs = pipe->crtc.dev->dev_private;
  370. bochs_hw_blank(bochs, true);
  371. }
  372. static void bochs_pipe_update(struct drm_simple_display_pipe *pipe,
  373. struct drm_plane_state *old_state)
  374. {
  375. struct bochs_device *bochs = pipe->crtc.dev->dev_private;
  376. bochs_plane_update(bochs, pipe->plane.state);
  377. }
  378. static const struct drm_simple_display_pipe_funcs bochs_pipe_funcs = {
  379. .enable = bochs_pipe_enable,
  380. .disable = bochs_pipe_disable,
  381. .update = bochs_pipe_update,
  382. .prepare_fb = drm_gem_vram_simple_display_pipe_prepare_fb,
  383. .cleanup_fb = drm_gem_vram_simple_display_pipe_cleanup_fb,
  384. };
  385. static int bochs_connector_get_modes(struct drm_connector *connector)
  386. {
  387. int count;
  388. count = drm_edid_connector_add_modes(connector);
  389. if (!count) {
  390. count = drm_add_modes_noedid(connector, 8192, 8192);
  391. drm_set_preferred_mode(connector, defx, defy);
  392. }
  393. return count;
  394. }
  395. static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
  396. .get_modes = bochs_connector_get_modes,
  397. };
  398. static const struct drm_connector_funcs bochs_connector_connector_funcs = {
  399. .fill_modes = drm_helper_probe_single_connector_modes,
  400. .destroy = drm_connector_cleanup,
  401. .reset = drm_atomic_helper_connector_reset,
  402. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  403. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  404. };
  405. static void bochs_connector_init(struct drm_device *dev)
  406. {
  407. struct bochs_device *bochs = dev->dev_private;
  408. struct drm_connector *connector = &bochs->connector;
  409. drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
  410. DRM_MODE_CONNECTOR_VIRTUAL);
  411. drm_connector_helper_add(connector, &bochs_connector_connector_helper_funcs);
  412. bochs_hw_load_edid(bochs);
  413. if (bochs->drm_edid) {
  414. DRM_INFO("Found EDID data blob.\n");
  415. drm_connector_attach_edid_property(connector);
  416. drm_edid_connector_update(&bochs->connector, bochs->drm_edid);
  417. }
  418. }
  419. static struct drm_framebuffer *
  420. bochs_gem_fb_create(struct drm_device *dev, struct drm_file *file,
  421. const struct drm_mode_fb_cmd2 *mode_cmd)
  422. {
  423. if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888 &&
  424. mode_cmd->pixel_format != DRM_FORMAT_BGRX8888)
  425. return ERR_PTR(-EINVAL);
  426. return drm_gem_fb_create(dev, file, mode_cmd);
  427. }
  428. static const struct drm_mode_config_funcs bochs_mode_funcs = {
  429. .fb_create = bochs_gem_fb_create,
  430. .mode_valid = drm_vram_helper_mode_valid,
  431. .atomic_check = drm_atomic_helper_check,
  432. .atomic_commit = drm_atomic_helper_commit,
  433. };
  434. static int bochs_kms_init(struct bochs_device *bochs)
  435. {
  436. int ret;
  437. ret = drmm_mode_config_init(bochs->dev);
  438. if (ret)
  439. return ret;
  440. bochs->dev->mode_config.max_width = 8192;
  441. bochs->dev->mode_config.max_height = 8192;
  442. bochs->dev->mode_config.preferred_depth = 24;
  443. bochs->dev->mode_config.prefer_shadow = 0;
  444. bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
  445. bochs->dev->mode_config.funcs = &bochs_mode_funcs;
  446. bochs_connector_init(bochs->dev);
  447. drm_simple_display_pipe_init(bochs->dev,
  448. &bochs->pipe,
  449. &bochs_pipe_funcs,
  450. bochs_formats,
  451. ARRAY_SIZE(bochs_formats),
  452. NULL,
  453. &bochs->connector);
  454. drm_mode_config_reset(bochs->dev);
  455. return 0;
  456. }
  457. /* ---------------------------------------------------------------------- */
  458. /* drm interface */
  459. static int bochs_load(struct drm_device *dev)
  460. {
  461. struct bochs_device *bochs;
  462. int ret;
  463. bochs = drmm_kzalloc(dev, sizeof(*bochs), GFP_KERNEL);
  464. if (bochs == NULL)
  465. return -ENOMEM;
  466. dev->dev_private = bochs;
  467. bochs->dev = dev;
  468. ret = bochs_hw_init(dev);
  469. if (ret)
  470. return ret;
  471. ret = drmm_vram_helper_init(dev, bochs->fb_base, bochs->fb_size);
  472. if (ret)
  473. goto err_hw_fini;
  474. ret = bochs_kms_init(bochs);
  475. if (ret)
  476. goto err_hw_fini;
  477. return 0;
  478. err_hw_fini:
  479. bochs_hw_fini(dev);
  480. return ret;
  481. }
  482. DEFINE_DRM_GEM_FOPS(bochs_fops);
  483. static const struct drm_driver bochs_driver = {
  484. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  485. .fops = &bochs_fops,
  486. .name = "bochs-drm",
  487. .desc = "bochs dispi vga interface (qemu stdvga)",
  488. .date = "20130925",
  489. .major = 1,
  490. .minor = 0,
  491. DRM_GEM_VRAM_DRIVER,
  492. };
  493. /* ---------------------------------------------------------------------- */
  494. /* pm interface */
  495. #ifdef CONFIG_PM_SLEEP
  496. static int bochs_pm_suspend(struct device *dev)
  497. {
  498. struct drm_device *drm_dev = dev_get_drvdata(dev);
  499. return drm_mode_config_helper_suspend(drm_dev);
  500. }
  501. static int bochs_pm_resume(struct device *dev)
  502. {
  503. struct drm_device *drm_dev = dev_get_drvdata(dev);
  504. return drm_mode_config_helper_resume(drm_dev);
  505. }
  506. #endif
  507. static const struct dev_pm_ops bochs_pm_ops = {
  508. SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
  509. bochs_pm_resume)
  510. };
  511. /* ---------------------------------------------------------------------- */
  512. /* pci interface */
  513. static int bochs_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  514. {
  515. struct drm_device *dev;
  516. unsigned long fbsize;
  517. int ret;
  518. fbsize = pci_resource_len(pdev, 0);
  519. if (fbsize < 4 * 1024 * 1024) {
  520. DRM_ERROR("less than 4 MB video memory, ignoring device\n");
  521. return -ENOMEM;
  522. }
  523. ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &bochs_driver);
  524. if (ret)
  525. return ret;
  526. dev = drm_dev_alloc(&bochs_driver, &pdev->dev);
  527. if (IS_ERR(dev))
  528. return PTR_ERR(dev);
  529. ret = pcim_enable_device(pdev);
  530. if (ret)
  531. goto err_free_dev;
  532. pci_set_drvdata(pdev, dev);
  533. ret = bochs_load(dev);
  534. if (ret)
  535. goto err_free_dev;
  536. ret = drm_dev_register(dev, 0);
  537. if (ret)
  538. goto err_hw_fini;
  539. drm_fbdev_ttm_setup(dev, 32);
  540. return ret;
  541. err_hw_fini:
  542. bochs_hw_fini(dev);
  543. err_free_dev:
  544. drm_dev_put(dev);
  545. return ret;
  546. }
  547. static void bochs_pci_remove(struct pci_dev *pdev)
  548. {
  549. struct drm_device *dev = pci_get_drvdata(pdev);
  550. drm_dev_unplug(dev);
  551. drm_atomic_helper_shutdown(dev);
  552. bochs_hw_fini(dev);
  553. drm_dev_put(dev);
  554. }
  555. static void bochs_pci_shutdown(struct pci_dev *pdev)
  556. {
  557. drm_atomic_helper_shutdown(pci_get_drvdata(pdev));
  558. }
  559. static const struct pci_device_id bochs_pci_tbl[] = {
  560. {
  561. .vendor = 0x1234,
  562. .device = 0x1111,
  563. .subvendor = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
  564. .subdevice = PCI_SUBDEVICE_ID_QEMU,
  565. .driver_data = BOCHS_QEMU_STDVGA,
  566. },
  567. {
  568. .vendor = 0x1234,
  569. .device = 0x1111,
  570. .subvendor = PCI_ANY_ID,
  571. .subdevice = PCI_ANY_ID,
  572. .driver_data = BOCHS_UNKNOWN,
  573. },
  574. {
  575. .vendor = 0x4321,
  576. .device = 0x1111,
  577. .subvendor = PCI_ANY_ID,
  578. .subdevice = PCI_ANY_ID,
  579. .driver_data = BOCHS_SIMICS,
  580. },
  581. { /* end of list */ }
  582. };
  583. static struct pci_driver bochs_pci_driver = {
  584. .name = "bochs-drm",
  585. .id_table = bochs_pci_tbl,
  586. .probe = bochs_pci_probe,
  587. .remove = bochs_pci_remove,
  588. .shutdown = bochs_pci_shutdown,
  589. .driver.pm = &bochs_pm_ops,
  590. };
  591. /* ---------------------------------------------------------------------- */
  592. /* module init/exit */
  593. drm_module_pci_driver_if_modeset(bochs_pci_driver, bochs_modeset);
  594. MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
  595. MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
  596. MODULE_DESCRIPTION("DRM Support for bochs dispi vga interface (qemu stdvga)");
  597. MODULE_LICENSE("GPL");