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/console.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/pfn_t.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/tty.h>
19
20 #include <drm/drm.h>
21 #include <drm/drm_crtc.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_fourcc.h>
24 #include <drm/drm_framebuffer.h>
25 #include <drm/drm_gem_framebuffer_helper.h>
26
27 #include "framebuffer.h"
28 #include "gem.h"
29 #include "psb_drv.h"
30 #include "psb_intel_drv.h"
31 #include "psb_intel_reg.h"
32
33 static const struct drm_framebuffer_funcs psb_fb_funcs = {
34 .destroy = drm_gem_fb_destroy,
35 .create_handle = drm_gem_fb_create_handle,
36 };
37
38 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
39
psbfb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)40 static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
41 unsigned blue, unsigned transp,
42 struct fb_info *info)
43 {
44 struct drm_fb_helper *fb_helper = info->par;
45 struct drm_framebuffer *fb = fb_helper->fb;
46 uint32_t v;
47
48 if (!fb)
49 return -ENOMEM;
50
51 if (regno > 255)
52 return 1;
53
54 red = CMAP_TOHW(red, info->var.red.length);
55 blue = CMAP_TOHW(blue, info->var.blue.length);
56 green = CMAP_TOHW(green, info->var.green.length);
57 transp = CMAP_TOHW(transp, info->var.transp.length);
58
59 v = (red << info->var.red.offset) |
60 (green << info->var.green.offset) |
61 (blue << info->var.blue.offset) |
62 (transp << info->var.transp.offset);
63
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
76 return 0;
77 }
78
psbfb_vm_fault(struct vm_fault * vmf)79 static vm_fault_t psbfb_vm_fault(struct vm_fault *vmf)
80 {
81 struct vm_area_struct *vma = vmf->vma;
82 struct drm_framebuffer *fb = vma->vm_private_data;
83 struct drm_device *dev = fb->dev;
84 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
85 struct psb_gem_object *pobj = to_psb_gem_object(fb->obj[0]);
86 int page_num;
87 int i;
88 unsigned long address;
89 vm_fault_t ret = VM_FAULT_SIGBUS;
90 unsigned long pfn;
91 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base + pobj->offset;
92
93 page_num = vma_pages(vma);
94 address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
95
96 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
97
98 for (i = 0; i < page_num; i++) {
99 pfn = (phys_addr >> PAGE_SHIFT);
100
101 ret = vmf_insert_mixed(vma, address,
102 __pfn_to_pfn_t(pfn, PFN_DEV));
103 if (unlikely(ret & VM_FAULT_ERROR))
104 break;
105 address += PAGE_SIZE;
106 phys_addr += PAGE_SIZE;
107 }
108 return ret;
109 }
110
psbfb_vm_open(struct vm_area_struct * vma)111 static void psbfb_vm_open(struct vm_area_struct *vma)
112 {
113 }
114
psbfb_vm_close(struct vm_area_struct * vma)115 static void psbfb_vm_close(struct vm_area_struct *vma)
116 {
117 }
118
119 static const struct vm_operations_struct psbfb_vm_ops = {
120 .fault = psbfb_vm_fault,
121 .open = psbfb_vm_open,
122 .close = psbfb_vm_close
123 };
124
psbfb_mmap(struct fb_info * info,struct vm_area_struct * vma)125 static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
126 {
127 struct drm_fb_helper *fb_helper = info->par;
128 struct drm_framebuffer *fb = fb_helper->fb;
129
130 if (vma->vm_pgoff != 0)
131 return -EINVAL;
132 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
133 return -EINVAL;
134
135 /*
136 * If this is a GEM object then info->screen_base is the virtual
137 * kernel remapping of the object. FIXME: Review if this is
138 * suitable for our mmap work
139 */
140 vma->vm_ops = &psbfb_vm_ops;
141 vma->vm_private_data = (void *)fb;
142 vm_flags_set(vma, VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP);
143 return 0;
144 }
145
146 static const struct fb_ops psbfb_unaccel_ops = {
147 .owner = THIS_MODULE,
148 DRM_FB_HELPER_DEFAULT_OPS,
149 .fb_setcolreg = psbfb_setcolreg,
150 .fb_fillrect = drm_fb_helper_cfb_fillrect,
151 .fb_copyarea = drm_fb_helper_cfb_copyarea,
152 .fb_imageblit = drm_fb_helper_cfb_imageblit,
153 .fb_mmap = psbfb_mmap,
154 };
155
156 /**
157 * psb_framebuffer_init - initialize a framebuffer
158 * @dev: our DRM device
159 * @fb: framebuffer to set up
160 * @mode_cmd: mode description
161 * @obj: backing object
162 *
163 * Configure and fill in the boilerplate for our frame buffer. Return
164 * 0 on success or an error code if we fail.
165 */
psb_framebuffer_init(struct drm_device * dev,struct drm_framebuffer * fb,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object * obj)166 static int psb_framebuffer_init(struct drm_device *dev,
167 struct drm_framebuffer *fb,
168 const struct drm_mode_fb_cmd2 *mode_cmd,
169 struct drm_gem_object *obj)
170 {
171 const struct drm_format_info *info;
172 int ret;
173
174 /*
175 * Reject unknown formats, YUV formats, and formats with more than
176 * 4 bytes per pixel.
177 */
178 info = drm_get_format_info(dev, mode_cmd);
179 if (!info || !info->depth || info->cpp[0] > 4)
180 return -EINVAL;
181
182 if (mode_cmd->pitches[0] & 63)
183 return -EINVAL;
184
185 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
186 fb->obj[0] = obj;
187 ret = drm_framebuffer_init(dev, fb, &psb_fb_funcs);
188 if (ret) {
189 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
190 return ret;
191 }
192 return 0;
193 }
194
195 /**
196 * psb_framebuffer_create - create a framebuffer backed by gt
197 * @dev: our DRM device
198 * @mode_cmd: the description of the requested mode
199 * @obj: the backing object
200 *
201 * Create a framebuffer object backed by the gt, and fill in the
202 * boilerplate required
203 *
204 * TODO: review object references
205 */
206
psb_framebuffer_create(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object * obj)207 static struct drm_framebuffer *psb_framebuffer_create
208 (struct drm_device *dev,
209 const struct drm_mode_fb_cmd2 *mode_cmd,
210 struct drm_gem_object *obj)
211 {
212 struct drm_framebuffer *fb;
213 int ret;
214
215 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
216 if (!fb)
217 return ERR_PTR(-ENOMEM);
218
219 ret = psb_framebuffer_init(dev, fb, mode_cmd, obj);
220 if (ret) {
221 kfree(fb);
222 return ERR_PTR(ret);
223 }
224 return fb;
225 }
226
227 /**
228 * psbfb_create - create a framebuffer
229 * @fb_helper: the framebuffer helper
230 * @sizes: specification of the layout
231 *
232 * Create a framebuffer to the specifications provided
233 */
psbfb_create(struct drm_fb_helper * fb_helper,struct drm_fb_helper_surface_size * sizes)234 static int psbfb_create(struct drm_fb_helper *fb_helper,
235 struct drm_fb_helper_surface_size *sizes)
236 {
237 struct drm_device *dev = fb_helper->dev;
238 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
239 struct pci_dev *pdev = to_pci_dev(dev->dev);
240 struct fb_info *info;
241 struct drm_framebuffer *fb;
242 struct drm_mode_fb_cmd2 mode_cmd;
243 int size;
244 int ret;
245 struct psb_gem_object *backing;
246 struct drm_gem_object *obj;
247 u32 bpp, depth;
248
249 mode_cmd.width = sizes->surface_width;
250 mode_cmd.height = sizes->surface_height;
251 bpp = sizes->surface_bpp;
252 depth = sizes->surface_depth;
253
254 /* No 24bit packed */
255 if (bpp == 24)
256 bpp = 32;
257
258 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * DIV_ROUND_UP(bpp, 8), 64);
259
260 size = mode_cmd.pitches[0] * mode_cmd.height;
261 size = ALIGN(size, PAGE_SIZE);
262
263 /* Allocate the framebuffer in the GTT with stolen page backing */
264 backing = psb_gem_create(dev, size, "fb", true, PAGE_SIZE);
265 if (IS_ERR(backing))
266 return PTR_ERR(backing);
267 obj = &backing->base;
268
269 memset(dev_priv->vram_addr + backing->offset, 0, size);
270
271 info = drm_fb_helper_alloc_fbi(fb_helper);
272 if (IS_ERR(info)) {
273 ret = PTR_ERR(info);
274 goto err_drm_gem_object_put;
275 }
276
277 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
278
279 fb = psb_framebuffer_create(dev, &mode_cmd, obj);
280 if (IS_ERR(fb)) {
281 ret = PTR_ERR(fb);
282 goto err_drm_gem_object_put;
283 }
284
285 fb_helper->fb = fb;
286
287 info->fbops = &psbfb_unaccel_ops;
288
289 info->fix.smem_start = dev->mode_config.fb_base;
290 info->fix.smem_len = size;
291 info->fix.ywrapstep = 0;
292 info->fix.ypanstep = 0;
293
294 /* Accessed stolen memory directly */
295 info->screen_base = dev_priv->vram_addr + backing->offset;
296 info->screen_size = size;
297
298 if (dev_priv->gtt.stolen_size) {
299 info->apertures->ranges[0].base = dev->mode_config.fb_base;
300 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
301 }
302
303 drm_fb_helper_fill_info(info, fb_helper, sizes);
304
305 info->fix.mmio_start = pci_resource_start(pdev, 0);
306 info->fix.mmio_len = pci_resource_len(pdev, 0);
307
308 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
309
310 dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
311
312 return 0;
313
314 err_drm_gem_object_put:
315 drm_gem_object_put(obj);
316 return ret;
317 }
318
319 /**
320 * psb_user_framebuffer_create - create framebuffer
321 * @dev: our DRM device
322 * @filp: client file
323 * @cmd: mode request
324 *
325 * Create a new framebuffer backed by a userspace GEM object
326 */
psb_user_framebuffer_create(struct drm_device * dev,struct drm_file * filp,const struct drm_mode_fb_cmd2 * cmd)327 static struct drm_framebuffer *psb_user_framebuffer_create
328 (struct drm_device *dev, struct drm_file *filp,
329 const struct drm_mode_fb_cmd2 *cmd)
330 {
331 struct drm_gem_object *obj;
332 struct drm_framebuffer *fb;
333
334 /*
335 * Find the GEM object and thus the gtt range object that is
336 * to back this space
337 */
338 obj = drm_gem_object_lookup(filp, cmd->handles[0]);
339 if (obj == NULL)
340 return ERR_PTR(-ENOENT);
341
342 /* Let the core code do all the work */
343 fb = psb_framebuffer_create(dev, cmd, obj);
344 if (IS_ERR(fb))
345 drm_gem_object_put(obj);
346
347 return fb;
348 }
349
psbfb_probe(struct drm_fb_helper * fb_helper,struct drm_fb_helper_surface_size * sizes)350 static int psbfb_probe(struct drm_fb_helper *fb_helper,
351 struct drm_fb_helper_surface_size *sizes)
352 {
353 struct drm_device *dev = fb_helper->dev;
354 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
355 unsigned int fb_size;
356 int bytespp;
357
358 bytespp = sizes->surface_bpp / 8;
359 if (bytespp == 3) /* no 24bit packed */
360 bytespp = 4;
361
362 /* If the mode will not fit in 32bit then switch to 16bit to get
363 a console on full resolution. The X mode setting server will
364 allocate its own 32bit GEM framebuffer */
365 fb_size = ALIGN(sizes->surface_width * bytespp, 64) *
366 sizes->surface_height;
367 fb_size = ALIGN(fb_size, PAGE_SIZE);
368
369 if (fb_size > dev_priv->vram_stolen_size) {
370 sizes->surface_bpp = 16;
371 sizes->surface_depth = 16;
372 }
373
374 return psbfb_create(fb_helper, sizes);
375 }
376
377 static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
378 .fb_probe = psbfb_probe,
379 };
380
psb_fbdev_destroy(struct drm_device * dev,struct drm_fb_helper * fb_helper)381 static int psb_fbdev_destroy(struct drm_device *dev,
382 struct drm_fb_helper *fb_helper)
383 {
384 struct drm_framebuffer *fb = fb_helper->fb;
385
386 drm_fb_helper_unregister_fbi(fb_helper);
387
388 drm_fb_helper_fini(fb_helper);
389 drm_framebuffer_unregister_private(fb);
390 drm_framebuffer_cleanup(fb);
391
392 if (fb->obj[0])
393 drm_gem_object_put(fb->obj[0]);
394 kfree(fb);
395
396 return 0;
397 }
398
psb_fbdev_init(struct drm_device * dev)399 int psb_fbdev_init(struct drm_device *dev)
400 {
401 struct drm_fb_helper *fb_helper;
402 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
403 int ret;
404
405 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
406 if (!fb_helper) {
407 dev_err(dev->dev, "no memory\n");
408 return -ENOMEM;
409 }
410
411 dev_priv->fb_helper = fb_helper;
412
413 drm_fb_helper_prepare(dev, fb_helper, &psb_fb_helper_funcs);
414
415 ret = drm_fb_helper_init(dev, fb_helper);
416 if (ret)
417 goto free;
418
419 /* disable all the possible outputs/crtcs before entering KMS mode */
420 drm_helper_disable_unused_functions(dev);
421
422 ret = drm_fb_helper_initial_config(fb_helper, 32);
423 if (ret)
424 goto fini;
425
426 return 0;
427
428 fini:
429 drm_fb_helper_fini(fb_helper);
430 free:
431 kfree(fb_helper);
432 return ret;
433 }
434
psb_fbdev_fini(struct drm_device * dev)435 static void psb_fbdev_fini(struct drm_device *dev)
436 {
437 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
438
439 if (!dev_priv->fb_helper)
440 return;
441
442 psb_fbdev_destroy(dev, dev_priv->fb_helper);
443 kfree(dev_priv->fb_helper);
444 dev_priv->fb_helper = NULL;
445 }
446
447 static const struct drm_mode_config_funcs psb_mode_funcs = {
448 .fb_create = psb_user_framebuffer_create,
449 .output_poll_changed = drm_fb_helper_output_poll_changed,
450 };
451
psb_setup_outputs(struct drm_device * dev)452 static void psb_setup_outputs(struct drm_device *dev)
453 {
454 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
455 struct drm_connector_list_iter conn_iter;
456 struct drm_connector *connector;
457
458 drm_mode_create_scaling_mode_property(dev);
459
460 /* It is ok for this to fail - we just don't get backlight control */
461 if (!dev_priv->backlight_property)
462 dev_priv->backlight_property = drm_property_create_range(dev, 0,
463 "backlight", 0, 100);
464 dev_priv->ops->output_init(dev);
465
466 drm_connector_list_iter_begin(dev, &conn_iter);
467 drm_for_each_connector_iter(connector, &conn_iter) {
468 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
469 struct drm_encoder *encoder = &gma_encoder->base;
470 int crtc_mask = 0, clone_mask = 0;
471
472 /* valid crtcs */
473 switch (gma_encoder->type) {
474 case INTEL_OUTPUT_ANALOG:
475 crtc_mask = (1 << 0);
476 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
477 break;
478 case INTEL_OUTPUT_SDVO:
479 crtc_mask = dev_priv->ops->sdvo_mask;
480 clone_mask = 0;
481 break;
482 case INTEL_OUTPUT_LVDS:
483 crtc_mask = dev_priv->ops->lvds_mask;
484 clone_mask = 0;
485 break;
486 case INTEL_OUTPUT_MIPI:
487 crtc_mask = (1 << 0);
488 clone_mask = 0;
489 break;
490 case INTEL_OUTPUT_MIPI2:
491 crtc_mask = (1 << 2);
492 clone_mask = 0;
493 break;
494 case INTEL_OUTPUT_HDMI:
495 crtc_mask = dev_priv->ops->hdmi_mask;
496 clone_mask = (1 << INTEL_OUTPUT_HDMI);
497 break;
498 case INTEL_OUTPUT_DISPLAYPORT:
499 crtc_mask = (1 << 0) | (1 << 1);
500 clone_mask = 0;
501 break;
502 case INTEL_OUTPUT_EDP:
503 crtc_mask = (1 << 1);
504 clone_mask = 0;
505 }
506 encoder->possible_crtcs = crtc_mask;
507 encoder->possible_clones =
508 gma_connector_clones(dev, clone_mask);
509 }
510 drm_connector_list_iter_end(&conn_iter);
511 }
512
psb_modeset_init(struct drm_device * dev)513 void psb_modeset_init(struct drm_device *dev)
514 {
515 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
516 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
517 struct pci_dev *pdev = to_pci_dev(dev->dev);
518 int i;
519
520 if (drmm_mode_config_init(dev))
521 return;
522
523 dev->mode_config.min_width = 0;
524 dev->mode_config.min_height = 0;
525
526 dev->mode_config.funcs = &psb_mode_funcs;
527
528 /* set memory base */
529 /* Oaktrail and Poulsbo should use BAR 2*/
530 pci_read_config_dword(pdev, PSB_BSM, (u32 *)&(dev->mode_config.fb_base));
531
532 /* num pipes is 2 for PSB but 1 for Mrst */
533 for (i = 0; i < dev_priv->num_pipe; i++)
534 psb_intel_crtc_init(dev, i, mode_dev);
535
536 dev->mode_config.max_width = 4096;
537 dev->mode_config.max_height = 4096;
538
539 psb_setup_outputs(dev);
540
541 if (dev_priv->ops->errata)
542 dev_priv->ops->errata(dev);
543
544 dev_priv->modeset = true;
545 }
546
psb_modeset_cleanup(struct drm_device * dev)547 void psb_modeset_cleanup(struct drm_device *dev)
548 {
549 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
550 if (dev_priv->modeset) {
551 drm_kms_helper_poll_fini(dev);
552 psb_fbdev_fini(dev);
553 }
554 }
555