Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh...
[pandora-kernel.git] / drivers / staging / memrar / memrar_handler.c
1 /*
2  *      memrar_handler 1.0:  An Intel restricted access region handler device
3  *
4  *      Copyright (C) 2010 Intel Corporation. All rights reserved.
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of version 2 of the GNU General
8  *      Public License as published by the Free Software Foundation.
9  *
10  *      This program is distributed in the hope that it will be
11  *      useful, but WITHOUT ANY WARRANTY; without even the implied
12  *      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  *      PURPOSE.  See the GNU General Public License for more details.
14  *      You should have received a copy of the GNU General Public
15  *      License along with this program; if not, write to the Free
16  *      Software Foundation, Inc., 59 Temple Place - Suite 330,
17  *      Boston, MA  02111-1307, USA.
18  *      The full GNU General Public License is included in this
19  *      distribution in the file called COPYING.
20  *
21  * -------------------------------------------------------------------
22  *
23  *      Moorestown restricted access regions (RAR) provide isolated
24  *      areas of main memory that are only acceessible by authorized
25  *      devices.
26  *
27  *      The Intel Moorestown RAR handler module exposes a kernel space
28  *      RAR memory management mechanism.  It is essentially a
29  *      RAR-specific allocator.
30  *
31  *      Besides providing RAR buffer management, the RAR handler also
32  *      behaves in many ways like an OS virtual memory manager.  For
33  *      example, the RAR "handles" created by the RAR handler are
34  *      analogous to user space virtual addresses.
35  *
36  *      RAR memory itself is never accessed directly by the RAR
37  *      handler.
38  */
39
40 #include <linux/miscdevice.h>
41 #include <linux/fs.h>
42 #include <linux/slab.h>
43 #include <linux/kref.h>
44 #include <linux/mutex.h>
45 #include <linux/kernel.h>
46 #include <linux/uaccess.h>
47 #include <linux/mm.h>
48 #include <linux/ioport.h>
49 #include <linux/io.h>
50
51 #include "../rar_register/rar_register.h"
52
53 #include "memrar.h"
54 #include "memrar_allocator.h"
55
56
57 #define MEMRAR_VER "1.0"
58
59 /*
60  * Moorestown supports three restricted access regions.
61  *
62  * We only care about the first two, video and audio.  The third,
63  * reserved for Chaabi and the P-unit, will be handled by their
64  * respective drivers.
65  */
66 #define MRST_NUM_RAR 2
67
68 /* ---------------- -------------------- ------------------- */
69
70 /**
71  * struct memrar_buffer_info - struct that keeps track of all RAR buffers
72  * @list:       Linked list of memrar_buffer_info objects.
73  * @buffer:     Core RAR buffer information.
74  * @refcount:   Reference count.
75  * @owner:      File handle corresponding to process that reserved the
76  *              block of memory in RAR.  This will be zero for buffers
77  *              allocated by other drivers instead of by a user space
78  *              process.
79  *
80  * This structure encapsulates a link list of RAR buffers, as well as
81  * other characteristics specific to a given list node, such as the
82  * reference count on the corresponding RAR buffer.
83  */
84 struct memrar_buffer_info {
85         struct list_head list;
86         struct RAR_buffer buffer;
87         struct kref refcount;
88         struct file *owner;
89 };
90
91 /**
92  * struct memrar_rar_info - characteristics of a given RAR
93  * @base:       Base bus address of the RAR.
94  * @length:     Length of the RAR.
95  * @iobase:     Virtual address of RAR mapped into kernel.
96  * @allocator:  Allocator associated with the RAR.  Note the allocator
97  *              "capacity" may be smaller than the RAR length if the
98  *              length is not a multiple of the configured allocator
99  *              block size.
100  * @buffers:    Table that keeps track of all reserved RAR buffers.
101  * @lock:       Lock used to synchronize access to RAR-specific data
102  *              structures.
103  *
104  * Each RAR has an associated memrar_rar_info structure that describes
105  * where in memory the RAR is located, how large it is, and a list of
106  * reserved RAR buffers inside that RAR.  Each RAR also has a mutex
107  * associated with it to reduce lock contention when operations on
108  * multiple RARs are performed in parallel.
109  */
110 struct memrar_rar_info {
111         dma_addr_t base;
112         unsigned long length;
113         void __iomem *iobase;
114         struct memrar_allocator *allocator;
115         struct memrar_buffer_info buffers;
116         struct mutex lock;
117         int allocated;  /* True if we own this RAR */
118 };
119
120 /*
121  * Array of RAR characteristics.
122  */
123 static struct memrar_rar_info memrars[MRST_NUM_RAR];
124
125 /* ---------------- -------------------- ------------------- */
126
127 /* Validate RAR type. */
128 static inline int memrar_is_valid_rar_type(u32 type)
129 {
130         return type == RAR_TYPE_VIDEO || type == RAR_TYPE_AUDIO;
131 }
132
133 /* Check if an address/handle falls with the given RAR memory range. */
134 static inline int memrar_handle_in_range(struct memrar_rar_info *rar,
135                                          u32 vaddr)
136 {
137         unsigned long const iobase = (unsigned long) (rar->iobase);
138         return (vaddr >= iobase && vaddr < iobase + rar->length);
139 }
140
141 /* Retrieve RAR information associated with the given handle. */
142 static struct memrar_rar_info *memrar_get_rar_info(u32 vaddr)
143 {
144         int i;
145         for (i = 0; i < MRST_NUM_RAR; ++i) {
146                 struct memrar_rar_info * const rar = &memrars[i];
147                 if (memrar_handle_in_range(rar, vaddr))
148                         return rar;
149         }
150
151         return NULL;
152 }
153
154 /**
155  *      memrar_get_bus address          -       handle to bus address
156  *
157  *      Retrieve bus address from given handle.
158  *
159  *      Returns address corresponding to given handle.  Zero if handle is
160  *      invalid.
161  */
162 static dma_addr_t memrar_get_bus_address(
163         struct memrar_rar_info *rar,
164         u32 vaddr)
165 {
166         unsigned long const iobase = (unsigned long) (rar->iobase);
167
168         if (!memrar_handle_in_range(rar, vaddr))
169                 return 0;
170
171         /*
172          * An assumption is made that the virtual address offset is
173          * the same as the bus address offset, at least based on the
174          * way this driver is implemented.  For example, vaddr + 2 ==
175          * baddr + 2.
176          *
177          * @todo Is that a valid assumption?
178          */
179         return rar->base + (vaddr - iobase);
180 }
181
182 /**
183  *      memrar_get_physical_address     -       handle to physical address
184  *
185  *      Retrieve physical address from given handle.
186  *
187  *      Returns address corresponding to given handle.  Zero if handle is
188  *      invalid.
189  */
190 static dma_addr_t memrar_get_physical_address(
191         struct memrar_rar_info *rar,
192         u32 vaddr)
193 {
194         /*
195          * @todo This assumes that the bus address and physical
196          *       address are the same.  That is true for Moorestown
197          *       but not necessarily on other platforms.  This
198          *       deficiency should be addressed at some point.
199          */
200         return memrar_get_bus_address(rar, vaddr);
201 }
202
203 /**
204  *      memrar_release_block    -       release a block to the pool
205  *      @kref: kref of block
206  *
207  *      Core block release code. A node has hit zero references so can
208  *      be released and the lists must be updated.
209  *
210  *      Note: This code removes the node from a list.  Make sure any list
211  *      iteration is performed using list_for_each_safe().
212  */
213 static void memrar_release_block_i(struct kref *ref)
214 {
215         /*
216          * Last reference is being released.  Remove from the table,
217          * and reclaim resources.
218          */
219
220         struct memrar_buffer_info * const node =
221                 container_of(ref, struct memrar_buffer_info, refcount);
222
223         struct RAR_block_info * const user_info =
224                 &node->buffer.info;
225
226         struct memrar_allocator * const allocator =
227                 memrars[user_info->type].allocator;
228
229         list_del(&node->list);
230
231         memrar_allocator_free(allocator, user_info->handle);
232
233         kfree(node);
234 }
235
236 /**
237  *      memrar_init_rar_resources       -       configure a RAR
238  *      @rarnum: rar that has been allocated
239  *      @devname: name of our device
240  *
241  *      Initialize RAR parameters, such as bus addresses, etc and make
242  *      the resource accessible.
243  */
244 static int memrar_init_rar_resources(int rarnum, char const *devname)
245 {
246         /* ---- Sanity Checks ----
247          * 1. RAR bus addresses in both Lincroft and Langwell RAR
248          *    registers should be the same.
249          *    a. There's no way we can do this through IA.
250          *
251          * 2. Secure device ID in Langwell RAR registers should be set
252          *    appropriately, e.g. only LPE DMA for the audio RAR, and
253          *    security for the other Langwell based RAR registers.
254          *    a. There's no way we can do this through IA.
255          *
256          * 3. Audio and video RAR registers and RAR access should be
257          *    locked down.  If not, enable RAR access control.  Except
258          *    for debugging purposes, there is no reason for them to
259          *    be unlocked.
260          *    a.  We can only do this for the Lincroft (IA) side.
261          *
262          * @todo Should the RAR handler driver even be aware of audio
263          *       and video RAR settings?
264          */
265
266         /*
267          * RAR buffer block size.
268          *
269          * We choose it to be the size of a page to simplify the
270          * /dev/memrar mmap() implementation and usage.  Otherwise
271          * paging is not involved once an RAR is locked down.
272          */
273         static size_t const RAR_BLOCK_SIZE = PAGE_SIZE;
274
275         dma_addr_t low, high;
276         struct memrar_rar_info * const rar = &memrars[rarnum];
277
278         BUG_ON(MRST_NUM_RAR != ARRAY_SIZE(memrars));
279         BUG_ON(!memrar_is_valid_rar_type(rarnum));
280         BUG_ON(rar->allocated);
281
282         mutex_init(&rar->lock);
283
284         /*
285          * Initialize the process table before we reach any
286          * code that exit on failure since the finalization
287          * code requires an initialized list.
288          */
289         INIT_LIST_HEAD(&rar->buffers.list);
290
291         if (rar_get_address(rarnum, &low, &high) != 0)
292                 /* No RAR is available. */
293                 return -ENODEV;
294         
295         if (low == 0 || high == 0) {
296                 rar->base      = 0;
297                 rar->length    = 0;
298                 rar->iobase    = NULL;
299                 rar->allocator = NULL;
300                 return -ENOSPC;
301         }
302
303         /*
304          * @todo Verify that LNC and LNW RAR register contents
305          *       addresses, security, etc are compatible and
306          *       consistent).
307          */
308
309         rar->length = high - low + 1;
310
311         /* Claim RAR memory as our own. */
312         if (request_mem_region(low, rar->length, devname) == NULL) {
313                 rar->length = 0;
314                 pr_err("%s: Unable to claim RAR[%d] memory.\n", devname, rarnum);
315                 pr_err("%s: RAR[%d] disabled.\n", devname, rarnum);
316                 return -EBUSY;
317         }
318
319         rar->base = low;
320
321         /*
322          * Now map it into the kernel address space.
323          *
324          * Note that the RAR memory may only be accessed by IA
325          * when debugging.  Otherwise attempts to access the
326          * RAR memory when it is locked down will result in
327          * behavior similar to writing to /dev/null and
328          * reading from /dev/zero.  This behavior is enforced
329          * by the hardware.  Even if we don't access the
330          * memory, mapping it into the kernel provides us with
331          * a convenient RAR handle to bus address mapping.
332          */
333         rar->iobase = ioremap_nocache(rar->base, rar->length);
334         if (rar->iobase == NULL) {
335                 pr_err("%s: Unable to map RAR memory.\n", devname);
336                 release_mem_region(low, rar->length);
337                 return -ENOMEM;
338         }
339
340         /* Initialize corresponding memory allocator. */
341         rar->allocator = memrar_create_allocator((unsigned long) rar->iobase,
342                                                 rar->length, RAR_BLOCK_SIZE);
343         if (rar->allocator == NULL) {
344                 iounmap(rar->iobase);
345                 release_mem_region(low, rar->length);
346                 return -ENOMEM;
347         }
348
349         pr_info("%s: BRAR[%d] bus address range = [0x%lx, 0x%lx]\n",
350                         devname, rarnum, (unsigned long) low, (unsigned long) high);
351
352         pr_info("%s: BRAR[%d] size = %zu KiB\n",
353                         devname, rarnum, rar->allocator->capacity / 1024);
354
355         rar->allocated = 1;
356         return 0;
357 }
358
359 /**
360  *      memrar_fini_rar_resources       -       free up RAR resources
361  *
362  *      Finalize RAR resources. Free up the resource tables, hand the memory
363  *      back to the kernel, unmap the device and release the address space.
364  */
365 static void memrar_fini_rar_resources(void)
366 {
367         int z;
368         struct memrar_buffer_info *pos;
369         struct memrar_buffer_info *tmp;
370
371         /*
372          * @todo Do we need to hold a lock at this point in time?
373          *       (module initialization failure or exit?)
374          */
375
376         for (z = MRST_NUM_RAR; z-- != 0; ) {
377                 struct memrar_rar_info * const rar = &memrars[z];
378
379                 if (!rar->allocated)
380                         continue;
381
382                 /* Clean up remaining resources. */
383
384                 list_for_each_entry_safe(pos,
385                                          tmp,
386                                          &rar->buffers.list,
387                                          list) {
388                         kref_put(&pos->refcount, memrar_release_block_i);
389                 }
390
391                 memrar_destroy_allocator(rar->allocator);
392                 rar->allocator = NULL;
393
394                 iounmap(rar->iobase);
395                 release_mem_region(rar->base, rar->length);
396
397                 rar->iobase = NULL;
398                 rar->base = 0;
399                 rar->length = 0;
400
401                 unregister_rar(z);
402         }
403 }
404
405 /**
406  *      memrar_reserve_block    -       handle an allocation request
407  *      @request: block being requested
408  *      @filp: owner it is tied to
409  *
410  *      Allocate a block of the requested RAR. If successful return the
411  *      request object filled in and zero, if not report an error code
412  */
413
414 static long memrar_reserve_block(struct RAR_buffer *request,
415                                  struct file *filp)
416 {
417         struct RAR_block_info * const rinfo = &request->info;
418         struct RAR_buffer *buffer;
419         struct memrar_buffer_info *buffer_info;
420         u32 handle;
421         struct memrar_rar_info *rar = NULL;
422
423         /* Prevent array overflow. */
424         if (!memrar_is_valid_rar_type(rinfo->type))
425                 return -EINVAL;
426
427         rar = &memrars[rinfo->type];
428         if (!rar->allocated)
429                 return -ENODEV;
430
431         /* Reserve memory in RAR. */
432         handle = memrar_allocator_alloc(rar->allocator, rinfo->size);
433         if (handle == 0)
434                 return -ENOMEM;
435
436         buffer_info = kmalloc(sizeof(*buffer_info), GFP_KERNEL);
437
438         if (buffer_info == NULL) {
439                 memrar_allocator_free(rar->allocator, handle);
440                 return -ENOMEM;
441         }
442
443         buffer = &buffer_info->buffer;
444         buffer->info.type = rinfo->type;
445         buffer->info.size = rinfo->size;
446
447         /* Memory handle corresponding to the bus address. */
448         buffer->info.handle = handle;
449         buffer->bus_address = memrar_get_bus_address(rar, handle);
450
451         /*
452          * Keep track of owner so that we can later cleanup if
453          * necessary.
454          */
455         buffer_info->owner = filp;
456
457         kref_init(&buffer_info->refcount);
458
459         mutex_lock(&rar->lock);
460         list_add(&buffer_info->list, &rar->buffers.list);
461         mutex_unlock(&rar->lock);
462
463         rinfo->handle = buffer->info.handle;
464         request->bus_address = buffer->bus_address;
465
466         return 0;
467 }
468
469 /**
470  *      memrar_release_block            -       release a RAR block
471  *      @addr: address in RAR space
472  *
473  *      Release a previously allocated block. Releases act on complete
474  *      blocks, partially freeing a block is not supported
475  */
476
477 static long memrar_release_block(u32 addr)
478 {
479         struct memrar_buffer_info *pos;
480         struct memrar_buffer_info *tmp;
481         struct memrar_rar_info * const rar = memrar_get_rar_info(addr);
482         long result = -EINVAL;
483
484         if (rar == NULL)
485                 return -ENOENT;
486
487         mutex_lock(&rar->lock);
488
489         /*
490          * Iterate through the buffer list to find the corresponding
491          * buffer to be released.
492          */
493         list_for_each_entry_safe(pos,
494                                  tmp,
495                                  &rar->buffers.list,
496                                  list) {
497                 struct RAR_block_info * const info =
498                         &pos->buffer.info;
499
500                 /*
501                  * Take into account handle offsets that may have been
502                  * added to the base handle, such as in the following
503                  * scenario:
504                  *
505                  *     u32 handle = base + offset;
506                  *     rar_handle_to_bus(handle);
507                  *     rar_release(handle);
508                  */
509                 if (addr >= info->handle
510                     && addr < (info->handle + info->size)
511                     && memrar_is_valid_rar_type(info->type)) {
512                         kref_put(&pos->refcount, memrar_release_block_i);
513                         result = 0;
514                         break;
515                 }
516         }
517
518         mutex_unlock(&rar->lock);
519
520         return result;
521 }
522
523 /**
524  *      memrar_get_stats        -       read statistics for a RAR
525  *      @r: statistics to be filled in
526  *
527  *      Returns the statistics data for the RAR, or an error code if
528  *      the request cannot be completed
529  */
530 static long memrar_get_stat(struct RAR_stat *r)
531 {
532         struct memrar_allocator *allocator;
533
534         if (!memrar_is_valid_rar_type(r->type))
535                 return -EINVAL;
536
537         if (!memrars[r->type].allocated)
538                 return -ENODEV;
539
540         allocator = memrars[r->type].allocator;
541
542         BUG_ON(allocator == NULL);
543
544         /*
545          * Allocator capacity doesn't change over time.  No
546          * need to synchronize.
547          */
548         r->capacity = allocator->capacity;
549
550         mutex_lock(&allocator->lock);
551         r->largest_block_size = allocator->largest_free_area;
552         mutex_unlock(&allocator->lock);
553         return 0;
554 }
555
556 /**
557  *      memrar_ioctl            -       ioctl callback
558  *      @filp: file issuing the request
559  *      @cmd: command
560  *      @arg: pointer to control information
561  *
562  *      Perform one of the ioctls supported by the memrar device
563  */
564
565 static long memrar_ioctl(struct file *filp,
566                          unsigned int cmd,
567                          unsigned long arg)
568 {
569         void __user *argp = (void __user *)arg;
570         long result = 0;
571
572         struct RAR_buffer buffer;
573         struct RAR_block_info * const request = &buffer.info;
574         struct RAR_stat rar_info;
575         u32 rar_handle;
576
577         switch (cmd) {
578         case RAR_HANDLER_RESERVE:
579                 if (copy_from_user(request,
580                                    argp,
581                                    sizeof(*request)))
582                         return -EFAULT;
583
584                 result = memrar_reserve_block(&buffer, filp);
585                 if (result != 0)
586                         return result;
587
588                 return copy_to_user(argp, request, sizeof(*request));
589
590         case RAR_HANDLER_RELEASE:
591                 if (copy_from_user(&rar_handle,
592                                    argp,
593                                    sizeof(rar_handle)))
594                         return -EFAULT;
595
596                 return memrar_release_block(rar_handle);
597
598         case RAR_HANDLER_STAT:
599                 if (copy_from_user(&rar_info,
600                                    argp,
601                                    sizeof(rar_info)))
602                         return -EFAULT;
603
604                 /*
605                  * Populate the RAR_stat structure based on the RAR
606                  * type given by the user
607                  */
608                 if (memrar_get_stat(&rar_info) != 0)
609                         return -EINVAL;
610
611                 /*
612                  * @todo Do we need to verify destination pointer
613                  *       "argp" is non-zero?  Is that already done by
614                  *       copy_to_user()?
615                  */
616                 return copy_to_user(argp,
617                                     &rar_info,
618                                     sizeof(rar_info)) ? -EFAULT : 0;
619
620         default:
621                 return -ENOTTY;
622         }
623
624         return 0;
625 }
626
627 /**
628  *      memrar_mmap             -       mmap helper for deubgging
629  *      @filp: handle doing the mapping
630  *      @vma: memory area
631  *
632  *      Support the mmap operation on the RAR space for debugging systems
633  *      when the memory is not locked down.
634  */
635
636 static int memrar_mmap(struct file *filp, struct vm_area_struct *vma)
637 {
638         /*
639          * This mmap() implementation is predominantly useful for
640          * debugging since the CPU will be prevented from accessing
641          * RAR memory by the hardware when RAR is properly locked
642          * down.
643          *
644          * In order for this implementation to be useful RAR memory
645          * must be not be locked down.  However, we only want to do
646          * that when debugging.  DO NOT leave RAR memory unlocked in a
647          * deployed device that utilizes RAR.
648          */
649
650         size_t const size = vma->vm_end - vma->vm_start;
651
652         /* Users pass the RAR handle as the mmap() offset parameter. */
653         unsigned long const handle = vma->vm_pgoff << PAGE_SHIFT;
654
655         struct memrar_rar_info * const rar = memrar_get_rar_info(handle);
656         unsigned long pfn;
657
658         /* Only allow priviledged apps to go poking around this way */
659         if (!capable(CAP_SYS_RAWIO))
660                 return -EPERM;
661
662         /* Invalid RAR handle or size passed to mmap(). */
663         if (rar == NULL
664             || handle == 0
665             || size > (handle - (unsigned long) rar->iobase))
666                 return -EINVAL;
667
668         /*
669          * Retrieve physical address corresponding to the RAR handle,
670          * and convert it to a page frame.
671          */
672         pfn = memrar_get_physical_address(rar, handle) >> PAGE_SHIFT;
673
674
675         pr_debug("memrar: mapping RAR range [0x%lx, 0x%lx) into user space.\n",
676                  handle,
677                  handle + size);
678
679         /*
680          * Map RAR memory into user space.  This is really only useful
681          * for debugging purposes since the memory won't be
682          * accessible, i.e. reads return zero and writes are ignored,
683          * when RAR access control is enabled.
684          */
685         if (remap_pfn_range(vma,
686                             vma->vm_start,
687                             pfn,
688                             size,
689                             vma->vm_page_prot))
690                 return -EAGAIN;
691
692         /* vma->vm_ops = &memrar_mem_ops; */
693
694         return 0;
695 }
696
697 /**
698  *      memrar_open             -       device open method
699  *      @inode: inode to open
700  *      @filp: file handle
701  *
702  *      As we support multiple arbitary opens there is no work to be done
703  *      really.
704  */
705
706 static int memrar_open(struct inode *inode, struct file *filp)
707 {
708         nonseekable_open(inode, filp);
709         return 0;
710 }
711
712 /**
713  *      memrar_release          -       close method for miscev
714  *      @inode: inode of device
715  *      @filp: handle that is going away
716  *
717  *      Free up all the regions that belong to this file handle. We use
718  *      the handle as a natural Linux style 'lifetime' indicator and to
719  *      ensure resources are not leaked when their owner explodes in an
720  *      unplanned fashion.
721  */
722
723 static int memrar_release(struct inode *inode, struct file *filp)
724 {
725         /* Free all regions associated with the given file handle. */
726
727         struct memrar_buffer_info *pos;
728         struct memrar_buffer_info *tmp;
729         int z;
730
731         for (z = 0; z != MRST_NUM_RAR; ++z) {
732                 struct memrar_rar_info * const rar = &memrars[z];
733
734                 mutex_lock(&rar->lock);
735
736                 list_for_each_entry_safe(pos,
737                                          tmp,
738                                          &rar->buffers.list,
739                                          list) {
740                         if (filp == pos->owner)
741                                 kref_put(&pos->refcount,
742                                          memrar_release_block_i);
743                 }
744
745                 mutex_unlock(&rar->lock);
746         }
747
748         return 0;
749 }
750
751 /**
752  *      rar_reserve             -       reserve RAR memory
753  *      @buffers: buffers to reserve
754  *      @count: number wanted
755  *
756  *      Reserve a series of buffers in the RAR space. Returns the number of
757  *      buffers successfully allocated
758  */
759
760 size_t rar_reserve(struct RAR_buffer *buffers, size_t count)
761 {
762         struct RAR_buffer * const end =
763                 (buffers == NULL ? buffers : buffers + count);
764         struct RAR_buffer *i;
765
766         size_t reserve_count = 0;
767
768         for (i = buffers; i != end; ++i) {
769                 if (memrar_reserve_block(i, NULL) == 0)
770                         ++reserve_count;
771                 else
772                         i->bus_address = 0;
773         }
774
775         return reserve_count;
776 }
777 EXPORT_SYMBOL(rar_reserve);
778
779 /**
780  *      rar_release             -       return RAR buffers
781  *      @buffers: buffers to release
782  *      @size: size of released block
783  *
784  *      Return a set of buffers to the RAR pool
785  */
786
787 size_t rar_release(struct RAR_buffer *buffers, size_t count)
788 {
789         struct RAR_buffer * const end =
790                 (buffers == NULL ? buffers : buffers + count);
791         struct RAR_buffer *i;
792
793         size_t release_count = 0;
794
795         for (i = buffers; i != end; ++i) {
796                 u32 * const handle = &i->info.handle;
797                 if (memrar_release_block(*handle) == 0) {
798                         /*
799                          * @todo We assume we should do this each time
800                          *       the ref count is decremented.  Should
801                          *       we instead only do this when the ref
802                          *       count has dropped to zero, and the
803                          *       buffer has been completely
804                          *       released/unmapped?
805                          */
806                         *handle = 0;
807                         ++release_count;
808                 }
809         }
810
811         return release_count;
812 }
813 EXPORT_SYMBOL(rar_release);
814
815 /**
816  *      rar_handle_to_bus       -       RAR to bus address
817  *      @buffers: RAR buffer structure
818  *      @count: number of buffers to convert
819  *
820  *      Turn a list of RAR handle mappings into actual bus addresses. Note
821  *      that when the device is locked down the bus addresses in question
822  *      are not CPU accessible.
823  */
824
825 size_t rar_handle_to_bus(struct RAR_buffer *buffers, size_t count)
826 {
827         struct RAR_buffer * const end =
828                 (buffers == NULL ? buffers : buffers + count);
829         struct RAR_buffer *i;
830         struct memrar_buffer_info *pos;
831
832         size_t conversion_count = 0;
833
834         /*
835          * Find all bus addresses corresponding to the given handles.
836          *
837          * @todo Not liking this nested loop.  Optimize.
838          */
839         for (i = buffers; i != end; ++i) {
840                 struct memrar_rar_info * const rar =
841                         memrar_get_rar_info(i->info.handle);
842
843                 /*
844                  * Check if we have a bogus handle, and then continue
845                  * with remaining buffers.
846                  */
847                 if (rar == NULL) {
848                         i->bus_address = 0;
849                         continue;
850                 }
851
852                 mutex_lock(&rar->lock);
853
854                 list_for_each_entry(pos, &rar->buffers.list, list) {
855                         struct RAR_block_info * const user_info =
856                                 &pos->buffer.info;
857
858                         /*
859                          * Take into account handle offsets that may
860                          * have been added to the base handle, such as
861                          * in the following scenario:
862                          *
863                          *     u32 handle = base + offset;
864                          *     rar_handle_to_bus(handle);
865                          */
866
867                         if (i->info.handle >= user_info->handle
868                             && i->info.handle < (user_info->handle
869                                                  + user_info->size)) {
870                                 u32 const offset =
871                                         i->info.handle - user_info->handle;
872
873                                 i->info.type = user_info->type;
874                                 i->info.size = user_info->size - offset;
875                                 i->bus_address =
876                                         pos->buffer.bus_address
877                                         + offset;
878
879                                 /* Increment the reference count. */
880                                 kref_get(&pos->refcount);
881
882                                 ++conversion_count;
883                                 break;
884                         } else {
885                                 i->bus_address = 0;
886                         }
887                 }
888
889                 mutex_unlock(&rar->lock);
890         }
891
892         return conversion_count;
893 }
894 EXPORT_SYMBOL(rar_handle_to_bus);
895
896 static const struct file_operations memrar_fops = {
897         .owner = THIS_MODULE,
898         .unlocked_ioctl = memrar_ioctl,
899         .mmap           = memrar_mmap,
900         .open           = memrar_open,
901         .release        = memrar_release,
902 };
903
904 static struct miscdevice memrar_miscdev = {
905         .minor = MISC_DYNAMIC_MINOR,    /* dynamic allocation */
906         .name = "memrar",               /* /dev/memrar */
907         .fops = &memrar_fops
908 };
909
910 static char const banner[] __initdata =
911         KERN_INFO
912         "Intel RAR Handler: " MEMRAR_VER " initialized.\n";
913
914 /**
915  *      memrar_registration_callback    -       RAR obtained
916  *      @rar: RAR number
917  *
918  *      We have been granted ownership of the RAR. Add it to our memory
919  *      management tables
920  */
921
922 static int memrar_registration_callback(unsigned long rar)
923 {
924         /*
925          * We initialize the RAR parameters early on so that we can
926          * discontinue memrar device initialization and registration
927          * if suitably configured RARs are not available.
928          */
929         return memrar_init_rar_resources(rar, memrar_miscdev.name);
930 }
931
932 /**
933  *      memrar_init     -       initialise RAR support
934  *
935  *      Initialise support for RAR handlers. This may get loaded before
936  *      the RAR support is activated, but the callbacks on the registration
937  *      will handle that situation for us anyway.
938  */
939
940 static int __init memrar_init(void)
941 {
942         int err;
943
944         printk(banner);
945
946         err = misc_register(&memrar_miscdev);
947         if (err)
948                 return err;
949
950         /* Now claim the two RARs we want */
951         err = register_rar(0, memrar_registration_callback, 0);
952         if (err)
953                 goto fail;
954
955         err = register_rar(1, memrar_registration_callback, 1);
956         if (err == 0)
957                 return 0;
958
959         /* It is possible rar 0 registered and allocated resources then rar 1
960            failed so do a full resource free */
961         memrar_fini_rar_resources();
962 fail:
963         misc_deregister(&memrar_miscdev);
964         return err;
965 }
966
967 /**
968  *      memrar_exit     -       unregister and unload
969  *
970  *      Unregister the device and then unload any mappings and release
971  *      the RAR resources
972  */
973
974 static void __exit memrar_exit(void)
975 {
976         misc_deregister(&memrar_miscdev);
977         memrar_fini_rar_resources();
978 }
979
980
981 module_init(memrar_init);
982 module_exit(memrar_exit);
983
984
985 MODULE_AUTHOR("Ossama Othman <ossama.othman@intel.com>");
986 MODULE_DESCRIPTION("Intel Restricted Access Region Handler");
987 MODULE_LICENSE("GPL");
988 MODULE_VERSION(MEMRAR_VER);
989
990
991
992 /*
993   Local Variables:
994     c-file-style: "linux"
995   End:
996 */