xref: /OK3568_Linux_fs/kernel/drivers/misc/mei/hw-txe.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2013-2020, Intel Corporation. All rights reserved.
4*4882a593Smuzhiyun  * Intel Management Engine Interface (Intel MEI) Linux driver
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/pci.h>
8*4882a593Smuzhiyun #include <linux/jiffies.h>
9*4882a593Smuzhiyun #include <linux/ktime.h>
10*4882a593Smuzhiyun #include <linux/delay.h>
11*4882a593Smuzhiyun #include <linux/kthread.h>
12*4882a593Smuzhiyun #include <linux/interrupt.h>
13*4882a593Smuzhiyun #include <linux/pm_runtime.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/mei.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "mei_dev.h"
18*4882a593Smuzhiyun #include "hw-txe.h"
19*4882a593Smuzhiyun #include "client.h"
20*4882a593Smuzhiyun #include "hbm.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "mei-trace.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define TXE_HBUF_DEPTH (PAYLOAD_SIZE / MEI_SLOT_SIZE)
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /**
27*4882a593Smuzhiyun  * mei_txe_reg_read - Reads 32bit data from the txe device
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * @base_addr: registers base address
30*4882a593Smuzhiyun  * @offset: register offset
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * Return: register value
33*4882a593Smuzhiyun  */
mei_txe_reg_read(void __iomem * base_addr,unsigned long offset)34*4882a593Smuzhiyun static inline u32 mei_txe_reg_read(void __iomem *base_addr,
35*4882a593Smuzhiyun 					unsigned long offset)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	return ioread32(base_addr + offset);
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun  * mei_txe_reg_write - Writes 32bit data to the txe device
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * @base_addr: registers base address
44*4882a593Smuzhiyun  * @offset: register offset
45*4882a593Smuzhiyun  * @value: the value to write
46*4882a593Smuzhiyun  */
mei_txe_reg_write(void __iomem * base_addr,unsigned long offset,u32 value)47*4882a593Smuzhiyun static inline void mei_txe_reg_write(void __iomem *base_addr,
48*4882a593Smuzhiyun 				unsigned long offset, u32 value)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	iowrite32(value, base_addr + offset);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun  * mei_txe_sec_reg_read_silent - Reads 32bit data from the SeC BAR
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * @hw: the txe hardware structure
57*4882a593Smuzhiyun  * @offset: register offset
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Doesn't check for aliveness while Reads 32bit data from the SeC BAR
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * Return: register value
62*4882a593Smuzhiyun  */
mei_txe_sec_reg_read_silent(struct mei_txe_hw * hw,unsigned long offset)63*4882a593Smuzhiyun static inline u32 mei_txe_sec_reg_read_silent(struct mei_txe_hw *hw,
64*4882a593Smuzhiyun 				unsigned long offset)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	return mei_txe_reg_read(hw->mem_addr[SEC_BAR], offset);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /**
70*4882a593Smuzhiyun  * mei_txe_sec_reg_read - Reads 32bit data from the SeC BAR
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * @hw: the txe hardware structure
73*4882a593Smuzhiyun  * @offset: register offset
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * Reads 32bit data from the SeC BAR and shout loud if aliveness is not set
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * Return: register value
78*4882a593Smuzhiyun  */
mei_txe_sec_reg_read(struct mei_txe_hw * hw,unsigned long offset)79*4882a593Smuzhiyun static inline u32 mei_txe_sec_reg_read(struct mei_txe_hw *hw,
80*4882a593Smuzhiyun 				unsigned long offset)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	WARN(!hw->aliveness, "sec read: aliveness not asserted\n");
83*4882a593Smuzhiyun 	return mei_txe_sec_reg_read_silent(hw, offset);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun /**
86*4882a593Smuzhiyun  * mei_txe_sec_reg_write_silent - Writes 32bit data to the SeC BAR
87*4882a593Smuzhiyun  *   doesn't check for aliveness
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * @hw: the txe hardware structure
90*4882a593Smuzhiyun  * @offset: register offset
91*4882a593Smuzhiyun  * @value: value to write
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * Doesn't check for aliveness while writes 32bit data from to the SeC BAR
94*4882a593Smuzhiyun  */
mei_txe_sec_reg_write_silent(struct mei_txe_hw * hw,unsigned long offset,u32 value)95*4882a593Smuzhiyun static inline void mei_txe_sec_reg_write_silent(struct mei_txe_hw *hw,
96*4882a593Smuzhiyun 				unsigned long offset, u32 value)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	mei_txe_reg_write(hw->mem_addr[SEC_BAR], offset, value);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /**
102*4882a593Smuzhiyun  * mei_txe_sec_reg_write - Writes 32bit data to the SeC BAR
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * @hw: the txe hardware structure
105*4882a593Smuzhiyun  * @offset: register offset
106*4882a593Smuzhiyun  * @value: value to write
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * Writes 32bit data from the SeC BAR and shout loud if aliveness is not set
109*4882a593Smuzhiyun  */
mei_txe_sec_reg_write(struct mei_txe_hw * hw,unsigned long offset,u32 value)110*4882a593Smuzhiyun static inline void mei_txe_sec_reg_write(struct mei_txe_hw *hw,
111*4882a593Smuzhiyun 				unsigned long offset, u32 value)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	WARN(!hw->aliveness, "sec write: aliveness not asserted\n");
114*4882a593Smuzhiyun 	mei_txe_sec_reg_write_silent(hw, offset, value);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun /**
117*4882a593Smuzhiyun  * mei_txe_br_reg_read - Reads 32bit data from the Bridge BAR
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * @hw: the txe hardware structure
120*4882a593Smuzhiyun  * @offset: offset from which to read the data
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  * Return: the byte read.
123*4882a593Smuzhiyun  */
mei_txe_br_reg_read(struct mei_txe_hw * hw,unsigned long offset)124*4882a593Smuzhiyun static inline u32 mei_txe_br_reg_read(struct mei_txe_hw *hw,
125*4882a593Smuzhiyun 				unsigned long offset)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	return mei_txe_reg_read(hw->mem_addr[BRIDGE_BAR], offset);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun  * mei_txe_br_reg_write - Writes 32bit data to the Bridge BAR
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * @hw: the txe hardware structure
134*4882a593Smuzhiyun  * @offset: offset from which to write the data
135*4882a593Smuzhiyun  * @value: the byte to write
136*4882a593Smuzhiyun  */
mei_txe_br_reg_write(struct mei_txe_hw * hw,unsigned long offset,u32 value)137*4882a593Smuzhiyun static inline void mei_txe_br_reg_write(struct mei_txe_hw *hw,
138*4882a593Smuzhiyun 				unsigned long offset, u32 value)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	mei_txe_reg_write(hw->mem_addr[BRIDGE_BAR], offset, value);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun  * mei_txe_aliveness_set - request for aliveness change
145*4882a593Smuzhiyun  *
146*4882a593Smuzhiyun  * @dev: the device structure
147*4882a593Smuzhiyun  * @req: requested aliveness value
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * Request for aliveness change and returns true if the change is
150*4882a593Smuzhiyun  *   really needed and false if aliveness is already
151*4882a593Smuzhiyun  *   in the requested state
152*4882a593Smuzhiyun  *
153*4882a593Smuzhiyun  * Locking: called under "dev->device_lock" lock
154*4882a593Smuzhiyun  *
155*4882a593Smuzhiyun  * Return: true if request was send
156*4882a593Smuzhiyun  */
mei_txe_aliveness_set(struct mei_device * dev,u32 req)157*4882a593Smuzhiyun static bool mei_txe_aliveness_set(struct mei_device *dev, u32 req)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
161*4882a593Smuzhiyun 	bool do_req = hw->aliveness != req;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	dev_dbg(dev->dev, "Aliveness current=%d request=%d\n",
164*4882a593Smuzhiyun 				hw->aliveness, req);
165*4882a593Smuzhiyun 	if (do_req) {
166*4882a593Smuzhiyun 		dev->pg_event = MEI_PG_EVENT_WAIT;
167*4882a593Smuzhiyun 		mei_txe_br_reg_write(hw, SICR_HOST_ALIVENESS_REQ_REG, req);
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 	return do_req;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  * mei_txe_aliveness_req_get - get aliveness requested register value
175*4882a593Smuzhiyun  *
176*4882a593Smuzhiyun  * @dev: the device structure
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * Extract HICR_HOST_ALIVENESS_RESP_ACK bit from
179*4882a593Smuzhiyun  * from HICR_HOST_ALIVENESS_REQ register value
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  * Return: SICR_HOST_ALIVENESS_REQ_REQUESTED bit value
182*4882a593Smuzhiyun  */
mei_txe_aliveness_req_get(struct mei_device * dev)183*4882a593Smuzhiyun static u32 mei_txe_aliveness_req_get(struct mei_device *dev)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
186*4882a593Smuzhiyun 	u32 reg;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	reg = mei_txe_br_reg_read(hw, SICR_HOST_ALIVENESS_REQ_REG);
189*4882a593Smuzhiyun 	return reg & SICR_HOST_ALIVENESS_REQ_REQUESTED;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun  * mei_txe_aliveness_get - get aliveness response register value
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * @dev: the device structure
196*4882a593Smuzhiyun  *
197*4882a593Smuzhiyun  * Return: HICR_HOST_ALIVENESS_RESP_ACK bit from HICR_HOST_ALIVENESS_RESP
198*4882a593Smuzhiyun  *         register
199*4882a593Smuzhiyun  */
mei_txe_aliveness_get(struct mei_device * dev)200*4882a593Smuzhiyun static u32 mei_txe_aliveness_get(struct mei_device *dev)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
203*4882a593Smuzhiyun 	u32 reg;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	reg = mei_txe_br_reg_read(hw, HICR_HOST_ALIVENESS_RESP_REG);
206*4882a593Smuzhiyun 	return reg & HICR_HOST_ALIVENESS_RESP_ACK;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun /**
210*4882a593Smuzhiyun  * mei_txe_aliveness_poll - waits for aliveness to settle
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * @dev: the device structure
213*4882a593Smuzhiyun  * @expected: expected aliveness value
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * Polls for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
216*4882a593Smuzhiyun  *
217*4882a593Smuzhiyun  * Return: 0 if the expected value was received, -ETIME otherwise
218*4882a593Smuzhiyun  */
mei_txe_aliveness_poll(struct mei_device * dev,u32 expected)219*4882a593Smuzhiyun static int mei_txe_aliveness_poll(struct mei_device *dev, u32 expected)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
222*4882a593Smuzhiyun 	ktime_t stop, start;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	start = ktime_get();
225*4882a593Smuzhiyun 	stop = ktime_add(start, ms_to_ktime(SEC_ALIVENESS_WAIT_TIMEOUT));
226*4882a593Smuzhiyun 	do {
227*4882a593Smuzhiyun 		hw->aliveness = mei_txe_aliveness_get(dev);
228*4882a593Smuzhiyun 		if (hw->aliveness == expected) {
229*4882a593Smuzhiyun 			dev->pg_event = MEI_PG_EVENT_IDLE;
230*4882a593Smuzhiyun 			dev_dbg(dev->dev, "aliveness settled after %lld usecs\n",
231*4882a593Smuzhiyun 				ktime_to_us(ktime_sub(ktime_get(), start)));
232*4882a593Smuzhiyun 			return 0;
233*4882a593Smuzhiyun 		}
234*4882a593Smuzhiyun 		usleep_range(20, 50);
235*4882a593Smuzhiyun 	} while (ktime_before(ktime_get(), stop));
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	dev->pg_event = MEI_PG_EVENT_IDLE;
238*4882a593Smuzhiyun 	dev_err(dev->dev, "aliveness timed out\n");
239*4882a593Smuzhiyun 	return -ETIME;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun  * mei_txe_aliveness_wait - waits for aliveness to settle
244*4882a593Smuzhiyun  *
245*4882a593Smuzhiyun  * @dev: the device structure
246*4882a593Smuzhiyun  * @expected: expected aliveness value
247*4882a593Smuzhiyun  *
248*4882a593Smuzhiyun  * Waits for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
249*4882a593Smuzhiyun  *
250*4882a593Smuzhiyun  * Return: 0 on success and < 0 otherwise
251*4882a593Smuzhiyun  */
mei_txe_aliveness_wait(struct mei_device * dev,u32 expected)252*4882a593Smuzhiyun static int mei_txe_aliveness_wait(struct mei_device *dev, u32 expected)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
255*4882a593Smuzhiyun 	const unsigned long timeout =
256*4882a593Smuzhiyun 			msecs_to_jiffies(SEC_ALIVENESS_WAIT_TIMEOUT);
257*4882a593Smuzhiyun 	long err;
258*4882a593Smuzhiyun 	int ret;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	hw->aliveness = mei_txe_aliveness_get(dev);
261*4882a593Smuzhiyun 	if (hw->aliveness == expected)
262*4882a593Smuzhiyun 		return 0;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	mutex_unlock(&dev->device_lock);
265*4882a593Smuzhiyun 	err = wait_event_timeout(hw->wait_aliveness_resp,
266*4882a593Smuzhiyun 			dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
267*4882a593Smuzhiyun 	mutex_lock(&dev->device_lock);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	hw->aliveness = mei_txe_aliveness_get(dev);
270*4882a593Smuzhiyun 	ret = hw->aliveness == expected ? 0 : -ETIME;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	if (ret)
273*4882a593Smuzhiyun 		dev_warn(dev->dev, "aliveness timed out = %ld aliveness = %d event = %d\n",
274*4882a593Smuzhiyun 			err, hw->aliveness, dev->pg_event);
275*4882a593Smuzhiyun 	else
276*4882a593Smuzhiyun 		dev_dbg(dev->dev, "aliveness settled after = %d msec aliveness = %d event = %d\n",
277*4882a593Smuzhiyun 			jiffies_to_msecs(timeout - err),
278*4882a593Smuzhiyun 			hw->aliveness, dev->pg_event);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	dev->pg_event = MEI_PG_EVENT_IDLE;
281*4882a593Smuzhiyun 	return ret;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun  * mei_txe_aliveness_set_sync - sets an wait for aliveness to complete
286*4882a593Smuzhiyun  *
287*4882a593Smuzhiyun  * @dev: the device structure
288*4882a593Smuzhiyun  * @req: requested aliveness value
289*4882a593Smuzhiyun  *
290*4882a593Smuzhiyun  * Return: 0 on success and < 0 otherwise
291*4882a593Smuzhiyun  */
mei_txe_aliveness_set_sync(struct mei_device * dev,u32 req)292*4882a593Smuzhiyun int mei_txe_aliveness_set_sync(struct mei_device *dev, u32 req)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	if (mei_txe_aliveness_set(dev, req))
295*4882a593Smuzhiyun 		return mei_txe_aliveness_wait(dev, req);
296*4882a593Smuzhiyun 	return 0;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun /**
300*4882a593Smuzhiyun  * mei_txe_pg_in_transition - is device now in pg transition
301*4882a593Smuzhiyun  *
302*4882a593Smuzhiyun  * @dev: the device structure
303*4882a593Smuzhiyun  *
304*4882a593Smuzhiyun  * Return: true if in pg transition, false otherwise
305*4882a593Smuzhiyun  */
mei_txe_pg_in_transition(struct mei_device * dev)306*4882a593Smuzhiyun static bool mei_txe_pg_in_transition(struct mei_device *dev)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	return dev->pg_event == MEI_PG_EVENT_WAIT;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun /**
312*4882a593Smuzhiyun  * mei_txe_pg_is_enabled - detect if PG is supported by HW
313*4882a593Smuzhiyun  *
314*4882a593Smuzhiyun  * @dev: the device structure
315*4882a593Smuzhiyun  *
316*4882a593Smuzhiyun  * Return: true is pg supported, false otherwise
317*4882a593Smuzhiyun  */
mei_txe_pg_is_enabled(struct mei_device * dev)318*4882a593Smuzhiyun static bool mei_txe_pg_is_enabled(struct mei_device *dev)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	return true;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun /**
324*4882a593Smuzhiyun  * mei_txe_pg_state  - translate aliveness register value
325*4882a593Smuzhiyun  *   to the mei power gating state
326*4882a593Smuzhiyun  *
327*4882a593Smuzhiyun  * @dev: the device structure
328*4882a593Smuzhiyun  *
329*4882a593Smuzhiyun  * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
330*4882a593Smuzhiyun  */
mei_txe_pg_state(struct mei_device * dev)331*4882a593Smuzhiyun static inline enum mei_pg_state mei_txe_pg_state(struct mei_device *dev)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	return hw->aliveness ? MEI_PG_OFF : MEI_PG_ON;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /**
339*4882a593Smuzhiyun  * mei_txe_input_ready_interrupt_enable - sets the Input Ready Interrupt
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * @dev: the device structure
342*4882a593Smuzhiyun  */
mei_txe_input_ready_interrupt_enable(struct mei_device * dev)343*4882a593Smuzhiyun static void mei_txe_input_ready_interrupt_enable(struct mei_device *dev)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
346*4882a593Smuzhiyun 	u32 hintmsk;
347*4882a593Smuzhiyun 	/* Enable the SEC_IPC_HOST_INT_MASK_IN_RDY interrupt */
348*4882a593Smuzhiyun 	hintmsk = mei_txe_sec_reg_read(hw, SEC_IPC_HOST_INT_MASK_REG);
349*4882a593Smuzhiyun 	hintmsk |= SEC_IPC_HOST_INT_MASK_IN_RDY;
350*4882a593Smuzhiyun 	mei_txe_sec_reg_write(hw, SEC_IPC_HOST_INT_MASK_REG, hintmsk);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun /**
354*4882a593Smuzhiyun  * mei_txe_input_doorbell_set - sets bit 0 in
355*4882a593Smuzhiyun  *    SEC_IPC_INPUT_DOORBELL.IPC_INPUT_DOORBELL.
356*4882a593Smuzhiyun  *
357*4882a593Smuzhiyun  * @hw: the txe hardware structure
358*4882a593Smuzhiyun  */
mei_txe_input_doorbell_set(struct mei_txe_hw * hw)359*4882a593Smuzhiyun static void mei_txe_input_doorbell_set(struct mei_txe_hw *hw)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun 	/* Clear the interrupt cause */
362*4882a593Smuzhiyun 	clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause);
363*4882a593Smuzhiyun 	mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_DOORBELL_REG, 1);
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun /**
367*4882a593Smuzhiyun  * mei_txe_output_ready_set - Sets the SICR_SEC_IPC_OUTPUT_STATUS bit to 1
368*4882a593Smuzhiyun  *
369*4882a593Smuzhiyun  * @hw: the txe hardware structure
370*4882a593Smuzhiyun  */
mei_txe_output_ready_set(struct mei_txe_hw * hw)371*4882a593Smuzhiyun static void mei_txe_output_ready_set(struct mei_txe_hw *hw)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw,
374*4882a593Smuzhiyun 			SICR_SEC_IPC_OUTPUT_STATUS_REG,
375*4882a593Smuzhiyun 			SEC_IPC_OUTPUT_STATUS_RDY);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun /**
379*4882a593Smuzhiyun  * mei_txe_is_input_ready - check if TXE is ready for receiving data
380*4882a593Smuzhiyun  *
381*4882a593Smuzhiyun  * @dev: the device structure
382*4882a593Smuzhiyun  *
383*4882a593Smuzhiyun  * Return: true if INPUT STATUS READY bit is set
384*4882a593Smuzhiyun  */
mei_txe_is_input_ready(struct mei_device * dev)385*4882a593Smuzhiyun static bool mei_txe_is_input_ready(struct mei_device *dev)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
388*4882a593Smuzhiyun 	u32 status;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	status = mei_txe_sec_reg_read(hw, SEC_IPC_INPUT_STATUS_REG);
391*4882a593Smuzhiyun 	return !!(SEC_IPC_INPUT_STATUS_RDY & status);
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun /**
395*4882a593Smuzhiyun  * mei_txe_intr_clear - clear all interrupts
396*4882a593Smuzhiyun  *
397*4882a593Smuzhiyun  * @dev: the device structure
398*4882a593Smuzhiyun  */
mei_txe_intr_clear(struct mei_device * dev)399*4882a593Smuzhiyun static inline void mei_txe_intr_clear(struct mei_device *dev)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	mei_txe_sec_reg_write_silent(hw, SEC_IPC_HOST_INT_STATUS_REG,
404*4882a593Smuzhiyun 		SEC_IPC_HOST_INT_STATUS_PENDING);
405*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_STS_MSK);
406*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, HHISR_REG, IPC_HHIER_MSK);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun /**
410*4882a593Smuzhiyun  * mei_txe_intr_disable - disable all interrupts
411*4882a593Smuzhiyun  *
412*4882a593Smuzhiyun  * @dev: the device structure
413*4882a593Smuzhiyun  */
mei_txe_intr_disable(struct mei_device * dev)414*4882a593Smuzhiyun static void mei_txe_intr_disable(struct mei_device *dev)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, HHIER_REG, 0);
419*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, HIER_REG, 0);
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun /**
422*4882a593Smuzhiyun  * mei_txe_intr_enable - enable all interrupts
423*4882a593Smuzhiyun  *
424*4882a593Smuzhiyun  * @dev: the device structure
425*4882a593Smuzhiyun  */
mei_txe_intr_enable(struct mei_device * dev)426*4882a593Smuzhiyun static void mei_txe_intr_enable(struct mei_device *dev)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, HHIER_REG, IPC_HHIER_MSK);
431*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, HIER_REG, HIER_INT_EN_MSK);
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun /**
435*4882a593Smuzhiyun  * mei_txe_synchronize_irq - wait for pending IRQ handlers
436*4882a593Smuzhiyun  *
437*4882a593Smuzhiyun  * @dev: the device structure
438*4882a593Smuzhiyun  */
mei_txe_synchronize_irq(struct mei_device * dev)439*4882a593Smuzhiyun static void mei_txe_synchronize_irq(struct mei_device *dev)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(dev->dev);
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	synchronize_irq(pdev->irq);
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /**
447*4882a593Smuzhiyun  * mei_txe_pending_interrupts - check if there are pending interrupts
448*4882a593Smuzhiyun  *	only Aliveness, Input ready, and output doorbell are of relevance
449*4882a593Smuzhiyun  *
450*4882a593Smuzhiyun  * @dev: the device structure
451*4882a593Smuzhiyun  *
452*4882a593Smuzhiyun  * Checks if there are pending interrupts
453*4882a593Smuzhiyun  * only Aliveness, Readiness, Input ready, and Output doorbell are relevant
454*4882a593Smuzhiyun  *
455*4882a593Smuzhiyun  * Return: true if there are pending interrupts
456*4882a593Smuzhiyun  */
mei_txe_pending_interrupts(struct mei_device * dev)457*4882a593Smuzhiyun static bool mei_txe_pending_interrupts(struct mei_device *dev)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
461*4882a593Smuzhiyun 	bool ret = (hw->intr_cause & (TXE_INTR_READINESS |
462*4882a593Smuzhiyun 				      TXE_INTR_ALIVENESS |
463*4882a593Smuzhiyun 				      TXE_INTR_IN_READY  |
464*4882a593Smuzhiyun 				      TXE_INTR_OUT_DB));
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	if (ret) {
467*4882a593Smuzhiyun 		dev_dbg(dev->dev,
468*4882a593Smuzhiyun 			"Pending Interrupts InReady=%01d Readiness=%01d, Aliveness=%01d, OutDoor=%01d\n",
469*4882a593Smuzhiyun 			!!(hw->intr_cause & TXE_INTR_IN_READY),
470*4882a593Smuzhiyun 			!!(hw->intr_cause & TXE_INTR_READINESS),
471*4882a593Smuzhiyun 			!!(hw->intr_cause & TXE_INTR_ALIVENESS),
472*4882a593Smuzhiyun 			!!(hw->intr_cause & TXE_INTR_OUT_DB));
473*4882a593Smuzhiyun 	}
474*4882a593Smuzhiyun 	return ret;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun /**
478*4882a593Smuzhiyun  * mei_txe_input_payload_write - write a dword to the host buffer
479*4882a593Smuzhiyun  *	at offset idx
480*4882a593Smuzhiyun  *
481*4882a593Smuzhiyun  * @dev: the device structure
482*4882a593Smuzhiyun  * @idx: index in the host buffer
483*4882a593Smuzhiyun  * @value: value
484*4882a593Smuzhiyun  */
mei_txe_input_payload_write(struct mei_device * dev,unsigned long idx,u32 value)485*4882a593Smuzhiyun static void mei_txe_input_payload_write(struct mei_device *dev,
486*4882a593Smuzhiyun 			unsigned long idx, u32 value)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_PAYLOAD_REG +
491*4882a593Smuzhiyun 			(idx * sizeof(u32)), value);
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun /**
495*4882a593Smuzhiyun  * mei_txe_out_data_read - read dword from the device buffer
496*4882a593Smuzhiyun  *	at offset idx
497*4882a593Smuzhiyun  *
498*4882a593Smuzhiyun  * @dev: the device structure
499*4882a593Smuzhiyun  * @idx: index in the device buffer
500*4882a593Smuzhiyun  *
501*4882a593Smuzhiyun  * Return: register value at index
502*4882a593Smuzhiyun  */
mei_txe_out_data_read(const struct mei_device * dev,unsigned long idx)503*4882a593Smuzhiyun static u32 mei_txe_out_data_read(const struct mei_device *dev,
504*4882a593Smuzhiyun 					unsigned long idx)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	return mei_txe_br_reg_read(hw,
509*4882a593Smuzhiyun 		BRIDGE_IPC_OUTPUT_PAYLOAD_REG + (idx * sizeof(u32)));
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun /* Readiness */
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun /**
515*4882a593Smuzhiyun  * mei_txe_readiness_set_host_rdy - set host readiness bit
516*4882a593Smuzhiyun  *
517*4882a593Smuzhiyun  * @dev: the device structure
518*4882a593Smuzhiyun  */
mei_txe_readiness_set_host_rdy(struct mei_device * dev)519*4882a593Smuzhiyun static void mei_txe_readiness_set_host_rdy(struct mei_device *dev)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw,
524*4882a593Smuzhiyun 		SICR_HOST_IPC_READINESS_REQ_REG,
525*4882a593Smuzhiyun 		SICR_HOST_IPC_READINESS_HOST_RDY);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun /**
529*4882a593Smuzhiyun  * mei_txe_readiness_clear - clear host readiness bit
530*4882a593Smuzhiyun  *
531*4882a593Smuzhiyun  * @dev: the device structure
532*4882a593Smuzhiyun  */
mei_txe_readiness_clear(struct mei_device * dev)533*4882a593Smuzhiyun static void mei_txe_readiness_clear(struct mei_device *dev)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, SICR_HOST_IPC_READINESS_REQ_REG,
538*4882a593Smuzhiyun 				SICR_HOST_IPC_READINESS_RDY_CLR);
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun  * mei_txe_readiness_get - Reads and returns
542*4882a593Smuzhiyun  *	the HICR_SEC_IPC_READINESS register value
543*4882a593Smuzhiyun  *
544*4882a593Smuzhiyun  * @dev: the device structure
545*4882a593Smuzhiyun  *
546*4882a593Smuzhiyun  * Return: the HICR_SEC_IPC_READINESS register value
547*4882a593Smuzhiyun  */
mei_txe_readiness_get(struct mei_device * dev)548*4882a593Smuzhiyun static u32 mei_txe_readiness_get(struct mei_device *dev)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	return mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /**
557*4882a593Smuzhiyun  * mei_txe_readiness_is_sec_rdy - check readiness
558*4882a593Smuzhiyun  *  for HICR_SEC_IPC_READINESS_SEC_RDY
559*4882a593Smuzhiyun  *
560*4882a593Smuzhiyun  * @readiness: cached readiness state
561*4882a593Smuzhiyun  *
562*4882a593Smuzhiyun  * Return: true if readiness bit is set
563*4882a593Smuzhiyun  */
mei_txe_readiness_is_sec_rdy(u32 readiness)564*4882a593Smuzhiyun static inline bool mei_txe_readiness_is_sec_rdy(u32 readiness)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun 	return !!(readiness & HICR_SEC_IPC_READINESS_SEC_RDY);
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun /**
570*4882a593Smuzhiyun  * mei_txe_hw_is_ready - check if the hw is ready
571*4882a593Smuzhiyun  *
572*4882a593Smuzhiyun  * @dev: the device structure
573*4882a593Smuzhiyun  *
574*4882a593Smuzhiyun  * Return: true if sec is ready
575*4882a593Smuzhiyun  */
mei_txe_hw_is_ready(struct mei_device * dev)576*4882a593Smuzhiyun static bool mei_txe_hw_is_ready(struct mei_device *dev)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun 	u32 readiness =  mei_txe_readiness_get(dev);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	return mei_txe_readiness_is_sec_rdy(readiness);
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /**
584*4882a593Smuzhiyun  * mei_txe_host_is_ready - check if the host is ready
585*4882a593Smuzhiyun  *
586*4882a593Smuzhiyun  * @dev: the device structure
587*4882a593Smuzhiyun  *
588*4882a593Smuzhiyun  * Return: true if host is ready
589*4882a593Smuzhiyun  */
mei_txe_host_is_ready(struct mei_device * dev)590*4882a593Smuzhiyun static inline bool mei_txe_host_is_ready(struct mei_device *dev)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
593*4882a593Smuzhiyun 	u32 reg = mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	return !!(reg & HICR_SEC_IPC_READINESS_HOST_RDY);
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun /**
599*4882a593Smuzhiyun  * mei_txe_readiness_wait - wait till readiness settles
600*4882a593Smuzhiyun  *
601*4882a593Smuzhiyun  * @dev: the device structure
602*4882a593Smuzhiyun  *
603*4882a593Smuzhiyun  * Return: 0 on success and -ETIME on timeout
604*4882a593Smuzhiyun  */
mei_txe_readiness_wait(struct mei_device * dev)605*4882a593Smuzhiyun static int mei_txe_readiness_wait(struct mei_device *dev)
606*4882a593Smuzhiyun {
607*4882a593Smuzhiyun 	if (mei_txe_hw_is_ready(dev))
608*4882a593Smuzhiyun 		return 0;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	mutex_unlock(&dev->device_lock);
611*4882a593Smuzhiyun 	wait_event_timeout(dev->wait_hw_ready, dev->recvd_hw_ready,
612*4882a593Smuzhiyun 			msecs_to_jiffies(SEC_RESET_WAIT_TIMEOUT));
613*4882a593Smuzhiyun 	mutex_lock(&dev->device_lock);
614*4882a593Smuzhiyun 	if (!dev->recvd_hw_ready) {
615*4882a593Smuzhiyun 		dev_err(dev->dev, "wait for readiness failed\n");
616*4882a593Smuzhiyun 		return -ETIME;
617*4882a593Smuzhiyun 	}
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	dev->recvd_hw_ready = false;
620*4882a593Smuzhiyun 	return 0;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun static const struct mei_fw_status mei_txe_fw_sts = {
624*4882a593Smuzhiyun 	.count = 2,
625*4882a593Smuzhiyun 	.status[0] = PCI_CFG_TXE_FW_STS0,
626*4882a593Smuzhiyun 	.status[1] = PCI_CFG_TXE_FW_STS1
627*4882a593Smuzhiyun };
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun /**
630*4882a593Smuzhiyun  * mei_txe_fw_status - read fw status register from pci config space
631*4882a593Smuzhiyun  *
632*4882a593Smuzhiyun  * @dev: mei device
633*4882a593Smuzhiyun  * @fw_status: fw status register values
634*4882a593Smuzhiyun  *
635*4882a593Smuzhiyun  * Return: 0 on success, error otherwise
636*4882a593Smuzhiyun  */
mei_txe_fw_status(struct mei_device * dev,struct mei_fw_status * fw_status)637*4882a593Smuzhiyun static int mei_txe_fw_status(struct mei_device *dev,
638*4882a593Smuzhiyun 			     struct mei_fw_status *fw_status)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun 	const struct mei_fw_status *fw_src = &mei_txe_fw_sts;
641*4882a593Smuzhiyun 	struct pci_dev *pdev = to_pci_dev(dev->dev);
642*4882a593Smuzhiyun 	int ret;
643*4882a593Smuzhiyun 	int i;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	if (!fw_status)
646*4882a593Smuzhiyun 		return -EINVAL;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	fw_status->count = fw_src->count;
649*4882a593Smuzhiyun 	for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
650*4882a593Smuzhiyun 		ret = pci_read_config_dword(pdev, fw_src->status[i],
651*4882a593Smuzhiyun 					    &fw_status->status[i]);
652*4882a593Smuzhiyun 		trace_mei_pci_cfg_read(dev->dev, "PCI_CFG_HSF_X",
653*4882a593Smuzhiyun 				       fw_src->status[i],
654*4882a593Smuzhiyun 				       fw_status->status[i]);
655*4882a593Smuzhiyun 		if (ret)
656*4882a593Smuzhiyun 			return ret;
657*4882a593Smuzhiyun 	}
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	return 0;
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun /**
663*4882a593Smuzhiyun  * mei_txe_hw_config - configure hardware at the start of the devices
664*4882a593Smuzhiyun  *
665*4882a593Smuzhiyun  * @dev: the device structure
666*4882a593Smuzhiyun  *
667*4882a593Smuzhiyun  * Configure hardware at the start of the device should be done only
668*4882a593Smuzhiyun  *   once at the device probe time
669*4882a593Smuzhiyun  *
670*4882a593Smuzhiyun  * Return: always 0
671*4882a593Smuzhiyun  */
mei_txe_hw_config(struct mei_device * dev)672*4882a593Smuzhiyun static int mei_txe_hw_config(struct mei_device *dev)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	hw->aliveness = mei_txe_aliveness_get(dev);
678*4882a593Smuzhiyun 	hw->readiness = mei_txe_readiness_get(dev);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	dev_dbg(dev->dev, "aliveness_resp = 0x%08x, readiness = 0x%08x.\n",
681*4882a593Smuzhiyun 		hw->aliveness, hw->readiness);
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	return 0;
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun /**
687*4882a593Smuzhiyun  * mei_txe_write - writes a message to device.
688*4882a593Smuzhiyun  *
689*4882a593Smuzhiyun  * @dev: the device structure
690*4882a593Smuzhiyun  * @hdr: header of message
691*4882a593Smuzhiyun  * @hdr_len: header length in bytes - must multiplication of a slot (4bytes)
692*4882a593Smuzhiyun  * @data: payload
693*4882a593Smuzhiyun  * @data_len: paylead length in bytes
694*4882a593Smuzhiyun  *
695*4882a593Smuzhiyun  * Return: 0 if success, < 0 - otherwise.
696*4882a593Smuzhiyun  */
mei_txe_write(struct mei_device * dev,const void * hdr,size_t hdr_len,const void * data,size_t data_len)697*4882a593Smuzhiyun static int mei_txe_write(struct mei_device *dev,
698*4882a593Smuzhiyun 			 const void *hdr, size_t hdr_len,
699*4882a593Smuzhiyun 			 const void *data, size_t data_len)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
702*4882a593Smuzhiyun 	unsigned long rem;
703*4882a593Smuzhiyun 	const u32 *reg_buf;
704*4882a593Smuzhiyun 	u32 slots = TXE_HBUF_DEPTH;
705*4882a593Smuzhiyun 	u32 dw_cnt;
706*4882a593Smuzhiyun 	unsigned long i, j;
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	if (WARN_ON(!hdr || !data || hdr_len & 0x3))
709*4882a593Smuzhiyun 		return -EINVAL;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM((struct mei_msg_hdr *)hdr));
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	dw_cnt = mei_data2slots(hdr_len + data_len);
714*4882a593Smuzhiyun 	if (dw_cnt > slots)
715*4882a593Smuzhiyun 		return -EMSGSIZE;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	if (WARN(!hw->aliveness, "txe write: aliveness not asserted\n"))
718*4882a593Smuzhiyun 		return -EAGAIN;
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	/* Enable Input Ready Interrupt. */
721*4882a593Smuzhiyun 	mei_txe_input_ready_interrupt_enable(dev);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	if (!mei_txe_is_input_ready(dev)) {
724*4882a593Smuzhiyun 		char fw_sts_str[MEI_FW_STATUS_STR_SZ];
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 		mei_fw_status_str(dev, fw_sts_str, MEI_FW_STATUS_STR_SZ);
727*4882a593Smuzhiyun 		dev_err(dev->dev, "Input is not ready %s\n", fw_sts_str);
728*4882a593Smuzhiyun 		return -EAGAIN;
729*4882a593Smuzhiyun 	}
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	reg_buf = hdr;
732*4882a593Smuzhiyun 	for (i = 0; i < hdr_len / MEI_SLOT_SIZE; i++)
733*4882a593Smuzhiyun 		mei_txe_input_payload_write(dev, i, reg_buf[i]);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	reg_buf = data;
736*4882a593Smuzhiyun 	for (j = 0; j < data_len / MEI_SLOT_SIZE; j++)
737*4882a593Smuzhiyun 		mei_txe_input_payload_write(dev, i + j, reg_buf[j]);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	rem = data_len & 0x3;
740*4882a593Smuzhiyun 	if (rem > 0) {
741*4882a593Smuzhiyun 		u32 reg = 0;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 		memcpy(&reg, (const u8 *)data + data_len - rem, rem);
744*4882a593Smuzhiyun 		mei_txe_input_payload_write(dev, i + j, reg);
745*4882a593Smuzhiyun 	}
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	/* after each write the whole buffer is consumed */
748*4882a593Smuzhiyun 	hw->slots = 0;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	/* Set Input-Doorbell */
751*4882a593Smuzhiyun 	mei_txe_input_doorbell_set(hw);
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	return 0;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun /**
757*4882a593Smuzhiyun  * mei_txe_hbuf_depth - mimics the me hbuf circular buffer
758*4882a593Smuzhiyun  *
759*4882a593Smuzhiyun  * @dev: the device structure
760*4882a593Smuzhiyun  *
761*4882a593Smuzhiyun  * Return: the TXE_HBUF_DEPTH
762*4882a593Smuzhiyun  */
mei_txe_hbuf_depth(const struct mei_device * dev)763*4882a593Smuzhiyun static u32 mei_txe_hbuf_depth(const struct mei_device *dev)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun 	return TXE_HBUF_DEPTH;
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun /**
769*4882a593Smuzhiyun  * mei_txe_hbuf_empty_slots - mimics the me hbuf circular buffer
770*4882a593Smuzhiyun  *
771*4882a593Smuzhiyun  * @dev: the device structure
772*4882a593Smuzhiyun  *
773*4882a593Smuzhiyun  * Return: always TXE_HBUF_DEPTH
774*4882a593Smuzhiyun  */
mei_txe_hbuf_empty_slots(struct mei_device * dev)775*4882a593Smuzhiyun static int mei_txe_hbuf_empty_slots(struct mei_device *dev)
776*4882a593Smuzhiyun {
777*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	return hw->slots;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun /**
783*4882a593Smuzhiyun  * mei_txe_count_full_read_slots - mimics the me device circular buffer
784*4882a593Smuzhiyun  *
785*4882a593Smuzhiyun  * @dev: the device structure
786*4882a593Smuzhiyun  *
787*4882a593Smuzhiyun  * Return: always buffer size in dwords count
788*4882a593Smuzhiyun  */
mei_txe_count_full_read_slots(struct mei_device * dev)789*4882a593Smuzhiyun static int mei_txe_count_full_read_slots(struct mei_device *dev)
790*4882a593Smuzhiyun {
791*4882a593Smuzhiyun 	/* read buffers has static size */
792*4882a593Smuzhiyun 	return TXE_HBUF_DEPTH;
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun /**
796*4882a593Smuzhiyun  * mei_txe_read_hdr - read message header which is always in 4 first bytes
797*4882a593Smuzhiyun  *
798*4882a593Smuzhiyun  * @dev: the device structure
799*4882a593Smuzhiyun  *
800*4882a593Smuzhiyun  * Return: mei message header
801*4882a593Smuzhiyun  */
802*4882a593Smuzhiyun 
mei_txe_read_hdr(const struct mei_device * dev)803*4882a593Smuzhiyun static u32 mei_txe_read_hdr(const struct mei_device *dev)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun 	return mei_txe_out_data_read(dev, 0);
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun /**
808*4882a593Smuzhiyun  * mei_txe_read - reads a message from the txe device.
809*4882a593Smuzhiyun  *
810*4882a593Smuzhiyun  * @dev: the device structure
811*4882a593Smuzhiyun  * @buf: message buffer will be written
812*4882a593Smuzhiyun  * @len: message size will be read
813*4882a593Smuzhiyun  *
814*4882a593Smuzhiyun  * Return: -EINVAL on error wrong argument and 0 on success
815*4882a593Smuzhiyun  */
mei_txe_read(struct mei_device * dev,unsigned char * buf,unsigned long len)816*4882a593Smuzhiyun static int mei_txe_read(struct mei_device *dev,
817*4882a593Smuzhiyun 		unsigned char *buf, unsigned long len)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
821*4882a593Smuzhiyun 	u32 *reg_buf, reg;
822*4882a593Smuzhiyun 	u32 rem;
823*4882a593Smuzhiyun 	u32 i;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	if (WARN_ON(!buf || !len))
826*4882a593Smuzhiyun 		return -EINVAL;
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	reg_buf = (u32 *)buf;
829*4882a593Smuzhiyun 	rem = len & 0x3;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	dev_dbg(dev->dev, "buffer-length = %lu buf[0]0x%08X\n",
832*4882a593Smuzhiyun 		len, mei_txe_out_data_read(dev, 0));
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	for (i = 0; i < len / MEI_SLOT_SIZE; i++) {
835*4882a593Smuzhiyun 		/* skip header: index starts from 1 */
836*4882a593Smuzhiyun 		reg = mei_txe_out_data_read(dev, i + 1);
837*4882a593Smuzhiyun 		dev_dbg(dev->dev, "buf[%d] = 0x%08X\n", i, reg);
838*4882a593Smuzhiyun 		*reg_buf++ = reg;
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	if (rem) {
842*4882a593Smuzhiyun 		reg = mei_txe_out_data_read(dev, i + 1);
843*4882a593Smuzhiyun 		memcpy(reg_buf, &reg, rem);
844*4882a593Smuzhiyun 	}
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	mei_txe_output_ready_set(hw);
847*4882a593Smuzhiyun 	return 0;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun /**
851*4882a593Smuzhiyun  * mei_txe_hw_reset - resets host and fw.
852*4882a593Smuzhiyun  *
853*4882a593Smuzhiyun  * @dev: the device structure
854*4882a593Smuzhiyun  * @intr_enable: if interrupt should be enabled after reset.
855*4882a593Smuzhiyun  *
856*4882a593Smuzhiyun  * Return: 0 on success and < 0 in case of error
857*4882a593Smuzhiyun  */
mei_txe_hw_reset(struct mei_device * dev,bool intr_enable)858*4882a593Smuzhiyun static int mei_txe_hw_reset(struct mei_device *dev, bool intr_enable)
859*4882a593Smuzhiyun {
860*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	u32 aliveness_req;
863*4882a593Smuzhiyun 	/*
864*4882a593Smuzhiyun 	 * read input doorbell to ensure consistency between  Bridge and SeC
865*4882a593Smuzhiyun 	 * return value might be garbage return
866*4882a593Smuzhiyun 	 */
867*4882a593Smuzhiyun 	(void)mei_txe_sec_reg_read_silent(hw, SEC_IPC_INPUT_DOORBELL_REG);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	aliveness_req = mei_txe_aliveness_req_get(dev);
870*4882a593Smuzhiyun 	hw->aliveness = mei_txe_aliveness_get(dev);
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	/* Disable interrupts in this stage we will poll */
873*4882a593Smuzhiyun 	mei_txe_intr_disable(dev);
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	/*
876*4882a593Smuzhiyun 	 * If Aliveness Request and Aliveness Response are not equal then
877*4882a593Smuzhiyun 	 * wait for them to be equal
878*4882a593Smuzhiyun 	 * Since we might have interrupts disabled - poll for it
879*4882a593Smuzhiyun 	 */
880*4882a593Smuzhiyun 	if (aliveness_req != hw->aliveness)
881*4882a593Smuzhiyun 		if (mei_txe_aliveness_poll(dev, aliveness_req) < 0) {
882*4882a593Smuzhiyun 			dev_err(dev->dev, "wait for aliveness settle failed ... bailing out\n");
883*4882a593Smuzhiyun 			return -EIO;
884*4882a593Smuzhiyun 		}
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	/*
887*4882a593Smuzhiyun 	 * If Aliveness Request and Aliveness Response are set then clear them
888*4882a593Smuzhiyun 	 */
889*4882a593Smuzhiyun 	if (aliveness_req) {
890*4882a593Smuzhiyun 		mei_txe_aliveness_set(dev, 0);
891*4882a593Smuzhiyun 		if (mei_txe_aliveness_poll(dev, 0) < 0) {
892*4882a593Smuzhiyun 			dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
893*4882a593Smuzhiyun 			return -EIO;
894*4882a593Smuzhiyun 		}
895*4882a593Smuzhiyun 	}
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	/*
898*4882a593Smuzhiyun 	 * Set readiness RDY_CLR bit
899*4882a593Smuzhiyun 	 */
900*4882a593Smuzhiyun 	mei_txe_readiness_clear(dev);
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	return 0;
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun /**
906*4882a593Smuzhiyun  * mei_txe_hw_start - start the hardware after reset
907*4882a593Smuzhiyun  *
908*4882a593Smuzhiyun  * @dev: the device structure
909*4882a593Smuzhiyun  *
910*4882a593Smuzhiyun  * Return: 0 on success an error code otherwise
911*4882a593Smuzhiyun  */
mei_txe_hw_start(struct mei_device * dev)912*4882a593Smuzhiyun static int mei_txe_hw_start(struct mei_device *dev)
913*4882a593Smuzhiyun {
914*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
915*4882a593Smuzhiyun 	int ret;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	u32 hisr;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	/* bring back interrupts */
920*4882a593Smuzhiyun 	mei_txe_intr_enable(dev);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	ret = mei_txe_readiness_wait(dev);
923*4882a593Smuzhiyun 	if (ret < 0) {
924*4882a593Smuzhiyun 		dev_err(dev->dev, "waiting for readiness failed\n");
925*4882a593Smuzhiyun 		return ret;
926*4882a593Smuzhiyun 	}
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	/*
929*4882a593Smuzhiyun 	 * If HISR.INT2_STS interrupt status bit is set then clear it.
930*4882a593Smuzhiyun 	 */
931*4882a593Smuzhiyun 	hisr = mei_txe_br_reg_read(hw, HISR_REG);
932*4882a593Smuzhiyun 	if (hisr & HISR_INT_2_STS)
933*4882a593Smuzhiyun 		mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_2_STS);
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	/* Clear the interrupt cause of OutputDoorbell */
936*4882a593Smuzhiyun 	clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause);
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	ret = mei_txe_aliveness_set_sync(dev, 1);
939*4882a593Smuzhiyun 	if (ret < 0) {
940*4882a593Smuzhiyun 		dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
941*4882a593Smuzhiyun 		return ret;
942*4882a593Smuzhiyun 	}
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	pm_runtime_set_active(dev->dev);
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	/* enable input ready interrupts:
947*4882a593Smuzhiyun 	 * SEC_IPC_HOST_INT_MASK.IPC_INPUT_READY_INT_MASK
948*4882a593Smuzhiyun 	 */
949*4882a593Smuzhiyun 	mei_txe_input_ready_interrupt_enable(dev);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	/*  Set the SICR_SEC_IPC_OUTPUT_STATUS.IPC_OUTPUT_READY bit */
953*4882a593Smuzhiyun 	mei_txe_output_ready_set(hw);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	/* Set bit SICR_HOST_IPC_READINESS.HOST_RDY
956*4882a593Smuzhiyun 	 */
957*4882a593Smuzhiyun 	mei_txe_readiness_set_host_rdy(dev);
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	return 0;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun /**
963*4882a593Smuzhiyun  * mei_txe_check_and_ack_intrs - translate multi BAR interrupt into
964*4882a593Smuzhiyun  *  single bit mask and acknowledge the interrupts
965*4882a593Smuzhiyun  *
966*4882a593Smuzhiyun  * @dev: the device structure
967*4882a593Smuzhiyun  * @do_ack: acknowledge interrupts
968*4882a593Smuzhiyun  *
969*4882a593Smuzhiyun  * Return: true if found interrupts to process.
970*4882a593Smuzhiyun  */
mei_txe_check_and_ack_intrs(struct mei_device * dev,bool do_ack)971*4882a593Smuzhiyun static bool mei_txe_check_and_ack_intrs(struct mei_device *dev, bool do_ack)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
974*4882a593Smuzhiyun 	u32 hisr;
975*4882a593Smuzhiyun 	u32 hhisr;
976*4882a593Smuzhiyun 	u32 ipc_isr;
977*4882a593Smuzhiyun 	u32 aliveness;
978*4882a593Smuzhiyun 	bool generated;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	/* read interrupt registers */
981*4882a593Smuzhiyun 	hhisr = mei_txe_br_reg_read(hw, HHISR_REG);
982*4882a593Smuzhiyun 	generated = (hhisr & IPC_HHIER_MSK);
983*4882a593Smuzhiyun 	if (!generated)
984*4882a593Smuzhiyun 		goto out;
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	hisr = mei_txe_br_reg_read(hw, HISR_REG);
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	aliveness = mei_txe_aliveness_get(dev);
989*4882a593Smuzhiyun 	if (hhisr & IPC_HHIER_SEC && aliveness) {
990*4882a593Smuzhiyun 		ipc_isr = mei_txe_sec_reg_read_silent(hw,
991*4882a593Smuzhiyun 				SEC_IPC_HOST_INT_STATUS_REG);
992*4882a593Smuzhiyun 	} else {
993*4882a593Smuzhiyun 		ipc_isr = 0;
994*4882a593Smuzhiyun 		hhisr &= ~IPC_HHIER_SEC;
995*4882a593Smuzhiyun 	}
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	generated = generated ||
998*4882a593Smuzhiyun 		(hisr & HISR_INT_STS_MSK) ||
999*4882a593Smuzhiyun 		(ipc_isr & SEC_IPC_HOST_INT_STATUS_PENDING);
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 	if (generated && do_ack) {
1002*4882a593Smuzhiyun 		/* Save the interrupt causes */
1003*4882a593Smuzhiyun 		hw->intr_cause |= hisr & HISR_INT_STS_MSK;
1004*4882a593Smuzhiyun 		if (ipc_isr & SEC_IPC_HOST_INT_STATUS_IN_RDY)
1005*4882a593Smuzhiyun 			hw->intr_cause |= TXE_INTR_IN_READY;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 		mei_txe_intr_disable(dev);
1009*4882a593Smuzhiyun 		/* Clear the interrupts in hierarchy:
1010*4882a593Smuzhiyun 		 * IPC and Bridge, than the High Level */
1011*4882a593Smuzhiyun 		mei_txe_sec_reg_write_silent(hw,
1012*4882a593Smuzhiyun 			SEC_IPC_HOST_INT_STATUS_REG, ipc_isr);
1013*4882a593Smuzhiyun 		mei_txe_br_reg_write(hw, HISR_REG, hisr);
1014*4882a593Smuzhiyun 		mei_txe_br_reg_write(hw, HHISR_REG, hhisr);
1015*4882a593Smuzhiyun 	}
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun out:
1018*4882a593Smuzhiyun 	return generated;
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun /**
1022*4882a593Smuzhiyun  * mei_txe_irq_quick_handler - The ISR of the MEI device
1023*4882a593Smuzhiyun  *
1024*4882a593Smuzhiyun  * @irq: The irq number
1025*4882a593Smuzhiyun  * @dev_id: pointer to the device structure
1026*4882a593Smuzhiyun  *
1027*4882a593Smuzhiyun  * Return: IRQ_WAKE_THREAD if interrupt is designed for the device
1028*4882a593Smuzhiyun  *         IRQ_NONE otherwise
1029*4882a593Smuzhiyun  */
mei_txe_irq_quick_handler(int irq,void * dev_id)1030*4882a593Smuzhiyun irqreturn_t mei_txe_irq_quick_handler(int irq, void *dev_id)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun 	struct mei_device *dev = dev_id;
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	if (mei_txe_check_and_ack_intrs(dev, true))
1035*4882a593Smuzhiyun 		return IRQ_WAKE_THREAD;
1036*4882a593Smuzhiyun 	return IRQ_NONE;
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun /**
1041*4882a593Smuzhiyun  * mei_txe_irq_thread_handler - txe interrupt thread
1042*4882a593Smuzhiyun  *
1043*4882a593Smuzhiyun  * @irq: The irq number
1044*4882a593Smuzhiyun  * @dev_id: pointer to the device structure
1045*4882a593Smuzhiyun  *
1046*4882a593Smuzhiyun  * Return: IRQ_HANDLED
1047*4882a593Smuzhiyun  */
mei_txe_irq_thread_handler(int irq,void * dev_id)1048*4882a593Smuzhiyun irqreturn_t mei_txe_irq_thread_handler(int irq, void *dev_id)
1049*4882a593Smuzhiyun {
1050*4882a593Smuzhiyun 	struct mei_device *dev = (struct mei_device *) dev_id;
1051*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
1052*4882a593Smuzhiyun 	struct list_head cmpl_list;
1053*4882a593Smuzhiyun 	s32 slots;
1054*4882a593Smuzhiyun 	int rets = 0;
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	dev_dbg(dev->dev, "irq thread: Interrupt Registers HHISR|HISR|SEC=%02X|%04X|%02X\n",
1057*4882a593Smuzhiyun 		mei_txe_br_reg_read(hw, HHISR_REG),
1058*4882a593Smuzhiyun 		mei_txe_br_reg_read(hw, HISR_REG),
1059*4882a593Smuzhiyun 		mei_txe_sec_reg_read_silent(hw, SEC_IPC_HOST_INT_STATUS_REG));
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	/* initialize our complete list */
1063*4882a593Smuzhiyun 	mutex_lock(&dev->device_lock);
1064*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cmpl_list);
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
1067*4882a593Smuzhiyun 		mei_txe_check_and_ack_intrs(dev, true);
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	/* show irq events */
1070*4882a593Smuzhiyun 	mei_txe_pending_interrupts(dev);
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	hw->aliveness = mei_txe_aliveness_get(dev);
1073*4882a593Smuzhiyun 	hw->readiness = mei_txe_readiness_get(dev);
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	/* Readiness:
1076*4882a593Smuzhiyun 	 * Detection of TXE driver going through reset
1077*4882a593Smuzhiyun 	 * or TXE driver resetting the HECI interface.
1078*4882a593Smuzhiyun 	 */
1079*4882a593Smuzhiyun 	if (test_and_clear_bit(TXE_INTR_READINESS_BIT, &hw->intr_cause)) {
1080*4882a593Smuzhiyun 		dev_dbg(dev->dev, "Readiness Interrupt was received...\n");
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 		/* Check if SeC is going through reset */
1083*4882a593Smuzhiyun 		if (mei_txe_readiness_is_sec_rdy(hw->readiness)) {
1084*4882a593Smuzhiyun 			dev_dbg(dev->dev, "we need to start the dev.\n");
1085*4882a593Smuzhiyun 			dev->recvd_hw_ready = true;
1086*4882a593Smuzhiyun 		} else {
1087*4882a593Smuzhiyun 			dev->recvd_hw_ready = false;
1088*4882a593Smuzhiyun 			if (dev->dev_state != MEI_DEV_RESETTING) {
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 				dev_warn(dev->dev, "FW not ready: resetting.\n");
1091*4882a593Smuzhiyun 				schedule_work(&dev->reset_work);
1092*4882a593Smuzhiyun 				goto end;
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 			}
1095*4882a593Smuzhiyun 		}
1096*4882a593Smuzhiyun 		wake_up(&dev->wait_hw_ready);
1097*4882a593Smuzhiyun 	}
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	/************************************************************/
1100*4882a593Smuzhiyun 	/* Check interrupt cause:
1101*4882a593Smuzhiyun 	 * Aliveness: Detection of SeC acknowledge of host request that
1102*4882a593Smuzhiyun 	 * it remain alive or host cancellation of that request.
1103*4882a593Smuzhiyun 	 */
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	if (test_and_clear_bit(TXE_INTR_ALIVENESS_BIT, &hw->intr_cause)) {
1106*4882a593Smuzhiyun 		/* Clear the interrupt cause */
1107*4882a593Smuzhiyun 		dev_dbg(dev->dev,
1108*4882a593Smuzhiyun 			"Aliveness Interrupt: Status: %d\n", hw->aliveness);
1109*4882a593Smuzhiyun 		dev->pg_event = MEI_PG_EVENT_RECEIVED;
1110*4882a593Smuzhiyun 		if (waitqueue_active(&hw->wait_aliveness_resp))
1111*4882a593Smuzhiyun 			wake_up(&hw->wait_aliveness_resp);
1112*4882a593Smuzhiyun 	}
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	/* Output Doorbell:
1116*4882a593Smuzhiyun 	 * Detection of SeC having sent output to host
1117*4882a593Smuzhiyun 	 */
1118*4882a593Smuzhiyun 	slots = mei_count_full_read_slots(dev);
1119*4882a593Smuzhiyun 	if (test_and_clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause)) {
1120*4882a593Smuzhiyun 		/* Read from TXE */
1121*4882a593Smuzhiyun 		rets = mei_irq_read_handler(dev, &cmpl_list, &slots);
1122*4882a593Smuzhiyun 		if (rets &&
1123*4882a593Smuzhiyun 		    (dev->dev_state != MEI_DEV_RESETTING &&
1124*4882a593Smuzhiyun 		     dev->dev_state != MEI_DEV_POWER_DOWN)) {
1125*4882a593Smuzhiyun 			dev_err(dev->dev,
1126*4882a593Smuzhiyun 				"mei_irq_read_handler ret = %d.\n", rets);
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 			schedule_work(&dev->reset_work);
1129*4882a593Smuzhiyun 			goto end;
1130*4882a593Smuzhiyun 		}
1131*4882a593Smuzhiyun 	}
1132*4882a593Smuzhiyun 	/* Input Ready: Detection if host can write to SeC */
1133*4882a593Smuzhiyun 	if (test_and_clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause)) {
1134*4882a593Smuzhiyun 		dev->hbuf_is_ready = true;
1135*4882a593Smuzhiyun 		hw->slots = TXE_HBUF_DEPTH;
1136*4882a593Smuzhiyun 	}
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	if (hw->aliveness && dev->hbuf_is_ready) {
1139*4882a593Smuzhiyun 		/* get the real register value */
1140*4882a593Smuzhiyun 		dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
1141*4882a593Smuzhiyun 		rets = mei_irq_write_handler(dev, &cmpl_list);
1142*4882a593Smuzhiyun 		if (rets && rets != -EMSGSIZE)
1143*4882a593Smuzhiyun 			dev_err(dev->dev, "mei_irq_write_handler ret = %d.\n",
1144*4882a593Smuzhiyun 				rets);
1145*4882a593Smuzhiyun 		dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
1146*4882a593Smuzhiyun 	}
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	mei_irq_compl_handler(dev, &cmpl_list);
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun end:
1151*4882a593Smuzhiyun 	dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	mutex_unlock(&dev->device_lock);
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	mei_enable_interrupts(dev);
1156*4882a593Smuzhiyun 	return IRQ_HANDLED;
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun static const struct mei_hw_ops mei_txe_hw_ops = {
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	.host_is_ready = mei_txe_host_is_ready,
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	.fw_status = mei_txe_fw_status,
1164*4882a593Smuzhiyun 	.pg_state = mei_txe_pg_state,
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	.hw_is_ready = mei_txe_hw_is_ready,
1167*4882a593Smuzhiyun 	.hw_reset = mei_txe_hw_reset,
1168*4882a593Smuzhiyun 	.hw_config = mei_txe_hw_config,
1169*4882a593Smuzhiyun 	.hw_start = mei_txe_hw_start,
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	.pg_in_transition = mei_txe_pg_in_transition,
1172*4882a593Smuzhiyun 	.pg_is_enabled = mei_txe_pg_is_enabled,
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	.intr_clear = mei_txe_intr_clear,
1175*4882a593Smuzhiyun 	.intr_enable = mei_txe_intr_enable,
1176*4882a593Smuzhiyun 	.intr_disable = mei_txe_intr_disable,
1177*4882a593Smuzhiyun 	.synchronize_irq = mei_txe_synchronize_irq,
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	.hbuf_free_slots = mei_txe_hbuf_empty_slots,
1180*4882a593Smuzhiyun 	.hbuf_is_ready = mei_txe_is_input_ready,
1181*4882a593Smuzhiyun 	.hbuf_depth = mei_txe_hbuf_depth,
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	.write = mei_txe_write,
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	.rdbuf_full_slots = mei_txe_count_full_read_slots,
1186*4882a593Smuzhiyun 	.read_hdr = mei_txe_read_hdr,
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	.read = mei_txe_read,
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun };
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun /**
1193*4882a593Smuzhiyun  * mei_txe_dev_init - allocates and initializes txe hardware specific structure
1194*4882a593Smuzhiyun  *
1195*4882a593Smuzhiyun  * @pdev: pci device
1196*4882a593Smuzhiyun  *
1197*4882a593Smuzhiyun  * Return: struct mei_device * on success or NULL
1198*4882a593Smuzhiyun  */
mei_txe_dev_init(struct pci_dev * pdev)1199*4882a593Smuzhiyun struct mei_device *mei_txe_dev_init(struct pci_dev *pdev)
1200*4882a593Smuzhiyun {
1201*4882a593Smuzhiyun 	struct mei_device *dev;
1202*4882a593Smuzhiyun 	struct mei_txe_hw *hw;
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev) + sizeof(*hw), GFP_KERNEL);
1205*4882a593Smuzhiyun 	if (!dev)
1206*4882a593Smuzhiyun 		return NULL;
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 	mei_device_init(dev, &pdev->dev, &mei_txe_hw_ops);
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	hw = to_txe_hw(dev);
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	init_waitqueue_head(&hw->wait_aliveness_resp);
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	return dev;
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun /**
1218*4882a593Smuzhiyun  * mei_txe_setup_satt2 - SATT2 configuration for DMA support.
1219*4882a593Smuzhiyun  *
1220*4882a593Smuzhiyun  * @dev:   the device structure
1221*4882a593Smuzhiyun  * @addr:  physical address start of the range
1222*4882a593Smuzhiyun  * @range: physical range size
1223*4882a593Smuzhiyun  *
1224*4882a593Smuzhiyun  * Return: 0 on success an error code otherwise
1225*4882a593Smuzhiyun  */
mei_txe_setup_satt2(struct mei_device * dev,phys_addr_t addr,u32 range)1226*4882a593Smuzhiyun int mei_txe_setup_satt2(struct mei_device *dev, phys_addr_t addr, u32 range)
1227*4882a593Smuzhiyun {
1228*4882a593Smuzhiyun 	struct mei_txe_hw *hw = to_txe_hw(dev);
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	u32 lo32 = lower_32_bits(addr);
1231*4882a593Smuzhiyun 	u32 hi32 = upper_32_bits(addr);
1232*4882a593Smuzhiyun 	u32 ctrl;
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	/* SATT is limited to 36 Bits */
1235*4882a593Smuzhiyun 	if (hi32 & ~0xF)
1236*4882a593Smuzhiyun 		return -EINVAL;
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	/* SATT has to be 16Byte aligned */
1239*4882a593Smuzhiyun 	if (lo32 & 0xF)
1240*4882a593Smuzhiyun 		return -EINVAL;
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 	/* SATT range has to be 4Bytes aligned */
1243*4882a593Smuzhiyun 	if (range & 0x4)
1244*4882a593Smuzhiyun 		return -EINVAL;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	/* SATT is limited to 32 MB range*/
1247*4882a593Smuzhiyun 	if (range > SATT_RANGE_MAX)
1248*4882a593Smuzhiyun 		return -EINVAL;
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 	ctrl = SATT2_CTRL_VALID_MSK;
1251*4882a593Smuzhiyun 	ctrl |= hi32  << SATT2_CTRL_BR_BASE_ADDR_REG_SHIFT;
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, SATT2_SAP_SIZE_REG, range);
1254*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, SATT2_BRG_BA_LSB_REG, lo32);
1255*4882a593Smuzhiyun 	mei_txe_br_reg_write(hw, SATT2_CTRL_REG, ctrl);
1256*4882a593Smuzhiyun 	dev_dbg(dev->dev, "SATT2: SAP_SIZE_OFFSET=0x%08X, BRG_BA_LSB_OFFSET=0x%08X, CTRL_OFFSET=0x%08X\n",
1257*4882a593Smuzhiyun 		range, lo32, ctrl);
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	return 0;
1260*4882a593Smuzhiyun }
1261