18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2013-2020, Intel Corporation. All rights reserved.
48c2ecf20Sopenharmony_ci * Intel Management Engine Interface (Intel MEI) Linux driver
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/pci.h>
88c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
98c2ecf20Sopenharmony_ci#include <linux/ktime.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/kthread.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/mei.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "mei_dev.h"
188c2ecf20Sopenharmony_ci#include "hw-txe.h"
198c2ecf20Sopenharmony_ci#include "client.h"
208c2ecf20Sopenharmony_ci#include "hbm.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "mei-trace.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define TXE_HBUF_DEPTH (PAYLOAD_SIZE / MEI_SLOT_SIZE)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/**
278c2ecf20Sopenharmony_ci * mei_txe_reg_read - Reads 32bit data from the txe device
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * @base_addr: registers base address
308c2ecf20Sopenharmony_ci * @offset: register offset
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * Return: register value
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_cistatic inline u32 mei_txe_reg_read(void __iomem *base_addr,
358c2ecf20Sopenharmony_ci					unsigned long offset)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	return ioread32(base_addr + offset);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/**
418c2ecf20Sopenharmony_ci * mei_txe_reg_write - Writes 32bit data to the txe device
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * @base_addr: registers base address
448c2ecf20Sopenharmony_ci * @offset: register offset
458c2ecf20Sopenharmony_ci * @value: the value to write
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistatic inline void mei_txe_reg_write(void __iomem *base_addr,
488c2ecf20Sopenharmony_ci				unsigned long offset, u32 value)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	iowrite32(value, base_addr + offset);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/**
548c2ecf20Sopenharmony_ci * mei_txe_sec_reg_read_silent - Reads 32bit data from the SeC BAR
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * @hw: the txe hardware structure
578c2ecf20Sopenharmony_ci * @offset: register offset
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * Doesn't check for aliveness while Reads 32bit data from the SeC BAR
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * Return: register value
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_cistatic inline u32 mei_txe_sec_reg_read_silent(struct mei_txe_hw *hw,
648c2ecf20Sopenharmony_ci				unsigned long offset)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	return mei_txe_reg_read(hw->mem_addr[SEC_BAR], offset);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/**
708c2ecf20Sopenharmony_ci * mei_txe_sec_reg_read - Reads 32bit data from the SeC BAR
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * @hw: the txe hardware structure
738c2ecf20Sopenharmony_ci * @offset: register offset
748c2ecf20Sopenharmony_ci *
758c2ecf20Sopenharmony_ci * Reads 32bit data from the SeC BAR and shout loud if aliveness is not set
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * Return: register value
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_cistatic inline u32 mei_txe_sec_reg_read(struct mei_txe_hw *hw,
808c2ecf20Sopenharmony_ci				unsigned long offset)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	WARN(!hw->aliveness, "sec read: aliveness not asserted\n");
838c2ecf20Sopenharmony_ci	return mei_txe_sec_reg_read_silent(hw, offset);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci/**
868c2ecf20Sopenharmony_ci * mei_txe_sec_reg_write_silent - Writes 32bit data to the SeC BAR
878c2ecf20Sopenharmony_ci *   doesn't check for aliveness
888c2ecf20Sopenharmony_ci *
898c2ecf20Sopenharmony_ci * @hw: the txe hardware structure
908c2ecf20Sopenharmony_ci * @offset: register offset
918c2ecf20Sopenharmony_ci * @value: value to write
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * Doesn't check for aliveness while writes 32bit data from to the SeC BAR
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_cistatic inline void mei_txe_sec_reg_write_silent(struct mei_txe_hw *hw,
968c2ecf20Sopenharmony_ci				unsigned long offset, u32 value)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	mei_txe_reg_write(hw->mem_addr[SEC_BAR], offset, value);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/**
1028c2ecf20Sopenharmony_ci * mei_txe_sec_reg_write - Writes 32bit data to the SeC BAR
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci * @hw: the txe hardware structure
1058c2ecf20Sopenharmony_ci * @offset: register offset
1068c2ecf20Sopenharmony_ci * @value: value to write
1078c2ecf20Sopenharmony_ci *
1088c2ecf20Sopenharmony_ci * Writes 32bit data from the SeC BAR and shout loud if aliveness is not set
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_cistatic inline void mei_txe_sec_reg_write(struct mei_txe_hw *hw,
1118c2ecf20Sopenharmony_ci				unsigned long offset, u32 value)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	WARN(!hw->aliveness, "sec write: aliveness not asserted\n");
1148c2ecf20Sopenharmony_ci	mei_txe_sec_reg_write_silent(hw, offset, value);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci/**
1178c2ecf20Sopenharmony_ci * mei_txe_br_reg_read - Reads 32bit data from the Bridge BAR
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci * @hw: the txe hardware structure
1208c2ecf20Sopenharmony_ci * @offset: offset from which to read the data
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * Return: the byte read.
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_cistatic inline u32 mei_txe_br_reg_read(struct mei_txe_hw *hw,
1258c2ecf20Sopenharmony_ci				unsigned long offset)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	return mei_txe_reg_read(hw->mem_addr[BRIDGE_BAR], offset);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/**
1318c2ecf20Sopenharmony_ci * mei_txe_br_reg_write - Writes 32bit data to the Bridge BAR
1328c2ecf20Sopenharmony_ci *
1338c2ecf20Sopenharmony_ci * @hw: the txe hardware structure
1348c2ecf20Sopenharmony_ci * @offset: offset from which to write the data
1358c2ecf20Sopenharmony_ci * @value: the byte to write
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_cistatic inline void mei_txe_br_reg_write(struct mei_txe_hw *hw,
1388c2ecf20Sopenharmony_ci				unsigned long offset, u32 value)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	mei_txe_reg_write(hw->mem_addr[BRIDGE_BAR], offset, value);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/**
1448c2ecf20Sopenharmony_ci * mei_txe_aliveness_set - request for aliveness change
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * @dev: the device structure
1478c2ecf20Sopenharmony_ci * @req: requested aliveness value
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci * Request for aliveness change and returns true if the change is
1508c2ecf20Sopenharmony_ci *   really needed and false if aliveness is already
1518c2ecf20Sopenharmony_ci *   in the requested state
1528c2ecf20Sopenharmony_ci *
1538c2ecf20Sopenharmony_ci * Locking: called under "dev->device_lock" lock
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * Return: true if request was send
1568c2ecf20Sopenharmony_ci */
1578c2ecf20Sopenharmony_cistatic bool mei_txe_aliveness_set(struct mei_device *dev, u32 req)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
1618c2ecf20Sopenharmony_ci	bool do_req = hw->aliveness != req;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "Aliveness current=%d request=%d\n",
1648c2ecf20Sopenharmony_ci				hw->aliveness, req);
1658c2ecf20Sopenharmony_ci	if (do_req) {
1668c2ecf20Sopenharmony_ci		dev->pg_event = MEI_PG_EVENT_WAIT;
1678c2ecf20Sopenharmony_ci		mei_txe_br_reg_write(hw, SICR_HOST_ALIVENESS_REQ_REG, req);
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci	return do_req;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/**
1748c2ecf20Sopenharmony_ci * mei_txe_aliveness_req_get - get aliveness requested register value
1758c2ecf20Sopenharmony_ci *
1768c2ecf20Sopenharmony_ci * @dev: the device structure
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * Extract HICR_HOST_ALIVENESS_RESP_ACK bit from
1798c2ecf20Sopenharmony_ci * from HICR_HOST_ALIVENESS_REQ register value
1808c2ecf20Sopenharmony_ci *
1818c2ecf20Sopenharmony_ci * Return: SICR_HOST_ALIVENESS_REQ_REQUESTED bit value
1828c2ecf20Sopenharmony_ci */
1838c2ecf20Sopenharmony_cistatic u32 mei_txe_aliveness_req_get(struct mei_device *dev)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
1868c2ecf20Sopenharmony_ci	u32 reg;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	reg = mei_txe_br_reg_read(hw, SICR_HOST_ALIVENESS_REQ_REG);
1898c2ecf20Sopenharmony_ci	return reg & SICR_HOST_ALIVENESS_REQ_REQUESTED;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/**
1938c2ecf20Sopenharmony_ci * mei_txe_aliveness_get - get aliveness response register value
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * @dev: the device structure
1968c2ecf20Sopenharmony_ci *
1978c2ecf20Sopenharmony_ci * Return: HICR_HOST_ALIVENESS_RESP_ACK bit from HICR_HOST_ALIVENESS_RESP
1988c2ecf20Sopenharmony_ci *         register
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_cistatic u32 mei_txe_aliveness_get(struct mei_device *dev)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
2038c2ecf20Sopenharmony_ci	u32 reg;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	reg = mei_txe_br_reg_read(hw, HICR_HOST_ALIVENESS_RESP_REG);
2068c2ecf20Sopenharmony_ci	return reg & HICR_HOST_ALIVENESS_RESP_ACK;
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci/**
2108c2ecf20Sopenharmony_ci * mei_txe_aliveness_poll - waits for aliveness to settle
2118c2ecf20Sopenharmony_ci *
2128c2ecf20Sopenharmony_ci * @dev: the device structure
2138c2ecf20Sopenharmony_ci * @expected: expected aliveness value
2148c2ecf20Sopenharmony_ci *
2158c2ecf20Sopenharmony_ci * Polls for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * Return: 0 if the expected value was received, -ETIME otherwise
2188c2ecf20Sopenharmony_ci */
2198c2ecf20Sopenharmony_cistatic int mei_txe_aliveness_poll(struct mei_device *dev, u32 expected)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
2228c2ecf20Sopenharmony_ci	ktime_t stop, start;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	start = ktime_get();
2258c2ecf20Sopenharmony_ci	stop = ktime_add(start, ms_to_ktime(SEC_ALIVENESS_WAIT_TIMEOUT));
2268c2ecf20Sopenharmony_ci	do {
2278c2ecf20Sopenharmony_ci		hw->aliveness = mei_txe_aliveness_get(dev);
2288c2ecf20Sopenharmony_ci		if (hw->aliveness == expected) {
2298c2ecf20Sopenharmony_ci			dev->pg_event = MEI_PG_EVENT_IDLE;
2308c2ecf20Sopenharmony_ci			dev_dbg(dev->dev, "aliveness settled after %lld usecs\n",
2318c2ecf20Sopenharmony_ci				ktime_to_us(ktime_sub(ktime_get(), start)));
2328c2ecf20Sopenharmony_ci			return 0;
2338c2ecf20Sopenharmony_ci		}
2348c2ecf20Sopenharmony_ci		usleep_range(20, 50);
2358c2ecf20Sopenharmony_ci	} while (ktime_before(ktime_get(), stop));
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	dev->pg_event = MEI_PG_EVENT_IDLE;
2388c2ecf20Sopenharmony_ci	dev_err(dev->dev, "aliveness timed out\n");
2398c2ecf20Sopenharmony_ci	return -ETIME;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci/**
2438c2ecf20Sopenharmony_ci * mei_txe_aliveness_wait - waits for aliveness to settle
2448c2ecf20Sopenharmony_ci *
2458c2ecf20Sopenharmony_ci * @dev: the device structure
2468c2ecf20Sopenharmony_ci * @expected: expected aliveness value
2478c2ecf20Sopenharmony_ci *
2488c2ecf20Sopenharmony_ci * Waits for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
2498c2ecf20Sopenharmony_ci *
2508c2ecf20Sopenharmony_ci * Return: 0 on success and < 0 otherwise
2518c2ecf20Sopenharmony_ci */
2528c2ecf20Sopenharmony_cistatic int mei_txe_aliveness_wait(struct mei_device *dev, u32 expected)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
2558c2ecf20Sopenharmony_ci	const unsigned long timeout =
2568c2ecf20Sopenharmony_ci			msecs_to_jiffies(SEC_ALIVENESS_WAIT_TIMEOUT);
2578c2ecf20Sopenharmony_ci	long err;
2588c2ecf20Sopenharmony_ci	int ret;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	hw->aliveness = mei_txe_aliveness_get(dev);
2618c2ecf20Sopenharmony_ci	if (hw->aliveness == expected)
2628c2ecf20Sopenharmony_ci		return 0;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	mutex_unlock(&dev->device_lock);
2658c2ecf20Sopenharmony_ci	err = wait_event_timeout(hw->wait_aliveness_resp,
2668c2ecf20Sopenharmony_ci			dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
2678c2ecf20Sopenharmony_ci	mutex_lock(&dev->device_lock);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	hw->aliveness = mei_txe_aliveness_get(dev);
2708c2ecf20Sopenharmony_ci	ret = hw->aliveness == expected ? 0 : -ETIME;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if (ret)
2738c2ecf20Sopenharmony_ci		dev_warn(dev->dev, "aliveness timed out = %ld aliveness = %d event = %d\n",
2748c2ecf20Sopenharmony_ci			err, hw->aliveness, dev->pg_event);
2758c2ecf20Sopenharmony_ci	else
2768c2ecf20Sopenharmony_ci		dev_dbg(dev->dev, "aliveness settled after = %d msec aliveness = %d event = %d\n",
2778c2ecf20Sopenharmony_ci			jiffies_to_msecs(timeout - err),
2788c2ecf20Sopenharmony_ci			hw->aliveness, dev->pg_event);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	dev->pg_event = MEI_PG_EVENT_IDLE;
2818c2ecf20Sopenharmony_ci	return ret;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/**
2858c2ecf20Sopenharmony_ci * mei_txe_aliveness_set_sync - sets an wait for aliveness to complete
2868c2ecf20Sopenharmony_ci *
2878c2ecf20Sopenharmony_ci * @dev: the device structure
2888c2ecf20Sopenharmony_ci * @req: requested aliveness value
2898c2ecf20Sopenharmony_ci *
2908c2ecf20Sopenharmony_ci * Return: 0 on success and < 0 otherwise
2918c2ecf20Sopenharmony_ci */
2928c2ecf20Sopenharmony_ciint mei_txe_aliveness_set_sync(struct mei_device *dev, u32 req)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	if (mei_txe_aliveness_set(dev, req))
2958c2ecf20Sopenharmony_ci		return mei_txe_aliveness_wait(dev, req);
2968c2ecf20Sopenharmony_ci	return 0;
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci/**
3008c2ecf20Sopenharmony_ci * mei_txe_pg_in_transition - is device now in pg transition
3018c2ecf20Sopenharmony_ci *
3028c2ecf20Sopenharmony_ci * @dev: the device structure
3038c2ecf20Sopenharmony_ci *
3048c2ecf20Sopenharmony_ci * Return: true if in pg transition, false otherwise
3058c2ecf20Sopenharmony_ci */
3068c2ecf20Sopenharmony_cistatic bool mei_txe_pg_in_transition(struct mei_device *dev)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	return dev->pg_event == MEI_PG_EVENT_WAIT;
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci/**
3128c2ecf20Sopenharmony_ci * mei_txe_pg_is_enabled - detect if PG is supported by HW
3138c2ecf20Sopenharmony_ci *
3148c2ecf20Sopenharmony_ci * @dev: the device structure
3158c2ecf20Sopenharmony_ci *
3168c2ecf20Sopenharmony_ci * Return: true is pg supported, false otherwise
3178c2ecf20Sopenharmony_ci */
3188c2ecf20Sopenharmony_cistatic bool mei_txe_pg_is_enabled(struct mei_device *dev)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	return true;
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci/**
3248c2ecf20Sopenharmony_ci * mei_txe_pg_state  - translate aliveness register value
3258c2ecf20Sopenharmony_ci *   to the mei power gating state
3268c2ecf20Sopenharmony_ci *
3278c2ecf20Sopenharmony_ci * @dev: the device structure
3288c2ecf20Sopenharmony_ci *
3298c2ecf20Sopenharmony_ci * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
3308c2ecf20Sopenharmony_ci */
3318c2ecf20Sopenharmony_cistatic inline enum mei_pg_state mei_txe_pg_state(struct mei_device *dev)
3328c2ecf20Sopenharmony_ci{
3338c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	return hw->aliveness ? MEI_PG_OFF : MEI_PG_ON;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci/**
3398c2ecf20Sopenharmony_ci * mei_txe_input_ready_interrupt_enable - sets the Input Ready Interrupt
3408c2ecf20Sopenharmony_ci *
3418c2ecf20Sopenharmony_ci * @dev: the device structure
3428c2ecf20Sopenharmony_ci */
3438c2ecf20Sopenharmony_cistatic void mei_txe_input_ready_interrupt_enable(struct mei_device *dev)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
3468c2ecf20Sopenharmony_ci	u32 hintmsk;
3478c2ecf20Sopenharmony_ci	/* Enable the SEC_IPC_HOST_INT_MASK_IN_RDY interrupt */
3488c2ecf20Sopenharmony_ci	hintmsk = mei_txe_sec_reg_read(hw, SEC_IPC_HOST_INT_MASK_REG);
3498c2ecf20Sopenharmony_ci	hintmsk |= SEC_IPC_HOST_INT_MASK_IN_RDY;
3508c2ecf20Sopenharmony_ci	mei_txe_sec_reg_write(hw, SEC_IPC_HOST_INT_MASK_REG, hintmsk);
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci/**
3548c2ecf20Sopenharmony_ci * mei_txe_input_doorbell_set - sets bit 0 in
3558c2ecf20Sopenharmony_ci *    SEC_IPC_INPUT_DOORBELL.IPC_INPUT_DOORBELL.
3568c2ecf20Sopenharmony_ci *
3578c2ecf20Sopenharmony_ci * @hw: the txe hardware structure
3588c2ecf20Sopenharmony_ci */
3598c2ecf20Sopenharmony_cistatic void mei_txe_input_doorbell_set(struct mei_txe_hw *hw)
3608c2ecf20Sopenharmony_ci{
3618c2ecf20Sopenharmony_ci	/* Clear the interrupt cause */
3628c2ecf20Sopenharmony_ci	clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause);
3638c2ecf20Sopenharmony_ci	mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_DOORBELL_REG, 1);
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci/**
3678c2ecf20Sopenharmony_ci * mei_txe_output_ready_set - Sets the SICR_SEC_IPC_OUTPUT_STATUS bit to 1
3688c2ecf20Sopenharmony_ci *
3698c2ecf20Sopenharmony_ci * @hw: the txe hardware structure
3708c2ecf20Sopenharmony_ci */
3718c2ecf20Sopenharmony_cistatic void mei_txe_output_ready_set(struct mei_txe_hw *hw)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw,
3748c2ecf20Sopenharmony_ci			SICR_SEC_IPC_OUTPUT_STATUS_REG,
3758c2ecf20Sopenharmony_ci			SEC_IPC_OUTPUT_STATUS_RDY);
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci/**
3798c2ecf20Sopenharmony_ci * mei_txe_is_input_ready - check if TXE is ready for receiving data
3808c2ecf20Sopenharmony_ci *
3818c2ecf20Sopenharmony_ci * @dev: the device structure
3828c2ecf20Sopenharmony_ci *
3838c2ecf20Sopenharmony_ci * Return: true if INPUT STATUS READY bit is set
3848c2ecf20Sopenharmony_ci */
3858c2ecf20Sopenharmony_cistatic bool mei_txe_is_input_ready(struct mei_device *dev)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
3888c2ecf20Sopenharmony_ci	u32 status;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	status = mei_txe_sec_reg_read(hw, SEC_IPC_INPUT_STATUS_REG);
3918c2ecf20Sopenharmony_ci	return !!(SEC_IPC_INPUT_STATUS_RDY & status);
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci/**
3958c2ecf20Sopenharmony_ci * mei_txe_intr_clear - clear all interrupts
3968c2ecf20Sopenharmony_ci *
3978c2ecf20Sopenharmony_ci * @dev: the device structure
3988c2ecf20Sopenharmony_ci */
3998c2ecf20Sopenharmony_cistatic inline void mei_txe_intr_clear(struct mei_device *dev)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	mei_txe_sec_reg_write_silent(hw, SEC_IPC_HOST_INT_STATUS_REG,
4048c2ecf20Sopenharmony_ci		SEC_IPC_HOST_INT_STATUS_PENDING);
4058c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_STS_MSK);
4068c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, HHISR_REG, IPC_HHIER_MSK);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci/**
4108c2ecf20Sopenharmony_ci * mei_txe_intr_disable - disable all interrupts
4118c2ecf20Sopenharmony_ci *
4128c2ecf20Sopenharmony_ci * @dev: the device structure
4138c2ecf20Sopenharmony_ci */
4148c2ecf20Sopenharmony_cistatic void mei_txe_intr_disable(struct mei_device *dev)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, HHIER_REG, 0);
4198c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, HIER_REG, 0);
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci/**
4228c2ecf20Sopenharmony_ci * mei_txe_intr_enable - enable all interrupts
4238c2ecf20Sopenharmony_ci *
4248c2ecf20Sopenharmony_ci * @dev: the device structure
4258c2ecf20Sopenharmony_ci */
4268c2ecf20Sopenharmony_cistatic void mei_txe_intr_enable(struct mei_device *dev)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, HHIER_REG, IPC_HHIER_MSK);
4318c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, HIER_REG, HIER_INT_EN_MSK);
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci/**
4358c2ecf20Sopenharmony_ci * mei_txe_synchronize_irq - wait for pending IRQ handlers
4368c2ecf20Sopenharmony_ci *
4378c2ecf20Sopenharmony_ci * @dev: the device structure
4388c2ecf20Sopenharmony_ci */
4398c2ecf20Sopenharmony_cistatic void mei_txe_synchronize_irq(struct mei_device *dev)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev->dev);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	synchronize_irq(pdev->irq);
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci/**
4478c2ecf20Sopenharmony_ci * mei_txe_pending_interrupts - check if there are pending interrupts
4488c2ecf20Sopenharmony_ci *	only Aliveness, Input ready, and output doorbell are of relevance
4498c2ecf20Sopenharmony_ci *
4508c2ecf20Sopenharmony_ci * @dev: the device structure
4518c2ecf20Sopenharmony_ci *
4528c2ecf20Sopenharmony_ci * Checks if there are pending interrupts
4538c2ecf20Sopenharmony_ci * only Aliveness, Readiness, Input ready, and Output doorbell are relevant
4548c2ecf20Sopenharmony_ci *
4558c2ecf20Sopenharmony_ci * Return: true if there are pending interrupts
4568c2ecf20Sopenharmony_ci */
4578c2ecf20Sopenharmony_cistatic bool mei_txe_pending_interrupts(struct mei_device *dev)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
4618c2ecf20Sopenharmony_ci	bool ret = (hw->intr_cause & (TXE_INTR_READINESS |
4628c2ecf20Sopenharmony_ci				      TXE_INTR_ALIVENESS |
4638c2ecf20Sopenharmony_ci				      TXE_INTR_IN_READY  |
4648c2ecf20Sopenharmony_ci				      TXE_INTR_OUT_DB));
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (ret) {
4678c2ecf20Sopenharmony_ci		dev_dbg(dev->dev,
4688c2ecf20Sopenharmony_ci			"Pending Interrupts InReady=%01d Readiness=%01d, Aliveness=%01d, OutDoor=%01d\n",
4698c2ecf20Sopenharmony_ci			!!(hw->intr_cause & TXE_INTR_IN_READY),
4708c2ecf20Sopenharmony_ci			!!(hw->intr_cause & TXE_INTR_READINESS),
4718c2ecf20Sopenharmony_ci			!!(hw->intr_cause & TXE_INTR_ALIVENESS),
4728c2ecf20Sopenharmony_ci			!!(hw->intr_cause & TXE_INTR_OUT_DB));
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci	return ret;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci/**
4788c2ecf20Sopenharmony_ci * mei_txe_input_payload_write - write a dword to the host buffer
4798c2ecf20Sopenharmony_ci *	at offset idx
4808c2ecf20Sopenharmony_ci *
4818c2ecf20Sopenharmony_ci * @dev: the device structure
4828c2ecf20Sopenharmony_ci * @idx: index in the host buffer
4838c2ecf20Sopenharmony_ci * @value: value
4848c2ecf20Sopenharmony_ci */
4858c2ecf20Sopenharmony_cistatic void mei_txe_input_payload_write(struct mei_device *dev,
4868c2ecf20Sopenharmony_ci			unsigned long idx, u32 value)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_PAYLOAD_REG +
4918c2ecf20Sopenharmony_ci			(idx * sizeof(u32)), value);
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci/**
4958c2ecf20Sopenharmony_ci * mei_txe_out_data_read - read dword from the device buffer
4968c2ecf20Sopenharmony_ci *	at offset idx
4978c2ecf20Sopenharmony_ci *
4988c2ecf20Sopenharmony_ci * @dev: the device structure
4998c2ecf20Sopenharmony_ci * @idx: index in the device buffer
5008c2ecf20Sopenharmony_ci *
5018c2ecf20Sopenharmony_ci * Return: register value at index
5028c2ecf20Sopenharmony_ci */
5038c2ecf20Sopenharmony_cistatic u32 mei_txe_out_data_read(const struct mei_device *dev,
5048c2ecf20Sopenharmony_ci					unsigned long idx)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	return mei_txe_br_reg_read(hw,
5098c2ecf20Sopenharmony_ci		BRIDGE_IPC_OUTPUT_PAYLOAD_REG + (idx * sizeof(u32)));
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci/* Readiness */
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci/**
5158c2ecf20Sopenharmony_ci * mei_txe_readiness_set_host_rdy - set host readiness bit
5168c2ecf20Sopenharmony_ci *
5178c2ecf20Sopenharmony_ci * @dev: the device structure
5188c2ecf20Sopenharmony_ci */
5198c2ecf20Sopenharmony_cistatic void mei_txe_readiness_set_host_rdy(struct mei_device *dev)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw,
5248c2ecf20Sopenharmony_ci		SICR_HOST_IPC_READINESS_REQ_REG,
5258c2ecf20Sopenharmony_ci		SICR_HOST_IPC_READINESS_HOST_RDY);
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci/**
5298c2ecf20Sopenharmony_ci * mei_txe_readiness_clear - clear host readiness bit
5308c2ecf20Sopenharmony_ci *
5318c2ecf20Sopenharmony_ci * @dev: the device structure
5328c2ecf20Sopenharmony_ci */
5338c2ecf20Sopenharmony_cistatic void mei_txe_readiness_clear(struct mei_device *dev)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, SICR_HOST_IPC_READINESS_REQ_REG,
5388c2ecf20Sopenharmony_ci				SICR_HOST_IPC_READINESS_RDY_CLR);
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci/**
5418c2ecf20Sopenharmony_ci * mei_txe_readiness_get - Reads and returns
5428c2ecf20Sopenharmony_ci *	the HICR_SEC_IPC_READINESS register value
5438c2ecf20Sopenharmony_ci *
5448c2ecf20Sopenharmony_ci * @dev: the device structure
5458c2ecf20Sopenharmony_ci *
5468c2ecf20Sopenharmony_ci * Return: the HICR_SEC_IPC_READINESS register value
5478c2ecf20Sopenharmony_ci */
5488c2ecf20Sopenharmony_cistatic u32 mei_txe_readiness_get(struct mei_device *dev)
5498c2ecf20Sopenharmony_ci{
5508c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	return mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci/**
5578c2ecf20Sopenharmony_ci * mei_txe_readiness_is_sec_rdy - check readiness
5588c2ecf20Sopenharmony_ci *  for HICR_SEC_IPC_READINESS_SEC_RDY
5598c2ecf20Sopenharmony_ci *
5608c2ecf20Sopenharmony_ci * @readiness: cached readiness state
5618c2ecf20Sopenharmony_ci *
5628c2ecf20Sopenharmony_ci * Return: true if readiness bit is set
5638c2ecf20Sopenharmony_ci */
5648c2ecf20Sopenharmony_cistatic inline bool mei_txe_readiness_is_sec_rdy(u32 readiness)
5658c2ecf20Sopenharmony_ci{
5668c2ecf20Sopenharmony_ci	return !!(readiness & HICR_SEC_IPC_READINESS_SEC_RDY);
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci/**
5708c2ecf20Sopenharmony_ci * mei_txe_hw_is_ready - check if the hw is ready
5718c2ecf20Sopenharmony_ci *
5728c2ecf20Sopenharmony_ci * @dev: the device structure
5738c2ecf20Sopenharmony_ci *
5748c2ecf20Sopenharmony_ci * Return: true if sec is ready
5758c2ecf20Sopenharmony_ci */
5768c2ecf20Sopenharmony_cistatic bool mei_txe_hw_is_ready(struct mei_device *dev)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	u32 readiness =  mei_txe_readiness_get(dev);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	return mei_txe_readiness_is_sec_rdy(readiness);
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci/**
5848c2ecf20Sopenharmony_ci * mei_txe_host_is_ready - check if the host is ready
5858c2ecf20Sopenharmony_ci *
5868c2ecf20Sopenharmony_ci * @dev: the device structure
5878c2ecf20Sopenharmony_ci *
5888c2ecf20Sopenharmony_ci * Return: true if host is ready
5898c2ecf20Sopenharmony_ci */
5908c2ecf20Sopenharmony_cistatic inline bool mei_txe_host_is_ready(struct mei_device *dev)
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
5938c2ecf20Sopenharmony_ci	u32 reg = mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	return !!(reg & HICR_SEC_IPC_READINESS_HOST_RDY);
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci/**
5998c2ecf20Sopenharmony_ci * mei_txe_readiness_wait - wait till readiness settles
6008c2ecf20Sopenharmony_ci *
6018c2ecf20Sopenharmony_ci * @dev: the device structure
6028c2ecf20Sopenharmony_ci *
6038c2ecf20Sopenharmony_ci * Return: 0 on success and -ETIME on timeout
6048c2ecf20Sopenharmony_ci */
6058c2ecf20Sopenharmony_cistatic int mei_txe_readiness_wait(struct mei_device *dev)
6068c2ecf20Sopenharmony_ci{
6078c2ecf20Sopenharmony_ci	if (mei_txe_hw_is_ready(dev))
6088c2ecf20Sopenharmony_ci		return 0;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	mutex_unlock(&dev->device_lock);
6118c2ecf20Sopenharmony_ci	wait_event_timeout(dev->wait_hw_ready, dev->recvd_hw_ready,
6128c2ecf20Sopenharmony_ci			msecs_to_jiffies(SEC_RESET_WAIT_TIMEOUT));
6138c2ecf20Sopenharmony_ci	mutex_lock(&dev->device_lock);
6148c2ecf20Sopenharmony_ci	if (!dev->recvd_hw_ready) {
6158c2ecf20Sopenharmony_ci		dev_err(dev->dev, "wait for readiness failed\n");
6168c2ecf20Sopenharmony_ci		return -ETIME;
6178c2ecf20Sopenharmony_ci	}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	dev->recvd_hw_ready = false;
6208c2ecf20Sopenharmony_ci	return 0;
6218c2ecf20Sopenharmony_ci}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_cistatic const struct mei_fw_status mei_txe_fw_sts = {
6248c2ecf20Sopenharmony_ci	.count = 2,
6258c2ecf20Sopenharmony_ci	.status[0] = PCI_CFG_TXE_FW_STS0,
6268c2ecf20Sopenharmony_ci	.status[1] = PCI_CFG_TXE_FW_STS1
6278c2ecf20Sopenharmony_ci};
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci/**
6308c2ecf20Sopenharmony_ci * mei_txe_fw_status - read fw status register from pci config space
6318c2ecf20Sopenharmony_ci *
6328c2ecf20Sopenharmony_ci * @dev: mei device
6338c2ecf20Sopenharmony_ci * @fw_status: fw status register values
6348c2ecf20Sopenharmony_ci *
6358c2ecf20Sopenharmony_ci * Return: 0 on success, error otherwise
6368c2ecf20Sopenharmony_ci */
6378c2ecf20Sopenharmony_cistatic int mei_txe_fw_status(struct mei_device *dev,
6388c2ecf20Sopenharmony_ci			     struct mei_fw_status *fw_status)
6398c2ecf20Sopenharmony_ci{
6408c2ecf20Sopenharmony_ci	const struct mei_fw_status *fw_src = &mei_txe_fw_sts;
6418c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev->dev);
6428c2ecf20Sopenharmony_ci	int ret;
6438c2ecf20Sopenharmony_ci	int i;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	if (!fw_status)
6468c2ecf20Sopenharmony_ci		return -EINVAL;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	fw_status->count = fw_src->count;
6498c2ecf20Sopenharmony_ci	for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
6508c2ecf20Sopenharmony_ci		ret = pci_read_config_dword(pdev, fw_src->status[i],
6518c2ecf20Sopenharmony_ci					    &fw_status->status[i]);
6528c2ecf20Sopenharmony_ci		trace_mei_pci_cfg_read(dev->dev, "PCI_CFG_HSF_X",
6538c2ecf20Sopenharmony_ci				       fw_src->status[i],
6548c2ecf20Sopenharmony_ci				       fw_status->status[i]);
6558c2ecf20Sopenharmony_ci		if (ret)
6568c2ecf20Sopenharmony_ci			return ret;
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	return 0;
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci/**
6638c2ecf20Sopenharmony_ci * mei_txe_hw_config - configure hardware at the start of the devices
6648c2ecf20Sopenharmony_ci *
6658c2ecf20Sopenharmony_ci * @dev: the device structure
6668c2ecf20Sopenharmony_ci *
6678c2ecf20Sopenharmony_ci * Configure hardware at the start of the device should be done only
6688c2ecf20Sopenharmony_ci *   once at the device probe time
6698c2ecf20Sopenharmony_ci *
6708c2ecf20Sopenharmony_ci * Return: always 0
6718c2ecf20Sopenharmony_ci */
6728c2ecf20Sopenharmony_cistatic int mei_txe_hw_config(struct mei_device *dev)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	hw->aliveness = mei_txe_aliveness_get(dev);
6788c2ecf20Sopenharmony_ci	hw->readiness = mei_txe_readiness_get(dev);
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "aliveness_resp = 0x%08x, readiness = 0x%08x.\n",
6818c2ecf20Sopenharmony_ci		hw->aliveness, hw->readiness);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	return 0;
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci/**
6878c2ecf20Sopenharmony_ci * mei_txe_write - writes a message to device.
6888c2ecf20Sopenharmony_ci *
6898c2ecf20Sopenharmony_ci * @dev: the device structure
6908c2ecf20Sopenharmony_ci * @hdr: header of message
6918c2ecf20Sopenharmony_ci * @hdr_len: header length in bytes - must multiplication of a slot (4bytes)
6928c2ecf20Sopenharmony_ci * @data: payload
6938c2ecf20Sopenharmony_ci * @data_len: paylead length in bytes
6948c2ecf20Sopenharmony_ci *
6958c2ecf20Sopenharmony_ci * Return: 0 if success, < 0 - otherwise.
6968c2ecf20Sopenharmony_ci */
6978c2ecf20Sopenharmony_cistatic int mei_txe_write(struct mei_device *dev,
6988c2ecf20Sopenharmony_ci			 const void *hdr, size_t hdr_len,
6998c2ecf20Sopenharmony_ci			 const void *data, size_t data_len)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
7028c2ecf20Sopenharmony_ci	unsigned long rem;
7038c2ecf20Sopenharmony_ci	const u32 *reg_buf;
7048c2ecf20Sopenharmony_ci	u32 slots = TXE_HBUF_DEPTH;
7058c2ecf20Sopenharmony_ci	u32 dw_cnt;
7068c2ecf20Sopenharmony_ci	unsigned long i, j;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	if (WARN_ON(!hdr || !data || hdr_len & 0x3))
7098c2ecf20Sopenharmony_ci		return -EINVAL;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM((struct mei_msg_hdr *)hdr));
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	dw_cnt = mei_data2slots(hdr_len + data_len);
7148c2ecf20Sopenharmony_ci	if (dw_cnt > slots)
7158c2ecf20Sopenharmony_ci		return -EMSGSIZE;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	if (WARN(!hw->aliveness, "txe write: aliveness not asserted\n"))
7188c2ecf20Sopenharmony_ci		return -EAGAIN;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	/* Enable Input Ready Interrupt. */
7218c2ecf20Sopenharmony_ci	mei_txe_input_ready_interrupt_enable(dev);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	if (!mei_txe_is_input_ready(dev)) {
7248c2ecf20Sopenharmony_ci		char fw_sts_str[MEI_FW_STATUS_STR_SZ];
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci		mei_fw_status_str(dev, fw_sts_str, MEI_FW_STATUS_STR_SZ);
7278c2ecf20Sopenharmony_ci		dev_err(dev->dev, "Input is not ready %s\n", fw_sts_str);
7288c2ecf20Sopenharmony_ci		return -EAGAIN;
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	reg_buf = hdr;
7328c2ecf20Sopenharmony_ci	for (i = 0; i < hdr_len / MEI_SLOT_SIZE; i++)
7338c2ecf20Sopenharmony_ci		mei_txe_input_payload_write(dev, i, reg_buf[i]);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	reg_buf = data;
7368c2ecf20Sopenharmony_ci	for (j = 0; j < data_len / MEI_SLOT_SIZE; j++)
7378c2ecf20Sopenharmony_ci		mei_txe_input_payload_write(dev, i + j, reg_buf[j]);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	rem = data_len & 0x3;
7408c2ecf20Sopenharmony_ci	if (rem > 0) {
7418c2ecf20Sopenharmony_ci		u32 reg = 0;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci		memcpy(&reg, (const u8 *)data + data_len - rem, rem);
7448c2ecf20Sopenharmony_ci		mei_txe_input_payload_write(dev, i + j, reg);
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	/* after each write the whole buffer is consumed */
7488c2ecf20Sopenharmony_ci	hw->slots = 0;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	/* Set Input-Doorbell */
7518c2ecf20Sopenharmony_ci	mei_txe_input_doorbell_set(hw);
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	return 0;
7548c2ecf20Sopenharmony_ci}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci/**
7578c2ecf20Sopenharmony_ci * mei_txe_hbuf_depth - mimics the me hbuf circular buffer
7588c2ecf20Sopenharmony_ci *
7598c2ecf20Sopenharmony_ci * @dev: the device structure
7608c2ecf20Sopenharmony_ci *
7618c2ecf20Sopenharmony_ci * Return: the TXE_HBUF_DEPTH
7628c2ecf20Sopenharmony_ci */
7638c2ecf20Sopenharmony_cistatic u32 mei_txe_hbuf_depth(const struct mei_device *dev)
7648c2ecf20Sopenharmony_ci{
7658c2ecf20Sopenharmony_ci	return TXE_HBUF_DEPTH;
7668c2ecf20Sopenharmony_ci}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci/**
7698c2ecf20Sopenharmony_ci * mei_txe_hbuf_empty_slots - mimics the me hbuf circular buffer
7708c2ecf20Sopenharmony_ci *
7718c2ecf20Sopenharmony_ci * @dev: the device structure
7728c2ecf20Sopenharmony_ci *
7738c2ecf20Sopenharmony_ci * Return: always TXE_HBUF_DEPTH
7748c2ecf20Sopenharmony_ci */
7758c2ecf20Sopenharmony_cistatic int mei_txe_hbuf_empty_slots(struct mei_device *dev)
7768c2ecf20Sopenharmony_ci{
7778c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	return hw->slots;
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci/**
7838c2ecf20Sopenharmony_ci * mei_txe_count_full_read_slots - mimics the me device circular buffer
7848c2ecf20Sopenharmony_ci *
7858c2ecf20Sopenharmony_ci * @dev: the device structure
7868c2ecf20Sopenharmony_ci *
7878c2ecf20Sopenharmony_ci * Return: always buffer size in dwords count
7888c2ecf20Sopenharmony_ci */
7898c2ecf20Sopenharmony_cistatic int mei_txe_count_full_read_slots(struct mei_device *dev)
7908c2ecf20Sopenharmony_ci{
7918c2ecf20Sopenharmony_ci	/* read buffers has static size */
7928c2ecf20Sopenharmony_ci	return TXE_HBUF_DEPTH;
7938c2ecf20Sopenharmony_ci}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci/**
7968c2ecf20Sopenharmony_ci * mei_txe_read_hdr - read message header which is always in 4 first bytes
7978c2ecf20Sopenharmony_ci *
7988c2ecf20Sopenharmony_ci * @dev: the device structure
7998c2ecf20Sopenharmony_ci *
8008c2ecf20Sopenharmony_ci * Return: mei message header
8018c2ecf20Sopenharmony_ci */
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic u32 mei_txe_read_hdr(const struct mei_device *dev)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	return mei_txe_out_data_read(dev, 0);
8068c2ecf20Sopenharmony_ci}
8078c2ecf20Sopenharmony_ci/**
8088c2ecf20Sopenharmony_ci * mei_txe_read - reads a message from the txe device.
8098c2ecf20Sopenharmony_ci *
8108c2ecf20Sopenharmony_ci * @dev: the device structure
8118c2ecf20Sopenharmony_ci * @buf: message buffer will be written
8128c2ecf20Sopenharmony_ci * @len: message size will be read
8138c2ecf20Sopenharmony_ci *
8148c2ecf20Sopenharmony_ci * Return: -EINVAL on error wrong argument and 0 on success
8158c2ecf20Sopenharmony_ci */
8168c2ecf20Sopenharmony_cistatic int mei_txe_read(struct mei_device *dev,
8178c2ecf20Sopenharmony_ci		unsigned char *buf, unsigned long len)
8188c2ecf20Sopenharmony_ci{
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
8218c2ecf20Sopenharmony_ci	u32 *reg_buf, reg;
8228c2ecf20Sopenharmony_ci	u32 rem;
8238c2ecf20Sopenharmony_ci	u32 i;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	if (WARN_ON(!buf || !len))
8268c2ecf20Sopenharmony_ci		return -EINVAL;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	reg_buf = (u32 *)buf;
8298c2ecf20Sopenharmony_ci	rem = len & 0x3;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "buffer-length = %lu buf[0]0x%08X\n",
8328c2ecf20Sopenharmony_ci		len, mei_txe_out_data_read(dev, 0));
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	for (i = 0; i < len / MEI_SLOT_SIZE; i++) {
8358c2ecf20Sopenharmony_ci		/* skip header: index starts from 1 */
8368c2ecf20Sopenharmony_ci		reg = mei_txe_out_data_read(dev, i + 1);
8378c2ecf20Sopenharmony_ci		dev_dbg(dev->dev, "buf[%d] = 0x%08X\n", i, reg);
8388c2ecf20Sopenharmony_ci		*reg_buf++ = reg;
8398c2ecf20Sopenharmony_ci	}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	if (rem) {
8428c2ecf20Sopenharmony_ci		reg = mei_txe_out_data_read(dev, i + 1);
8438c2ecf20Sopenharmony_ci		memcpy(reg_buf, &reg, rem);
8448c2ecf20Sopenharmony_ci	}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	mei_txe_output_ready_set(hw);
8478c2ecf20Sopenharmony_ci	return 0;
8488c2ecf20Sopenharmony_ci}
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci/**
8518c2ecf20Sopenharmony_ci * mei_txe_hw_reset - resets host and fw.
8528c2ecf20Sopenharmony_ci *
8538c2ecf20Sopenharmony_ci * @dev: the device structure
8548c2ecf20Sopenharmony_ci * @intr_enable: if interrupt should be enabled after reset.
8558c2ecf20Sopenharmony_ci *
8568c2ecf20Sopenharmony_ci * Return: 0 on success and < 0 in case of error
8578c2ecf20Sopenharmony_ci */
8588c2ecf20Sopenharmony_cistatic int mei_txe_hw_reset(struct mei_device *dev, bool intr_enable)
8598c2ecf20Sopenharmony_ci{
8608c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	u32 aliveness_req;
8638c2ecf20Sopenharmony_ci	/*
8648c2ecf20Sopenharmony_ci	 * read input doorbell to ensure consistency between  Bridge and SeC
8658c2ecf20Sopenharmony_ci	 * return value might be garbage return
8668c2ecf20Sopenharmony_ci	 */
8678c2ecf20Sopenharmony_ci	(void)mei_txe_sec_reg_read_silent(hw, SEC_IPC_INPUT_DOORBELL_REG);
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	aliveness_req = mei_txe_aliveness_req_get(dev);
8708c2ecf20Sopenharmony_ci	hw->aliveness = mei_txe_aliveness_get(dev);
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	/* Disable interrupts in this stage we will poll */
8738c2ecf20Sopenharmony_ci	mei_txe_intr_disable(dev);
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	/*
8768c2ecf20Sopenharmony_ci	 * If Aliveness Request and Aliveness Response are not equal then
8778c2ecf20Sopenharmony_ci	 * wait for them to be equal
8788c2ecf20Sopenharmony_ci	 * Since we might have interrupts disabled - poll for it
8798c2ecf20Sopenharmony_ci	 */
8808c2ecf20Sopenharmony_ci	if (aliveness_req != hw->aliveness)
8818c2ecf20Sopenharmony_ci		if (mei_txe_aliveness_poll(dev, aliveness_req) < 0) {
8828c2ecf20Sopenharmony_ci			dev_err(dev->dev, "wait for aliveness settle failed ... bailing out\n");
8838c2ecf20Sopenharmony_ci			return -EIO;
8848c2ecf20Sopenharmony_ci		}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	/*
8878c2ecf20Sopenharmony_ci	 * If Aliveness Request and Aliveness Response are set then clear them
8888c2ecf20Sopenharmony_ci	 */
8898c2ecf20Sopenharmony_ci	if (aliveness_req) {
8908c2ecf20Sopenharmony_ci		mei_txe_aliveness_set(dev, 0);
8918c2ecf20Sopenharmony_ci		if (mei_txe_aliveness_poll(dev, 0) < 0) {
8928c2ecf20Sopenharmony_ci			dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
8938c2ecf20Sopenharmony_ci			return -EIO;
8948c2ecf20Sopenharmony_ci		}
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	/*
8988c2ecf20Sopenharmony_ci	 * Set readiness RDY_CLR bit
8998c2ecf20Sopenharmony_ci	 */
9008c2ecf20Sopenharmony_ci	mei_txe_readiness_clear(dev);
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	return 0;
9038c2ecf20Sopenharmony_ci}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci/**
9068c2ecf20Sopenharmony_ci * mei_txe_hw_start - start the hardware after reset
9078c2ecf20Sopenharmony_ci *
9088c2ecf20Sopenharmony_ci * @dev: the device structure
9098c2ecf20Sopenharmony_ci *
9108c2ecf20Sopenharmony_ci * Return: 0 on success an error code otherwise
9118c2ecf20Sopenharmony_ci */
9128c2ecf20Sopenharmony_cistatic int mei_txe_hw_start(struct mei_device *dev)
9138c2ecf20Sopenharmony_ci{
9148c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
9158c2ecf20Sopenharmony_ci	int ret;
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	u32 hisr;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	/* bring back interrupts */
9208c2ecf20Sopenharmony_ci	mei_txe_intr_enable(dev);
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	ret = mei_txe_readiness_wait(dev);
9238c2ecf20Sopenharmony_ci	if (ret < 0) {
9248c2ecf20Sopenharmony_ci		dev_err(dev->dev, "waiting for readiness failed\n");
9258c2ecf20Sopenharmony_ci		return ret;
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	/*
9298c2ecf20Sopenharmony_ci	 * If HISR.INT2_STS interrupt status bit is set then clear it.
9308c2ecf20Sopenharmony_ci	 */
9318c2ecf20Sopenharmony_ci	hisr = mei_txe_br_reg_read(hw, HISR_REG);
9328c2ecf20Sopenharmony_ci	if (hisr & HISR_INT_2_STS)
9338c2ecf20Sopenharmony_ci		mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_2_STS);
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	/* Clear the interrupt cause of OutputDoorbell */
9368c2ecf20Sopenharmony_ci	clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause);
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	ret = mei_txe_aliveness_set_sync(dev, 1);
9398c2ecf20Sopenharmony_ci	if (ret < 0) {
9408c2ecf20Sopenharmony_ci		dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
9418c2ecf20Sopenharmony_ci		return ret;
9428c2ecf20Sopenharmony_ci	}
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	pm_runtime_set_active(dev->dev);
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	/* enable input ready interrupts:
9478c2ecf20Sopenharmony_ci	 * SEC_IPC_HOST_INT_MASK.IPC_INPUT_READY_INT_MASK
9488c2ecf20Sopenharmony_ci	 */
9498c2ecf20Sopenharmony_ci	mei_txe_input_ready_interrupt_enable(dev);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	/*  Set the SICR_SEC_IPC_OUTPUT_STATUS.IPC_OUTPUT_READY bit */
9538c2ecf20Sopenharmony_ci	mei_txe_output_ready_set(hw);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	/* Set bit SICR_HOST_IPC_READINESS.HOST_RDY
9568c2ecf20Sopenharmony_ci	 */
9578c2ecf20Sopenharmony_ci	mei_txe_readiness_set_host_rdy(dev);
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	return 0;
9608c2ecf20Sopenharmony_ci}
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci/**
9638c2ecf20Sopenharmony_ci * mei_txe_check_and_ack_intrs - translate multi BAR interrupt into
9648c2ecf20Sopenharmony_ci *  single bit mask and acknowledge the interrupts
9658c2ecf20Sopenharmony_ci *
9668c2ecf20Sopenharmony_ci * @dev: the device structure
9678c2ecf20Sopenharmony_ci * @do_ack: acknowledge interrupts
9688c2ecf20Sopenharmony_ci *
9698c2ecf20Sopenharmony_ci * Return: true if found interrupts to process.
9708c2ecf20Sopenharmony_ci */
9718c2ecf20Sopenharmony_cistatic bool mei_txe_check_and_ack_intrs(struct mei_device *dev, bool do_ack)
9728c2ecf20Sopenharmony_ci{
9738c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
9748c2ecf20Sopenharmony_ci	u32 hisr;
9758c2ecf20Sopenharmony_ci	u32 hhisr;
9768c2ecf20Sopenharmony_ci	u32 ipc_isr;
9778c2ecf20Sopenharmony_ci	u32 aliveness;
9788c2ecf20Sopenharmony_ci	bool generated;
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	/* read interrupt registers */
9818c2ecf20Sopenharmony_ci	hhisr = mei_txe_br_reg_read(hw, HHISR_REG);
9828c2ecf20Sopenharmony_ci	generated = (hhisr & IPC_HHIER_MSK);
9838c2ecf20Sopenharmony_ci	if (!generated)
9848c2ecf20Sopenharmony_ci		goto out;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	hisr = mei_txe_br_reg_read(hw, HISR_REG);
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	aliveness = mei_txe_aliveness_get(dev);
9898c2ecf20Sopenharmony_ci	if (hhisr & IPC_HHIER_SEC && aliveness) {
9908c2ecf20Sopenharmony_ci		ipc_isr = mei_txe_sec_reg_read_silent(hw,
9918c2ecf20Sopenharmony_ci				SEC_IPC_HOST_INT_STATUS_REG);
9928c2ecf20Sopenharmony_ci	} else {
9938c2ecf20Sopenharmony_ci		ipc_isr = 0;
9948c2ecf20Sopenharmony_ci		hhisr &= ~IPC_HHIER_SEC;
9958c2ecf20Sopenharmony_ci	}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	generated = generated ||
9988c2ecf20Sopenharmony_ci		(hisr & HISR_INT_STS_MSK) ||
9998c2ecf20Sopenharmony_ci		(ipc_isr & SEC_IPC_HOST_INT_STATUS_PENDING);
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	if (generated && do_ack) {
10028c2ecf20Sopenharmony_ci		/* Save the interrupt causes */
10038c2ecf20Sopenharmony_ci		hw->intr_cause |= hisr & HISR_INT_STS_MSK;
10048c2ecf20Sopenharmony_ci		if (ipc_isr & SEC_IPC_HOST_INT_STATUS_IN_RDY)
10058c2ecf20Sopenharmony_ci			hw->intr_cause |= TXE_INTR_IN_READY;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci		mei_txe_intr_disable(dev);
10098c2ecf20Sopenharmony_ci		/* Clear the interrupts in hierarchy:
10108c2ecf20Sopenharmony_ci		 * IPC and Bridge, than the High Level */
10118c2ecf20Sopenharmony_ci		mei_txe_sec_reg_write_silent(hw,
10128c2ecf20Sopenharmony_ci			SEC_IPC_HOST_INT_STATUS_REG, ipc_isr);
10138c2ecf20Sopenharmony_ci		mei_txe_br_reg_write(hw, HISR_REG, hisr);
10148c2ecf20Sopenharmony_ci		mei_txe_br_reg_write(hw, HHISR_REG, hhisr);
10158c2ecf20Sopenharmony_ci	}
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ciout:
10188c2ecf20Sopenharmony_ci	return generated;
10198c2ecf20Sopenharmony_ci}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci/**
10228c2ecf20Sopenharmony_ci * mei_txe_irq_quick_handler - The ISR of the MEI device
10238c2ecf20Sopenharmony_ci *
10248c2ecf20Sopenharmony_ci * @irq: The irq number
10258c2ecf20Sopenharmony_ci * @dev_id: pointer to the device structure
10268c2ecf20Sopenharmony_ci *
10278c2ecf20Sopenharmony_ci * Return: IRQ_WAKE_THREAD if interrupt is designed for the device
10288c2ecf20Sopenharmony_ci *         IRQ_NONE otherwise
10298c2ecf20Sopenharmony_ci */
10308c2ecf20Sopenharmony_ciirqreturn_t mei_txe_irq_quick_handler(int irq, void *dev_id)
10318c2ecf20Sopenharmony_ci{
10328c2ecf20Sopenharmony_ci	struct mei_device *dev = dev_id;
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	if (mei_txe_check_and_ack_intrs(dev, true))
10358c2ecf20Sopenharmony_ci		return IRQ_WAKE_THREAD;
10368c2ecf20Sopenharmony_ci	return IRQ_NONE;
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci/**
10418c2ecf20Sopenharmony_ci * mei_txe_irq_thread_handler - txe interrupt thread
10428c2ecf20Sopenharmony_ci *
10438c2ecf20Sopenharmony_ci * @irq: The irq number
10448c2ecf20Sopenharmony_ci * @dev_id: pointer to the device structure
10458c2ecf20Sopenharmony_ci *
10468c2ecf20Sopenharmony_ci * Return: IRQ_HANDLED
10478c2ecf20Sopenharmony_ci */
10488c2ecf20Sopenharmony_ciirqreturn_t mei_txe_irq_thread_handler(int irq, void *dev_id)
10498c2ecf20Sopenharmony_ci{
10508c2ecf20Sopenharmony_ci	struct mei_device *dev = (struct mei_device *) dev_id;
10518c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
10528c2ecf20Sopenharmony_ci	struct list_head cmpl_list;
10538c2ecf20Sopenharmony_ci	s32 slots;
10548c2ecf20Sopenharmony_ci	int rets = 0;
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "irq thread: Interrupt Registers HHISR|HISR|SEC=%02X|%04X|%02X\n",
10578c2ecf20Sopenharmony_ci		mei_txe_br_reg_read(hw, HHISR_REG),
10588c2ecf20Sopenharmony_ci		mei_txe_br_reg_read(hw, HISR_REG),
10598c2ecf20Sopenharmony_ci		mei_txe_sec_reg_read_silent(hw, SEC_IPC_HOST_INT_STATUS_REG));
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	/* initialize our complete list */
10638c2ecf20Sopenharmony_ci	mutex_lock(&dev->device_lock);
10648c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cmpl_list);
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
10678c2ecf20Sopenharmony_ci		mei_txe_check_and_ack_intrs(dev, true);
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	/* show irq events */
10708c2ecf20Sopenharmony_ci	mei_txe_pending_interrupts(dev);
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	hw->aliveness = mei_txe_aliveness_get(dev);
10738c2ecf20Sopenharmony_ci	hw->readiness = mei_txe_readiness_get(dev);
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	/* Readiness:
10768c2ecf20Sopenharmony_ci	 * Detection of TXE driver going through reset
10778c2ecf20Sopenharmony_ci	 * or TXE driver resetting the HECI interface.
10788c2ecf20Sopenharmony_ci	 */
10798c2ecf20Sopenharmony_ci	if (test_and_clear_bit(TXE_INTR_READINESS_BIT, &hw->intr_cause)) {
10808c2ecf20Sopenharmony_ci		dev_dbg(dev->dev, "Readiness Interrupt was received...\n");
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci		/* Check if SeC is going through reset */
10838c2ecf20Sopenharmony_ci		if (mei_txe_readiness_is_sec_rdy(hw->readiness)) {
10848c2ecf20Sopenharmony_ci			dev_dbg(dev->dev, "we need to start the dev.\n");
10858c2ecf20Sopenharmony_ci			dev->recvd_hw_ready = true;
10868c2ecf20Sopenharmony_ci		} else {
10878c2ecf20Sopenharmony_ci			dev->recvd_hw_ready = false;
10888c2ecf20Sopenharmony_ci			if (dev->dev_state != MEI_DEV_RESETTING) {
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci				dev_warn(dev->dev, "FW not ready: resetting.\n");
10918c2ecf20Sopenharmony_ci				schedule_work(&dev->reset_work);
10928c2ecf20Sopenharmony_ci				goto end;
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci			}
10958c2ecf20Sopenharmony_ci		}
10968c2ecf20Sopenharmony_ci		wake_up(&dev->wait_hw_ready);
10978c2ecf20Sopenharmony_ci	}
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	/************************************************************/
11008c2ecf20Sopenharmony_ci	/* Check interrupt cause:
11018c2ecf20Sopenharmony_ci	 * Aliveness: Detection of SeC acknowledge of host request that
11028c2ecf20Sopenharmony_ci	 * it remain alive or host cancellation of that request.
11038c2ecf20Sopenharmony_ci	 */
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	if (test_and_clear_bit(TXE_INTR_ALIVENESS_BIT, &hw->intr_cause)) {
11068c2ecf20Sopenharmony_ci		/* Clear the interrupt cause */
11078c2ecf20Sopenharmony_ci		dev_dbg(dev->dev,
11088c2ecf20Sopenharmony_ci			"Aliveness Interrupt: Status: %d\n", hw->aliveness);
11098c2ecf20Sopenharmony_ci		dev->pg_event = MEI_PG_EVENT_RECEIVED;
11108c2ecf20Sopenharmony_ci		if (waitqueue_active(&hw->wait_aliveness_resp))
11118c2ecf20Sopenharmony_ci			wake_up(&hw->wait_aliveness_resp);
11128c2ecf20Sopenharmony_ci	}
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	/* Output Doorbell:
11168c2ecf20Sopenharmony_ci	 * Detection of SeC having sent output to host
11178c2ecf20Sopenharmony_ci	 */
11188c2ecf20Sopenharmony_ci	slots = mei_count_full_read_slots(dev);
11198c2ecf20Sopenharmony_ci	if (test_and_clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause)) {
11208c2ecf20Sopenharmony_ci		/* Read from TXE */
11218c2ecf20Sopenharmony_ci		rets = mei_irq_read_handler(dev, &cmpl_list, &slots);
11228c2ecf20Sopenharmony_ci		if (rets &&
11238c2ecf20Sopenharmony_ci		    (dev->dev_state != MEI_DEV_RESETTING &&
11248c2ecf20Sopenharmony_ci		     dev->dev_state != MEI_DEV_POWER_DOWN)) {
11258c2ecf20Sopenharmony_ci			dev_err(dev->dev,
11268c2ecf20Sopenharmony_ci				"mei_irq_read_handler ret = %d.\n", rets);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci			schedule_work(&dev->reset_work);
11298c2ecf20Sopenharmony_ci			goto end;
11308c2ecf20Sopenharmony_ci		}
11318c2ecf20Sopenharmony_ci	}
11328c2ecf20Sopenharmony_ci	/* Input Ready: Detection if host can write to SeC */
11338c2ecf20Sopenharmony_ci	if (test_and_clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause)) {
11348c2ecf20Sopenharmony_ci		dev->hbuf_is_ready = true;
11358c2ecf20Sopenharmony_ci		hw->slots = TXE_HBUF_DEPTH;
11368c2ecf20Sopenharmony_ci	}
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	if (hw->aliveness && dev->hbuf_is_ready) {
11398c2ecf20Sopenharmony_ci		/* get the real register value */
11408c2ecf20Sopenharmony_ci		dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
11418c2ecf20Sopenharmony_ci		rets = mei_irq_write_handler(dev, &cmpl_list);
11428c2ecf20Sopenharmony_ci		if (rets && rets != -EMSGSIZE)
11438c2ecf20Sopenharmony_ci			dev_err(dev->dev, "mei_irq_write_handler ret = %d.\n",
11448c2ecf20Sopenharmony_ci				rets);
11458c2ecf20Sopenharmony_ci		dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
11468c2ecf20Sopenharmony_ci	}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	mei_irq_compl_handler(dev, &cmpl_list);
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ciend:
11518c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	mutex_unlock(&dev->device_lock);
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	mei_enable_interrupts(dev);
11568c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
11578c2ecf20Sopenharmony_ci}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_cistatic const struct mei_hw_ops mei_txe_hw_ops = {
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	.host_is_ready = mei_txe_host_is_ready,
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	.fw_status = mei_txe_fw_status,
11648c2ecf20Sopenharmony_ci	.pg_state = mei_txe_pg_state,
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	.hw_is_ready = mei_txe_hw_is_ready,
11678c2ecf20Sopenharmony_ci	.hw_reset = mei_txe_hw_reset,
11688c2ecf20Sopenharmony_ci	.hw_config = mei_txe_hw_config,
11698c2ecf20Sopenharmony_ci	.hw_start = mei_txe_hw_start,
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	.pg_in_transition = mei_txe_pg_in_transition,
11728c2ecf20Sopenharmony_ci	.pg_is_enabled = mei_txe_pg_is_enabled,
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	.intr_clear = mei_txe_intr_clear,
11758c2ecf20Sopenharmony_ci	.intr_enable = mei_txe_intr_enable,
11768c2ecf20Sopenharmony_ci	.intr_disable = mei_txe_intr_disable,
11778c2ecf20Sopenharmony_ci	.synchronize_irq = mei_txe_synchronize_irq,
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	.hbuf_free_slots = mei_txe_hbuf_empty_slots,
11808c2ecf20Sopenharmony_ci	.hbuf_is_ready = mei_txe_is_input_ready,
11818c2ecf20Sopenharmony_ci	.hbuf_depth = mei_txe_hbuf_depth,
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	.write = mei_txe_write,
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	.rdbuf_full_slots = mei_txe_count_full_read_slots,
11868c2ecf20Sopenharmony_ci	.read_hdr = mei_txe_read_hdr,
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	.read = mei_txe_read,
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci};
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci/**
11938c2ecf20Sopenharmony_ci * mei_txe_dev_init - allocates and initializes txe hardware specific structure
11948c2ecf20Sopenharmony_ci *
11958c2ecf20Sopenharmony_ci * @pdev: pci device
11968c2ecf20Sopenharmony_ci *
11978c2ecf20Sopenharmony_ci * Return: struct mei_device * on success or NULL
11988c2ecf20Sopenharmony_ci */
11998c2ecf20Sopenharmony_cistruct mei_device *mei_txe_dev_init(struct pci_dev *pdev)
12008c2ecf20Sopenharmony_ci{
12018c2ecf20Sopenharmony_ci	struct mei_device *dev;
12028c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw;
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	dev = devm_kzalloc(&pdev->dev, sizeof(*dev) + sizeof(*hw), GFP_KERNEL);
12058c2ecf20Sopenharmony_ci	if (!dev)
12068c2ecf20Sopenharmony_ci		return NULL;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	mei_device_init(dev, &pdev->dev, &mei_txe_hw_ops);
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	hw = to_txe_hw(dev);
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	init_waitqueue_head(&hw->wait_aliveness_resp);
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	return dev;
12158c2ecf20Sopenharmony_ci}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci/**
12188c2ecf20Sopenharmony_ci * mei_txe_setup_satt2 - SATT2 configuration for DMA support.
12198c2ecf20Sopenharmony_ci *
12208c2ecf20Sopenharmony_ci * @dev:   the device structure
12218c2ecf20Sopenharmony_ci * @addr:  physical address start of the range
12228c2ecf20Sopenharmony_ci * @range: physical range size
12238c2ecf20Sopenharmony_ci *
12248c2ecf20Sopenharmony_ci * Return: 0 on success an error code otherwise
12258c2ecf20Sopenharmony_ci */
12268c2ecf20Sopenharmony_ciint mei_txe_setup_satt2(struct mei_device *dev, phys_addr_t addr, u32 range)
12278c2ecf20Sopenharmony_ci{
12288c2ecf20Sopenharmony_ci	struct mei_txe_hw *hw = to_txe_hw(dev);
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	u32 lo32 = lower_32_bits(addr);
12318c2ecf20Sopenharmony_ci	u32 hi32 = upper_32_bits(addr);
12328c2ecf20Sopenharmony_ci	u32 ctrl;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	/* SATT is limited to 36 Bits */
12358c2ecf20Sopenharmony_ci	if (hi32 & ~0xF)
12368c2ecf20Sopenharmony_ci		return -EINVAL;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	/* SATT has to be 16Byte aligned */
12398c2ecf20Sopenharmony_ci	if (lo32 & 0xF)
12408c2ecf20Sopenharmony_ci		return -EINVAL;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	/* SATT range has to be 4Bytes aligned */
12438c2ecf20Sopenharmony_ci	if (range & 0x4)
12448c2ecf20Sopenharmony_ci		return -EINVAL;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	/* SATT is limited to 32 MB range*/
12478c2ecf20Sopenharmony_ci	if (range > SATT_RANGE_MAX)
12488c2ecf20Sopenharmony_ci		return -EINVAL;
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	ctrl = SATT2_CTRL_VALID_MSK;
12518c2ecf20Sopenharmony_ci	ctrl |= hi32  << SATT2_CTRL_BR_BASE_ADDR_REG_SHIFT;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, SATT2_SAP_SIZE_REG, range);
12548c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, SATT2_BRG_BA_LSB_REG, lo32);
12558c2ecf20Sopenharmony_ci	mei_txe_br_reg_write(hw, SATT2_CTRL_REG, ctrl);
12568c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "SATT2: SAP_SIZE_OFFSET=0x%08X, BRG_BA_LSB_OFFSET=0x%08X, CTRL_OFFSET=0x%08X\n",
12578c2ecf20Sopenharmony_ci		range, lo32, ctrl);
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	return 0;
12608c2ecf20Sopenharmony_ci}
1261