3 * sep_driver.c - Security Processor Driver main group of functions
5 * Copyright(c) 2009,2010 Intel Corporation. All rights reserved.
6 * Contributions(c) 2009,2010 Discretix. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59
19 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Mark Allyn mark.a.allyn@intel.com
24 * Jayant Mangalampalli jayant.mangalampalli@intel.com
28 * 2009.06.26 Initial publish
29 * 2010.09.14 Upgrade to Medfield
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/miscdevice.h>
37 #include <linux/cdev.h>
38 #include <linux/kdev_t.h>
39 #include <linux/mutex.h>
40 #include <linux/sched.h>
42 #include <linux/poll.h>
43 #include <linux/wait.h>
44 #include <linux/pci.h>
45 #include <linux/firmware.h>
46 #include <linux/slab.h>
47 #include <linux/ioctl.h>
48 #include <asm/current.h>
49 #include <linux/ioport.h>
51 #include <linux/interrupt.h>
52 #include <linux/pagemap.h>
53 #include <asm/cacheflush.h>
54 #include <linux/sched.h>
55 #include <linux/delay.h>
56 #include <linux/jiffies.h>
57 #include <linux/rar_register.h>
59 #include "../memrar/memrar.h"
61 #include "sep_driver_hw_defs.h"
62 #include "sep_driver_config.h"
63 #include "sep_driver_api.h"
66 /*----------------------------------------
68 -----------------------------------------*/
70 #define SEP_RAR_IO_MEM_REGION_SIZE 0x40000
72 /*--------------------------------------------
74 --------------------------------------------*/
76 /* Keep this a single static object for now to keep the conversion easy */
78 static struct sep_device *sep_dev;
81 * sep_load_firmware - copy firmware cache/resident
82 * @sep: pointer to struct sep_device we are loading
84 * This functions copies the cache and resident from their source
85 * location into destination shared memory.
87 static int sep_load_firmware(struct sep_device *sep)
89 const struct firmware *fw;
90 char *cache_name = "cache.image.bin";
91 char *res_name = "resident.image.bin";
92 char *extapp_name = "extapp.image.bin";
94 unsigned long work1, work2, work3;
96 /* Set addresses and load resident */
97 sep->resident_bus = sep->rar_bus;
98 sep->resident_addr = sep->rar_addr;
100 error = request_firmware(&fw, res_name, &sep->pdev->dev);
102 dev_warn(&sep->pdev->dev, "can't request resident fw\n");
106 memcpy(sep->resident_addr, (void *)fw->data, fw->size);
107 sep->resident_size = fw->size;
108 release_firmware(fw);
110 dev_dbg(&sep->pdev->dev, "resident virtual is %p\n",
112 dev_dbg(&sep->pdev->dev, "resident bus is %lx\n",
113 (unsigned long)sep->resident_bus);
114 dev_dbg(&sep->pdev->dev, "resident size is %08zx\n",
117 /* Set addresses for dcache (no loading needed) */
118 work1 = (unsigned long)sep->resident_bus;
119 work2 = (unsigned long)sep->resident_size;
120 work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
121 sep->dcache_bus = (dma_addr_t)work3;
123 work1 = (unsigned long)sep->resident_addr;
124 work2 = (unsigned long)sep->resident_size;
125 work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
126 sep->dcache_addr = (void *)work3;
128 sep->dcache_size = 1024 * 128;
130 /* Set addresses and load cache */
131 sep->cache_bus = sep->dcache_bus + sep->dcache_size;
132 sep->cache_addr = sep->dcache_addr + sep->dcache_size;
134 error = request_firmware(&fw, cache_name, &sep->pdev->dev);
136 dev_warn(&sep->pdev->dev, "Unable to request cache firmware\n");
140 memcpy(sep->cache_addr, (void *)fw->data, fw->size);
141 sep->cache_size = fw->size;
142 release_firmware(fw);
144 dev_dbg(&sep->pdev->dev, "cache virtual is %p\n",
146 dev_dbg(&sep->pdev->dev, "cache bus is %08lx\n",
147 (unsigned long)sep->cache_bus);
148 dev_dbg(&sep->pdev->dev, "cache size is %08zx\n",
151 /* Set addresses and load extapp */
152 sep->extapp_bus = sep->cache_bus + (1024 * 370);
153 sep->extapp_addr = sep->cache_addr + (1024 * 370);
155 error = request_firmware(&fw, extapp_name, &sep->pdev->dev);
157 dev_warn(&sep->pdev->dev, "Unable to request extapp firmware\n");
161 memcpy(sep->extapp_addr, (void *)fw->data, fw->size);
162 sep->extapp_size = fw->size;
163 release_firmware(fw);
165 dev_dbg(&sep->pdev->dev, "extapp virtual is %p\n",
167 dev_dbg(&sep->pdev->dev, "extapp bus is %08llx\n",
168 (unsigned long long)sep->extapp_bus);
169 dev_dbg(&sep->pdev->dev, "extapp size is %08zx\n",
175 MODULE_FIRMWARE("sep/cache.image.bin");
176 MODULE_FIRMWARE("sep/resident.image.bin");
177 MODULE_FIRMWARE("sep/extapp.image.bin");
180 * sep_dump_message - dump the message that is pending
183 static void sep_dump_message(struct sep_device *sep)
186 u32 *p = sep->shared_addr;
187 for (count = 0; count < 12 * 4; count += 4)
188 dev_dbg(&sep->pdev->dev, "Word %d of the message is %x\n",
193 * sep_map_and_alloc_shared_area - allocate shared block
194 * @sep: security processor
195 * @size: size of shared area
197 static int sep_map_and_alloc_shared_area(struct sep_device *sep)
199 sep->shared_addr = dma_alloc_coherent(&sep->pdev->dev,
201 &sep->shared_bus, GFP_KERNEL);
203 if (!sep->shared_addr) {
204 dev_warn(&sep->pdev->dev,
205 "shared memory dma_alloc_coherent failed\n");
208 dev_dbg(&sep->pdev->dev,
209 "shared_addr %zx bytes @%p (bus %llx)\n",
210 sep->shared_size, sep->shared_addr,
211 (unsigned long long)sep->shared_bus);
216 * sep_unmap_and_free_shared_area - free shared block
217 * @sep: security processor
219 static void sep_unmap_and_free_shared_area(struct sep_device *sep)
221 dev_dbg(&sep->pdev->dev, "shared area unmap and free\n");
222 dma_free_coherent(&sep->pdev->dev, sep->shared_size,
223 sep->shared_addr, sep->shared_bus);
227 * sep_shared_bus_to_virt - convert bus/virt addresses
228 * @sep: pointer to struct sep_device
229 * @bus_address: address to convert
231 * Returns virtual address inside the shared area according
232 * to the bus address.
234 static void *sep_shared_bus_to_virt(struct sep_device *sep,
235 dma_addr_t bus_address)
237 return sep->shared_addr + (bus_address - sep->shared_bus);
241 * open function for the singleton driver
242 * @inode_ptr struct inode *
243 * @file_ptr struct file *
245 * Called when the user opens the singleton device interface
247 static int sep_singleton_open(struct inode *inode_ptr, struct file *file_ptr)
250 struct sep_device *sep;
253 * Get the SEP device structure and use it for the
254 * private_data field in filp for other methods
258 file_ptr->private_data = sep;
260 dev_dbg(&sep->pdev->dev, "Singleton open for pid %d\n", current->pid);
262 dev_dbg(&sep->pdev->dev, "calling test and set for singleton 0\n");
263 if (test_and_set_bit(0, &sep->singleton_access_flag)) {
268 dev_dbg(&sep->pdev->dev, "sep_singleton_open end\n");
274 * sep_open - device open method
275 * @inode: inode of SEP device
276 * @filp: file handle to SEP device
278 * Open method for the SEP device. Called when userspace opens
279 * the SEP device node.
281 * Returns zero on success otherwise an error code.
283 static int sep_open(struct inode *inode, struct file *filp)
285 struct sep_device *sep;
288 * Get the SEP device structure and use it for the
289 * private_data field in filp for other methods
292 filp->private_data = sep;
294 dev_dbg(&sep->pdev->dev, "Open for pid %d\n", current->pid);
296 /* Anyone can open; locking takes place at transaction level */
301 * sep_singleton_release - close a SEP singleton device
302 * @inode: inode of SEP device
303 * @filp: file handle being closed
305 * Called on the final close of a SEP device. As the open protects against
306 * multiple simultaenous opens that means this method is called when the
307 * final reference to the open handle is dropped.
309 static int sep_singleton_release(struct inode *inode, struct file *filp)
311 struct sep_device *sep = filp->private_data;
313 dev_dbg(&sep->pdev->dev, "Singleton release for pid %d\n",
315 clear_bit(0, &sep->singleton_access_flag);
320 * sep_request_daemonopen - request daemon open method
321 * @inode: inode of SEP device
322 * @filp: file handle to SEP device
324 * Open method for the SEP request daemon. Called when
325 * request daemon in userspace opens the SEP device node.
327 * Returns zero on success otherwise an error code.
329 static int sep_request_daemon_open(struct inode *inode, struct file *filp)
331 struct sep_device *sep = sep_dev;
334 filp->private_data = sep;
336 dev_dbg(&sep->pdev->dev, "Request daemon open for pid %d\n",
339 /* There is supposed to be only one request daemon */
340 dev_dbg(&sep->pdev->dev, "calling test and set for req_dmon open 0\n");
341 if (test_and_set_bit(0, &sep->request_daemon_open))
347 * sep_request_daemon_release - close a SEP daemon
348 * @inode: inode of SEP device
349 * @filp: file handle being closed
351 * Called on the final close of a SEP daemon.
353 static int sep_request_daemon_release(struct inode *inode, struct file *filp)
355 struct sep_device *sep = filp->private_data;
357 dev_dbg(&sep->pdev->dev, "Reques daemon release for pid %d\n",
360 /* Clear the request_daemon_open flag */
361 clear_bit(0, &sep->request_daemon_open);
366 * sep_req_daemon_send_reply_command_handler - poke the SEP
367 * @sep: struct sep_device *
369 * This function raises interrupt to SEPm that signals that is has a
370 * new command from HOST
372 static int sep_req_daemon_send_reply_command_handler(struct sep_device *sep)
374 unsigned long lck_flags;
376 dev_dbg(&sep->pdev->dev,
377 "sep_req_daemon_send_reply_command_handler start\n");
379 sep_dump_message(sep);
381 /* Counters are lockable region */
382 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
386 /* Send the interrupt to SEP */
387 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR2_REG_ADDR, sep->send_ct);
390 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
392 dev_dbg(&sep->pdev->dev,
393 "sep_req_daemon_send_reply send_ct %lx reply_ct %lx\n",
394 sep->send_ct, sep->reply_ct);
396 dev_dbg(&sep->pdev->dev,
397 "sep_req_daemon_send_reply_command_handler end\n");
404 * sep_free_dma_table_data_handler - free DMA table
405 * @sep: pointere to struct sep_device
407 * Handles the request to free DMA table for synchronic actions
409 static int sep_free_dma_table_data_handler(struct sep_device *sep)
413 /* Pointer to the current dma_resource struct */
414 struct sep_dma_resource *dma;
416 dev_dbg(&sep->pdev->dev, "sep_free_dma_table_data_handler start\n");
418 for (dcb_counter = 0; dcb_counter < sep->nr_dcb_creat; dcb_counter++) {
419 dma = &sep->dma_res_arr[dcb_counter];
421 /* Unmap and free input map array */
422 if (dma->in_map_array) {
423 for (count = 0; count < dma->in_num_pages; count++) {
424 dma_unmap_page(&sep->pdev->dev,
425 dma->in_map_array[count].dma_addr,
426 dma->in_map_array[count].size,
429 kfree(dma->in_map_array);
432 /* Unmap output map array, DON'T free it yet */
433 if (dma->out_map_array) {
434 for (count = 0; count < dma->out_num_pages; count++) {
435 dma_unmap_page(&sep->pdev->dev,
436 dma->out_map_array[count].dma_addr,
437 dma->out_map_array[count].size,
440 kfree(dma->out_map_array);
443 /* Free page cache for output */
444 if (dma->in_page_array) {
445 for (count = 0; count < dma->in_num_pages; count++) {
446 flush_dcache_page(dma->in_page_array[count]);
447 page_cache_release(dma->in_page_array[count]);
449 kfree(dma->in_page_array);
452 if (dma->out_page_array) {
453 for (count = 0; count < dma->out_num_pages; count++) {
454 if (!PageReserved(dma->out_page_array[count]))
455 SetPageDirty(dma->out_page_array[count]);
456 flush_dcache_page(dma->out_page_array[count]);
457 page_cache_release(dma->out_page_array[count]);
459 kfree(dma->out_page_array);
462 /* Reset all the values */
463 dma->in_page_array = NULL;
464 dma->out_page_array = NULL;
465 dma->in_num_pages = 0;
466 dma->out_num_pages = 0;
467 dma->in_map_array = NULL;
468 dma->out_map_array = NULL;
469 dma->in_map_num_entries = 0;
470 dma->out_map_num_entries = 0;
473 sep->nr_dcb_creat = 0;
474 sep->num_lli_tables_created = 0;
476 dev_dbg(&sep->pdev->dev, "sep_free_dma_table_data_handler end\n");
481 * sep_request_daemon_mmap - maps the shared area to user space
482 * @filp: pointer to struct file
483 * @vma: pointer to vm_area_struct
485 * Called by the kernel when the daemon attempts an mmap() syscall
488 static int sep_request_daemon_mmap(struct file *filp,
489 struct vm_area_struct *vma)
491 struct sep_device *sep = filp->private_data;
492 dma_addr_t bus_address;
495 dev_dbg(&sep->pdev->dev, "daemon mmap start\n");
497 if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
502 /* Get physical address */
503 bus_address = sep->shared_bus;
505 dev_dbg(&sep->pdev->dev, "bus_address is %08lx\n",
506 (unsigned long)bus_address);
508 if (remap_pfn_range(vma, vma->vm_start, bus_address >> PAGE_SHIFT,
509 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
511 dev_warn(&sep->pdev->dev, "remap_page_range failed\n");
517 dev_dbg(&sep->pdev->dev, "daemon mmap end\n");
522 * sep_request_daemon_poll - poll implementation
523 * @sep: struct sep_device * for current SEP device
524 * @filp: struct file * for open file
525 * @wait: poll_table * for poll
527 * Called when our device is part of a poll() or select() syscall
529 static unsigned int sep_request_daemon_poll(struct file *filp,
535 unsigned long lck_flags;
536 struct sep_device *sep = filp->private_data;
538 dev_dbg(&sep->pdev->dev, "daemon poll: start\n");
540 poll_wait(filp, &sep->event_request_daemon, wait);
542 dev_dbg(&sep->pdev->dev, "daemon poll: send_ct is %lx reply ct is %lx\n",
543 sep->send_ct, sep->reply_ct);
545 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
546 /* Check if the data is ready */
547 if (sep->send_ct == sep->reply_ct) {
548 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
550 retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
551 dev_dbg(&sep->pdev->dev,
552 "daemon poll: data check (GPR2) is %x\n", retval2);
554 /* Check if PRINT request */
555 if ((retval2 >> 30) & 0x1) {
556 dev_dbg(&sep->pdev->dev, "daemon poll: PRINTF request in\n");
560 /* Check if NVS request */
562 dev_dbg(&sep->pdev->dev, "daemon poll: NVS request in\n");
563 mask |= POLLPRI | POLLWRNORM;
566 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
567 dev_dbg(&sep->pdev->dev,
568 "daemon poll: no reply received; returning 0\n");
572 dev_dbg(&sep->pdev->dev, "daemon poll: exit\n");
577 * sep_release - close a SEP device
578 * @inode: inode of SEP device
579 * @filp: file handle being closed
581 * Called on the final close of a SEP device.
583 static int sep_release(struct inode *inode, struct file *filp)
585 struct sep_device *sep = filp->private_data;
587 dev_dbg(&sep->pdev->dev, "Release for pid %d\n", current->pid);
589 mutex_lock(&sep->sep_mutex);
590 /* Is this the process that has a transaction open?
591 * If so, lets reset pid_doing_transaction to 0 and
592 * clear the in use flags, and then wake up sep_event
593 * so that other processes can do transactions
595 dev_dbg(&sep->pdev->dev, "waking up event and mmap_event\n");
596 if (sep->pid_doing_transaction == current->pid) {
597 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
598 clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
599 sep_free_dma_table_data_handler(sep);
600 wake_up(&sep->event);
601 sep->pid_doing_transaction = 0;
604 mutex_unlock(&sep->sep_mutex);
609 * sep_mmap - maps the shared area to user space
610 * @filp: pointer to struct file
611 * @vma: pointer to vm_area_struct
613 * Called on an mmap of our space via the normal SEP device
615 static int sep_mmap(struct file *filp, struct vm_area_struct *vma)
618 struct sep_device *sep = filp->private_data;
619 unsigned long error = 0;
621 dev_dbg(&sep->pdev->dev, "mmap start\n");
623 /* Set the transaction busy (own the device) */
624 wait_event_interruptible(sep->event,
625 test_and_set_bit(SEP_MMAP_LOCK_BIT,
626 &sep->in_use_flags) == 0);
628 if (signal_pending(current)) {
630 goto end_function_with_error;
633 * The pid_doing_transaction indicates that this process
634 * now owns the facilities to performa a transaction with
635 * the SEP. While this process is performing a transaction,
636 * no other process who has the SEP device open can perform
637 * any transactions. This method allows more than one process
638 * to have the device open at any given time, which provides
639 * finer granularity for device utilization by multiple
642 mutex_lock(&sep->sep_mutex);
643 sep->pid_doing_transaction = current->pid;
644 mutex_unlock(&sep->sep_mutex);
646 /* Zero the pools and the number of data pool alocation pointers */
647 sep->data_pool_bytes_allocated = 0;
648 sep->num_of_data_allocations = 0;
651 * Check that the size of the mapped range is as the size of the message
654 if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
656 goto end_function_with_error;
659 dev_dbg(&sep->pdev->dev, "shared_addr is %p\n", sep->shared_addr);
661 /* Get bus address */
662 bus_addr = sep->shared_bus;
664 dev_dbg(&sep->pdev->dev,
665 "bus_address is %lx\n", (unsigned long)bus_addr);
667 if (remap_pfn_range(vma, vma->vm_start, bus_addr >> PAGE_SHIFT,
668 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
669 dev_warn(&sep->pdev->dev, "remap_page_range failed\n");
671 goto end_function_with_error;
673 dev_dbg(&sep->pdev->dev, "mmap end\n");
676 end_function_with_error:
678 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
679 mutex_lock(&sep->sep_mutex);
680 sep->pid_doing_transaction = 0;
681 mutex_unlock(&sep->sep_mutex);
683 /* Raise event for stuck contextes */
685 dev_warn(&sep->pdev->dev, "mmap error - waking up event\n");
686 wake_up(&sep->event);
693 * sep_poll - poll handler
694 * @filp: pointer to struct file
695 * @wait: pointer to poll_table
697 * Called by the OS when the kernel is asked to do a poll on
700 static unsigned int sep_poll(struct file *filp, poll_table *wait)
705 unsigned long lck_flags;
707 struct sep_device *sep = filp->private_data;
709 dev_dbg(&sep->pdev->dev, "poll: start\n");
711 /* Am I the process that owns the transaction? */
712 mutex_lock(&sep->sep_mutex);
713 if (current->pid != sep->pid_doing_transaction) {
714 dev_warn(&sep->pdev->dev, "poll; wrong pid\n");
716 mutex_unlock(&sep->sep_mutex);
719 mutex_unlock(&sep->sep_mutex);
721 /* Check if send command or send_reply were activated previously */
722 if (!test_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
723 dev_warn(&sep->pdev->dev, "poll; lock bit set\n");
728 /* Add the event to the polling wait table */
729 dev_dbg(&sep->pdev->dev, "poll: calling wait sep_event\n");
731 poll_wait(filp, &sep->event, wait);
733 dev_dbg(&sep->pdev->dev, "poll: send_ct is %lx reply ct is %lx\n",
734 sep->send_ct, sep->reply_ct);
736 /* Check if error occured during poll */
737 retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
738 if (retval2 != 0x0) {
739 dev_warn(&sep->pdev->dev, "poll; poll error %x\n", retval2);
744 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
746 if (sep->send_ct == sep->reply_ct) {
747 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
748 retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
749 dev_dbg(&sep->pdev->dev, "poll: data ready check (GPR2) %x\n",
752 /* Check if printf request */
753 if ((retval >> 30) & 0x1) {
754 dev_dbg(&sep->pdev->dev, "poll: SEP printf request\n");
755 wake_up(&sep->event_request_daemon);
759 /* Check if the this is SEP reply or request */
761 dev_dbg(&sep->pdev->dev, "poll: SEP request\n");
762 wake_up(&sep->event_request_daemon);
764 dev_dbg(&sep->pdev->dev, "poll: normal return\n");
765 /* In case it is again by send_reply_comand */
766 clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
767 sep_dump_message(sep);
768 dev_dbg(&sep->pdev->dev,
769 "poll; SEP reply POLLIN | POLLRDNORM\n");
770 mask |= POLLIN | POLLRDNORM;
773 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
774 dev_dbg(&sep->pdev->dev,
775 "poll; no reply received; returning mask of 0\n");
780 dev_dbg(&sep->pdev->dev, "poll: end\n");
785 * sep_time_address - address in SEP memory of time
786 * @sep: SEP device we want the address from
788 * Return the address of the two dwords in memory used for time
791 static u32 *sep_time_address(struct sep_device *sep)
793 return sep->shared_addr + SEP_DRIVER_SYSTEM_TIME_MEMORY_OFFSET_IN_BYTES;
797 * sep_set_time - set the SEP time
798 * @sep: the SEP we are setting the time for
800 * Calculates time and sets it at the predefined address.
801 * Called with the SEP mutex held.
803 static unsigned long sep_set_time(struct sep_device *sep)
806 u32 *time_addr; /* Address of time as seen by the kernel */
809 dev_dbg(&sep->pdev->dev, "sep_set_time start\n");
811 do_gettimeofday(&time);
813 /* Set value in the SYSTEM MEMORY offset */
814 time_addr = sep_time_address(sep);
816 time_addr[0] = SEP_TIME_VAL_TOKEN;
817 time_addr[1] = time.tv_sec;
819 dev_dbg(&sep->pdev->dev, "time.tv_sec is %lu\n", time.tv_sec);
820 dev_dbg(&sep->pdev->dev, "time_addr is %p\n", time_addr);
821 dev_dbg(&sep->pdev->dev, "sep->shared_addr is %p\n", sep->shared_addr);
827 * sep_set_caller_id_handler - insert caller id entry
829 * @arg: pointer to struct caller_id_struct
831 * Inserts the data into the caller id table. Note that this function
832 * falls under the ioctl lock
834 static int sep_set_caller_id_handler(struct sep_device *sep, unsigned long arg)
839 struct caller_id_struct command_args;
841 dev_dbg(&sep->pdev->dev, "sep_set_caller_id_handler start\n");
843 for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
844 if (sep->caller_id_table[i].pid == 0)
848 if (i == SEP_CALLER_ID_TABLE_NUM_ENTRIES) {
849 dev_warn(&sep->pdev->dev, "no more caller id entries left\n");
850 dev_warn(&sep->pdev->dev, "maximum number is %d\n",
851 SEP_CALLER_ID_TABLE_NUM_ENTRIES);
857 if (copy_from_user(&command_args, (void __user *)arg,
858 sizeof(command_args))) {
863 hash = (void __user *)(unsigned long)command_args.callerIdAddress;
865 if (!command_args.pid || !command_args.callerIdSizeInBytes) {
870 dev_dbg(&sep->pdev->dev, "pid is %x\n", command_args.pid);
871 dev_dbg(&sep->pdev->dev, "callerIdSizeInBytes is %x\n",
872 command_args.callerIdSizeInBytes);
874 if (command_args.callerIdSizeInBytes >
875 SEP_CALLER_ID_HASH_SIZE_IN_BYTES) {
880 sep->caller_id_table[i].pid = command_args.pid;
882 if (copy_from_user(sep->caller_id_table[i].callerIdHash,
883 hash, command_args.callerIdSizeInBytes))
886 dev_dbg(&sep->pdev->dev, "sep_set_caller_id_handler end\n");
891 * sep_set_current_caller_id - set the caller id
892 * @sep: pointer to struct_sep_device
894 * Set the caller ID (if it exists) to the SEP. Note that this
895 * function falls under the ioctl lock
897 static int sep_set_current_caller_id(struct sep_device *sep)
902 dev_dbg(&sep->pdev->dev, "sep_set_current_caller_id start\n");
903 dev_dbg(&sep->pdev->dev, "current process is %d\n", current->pid);
905 /* Zero the previous value */
906 memset(sep->shared_addr + SEP_CALLER_ID_OFFSET_BYTES,
907 0, SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
909 for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
910 if (sep->caller_id_table[i].pid == current->pid) {
911 dev_dbg(&sep->pdev->dev, "Caller Id found\n");
913 memcpy(sep->shared_addr + SEP_CALLER_ID_OFFSET_BYTES,
914 (void *)(sep->caller_id_table[i].callerIdHash),
915 SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
919 /* Ensure data is in little endian */
920 hash_buf_ptr = (u32 *)sep->shared_addr +
921 SEP_CALLER_ID_OFFSET_BYTES;
923 for (i = 0; i < SEP_CALLER_ID_HASH_SIZE_IN_WORDS; i++)
924 hash_buf_ptr[i] = cpu_to_le32(hash_buf_ptr[i]);
926 dev_dbg(&sep->pdev->dev, "sep_set_current_caller_id end\n");
931 * sep_send_command_handler - kick off a command
932 * @sep: SEP being signalled
934 * This function raises interrupt to SEP that signals that is has a new
935 * command from the host
937 * Note that this function does fall under the ioctl lock
939 static int sep_send_command_handler(struct sep_device *sep)
941 unsigned long lck_flags;
944 dev_dbg(&sep->pdev->dev, "sep_send_command_handler start\n");
946 if (test_and_set_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
952 sep_set_current_caller_id(sep);
954 sep_dump_message(sep);
957 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
959 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
961 dev_dbg(&sep->pdev->dev,
962 "sep_send_command_handler send_ct %lx reply_ct %lx\n",
963 sep->send_ct, sep->reply_ct);
965 /* Send interrupt to SEP */
966 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);
969 dev_dbg(&sep->pdev->dev, "sep_send_command_handler end\n");
974 * sep_allocate_data_pool_memory_handler -allocate pool memory
975 * @sep: pointer to struct sep_device
976 * @arg: pointer to struct alloc_struct
978 * This function handles the allocate data pool memory request
979 * This function returns calculates the bus address of the
980 * allocated memory, and the offset of this area from the mapped address.
981 * Therefore, the FVOs in user space can calculate the exact virtual
982 * address of this allocated memory
984 static int sep_allocate_data_pool_memory_handler(struct sep_device *sep,
988 struct alloc_struct command_args;
990 /* Holds the allocated buffer address in the system memory pool */
993 dev_dbg(&sep->pdev->dev,
994 "sep_allocate_data_pool_memory_handler start\n");
996 if (copy_from_user(&command_args, (void __user *)arg,
997 sizeof(struct alloc_struct))) {
1002 /* Allocate memory */
1003 if ((sep->data_pool_bytes_allocated + command_args.num_bytes) >
1004 SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES) {
1009 dev_dbg(&sep->pdev->dev,
1010 "bytes_allocated: %x\n", (int)sep->data_pool_bytes_allocated);
1011 dev_dbg(&sep->pdev->dev,
1012 "offset: %x\n", SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES);
1013 /* Set the virtual and bus address */
1014 command_args.offset = SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
1015 sep->data_pool_bytes_allocated;
1017 dev_dbg(&sep->pdev->dev,
1018 "command_args.offset: %x\n", command_args.offset);
1020 /* Place in the shared area that is known by the SEP */
1021 token_addr = (u32 *)(sep->shared_addr +
1022 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES +
1023 (sep->num_of_data_allocations)*2*sizeof(u32));
1025 dev_dbg(&sep->pdev->dev, "allocation offset: %x\n",
1026 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES);
1027 dev_dbg(&sep->pdev->dev, "data pool token addr is %p\n", token_addr);
1029 token_addr[0] = SEP_DATA_POOL_POINTERS_VAL_TOKEN;
1030 token_addr[1] = (u32)sep->shared_bus +
1031 SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
1032 sep->data_pool_bytes_allocated;
1034 dev_dbg(&sep->pdev->dev, "data pool token [0] %x\n", token_addr[0]);
1035 dev_dbg(&sep->pdev->dev, "data pool token [1] %x\n", token_addr[1]);
1037 /* Write the memory back to the user space */
1038 error = copy_to_user((void *)arg, (void *)&command_args,
1039 sizeof(struct alloc_struct));
1045 /* Update the allocation */
1046 sep->data_pool_bytes_allocated += command_args.num_bytes;
1047 sep->num_of_data_allocations += 1;
1049 dev_dbg(&sep->pdev->dev, "data_allocations %d\n",
1050 sep->num_of_data_allocations);
1051 dev_dbg(&sep->pdev->dev, "bytes allocated %d\n",
1052 (int)sep->data_pool_bytes_allocated);
1055 dev_dbg(&sep->pdev->dev, "sep_allocate_data_pool_memory_handler end\n");
1060 * sep_lock_kernel_pages - map kernel pages for DMA
1061 * @sep: pointer to struct sep_device
1062 * @kernel_virt_addr: address of data buffer in kernel
1063 * @data_size: size of data
1064 * @lli_array_ptr: lli array
1065 * @in_out_flag: input into device or output from device
1067 * This function locks all the physical pages of the kernel virtual buffer
1068 * and construct a basic lli array, where each entry holds the physical
1069 * page address and the size that application data holds in this page
1070 * This function is used only during kernel crypto mod calls from within
1071 * the kernel (when ioctl is not used)
1073 static int sep_lock_kernel_pages(struct sep_device *sep,
1074 unsigned long kernel_virt_addr,
1076 struct sep_lli_entry **lli_array_ptr,
1082 struct sep_lli_entry *lli_array;
1084 struct sep_dma_map *map_array;
1086 dev_dbg(&sep->pdev->dev, "sep_lock_kernel_pages start\n");
1087 dev_dbg(&sep->pdev->dev, "kernel_virt_addr is %08lx\n",
1088 (unsigned long)kernel_virt_addr);
1089 dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1091 lli_array = kmalloc(sizeof(struct sep_lli_entry), GFP_ATOMIC);
1096 map_array = kmalloc(sizeof(struct sep_dma_map), GFP_ATOMIC);
1099 goto end_function_with_error;
1102 map_array[0].dma_addr =
1103 dma_map_single(&sep->pdev->dev, (void *)kernel_virt_addr,
1104 data_size, DMA_BIDIRECTIONAL);
1105 map_array[0].size = data_size;
1109 * Set the start address of the first page - app data may start not at
1110 * the beginning of the page
1112 lli_array[0].bus_address = (u32)map_array[0].dma_addr;
1113 lli_array[0].block_size = map_array[0].size;
1115 dev_dbg(&sep->pdev->dev,
1116 "lli_array[0].bus_address is %08lx, lli_array[0].block_size is %x\n",
1117 (unsigned long)lli_array[0].bus_address,
1118 lli_array[0].block_size);
1120 /* Set the output parameters */
1121 if (in_out_flag == SEP_DRIVER_IN_FLAG) {
1122 *lli_array_ptr = lli_array;
1123 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 1;
1124 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
1125 sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
1126 sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries = 1;
1128 *lli_array_ptr = lli_array;
1129 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = 1;
1130 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = NULL;
1131 sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
1132 sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries = 1;
1136 end_function_with_error:
1140 dev_dbg(&sep->pdev->dev, "sep_lock_kernel_pages end\n");
1145 * sep_lock_user_pages - lock and map user pages for DMA
1146 * @sep: pointer to struct sep_device
1147 * @app_virt_addr: user memory data buffer
1148 * @data_size: size of data buffer
1149 * @lli_array_ptr: lli array
1150 * @in_out_flag: input or output to device
1152 * This function locks all the physical pages of the application
1153 * virtual buffer and construct a basic lli array, where each entry
1154 * holds the physical page address and the size that application
1155 * data holds in this physical pages
1157 static int sep_lock_user_pages(struct sep_device *sep,
1160 struct sep_lli_entry **lli_array_ptr,
1167 /* The the page of the end address of the user space buffer */
1169 /* The page of the start address of the user space buffer */
1171 /* The range in pages */
1173 /* Array of pointers to page */
1174 struct page **page_array;
1176 struct sep_lli_entry *lli_array;
1178 struct sep_dma_map *map_array;
1179 /* Direction of the DMA mapping for locked pages */
1180 enum dma_data_direction dir;
1182 dev_dbg(&sep->pdev->dev, "sep_lock_user_pages start\n");
1184 /* Set start and end pages and num pages */
1185 end_page = (app_virt_addr + data_size - 1) >> PAGE_SHIFT;
1186 start_page = app_virt_addr >> PAGE_SHIFT;
1187 num_pages = end_page - start_page + 1;
1189 dev_dbg(&sep->pdev->dev, "app_virt_addr is %x\n", app_virt_addr);
1190 dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1191 dev_dbg(&sep->pdev->dev, "start_page is %x\n", start_page);
1192 dev_dbg(&sep->pdev->dev, "end_page is %x\n", end_page);
1193 dev_dbg(&sep->pdev->dev, "num_pages is %x\n", num_pages);
1195 dev_dbg(&sep->pdev->dev, "starting page_array malloc\n");
1197 /* Allocate array of pages structure pointers */
1198 page_array = kmalloc(sizeof(struct page *) * num_pages, GFP_ATOMIC);
1203 map_array = kmalloc(sizeof(struct sep_dma_map) * num_pages, GFP_ATOMIC);
1205 dev_warn(&sep->pdev->dev, "kmalloc for map_array failed\n");
1207 goto end_function_with_error1;
1210 lli_array = kmalloc(sizeof(struct sep_lli_entry) * num_pages,
1214 dev_warn(&sep->pdev->dev, "kmalloc for lli_array failed\n");
1216 goto end_function_with_error2;
1219 dev_dbg(&sep->pdev->dev, "starting get_user_pages\n");
1221 /* Convert the application virtual address into a set of physical */
1222 down_read(¤t->mm->mmap_sem);
1223 result = get_user_pages(current, current->mm, app_virt_addr,
1225 ((in_out_flag == SEP_DRIVER_IN_FLAG) ? 0 : 1),
1226 0, page_array, NULL);
1228 up_read(¤t->mm->mmap_sem);
1230 /* Check the number of pages locked - if not all then exit with error */
1231 if (result != num_pages) {
1232 dev_warn(&sep->pdev->dev,
1233 "not all pages locked by get_user_pages\n");
1235 goto end_function_with_error3;
1238 dev_dbg(&sep->pdev->dev, "get_user_pages succeeded\n");
1241 if (in_out_flag == SEP_DRIVER_IN_FLAG)
1242 dir = DMA_TO_DEVICE;
1244 dir = DMA_FROM_DEVICE;
1247 * Fill the array using page array data and
1248 * map the pages - this action will also flush the cache as needed
1250 for (count = 0; count < num_pages; count++) {
1251 /* Fill the map array */
1252 map_array[count].dma_addr =
1253 dma_map_page(&sep->pdev->dev, page_array[count],
1254 0, PAGE_SIZE, /*dir*/DMA_BIDIRECTIONAL);
1256 map_array[count].size = PAGE_SIZE;
1258 /* Fill the lli array entry */
1259 lli_array[count].bus_address = (u32)map_array[count].dma_addr;
1260 lli_array[count].block_size = PAGE_SIZE;
1262 dev_warn(&sep->pdev->dev, "lli_array[%x].bus_address is %08lx, lli_array[%x].block_size is %x\n",
1263 count, (unsigned long)lli_array[count].bus_address,
1264 count, lli_array[count].block_size);
1267 /* Check the offset for the first page */
1268 lli_array[0].bus_address =
1269 lli_array[0].bus_address + (app_virt_addr & (~PAGE_MASK));
1271 /* Check that not all the data is in the first page only */
1272 if ((PAGE_SIZE - (app_virt_addr & (~PAGE_MASK))) >= data_size)
1273 lli_array[0].block_size = data_size;
1275 lli_array[0].block_size =
1276 PAGE_SIZE - (app_virt_addr & (~PAGE_MASK));
1278 dev_dbg(&sep->pdev->dev,
1279 "lli_array[0].bus_address is %08lx, lli_array[0].block_size is %x\n",
1280 (unsigned long)lli_array[count].bus_address,
1281 lli_array[count].block_size);
1283 /* Check the size of the last page */
1284 if (num_pages > 1) {
1285 lli_array[num_pages - 1].block_size =
1286 (app_virt_addr + data_size) & (~PAGE_MASK);
1288 dev_warn(&sep->pdev->dev,
1289 "lli_array[%x].bus_address is %08lx, lli_array[%x].block_size is %x\n",
1291 (unsigned long)lli_array[count].bus_address,
1293 lli_array[count].block_size);
1296 /* Set output params acording to the in_out flag */
1297 if (in_out_flag == SEP_DRIVER_IN_FLAG) {
1298 *lli_array_ptr = lli_array;
1299 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = num_pages;
1300 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = page_array;
1301 sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
1302 sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries =
1305 *lli_array_ptr = lli_array;
1306 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = num_pages;
1307 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array =
1309 sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
1310 sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries =
1315 end_function_with_error3:
1316 /* Free lli array */
1319 end_function_with_error2:
1322 end_function_with_error1:
1323 /* Free page array */
1327 dev_dbg(&sep->pdev->dev, "sep_lock_user_pages end\n");
1332 * u32 sep_calculate_lli_table_max_size - size the LLI table
1333 * @sep: pointer to struct sep_device
1335 * @num_array_entries
1338 * This function calculates the size of data that can be inserted into
1339 * the lli table from this array, such that either the table is full
1340 * (all entries are entered), or there are no more entries in the
1343 static u32 sep_calculate_lli_table_max_size(struct sep_device *sep,
1344 struct sep_lli_entry *lli_in_array_ptr,
1345 u32 num_array_entries,
1346 u32 *last_table_flag)
1349 /* Table data size */
1350 u32 table_data_size = 0;
1351 /* Data size for the next table */
1352 u32 next_table_data_size;
1354 *last_table_flag = 0;
1357 * Calculate the data in the out lli table till we fill the whole
1358 * table or till the data has ended
1361 (counter < (SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP - 1)) &&
1362 (counter < num_array_entries); counter++)
1363 table_data_size += lli_in_array_ptr[counter].block_size;
1366 * Check if we reached the last entry,
1367 * meaning this ia the last table to build,
1368 * and no need to check the block alignment
1370 if (counter == num_array_entries) {
1371 /* Set the last table flag */
1372 *last_table_flag = 1;
1377 * Calculate the data size of the next table.
1378 * Stop if no entries left or if data size is more the DMA restriction
1380 next_table_data_size = 0;
1381 for (; counter < num_array_entries; counter++) {
1382 next_table_data_size += lli_in_array_ptr[counter].block_size;
1383 if (next_table_data_size >= SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)
1388 * Check if the next table data size is less then DMA rstriction.
1389 * if it is - recalculate the current table size, so that the next
1390 * table data size will be adaquete for DMA
1392 if (next_table_data_size &&
1393 next_table_data_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)
1395 table_data_size -= (SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE -
1396 next_table_data_size);
1398 dev_dbg(&sep->pdev->dev, "table data size is %x\n",
1401 return table_data_size;
1405 * sep_build_lli_table - build an lli array for the given table
1406 * @sep: pointer to struct sep_device
1407 * @lli_array_ptr: pointer to lli array
1408 * @lli_table_ptr: pointer to lli table
1409 * @num_processed_entries_ptr: pointer to number of entries
1410 * @num_table_entries_ptr: pointer to number of tables
1411 * @table_data_size: total data size
1413 * Builds ant lli table from the lli_array according to
1414 * the given size of data
1416 static void sep_build_lli_table(struct sep_device *sep,
1417 struct sep_lli_entry *lli_array_ptr,
1418 struct sep_lli_entry *lli_table_ptr,
1419 u32 *num_processed_entries_ptr,
1420 u32 *num_table_entries_ptr,
1421 u32 table_data_size)
1423 /* Current table data size */
1424 u32 curr_table_data_size;
1425 /* Counter of lli array entry */
1428 dev_dbg(&sep->pdev->dev, "sep_build_lli_table start\n");
1430 /* Init currrent table data size and lli array entry counter */
1431 curr_table_data_size = 0;
1433 *num_table_entries_ptr = 1;
1435 dev_dbg(&sep->pdev->dev, "table_data_size is %x\n", table_data_size);
1437 /* Fill the table till table size reaches the needed amount */
1438 while (curr_table_data_size < table_data_size) {
1439 /* Update the number of entries in table */
1440 (*num_table_entries_ptr)++;
1442 lli_table_ptr->bus_address =
1443 cpu_to_le32(lli_array_ptr[array_counter].bus_address);
1445 lli_table_ptr->block_size =
1446 cpu_to_le32(lli_array_ptr[array_counter].block_size);
1448 curr_table_data_size += lli_array_ptr[array_counter].block_size;
1450 dev_dbg(&sep->pdev->dev, "lli_table_ptr is %p\n",
1452 dev_dbg(&sep->pdev->dev, "lli_table_ptr->bus_address is %08lx\n",
1453 (unsigned long)lli_table_ptr->bus_address);
1454 dev_dbg(&sep->pdev->dev, "lli_table_ptr->block_size is %x\n",
1455 lli_table_ptr->block_size);
1457 /* Check for overflow of the table data */
1458 if (curr_table_data_size > table_data_size) {
1459 dev_dbg(&sep->pdev->dev,
1460 "curr_table_data_size too large\n");
1462 /* Update the size of block in the table */
1463 lli_table_ptr->block_size -=
1464 cpu_to_le32((curr_table_data_size - table_data_size));
1466 /* Update the physical address in the lli array */
1467 lli_array_ptr[array_counter].bus_address +=
1468 cpu_to_le32(lli_table_ptr->block_size);
1470 /* Update the block size left in the lli array */
1471 lli_array_ptr[array_counter].block_size =
1472 (curr_table_data_size - table_data_size);
1474 /* Advance to the next entry in the lli_array */
1477 dev_dbg(&sep->pdev->dev,
1478 "lli_table_ptr->bus_address is %08lx\n",
1479 (unsigned long)lli_table_ptr->bus_address);
1480 dev_dbg(&sep->pdev->dev,
1481 "lli_table_ptr->block_size is %x\n",
1482 lli_table_ptr->block_size);
1484 /* Move to the next entry in table */
1488 /* Set the info entry to default */
1489 lli_table_ptr->bus_address = 0xffffffff;
1490 lli_table_ptr->block_size = 0;
1492 dev_dbg(&sep->pdev->dev, "lli_table_ptr is %p\n", lli_table_ptr);
1493 dev_dbg(&sep->pdev->dev, "lli_table_ptr->bus_address is %08lx\n",
1494 (unsigned long)lli_table_ptr->bus_address);
1495 dev_dbg(&sep->pdev->dev, "lli_table_ptr->block_size is %x\n",
1496 lli_table_ptr->block_size);
1498 /* Set the output parameter */
1499 *num_processed_entries_ptr += array_counter;
1501 dev_dbg(&sep->pdev->dev, "num_processed_entries_ptr is %x\n",
1502 *num_processed_entries_ptr);
1504 dev_dbg(&sep->pdev->dev, "sep_build_lli_table end\n");
1508 * sep_shared_area_virt_to_bus - map shared area to bus address
1509 * @sep: pointer to struct sep_device
1510 * @virt_address: virtual address to convert
1512 * This functions returns the physical address inside shared area according
1513 * to the virtual address. It can be either on the externa RAM device
1514 * (ioremapped), or on the system RAM
1515 * This implementation is for the external RAM
1517 static dma_addr_t sep_shared_area_virt_to_bus(struct sep_device *sep,
1520 dev_dbg(&sep->pdev->dev, "sh virt to phys v %p\n", virt_address);
1521 dev_dbg(&sep->pdev->dev, "sh virt to phys p %08lx\n",
1523 sep->shared_bus + (virt_address - sep->shared_addr));
1525 return sep->shared_bus + (size_t)(virt_address - sep->shared_addr);
1529 * sep_shared_area_bus_to_virt - map shared area bus address to kernel
1530 * @sep: pointer to struct sep_device
1531 * @bus_address: bus address to convert
1533 * This functions returns the virtual address inside shared area
1534 * according to the physical address. It can be either on the
1535 * externa RAM device (ioremapped), or on the system RAM
1536 * This implementation is for the external RAM
1538 static void *sep_shared_area_bus_to_virt(struct sep_device *sep,
1539 dma_addr_t bus_address)
1541 dev_dbg(&sep->pdev->dev, "shared bus to virt b=%lx v=%lx\n",
1542 (unsigned long)bus_address, (unsigned long)(sep->shared_addr +
1543 (size_t)(bus_address - sep->shared_bus)));
1545 return sep->shared_addr + (size_t)(bus_address - sep->shared_bus);
1549 * sep_debug_print_lli_tables - dump LLI table
1550 * @sep: pointer to struct sep_device
1551 * @lli_table_ptr: pointer to sep_lli_entry
1552 * @num_table_entries: number of entries
1553 * @table_data_size: total data size
1555 * Walk the the list of the print created tables and print all the data
1557 static void sep_debug_print_lli_tables(struct sep_device *sep,
1558 struct sep_lli_entry *lli_table_ptr,
1559 unsigned long num_table_entries,
1560 unsigned long table_data_size)
1562 unsigned long table_count = 1;
1563 unsigned long entries_count = 0;
1565 dev_dbg(&sep->pdev->dev, "sep_debug_print_lli_tables start\n");
1567 while ((unsigned long) lli_table_ptr->bus_address != 0xffffffff) {
1568 dev_dbg(&sep->pdev->dev,
1569 "lli table %08lx, table_data_size is %lu\n",
1570 table_count, table_data_size);
1571 dev_dbg(&sep->pdev->dev, "num_table_entries is %lu\n",
1574 /* Print entries of the table (without info entry) */
1575 for (entries_count = 0; entries_count < num_table_entries;
1576 entries_count++, lli_table_ptr++) {
1578 dev_dbg(&sep->pdev->dev,
1579 "lli_table_ptr address is %08lx\n",
1580 (unsigned long) lli_table_ptr);
1582 dev_dbg(&sep->pdev->dev,
1583 "phys address is %08lx block size is %x\n",
1584 (unsigned long)lli_table_ptr->bus_address,
1585 lli_table_ptr->block_size);
1587 /* Point to the info entry */
1590 dev_dbg(&sep->pdev->dev,
1591 "phys lli_table_ptr->block_size is %x\n",
1592 lli_table_ptr->block_size);
1594 dev_dbg(&sep->pdev->dev,
1595 "phys lli_table_ptr->physical_address is %08lu\n",
1596 (unsigned long)lli_table_ptr->bus_address);
1599 table_data_size = lli_table_ptr->block_size & 0xffffff;
1600 num_table_entries = (lli_table_ptr->block_size >> 24) & 0xff;
1602 dev_dbg(&sep->pdev->dev,
1603 "phys table_data_size is %lu num_table_entries is"
1604 " %lu bus_address is%lu\n", table_data_size,
1605 num_table_entries, (unsigned long)lli_table_ptr->bus_address);
1607 if ((unsigned long)lli_table_ptr->bus_address != 0xffffffff)
1608 lli_table_ptr = (struct sep_lli_entry *)
1609 sep_shared_bus_to_virt(sep,
1610 (unsigned long)lli_table_ptr->bus_address);
1614 dev_dbg(&sep->pdev->dev, "sep_debug_print_lli_tables end\n");
1619 * sep_prepare_empty_lli_table - create a blank LLI table
1620 * @sep: pointer to struct sep_device
1621 * @lli_table_addr_ptr: pointer to lli table
1622 * @num_entries_ptr: pointer to number of entries
1623 * @table_data_size_ptr: point to table data size
1625 * This function creates empty lli tables when there is no data
1627 static void sep_prepare_empty_lli_table(struct sep_device *sep,
1628 dma_addr_t *lli_table_addr_ptr,
1629 u32 *num_entries_ptr,
1630 u32 *table_data_size_ptr)
1632 struct sep_lli_entry *lli_table_ptr;
1634 dev_dbg(&sep->pdev->dev, "sep_prepare_empty_lli_table start\n");
1636 /* Find the area for new table */
1638 (struct sep_lli_entry *)(sep->shared_addr +
1639 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1640 sep->num_lli_tables_created * sizeof(struct sep_lli_entry) *
1641 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
1643 lli_table_ptr->bus_address = 0;
1644 lli_table_ptr->block_size = 0;
1647 lli_table_ptr->bus_address = 0xFFFFFFFF;
1648 lli_table_ptr->block_size = 0;
1650 /* Set the output parameter value */
1651 *lli_table_addr_ptr = sep->shared_bus +
1652 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1653 sep->num_lli_tables_created *
1654 sizeof(struct sep_lli_entry) *
1655 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1657 /* Set the num of entries and table data size for empty table */
1658 *num_entries_ptr = 2;
1659 *table_data_size_ptr = 0;
1661 /* Update the number of created tables */
1662 sep->num_lli_tables_created++;
1664 dev_dbg(&sep->pdev->dev, "sep_prepare_empty_lli_table start\n");
1669 * sep_prepare_input_dma_table - prepare input DMA mappings
1670 * @sep: pointer to struct sep_device
1675 * @table_data_size_ptr:
1676 * @is_kva: set for kernel data (kernel cryptio call)
1678 * This function prepares only input DMA table for synhronic symmetric
1680 * Note that all bus addresses that are passed to the SEP
1681 * are in 32 bit format; the SEP is a 32 bit device
1683 static int sep_prepare_input_dma_table(struct sep_device *sep,
1684 unsigned long app_virt_addr,
1687 dma_addr_t *lli_table_ptr,
1688 u32 *num_entries_ptr,
1689 u32 *table_data_size_ptr,
1693 /* Pointer to the info entry of the table - the last entry */
1694 struct sep_lli_entry *info_entry_ptr;
1695 /* Array of pointers to page */
1696 struct sep_lli_entry *lli_array_ptr;
1697 /* Points to the first entry to be processed in the lli_in_array */
1698 u32 current_entry = 0;
1699 /* Num entries in the virtual buffer */
1700 u32 sep_lli_entries = 0;
1701 /* Lli table pointer */
1702 struct sep_lli_entry *in_lli_table_ptr;
1703 /* The total data in one table */
1704 u32 table_data_size = 0;
1705 /* Flag for last table */
1706 u32 last_table_flag = 0;
1707 /* Number of entries in lli table */
1708 u32 num_entries_in_table = 0;
1709 /* Next table address */
1710 void *lli_table_alloc_addr = 0;
1712 dev_dbg(&sep->pdev->dev, "sep_prepare_input_dma_table start\n");
1713 dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1714 dev_dbg(&sep->pdev->dev, "block_size is %x\n", block_size);
1716 /* Initialize the pages pointers */
1717 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
1718 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 0;
1720 /* Set the kernel address for first table to be allocated */
1721 lli_table_alloc_addr = (void *)(sep->shared_addr +
1722 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1723 sep->num_lli_tables_created * sizeof(struct sep_lli_entry) *
1724 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
1726 if (data_size == 0) {
1727 /* Special case - create meptu table - 2 entries, zero data */
1728 sep_prepare_empty_lli_table(sep, lli_table_ptr,
1729 num_entries_ptr, table_data_size_ptr);
1730 goto update_dcb_counter;
1733 /* Check if the pages are in Kernel Virtual Address layout */
1735 /* Lock the pages in the kernel */
1736 error = sep_lock_kernel_pages(sep, app_virt_addr,
1737 data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
1740 * Lock the pages of the user buffer
1741 * and translate them to pages
1743 error = sep_lock_user_pages(sep, app_virt_addr,
1744 data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
1749 dev_dbg(&sep->pdev->dev, "output sep_in_num_pages is %x\n",
1750 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
1753 info_entry_ptr = NULL;
1755 sep_lli_entries = sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages;
1757 /* Loop till all the entries in in array are not processed */
1758 while (current_entry < sep_lli_entries) {
1760 /* Set the new input and output tables */
1762 (struct sep_lli_entry *)lli_table_alloc_addr;
1764 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1765 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1767 if (lli_table_alloc_addr >
1768 ((void *)sep->shared_addr +
1769 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1770 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {
1773 goto end_function_error;
1777 /* Update the number of created tables */
1778 sep->num_lli_tables_created++;
1780 /* Calculate the maximum size of data for input table */
1781 table_data_size = sep_calculate_lli_table_max_size(sep,
1782 &lli_array_ptr[current_entry],
1783 (sep_lli_entries - current_entry),
1787 * If this is not the last table -
1788 * then allign it to the block size
1790 if (!last_table_flag)
1792 (table_data_size / block_size) * block_size;
1794 dev_dbg(&sep->pdev->dev, "output table_data_size is %x\n",
1797 /* Construct input lli table */
1798 sep_build_lli_table(sep, &lli_array_ptr[current_entry],
1800 ¤t_entry, &num_entries_in_table, table_data_size);
1802 if (info_entry_ptr == NULL) {
1804 /* Set the output parameters to physical addresses */
1805 *lli_table_ptr = sep_shared_area_virt_to_bus(sep,
1807 *num_entries_ptr = num_entries_in_table;
1808 *table_data_size_ptr = table_data_size;
1810 dev_dbg(&sep->pdev->dev,
1811 "output lli_table_in_ptr is %08lx\n",
1812 (unsigned long)*lli_table_ptr);
1815 /* Update the info entry of the previous in table */
1816 info_entry_ptr->bus_address =
1817 sep_shared_area_virt_to_bus(sep,
1819 info_entry_ptr->block_size =
1820 ((num_entries_in_table) << 24) |
1823 /* Save the pointer to the info entry of the current tables */
1824 info_entry_ptr = in_lli_table_ptr + num_entries_in_table - 1;
1826 /* Print input tables */
1827 sep_debug_print_lli_tables(sep, (struct sep_lli_entry *)
1828 sep_shared_area_bus_to_virt(sep, *lli_table_ptr),
1829 *num_entries_ptr, *table_data_size_ptr);
1830 /* The array of the pages */
1831 kfree(lli_array_ptr);
1834 /* Update DCB counter */
1835 sep->nr_dcb_creat++;
1839 /* Free all the allocated resources */
1840 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
1841 kfree(lli_array_ptr);
1842 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
1845 dev_dbg(&sep->pdev->dev, "sep_prepare_input_dma_table end\n");
1850 * sep_construct_dma_tables_from_lli - prepare AES/DES mappings
1851 * @sep: pointer to struct sep_device
1853 * @sep_in_lli_entries:
1855 * @sep_out_lli_entries
1858 * @lli_table_out_ptr
1859 * @in_num_entries_ptr
1860 * @out_num_entries_ptr
1861 * @table_data_size_ptr
1863 * This function creates the input and output DMA tables for
1864 * symmetric operations (AES/DES) according to the block
1865 * size from LLI arays
1866 * Note that all bus addresses that are passed to the SEP
1867 * are in 32 bit format; the SEP is a 32 bit device
1869 static int sep_construct_dma_tables_from_lli(
1870 struct sep_device *sep,
1871 struct sep_lli_entry *lli_in_array,
1872 u32 sep_in_lli_entries,
1873 struct sep_lli_entry *lli_out_array,
1874 u32 sep_out_lli_entries,
1876 dma_addr_t *lli_table_in_ptr,
1877 dma_addr_t *lli_table_out_ptr,
1878 u32 *in_num_entries_ptr,
1879 u32 *out_num_entries_ptr,
1880 u32 *table_data_size_ptr)
1882 /* Points to the area where next lli table can be allocated */
1883 void *lli_table_alloc_addr = 0;
1884 /* Input lli table */
1885 struct sep_lli_entry *in_lli_table_ptr = NULL;
1886 /* Output lli table */
1887 struct sep_lli_entry *out_lli_table_ptr = NULL;
1888 /* Pointer to the info entry of the table - the last entry */
1889 struct sep_lli_entry *info_in_entry_ptr = NULL;
1890 /* Pointer to the info entry of the table - the last entry */
1891 struct sep_lli_entry *info_out_entry_ptr = NULL;
1892 /* Points to the first entry to be processed in the lli_in_array */
1893 u32 current_in_entry = 0;
1894 /* Points to the first entry to be processed in the lli_out_array */
1895 u32 current_out_entry = 0;
1896 /* Max size of the input table */
1897 u32 in_table_data_size = 0;
1898 /* Max size of the output table */
1899 u32 out_table_data_size = 0;
1900 /* Flag te signifies if this is the last tables build */
1901 u32 last_table_flag = 0;
1902 /* The data size that should be in table */
1903 u32 table_data_size = 0;
1904 /* Number of etnries in the input table */
1905 u32 num_entries_in_table = 0;
1906 /* Number of etnries in the output table */
1907 u32 num_entries_out_table = 0;
1909 dev_dbg(&sep->pdev->dev, "sep_construct_dma_tables_from_lli start\n");
1911 /* Initiate to point after the message area */
1912 lli_table_alloc_addr = (void *)(sep->shared_addr +
1913 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1914 (sep->num_lli_tables_created *
1915 (sizeof(struct sep_lli_entry) *
1916 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP)));
1918 /* Loop till all the entries in in array are not processed */
1919 while (current_in_entry < sep_in_lli_entries) {
1920 /* Set the new input and output tables */
1922 (struct sep_lli_entry *)lli_table_alloc_addr;
1924 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1925 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1927 /* Set the first output tables */
1929 (struct sep_lli_entry *)lli_table_alloc_addr;
1931 /* Check if the DMA table area limit was overrun */
1932 if ((lli_table_alloc_addr + sizeof(struct sep_lli_entry) *
1933 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP) >
1934 ((void *)sep->shared_addr +
1935 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1936 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {
1938 dev_warn(&sep->pdev->dev, "dma table limit overrun\n");
1942 /* Update the number of the lli tables created */
1943 sep->num_lli_tables_created += 2;
1945 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1946 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1948 /* Calculate the maximum size of data for input table */
1949 in_table_data_size =
1950 sep_calculate_lli_table_max_size(sep,
1951 &lli_in_array[current_in_entry],
1952 (sep_in_lli_entries - current_in_entry),
1955 /* Calculate the maximum size of data for output table */
1956 out_table_data_size =
1957 sep_calculate_lli_table_max_size(sep,
1958 &lli_out_array[current_out_entry],
1959 (sep_out_lli_entries - current_out_entry),
1962 dev_dbg(&sep->pdev->dev,
1963 "in_table_data_size is %x\n",
1964 in_table_data_size);
1966 dev_dbg(&sep->pdev->dev,
1967 "out_table_data_size is %x\n",
1968 out_table_data_size);
1970 table_data_size = in_table_data_size;
1972 if (!last_table_flag) {
1974 * If this is not the last table,
1975 * then must check where the data is smallest
1976 * and then align it to the block size
1978 if (table_data_size > out_table_data_size)
1979 table_data_size = out_table_data_size;
1982 * Now calculate the table size so that
1983 * it will be module block size
1985 table_data_size = (table_data_size / block_size) *
1989 dev_dbg(&sep->pdev->dev, "table_data_size is %x\n",
1992 /* Construct input lli table */
1993 sep_build_lli_table(sep, &lli_in_array[current_in_entry],
1996 &num_entries_in_table,
1999 /* Construct output lli table */
2000 sep_build_lli_table(sep, &lli_out_array[current_out_entry],
2003 &num_entries_out_table,
2006 /* If info entry is null - this is the first table built */
2007 if (info_in_entry_ptr == NULL) {
2008 /* Set the output parameters to physical addresses */
2010 sep_shared_area_virt_to_bus(sep, in_lli_table_ptr);
2012 *in_num_entries_ptr = num_entries_in_table;
2014 *lli_table_out_ptr =
2015 sep_shared_area_virt_to_bus(sep,
2018 *out_num_entries_ptr = num_entries_out_table;
2019 *table_data_size_ptr = table_data_size;
2021 dev_dbg(&sep->pdev->dev,
2022 "output lli_table_in_ptr is %08lx\n",
2023 (unsigned long)*lli_table_in_ptr);
2024 dev_dbg(&sep->pdev->dev,
2025 "output lli_table_out_ptr is %08lx\n",
2026 (unsigned long)*lli_table_out_ptr);
2028 /* Update the info entry of the previous in table */
2029 info_in_entry_ptr->bus_address =
2030 sep_shared_area_virt_to_bus(sep,
2033 info_in_entry_ptr->block_size =
2034 ((num_entries_in_table) << 24) |
2037 /* Update the info entry of the previous in table */
2038 info_out_entry_ptr->bus_address =
2039 sep_shared_area_virt_to_bus(sep,
2042 info_out_entry_ptr->block_size =
2043 ((num_entries_out_table) << 24) |
2046 dev_dbg(&sep->pdev->dev,
2047 "output lli_table_in_ptr:%08lx %08x\n",
2048 (unsigned long)info_in_entry_ptr->bus_address,
2049 info_in_entry_ptr->block_size);
2051 dev_dbg(&sep->pdev->dev,
2052 "output lli_table_out_ptr:%08lx %08x\n",
2053 (unsigned long)info_out_entry_ptr->bus_address,
2054 info_out_entry_ptr->block_size);
2057 /* Save the pointer to the info entry of the current tables */
2058 info_in_entry_ptr = in_lli_table_ptr +
2059 num_entries_in_table - 1;
2060 info_out_entry_ptr = out_lli_table_ptr +
2061 num_entries_out_table - 1;
2063 dev_dbg(&sep->pdev->dev,
2064 "output num_entries_out_table is %x\n",
2065 (u32)num_entries_out_table);
2066 dev_dbg(&sep->pdev->dev,
2067 "output info_in_entry_ptr is %lx\n",
2068 (unsigned long)info_in_entry_ptr);
2069 dev_dbg(&sep->pdev->dev,
2070 "output info_out_entry_ptr is %lx\n",
2071 (unsigned long)info_out_entry_ptr);
2074 /* Print input tables */
2075 sep_debug_print_lli_tables(sep,
2076 (struct sep_lli_entry *)
2077 sep_shared_area_bus_to_virt(sep, *lli_table_in_ptr),
2078 *in_num_entries_ptr,
2079 *table_data_size_ptr);
2081 /* Print output tables */
2082 sep_debug_print_lli_tables(sep,
2083 (struct sep_lli_entry *)
2084 sep_shared_area_bus_to_virt(sep, *lli_table_out_ptr),
2085 *out_num_entries_ptr,
2086 *table_data_size_ptr);
2088 dev_dbg(&sep->pdev->dev, "sep_construct_dma_tables_from_lli end\n");
2093 * sep_prepare_input_output_dma_table - prepare DMA I/O table
2094 * @app_virt_in_addr:
2095 * @app_virt_out_addr:
2098 * @lli_table_in_ptr:
2099 * @lli_table_out_ptr:
2100 * @in_num_entries_ptr:
2101 * @out_num_entries_ptr:
2102 * @table_data_size_ptr:
2103 * @is_kva: set for kernel data; used only for kernel crypto module
2105 * This function builds input and output DMA tables for synhronic
2106 * symmetric operations (AES, DES, HASH). It also checks that each table
2107 * is of the modular block size
2108 * Note that all bus addresses that are passed to the SEP
2109 * are in 32 bit format; the SEP is a 32 bit device
2111 static int sep_prepare_input_output_dma_table(struct sep_device *sep,
2112 unsigned long app_virt_in_addr,
2113 unsigned long app_virt_out_addr,
2116 dma_addr_t *lli_table_in_ptr,
2117 dma_addr_t *lli_table_out_ptr,
2118 u32 *in_num_entries_ptr,
2119 u32 *out_num_entries_ptr,
2120 u32 *table_data_size_ptr,
2125 /* Array of pointers of page */
2126 struct sep_lli_entry *lli_in_array;
2127 /* Array of pointers of page */
2128 struct sep_lli_entry *lli_out_array;
2130 dev_dbg(&sep->pdev->dev, "sep_prepare_input_output_dma_table start\n");
2132 if (data_size == 0) {
2133 /* Prepare empty table for input and output */
2134 sep_prepare_empty_lli_table(sep, lli_table_in_ptr,
2135 in_num_entries_ptr, table_data_size_ptr);
2137 sep_prepare_empty_lli_table(sep, lli_table_out_ptr,
2138 out_num_entries_ptr, table_data_size_ptr);
2140 goto update_dcb_counter;
2143 /* Initialize the pages pointers */
2144 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
2145 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = NULL;
2147 /* Lock the pages of the buffer and translate them to pages */
2148 if (is_kva == true) {
2149 error = sep_lock_kernel_pages(sep, app_virt_in_addr,
2150 data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
2153 dev_warn(&sep->pdev->dev,
2154 "lock kernel for in failed\n");
2158 error = sep_lock_kernel_pages(sep, app_virt_out_addr,
2159 data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);
2162 dev_warn(&sep->pdev->dev,
2163 "lock kernel for out failed\n");
2169 error = sep_lock_user_pages(sep, app_virt_in_addr,
2170 data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
2172 dev_warn(&sep->pdev->dev,
2173 "sep_lock_user_pages for input virtual buffer failed\n");
2177 error = sep_lock_user_pages(sep, app_virt_out_addr,
2178 data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);
2181 dev_warn(&sep->pdev->dev,
2182 "sep_lock_user_pages for output virtual buffer failed\n");
2183 goto end_function_free_lli_in;
2187 dev_dbg(&sep->pdev->dev, "sep_in_num_pages is %x\n",
2188 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
2189 dev_dbg(&sep->pdev->dev, "sep_out_num_pages is %x\n",
2190 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages);
2191 dev_dbg(&sep->pdev->dev, "SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP is %x\n",
2192 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
2194 /* Call the fucntion that creates table from the lli arrays */
2195 error = sep_construct_dma_tables_from_lli(sep, lli_in_array,
2196 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages,
2198 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages,
2199 block_size, lli_table_in_ptr, lli_table_out_ptr,
2200 in_num_entries_ptr, out_num_entries_ptr, table_data_size_ptr);
2203 dev_warn(&sep->pdev->dev,
2204 "sep_construct_dma_tables_from_lli failed\n");
2205 goto end_function_with_error;
2208 kfree(lli_out_array);
2209 kfree(lli_in_array);
2212 /* Update DCB counter */
2213 sep->nr_dcb_creat++;
2214 /* Fall through - free the lli entry arrays */
2215 dev_dbg(&sep->pdev->dev, "in_num_entries_ptr is %08x\n",
2216 *in_num_entries_ptr);
2217 dev_dbg(&sep->pdev->dev, "out_num_entries_ptr is %08x\n",
2218 *out_num_entries_ptr);
2219 dev_dbg(&sep->pdev->dev, "table_data_size_ptr is %08x\n",
2220 *table_data_size_ptr);
2224 end_function_with_error:
2225 kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_map_array);
2226 kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_page_array);
2227 kfree(lli_out_array);
2230 end_function_free_lli_in:
2231 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
2232 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
2233 kfree(lli_in_array);
2236 dev_dbg(&sep->pdev->dev,
2237 "sep_prepare_input_output_dma_table end result = %d\n", error);
2244 * sep_prepare_input_output_dma_table_in_dcb - prepare control blocks
2245 * @app_in_address: unsigned long; for data buffer in (user space)
2246 * @app_out_address: unsigned long; for data buffer out (user space)
2247 * @data_in_size: u32; for size of data
2248 * @block_size: u32; for block size
2249 * @tail_block_size: u32; for size of tail block
2250 * @isapplet: bool; to indicate external app
2251 * @is_kva: bool; kernel buffer; only used for kernel crypto module
2253 * This function prepares the linked DMA tables and puts the
2254 * address for the linked list of tables inta a DCB (data control
2255 * block) the address of which is known by the SEP hardware
2256 * Note that all bus addresses that are passed to the SEP
2257 * are in 32 bit format; the SEP is a 32 bit device
2259 static int sep_prepare_input_output_dma_table_in_dcb(struct sep_device *sep,
2260 unsigned long app_in_address,
2261 unsigned long app_out_address,
2264 u32 tail_block_size,
2271 /* Address of the created DCB table */
2272 struct sep_dcblock *dcb_table_ptr = NULL;
2273 /* The physical address of the first input DMA table */
2274 dma_addr_t in_first_mlli_address = 0;
2275 /* Number of entries in the first input DMA table */
2276 u32 in_first_num_entries = 0;
2277 /* The physical address of the first output DMA table */
2278 dma_addr_t out_first_mlli_address = 0;
2279 /* Number of entries in the first output DMA table */
2280 u32 out_first_num_entries = 0;
2281 /* Data in the first input/output table */
2282 u32 first_data_size = 0;
2284 dev_dbg(&sep->pdev->dev, "prepare_input_output_dma_table_in_dcb start\n");
2286 if (sep->nr_dcb_creat == SEP_MAX_NUM_SYNC_DMA_OPS) {
2287 /* No more DCBs to allocate */
2288 dev_warn(&sep->pdev->dev, "no more DCBs available\n");
2293 /* Allocate new DCB */
2294 dcb_table_ptr = (struct sep_dcblock *)(sep->shared_addr +
2295 SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES +
2296 (sep->nr_dcb_creat * sizeof(struct sep_dcblock)));
2298 /* Set the default values in the DCB */
2299 dcb_table_ptr->input_mlli_address = 0;
2300 dcb_table_ptr->input_mlli_num_entries = 0;
2301 dcb_table_ptr->input_mlli_data_size = 0;
2302 dcb_table_ptr->output_mlli_address = 0;
2303 dcb_table_ptr->output_mlli_num_entries = 0;
2304 dcb_table_ptr->output_mlli_data_size = 0;
2305 dcb_table_ptr->tail_data_size = 0;
2306 dcb_table_ptr->out_vr_tail_pt = 0;
2308 if (isapplet == true) {
2309 tail_size = data_in_size % block_size;
2311 if (data_in_size < tail_block_size) {
2312 dev_warn(&sep->pdev->dev, "data in size smaller than tail block size\n");
2316 if (tail_block_size)
2318 * Case the tail size should be
2319 * bigger than the real block size
2321 tail_size = tail_block_size +
2323 tail_block_size) % block_size);
2326 /* Check if there is enough data for DMA operation */
2327 if (data_in_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE) {
2328 if (is_kva == true) {
2329 memcpy(dcb_table_ptr->tail_data,
2330 (void *)app_in_address, data_in_size);
2332 if (copy_from_user(dcb_table_ptr->tail_data,
2333 (void __user *)app_in_address,
2340 dcb_table_ptr->tail_data_size = data_in_size;
2342 /* Set the output user-space address for mem2mem op */
2343 if (app_out_address)
2344 dcb_table_ptr->out_vr_tail_pt =
2345 (u32)app_out_address;
2348 * Update both data length parameters in order to avoid
2349 * second data copy and allow building of empty mlli
2356 if (is_kva == true) {
2357 memcpy(dcb_table_ptr->tail_data,
2358 (void *)(app_in_address + data_in_size -
2359 tail_size), tail_size);
2361 /* We have tail data - copy it to DCB */
2362 if (copy_from_user(dcb_table_ptr->tail_data,
2363 (void *)(app_in_address +
2364 data_in_size - tail_size), tail_size)) {
2369 if (app_out_address)
2371 * Calculate the output address
2372 * according to tail data size
2374 dcb_table_ptr->out_vr_tail_pt =
2375 (u32)app_out_address + data_in_size
2378 /* Save the real tail data size */
2379 dcb_table_ptr->tail_data_size = tail_size;
2381 * Update the data size without the tail
2382 * data size AKA data for the dma
2384 data_in_size = (data_in_size - tail_size);
2387 /* Check if we need to build only input table or input/output */
2388 if (app_out_address) {
2389 /* Prepare input/output tables */
2390 error = sep_prepare_input_output_dma_table(sep,
2395 &in_first_mlli_address,
2396 &out_first_mlli_address,
2397 &in_first_num_entries,
2398 &out_first_num_entries,
2402 /* Prepare input tables */
2403 error = sep_prepare_input_dma_table(sep,
2407 &in_first_mlli_address,
2408 &in_first_num_entries,
2414 dev_warn(&sep->pdev->dev, "prepare DMA table call failed from prepare DCB call\n");
2418 /* Set the DCB values */
2419 dcb_table_ptr->input_mlli_address = in_first_mlli_address;
2420 dcb_table_ptr->input_mlli_num_entries = in_first_num_entries;
2421 dcb_table_ptr->input_mlli_data_size = first_data_size;
2422 dcb_table_ptr->output_mlli_address = out_first_mlli_address;
2423 dcb_table_ptr->output_mlli_num_entries = out_first_num_entries;
2424 dcb_table_ptr->output_mlli_data_size = first_data_size;
2427 dev_dbg(&sep->pdev->dev,
2428 "sep_prepare_input_output_dma_table_in_dcb end\n");
2435 * sep_create_sync_dma_tables_handler - create sync DMA tables
2436 * @sep: pointer to struct sep_device
2437 * @arg: pointer to struct bld_syn_tab_struct
2439 * Handle the request for creation of the DMA tables for the synchronic
2440 * symmetric operations (AES,DES). Note that all bus addresses that are
2441 * passed to the SEP are in 32 bit format; the SEP is a 32 bit device
2443 static int sep_create_sync_dma_tables_handler(struct sep_device *sep,
2448 /* Command arguments */
2449 struct bld_syn_tab_struct command_args;
2451 dev_dbg(&sep->pdev->dev,
2452 "sep_create_sync_dma_tables_handler start\n");
2454 if (copy_from_user(&command_args, (void __user *)arg,
2455 sizeof(struct bld_syn_tab_struct))) {
2460 dev_dbg(&sep->pdev->dev, "app_in_address is %08llx\n",
2461 command_args.app_in_address);
2462 dev_dbg(&sep->pdev->dev, "app_out_address is %08llx\n",
2463 command_args.app_out_address);
2464 dev_dbg(&sep->pdev->dev, "data_size is %u\n",
2465 command_args.data_in_size);
2466 dev_dbg(&sep->pdev->dev, "block_size is %u\n",
2467 command_args.block_size);
2469 /* Validate user parameters */
2470 if (!command_args.app_in_address) {
2475 error = sep_prepare_input_output_dma_table_in_dcb(sep,
2476 (unsigned long)command_args.app_in_address,
2477 (unsigned long)command_args.app_out_address,
2478 command_args.data_in_size,
2479 command_args.block_size,
2485 dev_dbg(&sep->pdev->dev, "sep_create_sync_dma_tables_handler end\n");
2490 * sep_free_dma_tables_and_dcb - free DMA tables and DCBs
2491 * @sep: pointer to struct sep_device
2492 * @isapplet: indicates external application (used for kernel access)
2493 * @is_kva: indicates kernel addresses (only used for kernel crypto)
2495 * This function frees the DMA tables and DCB
2497 static int sep_free_dma_tables_and_dcb(struct sep_device *sep, bool isapplet,
2503 struct sep_dcblock *dcb_table_ptr;
2504 unsigned long pt_hold;
2507 dev_dbg(&sep->pdev->dev, "sep_free_dma_tables_and_dcb start\n");
2509 if (isapplet == true) {
2510 /* Set pointer to first DCB table */
2511 dcb_table_ptr = (struct sep_dcblock *)
2513 SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES);
2515 /* Go over each DCB and see if tail pointer must be updated */
2516 for (i = 0; i < sep->nr_dcb_creat; i++, dcb_table_ptr++) {
2517 if (dcb_table_ptr->out_vr_tail_pt) {
2518 pt_hold = (unsigned long)dcb_table_ptr->out_vr_tail_pt;
2519 tail_pt = (void *)pt_hold;
2520 if (is_kva == true) {
2522 dcb_table_ptr->tail_data,
2523 dcb_table_ptr->tail_data_size);
2525 error_temp = copy_to_user(
2527 dcb_table_ptr->tail_data,
2528 dcb_table_ptr->tail_data_size);
2531 /* Release the DMA resource */
2538 /* Free the output pages, if any */
2539 sep_free_dma_table_data_handler(sep);
2541 dev_dbg(&sep->pdev->dev, "sep_free_dma_tables_and_dcb end\n");
2546 * sep_get_static_pool_addr_handler - get static pool address
2547 * @sep: pointer to struct sep_device
2549 * This function sets the bus and virtual addresses of the static pool
2551 static int sep_get_static_pool_addr_handler(struct sep_device *sep)
2553 u32 *static_pool_addr = NULL;
2555 dev_dbg(&sep->pdev->dev, "sep_get_static_pool_addr_handler start\n");
2557 static_pool_addr = (u32 *)(sep->shared_addr +
2558 SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);
2560 static_pool_addr[0] = SEP_STATIC_POOL_VAL_TOKEN;
2561 static_pool_addr[1] = (u32)sep->shared_bus +
2562 SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;
2564 dev_dbg(&sep->pdev->dev, "static pool: physical %x\n",
2565 (u32)static_pool_addr[1]);
2567 dev_dbg(&sep->pdev->dev, "sep_get_static_pool_addr_handler end\n");
2573 * sep_start_handler - start device
2574 * @sep: pointer to struct sep_device
2576 static int sep_start_handler(struct sep_device *sep)
2578 unsigned long reg_val;
2579 unsigned long error = 0;
2581 dev_dbg(&sep->pdev->dev, "sep_start_handler start\n");
2583 /* Wait in polling for message from SEP */
2585 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2588 /* Check the value */
2590 /* Fatal error - read error status from GPRO */
2591 error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
2592 dev_dbg(&sep->pdev->dev, "sep_start_handler end\n");
2597 * ep_check_sum_calc - checksum messages
2598 * @data: buffer to checksum
2599 * @length: buffer size
2601 * This function performs a checksum for messages that are sent
2604 static u32 sep_check_sum_calc(u8 *data, u32 length)
2607 u16 *Tdata = (u16 *)data;
2609 while (length > 1) {
2610 /* This is the inner loop */
2615 /* Add left-over byte, if any */
2617 sum += *(u8 *)Tdata;
2619 /* Fold 32-bit sum to 16 bits */
2621 sum = (sum & 0xffff) + (sum >> 16);
2623 return ~sum & 0xFFFF;
2627 * sep_init_handler -
2628 * @sep: pointer to struct sep_device
2629 * @arg: parameters from user space application
2631 * Handles the request for SEP initialization
2632 * Note that this will go away for Medfield once the SCU
2633 * SEP initialization is complete
2634 * Also note that the message to the SEP has components
2635 * from user space as well as components written by the driver
2636 * This is becuase the portions of the message that pertain to
2637 * physical addresses must be set by the driver after the message
2638 * leaves custody of the user space application for security
2641 static int sep_init_handler(struct sep_device *sep, unsigned long arg)
2643 u32 message_buff[14];
2647 dma_addr_t new_base_addr;
2648 unsigned long addr_hold;
2649 struct init_struct command_args;
2651 dev_dbg(&sep->pdev->dev, "sep_init_handler start\n");
2653 /* Make sure that we have not initialized already */
2654 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2656 if (reg_val != 0x2) {
2657 error = SEP_ALREADY_INITIALIZED_ERR;
2658 dev_warn(&sep->pdev->dev, "init; device already initialized\n");
2662 /* Only root can initialize */
2663 if (!capable(CAP_SYS_ADMIN)) {
2668 /* Copy in the parameters */
2669 error = copy_from_user(&command_args, (void __user *)arg,
2670 sizeof(struct init_struct));
2677 /* Validate parameters */
2678 if (!command_args.message_addr || !command_args.sep_sram_addr ||
2679 command_args.message_size_in_words > 14) {
2684 /* Copy in the SEP init message */
2685 addr_hold = (unsigned long)command_args.message_addr;
2686 error = copy_from_user(message_buff,
2687 (void __user *)addr_hold,
2688 command_args.message_size_in_words*sizeof(u32));
2695 /* Load resident, cache, and extapp firmware */
2696 error = sep_load_firmware(sep);
2699 dev_warn(&sep->pdev->dev,
2700 "init; copy SEP init message failed %x\n", error);
2704 /* Compute the base address */
2705 new_base_addr = sep->shared_bus;
2707 if (sep->resident_bus < new_base_addr)
2708 new_base_addr = sep->resident_bus;
2710 if (sep->cache_bus < new_base_addr)
2711 new_base_addr = sep->cache_bus;
2713 if (sep->dcache_bus < new_base_addr)
2714 new_base_addr = sep->dcache_bus;
2716 /* Put physical addresses in SEP message */
2717 message_buff[3] = (u32)new_base_addr;
2718 message_buff[4] = (u32)sep->shared_bus;
2719 message_buff[6] = (u32)sep->resident_bus;
2720 message_buff[7] = (u32)sep->cache_bus;
2721 message_buff[8] = (u32)sep->dcache_bus;
2723 message_buff[command_args.message_size_in_words - 1] = 0x0;
2724 message_buff[command_args.message_size_in_words - 1] =
2725 sep_check_sum_calc((u8 *)message_buff,
2726 command_args.message_size_in_words*sizeof(u32));
2728 /* Debug print of message */
2729 for (counter = 0; counter < command_args.message_size_in_words;
2731 dev_dbg(&sep->pdev->dev, "init; SEP message word %d is %x\n",
2732 counter, message_buff[counter]);
2734 /* Tell the SEP the sram address */
2735 sep_write_reg(sep, HW_SRAM_ADDR_REG_ADDR, command_args.sep_sram_addr);
2737 /* Push the message to the SEP */
2738 for (counter = 0; counter < command_args.message_size_in_words;
2740 sep_write_reg(sep, HW_SRAM_DATA_REG_ADDR,
2741 message_buff[counter]);
2742 sep_wait_sram_write(sep);
2745 /* Signal SEP that message is ready and to init */
2746 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x1);
2748 /* Wait for acknowledge */
2749 dev_dbg(&sep->pdev->dev, "init; waiting for msg response\n");
2752 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2753 } while (!(reg_val & 0xFFFFFFFD));
2755 if (reg_val == 0x1) {
2756 dev_warn(&sep->pdev->dev, "init; device int failed\n");
2757 error = sep_read_reg(sep, 0x8060);
2758 dev_warn(&sep->pdev->dev, "init; sw monitor is %x\n", error);
2759 error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
2760 dev_warn(&sep->pdev->dev, "init; error is %x\n", error);
2763 dev_dbg(&sep->pdev->dev, "init; end CC INIT, reg_val is %x\n", reg_val);
2765 /* Signal SEP to zero the GPR3 */
2766 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x10);
2768 /* Wait for response */
2769 dev_dbg(&sep->pdev->dev, "init; waiting for zero set response\n");
2772 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2773 } while (reg_val != 0);
2776 dev_dbg(&sep->pdev->dev, "init is done\n");
2781 * sep_end_transaction_handler - end transaction
2782 * @sep: pointer to struct sep_device
2784 * This API handles the end transaction request
2786 static int sep_end_transaction_handler(struct sep_device *sep)
2788 dev_dbg(&sep->pdev->dev, "sep_end_transaction_handler start\n");
2790 /* Clear the data pool pointers Token */
2791 memset((void *)(sep->shared_addr +
2792 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES),
2793 0, sep->num_of_data_allocations*2*sizeof(u32));
2795 /* Check that all the DMA resources were freed */
2796 sep_free_dma_table_data_handler(sep);
2798 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
2801 * We are now through with the transaction. Let's
2802 * allow other processes who have the device open
2803 * to perform transactions
2805 mutex_lock(&sep->sep_mutex);
2806 sep->pid_doing_transaction = 0;
2807 mutex_unlock(&sep->sep_mutex);
2808 /* Raise event for stuck contextes */
2809 wake_up(&sep->event);
2811 dev_dbg(&sep->pdev->dev, "waking up event\n");
2812 dev_dbg(&sep->pdev->dev, "sep_end_transaction_handler end\n");
2818 * sep_prepare_dcb_handler - prepare a control block
2819 * @sep: pointer to struct sep_device
2820 * @arg: pointer to user parameters
2822 * This function will retrieve the RAR buffer physical addresses, type
2823 * & size corresponding to the RAR handles provided in the buffers vector.
2825 static int sep_prepare_dcb_handler(struct sep_device *sep, unsigned long arg)
2828 /* Command arguments */
2829 struct build_dcb_struct command_args;
2831 dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler start\n");
2833 /* Get the command arguments */
2834 if (copy_from_user(&command_args, (void __user *)arg,
2835 sizeof(struct build_dcb_struct))) {
2840 dev_dbg(&sep->pdev->dev, "app_in_address is %08llx\n",
2841 command_args.app_in_address);
2842 dev_dbg(&sep->pdev->dev, "app_out_address is %08llx\n",
2843 command_args.app_out_address);
2844 dev_dbg(&sep->pdev->dev, "data_size is %x\n",
2845 command_args.data_in_size);
2846 dev_dbg(&sep->pdev->dev, "block_size is %x\n",
2847 command_args.block_size);
2848 dev_dbg(&sep->pdev->dev, "tail block_size is %x\n",
2849 command_args.tail_block_size);
2851 error = sep_prepare_input_output_dma_table_in_dcb(sep,
2852 (unsigned long)command_args.app_in_address,
2853 (unsigned long)command_args.app_out_address,
2854 command_args.data_in_size, command_args.block_size,
2855 command_args.tail_block_size, true, false);
2858 dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler end\n");
2864 * sep_free_dcb_handler - free control block resources
2865 * @sep: pointer to struct sep_device
2867 * This function frees the DCB resources and updates the needed
2868 * user-space buffers.
2870 static int sep_free_dcb_handler(struct sep_device *sep)
2874 dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler start\n");
2875 dev_dbg(&sep->pdev->dev, "num of DCBs %x\n", sep->nr_dcb_creat);
2877 error = sep_free_dma_tables_and_dcb(sep, false, false);
2879 dev_dbg(&sep->pdev->dev, "sep_free_dcb_handler end\n");
2884 * sep_rar_prepare_output_msg_handler - prepare an output message
2885 * @sep: pointer to struct sep_device
2886 * @arg: pointer to user parameters
2888 * This function will retrieve the RAR buffer physical addresses, type
2889 * & size corresponding to the RAR handles provided in the buffers vector.
2891 static int sep_rar_prepare_output_msg_handler(struct sep_device *sep,
2896 struct rar_hndl_to_bus_struct command_args;
2897 struct RAR_buffer rar_buf;
2899 dma_addr_t rar_bus = 0;
2900 /* Holds the RAR address in the system memory offset */
2903 dev_dbg(&sep->pdev->dev, "sep_rar_prepare_output_msg_handler start\n");
2906 if (copy_from_user(&command_args, (void __user *)arg,
2907 sizeof(command_args))) {
2912 /* Call to translation function only if user handle is not NULL */
2913 if (command_args.rar_handle) {
2914 memset(&rar_buf, 0, sizeof(rar_buf));
2915 rar_buf.info.handle = (u32)command_args.rar_handle;
2917 if (rar_handle_to_bus(&rar_buf, 1) != 1) {
2918 dev_dbg(&sep->pdev->dev, "rar_handle_to_bus failure\n");
2922 rar_bus = rar_buf.bus_address;
2924 dev_dbg(&sep->pdev->dev, "rar msg; rar_addr_bus = %x\n", (u32)rar_bus);
2926 /* Set value in the SYSTEM MEMORY offset */
2927 rar_addr = (u32 *)(sep->shared_addr +
2928 SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);
2930 /* Copy the physical address to the System Area for the SEP */
2931 rar_addr[0] = SEP_RAR_VAL_TOKEN;
2932 rar_addr[1] = rar_bus;
2935 dev_dbg(&sep->pdev->dev, "sep_rar_prepare_output_msg_handler start\n");
2940 * sep_realloc_ext_cache_handler - report location of extcache
2941 * @sep: pointer to struct sep_device
2942 * @arg: pointer to user parameters
2944 * This function tells the SEP where the extapp is located
2946 static int sep_realloc_ext_cache_handler(struct sep_device *sep,
2949 /* Holds the new ext cache address in the system memory offset */
2952 /* Set value in the SYSTEM MEMORY offset */
2953 system_addr = (u32 *)(sep->shared_addr +
2954 SEP_DRIVER_SYSTEM_EXT_CACHE_ADDR_OFFSET_IN_BYTES);
2956 /* Copy the physical address to the System Area for the SEP */
2957 system_addr[0] = SEP_EXT_CACHE_ADDR_VAL_TOKEN;
2958 dev_dbg(&sep->pdev->dev, "ext cache init; system addr 0 is %x\n",
2960 system_addr[1] = sep->extapp_bus;
2961 dev_dbg(&sep->pdev->dev, "ext cache init; system addr 1 is %x\n",
2968 * sep_ioctl - ioctl api
2969 * @filp: pointer to struct file
2971 * @arg: pointer to argument structure
2973 * Implement the ioctl methods availble on the SEP device.
2975 static long sep_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2978 struct sep_device *sep = filp->private_data;
2980 dev_dbg(&sep->pdev->dev, "ioctl start\n");
2982 dev_dbg(&sep->pdev->dev, "cmd is %x\n", cmd);
2984 /* Make sure we own this device */
2985 mutex_lock(&sep->sep_mutex);
2986 if ((current->pid != sep->pid_doing_transaction) &&
2987 (sep->pid_doing_transaction != 0)) {
2988 dev_dbg(&sep->pdev->dev, "ioctl pid is not owner\n");
2989 mutex_unlock(&sep->sep_mutex);
2994 mutex_unlock(&sep->sep_mutex);
2996 /* Check that the command is for SEP device */
2997 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
3002 /* Lock to prevent the daemon to interfere with operation */
3003 mutex_lock(&sep->ioctl_mutex);
3006 case SEP_IOCSENDSEPCOMMAND:
3007 /* Send command to SEP */
3008 error = sep_send_command_handler(sep);
3010 case SEP_IOCALLOCDATAPOLL:
3011 /* Allocate data pool */
3012 error = sep_allocate_data_pool_memory_handler(sep, arg);
3014 case SEP_IOCCREATESYMDMATABLE:
3015 /* Create DMA table for synhronic operation */
3016 error = sep_create_sync_dma_tables_handler(sep, arg);
3018 case SEP_IOCFREEDMATABLEDATA:
3019 /* Free the pages */
3020 error = sep_free_dma_table_data_handler(sep);
3022 case SEP_IOCSEPSTART:
3023 /* Start command to SEP */
3024 if (sep->pdev->revision == 0) /* Only for old chip */
3025 error = sep_start_handler(sep);
3027 error = -EPERM; /* Not permitted on new chip */
3029 case SEP_IOCSEPINIT:
3030 /* Init command to SEP */
3031 if (sep->pdev->revision == 0) /* Only for old chip */
3032 error = sep_init_handler(sep, arg);
3034 error = -EPERM; /* Not permitted on new chip */
3036 case SEP_IOCGETSTATICPOOLADDR:
3037 /* Inform the SEP the bus address of the static pool */
3038 error = sep_get_static_pool_addr_handler(sep);
3040 case SEP_IOCENDTRANSACTION:
3041 error = sep_end_transaction_handler(sep);
3043 case SEP_IOCREALLOCEXTCACHE:
3044 if (sep->pdev->revision == 0) /* Only for old chip */
3045 error = sep_realloc_ext_cache_handler(sep, arg);
3047 error = -EPERM; /* Not permitted on new chip */
3049 case SEP_IOCRARPREPAREMESSAGE:
3050 error = sep_rar_prepare_output_msg_handler(sep, arg);
3052 case SEP_IOCPREPAREDCB:
3053 error = sep_prepare_dcb_handler(sep, arg);
3055 case SEP_IOCFREEDCB:
3056 error = sep_free_dcb_handler(sep);
3059 dev_dbg(&sep->pdev->dev, "invalid ioctl %x\n", cmd);
3063 mutex_unlock(&sep->ioctl_mutex);
3066 dev_dbg(&sep->pdev->dev, "ioctl end\n");
3071 * sep_singleton_ioctl - ioctl api for singleton interface
3072 * @filp: pointer to struct file
3074 * @arg: pointer to argument structure
3076 * Implement the additional ioctls for the singleton device
3078 static long sep_singleton_ioctl(struct file *filp, u32 cmd, unsigned long arg)
3081 struct sep_device *sep = filp->private_data;
3083 dev_dbg(&sep->pdev->dev, "singleton_ioctl start\n");
3084 dev_dbg(&sep->pdev->dev, "cmd is %x\n", cmd);
3086 /* Check that the command is for the SEP device */
3087 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
3092 /* Make sure we own this device */
3093 mutex_lock(&sep->sep_mutex);
3094 if ((current->pid != sep->pid_doing_transaction) &&
3095 (sep->pid_doing_transaction != 0)) {
3096 dev_dbg(&sep->pdev->dev, "singleton ioctl pid is not owner\n");
3097 mutex_unlock(&sep->sep_mutex);
3102 mutex_unlock(&sep->sep_mutex);
3105 case SEP_IOCTLSETCALLERID:
3106 mutex_lock(&sep->ioctl_mutex);
3107 error = sep_set_caller_id_handler(sep, arg);
3108 mutex_unlock(&sep->ioctl_mutex);
3111 error = sep_ioctl(filp, cmd, arg);
3116 dev_dbg(&sep->pdev->dev, "singleton ioctl end\n");
3121 * sep_request_daemon_ioctl - ioctl for daemon
3122 * @filp: pointer to struct file
3124 * @arg: pointer to argument structure
3126 * Called by the request daemon to perform ioctls on the daemon device
3128 static long sep_request_daemon_ioctl(struct file *filp, u32 cmd,
3133 struct sep_device *sep = filp->private_data;
3135 dev_dbg(&sep->pdev->dev, "daemon ioctl: start\n");
3136 dev_dbg(&sep->pdev->dev, "daemon ioctl: cmd is %x\n", cmd);
3138 /* Check that the command is for SEP device */
3139 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
3144 /* Only one process can access ioctl at any given time */
3145 mutex_lock(&sep->ioctl_mutex);
3148 case SEP_IOCSENDSEPRPLYCOMMAND:
3149 /* Send reply command to SEP */
3150 error = sep_req_daemon_send_reply_command_handler(sep);
3152 case SEP_IOCENDTRANSACTION:
3154 * End req daemon transaction, do nothing
3155 * will be removed upon update in middleware
3161 dev_dbg(&sep->pdev->dev, "daemon ioctl: no such IOCTL\n");
3164 mutex_unlock(&sep->ioctl_mutex);
3167 dev_dbg(&sep->pdev->dev, "daemon ioctl: end\n");
3173 * sep_inthandler - interrupt handler
3175 * @dev_id: device id
3177 static irqreturn_t sep_inthandler(int irq, void *dev_id)
3179 irqreturn_t int_error = IRQ_HANDLED;
3180 unsigned long lck_flags;
3181 u32 reg_val, reg_val2 = 0;
3182 struct sep_device *sep = dev_id;
3184 /* Read the IRR register to check if this is SEP interrupt */
3185 reg_val = sep_read_reg(sep, HW_HOST_IRR_REG_ADDR);
3186 dev_dbg(&sep->pdev->dev, "SEP Interrupt - reg is %08x\n", reg_val);
3188 if (reg_val & (0x1 << 13)) {
3189 /* Lock and update the counter of reply messages */
3190 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
3192 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
3194 dev_dbg(&sep->pdev->dev, "sep int: send_ct %lx reply_ct %lx\n",
3195 sep->send_ct, sep->reply_ct);
3197 /* Is this printf or daemon request? */
3198 reg_val2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
3199 dev_dbg(&sep->pdev->dev,
3200 "SEP Interrupt - reg2 is %08x\n", reg_val2);
3202 if ((reg_val2 >> 30) & 0x1) {
3203 dev_dbg(&sep->pdev->dev, "int: printf request\n");
3204 wake_up(&sep->event_request_daemon);
3205 } else if (reg_val2 >> 31) {
3206 dev_dbg(&sep->pdev->dev, "int: daemon request\n");
3207 wake_up(&sep->event_request_daemon);
3209 dev_dbg(&sep->pdev->dev, "int: SEP reply\n");
3210 wake_up(&sep->event);
3213 dev_dbg(&sep->pdev->dev, "int: not SEP interrupt\n");
3214 int_error = IRQ_NONE;
3216 if (int_error == IRQ_HANDLED)
3217 sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, reg_val);
3223 * sep_reconfig_shared_area - reconfigure shared area
3224 * @sep: pointer to struct sep_device
3226 * Reconfig the shared area between HOST and SEP - needed in case
3227 * the DX_CC_Init function was called before OS loading.
3229 static int sep_reconfig_shared_area(struct sep_device *sep)
3233 /* use to limit waiting for SEP */
3234 unsigned long end_time;
3236 dev_dbg(&sep->pdev->dev, "reconfig shared area start\n");
3238 /* Send the new SHARED MESSAGE AREA to the SEP */
3239 dev_dbg(&sep->pdev->dev, "sending %08llx to sep\n",
3240 (unsigned long long)sep->shared_bus);
3242 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR1_REG_ADDR, sep->shared_bus);
3244 /* Poll for SEP response */
3245 ret_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
3247 end_time = jiffies + (WAIT_TIME * HZ);
3249 while ((time_before(jiffies, end_time)) && (ret_val != 0xffffffff) &&
3250 (ret_val != sep->shared_bus))
3251 ret_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
3253 /* Check the return value (register) */
3254 if (ret_val != sep->shared_bus) {
3255 dev_warn(&sep->pdev->dev, "could not reconfig shared area\n");
3256 dev_warn(&sep->pdev->dev, "result was %x\n", ret_val);
3261 dev_dbg(&sep->pdev->dev, "reconfig shared area end\n");
3265 /* File operation for singleton SEP operations */
3266 static const struct file_operations singleton_file_operations = {
3267 .owner = THIS_MODULE,
3268 .unlocked_ioctl = sep_singleton_ioctl,
3270 .open = sep_singleton_open,
3271 .release = sep_singleton_release,
3275 /* File operation for daemon operations */
3276 static const struct file_operations daemon_file_operations = {
3277 .owner = THIS_MODULE,
3278 .unlocked_ioctl = sep_request_daemon_ioctl,
3279 .poll = sep_request_daemon_poll,
3280 .open = sep_request_daemon_open,
3281 .release = sep_request_daemon_release,
3282 .mmap = sep_request_daemon_mmap,
3285 /* The files operations structure of the driver */
3286 static const struct file_operations sep_file_operations = {
3287 .owner = THIS_MODULE,
3288 .unlocked_ioctl = sep_ioctl,
3291 .release = sep_release,
3296 * sep_register_driver_with_fs - register misc devices
3297 * @sep: pointer to struct sep_device
3299 * This function registers the driver with the file system
3301 static int sep_register_driver_with_fs(struct sep_device *sep)
3305 sep->miscdev_sep.minor = MISC_DYNAMIC_MINOR;
3306 sep->miscdev_sep.name = SEP_DEV_NAME;
3307 sep->miscdev_sep.fops = &sep_file_operations;
3309 sep->miscdev_singleton.minor = MISC_DYNAMIC_MINOR;
3310 sep->miscdev_singleton.name = SEP_DEV_SINGLETON;
3311 sep->miscdev_singleton.fops = &singleton_file_operations;
3313 sep->miscdev_daemon.minor = MISC_DYNAMIC_MINOR;
3314 sep->miscdev_daemon.name = SEP_DEV_DAEMON;
3315 sep->miscdev_daemon.fops = &daemon_file_operations;
3317 ret_val = misc_register(&sep->miscdev_sep);
3319 dev_warn(&sep->pdev->dev, "misc reg fails for SEP %x\n",
3324 ret_val = misc_register(&sep->miscdev_singleton);
3326 dev_warn(&sep->pdev->dev, "misc reg fails for sing %x\n",
3328 misc_deregister(&sep->miscdev_sep);
3332 ret_val = misc_register(&sep->miscdev_daemon);
3334 dev_warn(&sep->pdev->dev, "misc reg fails for dmn %x\n",
3336 misc_deregister(&sep->miscdev_sep);
3337 misc_deregister(&sep->miscdev_singleton);
3346 * sep_probe - probe a matching PCI device
3348 * @end: pci_device_id
3350 * Attempt to set up and configure a SEP device that has been
3351 * discovered by the PCI layer.
3353 static int __devinit sep_probe(struct pci_dev *pdev,
3354 const struct pci_device_id *ent)
3357 struct sep_device *sep;
3359 pr_debug("SEP pci probe starting\n");
3360 if (sep_dev != NULL) {
3361 dev_warn(&pdev->dev, "only one SEP supported.\n");
3365 /* Enable the device */
3366 error = pci_enable_device(pdev);
3368 dev_warn(&pdev->dev, "error enabling pci device\n");
3372 /* Allocate the sep_device structure for this device */
3373 sep_dev = kzalloc(sizeof(struct sep_device), GFP_ATOMIC);
3374 if (sep_dev == NULL) {
3375 dev_warn(&pdev->dev,
3376 "can't kmalloc the sep_device structure\n");
3378 goto end_function_disable_device;
3382 * We're going to use another variable for actually
3383 * working with the device; this way, if we have
3384 * multiple devices in the future, it would be easier
3385 * to make appropriate changes
3389 sep->pdev = pci_dev_get(pdev);
3391 init_waitqueue_head(&sep->event);
3392 init_waitqueue_head(&sep->event_request_daemon);
3393 spin_lock_init(&sep->snd_rply_lck);
3394 mutex_init(&sep->sep_mutex);
3395 mutex_init(&sep->ioctl_mutex);
3397 dev_dbg(&sep->pdev->dev, "PCI obtained, device being prepared\n");
3398 dev_dbg(&sep->pdev->dev, "revision is %d\n", sep->pdev->revision);
3400 /* Set up our register area */
3401 sep->reg_physical_addr = pci_resource_start(sep->pdev, 0);
3402 if (!sep->reg_physical_addr) {
3403 dev_warn(&sep->pdev->dev, "Error getting register start\n");
3405 goto end_function_free_sep_dev;
3408 sep->reg_physical_end = pci_resource_end(sep->pdev, 0);
3409 if (!sep->reg_physical_end) {
3410 dev_warn(&sep->pdev->dev, "Error getting register end\n");
3412 goto end_function_free_sep_dev;
3415 sep->reg_addr = ioremap_nocache(sep->reg_physical_addr,
3416 (size_t)(sep->reg_physical_end - sep->reg_physical_addr + 1));
3417 if (!sep->reg_addr) {
3418 dev_warn(&sep->pdev->dev, "Error getting register virtual\n");
3420 goto end_function_free_sep_dev;
3423 dev_dbg(&sep->pdev->dev,
3424 "Register area start %llx end %llx virtual %p\n",
3425 (unsigned long long)sep->reg_physical_addr,
3426 (unsigned long long)sep->reg_physical_end,
3429 /* Allocate the shared area */
3430 sep->shared_size = SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES +
3431 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES +
3432 SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES +
3433 SEP_DRIVER_STATIC_AREA_SIZE_IN_BYTES +
3434 SEP_DRIVER_SYSTEM_DATA_MEMORY_SIZE_IN_BYTES;
3436 if (sep_map_and_alloc_shared_area(sep)) {
3438 /* Allocation failed */
3439 goto end_function_error;
3442 sep->rar_size = FAKE_RAR_SIZE;
3443 sep->rar_addr = dma_alloc_coherent(&sep->pdev->dev,
3444 sep->rar_size, &sep->rar_bus, GFP_KERNEL);
3445 if (sep->rar_addr == NULL) {
3446 dev_warn(&sep->pdev->dev, "can't allocate mfld rar\n");
3448 goto end_function_deallocate_sep_shared_area;
3451 dev_dbg(&sep->pdev->dev, "rar start is %p, phy is %llx,"
3452 " size is %zx\n", sep->rar_addr,
3453 (unsigned long long)sep->rar_bus,
3456 dev_dbg(&sep->pdev->dev, "about to write IMR and ICR REG_ADDR\n");
3458 /* Clear ICR register */
3459 sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, 0xFFFFFFFF);
3461 /* Set the IMR register - open only GPR 2 */
3462 sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));
3464 /* Read send/receive counters from SEP */
3465 sep->reply_ct = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
3466 sep->reply_ct &= 0x3FFFFFFF;
3467 sep->send_ct = sep->reply_ct;
3469 dev_dbg(&sep->pdev->dev, "about to call request_irq\n");
3470 /* Get the interrupt line */
3471 error = request_irq(pdev->irq, sep_inthandler, IRQF_SHARED,
3475 goto end_function_dealloc_rar;
3477 /* The new chip requires ashared area reconfigure */
3478 if (sep->pdev->revision == 4) { /* Only for new chip */
3479 error = sep_reconfig_shared_area(sep);
3481 goto end_function_free_irq;
3483 /* Finally magic up the device nodes */
3484 /* Register driver with the fs */
3485 error = sep_register_driver_with_fs(sep);
3490 end_function_free_irq:
3491 free_irq(pdev->irq, sep);
3493 end_function_dealloc_rar:
3495 dma_free_coherent(&sep->pdev->dev, sep->rar_size,
3496 sep->rar_addr, sep->rar_bus);
3499 end_function_deallocate_sep_shared_area:
3500 /* De-allocate shared area */
3501 sep_unmap_and_free_shared_area(sep);
3504 iounmap(sep->reg_addr);
3506 end_function_free_sep_dev:
3507 pci_dev_put(sep_dev->pdev);
3511 end_function_disable_device:
3512 pci_disable_device(pdev);
3518 static void sep_remove(struct pci_dev *pdev)
3520 struct sep_device *sep = sep_dev;
3522 /* Unregister from fs */
3523 misc_deregister(&sep->miscdev_sep);
3524 misc_deregister(&sep->miscdev_singleton);
3525 misc_deregister(&sep->miscdev_daemon);
3528 free_irq(sep->pdev->irq, sep);
3530 /* Free the shared area */
3531 sep_unmap_and_free_shared_area(sep_dev);
3532 iounmap((void *) sep_dev->reg_addr);
3535 static DEFINE_PCI_DEVICE_TABLE(sep_pci_id_tbl) = {
3536 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MFLD_PCI_DEVICE_ID)},
3540 MODULE_DEVICE_TABLE(pci, sep_pci_id_tbl);
3542 /* Field for registering driver to PCI device */
3543 static struct pci_driver sep_pci_driver = {
3544 .name = "sep_sec_driver",
3545 .id_table = sep_pci_id_tbl,
3547 .remove = sep_remove
3552 * sep_init - init function
3554 * Module load time. Register the PCI device driver.
3556 static int __init sep_init(void)
3558 return pci_register_driver(&sep_pci_driver);
3563 * sep_exit - called to unload driver
3565 * Drop the misc devices then remove and unmap the various resources
3566 * that are not released by the driver remove method.
3568 static void __exit sep_exit(void)
3570 pci_unregister_driver(&sep_pci_driver);
3574 module_init(sep_init);
3575 module_exit(sep_exit);
3577 MODULE_LICENSE("GPL");