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

Last change on this file since 8 was 3, checked in by alain, 8 years ago

Introduce dev_fbf, dev dma, dev_iob

File size: 6.9 KB
RevLine 
[3]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 <almos_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    // call driver init function
60    if( impl == IMPL_FBF_SCL )
61    {
62        // TODO
63    }
64    else
65    {
66        assert( false , __FUNCTION__ , "undefined FBF device implementation" );
67    }
68
69}  // end dev_fbf_init()
70
71/////////////////////////////////////////
72void dev_fbf_get_size( uint32_t  * width,
73                       uint32_t  * height )
74{
75    // get extended pointer on FBF chdev descriptor
76    xptr_t  dev_xp = chdev_dir.fbf[0];
77
78    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
79
80    // get FBF chdev cluster and local pointer
81    cxy_t     dev_cxy = GET_CXY( dev_xp );
82    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
83
84    // return values
85    *width  = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->ext.fbf.width ) );
86    *height = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->ext.fbf.height ) );
87
88}  // end dev_fbf_get_size()
89
90///////////////////////
91error_t dev_fbf_alloc()
92{
93    // get extended pointer on FBF chdev descriptor
94    xptr_t  dev_xp = chdev_dir.fbf[0];
95
96    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
97
98    // get FBF chdev cluster and local pointer
99    cxy_t     dev_cxy = GET_CXY( dev_xp );
100    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
101
102    // try to get FBF ownership
103    return remote_spinlock_trylock( XPTR( dev_cxy , &dev_ptr->wait_lock ) );
104
105}  // end dev_fbf_alloc()
106
107///////////////////
108void dev_fbf_free()
109{
110    // get extended pointer on FBF chdev descriptor
111    xptr_t  dev_xp = chdev_dir.fbf[0];
112
113    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
114
115    // get FBF chdev cluster and local pointer
116    cxy_t     dev_cxy = GET_CXY( dev_xp );
117    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
118
119    // release FBF ownership
120    remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) );
121
122}  // end dev_fbf_free()
123
124//////////////////////////////////////////////////////////////////////////////////
125// This static function is called by dev_fbf_read() & dev_fbf_write() functions.
126// It builds and registers the command in the calling thread descriptor, after
127// translation of buffer virtual address to physical address.
128// Then, it registers the calling thead in chdev waiting queue.
129// Finally it blocks on the THREAD_BLOCKED_DEV condition and deschedule.
130////////////////////////////////////i/////////////////////////////////////////////
131static error_t dev_fbf_access( bool_t    to_fbf,
132                               char    * buffer,
133                               uint32_t  length,
134                               uint32_t  offset )
135{
136    error_t     error;
137    paddr_t     buf_paddr;
138    bool_t      ident = CONFIG_KERNEL_IDENTITY;
139
140    thread_t * this = CURRENT_THREAD;              // pointer on client thread
141
142    // Get buffer physical address
143    error = vmm_v2p_translate( ident , buffer , &buf_paddr );
144 
145    if( error )
146    {
147        printk("\n[ERROR] in %s : cannot translate vaddr = %x in process %x\n",
148               __FUNCTION__ , (uint32_t)buffer , this->process->pid ); 
149        return EINVAL;
150    }
151
152    fbf_dmsg("\n[INFO] %s : thread %x in process %x / vaddr = %x / paddr = %l\n", 
153             __FUNCTION__ , this->trdid , this->process->pid , (uint32_t)buffer , buf_paddr );
154
155    // get extended pointer on FBF chdev descriptor
156    xptr_t  fbf_xp = chdev_dir.fbf[0];
157
158    assert( (fbf_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
159
160    // get FBF chdev cluster and local pointer
161    cxy_t     fbf_cxy = GET_CXY( fbf_xp );
162    chdev_t * fbf_ptr = (chdev_t *)GET_PTR( fbf_xp );
163
164    // get frame buffer base address, width and height
165    xptr_t   base   = hal_remote_lwd( XPTR( fbf_cxy , &fbf_ptr->base ) );
166    uint32_t width  = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.width ) );
167    uint32_t height = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.height ) );
168
169    // check offset and length versus FBF size
170    if( (offset + length) > (width * height) )
171    {
172        printk("\n[ERROR] in %s : offset = %d / length = %d / width = %d / height = %d\n",
173               __FUNCTION__ , offset , length , width , height ); 
174        return EINVAL;
175    }
176
177    // compute extended pointers on frame buffer and memory buffer
178    xptr_t  mem_buf_xp = XPTR( local_cxy , (void *)(intptr_t)buf_paddr );
179    xptr_t  fbf_buf_xp = base + offset;
180
181    // register command in DMA chdev
182    if( to_fbf )  dev_dma_remote_memcpy( fbf_buf_xp , mem_buf_xp , length );
183    else          dev_dma_remote_memcpy( mem_buf_xp , fbf_buf_xp , length );
184
185    return 0;
186
187}  // end dev_fbf_access()
188
189////////////////////////////////////////////
190error_t dev_fbf_read( char         * buffer,
191                      uint32_t       length,
192                      uint32_t       offset )
193{
194    return dev_fbf_access( false , buffer , length , offset ); 
195} 
196
197////////////////////////////////////////////
198error_t dev_fbf_write( char         * buffer,
199                       uint32_t       length,
200                       uint32_t       offset )
201{
202    return dev_fbf_access( true , buffer , length , offset ); 
203}
Note: See TracBrowser for help on using the repository browser.