9191#define USE_DEV_GROUPS 0
9292#endif
9393
94+ #if ((LINUX_VERSION_CODE >= 0x040100 ) && defined(CONFIG_OF ))
95+ #define USE_OF_RESERVED_MEM 1
96+ #else
97+ #define USE_OF_RESERVED_MEM 0
98+ #endif
99+
100+ #if ((LINUX_VERSION_CODE >= 0x040100 ) && defined(CONFIG_OF ))
101+ #define USE_OF_DMA_CONFIG 1
102+ #else
103+ #define USE_OF_DMA_CONFIG 0
104+ #endif
105+
94106#if (UDMABUF_DEBUG == 1 )
95107#define UDMABUF_DEBUG_CHECK (this ,debug ) (this->debug)
96108#else
97109#define UDMABUF_DEBUG_CHECK (this ,debug ) (0)
98110#endif
99111
112+ #if (USE_OF_RESERVED_MEM == 1 )
113+ #include <linux/of_reserved_mem.h>
114+ #endif
115+
100116static struct class * udmabuf_sys_class = NULL ;
101117static dev_t udmabuf_device_number = 0 ;
102118static int dma_mask_bit = 32 ;
@@ -116,7 +132,6 @@ struct udmabuf_driver_data {
116132 size_t alloc_size ;
117133 void * virt_addr ;
118134 dma_addr_t phys_addr ;
119- u64 dma_mask ;
120135#if (SYNC_ENABLE == 1 )
121136 int sync_mode ;
122137 int sync_offset ;
@@ -126,6 +141,9 @@ struct udmabuf_driver_data {
126141 int sync_for_cpu ;
127142 int sync_for_device ;
128143#endif
144+ #if (USE_OF_RESERVED_MEM == 1 )
145+ bool of_reserved_mem ;
146+ #endif
129147#if ((UDMABUF_DEBUG == 1 ) && (USE_VMA_FAULT == 1 ))
130148 bool debug_vma ;
131149#endif
@@ -645,6 +663,7 @@ static struct udmabuf_driver_data* udmabuf_driver_create(const char* name, struc
645663 const unsigned int DONE_CHRDEV_ADD = (1 << 1 );
646664 const unsigned int DONE_ALLOC_CMA = (1 << 2 );
647665 const unsigned int DONE_DEVICE_CREATE = (1 << 3 );
666+ const unsigned int DONE_RESERVED_MEM = (1 << 4 );
648667 /*
649668 * alloc device_minor_number
650669 */
@@ -690,6 +709,11 @@ static struct udmabuf_driver_data* udmabuf_driver_create(const char* name, struc
690709 this -> sync_for_device = 0 ;
691710 }
692711#endif
712+ #if (USE_OF_RESERVED_MEM == 1 )
713+ {
714+ this -> of_reserved_mem = 0 ;
715+ }
716+ #endif
693717#if ((UDMABUF_DEBUG == 1 ) && (USE_VMA_FAULT == 1 ))
694718 {
695719 this -> debug_vma = 0 ;
@@ -723,23 +747,39 @@ static struct udmabuf_driver_data* udmabuf_driver_create(const char* name, struc
723747 */
724748 if (parent != NULL ) {
725749 this -> dma_dev = parent ;
750+ #if (USE_OF_RESERVED_MEM == 1 )
751+ {
752+ int retval = of_reserved_mem_device_init (parent );
753+ if (retval == 0 ) {
754+ this -> of_reserved_mem = 1 ;
755+ done |= DONE_RESERVED_MEM ;
756+ } else if (retval != - ENODEV ) {
757+ printk (KERN_ERR "of_reserved_mem_device_init() failed\n" );
758+ goto failed ;
759+ }
760+ }
761+ #endif
726762 } else {
727763 this -> dma_dev = this -> sys_dev ;
728- #if (( LINUX_VERSION_CODE >= 0x040100 ) && defined( CONFIG_OF ) )
764+ #if (USE_OF_DMA_CONFIG == 1 )
729765 of_dma_configure (this -> dma_dev , NULL );
730- #else
731- if (this -> dma_dev -> dma_mask == NULL ) {
732- this -> dma_dev -> dma_mask = & this -> dma_mask ;
733- }
734- if (dma_set_mask (this -> dma_dev , DMA_BIT_MASK (dma_mask_bit )) == 0 ) {
735- dma_set_coherent_mask (this -> dma_dev , DMA_BIT_MASK (dma_mask_bit ));
736- } else {
737- printk (KERN_WARNING "dma_set_mask(DMA_BIT_MASK(%d)) failed\n" , dma_mask_bit );
738- dma_set_mask (this -> dma_dev , DMA_BIT_MASK (32 ));
739- dma_set_coherent_mask (this -> dma_dev , DMA_BIT_MASK (32 ));
740- }
741766#endif
742767 }
768+
769+ /*
770+ * setup dma_mask and coherent_dma_mask
771+ */
772+ if (this -> dma_dev -> dma_mask == NULL ) {
773+ this -> dma_dev -> dma_mask = & this -> dma_dev -> coherent_dma_mask ;
774+ }
775+ if (dma_set_mask (this -> dma_dev , DMA_BIT_MASK (dma_mask_bit )) == 0 ) {
776+ dma_set_coherent_mask (this -> dma_dev , DMA_BIT_MASK (dma_mask_bit ));
777+ } else {
778+ printk (KERN_WARNING "dma_set_mask(DMA_BIT_MASK(%d)) failed\n" , dma_mask_bit );
779+ dma_set_mask (this -> dma_dev , DMA_BIT_MASK (32 ));
780+ dma_set_coherent_mask (this -> dma_dev , DMA_BIT_MASK (32 ));
781+ }
782+
743783 /*
744784 * dma buffer allocation
745785 */
@@ -787,6 +827,9 @@ static struct udmabuf_driver_data* udmabuf_driver_create(const char* name, struc
787827 failed :
788828 if (done & DONE_CHRDEV_ADD ) { cdev_del (& this -> cdev ); }
789829 if (done & DONE_ALLOC_CMA ) { dma_free_coherent (this -> dma_dev , this -> alloc_size , this -> virt_addr , this -> phys_addr );}
830+ #if (USE_OF_RESERVED_MEM == 1 )
831+ if (done & DONE_RESERVED_MEM ) { of_reserved_mem_device_release (parent ); }
832+ #endif
790833 if (done & DONE_DEVICE_CREATE ) { device_destroy (udmabuf_sys_class , this -> device_number );}
791834 if (done & DONE_ALLOC_MINOR ) { udmabuf_device_minor_number_free (minor );}
792835 if (this != NULL ) { kfree (this ); }
@@ -811,6 +854,11 @@ static int udmabuf_driver_destroy(struct udmabuf_driver_data* this)
811854 dev_info (this -> sys_dev , "driver uninstalled\n" );
812855 }
813856 dma_free_coherent (this -> dma_dev , this -> alloc_size , this -> virt_addr , this -> phys_addr );
857+ #if (USE_OF_RESERVED_MEM == 1 )
858+ if (this -> of_reserved_mem ) {
859+ of_reserved_mem_device_release (this -> dma_dev );
860+ }
861+ #endif
814862 device_destroy (udmabuf_sys_class , this -> device_number );
815863 cdev_del (& this -> cdev );
816864 kfree (this );
0 commit comments