blob: d224fc12b75713a9bef3348e80c408a103f8ba29 [file] [log] [blame]
Alan Cox4d8d0962011-11-03 18:21:20 +00001/**************************************************************************
2 * Copyright (c) 2007-2011, Intel Corporation.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 **************************************************************************/
19
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/string.h>
Dan Williams01c8f1c2016-01-15 16:56:40 -080024#include <linux/pfn_t.h>
Alan Cox4d8d0962011-11-03 18:21:20 +000025#include <linux/mm.h>
26#include <linux/tty.h>
27#include <linux/slab.h>
28#include <linux/delay.h>
Alan Cox4d8d0962011-11-03 18:21:20 +000029#include <linux/init.h>
30#include <linux/console.h>
31
32#include <drm/drmP.h>
33#include <drm/drm.h>
34#include <drm/drm_crtc.h>
Dave Airliea9a644a2011-11-28 14:08:46 +000035#include <drm/drm_fb_helper.h>
Alan Cox4d8d0962011-11-03 18:21:20 +000036
37#include "psb_drv.h"
38#include "psb_intel_reg.h"
39#include "psb_intel_drv.h"
40#include "framebuffer.h"
Alan Coxa6ba5822011-11-29 22:27:22 +000041#include "gtt.h"
Alan Cox4d8d0962011-11-03 18:21:20 +000042
43static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
44static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
45 struct drm_file *file_priv,
46 unsigned int *handle);
47
48static const struct drm_framebuffer_funcs psb_fb_funcs = {
49 .destroy = psb_user_framebuffer_destroy,
50 .create_handle = psb_user_framebuffer_create_handle,
51};
52
53#define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
54
55static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
56 unsigned blue, unsigned transp,
57 struct fb_info *info)
58{
59 struct psb_fbdev *fbdev = info->par;
60 struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
61 uint32_t v;
62
63 if (!fb)
64 return -ENOMEM;
65
66 if (regno > 255)
67 return 1;
68
69 red = CMAP_TOHW(red, info->var.red.length);
70 blue = CMAP_TOHW(blue, info->var.blue.length);
71 green = CMAP_TOHW(green, info->var.green.length);
72 transp = CMAP_TOHW(transp, info->var.transp.length);
73
74 v = (red << info->var.red.offset) |
75 (green << info->var.green.offset) |
76 (blue << info->var.blue.offset) |
77 (transp << info->var.transp.offset);
78
79 if (regno < 16) {
80 switch (fb->bits_per_pixel) {
81 case 16:
82 ((uint32_t *) info->pseudo_palette)[regno] = v;
83 break;
84 case 24:
85 case 32:
86 ((uint32_t *) info->pseudo_palette)[regno] = v;
87 break;
88 }
89 }
90
91 return 0;
92}
93
Alan Coxa6ba5822011-11-29 22:27:22 +000094static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
95{
96 struct psb_fbdev *fbdev = info->par;
97 struct psb_framebuffer *psbfb = &fbdev->pfb;
98 struct drm_device *dev = psbfb->base.dev;
99
100 /*
101 * We have to poke our nose in here. The core fb code assumes
102 * panning is part of the hardware that can be invoked before
103 * the actual fb is mapped. In our case that isn't quite true.
104 */
105 if (psbfb->gtt->npage) {
106 /* GTT roll shifts in 4K pages, we need to shift the right
107 number of pages */
108 int pages = info->fix.line_length >> 12;
109 psb_gtt_roll(dev, psbfb->gtt, var->yoffset * pages);
110 }
111 return 0;
112}
Alan Cox4d8d0962011-11-03 18:21:20 +0000113
Alan Cox4d8d0962011-11-03 18:21:20 +0000114static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
115{
116 struct psb_framebuffer *psbfb = vma->vm_private_data;
117 struct drm_device *dev = psbfb->base.dev;
118 struct drm_psb_private *dev_priv = dev->dev_private;
119 int page_num;
120 int i;
121 unsigned long address;
122 int ret;
123 unsigned long pfn;
Patrik Jakobsson61bb3fe2013-05-14 14:37:10 +0200124 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
125 psbfb->gtt->offset;
Alan Cox4d8d0962011-11-03 18:21:20 +0000126
127 page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
Yoichi Yuasa1278f7d2012-03-15 14:50:16 +0000128 address = (unsigned long)vmf->virtual_address - (vmf->pgoff << PAGE_SHIFT);
Alan Cox4d8d0962011-11-03 18:21:20 +0000129
130 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
131
132 for (i = 0; i < page_num; i++) {
133 pfn = (phys_addr >> PAGE_SHIFT);
134
Dan Williams01c8f1c2016-01-15 16:56:40 -0800135 ret = vm_insert_mixed(vma, address,
136 __pfn_to_pfn_t(pfn, PFN_DEV));
Alan Cox4d8d0962011-11-03 18:21:20 +0000137 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
138 break;
139 else if (unlikely(ret != 0)) {
140 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
141 return ret;
142 }
143 address += PAGE_SIZE;
144 phys_addr += PAGE_SIZE;
145 }
146 return VM_FAULT_NOPAGE;
147}
148
149static void psbfb_vm_open(struct vm_area_struct *vma)
150{
151}
152
153static void psbfb_vm_close(struct vm_area_struct *vma)
154{
155}
156
Laurent Pinchart78b68552012-05-17 13:27:22 +0200157static const struct vm_operations_struct psbfb_vm_ops = {
Alan Cox4d8d0962011-11-03 18:21:20 +0000158 .fault = psbfb_vm_fault,
159 .open = psbfb_vm_open,
160 .close = psbfb_vm_close
161};
162
163static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
164{
165 struct psb_fbdev *fbdev = info->par;
166 struct psb_framebuffer *psbfb = &fbdev->pfb;
167
168 if (vma->vm_pgoff != 0)
169 return -EINVAL;
170 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
171 return -EINVAL;
172
173 if (!psbfb->addr_space)
174 psbfb->addr_space = vma->vm_file->f_mapping;
175 /*
176 * If this is a GEM object then info->screen_base is the virtual
177 * kernel remapping of the object. FIXME: Review if this is
178 * suitable for our mmap work
179 */
180 vma->vm_ops = &psbfb_vm_ops;
181 vma->vm_private_data = (void *)psbfb;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700182 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
Alan Cox4d8d0962011-11-03 18:21:20 +0000183 return 0;
184}
185
Alan Cox4d8d0962011-11-03 18:21:20 +0000186static struct fb_ops psbfb_ops = {
187 .owner = THIS_MODULE,
188 .fb_check_var = drm_fb_helper_check_var,
189 .fb_set_par = drm_fb_helper_set_par,
190 .fb_blank = drm_fb_helper_blank,
191 .fb_setcolreg = psbfb_setcolreg,
Archit Taneja546187c2015-07-22 14:58:10 +0530192 .fb_fillrect = drm_fb_helper_cfb_fillrect,
Alan Cox4d8d0962011-11-03 18:21:20 +0000193 .fb_copyarea = psbfb_copyarea,
Archit Taneja546187c2015-07-22 14:58:10 +0530194 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Alan Cox4d8d0962011-11-03 18:21:20 +0000195 .fb_mmap = psbfb_mmap,
196 .fb_sync = psbfb_sync,
Alan Cox4d8d0962011-11-03 18:21:20 +0000197};
198
Alan Coxa6ba5822011-11-29 22:27:22 +0000199static struct fb_ops psbfb_roll_ops = {
200 .owner = THIS_MODULE,
201 .fb_check_var = drm_fb_helper_check_var,
202 .fb_set_par = drm_fb_helper_set_par,
203 .fb_blank = drm_fb_helper_blank,
204 .fb_setcolreg = psbfb_setcolreg,
Archit Taneja546187c2015-07-22 14:58:10 +0530205 .fb_fillrect = drm_fb_helper_cfb_fillrect,
206 .fb_copyarea = drm_fb_helper_cfb_copyarea,
207 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Alan Coxa6ba5822011-11-29 22:27:22 +0000208 .fb_pan_display = psbfb_pan,
209 .fb_mmap = psbfb_mmap,
Alan Coxa6ba5822011-11-29 22:27:22 +0000210};
211
Alan Cox4d8d0962011-11-03 18:21:20 +0000212static struct fb_ops psbfb_unaccel_ops = {
213 .owner = THIS_MODULE,
214 .fb_check_var = drm_fb_helper_check_var,
215 .fb_set_par = drm_fb_helper_set_par,
216 .fb_blank = drm_fb_helper_blank,
217 .fb_setcolreg = psbfb_setcolreg,
Archit Taneja546187c2015-07-22 14:58:10 +0530218 .fb_fillrect = drm_fb_helper_cfb_fillrect,
219 .fb_copyarea = drm_fb_helper_cfb_copyarea,
220 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Alan Cox4d8d0962011-11-03 18:21:20 +0000221 .fb_mmap = psbfb_mmap,
Alan Cox4d8d0962011-11-03 18:21:20 +0000222};
223
224/**
225 * psb_framebuffer_init - initialize a framebuffer
226 * @dev: our DRM device
227 * @fb: framebuffer to set up
228 * @mode_cmd: mode description
229 * @gt: backing object
230 *
231 * Configure and fill in the boilerplate for our frame buffer. Return
232 * 0 on success or an error code if we fail.
233 */
234static int psb_framebuffer_init(struct drm_device *dev,
235 struct psb_framebuffer *fb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200236 const struct drm_mode_fb_cmd2 *mode_cmd,
Alan Cox4d8d0962011-11-03 18:21:20 +0000237 struct gtt_range *gt)
238{
Dave Airliea9a644a2011-11-28 14:08:46 +0000239 u32 bpp, depth;
Alan Cox4d8d0962011-11-03 18:21:20 +0000240 int ret;
241
Dave Airlie248dbc22011-11-29 20:02:54 +0000242 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
Dave Airliea9a644a2011-11-28 14:08:46 +0000243
244 if (mode_cmd->pitches[0] & 63)
Alan Cox4d8d0962011-11-03 18:21:20 +0000245 return -EINVAL;
Dave Airliea9a644a2011-11-28 14:08:46 +0000246 switch (bpp) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000247 case 8:
248 case 16:
249 case 24:
250 case 32:
251 break;
252 default:
253 return -EINVAL;
254 }
Daniel Vetterc7d73f62012-12-13 23:38:38 +0100255 drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
256 fb->gtt = gt;
Alan Cox4d8d0962011-11-03 18:21:20 +0000257 ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
258 if (ret) {
259 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
260 return ret;
261 }
Alan Cox4d8d0962011-11-03 18:21:20 +0000262 return 0;
263}
264
265/**
266 * psb_framebuffer_create - create a framebuffer backed by gt
267 * @dev: our DRM device
268 * @mode_cmd: the description of the requested mode
269 * @gt: the backing object
270 *
271 * Create a framebuffer object backed by the gt, and fill in the
272 * boilerplate required
273 *
274 * TODO: review object references
275 */
276
277static struct drm_framebuffer *psb_framebuffer_create
278 (struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200279 const struct drm_mode_fb_cmd2 *mode_cmd,
Alan Cox4d8d0962011-11-03 18:21:20 +0000280 struct gtt_range *gt)
281{
282 struct psb_framebuffer *fb;
283 int ret;
284
285 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
286 if (!fb)
287 return ERR_PTR(-ENOMEM);
288
289 ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
290 if (ret) {
291 kfree(fb);
292 return ERR_PTR(ret);
293 }
294 return &fb->base;
295}
296
297/**
298 * psbfb_alloc - allocate frame buffer memory
299 * @dev: the DRM device
300 * @aligned_size: space needed
Alan Coxa6ba5822011-11-29 22:27:22 +0000301 * @force: fall back to GEM buffers if need be
Alan Cox4d8d0962011-11-03 18:21:20 +0000302 *
303 * Allocate the frame buffer. In the usual case we get a GTT range that
304 * is stolen memory backed and life is simple. If there isn't sufficient
Alan Coxdffc9ce2011-11-29 22:19:47 +0000305 * we fail as we don't have the virtual mapping space to really vmap it
306 * and the kernel console code can't handle non linear framebuffers.
Alan Cox4d8d0962011-11-03 18:21:20 +0000307 *
Alan Coxdffc9ce2011-11-29 22:19:47 +0000308 * Re-address this as and if the framebuffer layer grows this ability.
Alan Cox4d8d0962011-11-03 18:21:20 +0000309 */
310static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
311{
312 struct gtt_range *backing;
313 /* Begin by trying to use stolen memory backing */
Patrik Jakobssonc269c682014-01-06 02:39:10 +0100314 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
Alan Cox4d8d0962011-11-03 18:21:20 +0000315 if (backing) {
David Herrmann89c82332013-07-11 11:56:32 +0200316 drm_gem_private_object_init(dev, &backing->gem, aligned_size);
317 return backing;
Alan Cox4d8d0962011-11-03 18:21:20 +0000318 }
Alan Coxdffc9ce2011-11-29 22:19:47 +0000319 return NULL;
Alan Cox4d8d0962011-11-03 18:21:20 +0000320}
321
322/**
323 * psbfb_create - create a framebuffer
324 * @fbdev: the framebuffer device
325 * @sizes: specification of the layout
326 *
327 * Create a framebuffer to the specifications provided
328 */
329static int psbfb_create(struct psb_fbdev *fbdev,
330 struct drm_fb_helper_surface_size *sizes)
331{
332 struct drm_device *dev = fbdev->psb_fb_helper.dev;
333 struct drm_psb_private *dev_priv = dev->dev_private;
334 struct fb_info *info;
335 struct drm_framebuffer *fb;
336 struct psb_framebuffer *psbfb = &fbdev->pfb;
Dave Airliea9a644a2011-11-28 14:08:46 +0000337 struct drm_mode_fb_cmd2 mode_cmd;
Alan Cox4d8d0962011-11-03 18:21:20 +0000338 int size;
339 int ret;
340 struct gtt_range *backing;
Dave Airliea9a644a2011-11-28 14:08:46 +0000341 u32 bpp, depth;
Alan Cox1b223c92011-11-29 22:27:34 +0000342 int gtt_roll = 0;
343 int pitch_lines = 0;
Alan Cox4d8d0962011-11-03 18:21:20 +0000344
345 mode_cmd.width = sizes->surface_width;
346 mode_cmd.height = sizes->surface_height;
Dave Airliea9a644a2011-11-28 14:08:46 +0000347 bpp = sizes->surface_bpp;
Kirill A. Shutemov6aa1ead2012-03-08 16:02:36 +0000348 depth = sizes->surface_depth;
Alan Cox4d8d0962011-11-03 18:21:20 +0000349
350 /* No 24bit packed */
Dave Airliea9a644a2011-11-28 14:08:46 +0000351 if (bpp == 24)
352 bpp = 32;
Alan Cox4d8d0962011-11-03 18:21:20 +0000353
Alan Cox1b223c92011-11-29 22:27:34 +0000354 do {
355 /*
356 * Acceleration via the GTT requires pitch to be
357 * power of two aligned. Preferably page but less
358 * is ok with some fonts
359 */
360 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
Alan Cox4d8d0962011-11-03 18:21:20 +0000361
Alan Cox1b223c92011-11-29 22:27:34 +0000362 size = mode_cmd.pitches[0] * mode_cmd.height;
363 size = ALIGN(size, PAGE_SIZE);
Alan Cox4d8d0962011-11-03 18:21:20 +0000364
Alan Cox1b223c92011-11-29 22:27:34 +0000365 /* Allocate the fb in the GTT with stolen page backing */
366 backing = psbfb_alloc(dev, size);
367
368 if (pitch_lines)
369 pitch_lines *= 2;
370 else
371 pitch_lines = 1;
372 gtt_roll++;
373 } while (backing == NULL && pitch_lines <= 16);
374
375 /* The final pitch we accepted if we succeeded */
376 pitch_lines /= 2;
377
Alan Coxa6ba5822011-11-29 22:27:22 +0000378 if (backing == NULL) {
379 /*
380 * We couldn't get the space we wanted, fall back to the
381 * display engine requirement instead. The HW requires
382 * the pitch to be 64 byte aligned
Alan Coxa6ba5822011-11-29 22:27:22 +0000383 */
384
385 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
Alan Cox1b223c92011-11-29 22:27:34 +0000386 pitch_lines = 64;
Alan Coxa6ba5822011-11-29 22:27:22 +0000387
388 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
389
390 size = mode_cmd.pitches[0] * mode_cmd.height;
391 size = ALIGN(size, PAGE_SIZE);
392
393 /* Allocate the framebuffer in the GTT with stolen page backing */
394 backing = psbfb_alloc(dev, size);
395 if (backing == NULL)
396 return -ENOMEM;
397 }
Alan Cox4d8d0962011-11-03 18:21:20 +0000398
Alan Coxbb849772012-05-03 15:05:53 +0100399 memset(dev_priv->vram_addr + backing->offset, 0, size);
400
Archit Taneja546187c2015-07-22 14:58:10 +0530401 info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
402 if (IS_ERR(info)) {
403 ret = PTR_ERR(info);
Sudip Mukherjee4cd54d92015-10-08 18:17:48 +0530404 goto err_free_range;
Alan Cox4d8d0962011-11-03 18:21:20 +0000405 }
406 info->par = fbdev;
407
Dave Airliea9a644a2011-11-28 14:08:46 +0000408 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
409
Alan Cox4d8d0962011-11-03 18:21:20 +0000410 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
411 if (ret)
Sudip Mukherjee4cd54d92015-10-08 18:17:48 +0530412 goto err_release;
Alan Cox4d8d0962011-11-03 18:21:20 +0000413
414 fb = &psbfb->base;
415 psbfb->fbdev = info;
416
417 fbdev->psb_fb_helper.fb = fb;
Alan Cox4d8d0962011-11-03 18:21:20 +0000418
Alan Cox45782402012-03-08 16:01:39 +0000419 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Patrik Jakobssonf9d81492013-04-03 23:52:04 +0200420 strcpy(info->fix.id, "psbdrmfb");
Alan Cox4d8d0962011-11-03 18:21:20 +0000421
422 info->flags = FBINFO_DEFAULT;
Alan Cox1b223c92011-11-29 22:27:34 +0000423 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
424 info->fbops = &psbfb_ops;
425 else if (gtt_roll) { /* GTT rolling seems best */
Alan Coxa6ba5822011-11-29 22:27:22 +0000426 info->fbops = &psbfb_roll_ops;
427 info->flags |= FBINFO_HWACCEL_YPAN;
Alan Cox1b223c92011-11-29 22:27:34 +0000428 } else /* Software */
Alan Coxa6ba5822011-11-29 22:27:22 +0000429 info->fbops = &psbfb_unaccel_ops;
Alan Cox4d8d0962011-11-03 18:21:20 +0000430
Alan Cox4d8d0962011-11-03 18:21:20 +0000431 info->fix.smem_start = dev->mode_config.fb_base;
432 info->fix.smem_len = size;
Alan Coxa6ba5822011-11-29 22:27:22 +0000433 info->fix.ywrapstep = gtt_roll;
434 info->fix.ypanstep = 0;
Alan Cox4d8d0962011-11-03 18:21:20 +0000435
Alan Coxdffc9ce2011-11-29 22:19:47 +0000436 /* Accessed stolen memory directly */
Kirill A. Shutemov37214ca2012-05-03 15:08:26 +0100437 info->screen_base = dev_priv->vram_addr + backing->offset;
Alan Cox4d8d0962011-11-03 18:21:20 +0000438 info->screen_size = size;
439
440 if (dev_priv->gtt.stolen_size) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000441 info->apertures->ranges[0].base = dev->mode_config.fb_base;
442 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
443 }
444
Alan Cox4d8d0962011-11-03 18:21:20 +0000445 drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
446 sizes->fb_width, sizes->fb_height);
447
448 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
449 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
450
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100451 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Alan Cox4d8d0962011-11-03 18:21:20 +0000452
Alan Cox31a06852012-05-11 11:32:31 +0100453 dev_dbg(dev->dev, "allocated %dx%d fb\n",
Alan Cox4d8d0962011-11-03 18:21:20 +0000454 psbfb->base.width, psbfb->base.height);
455
Alan Cox4d8d0962011-11-03 18:21:20 +0000456 return 0;
Sudip Mukherjee4cd54d92015-10-08 18:17:48 +0530457err_release:
Archit Taneja546187c2015-07-22 14:58:10 +0530458 drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
Sudip Mukherjee4cd54d92015-10-08 18:17:48 +0530459err_free_range:
Alan Cox4d8d0962011-11-03 18:21:20 +0000460 psb_gtt_free_range(dev, backing);
461 return ret;
462}
463
464/**
465 * psb_user_framebuffer_create - create framebuffer
466 * @dev: our DRM device
467 * @filp: client file
468 * @cmd: mode request
469 *
470 * Create a new framebuffer backed by a userspace GEM object
471 */
472static struct drm_framebuffer *psb_user_framebuffer_create
473 (struct drm_device *dev, struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200474 const struct drm_mode_fb_cmd2 *cmd)
Alan Cox4d8d0962011-11-03 18:21:20 +0000475{
476 struct gtt_range *r;
477 struct drm_gem_object *obj;
478
479 /*
480 * Find the GEM object and thus the gtt range object that is
481 * to back this space
482 */
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100483 obj = drm_gem_object_lookup(filp, cmd->handles[0]);
Alan Cox4d8d0962011-11-03 18:21:20 +0000484 if (obj == NULL)
485 return ERR_PTR(-ENOENT);
486
487 /* Let the core code do all the work */
488 r = container_of(obj, struct gtt_range, gem);
489 return psb_framebuffer_create(dev, cmd, r);
490}
491
492static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
493 u16 blue, int regno)
494{
Patrik Jakobsson63068652013-07-22 01:31:23 +0200495 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
Alan Cox3df546b2012-03-08 16:00:00 +0000496
Patrik Jakobsson63068652013-07-22 01:31:23 +0200497 gma_crtc->lut_r[regno] = red >> 8;
498 gma_crtc->lut_g[regno] = green >> 8;
499 gma_crtc->lut_b[regno] = blue >> 8;
Alan Cox4d8d0962011-11-03 18:21:20 +0000500}
501
502static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
503 u16 *green, u16 *blue, int regno)
504{
Patrik Jakobsson63068652013-07-22 01:31:23 +0200505 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
Alan Cox3df546b2012-03-08 16:00:00 +0000506
Patrik Jakobsson63068652013-07-22 01:31:23 +0200507 *red = gma_crtc->lut_r[regno] << 8;
508 *green = gma_crtc->lut_g[regno] << 8;
509 *blue = gma_crtc->lut_b[regno] << 8;
Alan Cox4d8d0962011-11-03 18:21:20 +0000510}
511
512static int psbfb_probe(struct drm_fb_helper *helper,
513 struct drm_fb_helper_surface_size *sizes)
514{
Fabian Frederickc39aa6a2014-09-14 18:40:20 +0200515 struct psb_fbdev *psb_fbdev =
516 container_of(helper, struct psb_fbdev, psb_fb_helper);
Alan Cox3aad16d2012-04-25 14:37:40 +0100517 struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
518 struct drm_psb_private *dev_priv = dev->dev_private;
Paul Kocialkowski3cabf122019-11-07 16:30:48 +0100519 unsigned int fb_size;
Alan Cox3aad16d2012-04-25 14:37:40 +0100520 int bytespp;
Alan Cox4d8d0962011-11-03 18:21:20 +0000521
Alan Cox3aad16d2012-04-25 14:37:40 +0100522 bytespp = sizes->surface_bpp / 8;
523 if (bytespp == 3) /* no 24bit packed */
524 bytespp = 4;
525
526 /* If the mode will not fit in 32bit then switch to 16bit to get
527 a console on full resolution. The X mode setting server will
528 allocate its own 32bit GEM framebuffer */
Paul Kocialkowski3cabf122019-11-07 16:30:48 +0100529 fb_size = ALIGN(sizes->surface_width * bytespp, 64) *
530 sizes->surface_height;
531 fb_size = ALIGN(fb_size, PAGE_SIZE);
532
533 if (fb_size > dev_priv->vram_stolen_size) {
Alan Cox3aad16d2012-04-25 14:37:40 +0100534 sizes->surface_bpp = 16;
535 sizes->surface_depth = 16;
536 }
537
Daniel Vettercd5428a2013-01-21 23:42:49 +0100538 return psbfb_create(psb_fbdev, sizes);
Alan Cox4d8d0962011-11-03 18:21:20 +0000539}
540
Thierry Reding3a493872014-06-27 17:19:23 +0200541static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
Alan Cox4d8d0962011-11-03 18:21:20 +0000542 .gamma_set = psbfb_gamma_set,
543 .gamma_get = psbfb_gamma_get,
544 .fb_probe = psbfb_probe,
545};
546
Kirill A. Shutemovbc7f2b02012-03-08 16:03:44 +0000547static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
Alan Cox4d8d0962011-11-03 18:21:20 +0000548{
Alan Cox4d8d0962011-11-03 18:21:20 +0000549 struct psb_framebuffer *psbfb = &fbdev->pfb;
550
Archit Taneja546187c2015-07-22 14:58:10 +0530551 drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
552 drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
553
Alan Cox4d8d0962011-11-03 18:21:20 +0000554 drm_fb_helper_fini(&fbdev->psb_fb_helper);
Daniel Vetter36206362012-12-10 20:42:17 +0100555 drm_framebuffer_unregister_private(&psbfb->base);
Alan Cox4d8d0962011-11-03 18:21:20 +0000556 drm_framebuffer_cleanup(&psbfb->base);
557
558 if (psbfb->gtt)
Daniel Vetter46a0f222015-11-23 10:32:51 +0100559 drm_gem_object_unreference_unlocked(&psbfb->gtt->gem);
Alan Cox4d8d0962011-11-03 18:21:20 +0000560 return 0;
561}
562
563int psb_fbdev_init(struct drm_device *dev)
564{
565 struct psb_fbdev *fbdev;
566 struct drm_psb_private *dev_priv = dev->dev_private;
Thierry Reding01934c22014-12-19 11:21:32 +0100567 int ret;
Alan Cox4d8d0962011-11-03 18:21:20 +0000568
569 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
570 if (!fbdev) {
571 dev_err(dev->dev, "no memory\n");
572 return -ENOMEM;
573 }
574
575 dev_priv->fbdev = fbdev;
Thierry Reding10a23102014-06-27 17:19:24 +0200576
577 drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
Alan Cox4d8d0962011-11-03 18:21:20 +0000578
Thierry Reding01934c22014-12-19 11:21:32 +0100579 ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
580 dev_priv->ops->crtcs, INTELFB_CONN_LIMIT);
581 if (ret)
582 goto free;
Alan Cox4d8d0962011-11-03 18:21:20 +0000583
Thierry Reding01934c22014-12-19 11:21:32 +0100584 ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
585 if (ret)
586 goto fini;
Daniel Vetter76a39db2013-01-20 23:12:54 +0100587
588 /* disable all the possible outputs/crtcs before entering KMS mode */
589 drm_helper_disable_unused_functions(dev);
590
Thierry Reding01934c22014-12-19 11:21:32 +0100591 ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
592 if (ret)
593 goto fini;
594
Alan Cox4d8d0962011-11-03 18:21:20 +0000595 return 0;
Thierry Reding01934c22014-12-19 11:21:32 +0100596
597fini:
598 drm_fb_helper_fini(&fbdev->psb_fb_helper);
599free:
600 kfree(fbdev);
601 return ret;
Alan Cox4d8d0962011-11-03 18:21:20 +0000602}
603
Kirill A. Shutemovbc7f2b02012-03-08 16:03:44 +0000604static void psb_fbdev_fini(struct drm_device *dev)
Alan Cox4d8d0962011-11-03 18:21:20 +0000605{
606 struct drm_psb_private *dev_priv = dev->dev_private;
607
608 if (!dev_priv->fbdev)
609 return;
610
611 psb_fbdev_destroy(dev, dev_priv->fbdev);
612 kfree(dev_priv->fbdev);
613 dev_priv->fbdev = NULL;
614}
615
616static void psbfb_output_poll_changed(struct drm_device *dev)
617{
618 struct drm_psb_private *dev_priv = dev->dev_private;
619 struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
620 drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
621}
622
623/**
624 * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
625 * @fb: framebuffer
626 * @file_priv: our DRM file
627 * @handle: returned handle
628 *
629 * Our framebuffer object is a GTT range which also contains a GEM
630 * object. We need to turn it into a handle for userspace. GEM will do
631 * the work for us
632 */
633static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
634 struct drm_file *file_priv,
635 unsigned int *handle)
636{
637 struct psb_framebuffer *psbfb = to_psb_fb(fb);
638 struct gtt_range *r = psbfb->gtt;
639 return drm_gem_handle_create(file_priv, &r->gem, handle);
640}
641
642/**
643 * psb_user_framebuffer_destroy - destruct user created fb
644 * @fb: framebuffer
645 *
646 * User framebuffers are backed by GEM objects so all we have to do is
647 * clean up a bit and drop the reference, GEM will handle the fallout
648 */
649static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
650{
651 struct psb_framebuffer *psbfb = to_psb_fb(fb);
652 struct gtt_range *r = psbfb->gtt;
Alan Cox4d8d0962011-11-03 18:21:20 +0000653
654 /* Let DRM do its clean up */
655 drm_framebuffer_cleanup(fb);
656 /* We are no longer using the resource in GEM */
657 drm_gem_object_unreference_unlocked(&r->gem);
658 kfree(fb);
659}
660
661static const struct drm_mode_config_funcs psb_mode_funcs = {
662 .fb_create = psb_user_framebuffer_create,
663 .output_poll_changed = psbfb_output_poll_changed,
664};
665
Alan Cox4d8d0962011-11-03 18:21:20 +0000666static void psb_setup_outputs(struct drm_device *dev)
667{
668 struct drm_psb_private *dev_priv = dev->dev_private;
669 struct drm_connector *connector;
670
671 drm_mode_create_scaling_mode_property(dev);
Alan Cox4d8d0962011-11-03 18:21:20 +0000672
Alan Cox13619ce2016-01-29 19:37:48 +0000673 /* It is ok for this to fail - we just don't get backlight control */
674 if (!dev_priv->backlight_property)
675 dev_priv->backlight_property = drm_property_create_range(dev, 0,
676 "backlight", 0, 100);
Alan Cox4d8d0962011-11-03 18:21:20 +0000677 dev_priv->ops->output_init(dev);
678
679 list_for_each_entry(connector, &dev->mode_config.connector_list,
680 head) {
Patrik Jakobsson367e4402013-07-22 17:45:26 +0200681 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
682 struct drm_encoder *encoder = &gma_encoder->base;
Alan Cox4d8d0962011-11-03 18:21:20 +0000683 int crtc_mask = 0, clone_mask = 0;
684
685 /* valid crtcs */
Patrik Jakobsson367e4402013-07-22 17:45:26 +0200686 switch (gma_encoder->type) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000687 case INTEL_OUTPUT_ANALOG:
688 crtc_mask = (1 << 0);
689 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
690 break;
691 case INTEL_OUTPUT_SDVO:
Patrik Jakobssoncf8efd32013-09-16 17:54:54 +0200692 crtc_mask = dev_priv->ops->sdvo_mask;
Alan Cox4d8d0962011-11-03 18:21:20 +0000693 clone_mask = (1 << INTEL_OUTPUT_SDVO);
694 break;
695 case INTEL_OUTPUT_LVDS:
Alan Coxd235e642012-04-25 14:38:07 +0100696 crtc_mask = dev_priv->ops->lvds_mask;
Alan Cox4d8d0962011-11-03 18:21:20 +0000697 clone_mask = (1 << INTEL_OUTPUT_LVDS);
698 break;
699 case INTEL_OUTPUT_MIPI:
700 crtc_mask = (1 << 0);
701 clone_mask = (1 << INTEL_OUTPUT_MIPI);
702 break;
703 case INTEL_OUTPUT_MIPI2:
704 crtc_mask = (1 << 2);
705 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
706 break;
707 case INTEL_OUTPUT_HDMI:
Alan Coxd235e642012-04-25 14:38:07 +0100708 crtc_mask = dev_priv->ops->hdmi_mask;
Alan Cox4d8d0962011-11-03 18:21:20 +0000709 clone_mask = (1 << INTEL_OUTPUT_HDMI);
710 break;
Alan Cox220801b2012-08-08 13:54:41 +0000711 case INTEL_OUTPUT_DISPLAYPORT:
712 crtc_mask = (1 << 0) | (1 << 1);
713 clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
714 break;
Zhao Yakuid112a812012-08-08 13:55:55 +0000715 case INTEL_OUTPUT_EDP:
716 crtc_mask = (1 << 1);
717 clone_mask = (1 << INTEL_OUTPUT_EDP);
Alan Cox4d8d0962011-11-03 18:21:20 +0000718 }
719 encoder->possible_crtcs = crtc_mask;
720 encoder->possible_clones =
Patrik Jakobssona3d5d752013-07-22 17:05:25 +0200721 gma_connector_clones(dev, clone_mask);
Alan Cox4d8d0962011-11-03 18:21:20 +0000722 }
723}
724
725void psb_modeset_init(struct drm_device *dev)
726{
727 struct drm_psb_private *dev_priv = dev->dev_private;
728 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
729 int i;
730
731 drm_mode_config_init(dev);
732
733 dev->mode_config.min_width = 0;
734 dev->mode_config.min_height = 0;
735
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200736 dev->mode_config.funcs = &psb_mode_funcs;
Alan Cox4d8d0962011-11-03 18:21:20 +0000737
738 /* set memory base */
Alan Coxdffc9ce2011-11-29 22:19:47 +0000739 /* Oaktrail and Poulsbo should use BAR 2*/
Alan Cox4d8d0962011-11-03 18:21:20 +0000740 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
741 &(dev->mode_config.fb_base));
742
743 /* num pipes is 2 for PSB but 1 for Mrst */
744 for (i = 0; i < dev_priv->num_pipe; i++)
745 psb_intel_crtc_init(dev, i, mode_dev);
746
Patrik Jakobssoncbbd3792013-04-25 22:23:36 +0200747 dev->mode_config.max_width = 4096;
748 dev->mode_config.max_height = 4096;
Alan Cox4d8d0962011-11-03 18:21:20 +0000749
750 psb_setup_outputs(dev);
Alan Coxd235e642012-04-25 14:38:07 +0100751
752 if (dev_priv->ops->errata)
753 dev_priv->ops->errata(dev);
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100754
755 dev_priv->modeset = true;
Alan Cox4d8d0962011-11-03 18:21:20 +0000756}
757
758void psb_modeset_cleanup(struct drm_device *dev)
759{
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100760 struct drm_psb_private *dev_priv = dev->dev_private;
761 if (dev_priv->modeset) {
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100762 drm_kms_helper_poll_fini(dev);
763 psb_fbdev_fini(dev);
764 drm_mode_config_cleanup(dev);
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100765 }
Alan Cox4d8d0962011-11-03 18:21:20 +0000766}