cirrus_main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright 2012 Red Hat
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License version 2. See the file COPYING in the main
  6. * directory of this archive for more details.
  7. *
  8. * Authors: Matthew Garrett
  9. * Dave Airlie
  10. */
  11. #include <drm/drmP.h>
  12. #include <drm/drm_crtc_helper.h>
  13. #include "cirrus_drv.h"
  14. static void cirrus_user_framebuffer_destroy(struct drm_framebuffer *fb)
  15. {
  16. struct cirrus_framebuffer *cirrus_fb = to_cirrus_framebuffer(fb);
  17. if (cirrus_fb->obj)
  18. drm_gem_object_unreference_unlocked(cirrus_fb->obj);
  19. drm_framebuffer_cleanup(fb);
  20. kfree(fb);
  21. }
  22. static const struct drm_framebuffer_funcs cirrus_fb_funcs = {
  23. .destroy = cirrus_user_framebuffer_destroy,
  24. };
  25. int cirrus_framebuffer_init(struct drm_device *dev,
  26. struct cirrus_framebuffer *gfb,
  27. struct drm_mode_fb_cmd2 *mode_cmd,
  28. struct drm_gem_object *obj)
  29. {
  30. int ret;
  31. drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd);
  32. gfb->obj = obj;
  33. ret = drm_framebuffer_init(dev, &gfb->base, &cirrus_fb_funcs);
  34. if (ret) {
  35. DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
  36. return ret;
  37. }
  38. return 0;
  39. }
  40. static struct drm_framebuffer *
  41. cirrus_user_framebuffer_create(struct drm_device *dev,
  42. struct drm_file *filp,
  43. struct drm_mode_fb_cmd2 *mode_cmd)
  44. {
  45. struct cirrus_device *cdev = dev->dev_private;
  46. struct drm_gem_object *obj;
  47. struct cirrus_framebuffer *cirrus_fb;
  48. int ret;
  49. u32 bpp, depth;
  50. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  51. if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
  52. bpp, mode_cmd->pitches[0]))
  53. return ERR_PTR(-EINVAL);
  54. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
  55. if (obj == NULL)
  56. return ERR_PTR(-ENOENT);
  57. cirrus_fb = kzalloc(sizeof(*cirrus_fb), GFP_KERNEL);
  58. if (!cirrus_fb) {
  59. drm_gem_object_unreference_unlocked(obj);
  60. return ERR_PTR(-ENOMEM);
  61. }
  62. ret = cirrus_framebuffer_init(dev, cirrus_fb, mode_cmd, obj);
  63. if (ret) {
  64. drm_gem_object_unreference_unlocked(obj);
  65. kfree(cirrus_fb);
  66. return ERR_PTR(ret);
  67. }
  68. return &cirrus_fb->base;
  69. }
  70. static const struct drm_mode_config_funcs cirrus_mode_funcs = {
  71. .fb_create = cirrus_user_framebuffer_create,
  72. };
  73. /* Unmap the framebuffer from the core and release the memory */
  74. static void cirrus_vram_fini(struct cirrus_device *cdev)
  75. {
  76. iounmap(cdev->rmmio);
  77. cdev->rmmio = NULL;
  78. if (cdev->mc.vram_base)
  79. release_mem_region(cdev->mc.vram_base, cdev->mc.vram_size);
  80. }
  81. /* Map the framebuffer from the card and configure the core */
  82. static int cirrus_vram_init(struct cirrus_device *cdev)
  83. {
  84. /* BAR 0 is VRAM */
  85. cdev->mc.vram_base = pci_resource_start(cdev->dev->pdev, 0);
  86. cdev->mc.vram_size = pci_resource_len(cdev->dev->pdev, 0);
  87. if (!request_mem_region(cdev->mc.vram_base, cdev->mc.vram_size,
  88. "cirrusdrmfb_vram")) {
  89. DRM_ERROR("can't reserve VRAM\n");
  90. return -ENXIO;
  91. }
  92. return 0;
  93. }
  94. /*
  95. * Our emulated hardware has two sets of memory. One is video RAM and can
  96. * simply be used as a linear framebuffer - the other provides mmio access
  97. * to the display registers. The latter can also be accessed via IO port
  98. * access, but we map the range and use mmio to program them instead
  99. */
  100. int cirrus_device_init(struct cirrus_device *cdev,
  101. struct drm_device *ddev,
  102. struct pci_dev *pdev, uint32_t flags)
  103. {
  104. int ret;
  105. cdev->dev = ddev;
  106. cdev->flags = flags;
  107. /* Hardcode the number of CRTCs to 1 */
  108. cdev->num_crtc = 1;
  109. /* BAR 0 is the framebuffer, BAR 1 contains registers */
  110. cdev->rmmio_base = pci_resource_start(cdev->dev->pdev, 1);
  111. cdev->rmmio_size = pci_resource_len(cdev->dev->pdev, 1);
  112. if (!request_mem_region(cdev->rmmio_base, cdev->rmmio_size,
  113. "cirrusdrmfb_mmio")) {
  114. DRM_ERROR("can't reserve mmio registers\n");
  115. return -ENOMEM;
  116. }
  117. cdev->rmmio = ioremap(cdev->rmmio_base, cdev->rmmio_size);
  118. if (cdev->rmmio == NULL)
  119. return -ENOMEM;
  120. ret = cirrus_vram_init(cdev);
  121. if (ret) {
  122. release_mem_region(cdev->rmmio_base, cdev->rmmio_size);
  123. return ret;
  124. }
  125. return 0;
  126. }
  127. void cirrus_device_fini(struct cirrus_device *cdev)
  128. {
  129. release_mem_region(cdev->rmmio_base, cdev->rmmio_size);
  130. cirrus_vram_fini(cdev);
  131. }
  132. /*
  133. * Functions here will be called by the core once it's bound the driver to
  134. * a PCI device
  135. */
  136. int cirrus_driver_load(struct drm_device *dev, unsigned long flags)
  137. {
  138. struct cirrus_device *cdev;
  139. int r;
  140. cdev = kzalloc(sizeof(struct cirrus_device), GFP_KERNEL);
  141. if (cdev == NULL)
  142. return -ENOMEM;
  143. dev->dev_private = (void *)cdev;
  144. r = cirrus_device_init(cdev, dev, dev->pdev, flags);
  145. if (r) {
  146. dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
  147. goto out;
  148. }
  149. r = cirrus_mm_init(cdev);
  150. if (r) {
  151. dev_err(&dev->pdev->dev, "fatal err on mm init\n");
  152. goto out;
  153. }
  154. r = cirrus_modeset_init(cdev);
  155. if (r) {
  156. dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
  157. goto out;
  158. }
  159. dev->mode_config.funcs = (void *)&cirrus_mode_funcs;
  160. return 0;
  161. out:
  162. cirrus_driver_unload(dev);
  163. return r;
  164. }
  165. int cirrus_driver_unload(struct drm_device *dev)
  166. {
  167. struct cirrus_device *cdev = dev->dev_private;
  168. if (cdev == NULL)
  169. return 0;
  170. cirrus_modeset_fini(cdev);
  171. cirrus_mm_fini(cdev);
  172. cirrus_device_fini(cdev);
  173. kfree(cdev);
  174. dev->dev_private = NULL;
  175. return 0;
  176. }
  177. int cirrus_gem_create(struct drm_device *dev,
  178. u32 size, bool iskernel,
  179. struct drm_gem_object **obj)
  180. {
  181. struct cirrus_bo *cirrusbo;
  182. int ret;
  183. *obj = NULL;
  184. size = roundup(size, PAGE_SIZE);
  185. if (size == 0)
  186. return -EINVAL;
  187. ret = cirrus_bo_create(dev, size, 0, 0, &cirrusbo);
  188. if (ret) {
  189. if (ret != -ERESTARTSYS)
  190. DRM_ERROR("failed to allocate GEM object\n");
  191. return ret;
  192. }
  193. *obj = &cirrusbo->gem;
  194. return 0;
  195. }
  196. int cirrus_dumb_create(struct drm_file *file,
  197. struct drm_device *dev,
  198. struct drm_mode_create_dumb *args)
  199. {
  200. int ret;
  201. struct drm_gem_object *gobj;
  202. u32 handle;
  203. args->pitch = args->width * ((args->bpp + 7) / 8);
  204. args->size = args->pitch * args->height;
  205. ret = cirrus_gem_create(dev, args->size, false,
  206. &gobj);
  207. if (ret)
  208. return ret;
  209. ret = drm_gem_handle_create(file, gobj, &handle);
  210. drm_gem_object_unreference_unlocked(gobj);
  211. if (ret)
  212. return ret;
  213. args->handle = handle;
  214. return 0;
  215. }
  216. static void cirrus_bo_unref(struct cirrus_bo **bo)
  217. {
  218. struct ttm_buffer_object *tbo;
  219. if ((*bo) == NULL)
  220. return;
  221. tbo = &((*bo)->bo);
  222. ttm_bo_unref(&tbo);
  223. *bo = NULL;
  224. }
  225. void cirrus_gem_free_object(struct drm_gem_object *obj)
  226. {
  227. struct cirrus_bo *cirrus_bo = gem_to_cirrus_bo(obj);
  228. cirrus_bo_unref(&cirrus_bo);
  229. }
  230. static inline u64 cirrus_bo_mmap_offset(struct cirrus_bo *bo)
  231. {
  232. return drm_vma_node_offset_addr(&bo->bo.vma_node);
  233. }
  234. int
  235. cirrus_dumb_mmap_offset(struct drm_file *file,
  236. struct drm_device *dev,
  237. uint32_t handle,
  238. uint64_t *offset)
  239. {
  240. struct drm_gem_object *obj;
  241. struct cirrus_bo *bo;
  242. obj = drm_gem_object_lookup(dev, file, handle);
  243. if (obj == NULL)
  244. return -ENOENT;
  245. bo = gem_to_cirrus_bo(obj);
  246. *offset = cirrus_bo_mmap_offset(bo);
  247. drm_gem_object_unreference_unlocked(obj);
  248. return 0;
  249. }
  250. bool cirrus_check_framebuffer(struct cirrus_device *cdev, int width, int height,
  251. int bpp, int pitch)
  252. {
  253. const int max_pitch = 0x1FF << 3; /* (4096 - 1) & ~111b bytes */
  254. const int max_size = cdev->mc.vram_size;
  255. if (bpp > cirrus_bpp)
  256. return false;
  257. if (bpp > 32)
  258. return false;
  259. if (pitch > max_pitch)
  260. return false;
  261. if (pitch * height > max_size)
  262. return false;
  263. return true;
  264. }