3232#include <linux/printk.h>
3333#include <linux/console.h>
3434#include <linux/debugfs.h>
35+ #include <linux/uaccess.h>
3536#include <linux/io.h>
3637#include <linux/dma-mapping.h>
38+ #include <linux/cred.h>
3739#include <soc/bcm2835/raspberrypi-firmware.h>
3840#include <linux/mutex.h>
3941
@@ -184,9 +186,6 @@ static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
184186
185187 fb -> debugfs_subdir = debugfs_create_dir (buf , fb -> debugfs_dir );
186188
187- debugfs_create_regset32 ("stats" , 0444 , fb -> debugfs_dir ,
188- & fb -> stats .regset );
189-
190189 if (!fb -> debugfs_subdir ) {
191190 dev_warn (fb -> fb .dev , "%s: could not create debugfs entry %u\n" ,
192191 __func__ , fb -> display_settings .display_num );
@@ -603,7 +602,110 @@ static int bcm2708_fb_pan_display(struct fb_var_screeninfo *var,
603602 return result ;
604603}
605604
606- static int bcm2708_ioctl (struct fb_info * info , unsigned int cmd , unsigned long arg )
605+ static void dma_memcpy (struct bcm2708_fb * fb , dma_addr_t dst , dma_addr_t src ,
606+ int size )
607+ {
608+ struct bcm2708_fb_dev * fbdev = fb -> fbdev ;
609+ struct bcm2708_dma_cb * cb = fbdev -> cb_base ;
610+ int burst_size = (fbdev -> dma_chan == 0 ) ? 8 : 2 ;
611+
612+ cb -> info = BCM2708_DMA_BURST (burst_size ) | BCM2708_DMA_S_WIDTH |
613+ BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
614+ BCM2708_DMA_D_INC ;
615+ cb -> dst = dst ;
616+ cb -> src = src ;
617+ cb -> length = size ;
618+ cb -> stride = 0 ;
619+ cb -> pad [0 ] = 0 ;
620+ cb -> pad [1 ] = 0 ;
621+ cb -> next = 0 ;
622+
623+ // Not sure what to do if this gets a signal whilst waiting
624+ if (mutex_lock_interruptible (& fbdev -> dma_mutex ))
625+ return ;
626+
627+ if (size < dma_busy_wait_threshold ) {
628+ bcm_dma_start (fbdev -> dma_chan_base , fbdev -> cb_handle );
629+ bcm_dma_wait_idle (fbdev -> dma_chan_base );
630+ } else {
631+ void __iomem * local_dma_chan = fbdev -> dma_chan_base ;
632+
633+ cb -> info |= BCM2708_DMA_INT_EN ;
634+ bcm_dma_start (fbdev -> dma_chan_base , fbdev -> cb_handle );
635+ while (bcm_dma_is_busy (local_dma_chan )) {
636+ wait_event_interruptible (fbdev -> dma_waitq ,
637+ !bcm_dma_is_busy (local_dma_chan ));
638+ }
639+ fbdev -> dma_stats .dma_irqs ++ ;
640+ }
641+ fbdev -> dma_stats .dma_copies ++ ;
642+
643+ mutex_unlock (& fbdev -> dma_mutex );
644+ }
645+
646+ /* address with no aliases */
647+ #define INTALIAS_NORMAL (x ) ((x) & ~0xc0000000)
648+ /* cache coherent but non-allocating in L1 and L2 */
649+ #define INTALIAS_L1L2_NONALLOCATING (x ) (((x) & ~0xc0000000) | 0x80000000)
650+
651+ static long vc_mem_copy (struct bcm2708_fb * fb , struct fb_dmacopy * ioparam )
652+ {
653+ size_t size = PAGE_SIZE ;
654+ u32 * buf = NULL ;
655+ dma_addr_t bus_addr ;
656+ long rc = 0 ;
657+ size_t offset ;
658+
659+ /* restrict this to root user */
660+ if (!uid_eq (current_euid (), GLOBAL_ROOT_UID )) {
661+ rc = - EFAULT ;
662+ goto out ;
663+ }
664+
665+ if (!fb -> gpu .base || !fb -> gpu .length ) {
666+ pr_err ("[%s]: Unable to determine gpu memory (%x,%x)\n" ,
667+ __func__ , fb -> gpu .base , fb -> gpu .length );
668+ return - EFAULT ;
669+ }
670+
671+ if (INTALIAS_NORMAL (ioparam -> src ) < fb -> gpu .base ||
672+ INTALIAS_NORMAL (ioparam -> src ) >= fb -> gpu .base + fb -> gpu .length ) {
673+ pr_err ("[%s]: Invalid memory access %x (%x-%x)" , __func__ ,
674+ INTALIAS_NORMAL (ioparam -> src ), fb -> gpu .base ,
675+ fb -> gpu .base + fb -> gpu .length );
676+ return - EFAULT ;
677+ }
678+
679+ buf = dma_alloc_coherent (fb -> fb .device , PAGE_ALIGN (size ), & bus_addr ,
680+ GFP_ATOMIC );
681+ if (!buf ) {
682+ pr_err ("[%s]: failed to dma_alloc_coherent(%zd)\n" , __func__ ,
683+ size );
684+ rc = - ENOMEM ;
685+ goto out ;
686+ }
687+
688+ for (offset = 0 ; offset < ioparam -> length ; offset += size ) {
689+ size_t remaining = ioparam -> length - offset ;
690+ size_t s = min (size , remaining );
691+ u8 * p = (u8 * )((uintptr_t )ioparam -> src + offset );
692+ u8 * q = (u8 * )ioparam -> dst + offset ;
693+
694+ dma_memcpy (fb , bus_addr ,
695+ INTALIAS_L1L2_NONALLOCATING ((dma_addr_t )p ), size );
696+ if (copy_to_user (q , buf , s ) != 0 ) {
697+ pr_err ("[%s]: failed to copy-to-user\n" , __func__ );
698+ rc = - EFAULT ;
699+ goto out ;
700+ }
701+ }
702+ out :
703+ if (buf )
704+ dma_free_coherent (fb -> fb .device , PAGE_ALIGN (size ), buf ,
705+ bus_addr );
706+ return rc ;
707+ }
708+
607709static int bcm2708_ioctl (struct fb_info * info , unsigned int cmd ,
608710 unsigned long arg )
609711{
@@ -619,6 +721,21 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
619721 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC ,
620722 & dummy , sizeof (dummy ));
621723 break ;
724+
725+ case FBIODMACOPY :
726+ {
727+ struct fb_dmacopy ioparam ;
728+ /* Get the parameter data.
729+ */
730+ if (copy_from_user
731+ (& ioparam , (void * )arg , sizeof (ioparam ))) {
732+ pr_err ("[%s]: failed to copy-from-user\n" , __func__ );
733+ ret = - EFAULT ;
734+ break ;
735+ }
736+ ret = vc_mem_copy (fb , & ioparam );
737+ break ;
738+ }
622739 default :
623740 dev_dbg (info -> device , "Unknown ioctl 0x%x\n" , cmd );
624741 return - ENOTTY ;
@@ -629,6 +746,48 @@ static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd,
629746
630747 return ret ;
631748}
749+
750+ #ifdef CONFIG_COMPAT
751+ struct fb_dmacopy32 {
752+ compat_uptr_t dst ;
753+ __u32 src ;
754+ __u32 length ;
755+ };
756+
757+ #define FBIODMACOPY32 _IOW('z', 0x22, struct fb_dmacopy32)
758+
759+ static int bcm2708_compat_ioctl (struct fb_info * info , unsigned int cmd ,
760+ unsigned long arg )
761+ {
762+ struct bcm2708_fb * fb = to_bcm2708 (info );
763+ int ret ;
764+
765+ switch (cmd ) {
766+ case FBIODMACOPY32 :
767+ {
768+ struct fb_dmacopy32 param32 ;
769+ struct fb_dmacopy param ;
770+ /* Get the parameter data.
771+ */
772+ if (copy_from_user (& param32 , (void * )arg , sizeof (param32 ))) {
773+ pr_err ("[%s]: failed to copy-from-user\n" , __func__ );
774+ ret = - EFAULT ;
775+ break ;
776+ }
777+ param .dst = compat_ptr (param32 .dst );
778+ param .src = param32 .src ;
779+ param .length = param32 .length ;
780+ ret = vc_mem_copy (fb , & param );
781+ break ;
782+ }
783+ default :
784+ ret = bcm2708_ioctl (info , cmd , arg );
785+ break ;
786+ }
787+ return ret ;
788+ }
789+ #endif
790+
632791static void bcm2708_fb_fillrect (struct fb_info * info ,
633792 const struct fb_fillrect * rect )
634793{
@@ -821,6 +980,9 @@ static struct fb_ops bcm2708_fb_ops = {
821980 .fb_imageblit = bcm2708_fb_imageblit ,
822981 .fb_pan_display = bcm2708_fb_pan_display ,
823982 .fb_ioctl = bcm2708_ioctl ,
983+ #ifdef CONFIG_COMPAT
984+ .fb_compat_ioctl = bcm2708_compat_ioctl ,
985+ #endif
824986};
825987
826988static int bcm2708_fb_register (struct bcm2708_fb * fb )
0 commit comments