cirrus_main.c 7.9 KB

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