fbdev.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <drm/drm_crtc_helper.h>
  9. #include <drm/drm_drv.h>
  10. #include <drm/drm_fb_helper.h>
  11. #include <drm/drm_framebuffer.h>
  12. #include "gem.h"
  13. #include "psb_drv.h"
  14. /*
  15. * VM area struct
  16. */
  17. static vm_fault_t psb_fbdev_vm_fault(struct vm_fault *vmf)
  18. {
  19. struct vm_area_struct *vma = vmf->vma;
  20. struct fb_info *info = vma->vm_private_data;
  21. unsigned long address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
  22. unsigned long pfn = info->fix.smem_start >> PAGE_SHIFT;
  23. vm_fault_t err = VM_FAULT_SIGBUS;
  24. unsigned long page_num = vma_pages(vma);
  25. unsigned long i;
  26. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  27. for (i = 0; i < page_num; ++i) {
  28. err = vmf_insert_mixed(vma, address, pfn);
  29. if (unlikely(err & VM_FAULT_ERROR))
  30. break;
  31. address += PAGE_SIZE;
  32. ++pfn;
  33. }
  34. return err;
  35. }
  36. static const struct vm_operations_struct psb_fbdev_vm_ops = {
  37. .fault = psb_fbdev_vm_fault,
  38. };
  39. /*
  40. * struct fb_ops
  41. */
  42. static int psb_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  43. {
  44. if (vma->vm_pgoff != 0)
  45. return -EINVAL;
  46. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  47. return -EINVAL;
  48. /*
  49. * If this is a GEM object then info->screen_base is the virtual
  50. * kernel remapping of the object. FIXME: Review if this is
  51. * suitable for our mmap work
  52. */
  53. vma->vm_ops = &psb_fbdev_vm_ops;
  54. vma->vm_private_data = info;
  55. vm_flags_set(vma, VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP);
  56. return 0;
  57. }
  58. static void psb_fbdev_fb_destroy(struct fb_info *info)
  59. {
  60. struct drm_fb_helper *fb_helper = info->par;
  61. struct drm_framebuffer *fb = fb_helper->fb;
  62. struct drm_gem_object *obj = fb->obj[0];
  63. drm_fb_helper_fini(fb_helper);
  64. drm_framebuffer_unregister_private(fb);
  65. drm_framebuffer_cleanup(fb);
  66. kfree(fb);
  67. drm_gem_object_put(obj);
  68. drm_client_release(&fb_helper->client);
  69. }
  70. static const struct fb_ops psb_fbdev_fb_ops = {
  71. .owner = THIS_MODULE,
  72. __FB_DEFAULT_IOMEM_OPS_RDWR,
  73. DRM_FB_HELPER_DEFAULT_OPS,
  74. __FB_DEFAULT_IOMEM_OPS_DRAW,
  75. .fb_mmap = psb_fbdev_fb_mmap,
  76. .fb_destroy = psb_fbdev_fb_destroy,
  77. };
  78. static const struct drm_fb_helper_funcs psb_fbdev_fb_helper_funcs = {
  79. };
  80. /*
  81. * struct drm_driver
  82. */
  83. int psb_fbdev_driver_fbdev_probe(struct drm_fb_helper *fb_helper,
  84. struct drm_fb_helper_surface_size *sizes)
  85. {
  86. struct drm_device *dev = fb_helper->dev;
  87. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  88. struct pci_dev *pdev = to_pci_dev(dev->dev);
  89. struct fb_info *info = fb_helper->info;
  90. struct drm_framebuffer *fb;
  91. struct drm_mode_fb_cmd2 mode_cmd = { };
  92. int size;
  93. int ret;
  94. struct psb_gem_object *backing;
  95. struct drm_gem_object *obj;
  96. u32 bpp, depth;
  97. /* No 24-bit packed mode */
  98. if (sizes->surface_bpp == 24) {
  99. sizes->surface_bpp = 32;
  100. sizes->surface_depth = 24;
  101. }
  102. bpp = sizes->surface_bpp;
  103. depth = sizes->surface_depth;
  104. /*
  105. * If the mode does not fit in 32 bit then switch to 16 bit to get
  106. * a console on full resolution. The X mode setting server will
  107. * allocate its own 32-bit GEM framebuffer.
  108. */
  109. size = ALIGN(sizes->surface_width * DIV_ROUND_UP(bpp, 8), 64) *
  110. sizes->surface_height;
  111. size = ALIGN(size, PAGE_SIZE);
  112. if (size > dev_priv->vram_stolen_size) {
  113. sizes->surface_bpp = 16;
  114. sizes->surface_depth = 16;
  115. }
  116. bpp = sizes->surface_bpp;
  117. depth = sizes->surface_depth;
  118. mode_cmd.width = sizes->surface_width;
  119. mode_cmd.height = sizes->surface_height;
  120. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * DIV_ROUND_UP(bpp, 8), 64);
  121. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  122. size = mode_cmd.pitches[0] * mode_cmd.height;
  123. size = ALIGN(size, PAGE_SIZE);
  124. /* Allocate the framebuffer in the GTT with stolen page backing */
  125. backing = psb_gem_create(dev, size, "fb", true, PAGE_SIZE);
  126. if (IS_ERR(backing))
  127. return PTR_ERR(backing);
  128. obj = &backing->base;
  129. fb = psb_framebuffer_create(dev,
  130. drm_get_format_info(dev, mode_cmd.pixel_format,
  131. mode_cmd.modifier[0]),
  132. &mode_cmd, obj);
  133. if (IS_ERR(fb)) {
  134. ret = PTR_ERR(fb);
  135. goto err_drm_gem_object_put;
  136. }
  137. fb_helper->funcs = &psb_fbdev_fb_helper_funcs;
  138. fb_helper->fb = fb;
  139. info->fbops = &psb_fbdev_fb_ops;
  140. /* Accessed stolen memory directly */
  141. info->screen_base = dev_priv->vram_addr + backing->offset;
  142. info->screen_size = size;
  143. drm_fb_helper_fill_info(info, fb_helper, sizes);
  144. info->fix.smem_start = dev_priv->stolen_base + backing->offset;
  145. info->fix.smem_len = size;
  146. info->fix.ywrapstep = 0;
  147. info->fix.ypanstep = 0;
  148. info->fix.mmio_start = pci_resource_start(pdev, 0);
  149. info->fix.mmio_len = pci_resource_len(pdev, 0);
  150. fb_memset_io(info->screen_base, 0, info->screen_size);
  151. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  152. dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
  153. return 0;
  154. err_drm_gem_object_put:
  155. drm_gem_object_put(obj);
  156. return ret;
  157. }