| 1 | |
| 2 |
|
| 3 | mod continuation;
|
| 4 |
|
| 5 | use core::{
|
| 6 | mem,
|
| 7 | sync::atomic::{
|
| 8 | fence,
|
| 9 | Ordering, |
| 10 | },
|
| 11 | };
|
| 12 |
|
| 13 | use kernel::{
|
| 14 | device,
|
| 15 | dma::{
|
| 16 | Coherent,
|
| 17 | CoherentBox,
|
| 18 | DmaAddress, |
| 19 | },
|
| 20 | io::{
|
| 21 | io_project,
|
| 22 | poll::read_poll_timeout,
|
| 23 | Io, |
| 24 | },
|
| 25 | new_mutex,
|
| 26 | prelude::*,
|
| 27 | ptr,
|
| 28 | sync::{
|
| 29 | aref::ARef,
|
| 30 | Mutex, |
| 31 | },
|
| 32 | time::Delta,
|
| 33 | transmute::{
|
| 34 | AsBytes,
|
| 35 | FromBytes, |
| 36 | },
|
| 37 | };
|
| 38 |
|
| 39 | use continuation::{
|
| 40 | ContinuationRecord,
|
| 41 | SplitState, |
| 42 | };
|
| 43 |
|
| 44 | use pin_init::pin_init_scope;
|
| 45 |
|
| 46 | use crate::{
|
| 47 | driver::Bar0,
|
| 48 | gsp::{
|
| 49 | fw::{
|
| 50 | GspMsgElement,
|
| 51 | MsgFunction,
|
| 52 | MsgqRxHeader,
|
| 53 | MsgqTxHeader,
|
| 54 | GSP_MSG_QUEUE_ELEMENT_SIZE_MAX, |
| 55 | },
|
| 56 | PteArray,
|
| 57 | GSP_PAGE_SHIFT,
|
| 58 | GSP_PAGE_SIZE, |
| 59 | },
|
| 60 | num,
|
| 61 | sbuffer::SBufferIter, |
| 62 | };
|
| 63 |
|
| 64 | use super::regs;
|
| 65 |
|
| 66 | |
| 67 | |
| 68 | pub(crate) struct NoReply;
|
| 69 |
|
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | pub(crate) trait CommandToGsp {
|
| 82 | |
| 83 | const FUNCTION: MsgFunction;
|
| 84 |
|
| 85 | |
| 86 | type Command: FromBytes + AsBytes;
|
| 87 |
|
| 88 | |
| 89 | |
| 90 | type Reply;
|
| 91 |
|
| 92 | |
| 93 | type InitError;
|
| 94 |
|
| 95 | |
| 96 | |
| 97 | fn init(&self) -> impl Init<Self::Command, Self::InitError>;
|
| 98 |
|
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | fn variable_payload_len(&self) -> usize {
|
| 104 | 0
|
| 105 | }
|
| 106 |
|
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | fn init_variable_payload(
|
| 119 | &self,
|
| 120 | _dst: &mut SBufferIter<core::array::IntoIter<&mut [u8], 2>>,
|
| 121 | ) -> Result {
|
| 122 | Ok(())
|
| 123 | }
|
| 124 |
|
| 125 | |
| 126 | |
| 127 | fn size(&self) -> usize {
|
| 128 | size_of::<Self::Command>() + self.variable_payload_len()
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | |
| 133 | |
| 134 | |
| 135 | pub(crate) trait MessageFromGsp: Sized {
|
| 136 | |
| 137 | const FUNCTION: MsgFunction;
|
| 138 |
|
| 139 | |
| 140 | type InitError;
|
| 141 |
|
| 142 | |
| 143 | type Message: FromBytes;
|
| 144 |
|
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | fn read(
|
| 150 | msg: &Self::Message,
|
| 151 | sbuffer: &mut SBufferIter<core::array::IntoIter<&[u8], 2>>,
|
| 152 | ) -> Result<Self, Self::InitError>;
|
| 153 | }
|
| 154 |
|
| 155 | |
| 156 | pub(crate) const MSGQ_NUM_PAGES: u32 = 0x3f;
|
| 157 |
|
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | #[repr(C, align(0x1000))]
|
| 163 | #[derive(Debug)]
|
| 164 | struct MsgqData {
|
| 165 | data: [[u8; GSP_PAGE_SIZE]; num::u32_as_usize(MSGQ_NUM_PAGES)],
|
| 166 | }
|
| 167 |
|
| 168 | |
| 169 | |
| 170 | static_assert!(align_of::<MsgqData>() == GSP_PAGE_SIZE);
|
| 171 |
|
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | #[repr(C)]
|
| 180 | |
| 181 | |
| 182 | struct Msgq {
|
| 183 | |
| 184 | tx: MsgqTxHeader,
|
| 185 | |
| 186 | rx: MsgqRxHeader,
|
| 187 | |
| 188 | msgq: MsgqData,
|
| 189 | }
|
| 190 |
|
| 191 | |
| 192 | #[repr(C)]
|
| 193 | struct GspMem {
|
| 194 | |
| 195 | ptes: PteArray<{ Self::PTE_ARRAY_SIZE }>,
|
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | cpuq: Msgq,
|
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | gspq: Msgq,
|
| 208 | }
|
| 209 |
|
| 210 | impl GspMem {
|
| 211 | const PTE_ARRAY_SIZE: usize = GSP_PAGE_SIZE / size_of::<u64>();
|
| 212 | }
|
| 213 |
|
| 214 | |
| 215 | |
| 216 | unsafe impl AsBytes for GspMem {}
|
| 217 |
|
| 218 | |
| 219 | |
| 220 | unsafe impl FromBytes for GspMem {}
|
| 221 |
|
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | struct DmaGspMem(Coherent<GspMem>);
|
| 234 |
|
| 235 | impl DmaGspMem {
|
| 236 | |
| 237 | fn new(dev: &device::Device<device::Bound>) -> Result<Self> {
|
| 238 | const MSGQ_SIZE: u32 = num::usize_into_u32::<{ size_of::<Msgq>() }>();
|
| 239 | const RX_HDR_OFF: u32 = num::usize_into_u32::<{ mem::offset_of!(Msgq, rx) }>();
|
| 240 |
|
| 241 | let mut gsp_mem = CoherentBox::<GspMem>::zeroed(dev, GFP_KERNEL)?;
|
| 242 | gsp_mem.cpuq.tx = MsgqTxHeader::new(MSGQ_SIZE, RX_HDR_OFF, MSGQ_NUM_PAGES);
|
| 243 | gsp_mem.cpuq.rx = MsgqRxHeader::new();
|
| 244 |
|
| 245 | let gsp_mem: Coherent<_> = gsp_mem.into();
|
| 246 | PteArray::init(io_project!(gsp_mem, .ptes), gsp_mem.dma_address())?;
|
| 247 |
|
| 248 | Ok(Self(gsp_mem))
|
| 249 | }
|
| 250 |
|
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | fn driver_write_area(&mut self) -> (&mut [[u8; GSP_PAGE_SIZE]], &mut [[u8; GSP_PAGE_SIZE]]) {
|
| 257 | let tx = self.cpu_write_ptr();
|
| 258 | let rx = self.gsp_read_ptr();
|
| 259 |
|
| 260 | |
| 261 | let data = ptr::project!(mut self.0.as_mut_ptr(), .cpuq.msgq.data[build: 0]);
|
| 262 |
|
| 263 | let (tail_end, wrap_end) = if rx == 0 {
|
| 264 | |
| 265 | |
| 266 | (MSGQ_NUM_PAGES - 1, 0)
|
| 267 | } else if rx <= tx {
|
| 268 | |
| 269 | (MSGQ_NUM_PAGES, rx - 1)
|
| 270 | } else {
|
| 271 | |
| 272 | (rx - 1, 0)
|
| 273 | };
|
| 274 |
|
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | unsafe {
|
| 290 | (
|
| 291 | core::slice::from_raw_parts_mut(
|
| 292 | data.add(num::u32_as_usize(tx)),
|
| 293 | num::u32_as_usize(tail_end - tx),
|
| 294 | ),
|
| 295 | core::slice::from_raw_parts_mut(data, num::u32_as_usize(wrap_end)),
|
| 296 | )
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | |
| 301 | |
| 302 | fn driver_write_area_size(&self) -> usize {
|
| 303 | let tx = self.cpu_write_ptr();
|
| 304 | let rx = self.gsp_read_ptr();
|
| 305 |
|
| 306 | |
| 307 | |
| 308 | |
| 309 | let slots = (rx + MSGQ_NUM_PAGES - tx - 1) % MSGQ_NUM_PAGES;
|
| 310 | num::u32_as_usize(slots) * GSP_PAGE_SIZE
|
| 311 | }
|
| 312 |
|
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | fn driver_read_area(&self) -> (&[[u8; GSP_PAGE_SIZE]], &[[u8; GSP_PAGE_SIZE]]) {
|
| 319 | let tx = self.gsp_write_ptr();
|
| 320 | let rx = self.cpu_read_ptr();
|
| 321 |
|
| 322 | |
| 323 | let data = ptr::project!(self.0.as_ptr(), .gspq.msgq.data[build: 0]);
|
| 324 |
|
| 325 | let (tail_end, wrap_end) = if rx <= tx {
|
| 326 | |
| 327 | (tx, 0)
|
| 328 | } else {
|
| 329 | |
| 330 | (MSGQ_NUM_PAGES, tx)
|
| 331 | };
|
| 332 |
|
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | unsafe {
|
| 344 | (
|
| 345 | core::slice::from_raw_parts(
|
| 346 | data.add(num::u32_as_usize(rx)),
|
| 347 | num::u32_as_usize(tail_end - rx),
|
| 348 | ),
|
| 349 | core::slice::from_raw_parts(data, num::u32_as_usize(wrap_end)),
|
| 350 | )
|
| 351 | }
|
| 352 | }
|
| 353 |
|
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | fn allocate_command(&mut self, size: usize, timeout: Delta) -> Result<GspCommand<'_>> {
|
| 365 | if size_of::<GspMsgElement>() + size > GSP_MSG_QUEUE_ELEMENT_SIZE_MAX {
|
| 366 | return Err(EMSGSIZE);
|
| 367 | }
|
| 368 | read_poll_timeout(
|
| 369 | || Ok(self.driver_write_area_size()),
|
| 370 | |available_bytes| *available_bytes >= size_of::<GspMsgElement>() + size,
|
| 371 | Delta::from_micros(1),
|
| 372 | timeout,
|
| 373 | )?;
|
| 374 |
|
| 375 | |
| 376 | let (slice_1, slice_2) = {
|
| 377 | let (slice_1, slice_2) = self.driver_write_area();
|
| 378 |
|
| 379 | (slice_1.as_flattened_mut(), slice_2.as_flattened_mut())
|
| 380 | };
|
| 381 |
|
| 382 | |
| 383 | let (header, slice_1) = GspMsgElement::from_bytes_mut_prefix(slice_1).ok_or(EIO)?;
|
| 384 |
|
| 385 | |
| 386 | let (slice_1, slice_2) = if slice_1.len() > size {
|
| 387 | |
| 388 | (&mut slice_1[..size], &mut slice_2[0..0])
|
| 389 | } else {
|
| 390 | |
| 391 | let slice_2_len = size - slice_1.len();
|
| 392 | (slice_1, &mut slice_2[..slice_2_len])
|
| 393 | };
|
| 394 |
|
| 395 | Ok(GspCommand {
|
| 396 | header,
|
| 397 | contents: (slice_1, slice_2),
|
| 398 | })
|
| 399 | }
|
| 400 |
|
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | fn gsp_write_ptr(&self) -> u32 {
|
| 407 | MsgqTxHeader::write_ptr(io_project!(self.0, .gspq.tx)) % MSGQ_NUM_PAGES
|
| 408 | }
|
| 409 |
|
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | fn gsp_read_ptr(&self) -> u32 {
|
| 416 | MsgqRxHeader::read_ptr(io_project!(self.0, .gspq.rx)) % MSGQ_NUM_PAGES
|
| 417 | }
|
| 418 |
|
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | fn cpu_read_ptr(&self) -> u32 {
|
| 425 | MsgqRxHeader::read_ptr(io_project!(self.0, .cpuq.rx)) % MSGQ_NUM_PAGES
|
| 426 | }
|
| 427 |
|
| 428 | |
| 429 | fn advance_cpu_read_ptr(&mut self, elem_count: u32) {
|
| 430 | let rx = io_project!(self.0, .cpuq.rx);
|
| 431 | let rptr = MsgqRxHeader::read_ptr(rx).wrapping_add(elem_count) % MSGQ_NUM_PAGES;
|
| 432 |
|
| 433 | |
| 434 | fence(Ordering::SeqCst);
|
| 435 |
|
| 436 | MsgqRxHeader::set_read_ptr(rx, rptr)
|
| 437 | }
|
| 438 |
|
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | fn cpu_write_ptr(&self) -> u32 {
|
| 445 | MsgqTxHeader::write_ptr(io_project!(self.0, .cpuq.tx)) % MSGQ_NUM_PAGES
|
| 446 | }
|
| 447 |
|
| 448 | |
| 449 | fn advance_cpu_write_ptr(&mut self, elem_count: u32) {
|
| 450 | let tx = io_project!(self.0, .cpuq.tx);
|
| 451 | let wptr = MsgqTxHeader::write_ptr(tx).wrapping_add(elem_count) % MSGQ_NUM_PAGES;
|
| 452 | MsgqTxHeader::set_write_ptr(tx, wptr);
|
| 453 |
|
| 454 | |
| 455 | fence(Ordering::SeqCst);
|
| 456 | }
|
| 457 | }
|
| 458 |
|
| 459 | |
| 460 | |
| 461 | |
| 462 | struct GspCommand<'a> {
|
| 463 | |
| 464 | header: &'a mut GspMsgElement,
|
| 465 | |
| 466 | |
| 467 | contents: (&'a mut [u8], &'a mut [u8]),
|
| 468 | }
|
| 469 |
|
| 470 | |
| 471 | |
| 472 | |
| 473 | struct GspMessage<'a> {
|
| 474 | |
| 475 | header: &'a GspMsgElement,
|
| 476 | |
| 477 | |
| 478 | contents: (&'a [u8], &'a [u8]),
|
| 479 | }
|
| 480 |
|
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | #[pin_data]
|
| 486 | pub(crate) struct Cmdq {
|
| 487 | |
| 488 | #[pin]
|
| 489 | inner: Mutex<CmdqInner>,
|
| 490 | |
| 491 | pub(super) dma_addr: DmaAddress,
|
| 492 | }
|
| 493 |
|
| 494 | impl Cmdq {
|
| 495 | |
| 496 | const POST_PTE_OFFSET: usize = core::mem::offset_of!(GspMem, cpuq);
|
| 497 |
|
| 498 | |
| 499 | pub(crate) const CMDQ_OFFSET: usize = core::mem::offset_of!(GspMem, cpuq)
|
| 500 | + core::mem::offset_of!(Msgq, msgq)
|
| 501 | - Self::POST_PTE_OFFSET;
|
| 502 |
|
| 503 | |
| 504 | pub(crate) const STATQ_OFFSET: usize = core::mem::offset_of!(GspMem, gspq)
|
| 505 | + core::mem::offset_of!(Msgq, msgq)
|
| 506 | - Self::POST_PTE_OFFSET;
|
| 507 |
|
| 508 | |
| 509 | pub(crate) const NUM_PTES: usize = size_of::<GspMem>() >> GSP_PAGE_SHIFT;
|
| 510 |
|
| 511 | |
| 512 | pub(super) const RECEIVE_TIMEOUT: Delta = Delta::from_secs(5);
|
| 513 |
|
| 514 | |
| 515 | pub(crate) fn new(dev: &device::Device<device::Bound>) -> impl PinInit<Self, Error> + '_ {
|
| 516 | pin_init_scope(move || {
|
| 517 | let gsp_mem = DmaGspMem::new(dev)?;
|
| 518 |
|
| 519 | Ok(try_pin_init!(Self {
|
| 520 | dma_addr: gsp_mem.0.dma_address(),
|
| 521 | inner <- new_mutex!(CmdqInner {
|
| 522 | dev: dev.into(),
|
| 523 | gsp_mem,
|
| 524 | seq: 0,
|
| 525 | }),
|
| 526 | }))
|
| 527 | })
|
| 528 | }
|
| 529 |
|
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | fn calculate_checksum<T: Iterator<Item = u8>>(it: T) -> u32 {
|
| 535 | let sum64 = it
|
| 536 | .enumerate()
|
| 537 | .map(|(idx, byte)| (((idx % 8) * 8) as u32, byte))
|
| 538 | .fold(0, |acc, (rol, byte)| acc ^ u64::from(byte).rotate_left(rol));
|
| 539 |
|
| 540 | ((sum64 >> 32) as u32) ^ (sum64 as u32)
|
| 541 | }
|
| 542 |
|
| 543 | |
| 544 | fn notify_gsp(bar: Bar0<'_>) {
|
| 545 | bar.write_reg(regs::NV_PGSP_QUEUE_HEAD::zeroed().with_address(0u32));
|
| 546 | }
|
| 547 |
|
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | pub(crate) fn send_command<M>(&self, bar: Bar0<'_>, command: M) -> Result<M::Reply>
|
| 565 | where
|
| 566 | M: CommandToGsp,
|
| 567 | M::Reply: MessageFromGsp,
|
| 568 | Error: From<M::InitError>,
|
| 569 | Error: From<<M::Reply as MessageFromGsp>::InitError>,
|
| 570 | {
|
| 571 | let mut inner = self.inner.lock();
|
| 572 | inner.send_command(bar, command)?;
|
| 573 |
|
| 574 | loop {
|
| 575 | match inner.receive_msg::<M::Reply>(Self::RECEIVE_TIMEOUT) {
|
| 576 | Ok(reply) => break Ok(reply),
|
| 577 | Err(ERANGE) => continue,
|
| 578 | Err(e) => break Err(e),
|
| 579 | }
|
| 580 | }
|
| 581 | }
|
| 582 |
|
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | pub(crate) fn send_command_no_wait<M>(&self, bar: Bar0<'_>, command: M) -> Result
|
| 593 | where
|
| 594 | M: CommandToGsp<Reply = NoReply>,
|
| 595 | Error: From<M::InitError>,
|
| 596 | {
|
| 597 | self.inner.lock().send_command(bar, command)
|
| 598 | }
|
| 599 |
|
| 600 | |
| 601 | |
| 602 | |
| 603 | pub(crate) fn receive_msg<M: MessageFromGsp>(&self, timeout: Delta) -> Result<M>
|
| 604 | where
|
| 605 | |
| 606 | Error: From<M::InitError>,
|
| 607 | {
|
| 608 | self.inner.lock().receive_msg(timeout)
|
| 609 | }
|
| 610 | }
|
| 611 |
|
| 612 | |
| 613 | struct CmdqInner {
|
| 614 | |
| 615 | dev: ARef<device::Device>,
|
| 616 | |
| 617 | seq: u32,
|
| 618 | |
| 619 | gsp_mem: DmaGspMem,
|
| 620 | }
|
| 621 |
|
| 622 | impl CmdqInner {
|
| 623 | |
| 624 | const ALLOCATE_TIMEOUT: Delta = Delta::from_secs(1);
|
| 625 |
|
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | fn send_single_command<M>(&mut self, bar: Bar0<'_>, command: M) -> Result
|
| 637 | where
|
| 638 | M: CommandToGsp,
|
| 639 | |
| 640 | Error: From<M::InitError>,
|
| 641 | {
|
| 642 | let size_in_bytes = command.size();
|
| 643 | let dst = self
|
| 644 | .gsp_mem
|
| 645 | .allocate_command(size_in_bytes, Self::ALLOCATE_TIMEOUT)?;
|
| 646 |
|
| 647 | |
| 648 | |
| 649 | |
| 650 | let (cmd, payload_1) = M::Command::from_bytes_mut_prefix(dst.contents.0).ok_or(EIO)?;
|
| 651 |
|
| 652 | |
| 653 | let msg_element = GspMsgElement::init(self.seq, size_in_bytes, M::FUNCTION);
|
| 654 | |
| 655 | |
| 656 | unsafe {
|
| 657 | pin_init::raw_try_init(core::ptr::from_mut(dst.header), msg_element)?;
|
| 658 | pin_init::raw_try_init(core::ptr::from_mut(cmd), command.init())?;
|
| 659 | }
|
| 660 |
|
| 661 | |
| 662 | let mut sbuffer = SBufferIter::new_writer([&mut payload_1[..], &mut dst.contents.1[..]]);
|
| 663 | command.init_variable_payload(&mut sbuffer)?;
|
| 664 |
|
| 665 | if !sbuffer.is_empty() {
|
| 666 | return Err(EIO);
|
| 667 | }
|
| 668 | drop(sbuffer);
|
| 669 |
|
| 670 | |
| 671 | dst.header
|
| 672 | .set_checksum(Cmdq::calculate_checksum(SBufferIter::new_reader([
|
| 673 | dst.header.as_bytes(),
|
| 674 | dst.contents.0,
|
| 675 | dst.contents.1,
|
| 676 | ])));
|
| 677 |
|
| 678 | dev_dbg!(
|
| 679 | &self.dev,
|
| 680 | "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
|
| 681 | self.seq,
|
| 682 | M::FUNCTION,
|
| 683 | dst.header.length(),
|
| 684 | );
|
| 685 |
|
| 686 | |
| 687 | let elem_count = dst.header.element_count();
|
| 688 | self.seq += 1;
|
| 689 | self.gsp_mem.advance_cpu_write_ptr(elem_count);
|
| 690 | Cmdq::notify_gsp(bar);
|
| 691 |
|
| 692 | Ok(())
|
| 693 | }
|
| 694 |
|
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | fn send_command<M>(&mut self, bar: Bar0<'_>, command: M) -> Result
|
| 707 | where
|
| 708 | M: CommandToGsp,
|
| 709 | Error: From<M::InitError>,
|
| 710 | {
|
| 711 | match SplitState::new(command)? {
|
| 712 | SplitState::Single(command) => self.send_single_command(bar, command),
|
| 713 | SplitState::Split(command, mut continuations) => {
|
| 714 | self.send_single_command(bar, command)?;
|
| 715 |
|
| 716 | while let Some(continuation) = continuations.next() {
|
| 717 | |
| 718 | self.send_single_command::<ContinuationRecord<'_>>(bar, continuation)?;
|
| 719 | }
|
| 720 |
|
| 721 | Ok(())
|
| 722 | }
|
| 723 | }
|
| 724 | }
|
| 725 |
|
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
|
| 745 | |
| 746 | let (slice_1, slice_2) = read_poll_timeout(
|
| 747 | || Ok(self.gsp_mem.driver_read_area()),
|
| 748 | |driver_area| !driver_area.0.is_empty(),
|
| 749 | Delta::from_millis(1),
|
| 750 | timeout,
|
| 751 | )
|
| 752 | .map(|(slice_1, slice_2)| (slice_1.as_flattened(), slice_2.as_flattened()))?;
|
| 753 |
|
| 754 | |
| 755 | let (header, slice_1) = GspMsgElement::from_bytes_prefix(slice_1).ok_or(EIO)?;
|
| 756 |
|
| 757 | dev_dbg!(
|
| 758 | &self.dev,
|
| 759 | "GSP RPC: receive: seq# {}, function={:?}, length=0x{:x}\n",
|
| 760 | header.sequence(),
|
| 761 | header.function(),
|
| 762 | header.length(),
|
| 763 | );
|
| 764 |
|
| 765 | let payload_length = header.payload_length();
|
| 766 |
|
| 767 | |
| 768 | if slice_1.len() + slice_2.len() < payload_length {
|
| 769 | return Err(EIO);
|
| 770 | }
|
| 771 |
|
| 772 | |
| 773 | let (slice_1, slice_2) = if slice_1.len() > payload_length {
|
| 774 | |
| 775 | (slice_1.split_at(payload_length).0, &slice_2[0..0])
|
| 776 | } else {
|
| 777 | (
|
| 778 | slice_1,
|
| 779 | |
| 780 | |
| 781 | slice_2.split_at(payload_length - slice_1.len()).0,
|
| 782 | )
|
| 783 | };
|
| 784 |
|
| 785 | |
| 786 | if Cmdq::calculate_checksum(SBufferIter::new_reader([
|
| 787 | header.as_bytes(),
|
| 788 | slice_1,
|
| 789 | slice_2,
|
| 790 | ])) != 0
|
| 791 | {
|
| 792 | dev_err!(
|
| 793 | &self.dev,
|
| 794 | "GSP RPC: receive: Call {} - bad checksum\n",
|
| 795 | header.sequence()
|
| 796 | );
|
| 797 | return Err(EIO);
|
| 798 | }
|
| 799 |
|
| 800 | Ok(GspMessage {
|
| 801 | header,
|
| 802 | contents: (slice_1, slice_2),
|
| 803 | })
|
| 804 | }
|
| 805 |
|
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
|
| 823 | where
|
| 824 | |
| 825 | Error: From<M::InitError>,
|
| 826 | {
|
| 827 | let message = self.wait_for_msg(timeout)?;
|
| 828 | let function = message.header.function().map_err(|_| EINVAL)?;
|
| 829 |
|
| 830 | |
| 831 | |
| 832 | let result = if function == M::FUNCTION {
|
| 833 | let (cmd, contents_1) = M::Message::from_bytes_prefix(message.contents.0).ok_or(EIO)?;
|
| 834 | let mut sbuffer = SBufferIter::new_reader([contents_1, message.contents.1]);
|
| 835 |
|
| 836 | M::read(cmd, &mut sbuffer)
|
| 837 | .map_err(|e| e.into())
|
| 838 | .inspect(|_| {
|
| 839 | if !sbuffer.is_empty() {
|
| 840 | dev_warn!(
|
| 841 | &self.dev,
|
| 842 | "GSP message {:?} has unprocessed data\n",
|
| 843 | function
|
| 844 | );
|
| 845 | }
|
| 846 | })
|
| 847 | } else {
|
| 848 | Err(ERANGE)
|
| 849 | };
|
| 850 |
|
| 851 | |
| 852 | self.gsp_mem.advance_cpu_read_ptr(u32::try_from(
|
| 853 | message.header.length().div_ceil(GSP_PAGE_SIZE),
|
| 854 | )?);
|
| 855 |
|
| 856 | result
|
| 857 | }
|
| 858 | }
|