165c80d60SJolly Shah /* 2*fe550edeSVenkatesh 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 2165c80d60SJolly Shah DEFINE_BAKERY_LOCK(pm_secure_lock); 2265c80d60SJolly Shah 2365c80d60SJolly Shah /** 2465c80d60SJolly Shah * pm_ipi_init() - Initialize IPI peripheral for communication with 2565c80d60SJolly Shah * remote processor 2665c80d60SJolly Shah * 2765c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 2865c80d60SJolly Shah * @return On success, the initialization function must return 0. 2965c80d60SJolly Shah * Any other return value will cause the framework to ignore 3065c80d60SJolly Shah * the service 3165c80d60SJolly Shah * 3265c80d60SJolly Shah * Called from pm_setup initialization function 3365c80d60SJolly Shah */ 3465c80d60SJolly Shah int pm_ipi_init(const struct pm_proc *proc) 3565c80d60SJolly Shah { 3665c80d60SJolly Shah bakery_lock_init(&pm_secure_lock); 3765c80d60SJolly Shah ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); 3865c80d60SJolly Shah 3965c80d60SJolly Shah return 0; 4065c80d60SJolly Shah } 4165c80d60SJolly Shah 4265c80d60SJolly Shah /** 4365c80d60SJolly Shah * pm_ipi_send_common() - Sends IPI request to the remote processor 4465c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 4565c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 4665c80d60SJolly Shah * 4765c80d60SJolly Shah * Send an IPI request to the power controller. Caller needs to hold 4865c80d60SJolly Shah * the 'pm_secure_lock' lock. 4965c80d60SJolly Shah * 5065c80d60SJolly Shah * @return Returns status, either success or error+reason 5165c80d60SJolly Shah */ 5265c80d60SJolly Shah static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc, 5365c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT], 5465c80d60SJolly Shah uint32_t is_blocking) 5565c80d60SJolly Shah { 5665c80d60SJolly Shah unsigned int offset = 0; 5765c80d60SJolly Shah uintptr_t buffer_base = proc->ipi->buffer_base + 5865c80d60SJolly Shah IPI_BUFFER_TARGET_REMOTE_OFFSET + 5965c80d60SJolly Shah IPI_BUFFER_REQ_OFFSET; 60*fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 61*fe550edeSVenkatesh Yadav Abbarapu payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE); 62*fe550edeSVenkatesh Yadav Abbarapu #endif 6365c80d60SJolly Shah 6465c80d60SJolly Shah /* Write payload into IPI buffer */ 6565c80d60SJolly Shah for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) { 6665c80d60SJolly Shah mmio_write_32(buffer_base + offset, payload[i]); 6765c80d60SJolly Shah offset += PAYLOAD_ARG_SIZE; 6865c80d60SJolly Shah } 6965c80d60SJolly Shah 7065c80d60SJolly Shah /* Generate IPI to remote processor */ 7165c80d60SJolly Shah ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id, 7265c80d60SJolly Shah is_blocking); 7365c80d60SJolly Shah 7465c80d60SJolly Shah return PM_RET_SUCCESS; 7565c80d60SJolly Shah } 7665c80d60SJolly Shah 7765c80d60SJolly Shah /** 7865c80d60SJolly Shah * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor 7965c80d60SJolly Shah * without blocking notification 8065c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 8165c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 8265c80d60SJolly Shah * 8365c80d60SJolly Shah * Send an IPI request to the power controller. 8465c80d60SJolly Shah * 8565c80d60SJolly Shah * @return Returns status, either success or error+reason 8665c80d60SJolly Shah */ 8765c80d60SJolly Shah enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc, 8865c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT]) 8965c80d60SJolly Shah { 9065c80d60SJolly Shah enum pm_ret_status ret; 9165c80d60SJolly Shah 9265c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 9365c80d60SJolly Shah 9465c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING); 9565c80d60SJolly Shah 9665c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 9765c80d60SJolly Shah 9865c80d60SJolly Shah return ret; 9965c80d60SJolly Shah } 10065c80d60SJolly Shah 10165c80d60SJolly Shah /** 10265c80d60SJolly Shah * pm_ipi_send() - Sends IPI request to the remote processor 10365c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 10465c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 10565c80d60SJolly Shah * 10665c80d60SJolly Shah * Send an IPI request to the power controller. 10765c80d60SJolly Shah * 10865c80d60SJolly Shah * @return Returns status, either success or error+reason 10965c80d60SJolly Shah */ 11065c80d60SJolly Shah enum pm_ret_status pm_ipi_send(const struct pm_proc *proc, 11165c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT]) 11265c80d60SJolly Shah { 11365c80d60SJolly Shah enum pm_ret_status ret; 11465c80d60SJolly Shah 11565c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 11665c80d60SJolly Shah 11765c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); 11865c80d60SJolly Shah 11965c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 12065c80d60SJolly Shah 12165c80d60SJolly Shah return ret; 12265c80d60SJolly Shah } 12365c80d60SJolly Shah 12465c80d60SJolly Shah 12565c80d60SJolly Shah /** 12665c80d60SJolly Shah * pm_ipi_buff_read() - Reads IPI response after remote processor has handled 12765c80d60SJolly Shah * interrupt 12865c80d60SJolly Shah * @proc Pointer to the processor who is waiting and reading response 12965c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 13065c80d60SJolly Shah * @count Number of values to return in @value 13165c80d60SJolly Shah * 13265c80d60SJolly Shah * @return Returns status, either success or error+reason 13365c80d60SJolly Shah */ 13465c80d60SJolly Shah static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc, 13565c80d60SJolly Shah unsigned int *value, size_t count) 13665c80d60SJolly Shah { 13765c80d60SJolly Shah size_t i; 138*fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 139*fe550edeSVenkatesh Yadav Abbarapu size_t j; 140*fe550edeSVenkatesh Yadav Abbarapu unsigned int response_payload[PAYLOAD_ARG_CNT]; 141*fe550edeSVenkatesh Yadav Abbarapu #endif 14265c80d60SJolly Shah uintptr_t buffer_base = proc->ipi->buffer_base + 14365c80d60SJolly Shah IPI_BUFFER_TARGET_REMOTE_OFFSET + 14465c80d60SJolly Shah IPI_BUFFER_RESP_OFFSET; 14565c80d60SJolly Shah 14665c80d60SJolly Shah /* 14765c80d60SJolly Shah * Read response from IPI buffer 14865c80d60SJolly Shah * buf-0: success or error+reason 14965c80d60SJolly Shah * buf-1: value 15065c80d60SJolly Shah * buf-2: unused 15165c80d60SJolly Shah * buf-3: unused 15265c80d60SJolly Shah */ 15365c80d60SJolly Shah for (i = 1; i <= count; i++) { 15465c80d60SJolly Shah *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); 15565c80d60SJolly Shah value++; 15665c80d60SJolly Shah } 157*fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 158*fe550edeSVenkatesh Yadav Abbarapu for (j = 0; j < PAYLOAD_ARG_CNT; j++) 159*fe550edeSVenkatesh Yadav Abbarapu response_payload[j] = mmio_read_32(buffer_base + 160*fe550edeSVenkatesh Yadav Abbarapu (j * PAYLOAD_ARG_SIZE)); 161*fe550edeSVenkatesh Yadav Abbarapu 162*fe550edeSVenkatesh Yadav Abbarapu if (response_payload[PAYLOAD_CRC_POS] != 163*fe550edeSVenkatesh Yadav Abbarapu calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) 164*fe550edeSVenkatesh Yadav Abbarapu NOTICE("ERROR in CRC response payload value:0x%x\n", 165*fe550edeSVenkatesh Yadav Abbarapu response_payload[PAYLOAD_CRC_POS]); 166*fe550edeSVenkatesh Yadav Abbarapu #endif 16765c80d60SJolly Shah 16865c80d60SJolly Shah return mmio_read_32(buffer_base); 16965c80d60SJolly Shah } 17065c80d60SJolly Shah 17165c80d60SJolly Shah /** 17265c80d60SJolly Shah * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has 17365c80d60SJolly Shah * handled interrupt 17465c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 17565c80d60SJolly Shah * @count Number of values to return in @value 17665c80d60SJolly Shah * 17765c80d60SJolly Shah * @return Returns status, either success or error+reason 17865c80d60SJolly Shah */ 17965c80d60SJolly Shah void pm_ipi_buff_read_callb(unsigned int *value, size_t count) 18065c80d60SJolly Shah { 18165c80d60SJolly Shah size_t i; 182*fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 183*fe550edeSVenkatesh Yadav Abbarapu size_t j; 184*fe550edeSVenkatesh Yadav Abbarapu unsigned int response_payload[PAYLOAD_ARG_CNT]; 185*fe550edeSVenkatesh Yadav Abbarapu #endif 18665c80d60SJolly Shah uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE + 18765c80d60SJolly Shah IPI_BUFFER_TARGET_LOCAL_OFFSET + 18865c80d60SJolly Shah IPI_BUFFER_REQ_OFFSET; 18965c80d60SJolly Shah 19065c80d60SJolly Shah if (count > IPI_BUFFER_MAX_WORDS) 19165c80d60SJolly Shah count = IPI_BUFFER_MAX_WORDS; 19265c80d60SJolly Shah 19365c80d60SJolly Shah for (i = 0; i <= count; i++) { 19465c80d60SJolly Shah *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); 19565c80d60SJolly Shah value++; 19665c80d60SJolly Shah } 197*fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 198*fe550edeSVenkatesh Yadav Abbarapu for (j = 0; j < PAYLOAD_ARG_CNT; j++) 199*fe550edeSVenkatesh Yadav Abbarapu response_payload[j] = mmio_read_32(buffer_base + 200*fe550edeSVenkatesh Yadav Abbarapu (j * PAYLOAD_ARG_SIZE)); 201*fe550edeSVenkatesh Yadav Abbarapu 202*fe550edeSVenkatesh Yadav Abbarapu if (response_payload[PAYLOAD_CRC_POS] != 203*fe550edeSVenkatesh Yadav Abbarapu calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) 204*fe550edeSVenkatesh Yadav Abbarapu NOTICE("ERROR in CRC response payload value:0x%x\n", 205*fe550edeSVenkatesh Yadav Abbarapu response_payload[PAYLOAD_CRC_POS]); 206*fe550edeSVenkatesh Yadav Abbarapu #endif 20765c80d60SJolly Shah } 20865c80d60SJolly Shah 20965c80d60SJolly Shah /** 21065c80d60SJolly Shah * pm_ipi_send_sync() - Sends IPI request to the remote processor 21165c80d60SJolly Shah * @proc Pointer to the processor who is initiating request 21265c80d60SJolly Shah * @payload API id and call arguments to be written in IPI buffer 21365c80d60SJolly Shah * @value Used to return value from IPI buffer element (optional) 21465c80d60SJolly Shah * @count Number of values to return in @value 21565c80d60SJolly Shah * 21665c80d60SJolly Shah * Send an IPI request to the power controller and wait for it to be handled. 21765c80d60SJolly Shah * 21865c80d60SJolly Shah * @return Returns status, either success or error+reason and, optionally, 21965c80d60SJolly Shah * @value 22065c80d60SJolly Shah */ 22165c80d60SJolly Shah enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc, 22265c80d60SJolly Shah uint32_t payload[PAYLOAD_ARG_CNT], 22365c80d60SJolly Shah unsigned int *value, size_t count) 22465c80d60SJolly Shah { 22565c80d60SJolly Shah enum pm_ret_status ret; 22665c80d60SJolly Shah 22765c80d60SJolly Shah bakery_lock_get(&pm_secure_lock); 22865c80d60SJolly Shah 22965c80d60SJolly Shah ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); 23065c80d60SJolly Shah if (ret != PM_RET_SUCCESS) 23165c80d60SJolly Shah goto unlock; 23265c80d60SJolly Shah 23365c80d60SJolly Shah ret = pm_ipi_buff_read(proc, value, count); 23465c80d60SJolly Shah 23565c80d60SJolly Shah unlock: 23665c80d60SJolly Shah bakery_lock_release(&pm_secure_lock); 23765c80d60SJolly Shah 23865c80d60SJolly Shah return ret; 23965c80d60SJolly Shah } 24065c80d60SJolly Shah 24165c80d60SJolly Shah void pm_ipi_irq_enable(const struct pm_proc *proc) 24265c80d60SJolly Shah { 24365c80d60SJolly Shah ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); 24465c80d60SJolly Shah } 24565c80d60SJolly Shah 24665c80d60SJolly Shah void pm_ipi_irq_clear(const struct pm_proc *proc) 24765c80d60SJolly Shah { 24865c80d60SJolly Shah ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); 24965c80d60SJolly Shah } 25065c80d60SJolly Shah 25165c80d60SJolly Shah uint32_t pm_ipi_irq_status(const struct pm_proc *proc) 25265c80d60SJolly Shah { 25365c80d60SJolly Shah int ret; 25465c80d60SJolly Shah 25565c80d60SJolly Shah ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id, 25665c80d60SJolly Shah proc->ipi->remote_ipi_id); 25765c80d60SJolly Shah if (ret & IPI_MB_STATUS_RECV_PENDING) 25865c80d60SJolly Shah return 1; 25965c80d60SJolly Shah else 26065c80d60SJolly Shah return 0; 26165c80d60SJolly Shah } 262*fe550edeSVenkatesh Yadav Abbarapu 263*fe550edeSVenkatesh Yadav Abbarapu #if ZYNQMP_IPI_CRC_CHECK 264*fe550edeSVenkatesh Yadav Abbarapu uint32_t calculate_crc(uint32_t *payload, uint32_t bufsize) 265*fe550edeSVenkatesh Yadav Abbarapu { 266*fe550edeSVenkatesh Yadav Abbarapu uint32_t crcinit = CRC_INIT_VALUE; 267*fe550edeSVenkatesh Yadav Abbarapu uint32_t order = CRC_ORDER; 268*fe550edeSVenkatesh Yadav Abbarapu uint32_t polynom = CRC_POLYNOM; 269*fe550edeSVenkatesh Yadav Abbarapu uint32_t i, j, c, bit, datain, crcmask, crchighbit; 270*fe550edeSVenkatesh Yadav Abbarapu uint32_t crc = crcinit; 271*fe550edeSVenkatesh Yadav Abbarapu 272*fe550edeSVenkatesh Yadav Abbarapu crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U; 273*fe550edeSVenkatesh Yadav Abbarapu crchighbit = (uint32_t)(1U << (order - 1U)); 274*fe550edeSVenkatesh Yadav Abbarapu 275*fe550edeSVenkatesh Yadav Abbarapu for (i = 0U; i < bufsize; i++) { 276*fe550edeSVenkatesh Yadav Abbarapu datain = mmio_read_8((unsigned long)payload + i); 277*fe550edeSVenkatesh Yadav Abbarapu c = datain; 278*fe550edeSVenkatesh Yadav Abbarapu j = 0x80U; 279*fe550edeSVenkatesh Yadav Abbarapu while (j != 0U) { 280*fe550edeSVenkatesh Yadav Abbarapu bit = crc & crchighbit; 281*fe550edeSVenkatesh Yadav Abbarapu crc <<= 1U; 282*fe550edeSVenkatesh Yadav Abbarapu if (0U != (c & j)) 283*fe550edeSVenkatesh Yadav Abbarapu bit ^= crchighbit; 284*fe550edeSVenkatesh Yadav Abbarapu if (bit != 0U) 285*fe550edeSVenkatesh Yadav Abbarapu crc ^= polynom; 286*fe550edeSVenkatesh Yadav Abbarapu j >>= 1U; 287*fe550edeSVenkatesh Yadav Abbarapu } 288*fe550edeSVenkatesh Yadav Abbarapu crc &= crcmask; 289*fe550edeSVenkatesh Yadav Abbarapu } 290*fe550edeSVenkatesh Yadav Abbarapu return crc; 291*fe550edeSVenkatesh Yadav Abbarapu } 292*fe550edeSVenkatesh Yadav Abbarapu #endif 293