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 { 58*4d9b9b23STejas Patel int status; 5965c80d60SJolly Shah unsigned int offset = 0; 6065c80d60SJolly Shah uintptr_t buffer_base = proc->ipi->buffer_base + 6165c80d60SJolly Shah IPI_BUFFER_TARGET_REMOTE_OFFSET + 6265c80d60SJolly Shah IPI_BUFFER_REQ_OFFSET; 63fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 64fe550edeSVenkatesh Yadav Abbarapu payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE); 65fe550edeSVenkatesh Yadav Abbarapu #endif 6665c80d60SJolly Shah 6765c80d60SJolly Shah /* Write payload into IPI buffer */ 6865c80d60SJolly Shah for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) { 6965c80d60SJolly Shah mmio_write_32(buffer_base + offset, payload[i]); 7065c80d60SJolly Shah offset += PAYLOAD_ARG_SIZE; 7165c80d60SJolly Shah } 7265c80d60SJolly Shah 7365c80d60SJolly Shah /* Generate IPI to remote processor */ 74*4d9b9b23STejas Patel status = ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id, 7565c80d60SJolly Shah is_blocking); 76*4d9b9b23STejas Patel if (status == 0) { 7765c80d60SJolly Shah return PM_RET_SUCCESS; 7865c80d60SJolly Shah } 7965c80d60SJolly Shah 80*4d9b9b23STejas Patel return PM_RET_ERROR_TIMEOUT; 81*4d9b9b23STejas Patel } 82*4d9b9b23STejas Patel 8365c80d60SJolly Shah /** 8465c80d60SJolly Shah * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor 8565c80d60SJolly Shah * without blocking notification 8665c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 8765c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 8865c80d60SJolly Shah * 8965c80d60SJolly Shah * Send an IPI request to the power controller. 9065c80d60SJolly Shah * 9165c80d60SJolly Shah * @return Returns status, either success or error+reason 9265c80d60SJolly Shah */ 9365c80d60SJolly Shah enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc, 9465c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT]) 9565c80d60SJolly Shah { 9665c80d60SJolly Shah enum pm_ret_status ret; 9765c80d60SJolly Shah 9865c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 9965c80d60SJolly Shah 10065c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING); 10165c80d60SJolly Shah 10265c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 10365c80d60SJolly Shah 10465c80d60SJolly Shah return ret; 10565c80d60SJolly Shah } 10665c80d60SJolly Shah 10765c80d60SJolly Shah /** 10865c80d60SJolly Shah * pm_ipi_send() - Sends IPI request to the remote processor 10965c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 11065c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 11165c80d60SJolly Shah * 11265c80d60SJolly Shah * Send an IPI request to the power controller. 11365c80d60SJolly Shah * 11465c80d60SJolly Shah * @return Returns status, either success or error+reason 11565c80d60SJolly Shah */ 11665c80d60SJolly Shah enum pm_ret_status pm_ipi_send(const struct pm_proc *proc, 11765c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT]) 11865c80d60SJolly Shah { 11965c80d60SJolly Shah enum pm_ret_status ret; 12065c80d60SJolly Shah 12165c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 12265c80d60SJolly Shah 12365c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); 12465c80d60SJolly Shah 12565c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 12665c80d60SJolly Shah 12765c80d60SJolly Shah return ret; 12865c80d60SJolly Shah } 12965c80d60SJolly Shah 13065c80d60SJolly Shah 13165c80d60SJolly Shah /** 13265c80d60SJolly Shah * pm_ipi_buff_read() - Reads IPI response after remote processor has handled 13365c80d60SJolly Shah * interrupt 13465c80d60SJolly Shah * @proc Pointer to the processor who is waiting and reading response 13565c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 13665c80d60SJolly Shah * @count Number of values to return in @value 13765c80d60SJolly Shah * 13865c80d60SJolly Shah * @return Returns status, either success or error+reason 13965c80d60SJolly Shah */ 14065c80d60SJolly Shah static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc, 14165c80d60SJolly Shah unsigned int *value, size_t count) 14265c80d60SJolly Shah { 14365c80d60SJolly Shah size_t i; 144fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 145fe550edeSVenkatesh Yadav Abbarapu size_t j; 146fe550edeSVenkatesh Yadav Abbarapu unsigned int response_payload[PAYLOAD_ARG_CNT]; 147fe550edeSVenkatesh Yadav Abbarapu #endif 14865c80d60SJolly Shah uintptr_t buffer_base = proc->ipi->buffer_base + 14965c80d60SJolly Shah IPI_BUFFER_TARGET_REMOTE_OFFSET + 15065c80d60SJolly Shah IPI_BUFFER_RESP_OFFSET; 15165c80d60SJolly Shah 15265c80d60SJolly Shah /* 15365c80d60SJolly Shah * Read response from IPI buffer 15465c80d60SJolly Shah * buf-0: success or error+reason 15565c80d60SJolly Shah * buf-1: value 15665c80d60SJolly Shah * buf-2: unused 15765c80d60SJolly Shah * buf-3: unused 15865c80d60SJolly Shah */ 15965c80d60SJolly Shah for (i = 1; i <= count; i++) { 16065c80d60SJolly Shah *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); 16165c80d60SJolly Shah value++; 16265c80d60SJolly Shah } 163fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 164fe550edeSVenkatesh Yadav Abbarapu for (j = 0; j < PAYLOAD_ARG_CNT; j++) 165fe550edeSVenkatesh Yadav Abbarapu response_payload[j] = mmio_read_32(buffer_base + 166fe550edeSVenkatesh Yadav Abbarapu (j * PAYLOAD_ARG_SIZE)); 167fe550edeSVenkatesh Yadav Abbarapu 168fe550edeSVenkatesh Yadav Abbarapu if (response_payload[PAYLOAD_CRC_POS] != 169fe550edeSVenkatesh Yadav Abbarapu calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) 170fe550edeSVenkatesh Yadav Abbarapu NOTICE("ERROR in CRC response payload value:0x%x\n", 171fe550edeSVenkatesh Yadav Abbarapu response_payload[PAYLOAD_CRC_POS]); 172fe550edeSVenkatesh Yadav Abbarapu #endif 17365c80d60SJolly Shah 17465c80d60SJolly Shah return mmio_read_32(buffer_base); 17565c80d60SJolly Shah } 17665c80d60SJolly Shah 17765c80d60SJolly Shah /** 17865c80d60SJolly Shah * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has 17965c80d60SJolly Shah * handled interrupt 18065c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 18165c80d60SJolly Shah * @count Number of values to return in @value 18265c80d60SJolly Shah * 18365c80d60SJolly Shah * @return Returns status, either success or error+reason 18465c80d60SJolly Shah */ 18565c80d60SJolly Shah void pm_ipi_buff_read_callb(unsigned int *value, size_t count) 18665c80d60SJolly Shah { 18765c80d60SJolly Shah size_t i; 188fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 189fe550edeSVenkatesh Yadav Abbarapu size_t j; 190fe550edeSVenkatesh Yadav Abbarapu unsigned int response_payload[PAYLOAD_ARG_CNT]; 191fe550edeSVenkatesh Yadav Abbarapu #endif 19265c80d60SJolly Shah uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE + 19365c80d60SJolly Shah IPI_BUFFER_TARGET_LOCAL_OFFSET + 19465c80d60SJolly Shah IPI_BUFFER_REQ_OFFSET; 19565c80d60SJolly Shah 19665c80d60SJolly Shah if (count > IPI_BUFFER_MAX_WORDS) 19765c80d60SJolly Shah count = IPI_BUFFER_MAX_WORDS; 19865c80d60SJolly Shah 19965c80d60SJolly Shah for (i = 0; i <= count; i++) { 20065c80d60SJolly Shah *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); 20165c80d60SJolly Shah value++; 20265c80d60SJolly Shah } 203fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 204fe550edeSVenkatesh Yadav Abbarapu for (j = 0; j < PAYLOAD_ARG_CNT; j++) 205fe550edeSVenkatesh Yadav Abbarapu response_payload[j] = mmio_read_32(buffer_base + 206fe550edeSVenkatesh Yadav Abbarapu (j * PAYLOAD_ARG_SIZE)); 207fe550edeSVenkatesh Yadav Abbarapu 208fe550edeSVenkatesh Yadav Abbarapu if (response_payload[PAYLOAD_CRC_POS] != 209fe550edeSVenkatesh Yadav Abbarapu calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) 210fe550edeSVenkatesh Yadav Abbarapu NOTICE("ERROR in CRC response payload value:0x%x\n", 211fe550edeSVenkatesh Yadav Abbarapu response_payload[PAYLOAD_CRC_POS]); 212fe550edeSVenkatesh Yadav Abbarapu #endif 21365c80d60SJolly Shah } 21465c80d60SJolly Shah 21565c80d60SJolly Shah /** 21665c80d60SJolly Shah * pm_ipi_send_sync() - Sends IPI request to the remote processor 21765c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 21865c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 21965c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 22065c80d60SJolly Shah * @count Number of values to return in @value 22165c80d60SJolly Shah * 22265c80d60SJolly Shah * Send an IPI request to the power controller and wait for it to be handled. 22365c80d60SJolly Shah * 22465c80d60SJolly Shah * @return Returns status, either success or error+reason and, optionally, 22565c80d60SJolly Shah * @value 22665c80d60SJolly Shah */ 22765c80d60SJolly Shah enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc, 22865c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT], 22965c80d60SJolly Shah unsigned int *value, size_t count) 23065c80d60SJolly Shah { 23165c80d60SJolly Shah enum pm_ret_status ret; 23265c80d60SJolly Shah 23365c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 23465c80d60SJolly Shah 23565c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); 23665c80d60SJolly Shah if (ret != PM_RET_SUCCESS) 23765c80d60SJolly Shah goto unlock; 23865c80d60SJolly Shah 239addc4e96SRavi Patel ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count)); 24065c80d60SJolly Shah 24165c80d60SJolly Shah unlock: 24265c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 24365c80d60SJolly Shah 24465c80d60SJolly Shah return ret; 24565c80d60SJolly Shah } 24665c80d60SJolly Shah 24765c80d60SJolly Shah void pm_ipi_irq_enable(const struct pm_proc *proc) 24865c80d60SJolly Shah { 24965c80d60SJolly Shah ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); 25065c80d60SJolly Shah } 25165c80d60SJolly Shah 25265c80d60SJolly Shah void pm_ipi_irq_clear(const struct pm_proc *proc) 25365c80d60SJolly Shah { 25465c80d60SJolly Shah ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); 25565c80d60SJolly Shah } 25665c80d60SJolly Shah 25765c80d60SJolly Shah uint32_t pm_ipi_irq_status(const struct pm_proc *proc) 25865c80d60SJolly Shah { 25965c80d60SJolly Shah int ret; 26065c80d60SJolly Shah 26165c80d60SJolly Shah ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id, 26265c80d60SJolly Shah proc->ipi->remote_ipi_id); 26365c80d60SJolly Shah if (ret & IPI_MB_STATUS_RECV_PENDING) 26465c80d60SJolly Shah return 1; 26565c80d60SJolly Shah else 26665c80d60SJolly Shah return 0; 26765c80d60SJolly Shah } 268fe550edeSVenkatesh Yadav Abbarapu 269fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 270fe550edeSVenkatesh Yadav Abbarapu uint32_t calculate_crc(uint32_t *payload, uint32_t bufsize) 271fe550edeSVenkatesh Yadav Abbarapu { 272fe550edeSVenkatesh Yadav Abbarapu uint32_t crcinit = CRC_INIT_VALUE; 273fe550edeSVenkatesh Yadav Abbarapu uint32_t order = CRC_ORDER; 274fe550edeSVenkatesh Yadav Abbarapu uint32_t polynom = CRC_POLYNOM; 275fe550edeSVenkatesh Yadav Abbarapu uint32_t i, j, c, bit, datain, crcmask, crchighbit; 276fe550edeSVenkatesh Yadav Abbarapu uint32_t crc = crcinit; 277fe550edeSVenkatesh Yadav Abbarapu 278fe550edeSVenkatesh Yadav Abbarapu crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U; 279fe550edeSVenkatesh Yadav Abbarapu crchighbit = (uint32_t)(1U << (order - 1U)); 280fe550edeSVenkatesh Yadav Abbarapu 281fe550edeSVenkatesh Yadav Abbarapu for (i = 0U; i < bufsize; i++) { 282fe550edeSVenkatesh Yadav Abbarapu datain = mmio_read_8((unsigned long)payload + i); 283fe550edeSVenkatesh Yadav Abbarapu c = datain; 284fe550edeSVenkatesh Yadav Abbarapu j = 0x80U; 285fe550edeSVenkatesh Yadav Abbarapu while (j != 0U) { 286fe550edeSVenkatesh Yadav Abbarapu bit = crc & crchighbit; 287fe550edeSVenkatesh Yadav Abbarapu crc <<= 1U; 288fe550edeSVenkatesh Yadav Abbarapu if (0U != (c & j)) 289fe550edeSVenkatesh Yadav Abbarapu bit ^= crchighbit; 290fe550edeSVenkatesh Yadav Abbarapu if (bit != 0U) 291fe550edeSVenkatesh Yadav Abbarapu crc ^= polynom; 292fe550edeSVenkatesh Yadav Abbarapu j >>= 1U; 293fe550edeSVenkatesh Yadav Abbarapu } 294fe550edeSVenkatesh Yadav Abbarapu crc &= crcmask; 295fe550edeSVenkatesh Yadav Abbarapu } 296fe550edeSVenkatesh Yadav Abbarapu return crc; 297fe550edeSVenkatesh Yadav Abbarapu } 298fe550edeSVenkatesh Yadav Abbarapu #endif 299