blob: 7ed08fdc4c4285eff29f109ce67de4b79e970de2 [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_gem.c
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob.clark@linaro.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Rob Clarkcd5351f2011-11-12 12:09:40 -060020#include <linux/shmem_fs.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020021#include <linux/spinlock.h>
22
David Herrmann0de23972013-07-24 21:07:52 +020023#include <drm/drm_vma_manager.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060024
25#include "omap_drv.h"
Rob Clarkf7f9f452011-12-05 19:19:22 -060026#include "omap_dmm_tiler.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060027
28/* remove these once drm core helpers are merged */
YAMANE Toshiaki801d5bc2012-11-14 19:32:56 +090029struct page **_drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
Rob Clarkcd5351f2011-11-12 12:09:40 -060030void _drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
31 bool dirty, bool accessed);
Rob Clarkf7f9f452011-12-05 19:19:22 -060032int _drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
Rob Clarkcd5351f2011-11-12 12:09:40 -060033
34/*
35 * GEM buffer object implementation.
36 */
37
38#define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
39
40/* note: we use upper 8 bits of flags for driver-internal flags: */
41#define OMAP_BO_DMA 0x01000000 /* actually is physically contiguous */
42#define OMAP_BO_EXT_SYNC 0x02000000 /* externally allocated sync object */
43#define OMAP_BO_EXT_MEM 0x04000000 /* externally allocated memory */
44
45
46struct omap_gem_object {
47 struct drm_gem_object base;
48
Rob Clarkf6b60362012-03-05 10:48:36 -060049 struct list_head mm_list;
50
Rob Clarkcd5351f2011-11-12 12:09:40 -060051 uint32_t flags;
52
Rob Clarkf7f9f452011-12-05 19:19:22 -060053 /** width/height for tiled formats (rounded up to slot boundaries) */
54 uint16_t width, height;
55
Rob Clarka6a91822011-12-09 23:26:08 -060056 /** roll applied when mapping to DMM */
57 uint32_t roll;
58
Rob Clarkcd5351f2011-11-12 12:09:40 -060059 /**
60 * If buffer is allocated physically contiguous, the OMAP_BO_DMA flag
Rob Clarkf7f9f452011-12-05 19:19:22 -060061 * is set and the paddr is valid. Also if the buffer is remapped in
62 * TILER and paddr_cnt > 0, then paddr is valid. But if you are using
63 * the physical address and OMAP_BO_DMA is not set, then you should
64 * be going thru omap_gem_{get,put}_paddr() to ensure the mapping is
65 * not removed from under your feet.
Rob Clarkcd5351f2011-11-12 12:09:40 -060066 *
67 * Note that OMAP_BO_SCANOUT is a hint from userspace that DMA capable
68 * buffer is requested, but doesn't mean that it is. Use the
69 * OMAP_BO_DMA flag to determine if the buffer has a DMA capable
70 * physical address.
71 */
72 dma_addr_t paddr;
73
74 /**
Rob Clarkf7f9f452011-12-05 19:19:22 -060075 * # of users of paddr
76 */
77 uint32_t paddr_cnt;
78
79 /**
80 * tiler block used when buffer is remapped in DMM/TILER.
81 */
82 struct tiler_block *block;
83
84 /**
Rob Clarkcd5351f2011-11-12 12:09:40 -060085 * Array of backing pages, if allocated. Note that pages are never
86 * allocated for buffers originally allocated from contiguous memory
87 */
88 struct page **pages;
89
Rob Clarkf3bc9d22011-12-20 16:54:28 -060090 /** addresses corresponding to pages in above array */
91 dma_addr_t *addrs;
92
Rob Clarkcd5351f2011-11-12 12:09:40 -060093 /**
94 * Virtual address, if mapped.
95 */
96 void *vaddr;
97
98 /**
99 * sync-object allocated on demand (if needed)
100 *
101 * Per-buffer sync-object for tracking pending and completed hw/dma
102 * read and write operations. The layout in memory is dictated by
103 * the SGX firmware, which uses this information to stall the command
104 * stream if a surface is not ready yet.
105 *
106 * Note that when buffer is used by SGX, the sync-object needs to be
107 * allocated from a special heap of sync-objects. This way many sync
108 * objects can be packed in a page, and not waste GPU virtual address
109 * space. Because of this we have to have a omap_gem_set_sync_object()
110 * API to allow replacement of the syncobj after it has (potentially)
111 * already been allocated. A bit ugly but I haven't thought of a
112 * better alternative.
113 */
114 struct {
115 uint32_t write_pending;
116 uint32_t write_complete;
117 uint32_t read_pending;
118 uint32_t read_complete;
119 } *sync;
120};
121
Rob Clarkc5b12472012-01-18 18:33:02 -0600122static int get_pages(struct drm_gem_object *obj, struct page ***pages);
123static uint64_t mmap_offset(struct drm_gem_object *obj);
124
Rob Clarkf7f9f452011-12-05 19:19:22 -0600125/* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
126 * not necessarily pinned in TILER all the time, and (b) when they are
127 * they are not necessarily page aligned, we reserve one or more small
128 * regions in each of the 2d containers to use as a user-GART where we
129 * can create a second page-aligned mapping of parts of the buffer
130 * being accessed from userspace.
131 *
132 * Note that we could optimize slightly when we know that multiple
133 * tiler containers are backed by the same PAT.. but I'll leave that
134 * for later..
135 */
136#define NUM_USERGART_ENTRIES 2
137struct usergart_entry {
138 struct tiler_block *block; /* the reserved tiler block */
139 dma_addr_t paddr;
140 struct drm_gem_object *obj; /* the current pinned obj */
141 pgoff_t obj_pgoff; /* page offset of obj currently
142 mapped in */
143};
144static struct {
145 struct usergart_entry entry[NUM_USERGART_ENTRIES];
146 int height; /* height in rows */
147 int height_shift; /* ilog2(height in rows) */
148 int slot_shift; /* ilog2(width per slot) */
149 int stride_pfn; /* stride in pages */
150 int last; /* index of last used entry */
151} *usergart;
152
153static void evict_entry(struct drm_gem_object *obj,
154 enum tiler_fmt fmt, struct usergart_entry *entry)
155{
David Herrmann6796cb12014-01-03 14:24:19 +0100156 struct omap_gem_object *omap_obj = to_omap_bo(obj);
157 int n = usergart[fmt].height;
158 size_t size = PAGE_SIZE * n;
159 loff_t off = mmap_offset(obj) +
160 (entry->obj_pgoff << PAGE_SHIFT);
161 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
162
163 if (m > 1) {
164 int i;
165 /* if stride > than PAGE_SIZE then sparse mapping: */
166 for (i = n; i > 0; i--) {
167 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
168 off, PAGE_SIZE, 1);
169 off += PAGE_SIZE * m;
Rob Clarke5598952012-03-05 10:48:40 -0600170 }
David Herrmann6796cb12014-01-03 14:24:19 +0100171 } else {
172 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
173 off, size, 1);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600174 }
175
176 entry->obj = NULL;
177}
178
179/* Evict a buffer from usergart, if it is mapped there */
180static void evict(struct drm_gem_object *obj)
181{
182 struct omap_gem_object *omap_obj = to_omap_bo(obj);
183
184 if (omap_obj->flags & OMAP_BO_TILED) {
185 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
186 int i;
187
188 if (!usergart)
189 return;
190
191 for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
192 struct usergart_entry *entry = &usergart[fmt].entry[i];
193 if (entry->obj == obj)
194 evict_entry(obj, fmt, entry);
195 }
196 }
197}
198
Rob Clarkcd5351f2011-11-12 12:09:40 -0600199/* GEM objects can either be allocated from contiguous memory (in which
200 * case obj->filp==NULL), or w/ shmem backing (obj->filp!=NULL). But non
201 * contiguous buffers can be remapped in TILER/DMM if they need to be
202 * contiguous... but we don't do this all the time to reduce pressure
203 * on TILER/DMM space when we know at allocation time that the buffer
204 * will need to be scanned out.
205 */
206static inline bool is_shmem(struct drm_gem_object *obj)
207{
208 return obj->filp != NULL;
209}
210
Rob Clark8b6b5692012-05-17 02:37:25 -0600211/**
212 * shmem buffers that are mapped cached can simulate coherency via using
213 * page faulting to keep track of dirty pages
214 */
215static inline bool is_cached_coherent(struct drm_gem_object *obj)
216{
217 struct omap_gem_object *omap_obj = to_omap_bo(obj);
218 return is_shmem(obj) &&
219 ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED);
220}
221
Rob Clarkcd5351f2011-11-12 12:09:40 -0600222static DEFINE_SPINLOCK(sync_lock);
223
224/** ensure backing pages are allocated */
225static int omap_gem_attach_pages(struct drm_gem_object *obj)
226{
Rob Clark8b6b5692012-05-17 02:37:25 -0600227 struct drm_device *dev = obj->dev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600228 struct omap_gem_object *omap_obj = to_omap_bo(obj);
229 struct page **pages;
Emil Gooded4eb23a2012-08-17 18:53:26 +0200230 int npages = obj->size >> PAGE_SHIFT;
231 int i, ret;
Rob Clark8b6b5692012-05-17 02:37:25 -0600232 dma_addr_t *addrs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600233
234 WARN_ON(omap_obj->pages);
235
David Herrmann0cdbe8a2014-05-25 12:59:47 +0200236 pages = drm_gem_get_pages(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600237 if (IS_ERR(pages)) {
238 dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
239 return PTR_ERR(pages);
240 }
241
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600242 /* for non-cached buffers, ensure the new pages are clean because
243 * DSS, GPU, etc. are not cache coherent:
244 */
245 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
Vincent Penquerc'h23d84ed2012-10-09 19:40:39 +0100246 addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL);
Emil Gooded4eb23a2012-08-17 18:53:26 +0200247 if (!addrs) {
248 ret = -ENOMEM;
249 goto free_pages;
250 }
251
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600252 for (i = 0; i < npages; i++) {
Rob Clark8b6b5692012-05-17 02:37:25 -0600253 addrs[i] = dma_map_page(dev->dev, pages[i],
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600254 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
255 }
Rob Clark8b6b5692012-05-17 02:37:25 -0600256 } else {
Vincent Penquerc'h23d84ed2012-10-09 19:40:39 +0100257 addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL);
Emil Gooded4eb23a2012-08-17 18:53:26 +0200258 if (!addrs) {
259 ret = -ENOMEM;
260 goto free_pages;
261 }
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600262 }
263
Rob Clark8b6b5692012-05-17 02:37:25 -0600264 omap_obj->addrs = addrs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600265 omap_obj->pages = pages;
Rob Clark8b6b5692012-05-17 02:37:25 -0600266
Rob Clarkcd5351f2011-11-12 12:09:40 -0600267 return 0;
Emil Gooded4eb23a2012-08-17 18:53:26 +0200268
269free_pages:
Rob Clarkddcd09d2013-08-07 13:41:27 -0400270 drm_gem_put_pages(obj, pages, true, false);
Emil Gooded4eb23a2012-08-17 18:53:26 +0200271
272 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600273}
274
275/** release backing pages */
276static void omap_gem_detach_pages(struct drm_gem_object *obj)
277{
278 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600279
280 /* for non-cached buffers, ensure the new pages are clean because
281 * DSS, GPU, etc. are not cache coherent:
282 */
283 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
284 int i, npages = obj->size >> PAGE_SHIFT;
285 for (i = 0; i < npages; i++) {
286 dma_unmap_page(obj->dev->dev, omap_obj->addrs[i],
287 PAGE_SIZE, DMA_BIDIRECTIONAL);
288 }
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600289 }
290
Rob Clark8b6b5692012-05-17 02:37:25 -0600291 kfree(omap_obj->addrs);
292 omap_obj->addrs = NULL;
293
Rob Clarkddcd09d2013-08-07 13:41:27 -0400294 drm_gem_put_pages(obj, omap_obj->pages, true, false);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600295 omap_obj->pages = NULL;
296}
297
Rob Clark6ad11bc2012-04-10 13:19:55 -0500298/* get buffer flags */
299uint32_t omap_gem_flags(struct drm_gem_object *obj)
300{
301 return to_omap_bo(obj)->flags;
302}
303
Rob Clarkcd5351f2011-11-12 12:09:40 -0600304/** get mmap offset */
Rob Clarkc5b12472012-01-18 18:33:02 -0600305static uint64_t mmap_offset(struct drm_gem_object *obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600306{
Rob Clarkf6b60362012-03-05 10:48:36 -0600307 struct drm_device *dev = obj->dev;
David Herrmann0de23972013-07-24 21:07:52 +0200308 int ret;
309 size_t size;
Rob Clarkf6b60362012-03-05 10:48:36 -0600310
311 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
312
David Herrmann0de23972013-07-24 21:07:52 +0200313 /* Make it mmapable */
314 size = omap_gem_mmap_size(obj);
Rob Clarkddcd09d2013-08-07 13:41:27 -0400315 ret = drm_gem_create_mmap_offset_size(obj, size);
David Herrmann0de23972013-07-24 21:07:52 +0200316 if (ret) {
317 dev_err(dev->dev, "could not allocate mmap offset\n");
318 return 0;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600319 }
320
David Herrmann0de23972013-07-24 21:07:52 +0200321 return drm_vma_node_offset_addr(&obj->vma_node);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600322}
323
Rob Clarkc5b12472012-01-18 18:33:02 -0600324uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj)
325{
326 uint64_t offset;
327 mutex_lock(&obj->dev->struct_mutex);
328 offset = mmap_offset(obj);
329 mutex_unlock(&obj->dev->struct_mutex);
330 return offset;
331}
332
Rob Clarkf7f9f452011-12-05 19:19:22 -0600333/** get mmap size */
334size_t omap_gem_mmap_size(struct drm_gem_object *obj)
335{
336 struct omap_gem_object *omap_obj = to_omap_bo(obj);
337 size_t size = obj->size;
338
339 if (omap_obj->flags & OMAP_BO_TILED) {
340 /* for tiled buffers, the virtual size has stride rounded up
341 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
342 * 32kb later!). But we don't back the entire buffer with
343 * pages, only the valid picture part.. so need to adjust for
344 * this in the size used to mmap and generate mmap offset
345 */
346 size = tiler_vsize(gem2fmt(omap_obj->flags),
347 omap_obj->width, omap_obj->height);
348 }
349
350 return size;
351}
352
Rob Clark3c810c62012-08-15 15:18:01 -0500353/* get tiled size, returns -EINVAL if not tiled buffer */
354int omap_gem_tiled_size(struct drm_gem_object *obj, uint16_t *w, uint16_t *h)
355{
356 struct omap_gem_object *omap_obj = to_omap_bo(obj);
357 if (omap_obj->flags & OMAP_BO_TILED) {
358 *w = omap_obj->width;
359 *h = omap_obj->height;
360 return 0;
361 }
362 return -EINVAL;
363}
Rob Clarkf7f9f452011-12-05 19:19:22 -0600364
365/* Normal handling for the case of faulting in non-tiled buffers */
366static int fault_1d(struct drm_gem_object *obj,
367 struct vm_area_struct *vma, struct vm_fault *vmf)
368{
369 struct omap_gem_object *omap_obj = to_omap_bo(obj);
370 unsigned long pfn;
371 pgoff_t pgoff;
372
373 /* We don't use vmf->pgoff since that has the fake offset: */
374 pgoff = ((unsigned long)vmf->virtual_address -
375 vma->vm_start) >> PAGE_SHIFT;
376
377 if (omap_obj->pages) {
Rob Clark8b6b5692012-05-17 02:37:25 -0600378 omap_gem_cpu_sync(obj, pgoff);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600379 pfn = page_to_pfn(omap_obj->pages[pgoff]);
380 } else {
381 BUG_ON(!(omap_obj->flags & OMAP_BO_DMA));
382 pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff;
383 }
384
385 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
386 pfn, pfn << PAGE_SHIFT);
387
388 return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
389}
390
391/* Special handling for the case of faulting in 2d tiled buffers */
392static int fault_2d(struct drm_gem_object *obj,
393 struct vm_area_struct *vma, struct vm_fault *vmf)
394{
395 struct omap_gem_object *omap_obj = to_omap_bo(obj);
396 struct usergart_entry *entry;
397 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
398 struct page *pages[64]; /* XXX is this too much to have on stack? */
399 unsigned long pfn;
400 pgoff_t pgoff, base_pgoff;
401 void __user *vaddr;
402 int i, ret, slots;
403
Rob Clarke5598952012-03-05 10:48:40 -0600404 /*
405 * Note the height of the slot is also equal to the number of pages
406 * that need to be mapped in to fill 4kb wide CPU page. If the slot
407 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
Rob Clarkf7f9f452011-12-05 19:19:22 -0600408 */
Rob Clarke5598952012-03-05 10:48:40 -0600409 const int n = usergart[fmt].height;
410 const int n_shift = usergart[fmt].height_shift;
411
412 /*
413 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
414 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
415 * into account in some of the math, so figure out virtual stride
416 * in pages
417 */
418 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600419
420 /* We don't use vmf->pgoff since that has the fake offset: */
421 pgoff = ((unsigned long)vmf->virtual_address -
422 vma->vm_start) >> PAGE_SHIFT;
423
Rob Clarke5598952012-03-05 10:48:40 -0600424 /*
425 * Actual address we start mapping at is rounded down to previous slot
Rob Clarkf7f9f452011-12-05 19:19:22 -0600426 * boundary in the y direction:
427 */
Rob Clarke5598952012-03-05 10:48:40 -0600428 base_pgoff = round_down(pgoff, m << n_shift);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600429
Rob Clarke5598952012-03-05 10:48:40 -0600430 /* figure out buffer width in slots */
Rob Clarkf7f9f452011-12-05 19:19:22 -0600431 slots = omap_obj->width >> usergart[fmt].slot_shift;
432
Rob Clarke5598952012-03-05 10:48:40 -0600433 vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT);
434
435 entry = &usergart[fmt].entry[usergart[fmt].last];
436
Rob Clarkf7f9f452011-12-05 19:19:22 -0600437 /* evict previous buffer using this usergart entry, if any: */
438 if (entry->obj)
439 evict_entry(entry->obj, fmt, entry);
440
441 entry->obj = obj;
442 entry->obj_pgoff = base_pgoff;
443
Rob Clarke5598952012-03-05 10:48:40 -0600444 /* now convert base_pgoff to phys offset from virt offset: */
445 base_pgoff = (base_pgoff >> n_shift) * slots;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600446
Rob Clarke5598952012-03-05 10:48:40 -0600447 /* for wider-than 4k.. figure out which part of the slot-row we want: */
448 if (m > 1) {
449 int off = pgoff % m;
450 entry->obj_pgoff += off;
451 base_pgoff /= m;
452 slots = min(slots - (off << n_shift), n);
453 base_pgoff += off << n_shift;
454 vaddr += off << PAGE_SHIFT;
455 }
456
457 /*
458 * Map in pages. Beyond the valid pixel part of the buffer, we set
459 * pages[i] to NULL to get a dummy page mapped in.. if someone
460 * reads/writes it they will get random/undefined content, but at
461 * least it won't be corrupting whatever other random page used to
462 * be mapped in, or other undefined behavior.
Rob Clarkf7f9f452011-12-05 19:19:22 -0600463 */
464 memcpy(pages, &omap_obj->pages[base_pgoff],
465 sizeof(struct page *) * slots);
466 memset(pages + slots, 0,
Rob Clarke5598952012-03-05 10:48:40 -0600467 sizeof(struct page *) * (n - slots));
Rob Clarkf7f9f452011-12-05 19:19:22 -0600468
Rob Clarka6a91822011-12-09 23:26:08 -0600469 ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600470 if (ret) {
471 dev_err(obj->dev->dev, "failed to pin: %d\n", ret);
472 return ret;
473 }
474
Rob Clarkf7f9f452011-12-05 19:19:22 -0600475 pfn = entry->paddr >> PAGE_SHIFT;
476
477 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
478 pfn, pfn << PAGE_SHIFT);
479
Rob Clarke5598952012-03-05 10:48:40 -0600480 for (i = n; i > 0; i--) {
Rob Clarkf7f9f452011-12-05 19:19:22 -0600481 vm_insert_mixed(vma, (unsigned long)vaddr, pfn);
482 pfn += usergart[fmt].stride_pfn;
Rob Clarke5598952012-03-05 10:48:40 -0600483 vaddr += PAGE_SIZE * m;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600484 }
485
486 /* simple round-robin: */
487 usergart[fmt].last = (usergart[fmt].last + 1) % NUM_USERGART_ENTRIES;
488
489 return 0;
490}
491
Rob Clarkcd5351f2011-11-12 12:09:40 -0600492/**
493 * omap_gem_fault - pagefault handler for GEM objects
494 * @vma: the VMA of the GEM object
495 * @vmf: fault detail
496 *
497 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
498 * does most of the work for us including the actual map/unmap calls
499 * but we need to do the actual page work.
500 *
501 * The VMA was set up by GEM. In doing so it also ensured that the
502 * vma->vm_private_data points to the GEM object that is backing this
503 * mapping.
504 */
505int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
506{
507 struct drm_gem_object *obj = vma->vm_private_data;
508 struct omap_gem_object *omap_obj = to_omap_bo(obj);
509 struct drm_device *dev = obj->dev;
510 struct page **pages;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600511 int ret;
512
513 /* Make sure we don't parallel update on a fault, nor move or remove
514 * something from beneath our feet
515 */
516 mutex_lock(&dev->struct_mutex);
517
518 /* if a shmem backed object, make sure we have pages attached now */
519 ret = get_pages(obj, &pages);
YAMANE Toshiakiae053032012-11-14 19:33:17 +0900520 if (ret)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600521 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600522
523 /* where should we do corresponding put_pages().. we are mapping
524 * the original page, rather than thru a GART, so we can't rely
525 * on eviction to trigger this. But munmap() or all mappings should
526 * probably trigger put_pages()?
527 */
528
Rob Clarkf7f9f452011-12-05 19:19:22 -0600529 if (omap_obj->flags & OMAP_BO_TILED)
530 ret = fault_2d(obj, vma, vmf);
531 else
532 ret = fault_1d(obj, vma, vmf);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600533
Rob Clarkcd5351f2011-11-12 12:09:40 -0600534
535fail:
536 mutex_unlock(&dev->struct_mutex);
537 switch (ret) {
538 case 0:
539 case -ERESTARTSYS:
540 case -EINTR:
541 return VM_FAULT_NOPAGE;
542 case -ENOMEM:
543 return VM_FAULT_OOM;
544 default:
545 return VM_FAULT_SIGBUS;
546 }
547}
548
549/** We override mainly to fix up some of the vm mapping flags.. */
550int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
551{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600552 int ret;
553
554 ret = drm_gem_mmap(filp, vma);
555 if (ret) {
556 DBG("mmap failed: %d", ret);
557 return ret;
558 }
559
Rob Clark8b6b5692012-05-17 02:37:25 -0600560 return omap_gem_mmap_obj(vma->vm_private_data, vma);
561}
562
563int omap_gem_mmap_obj(struct drm_gem_object *obj,
564 struct vm_area_struct *vma)
565{
566 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600567
568 vma->vm_flags &= ~VM_PFNMAP;
569 vma->vm_flags |= VM_MIXEDMAP;
570
571 if (omap_obj->flags & OMAP_BO_WC) {
572 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
573 } else if (omap_obj->flags & OMAP_BO_UNCACHED) {
574 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
575 } else {
Rob Clark8b6b5692012-05-17 02:37:25 -0600576 /*
577 * We do have some private objects, at least for scanout buffers
578 * on hardware without DMM/TILER. But these are allocated write-
579 * combine
580 */
581 if (WARN_ON(!obj->filp))
582 return -EINVAL;
583
584 /*
585 * Shunt off cached objs to shmem file so they have their own
586 * address_space (so unmap_mapping_range does what we want,
587 * in particular in the case of mmap'd dmabufs)
588 */
589 fput(vma->vm_file);
Rob Clark8b6b5692012-05-17 02:37:25 -0600590 vma->vm_pgoff = 0;
Al Virocb0942b2012-08-27 14:48:26 -0400591 vma->vm_file = get_file(obj->filp);
Rob Clark8b6b5692012-05-17 02:37:25 -0600592
Rob Clarkcd5351f2011-11-12 12:09:40 -0600593 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
594 }
595
Rob Clark8b6b5692012-05-17 02:37:25 -0600596 return 0;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600597}
598
Rob Clark8b6b5692012-05-17 02:37:25 -0600599
Rob Clarkcd5351f2011-11-12 12:09:40 -0600600/**
601 * omap_gem_dumb_create - create a dumb buffer
602 * @drm_file: our client file
603 * @dev: our device
604 * @args: the requested arguments copied from userspace
605 *
606 * Allocate a buffer suitable for use for a frame buffer of the
607 * form described by user space. Give userspace a handle by which
608 * to reference it.
609 */
610int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
611 struct drm_mode_create_dumb *args)
612{
613 union omap_gem_size gsize;
614
Thierry Redingbdb2b932014-11-03 11:57:33 +0100615 args->pitch = align_pitch(0, args->width, args->bpp);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600616 args->size = PAGE_ALIGN(args->pitch * args->height);
617
618 gsize = (union omap_gem_size){
619 .bytes = args->size,
620 };
621
622 return omap_gem_new_handle(dev, file, gsize,
623 OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
624}
625
626/**
Rob Clarkcd5351f2011-11-12 12:09:40 -0600627 * omap_gem_dumb_map - buffer mapping for dumb interface
628 * @file: our drm client file
629 * @dev: drm device
630 * @handle: GEM handle to the object (from dumb_create)
631 *
632 * Do the necessary setup to allow the mapping of the frame buffer
633 * into user memory. We don't have to do much here at the moment.
634 */
635int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
636 uint32_t handle, uint64_t *offset)
637{
638 struct drm_gem_object *obj;
639 int ret = 0;
640
Rob Clarkcd5351f2011-11-12 12:09:40 -0600641 /* GEM does all our handle to object mapping */
642 obj = drm_gem_object_lookup(dev, file, handle);
643 if (obj == NULL) {
644 ret = -ENOENT;
645 goto fail;
646 }
647
648 *offset = omap_gem_mmap_offset(obj);
649
650 drm_gem_object_unreference_unlocked(obj);
651
652fail:
Rob Clarkcd5351f2011-11-12 12:09:40 -0600653 return ret;
654}
655
Rob Clarka6a91822011-12-09 23:26:08 -0600656/* Set scrolling position. This allows us to implement fast scrolling
657 * for console.
Rob Clark9b55b952012-03-05 10:48:33 -0600658 *
659 * Call only from non-atomic contexts.
Rob Clarka6a91822011-12-09 23:26:08 -0600660 */
661int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
662{
663 struct omap_gem_object *omap_obj = to_omap_bo(obj);
664 uint32_t npages = obj->size >> PAGE_SHIFT;
665 int ret = 0;
666
667 if (roll > npages) {
668 dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
669 return -EINVAL;
670 }
671
Rob Clarka6a91822011-12-09 23:26:08 -0600672 omap_obj->roll = roll;
673
Rob Clarkaf695922011-12-16 11:34:34 -0600674 mutex_lock(&obj->dev->struct_mutex);
675
Rob Clarka6a91822011-12-09 23:26:08 -0600676 /* if we aren't mapped yet, we don't need to do anything */
677 if (omap_obj->block) {
678 struct page **pages;
679 ret = get_pages(obj, &pages);
680 if (ret)
681 goto fail;
682 ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
683 if (ret)
684 dev_err(obj->dev->dev, "could not repin: %d\n", ret);
685 }
686
687fail:
688 mutex_unlock(&obj->dev->struct_mutex);
689
690 return ret;
691}
692
Rob Clark8b6b5692012-05-17 02:37:25 -0600693/* Sync the buffer for CPU access.. note pages should already be
694 * attached, ie. omap_gem_get_pages()
695 */
696void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff)
697{
698 struct drm_device *dev = obj->dev;
699 struct omap_gem_object *omap_obj = to_omap_bo(obj);
700
701 if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) {
702 dma_unmap_page(dev->dev, omap_obj->addrs[pgoff],
703 PAGE_SIZE, DMA_BIDIRECTIONAL);
704 omap_obj->addrs[pgoff] = 0;
705 }
706}
707
708/* sync the buffer for DMA access */
709void omap_gem_dma_sync(struct drm_gem_object *obj,
710 enum dma_data_direction dir)
711{
712 struct drm_device *dev = obj->dev;
713 struct omap_gem_object *omap_obj = to_omap_bo(obj);
714
715 if (is_cached_coherent(obj)) {
716 int i, npages = obj->size >> PAGE_SHIFT;
717 struct page **pages = omap_obj->pages;
718 bool dirty = false;
719
720 for (i = 0; i < npages; i++) {
721 if (!omap_obj->addrs[i]) {
722 omap_obj->addrs[i] = dma_map_page(dev->dev, pages[i], 0,
723 PAGE_SIZE, DMA_BIDIRECTIONAL);
724 dirty = true;
725 }
726 }
727
728 if (dirty) {
729 unmap_mapping_range(obj->filp->f_mapping, 0,
730 omap_gem_mmap_size(obj), 1);
731 }
732 }
733}
734
Rob Clarkcd5351f2011-11-12 12:09:40 -0600735/* Get physical address for DMA.. if 'remap' is true, and the buffer is not
736 * already contiguous, remap it to pin in physically contiguous memory.. (ie.
737 * map in TILER)
738 */
739int omap_gem_get_paddr(struct drm_gem_object *obj,
740 dma_addr_t *paddr, bool remap)
741{
Rob Clarka6a91822011-12-09 23:26:08 -0600742 struct omap_drm_private *priv = obj->dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600743 struct omap_gem_object *omap_obj = to_omap_bo(obj);
744 int ret = 0;
745
Rob Clarkf7f9f452011-12-05 19:19:22 -0600746 mutex_lock(&obj->dev->struct_mutex);
747
Rob Clarka6a91822011-12-09 23:26:08 -0600748 if (remap && is_shmem(obj) && priv->has_dmm) {
Rob Clarkf7f9f452011-12-05 19:19:22 -0600749 if (omap_obj->paddr_cnt == 0) {
750 struct page **pages;
Rob Clarka6a91822011-12-09 23:26:08 -0600751 uint32_t npages = obj->size >> PAGE_SHIFT;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600752 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
753 struct tiler_block *block;
Rob Clarka6a91822011-12-09 23:26:08 -0600754
Rob Clarkf7f9f452011-12-05 19:19:22 -0600755 BUG_ON(omap_obj->block);
756
757 ret = get_pages(obj, &pages);
758 if (ret)
759 goto fail;
760
Rob Clarkf7f9f452011-12-05 19:19:22 -0600761 if (omap_obj->flags & OMAP_BO_TILED) {
762 block = tiler_reserve_2d(fmt,
763 omap_obj->width,
764 omap_obj->height, 0);
765 } else {
766 block = tiler_reserve_1d(obj->size);
767 }
768
769 if (IS_ERR(block)) {
770 ret = PTR_ERR(block);
771 dev_err(obj->dev->dev,
772 "could not remap: %d (%d)\n", ret, fmt);
773 goto fail;
774 }
775
776 /* TODO: enable async refill.. */
Rob Clarka6a91822011-12-09 23:26:08 -0600777 ret = tiler_pin(block, pages, npages,
778 omap_obj->roll, true);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600779 if (ret) {
780 tiler_release(block);
781 dev_err(obj->dev->dev,
782 "could not pin: %d\n", ret);
783 goto fail;
784 }
785
786 omap_obj->paddr = tiler_ssptr(block);
787 omap_obj->block = block;
788
Russell King2d31ca32014-07-12 10:53:41 +0100789 DBG("got paddr: %pad", &omap_obj->paddr);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600790 }
791
792 omap_obj->paddr_cnt++;
793
794 *paddr = omap_obj->paddr;
795 } else if (omap_obj->flags & OMAP_BO_DMA) {
796 *paddr = omap_obj->paddr;
797 } else {
798 ret = -EINVAL;
Rob Clark8b6b5692012-05-17 02:37:25 -0600799 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600800 }
801
Rob Clarkf7f9f452011-12-05 19:19:22 -0600802fail:
803 mutex_unlock(&obj->dev->struct_mutex);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600804
805 return ret;
806}
807
808/* Release physical address, when DMA is no longer being performed.. this
809 * could potentially unpin and unmap buffers from TILER
810 */
Tomi Valkeinen393a9492015-04-28 14:01:36 +0300811void omap_gem_put_paddr(struct drm_gem_object *obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600812{
Rob Clarkf7f9f452011-12-05 19:19:22 -0600813 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Tomi Valkeinen393a9492015-04-28 14:01:36 +0300814 int ret;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600815
816 mutex_lock(&obj->dev->struct_mutex);
817 if (omap_obj->paddr_cnt > 0) {
818 omap_obj->paddr_cnt--;
819 if (omap_obj->paddr_cnt == 0) {
820 ret = tiler_unpin(omap_obj->block);
821 if (ret) {
822 dev_err(obj->dev->dev,
823 "could not unpin pages: %d\n", ret);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600824 }
825 ret = tiler_release(omap_obj->block);
826 if (ret) {
827 dev_err(obj->dev->dev,
828 "could not release unmap: %d\n", ret);
829 }
Tomi Valkeinen3f4d17c2014-09-03 19:25:53 +0000830 omap_obj->paddr = 0;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600831 omap_obj->block = NULL;
832 }
833 }
Tomi Valkeinen393a9492015-04-28 14:01:36 +0300834
Rob Clarkf7f9f452011-12-05 19:19:22 -0600835 mutex_unlock(&obj->dev->struct_mutex);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600836}
837
Rob Clark3c810c62012-08-15 15:18:01 -0500838/* Get rotated scanout address (only valid if already pinned), at the
839 * specified orientation and x,y offset from top-left corner of buffer
840 * (only valid for tiled 2d buffers)
841 */
842int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient,
843 int x, int y, dma_addr_t *paddr)
844{
845 struct omap_gem_object *omap_obj = to_omap_bo(obj);
846 int ret = -EINVAL;
847
848 mutex_lock(&obj->dev->struct_mutex);
849 if ((omap_obj->paddr_cnt > 0) && omap_obj->block &&
850 (omap_obj->flags & OMAP_BO_TILED)) {
851 *paddr = tiler_tsptr(omap_obj->block, orient, x, y);
852 ret = 0;
853 }
854 mutex_unlock(&obj->dev->struct_mutex);
855 return ret;
856}
857
858/* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
859int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient)
860{
861 struct omap_gem_object *omap_obj = to_omap_bo(obj);
862 int ret = -EINVAL;
863 if (omap_obj->flags & OMAP_BO_TILED)
864 ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
865 return ret;
866}
867
Rob Clarkcd5351f2011-11-12 12:09:40 -0600868/* acquire pages when needed (for example, for DMA where physically
869 * contiguous buffer is not required
870 */
871static int get_pages(struct drm_gem_object *obj, struct page ***pages)
872{
873 struct omap_gem_object *omap_obj = to_omap_bo(obj);
874 int ret = 0;
875
876 if (is_shmem(obj) && !omap_obj->pages) {
877 ret = omap_gem_attach_pages(obj);
878 if (ret) {
879 dev_err(obj->dev->dev, "could not attach pages\n");
880 return ret;
881 }
882 }
883
884 /* TODO: even phys-contig.. we should have a list of pages? */
885 *pages = omap_obj->pages;
886
887 return 0;
888}
889
Rob Clark6ad11bc2012-04-10 13:19:55 -0500890/* if !remap, and we don't have pages backing, then fail, rather than
891 * increasing the pin count (which we don't really do yet anyways,
892 * because we don't support swapping pages back out). And 'remap'
893 * might not be quite the right name, but I wanted to keep it working
894 * similarly to omap_gem_get_paddr(). Note though that mutex is not
895 * aquired if !remap (because this can be called in atomic ctxt),
896 * but probably omap_gem_get_paddr() should be changed to work in the
897 * same way. If !remap, a matching omap_gem_put_pages() call is not
898 * required (and should not be made).
899 */
900int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
901 bool remap)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600902{
903 int ret;
Rob Clark6ad11bc2012-04-10 13:19:55 -0500904 if (!remap) {
905 struct omap_gem_object *omap_obj = to_omap_bo(obj);
906 if (!omap_obj->pages)
907 return -ENOMEM;
908 *pages = omap_obj->pages;
909 return 0;
910 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600911 mutex_lock(&obj->dev->struct_mutex);
912 ret = get_pages(obj, pages);
913 mutex_unlock(&obj->dev->struct_mutex);
914 return ret;
915}
916
917/* release pages when DMA no longer being performed */
918int omap_gem_put_pages(struct drm_gem_object *obj)
919{
920 /* do something here if we dynamically attach/detach pages.. at
921 * least they would no longer need to be pinned if everyone has
922 * released the pages..
923 */
924 return 0;
925}
926
Rob Clarkf7f9f452011-12-05 19:19:22 -0600927/* Get kernel virtual address for CPU access.. this more or less only
928 * exists for omap_fbdev. This should be called with struct_mutex
929 * held.
Rob Clarkcd5351f2011-11-12 12:09:40 -0600930 */
931void *omap_gem_vaddr(struct drm_gem_object *obj)
932{
933 struct omap_gem_object *omap_obj = to_omap_bo(obj);
YAMANE Toshiaki696e3ca2012-11-14 19:33:43 +0900934 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
Rob Clarkf7f9f452011-12-05 19:19:22 -0600935 if (!omap_obj->vaddr) {
936 struct page **pages;
937 int ret = get_pages(obj, &pages);
938 if (ret)
939 return ERR_PTR(ret);
940 omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
941 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
942 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600943 return omap_obj->vaddr;
944}
945
Andy Grosse78edba2012-12-19 14:53:37 -0600946#ifdef CONFIG_PM
947/* re-pin objects in DMM in resume path: */
948int omap_gem_resume(struct device *dev)
949{
950 struct drm_device *drm_dev = dev_get_drvdata(dev);
951 struct omap_drm_private *priv = drm_dev->dev_private;
952 struct omap_gem_object *omap_obj;
953 int ret = 0;
954
955 list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
956 if (omap_obj->block) {
957 struct drm_gem_object *obj = &omap_obj->base;
958 uint32_t npages = obj->size >> PAGE_SHIFT;
959 WARN_ON(!omap_obj->pages); /* this can't happen */
960 ret = tiler_pin(omap_obj->block,
961 omap_obj->pages, npages,
962 omap_obj->roll, true);
963 if (ret) {
964 dev_err(dev, "could not repin: %d\n", ret);
965 return ret;
966 }
967 }
968 }
969
970 return 0;
971}
972#endif
973
Rob Clarkf6b60362012-03-05 10:48:36 -0600974#ifdef CONFIG_DEBUG_FS
975void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
976{
Rob Clarkf6b60362012-03-05 10:48:36 -0600977 struct omap_gem_object *omap_obj = to_omap_bo(obj);
David Herrmann0de23972013-07-24 21:07:52 +0200978 uint64_t off;
Rob Clarkf6b60362012-03-05 10:48:36 -0600979
David Herrmann0de23972013-07-24 21:07:52 +0200980 off = drm_vma_node_start(&obj->vma_node);
Rob Clarkf6b60362012-03-05 10:48:36 -0600981
Russell King2d31ca32014-07-12 10:53:41 +0100982 seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
Rob Clarkf6b60362012-03-05 10:48:36 -0600983 omap_obj->flags, obj->name, obj->refcount.refcount.counter,
Russell King2d31ca32014-07-12 10:53:41 +0100984 off, &omap_obj->paddr, omap_obj->paddr_cnt,
Rob Clarkf6b60362012-03-05 10:48:36 -0600985 omap_obj->vaddr, omap_obj->roll);
986
987 if (omap_obj->flags & OMAP_BO_TILED) {
988 seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
989 if (omap_obj->block) {
990 struct tcm_area *area = &omap_obj->block->area;
991 seq_printf(m, " (%dx%d, %dx%d)",
992 area->p0.x, area->p0.y,
993 area->p1.x, area->p1.y);
994 }
995 } else {
996 seq_printf(m, " %d", obj->size);
997 }
998
999 seq_printf(m, "\n");
1000}
1001
1002void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1003{
1004 struct omap_gem_object *omap_obj;
1005 int count = 0;
1006 size_t size = 0;
1007
1008 list_for_each_entry(omap_obj, list, mm_list) {
1009 struct drm_gem_object *obj = &omap_obj->base;
1010 seq_printf(m, " ");
1011 omap_gem_describe(obj, m);
1012 count++;
1013 size += obj->size;
1014 }
1015
1016 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1017}
1018#endif
1019
Rob Clarkcd5351f2011-11-12 12:09:40 -06001020/* Buffer Synchronization:
1021 */
1022
1023struct omap_gem_sync_waiter {
1024 struct list_head list;
1025 struct omap_gem_object *omap_obj;
1026 enum omap_gem_op op;
1027 uint32_t read_target, write_target;
1028 /* notify called w/ sync_lock held */
1029 void (*notify)(void *arg);
1030 void *arg;
1031};
1032
1033/* list of omap_gem_sync_waiter.. the notify fxn gets called back when
1034 * the read and/or write target count is achieved which can call a user
1035 * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
1036 * cpu access), etc.
1037 */
1038static LIST_HEAD(waiters);
1039
1040static inline bool is_waiting(struct omap_gem_sync_waiter *waiter)
1041{
1042 struct omap_gem_object *omap_obj = waiter->omap_obj;
1043 if ((waiter->op & OMAP_GEM_READ) &&
Archit Tanejaf2cff0f2014-04-11 12:53:31 +05301044 (omap_obj->sync->write_complete < waiter->write_target))
Rob Clarkcd5351f2011-11-12 12:09:40 -06001045 return true;
1046 if ((waiter->op & OMAP_GEM_WRITE) &&
Archit Tanejaf2cff0f2014-04-11 12:53:31 +05301047 (omap_obj->sync->read_complete < waiter->read_target))
Rob Clarkcd5351f2011-11-12 12:09:40 -06001048 return true;
1049 return false;
1050}
1051
1052/* macro for sync debug.. */
1053#define SYNCDBG 0
1054#define SYNC(fmt, ...) do { if (SYNCDBG) \
1055 printk(KERN_ERR "%s:%d: "fmt"\n", \
1056 __func__, __LINE__, ##__VA_ARGS__); \
1057 } while (0)
1058
1059
1060static void sync_op_update(void)
1061{
1062 struct omap_gem_sync_waiter *waiter, *n;
1063 list_for_each_entry_safe(waiter, n, &waiters, list) {
1064 if (!is_waiting(waiter)) {
1065 list_del(&waiter->list);
1066 SYNC("notify: %p", waiter);
1067 waiter->notify(waiter->arg);
1068 kfree(waiter);
1069 }
1070 }
1071}
1072
1073static inline int sync_op(struct drm_gem_object *obj,
1074 enum omap_gem_op op, bool start)
1075{
1076 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1077 int ret = 0;
1078
1079 spin_lock(&sync_lock);
1080
1081 if (!omap_obj->sync) {
1082 omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
1083 if (!omap_obj->sync) {
1084 ret = -ENOMEM;
1085 goto unlock;
1086 }
1087 }
1088
1089 if (start) {
1090 if (op & OMAP_GEM_READ)
1091 omap_obj->sync->read_pending++;
1092 if (op & OMAP_GEM_WRITE)
1093 omap_obj->sync->write_pending++;
1094 } else {
1095 if (op & OMAP_GEM_READ)
1096 omap_obj->sync->read_complete++;
1097 if (op & OMAP_GEM_WRITE)
1098 omap_obj->sync->write_complete++;
1099 sync_op_update();
1100 }
1101
1102unlock:
1103 spin_unlock(&sync_lock);
1104
1105 return ret;
1106}
1107
1108/* it is a bit lame to handle updates in this sort of polling way, but
1109 * in case of PVR, the GPU can directly update read/write complete
1110 * values, and not really tell us which ones it updated.. this also
1111 * means that sync_lock is not quite sufficient. So we'll need to
1112 * do something a bit better when it comes time to add support for
1113 * separate 2d hw..
1114 */
1115void omap_gem_op_update(void)
1116{
1117 spin_lock(&sync_lock);
1118 sync_op_update();
1119 spin_unlock(&sync_lock);
1120}
1121
1122/* mark the start of read and/or write operation */
1123int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
1124{
1125 return sync_op(obj, op, true);
1126}
1127
1128int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
1129{
1130 return sync_op(obj, op, false);
1131}
1132
1133static DECLARE_WAIT_QUEUE_HEAD(sync_event);
1134
1135static void sync_notify(void *arg)
1136{
1137 struct task_struct **waiter_task = arg;
1138 *waiter_task = NULL;
1139 wake_up_all(&sync_event);
1140}
1141
1142int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op)
1143{
1144 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1145 int ret = 0;
1146 if (omap_obj->sync) {
1147 struct task_struct *waiter_task = current;
1148 struct omap_gem_sync_waiter *waiter =
1149 kzalloc(sizeof(*waiter), GFP_KERNEL);
1150
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001151 if (!waiter)
Rob Clarkcd5351f2011-11-12 12:09:40 -06001152 return -ENOMEM;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001153
1154 waiter->omap_obj = omap_obj;
1155 waiter->op = op;
1156 waiter->read_target = omap_obj->sync->read_pending;
1157 waiter->write_target = omap_obj->sync->write_pending;
1158 waiter->notify = sync_notify;
1159 waiter->arg = &waiter_task;
1160
1161 spin_lock(&sync_lock);
1162 if (is_waiting(waiter)) {
1163 SYNC("waited: %p", waiter);
1164 list_add_tail(&waiter->list, &waiters);
1165 spin_unlock(&sync_lock);
1166 ret = wait_event_interruptible(sync_event,
1167 (waiter_task == NULL));
1168 spin_lock(&sync_lock);
1169 if (waiter_task) {
1170 SYNC("interrupted: %p", waiter);
1171 /* we were interrupted */
1172 list_del(&waiter->list);
1173 waiter_task = NULL;
1174 } else {
1175 /* freed in sync_op_update() */
1176 waiter = NULL;
1177 }
1178 }
1179 spin_unlock(&sync_lock);
Fabian Frederickd2c87e22014-07-04 21:17:15 +02001180 kfree(waiter);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001181 }
1182 return ret;
1183}
1184
1185/* call fxn(arg), either synchronously or asynchronously if the op
1186 * is currently blocked.. fxn() can be called from any context
1187 *
1188 * (TODO for now fxn is called back from whichever context calls
1189 * omap_gem_op_update().. but this could be better defined later
1190 * if needed)
1191 *
1192 * TODO more code in common w/ _sync()..
1193 */
1194int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op,
1195 void (*fxn)(void *arg), void *arg)
1196{
1197 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1198 if (omap_obj->sync) {
1199 struct omap_gem_sync_waiter *waiter =
1200 kzalloc(sizeof(*waiter), GFP_ATOMIC);
1201
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001202 if (!waiter)
Rob Clarkcd5351f2011-11-12 12:09:40 -06001203 return -ENOMEM;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001204
1205 waiter->omap_obj = omap_obj;
1206 waiter->op = op;
1207 waiter->read_target = omap_obj->sync->read_pending;
1208 waiter->write_target = omap_obj->sync->write_pending;
1209 waiter->notify = fxn;
1210 waiter->arg = arg;
1211
1212 spin_lock(&sync_lock);
1213 if (is_waiting(waiter)) {
1214 SYNC("waited: %p", waiter);
1215 list_add_tail(&waiter->list, &waiters);
1216 spin_unlock(&sync_lock);
1217 return 0;
1218 }
1219
1220 spin_unlock(&sync_lock);
Subhajit Paul15ec2ca2014-04-11 12:53:30 +05301221
1222 kfree(waiter);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001223 }
1224
1225 /* no waiting.. */
1226 fxn(arg);
1227
1228 return 0;
1229}
1230
1231/* special API so PVR can update the buffer to use a sync-object allocated
1232 * from it's sync-obj heap. Only used for a newly allocated (from PVR's
1233 * perspective) sync-object, so we overwrite the new syncobj w/ values
1234 * from the already allocated syncobj (if there is one)
1235 */
1236int omap_gem_set_sync_object(struct drm_gem_object *obj, void *syncobj)
1237{
1238 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1239 int ret = 0;
1240
1241 spin_lock(&sync_lock);
1242
1243 if ((omap_obj->flags & OMAP_BO_EXT_SYNC) && !syncobj) {
1244 /* clearing a previously set syncobj */
Peter Huewee6200962013-01-26 00:40:13 +01001245 syncobj = kmemdup(omap_obj->sync, sizeof(*omap_obj->sync),
1246 GFP_ATOMIC);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001247 if (!syncobj) {
1248 ret = -ENOMEM;
1249 goto unlock;
1250 }
Rob Clarkcd5351f2011-11-12 12:09:40 -06001251 omap_obj->flags &= ~OMAP_BO_EXT_SYNC;
1252 omap_obj->sync = syncobj;
1253 } else if (syncobj && !(omap_obj->flags & OMAP_BO_EXT_SYNC)) {
1254 /* replacing an existing syncobj */
1255 if (omap_obj->sync) {
1256 memcpy(syncobj, omap_obj->sync, sizeof(*omap_obj->sync));
1257 kfree(omap_obj->sync);
1258 }
1259 omap_obj->flags |= OMAP_BO_EXT_SYNC;
1260 omap_obj->sync = syncobj;
1261 }
1262
1263unlock:
1264 spin_unlock(&sync_lock);
1265 return ret;
1266}
1267
Rob Clarkcd5351f2011-11-12 12:09:40 -06001268/* don't call directly.. called from GEM core when it is time to actually
1269 * free the object..
1270 */
1271void omap_gem_free_object(struct drm_gem_object *obj)
1272{
1273 struct drm_device *dev = obj->dev;
Tomi Valkeinen76c40552014-12-17 14:34:22 +02001274 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001275 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1276
Rob Clarkf7f9f452011-12-05 19:19:22 -06001277 evict(obj);
1278
Rob Clarkf6b60362012-03-05 10:48:36 -06001279 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1280
Tomi Valkeinen76c40552014-12-17 14:34:22 +02001281 spin_lock(&priv->list_lock);
Rob Clarkf6b60362012-03-05 10:48:36 -06001282 list_del(&omap_obj->mm_list);
Tomi Valkeinen76c40552014-12-17 14:34:22 +02001283 spin_unlock(&priv->list_lock);
Rob Clarkf6b60362012-03-05 10:48:36 -06001284
David Herrmann0de23972013-07-24 21:07:52 +02001285 drm_gem_free_mmap_offset(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001286
Rob Clark9a0774e2012-01-16 12:51:17 -06001287 /* this means the object is still pinned.. which really should
1288 * not happen. I think..
1289 */
1290 WARN_ON(omap_obj->paddr_cnt > 0);
1291
Rob Clarkcd5351f2011-11-12 12:09:40 -06001292 /* don't free externally allocated backing memory */
1293 if (!(omap_obj->flags & OMAP_BO_EXT_MEM)) {
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001294 if (omap_obj->pages)
Rob Clarkcd5351f2011-11-12 12:09:40 -06001295 omap_gem_detach_pages(obj);
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001296
Rob Clarkcd5351f2011-11-12 12:09:40 -06001297 if (!is_shmem(obj)) {
1298 dma_free_writecombine(dev->dev, obj->size,
1299 omap_obj->vaddr, omap_obj->paddr);
Rob Clarkf7f9f452011-12-05 19:19:22 -06001300 } else if (omap_obj->vaddr) {
1301 vunmap(omap_obj->vaddr);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001302 }
1303 }
1304
1305 /* don't free externally allocated syncobj */
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001306 if (!(omap_obj->flags & OMAP_BO_EXT_SYNC))
Rob Clarkcd5351f2011-11-12 12:09:40 -06001307 kfree(omap_obj->sync);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001308
1309 drm_gem_object_release(obj);
1310
1311 kfree(obj);
1312}
1313
1314/* convenience method to construct a GEM buffer object, and userspace handle */
1315int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1316 union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
1317{
1318 struct drm_gem_object *obj;
1319 int ret;
1320
1321 obj = omap_gem_new(dev, gsize, flags);
1322 if (!obj)
1323 return -ENOMEM;
1324
1325 ret = drm_gem_handle_create(file, obj, handle);
1326 if (ret) {
1327 drm_gem_object_release(obj);
1328 kfree(obj); /* TODO isn't there a dtor to call? just copying i915 */
1329 return ret;
1330 }
1331
1332 /* drop reference from allocate - handle holds it now */
1333 drm_gem_object_unreference_unlocked(obj);
1334
1335 return 0;
1336}
1337
1338/* GEM buffer object constructor */
1339struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1340 union omap_gem_size gsize, uint32_t flags)
1341{
Rob Clarka6a91822011-12-09 23:26:08 -06001342 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001343 struct omap_gem_object *omap_obj;
1344 struct drm_gem_object *obj = NULL;
David Herrmannab5a60c2014-05-25 12:45:39 +02001345 struct address_space *mapping;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001346 size_t size;
1347 int ret;
1348
1349 if (flags & OMAP_BO_TILED) {
Rob Clarkf7f9f452011-12-05 19:19:22 -06001350 if (!usergart) {
1351 dev_err(dev->dev, "Tiled buffers require DMM\n");
1352 goto fail;
1353 }
Rob Clarkcd5351f2011-11-12 12:09:40 -06001354
Rob Clarkf7f9f452011-12-05 19:19:22 -06001355 /* tiled buffers are always shmem paged backed.. when they are
1356 * scanned out, they are remapped into DMM/TILER
1357 */
1358 flags &= ~OMAP_BO_SCANOUT;
1359
1360 /* currently don't allow cached buffers.. there is some caching
1361 * stuff that needs to be handled better
1362 */
Tomi Valkeinen7cb0d6c2014-09-25 19:24:29 +00001363 flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
1364 flags |= tiler_get_cpu_cache_flags();
Rob Clarkf7f9f452011-12-05 19:19:22 -06001365
1366 /* align dimensions to slot boundaries... */
1367 tiler_align(gem2fmt(flags),
1368 &gsize.tiled.width, &gsize.tiled.height);
1369
1370 /* ...and calculate size based on aligned dimensions */
1371 size = tiler_size(gem2fmt(flags),
1372 gsize.tiled.width, gsize.tiled.height);
1373 } else {
1374 size = PAGE_ALIGN(gsize.bytes);
1375 }
Rob Clarkcd5351f2011-11-12 12:09:40 -06001376
1377 omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -08001378 if (!omap_obj)
Tomi Valkeinena903e3b2015-03-17 15:31:11 +02001379 return NULL;
Rob Clarkf6b60362012-03-05 10:48:36 -06001380
Rob Clarkcd5351f2011-11-12 12:09:40 -06001381 obj = &omap_obj->base;
1382
Rob Clarka6a91822011-12-09 23:26:08 -06001383 if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1384 /* attempt to allocate contiguous memory if we don't
1385 * have DMM for remappign discontiguous buffers
1386 */
Rob Clarkcd5351f2011-11-12 12:09:40 -06001387 omap_obj->vaddr = dma_alloc_writecombine(dev->dev, size,
1388 &omap_obj->paddr, GFP_KERNEL);
Tomi Valkeinena903e3b2015-03-17 15:31:11 +02001389 if (!omap_obj->vaddr) {
1390 kfree(omap_obj);
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001391
Tomi Valkeinena903e3b2015-03-17 15:31:11 +02001392 return NULL;
1393 }
1394
1395 flags |= OMAP_BO_DMA;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001396 }
1397
Tomi Valkeinena903e3b2015-03-17 15:31:11 +02001398 spin_lock(&priv->list_lock);
1399 list_add(&omap_obj->mm_list, &priv->obj_list);
1400 spin_unlock(&priv->list_lock);
1401
Rob Clarkcd5351f2011-11-12 12:09:40 -06001402 omap_obj->flags = flags;
1403
Rob Clarkf7f9f452011-12-05 19:19:22 -06001404 if (flags & OMAP_BO_TILED) {
1405 omap_obj->width = gsize.tiled.width;
1406 omap_obj->height = gsize.tiled.height;
1407 }
1408
David Herrmannab5a60c2014-05-25 12:45:39 +02001409 if (flags & (OMAP_BO_DMA|OMAP_BO_EXT_MEM)) {
David Herrmann89c82332013-07-11 11:56:32 +02001410 drm_gem_private_object_init(dev, obj, size);
David Herrmannab5a60c2014-05-25 12:45:39 +02001411 } else {
Rob Clarkcd5351f2011-11-12 12:09:40 -06001412 ret = drm_gem_object_init(dev, obj, size);
David Herrmannab5a60c2014-05-25 12:45:39 +02001413 if (ret)
1414 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001415
David Herrmannab5a60c2014-05-25 12:45:39 +02001416 mapping = file_inode(obj->filp)->i_mapping;
1417 mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
1418 }
Rob Clarkcd5351f2011-11-12 12:09:40 -06001419
1420 return obj;
1421
1422fail:
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001423 if (obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -06001424 omap_gem_free_object(obj);
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001425
Rob Clarkcd5351f2011-11-12 12:09:40 -06001426 return NULL;
1427}
Rob Clarkf7f9f452011-12-05 19:19:22 -06001428
1429/* init/cleanup.. if DMM is used, we need to set some stuff up.. */
1430void omap_gem_init(struct drm_device *dev)
1431{
Rob Clarka6a91822011-12-09 23:26:08 -06001432 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001433 const enum tiler_fmt fmts[] = {
1434 TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1435 };
Andy Gross5c137792012-03-05 10:48:39 -06001436 int i, j;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001437
Andy Grosse5e4e9b2012-10-17 00:30:03 -05001438 if (!dmm_is_available()) {
Rob Clarkf7f9f452011-12-05 19:19:22 -06001439 /* DMM only supported on OMAP4 and later, so this isn't fatal */
Andy Gross5c137792012-03-05 10:48:39 -06001440 dev_warn(dev->dev, "DMM not available, disable DMM support\n");
Rob Clarkf7f9f452011-12-05 19:19:22 -06001441 return;
1442 }
1443
Joe Perches78110bb2013-02-11 09:41:29 -08001444 usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1445 if (!usergart)
Rob Clarkb3698392011-12-09 23:26:06 -06001446 return;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001447
1448 /* reserve 4k aligned/wide regions for userspace mappings: */
1449 for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1450 uint16_t h = 1, w = PAGE_SIZE >> i;
1451 tiler_align(fmts[i], &w, &h);
1452 /* note: since each region is 1 4kb page wide, and minimum
1453 * number of rows, the height ends up being the same as the
1454 * # of pages in the region
1455 */
1456 usergart[i].height = h;
1457 usergart[i].height_shift = ilog2(h);
Rob Clark3c810c62012-08-15 15:18:01 -05001458 usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001459 usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1460 for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1461 struct usergart_entry *entry = &usergart[i].entry[j];
1462 struct tiler_block *block =
1463 tiler_reserve_2d(fmts[i], w, h,
1464 PAGE_SIZE);
1465 if (IS_ERR(block)) {
1466 dev_err(dev->dev,
1467 "reserve failed: %d, %d, %ld\n",
1468 i, j, PTR_ERR(block));
1469 return;
1470 }
1471 entry->paddr = tiler_ssptr(block);
1472 entry->block = block;
1473
Russell King2d31ca32014-07-12 10:53:41 +01001474 DBG("%d:%d: %dx%d: paddr=%pad stride=%d", i, j, w, h,
1475 &entry->paddr,
Rob Clarkf7f9f452011-12-05 19:19:22 -06001476 usergart[i].stride_pfn << PAGE_SHIFT);
1477 }
1478 }
Rob Clarka6a91822011-12-09 23:26:08 -06001479
1480 priv->has_dmm = true;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001481}
1482
1483void omap_gem_deinit(struct drm_device *dev)
1484{
1485 /* I believe we can rely on there being no more outstanding GEM
1486 * objects which could depend on usergart/dmm at this point.
1487 */
Rob Clarkf7f9f452011-12-05 19:19:22 -06001488 kfree(usergart);
1489}