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