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

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

cosmetic

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 <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    if( error )
140    {
141        printk("\n[ERROR] in %s : cannot translate vaddr = %p in process %x\n",
142               __FUNCTION__ , buffer , this->process->pid ); 
143        return EINVAL;
144    }
145
146    fbf_dmsg("\n[DBG] %s : thread %x in process %x / vaddr = %p / paddr = %l\n", 
147             __FUNCTION__ , this->trdid , this->process->pid , buffer , buf_paddr );
148
149    // get extended pointer on FBF chdev descriptor
150    xptr_t  fbf_xp = chdev_dir.fbf[0];
151
152    assert( (fbf_xp != XPTR_NULL) , __FUNCTION__ , "undefined FBF chdev descriptor" );
153
154    // get FBF chdev cluster and local pointer
155    cxy_t     fbf_cxy = GET_CXY( fbf_xp );
156    chdev_t * fbf_ptr = (chdev_t *)GET_PTR( fbf_xp );
157
158    // get frame buffer base address, width and height
159    xptr_t   base   = hal_remote_lwd( XPTR( fbf_cxy , &fbf_ptr->base ) );
160    uint32_t width  = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.width ) );
161    uint32_t height = hal_remote_lw ( XPTR( fbf_cxy , &fbf_ptr->ext.fbf.height ) );
162
163    // check offset and length versus FBF size
164    if( (offset + length) > (width * height) )
165    {
166        printk("\n[ERROR] in %s : offset = %d / length = %d / width = %d / height = %d\n",
167               __FUNCTION__ , offset , length , width , height ); 
168        return EINVAL;
169    }
170
171    // compute extended pointers on frame buffer and memory buffer
172    xptr_t  mem_buf_xp = XPTR( local_cxy , (void *)(intptr_t)buf_paddr );
173    xptr_t  fbf_buf_xp = base + offset;
174
175    // register command in DMA chdev
176    if( to_fbf )  dev_dma_remote_memcpy( fbf_buf_xp , mem_buf_xp , length );
177    else          dev_dma_remote_memcpy( mem_buf_xp , fbf_buf_xp , length );
178
179    return 0;
180
181}  // end dev_fbf_access()
182
183////////////////////////////////////////////
184error_t dev_fbf_read( char         * buffer,
185                      uint32_t       length,
186                      uint32_t       offset )
187{
188    return dev_fbf_access( false , buffer , length , offset ); 
189} 
190
191////////////////////////////////////////////
192error_t dev_fbf_write( char         * buffer,
193                       uint32_t       length,
194                       uint32_t       offset )
195{
196    return dev_fbf_access( true , buffer , length , offset ); 
197}
Note: See TracBrowser for help on using the repository browser.