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