1 | /* |
---|
2 | * dev_ioc.c - IOC (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-MKH; 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 <thread.h> |
---|
30 | #include <printk.h> |
---|
31 | #include <chdev.h> |
---|
32 | #include <dev_ioc.h> |
---|
33 | |
---|
34 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
35 | // Extern global variables |
---|
36 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
37 | |
---|
38 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
39 | |
---|
40 | ////////////////////////////////// |
---|
41 | void dev_ioc_init( chdev_t * ioc ) |
---|
42 | { |
---|
43 | // the PIC chdev must be initialized before the IOC chdev, because |
---|
44 | // the IOC chdev initialisation requires the routing of an external IRQ |
---|
45 | xptr_t pic_xp = chdev_dir.pic; |
---|
46 | |
---|
47 | assert( (pic_xp != XPTR_NULL) , __FUNCTION__ , "PIC not initialised before IOC" ); |
---|
48 | |
---|
49 | // get implementation and channel from chdev descriptor |
---|
50 | uint32_t impl = ioc->impl; |
---|
51 | uint32_t channel = ioc->channel; |
---|
52 | |
---|
53 | // set chdev name |
---|
54 | snprintf( ioc->name , 16 , "ioc_%d" , channel ); |
---|
55 | |
---|
56 | // call driver init function |
---|
57 | if( impl == IMPL_IOC_BDV ) |
---|
58 | { |
---|
59 | soclib_bdv_init( ioc ); |
---|
60 | } |
---|
61 | else if( impl == IMPL_IOC_HBA ) |
---|
62 | { |
---|
63 | soclib_hba_init( ioc ); |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | assert( false , __FUNCTION__ , "undefined IOC device implementation" ); |
---|
68 | } |
---|
69 | |
---|
70 | // select a core to execute the IOC server thread |
---|
71 | lid_t lid = cluster_select_local_core(); |
---|
72 | |
---|
73 | // bind the IOC IRQ to the selected core |
---|
74 | dev_pic_bind_irq( lid , ioc ); |
---|
75 | |
---|
76 | // enable IRQ |
---|
77 | dev_pic_enable_irq( lid ,ioc ); |
---|
78 | |
---|
79 | // create server thread |
---|
80 | thread_t * new_thread; |
---|
81 | error_t error; |
---|
82 | |
---|
83 | error = thread_kernel_create( &new_thread, |
---|
84 | THREAD_DEV, |
---|
85 | &chdev_sequencial_server, |
---|
86 | ioc, |
---|
87 | lid ); |
---|
88 | |
---|
89 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); |
---|
90 | |
---|
91 | // set "server" field in ioc descriptor |
---|
92 | ioc->server = new_thread; |
---|
93 | |
---|
94 | // start server thread |
---|
95 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
96 | |
---|
97 | } // end dev_ioc_init() |
---|
98 | |
---|
99 | ////////////////////////////////////////////////////////////////////////////////// |
---|
100 | // This static function is called by dev_ioc_read() & dev_ioc_write() functions. |
---|
101 | // It builds and registers the command in the calling thread descriptor. |
---|
102 | // Then, it registers the calling thead in chdev waiting queue. |
---|
103 | // Finally it blocks on the THREAD_BLOCKED_DEV condition and deschedule. |
---|
104 | ////////////////////////////////////i///////////////////////////////////////////// |
---|
105 | static error_t dev_ioc_access( uint32_t cmd_type, |
---|
106 | uint8_t * buffer, |
---|
107 | uint32_t lba, |
---|
108 | uint32_t count ) |
---|
109 | { |
---|
110 | thread_t * this = CURRENT_THREAD; // pointer on client thread |
---|
111 | |
---|
112 | ioc_dmsg("\n[INFO] in %s : thread %x in process %x" |
---|
113 | " for lba = %x / buffer = %x / at cycle %d\n", |
---|
114 | __FUNCTION__ , this->trdid , this->process->pid , |
---|
115 | lba , (intptr_t)buffer , hal_get_cycles() ); |
---|
116 | |
---|
117 | // software L2/L3 cache coherence for memory buffer |
---|
118 | if( chdev_dir.iob ) |
---|
119 | { |
---|
120 | if ( cmd_type == IOC_READ ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 ); |
---|
121 | else dev_mmc_sync ( XPTR( local_cxy , buffer ) , count<<9 ); |
---|
122 | } |
---|
123 | |
---|
124 | // get extended pointer on IOC chdev descriptor |
---|
125 | xptr_t dev_xp = chdev_dir.ioc[0]; |
---|
126 | |
---|
127 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined IOC chdev descriptor" ); |
---|
128 | |
---|
129 | // register command in calling thread descriptor |
---|
130 | this->command.ioc.dev_xp = dev_xp; |
---|
131 | this->command.ioc.type = cmd_type; |
---|
132 | this->command.ioc.buf_xp = XPTR( local_cxy , buffer ); |
---|
133 | this->command.ioc.lba = lba; |
---|
134 | this->command.ioc.count = count; |
---|
135 | |
---|
136 | // register client thread in IOC chdev waiting queue, activate server thread, |
---|
137 | // block client thread on THREAD_BLOCKED_IO and deschedule. |
---|
138 | // it is re-activated by the ISR signaling IO operation completion. |
---|
139 | chdev_register_command( dev_xp , this ); |
---|
140 | |
---|
141 | ioc_dmsg("\n[INFO] in %s : thread %x in process %x" |
---|
142 | " completes / error = %d / at cycle %d\n", |
---|
143 | __FUNCTION__ , this->trdid , this->process->pid , |
---|
144 | this->command.ioc.error , hal_get_cycles() ); |
---|
145 | |
---|
146 | // return I/O operation status |
---|
147 | return this->command.ioc.error; |
---|
148 | |
---|
149 | } // end dev_ioc_access() |
---|
150 | |
---|
151 | //////////////////////////////////////////// |
---|
152 | error_t dev_ioc_read( uint8_t * buffer, |
---|
153 | uint32_t lba, |
---|
154 | uint32_t count ) |
---|
155 | { |
---|
156 | return dev_ioc_access( IOC_READ , buffer , lba , count ); |
---|
157 | } |
---|
158 | |
---|
159 | //////////////////////////////////////////// |
---|
160 | error_t dev_ioc_write( uint8_t * buffer, |
---|
161 | uint32_t lba, |
---|
162 | uint32_t count ) |
---|
163 | { |
---|
164 | return dev_ioc_access( IOC_WRITE , buffer , lba , count ); |
---|
165 | } |
---|
166 | |
---|
167 | ///////////////////////////////////////////// |
---|
168 | error_t dev_ioc_sync_read( uint8_t * buffer, |
---|
169 | uint32_t lba, |
---|
170 | uint32_t count ) |
---|
171 | { |
---|
172 | // get pointer on calling thread |
---|
173 | thread_t * this = CURRENT_THREAD; |
---|
174 | |
---|
175 | // software L2/L3 cache coherence for memory buffer |
---|
176 | if( chdev_dir.iob ) dev_mmc_inval( XPTR( local_cxy , buffer ) , count<<9 ); |
---|
177 | |
---|
178 | // get extended pointer on IOC[0] chdev |
---|
179 | xptr_t dev_xp = chdev_dir.ioc[0]; |
---|
180 | |
---|
181 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined IOC chdev descriptor" ); |
---|
182 | |
---|
183 | // register command in calling thread descriptor |
---|
184 | this->command.ioc.dev_xp = dev_xp; |
---|
185 | this->command.ioc.type = IOC_SYNC_READ; |
---|
186 | this->command.ioc.buf_xp = XPTR( local_cxy , buffer ); |
---|
187 | this->command.ioc.lba = lba; |
---|
188 | this->command.ioc.count = count; |
---|
189 | |
---|
190 | // get driver command function |
---|
191 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
192 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
193 | dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); |
---|
194 | |
---|
195 | // mask the IRQ |
---|
196 | thread_t * server = (thread_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->server ) ); |
---|
197 | core_t * core = (core_t *)hal_remote_lpt( XPTR( dev_cxy , &server->core ) ); |
---|
198 | lid_t lid = (lid_t)hal_remote_lw( XPTR( dev_cxy , &core->lid ) ); |
---|
199 | dev_pic_disable_irq( lid , dev_xp ); |
---|
200 | |
---|
201 | // call directly driver command |
---|
202 | cmd( XPTR( local_cxy , this ) ); |
---|
203 | |
---|
204 | // unmask the IRQ |
---|
205 | dev_pic_enable_irq( lid , dev_xp ); |
---|
206 | |
---|
207 | // return I/O operation status from calling thread descriptor |
---|
208 | return this->command.ioc.error; |
---|
209 | } |
---|
210 | |
---|