mm: page_alloc: do not treat a zone that cannot be used for dirty pages as "full"
[linux/fpc-iii.git] / drivers / misc / mei / hw-me.c
blobfbc4a7bbdfa915ec9805168a6655bf3ff0901c2a
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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.
17 #include <linux/pci.h>
19 #include <linux/kthread.h>
20 #include <linux/interrupt.h>
22 #include "mei_dev.h"
23 #include "hw-me.h"
25 #include "hbm.h"
28 /**
29 * mei_me_reg_read - Reads 32bit data from the mei device
31 * @dev: the device structure
32 * @offset: offset from which to read the data
34 * returns register value (u32)
36 static inline u32 mei_me_reg_read(const struct mei_me_hw *hw,
37 unsigned long offset)
39 return ioread32(hw->mem_addr + offset);
43 /**
44 * mei_me_reg_write - Writes 32bit data to the mei device
46 * @dev: the device structure
47 * @offset: offset from which to write the data
48 * @value: register value to write (u32)
50 static inline void mei_me_reg_write(const struct mei_me_hw *hw,
51 unsigned long offset, u32 value)
53 iowrite32(value, hw->mem_addr + offset);
56 /**
57 * mei_me_mecbrw_read - Reads 32bit data from ME circular buffer
58 * read window register
60 * @dev: the device structure
62 * returns ME_CB_RW register value (u32)
64 static u32 mei_me_mecbrw_read(const struct mei_device *dev)
66 return mei_me_reg_read(to_me_hw(dev), ME_CB_RW);
68 /**
69 * mei_me_mecsr_read - Reads 32bit data from the ME CSR
71 * @dev: the device structure
73 * returns ME_CSR_HA register value (u32)
75 static inline u32 mei_me_mecsr_read(const struct mei_me_hw *hw)
77 return mei_me_reg_read(hw, ME_CSR_HA);
80 /**
81 * mei_hcsr_read - Reads 32bit data from the host CSR
83 * @dev: the device structure
85 * returns H_CSR register value (u32)
87 static inline u32 mei_hcsr_read(const struct mei_me_hw *hw)
89 return mei_me_reg_read(hw, H_CSR);
92 /**
93 * mei_hcsr_set - writes H_CSR register to the mei device,
94 * and ignores the H_IS bit for it is write-one-to-zero.
96 * @dev: the device structure
98 static inline void mei_hcsr_set(struct mei_me_hw *hw, u32 hcsr)
100 hcsr &= ~H_IS;
101 mei_me_reg_write(hw, H_CSR, hcsr);
106 * mei_me_hw_config - configure hw dependent settings
108 * @dev: mei device
110 static void mei_me_hw_config(struct mei_device *dev)
112 u32 hcsr = mei_hcsr_read(to_me_hw(dev));
113 /* Doesn't change in runtime */
114 dev->hbuf_depth = (hcsr & H_CBD) >> 24;
117 * mei_clear_interrupts - clear and stop interrupts
119 * @dev: the device structure
121 static void mei_me_intr_clear(struct mei_device *dev)
123 struct mei_me_hw *hw = to_me_hw(dev);
124 u32 hcsr = mei_hcsr_read(hw);
125 if ((hcsr & H_IS) == H_IS)
126 mei_me_reg_write(hw, H_CSR, hcsr);
129 * mei_me_intr_enable - enables mei device interrupts
131 * @dev: the device structure
133 static void mei_me_intr_enable(struct mei_device *dev)
135 struct mei_me_hw *hw = to_me_hw(dev);
136 u32 hcsr = mei_hcsr_read(hw);
137 hcsr |= H_IE;
138 mei_hcsr_set(hw, hcsr);
142 * mei_disable_interrupts - disables mei device interrupts
144 * @dev: the device structure
146 static void mei_me_intr_disable(struct mei_device *dev)
148 struct mei_me_hw *hw = to_me_hw(dev);
149 u32 hcsr = mei_hcsr_read(hw);
150 hcsr &= ~H_IE;
151 mei_hcsr_set(hw, hcsr);
155 * mei_me_hw_reset_release - release device from the reset
157 * @dev: the device structure
159 static void mei_me_hw_reset_release(struct mei_device *dev)
161 struct mei_me_hw *hw = to_me_hw(dev);
162 u32 hcsr = mei_hcsr_read(hw);
164 hcsr |= H_IG;
165 hcsr &= ~H_RST;
166 mei_hcsr_set(hw, hcsr);
168 /* complete this write before we set host ready on another CPU */
169 mmiowb();
172 * mei_me_hw_reset - resets fw via mei csr register.
174 * @dev: the device structure
175 * @intr_enable: if interrupt should be enabled after reset.
177 static int mei_me_hw_reset(struct mei_device *dev, bool intr_enable)
179 struct mei_me_hw *hw = to_me_hw(dev);
180 u32 hcsr = mei_hcsr_read(hw);
182 hcsr |= H_RST | H_IG | H_IS;
184 if (intr_enable)
185 hcsr |= H_IE;
186 else
187 hcsr &= ~H_IE;
189 dev->recvd_hw_ready = false;
190 mei_me_reg_write(hw, H_CSR, hcsr);
193 * Host reads the H_CSR once to ensure that the
194 * posted write to H_CSR completes.
196 hcsr = mei_hcsr_read(hw);
198 if ((hcsr & H_RST) == 0)
199 dev_warn(&dev->pdev->dev, "H_RST is not set = 0x%08X", hcsr);
201 if ((hcsr & H_RDY) == H_RDY)
202 dev_warn(&dev->pdev->dev, "H_RDY is not cleared 0x%08X", hcsr);
204 if (intr_enable == false)
205 mei_me_hw_reset_release(dev);
207 return 0;
211 * mei_me_host_set_ready - enable device
213 * @dev - mei device
214 * returns bool
217 static void mei_me_host_set_ready(struct mei_device *dev)
219 struct mei_me_hw *hw = to_me_hw(dev);
220 hw->host_hw_state = mei_hcsr_read(hw);
221 hw->host_hw_state |= H_IE | H_IG | H_RDY;
222 mei_hcsr_set(hw, hw->host_hw_state);
225 * mei_me_host_is_ready - check whether the host has turned ready
227 * @dev - mei device
228 * returns bool
230 static bool mei_me_host_is_ready(struct mei_device *dev)
232 struct mei_me_hw *hw = to_me_hw(dev);
233 hw->host_hw_state = mei_hcsr_read(hw);
234 return (hw->host_hw_state & H_RDY) == H_RDY;
238 * mei_me_hw_is_ready - check whether the me(hw) has turned ready
240 * @dev - mei device
241 * returns bool
243 static bool mei_me_hw_is_ready(struct mei_device *dev)
245 struct mei_me_hw *hw = to_me_hw(dev);
246 hw->me_hw_state = mei_me_mecsr_read(hw);
247 return (hw->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA;
250 static int mei_me_hw_ready_wait(struct mei_device *dev)
252 int err;
254 mutex_unlock(&dev->device_lock);
255 err = wait_event_interruptible_timeout(dev->wait_hw_ready,
256 dev->recvd_hw_ready,
257 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
258 mutex_lock(&dev->device_lock);
259 if (!err && !dev->recvd_hw_ready) {
260 if (!err)
261 err = -ETIMEDOUT;
262 dev_err(&dev->pdev->dev,
263 "wait hw ready failed. status = %d\n", err);
264 return err;
267 dev->recvd_hw_ready = false;
268 return 0;
271 static int mei_me_hw_start(struct mei_device *dev)
273 int ret = mei_me_hw_ready_wait(dev);
274 if (ret)
275 return ret;
276 dev_dbg(&dev->pdev->dev, "hw is ready\n");
278 mei_me_host_set_ready(dev);
279 return ret;
284 * mei_hbuf_filled_slots - gets number of device filled buffer slots
286 * @dev: the device structure
288 * returns number of filled slots
290 static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
292 struct mei_me_hw *hw = to_me_hw(dev);
293 char read_ptr, write_ptr;
295 hw->host_hw_state = mei_hcsr_read(hw);
297 read_ptr = (char) ((hw->host_hw_state & H_CBRP) >> 8);
298 write_ptr = (char) ((hw->host_hw_state & H_CBWP) >> 16);
300 return (unsigned char) (write_ptr - read_ptr);
304 * mei_me_hbuf_is_empty - checks if host buffer is empty.
306 * @dev: the device structure
308 * returns true if empty, false - otherwise.
310 static bool mei_me_hbuf_is_empty(struct mei_device *dev)
312 return mei_hbuf_filled_slots(dev) == 0;
316 * mei_me_hbuf_empty_slots - counts write empty slots.
318 * @dev: the device structure
320 * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
322 static int mei_me_hbuf_empty_slots(struct mei_device *dev)
324 unsigned char filled_slots, empty_slots;
326 filled_slots = mei_hbuf_filled_slots(dev);
327 empty_slots = dev->hbuf_depth - filled_slots;
329 /* check for overflow */
330 if (filled_slots > dev->hbuf_depth)
331 return -EOVERFLOW;
333 return empty_slots;
336 static size_t mei_me_hbuf_max_len(const struct mei_device *dev)
338 return dev->hbuf_depth * sizeof(u32) - sizeof(struct mei_msg_hdr);
343 * mei_write_message - writes a message to mei device.
345 * @dev: the device structure
346 * @header: mei HECI header of message
347 * @buf: message payload will be written
349 * This function returns -EIO if write has failed
351 static int mei_me_write_message(struct mei_device *dev,
352 struct mei_msg_hdr *header,
353 unsigned char *buf)
355 struct mei_me_hw *hw = to_me_hw(dev);
356 unsigned long rem;
357 unsigned long length = header->length;
358 u32 *reg_buf = (u32 *)buf;
359 u32 hcsr;
360 u32 dw_cnt;
361 int i;
362 int empty_slots;
364 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
366 empty_slots = mei_hbuf_empty_slots(dev);
367 dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
369 dw_cnt = mei_data2slots(length);
370 if (empty_slots < 0 || dw_cnt > empty_slots)
371 return -EIO;
373 mei_me_reg_write(hw, H_CB_WW, *((u32 *) header));
375 for (i = 0; i < length / 4; i++)
376 mei_me_reg_write(hw, H_CB_WW, reg_buf[i]);
378 rem = length & 0x3;
379 if (rem > 0) {
380 u32 reg = 0;
381 memcpy(&reg, &buf[length - rem], rem);
382 mei_me_reg_write(hw, H_CB_WW, reg);
385 hcsr = mei_hcsr_read(hw) | H_IG;
386 mei_hcsr_set(hw, hcsr);
387 if (!mei_me_hw_is_ready(dev))
388 return -EIO;
390 return 0;
394 * mei_me_count_full_read_slots - counts read full slots.
396 * @dev: the device structure
398 * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
400 static int mei_me_count_full_read_slots(struct mei_device *dev)
402 struct mei_me_hw *hw = to_me_hw(dev);
403 char read_ptr, write_ptr;
404 unsigned char buffer_depth, filled_slots;
406 hw->me_hw_state = mei_me_mecsr_read(hw);
407 buffer_depth = (unsigned char)((hw->me_hw_state & ME_CBD_HRA) >> 24);
408 read_ptr = (char) ((hw->me_hw_state & ME_CBRP_HRA) >> 8);
409 write_ptr = (char) ((hw->me_hw_state & ME_CBWP_HRA) >> 16);
410 filled_slots = (unsigned char) (write_ptr - read_ptr);
412 /* check for overflow */
413 if (filled_slots > buffer_depth)
414 return -EOVERFLOW;
416 dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
417 return (int)filled_slots;
421 * mei_me_read_slots - reads a message from mei device.
423 * @dev: the device structure
424 * @buffer: message buffer will be written
425 * @buffer_length: message size will be read
427 static int mei_me_read_slots(struct mei_device *dev, unsigned char *buffer,
428 unsigned long buffer_length)
430 struct mei_me_hw *hw = to_me_hw(dev);
431 u32 *reg_buf = (u32 *)buffer;
432 u32 hcsr;
434 for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
435 *reg_buf++ = mei_me_mecbrw_read(dev);
437 if (buffer_length > 0) {
438 u32 reg = mei_me_mecbrw_read(dev);
439 memcpy(reg_buf, &reg, buffer_length);
442 hcsr = mei_hcsr_read(hw) | H_IG;
443 mei_hcsr_set(hw, hcsr);
444 return 0;
448 * mei_me_irq_quick_handler - The ISR of the MEI device
450 * @irq: The irq number
451 * @dev_id: pointer to the device structure
453 * returns irqreturn_t
456 irqreturn_t mei_me_irq_quick_handler(int irq, void *dev_id)
458 struct mei_device *dev = (struct mei_device *) dev_id;
459 struct mei_me_hw *hw = to_me_hw(dev);
460 u32 csr_reg = mei_hcsr_read(hw);
462 if ((csr_reg & H_IS) != H_IS)
463 return IRQ_NONE;
465 /* clear H_IS bit in H_CSR */
466 mei_me_reg_write(hw, H_CSR, csr_reg);
468 return IRQ_WAKE_THREAD;
472 * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
473 * processing.
475 * @irq: The irq number
476 * @dev_id: pointer to the device structure
478 * returns irqreturn_t
481 irqreturn_t mei_me_irq_thread_handler(int irq, void *dev_id)
483 struct mei_device *dev = (struct mei_device *) dev_id;
484 struct mei_cl_cb complete_list;
485 s32 slots;
486 int rets = 0;
488 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
489 /* initialize our complete list */
490 mutex_lock(&dev->device_lock);
491 mei_io_list_init(&complete_list);
493 /* Ack the interrupt here
494 * In case of MSI we don't go through the quick handler */
495 if (pci_dev_msi_enabled(dev->pdev))
496 mei_clear_interrupts(dev);
498 /* check if ME wants a reset */
499 if (!mei_hw_is_ready(dev) && dev->dev_state != MEI_DEV_RESETTING) {
500 dev_warn(&dev->pdev->dev, "FW not ready: resetting.\n");
501 schedule_work(&dev->reset_work);
502 goto end;
505 /* check if we need to start the dev */
506 if (!mei_host_is_ready(dev)) {
507 if (mei_hw_is_ready(dev)) {
508 mei_me_hw_reset_release(dev);
509 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
511 dev->recvd_hw_ready = true;
512 wake_up_interruptible(&dev->wait_hw_ready);
513 } else {
514 dev_dbg(&dev->pdev->dev, "Spurious Interrupt\n");
516 goto end;
518 /* check slots available for reading */
519 slots = mei_count_full_read_slots(dev);
520 while (slots > 0) {
521 /* we have urgent data to send so break the read */
522 if (dev->wr_ext_msg.hdr.length)
523 break;
524 dev_dbg(&dev->pdev->dev, "slots to read = %08x\n", slots);
525 rets = mei_irq_read_handler(dev, &complete_list, &slots);
526 if (rets && dev->dev_state != MEI_DEV_RESETTING) {
527 schedule_work(&dev->reset_work);
528 goto end;
532 rets = mei_irq_write_handler(dev, &complete_list);
534 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
536 mei_irq_compl_handler(dev, &complete_list);
538 end:
539 dev_dbg(&dev->pdev->dev, "interrupt thread end ret = %d\n", rets);
540 mutex_unlock(&dev->device_lock);
541 return IRQ_HANDLED;
543 static const struct mei_hw_ops mei_me_hw_ops = {
545 .host_is_ready = mei_me_host_is_ready,
547 .hw_is_ready = mei_me_hw_is_ready,
548 .hw_reset = mei_me_hw_reset,
549 .hw_config = mei_me_hw_config,
550 .hw_start = mei_me_hw_start,
552 .intr_clear = mei_me_intr_clear,
553 .intr_enable = mei_me_intr_enable,
554 .intr_disable = mei_me_intr_disable,
556 .hbuf_free_slots = mei_me_hbuf_empty_slots,
557 .hbuf_is_ready = mei_me_hbuf_is_empty,
558 .hbuf_max_len = mei_me_hbuf_max_len,
560 .write = mei_me_write_message,
562 .rdbuf_full_slots = mei_me_count_full_read_slots,
563 .read_hdr = mei_me_mecbrw_read,
564 .read = mei_me_read_slots
568 * mei_me_dev_init - allocates and initializes the mei device structure
570 * @pdev: The pci device structure
572 * returns The mei_device_device pointer on success, NULL on failure.
574 struct mei_device *mei_me_dev_init(struct pci_dev *pdev)
576 struct mei_device *dev;
578 dev = kzalloc(sizeof(struct mei_device) +
579 sizeof(struct mei_me_hw), GFP_KERNEL);
580 if (!dev)
581 return NULL;
583 mei_device_init(dev);
585 dev->ops = &mei_me_hw_ops;
587 dev->pdev = pdev;
588 return dev;