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