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