blob: c5e1902e493f854a845f0114cc50c38aabfcfefa [file] [log] [blame]
Tomas Winkler32e2b592014-01-16 00:58:34 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2013-2014, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16
17#include <linux/pci.h>
18#include <linux/jiffies.h>
19#include <linux/delay.h>
20#include <linux/kthread.h>
Stephen Rothwell4a221762014-02-21 16:38:28 +110021#include <linux/irqreturn.h>
Tomas Winkler32e2b592014-01-16 00:58:34 +020022
23#include <linux/mei.h>
24
25#include "mei_dev.h"
26#include "hw-txe.h"
27#include "client.h"
28#include "hbm.h"
29
30/**
Alexander Usyskince231392014-09-29 16:31:50 +030031 * mei_txe_reg_read - Reads 32bit data from the txe device
Tomas Winkler32e2b592014-01-16 00:58:34 +020032 *
33 * @base_addr: registers base address
34 * @offset: register offset
35 *
Alexander Usyskince231392014-09-29 16:31:50 +030036 * Return: register value
Tomas Winkler32e2b592014-01-16 00:58:34 +020037 */
38static inline u32 mei_txe_reg_read(void __iomem *base_addr,
39 unsigned long offset)
40{
41 return ioread32(base_addr + offset);
42}
43
44/**
Alexander Usyskince231392014-09-29 16:31:50 +030045 * mei_txe_reg_write - Writes 32bit data to the txe device
Tomas Winkler32e2b592014-01-16 00:58:34 +020046 *
47 * @base_addr: registers base address
48 * @offset: register offset
49 * @value: the value to write
50 */
51static inline void mei_txe_reg_write(void __iomem *base_addr,
52 unsigned long offset, u32 value)
53{
54 iowrite32(value, base_addr + offset);
55}
56
57/**
58 * mei_txe_sec_reg_read_silent - Reads 32bit data from the SeC BAR
59 *
Alexander Usyskince231392014-09-29 16:31:50 +030060 * @hw: the txe hardware structure
Tomas Winkler32e2b592014-01-16 00:58:34 +020061 * @offset: register offset
62 *
63 * Doesn't check for aliveness while Reads 32bit data from the SeC BAR
Alexander Usyskince231392014-09-29 16:31:50 +030064 *
65 * Return: register value
Tomas Winkler32e2b592014-01-16 00:58:34 +020066 */
67static inline u32 mei_txe_sec_reg_read_silent(struct mei_txe_hw *hw,
68 unsigned long offset)
69{
70 return mei_txe_reg_read(hw->mem_addr[SEC_BAR], offset);
71}
72
73/**
74 * mei_txe_sec_reg_read - Reads 32bit data from the SeC BAR
75 *
Alexander Usyskince231392014-09-29 16:31:50 +030076 * @hw: the txe hardware structure
Tomas Winkler32e2b592014-01-16 00:58:34 +020077 * @offset: register offset
78 *
79 * Reads 32bit data from the SeC BAR and shout loud if aliveness is not set
Alexander Usyskince231392014-09-29 16:31:50 +030080 *
81 * Return: register value
Tomas Winkler32e2b592014-01-16 00:58:34 +020082 */
83static inline u32 mei_txe_sec_reg_read(struct mei_txe_hw *hw,
84 unsigned long offset)
85{
86 WARN(!hw->aliveness, "sec read: aliveness not asserted\n");
87 return mei_txe_sec_reg_read_silent(hw, offset);
88}
89/**
90 * mei_txe_sec_reg_write_silent - Writes 32bit data to the SeC BAR
91 * doesn't check for aliveness
92 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030093 * @hw: the txe hardware structure
Tomas Winkler32e2b592014-01-16 00:58:34 +020094 * @offset: register offset
95 * @value: value to write
96 *
97 * Doesn't check for aliveness while writes 32bit data from to the SeC BAR
98 */
99static inline void mei_txe_sec_reg_write_silent(struct mei_txe_hw *hw,
100 unsigned long offset, u32 value)
101{
102 mei_txe_reg_write(hw->mem_addr[SEC_BAR], offset, value);
103}
104
105/**
106 * mei_txe_sec_reg_write - Writes 32bit data to the SeC BAR
107 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300108 * @hw: the txe hardware structure
Tomas Winkler32e2b592014-01-16 00:58:34 +0200109 * @offset: register offset
110 * @value: value to write
111 *
112 * Writes 32bit data from the SeC BAR and shout loud if aliveness is not set
113 */
114static inline void mei_txe_sec_reg_write(struct mei_txe_hw *hw,
115 unsigned long offset, u32 value)
116{
117 WARN(!hw->aliveness, "sec write: aliveness not asserted\n");
118 mei_txe_sec_reg_write_silent(hw, offset, value);
119}
120/**
121 * mei_txe_br_reg_read - Reads 32bit data from the Bridge BAR
122 *
Alexander Usyskince231392014-09-29 16:31:50 +0300123 * @hw: the txe hardware structure
Tomas Winkler32e2b592014-01-16 00:58:34 +0200124 * @offset: offset from which to read the data
125 *
Alexander Usyskince231392014-09-29 16:31:50 +0300126 * Return: the byte read.
Tomas Winkler32e2b592014-01-16 00:58:34 +0200127 */
128static inline u32 mei_txe_br_reg_read(struct mei_txe_hw *hw,
129 unsigned long offset)
130{
131 return mei_txe_reg_read(hw->mem_addr[BRIDGE_BAR], offset);
132}
133
134/**
135 * mei_txe_br_reg_write - Writes 32bit data to the Bridge BAR
136 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300137 * @hw: the txe hardware structure
Tomas Winkler32e2b592014-01-16 00:58:34 +0200138 * @offset: offset from which to write the data
139 * @value: the byte to write
140 */
141static inline void mei_txe_br_reg_write(struct mei_txe_hw *hw,
142 unsigned long offset, u32 value)
143{
144 mei_txe_reg_write(hw->mem_addr[BRIDGE_BAR], offset, value);
145}
146
147/**
148 * mei_txe_aliveness_set - request for aliveness change
149 *
150 * @dev: the device structure
151 * @req: requested aliveness value
152 *
153 * Request for aliveness change and returns true if the change is
154 * really needed and false if aliveness is already
155 * in the requested state
Alexander Usyskince231392014-09-29 16:31:50 +0300156 *
157 * Locking: called under "dev->device_lock" lock
158 *
159 * Return: true if request was send
Tomas Winkler32e2b592014-01-16 00:58:34 +0200160 */
161static bool mei_txe_aliveness_set(struct mei_device *dev, u32 req)
162{
163
164 struct mei_txe_hw *hw = to_txe_hw(dev);
165 bool do_req = hw->aliveness != req;
166
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300167 dev_dbg(dev->dev, "Aliveness current=%d request=%d\n",
Tomas Winkler32e2b592014-01-16 00:58:34 +0200168 hw->aliveness, req);
169 if (do_req) {
Tomas Winkler964a2332014-03-18 22:51:59 +0200170 dev->pg_event = MEI_PG_EVENT_WAIT;
Tomas Winkler32e2b592014-01-16 00:58:34 +0200171 mei_txe_br_reg_write(hw, SICR_HOST_ALIVENESS_REQ_REG, req);
172 }
173 return do_req;
174}
175
176
177/**
178 * mei_txe_aliveness_req_get - get aliveness requested register value
179 *
180 * @dev: the device structure
181 *
182 * Extract HICR_HOST_ALIVENESS_RESP_ACK bit from
183 * from HICR_HOST_ALIVENESS_REQ register value
Alexander Usyskince231392014-09-29 16:31:50 +0300184 *
185 * Return: SICR_HOST_ALIVENESS_REQ_REQUESTED bit value
Tomas Winkler32e2b592014-01-16 00:58:34 +0200186 */
187static u32 mei_txe_aliveness_req_get(struct mei_device *dev)
188{
189 struct mei_txe_hw *hw = to_txe_hw(dev);
190 u32 reg;
Tomas Winkler92db1552014-09-29 16:31:37 +0300191
Tomas Winkler32e2b592014-01-16 00:58:34 +0200192 reg = mei_txe_br_reg_read(hw, SICR_HOST_ALIVENESS_REQ_REG);
193 return reg & SICR_HOST_ALIVENESS_REQ_REQUESTED;
194}
195
196/**
197 * mei_txe_aliveness_get - get aliveness response register value
Alexander Usyskince231392014-09-29 16:31:50 +0300198 *
Tomas Winkler32e2b592014-01-16 00:58:34 +0200199 * @dev: the device structure
200 *
Alexander Usyskince231392014-09-29 16:31:50 +0300201 * Return: HICR_HOST_ALIVENESS_RESP_ACK bit from HICR_HOST_ALIVENESS_RESP
202 * register
Tomas Winkler32e2b592014-01-16 00:58:34 +0200203 */
204static u32 mei_txe_aliveness_get(struct mei_device *dev)
205{
206 struct mei_txe_hw *hw = to_txe_hw(dev);
207 u32 reg;
Tomas Winkler92db1552014-09-29 16:31:37 +0300208
Tomas Winkler32e2b592014-01-16 00:58:34 +0200209 reg = mei_txe_br_reg_read(hw, HICR_HOST_ALIVENESS_RESP_REG);
210 return reg & HICR_HOST_ALIVENESS_RESP_ACK;
211}
212
213/**
214 * mei_txe_aliveness_poll - waits for aliveness to settle
215 *
216 * @dev: the device structure
217 * @expected: expected aliveness value
218 *
219 * Polls for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300220 *
221 * Return: > 0 if the expected value was received, -ETIME otherwise
Tomas Winkler32e2b592014-01-16 00:58:34 +0200222 */
223static int mei_txe_aliveness_poll(struct mei_device *dev, u32 expected)
224{
225 struct mei_txe_hw *hw = to_txe_hw(dev);
226 int t = 0;
227
228 do {
229 hw->aliveness = mei_txe_aliveness_get(dev);
230 if (hw->aliveness == expected) {
Tomas Winkler964a2332014-03-18 22:51:59 +0200231 dev->pg_event = MEI_PG_EVENT_IDLE;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300232 dev_dbg(dev->dev,
Tomas Winkler32e2b592014-01-16 00:58:34 +0200233 "aliveness settled after %d msecs\n", t);
234 return t;
235 }
236 mutex_unlock(&dev->device_lock);
237 msleep(MSEC_PER_SEC / 5);
238 mutex_lock(&dev->device_lock);
239 t += MSEC_PER_SEC / 5;
240 } while (t < SEC_ALIVENESS_WAIT_TIMEOUT);
241
Tomas Winkler964a2332014-03-18 22:51:59 +0200242 dev->pg_event = MEI_PG_EVENT_IDLE;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300243 dev_err(dev->dev, "aliveness timed out\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +0200244 return -ETIME;
245}
246
247/**
248 * mei_txe_aliveness_wait - waits for aliveness to settle
249 *
250 * @dev: the device structure
251 * @expected: expected aliveness value
252 *
253 * Waits for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300254 *
255 * Return: 0 on success and < 0 otherwise
Tomas Winkler32e2b592014-01-16 00:58:34 +0200256 */
257static int mei_txe_aliveness_wait(struct mei_device *dev, u32 expected)
258{
259 struct mei_txe_hw *hw = to_txe_hw(dev);
260 const unsigned long timeout =
261 msecs_to_jiffies(SEC_ALIVENESS_WAIT_TIMEOUT);
262 long err;
263 int ret;
264
265 hw->aliveness = mei_txe_aliveness_get(dev);
266 if (hw->aliveness == expected)
267 return 0;
268
269 mutex_unlock(&dev->device_lock);
Tomas Winkler964a2332014-03-18 22:51:59 +0200270 err = wait_event_timeout(hw->wait_aliveness_resp,
271 dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
Tomas Winkler32e2b592014-01-16 00:58:34 +0200272 mutex_lock(&dev->device_lock);
273
274 hw->aliveness = mei_txe_aliveness_get(dev);
275 ret = hw->aliveness == expected ? 0 : -ETIME;
276
277 if (ret)
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300278 dev_warn(dev->dev, "aliveness timed out = %ld aliveness = %d event = %d\n",
Tomas Winkler964a2332014-03-18 22:51:59 +0200279 err, hw->aliveness, dev->pg_event);
Tomas Winkler32e2b592014-01-16 00:58:34 +0200280 else
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300281 dev_dbg(dev->dev, "aliveness settled after = %d msec aliveness = %d event = %d\n",
Tomas Winkler964a2332014-03-18 22:51:59 +0200282 jiffies_to_msecs(timeout - err),
283 hw->aliveness, dev->pg_event);
284
285 dev->pg_event = MEI_PG_EVENT_IDLE;
Tomas Winkler32e2b592014-01-16 00:58:34 +0200286 return ret;
287}
288
289/**
290 * mei_txe_aliveness_set_sync - sets an wait for aliveness to complete
291 *
292 * @dev: the device structure
Alexander Usyskince231392014-09-29 16:31:50 +0300293 * @req: requested aliveness value
Tomas Winkler32e2b592014-01-16 00:58:34 +0200294 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300295 * Return: 0 on success and < 0 otherwise
Tomas Winkler32e2b592014-01-16 00:58:34 +0200296 */
297int mei_txe_aliveness_set_sync(struct mei_device *dev, u32 req)
298{
299 if (mei_txe_aliveness_set(dev, req))
300 return mei_txe_aliveness_wait(dev, req);
301 return 0;
302}
303
304/**
Tomas Winkleree7e5af2014-03-18 22:51:58 +0200305 * mei_txe_pg_is_enabled - detect if PG is supported by HW
306 *
307 * @dev: the device structure
308 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300309 * Return: true is pg supported, false otherwise
Tomas Winkleree7e5af2014-03-18 22:51:58 +0200310 */
311static bool mei_txe_pg_is_enabled(struct mei_device *dev)
312{
313 return true;
314}
315
316/**
Tomas Winkler964a2332014-03-18 22:51:59 +0200317 * mei_txe_pg_state - translate aliveness register value
318 * to the mei power gating state
319 *
320 * @dev: the device structure
321 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300322 * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
Tomas Winkler964a2332014-03-18 22:51:59 +0200323 */
324static inline enum mei_pg_state mei_txe_pg_state(struct mei_device *dev)
325{
326 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300327
Tomas Winkler964a2332014-03-18 22:51:59 +0200328 return hw->aliveness ? MEI_PG_OFF : MEI_PG_ON;
329}
330
331/**
Tomas Winkler32e2b592014-01-16 00:58:34 +0200332 * mei_txe_input_ready_interrupt_enable - sets the Input Ready Interrupt
333 *
334 * @dev: the device structure
335 */
336static void mei_txe_input_ready_interrupt_enable(struct mei_device *dev)
337{
338 struct mei_txe_hw *hw = to_txe_hw(dev);
339 u32 hintmsk;
340 /* Enable the SEC_IPC_HOST_INT_MASK_IN_RDY interrupt */
341 hintmsk = mei_txe_sec_reg_read(hw, SEC_IPC_HOST_INT_MASK_REG);
342 hintmsk |= SEC_IPC_HOST_INT_MASK_IN_RDY;
343 mei_txe_sec_reg_write(hw, SEC_IPC_HOST_INT_MASK_REG, hintmsk);
344}
345
346/**
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300347 * mei_txe_input_doorbell_set - sets bit 0 in
348 * SEC_IPC_INPUT_DOORBELL.IPC_INPUT_DOORBELL.
349 *
350 * @hw: the txe hardware structure
Tomas Winkler32e2b592014-01-16 00:58:34 +0200351 */
352static void mei_txe_input_doorbell_set(struct mei_txe_hw *hw)
353{
354 /* Clear the interrupt cause */
355 clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause);
356 mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_DOORBELL_REG, 1);
357}
358
359/**
360 * mei_txe_output_ready_set - Sets the SICR_SEC_IPC_OUTPUT_STATUS bit to 1
361 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300362 * @hw: the txe hardware structure
Tomas Winkler32e2b592014-01-16 00:58:34 +0200363 */
364static void mei_txe_output_ready_set(struct mei_txe_hw *hw)
365{
366 mei_txe_br_reg_write(hw,
367 SICR_SEC_IPC_OUTPUT_STATUS_REG,
368 SEC_IPC_OUTPUT_STATUS_RDY);
369}
370
371/**
372 * mei_txe_is_input_ready - check if TXE is ready for receiving data
373 *
374 * @dev: the device structure
Alexander Usyskince231392014-09-29 16:31:50 +0300375 *
376 * Return: true if INPUT STATUS READY bit is set
Tomas Winkler32e2b592014-01-16 00:58:34 +0200377 */
378static bool mei_txe_is_input_ready(struct mei_device *dev)
379{
380 struct mei_txe_hw *hw = to_txe_hw(dev);
381 u32 status;
Tomas Winkler92db1552014-09-29 16:31:37 +0300382
Tomas Winkler32e2b592014-01-16 00:58:34 +0200383 status = mei_txe_sec_reg_read(hw, SEC_IPC_INPUT_STATUS_REG);
384 return !!(SEC_IPC_INPUT_STATUS_RDY & status);
385}
386
387/**
388 * mei_txe_intr_clear - clear all interrupts
389 *
390 * @dev: the device structure
391 */
392static inline void mei_txe_intr_clear(struct mei_device *dev)
393{
394 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300395
Tomas Winkler32e2b592014-01-16 00:58:34 +0200396 mei_txe_sec_reg_write_silent(hw, SEC_IPC_HOST_INT_STATUS_REG,
397 SEC_IPC_HOST_INT_STATUS_PENDING);
398 mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_STS_MSK);
399 mei_txe_br_reg_write(hw, HHISR_REG, IPC_HHIER_MSK);
400}
401
402/**
403 * mei_txe_intr_disable - disable all interrupts
404 *
405 * @dev: the device structure
406 */
407static void mei_txe_intr_disable(struct mei_device *dev)
408{
409 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300410
Tomas Winkler32e2b592014-01-16 00:58:34 +0200411 mei_txe_br_reg_write(hw, HHIER_REG, 0);
412 mei_txe_br_reg_write(hw, HIER_REG, 0);
413}
414/**
415 * mei_txe_intr_disable - enable all interrupts
416 *
417 * @dev: the device structure
418 */
419static void mei_txe_intr_enable(struct mei_device *dev)
420{
421 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300422
Tomas Winkler32e2b592014-01-16 00:58:34 +0200423 mei_txe_br_reg_write(hw, HHIER_REG, IPC_HHIER_MSK);
424 mei_txe_br_reg_write(hw, HIER_REG, HIER_INT_EN_MSK);
425}
426
427/**
428 * mei_txe_pending_interrupts - check if there are pending interrupts
429 * only Aliveness, Input ready, and output doorbell are of relevance
430 *
431 * @dev: the device structure
432 *
433 * Checks if there are pending interrupts
434 * only Aliveness, Readiness, Input ready, and Output doorbell are relevant
Alexander Usyskince231392014-09-29 16:31:50 +0300435 *
436 * Return: true if there are pending interrupts
Tomas Winkler32e2b592014-01-16 00:58:34 +0200437 */
438static bool mei_txe_pending_interrupts(struct mei_device *dev)
439{
440
441 struct mei_txe_hw *hw = to_txe_hw(dev);
442 bool ret = (hw->intr_cause & (TXE_INTR_READINESS |
443 TXE_INTR_ALIVENESS |
444 TXE_INTR_IN_READY |
445 TXE_INTR_OUT_DB));
446
447 if (ret) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300448 dev_dbg(dev->dev,
Tomas Winkler32e2b592014-01-16 00:58:34 +0200449 "Pending Interrupts InReady=%01d Readiness=%01d, Aliveness=%01d, OutDoor=%01d\n",
450 !!(hw->intr_cause & TXE_INTR_IN_READY),
451 !!(hw->intr_cause & TXE_INTR_READINESS),
452 !!(hw->intr_cause & TXE_INTR_ALIVENESS),
453 !!(hw->intr_cause & TXE_INTR_OUT_DB));
454 }
455 return ret;
456}
457
458/**
459 * mei_txe_input_payload_write - write a dword to the host buffer
460 * at offset idx
461 *
462 * @dev: the device structure
463 * @idx: index in the host buffer
464 * @value: value
465 */
466static void mei_txe_input_payload_write(struct mei_device *dev,
467 unsigned long idx, u32 value)
468{
469 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300470
Tomas Winkler32e2b592014-01-16 00:58:34 +0200471 mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_PAYLOAD_REG +
472 (idx * sizeof(u32)), value);
473}
474
475/**
476 * mei_txe_out_data_read - read dword from the device buffer
477 * at offset idx
478 *
479 * @dev: the device structure
480 * @idx: index in the device buffer
481 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300482 * Return: register value at index
Tomas Winkler32e2b592014-01-16 00:58:34 +0200483 */
484static u32 mei_txe_out_data_read(const struct mei_device *dev,
485 unsigned long idx)
486{
487 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300488
Tomas Winkler32e2b592014-01-16 00:58:34 +0200489 return mei_txe_br_reg_read(hw,
490 BRIDGE_IPC_OUTPUT_PAYLOAD_REG + (idx * sizeof(u32)));
491}
492
493/* Readiness */
494
495/**
Alexander Usyskince231392014-09-29 16:31:50 +0300496 * mei_txe_readiness_set_host_rdy - set host readiness bit
Tomas Winkler32e2b592014-01-16 00:58:34 +0200497 *
498 * @dev: the device structure
499 */
500static void mei_txe_readiness_set_host_rdy(struct mei_device *dev)
501{
502 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300503
Tomas Winkler32e2b592014-01-16 00:58:34 +0200504 mei_txe_br_reg_write(hw,
505 SICR_HOST_IPC_READINESS_REQ_REG,
506 SICR_HOST_IPC_READINESS_HOST_RDY);
507}
508
509/**
Alexander Usyskince231392014-09-29 16:31:50 +0300510 * mei_txe_readiness_clear - clear host readiness bit
Tomas Winkler32e2b592014-01-16 00:58:34 +0200511 *
512 * @dev: the device structure
513 */
514static void mei_txe_readiness_clear(struct mei_device *dev)
515{
516 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300517
Tomas Winkler32e2b592014-01-16 00:58:34 +0200518 mei_txe_br_reg_write(hw, SICR_HOST_IPC_READINESS_REQ_REG,
519 SICR_HOST_IPC_READINESS_RDY_CLR);
520}
521/**
522 * mei_txe_readiness_get - Reads and returns
523 * the HICR_SEC_IPC_READINESS register value
524 *
525 * @dev: the device structure
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300526 *
527 * Return: the HICR_SEC_IPC_READINESS register value
Tomas Winkler32e2b592014-01-16 00:58:34 +0200528 */
529static u32 mei_txe_readiness_get(struct mei_device *dev)
530{
531 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300532
Tomas Winkler32e2b592014-01-16 00:58:34 +0200533 return mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
534}
535
536
537/**
538 * mei_txe_readiness_is_sec_rdy - check readiness
539 * for HICR_SEC_IPC_READINESS_SEC_RDY
540 *
Alexander Usyskince231392014-09-29 16:31:50 +0300541 * @readiness: cached readiness state
542 *
543 * Return: true if readiness bit is set
Tomas Winkler32e2b592014-01-16 00:58:34 +0200544 */
545static inline bool mei_txe_readiness_is_sec_rdy(u32 readiness)
546{
547 return !!(readiness & HICR_SEC_IPC_READINESS_SEC_RDY);
548}
549
550/**
551 * mei_txe_hw_is_ready - check if the hw is ready
552 *
553 * @dev: the device structure
Alexander Usyskince231392014-09-29 16:31:50 +0300554 *
555 * Return: true if sec is ready
Tomas Winkler32e2b592014-01-16 00:58:34 +0200556 */
557static bool mei_txe_hw_is_ready(struct mei_device *dev)
558{
559 u32 readiness = mei_txe_readiness_get(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300560
Tomas Winkler32e2b592014-01-16 00:58:34 +0200561 return mei_txe_readiness_is_sec_rdy(readiness);
562}
563
564/**
565 * mei_txe_host_is_ready - check if the host is ready
566 *
567 * @dev: the device structure
Alexander Usyskince231392014-09-29 16:31:50 +0300568 *
569 * Return: true if host is ready
Tomas Winkler32e2b592014-01-16 00:58:34 +0200570 */
571static inline bool mei_txe_host_is_ready(struct mei_device *dev)
572{
573 struct mei_txe_hw *hw = to_txe_hw(dev);
574 u32 reg = mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
Tomas Winkler92db1552014-09-29 16:31:37 +0300575
Tomas Winkler32e2b592014-01-16 00:58:34 +0200576 return !!(reg & HICR_SEC_IPC_READINESS_HOST_RDY);
577}
578
579/**
580 * mei_txe_readiness_wait - wait till readiness settles
581 *
582 * @dev: the device structure
583 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300584 * Return: 0 on success and -ETIME on timeout
Tomas Winkler32e2b592014-01-16 00:58:34 +0200585 */
586static int mei_txe_readiness_wait(struct mei_device *dev)
587{
588 if (mei_txe_hw_is_ready(dev))
589 return 0;
590
591 mutex_unlock(&dev->device_lock);
592 wait_event_timeout(dev->wait_hw_ready, dev->recvd_hw_ready,
593 msecs_to_jiffies(SEC_RESET_WAIT_TIMEOUT));
594 mutex_lock(&dev->device_lock);
595 if (!dev->recvd_hw_ready) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300596 dev_err(dev->dev, "wait for readiness failed\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +0200597 return -ETIME;
598 }
599
600 dev->recvd_hw_ready = false;
601 return 0;
602}
603
Fengguang Wu480bd3c2014-09-29 18:21:46 -0700604static const struct mei_fw_status mei_txe_fw_sts = {
Tomas Winkler4ad96db2014-09-29 16:31:45 +0300605 .count = 2,
606 .status[0] = PCI_CFG_TXE_FW_STS0,
607 .status[1] = PCI_CFG_TXE_FW_STS1
608};
Tomas Winkler1bd30b62014-09-29 16:31:43 +0300609
610/**
611 * mei_txe_fw_status - read fw status register from pci config space
612 *
613 * @dev: mei device
614 * @fw_status: fw status register values
Alexander Usyskince231392014-09-29 16:31:50 +0300615 *
616 * Return: 0 on success, error otherwise
Tomas Winkler1bd30b62014-09-29 16:31:43 +0300617 */
618static int mei_txe_fw_status(struct mei_device *dev,
619 struct mei_fw_status *fw_status)
620{
Tomas Winkler4ad96db2014-09-29 16:31:45 +0300621 const struct mei_fw_status *fw_src = &mei_txe_fw_sts;
Tomas Winkler1bd30b62014-09-29 16:31:43 +0300622 struct pci_dev *pdev = to_pci_dev(dev->dev);
623 int ret;
624 int i;
625
626 if (!fw_status)
627 return -EINVAL;
628
629 fw_status->count = fw_src->count;
630 for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
631 ret = pci_read_config_dword(pdev,
632 fw_src->status[i], &fw_status->status[i]);
633 if (ret)
634 return ret;
635 }
636
637 return 0;
638}
639
Tomas Winkler32e2b592014-01-16 00:58:34 +0200640/**
641 * mei_txe_hw_config - configure hardware at the start of the devices
642 *
643 * @dev: the device structure
644 *
645 * Configure hardware at the start of the device should be done only
646 * once at the device probe time
647 */
648static void mei_txe_hw_config(struct mei_device *dev)
649{
650
651 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300652
Tomas Winkler32e2b592014-01-16 00:58:34 +0200653 /* Doesn't change in runtime */
654 dev->hbuf_depth = PAYLOAD_SIZE / 4;
655
656 hw->aliveness = mei_txe_aliveness_get(dev);
657 hw->readiness = mei_txe_readiness_get(dev);
658
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300659 dev_dbg(dev->dev, "aliveness_resp = 0x%08x, readiness = 0x%08x.\n",
Tomas Winkler32e2b592014-01-16 00:58:34 +0200660 hw->aliveness, hw->readiness);
661}
662
663
664/**
665 * mei_txe_write - writes a message to device.
666 *
667 * @dev: the device structure
668 * @header: header of message
669 * @buf: message buffer will be written
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300670 *
Alexander Usyskince231392014-09-29 16:31:50 +0300671 * Return: 0 if success, <0 - otherwise.
Tomas Winkler32e2b592014-01-16 00:58:34 +0200672 */
673
674static int mei_txe_write(struct mei_device *dev,
675 struct mei_msg_hdr *header, unsigned char *buf)
676{
677 struct mei_txe_hw *hw = to_txe_hw(dev);
678 unsigned long rem;
679 unsigned long length;
Tomas Winkler9d098192014-02-19 17:35:48 +0200680 int slots = dev->hbuf_depth;
Tomas Winkler32e2b592014-01-16 00:58:34 +0200681 u32 *reg_buf = (u32 *)buf;
Tomas Winkler9d098192014-02-19 17:35:48 +0200682 u32 dw_cnt;
Tomas Winkler32e2b592014-01-16 00:58:34 +0200683 int i;
684
685 if (WARN_ON(!header || !buf))
686 return -EINVAL;
687
688 length = header->length;
689
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300690 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
Tomas Winkler32e2b592014-01-16 00:58:34 +0200691
Tomas Winkler9d098192014-02-19 17:35:48 +0200692 dw_cnt = mei_data2slots(length);
693 if (dw_cnt > slots)
694 return -EMSGSIZE;
Tomas Winkler32e2b592014-01-16 00:58:34 +0200695
696 if (WARN(!hw->aliveness, "txe write: aliveness not asserted\n"))
697 return -EAGAIN;
698
699 /* Enable Input Ready Interrupt. */
700 mei_txe_input_ready_interrupt_enable(dev);
701
702 if (!mei_txe_is_input_ready(dev)) {
Alexander Usyskin04dd3662014-03-31 17:59:23 +0300703 struct mei_fw_status fw_status;
Tomas Winkler92db1552014-09-29 16:31:37 +0300704
Alexander Usyskin04dd3662014-03-31 17:59:23 +0300705 mei_fw_status(dev, &fw_status);
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300706 dev_err(dev->dev, "Input is not ready " FW_STS_FMT "\n",
Alexander Usyskin04dd3662014-03-31 17:59:23 +0300707 FW_STS_PRM(fw_status));
Tomas Winkler32e2b592014-01-16 00:58:34 +0200708 return -EAGAIN;
709 }
710
711 mei_txe_input_payload_write(dev, 0, *((u32 *)header));
712
713 for (i = 0; i < length / 4; i++)
714 mei_txe_input_payload_write(dev, i + 1, reg_buf[i]);
715
716 rem = length & 0x3;
717 if (rem > 0) {
718 u32 reg = 0;
Tomas Winkler92db1552014-09-29 16:31:37 +0300719
Tomas Winkler32e2b592014-01-16 00:58:34 +0200720 memcpy(&reg, &buf[length - rem], rem);
721 mei_txe_input_payload_write(dev, i + 1, reg);
722 }
723
Tomas Winkler9d098192014-02-19 17:35:48 +0200724 /* after each write the whole buffer is consumed */
725 hw->slots = 0;
726
Tomas Winkler32e2b592014-01-16 00:58:34 +0200727 /* Set Input-Doorbell */
728 mei_txe_input_doorbell_set(hw);
729
730 return 0;
731}
732
733/**
734 * mei_txe_hbuf_max_len - mimics the me hbuf circular buffer
735 *
736 * @dev: the device structure
737 *
Alexander Usyskince231392014-09-29 16:31:50 +0300738 * Return: the PAYLOAD_SIZE - 4
Tomas Winkler32e2b592014-01-16 00:58:34 +0200739 */
740static size_t mei_txe_hbuf_max_len(const struct mei_device *dev)
741{
742 return PAYLOAD_SIZE - sizeof(struct mei_msg_hdr);
743}
744
745/**
746 * mei_txe_hbuf_empty_slots - mimics the me hbuf circular buffer
747 *
748 * @dev: the device structure
749 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300750 * Return: always hbuf_depth
Tomas Winkler32e2b592014-01-16 00:58:34 +0200751 */
752static int mei_txe_hbuf_empty_slots(struct mei_device *dev)
753{
Tomas Winkler9d098192014-02-19 17:35:48 +0200754 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300755
Tomas Winkler9d098192014-02-19 17:35:48 +0200756 return hw->slots;
Tomas Winkler32e2b592014-01-16 00:58:34 +0200757}
758
759/**
760 * mei_txe_count_full_read_slots - mimics the me device circular buffer
761 *
762 * @dev: the device structure
763 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300764 * Return: always buffer size in dwords count
Tomas Winkler32e2b592014-01-16 00:58:34 +0200765 */
766static int mei_txe_count_full_read_slots(struct mei_device *dev)
767{
768 /* read buffers has static size */
769 return PAYLOAD_SIZE / 4;
770}
771
772/**
773 * mei_txe_read_hdr - read message header which is always in 4 first bytes
774 *
775 * @dev: the device structure
776 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300777 * Return: mei message header
Tomas Winkler32e2b592014-01-16 00:58:34 +0200778 */
779
780static u32 mei_txe_read_hdr(const struct mei_device *dev)
781{
782 return mei_txe_out_data_read(dev, 0);
783}
784/**
785 * mei_txe_read - reads a message from the txe device.
786 *
787 * @dev: the device structure
788 * @buf: message buffer will be written
789 * @len: message size will be read
790 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300791 * Return: -EINVAL on error wrong argument and 0 on success
Tomas Winkler32e2b592014-01-16 00:58:34 +0200792 */
793static int mei_txe_read(struct mei_device *dev,
794 unsigned char *buf, unsigned long len)
795{
796
797 struct mei_txe_hw *hw = to_txe_hw(dev);
Tomas Winkler92db1552014-09-29 16:31:37 +0300798 u32 *reg_buf, reg;
799 u32 rem;
Tomas Winkler32e2b592014-01-16 00:58:34 +0200800 u32 i;
Tomas Winkler32e2b592014-01-16 00:58:34 +0200801
802 if (WARN_ON(!buf || !len))
803 return -EINVAL;
804
Tomas Winkler92db1552014-09-29 16:31:37 +0300805 reg_buf = (u32 *)buf;
806 rem = len & 0x3;
807
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300808 dev_dbg(dev->dev, "buffer-length = %lu buf[0]0x%08X\n",
Tomas Winkler32e2b592014-01-16 00:58:34 +0200809 len, mei_txe_out_data_read(dev, 0));
810
811 for (i = 0; i < len / 4; i++) {
812 /* skip header: index starts from 1 */
Tomas Winkler92db1552014-09-29 16:31:37 +0300813 reg = mei_txe_out_data_read(dev, i + 1);
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300814 dev_dbg(dev->dev, "buf[%d] = 0x%08X\n", i, reg);
Tomas Winkler32e2b592014-01-16 00:58:34 +0200815 *reg_buf++ = reg;
816 }
817
818 if (rem) {
Tomas Winkler92db1552014-09-29 16:31:37 +0300819 reg = mei_txe_out_data_read(dev, i + 1);
Tomas Winkler32e2b592014-01-16 00:58:34 +0200820 memcpy(reg_buf, &reg, rem);
821 }
822
823 mei_txe_output_ready_set(hw);
824 return 0;
825}
826
827/**
828 * mei_txe_hw_reset - resets host and fw.
829 *
830 * @dev: the device structure
831 * @intr_enable: if interrupt should be enabled after reset.
832 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300833 * Return: 0 on success and < 0 in case of error
Tomas Winkler32e2b592014-01-16 00:58:34 +0200834 */
835static int mei_txe_hw_reset(struct mei_device *dev, bool intr_enable)
836{
837 struct mei_txe_hw *hw = to_txe_hw(dev);
838
839 u32 aliveness_req;
840 /*
841 * read input doorbell to ensure consistency between Bridge and SeC
842 * return value might be garbage return
843 */
844 (void)mei_txe_sec_reg_read_silent(hw, SEC_IPC_INPUT_DOORBELL_REG);
845
846 aliveness_req = mei_txe_aliveness_req_get(dev);
847 hw->aliveness = mei_txe_aliveness_get(dev);
848
849 /* Disable interrupts in this stage we will poll */
850 mei_txe_intr_disable(dev);
851
852 /*
853 * If Aliveness Request and Aliveness Response are not equal then
854 * wait for them to be equal
855 * Since we might have interrupts disabled - poll for it
856 */
857 if (aliveness_req != hw->aliveness)
858 if (mei_txe_aliveness_poll(dev, aliveness_req) < 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300859 dev_err(dev->dev, "wait for aliveness settle failed ... bailing out\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +0200860 return -EIO;
861 }
862
863 /*
864 * If Aliveness Request and Aliveness Response are set then clear them
865 */
866 if (aliveness_req) {
867 mei_txe_aliveness_set(dev, 0);
868 if (mei_txe_aliveness_poll(dev, 0) < 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300869 dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +0200870 return -EIO;
871 }
872 }
873
874 /*
Alexander Usyskin0a01e972014-09-29 16:31:47 +0300875 * Set readiness RDY_CLR bit
Tomas Winkler32e2b592014-01-16 00:58:34 +0200876 */
877 mei_txe_readiness_clear(dev);
878
879 return 0;
880}
881
882/**
883 * mei_txe_hw_start - start the hardware after reset
884 *
885 * @dev: the device structure
886 *
Alexander Usyskince231392014-09-29 16:31:50 +0300887 * Return: 0 on success an error code otherwise
Tomas Winkler32e2b592014-01-16 00:58:34 +0200888 */
889static int mei_txe_hw_start(struct mei_device *dev)
890{
891 struct mei_txe_hw *hw = to_txe_hw(dev);
892 int ret;
893
894 u32 hisr;
895
896 /* bring back interrupts */
897 mei_txe_intr_enable(dev);
898
899 ret = mei_txe_readiness_wait(dev);
900 if (ret < 0) {
Alexander Usyskin0a01e972014-09-29 16:31:47 +0300901 dev_err(dev->dev, "waiting for readiness failed\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +0200902 return ret;
903 }
904
905 /*
906 * If HISR.INT2_STS interrupt status bit is set then clear it.
907 */
908 hisr = mei_txe_br_reg_read(hw, HISR_REG);
909 if (hisr & HISR_INT_2_STS)
910 mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_2_STS);
911
912 /* Clear the interrupt cause of OutputDoorbell */
913 clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause);
914
915 ret = mei_txe_aliveness_set_sync(dev, 1);
916 if (ret < 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300917 dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +0200918 return ret;
919 }
920
921 /* enable input ready interrupts:
922 * SEC_IPC_HOST_INT_MASK.IPC_INPUT_READY_INT_MASK
923 */
924 mei_txe_input_ready_interrupt_enable(dev);
925
926
927 /* Set the SICR_SEC_IPC_OUTPUT_STATUS.IPC_OUTPUT_READY bit */
928 mei_txe_output_ready_set(hw);
929
930 /* Set bit SICR_HOST_IPC_READINESS.HOST_RDY
931 */
932 mei_txe_readiness_set_host_rdy(dev);
933
934 return 0;
935}
936
937/**
938 * mei_txe_check_and_ack_intrs - translate multi BAR interrupt into
939 * single bit mask and acknowledge the interrupts
940 *
941 * @dev: the device structure
942 * @do_ack: acknowledge interrupts
Alexander Usyskince231392014-09-29 16:31:50 +0300943 *
944 * Return: true if found interrupts to process.
Tomas Winkler32e2b592014-01-16 00:58:34 +0200945 */
946static bool mei_txe_check_and_ack_intrs(struct mei_device *dev, bool do_ack)
947{
948 struct mei_txe_hw *hw = to_txe_hw(dev);
949 u32 hisr;
950 u32 hhisr;
951 u32 ipc_isr;
952 u32 aliveness;
953 bool generated;
954
955 /* read interrupt registers */
956 hhisr = mei_txe_br_reg_read(hw, HHISR_REG);
957 generated = (hhisr & IPC_HHIER_MSK);
958 if (!generated)
959 goto out;
960
961 hisr = mei_txe_br_reg_read(hw, HISR_REG);
962
963 aliveness = mei_txe_aliveness_get(dev);
964 if (hhisr & IPC_HHIER_SEC && aliveness)
965 ipc_isr = mei_txe_sec_reg_read_silent(hw,
966 SEC_IPC_HOST_INT_STATUS_REG);
967 else
968 ipc_isr = 0;
969
970 generated = generated ||
971 (hisr & HISR_INT_STS_MSK) ||
972 (ipc_isr & SEC_IPC_HOST_INT_STATUS_PENDING);
973
974 if (generated && do_ack) {
975 /* Save the interrupt causes */
976 hw->intr_cause |= hisr & HISR_INT_STS_MSK;
977 if (ipc_isr & SEC_IPC_HOST_INT_STATUS_IN_RDY)
978 hw->intr_cause |= TXE_INTR_IN_READY;
979
980
981 mei_txe_intr_disable(dev);
982 /* Clear the interrupts in hierarchy:
983 * IPC and Bridge, than the High Level */
984 mei_txe_sec_reg_write_silent(hw,
985 SEC_IPC_HOST_INT_STATUS_REG, ipc_isr);
986 mei_txe_br_reg_write(hw, HISR_REG, hisr);
987 mei_txe_br_reg_write(hw, HHISR_REG, hhisr);
988 }
989
990out:
991 return generated;
992}
993
994/**
995 * mei_txe_irq_quick_handler - The ISR of the MEI device
996 *
997 * @irq: The irq number
998 * @dev_id: pointer to the device structure
999 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001000 * Return: IRQ_WAKE_THREAD if interrupt is designed for the device
1001 * IRQ_NONE otherwise
Tomas Winkler32e2b592014-01-16 00:58:34 +02001002 */
1003irqreturn_t mei_txe_irq_quick_handler(int irq, void *dev_id)
1004{
1005 struct mei_device *dev = dev_id;
1006
1007 if (mei_txe_check_and_ack_intrs(dev, true))
1008 return IRQ_WAKE_THREAD;
1009 return IRQ_NONE;
1010}
1011
1012
1013/**
1014 * mei_txe_irq_thread_handler - txe interrupt thread
1015 *
1016 * @irq: The irq number
1017 * @dev_id: pointer to the device structure
1018 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +03001019 * Return: IRQ_HANDLED
Tomas Winkler32e2b592014-01-16 00:58:34 +02001020 */
1021irqreturn_t mei_txe_irq_thread_handler(int irq, void *dev_id)
1022{
1023 struct mei_device *dev = (struct mei_device *) dev_id;
1024 struct mei_txe_hw *hw = to_txe_hw(dev);
1025 struct mei_cl_cb complete_list;
1026 s32 slots;
1027 int rets = 0;
1028
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001029 dev_dbg(dev->dev, "irq thread: Interrupt Registers HHISR|HISR|SEC=%02X|%04X|%02X\n",
Tomas Winkler32e2b592014-01-16 00:58:34 +02001030 mei_txe_br_reg_read(hw, HHISR_REG),
1031 mei_txe_br_reg_read(hw, HISR_REG),
1032 mei_txe_sec_reg_read_silent(hw, SEC_IPC_HOST_INT_STATUS_REG));
1033
1034
1035 /* initialize our complete list */
1036 mutex_lock(&dev->device_lock);
1037 mei_io_list_init(&complete_list);
1038
Tomas Winklerd08b8fc2014-09-29 16:31:44 +03001039 if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
Tomas Winkler32e2b592014-01-16 00:58:34 +02001040 mei_txe_check_and_ack_intrs(dev, true);
1041
1042 /* show irq events */
1043 mei_txe_pending_interrupts(dev);
1044
1045 hw->aliveness = mei_txe_aliveness_get(dev);
1046 hw->readiness = mei_txe_readiness_get(dev);
1047
1048 /* Readiness:
1049 * Detection of TXE driver going through reset
1050 * or TXE driver resetting the HECI interface.
1051 */
1052 if (test_and_clear_bit(TXE_INTR_READINESS_BIT, &hw->intr_cause)) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001053 dev_dbg(dev->dev, "Readiness Interrupt was received...\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +02001054
1055 /* Check if SeC is going through reset */
1056 if (mei_txe_readiness_is_sec_rdy(hw->readiness)) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001057 dev_dbg(dev->dev, "we need to start the dev.\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +02001058 dev->recvd_hw_ready = true;
1059 } else {
1060 dev->recvd_hw_ready = false;
1061 if (dev->dev_state != MEI_DEV_RESETTING) {
1062
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001063 dev_warn(dev->dev, "FW not ready: resetting.\n");
Tomas Winkler32e2b592014-01-16 00:58:34 +02001064 schedule_work(&dev->reset_work);
1065 goto end;
1066
1067 }
1068 }
1069 wake_up(&dev->wait_hw_ready);
1070 }
1071
1072 /************************************************************/
1073 /* Check interrupt cause:
1074 * Aliveness: Detection of SeC acknowledge of host request that
1075 * it remain alive or host cancellation of that request.
1076 */
1077
1078 if (test_and_clear_bit(TXE_INTR_ALIVENESS_BIT, &hw->intr_cause)) {
1079 /* Clear the interrupt cause */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001080 dev_dbg(dev->dev,
Tomas Winkler32e2b592014-01-16 00:58:34 +02001081 "Aliveness Interrupt: Status: %d\n", hw->aliveness);
Tomas Winkler964a2332014-03-18 22:51:59 +02001082 dev->pg_event = MEI_PG_EVENT_RECEIVED;
1083 if (waitqueue_active(&hw->wait_aliveness_resp))
1084 wake_up(&hw->wait_aliveness_resp);
Tomas Winkler32e2b592014-01-16 00:58:34 +02001085 }
1086
1087
1088 /* Output Doorbell:
1089 * Detection of SeC having sent output to host
1090 */
1091 slots = mei_count_full_read_slots(dev);
1092 if (test_and_clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause)) {
1093 /* Read from TXE */
1094 rets = mei_irq_read_handler(dev, &complete_list, &slots);
1095 if (rets && dev->dev_state != MEI_DEV_RESETTING) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001096 dev_err(dev->dev,
Tomas Winkler32e2b592014-01-16 00:58:34 +02001097 "mei_irq_read_handler ret = %d.\n", rets);
1098
1099 schedule_work(&dev->reset_work);
1100 goto end;
1101 }
1102 }
1103 /* Input Ready: Detection if host can write to SeC */
Tomas Winkler9d098192014-02-19 17:35:48 +02001104 if (test_and_clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause)) {
Tomas Winkler32e2b592014-01-16 00:58:34 +02001105 dev->hbuf_is_ready = true;
Tomas Winkler9d098192014-02-19 17:35:48 +02001106 hw->slots = dev->hbuf_depth;
1107 }
Tomas Winkler32e2b592014-01-16 00:58:34 +02001108
1109 if (hw->aliveness && dev->hbuf_is_ready) {
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001110 /* get the real register value */
1111 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
Tomas Winkler32e2b592014-01-16 00:58:34 +02001112 rets = mei_irq_write_handler(dev, &complete_list);
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001113 if (rets && rets != -EMSGSIZE)
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001114 dev_err(dev->dev, "mei_irq_write_handler ret = %d.\n",
Tomas Winkler6aae48f2014-02-19 17:35:47 +02001115 rets);
1116 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
Tomas Winkler32e2b592014-01-16 00:58:34 +02001117 }
1118
Tomas Winkler32e2b592014-01-16 00:58:34 +02001119 mei_irq_compl_handler(dev, &complete_list);
1120
1121end:
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001122 dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
Tomas Winkler32e2b592014-01-16 00:58:34 +02001123
1124 mutex_unlock(&dev->device_lock);
1125
1126 mei_enable_interrupts(dev);
1127 return IRQ_HANDLED;
1128}
1129
1130static const struct mei_hw_ops mei_txe_hw_ops = {
1131
1132 .host_is_ready = mei_txe_host_is_ready,
1133
Tomas Winkler1bd30b62014-09-29 16:31:43 +03001134 .fw_status = mei_txe_fw_status,
Tomas Winkler964a2332014-03-18 22:51:59 +02001135 .pg_state = mei_txe_pg_state,
1136
Tomas Winkler32e2b592014-01-16 00:58:34 +02001137 .hw_is_ready = mei_txe_hw_is_ready,
1138 .hw_reset = mei_txe_hw_reset,
1139 .hw_config = mei_txe_hw_config,
1140 .hw_start = mei_txe_hw_start,
1141
Tomas Winkleree7e5af2014-03-18 22:51:58 +02001142 .pg_is_enabled = mei_txe_pg_is_enabled,
1143
Tomas Winkler32e2b592014-01-16 00:58:34 +02001144 .intr_clear = mei_txe_intr_clear,
1145 .intr_enable = mei_txe_intr_enable,
1146 .intr_disable = mei_txe_intr_disable,
1147
1148 .hbuf_free_slots = mei_txe_hbuf_empty_slots,
1149 .hbuf_is_ready = mei_txe_is_input_ready,
1150 .hbuf_max_len = mei_txe_hbuf_max_len,
1151
1152 .write = mei_txe_write,
1153
1154 .rdbuf_full_slots = mei_txe_count_full_read_slots,
1155 .read_hdr = mei_txe_read_hdr,
1156
1157 .read = mei_txe_read,
1158
1159};
1160
1161/**
1162 * mei_txe_dev_init - allocates and initializes txe hardware specific structure
1163 *
Alexander Usyskince231392014-09-29 16:31:50 +03001164 * @pdev: pci device
Alexander Usyskin8d929d42014-05-13 01:30:53 +03001165 *
Alexander Usyskince231392014-09-29 16:31:50 +03001166 * Return: struct mei_device * on success or NULL
Tomas Winkler32e2b592014-01-16 00:58:34 +02001167 */
Tomas Winkler4ad96db2014-09-29 16:31:45 +03001168struct mei_device *mei_txe_dev_init(struct pci_dev *pdev)
Tomas Winkler32e2b592014-01-16 00:58:34 +02001169{
1170 struct mei_device *dev;
1171 struct mei_txe_hw *hw;
1172
1173 dev = kzalloc(sizeof(struct mei_device) +
1174 sizeof(struct mei_txe_hw), GFP_KERNEL);
1175 if (!dev)
1176 return NULL;
1177
Tomas Winkler3a7e9b62014-09-29 16:31:41 +03001178 mei_device_init(dev, &pdev->dev, &mei_txe_hw_ops);
Tomas Winkler32e2b592014-01-16 00:58:34 +02001179
1180 hw = to_txe_hw(dev);
1181
Tomas Winkler964a2332014-03-18 22:51:59 +02001182 init_waitqueue_head(&hw->wait_aliveness_resp);
Tomas Winkler32e2b592014-01-16 00:58:34 +02001183
Tomas Winkler32e2b592014-01-16 00:58:34 +02001184 return dev;
1185}
1186
1187/**
1188 * mei_txe_setup_satt2 - SATT2 configuration for DMA support.
1189 *
1190 * @dev: the device structure
1191 * @addr: physical address start of the range
1192 * @range: physical range size
Alexander Usyskince231392014-09-29 16:31:50 +03001193 *
1194 * Return: 0 on success an error code otherwise
Tomas Winkler32e2b592014-01-16 00:58:34 +02001195 */
1196int mei_txe_setup_satt2(struct mei_device *dev, phys_addr_t addr, u32 range)
1197{
1198 struct mei_txe_hw *hw = to_txe_hw(dev);
1199
1200 u32 lo32 = lower_32_bits(addr);
1201 u32 hi32 = upper_32_bits(addr);
1202 u32 ctrl;
1203
1204 /* SATT is limited to 36 Bits */
1205 if (hi32 & ~0xF)
1206 return -EINVAL;
1207
1208 /* SATT has to be 16Byte aligned */
1209 if (lo32 & 0xF)
1210 return -EINVAL;
1211
1212 /* SATT range has to be 4Bytes aligned */
1213 if (range & 0x4)
1214 return -EINVAL;
1215
1216 /* SATT is limited to 32 MB range*/
1217 if (range > SATT_RANGE_MAX)
1218 return -EINVAL;
1219
1220 ctrl = SATT2_CTRL_VALID_MSK;
1221 ctrl |= hi32 << SATT2_CTRL_BR_BASE_ADDR_REG_SHIFT;
1222
1223 mei_txe_br_reg_write(hw, SATT2_SAP_SIZE_REG, range);
1224 mei_txe_br_reg_write(hw, SATT2_BRG_BA_LSB_REG, lo32);
1225 mei_txe_br_reg_write(hw, SATT2_CTRL_REG, ctrl);
Tomas Winkler2bf94cab2014-09-29 16:31:42 +03001226 dev_dbg(dev->dev, "SATT2: SAP_SIZE_OFFSET=0x%08X, BRG_BA_LSB_OFFSET=0x%08X, CTRL_OFFSET=0x%08X\n",
Tomas Winkler32e2b592014-01-16 00:58:34 +02001227 range, lo32, ctrl);
1228
1229 return 0;
1230}