source: trunk/kernel/devices/dev_fbf.c @ 32

Last change on this file since 32 was 23, checked in by alain, 7 years ago

Introduce syscalls.

File size: 7.0 KB
Line 
1/*
2 * dev_fbf.c - FBF (Block Device Controler) generic device API implementation.
3 *
4 * Author  Alain Greiner    (2016)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MK
9 *
10 * ALMOS-MKH.is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH.is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-kernel; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_types.h>
26#include <hal_gpt.h>
27#include <soclib_bdv.h>
28#include <soclib_hba.h>
29//#include <soclib_sdc.h>
30//#include <soclib_spi.h>
31//#include <soclib_rdk.h>
32#include <thread.h>
33#include <printk.h>
34#include <chdev.h>
35#include <dev_fbf.h>
36
37/////////////////////////////////////////////////////////////////////////////////////////
38// Extern global variables
39/////////////////////////////////////////////////////////////////////////////////////////
40
41extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
42
43extern chdev_pic_input_t  chdev_input_irq;   // allocated in kernel_init.c
44
45////////////////////////////////////
46void dev_fbf_init( chdev_t  * chdev,
47                   uint32_t   width,
48                   uint32_t   height )
49{
50    // set FBF chdev extension fields
51    // TODO for the "width" and "height", fields, this should be done in the impementation
52    // TODO specific part, as these parameters must be obtained from the hardware.
53    chdev->ext.fbf.width  = width;
54    chdev->ext.fbf.height = height;
55
56    // get implementation
57    uint32_t  impl = chdev->impl;
58
59    // set chdev name
60    strcpy( chdev->name, "fbf" );
61    // call driver init function
62    if( impl == IMPL_FBF_SCL )
63    {
64        // TODO
65    }
66    else
67    {
68        assert( false , __FUNCTION__ , "undefined FBF device implementation" );
69    }
70
71}  // end dev_fbf_init()
72
73/////////////////////////////////////////
74void dev_fbf_get_size( uint32_t  * width,
75                       uint32_t  * height )
76{
77    // get extended pointer on FBF chdev descriptor
78    xptr_t  dev_xp = chdev_dir.fbf[0];
79
80    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
81
82    // get FBF chdev cluster and local pointer
83    cxy_t     dev_cxy = GET_CXY( dev_xp );
84    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
85
86    // return values
87    *width  = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->ext.fbf.width ) );
88    *height = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->ext.fbf.height ) );
89
90}  // end dev_fbf_get_size()
91
92///////////////////////
93error_t dev_fbf_alloc()
94{
95    // get extended pointer on FBF chdev descriptor
96    xptr_t  dev_xp = chdev_dir.fbf[0];
97
98    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
99
100    // get FBF chdev cluster and local pointer
101    cxy_t     dev_cxy = GET_CXY( dev_xp );
102    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
103
104    // try to get FBF ownership
105    return remote_spinlock_trylock( XPTR( dev_cxy , &dev_ptr->wait_lock ) );
106
107}  // end dev_fbf_alloc()
108
109///////////////////
110void dev_fbf_free()
111{
112    // get extended pointer on FBF chdev descriptor
113    xptr_t  dev_xp = chdev_dir.fbf[0];
114
115    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
116
117    // get FBF chdev cluster and local pointer
118    cxy_t     dev_cxy = GET_CXY( dev_xp );
119    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
120
121    // release FBF ownership
122    remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) );
123
124}  // end dev_fbf_free()
125
126//////////////////////////////////////////////////////////////////////////////////
127// This static function is called by dev_fbf_read() & dev_fbf_write() functions.
128// It builds and registers the command in the calling thread descriptor, after
129// translation of buffer virtual address to physical address.
130// Then, it registers the calling thead in chdev waiting queue.
131// Finally it blocks on the THREAD_BLOCKED_DEV condition and deschedule.
132////////////////////////////////////i/////////////////////////////////////////////
133static error_t dev_fbf_access( bool_t    to_fbf,
134                               char    * buffer,
135                               uint32_t  length,
136                               uint32_t  offset )
137{
138    error_t     error;
139    paddr_t     buf_paddr;
140
141    thread_t * this = CURRENT_THREAD;              // pointer on client thread
142
143    // Get buffer physical address
144    error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buffer , &buf_paddr );
145 
146    if( error )
147    {
148        printk("\n[ERROR] in %s : cannot translate vaddr = %x in process %x\n",
149               __FUNCTION__ , (uint32_t)buffer , this->process->pid ); 
150        return EINVAL;
151    }
152
153    fbf_dmsg("\n[INFO] %s : thread %x in process %x / vaddr = %x / paddr = %l\n", 
154             __FUNCTION__ , this->trdid , this->process->pid , (uint32_t)buffer , buf_paddr );
155
156    // get extended pointer on FBF chdev descriptor
157    xptr_t  fbf_xp = chdev_dir.fbf[0];
158
159    assert( (fbf_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
160
161    // get FBF chdev cluster and local pointer
162    cxy_t     fbf_cxy = GET_CXY( fbf_xp );
163    chdev_t * fbf_ptr = (chdev_t *)GET_PTR( fbf_xp );
164
165    // get frame buffer base address, width and height
166    xptr_t   base   = hal_remote_lwd( XPTR( fbf_cxy , &fbf_ptr->base ) );
167    uint32_t width  = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.width ) );
168    uint32_t height = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.height ) );
169
170    // check offset and length versus FBF size
171    if( (offset + length) > (width * height) )
172    {
173        printk("\n[ERROR] in %s : offset = %d / length = %d / width = %d / height = %d\n",
174               __FUNCTION__ , offset , length , width , height ); 
175        return EINVAL;
176    }
177
178    // compute extended pointers on frame buffer and memory buffer
179    xptr_t  mem_buf_xp = XPTR( local_cxy , (void *)(intptr_t)buf_paddr );
180    xptr_t  fbf_buf_xp = base + offset;
181
182    // register command in DMA chdev
183    if( to_fbf )  dev_dma_remote_memcpy( fbf_buf_xp , mem_buf_xp , length );
184    else          dev_dma_remote_memcpy( mem_buf_xp , fbf_buf_xp , length );
185
186    return 0;
187
188}  // end dev_fbf_access()
189
190////////////////////////////////////////////
191error_t dev_fbf_read( char         * buffer,
192                      uint32_t       length,
193                      uint32_t       offset )
194{
195    return dev_fbf_access( false , buffer , length , offset ); 
196} 
197
198////////////////////////////////////////////
199error_t dev_fbf_write( char         * buffer,
200                       uint32_t       length,
201                       uint32_t       offset )
202{
203    return dev_fbf_access( true , buffer , length , offset ); 
204}
Note: See TracBrowser for help on using the repository browser.