qxl_kms.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <linux/io-mapping.h>
  28. int qxl_log_level;
  29. static void qxl_dump_mode(struct qxl_device *qdev, void *p)
  30. {
  31. struct qxl_mode *m = p;
  32. DRM_DEBUG_KMS("%d: %dx%d %d bits, stride %d, %dmm x %dmm, orientation %d\n",
  33. m->id, m->x_res, m->y_res, m->bits, m->stride, m->x_mili,
  34. m->y_mili, m->orientation);
  35. }
  36. static bool qxl_check_device(struct qxl_device *qdev)
  37. {
  38. struct qxl_rom *rom = qdev->rom;
  39. int mode_offset;
  40. int i;
  41. if (rom->magic != 0x4f525851) {
  42. DRM_ERROR("bad rom signature %x\n", rom->magic);
  43. return false;
  44. }
  45. DRM_INFO("Device Version %d.%d\n", rom->id, rom->update_id);
  46. DRM_INFO("Compression level %d log level %d\n", rom->compression_level,
  47. rom->log_level);
  48. DRM_INFO("Currently using mode #%d, list at 0x%x\n",
  49. rom->mode, rom->modes_offset);
  50. DRM_INFO("%d io pages at offset 0x%x\n",
  51. rom->num_io_pages, rom->pages_offset);
  52. DRM_INFO("%d byte draw area at offset 0x%x\n",
  53. rom->surface0_area_size, rom->draw_area_offset);
  54. qdev->vram_size = rom->surface0_area_size;
  55. DRM_INFO("RAM header offset: 0x%x\n", rom->ram_header_offset);
  56. mode_offset = rom->modes_offset / 4;
  57. qdev->mode_info.num_modes = ((u32 *)rom)[mode_offset];
  58. DRM_INFO("rom modes offset 0x%x for %d modes\n", rom->modes_offset,
  59. qdev->mode_info.num_modes);
  60. qdev->mode_info.modes = (void *)((uint32_t *)rom + mode_offset + 1);
  61. for (i = 0; i < qdev->mode_info.num_modes; i++)
  62. qxl_dump_mode(qdev, qdev->mode_info.modes + i);
  63. return true;
  64. }
  65. static uint8_t setup_slot(struct qxl_device *qdev, uint8_t slot_index_offset,
  66. unsigned long start_phys_addr, unsigned long end_phys_addr)
  67. {
  68. uint64_t high_bits;
  69. struct qxl_memslot *slot;
  70. uint8_t slot_index;
  71. struct qxl_ram_header *ram_header = qdev->ram_header;
  72. slot_index = qdev->rom->slots_start + slot_index_offset;
  73. slot = &qdev->mem_slots[slot_index];
  74. slot->start_phys_addr = start_phys_addr;
  75. slot->end_phys_addr = end_phys_addr;
  76. ram_header->mem_slot.mem_start = slot->start_phys_addr;
  77. ram_header->mem_slot.mem_end = slot->end_phys_addr;
  78. qxl_io_memslot_add(qdev, slot_index);
  79. slot->generation = qdev->rom->slot_generation;
  80. high_bits = slot_index << qdev->slot_gen_bits;
  81. high_bits |= slot->generation;
  82. high_bits <<= (64 - (qdev->slot_gen_bits + qdev->slot_id_bits));
  83. slot->high_bits = high_bits;
  84. return slot_index;
  85. }
  86. static void qxl_gc_work(struct work_struct *work)
  87. {
  88. struct qxl_device *qdev = container_of(work, struct qxl_device, gc_work);
  89. qxl_garbage_collect(qdev);
  90. }
  91. int qxl_device_init(struct qxl_device *qdev,
  92. struct drm_device *ddev,
  93. struct pci_dev *pdev,
  94. unsigned long flags)
  95. {
  96. int r;
  97. qdev->dev = &pdev->dev;
  98. qdev->ddev = ddev;
  99. qdev->pdev = pdev;
  100. qdev->flags = flags;
  101. mutex_init(&qdev->gem.mutex);
  102. mutex_init(&qdev->update_area_mutex);
  103. mutex_init(&qdev->release_mutex);
  104. mutex_init(&qdev->surf_evict_mutex);
  105. INIT_LIST_HEAD(&qdev->gem.objects);
  106. qdev->rom_base = pci_resource_start(pdev, 2);
  107. qdev->rom_size = pci_resource_len(pdev, 2);
  108. qdev->vram_base = pci_resource_start(pdev, 0);
  109. qdev->surfaceram_base = pci_resource_start(pdev, 1);
  110. qdev->surfaceram_size = pci_resource_len(pdev, 1);
  111. qdev->io_base = pci_resource_start(pdev, 3);
  112. qdev->vram_mapping = io_mapping_create_wc(qdev->vram_base, pci_resource_len(pdev, 0));
  113. qdev->surface_mapping = io_mapping_create_wc(qdev->surfaceram_base, qdev->surfaceram_size);
  114. DRM_DEBUG_KMS("qxl: vram %llx-%llx(%dM %dk), surface %llx-%llx(%dM %dk)\n",
  115. (unsigned long long)qdev->vram_base,
  116. (unsigned long long)pci_resource_end(pdev, 0),
  117. (int)pci_resource_len(pdev, 0) / 1024 / 1024,
  118. (int)pci_resource_len(pdev, 0) / 1024,
  119. (unsigned long long)qdev->surfaceram_base,
  120. (unsigned long long)pci_resource_end(pdev, 1),
  121. (int)qdev->surfaceram_size / 1024 / 1024,
  122. (int)qdev->surfaceram_size / 1024);
  123. qdev->rom = ioremap(qdev->rom_base, qdev->rom_size);
  124. if (!qdev->rom) {
  125. pr_err("Unable to ioremap ROM\n");
  126. return -ENOMEM;
  127. }
  128. qxl_check_device(qdev);
  129. r = qxl_bo_init(qdev);
  130. if (r) {
  131. DRM_ERROR("bo init failed %d\n", r);
  132. return r;
  133. }
  134. qdev->ram_header = ioremap(qdev->vram_base +
  135. qdev->rom->ram_header_offset,
  136. sizeof(*qdev->ram_header));
  137. qdev->command_ring = qxl_ring_create(&(qdev->ram_header->cmd_ring_hdr),
  138. sizeof(struct qxl_command),
  139. QXL_COMMAND_RING_SIZE,
  140. qdev->io_base + QXL_IO_NOTIFY_CMD,
  141. false,
  142. &qdev->display_event);
  143. qdev->cursor_ring = qxl_ring_create(
  144. &(qdev->ram_header->cursor_ring_hdr),
  145. sizeof(struct qxl_command),
  146. QXL_CURSOR_RING_SIZE,
  147. qdev->io_base + QXL_IO_NOTIFY_CMD,
  148. false,
  149. &qdev->cursor_event);
  150. qdev->release_ring = qxl_ring_create(
  151. &(qdev->ram_header->release_ring_hdr),
  152. sizeof(uint64_t),
  153. QXL_RELEASE_RING_SIZE, 0, true,
  154. NULL);
  155. /* TODO - slot initialization should happen on reset. where is our
  156. * reset handler? */
  157. qdev->n_mem_slots = qdev->rom->slots_end;
  158. qdev->slot_gen_bits = qdev->rom->slot_gen_bits;
  159. qdev->slot_id_bits = qdev->rom->slot_id_bits;
  160. qdev->va_slot_mask =
  161. (~(uint64_t)0) >> (qdev->slot_id_bits + qdev->slot_gen_bits);
  162. qdev->mem_slots =
  163. kmalloc(qdev->n_mem_slots * sizeof(struct qxl_memslot),
  164. GFP_KERNEL);
  165. idr_init(&qdev->release_idr);
  166. spin_lock_init(&qdev->release_idr_lock);
  167. idr_init(&qdev->surf_id_idr);
  168. spin_lock_init(&qdev->surf_id_idr_lock);
  169. mutex_init(&qdev->async_io_mutex);
  170. /* reset the device into a known state - no memslots, no primary
  171. * created, no surfaces. */
  172. qxl_io_reset(qdev);
  173. /* must initialize irq before first async io - slot creation */
  174. r = qxl_irq_init(qdev);
  175. if (r)
  176. return r;
  177. /*
  178. * Note that virtual is surface0. We rely on the single ioremap done
  179. * before.
  180. */
  181. qdev->main_mem_slot = setup_slot(qdev, 0,
  182. (unsigned long)qdev->vram_base,
  183. (unsigned long)qdev->vram_base + qdev->rom->ram_header_offset);
  184. qdev->surfaces_mem_slot = setup_slot(qdev, 1,
  185. (unsigned long)qdev->surfaceram_base,
  186. (unsigned long)qdev->surfaceram_base + qdev->surfaceram_size);
  187. DRM_INFO("main mem slot %d [%lx,%x)\n",
  188. qdev->main_mem_slot,
  189. (unsigned long)qdev->vram_base, qdev->rom->ram_header_offset);
  190. qdev->gc_queue = create_singlethread_workqueue("qxl_gc");
  191. INIT_WORK(&qdev->gc_work, qxl_gc_work);
  192. r = qxl_fb_init(qdev);
  193. if (r)
  194. return r;
  195. return 0;
  196. }
  197. static void qxl_device_fini(struct qxl_device *qdev)
  198. {
  199. if (qdev->current_release_bo[0])
  200. qxl_bo_unref(&qdev->current_release_bo[0]);
  201. if (qdev->current_release_bo[1])
  202. qxl_bo_unref(&qdev->current_release_bo[1]);
  203. flush_workqueue(qdev->gc_queue);
  204. destroy_workqueue(qdev->gc_queue);
  205. qdev->gc_queue = NULL;
  206. qxl_ring_free(qdev->command_ring);
  207. qxl_ring_free(qdev->cursor_ring);
  208. qxl_ring_free(qdev->release_ring);
  209. qxl_bo_fini(qdev);
  210. io_mapping_free(qdev->surface_mapping);
  211. io_mapping_free(qdev->vram_mapping);
  212. iounmap(qdev->ram_header);
  213. iounmap(qdev->rom);
  214. qdev->rom = NULL;
  215. qdev->mode_info.modes = NULL;
  216. qdev->mode_info.num_modes = 0;
  217. qxl_debugfs_remove_files(qdev);
  218. }
  219. int qxl_driver_unload(struct drm_device *dev)
  220. {
  221. struct qxl_device *qdev = dev->dev_private;
  222. if (qdev == NULL)
  223. return 0;
  224. qxl_modeset_fini(qdev);
  225. qxl_device_fini(qdev);
  226. kfree(qdev);
  227. dev->dev_private = NULL;
  228. return 0;
  229. }
  230. int qxl_driver_load(struct drm_device *dev, unsigned long flags)
  231. {
  232. struct qxl_device *qdev;
  233. int r;
  234. /* require kms */
  235. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  236. return -ENODEV;
  237. qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL);
  238. if (qdev == NULL)
  239. return -ENOMEM;
  240. dev->dev_private = qdev;
  241. r = qxl_device_init(qdev, dev, dev->pdev, flags);
  242. if (r)
  243. goto out;
  244. r = qxl_modeset_init(qdev);
  245. if (r) {
  246. qxl_driver_unload(dev);
  247. goto out;
  248. }
  249. return 0;
  250. out:
  251. kfree(qdev);
  252. return r;
  253. }