ccf7bd96f7544d56ccda152eff87805cfccb2ae8
[openwrt/staging/blogic.git] /
1 From 35cc50251cbf17c5e855879bcf6d71995f0f271a Mon Sep 17 00:00:00 2001
2 From: James Hughes <james.hughes@raspberrypi.org>
3 Date: Thu, 14 Mar 2019 13:27:54 +0000
4 Subject: [PATCH] Pulled in the multi frame buffer support from the Pi3
5 repo
6
7 ---
8 drivers/video/fbdev/bcm2708_fb.c | 457 +++++++++++++++------
9 include/soc/bcm2835/raspberrypi-firmware.h | 13 +
10 2 files changed, 337 insertions(+), 133 deletions(-)
11
12 --- a/drivers/video/fbdev/bcm2708_fb.c
13 +++ b/drivers/video/fbdev/bcm2708_fb.c
14 @@ -2,6 +2,7 @@
15 * linux/drivers/video/bcm2708_fb.c
16 *
17 * Copyright (C) 2010 Broadcom
18 + * Copyright (C) 2018 Raspberry Pi (Trading) Ltd
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of this archive
22 @@ -13,6 +14,7 @@
23 * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
24 *
25 */
26 +
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 @@ -33,6 +35,7 @@
31 #include <linux/io.h>
32 #include <linux/dma-mapping.h>
33 #include <soc/bcm2835/raspberrypi-firmware.h>
34 +#include <linux/mutex.h>
35
36 //#define BCM2708_FB_DEBUG
37 #define MODULE_NAME "bcm2708_fb"
38 @@ -79,64 +82,150 @@ struct bcm2708_fb_stats {
39 u32 dma_irqs;
40 };
41
42 +struct vc4_display_settings_t {
43 + u32 display_num;
44 + u32 width;
45 + u32 height;
46 + u32 depth;
47 + u32 pitch;
48 + u32 virtual_width;
49 + u32 virtual_height;
50 + u32 virtual_width_offset;
51 + u32 virtual_height_offset;
52 + unsigned long fb_bus_address;
53 +};
54 +
55 +struct bcm2708_fb_dev;
56 +
57 struct bcm2708_fb {
58 struct fb_info fb;
59 struct platform_device *dev;
60 - struct rpi_firmware *fw;
61 u32 cmap[16];
62 u32 gpu_cmap[256];
63 - int dma_chan;
64 - int dma_irq;
65 - void __iomem *dma_chan_base;
66 - void *cb_base; /* DMA control blocks */
67 - dma_addr_t cb_handle;
68 struct dentry *debugfs_dir;
69 - wait_queue_head_t dma_waitq;
70 - struct bcm2708_fb_stats stats;
71 + struct dentry *debugfs_subdir;
72 unsigned long fb_bus_address;
73 - bool disable_arm_alloc;
74 + struct { u32 base, length; } gpu;
75 + struct vc4_display_settings_t display_settings;
76 + struct debugfs_regset32 screeninfo_regset;
77 + struct bcm2708_fb_dev *fbdev;
78 unsigned int image_size;
79 dma_addr_t dma_addr;
80 void *cpuaddr;
81 };
82
83 +#define MAX_FRAMEBUFFERS 3
84 +
85 +struct bcm2708_fb_dev {
86 + int firmware_supports_multifb;
87 + /* Protects the DMA system from multiple FB access */
88 + struct mutex dma_mutex;
89 + int dma_chan;
90 + int dma_irq;
91 + void __iomem *dma_chan_base;
92 + wait_queue_head_t dma_waitq;
93 + bool disable_arm_alloc;
94 + struct bcm2708_fb_stats dma_stats;
95 + void *cb_base; /* DMA control blocks */
96 + dma_addr_t cb_handle;
97 + int instance_count;
98 + int num_displays;
99 + struct rpi_firmware *fw;
100 + struct bcm2708_fb displays[MAX_FRAMEBUFFERS];
101 +};
102 +
103 #define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
104
105 static void bcm2708_fb_debugfs_deinit(struct bcm2708_fb *fb)
106 {
107 - debugfs_remove_recursive(fb->debugfs_dir);
108 - fb->debugfs_dir = NULL;
109 + debugfs_remove_recursive(fb->debugfs_subdir);
110 + fb->debugfs_subdir = NULL;
111 +
112 + fb->fbdev->instance_count--;
113 +
114 + if (!fb->fbdev->instance_count) {
115 + debugfs_remove_recursive(fb->debugfs_dir);
116 + fb->debugfs_dir = NULL;
117 + }
118 }
119
120 static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
121 {
122 + char buf[3];
123 + struct bcm2708_fb_dev *fbdev = fb->fbdev;
124 +
125 static struct debugfs_reg32 stats_registers[] = {
126 - {
127 - "dma_copies",
128 - offsetof(struct bcm2708_fb_stats, dma_copies)
129 - },
130 - {
131 - "dma_irqs",
132 - offsetof(struct bcm2708_fb_stats, dma_irqs)
133 - },
134 + {"dma_copies", offsetof(struct bcm2708_fb_stats, dma_copies)},
135 + {"dma_irqs", offsetof(struct bcm2708_fb_stats, dma_irqs)},
136 };
137
138 - fb->debugfs_dir = debugfs_create_dir(DRIVER_NAME, NULL);
139 + static struct debugfs_reg32 screeninfo[] = {
140 + {"width", offsetof(struct fb_var_screeninfo, xres)},
141 + {"height", offsetof(struct fb_var_screeninfo, yres)},
142 + {"bpp", offsetof(struct fb_var_screeninfo, bits_per_pixel)},
143 + {"xres_virtual", offsetof(struct fb_var_screeninfo, xres_virtual)},
144 + {"yres_virtual", offsetof(struct fb_var_screeninfo, yres_virtual)},
145 + {"xoffset", offsetof(struct fb_var_screeninfo, xoffset)},
146 + {"yoffset", offsetof(struct fb_var_screeninfo, yoffset)},
147 + };
148 +
149 + fb->debugfs_dir = debugfs_lookup(DRIVER_NAME, NULL);
150 +
151 + if (!fb->debugfs_dir)
152 + fb->debugfs_dir = debugfs_create_dir(DRIVER_NAME, NULL);
153 +
154 if (!fb->debugfs_dir) {
155 - pr_warn("%s: could not create debugfs entry\n",
156 - __func__);
157 + dev_warn(fb->fb.dev, "%s: could not create debugfs folder\n",
158 + __func__);
159 return -EFAULT;
160 }
161
162 - fb->stats.regset.regs = stats_registers;
163 - fb->stats.regset.nregs = ARRAY_SIZE(stats_registers);
164 - fb->stats.regset.base = &fb->stats;
165 + snprintf(buf, sizeof(buf), "%u", fb->display_settings.display_num);
166 +
167 + fb->debugfs_subdir = debugfs_create_dir(buf, fb->debugfs_dir);
168
169 debugfs_create_regset32("stats", 0444, fb->debugfs_dir,
170 &fb->stats.regset);
171 +
172 + if (!fb->debugfs_subdir) {
173 + dev_warn(fb->fb.dev, "%s: could not create debugfs entry %u\n",
174 + __func__, fb->display_settings.display_num);
175 + return -EFAULT;
176 + }
177 +
178 + fbdev->dma_stats.regset.regs = stats_registers;
179 + fbdev->dma_stats.regset.nregs = ARRAY_SIZE(stats_registers);
180 + fbdev->dma_stats.regset.base = &fbdev->dma_stats;
181 +
182 + debugfs_create_regset32("dma_stats", 0444, fb->debugfs_subdir,
183 + &fbdev->dma_stats.regset);
184 +
185 + fb->screeninfo_regset.regs = screeninfo;
186 + fb->screeninfo_regset.nregs = ARRAY_SIZE(screeninfo);
187 + fb->screeninfo_regset.base = &fb->fb.var;
188 +
189 + debugfs_create_regset32("screeninfo", 0444, fb->debugfs_subdir,
190 + &fb->screeninfo_regset);
191 +
192 + fbdev->instance_count++;
193 +
194 return 0;
195 }
196
197 +static void set_display_num(struct bcm2708_fb *fb)
198 +{
199 + if (fb && fb->fbdev && fb->fbdev->firmware_supports_multifb) {
200 + u32 tmp = fb->display_settings.display_num;
201 +
202 + if (rpi_firmware_property(fb->fbdev->fw,
203 + RPI_FIRMWARE_FRAMEBUFFER_SET_DISPLAY_NUM,
204 + &tmp,
205 + sizeof(tmp)))
206 + dev_warn_once(fb->fb.dev,
207 + "Set display number call failed. Old GPU firmware?");
208 + }
209 +}
210 +
211 static int bcm2708_fb_set_bitfields(struct fb_var_screeninfo *var)
212 {
213 int ret = 0;
214 @@ -214,11 +303,11 @@ static int bcm2708_fb_check_var(struct f
215 struct fb_info *info)
216 {
217 /* info input, var output */
218 - print_debug("%s(%p) %dx%d (%dx%d), %d, %d\n",
219 + print_debug("%s(%p) %ux%u (%ux%u), %ul, %u\n",
220 __func__, info, info->var.xres, info->var.yres,
221 info->var.xres_virtual, info->var.yres_virtual,
222 - (int)info->screen_size, info->var.bits_per_pixel);
223 - print_debug("%s(%p) %dx%d (%dx%d), %d\n", __func__, var, var->xres,
224 + info->screen_size, info->var.bits_per_pixel);
225 + print_debug("%s(%p) %ux%u (%ux%u), %u\n", __func__, var, var->xres,
226 var->yres, var->xres_virtual, var->yres_virtual,
227 var->bits_per_pixel);
228
229 @@ -281,17 +370,24 @@ static int bcm2708_fb_set_par(struct fb_
230 };
231 int ret, image_size;
232
233 -
234 - print_debug("%s(%p) %dx%d (%dx%d), %d, %d\n", __func__, info,
235 + print_debug("%s(%p) %dx%d (%dx%d), %d, %d (display %d)\n", __func__,
236 + info,
237 info->var.xres, info->var.yres, info->var.xres_virtual,
238 info->var.yres_virtual, (int)info->screen_size,
239 - info->var.bits_per_pixel);
240 + info->var.bits_per_pixel, value);
241 +
242 + /* Need to set the display number to act on first
243 + * Cannot do it in the tag list because on older firmware the call
244 + * will fail and stop the rest of the list being executed.
245 + * We can ignore this call failing as the default at other end is 0
246 + */
247 + set_display_num(fb);
248
249 /* Try allocating our own buffer. We can specify all the parameters */
250 image_size = ((info->var.xres * info->var.yres) *
251 info->var.bits_per_pixel) >> 3;
252
253 - if (!fb->disable_arm_alloc &&
254 + if (!fb->fbdev->disable_arm_alloc &&
255 (image_size != fb->image_size || !fb->dma_addr)) {
256 if (fb->dma_addr) {
257 dma_free_coherent(info->device, fb->image_size,
258 @@ -306,7 +402,7 @@ static int bcm2708_fb_set_par(struct fb_
259
260 if (!fb->cpuaddr) {
261 fb->dma_addr = 0;
262 - fb->disable_arm_alloc = true;
263 + fb->fbdev->disable_arm_alloc = true;
264 } else {
265 fb->image_size = image_size;
266 }
267 @@ -317,7 +413,7 @@ static int bcm2708_fb_set_par(struct fb_
268 fbinfo.screen_size = image_size;
269 fbinfo.pitch = (info->var.xres * info->var.bits_per_pixel) >> 3;
270
271 - ret = rpi_firmware_property_list(fb->fw, &fbinfo,
272 + ret = rpi_firmware_property_list(fb->fbdev->fw, &fbinfo,
273 sizeof(fbinfo));
274 if (ret || fbinfo.base != fb->dma_addr) {
275 /* Firmware either failed, or assigned a different base
276 @@ -330,7 +426,7 @@ static int bcm2708_fb_set_par(struct fb_
277 fb->image_size = 0;
278 fb->cpuaddr = NULL;
279 fb->dma_addr = 0;
280 - fb->disable_arm_alloc = true;
281 + fb->fbdev->disable_arm_alloc = true;
282 }
283 } else {
284 /* Our allocation failed - drop into the old scheme of
285 @@ -349,7 +445,7 @@ static int bcm2708_fb_set_par(struct fb_
286 fbinfo.tag6.tag = RPI_FIRMWARE_FRAMEBUFFER_GET_PITCH;
287 fbinfo.pitch = 0;
288
289 - ret = rpi_firmware_property_list(fb->fw, &fbinfo,
290 + ret = rpi_firmware_property_list(fb->fbdev->fw, &fbinfo,
291 sizeof(fbinfo));
292 if (ret) {
293 dev_err(info->device,
294 @@ -439,7 +535,10 @@ static int bcm2708_fb_setcolreg(unsigned
295 packet->length = regno + 1;
296 memcpy(packet->cmap, fb->gpu_cmap,
297 sizeof(packet->cmap));
298 - ret = rpi_firmware_property(fb->fw,
299 +
300 + set_display_num(fb);
301 +
302 + ret = rpi_firmware_property(fb->fbdev->fw,
303 RPI_FIRMWARE_FRAMEBUFFER_SET_PALETTE,
304 packet,
305 (2 + packet->length) * sizeof(u32));
306 @@ -478,8 +577,11 @@ static int bcm2708_fb_blank(int blank_mo
307 return -EINVAL;
308 }
309
310 - ret = rpi_firmware_property(fb->fw, RPI_FIRMWARE_FRAMEBUFFER_BLANK,
311 + set_display_num(fb);
312 +
313 + ret = rpi_firmware_property(fb->fbdev->fw, RPI_FIRMWARE_FRAMEBUFFER_BLANK,
314 &value, sizeof(value));
315 +
316 if (ret)
317 dev_err(info->device, "%s(%d) failed: %d\n", __func__,
318 blank_mode, ret);
319 @@ -496,12 +598,14 @@ static int bcm2708_fb_pan_display(struct
320 info->var.yoffset = var->yoffset;
321 result = bcm2708_fb_set_par(info);
322 if (result != 0)
323 - pr_err("%s(%d,%d) returns=%d\n", __func__, var->xoffset,
324 + pr_err("%s(%u,%u) returns=%d\n", __func__, var->xoffset,
325 var->yoffset, result);
326 return result;
327 }
328
329 static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
330 +static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
331 + unsigned long arg)
332 {
333 struct bcm2708_fb *fb = to_bcm2708(info);
334 u32 dummy = 0;
335 @@ -509,7 +613,9 @@ static int bcm2708_ioctl(struct fb_info
336
337 switch (cmd) {
338 case FBIO_WAITFORVSYNC:
339 - ret = rpi_firmware_property(fb->fw,
340 + set_display_num(fb);
341 +
342 + ret = rpi_firmware_property(fb->fbdev->fw,
343 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC,
344 &dummy, sizeof(dummy));
345 break;
346 @@ -526,23 +632,22 @@ static int bcm2708_ioctl(struct fb_info
347 static void bcm2708_fb_fillrect(struct fb_info *info,
348 const struct fb_fillrect *rect)
349 {
350 - /* (is called) print_debug("bcm2708_fb_fillrect\n"); */
351 cfb_fillrect(info, rect);
352 }
353
354 /* A helper function for configuring dma control block */
355 static void set_dma_cb(struct bcm2708_dma_cb *cb,
356 - int burst_size,
357 - dma_addr_t dst,
358 - int dst_stride,
359 - dma_addr_t src,
360 - int src_stride,
361 - int w,
362 - int h)
363 + int burst_size,
364 + dma_addr_t dst,
365 + int dst_stride,
366 + dma_addr_t src,
367 + int src_stride,
368 + int w,
369 + int h)
370 {
371 cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
372 - BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
373 - BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
374 + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
375 + BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
376 cb->dst = dst;
377 cb->src = src;
378 /*
379 @@ -560,15 +665,19 @@ static void bcm2708_fb_copyarea(struct f
380 const struct fb_copyarea *region)
381 {
382 struct bcm2708_fb *fb = to_bcm2708(info);
383 - struct bcm2708_dma_cb *cb = fb->cb_base;
384 + struct bcm2708_fb_dev *fbdev = fb->fbdev;
385 + struct bcm2708_dma_cb *cb = fbdev->cb_base;
386 int bytes_per_pixel = (info->var.bits_per_pixel + 7) >> 3;
387
388 /* Channel 0 supports larger bursts and is a bit faster */
389 - int burst_size = (fb->dma_chan == 0) ? 8 : 2;
390 + int burst_size = (fbdev->dma_chan == 0) ? 8 : 2;
391 int pixels = region->width * region->height;
392
393 - /* Fallback to cfb_copyarea() if we don't like something */
394 - if (bytes_per_pixel > 4 ||
395 + /* If DMA is currently in use (ie being used on another FB), then
396 + * rather than wait for it to finish, just use the cfb_copyarea
397 + */
398 + if (!mutex_trylock(&fbdev->dma_mutex) ||
399 + bytes_per_pixel > 4 ||
400 info->var.xres * info->var.yres > 1920 * 1200 ||
401 region->width <= 0 || region->width > info->var.xres ||
402 region->height <= 0 || region->height > info->var.yres ||
403 @@ -595,8 +704,8 @@ static void bcm2708_fb_copyarea(struct f
404 * 1920x1200 resolution at 32bpp pixel depth.
405 */
406 int y;
407 - dma_addr_t control_block_pa = fb->cb_handle;
408 - dma_addr_t scratchbuf = fb->cb_handle + 16 * 1024;
409 + dma_addr_t control_block_pa = fbdev->cb_handle;
410 + dma_addr_t scratchbuf = fbdev->cb_handle + 16 * 1024;
411 int scanline_size = bytes_per_pixel * region->width;
412 int scanlines_per_cb = (64 * 1024 - 16 * 1024) / scanline_size;
413
414 @@ -646,10 +755,10 @@ static void bcm2708_fb_copyarea(struct f
415 }
416 set_dma_cb(cb, burst_size,
417 fb->fb_bus_address + dy * fb->fb.fix.line_length +
418 - bytes_per_pixel * region->dx,
419 + bytes_per_pixel * region->dx,
420 stride,
421 fb->fb_bus_address + sy * fb->fb.fix.line_length +
422 - bytes_per_pixel * region->sx,
423 + bytes_per_pixel * region->sx,
424 stride,
425 region->width * bytes_per_pixel,
426 region->height);
427 @@ -659,32 +768,33 @@ static void bcm2708_fb_copyarea(struct f
428 cb->next = 0;
429
430 if (pixels < dma_busy_wait_threshold) {
431 - bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
432 - bcm_dma_wait_idle(fb->dma_chan_base);
433 + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
434 + bcm_dma_wait_idle(fbdev->dma_chan_base);
435 } else {
436 - void __iomem *dma_chan = fb->dma_chan_base;
437 + void __iomem *local_dma_chan = fbdev->dma_chan_base;
438
439 cb->info |= BCM2708_DMA_INT_EN;
440 - bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
441 - while (bcm_dma_is_busy(dma_chan)) {
442 - wait_event_interruptible(fb->dma_waitq,
443 - !bcm_dma_is_busy(dma_chan));
444 + bcm_dma_start(fbdev->dma_chan_base, fbdev->cb_handle);
445 + while (bcm_dma_is_busy(local_dma_chan)) {
446 + wait_event_interruptible(fbdev->dma_waitq,
447 + !bcm_dma_is_busy(local_dma_chan));
448 }
449 - fb->stats.dma_irqs++;
450 + fbdev->dma_stats.dma_irqs++;
451 }
452 - fb->stats.dma_copies++;
453 + fbdev->dma_stats.dma_copies++;
454 +
455 + mutex_unlock(&fbdev->dma_mutex);
456 }
457
458 static void bcm2708_fb_imageblit(struct fb_info *info,
459 const struct fb_image *image)
460 {
461 - /* (is called) print_debug("bcm2708_fb_imageblit\n"); */
462 cfb_imageblit(info, image);
463 }
464
465 static irqreturn_t bcm2708_fb_dma_irq(int irq, void *cxt)
466 {
467 - struct bcm2708_fb *fb = cxt;
468 + struct bcm2708_fb_dev *fbdev = cxt;
469
470 /* FIXME: should read status register to check if this is
471 * actually interrupting us or not, in case this interrupt
472 @@ -694,9 +804,9 @@ static irqreturn_t bcm2708_fb_dma_irq(in
473 */
474
475 /* acknowledge the interrupt */
476 - writel(BCM2708_DMA_INT, fb->dma_chan_base + BCM2708_DMA_CS);
477 + writel(BCM2708_DMA_INT, fbdev->dma_chan_base + BCM2708_DMA_CS);
478
479 - wake_up(&fb->dma_waitq);
480 + wake_up(&fbdev->dma_waitq);
481 return IRQ_HANDLED;
482 }
483
484 @@ -729,11 +839,23 @@ static int bcm2708_fb_register(struct bc
485 fb->fb.fix.ywrapstep = 0;
486 fb->fb.fix.accel = FB_ACCEL_NONE;
487
488 - fb->fb.var.xres = fbwidth;
489 - fb->fb.var.yres = fbheight;
490 - fb->fb.var.xres_virtual = fbwidth;
491 - fb->fb.var.yres_virtual = fbheight;
492 - fb->fb.var.bits_per_pixel = fbdepth;
493 + /* If we have data from the VC4 on FB's, use that, otherwise use the
494 + * module parameters
495 + */
496 + if (fb->display_settings.width) {
497 + fb->fb.var.xres = fb->display_settings.width;
498 + fb->fb.var.yres = fb->display_settings.height;
499 + fb->fb.var.xres_virtual = fb->fb.var.xres;
500 + fb->fb.var.yres_virtual = fb->fb.var.yres;
501 + fb->fb.var.bits_per_pixel = fb->display_settings.depth;
502 + } else {
503 + fb->fb.var.xres = fbwidth;
504 + fb->fb.var.yres = fbheight;
505 + fb->fb.var.xres_virtual = fbwidth;
506 + fb->fb.var.yres_virtual = fbheight;
507 + fb->fb.var.bits_per_pixel = fbdepth;
508 + }
509 +
510 fb->fb.var.vmode = FB_VMODE_NONINTERLACED;
511 fb->fb.var.activate = FB_ACTIVATE_NOW;
512 fb->fb.var.nonstd = 0;
513 @@ -749,26 +871,23 @@ static int bcm2708_fb_register(struct bc
514 fb->fb.monspecs.dclkmax = 100000000;
515
516 bcm2708_fb_set_bitfields(&fb->fb.var);
517 - init_waitqueue_head(&fb->dma_waitq);
518
519 /*
520 * Allocate colourmap.
521 */
522 -
523 fb_set_var(&fb->fb, &fb->fb.var);
524 +
525 ret = bcm2708_fb_set_par(&fb->fb);
526 +
527 if (ret)
528 return ret;
529
530 - print_debug("BCM2708FB: registering framebuffer (%dx%d@%d) (%d)\n",
531 - fbwidth, fbheight, fbdepth, fbswap);
532 -
533 ret = register_framebuffer(&fb->fb);
534 - print_debug("BCM2708FB: register framebuffer (%d)\n", ret);
535 +
536 if (ret == 0)
537 goto out;
538
539 - print_debug("BCM2708FB: cannot register framebuffer (%d)\n", ret);
540 + dev_warn(fb->fb.dev, "Unable to register framebuffer (%d)\n", ret);
541 out:
542 return ret;
543 }
544 @@ -777,10 +896,18 @@ static int bcm2708_fb_probe(struct platf
545 {
546 struct device_node *fw_np;
547 struct rpi_firmware *fw;
548 - struct bcm2708_fb *fb;
549 - int ret;
550 + int ret, i;
551 + u32 num_displays;
552 + struct bcm2708_fb_dev *fbdev;
553 + struct { u32 base, length; } gpu_mem;
554 +
555 + fbdev = devm_kzalloc(&dev->dev, sizeof(*fbdev), GFP_KERNEL);
556 +
557 + if (!fbdev)
558 + return -ENOMEM;
559
560 fw_np = of_parse_phandle(dev->dev.of_node, "firmware", 0);
561 +
562 /* Remove comment when booting without Device Tree is no longer supported
563 * if (!fw_np) {
564 * dev_err(&dev->dev, "Missing firmware node\n");
565 @@ -788,90 +915,154 @@ static int bcm2708_fb_probe(struct platf
566 * }
567 */
568 fw = rpi_firmware_get(fw_np);
569 + fbdev->fw = fw;
570 +
571 if (!fw)
572 return -EPROBE_DEFER;
573
574 - fb = kzalloc(sizeof(*fb), GFP_KERNEL);
575 - if (!fb) {
576 - ret = -ENOMEM;
577 - goto free_region;
578 + ret = rpi_firmware_property(fw,
579 + RPI_FIRMWARE_FRAMEBUFFER_GET_NUM_DISPLAYS,
580 + &num_displays, sizeof(u32));
581 +
582 + /* If we fail to get the number of displays, or it returns 0, then
583 + * assume old firmware that doesn't have the mailbox call, so just
584 + * set one display
585 + */
586 + if (ret || num_displays == 0) {
587 + num_displays = 1;
588 + dev_err(&dev->dev,
589 + "Unable to determine number of FB's. Assuming 1\n");
590 + ret = 0;
591 + } else {
592 + fbdev->firmware_supports_multifb = 1;
593 + }
594 +
595 + if (num_displays > MAX_FRAMEBUFFERS) {
596 + dev_warn(&dev->dev,
597 + "More displays reported from firmware than supported in driver (%u vs %u)",
598 + num_displays, MAX_FRAMEBUFFERS);
599 + num_displays = MAX_FRAMEBUFFERS;
600 }
601
602 - fb->fw = fw;
603 - bcm2708_fb_debugfs_init(fb);
604 + dev_info(&dev->dev, "FB found %d display(s)\n", num_displays);
605 +
606 + /* Set up the DMA information. Note we have just one set of DMA
607 + * parameters to work with all the FB's so requires synchronising when
608 + * being used
609 + */
610 +
611 + mutex_init(&fbdev->dma_mutex);
612
613 - fb->cb_base = dma_alloc_wc(&dev->dev, SZ_64K,
614 - &fb->cb_handle, GFP_KERNEL);
615 - if (!fb->cb_base) {
616 + fbdev->cb_base = dma_alloc_wc(&dev->dev, SZ_64K,
617 + &fbdev->cb_handle,
618 + GFP_KERNEL);
619 + if (!fbdev->cb_base) {
620 dev_err(&dev->dev, "cannot allocate DMA CBs\n");
621 ret = -ENOMEM;
622 goto free_fb;
623 }
624
625 - pr_info("BCM2708FB: allocated DMA memory %pad\n", &fb->cb_handle);
626 -
627 ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_BULK,
628 - &fb->dma_chan_base, &fb->dma_irq);
629 + &fbdev->dma_chan_base,
630 + &fbdev->dma_irq);
631 if (ret < 0) {
632 - dev_err(&dev->dev, "couldn't allocate a DMA channel\n");
633 + dev_err(&dev->dev, "Couldn't allocate a DMA channel\n");
634 goto free_cb;
635 }
636 - fb->dma_chan = ret;
637 + fbdev->dma_chan = ret;
638
639 - ret = request_irq(fb->dma_irq, bcm2708_fb_dma_irq,
640 - 0, "bcm2708_fb dma", fb);
641 + ret = request_irq(fbdev->dma_irq, bcm2708_fb_dma_irq,
642 + 0, "bcm2708_fb DMA", fbdev);
643 if (ret) {
644 - pr_err("%s: failed to request DMA irq\n", __func__);
645 + dev_err(&dev->dev,
646 + "Failed to request DMA irq\n");
647 goto free_dma_chan;
648 }
649
650 - pr_info("BCM2708FB: allocated DMA channel %d\n", fb->dma_chan);
651 + rpi_firmware_property(fbdev->fw,
652 + RPI_FIRMWARE_GET_VC_MEMORY,
653 + &gpu_mem, sizeof(gpu_mem));
654 +
655 + for (i = 0; i < num_displays; i++) {
656 + struct bcm2708_fb *fb = &fbdev->displays[i];
657 +
658 + fb->display_settings.display_num = i;
659 + fb->dev = dev;
660 + fb->fb.device = &dev->dev;
661 + fb->fbdev = fbdev;
662 +
663 + fb->gpu.base = gpu_mem.base;
664 + fb->gpu.length = gpu_mem.length;
665 +
666 + if (fbdev->firmware_supports_multifb) {
667 + ret = rpi_firmware_property(fw,
668 + RPI_FIRMWARE_FRAMEBUFFER_GET_DISPLAY_SETTINGS,
669 + &fb->display_settings,
670 + GET_DISPLAY_SETTINGS_PAYLOAD_SIZE);
671 + } else {
672 + memset(&fb->display_settings, 0,
673 + sizeof(fb->display_settings));
674 + }
675
676 - fb->dev = dev;
677 - fb->fb.device = &dev->dev;
678 + ret = bcm2708_fb_register(fb);
679
680 - /* failure here isn't fatal, but we'll fail in vc_mem_copy if
681 - * fb->gpu is not valid
682 - */
683 - rpi_firmware_property(fb->fw, RPI_FIRMWARE_GET_VC_MEMORY, &fb->gpu,
684 - sizeof(fb->gpu));
685 + if (ret == 0) {
686 + bcm2708_fb_debugfs_init(fb);
687
688 - ret = bcm2708_fb_register(fb);
689 - if (ret == 0) {
690 - platform_set_drvdata(dev, fb);
691 - goto out;
692 + fbdev->num_displays++;
693 +
694 + dev_info(&dev->dev,
695 + "Registered framebuffer for display %u, size %ux%u\n",
696 + fb->display_settings.display_num,
697 + fb->fb.var.xres,
698 + fb->fb.var.yres);
699 + } else {
700 + // Use this to flag if this FB entry is in use.
701 + fb->fbdev = NULL;
702 + }
703 + }
704 +
705 + // Did we actually successfully create any FB's?
706 + if (fbdev->num_displays) {
707 + init_waitqueue_head(&fbdev->dma_waitq);
708 + platform_set_drvdata(dev, fbdev);
709 + return ret;
710 }
711
712 free_dma_chan:
713 - bcm_dma_chan_free(fb->dma_chan);
714 + bcm_dma_chan_free(fbdev->dma_chan);
715 free_cb:
716 - dma_free_wc(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
717 + dma_free_wc(&dev->dev, SZ_64K, fbdev->cb_base,
718 + fbdev->cb_handle);
719 free_fb:
720 - kfree(fb);
721 -free_region:
722 dev_err(&dev->dev, "probe failed, err %d\n", ret);
723 -out:
724 +
725 return ret;
726 }
727
728 static int bcm2708_fb_remove(struct platform_device *dev)
729 {
730 - struct bcm2708_fb *fb = platform_get_drvdata(dev);
731 + struct bcm2708_fb_dev *fbdev = platform_get_drvdata(dev);
732 + int i;
733
734 platform_set_drvdata(dev, NULL);
735
736 - if (fb->fb.screen_base)
737 - iounmap(fb->fb.screen_base);
738 - unregister_framebuffer(&fb->fb);
739 -
740 - dma_free_wc(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
741 - bcm_dma_chan_free(fb->dma_chan);
742 -
743 - bcm2708_fb_debugfs_deinit(fb);
744 + for (i = 0; i < fbdev->num_displays; i++) {
745 + if (fbdev->displays[i].fb.screen_base)
746 + iounmap(fbdev->displays[i].fb.screen_base);
747 +
748 + if (fbdev->displays[i].fbdev) {
749 + unregister_framebuffer(&fbdev->displays[i].fb);
750 + bcm2708_fb_debugfs_deinit(&fbdev->displays[i]);
751 + }
752 + }
753
754 - free_irq(fb->dma_irq, fb);
755 + dma_free_wc(&dev->dev, SZ_64K, fbdev->cb_base,
756 + fbdev->cb_handle);
757 + bcm_dma_chan_free(fbdev->dma_chan);
758 + free_irq(fbdev->dma_irq, fbdev);
759
760 - kfree(fb);
761 + mutex_destroy(&fbdev->dma_mutex);
762
763 return 0;
764 }
765 @@ -886,10 +1077,10 @@ static struct platform_driver bcm2708_fb
766 .probe = bcm2708_fb_probe,
767 .remove = bcm2708_fb_remove,
768 .driver = {
769 - .name = DRIVER_NAME,
770 - .owner = THIS_MODULE,
771 - .of_match_table = bcm2708_fb_of_match_table,
772 - },
773 + .name = DRIVER_NAME,
774 + .owner = THIS_MODULE,
775 + .of_match_table = bcm2708_fb_of_match_table,
776 + },
777 };
778
779 static int __init bcm2708_fb_init(void)
780 --- a/include/soc/bcm2835/raspberrypi-firmware.h
781 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
782 @@ -105,9 +105,15 @@ enum rpi_firmware_property_tag {
783 RPI_FIRMWARE_FRAMEBUFFER_GET_VIRTUAL_OFFSET = 0x00040009,
784 RPI_FIRMWARE_FRAMEBUFFER_GET_OVERSCAN = 0x0004000a,
785 RPI_FIRMWARE_FRAMEBUFFER_GET_PALETTE = 0x0004000b,
786 + RPI_FIRMWARE_FRAMEBUFFER_GET_LAYER = 0x0004000c,
787 + RPI_FIRMWARE_FRAMEBUFFER_GET_TRANSFORM = 0x0004000d,
788 + RPI_FIRMWARE_FRAMEBUFFER_GET_VSYNC = 0x0004000e,
789 RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF = 0x0004000f,
790 RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF = 0x00040010,
791 RPI_FIRMWARE_FRAMEBUFFER_RELEASE = 0x00048001,
792 + RPI_FIRMWARE_FRAMEBUFFER_SET_DISPLAY_NUM = 0x00048013,
793 + RPI_FIRMWARE_FRAMEBUFFER_GET_NUM_DISPLAYS = 0x00040013,
794 + RPI_FIRMWARE_FRAMEBUFFER_GET_DISPLAY_SETTINGS = 0x00040014,
795 RPI_FIRMWARE_FRAMEBUFFER_TEST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
796 RPI_FIRMWARE_FRAMEBUFFER_TEST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,
797 RPI_FIRMWARE_FRAMEBUFFER_TEST_DEPTH = 0x00044005,
798 @@ -116,6 +122,8 @@ enum rpi_firmware_property_tag {
799 RPI_FIRMWARE_FRAMEBUFFER_TEST_VIRTUAL_OFFSET = 0x00044009,
800 RPI_FIRMWARE_FRAMEBUFFER_TEST_OVERSCAN = 0x0004400a,
801 RPI_FIRMWARE_FRAMEBUFFER_TEST_PALETTE = 0x0004400b,
802 + RPI_FIRMWARE_FRAMEBUFFER_TEST_LAYER = 0x0004400c,
803 + RPI_FIRMWARE_FRAMEBUFFER_TEST_TRANSFORM = 0x0004400d,
804 RPI_FIRMWARE_FRAMEBUFFER_TEST_VSYNC = 0x0004400e,
805 RPI_FIRMWARE_FRAMEBUFFER_SET_PHYSICAL_WIDTH_HEIGHT = 0x00048003,
806 RPI_FIRMWARE_FRAMEBUFFER_SET_VIRTUAL_WIDTH_HEIGHT = 0x00048004,
807 @@ -126,9 +134,12 @@ enum rpi_firmware_property_tag {
808 RPI_FIRMWARE_FRAMEBUFFER_SET_VIRTUAL_OFFSET = 0x00048009,
809 RPI_FIRMWARE_FRAMEBUFFER_SET_OVERSCAN = 0x0004800a,
810 RPI_FIRMWARE_FRAMEBUFFER_SET_PALETTE = 0x0004800b,
811 +
812 RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF = 0x0004801f,
813 RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF = 0x00048020,
814 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC = 0x0004800e,
815 + RPI_FIRMWARE_FRAMEBUFFER_SET_LAYER = 0x0004800c,
816 + RPI_FIRMWARE_FRAMEBUFFER_SET_TRANSFORM = 0x0004800d,
817 RPI_FIRMWARE_FRAMEBUFFER_SET_BACKLIGHT = 0x0004800f,
818
819 RPI_FIRMWARE_VCHIQ_INIT = 0x00048010,
820 @@ -137,6 +148,8 @@ enum rpi_firmware_property_tag {
821 RPI_FIRMWARE_GET_DMA_CHANNELS = 0x00060001,
822 };
823
824 +#define GET_DISPLAY_SETTINGS_PAYLOAD_SIZE 64
825 +
826 #if IS_ENABLED(CONFIG_RASPBERRYPI_FIRMWARE)
827 int rpi_firmware_property(struct rpi_firmware *fw,
828 u32 tag, void *data, size_t len);