qxl_kms.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie
  23. * Alon Levy
  24. */
  25. #include "qxl_drv.h"
  26. #include "qxl_object.h"
  27. #include <drm/drm_crtc_helper.h>
  28. #include <linux/io-mapping.h>
  29. int qxl_log_level;
  30. static bool qxl_check_device(struct qxl_device *qdev)
  31. {
  32. struct qxl_rom *rom = qdev->rom;
  33. if (rom->magic != 0x4f525851) {
  34. DRM_ERROR("bad rom signature %x\n", rom->magic);
  35. return false;
  36. }
  37. DRM_INFO("Device Version %d.%d\n", rom->id, rom->update_id);
  38. DRM_INFO("Compression level %d log level %d\n", rom->compression_level,
  39. rom->log_level);
  40. DRM_INFO("%d io pages at offset 0x%x\n",
  41. rom->num_io_pages, rom->pages_offset);
  42. DRM_INFO("%d byte draw area at offset 0x%x\n",
  43. rom->surface0_area_size, rom->draw_area_offset);
  44. qdev->vram_size = rom->surface0_area_size;
  45. DRM_INFO("RAM header offset: 0x%x\n", rom->ram_header_offset);
  46. return true;
  47. }
  48. static void setup_hw_slot(struct qxl_device *qdev, int slot_index,
  49. struct qxl_memslot *slot)
  50. {
  51. qdev->ram_header->mem_slot.mem_start = slot->start_phys_addr;
  52. qdev->ram_header->mem_slot.mem_end = slot->end_phys_addr;
  53. qxl_io_memslot_add(qdev, slot_index);
  54. }
  55. static uint8_t setup_slot(struct qxl_device *qdev, uint8_t slot_index_offset,
  56. unsigned long start_phys_addr, unsigned long end_phys_addr)
  57. {
  58. uint64_t high_bits;
  59. struct qxl_memslot *slot;
  60. uint8_t slot_index;
  61. slot_index = qdev->rom->slots_start + slot_index_offset;
  62. slot = &qdev->mem_slots[slot_index];
  63. slot->start_phys_addr = start_phys_addr;
  64. slot->end_phys_addr = end_phys_addr;
  65. setup_hw_slot(qdev, slot_index, slot);
  66. slot->generation = qdev->rom->slot_generation;
  67. high_bits = slot_index << qdev->slot_gen_bits;
  68. high_bits |= slot->generation;
  69. high_bits <<= (64 - (qdev->slot_gen_bits + qdev->slot_id_bits));
  70. slot->high_bits = high_bits;
  71. return slot_index;
  72. }
  73. void qxl_reinit_memslots(struct qxl_device *qdev)
  74. {
  75. setup_hw_slot(qdev, qdev->main_mem_slot, &qdev->mem_slots[qdev->main_mem_slot]);
  76. setup_hw_slot(qdev, qdev->surfaces_mem_slot, &qdev->mem_slots[qdev->surfaces_mem_slot]);
  77. }
  78. static void qxl_gc_work(struct work_struct *work)
  79. {
  80. struct qxl_device *qdev = container_of(work, struct qxl_device, gc_work);
  81. qxl_garbage_collect(qdev);
  82. }
  83. int qxl_device_init(struct qxl_device *qdev,
  84. struct drm_driver *drv,
  85. struct pci_dev *pdev)
  86. {
  87. int r, sb;
  88. r = drm_dev_init(&qdev->ddev, drv, &pdev->dev);
  89. if (r) {
  90. pr_err("Unable to init drm dev");
  91. goto error;
  92. }
  93. qdev->ddev.pdev = pdev;
  94. pci_set_drvdata(pdev, &qdev->ddev);
  95. qdev->ddev.dev_private = qdev;
  96. mutex_init(&qdev->gem.mutex);
  97. mutex_init(&qdev->update_area_mutex);
  98. mutex_init(&qdev->release_mutex);
  99. mutex_init(&qdev->surf_evict_mutex);
  100. qxl_gem_init(qdev);
  101. qdev->rom_base = pci_resource_start(pdev, 2);
  102. qdev->rom_size = pci_resource_len(pdev, 2);
  103. qdev->vram_base = pci_resource_start(pdev, 0);
  104. qdev->io_base = pci_resource_start(pdev, 3);
  105. qdev->vram_mapping = io_mapping_create_wc(qdev->vram_base, pci_resource_len(pdev, 0));
  106. if (!qdev->vram_mapping) {
  107. pr_err("Unable to create vram_mapping");
  108. r = -ENOMEM;
  109. goto error;
  110. }
  111. if (pci_resource_len(pdev, 4) > 0) {
  112. /* 64bit surface bar present */
  113. sb = 4;
  114. qdev->surfaceram_base = pci_resource_start(pdev, sb);
  115. qdev->surfaceram_size = pci_resource_len(pdev, sb);
  116. qdev->surface_mapping =
  117. io_mapping_create_wc(qdev->surfaceram_base,
  118. qdev->surfaceram_size);
  119. }
  120. if (qdev->surface_mapping == NULL) {
  121. /* 64bit surface bar not present (or mapping failed) */
  122. sb = 1;
  123. qdev->surfaceram_base = pci_resource_start(pdev, sb);
  124. qdev->surfaceram_size = pci_resource_len(pdev, sb);
  125. qdev->surface_mapping =
  126. io_mapping_create_wc(qdev->surfaceram_base,
  127. qdev->surfaceram_size);
  128. if (!qdev->surface_mapping) {
  129. pr_err("Unable to create surface_mapping");
  130. r = -ENOMEM;
  131. goto vram_mapping_free;
  132. }
  133. }
  134. DRM_DEBUG_KMS("qxl: vram %llx-%llx(%dM %dk), surface %llx-%llx(%dM %dk, %s)\n",
  135. (unsigned long long)qdev->vram_base,
  136. (unsigned long long)pci_resource_end(pdev, 0),
  137. (int)pci_resource_len(pdev, 0) / 1024 / 1024,
  138. (int)pci_resource_len(pdev, 0) / 1024,
  139. (unsigned long long)qdev->surfaceram_base,
  140. (unsigned long long)pci_resource_end(pdev, sb),
  141. (int)qdev->surfaceram_size / 1024 / 1024,
  142. (int)qdev->surfaceram_size / 1024,
  143. (sb == 4) ? "64bit" : "32bit");
  144. qdev->rom = ioremap(qdev->rom_base, qdev->rom_size);
  145. if (!qdev->rom) {
  146. pr_err("Unable to ioremap ROM\n");
  147. r = -ENOMEM;
  148. goto surface_mapping_free;
  149. }
  150. if (!qxl_check_device(qdev)) {
  151. r = -ENODEV;
  152. goto surface_mapping_free;
  153. }
  154. r = qxl_bo_init(qdev);
  155. if (r) {
  156. DRM_ERROR("bo init failed %d\n", r);
  157. goto rom_unmap;
  158. }
  159. qdev->ram_header = ioremap(qdev->vram_base +
  160. qdev->rom->ram_header_offset,
  161. sizeof(*qdev->ram_header));
  162. if (!qdev->ram_header) {
  163. DRM_ERROR("Unable to ioremap RAM header\n");
  164. r = -ENOMEM;
  165. goto bo_fini;
  166. }
  167. qdev->command_ring = qxl_ring_create(&(qdev->ram_header->cmd_ring_hdr),
  168. sizeof(struct qxl_command),
  169. QXL_COMMAND_RING_SIZE,
  170. qdev->io_base + QXL_IO_NOTIFY_CMD,
  171. false,
  172. &qdev->display_event);
  173. if (!qdev->command_ring) {
  174. DRM_ERROR("Unable to create command ring\n");
  175. r = -ENOMEM;
  176. goto ram_header_unmap;
  177. }
  178. qdev->cursor_ring = qxl_ring_create(
  179. &(qdev->ram_header->cursor_ring_hdr),
  180. sizeof(struct qxl_command),
  181. QXL_CURSOR_RING_SIZE,
  182. qdev->io_base + QXL_IO_NOTIFY_CMD,
  183. false,
  184. &qdev->cursor_event);
  185. if (!qdev->cursor_ring) {
  186. DRM_ERROR("Unable to create cursor ring\n");
  187. r = -ENOMEM;
  188. goto command_ring_free;
  189. }
  190. qdev->release_ring = qxl_ring_create(
  191. &(qdev->ram_header->release_ring_hdr),
  192. sizeof(uint64_t),
  193. QXL_RELEASE_RING_SIZE, 0, true,
  194. NULL);
  195. if (!qdev->release_ring) {
  196. DRM_ERROR("Unable to create release ring\n");
  197. r = -ENOMEM;
  198. goto cursor_ring_free;
  199. }
  200. /* TODO - slot initialization should happen on reset. where is our
  201. * reset handler? */
  202. qdev->n_mem_slots = qdev->rom->slots_end;
  203. qdev->slot_gen_bits = qdev->rom->slot_gen_bits;
  204. qdev->slot_id_bits = qdev->rom->slot_id_bits;
  205. qdev->va_slot_mask =
  206. (~(uint64_t)0) >> (qdev->slot_id_bits + qdev->slot_gen_bits);
  207. qdev->mem_slots =
  208. kmalloc_array(qdev->n_mem_slots, sizeof(struct qxl_memslot),
  209. GFP_KERNEL);
  210. if (!qdev->mem_slots) {
  211. DRM_ERROR("Unable to alloc mem slots\n");
  212. r = -ENOMEM;
  213. goto release_ring_free;
  214. }
  215. idr_init(&qdev->release_idr);
  216. spin_lock_init(&qdev->release_idr_lock);
  217. spin_lock_init(&qdev->release_lock);
  218. idr_init(&qdev->surf_id_idr);
  219. spin_lock_init(&qdev->surf_id_idr_lock);
  220. mutex_init(&qdev->async_io_mutex);
  221. /* reset the device into a known state - no memslots, no primary
  222. * created, no surfaces. */
  223. qxl_io_reset(qdev);
  224. /* must initialize irq before first async io - slot creation */
  225. r = qxl_irq_init(qdev);
  226. if (r) {
  227. DRM_ERROR("Unable to init qxl irq\n");
  228. goto mem_slots_free;
  229. }
  230. /*
  231. * Note that virtual is surface0. We rely on the single ioremap done
  232. * before.
  233. */
  234. qdev->main_mem_slot = setup_slot(qdev, 0,
  235. (unsigned long)qdev->vram_base,
  236. (unsigned long)qdev->vram_base + qdev->rom->ram_header_offset);
  237. qdev->surfaces_mem_slot = setup_slot(qdev, 1,
  238. (unsigned long)qdev->surfaceram_base,
  239. (unsigned long)qdev->surfaceram_base + qdev->surfaceram_size);
  240. DRM_INFO("main mem slot %d [%lx,%x]\n",
  241. qdev->main_mem_slot,
  242. (unsigned long)qdev->vram_base, qdev->rom->ram_header_offset);
  243. DRM_INFO("surface mem slot %d [%lx,%lx]\n",
  244. qdev->surfaces_mem_slot,
  245. (unsigned long)qdev->surfaceram_base,
  246. (unsigned long)qdev->surfaceram_size);
  247. INIT_WORK(&qdev->gc_work, qxl_gc_work);
  248. return 0;
  249. mem_slots_free:
  250. kfree(qdev->mem_slots);
  251. release_ring_free:
  252. qxl_ring_free(qdev->release_ring);
  253. cursor_ring_free:
  254. qxl_ring_free(qdev->cursor_ring);
  255. command_ring_free:
  256. qxl_ring_free(qdev->command_ring);
  257. ram_header_unmap:
  258. iounmap(qdev->ram_header);
  259. bo_fini:
  260. qxl_bo_fini(qdev);
  261. rom_unmap:
  262. iounmap(qdev->rom);
  263. surface_mapping_free:
  264. io_mapping_free(qdev->surface_mapping);
  265. vram_mapping_free:
  266. io_mapping_free(qdev->vram_mapping);
  267. error:
  268. return r;
  269. }
  270. void qxl_device_fini(struct qxl_device *qdev)
  271. {
  272. if (qdev->current_release_bo[0])
  273. qxl_bo_unref(&qdev->current_release_bo[0]);
  274. if (qdev->current_release_bo[1])
  275. qxl_bo_unref(&qdev->current_release_bo[1]);
  276. flush_work(&qdev->gc_work);
  277. qxl_ring_free(qdev->command_ring);
  278. qxl_ring_free(qdev->cursor_ring);
  279. qxl_ring_free(qdev->release_ring);
  280. qxl_gem_fini(qdev);
  281. qxl_bo_fini(qdev);
  282. io_mapping_free(qdev->surface_mapping);
  283. io_mapping_free(qdev->vram_mapping);
  284. iounmap(qdev->ram_header);
  285. iounmap(qdev->rom);
  286. qdev->rom = NULL;
  287. }