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