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
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/miscdevice.h>
36 #include <linux/cdev.h>
37 #include <linux/kdev_t.h>
38 #include <linux/mutex.h>
39 #include <linux/sched.h>
41 #include <linux/poll.h>
42 #include <linux/wait.h>
43 #include <linux/pci.h>
44 #include <linux/firmware.h>
45 #include <linux/slab.h>
46 #include <linux/ioctl.h>
47 #include <asm/current.h>
48 #include <linux/ioport.h>
50 #include <linux/interrupt.h>
51 #include <linux/pagemap.h>
52 #include <asm/cacheflush.h>
53 #include <linux/sched.h>
54 #include <linux/delay.h>
55 #include <linux/jiffies.h>
56 #include <linux/rar_register.h>
58 #include "../memrar/memrar.h"
60 #include "sep_driver_hw_defs.h"
61 #include "sep_driver_config.h"
62 #include "sep_driver_api.h"
65 /*----------------------------------------
67 -----------------------------------------*/
69 #define SEP_RAR_IO_MEM_REGION_SIZE 0x40000
71 /*--------------------------------------------
73 --------------------------------------------*/
75 /* Keep this a single static object for now to keep the conversion easy */
77 static struct sep_device *sep_dev;
80 * sep_load_firmware - copy firmware cache/resident
81 * @sep: pointer to struct sep_device we are loading
83 * This functions copies the cache and resident from their source
84 * location into destination shared memory.
86 static int sep_load_firmware(struct sep_device *sep)
88 const struct firmware *fw;
89 char *cache_name = "cache.image.bin";
90 char *res_name = "resident.image.bin";
91 char *extapp_name = "extapp.image.bin";
93 unsigned long work1, work2, work3;
95 /* Set addresses and load resident */
96 sep->resident_bus = sep->rar_bus;
97 sep->resident_addr = sep->rar_addr;
99 error = request_firmware(&fw, res_name, &sep->pdev->dev);
101 dev_warn(&sep->pdev->dev, "can't request resident fw\n");
105 memcpy(sep->resident_addr, (void *)fw->data, fw->size);
106 sep->resident_size = fw->size;
107 release_firmware(fw);
109 dev_dbg(&sep->pdev->dev, "resident bus is %lx\n",
110 (unsigned long)sep->resident_bus);
112 /* Set addresses for dcache (no loading needed) */
113 work1 = (unsigned long)sep->resident_bus;
114 work2 = (unsigned long)sep->resident_size;
115 work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
116 sep->dcache_bus = (dma_addr_t)work3;
118 work1 = (unsigned long)sep->resident_addr;
119 work2 = (unsigned long)sep->resident_size;
120 work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
121 sep->dcache_addr = (void *)work3;
123 sep->dcache_size = 1024 * 128;
125 /* Set addresses and load cache */
126 sep->cache_bus = sep->dcache_bus + sep->dcache_size;
127 sep->cache_addr = sep->dcache_addr + sep->dcache_size;
129 error = request_firmware(&fw, cache_name, &sep->pdev->dev);
131 dev_warn(&sep->pdev->dev, "Unable to request cache firmware\n");
135 memcpy(sep->cache_addr, (void *)fw->data, fw->size);
136 sep->cache_size = fw->size;
137 release_firmware(fw);
139 dev_dbg(&sep->pdev->dev, "cache bus is %08lx\n",
140 (unsigned long)sep->cache_bus);
142 /* Set addresses and load extapp */
143 sep->extapp_bus = sep->cache_bus + (1024 * 370);
144 sep->extapp_addr = sep->cache_addr + (1024 * 370);
146 error = request_firmware(&fw, extapp_name, &sep->pdev->dev);
148 dev_warn(&sep->pdev->dev, "Unable to request extapp firmware\n");
152 memcpy(sep->extapp_addr, (void *)fw->data, fw->size);
153 sep->extapp_size = fw->size;
154 release_firmware(fw);
156 dev_dbg(&sep->pdev->dev, "extapp bus is %08llx\n",
157 (unsigned long long)sep->extapp_bus);
162 MODULE_FIRMWARE("sep/cache.image.bin");
163 MODULE_FIRMWARE("sep/resident.image.bin");
164 MODULE_FIRMWARE("sep/extapp.image.bin");
167 * sep_dump_message - dump the message that is pending
170 static void sep_dump_message(struct sep_device *sep)
173 u32 *p = sep->shared_addr;
174 for (count = 0; count < 12 * 4; count += 4)
175 dev_dbg(&sep->pdev->dev, "Word %d of the message is %x\n",
180 * sep_map_and_alloc_shared_area - allocate shared block
181 * @sep: security processor
182 * @size: size of shared area
184 static int sep_map_and_alloc_shared_area(struct sep_device *sep)
186 sep->shared_addr = dma_alloc_coherent(&sep->pdev->dev,
188 &sep->shared_bus, GFP_KERNEL);
190 if (!sep->shared_addr) {
191 dev_warn(&sep->pdev->dev,
192 "shared memory dma_alloc_coherent failed\n");
195 dev_dbg(&sep->pdev->dev,
196 "shared_addr %zx bytes @%p (bus %llx)\n",
197 sep->shared_size, sep->shared_addr,
198 (unsigned long long)sep->shared_bus);
203 * sep_unmap_and_free_shared_area - free shared block
204 * @sep: security processor
206 static void sep_unmap_and_free_shared_area(struct sep_device *sep)
208 dma_free_coherent(&sep->pdev->dev, sep->shared_size,
209 sep->shared_addr, sep->shared_bus);
213 * sep_shared_bus_to_virt - convert bus/virt addresses
214 * @sep: pointer to struct sep_device
215 * @bus_address: address to convert
217 * Returns virtual address inside the shared area according
218 * to the bus address.
220 static void *sep_shared_bus_to_virt(struct sep_device *sep,
221 dma_addr_t bus_address)
223 return sep->shared_addr + (bus_address - sep->shared_bus);
227 * open function for the singleton driver
228 * @inode_ptr struct inode *
229 * @file_ptr struct file *
231 * Called when the user opens the singleton device interface
233 static int sep_singleton_open(struct inode *inode_ptr, struct file *file_ptr)
236 struct sep_device *sep;
239 * Get the SEP device structure and use it for the
240 * private_data field in filp for other methods
244 file_ptr->private_data = sep;
246 if (test_and_set_bit(0, &sep->singleton_access_flag)) {
256 * sep_open - device open method
257 * @inode: inode of SEP device
258 * @filp: file handle to SEP device
260 * Open method for the SEP device. Called when userspace opens
261 * the SEP device node.
263 * Returns zero on success otherwise an error code.
265 static int sep_open(struct inode *inode, struct file *filp)
267 struct sep_device *sep;
270 * Get the SEP device structure and use it for the
271 * private_data field in filp for other methods
274 filp->private_data = sep;
276 dev_dbg(&sep->pdev->dev, "Open for pid %d\n", current->pid);
278 /* Anyone can open; locking takes place at transaction level */
283 * sep_singleton_release - close a SEP singleton device
284 * @inode: inode of SEP device
285 * @filp: file handle being closed
287 * Called on the final close of a SEP device. As the open protects against
288 * multiple simultaenous opens that means this method is called when the
289 * final reference to the open handle is dropped.
291 static int sep_singleton_release(struct inode *inode, struct file *filp)
293 struct sep_device *sep = filp->private_data;
295 clear_bit(0, &sep->singleton_access_flag);
300 * sep_request_daemonopen - request daemon open method
301 * @inode: inode of SEP device
302 * @filp: file handle to SEP device
304 * Open method for the SEP request daemon. Called when
305 * request daemon in userspace opens the SEP device node.
307 * Returns zero on success otherwise an error code.
309 static int sep_request_daemon_open(struct inode *inode, struct file *filp)
311 struct sep_device *sep = sep_dev;
314 filp->private_data = sep;
316 dev_dbg(&sep->pdev->dev, "Request daemon open for pid %d\n",
319 /* There is supposed to be only one request daemon */
320 if (test_and_set_bit(0, &sep->request_daemon_open))
326 * sep_request_daemon_release - close a SEP daemon
327 * @inode: inode of SEP device
328 * @filp: file handle being closed
330 * Called on the final close of a SEP daemon.
332 static int sep_request_daemon_release(struct inode *inode, struct file *filp)
334 struct sep_device *sep = filp->private_data;
336 dev_dbg(&sep->pdev->dev, "Request daemon release for pid %d\n",
339 /* Clear the request_daemon_open flag */
340 clear_bit(0, &sep->request_daemon_open);
345 * sep_req_daemon_send_reply_command_handler - poke the SEP
346 * @sep: struct sep_device *
348 * This function raises interrupt to SEPm that signals that is has a
349 * new command from HOST
351 static int sep_req_daemon_send_reply_command_handler(struct sep_device *sep)
353 unsigned long lck_flags;
355 sep_dump_message(sep);
357 /* Counters are lockable region */
358 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
362 /* Send the interrupt to SEP */
363 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR2_REG_ADDR, sep->send_ct);
366 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
368 dev_dbg(&sep->pdev->dev,
369 "sep_req_daemon_send_reply send_ct %lx reply_ct %lx\n",
370 sep->send_ct, sep->reply_ct);
377 * sep_free_dma_table_data_handler - free DMA table
378 * @sep: pointere to struct sep_device
380 * Handles the request to free DMA table for synchronic actions
382 static int sep_free_dma_table_data_handler(struct sep_device *sep)
386 /* Pointer to the current dma_resource struct */
387 struct sep_dma_resource *dma;
389 for (dcb_counter = 0; dcb_counter < sep->nr_dcb_creat; dcb_counter++) {
390 dma = &sep->dma_res_arr[dcb_counter];
392 /* Unmap and free input map array */
393 if (dma->in_map_array) {
394 for (count = 0; count < dma->in_num_pages; count++) {
395 dma_unmap_page(&sep->pdev->dev,
396 dma->in_map_array[count].dma_addr,
397 dma->in_map_array[count].size,
400 kfree(dma->in_map_array);
403 /* Unmap output map array, DON'T free it yet */
404 if (dma->out_map_array) {
405 for (count = 0; count < dma->out_num_pages; count++) {
406 dma_unmap_page(&sep->pdev->dev,
407 dma->out_map_array[count].dma_addr,
408 dma->out_map_array[count].size,
411 kfree(dma->out_map_array);
414 /* Free page cache for output */
415 if (dma->in_page_array) {
416 for (count = 0; count < dma->in_num_pages; count++) {
417 flush_dcache_page(dma->in_page_array[count]);
418 page_cache_release(dma->in_page_array[count]);
420 kfree(dma->in_page_array);
423 if (dma->out_page_array) {
424 for (count = 0; count < dma->out_num_pages; count++) {
425 if (!PageReserved(dma->out_page_array[count]))
426 SetPageDirty(dma->out_page_array[count]);
427 flush_dcache_page(dma->out_page_array[count]);
428 page_cache_release(dma->out_page_array[count]);
430 kfree(dma->out_page_array);
433 /* Reset all the values */
434 dma->in_page_array = NULL;
435 dma->out_page_array = NULL;
436 dma->in_num_pages = 0;
437 dma->out_num_pages = 0;
438 dma->in_map_array = NULL;
439 dma->out_map_array = NULL;
440 dma->in_map_num_entries = 0;
441 dma->out_map_num_entries = 0;
444 sep->nr_dcb_creat = 0;
445 sep->num_lli_tables_created = 0;
451 * sep_request_daemon_mmap - maps the shared area to user space
452 * @filp: pointer to struct file
453 * @vma: pointer to vm_area_struct
455 * Called by the kernel when the daemon attempts an mmap() syscall
458 static int sep_request_daemon_mmap(struct file *filp,
459 struct vm_area_struct *vma)
461 struct sep_device *sep = filp->private_data;
462 dma_addr_t bus_address;
465 if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
470 /* Get physical address */
471 bus_address = sep->shared_bus;
473 if (remap_pfn_range(vma, vma->vm_start, bus_address >> PAGE_SHIFT,
474 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
476 dev_warn(&sep->pdev->dev, "remap_page_range failed\n");
486 * sep_request_daemon_poll - poll implementation
487 * @sep: struct sep_device * for current SEP device
488 * @filp: struct file * for open file
489 * @wait: poll_table * for poll
491 * Called when our device is part of a poll() or select() syscall
493 static unsigned int sep_request_daemon_poll(struct file *filp,
499 unsigned long lck_flags;
500 struct sep_device *sep = filp->private_data;
502 poll_wait(filp, &sep->event_request_daemon, wait);
504 dev_dbg(&sep->pdev->dev, "daemon poll: send_ct is %lx reply ct is %lx\n",
505 sep->send_ct, sep->reply_ct);
507 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
508 /* Check if the data is ready */
509 if (sep->send_ct == sep->reply_ct) {
510 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
512 retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
513 dev_dbg(&sep->pdev->dev,
514 "daemon poll: data check (GPR2) is %x\n", retval2);
516 /* Check if PRINT request */
517 if ((retval2 >> 30) & 0x1) {
518 dev_dbg(&sep->pdev->dev, "daemon poll: PRINTF request in\n");
522 /* Check if NVS request */
524 dev_dbg(&sep->pdev->dev, "daemon poll: NVS request in\n");
525 mask |= POLLPRI | POLLWRNORM;
528 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
529 dev_dbg(&sep->pdev->dev,
530 "daemon poll: no reply received; returning 0\n");
538 * sep_release - close a SEP device
539 * @inode: inode of SEP device
540 * @filp: file handle being closed
542 * Called on the final close of a SEP device.
544 static int sep_release(struct inode *inode, struct file *filp)
546 struct sep_device *sep = filp->private_data;
548 dev_dbg(&sep->pdev->dev, "Release for pid %d\n", current->pid);
550 mutex_lock(&sep->sep_mutex);
551 /* Is this the process that has a transaction open?
552 * If so, lets reset pid_doing_transaction to 0 and
553 * clear the in use flags, and then wake up sep_event
554 * so that other processes can do transactions
556 if (sep->pid_doing_transaction == current->pid) {
557 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
558 clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
559 sep_free_dma_table_data_handler(sep);
560 wake_up(&sep->event);
561 sep->pid_doing_transaction = 0;
564 mutex_unlock(&sep->sep_mutex);
569 * sep_mmap - maps the shared area to user space
570 * @filp: pointer to struct file
571 * @vma: pointer to vm_area_struct
573 * Called on an mmap of our space via the normal SEP device
575 static int sep_mmap(struct file *filp, struct vm_area_struct *vma)
578 struct sep_device *sep = filp->private_data;
579 unsigned long error = 0;
581 /* Set the transaction busy (own the device) */
582 wait_event_interruptible(sep->event,
583 test_and_set_bit(SEP_MMAP_LOCK_BIT,
584 &sep->in_use_flags) == 0);
586 if (signal_pending(current)) {
588 goto end_function_with_error;
591 * The pid_doing_transaction indicates that this process
592 * now owns the facilities to performa a transaction with
593 * the SEP. While this process is performing a transaction,
594 * no other process who has the SEP device open can perform
595 * any transactions. This method allows more than one process
596 * to have the device open at any given time, which provides
597 * finer granularity for device utilization by multiple
600 mutex_lock(&sep->sep_mutex);
601 sep->pid_doing_transaction = current->pid;
602 mutex_unlock(&sep->sep_mutex);
604 /* Zero the pools and the number of data pool alocation pointers */
605 sep->data_pool_bytes_allocated = 0;
606 sep->num_of_data_allocations = 0;
609 * Check that the size of the mapped range is as the size of the message
612 if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
614 goto end_function_with_error;
617 dev_dbg(&sep->pdev->dev, "shared_addr is %p\n", sep->shared_addr);
619 /* Get bus address */
620 bus_addr = sep->shared_bus;
622 if (remap_pfn_range(vma, vma->vm_start, bus_addr >> PAGE_SHIFT,
623 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
624 dev_warn(&sep->pdev->dev, "remap_page_range failed\n");
626 goto end_function_with_error;
630 end_function_with_error:
632 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
633 mutex_lock(&sep->sep_mutex);
634 sep->pid_doing_transaction = 0;
635 mutex_unlock(&sep->sep_mutex);
637 /* Raise event for stuck contextes */
639 wake_up(&sep->event);
646 * sep_poll - poll handler
647 * @filp: pointer to struct file
648 * @wait: pointer to poll_table
650 * Called by the OS when the kernel is asked to do a poll on
653 static unsigned int sep_poll(struct file *filp, poll_table *wait)
658 unsigned long lck_flags;
660 struct sep_device *sep = filp->private_data;
662 /* Am I the process that owns the transaction? */
663 mutex_lock(&sep->sep_mutex);
664 if (current->pid != sep->pid_doing_transaction) {
665 dev_warn(&sep->pdev->dev, "poll; wrong pid\n");
667 mutex_unlock(&sep->sep_mutex);
670 mutex_unlock(&sep->sep_mutex);
672 /* Check if send command or send_reply were activated previously */
673 if (!test_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
678 /* Add the event to the polling wait table */
679 dev_dbg(&sep->pdev->dev, "poll: calling wait sep_event\n");
681 poll_wait(filp, &sep->event, wait);
683 dev_dbg(&sep->pdev->dev, "poll: send_ct is %lx reply ct is %lx\n",
684 sep->send_ct, sep->reply_ct);
686 /* Check if error occured during poll */
687 retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
688 if (retval2 != 0x0) {
689 dev_warn(&sep->pdev->dev, "poll; poll error %x\n", retval2);
694 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
696 if (sep->send_ct == sep->reply_ct) {
697 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
698 retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
699 dev_dbg(&sep->pdev->dev, "poll: data ready check (GPR2) %x\n",
702 /* Check if printf request */
703 if ((retval >> 30) & 0x1) {
704 dev_dbg(&sep->pdev->dev, "poll: SEP printf request\n");
705 wake_up(&sep->event_request_daemon);
709 /* Check if the this is SEP reply or request */
711 dev_dbg(&sep->pdev->dev, "poll: SEP request\n");
712 wake_up(&sep->event_request_daemon);
714 dev_dbg(&sep->pdev->dev, "poll: normal return\n");
715 /* In case it is again by send_reply_comand */
716 clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
717 sep_dump_message(sep);
718 dev_dbg(&sep->pdev->dev,
719 "poll; SEP reply POLLIN | POLLRDNORM\n");
720 mask |= POLLIN | POLLRDNORM;
723 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
724 dev_dbg(&sep->pdev->dev,
725 "poll; no reply received; returning mask of 0\n");
734 * sep_time_address - address in SEP memory of time
735 * @sep: SEP device we want the address from
737 * Return the address of the two dwords in memory used for time
740 static u32 *sep_time_address(struct sep_device *sep)
742 return sep->shared_addr + SEP_DRIVER_SYSTEM_TIME_MEMORY_OFFSET_IN_BYTES;
746 * sep_set_time - set the SEP time
747 * @sep: the SEP we are setting the time for
749 * Calculates time and sets it at the predefined address.
750 * Called with the SEP mutex held.
752 static unsigned long sep_set_time(struct sep_device *sep)
755 u32 *time_addr; /* Address of time as seen by the kernel */
758 do_gettimeofday(&time);
760 /* Set value in the SYSTEM MEMORY offset */
761 time_addr = sep_time_address(sep);
763 time_addr[0] = SEP_TIME_VAL_TOKEN;
764 time_addr[1] = time.tv_sec;
766 dev_dbg(&sep->pdev->dev, "time.tv_sec is %lu\n", time.tv_sec);
767 dev_dbg(&sep->pdev->dev, "time_addr is %p\n", time_addr);
768 dev_dbg(&sep->pdev->dev, "sep->shared_addr is %p\n", sep->shared_addr);
774 * sep_set_caller_id_handler - insert caller id entry
776 * @arg: pointer to struct caller_id_struct
778 * Inserts the data into the caller id table. Note that this function
779 * falls under the ioctl lock
781 static int sep_set_caller_id_handler(struct sep_device *sep, unsigned long arg)
786 struct caller_id_struct command_args;
788 for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
789 if (sep->caller_id_table[i].pid == 0)
793 if (i == SEP_CALLER_ID_TABLE_NUM_ENTRIES) {
794 dev_warn(&sep->pdev->dev, "no more caller id entries left\n");
795 dev_warn(&sep->pdev->dev, "maximum number is %d\n",
796 SEP_CALLER_ID_TABLE_NUM_ENTRIES);
802 if (copy_from_user(&command_args, (void __user *)arg,
803 sizeof(command_args))) {
808 hash = (void __user *)(unsigned long)command_args.callerIdAddress;
810 if (!command_args.pid || !command_args.callerIdSizeInBytes) {
815 dev_dbg(&sep->pdev->dev, "pid is %x\n", command_args.pid);
816 dev_dbg(&sep->pdev->dev, "callerIdSizeInBytes is %x\n",
817 command_args.callerIdSizeInBytes);
819 if (command_args.callerIdSizeInBytes >
820 SEP_CALLER_ID_HASH_SIZE_IN_BYTES) {
825 sep->caller_id_table[i].pid = command_args.pid;
827 if (copy_from_user(sep->caller_id_table[i].callerIdHash,
828 hash, command_args.callerIdSizeInBytes))
835 * sep_set_current_caller_id - set the caller id
836 * @sep: pointer to struct_sep_device
838 * Set the caller ID (if it exists) to the SEP. Note that this
839 * function falls under the ioctl lock
841 static int sep_set_current_caller_id(struct sep_device *sep)
846 /* Zero the previous value */
847 memset(sep->shared_addr + SEP_CALLER_ID_OFFSET_BYTES,
848 0, SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
850 for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
851 if (sep->caller_id_table[i].pid == current->pid) {
852 dev_dbg(&sep->pdev->dev, "Caller Id found\n");
854 memcpy(sep->shared_addr + SEP_CALLER_ID_OFFSET_BYTES,
855 (void *)(sep->caller_id_table[i].callerIdHash),
856 SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
860 /* Ensure data is in little endian */
861 hash_buf_ptr = (u32 *)sep->shared_addr +
862 SEP_CALLER_ID_OFFSET_BYTES;
864 for (i = 0; i < SEP_CALLER_ID_HASH_SIZE_IN_WORDS; i++)
865 hash_buf_ptr[i] = cpu_to_le32(hash_buf_ptr[i]);
871 * sep_send_command_handler - kick off a command
872 * @sep: SEP being signalled
874 * This function raises interrupt to SEP that signals that is has a new
875 * command from the host
877 * Note that this function does fall under the ioctl lock
879 static int sep_send_command_handler(struct sep_device *sep)
881 unsigned long lck_flags;
884 if (test_and_set_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
890 sep_set_current_caller_id(sep);
892 sep_dump_message(sep);
895 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
897 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
899 dev_dbg(&sep->pdev->dev,
900 "sep_send_command_handler send_ct %lx reply_ct %lx\n",
901 sep->send_ct, sep->reply_ct);
903 /* Send interrupt to SEP */
904 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);
911 * sep_allocate_data_pool_memory_handler -allocate pool memory
912 * @sep: pointer to struct sep_device
913 * @arg: pointer to struct alloc_struct
915 * This function handles the allocate data pool memory request
916 * This function returns calculates the bus address of the
917 * allocated memory, and the offset of this area from the mapped address.
918 * Therefore, the FVOs in user space can calculate the exact virtual
919 * address of this allocated memory
921 static int sep_allocate_data_pool_memory_handler(struct sep_device *sep,
925 struct alloc_struct command_args;
927 /* Holds the allocated buffer address in the system memory pool */
930 if (copy_from_user(&command_args, (void __user *)arg,
931 sizeof(struct alloc_struct))) {
936 /* Allocate memory */
937 if ((sep->data_pool_bytes_allocated + command_args.num_bytes) >
938 SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES) {
943 dev_dbg(&sep->pdev->dev,
944 "data pool bytes_allocated: %x\n", (int)sep->data_pool_bytes_allocated);
945 dev_dbg(&sep->pdev->dev,
946 "offset: %x\n", SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES);
947 /* Set the virtual and bus address */
948 command_args.offset = SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
949 sep->data_pool_bytes_allocated;
951 /* Place in the shared area that is known by the SEP */
952 token_addr = (u32 *)(sep->shared_addr +
953 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES +
954 (sep->num_of_data_allocations)*2*sizeof(u32));
956 token_addr[0] = SEP_DATA_POOL_POINTERS_VAL_TOKEN;
957 token_addr[1] = (u32)sep->shared_bus +
958 SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
959 sep->data_pool_bytes_allocated;
961 /* Write the memory back to the user space */
962 error = copy_to_user((void *)arg, (void *)&command_args,
963 sizeof(struct alloc_struct));
969 /* Update the allocation */
970 sep->data_pool_bytes_allocated += command_args.num_bytes;
971 sep->num_of_data_allocations += 1;
974 dev_dbg(&sep->pdev->dev, "sep_allocate_data_pool_memory_handler end\n");
979 * sep_lock_kernel_pages - map kernel pages for DMA
980 * @sep: pointer to struct sep_device
981 * @kernel_virt_addr: address of data buffer in kernel
982 * @data_size: size of data
983 * @lli_array_ptr: lli array
984 * @in_out_flag: input into device or output from device
986 * This function locks all the physical pages of the kernel virtual buffer
987 * and construct a basic lli array, where each entry holds the physical
988 * page address and the size that application data holds in this page
989 * This function is used only during kernel crypto mod calls from within
990 * the kernel (when ioctl is not used)
992 static int sep_lock_kernel_pages(struct sep_device *sep,
993 unsigned long kernel_virt_addr,
995 struct sep_lli_entry **lli_array_ptr,
1001 struct sep_lli_entry *lli_array;
1003 struct sep_dma_map *map_array;
1005 dev_dbg(&sep->pdev->dev, "lock kernel pages kernel_virt_addr is %08lx\n",
1006 (unsigned long)kernel_virt_addr);
1007 dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1009 lli_array = kmalloc(sizeof(struct sep_lli_entry), GFP_ATOMIC);
1014 map_array = kmalloc(sizeof(struct sep_dma_map), GFP_ATOMIC);
1017 goto end_function_with_error;
1020 map_array[0].dma_addr =
1021 dma_map_single(&sep->pdev->dev, (void *)kernel_virt_addr,
1022 data_size, DMA_BIDIRECTIONAL);
1023 map_array[0].size = data_size;
1027 * Set the start address of the first page - app data may start not at
1028 * the beginning of the page
1030 lli_array[0].bus_address = (u32)map_array[0].dma_addr;
1031 lli_array[0].block_size = map_array[0].size;
1033 dev_dbg(&sep->pdev->dev,
1034 "lli_array[0].bus_address is %08lx, lli_array[0].block_size is %x\n",
1035 (unsigned long)lli_array[0].bus_address,
1036 lli_array[0].block_size);
1038 /* Set the output parameters */
1039 if (in_out_flag == SEP_DRIVER_IN_FLAG) {
1040 *lli_array_ptr = lli_array;
1041 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 1;
1042 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
1043 sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
1044 sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries = 1;
1046 *lli_array_ptr = lli_array;
1047 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = 1;
1048 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = NULL;
1049 sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
1050 sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries = 1;
1054 end_function_with_error:
1062 * sep_lock_user_pages - lock and map user pages for DMA
1063 * @sep: pointer to struct sep_device
1064 * @app_virt_addr: user memory data buffer
1065 * @data_size: size of data buffer
1066 * @lli_array_ptr: lli array
1067 * @in_out_flag: input or output to device
1069 * This function locks all the physical pages of the application
1070 * virtual buffer and construct a basic lli array, where each entry
1071 * holds the physical page address and the size that application
1072 * data holds in this physical pages
1074 static int sep_lock_user_pages(struct sep_device *sep,
1077 struct sep_lli_entry **lli_array_ptr,
1084 /* The the page of the end address of the user space buffer */
1086 /* The page of the start address of the user space buffer */
1088 /* The range in pages */
1090 /* Array of pointers to page */
1091 struct page **page_array;
1093 struct sep_lli_entry *lli_array;
1095 struct sep_dma_map *map_array;
1096 /* Direction of the DMA mapping for locked pages */
1097 enum dma_data_direction dir;
1099 /* Set start and end pages and num pages */
1100 end_page = (app_virt_addr + data_size - 1) >> PAGE_SHIFT;
1101 start_page = app_virt_addr >> PAGE_SHIFT;
1102 num_pages = end_page - start_page + 1;
1104 dev_dbg(&sep->pdev->dev, "lock user pages app_virt_addr is %x\n", app_virt_addr);
1105 dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1106 dev_dbg(&sep->pdev->dev, "start_page is %x\n", start_page);
1107 dev_dbg(&sep->pdev->dev, "end_page is %x\n", end_page);
1108 dev_dbg(&sep->pdev->dev, "num_pages is %x\n", num_pages);
1110 /* Allocate array of pages structure pointers */
1111 page_array = kmalloc(sizeof(struct page *) * num_pages, GFP_ATOMIC);
1116 map_array = kmalloc(sizeof(struct sep_dma_map) * num_pages, GFP_ATOMIC);
1118 dev_warn(&sep->pdev->dev, "kmalloc for map_array failed\n");
1120 goto end_function_with_error1;
1123 lli_array = kmalloc(sizeof(struct sep_lli_entry) * num_pages,
1127 dev_warn(&sep->pdev->dev, "kmalloc for lli_array failed\n");
1129 goto end_function_with_error2;
1132 /* Convert the application virtual address into a set of physical */
1133 down_read(¤t->mm->mmap_sem);
1134 result = get_user_pages(current, current->mm, app_virt_addr,
1136 ((in_out_flag == SEP_DRIVER_IN_FLAG) ? 0 : 1),
1137 0, page_array, NULL);
1139 up_read(¤t->mm->mmap_sem);
1141 /* Check the number of pages locked - if not all then exit with error */
1142 if (result != num_pages) {
1143 dev_warn(&sep->pdev->dev,
1144 "not all pages locked by get_user_pages\n");
1146 goto end_function_with_error3;
1149 dev_dbg(&sep->pdev->dev, "get_user_pages succeeded\n");
1152 if (in_out_flag == SEP_DRIVER_IN_FLAG)
1153 dir = DMA_TO_DEVICE;
1155 dir = DMA_FROM_DEVICE;
1158 * Fill the array using page array data and
1159 * map the pages - this action will also flush the cache as needed
1161 for (count = 0; count < num_pages; count++) {
1162 /* Fill the map array */
1163 map_array[count].dma_addr =
1164 dma_map_page(&sep->pdev->dev, page_array[count],
1165 0, PAGE_SIZE, /*dir*/DMA_BIDIRECTIONAL);
1167 map_array[count].size = PAGE_SIZE;
1169 /* Fill the lli array entry */
1170 lli_array[count].bus_address = (u32)map_array[count].dma_addr;
1171 lli_array[count].block_size = PAGE_SIZE;
1173 dev_warn(&sep->pdev->dev, "lli_array[%x].bus_address is %08lx, lli_array[%x].block_size is %x\n",
1174 count, (unsigned long)lli_array[count].bus_address,
1175 count, lli_array[count].block_size);
1178 /* Check the offset for the first page */
1179 lli_array[0].bus_address =
1180 lli_array[0].bus_address + (app_virt_addr & (~PAGE_MASK));
1182 /* Check that not all the data is in the first page only */
1183 if ((PAGE_SIZE - (app_virt_addr & (~PAGE_MASK))) >= data_size)
1184 lli_array[0].block_size = data_size;
1186 lli_array[0].block_size =
1187 PAGE_SIZE - (app_virt_addr & (~PAGE_MASK));
1189 dev_dbg(&sep->pdev->dev,
1190 "lli_array[0].bus_address is %08lx, lli_array[0].block_size is %x\n",
1191 (unsigned long)lli_array[count].bus_address,
1192 lli_array[count].block_size);
1194 /* Check the size of the last page */
1195 if (num_pages > 1) {
1196 lli_array[num_pages - 1].block_size =
1197 (app_virt_addr + data_size) & (~PAGE_MASK);
1199 dev_warn(&sep->pdev->dev,
1200 "lli_array[%x].bus_address is %08lx, lli_array[%x].block_size is %x\n",
1202 (unsigned long)lli_array[count].bus_address,
1204 lli_array[count].block_size);
1207 /* Set output params acording to the in_out flag */
1208 if (in_out_flag == SEP_DRIVER_IN_FLAG) {
1209 *lli_array_ptr = lli_array;
1210 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = num_pages;
1211 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = page_array;
1212 sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
1213 sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries =
1216 *lli_array_ptr = lli_array;
1217 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = num_pages;
1218 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array =
1220 sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
1221 sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries =
1226 end_function_with_error3:
1227 /* Free lli array */
1230 end_function_with_error2:
1233 end_function_with_error1:
1234 /* Free page array */
1242 * u32 sep_calculate_lli_table_max_size - size the LLI table
1243 * @sep: pointer to struct sep_device
1245 * @num_array_entries
1248 * This function calculates the size of data that can be inserted into
1249 * the lli table from this array, such that either the table is full
1250 * (all entries are entered), or there are no more entries in the
1253 static u32 sep_calculate_lli_table_max_size(struct sep_device *sep,
1254 struct sep_lli_entry *lli_in_array_ptr,
1255 u32 num_array_entries,
1256 u32 *last_table_flag)
1259 /* Table data size */
1260 u32 table_data_size = 0;
1261 /* Data size for the next table */
1262 u32 next_table_data_size;
1264 *last_table_flag = 0;
1267 * Calculate the data in the out lli table till we fill the whole
1268 * table or till the data has ended
1271 (counter < (SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP - 1)) &&
1272 (counter < num_array_entries); counter++)
1273 table_data_size += lli_in_array_ptr[counter].block_size;
1276 * Check if we reached the last entry,
1277 * meaning this ia the last table to build,
1278 * and no need to check the block alignment
1280 if (counter == num_array_entries) {
1281 /* Set the last table flag */
1282 *last_table_flag = 1;
1287 * Calculate the data size of the next table.
1288 * Stop if no entries left or if data size is more the DMA restriction
1290 next_table_data_size = 0;
1291 for (; counter < num_array_entries; counter++) {
1292 next_table_data_size += lli_in_array_ptr[counter].block_size;
1293 if (next_table_data_size >= SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)
1298 * Check if the next table data size is less then DMA rstriction.
1299 * if it is - recalculate the current table size, so that the next
1300 * table data size will be adaquete for DMA
1302 if (next_table_data_size &&
1303 next_table_data_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)
1305 table_data_size -= (SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE -
1306 next_table_data_size);
1309 return table_data_size;
1313 * sep_build_lli_table - build an lli array for the given table
1314 * @sep: pointer to struct sep_device
1315 * @lli_array_ptr: pointer to lli array
1316 * @lli_table_ptr: pointer to lli table
1317 * @num_processed_entries_ptr: pointer to number of entries
1318 * @num_table_entries_ptr: pointer to number of tables
1319 * @table_data_size: total data size
1321 * Builds ant lli table from the lli_array according to
1322 * the given size of data
1324 static void sep_build_lli_table(struct sep_device *sep,
1325 struct sep_lli_entry *lli_array_ptr,
1326 struct sep_lli_entry *lli_table_ptr,
1327 u32 *num_processed_entries_ptr,
1328 u32 *num_table_entries_ptr,
1329 u32 table_data_size)
1331 /* Current table data size */
1332 u32 curr_table_data_size;
1333 /* Counter of lli array entry */
1336 /* Init currrent table data size and lli array entry counter */
1337 curr_table_data_size = 0;
1339 *num_table_entries_ptr = 1;
1341 dev_dbg(&sep->pdev->dev, "build lli table table_data_size is %x\n", table_data_size);
1343 /* Fill the table till table size reaches the needed amount */
1344 while (curr_table_data_size < table_data_size) {
1345 /* Update the number of entries in table */
1346 (*num_table_entries_ptr)++;
1348 lli_table_ptr->bus_address =
1349 cpu_to_le32(lli_array_ptr[array_counter].bus_address);
1351 lli_table_ptr->block_size =
1352 cpu_to_le32(lli_array_ptr[array_counter].block_size);
1354 curr_table_data_size += lli_array_ptr[array_counter].block_size;
1356 dev_dbg(&sep->pdev->dev, "lli_table_ptr is %p\n",
1358 dev_dbg(&sep->pdev->dev, "lli_table_ptr->bus_address is %08lx\n",
1359 (unsigned long)lli_table_ptr->bus_address);
1360 dev_dbg(&sep->pdev->dev, "lli_table_ptr->block_size is %x\n",
1361 lli_table_ptr->block_size);
1363 /* Check for overflow of the table data */
1364 if (curr_table_data_size > table_data_size) {
1365 dev_dbg(&sep->pdev->dev,
1366 "curr_table_data_size too large\n");
1368 /* Update the size of block in the table */
1369 lli_table_ptr->block_size -=
1370 cpu_to_le32((curr_table_data_size - table_data_size));
1372 /* Update the physical address in the lli array */
1373 lli_array_ptr[array_counter].bus_address +=
1374 cpu_to_le32(lli_table_ptr->block_size);
1376 /* Update the block size left in the lli array */
1377 lli_array_ptr[array_counter].block_size =
1378 (curr_table_data_size - table_data_size);
1380 /* Advance to the next entry in the lli_array */
1383 dev_dbg(&sep->pdev->dev,
1384 "lli_table_ptr->bus_address is %08lx\n",
1385 (unsigned long)lli_table_ptr->bus_address);
1386 dev_dbg(&sep->pdev->dev,
1387 "lli_table_ptr->block_size is %x\n",
1388 lli_table_ptr->block_size);
1390 /* Move to the next entry in table */
1394 /* Set the info entry to default */
1395 lli_table_ptr->bus_address = 0xffffffff;
1396 lli_table_ptr->block_size = 0;
1398 /* Set the output parameter */
1399 *num_processed_entries_ptr += array_counter;
1404 * sep_shared_area_virt_to_bus - map shared area to bus address
1405 * @sep: pointer to struct sep_device
1406 * @virt_address: virtual address to convert
1408 * This functions returns the physical address inside shared area according
1409 * to the virtual address. It can be either on the externa RAM device
1410 * (ioremapped), or on the system RAM
1411 * This implementation is for the external RAM
1413 static dma_addr_t sep_shared_area_virt_to_bus(struct sep_device *sep,
1416 dev_dbg(&sep->pdev->dev, "sh virt to phys v %p\n", virt_address);
1417 dev_dbg(&sep->pdev->dev, "sh virt to phys p %08lx\n",
1419 sep->shared_bus + (virt_address - sep->shared_addr));
1421 return sep->shared_bus + (size_t)(virt_address - sep->shared_addr);
1425 * sep_shared_area_bus_to_virt - map shared area bus address to kernel
1426 * @sep: pointer to struct sep_device
1427 * @bus_address: bus address to convert
1429 * This functions returns the virtual address inside shared area
1430 * according to the physical address. It can be either on the
1431 * externa RAM device (ioremapped), or on the system RAM
1432 * This implementation is for the external RAM
1434 static void *sep_shared_area_bus_to_virt(struct sep_device *sep,
1435 dma_addr_t bus_address)
1437 dev_dbg(&sep->pdev->dev, "shared bus to virt b=%lx v=%lx\n",
1438 (unsigned long)bus_address, (unsigned long)(sep->shared_addr +
1439 (size_t)(bus_address - sep->shared_bus)));
1441 return sep->shared_addr + (size_t)(bus_address - sep->shared_bus);
1445 * sep_debug_print_lli_tables - dump LLI table
1446 * @sep: pointer to struct sep_device
1447 * @lli_table_ptr: pointer to sep_lli_entry
1448 * @num_table_entries: number of entries
1449 * @table_data_size: total data size
1451 * Walk the the list of the print created tables and print all the data
1453 static void sep_debug_print_lli_tables(struct sep_device *sep,
1454 struct sep_lli_entry *lli_table_ptr,
1455 unsigned long num_table_entries,
1456 unsigned long table_data_size)
1458 unsigned long table_count = 1;
1459 unsigned long entries_count = 0;
1461 dev_dbg(&sep->pdev->dev, "sep_debug_print_lli_tables start\n");
1463 while ((unsigned long) lli_table_ptr->bus_address != 0xffffffff) {
1464 dev_dbg(&sep->pdev->dev,
1465 "lli table %08lx, table_data_size is %lu\n",
1466 table_count, table_data_size);
1467 dev_dbg(&sep->pdev->dev, "num_table_entries is %lu\n",
1470 /* Print entries of the table (without info entry) */
1471 for (entries_count = 0; entries_count < num_table_entries;
1472 entries_count++, lli_table_ptr++) {
1474 dev_dbg(&sep->pdev->dev,
1475 "lli_table_ptr address is %08lx\n",
1476 (unsigned long) lli_table_ptr);
1478 dev_dbg(&sep->pdev->dev,
1479 "phys address is %08lx block size is %x\n",
1480 (unsigned long)lli_table_ptr->bus_address,
1481 lli_table_ptr->block_size);
1483 /* Point to the info entry */
1486 dev_dbg(&sep->pdev->dev,
1487 "phys lli_table_ptr->block_size is %x\n",
1488 lli_table_ptr->block_size);
1490 dev_dbg(&sep->pdev->dev,
1491 "phys lli_table_ptr->physical_address is %08lu\n",
1492 (unsigned long)lli_table_ptr->bus_address);
1495 table_data_size = lli_table_ptr->block_size & 0xffffff;
1496 num_table_entries = (lli_table_ptr->block_size >> 24) & 0xff;
1498 dev_dbg(&sep->pdev->dev,
1499 "phys table_data_size is %lu num_table_entries is"
1500 " %lu bus_address is%lu\n", table_data_size,
1501 num_table_entries, (unsigned long)lli_table_ptr->bus_address);
1503 if ((unsigned long)lli_table_ptr->bus_address != 0xffffffff)
1504 lli_table_ptr = (struct sep_lli_entry *)
1505 sep_shared_bus_to_virt(sep,
1506 (unsigned long)lli_table_ptr->bus_address);
1510 dev_dbg(&sep->pdev->dev, "sep_debug_print_lli_tables end\n");
1515 * sep_prepare_empty_lli_table - create a blank LLI table
1516 * @sep: pointer to struct sep_device
1517 * @lli_table_addr_ptr: pointer to lli table
1518 * @num_entries_ptr: pointer to number of entries
1519 * @table_data_size_ptr: point to table data size
1521 * This function creates empty lli tables when there is no data
1523 static void sep_prepare_empty_lli_table(struct sep_device *sep,
1524 dma_addr_t *lli_table_addr_ptr,
1525 u32 *num_entries_ptr,
1526 u32 *table_data_size_ptr)
1528 struct sep_lli_entry *lli_table_ptr;
1530 /* Find the area for new table */
1532 (struct sep_lli_entry *)(sep->shared_addr +
1533 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1534 sep->num_lli_tables_created * sizeof(struct sep_lli_entry) *
1535 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
1537 lli_table_ptr->bus_address = 0;
1538 lli_table_ptr->block_size = 0;
1541 lli_table_ptr->bus_address = 0xFFFFFFFF;
1542 lli_table_ptr->block_size = 0;
1544 /* Set the output parameter value */
1545 *lli_table_addr_ptr = sep->shared_bus +
1546 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1547 sep->num_lli_tables_created *
1548 sizeof(struct sep_lli_entry) *
1549 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1551 /* Set the num of entries and table data size for empty table */
1552 *num_entries_ptr = 2;
1553 *table_data_size_ptr = 0;
1555 /* Update the number of created tables */
1556 sep->num_lli_tables_created++;
1560 * sep_prepare_input_dma_table - prepare input DMA mappings
1561 * @sep: pointer to struct sep_device
1566 * @table_data_size_ptr:
1567 * @is_kva: set for kernel data (kernel cryptio call)
1569 * This function prepares only input DMA table for synhronic symmetric
1571 * Note that all bus addresses that are passed to the SEP
1572 * are in 32 bit format; the SEP is a 32 bit device
1574 static int sep_prepare_input_dma_table(struct sep_device *sep,
1575 unsigned long app_virt_addr,
1578 dma_addr_t *lli_table_ptr,
1579 u32 *num_entries_ptr,
1580 u32 *table_data_size_ptr,
1584 /* Pointer to the info entry of the table - the last entry */
1585 struct sep_lli_entry *info_entry_ptr;
1586 /* Array of pointers to page */
1587 struct sep_lli_entry *lli_array_ptr;
1588 /* Points to the first entry to be processed in the lli_in_array */
1589 u32 current_entry = 0;
1590 /* Num entries in the virtual buffer */
1591 u32 sep_lli_entries = 0;
1592 /* Lli table pointer */
1593 struct sep_lli_entry *in_lli_table_ptr;
1594 /* The total data in one table */
1595 u32 table_data_size = 0;
1596 /* Flag for last table */
1597 u32 last_table_flag = 0;
1598 /* Number of entries in lli table */
1599 u32 num_entries_in_table = 0;
1600 /* Next table address */
1601 void *lli_table_alloc_addr = 0;
1603 dev_dbg(&sep->pdev->dev, "prepare intput dma table data_size is %x\n", data_size);
1604 dev_dbg(&sep->pdev->dev, "block_size is %x\n", block_size);
1606 /* Initialize the pages pointers */
1607 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
1608 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 0;
1610 /* Set the kernel address for first table to be allocated */
1611 lli_table_alloc_addr = (void *)(sep->shared_addr +
1612 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1613 sep->num_lli_tables_created * sizeof(struct sep_lli_entry) *
1614 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
1616 if (data_size == 0) {
1617 /* Special case - create meptu table - 2 entries, zero data */
1618 sep_prepare_empty_lli_table(sep, lli_table_ptr,
1619 num_entries_ptr, table_data_size_ptr);
1620 goto update_dcb_counter;
1623 /* Check if the pages are in Kernel Virtual Address layout */
1625 /* Lock the pages in the kernel */
1626 error = sep_lock_kernel_pages(sep, app_virt_addr,
1627 data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
1630 * Lock the pages of the user buffer
1631 * and translate them to pages
1633 error = sep_lock_user_pages(sep, app_virt_addr,
1634 data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
1639 dev_dbg(&sep->pdev->dev, "output sep_in_num_pages is %x\n",
1640 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
1643 info_entry_ptr = NULL;
1645 sep_lli_entries = sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages;
1647 /* Loop till all the entries in in array are not processed */
1648 while (current_entry < sep_lli_entries) {
1650 /* Set the new input and output tables */
1652 (struct sep_lli_entry *)lli_table_alloc_addr;
1654 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1655 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1657 if (lli_table_alloc_addr >
1658 ((void *)sep->shared_addr +
1659 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1660 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {
1663 goto end_function_error;
1667 /* Update the number of created tables */
1668 sep->num_lli_tables_created++;
1670 /* Calculate the maximum size of data for input table */
1671 table_data_size = sep_calculate_lli_table_max_size(sep,
1672 &lli_array_ptr[current_entry],
1673 (sep_lli_entries - current_entry),
1677 * If this is not the last table -
1678 * then allign it to the block size
1680 if (!last_table_flag)
1682 (table_data_size / block_size) * block_size;
1684 dev_dbg(&sep->pdev->dev, "output table_data_size is %x\n",
1687 /* Construct input lli table */
1688 sep_build_lli_table(sep, &lli_array_ptr[current_entry],
1690 ¤t_entry, &num_entries_in_table, table_data_size);
1692 if (info_entry_ptr == NULL) {
1694 /* Set the output parameters to physical addresses */
1695 *lli_table_ptr = sep_shared_area_virt_to_bus(sep,
1697 *num_entries_ptr = num_entries_in_table;
1698 *table_data_size_ptr = table_data_size;
1700 dev_dbg(&sep->pdev->dev,
1701 "output lli_table_in_ptr is %08lx\n",
1702 (unsigned long)*lli_table_ptr);
1705 /* Update the info entry of the previous in table */
1706 info_entry_ptr->bus_address =
1707 sep_shared_area_virt_to_bus(sep,
1709 info_entry_ptr->block_size =
1710 ((num_entries_in_table) << 24) |
1713 /* Save the pointer to the info entry of the current tables */
1714 info_entry_ptr = in_lli_table_ptr + num_entries_in_table - 1;
1716 /* Print input tables */
1717 sep_debug_print_lli_tables(sep, (struct sep_lli_entry *)
1718 sep_shared_area_bus_to_virt(sep, *lli_table_ptr),
1719 *num_entries_ptr, *table_data_size_ptr);
1720 /* The array of the pages */
1721 kfree(lli_array_ptr);
1724 /* Update DCB counter */
1725 sep->nr_dcb_creat++;
1729 /* Free all the allocated resources */
1730 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
1731 kfree(lli_array_ptr);
1732 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
1739 * sep_construct_dma_tables_from_lli - prepare AES/DES mappings
1740 * @sep: pointer to struct sep_device
1742 * @sep_in_lli_entries:
1744 * @sep_out_lli_entries
1747 * @lli_table_out_ptr
1748 * @in_num_entries_ptr
1749 * @out_num_entries_ptr
1750 * @table_data_size_ptr
1752 * This function creates the input and output DMA tables for
1753 * symmetric operations (AES/DES) according to the block
1754 * size from LLI arays
1755 * Note that all bus addresses that are passed to the SEP
1756 * are in 32 bit format; the SEP is a 32 bit device
1758 static int sep_construct_dma_tables_from_lli(
1759 struct sep_device *sep,
1760 struct sep_lli_entry *lli_in_array,
1761 u32 sep_in_lli_entries,
1762 struct sep_lli_entry *lli_out_array,
1763 u32 sep_out_lli_entries,
1765 dma_addr_t *lli_table_in_ptr,
1766 dma_addr_t *lli_table_out_ptr,
1767 u32 *in_num_entries_ptr,
1768 u32 *out_num_entries_ptr,
1769 u32 *table_data_size_ptr)
1771 /* Points to the area where next lli table can be allocated */
1772 void *lli_table_alloc_addr = 0;
1773 /* Input lli table */
1774 struct sep_lli_entry *in_lli_table_ptr = NULL;
1775 /* Output lli table */
1776 struct sep_lli_entry *out_lli_table_ptr = NULL;
1777 /* Pointer to the info entry of the table - the last entry */
1778 struct sep_lli_entry *info_in_entry_ptr = NULL;
1779 /* Pointer to the info entry of the table - the last entry */
1780 struct sep_lli_entry *info_out_entry_ptr = NULL;
1781 /* Points to the first entry to be processed in the lli_in_array */
1782 u32 current_in_entry = 0;
1783 /* Points to the first entry to be processed in the lli_out_array */
1784 u32 current_out_entry = 0;
1785 /* Max size of the input table */
1786 u32 in_table_data_size = 0;
1787 /* Max size of the output table */
1788 u32 out_table_data_size = 0;
1789 /* Flag te signifies if this is the last tables build */
1790 u32 last_table_flag = 0;
1791 /* The data size that should be in table */
1792 u32 table_data_size = 0;
1793 /* Number of etnries in the input table */
1794 u32 num_entries_in_table = 0;
1795 /* Number of etnries in the output table */
1796 u32 num_entries_out_table = 0;
1798 /* Initiate to point after the message area */
1799 lli_table_alloc_addr = (void *)(sep->shared_addr +
1800 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1801 (sep->num_lli_tables_created *
1802 (sizeof(struct sep_lli_entry) *
1803 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP)));
1805 /* Loop till all the entries in in array are not processed */
1806 while (current_in_entry < sep_in_lli_entries) {
1807 /* Set the new input and output tables */
1809 (struct sep_lli_entry *)lli_table_alloc_addr;
1811 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1812 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1814 /* Set the first output tables */
1816 (struct sep_lli_entry *)lli_table_alloc_addr;
1818 /* Check if the DMA table area limit was overrun */
1819 if ((lli_table_alloc_addr + sizeof(struct sep_lli_entry) *
1820 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP) >
1821 ((void *)sep->shared_addr +
1822 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1823 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {
1825 dev_warn(&sep->pdev->dev, "dma table limit overrun\n");
1829 /* Update the number of the lli tables created */
1830 sep->num_lli_tables_created += 2;
1832 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1833 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1835 /* Calculate the maximum size of data for input table */
1836 in_table_data_size =
1837 sep_calculate_lli_table_max_size(sep,
1838 &lli_in_array[current_in_entry],
1839 (sep_in_lli_entries - current_in_entry),
1842 /* Calculate the maximum size of data for output table */
1843 out_table_data_size =
1844 sep_calculate_lli_table_max_size(sep,
1845 &lli_out_array[current_out_entry],
1846 (sep_out_lli_entries - current_out_entry),
1849 dev_dbg(&sep->pdev->dev,
1850 "construct tables from lli in_table_data_size is %x\n",
1851 in_table_data_size);
1853 dev_dbg(&sep->pdev->dev,
1854 "construct tables from lli out_table_data_size is %x\n",
1855 out_table_data_size);
1857 table_data_size = in_table_data_size;
1859 if (!last_table_flag) {
1861 * If this is not the last table,
1862 * then must check where the data is smallest
1863 * and then align it to the block size
1865 if (table_data_size > out_table_data_size)
1866 table_data_size = out_table_data_size;
1869 * Now calculate the table size so that
1870 * it will be module block size
1872 table_data_size = (table_data_size / block_size) *
1876 /* Construct input lli table */
1877 sep_build_lli_table(sep, &lli_in_array[current_in_entry],
1880 &num_entries_in_table,
1883 /* Construct output lli table */
1884 sep_build_lli_table(sep, &lli_out_array[current_out_entry],
1887 &num_entries_out_table,
1890 /* If info entry is null - this is the first table built */
1891 if (info_in_entry_ptr == NULL) {
1892 /* Set the output parameters to physical addresses */
1894 sep_shared_area_virt_to_bus(sep, in_lli_table_ptr);
1896 *in_num_entries_ptr = num_entries_in_table;
1898 *lli_table_out_ptr =
1899 sep_shared_area_virt_to_bus(sep,
1902 *out_num_entries_ptr = num_entries_out_table;
1903 *table_data_size_ptr = table_data_size;
1905 dev_dbg(&sep->pdev->dev,
1906 "output lli_table_in_ptr is %08lx\n",
1907 (unsigned long)*lli_table_in_ptr);
1908 dev_dbg(&sep->pdev->dev,
1909 "output lli_table_out_ptr is %08lx\n",
1910 (unsigned long)*lli_table_out_ptr);
1912 /* Update the info entry of the previous in table */
1913 info_in_entry_ptr->bus_address =
1914 sep_shared_area_virt_to_bus(sep,
1917 info_in_entry_ptr->block_size =
1918 ((num_entries_in_table) << 24) |
1921 /* Update the info entry of the previous in table */
1922 info_out_entry_ptr->bus_address =
1923 sep_shared_area_virt_to_bus(sep,
1926 info_out_entry_ptr->block_size =
1927 ((num_entries_out_table) << 24) |
1930 dev_dbg(&sep->pdev->dev,
1931 "output lli_table_in_ptr:%08lx %08x\n",
1932 (unsigned long)info_in_entry_ptr->bus_address,
1933 info_in_entry_ptr->block_size);
1935 dev_dbg(&sep->pdev->dev,
1936 "output lli_table_out_ptr:%08lx %08x\n",
1937 (unsigned long)info_out_entry_ptr->bus_address,
1938 info_out_entry_ptr->block_size);
1941 /* Save the pointer to the info entry of the current tables */
1942 info_in_entry_ptr = in_lli_table_ptr +
1943 num_entries_in_table - 1;
1944 info_out_entry_ptr = out_lli_table_ptr +
1945 num_entries_out_table - 1;
1947 dev_dbg(&sep->pdev->dev,
1948 "output num_entries_out_table is %x\n",
1949 (u32)num_entries_out_table);
1950 dev_dbg(&sep->pdev->dev,
1951 "output info_in_entry_ptr is %lx\n",
1952 (unsigned long)info_in_entry_ptr);
1953 dev_dbg(&sep->pdev->dev,
1954 "output info_out_entry_ptr is %lx\n",
1955 (unsigned long)info_out_entry_ptr);
1958 /* Print input tables */
1959 sep_debug_print_lli_tables(sep,
1960 (struct sep_lli_entry *)
1961 sep_shared_area_bus_to_virt(sep, *lli_table_in_ptr),
1962 *in_num_entries_ptr,
1963 *table_data_size_ptr);
1965 /* Print output tables */
1966 sep_debug_print_lli_tables(sep,
1967 (struct sep_lli_entry *)
1968 sep_shared_area_bus_to_virt(sep, *lli_table_out_ptr),
1969 *out_num_entries_ptr,
1970 *table_data_size_ptr);
1976 * sep_prepare_input_output_dma_table - prepare DMA I/O table
1977 * @app_virt_in_addr:
1978 * @app_virt_out_addr:
1981 * @lli_table_in_ptr:
1982 * @lli_table_out_ptr:
1983 * @in_num_entries_ptr:
1984 * @out_num_entries_ptr:
1985 * @table_data_size_ptr:
1986 * @is_kva: set for kernel data; used only for kernel crypto module
1988 * This function builds input and output DMA tables for synhronic
1989 * symmetric operations (AES, DES, HASH). It also checks that each table
1990 * is of the modular block size
1991 * Note that all bus addresses that are passed to the SEP
1992 * are in 32 bit format; the SEP is a 32 bit device
1994 static int sep_prepare_input_output_dma_table(struct sep_device *sep,
1995 unsigned long app_virt_in_addr,
1996 unsigned long app_virt_out_addr,
1999 dma_addr_t *lli_table_in_ptr,
2000 dma_addr_t *lli_table_out_ptr,
2001 u32 *in_num_entries_ptr,
2002 u32 *out_num_entries_ptr,
2003 u32 *table_data_size_ptr,
2008 /* Array of pointers of page */
2009 struct sep_lli_entry *lli_in_array;
2010 /* Array of pointers of page */
2011 struct sep_lli_entry *lli_out_array;
2013 if (data_size == 0) {
2014 /* Prepare empty table for input and output */
2015 sep_prepare_empty_lli_table(sep, lli_table_in_ptr,
2016 in_num_entries_ptr, table_data_size_ptr);
2018 sep_prepare_empty_lli_table(sep, lli_table_out_ptr,
2019 out_num_entries_ptr, table_data_size_ptr);
2021 goto update_dcb_counter;
2024 /* Initialize the pages pointers */
2025 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
2026 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = NULL;
2028 /* Lock the pages of the buffer and translate them to pages */
2029 if (is_kva == true) {
2030 error = sep_lock_kernel_pages(sep, app_virt_in_addr,
2031 data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
2034 dev_warn(&sep->pdev->dev,
2035 "lock kernel for in failed\n");
2039 error = sep_lock_kernel_pages(sep, app_virt_out_addr,
2040 data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);
2043 dev_warn(&sep->pdev->dev,
2044 "lock kernel for out failed\n");
2050 error = sep_lock_user_pages(sep, app_virt_in_addr,
2051 data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
2053 dev_warn(&sep->pdev->dev,
2054 "sep_lock_user_pages for input virtual buffer failed\n");
2058 error = sep_lock_user_pages(sep, app_virt_out_addr,
2059 data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);
2062 dev_warn(&sep->pdev->dev,
2063 "sep_lock_user_pages for output virtual buffer failed\n");
2064 goto end_function_free_lli_in;
2068 dev_dbg(&sep->pdev->dev, "prep input output dma table sep_in_num_pages is %x\n",
2069 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
2070 dev_dbg(&sep->pdev->dev, "sep_out_num_pages is %x\n",
2071 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages);
2072 dev_dbg(&sep->pdev->dev, "SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP is %x\n",
2073 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
2075 /* Call the fucntion that creates table from the lli arrays */
2076 error = sep_construct_dma_tables_from_lli(sep, lli_in_array,
2077 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages,
2079 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages,
2080 block_size, lli_table_in_ptr, lli_table_out_ptr,
2081 in_num_entries_ptr, out_num_entries_ptr, table_data_size_ptr);
2084 dev_warn(&sep->pdev->dev,
2085 "sep_construct_dma_tables_from_lli failed\n");
2086 goto end_function_with_error;
2089 kfree(lli_out_array);
2090 kfree(lli_in_array);
2093 /* Update DCB counter */
2094 sep->nr_dcb_creat++;
2098 end_function_with_error:
2099 kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_map_array);
2100 kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_page_array);
2101 kfree(lli_out_array);
2104 end_function_free_lli_in:
2105 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
2106 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
2107 kfree(lli_in_array);
2116 * sep_prepare_input_output_dma_table_in_dcb - prepare control blocks
2117 * @app_in_address: unsigned long; for data buffer in (user space)
2118 * @app_out_address: unsigned long; for data buffer out (user space)
2119 * @data_in_size: u32; for size of data
2120 * @block_size: u32; for block size
2121 * @tail_block_size: u32; for size of tail block
2122 * @isapplet: bool; to indicate external app
2123 * @is_kva: bool; kernel buffer; only used for kernel crypto module
2125 * This function prepares the linked DMA tables and puts the
2126 * address for the linked list of tables inta a DCB (data control
2127 * block) the address of which is known by the SEP hardware
2128 * Note that all bus addresses that are passed to the SEP
2129 * are in 32 bit format; the SEP is a 32 bit device
2131 static int sep_prepare_input_output_dma_table_in_dcb(struct sep_device *sep,
2132 unsigned long app_in_address,
2133 unsigned long app_out_address,
2136 u32 tail_block_size,
2143 /* Address of the created DCB table */
2144 struct sep_dcblock *dcb_table_ptr = NULL;
2145 /* The physical address of the first input DMA table */
2146 dma_addr_t in_first_mlli_address = 0;
2147 /* Number of entries in the first input DMA table */
2148 u32 in_first_num_entries = 0;
2149 /* The physical address of the first output DMA table */
2150 dma_addr_t out_first_mlli_address = 0;
2151 /* Number of entries in the first output DMA table */
2152 u32 out_first_num_entries = 0;
2153 /* Data in the first input/output table */
2154 u32 first_data_size = 0;
2156 if (sep->nr_dcb_creat == SEP_MAX_NUM_SYNC_DMA_OPS) {
2157 /* No more DCBs to allocate */
2158 dev_warn(&sep->pdev->dev, "no more DCBs available\n");
2163 /* Allocate new DCB */
2164 dcb_table_ptr = (struct sep_dcblock *)(sep->shared_addr +
2165 SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES +
2166 (sep->nr_dcb_creat * sizeof(struct sep_dcblock)));
2168 /* Set the default values in the DCB */
2169 dcb_table_ptr->input_mlli_address = 0;
2170 dcb_table_ptr->input_mlli_num_entries = 0;
2171 dcb_table_ptr->input_mlli_data_size = 0;
2172 dcb_table_ptr->output_mlli_address = 0;
2173 dcb_table_ptr->output_mlli_num_entries = 0;
2174 dcb_table_ptr->output_mlli_data_size = 0;
2175 dcb_table_ptr->tail_data_size = 0;
2176 dcb_table_ptr->out_vr_tail_pt = 0;
2178 if (isapplet == true) {
2180 /* Check if there is enough data for DMA operation */
2181 if (data_in_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE) {
2182 if (is_kva == true) {
2183 memcpy(dcb_table_ptr->tail_data,
2184 (void *)app_in_address, data_in_size);
2186 if (copy_from_user(dcb_table_ptr->tail_data,
2187 (void __user *)app_in_address,
2194 dcb_table_ptr->tail_data_size = data_in_size;
2196 /* Set the output user-space address for mem2mem op */
2197 if (app_out_address)
2198 dcb_table_ptr->out_vr_tail_pt =
2199 (aligned_u64)app_out_address;
2202 * Update both data length parameters in order to avoid
2203 * second data copy and allow building of empty mlli
2210 if (!app_out_address) {
2211 tail_size = data_in_size % block_size;
2213 if (tail_block_size == block_size)
2214 tail_size = block_size;
2221 if (is_kva == true) {
2222 memcpy(dcb_table_ptr->tail_data,
2223 (void *)(app_in_address + data_in_size -
2224 tail_size), tail_size);
2226 /* We have tail data - copy it to DCB */
2227 if (copy_from_user(dcb_table_ptr->tail_data,
2228 (void *)(app_in_address +
2229 data_in_size - tail_size), tail_size)) {
2234 if (app_out_address)
2236 * Calculate the output address
2237 * according to tail data size
2239 dcb_table_ptr->out_vr_tail_pt =
2240 (aligned_u64)app_out_address + data_in_size
2243 /* Save the real tail data size */
2244 dcb_table_ptr->tail_data_size = tail_size;
2246 * Update the data size without the tail
2247 * data size AKA data for the dma
2249 data_in_size = (data_in_size - tail_size);
2252 /* Check if we need to build only input table or input/output */
2253 if (app_out_address) {
2254 /* Prepare input/output tables */
2255 error = sep_prepare_input_output_dma_table(sep,
2260 &in_first_mlli_address,
2261 &out_first_mlli_address,
2262 &in_first_num_entries,
2263 &out_first_num_entries,
2267 /* Prepare input tables */
2268 error = sep_prepare_input_dma_table(sep,
2272 &in_first_mlli_address,
2273 &in_first_num_entries,
2279 dev_warn(&sep->pdev->dev, "prepare DMA table call failed from prepare DCB call\n");
2283 /* Set the DCB values */
2284 dcb_table_ptr->input_mlli_address = in_first_mlli_address;
2285 dcb_table_ptr->input_mlli_num_entries = in_first_num_entries;
2286 dcb_table_ptr->input_mlli_data_size = first_data_size;
2287 dcb_table_ptr->output_mlli_address = out_first_mlli_address;
2288 dcb_table_ptr->output_mlli_num_entries = out_first_num_entries;
2289 dcb_table_ptr->output_mlli_data_size = first_data_size;
2298 * sep_create_sync_dma_tables_handler - create sync DMA tables
2299 * @sep: pointer to struct sep_device
2300 * @arg: pointer to struct bld_syn_tab_struct
2302 * Handle the request for creation of the DMA tables for the synchronic
2303 * symmetric operations (AES,DES). Note that all bus addresses that are
2304 * passed to the SEP are in 32 bit format; the SEP is a 32 bit device
2306 static int sep_create_sync_dma_tables_handler(struct sep_device *sep,
2311 /* Command arguments */
2312 struct bld_syn_tab_struct command_args;
2314 if (copy_from_user(&command_args, (void __user *)arg,
2315 sizeof(struct bld_syn_tab_struct))) {
2320 dev_dbg(&sep->pdev->dev, "create dma table handler app_in_address is %08llx\n",
2321 command_args.app_in_address);
2322 dev_dbg(&sep->pdev->dev, "app_out_address is %08llx\n",
2323 command_args.app_out_address);
2324 dev_dbg(&sep->pdev->dev, "data_size is %u\n",
2325 command_args.data_in_size);
2326 dev_dbg(&sep->pdev->dev, "block_size is %u\n",
2327 command_args.block_size);
2329 /* Validate user parameters */
2330 if (!command_args.app_in_address) {
2335 error = sep_prepare_input_output_dma_table_in_dcb(sep,
2336 (unsigned long)command_args.app_in_address,
2337 (unsigned long)command_args.app_out_address,
2338 command_args.data_in_size,
2339 command_args.block_size,
2349 * sep_free_dma_tables_and_dcb - free DMA tables and DCBs
2350 * @sep: pointer to struct sep_device
2351 * @isapplet: indicates external application (used for kernel access)
2352 * @is_kva: indicates kernel addresses (only used for kernel crypto)
2354 * This function frees the DMA tables and DCB
2356 static int sep_free_dma_tables_and_dcb(struct sep_device *sep, bool isapplet,
2362 struct sep_dcblock *dcb_table_ptr;
2363 unsigned long pt_hold;
2366 if (isapplet == true) {
2367 /* Set pointer to first DCB table */
2368 dcb_table_ptr = (struct sep_dcblock *)
2370 SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES);
2372 /* Go over each DCB and see if tail pointer must be updated */
2373 for (i = 0; i < sep->nr_dcb_creat; i++, dcb_table_ptr++) {
2374 if (dcb_table_ptr->out_vr_tail_pt) {
2375 pt_hold = (unsigned long)dcb_table_ptr->out_vr_tail_pt;
2376 tail_pt = (void *)pt_hold;
2377 if (is_kva == true) {
2379 dcb_table_ptr->tail_data,
2380 dcb_table_ptr->tail_data_size);
2382 error_temp = copy_to_user(
2384 dcb_table_ptr->tail_data,
2385 dcb_table_ptr->tail_data_size);
2388 /* Release the DMA resource */
2395 /* Free the output pages, if any */
2396 sep_free_dma_table_data_handler(sep);
2402 * sep_get_static_pool_addr_handler - get static pool address
2403 * @sep: pointer to struct sep_device
2405 * This function sets the bus and virtual addresses of the static pool
2407 static int sep_get_static_pool_addr_handler(struct sep_device *sep)
2409 u32 *static_pool_addr = NULL;
2411 static_pool_addr = (u32 *)(sep->shared_addr +
2412 SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);
2414 static_pool_addr[0] = SEP_STATIC_POOL_VAL_TOKEN;
2415 static_pool_addr[1] = (u32)sep->shared_bus +
2416 SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;
2418 dev_dbg(&sep->pdev->dev, "static pool segment: physical %x\n",
2419 (u32)static_pool_addr[1]);
2425 * sep_start_handler - start device
2426 * @sep: pointer to struct sep_device
2428 static int sep_start_handler(struct sep_device *sep)
2430 unsigned long reg_val;
2431 unsigned long error = 0;
2433 /* Wait in polling for message from SEP */
2435 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2438 /* Check the value */
2440 /* Fatal error - read error status from GPRO */
2441 error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
2446 * ep_check_sum_calc - checksum messages
2447 * @data: buffer to checksum
2448 * @length: buffer size
2450 * This function performs a checksum for messages that are sent
2453 static u32 sep_check_sum_calc(u8 *data, u32 length)
2456 u16 *Tdata = (u16 *)data;
2458 while (length > 1) {
2459 /* This is the inner loop */
2464 /* Add left-over byte, if any */
2466 sum += *(u8 *)Tdata;
2468 /* Fold 32-bit sum to 16 bits */
2470 sum = (sum & 0xffff) + (sum >> 16);
2472 return ~sum & 0xFFFF;
2476 * sep_init_handler -
2477 * @sep: pointer to struct sep_device
2478 * @arg: parameters from user space application
2480 * Handles the request for SEP initialization
2481 * Note that this will go away for Medfield once the SCU
2482 * SEP initialization is complete
2483 * Also note that the message to the SEP has components
2484 * from user space as well as components written by the driver
2485 * This is becuase the portions of the message that pertain to
2486 * physical addresses must be set by the driver after the message
2487 * leaves custody of the user space application for security
2490 static int sep_init_handler(struct sep_device *sep, unsigned long arg)
2492 u32 message_buff[14];
2496 dma_addr_t new_base_addr;
2497 unsigned long addr_hold;
2498 struct init_struct command_args;
2500 /* Make sure that we have not initialized already */
2501 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2503 if (reg_val != 0x2) {
2504 error = SEP_ALREADY_INITIALIZED_ERR;
2505 dev_warn(&sep->pdev->dev, "init; device already initialized\n");
2509 /* Only root can initialize */
2510 if (!capable(CAP_SYS_ADMIN)) {
2515 /* Copy in the parameters */
2516 error = copy_from_user(&command_args, (void __user *)arg,
2517 sizeof(struct init_struct));
2524 /* Validate parameters */
2525 if (!command_args.message_addr || !command_args.sep_sram_addr ||
2526 command_args.message_size_in_words > 14) {
2531 /* Copy in the SEP init message */
2532 addr_hold = (unsigned long)command_args.message_addr;
2533 error = copy_from_user(message_buff,
2534 (void __user *)addr_hold,
2535 command_args.message_size_in_words*sizeof(u32));
2542 /* Load resident, cache, and extapp firmware */
2543 error = sep_load_firmware(sep);
2546 dev_warn(&sep->pdev->dev,
2547 "init; copy SEP init message failed %x\n", error);
2551 /* Compute the base address */
2552 new_base_addr = sep->shared_bus;
2554 if (sep->resident_bus < new_base_addr)
2555 new_base_addr = sep->resident_bus;
2557 if (sep->cache_bus < new_base_addr)
2558 new_base_addr = sep->cache_bus;
2560 if (sep->dcache_bus < new_base_addr)
2561 new_base_addr = sep->dcache_bus;
2563 /* Put physical addresses in SEP message */
2564 message_buff[3] = (u32)new_base_addr;
2565 message_buff[4] = (u32)sep->shared_bus;
2566 message_buff[6] = (u32)sep->resident_bus;
2567 message_buff[7] = (u32)sep->cache_bus;
2568 message_buff[8] = (u32)sep->dcache_bus;
2570 message_buff[command_args.message_size_in_words - 1] = 0x0;
2571 message_buff[command_args.message_size_in_words - 1] =
2572 sep_check_sum_calc((u8 *)message_buff,
2573 command_args.message_size_in_words*sizeof(u32));
2575 /* Debug print of message */
2576 for (counter = 0; counter < command_args.message_size_in_words;
2578 dev_dbg(&sep->pdev->dev, "init; SEP message word %d is %x\n",
2579 counter, message_buff[counter]);
2581 /* Tell the SEP the sram address */
2582 sep_write_reg(sep, HW_SRAM_ADDR_REG_ADDR, command_args.sep_sram_addr);
2584 /* Push the message to the SEP */
2585 for (counter = 0; counter < command_args.message_size_in_words;
2587 sep_write_reg(sep, HW_SRAM_DATA_REG_ADDR,
2588 message_buff[counter]);
2589 sep_wait_sram_write(sep);
2592 /* Signal SEP that message is ready and to init */
2593 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x1);
2595 /* Wait for acknowledge */
2598 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2599 } while (!(reg_val & 0xFFFFFFFD));
2601 if (reg_val == 0x1) {
2602 dev_warn(&sep->pdev->dev, "init; device int failed\n");
2603 error = sep_read_reg(sep, 0x8060);
2604 dev_warn(&sep->pdev->dev, "init; sw monitor is %x\n", error);
2605 error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
2606 dev_warn(&sep->pdev->dev, "init; error is %x\n", error);
2609 /* Signal SEP to zero the GPR3 */
2610 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x10);
2612 /* Wait for response */
2615 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2616 } while (reg_val != 0);
2623 * sep_end_transaction_handler - end transaction
2624 * @sep: pointer to struct sep_device
2626 * This API handles the end transaction request
2628 static int sep_end_transaction_handler(struct sep_device *sep)
2630 /* Clear the data pool pointers Token */
2631 memset((void *)(sep->shared_addr +
2632 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES),
2633 0, sep->num_of_data_allocations*2*sizeof(u32));
2635 /* Check that all the DMA resources were freed */
2636 sep_free_dma_table_data_handler(sep);
2638 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
2641 * We are now through with the transaction. Let's
2642 * allow other processes who have the device open
2643 * to perform transactions
2645 mutex_lock(&sep->sep_mutex);
2646 sep->pid_doing_transaction = 0;
2647 mutex_unlock(&sep->sep_mutex);
2648 /* Raise event for stuck contextes */
2649 wake_up(&sep->event);
2655 * sep_prepare_dcb_handler - prepare a control block
2656 * @sep: pointer to struct sep_device
2657 * @arg: pointer to user parameters
2659 * This function will retrieve the RAR buffer physical addresses, type
2660 * & size corresponding to the RAR handles provided in the buffers vector.
2662 static int sep_prepare_dcb_handler(struct sep_device *sep, unsigned long arg)
2665 /* Command arguments */
2666 struct build_dcb_struct command_args;
2668 /* Get the command arguments */
2669 if (copy_from_user(&command_args, (void __user *)arg,
2670 sizeof(struct build_dcb_struct))) {
2675 dev_dbg(&sep->pdev->dev, "prep dcb handler app_in_address is %08llx\n",
2676 command_args.app_in_address);
2677 dev_dbg(&sep->pdev->dev, "app_out_address is %08llx\n",
2678 command_args.app_out_address);
2679 dev_dbg(&sep->pdev->dev, "data_size is %x\n",
2680 command_args.data_in_size);
2681 dev_dbg(&sep->pdev->dev, "block_size is %x\n",
2682 command_args.block_size);
2683 dev_dbg(&sep->pdev->dev, "tail block_size is %x\n",
2684 command_args.tail_block_size);
2686 error = sep_prepare_input_output_dma_table_in_dcb(sep,
2687 (unsigned long)command_args.app_in_address,
2688 (unsigned long)command_args.app_out_address,
2689 command_args.data_in_size, command_args.block_size,
2690 command_args.tail_block_size, true, false);
2698 * sep_free_dcb_handler - free control block resources
2699 * @sep: pointer to struct sep_device
2701 * This function frees the DCB resources and updates the needed
2702 * user-space buffers.
2704 static int sep_free_dcb_handler(struct sep_device *sep)
2708 dev_dbg(&sep->pdev->dev, "free dcbs num of DCBs %x\n", sep->nr_dcb_creat);
2710 error = sep_free_dma_tables_and_dcb(sep, false, false);
2716 * sep_rar_prepare_output_msg_handler - prepare an output message
2717 * @sep: pointer to struct sep_device
2718 * @arg: pointer to user parameters
2720 * This function will retrieve the RAR buffer physical addresses, type
2721 * & size corresponding to the RAR handles provided in the buffers vector.
2723 static int sep_rar_prepare_output_msg_handler(struct sep_device *sep,
2728 struct rar_hndl_to_bus_struct command_args;
2729 struct RAR_buffer rar_buf;
2731 dma_addr_t rar_bus = 0;
2732 /* Holds the RAR address in the system memory offset */
2736 if (copy_from_user(&command_args, (void __user *)arg,
2737 sizeof(command_args))) {
2742 /* Call to translation function only if user handle is not NULL */
2743 if (command_args.rar_handle) {
2744 memset(&rar_buf, 0, sizeof(rar_buf));
2745 rar_buf.info.handle = (u32)command_args.rar_handle;
2747 if (rar_handle_to_bus(&rar_buf, 1) != 1) {
2751 rar_bus = rar_buf.bus_address;
2753 dev_dbg(&sep->pdev->dev, "rar msg; rar_addr_bus = %x\n", (u32)rar_bus);
2755 /* Set value in the SYSTEM MEMORY offset */
2756 rar_addr = (u32 *)(sep->shared_addr +
2757 SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);
2759 /* Copy the physical address to the System Area for the SEP */
2760 rar_addr[0] = SEP_RAR_VAL_TOKEN;
2761 rar_addr[1] = rar_bus;
2768 * sep_realloc_ext_cache_handler - report location of extcache
2769 * @sep: pointer to struct sep_device
2770 * @arg: pointer to user parameters
2772 * This function tells the SEP where the extapp is located
2774 static int sep_realloc_ext_cache_handler(struct sep_device *sep,
2777 /* Holds the new ext cache address in the system memory offset */
2780 /* Set value in the SYSTEM MEMORY offset */
2781 system_addr = (u32 *)(sep->shared_addr +
2782 SEP_DRIVER_SYSTEM_EXT_CACHE_ADDR_OFFSET_IN_BYTES);
2784 /* Copy the physical address to the System Area for the SEP */
2785 system_addr[0] = SEP_EXT_CACHE_ADDR_VAL_TOKEN;
2786 system_addr[1] = sep->extapp_bus;
2792 * sep_ioctl - ioctl api
2793 * @filp: pointer to struct file
2795 * @arg: pointer to argument structure
2797 * Implement the ioctl methods availble on the SEP device.
2799 static long sep_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2802 struct sep_device *sep = filp->private_data;
2804 dev_dbg(&sep->pdev->dev, "ioctl cmd is %x\n", cmd);
2806 /* Make sure we own this device */
2807 mutex_lock(&sep->sep_mutex);
2808 if ((current->pid != sep->pid_doing_transaction) &&
2809 (sep->pid_doing_transaction != 0)) {
2810 dev_warn(&sep->pdev->dev, "ioctl pid is not owner\n");
2811 mutex_unlock(&sep->sep_mutex);
2816 mutex_unlock(&sep->sep_mutex);
2818 /* Check that the command is for SEP device */
2819 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
2824 /* Lock to prevent the daemon to interfere with operation */
2825 mutex_lock(&sep->ioctl_mutex);
2828 case SEP_IOCSENDSEPCOMMAND:
2829 /* Send command to SEP */
2830 error = sep_send_command_handler(sep);
2832 case SEP_IOCALLOCDATAPOLL:
2833 /* Allocate data pool */
2834 error = sep_allocate_data_pool_memory_handler(sep, arg);
2836 case SEP_IOCCREATESYMDMATABLE:
2837 /* Create DMA table for synhronic operation */
2838 error = sep_create_sync_dma_tables_handler(sep, arg);
2840 case SEP_IOCFREEDMATABLEDATA:
2841 /* Free the pages */
2842 error = sep_free_dma_table_data_handler(sep);
2844 case SEP_IOCSEPSTART:
2845 /* Start command to SEP */
2846 if (sep->pdev->revision == 0) /* Only for old chip */
2847 error = sep_start_handler(sep);
2849 error = -EPERM; /* Not permitted on new chip */
2851 case SEP_IOCSEPINIT:
2852 /* Init command to SEP */
2853 if (sep->pdev->revision == 0) /* Only for old chip */
2854 error = sep_init_handler(sep, arg);
2856 error = -EPERM; /* Not permitted on new chip */
2858 case SEP_IOCGETSTATICPOOLADDR:
2859 /* Inform the SEP the bus address of the static pool */
2860 error = sep_get_static_pool_addr_handler(sep);
2862 case SEP_IOCENDTRANSACTION:
2863 error = sep_end_transaction_handler(sep);
2865 case SEP_IOCREALLOCEXTCACHE:
2866 if (sep->pdev->revision == 0) /* Only for old chip */
2867 error = sep_realloc_ext_cache_handler(sep, arg);
2869 error = -EPERM; /* Not permitted on new chip */
2871 case SEP_IOCRARPREPAREMESSAGE:
2872 error = sep_rar_prepare_output_msg_handler(sep, arg);
2874 case SEP_IOCPREPAREDCB:
2875 error = sep_prepare_dcb_handler(sep, arg);
2877 case SEP_IOCFREEDCB:
2878 error = sep_free_dcb_handler(sep);
2881 dev_dbg(&sep->pdev->dev, "invalid ioctl %x\n", cmd);
2885 mutex_unlock(&sep->ioctl_mutex);
2888 dev_dbg(&sep->pdev->dev, "ioctl end\n");
2893 * sep_singleton_ioctl - ioctl api for singleton interface
2894 * @filp: pointer to struct file
2896 * @arg: pointer to argument structure
2898 * Implement the additional ioctls for the singleton device
2900 static long sep_singleton_ioctl(struct file *filp, u32 cmd, unsigned long arg)
2903 struct sep_device *sep = filp->private_data;
2905 dev_dbg(&sep->pdev->dev, "singleton ioctl cmd is %x\n", cmd);
2907 /* Check that the command is for the SEP device */
2908 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
2913 /* Make sure we own this device */
2914 mutex_lock(&sep->sep_mutex);
2915 if ((current->pid != sep->pid_doing_transaction) &&
2916 (sep->pid_doing_transaction != 0)) {
2917 dev_warn(&sep->pdev->dev, "singleton ioctl pid is not owner\n");
2918 mutex_unlock(&sep->sep_mutex);
2923 mutex_unlock(&sep->sep_mutex);
2926 case SEP_IOCTLSETCALLERID:
2927 mutex_lock(&sep->ioctl_mutex);
2928 error = sep_set_caller_id_handler(sep, arg);
2929 mutex_unlock(&sep->ioctl_mutex);
2932 error = sep_ioctl(filp, cmd, arg);
2941 * sep_request_daemon_ioctl - ioctl for daemon
2942 * @filp: pointer to struct file
2944 * @arg: pointer to argument structure
2946 * Called by the request daemon to perform ioctls on the daemon device
2948 static long sep_request_daemon_ioctl(struct file *filp, u32 cmd,
2953 struct sep_device *sep = filp->private_data;
2955 dev_dbg(&sep->pdev->dev, "daemon ioctl: cmd is %x\n", cmd);
2957 /* Check that the command is for SEP device */
2958 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
2963 /* Only one process can access ioctl at any given time */
2964 mutex_lock(&sep->ioctl_mutex);
2967 case SEP_IOCSENDSEPRPLYCOMMAND:
2968 /* Send reply command to SEP */
2969 error = sep_req_daemon_send_reply_command_handler(sep);
2971 case SEP_IOCENDTRANSACTION:
2973 * End req daemon transaction, do nothing
2974 * will be removed upon update in middleware
2980 dev_warn(&sep->pdev->dev, "daemon ioctl: no such IOCTL\n");
2983 mutex_unlock(&sep->ioctl_mutex);
2991 * sep_inthandler - interrupt handler
2993 * @dev_id: device id
2995 static irqreturn_t sep_inthandler(int irq, void *dev_id)
2997 irqreturn_t int_error = IRQ_HANDLED;
2998 unsigned long lck_flags;
2999 u32 reg_val, reg_val2 = 0;
3000 struct sep_device *sep = dev_id;
3002 /* Read the IRR register to check if this is SEP interrupt */
3003 reg_val = sep_read_reg(sep, HW_HOST_IRR_REG_ADDR);
3005 if (reg_val & (0x1 << 13)) {
3006 /* Lock and update the counter of reply messages */
3007 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
3009 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
3011 dev_dbg(&sep->pdev->dev, "sep int: send_ct %lx reply_ct %lx\n",
3012 sep->send_ct, sep->reply_ct);
3014 /* Is this printf or daemon request? */
3015 reg_val2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
3016 dev_dbg(&sep->pdev->dev,
3017 "SEP Interrupt - reg2 is %08x\n", reg_val2);
3019 if ((reg_val2 >> 30) & 0x1) {
3020 dev_dbg(&sep->pdev->dev, "int: printf request\n");
3021 wake_up(&sep->event_request_daemon);
3022 } else if (reg_val2 >> 31) {
3023 dev_dbg(&sep->pdev->dev, "int: daemon request\n");
3024 wake_up(&sep->event_request_daemon);
3026 dev_dbg(&sep->pdev->dev, "int: SEP reply\n");
3027 wake_up(&sep->event);
3030 dev_dbg(&sep->pdev->dev, "int: not SEP interrupt\n");
3031 int_error = IRQ_NONE;
3033 if (int_error == IRQ_HANDLED)
3034 sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, reg_val);
3040 * sep_reconfig_shared_area - reconfigure shared area
3041 * @sep: pointer to struct sep_device
3043 * Reconfig the shared area between HOST and SEP - needed in case
3044 * the DX_CC_Init function was called before OS loading.
3046 static int sep_reconfig_shared_area(struct sep_device *sep)
3050 /* use to limit waiting for SEP */
3051 unsigned long end_time;
3053 /* Send the new SHARED MESSAGE AREA to the SEP */
3054 dev_dbg(&sep->pdev->dev, "reconfig shared; sending %08llx to sep\n",
3055 (unsigned long long)sep->shared_bus);
3057 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR1_REG_ADDR, sep->shared_bus);
3059 /* Poll for SEP response */
3060 ret_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
3062 end_time = jiffies + (WAIT_TIME * HZ);
3064 while ((time_before(jiffies, end_time)) && (ret_val != 0xffffffff) &&
3065 (ret_val != sep->shared_bus))
3066 ret_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
3068 /* Check the return value (register) */
3069 if (ret_val != sep->shared_bus) {
3070 dev_warn(&sep->pdev->dev, "could not reconfig shared area\n");
3071 dev_warn(&sep->pdev->dev, "result was %x\n", ret_val);
3076 dev_dbg(&sep->pdev->dev, "reconfig shared area end\n");
3080 /* File operation for singleton SEP operations */
3081 static const struct file_operations singleton_file_operations = {
3082 .owner = THIS_MODULE,
3083 .unlocked_ioctl = sep_singleton_ioctl,
3085 .open = sep_singleton_open,
3086 .release = sep_singleton_release,
3090 /* File operation for daemon operations */
3091 static const struct file_operations daemon_file_operations = {
3092 .owner = THIS_MODULE,
3093 .unlocked_ioctl = sep_request_daemon_ioctl,
3094 .poll = sep_request_daemon_poll,
3095 .open = sep_request_daemon_open,
3096 .release = sep_request_daemon_release,
3097 .mmap = sep_request_daemon_mmap,
3100 /* The files operations structure of the driver */
3101 static const struct file_operations sep_file_operations = {
3102 .owner = THIS_MODULE,
3103 .unlocked_ioctl = sep_ioctl,
3106 .release = sep_release,
3111 * sep_register_driver_with_fs - register misc devices
3112 * @sep: pointer to struct sep_device
3114 * This function registers the driver with the file system
3116 static int sep_register_driver_with_fs(struct sep_device *sep)
3120 sep->miscdev_sep.minor = MISC_DYNAMIC_MINOR;
3121 sep->miscdev_sep.name = SEP_DEV_NAME;
3122 sep->miscdev_sep.fops = &sep_file_operations;
3124 sep->miscdev_singleton.minor = MISC_DYNAMIC_MINOR;
3125 sep->miscdev_singleton.name = SEP_DEV_SINGLETON;
3126 sep->miscdev_singleton.fops = &singleton_file_operations;
3128 sep->miscdev_daemon.minor = MISC_DYNAMIC_MINOR;
3129 sep->miscdev_daemon.name = SEP_DEV_DAEMON;
3130 sep->miscdev_daemon.fops = &daemon_file_operations;
3132 ret_val = misc_register(&sep->miscdev_sep);
3134 dev_warn(&sep->pdev->dev, "misc reg fails for SEP %x\n",
3139 ret_val = misc_register(&sep->miscdev_singleton);
3141 dev_warn(&sep->pdev->dev, "misc reg fails for sing %x\n",
3143 misc_deregister(&sep->miscdev_sep);
3147 ret_val = misc_register(&sep->miscdev_daemon);
3149 dev_warn(&sep->pdev->dev, "misc reg fails for dmn %x\n",
3151 misc_deregister(&sep->miscdev_sep);
3152 misc_deregister(&sep->miscdev_singleton);
3161 * sep_probe - probe a matching PCI device
3163 * @end: pci_device_id
3165 * Attempt to set up and configure a SEP device that has been
3166 * discovered by the PCI layer.
3168 static int __devinit sep_probe(struct pci_dev *pdev,
3169 const struct pci_device_id *ent)
3172 struct sep_device *sep;
3174 if (sep_dev != NULL) {
3175 dev_warn(&pdev->dev, "only one SEP supported.\n");
3179 /* Enable the device */
3180 error = pci_enable_device(pdev);
3182 dev_warn(&pdev->dev, "error enabling pci device\n");
3186 /* Allocate the sep_device structure for this device */
3187 sep_dev = kzalloc(sizeof(struct sep_device), GFP_ATOMIC);
3188 if (sep_dev == NULL) {
3189 dev_warn(&pdev->dev,
3190 "can't kmalloc the sep_device structure\n");
3192 goto end_function_disable_device;
3196 * We're going to use another variable for actually
3197 * working with the device; this way, if we have
3198 * multiple devices in the future, it would be easier
3199 * to make appropriate changes
3203 sep->pdev = pci_dev_get(pdev);
3205 init_waitqueue_head(&sep->event);
3206 init_waitqueue_head(&sep->event_request_daemon);
3207 spin_lock_init(&sep->snd_rply_lck);
3208 mutex_init(&sep->sep_mutex);
3209 mutex_init(&sep->ioctl_mutex);
3211 dev_dbg(&sep->pdev->dev, "sep probe: PCI obtained, device being prepared\n");
3212 dev_dbg(&sep->pdev->dev, "revision is %d\n", sep->pdev->revision);
3214 /* Set up our register area */
3215 sep->reg_physical_addr = pci_resource_start(sep->pdev, 0);
3216 if (!sep->reg_physical_addr) {
3217 dev_warn(&sep->pdev->dev, "Error getting register start\n");
3219 goto end_function_free_sep_dev;
3222 sep->reg_physical_end = pci_resource_end(sep->pdev, 0);
3223 if (!sep->reg_physical_end) {
3224 dev_warn(&sep->pdev->dev, "Error getting register end\n");
3226 goto end_function_free_sep_dev;
3229 sep->reg_addr = ioremap_nocache(sep->reg_physical_addr,
3230 (size_t)(sep->reg_physical_end - sep->reg_physical_addr + 1));
3231 if (!sep->reg_addr) {
3232 dev_warn(&sep->pdev->dev, "Error getting register virtual\n");
3234 goto end_function_free_sep_dev;
3237 dev_dbg(&sep->pdev->dev,
3238 "Register area start %llx end %llx virtual %p\n",
3239 (unsigned long long)sep->reg_physical_addr,
3240 (unsigned long long)sep->reg_physical_end,
3243 /* Allocate the shared area */
3244 sep->shared_size = SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES +
3245 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES +
3246 SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES +
3247 SEP_DRIVER_STATIC_AREA_SIZE_IN_BYTES +
3248 SEP_DRIVER_SYSTEM_DATA_MEMORY_SIZE_IN_BYTES;
3250 if (sep_map_and_alloc_shared_area(sep)) {
3252 /* Allocation failed */
3253 goto end_function_error;
3256 sep->rar_size = FAKE_RAR_SIZE;
3257 sep->rar_addr = dma_alloc_coherent(&sep->pdev->dev,
3258 sep->rar_size, &sep->rar_bus, GFP_KERNEL);
3259 if (sep->rar_addr == NULL) {
3260 dev_warn(&sep->pdev->dev, "can't allocate mfld rar\n");
3262 goto end_function_deallocate_sep_shared_area;
3265 dev_dbg(&sep->pdev->dev, "rar start is %p, phy is %llx,"
3266 " size is %zx\n", sep->rar_addr,
3267 (unsigned long long)sep->rar_bus,
3270 /* Clear ICR register */
3271 sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, 0xFFFFFFFF);
3273 /* Set the IMR register - open only GPR 2 */
3274 sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));
3276 /* Read send/receive counters from SEP */
3277 sep->reply_ct = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
3278 sep->reply_ct &= 0x3FFFFFFF;
3279 sep->send_ct = sep->reply_ct;
3281 /* Get the interrupt line */
3282 error = request_irq(pdev->irq, sep_inthandler, IRQF_SHARED,
3286 goto end_function_dealloc_rar;
3288 /* The new chip requires ashared area reconfigure */
3289 if (sep->pdev->revision == 4) { /* Only for new chip */
3290 error = sep_reconfig_shared_area(sep);
3292 goto end_function_free_irq;
3294 /* Finally magic up the device nodes */
3295 /* Register driver with the fs */
3296 error = sep_register_driver_with_fs(sep);
3301 end_function_free_irq:
3302 free_irq(pdev->irq, sep);
3304 end_function_dealloc_rar:
3306 dma_free_coherent(&sep->pdev->dev, sep->rar_size,
3307 sep->rar_addr, sep->rar_bus);
3310 end_function_deallocate_sep_shared_area:
3311 /* De-allocate shared area */
3312 sep_unmap_and_free_shared_area(sep);
3315 iounmap(sep->reg_addr);
3317 end_function_free_sep_dev:
3318 pci_dev_put(sep_dev->pdev);
3322 end_function_disable_device:
3323 pci_disable_device(pdev);
3329 static void sep_remove(struct pci_dev *pdev)
3331 struct sep_device *sep = sep_dev;
3333 /* Unregister from fs */
3334 misc_deregister(&sep->miscdev_sep);
3335 misc_deregister(&sep->miscdev_singleton);
3336 misc_deregister(&sep->miscdev_daemon);
3339 free_irq(sep->pdev->irq, sep);
3341 /* Free the shared area */
3342 sep_unmap_and_free_shared_area(sep_dev);
3343 iounmap((void *) sep_dev->reg_addr);
3346 static DEFINE_PCI_DEVICE_TABLE(sep_pci_id_tbl) = {
3347 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MFLD_PCI_DEVICE_ID)},
3351 MODULE_DEVICE_TABLE(pci, sep_pci_id_tbl);
3353 /* Field for registering driver to PCI device */
3354 static struct pci_driver sep_pci_driver = {
3355 .name = "sep_sec_driver",
3356 .id_table = sep_pci_id_tbl,
3358 .remove = sep_remove
3363 * sep_init - init function
3365 * Module load time. Register the PCI device driver.
3367 static int __init sep_init(void)
3369 return pci_register_driver(&sep_pci_driver);
3374 * sep_exit - called to unload driver
3376 * Drop the misc devices then remove and unmap the various resources
3377 * that are not released by the driver remove method.
3379 static void __exit sep_exit(void)
3381 pci_unregister_driver(&sep_pci_driver);
3385 module_init(sep_init);
3386 module_exit(sep_exit);
3388 MODULE_LICENSE("GPL");