qxl_kms.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <linux/io-mapping.h>
  26. #include <linux/pci.h>
  27. #include <drm/drm_drv.h>
  28. #include <drm/drm_managed.h>
  29. #include <drm/drm_probe_helper.h>
  30. #include "qxl_drv.h"
  31. #include "qxl_object.h"
  32. static bool qxl_check_device(struct qxl_device *qdev)
  33. {
  34. struct qxl_rom *rom = qdev->rom;
  35. if (rom->magic != 0x4f525851) {
  36. DRM_ERROR("bad rom signature %x\n", rom->magic);
  37. return false;
  38. }
  39. DRM_INFO("Device Version %d.%d\n", rom->id, rom->update_id);
  40. DRM_INFO("Compression level %d log level %d\n", rom->compression_level,
  41. rom->log_level);
  42. DRM_INFO("%d io pages at offset 0x%x\n",
  43. rom->num_io_pages, rom->pages_offset);
  44. DRM_INFO("%d byte draw area at offset 0x%x\n",
  45. rom->surface0_area_size, rom->draw_area_offset);
  46. qdev->vram_size = rom->surface0_area_size;
  47. DRM_INFO("RAM header offset: 0x%x\n", rom->ram_header_offset);
  48. return true;
  49. }
  50. static void setup_hw_slot(struct qxl_device *qdev, struct qxl_memslot *slot)
  51. {
  52. qdev->ram_header->mem_slot.mem_start = slot->start_phys_addr;
  53. qdev->ram_header->mem_slot.mem_end = slot->start_phys_addr + slot->size;
  54. qxl_io_memslot_add(qdev, qdev->rom->slots_start + slot->index);
  55. }
  56. static void setup_slot(struct qxl_device *qdev,
  57. struct qxl_memslot *slot,
  58. unsigned int slot_index,
  59. const char *slot_name,
  60. unsigned long start_phys_addr,
  61. unsigned long size)
  62. {
  63. uint64_t high_bits;
  64. slot->index = slot_index;
  65. slot->name = slot_name;
  66. slot->start_phys_addr = start_phys_addr;
  67. slot->size = size;
  68. setup_hw_slot(qdev, slot);
  69. slot->generation = qdev->rom->slot_generation;
  70. high_bits = (qdev->rom->slots_start + slot->index)
  71. << qdev->rom->slot_gen_bits;
  72. high_bits |= slot->generation;
  73. high_bits <<= (64 - (qdev->rom->slot_gen_bits + qdev->rom->slot_id_bits));
  74. slot->high_bits = high_bits;
  75. DRM_INFO("slot %d (%s): base 0x%08lx, size 0x%08lx\n",
  76. slot->index, slot->name,
  77. (unsigned long)slot->start_phys_addr,
  78. (unsigned long)slot->size);
  79. }
  80. void qxl_reinit_memslots(struct qxl_device *qdev)
  81. {
  82. setup_hw_slot(qdev, &qdev->main_slot);
  83. setup_hw_slot(qdev, &qdev->surfaces_slot);
  84. }
  85. static void qxl_gc_work(struct work_struct *work)
  86. {
  87. struct qxl_device *qdev = container_of(work, struct qxl_device, gc_work);
  88. qxl_garbage_collect(qdev);
  89. }
  90. int qxl_device_init(struct qxl_device *qdev,
  91. struct pci_dev *pdev)
  92. {
  93. int r, sb;
  94. pci_set_drvdata(pdev, &qdev->ddev);
  95. mutex_init(&qdev->gem.mutex);
  96. mutex_init(&qdev->update_area_mutex);
  97. mutex_init(&qdev->release_mutex);
  98. mutex_init(&qdev->surf_evict_mutex);
  99. qxl_gem_init(qdev);
  100. qdev->rom_base = pci_resource_start(pdev, 2);
  101. qdev->rom_size = pci_resource_len(pdev, 2);
  102. qdev->vram_base = pci_resource_start(pdev, 0);
  103. qdev->io_base = pci_resource_start(pdev, 3);
  104. qdev->vram_mapping = io_mapping_create_wc(qdev->vram_base, pci_resource_len(pdev, 0));
  105. if (!qdev->vram_mapping) {
  106. pr_err("Unable to create vram_mapping");
  107. return -ENOMEM;
  108. }
  109. if (pci_resource_len(pdev, 4) > 0) {
  110. /* 64bit surface bar present */
  111. sb = 4;
  112. qdev->surfaceram_base = pci_resource_start(pdev, sb);
  113. qdev->surfaceram_size = pci_resource_len(pdev, sb);
  114. qdev->surface_mapping =
  115. io_mapping_create_wc(qdev->surfaceram_base,
  116. qdev->surfaceram_size);
  117. }
  118. if (qdev->surface_mapping == NULL) {
  119. /* 64bit surface bar not present (or mapping failed) */
  120. sb = 1;
  121. qdev->surfaceram_base = pci_resource_start(pdev, sb);
  122. qdev->surfaceram_size = pci_resource_len(pdev, sb);
  123. qdev->surface_mapping =
  124. io_mapping_create_wc(qdev->surfaceram_base,
  125. qdev->surfaceram_size);
  126. if (!qdev->surface_mapping) {
  127. pr_err("Unable to create surface_mapping");
  128. r = -ENOMEM;
  129. goto vram_mapping_free;
  130. }
  131. }
  132. DRM_DEBUG_KMS("qxl: vram %llx-%llx(%dM %dk), surface %llx-%llx(%dM %dk, %s)\n",
  133. (unsigned long long)qdev->vram_base,
  134. (unsigned long long)pci_resource_end(pdev, 0),
  135. (int)pci_resource_len(pdev, 0) / 1024 / 1024,
  136. (int)pci_resource_len(pdev, 0) / 1024,
  137. (unsigned long long)qdev->surfaceram_base,
  138. (unsigned long long)pci_resource_end(pdev, sb),
  139. (int)qdev->surfaceram_size / 1024 / 1024,
  140. (int)qdev->surfaceram_size / 1024,
  141. (sb == 4) ? "64bit" : "32bit");
  142. qdev->rom = ioremap_wc(qdev->rom_base, qdev->rom_size);
  143. if (!qdev->rom) {
  144. pr_err("Unable to ioremap ROM\n");
  145. r = -ENOMEM;
  146. goto surface_mapping_free;
  147. }
  148. if (!qxl_check_device(qdev)) {
  149. r = -ENODEV;
  150. goto rom_unmap;
  151. }
  152. r = qxl_bo_init(qdev);
  153. if (r) {
  154. DRM_ERROR("bo init failed %d\n", r);
  155. goto rom_unmap;
  156. }
  157. qdev->ram_header = ioremap_wc(qdev->vram_base +
  158. qdev->rom->ram_header_offset,
  159. sizeof(*qdev->ram_header));
  160. if (!qdev->ram_header) {
  161. DRM_ERROR("Unable to ioremap RAM header\n");
  162. r = -ENOMEM;
  163. goto bo_fini;
  164. }
  165. qdev->command_ring = qxl_ring_create(&(qdev->ram_header->cmd_ring_hdr),
  166. sizeof(struct qxl_command),
  167. QXL_COMMAND_RING_SIZE,
  168. qdev->io_base + QXL_IO_NOTIFY_CMD,
  169. &qdev->display_event);
  170. if (!qdev->command_ring) {
  171. DRM_ERROR("Unable to create command ring\n");
  172. r = -ENOMEM;
  173. goto ram_header_unmap;
  174. }
  175. qdev->cursor_ring = qxl_ring_create(
  176. &(qdev->ram_header->cursor_ring_hdr),
  177. sizeof(struct qxl_command),
  178. QXL_CURSOR_RING_SIZE,
  179. qdev->io_base + QXL_IO_NOTIFY_CURSOR,
  180. &qdev->cursor_event);
  181. if (!qdev->cursor_ring) {
  182. DRM_ERROR("Unable to create cursor ring\n");
  183. r = -ENOMEM;
  184. goto command_ring_free;
  185. }
  186. qdev->release_ring = qxl_ring_create(
  187. &(qdev->ram_header->release_ring_hdr),
  188. sizeof(uint64_t),
  189. QXL_RELEASE_RING_SIZE, 0,
  190. NULL);
  191. if (!qdev->release_ring) {
  192. DRM_ERROR("Unable to create release ring\n");
  193. r = -ENOMEM;
  194. goto cursor_ring_free;
  195. }
  196. idr_init_base(&qdev->release_idr, 1);
  197. spin_lock_init(&qdev->release_idr_lock);
  198. spin_lock_init(&qdev->release_lock);
  199. idr_init_base(&qdev->surf_id_idr, 1);
  200. spin_lock_init(&qdev->surf_id_idr_lock);
  201. mutex_init(&qdev->async_io_mutex);
  202. /* reset the device into a known state - no memslots, no primary
  203. * created, no surfaces. */
  204. qxl_io_reset(qdev);
  205. /* must initialize irq before first async io - slot creation */
  206. r = qxl_irq_init(qdev);
  207. if (r) {
  208. DRM_ERROR("Unable to init qxl irq\n");
  209. goto release_ring_free;
  210. }
  211. /*
  212. * Note that virtual is surface0. We rely on the single ioremap done
  213. * before.
  214. */
  215. setup_slot(qdev, &qdev->main_slot, 0, "main",
  216. (unsigned long)qdev->vram_base,
  217. (unsigned long)qdev->rom->ram_header_offset);
  218. setup_slot(qdev, &qdev->surfaces_slot, 1, "surfaces",
  219. (unsigned long)qdev->surfaceram_base,
  220. (unsigned long)qdev->surfaceram_size);
  221. INIT_WORK(&qdev->gc_work, qxl_gc_work);
  222. return 0;
  223. release_ring_free:
  224. qxl_ring_free(qdev->release_ring);
  225. cursor_ring_free:
  226. qxl_ring_free(qdev->cursor_ring);
  227. command_ring_free:
  228. qxl_ring_free(qdev->command_ring);
  229. ram_header_unmap:
  230. iounmap(qdev->ram_header);
  231. bo_fini:
  232. qxl_bo_fini(qdev);
  233. rom_unmap:
  234. iounmap(qdev->rom);
  235. surface_mapping_free:
  236. io_mapping_free(qdev->surface_mapping);
  237. vram_mapping_free:
  238. io_mapping_free(qdev->vram_mapping);
  239. return r;
  240. }
  241. void qxl_device_fini(struct qxl_device *qdev)
  242. {
  243. int cur_idx;
  244. /* check if qxl_device_init() was successful (gc_work is initialized last) */
  245. if (!qdev->gc_work.func)
  246. return;
  247. for (cur_idx = 0; cur_idx < 3; cur_idx++) {
  248. if (!qdev->current_release_bo[cur_idx])
  249. continue;
  250. qxl_bo_unpin(qdev->current_release_bo[cur_idx]);
  251. qxl_bo_unref(&qdev->current_release_bo[cur_idx]);
  252. qdev->current_release_bo_offset[cur_idx] = 0;
  253. qdev->current_release_bo[cur_idx] = NULL;
  254. }
  255. /*
  256. * Ask host to release resources (+fill release ring),
  257. * then wait for the release actually happening.
  258. */
  259. qxl_io_notify_oom(qdev);
  260. wait_event_timeout(qdev->release_event,
  261. atomic_read(&qdev->release_count) == 0,
  262. HZ);
  263. flush_work(&qdev->gc_work);
  264. qxl_surf_evict(qdev);
  265. qxl_vram_evict(qdev);
  266. qxl_gem_fini(qdev);
  267. qxl_bo_fini(qdev);
  268. qxl_ring_free(qdev->command_ring);
  269. qxl_ring_free(qdev->cursor_ring);
  270. qxl_ring_free(qdev->release_ring);
  271. io_mapping_free(qdev->surface_mapping);
  272. io_mapping_free(qdev->vram_mapping);
  273. iounmap(qdev->ram_header);
  274. iounmap(qdev->rom);
  275. qdev->rom = NULL;
  276. }