cirrus_main.c 7.6 KB

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