tegra: Add nvmap_mark_global()
[linux-2.6.git] / drivers / video / tegra / nvmap / nvmap.c
1 /*
2  * drivers/video/tegra/nvmap/nvmap.c
3  *
4  * Memory manager for Tegra GPU
5  *
6  * Copyright (c) 2009-2012, NVIDIA Corporation.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #include <linux/err.h>
24 #include <linux/highmem.h>
25 #include <linux/io.h>
26 #include <linux/rbtree.h>
27 #include <linux/vmalloc.h>
28 #include <linux/wait.h>
29 #include <linux/slab.h>
30
31 #include <asm/pgtable.h>
32 #include <asm/tlbflush.h>
33
34 #include <mach/iovmm.h>
35 #include <linux/nvmap.h>
36
37 #include "nvmap.h"
38 #include "nvmap_mru.h"
39
40 /* private nvmap_handle flag for pinning duplicate detection */
41 #define NVMAP_HANDLE_VISITED (0x1ul << 31)
42
43 /* map the backing pages for a heap_pgalloc handle into its IOVMM area */
44 static void map_iovmm_area(struct nvmap_handle *h)
45 {
46         tegra_iovmm_addr_t va;
47         unsigned long i;
48
49         BUG_ON(!h->heap_pgalloc || !h->pgalloc.area);
50         BUG_ON(h->size & ~PAGE_MASK);
51         WARN_ON(!h->pgalloc.dirty);
52
53         for (va = h->pgalloc.area->iovm_start, i = 0;
54              va < (h->pgalloc.area->iovm_start + h->size);
55              i++, va += PAGE_SIZE) {
56                 BUG_ON(!pfn_valid(page_to_pfn(h->pgalloc.pages[i])));
57                 tegra_iovmm_vm_insert_pfn(h->pgalloc.area, va,
58                                           page_to_pfn(h->pgalloc.pages[i]));
59         }
60         h->pgalloc.dirty = false;
61 }
62
63 /* must be called inside nvmap_pin_lock, to ensure that an entire stream
64  * of pins will complete without racing with a second stream. handle should
65  * have nvmap_handle_get (or nvmap_validate_get) called before calling
66  * this function. */
67 static int pin_locked(struct nvmap_client *client, struct nvmap_handle *h)
68 {
69         struct tegra_iovmm_area *area;
70         BUG_ON(!h->alloc);
71
72         nvmap_mru_lock(client->share);
73         if (atomic_inc_return(&h->pin) == 1) {
74                 if (h->heap_pgalloc && !h->pgalloc.contig) {
75                         area = nvmap_handle_iovmm_locked(client, h);
76                         if (!area) {
77                                 /* no race here, inside the pin mutex */
78                                 atomic_dec(&h->pin);
79                                 nvmap_mru_unlock(client->share);
80                                 return -ENOMEM;
81                         }
82                         if (area != h->pgalloc.area)
83                                 h->pgalloc.dirty = true;
84                         h->pgalloc.area = area;
85                 }
86         }
87         nvmap_mru_unlock(client->share);
88         return 0;
89 }
90
91 /* doesn't need to be called inside nvmap_pin_lock, since this will only
92  * expand the available VM area */
93 static int handle_unpin(struct nvmap_client *client,
94                 struct nvmap_handle *h, int free_vm)
95 {
96         int ret = 0;
97         nvmap_mru_lock(client->share);
98
99         if (atomic_read(&h->pin) == 0) {
100                 nvmap_err(client, "%s unpinning unpinned handle %p\n",
101                           current->group_leader->comm, h);
102                 nvmap_mru_unlock(client->share);
103                 return 0;
104         }
105
106         BUG_ON(!h->alloc);
107
108         if (!atomic_dec_return(&h->pin)) {
109                 if (h->heap_pgalloc && h->pgalloc.area) {
110                         /* if a secure handle is clean (i.e., mapped into
111                          * IOVMM, it needs to be zapped on unpin. */
112                         if (h->secure && !h->pgalloc.dirty) {
113                                 tegra_iovmm_zap_vm(h->pgalloc.area);
114                                 h->pgalloc.dirty = true;
115                         }
116                         if (free_vm) {
117                                 tegra_iovmm_free_vm(h->pgalloc.area);
118                                 h->pgalloc.area = NULL;
119                         } else
120                                 nvmap_mru_insert_locked(client->share, h);
121                         ret = 1;
122                 }
123         }
124
125         nvmap_mru_unlock(client->share);
126         nvmap_handle_put(h);
127         return ret;
128 }
129
130 static int pin_array_locked(struct nvmap_client *client,
131                 struct nvmap_handle **h, int count)
132 {
133         int pinned;
134         int i;
135         int err = 0;
136
137         for (pinned = 0; pinned < count; pinned++) {
138                 err = pin_locked(client, h[pinned]);
139                 if (err)
140                         break;
141         }
142
143         if (err) {
144                 /* unpin pinned handles */
145                 for (i = 0; i < pinned; i++) {
146                         /* inc ref counter, because
147                          * handle_unpin decrements it */
148                         nvmap_handle_get(h[i]);
149                         /* unpin handles and free vm */
150                         handle_unpin(client, h[i], true);
151                 }
152         }
153
154         if (err && tegra_iovmm_get_max_free(client->share->iovmm) >=
155                                                         client->iovm_limit) {
156                 /* First attempt to pin in empty iovmm
157                  * may still fail because of fragmentation caused by
158                  * placing handles in MRU areas. After such failure
159                  * all MRU gets cleaned and iovm space is freed.
160                  *
161                  * We have to do pinning again here since there might be is
162                  * no more incoming pin_wait wakeup calls from unpin
163                  * operations */
164                 for (pinned = 0; pinned < count; pinned++) {
165                         err = pin_locked(client, h[pinned]);
166                         if (err)
167                                 break;
168                 }
169                 if (err) {
170                         pr_err("Pinning in empty iovmm failed!!!\n");
171                         BUG_ON(1);
172                 }
173         }
174         return err;
175 }
176
177 static int wait_pin_array_locked(struct nvmap_client *client,
178                 struct nvmap_handle **h, int count)
179 {
180         int ret = 0;
181
182         ret = pin_array_locked(client, h, count);
183
184         if (ret) {
185                 ret = wait_event_interruptible(client->share->pin_wait,
186                                 !pin_array_locked(client, h, count));
187         }
188         return ret ? -EINTR : 0;
189 }
190
191 static int handle_unpin_noref(struct nvmap_client *client, unsigned long id)
192 {
193         struct nvmap_handle *h;
194         int w;
195
196         h = nvmap_validate_get(client, id);
197         if (unlikely(!h)) {
198                 nvmap_err(client, "%s attempting to unpin invalid handle %p\n",
199                           current->group_leader->comm, (void *)id);
200                 return 0;
201         }
202
203         nvmap_err(client, "%s unpinning unreferenced handle %p\n",
204                   current->group_leader->comm, h);
205         WARN_ON(1);
206
207         w = handle_unpin(client, h, false);
208         nvmap_handle_put(h);
209         return w;
210 }
211
212 void nvmap_unpin_ids(struct nvmap_client *client,
213                      unsigned int nr, const unsigned long *ids)
214 {
215         unsigned int i;
216         int do_wake = 0;
217
218         for (i = 0; i < nr; i++) {
219                 struct nvmap_handle_ref *ref;
220
221                 if (!ids[i])
222                         continue;
223
224                 nvmap_ref_lock(client);
225                 ref = _nvmap_validate_id_locked(client, ids[i]);
226                 if (ref) {
227                         struct nvmap_handle *h = ref->handle;
228                         int e = atomic_add_unless(&ref->pin, -1, 0);
229
230                         nvmap_ref_unlock(client);
231
232                         if (!e) {
233                                 nvmap_err(client, "%s unpinning unpinned "
234                                           "handle %08lx\n",
235                                           current->group_leader->comm, ids[i]);
236                         } else {
237                                 do_wake |= handle_unpin(client, h, false);
238                         }
239                 } else {
240                         nvmap_ref_unlock(client);
241                         if (client->super)
242                                 do_wake |= handle_unpin_noref(client, ids[i]);
243                         else
244                                 nvmap_err(client, "%s unpinning invalid "
245                                           "handle %08lx\n",
246                                           current->group_leader->comm, ids[i]);
247                 }
248         }
249
250         if (do_wake)
251                 wake_up(&client->share->pin_wait);
252 }
253
254 /* pins a list of handle_ref objects; same conditions apply as to
255  * _nvmap_handle_pin, but also bumps the pin count of each handle_ref. */
256 int nvmap_pin_ids(struct nvmap_client *client,
257                   unsigned int nr, const unsigned long *ids)
258 {
259         int ret = 0;
260         unsigned int i;
261         struct nvmap_handle **h = (struct nvmap_handle **)ids;
262         struct nvmap_handle_ref *ref;
263
264         /* to optimize for the common case (client provided valid handle
265          * references and the pin succeeds), increment the handle_ref pin
266          * count during validation. in error cases, the tree will need to
267          * be re-walked, since the handle_ref is discarded so that an
268          * allocation isn't required. if a handle_ref is not found,
269          * locally validate that the caller has permission to pin the handle;
270          * handle_refs are not created in this case, so it is possible that
271          * if the caller crashes after pinning a global handle, the handle
272          * will be permanently leaked. */
273         nvmap_ref_lock(client);
274         for (i = 0; i < nr; i++) {
275                 ref = _nvmap_validate_id_locked(client, ids[i]);
276                 if (ref) {
277                         atomic_inc(&ref->pin);
278                         nvmap_handle_get(h[i]);
279                 } else {
280                         struct nvmap_handle *verify;
281                         nvmap_ref_unlock(client);
282                         verify = nvmap_validate_get(client, ids[i]);
283                         if (verify) {
284                                 nvmap_warn(client, "%s pinning unreferenced "
285                                            "handle %p\n",
286                                            current->group_leader->comm, h[i]);
287                         } else {
288                                 h[i] = NULL;
289                                 ret = -EPERM;
290                         }
291                         nvmap_ref_lock(client);
292                 }
293         }
294         nvmap_ref_unlock(client);
295
296         if (ret)
297                 goto out;
298
299         ret = mutex_lock_interruptible(&client->share->pin_lock);
300         if (WARN_ON(ret))
301                 goto out;
302
303         ret = wait_pin_array_locked(client, h, nr);
304
305         mutex_unlock(&client->share->pin_lock);
306
307         if (ret) {
308                 ret = -EINTR;
309         } else {
310                 for (i = 0; i < nr; i++) {
311                         if (h[i]->heap_pgalloc && h[i]->pgalloc.dirty)
312                                 map_iovmm_area(h[i]);
313                 }
314         }
315
316 out:
317         if (ret) {
318                 nvmap_ref_lock(client);
319                 for (i = 0; i < nr; i++) {
320                         if(!ids[i])
321                                 continue;
322
323                         ref = _nvmap_validate_id_locked(client, ids[i]);
324                         if (!ref) {
325                                 nvmap_warn(client, "%s freed handle %p "
326                                            "during pinning\n",
327                                            current->group_leader->comm,
328                                            (void *)ids[i]);
329                                 continue;
330                         }
331                         atomic_dec(&ref->pin);
332                 }
333                 nvmap_ref_unlock(client);
334
335                 for (i = 0; i < nr; i++)
336                         if(h[i])
337                                 nvmap_handle_put(h[i]);
338         }
339
340         return ret;
341 }
342
343 static phys_addr_t handle_phys(struct nvmap_handle *h)
344 {
345         phys_addr_t addr;
346
347         if (h->heap_pgalloc && h->pgalloc.contig) {
348                 addr = page_to_phys(h->pgalloc.pages[0]);
349         } else if (h->heap_pgalloc) {
350                 BUG_ON(!h->pgalloc.area);
351                 addr = h->pgalloc.area->iovm_start;
352         } else {
353                 addr = h->carveout->base;
354         }
355
356         return addr;
357 }
358
359 phys_addr_t nvmap_pin(struct nvmap_client *client,
360                         struct nvmap_handle_ref *ref)
361 {
362         struct nvmap_handle *h;
363         phys_addr_t phys;
364         int ret = 0;
365
366         h = nvmap_handle_get(ref->handle);
367         if (WARN_ON(!h))
368                 return -EINVAL;
369
370         atomic_inc(&ref->pin);
371
372         if (WARN_ON(mutex_lock_interruptible(&client->share->pin_lock))) {
373                 ret = -EINTR;
374         } else {
375                 ret = wait_pin_array_locked(client, &h, 1);
376                 mutex_unlock(&client->share->pin_lock);
377         }
378
379         if (ret) {
380                 atomic_dec(&ref->pin);
381                 nvmap_handle_put(h);
382         } else {
383                 if (h->heap_pgalloc && h->pgalloc.dirty)
384                         map_iovmm_area(h);
385                 phys = handle_phys(h);
386         }
387
388         return ret ?: phys;
389 }
390
391 phys_addr_t nvmap_handle_address(struct nvmap_client *c, unsigned long id)
392 {
393         struct nvmap_handle *h;
394         phys_addr_t phys;
395
396         h = nvmap_get_handle_id(c, id);
397         if (!h)
398                 return -EPERM;
399         mutex_lock(&h->lock);
400         phys = handle_phys(h);
401         mutex_unlock(&h->lock);
402         nvmap_handle_put(h);
403
404         return phys;
405 }
406
407 void nvmap_unpin(struct nvmap_client *client, struct nvmap_handle_ref *ref)
408 {
409         if (!ref)
410                 return;
411
412         atomic_dec(&ref->pin);
413         if (handle_unpin(client, ref->handle, false))
414                 wake_up(&client->share->pin_wait);
415 }
416
417 void nvmap_unpin_handles(struct nvmap_client *client,
418                          struct nvmap_handle **h, int nr)
419 {
420         int i;
421         int do_wake = 0;
422
423         for (i = 0; i < nr; i++) {
424                 if (WARN_ON(!h[i]))
425                         continue;
426                 do_wake |= handle_unpin(client, h[i], false);
427         }
428
429         if (do_wake)
430                 wake_up(&client->share->pin_wait);
431 }
432
433 void *nvmap_mmap(struct nvmap_handle_ref *ref)
434 {
435         struct nvmap_handle *h;
436         pgprot_t prot;
437         unsigned long adj_size;
438         unsigned long offs;
439         struct vm_struct *v;
440         void *p;
441
442         h = nvmap_handle_get(ref->handle);
443         if (!h)
444                 return NULL;
445
446         prot = nvmap_pgprot(h, pgprot_kernel);
447
448         if (h->heap_pgalloc)
449                 return vm_map_ram(h->pgalloc.pages, h->size >> PAGE_SHIFT,
450                                   -1, prot);
451
452         /* carveout - explicitly map the pfns into a vmalloc area */
453
454         nvmap_usecount_inc(h);
455
456         adj_size = h->carveout->base & ~PAGE_MASK;
457         adj_size += h->size;
458         adj_size = PAGE_ALIGN(adj_size);
459
460         v = alloc_vm_area(adj_size);
461         if (!v) {
462                 nvmap_usecount_dec(h);
463                 nvmap_handle_put(h);
464                 return NULL;
465         }
466
467         p = v->addr + (h->carveout->base & ~PAGE_MASK);
468
469         for (offs = 0; offs < adj_size; offs += PAGE_SIZE) {
470                 unsigned long addr = (unsigned long) v->addr + offs;
471                 unsigned int pfn;
472                 pgd_t *pgd;
473                 pud_t *pud;
474                 pmd_t *pmd;
475                 pte_t *pte;
476
477                 pfn = __phys_to_pfn(h->carveout->base + offs);
478                 pgd = pgd_offset_k(addr);
479                 pud = pud_alloc(&init_mm, pgd, addr);
480                 if (!pud)
481                         break;
482                 pmd = pmd_alloc(&init_mm, pud, addr);
483                 if (!pmd)
484                         break;
485                 pte = pte_alloc_kernel(pmd, addr);
486                 if (!pte)
487                         break;
488                 set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
489                 flush_tlb_kernel_page(addr);
490         }
491
492         if (offs != adj_size) {
493                 free_vm_area(v);
494                 nvmap_usecount_dec(h);
495                 nvmap_handle_put(h);
496                 return NULL;
497         }
498
499         /* leave the handle ref count incremented by 1, so that
500          * the handle will not be freed while the kernel mapping exists.
501          * nvmap_handle_put will be called by unmapping this address */
502         return p;
503 }
504
505 void nvmap_munmap(struct nvmap_handle_ref *ref, void *addr)
506 {
507         struct nvmap_handle *h;
508
509         if (!ref)
510                 return;
511
512         h = ref->handle;
513
514         if (h->heap_pgalloc) {
515                 vm_unmap_ram(addr, h->size >> PAGE_SHIFT);
516         } else {
517                 struct vm_struct *vm;
518                 addr -= (h->carveout->base & ~PAGE_MASK);
519                 vm = remove_vm_area(addr);
520                 BUG_ON(!vm);
521                 kfree(vm);
522                 nvmap_usecount_dec(h);
523         }
524         nvmap_handle_put(h);
525 }
526
527 struct nvmap_handle_ref *nvmap_alloc(struct nvmap_client *client, size_t size,
528                                      size_t align, unsigned int flags,
529                                      unsigned int heap_mask)
530 {
531         const unsigned int default_heap = (NVMAP_HEAP_SYSMEM |
532                                            NVMAP_HEAP_CARVEOUT_GENERIC);
533         struct nvmap_handle_ref *r = NULL;
534         int err;
535
536         if (heap_mask == 0)
537                 heap_mask = default_heap;
538
539         r = nvmap_create_handle(client, size);
540         if (IS_ERR(r))
541                 return r;
542
543         err = nvmap_alloc_handle_id(client, nvmap_ref_to_id(r),
544                                     heap_mask, align, flags);
545
546         if (err) {
547                 nvmap_free_handle_id(client, nvmap_ref_to_id(r));
548                 return ERR_PTR(err);
549         }
550
551         return r;
552 }
553
554 /* allocates memory with specifed iovm_start address. */
555 struct nvmap_handle_ref *nvmap_alloc_iovm(struct nvmap_client *client,
556         size_t size, size_t align, unsigned int flags, unsigned int iovm_start)
557 {
558         int err;
559         struct nvmap_handle *h;
560         struct nvmap_handle_ref *r;
561         const unsigned int default_heap = NVMAP_HEAP_IOVMM;
562
563         /* size need to be more than one page.
564          * otherwise heap preference would change to system heap.
565          */
566         if (size <= PAGE_SIZE)
567                 size = PAGE_SIZE << 1;
568         r = nvmap_create_handle(client, size);
569         if (IS_ERR_OR_NULL(r))
570                 return r;
571
572         h = r->handle;
573         h->pgalloc.iovm_addr = iovm_start;
574         err = nvmap_alloc_handle_id(client, nvmap_ref_to_id(r),
575                         default_heap, align, flags);
576         if (err)
577                 goto fail;
578
579         err = mutex_lock_interruptible(&client->share->pin_lock);
580         if (WARN_ON(err))
581                 goto fail;
582         err = pin_locked(client, h);
583         mutex_unlock(&client->share->pin_lock);
584         if (err)
585                 goto fail;
586         return r;
587
588 fail:
589         nvmap_free_handle_id(client, nvmap_ref_to_id(r));
590         return ERR_PTR(err);
591 }
592
593 void nvmap_free_iovm(struct nvmap_client *client, struct nvmap_handle_ref *r)
594 {
595         unsigned long ref_id = nvmap_ref_to_id(r);
596
597         nvmap_unpin_ids(client, 1, &ref_id);
598         nvmap_free_handle_id(client, ref_id);
599 }
600
601 void nvmap_free(struct nvmap_client *client, struct nvmap_handle_ref *r)
602 {
603         if (!r)
604                 return;
605
606         nvmap_free_handle_id(client, nvmap_ref_to_id(r));
607 }
608
609 int nvmap_mark_global(struct nvmap_client *client, struct nvmap_handle_ref *r)
610 {
611         struct nvmap_handle *h;
612
613         h = nvmap_get_handle_id(client, (unsigned long)r->handle);
614         if (!h)
615                 return -EINVAL;
616         r->handle->global = true;
617         nvmap_handle_put(h);
618
619         return 0;
620 }