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

Last change on this file since 267 was 214, checked in by max@…, 7 years ago

remove unused includes

File size: 6.7 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 <thread.h>
28#include <printk.h>
29#include <chdev.h>
30#include <dev_fbf.h>
31
32/////////////////////////////////////////////////////////////////////////////////////////
33// Extern global variables
34/////////////////////////////////////////////////////////////////////////////////////////
35
36extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
37
38////////////////////////////////////
39void dev_fbf_init( chdev_t  * chdev )
40{
41    // set FBF chdev extension fields
42    // TODO this should be done in the impementation
43    // TODO specific part, as these parameters must be obtained from the hardware.
44    chdev->ext.fbf.width  = CONFIG_FBF_WIDTH;
45    chdev->ext.fbf.height = CONFIG_FBF_HEIGHT;
46
47    // get implementation
48    uint32_t  impl = chdev->impl;
49
50    // set chdev name
51    strcpy( chdev->name, "fbf" );
52
53    // call driver init function
54    if( impl == IMPL_FBF_SCL )
55    {
56        // TODO
57    }
58    else
59    {
60        assert( false , __FUNCTION__ , "undefined FBF device implementation" );
61    }
62
63}  // end dev_fbf_init()
64
65/////////////////////////////////////////
66void dev_fbf_get_size( uint32_t  * width,
67                       uint32_t  * height )
68{
69    // get extended pointer on FBF chdev descriptor
70    xptr_t  dev_xp = chdev_dir.fbf[0];
71
72    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
73
74    // get FBF chdev cluster and local pointer
75    cxy_t     dev_cxy = GET_CXY( dev_xp );
76    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
77
78    // return values
79    *width  = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->ext.fbf.width ) );
80    *height = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->ext.fbf.height ) );
81
82}  // end dev_fbf_get_size()
83
84///////////////////////
85error_t dev_fbf_alloc()
86{
87    // get extended pointer on FBF chdev descriptor
88    xptr_t  dev_xp = chdev_dir.fbf[0];
89
90    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
91
92    // get FBF chdev cluster and local pointer
93    cxy_t     dev_cxy = GET_CXY( dev_xp );
94    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
95
96    // try to get FBF ownership
97    return remote_spinlock_trylock( XPTR( dev_cxy , &dev_ptr->wait_lock ) );
98
99}  // end dev_fbf_alloc()
100
101///////////////////
102void dev_fbf_free()
103{
104    // get extended pointer on FBF chdev descriptor
105    xptr_t  dev_xp = chdev_dir.fbf[0];
106
107    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
108
109    // get FBF chdev cluster and local pointer
110    cxy_t     dev_cxy = GET_CXY( dev_xp );
111    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
112
113    // release FBF ownership
114    remote_spinlock_unlock( XPTR( dev_cxy , &dev_ptr->wait_lock ) );
115
116}  // end dev_fbf_free()
117
118//////////////////////////////////////////////////////////////////////////////////
119// This static function is called by dev_fbf_read() & dev_fbf_write() functions.
120// It builds and registers the command in the calling thread descriptor, after
121// translation of buffer virtual address to physical address.
122// Then, it registers the calling thead in chdev waiting queue.
123// Finally it blocks on the THREAD_BLOCKED_DEV condition and deschedule.
124////////////////////////////////////i/////////////////////////////////////////////
125static error_t dev_fbf_access( bool_t    to_fbf,
126                               char    * buffer,
127                               uint32_t  length,
128                               uint32_t  offset )
129{
130    error_t     error;
131    paddr_t     buf_paddr;
132
133    thread_t * this = CURRENT_THREAD;              // pointer on client thread
134
135    // Get buffer physical address
136    error = vmm_v2p_translate( CONFIG_KERNEL_IDENTITY_MAP , buffer , &buf_paddr );
137 
138    if( error )
139    {
140        printk("\n[ERROR] in %s : cannot translate vaddr = %p in process %x\n",
141               __FUNCTION__ , buffer , this->process->pid ); 
142        return EINVAL;
143    }
144
145    fbf_dmsg("\n[INFO] %s : thread %x in process %x / vaddr = %p / paddr = %l\n", 
146             __FUNCTION__ , this->trdid , this->process->pid , buffer , buf_paddr );
147
148    // get extended pointer on FBF chdev descriptor
149    xptr_t  fbf_xp = chdev_dir.fbf[0];
150
151    assert( (fbf_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
152
153    // get FBF chdev cluster and local pointer
154    cxy_t     fbf_cxy = GET_CXY( fbf_xp );
155    chdev_t * fbf_ptr = (chdev_t *)GET_PTR( fbf_xp );
156
157    // get frame buffer base address, width and height
158    xptr_t   base   = hal_remote_lwd( XPTR( fbf_cxy , &fbf_ptr->base ) );
159    uint32_t width  = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.width ) );
160    uint32_t height = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.height ) );
161
162    // check offset and length versus FBF size
163    if( (offset + length) > (width * height) )
164    {
165        printk("\n[ERROR] in %s : offset = %d / length = %d / width = %d / height = %d\n",
166               __FUNCTION__ , offset , length , width , height ); 
167        return EINVAL;
168    }
169
170    // compute extended pointers on frame buffer and memory buffer
171    xptr_t  mem_buf_xp = XPTR( local_cxy , (void *)(intptr_t)buf_paddr );
172    xptr_t  fbf_buf_xp = base + offset;
173
174    // register command in DMA chdev
175    if( to_fbf )  dev_dma_remote_memcpy( fbf_buf_xp , mem_buf_xp , length );
176    else          dev_dma_remote_memcpy( mem_buf_xp , fbf_buf_xp , length );
177
178    return 0;
179
180}  // end dev_fbf_access()
181
182////////////////////////////////////////////
183error_t dev_fbf_read( char         * buffer,
184                      uint32_t       length,
185                      uint32_t       offset )
186{
187    return dev_fbf_access( false , buffer , length , offset ); 
188} 
189
190////////////////////////////////////////////
191error_t dev_fbf_write( char         * buffer,
192                       uint32_t       length,
193                       uint32_t       offset )
194{
195    return dev_fbf_access( true , buffer , length , offset ); 
196}
Note: See TracBrowser for help on using the repository browser.