165c80d60SJolly Shah /* 2fe550edeSVenkatesh Yadav Abbarapu * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved. 365c80d60SJolly Shah * 465c80d60SJolly Shah * SPDX-License-Identifier: BSD-3-Clause 565c80d60SJolly Shah */ 665c80d60SJolly Shah 765c80d60SJolly Shah 865c80d60SJolly Shah #include <arch_helpers.h> 965c80d60SJolly Shah 1065c80d60SJolly Shah #include <lib/bakery_lock.h> 1165c80d60SJolly Shah #include <lib/mmio.h> 1265c80d60SJolly Shah 1365c80d60SJolly Shah #include <ipi.h> 1465c80d60SJolly Shah #include <plat_ipi.h> 1565c80d60SJolly Shah #include <plat_private.h> 1665c80d60SJolly Shah #include <plat/common/platform.h> 1765c80d60SJolly Shah 1865c80d60SJolly Shah #include "pm_ipi.h" 1965c80d60SJolly Shah 2065c80d60SJolly Shah 21addc4e96SRavi Patel #define ERROR_CODE_MASK 0xFFFFU 22addc4e96SRavi Patel 2365c80d60SJolly Shah DEFINE_BAKERY_LOCK(pm_secure_lock); 2465c80d60SJolly Shah 2565c80d60SJolly Shah /** 2665c80d60SJolly Shah * pm_ipi_init() - Initialize IPI peripheral for communication with 2765c80d60SJolly Shah * remote processor 2865c80d60SJolly Shah * 2965c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 3065c80d60SJolly Shah * @return On success, the initialization function must return 0. 3165c80d60SJolly Shah * Any other return value will cause the framework to ignore 3265c80d60SJolly Shah * the service 3365c80d60SJolly Shah * 3465c80d60SJolly Shah * Called from pm_setup initialization function 3565c80d60SJolly Shah */ 3665c80d60SJolly Shah int pm_ipi_init(const struct pm_proc *proc) 3765c80d60SJolly Shah { 3865c80d60SJolly Shah bakery_lock_init(&pm_secure_lock); 3965c80d60SJolly Shah ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); 4065c80d60SJolly Shah 4165c80d60SJolly Shah return 0; 4265c80d60SJolly Shah } 4365c80d60SJolly Shah 4465c80d60SJolly Shah /** 4565c80d60SJolly Shah * pm_ipi_send_common() - Sends IPI request to the remote processor 4665c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 4765c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 4865c80d60SJolly Shah * 4965c80d60SJolly Shah * Send an IPI request to the power controller. Caller needs to hold 5065c80d60SJolly Shah * the 'pm_secure_lock' lock. 5165c80d60SJolly Shah * 5265c80d60SJolly Shah * @return Returns status, either success or error+reason 5365c80d60SJolly Shah */ 5465c80d60SJolly Shah static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc, 5565c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT], 5665c80d60SJolly Shah uint32_t is_blocking) 5765c80d60SJolly Shah { 5865c80d60SJolly Shah unsigned int offset = 0; 5965c80d60SJolly Shah uintptr_t buffer_base = proc->ipi->buffer_base + 6065c80d60SJolly Shah IPI_BUFFER_TARGET_REMOTE_OFFSET + 6165c80d60SJolly Shah IPI_BUFFER_REQ_OFFSET; 62d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK 63fe550edeSVenkatesh Yadav Abbarapu payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE); 64fe550edeSVenkatesh Yadav Abbarapu #endif 6565c80d60SJolly Shah 6665c80d60SJolly Shah /* Write payload into IPI buffer */ 6765c80d60SJolly Shah for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) { 6865c80d60SJolly Shah mmio_write_32(buffer_base + offset, payload[i]); 6965c80d60SJolly Shah offset += PAYLOAD_ARG_SIZE; 7065c80d60SJolly Shah } 7165c80d60SJolly Shah 7265c80d60SJolly Shah /* Generate IPI to remote processor */ 73*62f9134dSVenkatesh Yadav Abbarapu ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id, 7465c80d60SJolly Shah is_blocking); 7565c80d60SJolly Shah 76*62f9134dSVenkatesh Yadav Abbarapu return PM_RET_SUCCESS; 774d9b9b23STejas Patel } 784d9b9b23STejas Patel 7965c80d60SJolly Shah /** 8065c80d60SJolly Shah * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor 8165c80d60SJolly Shah * without blocking notification 8265c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 8365c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 8465c80d60SJolly Shah * 8565c80d60SJolly Shah * Send an IPI request to the power controller. 8665c80d60SJolly Shah * 8765c80d60SJolly Shah * @return Returns status, either success or error+reason 8865c80d60SJolly Shah */ 8965c80d60SJolly Shah enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc, 9065c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT]) 9165c80d60SJolly Shah { 9265c80d60SJolly Shah enum pm_ret_status ret; 9365c80d60SJolly Shah 9465c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 9565c80d60SJolly Shah 9665c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING); 9765c80d60SJolly Shah 9865c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 9965c80d60SJolly Shah 10065c80d60SJolly Shah return ret; 10165c80d60SJolly Shah } 10265c80d60SJolly Shah 10365c80d60SJolly Shah /** 10465c80d60SJolly Shah * pm_ipi_send() - Sends IPI request to the remote processor 10565c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 10665c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 10765c80d60SJolly Shah * 10865c80d60SJolly Shah * Send an IPI request to the power controller. 10965c80d60SJolly Shah * 11065c80d60SJolly Shah * @return Returns status, either success or error+reason 11165c80d60SJolly Shah */ 11265c80d60SJolly Shah enum pm_ret_status pm_ipi_send(const struct pm_proc *proc, 11365c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT]) 11465c80d60SJolly Shah { 11565c80d60SJolly Shah enum pm_ret_status ret; 11665c80d60SJolly Shah 11765c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 11865c80d60SJolly Shah 11965c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); 12065c80d60SJolly Shah 12165c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 12265c80d60SJolly Shah 12365c80d60SJolly Shah return ret; 12465c80d60SJolly Shah } 12565c80d60SJolly Shah 12665c80d60SJolly Shah 12765c80d60SJolly Shah /** 12865c80d60SJolly Shah * pm_ipi_buff_read() - Reads IPI response after remote processor has handled 12965c80d60SJolly Shah * interrupt 13065c80d60SJolly Shah * @proc Pointer to the processor who is waiting and reading response 13165c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 13265c80d60SJolly Shah * @count Number of values to return in @value 13365c80d60SJolly Shah * 13465c80d60SJolly Shah * @return Returns status, either success or error+reason 13565c80d60SJolly Shah */ 13665c80d60SJolly Shah static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc, 13765c80d60SJolly Shah unsigned int *value, size_t count) 13865c80d60SJolly Shah { 13965c80d60SJolly Shah size_t i; 140d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK 141fe550edeSVenkatesh Yadav Abbarapu size_t j; 142fe550edeSVenkatesh Yadav Abbarapu unsigned int response_payload[PAYLOAD_ARG_CNT]; 143fe550edeSVenkatesh Yadav Abbarapu #endif 14465c80d60SJolly Shah uintptr_t buffer_base = proc->ipi->buffer_base + 14565c80d60SJolly Shah IPI_BUFFER_TARGET_REMOTE_OFFSET + 14665c80d60SJolly Shah IPI_BUFFER_RESP_OFFSET; 14765c80d60SJolly Shah 14865c80d60SJolly Shah /* 14965c80d60SJolly Shah * Read response from IPI buffer 15065c80d60SJolly Shah * buf-0: success or error+reason 15165c80d60SJolly Shah * buf-1: value 15265c80d60SJolly Shah * buf-2: unused 15365c80d60SJolly Shah * buf-3: unused 15465c80d60SJolly Shah */ 15565c80d60SJolly Shah for (i = 1; i <= count; i++) { 15665c80d60SJolly Shah *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); 15765c80d60SJolly Shah value++; 15865c80d60SJolly Shah } 159d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK 160fe550edeSVenkatesh Yadav Abbarapu for (j = 0; j < PAYLOAD_ARG_CNT; j++) 161fe550edeSVenkatesh Yadav Abbarapu response_payload[j] = mmio_read_32(buffer_base + 162fe550edeSVenkatesh Yadav Abbarapu (j * PAYLOAD_ARG_SIZE)); 163fe550edeSVenkatesh Yadav Abbarapu 164fe550edeSVenkatesh Yadav Abbarapu if (response_payload[PAYLOAD_CRC_POS] != 165fe550edeSVenkatesh Yadav Abbarapu calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) 166fe550edeSVenkatesh Yadav Abbarapu NOTICE("ERROR in CRC response payload value:0x%x\n", 167fe550edeSVenkatesh Yadav Abbarapu response_payload[PAYLOAD_CRC_POS]); 168fe550edeSVenkatesh Yadav Abbarapu #endif 16965c80d60SJolly Shah 17065c80d60SJolly Shah return mmio_read_32(buffer_base); 17165c80d60SJolly Shah } 17265c80d60SJolly Shah 17365c80d60SJolly Shah /** 17465c80d60SJolly Shah * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has 17565c80d60SJolly Shah * handled interrupt 17665c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 17765c80d60SJolly Shah * @count Number of values to return in @value 17865c80d60SJolly Shah * 17965c80d60SJolly Shah * @return Returns status, either success or error+reason 18065c80d60SJolly Shah */ 18165c80d60SJolly Shah void pm_ipi_buff_read_callb(unsigned int *value, size_t count) 18265c80d60SJolly Shah { 18365c80d60SJolly Shah size_t i; 184d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK 185fe550edeSVenkatesh Yadav Abbarapu size_t j; 186fe550edeSVenkatesh Yadav Abbarapu unsigned int response_payload[PAYLOAD_ARG_CNT]; 187fe550edeSVenkatesh Yadav Abbarapu #endif 18865c80d60SJolly Shah uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE + 18965c80d60SJolly Shah IPI_BUFFER_TARGET_LOCAL_OFFSET + 19065c80d60SJolly Shah IPI_BUFFER_REQ_OFFSET; 19165c80d60SJolly Shah 19265c80d60SJolly Shah if (count > IPI_BUFFER_MAX_WORDS) 19365c80d60SJolly Shah count = IPI_BUFFER_MAX_WORDS; 19465c80d60SJolly Shah 19565c80d60SJolly Shah for (i = 0; i <= count; i++) { 19665c80d60SJolly Shah *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); 19765c80d60SJolly Shah value++; 19865c80d60SJolly Shah } 199d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK 200fe550edeSVenkatesh Yadav Abbarapu for (j = 0; j < PAYLOAD_ARG_CNT; j++) 201fe550edeSVenkatesh Yadav Abbarapu response_payload[j] = mmio_read_32(buffer_base + 202fe550edeSVenkatesh Yadav Abbarapu (j * PAYLOAD_ARG_SIZE)); 203fe550edeSVenkatesh Yadav Abbarapu 204fe550edeSVenkatesh Yadav Abbarapu if (response_payload[PAYLOAD_CRC_POS] != 205fe550edeSVenkatesh Yadav Abbarapu calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) 206fe550edeSVenkatesh Yadav Abbarapu NOTICE("ERROR in CRC response payload value:0x%x\n", 207fe550edeSVenkatesh Yadav Abbarapu response_payload[PAYLOAD_CRC_POS]); 208fe550edeSVenkatesh Yadav Abbarapu #endif 20965c80d60SJolly Shah } 21065c80d60SJolly Shah 21165c80d60SJolly Shah /** 21265c80d60SJolly Shah * pm_ipi_send_sync() - Sends IPI request to the remote processor 21365c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 21465c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 21565c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 21665c80d60SJolly Shah * @count Number of values to return in @value 21765c80d60SJolly Shah * 21865c80d60SJolly Shah * Send an IPI request to the power controller and wait for it to be handled. 21965c80d60SJolly Shah * 22065c80d60SJolly Shah * @return Returns status, either success or error+reason and, optionally, 22165c80d60SJolly Shah * @value 22265c80d60SJolly Shah */ 22365c80d60SJolly Shah enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc, 22465c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT], 22565c80d60SJolly Shah unsigned int *value, size_t count) 22665c80d60SJolly Shah { 22765c80d60SJolly Shah enum pm_ret_status ret; 22865c80d60SJolly Shah 22965c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 23065c80d60SJolly Shah 23165c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); 23265c80d60SJolly Shah if (ret != PM_RET_SUCCESS) 23365c80d60SJolly Shah goto unlock; 23465c80d60SJolly Shah 235addc4e96SRavi Patel ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count)); 23665c80d60SJolly Shah 23765c80d60SJolly Shah unlock: 23865c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 23965c80d60SJolly Shah 24065c80d60SJolly Shah return ret; 24165c80d60SJolly Shah } 24265c80d60SJolly Shah 24365c80d60SJolly Shah void pm_ipi_irq_enable(const struct pm_proc *proc) 24465c80d60SJolly Shah { 24565c80d60SJolly Shah ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); 24665c80d60SJolly Shah } 24765c80d60SJolly Shah 24865c80d60SJolly Shah void pm_ipi_irq_clear(const struct pm_proc *proc) 24965c80d60SJolly Shah { 25065c80d60SJolly Shah ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); 25165c80d60SJolly Shah } 25265c80d60SJolly Shah 25365c80d60SJolly Shah uint32_t pm_ipi_irq_status(const struct pm_proc *proc) 25465c80d60SJolly Shah { 25565c80d60SJolly Shah int ret; 25665c80d60SJolly Shah 25765c80d60SJolly Shah ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id, 25865c80d60SJolly Shah proc->ipi->remote_ipi_id); 25965c80d60SJolly Shah if (ret & IPI_MB_STATUS_RECV_PENDING) 26065c80d60SJolly Shah return 1; 26165c80d60SJolly Shah else 26265c80d60SJolly Shah return 0; 26365c80d60SJolly Shah } 264fe550edeSVenkatesh Yadav Abbarapu 265d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK 266fe550edeSVenkatesh Yadav Abbarapu uint32_t calculate_crc(uint32_t *payload, uint32_t bufsize) 267fe550edeSVenkatesh Yadav Abbarapu { 268fe550edeSVenkatesh Yadav Abbarapu uint32_t crcinit = CRC_INIT_VALUE; 269fe550edeSVenkatesh Yadav Abbarapu uint32_t order = CRC_ORDER; 270fe550edeSVenkatesh Yadav Abbarapu uint32_t polynom = CRC_POLYNOM; 271fe550edeSVenkatesh Yadav Abbarapu uint32_t i, j, c, bit, datain, crcmask, crchighbit; 272fe550edeSVenkatesh Yadav Abbarapu uint32_t crc = crcinit; 273fe550edeSVenkatesh Yadav Abbarapu 274fe550edeSVenkatesh Yadav Abbarapu crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U; 275fe550edeSVenkatesh Yadav Abbarapu crchighbit = (uint32_t)(1U << (order - 1U)); 276fe550edeSVenkatesh Yadav Abbarapu 277fe550edeSVenkatesh Yadav Abbarapu for (i = 0U; i < bufsize; i++) { 278fe550edeSVenkatesh Yadav Abbarapu datain = mmio_read_8((unsigned long)payload + i); 279fe550edeSVenkatesh Yadav Abbarapu c = datain; 280fe550edeSVenkatesh Yadav Abbarapu j = 0x80U; 281fe550edeSVenkatesh Yadav Abbarapu while (j != 0U) { 282fe550edeSVenkatesh Yadav Abbarapu bit = crc & crchighbit; 283fe550edeSVenkatesh Yadav Abbarapu crc <<= 1U; 284fe550edeSVenkatesh Yadav Abbarapu if (0U != (c & j)) 285fe550edeSVenkatesh Yadav Abbarapu bit ^= crchighbit; 286fe550edeSVenkatesh Yadav Abbarapu if (bit != 0U) 287fe550edeSVenkatesh Yadav Abbarapu crc ^= polynom; 288fe550edeSVenkatesh Yadav Abbarapu j >>= 1U; 289fe550edeSVenkatesh Yadav Abbarapu } 290fe550edeSVenkatesh Yadav Abbarapu crc &= crcmask; 291fe550edeSVenkatesh Yadav Abbarapu } 292fe550edeSVenkatesh Yadav Abbarapu return crc; 293fe550edeSVenkatesh Yadav Abbarapu } 294fe550edeSVenkatesh Yadav Abbarapu #endif 295