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

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

Fix a bug in scheduler related to RPC blocking.

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