bochs_hw.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. */
  7. #include "bochs.h"
  8. /* ---------------------------------------------------------------------- */
  9. static void bochs_vga_writeb(struct bochs_device *bochs, u16 ioport, u8 val)
  10. {
  11. if (WARN_ON(ioport < 0x3c0 || ioport > 0x3df))
  12. return;
  13. if (bochs->mmio) {
  14. int offset = ioport - 0x3c0 + 0x400;
  15. writeb(val, bochs->mmio + offset);
  16. } else {
  17. outb(val, ioport);
  18. }
  19. }
  20. static u16 bochs_dispi_read(struct bochs_device *bochs, u16 reg)
  21. {
  22. u16 ret = 0;
  23. if (bochs->mmio) {
  24. int offset = 0x500 + (reg << 1);
  25. ret = readw(bochs->mmio + offset);
  26. } else {
  27. outw(reg, VBE_DISPI_IOPORT_INDEX);
  28. ret = inw(VBE_DISPI_IOPORT_DATA);
  29. }
  30. return ret;
  31. }
  32. static void bochs_dispi_write(struct bochs_device *bochs, u16 reg, u16 val)
  33. {
  34. if (bochs->mmio) {
  35. int offset = 0x500 + (reg << 1);
  36. writew(val, bochs->mmio + offset);
  37. } else {
  38. outw(reg, VBE_DISPI_IOPORT_INDEX);
  39. outw(val, VBE_DISPI_IOPORT_DATA);
  40. }
  41. }
  42. int bochs_hw_init(struct drm_device *dev, uint32_t flags)
  43. {
  44. struct bochs_device *bochs = dev->dev_private;
  45. struct pci_dev *pdev = dev->pdev;
  46. unsigned long addr, size, mem, ioaddr, iosize, qext_size;
  47. u16 id;
  48. if (pdev->resource[2].flags & IORESOURCE_MEM) {
  49. /* mmio bar with vga and bochs registers present */
  50. if (pci_request_region(pdev, 2, "bochs-drm") != 0) {
  51. DRM_ERROR("Cannot request mmio region\n");
  52. return -EBUSY;
  53. }
  54. ioaddr = pci_resource_start(pdev, 2);
  55. iosize = pci_resource_len(pdev, 2);
  56. bochs->mmio = ioremap(ioaddr, iosize);
  57. if (bochs->mmio == NULL) {
  58. DRM_ERROR("Cannot map mmio region\n");
  59. return -ENOMEM;
  60. }
  61. } else {
  62. ioaddr = VBE_DISPI_IOPORT_INDEX;
  63. iosize = 2;
  64. if (!request_region(ioaddr, iosize, "bochs-drm")) {
  65. DRM_ERROR("Cannot request ioports\n");
  66. return -EBUSY;
  67. }
  68. bochs->ioports = 1;
  69. }
  70. id = bochs_dispi_read(bochs, VBE_DISPI_INDEX_ID);
  71. mem = bochs_dispi_read(bochs, VBE_DISPI_INDEX_VIDEO_MEMORY_64K)
  72. * 64 * 1024;
  73. if ((id & 0xfff0) != VBE_DISPI_ID0) {
  74. DRM_ERROR("ID mismatch\n");
  75. return -ENODEV;
  76. }
  77. if ((pdev->resource[0].flags & IORESOURCE_MEM) == 0)
  78. return -ENODEV;
  79. addr = pci_resource_start(pdev, 0);
  80. size = pci_resource_len(pdev, 0);
  81. if (addr == 0)
  82. return -ENODEV;
  83. if (size != mem) {
  84. DRM_ERROR("Size mismatch: pci=%ld, bochs=%ld\n",
  85. size, mem);
  86. size = min(size, mem);
  87. }
  88. if (pci_request_region(pdev, 0, "bochs-drm") != 0) {
  89. DRM_ERROR("Cannot request framebuffer\n");
  90. return -EBUSY;
  91. }
  92. bochs->fb_map = ioremap(addr, size);
  93. if (bochs->fb_map == NULL) {
  94. DRM_ERROR("Cannot map framebuffer\n");
  95. return -ENOMEM;
  96. }
  97. bochs->fb_base = addr;
  98. bochs->fb_size = size;
  99. DRM_INFO("Found bochs VGA, ID 0x%x.\n", id);
  100. DRM_INFO("Framebuffer size %ld kB @ 0x%lx, %s @ 0x%lx.\n",
  101. size / 1024, addr,
  102. bochs->ioports ? "ioports" : "mmio",
  103. ioaddr);
  104. if (bochs->mmio && pdev->revision >= 2) {
  105. qext_size = readl(bochs->mmio + 0x600);
  106. if (qext_size < 4 || qext_size > iosize)
  107. goto noext;
  108. DRM_DEBUG("Found qemu ext regs, size %ld\n", qext_size);
  109. if (qext_size >= 8) {
  110. #ifdef __BIG_ENDIAN
  111. writel(0xbebebebe, bochs->mmio + 0x604);
  112. #else
  113. writel(0x1e1e1e1e, bochs->mmio + 0x604);
  114. #endif
  115. DRM_DEBUG(" qext endian: 0x%x\n",
  116. readl(bochs->mmio + 0x604));
  117. }
  118. }
  119. noext:
  120. return 0;
  121. }
  122. void bochs_hw_fini(struct drm_device *dev)
  123. {
  124. struct bochs_device *bochs = dev->dev_private;
  125. if (bochs->mmio)
  126. iounmap(bochs->mmio);
  127. if (bochs->ioports)
  128. release_region(VBE_DISPI_IOPORT_INDEX, 2);
  129. if (bochs->fb_map)
  130. iounmap(bochs->fb_map);
  131. pci_release_regions(dev->pdev);
  132. }
  133. void bochs_hw_setmode(struct bochs_device *bochs,
  134. struct drm_display_mode *mode)
  135. {
  136. bochs->xres = mode->hdisplay;
  137. bochs->yres = mode->vdisplay;
  138. bochs->bpp = 32;
  139. bochs->stride = mode->hdisplay * (bochs->bpp / 8);
  140. bochs->yres_virtual = bochs->fb_size / bochs->stride;
  141. DRM_DEBUG_DRIVER("%dx%d @ %d bpp, vy %d\n",
  142. bochs->xres, bochs->yres, bochs->bpp,
  143. bochs->yres_virtual);
  144. bochs_vga_writeb(bochs, 0x3c0, 0x20); /* unblank */
  145. bochs_dispi_write(bochs, VBE_DISPI_INDEX_ENABLE, 0);
  146. bochs_dispi_write(bochs, VBE_DISPI_INDEX_BPP, bochs->bpp);
  147. bochs_dispi_write(bochs, VBE_DISPI_INDEX_XRES, bochs->xres);
  148. bochs_dispi_write(bochs, VBE_DISPI_INDEX_YRES, bochs->yres);
  149. bochs_dispi_write(bochs, VBE_DISPI_INDEX_BANK, 0);
  150. bochs_dispi_write(bochs, VBE_DISPI_INDEX_VIRT_WIDTH, bochs->xres);
  151. bochs_dispi_write(bochs, VBE_DISPI_INDEX_VIRT_HEIGHT,
  152. bochs->yres_virtual);
  153. bochs_dispi_write(bochs, VBE_DISPI_INDEX_X_OFFSET, 0);
  154. bochs_dispi_write(bochs, VBE_DISPI_INDEX_Y_OFFSET, 0);
  155. bochs_dispi_write(bochs, VBE_DISPI_INDEX_ENABLE,
  156. VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED);
  157. }
  158. void bochs_hw_setbase(struct bochs_device *bochs,
  159. int x, int y, u64 addr)
  160. {
  161. unsigned long offset = (unsigned long)addr +
  162. y * bochs->stride +
  163. x * (bochs->bpp / 8);
  164. int vy = offset / bochs->stride;
  165. int vx = (offset % bochs->stride) * 8 / bochs->bpp;
  166. DRM_DEBUG_DRIVER("x %d, y %d, addr %llx -> offset %lx, vx %d, vy %d\n",
  167. x, y, addr, offset, vx, vy);
  168. bochs_dispi_write(bochs, VBE_DISPI_INDEX_X_OFFSET, vx);
  169. bochs_dispi_write(bochs, VBE_DISPI_INDEX_Y_OFFSET, vy);
  170. }