fbdev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**************************************************************************
  3. * Copyright (c) 2007-2011, Intel Corporation.
  4. * All Rights Reserved.
  5. *
  6. **************************************************************************/
  7. #include <linux/fb.h>
  8. #include <linux/pfn_t.h>
  9. #include <drm/drm_crtc_helper.h>
  10. #include <drm/drm_drv.h>
  11. #include <drm/drm_fb_helper.h>
  12. #include <drm/drm_framebuffer.h>
  13. #include "gem.h"
  14. #include "psb_drv.h"
  15. /*
  16. * VM area struct
  17. */
  18. static vm_fault_t psb_fbdev_vm_fault(struct vm_fault *vmf)
  19. {
  20. struct vm_area_struct *vma = vmf->vma;
  21. struct fb_info *info = vma->vm_private_data;
  22. unsigned long address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
  23. unsigned long pfn = info->fix.smem_start >> PAGE_SHIFT;
  24. vm_fault_t err = VM_FAULT_SIGBUS;
  25. unsigned long page_num = vma_pages(vma);
  26. unsigned long i;
  27. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  28. for (i = 0; i < page_num; ++i) {
  29. err = vmf_insert_mixed(vma, address, __pfn_to_pfn_t(pfn, PFN_DEV));
  30. if (unlikely(err & VM_FAULT_ERROR))
  31. break;
  32. address += PAGE_SIZE;
  33. ++pfn;
  34. }
  35. return err;
  36. }
  37. static const struct vm_operations_struct psb_fbdev_vm_ops = {
  38. .fault = psb_fbdev_vm_fault,
  39. };
  40. /*
  41. * struct fb_ops
  42. */
  43. #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
  44. static int psb_fbdev_fb_setcolreg(unsigned int regno,
  45. unsigned int red, unsigned int green,
  46. unsigned int blue, unsigned int transp,
  47. struct fb_info *info)
  48. {
  49. struct drm_fb_helper *fb_helper = info->par;
  50. struct drm_framebuffer *fb = fb_helper->fb;
  51. uint32_t v;
  52. if (!fb)
  53. return -ENOMEM;
  54. if (regno > 255)
  55. return 1;
  56. red = CMAP_TOHW(red, info->var.red.length);
  57. blue = CMAP_TOHW(blue, info->var.blue.length);
  58. green = CMAP_TOHW(green, info->var.green.length);
  59. transp = CMAP_TOHW(transp, info->var.transp.length);
  60. v = (red << info->var.red.offset) |
  61. (green << info->var.green.offset) |
  62. (blue << info->var.blue.offset) |
  63. (transp << info->var.transp.offset);
  64. if (regno < 16) {
  65. switch (fb->format->cpp[0] * 8) {
  66. case 16:
  67. ((uint32_t *) info->pseudo_palette)[regno] = v;
  68. break;
  69. case 24:
  70. case 32:
  71. ((uint32_t *) info->pseudo_palette)[regno] = v;
  72. break;
  73. }
  74. }
  75. return 0;
  76. }
  77. static int psb_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  78. {
  79. if (vma->vm_pgoff != 0)
  80. return -EINVAL;
  81. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  82. return -EINVAL;
  83. /*
  84. * If this is a GEM object then info->screen_base is the virtual
  85. * kernel remapping of the object. FIXME: Review if this is
  86. * suitable for our mmap work
  87. */
  88. vma->vm_ops = &psb_fbdev_vm_ops;
  89. vma->vm_private_data = info;
  90. vm_flags_set(vma, VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP);
  91. return 0;
  92. }
  93. static void psb_fbdev_fb_destroy(struct fb_info *info)
  94. {
  95. struct drm_fb_helper *fb_helper = info->par;
  96. struct drm_framebuffer *fb = fb_helper->fb;
  97. struct drm_gem_object *obj = fb->obj[0];
  98. drm_fb_helper_fini(fb_helper);
  99. drm_framebuffer_unregister_private(fb);
  100. fb->obj[0] = NULL;
  101. drm_framebuffer_cleanup(fb);
  102. kfree(fb);
  103. drm_gem_object_put(obj);
  104. drm_client_release(&fb_helper->client);
  105. drm_fb_helper_unprepare(fb_helper);
  106. kfree(fb_helper);
  107. }
  108. static const struct fb_ops psb_fbdev_fb_ops = {
  109. .owner = THIS_MODULE,
  110. __FB_DEFAULT_IOMEM_OPS_RDWR,
  111. DRM_FB_HELPER_DEFAULT_OPS,
  112. .fb_setcolreg = psb_fbdev_fb_setcolreg,
  113. __FB_DEFAULT_IOMEM_OPS_DRAW,
  114. .fb_mmap = psb_fbdev_fb_mmap,
  115. .fb_destroy = psb_fbdev_fb_destroy,
  116. };
  117. /*
  118. * struct drm_fb_helper_funcs
  119. */
  120. static int psb_fbdev_fb_probe(struct drm_fb_helper *fb_helper,
  121. struct drm_fb_helper_surface_size *sizes)
  122. {
  123. struct drm_device *dev = fb_helper->dev;
  124. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  125. struct pci_dev *pdev = to_pci_dev(dev->dev);
  126. struct fb_info *info;
  127. struct drm_framebuffer *fb;
  128. struct drm_mode_fb_cmd2 mode_cmd = { };
  129. int size;
  130. int ret;
  131. struct psb_gem_object *backing;
  132. struct drm_gem_object *obj;
  133. u32 bpp, depth;
  134. /* No 24-bit packed mode */
  135. if (sizes->surface_bpp == 24) {
  136. sizes->surface_bpp = 32;
  137. sizes->surface_depth = 24;
  138. }
  139. bpp = sizes->surface_bpp;
  140. depth = sizes->surface_depth;
  141. /*
  142. * If the mode does not fit in 32 bit then switch to 16 bit to get
  143. * a console on full resolution. The X mode setting server will
  144. * allocate its own 32-bit GEM framebuffer.
  145. */
  146. size = ALIGN(sizes->surface_width * DIV_ROUND_UP(bpp, 8), 64) *
  147. sizes->surface_height;
  148. size = ALIGN(size, PAGE_SIZE);
  149. if (size > dev_priv->vram_stolen_size) {
  150. sizes->surface_bpp = 16;
  151. sizes->surface_depth = 16;
  152. }
  153. bpp = sizes->surface_bpp;
  154. depth = sizes->surface_depth;
  155. mode_cmd.width = sizes->surface_width;
  156. mode_cmd.height = sizes->surface_height;
  157. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * DIV_ROUND_UP(bpp, 8), 64);
  158. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  159. size = mode_cmd.pitches[0] * mode_cmd.height;
  160. size = ALIGN(size, PAGE_SIZE);
  161. /* Allocate the framebuffer in the GTT with stolen page backing */
  162. backing = psb_gem_create(dev, size, "fb", true, PAGE_SIZE);
  163. if (IS_ERR(backing))
  164. return PTR_ERR(backing);
  165. obj = &backing->base;
  166. fb = psb_framebuffer_create(dev, &mode_cmd, obj);
  167. if (IS_ERR(fb)) {
  168. ret = PTR_ERR(fb);
  169. goto err_drm_gem_object_put;
  170. }
  171. fb_helper->fb = fb;
  172. info = drm_fb_helper_alloc_info(fb_helper);
  173. if (IS_ERR(info)) {
  174. ret = PTR_ERR(info);
  175. goto err_drm_framebuffer_unregister_private;
  176. }
  177. info->fbops = &psb_fbdev_fb_ops;
  178. /* Accessed stolen memory directly */
  179. info->screen_base = dev_priv->vram_addr + backing->offset;
  180. info->screen_size = size;
  181. drm_fb_helper_fill_info(info, fb_helper, sizes);
  182. info->fix.smem_start = dev_priv->stolen_base + backing->offset;
  183. info->fix.smem_len = size;
  184. info->fix.ywrapstep = 0;
  185. info->fix.ypanstep = 0;
  186. info->fix.mmio_start = pci_resource_start(pdev, 0);
  187. info->fix.mmio_len = pci_resource_len(pdev, 0);
  188. fb_memset_io(info->screen_base, 0, info->screen_size);
  189. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  190. dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
  191. return 0;
  192. err_drm_framebuffer_unregister_private:
  193. drm_framebuffer_unregister_private(fb);
  194. fb->obj[0] = NULL;
  195. drm_framebuffer_cleanup(fb);
  196. kfree(fb);
  197. err_drm_gem_object_put:
  198. drm_gem_object_put(obj);
  199. return ret;
  200. }
  201. static const struct drm_fb_helper_funcs psb_fbdev_fb_helper_funcs = {
  202. .fb_probe = psb_fbdev_fb_probe,
  203. };
  204. /*
  205. * struct drm_client_funcs and setup code
  206. */
  207. static void psb_fbdev_client_unregister(struct drm_client_dev *client)
  208. {
  209. struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
  210. if (fb_helper->info) {
  211. drm_fb_helper_unregister_info(fb_helper);
  212. } else {
  213. drm_fb_helper_unprepare(fb_helper);
  214. drm_client_release(&fb_helper->client);
  215. kfree(fb_helper);
  216. }
  217. }
  218. static int psb_fbdev_client_restore(struct drm_client_dev *client)
  219. {
  220. drm_fb_helper_lastclose(client->dev);
  221. return 0;
  222. }
  223. static int psb_fbdev_client_hotplug(struct drm_client_dev *client)
  224. {
  225. struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
  226. struct drm_device *dev = client->dev;
  227. int ret;
  228. if (dev->fb_helper)
  229. return drm_fb_helper_hotplug_event(dev->fb_helper);
  230. ret = drm_fb_helper_init(dev, fb_helper);
  231. if (ret)
  232. goto err_drm_err;
  233. if (!drm_drv_uses_atomic_modeset(dev))
  234. drm_helper_disable_unused_functions(dev);
  235. ret = drm_fb_helper_initial_config(fb_helper);
  236. if (ret)
  237. goto err_drm_fb_helper_fini;
  238. return 0;
  239. err_drm_fb_helper_fini:
  240. drm_fb_helper_fini(fb_helper);
  241. err_drm_err:
  242. drm_err(dev, "Failed to setup gma500 fbdev emulation (ret=%d)\n", ret);
  243. return ret;
  244. }
  245. static const struct drm_client_funcs psb_fbdev_client_funcs = {
  246. .owner = THIS_MODULE,
  247. .unregister = psb_fbdev_client_unregister,
  248. .restore = psb_fbdev_client_restore,
  249. .hotplug = psb_fbdev_client_hotplug,
  250. };
  251. void psb_fbdev_setup(struct drm_psb_private *dev_priv)
  252. {
  253. struct drm_device *dev = &dev_priv->dev;
  254. struct drm_fb_helper *fb_helper;
  255. int ret;
  256. fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
  257. if (!fb_helper)
  258. return;
  259. drm_fb_helper_prepare(dev, fb_helper, 32, &psb_fbdev_fb_helper_funcs);
  260. ret = drm_client_init(dev, &fb_helper->client, "fbdev-gma500", &psb_fbdev_client_funcs);
  261. if (ret) {
  262. drm_err(dev, "Failed to register client: %d\n", ret);
  263. goto err_drm_fb_helper_unprepare;
  264. }
  265. drm_client_register(&fb_helper->client);
  266. return;
  267. err_drm_fb_helper_unprepare:
  268. drm_fb_helper_unprepare(fb_helper);
  269. kfree(fb_helper);
  270. }