Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Fundation, Inc. nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <smd.h> |
| 31 | #include <smem.h> |
| 32 | #include <debug.h> |
Channagoud Kadabi | 54aafe4 | 2014-11-21 19:26:16 -0800 | [diff] [blame^] | 33 | #include <kernel/event.h> |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 34 | #include <platform/irqs.h> |
| 35 | #include <platform/iomap.h> |
| 36 | #include <platform/interrupts.h> |
| 37 | #include <reg.h> |
| 38 | #include <malloc.h> |
| 39 | #include <bits.h> |
| 40 | |
| 41 | smd_channel_alloc_entry_t *smd_channel_alloc_entry; |
Channagoud Kadabi | 54aafe4 | 2014-11-21 19:26:16 -0800 | [diff] [blame^] | 42 | static event_t smd_closed; |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 43 | |
| 44 | static void smd_write_state(smd_channel_info_t *ch, uint32_t state) |
| 45 | { |
| 46 | if(state == SMD_SS_OPENED) |
| 47 | { |
| 48 | ch->port_info->ch0.DTR_DSR = 1; |
| 49 | ch->port_info->ch0.CTS_RTS = 1; |
| 50 | ch->port_info->ch0.CD = 1; |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | ch->port_info->ch0.DTR_DSR = 0; |
| 55 | ch->port_info->ch0.CTS_RTS = 0; |
| 56 | ch->port_info->ch0.CD = 0; |
| 57 | } |
| 58 | |
| 59 | ch->port_info->ch0.stream_state = state; |
| 60 | } |
| 61 | |
| 62 | static void smd_state_update(smd_channel_info_t *ch, uint32_t flag) |
| 63 | { |
| 64 | ch->port_info->ch0.state_updated = flag; |
| 65 | } |
| 66 | |
| 67 | void smd_get_channel_entry(smd_channel_info_t *ch, uint32_t ch_type) |
| 68 | { |
| 69 | int i = 0; |
| 70 | |
| 71 | for(i = 0; i< SMEM_NUM_SMD_STREAM_CHANNELS; i++) |
| 72 | { |
| 73 | if((smd_channel_alloc_entry[i].ctype & 0xFF) == ch_type) |
| 74 | { |
| 75 | memcpy(&ch->alloc_entry, &smd_channel_alloc_entry[i], sizeof(smd_channel_alloc_entry_t)); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* Channel not found */ |
| 81 | if(i == SMEM_NUM_SMD_STREAM_CHANNELS) |
| 82 | { |
| 83 | dprintf(CRITICAL, "smd channel type %x not found\n", ch_type); |
| 84 | ASSERT(0); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | int smd_get_channel_info(smd_channel_info_t *ch, uint32_t ch_type) |
| 89 | { |
| 90 | int ret = 0; |
| 91 | uint8_t *fifo_buf = NULL; |
| 92 | uint32_t fifo_buf_size = 0; |
| 93 | uint32_t size = 0; |
| 94 | |
| 95 | smd_get_channel_entry(ch, ch_type); |
| 96 | |
| 97 | |
| 98 | ch->port_info = smem_get_alloc_entry(SMEM_SMD_BASE_ID + ch->alloc_entry.cid, |
| 99 | &size); |
| 100 | |
| 101 | fifo_buf = smem_get_alloc_entry(SMEM_SMD_FIFO_BASE_ID + ch->alloc_entry.cid, |
| 102 | &fifo_buf_size); |
| 103 | |
| 104 | fifo_buf_size /= 2; |
| 105 | ch->send_buf = fifo_buf; |
| 106 | ch->recv_buf = fifo_buf + fifo_buf_size; |
| 107 | ch->fifo_size = fifo_buf_size; |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | int smd_init(smd_channel_info_t *ch, uint32_t ch_type) |
| 113 | { |
| 114 | unsigned ret = 0; |
| 115 | |
| 116 | smd_channel_alloc_entry = (smd_channel_alloc_entry_t*)memalign(CACHE_LINE, SMD_CHANNEL_ALLOC_MAX); |
| 117 | ASSERT(smd_channel_alloc_entry); |
| 118 | |
| 119 | ret = smem_read_alloc_entry(SMEM_CHANNEL_ALLOC_TBL, |
| 120 | (void*)smd_channel_alloc_entry, |
| 121 | SMD_CHANNEL_ALLOC_MAX); |
| 122 | if(ret) |
| 123 | { |
| 124 | dprintf(CRITICAL,"ERROR reading smem channel alloc tbl\n"); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | smd_get_channel_info(ch, ch_type); |
| 129 | |
| 130 | register_int_handler(SMD_IRQ, smd_irq_handler, ch); |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 131 | |
| 132 | smd_set_state(ch, SMD_SS_OPENING, 1); |
| 133 | |
| 134 | smd_notify_rpm(); |
| 135 | |
Unnati Gandhi | a7d9a0b | 2014-11-13 16:03:35 +0530 | [diff] [blame] | 136 | unmask_interrupt(SMD_IRQ); |
| 137 | |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | void smd_uninit(smd_channel_info_t *ch) |
| 142 | { |
Channagoud Kadabi | 54aafe4 | 2014-11-21 19:26:16 -0800 | [diff] [blame^] | 143 | event_init(&smd_closed, false, EVENT_FLAG_AUTOUNSIGNAL); |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 144 | smd_set_state(ch, SMD_SS_CLOSING, 1); |
| 145 | |
| 146 | smd_notify_rpm(); |
Channagoud Kadabi | 54aafe4 | 2014-11-21 19:26:16 -0800 | [diff] [blame^] | 147 | /* Wait for the SMD-RPM channel to be closed */ |
| 148 | event_wait(&smd_closed); |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | bool is_channel_open(smd_channel_info_t *ch) |
| 152 | { |
| 153 | if(ch->port_info->ch0.stream_state == SMD_SS_OPENED && |
| 154 | (ch->port_info->ch1.stream_state == SMD_SS_OPENED || |
| 155 | ch->port_info->ch1.stream_state == SMD_SS_FLUSHING)) |
| 156 | return true; |
| 157 | else |
| 158 | return false; |
| 159 | } |
| 160 | |
Channagoud Kadabi | 4e3dc81 | 2014-10-14 12:32:56 -0700 | [diff] [blame] | 161 | /* Copy the local buffer to fifo buffer. |
| 162 | * Takes care of fifo overlap. |
| 163 | * Uses the fifo as circular buffer, if the request data |
| 164 | * exceeds the max size of the buffer start from the beginning. |
| 165 | */ |
| 166 | static void memcpy_to_fifo(smd_channel_info_t *ch_ptr, uint32_t *src, size_t len) |
| 167 | { |
| 168 | uint32_t write_index = ch_ptr->port_info->ch0.write_index; |
| 169 | uint32_t *dest = (uint32_t *)(ch_ptr->send_buf + write_index); |
| 170 | |
| 171 | while(len) |
| 172 | { |
| 173 | *dest++ = *src++; |
| 174 | write_index += 4; |
| 175 | len -= 4; |
| 176 | |
| 177 | if (write_index >= ch_ptr->fifo_size) |
| 178 | { |
| 179 | write_index = 0; |
| 180 | dest = (uint32_t *)(ch_ptr->send_buf + write_index); |
| 181 | } |
| 182 | } |
| 183 | ch_ptr->port_info->ch0.write_index = write_index; |
| 184 | } |
| 185 | |
| 186 | /* Copy the fifo buffer to a local destination. |
| 187 | * Takes care of fifo overlap. |
| 188 | * If the response data is split across with some part at |
| 189 | * end of fifo and some at the beginning of the fifo |
| 190 | */ |
| 191 | void memcpy_from_fifo(smd_channel_info_t *ch_ptr, uint32_t *dest, size_t len) |
| 192 | { |
| 193 | uint32_t read_index = ch_ptr->port_info->ch1.read_index; |
| 194 | uint32_t *src = (uint32_t *)(ch_ptr->recv_buf + read_index); |
| 195 | |
| 196 | while(len) |
| 197 | { |
| 198 | *dest++ = *src++; |
| 199 | read_index += 4; |
| 200 | len -= 4; |
| 201 | |
| 202 | if (read_index >= ch_ptr->fifo_size) |
| 203 | { |
| 204 | read_index = 0; |
| 205 | src = (uint32_t *) (ch_ptr->recv_buf + read_index); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | ch_ptr->port_info->ch1.read_index = read_index; |
| 210 | } |
| 211 | |
Sridhar Parasuram | 9f47420 | 2014-11-18 16:19:19 -0800 | [diff] [blame] | 212 | uint8_t* smd_read(smd_channel_info_t *ch, uint32_t *len, int ch_type, uint32_t *resp) |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 213 | { |
| 214 | smd_pkt_hdr smd_hdr; |
| 215 | uint32_t size = 0; |
Channagoud Kadabi | 4e3dc81 | 2014-10-14 12:32:56 -0700 | [diff] [blame] | 216 | /* Response as per the current design does not exceed 20 bytes */ |
Sridhar Parasuram | 9f47420 | 2014-11-18 16:19:19 -0800 | [diff] [blame] | 217 | uint32_t *response = resp; |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 218 | |
| 219 | /* Read the indices from smem */ |
| 220 | ch->port_info = smem_get_alloc_entry(SMEM_SMD_BASE_ID + ch->alloc_entry.cid, |
| 221 | &size); |
| 222 | if(!ch->port_info->ch1.DTR_DSR) |
| 223 | { |
| 224 | dprintf(CRITICAL,"%s: DTR is off\n", __func__); |
| 225 | return -1; |
| 226 | } |
| 227 | |
Channagoud Kadabi | 161d7cc | 2014-08-13 12:57:16 -0700 | [diff] [blame] | 228 | /* Wait until the data updated in the smd buffer is equal to smd packet header*/ |
| 229 | while ((ch->port_info->ch1.write_index - ch->port_info->ch1.read_index) < sizeof(smd_pkt_hdr)) |
| 230 | { |
| 231 | /* Get the update info from memory */ |
| 232 | arch_invalidate_cache_range((addr_t) ch->port_info, size); |
Channagoud Kadabi | 161d7cc | 2014-08-13 12:57:16 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 235 | /* Copy the smd buffer to local buf */ |
Channagoud Kadabi | 4e3dc81 | 2014-10-14 12:32:56 -0700 | [diff] [blame] | 236 | memcpy_from_fifo(ch, &smd_hdr, sizeof(smd_hdr)); |
| 237 | |
| 238 | arch_invalidate_cache_range((addr_t)&smd_hdr, sizeof(smd_hdr)); |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 239 | |
| 240 | *len = smd_hdr.pkt_size; |
| 241 | |
Channagoud Kadabi | 161d7cc | 2014-08-13 12:57:16 -0700 | [diff] [blame] | 242 | /* Wait on the data being updated in SMEM before returing the response */ |
| 243 | while ((ch->port_info->ch1.write_index - ch->port_info->ch1.read_index) < smd_hdr.pkt_size) |
| 244 | { |
| 245 | /* Get the update info from memory */ |
| 246 | arch_invalidate_cache_range((addr_t) ch->port_info, size); |
Channagoud Kadabi | 161d7cc | 2014-08-13 12:57:16 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* We are good to return the response now */ |
Channagoud Kadabi | 4e3dc81 | 2014-10-14 12:32:56 -0700 | [diff] [blame] | 250 | memcpy_from_fifo(ch, response, sizeof(response)); |
| 251 | |
| 252 | arch_invalidate_cache_range((addr_t)response, sizeof(response)); |
| 253 | |
| 254 | return response; |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void smd_signal_read_complete(smd_channel_info_t *ch, uint32_t len) |
| 258 | { |
Channagoud Kadabi | 161d7cc | 2014-08-13 12:57:16 -0700 | [diff] [blame] | 259 | /* Clear the data_written flag */ |
| 260 | ch->port_info->ch1.data_written = 0; |
| 261 | |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 262 | /* Set the data_read flag */ |
Channagoud Kadabi | 161d7cc | 2014-08-13 12:57:16 -0700 | [diff] [blame] | 263 | ch->port_info->ch0.data_read = 1; |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 264 | ch->port_info->ch0.mask_recv_intr = 1; |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 265 | |
| 266 | dsb(); |
| 267 | |
| 268 | smd_notify_rpm(); |
| 269 | } |
| 270 | |
| 271 | int smd_write(smd_channel_info_t *ch, void *data, uint32_t len, int ch_type) |
| 272 | { |
| 273 | smd_pkt_hdr smd_hdr; |
| 274 | uint32_t size = 0; |
| 275 | |
| 276 | memset(&smd_hdr, 0, sizeof(smd_pkt_hdr)); |
| 277 | |
| 278 | if(len + sizeof(smd_hdr) > ch->fifo_size) |
| 279 | { |
| 280 | dprintf(CRITICAL,"%s: len is greater than fifo sz\n", __func__); |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | /* Read the indices from smem */ |
| 285 | ch->port_info = smem_get_alloc_entry(SMEM_SMD_BASE_ID + ch->alloc_entry.cid, |
| 286 | &size); |
| 287 | |
| 288 | if(!is_channel_open(ch)) |
| 289 | { |
| 290 | dprintf(CRITICAL,"%s: channel is not in OPEN state \n", __func__); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | if(!ch->port_info->ch0.DTR_DSR) |
| 295 | { |
| 296 | dprintf(CRITICAL,"%s: DTR is off\n", __func__); |
| 297 | return -1; |
| 298 | } |
| 299 | |
| 300 | /* Clear the data_read flag */ |
| 301 | ch->port_info->ch1.data_read = 0; |
| 302 | |
| 303 | /*copy the local buf to smd buf */ |
| 304 | smd_hdr.pkt_size = len; |
| 305 | |
Channagoud Kadabi | 4e3dc81 | 2014-10-14 12:32:56 -0700 | [diff] [blame] | 306 | memcpy_to_fifo(ch, (uint32_t *)&smd_hdr, sizeof(smd_hdr)); |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 307 | |
Channagoud Kadabi | 4e3dc81 | 2014-10-14 12:32:56 -0700 | [diff] [blame] | 308 | memcpy_to_fifo(ch, data, len); |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 309 | |
| 310 | dsb(); |
| 311 | |
| 312 | /* Set the necessary flags */ |
| 313 | |
| 314 | ch->port_info->ch0.data_written = 1; |
| 315 | ch->port_info->ch0.mask_recv_intr = 0; |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 316 | |
| 317 | dsb(); |
| 318 | |
| 319 | smd_notify_rpm(); |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | void smd_notify_rpm() |
| 325 | { |
| 326 | /* Set BIT 0 to notify RPM via IPC interrupt*/ |
| 327 | writel(BIT(0), APCS_ALIAS0_IPC_INTERRUPT); |
| 328 | } |
| 329 | |
| 330 | void smd_set_state(smd_channel_info_t *ch, uint32_t state, uint32_t flag) |
| 331 | { |
| 332 | uint32_t current_state; |
| 333 | uint32_t size = 0; |
| 334 | |
| 335 | if(!ch->port_info) |
| 336 | { |
| 337 | ch->port_info = smem_get_alloc_entry(SMEM_SMD_BASE_ID + ch->alloc_entry.cid, |
| 338 | &size); |
| 339 | ASSERT(ch->port_info); |
| 340 | } |
| 341 | |
| 342 | current_state = ch->port_info->ch0.stream_state; |
| 343 | |
| 344 | switch(state) |
| 345 | { |
| 346 | case SMD_SS_CLOSED: |
| 347 | if(current_state == SMD_SS_OPENED) |
| 348 | { |
| 349 | smd_write_state(ch, SMD_SS_CLOSING); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | smd_write_state(ch, SMD_SS_CLOSED); |
| 354 | } |
| 355 | break; |
| 356 | case SMD_SS_OPENING: |
| 357 | if(current_state == SMD_SS_CLOSING || current_state == SMD_SS_CLOSED) |
| 358 | { |
| 359 | smd_write_state(ch, SMD_SS_OPENING); |
| 360 | ch->port_info->ch1.read_index = 0; |
| 361 | ch->port_info->ch0.write_index = 0; |
| 362 | ch->port_info->ch0.mask_recv_intr = 0; |
| 363 | } |
| 364 | break; |
| 365 | case SMD_SS_OPENED: |
| 366 | if(current_state == SMD_SS_OPENING) |
| 367 | { |
| 368 | smd_write_state(ch, SMD_SS_OPENED); |
| 369 | } |
| 370 | break; |
| 371 | case SMD_SS_CLOSING: |
| 372 | if(current_state == SMD_SS_OPENED) |
| 373 | { |
| 374 | smd_write_state(ch, SMD_SS_CLOSING); |
| 375 | } |
| 376 | break; |
| 377 | case SMD_SS_FLUSHING: |
| 378 | case SMD_SS_RESET: |
| 379 | case SMD_SS_RESET_OPENING: |
| 380 | default: |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | ch->current_state = state; |
| 385 | |
| 386 | smd_state_update(ch, flag); |
| 387 | } |
| 388 | |
| 389 | |
| 390 | enum handler_return smd_irq_handler(void* data) |
| 391 | { |
| 392 | smd_channel_info_t *ch = (smd_channel_info_t*)data; |
| 393 | |
| 394 | if(ch->current_state == SMD_SS_CLOSED) |
| 395 | { |
| 396 | free(smd_channel_alloc_entry); |
Channagoud Kadabi | 54aafe4 | 2014-11-21 19:26:16 -0800 | [diff] [blame^] | 397 | event_signal(&smd_closed, false); |
Sundarajan Srinivasan | ae6ece0 | 2014-07-01 14:01:29 -0700 | [diff] [blame] | 398 | return INT_NO_RESCHEDULE; |
| 399 | } |
| 400 | |
| 401 | if(ch->port_info->ch1.state_updated) |
| 402 | ch->port_info->ch1.state_updated = 0; |
| 403 | |
| 404 | /* Should we have to use a do while and change states until we complete */ |
| 405 | if(ch->current_state != ch->port_info->ch1.stream_state) |
| 406 | { |
| 407 | smd_set_state(ch, ch->port_info->ch1.stream_state, 0); |
| 408 | } |
| 409 | |
| 410 | if(ch->current_state == SMD_SS_CLOSING) |
| 411 | { |
| 412 | smd_set_state(ch, SMD_SS_CLOSED, 1); |
| 413 | smd_notify_rpm(); |
| 414 | dprintf(CRITICAL,"Channel alloc freed\n"); |
| 415 | } |
| 416 | |
| 417 | return INT_NO_RESCHEDULE; |
| 418 | } |