qxl_kms.c 9.8 KB

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