3131#include <linux/console.h>
3232#include <linux/debugfs.h>
3333#include <asm/sizes.h>
34+ #include <linux/uaccess.h>
3435#include <linux/io.h>
3536#include <linux/dma-mapping.h>
37+ #include <linux/cred.h>
3638#include <soc/bcm2835/raspberrypi-firmware.h>
3739
3840//#define BCM2708_FB_DEBUG
@@ -99,6 +101,7 @@ struct bcm2708_fb {
99101 unsigned int image_size ;
100102 dma_addr_t dma_addr ;
101103 void * cpuaddr ;
104+ struct { u32 base , length ; } gpu ;
102105};
103106
104107#define to_bcm2708 (info ) container_of(info, struct bcm2708_fb, fb)
@@ -510,7 +513,105 @@ static int bcm2708_fb_pan_display(struct fb_var_screeninfo *var,
510513 return result ;
511514}
512515
513- static int bcm2708_ioctl (struct fb_info * info , unsigned int cmd , unsigned long arg )
516+ static void dma_memcpy (struct bcm2708_fb * fb , dma_addr_t dst , dma_addr_t src ,
517+ int size )
518+ {
519+ int burst_size = (fb -> dma_chan == 0 ) ? 8 : 2 ;
520+ struct bcm2708_dma_cb * cb = fb -> cb_base ;
521+
522+ cb -> info = BCM2708_DMA_BURST (burst_size ) | BCM2708_DMA_S_WIDTH |
523+ BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
524+ BCM2708_DMA_D_INC ;
525+ cb -> dst = dst ;
526+ cb -> src = src ;
527+ cb -> length = size ;
528+ cb -> stride = 0 ;
529+ cb -> pad [0 ] = 0 ;
530+ cb -> pad [1 ] = 0 ;
531+ cb -> next = 0 ;
532+
533+ if (size < dma_busy_wait_threshold ) {
534+ bcm_dma_start (fb -> dma_chan_base , fb -> cb_handle );
535+ bcm_dma_wait_idle (fb -> dma_chan_base );
536+ } else {
537+ void __iomem * dma_chan = fb -> dma_chan_base ;
538+
539+ cb -> info |= BCM2708_DMA_INT_EN ;
540+ bcm_dma_start (fb -> dma_chan_base , fb -> cb_handle );
541+ while (bcm_dma_is_busy (dma_chan )) {
542+ wait_event_interruptible (fb -> dma_waitq ,
543+ !bcm_dma_is_busy (dma_chan ));
544+ }
545+ fb -> stats .dma_irqs ++ ;
546+ }
547+ fb -> stats .dma_copies ++ ;
548+ }
549+
550+ /* address with no aliases */
551+ #define INTALIAS_NORMAL (x ) ((x) & ~0xc0000000)
552+ /* cache coherent but non-allocating in L1 and L2 */
553+ #define INTALIAS_L1L2_NONALLOCATING (x ) (((x) & ~0xc0000000) | 0x80000000)
554+
555+ static long vc_mem_copy (struct bcm2708_fb * fb , struct fb_dmacopy * ioparam )
556+ {
557+ size_t size = PAGE_SIZE ;
558+ u32 * buf = NULL ;
559+ dma_addr_t bus_addr ;
560+ long rc = 0 ;
561+ size_t offset ;
562+
563+ /* restrict this to root user */
564+ if (!uid_eq (current_euid (), GLOBAL_ROOT_UID )) {
565+ rc = - EFAULT ;
566+ goto out ;
567+ }
568+
569+ if (!fb -> gpu .base || !fb -> gpu .length ) {
570+ pr_err ("[%s]: Unable to determine gpu memory (%x,%x)\n" ,
571+ __func__ , fb -> gpu .base , fb -> gpu .length );
572+ return - EFAULT ;
573+ }
574+
575+ if (INTALIAS_NORMAL (ioparam -> src ) < fb -> gpu .base ||
576+ INTALIAS_NORMAL (ioparam -> src ) >= fb -> gpu .base + fb -> gpu .length ) {
577+ pr_err ("[%s]: Invalid memory access %x (%x-%x)" , __func__ ,
578+ INTALIAS_NORMAL (ioparam -> src ), fb -> gpu .base ,
579+ fb -> gpu .base + fb -> gpu .length );
580+ return - EFAULT ;
581+ }
582+
583+ buf = dma_alloc_coherent (fb -> fb .device , PAGE_ALIGN (size ), & bus_addr ,
584+ GFP_ATOMIC );
585+ if (!buf ) {
586+ pr_err ("[%s]: failed to dma_alloc_coherent(%zd)\n" , __func__ ,
587+ size );
588+ rc = - ENOMEM ;
589+ goto out ;
590+ }
591+
592+ for (offset = 0 ; offset < ioparam -> length ; offset += size ) {
593+ size_t remaining = ioparam -> length - offset ;
594+ size_t s = min (size , remaining );
595+ u8 * p = (u8 * )((uintptr_t )ioparam -> src + offset );
596+ u8 * q = (u8 * )ioparam -> dst + offset ;
597+
598+ dma_memcpy (fb , bus_addr ,
599+ INTALIAS_L1L2_NONALLOCATING ((dma_addr_t )p ), size );
600+ if (copy_to_user (q , buf , s ) != 0 ) {
601+ pr_err ("[%s]: failed to copy-to-user\n" , __func__ );
602+ rc = - EFAULT ;
603+ goto out ;
604+ }
605+ }
606+ out :
607+ if (buf )
608+ dma_free_coherent (fb -> fb .device , PAGE_ALIGN (size ), buf ,
609+ bus_addr );
610+ return rc ;
611+ }
612+
613+ static int bcm2708_ioctl (struct fb_info * info , unsigned int cmd ,
614+ unsigned long arg )
514615{
515616 struct bcm2708_fb * fb = to_bcm2708 (info );
516617 u32 dummy = 0 ;
@@ -522,6 +623,20 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long a
522623 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC ,
523624 & dummy , sizeof (dummy ));
524625 break ;
626+ case FBIODMACOPY :
627+ {
628+ struct fb_dmacopy ioparam ;
629+ /* Get the parameter data.
630+ */
631+ if (copy_from_user
632+ (& ioparam , (void * )arg , sizeof (ioparam ))) {
633+ pr_err ("[%s]: failed to copy-from-user\n" , __func__ );
634+ ret = - EFAULT ;
635+ break ;
636+ }
637+ ret = vc_mem_copy (fb , & ioparam );
638+ break ;
639+ }
525640 default :
526641 dev_dbg (info -> device , "Unknown ioctl 0x%x\n" , cmd );
527642 return - ENOTTY ;
@@ -532,6 +647,48 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long a
532647
533648 return ret ;
534649}
650+
651+ #ifdef CONFIG_COMPAT
652+ struct fb_dmacopy32 {
653+ compat_uptr_t dst ;
654+ __u32 src ;
655+ __u32 length ;
656+ };
657+
658+ #define FBIODMACOPY32 _IOW('z', 0x22, struct fb_dmacopy32)
659+
660+ static int bcm2708_compat_ioctl (struct fb_info * info , unsigned int cmd ,
661+ unsigned long arg )
662+ {
663+ struct bcm2708_fb * fb = to_bcm2708 (info );
664+ int ret ;
665+
666+ switch (cmd ) {
667+ case FBIODMACOPY32 :
668+ {
669+ struct fb_dmacopy32 param32 ;
670+ struct fb_dmacopy param ;
671+ /* Get the parameter data.
672+ */
673+ if (copy_from_user (& param32 , (void * )arg , sizeof (param32 ))) {
674+ pr_err ("[%s]: failed to copy-from-user\n" , __func__ );
675+ ret = - EFAULT ;
676+ break ;
677+ }
678+ param .dst = compat_ptr (param32 .dst );
679+ param .src = param32 .src ;
680+ param .length = param32 .length ;
681+ ret = vc_mem_copy (fb , & param );
682+ break ;
683+ }
684+ default :
685+ ret = bcm2708_ioctl (info , cmd , arg );
686+ break ;
687+ }
688+ return ret ;
689+ }
690+ #endif
691+
535692static void bcm2708_fb_fillrect (struct fb_info * info ,
536693 const struct fb_fillrect * rect )
537694{
@@ -720,6 +877,9 @@ static struct fb_ops bcm2708_fb_ops = {
720877 .fb_imageblit = bcm2708_fb_imageblit ,
721878 .fb_pan_display = bcm2708_fb_pan_display ,
722879 .fb_ioctl = bcm2708_ioctl ,
880+ #ifdef CONFIG_COMPAT
881+ .fb_compat_ioctl = bcm2708_compat_ioctl ,
882+ #endif
723883};
724884
725885static int bcm2708_fb_register (struct bcm2708_fb * fb )
0 commit comments