blob: 03698b6c806c6c63eabc10b7b304f9fb1a3bb43c [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
Arnd Bergmann2d802452016-05-11 18:01:45 +020020#include <linux/seq_file.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060021#include <linux/shmem_fs.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020022#include <linux/spinlock.h>
Dan Williams01c8f1c2016-01-15 16:56:40 -080023#include <linux/pfn_t.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020024
David Herrmann0de23972013-07-24 21:07:52 +020025#include <drm/drm_vma_manager.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060026
27#include "omap_drv.h"
Rob Clarkf7f9f452011-12-05 19:19:22 -060028#include "omap_dmm_tiler.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060029
Rob Clarkcd5351f2011-11-12 12:09:40 -060030/*
31 * GEM buffer object implementation.
32 */
33
Rob Clarkcd5351f2011-11-12 12:09:40 -060034/* note: we use upper 8 bits of flags for driver-internal flags: */
Laurent Pinchartcdb03812015-12-14 22:39:37 +020035#define OMAP_BO_MEM_DMA_API 0x01000000 /* memory allocated with the dma_alloc_* API */
36#define OMAP_BO_MEM_SHMEM 0x02000000 /* memory allocated through shmem backing */
Laurent Pinchartb22e6692015-12-14 22:39:44 +020037#define OMAP_BO_MEM_DMABUF 0x08000000 /* memory imported from a dmabuf */
Rob Clarkcd5351f2011-11-12 12:09:40 -060038
Rob Clarkcd5351f2011-11-12 12:09:40 -060039struct omap_gem_object {
40 struct drm_gem_object base;
41
Rob Clarkf6b60362012-03-05 10:48:36 -060042 struct list_head mm_list;
43
Rob Clarkcd5351f2011-11-12 12:09:40 -060044 uint32_t flags;
45
Rob Clarkf7f9f452011-12-05 19:19:22 -060046 /** width/height for tiled formats (rounded up to slot boundaries) */
47 uint16_t width, height;
48
Rob Clarka6a91822011-12-09 23:26:08 -060049 /** roll applied when mapping to DMM */
50 uint32_t roll;
51
Rob Clarkcd5351f2011-11-12 12:09:40 -060052 /**
Laurent Pinchartb22e6692015-12-14 22:39:44 +020053 * paddr contains the buffer DMA address. It is valid for
Rob Clarkcd5351f2011-11-12 12:09:40 -060054 *
Laurent Pinchartb22e6692015-12-14 22:39:44 +020055 * - buffers allocated through the DMA mapping API (with the
56 * OMAP_BO_MEM_DMA_API flag set)
57 *
58 * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set)
59 * if they are physically contiguous (when sgt->orig_nents == 1)
60 *
61 * - buffers mapped through the TILER when paddr_cnt is not zero, in
62 * which case the DMA address points to the TILER aperture
63 *
64 * Physically contiguous buffers have their DMA address equal to the
65 * physical address as we don't remap those buffers through the TILER.
66 *
67 * Buffers mapped to the TILER have their DMA address pointing to the
68 * TILER aperture. As TILER mappings are refcounted (through paddr_cnt)
69 * the DMA address must be accessed through omap_get_get_paddr() to
70 * ensure that the mapping won't disappear unexpectedly. References must
71 * be released with omap_gem_put_paddr().
Rob Clarkcd5351f2011-11-12 12:09:40 -060072 */
73 dma_addr_t paddr;
74
75 /**
Rob Clarkf7f9f452011-12-05 19:19:22 -060076 * # of users of paddr
77 */
78 uint32_t paddr_cnt;
79
80 /**
Laurent Pinchartb22e6692015-12-14 22:39:44 +020081 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag
82 * is set and the sgt field is valid.
83 */
84 struct sg_table *sgt;
85
86 /**
Rob Clarkf7f9f452011-12-05 19:19:22 -060087 * tiler block used when buffer is remapped in DMM/TILER.
88 */
89 struct tiler_block *block;
90
91 /**
Rob Clarkcd5351f2011-11-12 12:09:40 -060092 * Array of backing pages, if allocated. Note that pages are never
93 * allocated for buffers originally allocated from contiguous memory
94 */
95 struct page **pages;
96
Rob Clarkf3bc9d22011-12-20 16:54:28 -060097 /** addresses corresponding to pages in above array */
98 dma_addr_t *addrs;
99
Rob Clarkcd5351f2011-11-12 12:09:40 -0600100 /**
101 * Virtual address, if mapped.
102 */
103 void *vaddr;
104
105 /**
106 * sync-object allocated on demand (if needed)
107 *
108 * Per-buffer sync-object for tracking pending and completed hw/dma
Tomi Valkeinen3f50eff2016-01-27 10:58:43 +0200109 * read and write operations.
Rob Clarkcd5351f2011-11-12 12:09:40 -0600110 */
111 struct {
112 uint32_t write_pending;
113 uint32_t write_complete;
114 uint32_t read_pending;
115 uint32_t read_complete;
116 } *sync;
117};
118
Laurent Pinchart7ef93b02015-12-14 22:39:33 +0200119#define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
Rob Clarkc5b12472012-01-18 18:33:02 -0600120
Rob Clarkf7f9f452011-12-05 19:19:22 -0600121/* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
122 * not necessarily pinned in TILER all the time, and (b) when they are
123 * they are not necessarily page aligned, we reserve one or more small
124 * regions in each of the 2d containers to use as a user-GART where we
125 * can create a second page-aligned mapping of parts of the buffer
126 * being accessed from userspace.
127 *
128 * Note that we could optimize slightly when we know that multiple
129 * tiler containers are backed by the same PAT.. but I'll leave that
130 * for later..
131 */
132#define NUM_USERGART_ENTRIES 2
Laurent Pinchartf4302742015-12-14 22:39:34 +0200133struct omap_drm_usergart_entry {
Rob Clarkf7f9f452011-12-05 19:19:22 -0600134 struct tiler_block *block; /* the reserved tiler block */
135 dma_addr_t paddr;
136 struct drm_gem_object *obj; /* the current pinned obj */
137 pgoff_t obj_pgoff; /* page offset of obj currently
138 mapped in */
139};
Laurent Pinchartf4302742015-12-14 22:39:34 +0200140
141struct omap_drm_usergart {
142 struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES];
Rob Clarkf7f9f452011-12-05 19:19:22 -0600143 int height; /* height in rows */
144 int height_shift; /* ilog2(height in rows) */
145 int slot_shift; /* ilog2(width per slot) */
146 int stride_pfn; /* stride in pages */
147 int last; /* index of last used entry */
Laurent Pinchartf4302742015-12-14 22:39:34 +0200148};
Rob Clarkf7f9f452011-12-05 19:19:22 -0600149
Laurent Pinchartb902f8f2015-12-14 22:39:32 +0200150/* -----------------------------------------------------------------------------
151 * Helpers
152 */
153
154/** get mmap offset */
155static uint64_t mmap_offset(struct drm_gem_object *obj)
156{
157 struct drm_device *dev = obj->dev;
158 int ret;
159 size_t size;
160
161 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
162
163 /* Make it mmapable */
164 size = omap_gem_mmap_size(obj);
165 ret = drm_gem_create_mmap_offset_size(obj, size);
166 if (ret) {
167 dev_err(dev->dev, "could not allocate mmap offset\n");
168 return 0;
169 }
170
171 return drm_vma_node_offset_addr(&obj->vma_node);
172}
173
Laurent Pinchartb22e6692015-12-14 22:39:44 +0200174static bool is_contiguous(struct omap_gem_object *omap_obj)
Laurent Pinchart7ef93b02015-12-14 22:39:33 +0200175{
Laurent Pinchartb22e6692015-12-14 22:39:44 +0200176 if (omap_obj->flags & OMAP_BO_MEM_DMA_API)
177 return true;
178
179 if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1)
180 return true;
181
182 return false;
Laurent Pinchart7ef93b02015-12-14 22:39:33 +0200183}
184
185/* -----------------------------------------------------------------------------
186 * Eviction
187 */
Rob Clarkf7f9f452011-12-05 19:19:22 -0600188
189static void evict_entry(struct drm_gem_object *obj,
Laurent Pinchartf4302742015-12-14 22:39:34 +0200190 enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
Rob Clarkf7f9f452011-12-05 19:19:22 -0600191{
David Herrmann6796cb12014-01-03 14:24:19 +0100192 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Laurent Pinchartf4302742015-12-14 22:39:34 +0200193 struct omap_drm_private *priv = obj->dev->dev_private;
194 int n = priv->usergart[fmt].height;
David Herrmann6796cb12014-01-03 14:24:19 +0100195 size_t size = PAGE_SIZE * n;
196 loff_t off = mmap_offset(obj) +
197 (entry->obj_pgoff << PAGE_SHIFT);
198 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
199
200 if (m > 1) {
201 int i;
202 /* if stride > than PAGE_SIZE then sparse mapping: */
203 for (i = n; i > 0; i--) {
204 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
205 off, PAGE_SIZE, 1);
206 off += PAGE_SIZE * m;
Rob Clarke5598952012-03-05 10:48:40 -0600207 }
David Herrmann6796cb12014-01-03 14:24:19 +0100208 } else {
209 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
210 off, size, 1);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600211 }
212
213 entry->obj = NULL;
214}
215
216/* Evict a buffer from usergart, if it is mapped there */
217static void evict(struct drm_gem_object *obj)
218{
219 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Laurent Pinchartf4302742015-12-14 22:39:34 +0200220 struct omap_drm_private *priv = obj->dev->dev_private;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600221
222 if (omap_obj->flags & OMAP_BO_TILED) {
223 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
224 int i;
225
Rob Clarkf7f9f452011-12-05 19:19:22 -0600226 for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
Laurent Pinchartf4302742015-12-14 22:39:34 +0200227 struct omap_drm_usergart_entry *entry =
228 &priv->usergart[fmt].entry[i];
229
Rob Clarkf7f9f452011-12-05 19:19:22 -0600230 if (entry->obj == obj)
231 evict_entry(obj, fmt, entry);
232 }
233 }
234}
235
Laurent Pinchart7ef93b02015-12-14 22:39:33 +0200236/* -----------------------------------------------------------------------------
237 * Page Management
Rob Clarkcd5351f2011-11-12 12:09:40 -0600238 */
Rob Clarkcd5351f2011-11-12 12:09:40 -0600239
240/** ensure backing pages are allocated */
241static int omap_gem_attach_pages(struct drm_gem_object *obj)
242{
Rob Clark8b6b5692012-05-17 02:37:25 -0600243 struct drm_device *dev = obj->dev;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600244 struct omap_gem_object *omap_obj = to_omap_bo(obj);
245 struct page **pages;
Emil Gooded4eb23a2012-08-17 18:53:26 +0200246 int npages = obj->size >> PAGE_SHIFT;
247 int i, ret;
Rob Clark8b6b5692012-05-17 02:37:25 -0600248 dma_addr_t *addrs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600249
250 WARN_ON(omap_obj->pages);
251
David Herrmann0cdbe8a2014-05-25 12:59:47 +0200252 pages = drm_gem_get_pages(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600253 if (IS_ERR(pages)) {
254 dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
255 return PTR_ERR(pages);
256 }
257
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600258 /* for non-cached buffers, ensure the new pages are clean because
259 * DSS, GPU, etc. are not cache coherent:
260 */
261 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
Vincent Penquerc'h23d84ed2012-10-09 19:40:39 +0100262 addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL);
Emil Gooded4eb23a2012-08-17 18:53:26 +0200263 if (!addrs) {
264 ret = -ENOMEM;
265 goto free_pages;
266 }
267
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600268 for (i = 0; i < npages; i++) {
Rob Clark8b6b5692012-05-17 02:37:25 -0600269 addrs[i] = dma_map_page(dev->dev, pages[i],
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600270 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
Tomi Valkeinen579ef252016-01-05 11:43:14 +0200271
272 if (dma_mapping_error(dev->dev, addrs[i])) {
273 dev_warn(dev->dev,
274 "%s: failed to map page\n", __func__);
275
276 for (i = i - 1; i >= 0; --i) {
277 dma_unmap_page(dev->dev, addrs[i],
278 PAGE_SIZE, DMA_BIDIRECTIONAL);
279 }
280
281 ret = -ENOMEM;
282 goto free_addrs;
283 }
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600284 }
Rob Clark8b6b5692012-05-17 02:37:25 -0600285 } else {
Vincent Penquerc'h23d84ed2012-10-09 19:40:39 +0100286 addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL);
Emil Gooded4eb23a2012-08-17 18:53:26 +0200287 if (!addrs) {
288 ret = -ENOMEM;
289 goto free_pages;
290 }
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600291 }
292
Rob Clark8b6b5692012-05-17 02:37:25 -0600293 omap_obj->addrs = addrs;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600294 omap_obj->pages = pages;
Rob Clark8b6b5692012-05-17 02:37:25 -0600295
Rob Clarkcd5351f2011-11-12 12:09:40 -0600296 return 0;
Emil Gooded4eb23a2012-08-17 18:53:26 +0200297
Tomi Valkeinen579ef252016-01-05 11:43:14 +0200298free_addrs:
299 kfree(addrs);
Emil Gooded4eb23a2012-08-17 18:53:26 +0200300free_pages:
Rob Clarkddcd09d2013-08-07 13:41:27 -0400301 drm_gem_put_pages(obj, pages, true, false);
Emil Gooded4eb23a2012-08-17 18:53:26 +0200302
303 return ret;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600304}
305
Laurent Pinchartb902f8f2015-12-14 22:39:32 +0200306/* acquire pages when needed (for example, for DMA where physically
307 * contiguous buffer is not required
308 */
309static int get_pages(struct drm_gem_object *obj, struct page ***pages)
310{
311 struct omap_gem_object *omap_obj = to_omap_bo(obj);
312 int ret = 0;
313
Laurent Pinchartcdb03812015-12-14 22:39:37 +0200314 if ((omap_obj->flags & OMAP_BO_MEM_SHMEM) && !omap_obj->pages) {
Laurent Pinchartb902f8f2015-12-14 22:39:32 +0200315 ret = omap_gem_attach_pages(obj);
316 if (ret) {
317 dev_err(obj->dev->dev, "could not attach pages\n");
318 return ret;
319 }
320 }
321
322 /* TODO: even phys-contig.. we should have a list of pages? */
323 *pages = omap_obj->pages;
324
325 return 0;
326}
327
Rob Clarkcd5351f2011-11-12 12:09:40 -0600328/** release backing pages */
329static void omap_gem_detach_pages(struct drm_gem_object *obj)
330{
331 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600332
333 /* for non-cached buffers, ensure the new pages are clean because
334 * DSS, GPU, etc. are not cache coherent:
335 */
336 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
337 int i, npages = obj->size >> PAGE_SHIFT;
338 for (i = 0; i < npages; i++) {
339 dma_unmap_page(obj->dev->dev, omap_obj->addrs[i],
340 PAGE_SIZE, DMA_BIDIRECTIONAL);
341 }
Rob Clarkf3bc9d22011-12-20 16:54:28 -0600342 }
343
Rob Clark8b6b5692012-05-17 02:37:25 -0600344 kfree(omap_obj->addrs);
345 omap_obj->addrs = NULL;
346
Rob Clarkddcd09d2013-08-07 13:41:27 -0400347 drm_gem_put_pages(obj, omap_obj->pages, true, false);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600348 omap_obj->pages = NULL;
349}
350
Rob Clark6ad11bc2012-04-10 13:19:55 -0500351/* get buffer flags */
352uint32_t omap_gem_flags(struct drm_gem_object *obj)
353{
354 return to_omap_bo(obj)->flags;
355}
356
Rob Clarkc5b12472012-01-18 18:33:02 -0600357uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj)
358{
359 uint64_t offset;
360 mutex_lock(&obj->dev->struct_mutex);
361 offset = mmap_offset(obj);
362 mutex_unlock(&obj->dev->struct_mutex);
363 return offset;
364}
365
Rob Clarkf7f9f452011-12-05 19:19:22 -0600366/** get mmap size */
367size_t omap_gem_mmap_size(struct drm_gem_object *obj)
368{
369 struct omap_gem_object *omap_obj = to_omap_bo(obj);
370 size_t size = obj->size;
371
372 if (omap_obj->flags & OMAP_BO_TILED) {
373 /* for tiled buffers, the virtual size has stride rounded up
374 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
375 * 32kb later!). But we don't back the entire buffer with
376 * pages, only the valid picture part.. so need to adjust for
377 * this in the size used to mmap and generate mmap offset
378 */
379 size = tiler_vsize(gem2fmt(omap_obj->flags),
380 omap_obj->width, omap_obj->height);
381 }
382
383 return size;
384}
385
Rob Clark3c810c62012-08-15 15:18:01 -0500386/* get tiled size, returns -EINVAL if not tiled buffer */
387int omap_gem_tiled_size(struct drm_gem_object *obj, uint16_t *w, uint16_t *h)
388{
389 struct omap_gem_object *omap_obj = to_omap_bo(obj);
390 if (omap_obj->flags & OMAP_BO_TILED) {
391 *w = omap_obj->width;
392 *h = omap_obj->height;
393 return 0;
394 }
395 return -EINVAL;
396}
Rob Clarkf7f9f452011-12-05 19:19:22 -0600397
Laurent Pinchart7ef93b02015-12-14 22:39:33 +0200398/* -----------------------------------------------------------------------------
399 * Fault Handling
400 */
401
Rob Clarkf7f9f452011-12-05 19:19:22 -0600402/* Normal handling for the case of faulting in non-tiled buffers */
403static int fault_1d(struct drm_gem_object *obj,
404 struct vm_area_struct *vma, struct vm_fault *vmf)
405{
406 struct omap_gem_object *omap_obj = to_omap_bo(obj);
407 unsigned long pfn;
408 pgoff_t pgoff;
409
410 /* We don't use vmf->pgoff since that has the fake offset: */
411 pgoff = ((unsigned long)vmf->virtual_address -
412 vma->vm_start) >> PAGE_SHIFT;
413
414 if (omap_obj->pages) {
Rob Clark8b6b5692012-05-17 02:37:25 -0600415 omap_gem_cpu_sync(obj, pgoff);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600416 pfn = page_to_pfn(omap_obj->pages[pgoff]);
417 } else {
Laurent Pinchartb22e6692015-12-14 22:39:44 +0200418 BUG_ON(!is_contiguous(omap_obj));
Rob Clarkf7f9f452011-12-05 19:19:22 -0600419 pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff;
420 }
421
422 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
423 pfn, pfn << PAGE_SHIFT);
424
Dan Williams01c8f1c2016-01-15 16:56:40 -0800425 return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
426 __pfn_to_pfn_t(pfn, PFN_DEV));
Rob Clarkf7f9f452011-12-05 19:19:22 -0600427}
428
429/* Special handling for the case of faulting in 2d tiled buffers */
430static int fault_2d(struct drm_gem_object *obj,
431 struct vm_area_struct *vma, struct vm_fault *vmf)
432{
433 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Laurent Pinchartf4302742015-12-14 22:39:34 +0200434 struct omap_drm_private *priv = obj->dev->dev_private;
435 struct omap_drm_usergart_entry *entry;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600436 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
437 struct page *pages[64]; /* XXX is this too much to have on stack? */
438 unsigned long pfn;
439 pgoff_t pgoff, base_pgoff;
440 void __user *vaddr;
441 int i, ret, slots;
442
Rob Clarke5598952012-03-05 10:48:40 -0600443 /*
444 * Note the height of the slot is also equal to the number of pages
445 * that need to be mapped in to fill 4kb wide CPU page. If the slot
446 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
Rob Clarkf7f9f452011-12-05 19:19:22 -0600447 */
Laurent Pinchartf4302742015-12-14 22:39:34 +0200448 const int n = priv->usergart[fmt].height;
449 const int n_shift = priv->usergart[fmt].height_shift;
Rob Clarke5598952012-03-05 10:48:40 -0600450
451 /*
452 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
453 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
454 * into account in some of the math, so figure out virtual stride
455 * in pages
456 */
457 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600458
459 /* We don't use vmf->pgoff since that has the fake offset: */
460 pgoff = ((unsigned long)vmf->virtual_address -
461 vma->vm_start) >> PAGE_SHIFT;
462
Rob Clarke5598952012-03-05 10:48:40 -0600463 /*
464 * Actual address we start mapping at is rounded down to previous slot
Rob Clarkf7f9f452011-12-05 19:19:22 -0600465 * boundary in the y direction:
466 */
Rob Clarke5598952012-03-05 10:48:40 -0600467 base_pgoff = round_down(pgoff, m << n_shift);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600468
Rob Clarke5598952012-03-05 10:48:40 -0600469 /* figure out buffer width in slots */
Laurent Pinchartf4302742015-12-14 22:39:34 +0200470 slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600471
Rob Clarke5598952012-03-05 10:48:40 -0600472 vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT);
473
Laurent Pinchartf4302742015-12-14 22:39:34 +0200474 entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
Rob Clarke5598952012-03-05 10:48:40 -0600475
Rob Clarkf7f9f452011-12-05 19:19:22 -0600476 /* evict previous buffer using this usergart entry, if any: */
477 if (entry->obj)
478 evict_entry(entry->obj, fmt, entry);
479
480 entry->obj = obj;
481 entry->obj_pgoff = base_pgoff;
482
Rob Clarke5598952012-03-05 10:48:40 -0600483 /* now convert base_pgoff to phys offset from virt offset: */
484 base_pgoff = (base_pgoff >> n_shift) * slots;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600485
Rob Clarke5598952012-03-05 10:48:40 -0600486 /* for wider-than 4k.. figure out which part of the slot-row we want: */
487 if (m > 1) {
488 int off = pgoff % m;
489 entry->obj_pgoff += off;
490 base_pgoff /= m;
491 slots = min(slots - (off << n_shift), n);
492 base_pgoff += off << n_shift;
493 vaddr += off << PAGE_SHIFT;
494 }
495
496 /*
497 * Map in pages. Beyond the valid pixel part of the buffer, we set
498 * pages[i] to NULL to get a dummy page mapped in.. if someone
499 * reads/writes it they will get random/undefined content, but at
500 * least it won't be corrupting whatever other random page used to
501 * be mapped in, or other undefined behavior.
Rob Clarkf7f9f452011-12-05 19:19:22 -0600502 */
503 memcpy(pages, &omap_obj->pages[base_pgoff],
504 sizeof(struct page *) * slots);
505 memset(pages + slots, 0,
Rob Clarke5598952012-03-05 10:48:40 -0600506 sizeof(struct page *) * (n - slots));
Rob Clarkf7f9f452011-12-05 19:19:22 -0600507
Rob Clarka6a91822011-12-09 23:26:08 -0600508 ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600509 if (ret) {
510 dev_err(obj->dev->dev, "failed to pin: %d\n", ret);
511 return ret;
512 }
513
Rob Clarkf7f9f452011-12-05 19:19:22 -0600514 pfn = entry->paddr >> PAGE_SHIFT;
515
516 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
517 pfn, pfn << PAGE_SHIFT);
518
Rob Clarke5598952012-03-05 10:48:40 -0600519 for (i = n; i > 0; i--) {
Dan Williams01c8f1c2016-01-15 16:56:40 -0800520 vm_insert_mixed(vma, (unsigned long)vaddr,
521 __pfn_to_pfn_t(pfn, PFN_DEV));
Laurent Pinchartf4302742015-12-14 22:39:34 +0200522 pfn += priv->usergart[fmt].stride_pfn;
Rob Clarke5598952012-03-05 10:48:40 -0600523 vaddr += PAGE_SIZE * m;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600524 }
525
526 /* simple round-robin: */
Laurent Pinchartf4302742015-12-14 22:39:34 +0200527 priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
528 % NUM_USERGART_ENTRIES;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600529
530 return 0;
531}
532
Rob Clarkcd5351f2011-11-12 12:09:40 -0600533/**
534 * omap_gem_fault - pagefault handler for GEM objects
535 * @vma: the VMA of the GEM object
536 * @vmf: fault detail
537 *
538 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
539 * does most of the work for us including the actual map/unmap calls
540 * but we need to do the actual page work.
541 *
542 * The VMA was set up by GEM. In doing so it also ensured that the
543 * vma->vm_private_data points to the GEM object that is backing this
544 * mapping.
545 */
546int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
547{
548 struct drm_gem_object *obj = vma->vm_private_data;
549 struct omap_gem_object *omap_obj = to_omap_bo(obj);
550 struct drm_device *dev = obj->dev;
551 struct page **pages;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600552 int ret;
553
554 /* Make sure we don't parallel update on a fault, nor move or remove
555 * something from beneath our feet
556 */
557 mutex_lock(&dev->struct_mutex);
558
559 /* if a shmem backed object, make sure we have pages attached now */
560 ret = get_pages(obj, &pages);
YAMANE Toshiakiae053032012-11-14 19:33:17 +0900561 if (ret)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600562 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600563
564 /* where should we do corresponding put_pages().. we are mapping
565 * the original page, rather than thru a GART, so we can't rely
566 * on eviction to trigger this. But munmap() or all mappings should
567 * probably trigger put_pages()?
568 */
569
Rob Clarkf7f9f452011-12-05 19:19:22 -0600570 if (omap_obj->flags & OMAP_BO_TILED)
571 ret = fault_2d(obj, vma, vmf);
572 else
573 ret = fault_1d(obj, vma, vmf);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600574
Rob Clarkcd5351f2011-11-12 12:09:40 -0600575
576fail:
577 mutex_unlock(&dev->struct_mutex);
578 switch (ret) {
579 case 0:
580 case -ERESTARTSYS:
581 case -EINTR:
Rob Clarke1d4ee02013-10-20 12:07:42 -0400582 case -EBUSY:
583 /*
584 * EBUSY is ok: this just means that another thread
585 * already did the job.
586 */
Rob Clarkcd5351f2011-11-12 12:09:40 -0600587 return VM_FAULT_NOPAGE;
588 case -ENOMEM:
589 return VM_FAULT_OOM;
590 default:
591 return VM_FAULT_SIGBUS;
592 }
593}
594
595/** We override mainly to fix up some of the vm mapping flags.. */
596int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
597{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600598 int ret;
599
600 ret = drm_gem_mmap(filp, vma);
601 if (ret) {
602 DBG("mmap failed: %d", ret);
603 return ret;
604 }
605
Rob Clark8b6b5692012-05-17 02:37:25 -0600606 return omap_gem_mmap_obj(vma->vm_private_data, vma);
607}
608
609int omap_gem_mmap_obj(struct drm_gem_object *obj,
610 struct vm_area_struct *vma)
611{
612 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600613
614 vma->vm_flags &= ~VM_PFNMAP;
615 vma->vm_flags |= VM_MIXEDMAP;
616
617 if (omap_obj->flags & OMAP_BO_WC) {
618 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
619 } else if (omap_obj->flags & OMAP_BO_UNCACHED) {
620 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
621 } else {
Rob Clark8b6b5692012-05-17 02:37:25 -0600622 /*
623 * We do have some private objects, at least for scanout buffers
624 * on hardware without DMM/TILER. But these are allocated write-
625 * combine
626 */
627 if (WARN_ON(!obj->filp))
628 return -EINVAL;
629
630 /*
631 * Shunt off cached objs to shmem file so they have their own
632 * address_space (so unmap_mapping_range does what we want,
633 * in particular in the case of mmap'd dmabufs)
634 */
635 fput(vma->vm_file);
Rob Clark8b6b5692012-05-17 02:37:25 -0600636 vma->vm_pgoff = 0;
Al Virocb0942b2012-08-27 14:48:26 -0400637 vma->vm_file = get_file(obj->filp);
Rob Clark8b6b5692012-05-17 02:37:25 -0600638
Rob Clarkcd5351f2011-11-12 12:09:40 -0600639 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
640 }
641
Rob Clark8b6b5692012-05-17 02:37:25 -0600642 return 0;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600643}
644
Laurent Pinchart7ef93b02015-12-14 22:39:33 +0200645/* -----------------------------------------------------------------------------
646 * Dumb Buffers
647 */
Rob Clark8b6b5692012-05-17 02:37:25 -0600648
Rob Clarkcd5351f2011-11-12 12:09:40 -0600649/**
650 * omap_gem_dumb_create - create a dumb buffer
651 * @drm_file: our client file
652 * @dev: our device
653 * @args: the requested arguments copied from userspace
654 *
655 * Allocate a buffer suitable for use for a frame buffer of the
656 * form described by user space. Give userspace a handle by which
657 * to reference it.
658 */
659int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
660 struct drm_mode_create_dumb *args)
661{
662 union omap_gem_size gsize;
663
Thierry Redingbdb2b932014-11-03 11:57:33 +0100664 args->pitch = align_pitch(0, args->width, args->bpp);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600665 args->size = PAGE_ALIGN(args->pitch * args->height);
666
667 gsize = (union omap_gem_size){
668 .bytes = args->size,
669 };
670
671 return omap_gem_new_handle(dev, file, gsize,
672 OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
673}
674
675/**
Rob Clarkcd5351f2011-11-12 12:09:40 -0600676 * omap_gem_dumb_map - buffer mapping for dumb interface
677 * @file: our drm client file
678 * @dev: drm device
679 * @handle: GEM handle to the object (from dumb_create)
680 *
681 * Do the necessary setup to allow the mapping of the frame buffer
682 * into user memory. We don't have to do much here at the moment.
683 */
684int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
685 uint32_t handle, uint64_t *offset)
686{
687 struct drm_gem_object *obj;
688 int ret = 0;
689
Rob Clarkcd5351f2011-11-12 12:09:40 -0600690 /* GEM does all our handle to object mapping */
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100691 obj = drm_gem_object_lookup(file, handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600692 if (obj == NULL) {
693 ret = -ENOENT;
694 goto fail;
695 }
696
697 *offset = omap_gem_mmap_offset(obj);
698
699 drm_gem_object_unreference_unlocked(obj);
700
701fail:
Rob Clarkcd5351f2011-11-12 12:09:40 -0600702 return ret;
703}
704
Laurent Pincharte1c11742015-12-14 22:39:30 +0200705#ifdef CONFIG_DRM_FBDEV_EMULATION
Rob Clarka6a91822011-12-09 23:26:08 -0600706/* Set scrolling position. This allows us to implement fast scrolling
707 * for console.
Rob Clark9b55b952012-03-05 10:48:33 -0600708 *
709 * Call only from non-atomic contexts.
Rob Clarka6a91822011-12-09 23:26:08 -0600710 */
711int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
712{
713 struct omap_gem_object *omap_obj = to_omap_bo(obj);
714 uint32_t npages = obj->size >> PAGE_SHIFT;
715 int ret = 0;
716
717 if (roll > npages) {
718 dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
719 return -EINVAL;
720 }
721
Rob Clarka6a91822011-12-09 23:26:08 -0600722 omap_obj->roll = roll;
723
Rob Clarkaf695922011-12-16 11:34:34 -0600724 mutex_lock(&obj->dev->struct_mutex);
725
Rob Clarka6a91822011-12-09 23:26:08 -0600726 /* if we aren't mapped yet, we don't need to do anything */
727 if (omap_obj->block) {
728 struct page **pages;
729 ret = get_pages(obj, &pages);
730 if (ret)
731 goto fail;
732 ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
733 if (ret)
734 dev_err(obj->dev->dev, "could not repin: %d\n", ret);
735 }
736
737fail:
738 mutex_unlock(&obj->dev->struct_mutex);
739
740 return ret;
741}
Laurent Pincharte1c11742015-12-14 22:39:30 +0200742#endif
Rob Clarka6a91822011-12-09 23:26:08 -0600743
Laurent Pinchart7ef93b02015-12-14 22:39:33 +0200744/* -----------------------------------------------------------------------------
745 * Memory Management & DMA Sync
746 */
747
748/**
749 * shmem buffers that are mapped cached can simulate coherency via using
750 * page faulting to keep track of dirty pages
751 */
752static inline bool is_cached_coherent(struct drm_gem_object *obj)
753{
754 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Laurent Pinchartcdb03812015-12-14 22:39:37 +0200755
756 return (omap_obj->flags & OMAP_BO_MEM_SHMEM) &&
Laurent Pinchart7ef93b02015-12-14 22:39:33 +0200757 ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED);
758}
Rob Clarkcd5351f2011-11-12 12:09:40 -0600759
Rob Clark8b6b5692012-05-17 02:37:25 -0600760/* Sync the buffer for CPU access.. note pages should already be
761 * attached, ie. omap_gem_get_pages()
762 */
763void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff)
764{
765 struct drm_device *dev = obj->dev;
766 struct omap_gem_object *omap_obj = to_omap_bo(obj);
767
768 if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) {
769 dma_unmap_page(dev->dev, omap_obj->addrs[pgoff],
770 PAGE_SIZE, DMA_BIDIRECTIONAL);
771 omap_obj->addrs[pgoff] = 0;
772 }
773}
774
775/* sync the buffer for DMA access */
776void omap_gem_dma_sync(struct drm_gem_object *obj,
777 enum dma_data_direction dir)
778{
779 struct drm_device *dev = obj->dev;
780 struct omap_gem_object *omap_obj = to_omap_bo(obj);
781
782 if (is_cached_coherent(obj)) {
783 int i, npages = obj->size >> PAGE_SHIFT;
784 struct page **pages = omap_obj->pages;
785 bool dirty = false;
786
787 for (i = 0; i < npages; i++) {
788 if (!omap_obj->addrs[i]) {
Tomi Valkeinena3d63452016-01-05 11:43:15 +0200789 dma_addr_t addr;
790
791 addr = dma_map_page(dev->dev, pages[i], 0,
Rob Clark8b6b5692012-05-17 02:37:25 -0600792 PAGE_SIZE, DMA_BIDIRECTIONAL);
Tomi Valkeinena3d63452016-01-05 11:43:15 +0200793
794 if (dma_mapping_error(dev->dev, addr)) {
795 dev_warn(dev->dev,
796 "%s: failed to map page\n",
797 __func__);
798 break;
799 }
800
Rob Clark8b6b5692012-05-17 02:37:25 -0600801 dirty = true;
Tomi Valkeinena3d63452016-01-05 11:43:15 +0200802 omap_obj->addrs[i] = addr;
Rob Clark8b6b5692012-05-17 02:37:25 -0600803 }
804 }
805
806 if (dirty) {
807 unmap_mapping_range(obj->filp->f_mapping, 0,
808 omap_gem_mmap_size(obj), 1);
809 }
810 }
811}
812
Rob Clarkcd5351f2011-11-12 12:09:40 -0600813/* Get physical address for DMA.. if 'remap' is true, and the buffer is not
814 * already contiguous, remap it to pin in physically contiguous memory.. (ie.
815 * map in TILER)
816 */
817int omap_gem_get_paddr(struct drm_gem_object *obj,
818 dma_addr_t *paddr, bool remap)
819{
Rob Clarka6a91822011-12-09 23:26:08 -0600820 struct omap_drm_private *priv = obj->dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600821 struct omap_gem_object *omap_obj = to_omap_bo(obj);
822 int ret = 0;
823
Rob Clarkf7f9f452011-12-05 19:19:22 -0600824 mutex_lock(&obj->dev->struct_mutex);
825
Laurent Pinchartb22e6692015-12-14 22:39:44 +0200826 if (!is_contiguous(omap_obj) && remap && priv->has_dmm) {
Rob Clarkf7f9f452011-12-05 19:19:22 -0600827 if (omap_obj->paddr_cnt == 0) {
828 struct page **pages;
Rob Clarka6a91822011-12-09 23:26:08 -0600829 uint32_t npages = obj->size >> PAGE_SHIFT;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600830 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
831 struct tiler_block *block;
Rob Clarka6a91822011-12-09 23:26:08 -0600832
Rob Clarkf7f9f452011-12-05 19:19:22 -0600833 BUG_ON(omap_obj->block);
834
835 ret = get_pages(obj, &pages);
836 if (ret)
837 goto fail;
838
Rob Clarkf7f9f452011-12-05 19:19:22 -0600839 if (omap_obj->flags & OMAP_BO_TILED) {
840 block = tiler_reserve_2d(fmt,
841 omap_obj->width,
842 omap_obj->height, 0);
843 } else {
844 block = tiler_reserve_1d(obj->size);
845 }
846
847 if (IS_ERR(block)) {
848 ret = PTR_ERR(block);
849 dev_err(obj->dev->dev,
850 "could not remap: %d (%d)\n", ret, fmt);
851 goto fail;
852 }
853
854 /* TODO: enable async refill.. */
Rob Clarka6a91822011-12-09 23:26:08 -0600855 ret = tiler_pin(block, pages, npages,
856 omap_obj->roll, true);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600857 if (ret) {
858 tiler_release(block);
859 dev_err(obj->dev->dev,
860 "could not pin: %d\n", ret);
861 goto fail;
862 }
863
864 omap_obj->paddr = tiler_ssptr(block);
865 omap_obj->block = block;
866
Russell King2d31ca32014-07-12 10:53:41 +0100867 DBG("got paddr: %pad", &omap_obj->paddr);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600868 }
869
870 omap_obj->paddr_cnt++;
871
872 *paddr = omap_obj->paddr;
Laurent Pinchartb22e6692015-12-14 22:39:44 +0200873 } else if (is_contiguous(omap_obj)) {
Rob Clarkf7f9f452011-12-05 19:19:22 -0600874 *paddr = omap_obj->paddr;
875 } else {
876 ret = -EINVAL;
Rob Clark8b6b5692012-05-17 02:37:25 -0600877 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600878 }
879
Rob Clarkf7f9f452011-12-05 19:19:22 -0600880fail:
881 mutex_unlock(&obj->dev->struct_mutex);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600882
883 return ret;
884}
885
886/* Release physical address, when DMA is no longer being performed.. this
887 * could potentially unpin and unmap buffers from TILER
888 */
Tomi Valkeinen393a9492015-04-28 14:01:36 +0300889void omap_gem_put_paddr(struct drm_gem_object *obj)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600890{
Rob Clarkf7f9f452011-12-05 19:19:22 -0600891 struct omap_gem_object *omap_obj = to_omap_bo(obj);
Tomi Valkeinen393a9492015-04-28 14:01:36 +0300892 int ret;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600893
894 mutex_lock(&obj->dev->struct_mutex);
895 if (omap_obj->paddr_cnt > 0) {
896 omap_obj->paddr_cnt--;
897 if (omap_obj->paddr_cnt == 0) {
898 ret = tiler_unpin(omap_obj->block);
899 if (ret) {
900 dev_err(obj->dev->dev,
901 "could not unpin pages: %d\n", ret);
Rob Clarkf7f9f452011-12-05 19:19:22 -0600902 }
903 ret = tiler_release(omap_obj->block);
904 if (ret) {
905 dev_err(obj->dev->dev,
906 "could not release unmap: %d\n", ret);
907 }
Tomi Valkeinen3f4d17c2014-09-03 19:25:53 +0000908 omap_obj->paddr = 0;
Rob Clarkf7f9f452011-12-05 19:19:22 -0600909 omap_obj->block = NULL;
910 }
911 }
Tomi Valkeinen393a9492015-04-28 14:01:36 +0300912
Rob Clarkf7f9f452011-12-05 19:19:22 -0600913 mutex_unlock(&obj->dev->struct_mutex);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600914}
915
Rob Clark3c810c62012-08-15 15:18:01 -0500916/* Get rotated scanout address (only valid if already pinned), at the
917 * specified orientation and x,y offset from top-left corner of buffer
918 * (only valid for tiled 2d buffers)
919 */
920int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient,
921 int x, int y, dma_addr_t *paddr)
922{
923 struct omap_gem_object *omap_obj = to_omap_bo(obj);
924 int ret = -EINVAL;
925
926 mutex_lock(&obj->dev->struct_mutex);
927 if ((omap_obj->paddr_cnt > 0) && omap_obj->block &&
928 (omap_obj->flags & OMAP_BO_TILED)) {
929 *paddr = tiler_tsptr(omap_obj->block, orient, x, y);
930 ret = 0;
931 }
932 mutex_unlock(&obj->dev->struct_mutex);
933 return ret;
934}
935
936/* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
937int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient)
938{
939 struct omap_gem_object *omap_obj = to_omap_bo(obj);
940 int ret = -EINVAL;
941 if (omap_obj->flags & OMAP_BO_TILED)
942 ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
943 return ret;
944}
945
Rob Clark6ad11bc2012-04-10 13:19:55 -0500946/* if !remap, and we don't have pages backing, then fail, rather than
947 * increasing the pin count (which we don't really do yet anyways,
948 * because we don't support swapping pages back out). And 'remap'
949 * might not be quite the right name, but I wanted to keep it working
950 * similarly to omap_gem_get_paddr(). Note though that mutex is not
951 * aquired if !remap (because this can be called in atomic ctxt),
952 * but probably omap_gem_get_paddr() should be changed to work in the
953 * same way. If !remap, a matching omap_gem_put_pages() call is not
954 * required (and should not be made).
955 */
956int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
957 bool remap)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600958{
959 int ret;
Rob Clark6ad11bc2012-04-10 13:19:55 -0500960 if (!remap) {
961 struct omap_gem_object *omap_obj = to_omap_bo(obj);
962 if (!omap_obj->pages)
963 return -ENOMEM;
964 *pages = omap_obj->pages;
965 return 0;
966 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600967 mutex_lock(&obj->dev->struct_mutex);
968 ret = get_pages(obj, pages);
969 mutex_unlock(&obj->dev->struct_mutex);
970 return ret;
971}
972
973/* release pages when DMA no longer being performed */
974int omap_gem_put_pages(struct drm_gem_object *obj)
975{
976 /* do something here if we dynamically attach/detach pages.. at
977 * least they would no longer need to be pinned if everyone has
978 * released the pages..
979 */
980 return 0;
981}
982
Laurent Pincharte1c11742015-12-14 22:39:30 +0200983#ifdef CONFIG_DRM_FBDEV_EMULATION
Rob Clarkf7f9f452011-12-05 19:19:22 -0600984/* Get kernel virtual address for CPU access.. this more or less only
985 * exists for omap_fbdev. This should be called with struct_mutex
986 * held.
Rob Clarkcd5351f2011-11-12 12:09:40 -0600987 */
988void *omap_gem_vaddr(struct drm_gem_object *obj)
989{
990 struct omap_gem_object *omap_obj = to_omap_bo(obj);
YAMANE Toshiaki696e3ca2012-11-14 19:33:43 +0900991 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
Rob Clarkf7f9f452011-12-05 19:19:22 -0600992 if (!omap_obj->vaddr) {
993 struct page **pages;
994 int ret = get_pages(obj, &pages);
995 if (ret)
996 return ERR_PTR(ret);
997 omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
998 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
999 }
Rob Clarkcd5351f2011-11-12 12:09:40 -06001000 return omap_obj->vaddr;
1001}
Laurent Pincharte1c11742015-12-14 22:39:30 +02001002#endif
Rob Clarkcd5351f2011-11-12 12:09:40 -06001003
Laurent Pinchart7ef93b02015-12-14 22:39:33 +02001004/* -----------------------------------------------------------------------------
1005 * Power Management
1006 */
Rob Clarkcd5351f2011-11-12 12:09:40 -06001007
Andy Grosse78edba2012-12-19 14:53:37 -06001008#ifdef CONFIG_PM
1009/* re-pin objects in DMM in resume path: */
1010int omap_gem_resume(struct device *dev)
1011{
1012 struct drm_device *drm_dev = dev_get_drvdata(dev);
1013 struct omap_drm_private *priv = drm_dev->dev_private;
1014 struct omap_gem_object *omap_obj;
1015 int ret = 0;
1016
1017 list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
1018 if (omap_obj->block) {
1019 struct drm_gem_object *obj = &omap_obj->base;
1020 uint32_t npages = obj->size >> PAGE_SHIFT;
1021 WARN_ON(!omap_obj->pages); /* this can't happen */
1022 ret = tiler_pin(omap_obj->block,
1023 omap_obj->pages, npages,
1024 omap_obj->roll, true);
1025 if (ret) {
1026 dev_err(dev, "could not repin: %d\n", ret);
1027 return ret;
1028 }
1029 }
1030 }
1031
1032 return 0;
1033}
1034#endif
1035
Laurent Pinchart7ef93b02015-12-14 22:39:33 +02001036/* -----------------------------------------------------------------------------
1037 * DebugFS
1038 */
1039
Rob Clarkf6b60362012-03-05 10:48:36 -06001040#ifdef CONFIG_DEBUG_FS
1041void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
1042{
Rob Clarkf6b60362012-03-05 10:48:36 -06001043 struct omap_gem_object *omap_obj = to_omap_bo(obj);
David Herrmann0de23972013-07-24 21:07:52 +02001044 uint64_t off;
Rob Clarkf6b60362012-03-05 10:48:36 -06001045
David Herrmann0de23972013-07-24 21:07:52 +02001046 off = drm_vma_node_start(&obj->vma_node);
Rob Clarkf6b60362012-03-05 10:48:36 -06001047
Russell King2d31ca32014-07-12 10:53:41 +01001048 seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
Rob Clarkf6b60362012-03-05 10:48:36 -06001049 omap_obj->flags, obj->name, obj->refcount.refcount.counter,
Russell King2d31ca32014-07-12 10:53:41 +01001050 off, &omap_obj->paddr, omap_obj->paddr_cnt,
Rob Clarkf6b60362012-03-05 10:48:36 -06001051 omap_obj->vaddr, omap_obj->roll);
1052
1053 if (omap_obj->flags & OMAP_BO_TILED) {
1054 seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
1055 if (omap_obj->block) {
1056 struct tcm_area *area = &omap_obj->block->area;
1057 seq_printf(m, " (%dx%d, %dx%d)",
1058 area->p0.x, area->p0.y,
1059 area->p1.x, area->p1.y);
1060 }
1061 } else {
1062 seq_printf(m, " %d", obj->size);
1063 }
1064
1065 seq_printf(m, "\n");
1066}
1067
1068void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1069{
1070 struct omap_gem_object *omap_obj;
1071 int count = 0;
1072 size_t size = 0;
1073
1074 list_for_each_entry(omap_obj, list, mm_list) {
1075 struct drm_gem_object *obj = &omap_obj->base;
1076 seq_printf(m, " ");
1077 omap_gem_describe(obj, m);
1078 count++;
1079 size += obj->size;
1080 }
1081
1082 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1083}
1084#endif
1085
Laurent Pinchart7ef93b02015-12-14 22:39:33 +02001086/* -----------------------------------------------------------------------------
1087 * Buffer Synchronization
Rob Clarkcd5351f2011-11-12 12:09:40 -06001088 */
1089
Laurent Pinchart7ef93b02015-12-14 22:39:33 +02001090static DEFINE_SPINLOCK(sync_lock);
1091
Rob Clarkcd5351f2011-11-12 12:09:40 -06001092struct omap_gem_sync_waiter {
1093 struct list_head list;
1094 struct omap_gem_object *omap_obj;
1095 enum omap_gem_op op;
1096 uint32_t read_target, write_target;
1097 /* notify called w/ sync_lock held */
1098 void (*notify)(void *arg);
1099 void *arg;
1100};
1101
1102/* list of omap_gem_sync_waiter.. the notify fxn gets called back when
1103 * the read and/or write target count is achieved which can call a user
1104 * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
1105 * cpu access), etc.
1106 */
1107static LIST_HEAD(waiters);
1108
1109static inline bool is_waiting(struct omap_gem_sync_waiter *waiter)
1110{
1111 struct omap_gem_object *omap_obj = waiter->omap_obj;
1112 if ((waiter->op & OMAP_GEM_READ) &&
Archit Tanejaf2cff0f2014-04-11 12:53:31 +05301113 (omap_obj->sync->write_complete < waiter->write_target))
Rob Clarkcd5351f2011-11-12 12:09:40 -06001114 return true;
1115 if ((waiter->op & OMAP_GEM_WRITE) &&
Archit Tanejaf2cff0f2014-04-11 12:53:31 +05301116 (omap_obj->sync->read_complete < waiter->read_target))
Rob Clarkcd5351f2011-11-12 12:09:40 -06001117 return true;
1118 return false;
1119}
1120
1121/* macro for sync debug.. */
1122#define SYNCDBG 0
1123#define SYNC(fmt, ...) do { if (SYNCDBG) \
1124 printk(KERN_ERR "%s:%d: "fmt"\n", \
1125 __func__, __LINE__, ##__VA_ARGS__); \
1126 } while (0)
1127
1128
1129static void sync_op_update(void)
1130{
1131 struct omap_gem_sync_waiter *waiter, *n;
1132 list_for_each_entry_safe(waiter, n, &waiters, list) {
1133 if (!is_waiting(waiter)) {
1134 list_del(&waiter->list);
1135 SYNC("notify: %p", waiter);
1136 waiter->notify(waiter->arg);
1137 kfree(waiter);
1138 }
1139 }
1140}
1141
1142static inline int sync_op(struct drm_gem_object *obj,
1143 enum omap_gem_op op, bool start)
1144{
1145 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1146 int ret = 0;
1147
1148 spin_lock(&sync_lock);
1149
1150 if (!omap_obj->sync) {
1151 omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
1152 if (!omap_obj->sync) {
1153 ret = -ENOMEM;
1154 goto unlock;
1155 }
1156 }
1157
1158 if (start) {
1159 if (op & OMAP_GEM_READ)
1160 omap_obj->sync->read_pending++;
1161 if (op & OMAP_GEM_WRITE)
1162 omap_obj->sync->write_pending++;
1163 } else {
1164 if (op & OMAP_GEM_READ)
1165 omap_obj->sync->read_complete++;
1166 if (op & OMAP_GEM_WRITE)
1167 omap_obj->sync->write_complete++;
1168 sync_op_update();
1169 }
1170
1171unlock:
1172 spin_unlock(&sync_lock);
1173
1174 return ret;
1175}
1176
Rob Clarkcd5351f2011-11-12 12:09:40 -06001177/* mark the start of read and/or write operation */
1178int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
1179{
1180 return sync_op(obj, op, true);
1181}
1182
1183int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
1184{
1185 return sync_op(obj, op, false);
1186}
1187
1188static DECLARE_WAIT_QUEUE_HEAD(sync_event);
1189
1190static void sync_notify(void *arg)
1191{
1192 struct task_struct **waiter_task = arg;
1193 *waiter_task = NULL;
1194 wake_up_all(&sync_event);
1195}
1196
1197int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op)
1198{
1199 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1200 int ret = 0;
1201 if (omap_obj->sync) {
1202 struct task_struct *waiter_task = current;
1203 struct omap_gem_sync_waiter *waiter =
1204 kzalloc(sizeof(*waiter), GFP_KERNEL);
1205
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001206 if (!waiter)
Rob Clarkcd5351f2011-11-12 12:09:40 -06001207 return -ENOMEM;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001208
1209 waiter->omap_obj = omap_obj;
1210 waiter->op = op;
1211 waiter->read_target = omap_obj->sync->read_pending;
1212 waiter->write_target = omap_obj->sync->write_pending;
1213 waiter->notify = sync_notify;
1214 waiter->arg = &waiter_task;
1215
1216 spin_lock(&sync_lock);
1217 if (is_waiting(waiter)) {
1218 SYNC("waited: %p", waiter);
1219 list_add_tail(&waiter->list, &waiters);
1220 spin_unlock(&sync_lock);
1221 ret = wait_event_interruptible(sync_event,
1222 (waiter_task == NULL));
1223 spin_lock(&sync_lock);
1224 if (waiter_task) {
1225 SYNC("interrupted: %p", waiter);
1226 /* we were interrupted */
1227 list_del(&waiter->list);
1228 waiter_task = NULL;
1229 } else {
1230 /* freed in sync_op_update() */
1231 waiter = NULL;
1232 }
1233 }
1234 spin_unlock(&sync_lock);
Fabian Frederickd2c87e22014-07-04 21:17:15 +02001235 kfree(waiter);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001236 }
1237 return ret;
1238}
1239
1240/* call fxn(arg), either synchronously or asynchronously if the op
1241 * is currently blocked.. fxn() can be called from any context
1242 *
1243 * (TODO for now fxn is called back from whichever context calls
Tomi Valkeinen3f50eff2016-01-27 10:58:43 +02001244 * omap_gem_op_finish().. but this could be better defined later
Rob Clarkcd5351f2011-11-12 12:09:40 -06001245 * if needed)
1246 *
1247 * TODO more code in common w/ _sync()..
1248 */
1249int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op,
1250 void (*fxn)(void *arg), void *arg)
1251{
1252 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1253 if (omap_obj->sync) {
1254 struct omap_gem_sync_waiter *waiter =
1255 kzalloc(sizeof(*waiter), GFP_ATOMIC);
1256
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001257 if (!waiter)
Rob Clarkcd5351f2011-11-12 12:09:40 -06001258 return -ENOMEM;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001259
1260 waiter->omap_obj = omap_obj;
1261 waiter->op = op;
1262 waiter->read_target = omap_obj->sync->read_pending;
1263 waiter->write_target = omap_obj->sync->write_pending;
1264 waiter->notify = fxn;
1265 waiter->arg = arg;
1266
1267 spin_lock(&sync_lock);
1268 if (is_waiting(waiter)) {
1269 SYNC("waited: %p", waiter);
1270 list_add_tail(&waiter->list, &waiters);
1271 spin_unlock(&sync_lock);
1272 return 0;
1273 }
1274
1275 spin_unlock(&sync_lock);
Subhajit Paul15ec2ca2014-04-11 12:53:30 +05301276
1277 kfree(waiter);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001278 }
1279
1280 /* no waiting.. */
1281 fxn(arg);
1282
1283 return 0;
1284}
1285
Laurent Pinchart7ef93b02015-12-14 22:39:33 +02001286/* -----------------------------------------------------------------------------
1287 * Constructor & Destructor
1288 */
1289
Rob Clarkcd5351f2011-11-12 12:09:40 -06001290void omap_gem_free_object(struct drm_gem_object *obj)
1291{
1292 struct drm_device *dev = obj->dev;
Tomi Valkeinen76c40552014-12-17 14:34:22 +02001293 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001294 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1295
Rob Clarkf7f9f452011-12-05 19:19:22 -06001296 evict(obj);
1297
Rob Clarkf6b60362012-03-05 10:48:36 -06001298 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1299
Tomi Valkeinen76c40552014-12-17 14:34:22 +02001300 spin_lock(&priv->list_lock);
Rob Clarkf6b60362012-03-05 10:48:36 -06001301 list_del(&omap_obj->mm_list);
Tomi Valkeinen76c40552014-12-17 14:34:22 +02001302 spin_unlock(&priv->list_lock);
Rob Clarkf6b60362012-03-05 10:48:36 -06001303
Rob Clark9a0774e2012-01-16 12:51:17 -06001304 /* this means the object is still pinned.. which really should
1305 * not happen. I think..
1306 */
1307 WARN_ON(omap_obj->paddr_cnt > 0);
1308
Tomi Valkeinen3f50eff2016-01-27 10:58:43 +02001309 if (omap_obj->pages) {
1310 if (omap_obj->flags & OMAP_BO_MEM_DMABUF)
1311 kfree(omap_obj->pages);
1312 else
Rob Clarkcd5351f2011-11-12 12:09:40 -06001313 omap_gem_detach_pages(obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001314 }
1315
Tomi Valkeinen3f50eff2016-01-27 10:58:43 +02001316 if (omap_obj->flags & OMAP_BO_MEM_DMA_API) {
Linus Torvalds266c73b2016-03-21 13:48:00 -07001317 dma_free_wc(dev->dev, obj->size, omap_obj->vaddr,
1318 omap_obj->paddr);
Tomi Valkeinen3f50eff2016-01-27 10:58:43 +02001319 } else if (omap_obj->vaddr) {
1320 vunmap(omap_obj->vaddr);
1321 } else if (obj->import_attach) {
1322 drm_prime_gem_destroy(obj, omap_obj->sgt);
1323 }
1324
1325 kfree(omap_obj->sync);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001326
1327 drm_gem_object_release(obj);
1328
Laurent Pinchart00e9c7c2015-12-14 22:39:38 +02001329 kfree(omap_obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001330}
1331
1332/* GEM buffer object constructor */
1333struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1334 union omap_gem_size gsize, uint32_t flags)
1335{
Rob Clarka6a91822011-12-09 23:26:08 -06001336 struct omap_drm_private *priv = dev->dev_private;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001337 struct omap_gem_object *omap_obj;
Laurent Pinchart92b4b442015-12-14 22:39:41 +02001338 struct drm_gem_object *obj;
David Herrmannab5a60c2014-05-25 12:45:39 +02001339 struct address_space *mapping;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001340 size_t size;
1341 int ret;
1342
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001343 /* Validate the flags and compute the memory and cache flags. */
Rob Clarkcd5351f2011-11-12 12:09:40 -06001344 if (flags & OMAP_BO_TILED) {
Laurent Pinchartf4302742015-12-14 22:39:34 +02001345 if (!priv->usergart) {
Rob Clarkf7f9f452011-12-05 19:19:22 -06001346 dev_err(dev->dev, "Tiled buffers require DMM\n");
Laurent Pinchart92b4b442015-12-14 22:39:41 +02001347 return NULL;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001348 }
Rob Clarkcd5351f2011-11-12 12:09:40 -06001349
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001350 /*
1351 * Tiled buffers are always shmem paged backed. When they are
1352 * scanned out, they are remapped into DMM/TILER.
Rob Clarkf7f9f452011-12-05 19:19:22 -06001353 */
1354 flags &= ~OMAP_BO_SCANOUT;
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001355 flags |= OMAP_BO_MEM_SHMEM;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001356
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001357 /*
1358 * Currently don't allow cached buffers. There is some caching
1359 * stuff that needs to be handled better.
Rob Clarkf7f9f452011-12-05 19:19:22 -06001360 */
Tomi Valkeinen7cb0d6c2014-09-25 19:24:29 +00001361 flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
1362 flags |= tiler_get_cpu_cache_flags();
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001363 } else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1364 /*
Laurent Pinchartb22e6692015-12-14 22:39:44 +02001365 * OMAP_BO_SCANOUT hints that the buffer doesn't need to be
1366 * tiled. However, to lower the pressure on memory allocation,
1367 * use contiguous memory only if no TILER is available.
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001368 */
1369 flags |= OMAP_BO_MEM_DMA_API;
Tomi Valkeinen3f50eff2016-01-27 10:58:43 +02001370 } else if (!(flags & OMAP_BO_MEM_DMABUF)) {
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001371 /*
Tomi Valkeinen3f50eff2016-01-27 10:58:43 +02001372 * All other buffers not backed by dma_buf are shmem-backed.
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001373 */
1374 flags |= OMAP_BO_MEM_SHMEM;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001375 }
Rob Clarkcd5351f2011-11-12 12:09:40 -06001376
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001377 /* Allocate the initialize the OMAP GEM object. */
Rob Clarkcd5351f2011-11-12 12:09:40 -06001378 omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -08001379 if (!omap_obj)
Tomi Valkeinena903e3b2015-03-17 15:31:11 +02001380 return NULL;
Rob Clarkf6b60362012-03-05 10:48:36 -06001381
Rob Clarkcd5351f2011-11-12 12:09:40 -06001382 obj = &omap_obj->base;
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001383 omap_obj->flags = flags;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001384
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001385 if (flags & OMAP_BO_TILED) {
1386 /*
1387 * For tiled buffers align dimensions to slot boundaries and
1388 * calculate size based on aligned dimensions.
Rob Clarka6a91822011-12-09 23:26:08 -06001389 */
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001390 tiler_align(gem2fmt(flags), &gsize.tiled.width,
1391 &gsize.tiled.height);
YAMANE Toshiakiae053032012-11-14 19:33:17 +09001392
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001393 size = tiler_size(gem2fmt(flags), gsize.tiled.width,
1394 gsize.tiled.height);
Tomi Valkeinena903e3b2015-03-17 15:31:11 +02001395
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001396 omap_obj->width = gsize.tiled.width;
1397 omap_obj->height = gsize.tiled.height;
1398 } else {
1399 size = PAGE_ALIGN(gsize.bytes);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001400 }
1401
Laurent Pinchartc2eb77f2016-03-02 12:51:19 +02001402 /* Initialize the GEM object. */
1403 if (!(flags & OMAP_BO_MEM_SHMEM)) {
1404 drm_gem_private_object_init(dev, obj, size);
1405 } else {
1406 ret = drm_gem_object_init(dev, obj, size);
1407 if (ret)
1408 goto err_free;
1409
1410 mapping = file_inode(obj->filp)->i_mapping;
1411 mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
1412 }
Tomi Valkeinena903e3b2015-03-17 15:31:11 +02001413
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001414 /* Allocate memory if needed. */
1415 if (flags & OMAP_BO_MEM_DMA_API) {
Linus Torvalds266c73b2016-03-21 13:48:00 -07001416 omap_obj->vaddr = dma_alloc_wc(dev->dev, size,
1417 &omap_obj->paddr,
1418 GFP_KERNEL);
Laurent Pinchart9cba3b92015-12-14 22:39:43 +02001419 if (!omap_obj->vaddr)
Laurent Pinchartc2eb77f2016-03-02 12:51:19 +02001420 goto err_release;
Rob Clarkcd5351f2011-11-12 12:09:40 -06001421 }
1422
1423 spin_lock(&priv->list_lock);
1424 list_add(&omap_obj->mm_list, &priv->obj_list);
Rob Clarkf7f9f452011-12-05 19:19:22 -06001425 spin_unlock(&priv->list_lock);
1426
Rob Clarkcd5351f2011-11-12 12:09:40 -06001427 return obj;
1428
Laurent Pinchartc2eb77f2016-03-02 12:51:19 +02001429err_release:
1430 drm_gem_object_release(obj);
1431err_free:
1432 kfree(omap_obj);
Rob Clarkcd5351f2011-11-12 12:09:40 -06001433 return NULL;
1434}
Rob Clarkf7f9f452011-12-05 19:19:22 -06001435
Laurent Pinchartb22e6692015-12-14 22:39:44 +02001436struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size,
1437 struct sg_table *sgt)
1438{
1439 struct omap_drm_private *priv = dev->dev_private;
1440 struct omap_gem_object *omap_obj;
1441 struct drm_gem_object *obj;
1442 union omap_gem_size gsize;
1443
1444 /* Without a DMM only physically contiguous buffers can be supported. */
1445 if (sgt->orig_nents != 1 && !priv->has_dmm)
1446 return ERR_PTR(-EINVAL);
1447
1448 mutex_lock(&dev->struct_mutex);
1449
1450 gsize.bytes = PAGE_ALIGN(size);
1451 obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC);
1452 if (!obj) {
1453 obj = ERR_PTR(-ENOMEM);
1454 goto done;
1455 }
1456
1457 omap_obj = to_omap_bo(obj);
1458 omap_obj->sgt = sgt;
1459
1460 if (sgt->orig_nents == 1) {
1461 omap_obj->paddr = sg_dma_address(sgt->sgl);
1462 } else {
1463 /* Create pages list from sgt */
1464 struct sg_page_iter iter;
1465 struct page **pages;
1466 unsigned int npages;
1467 unsigned int i = 0;
1468
1469 npages = DIV_ROUND_UP(size, PAGE_SIZE);
1470 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1471 if (!pages) {
1472 omap_gem_free_object(obj);
1473 obj = ERR_PTR(-ENOMEM);
1474 goto done;
1475 }
1476
1477 omap_obj->pages = pages;
1478
1479 for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) {
1480 pages[i++] = sg_page_iter_page(&iter);
1481 if (i > npages)
1482 break;
1483 }
1484
1485 if (WARN_ON(i != npages)) {
1486 omap_gem_free_object(obj);
1487 obj = ERR_PTR(-ENOMEM);
1488 goto done;
1489 }
1490 }
1491
1492done:
1493 mutex_unlock(&dev->struct_mutex);
1494 return obj;
1495}
1496
Laurent Pinchart7ef93b02015-12-14 22:39:33 +02001497/* convenience method to construct a GEM buffer object, and userspace handle */
1498int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1499 union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
1500{
1501 struct drm_gem_object *obj;
1502 int ret;
1503
1504 obj = omap_gem_new(dev, gsize, flags);
1505 if (!obj)
1506 return -ENOMEM;
1507
1508 ret = drm_gem_handle_create(file, obj, handle);
1509 if (ret) {
Laurent Pinchart74128a22015-12-14 22:39:39 +02001510 omap_gem_free_object(obj);
Laurent Pinchart7ef93b02015-12-14 22:39:33 +02001511 return ret;
1512 }
1513
1514 /* drop reference from allocate - handle holds it now */
1515 drm_gem_object_unreference_unlocked(obj);
1516
1517 return 0;
1518}
1519
1520/* -----------------------------------------------------------------------------
1521 * Init & Cleanup
1522 */
1523
1524/* If DMM is used, we need to set some stuff up.. */
Rob Clarkf7f9f452011-12-05 19:19:22 -06001525void omap_gem_init(struct drm_device *dev)
1526{
Rob Clarka6a91822011-12-09 23:26:08 -06001527 struct omap_drm_private *priv = dev->dev_private;
Laurent Pinchartf4302742015-12-14 22:39:34 +02001528 struct omap_drm_usergart *usergart;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001529 const enum tiler_fmt fmts[] = {
1530 TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1531 };
Andy Gross5c137792012-03-05 10:48:39 -06001532 int i, j;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001533
Andy Grosse5e4e9b2012-10-17 00:30:03 -05001534 if (!dmm_is_available()) {
Rob Clarkf7f9f452011-12-05 19:19:22 -06001535 /* DMM only supported on OMAP4 and later, so this isn't fatal */
Andy Gross5c137792012-03-05 10:48:39 -06001536 dev_warn(dev->dev, "DMM not available, disable DMM support\n");
Rob Clarkf7f9f452011-12-05 19:19:22 -06001537 return;
1538 }
1539
Joe Perches78110bb2013-02-11 09:41:29 -08001540 usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1541 if (!usergart)
Rob Clarkb3698392011-12-09 23:26:06 -06001542 return;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001543
1544 /* reserve 4k aligned/wide regions for userspace mappings: */
1545 for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1546 uint16_t h = 1, w = PAGE_SIZE >> i;
1547 tiler_align(fmts[i], &w, &h);
1548 /* note: since each region is 1 4kb page wide, and minimum
1549 * number of rows, the height ends up being the same as the
1550 * # of pages in the region
1551 */
1552 usergart[i].height = h;
1553 usergart[i].height_shift = ilog2(h);
Rob Clark3c810c62012-08-15 15:18:01 -05001554 usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001555 usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1556 for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
Laurent Pinchartf4302742015-12-14 22:39:34 +02001557 struct omap_drm_usergart_entry *entry;
1558 struct tiler_block *block;
1559
1560 entry = &usergart[i].entry[j];
1561 block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
Rob Clarkf7f9f452011-12-05 19:19:22 -06001562 if (IS_ERR(block)) {
1563 dev_err(dev->dev,
1564 "reserve failed: %d, %d, %ld\n",
1565 i, j, PTR_ERR(block));
1566 return;
1567 }
1568 entry->paddr = tiler_ssptr(block);
1569 entry->block = block;
1570
Russell King2d31ca32014-07-12 10:53:41 +01001571 DBG("%d:%d: %dx%d: paddr=%pad stride=%d", i, j, w, h,
1572 &entry->paddr,
Rob Clarkf7f9f452011-12-05 19:19:22 -06001573 usergart[i].stride_pfn << PAGE_SHIFT);
1574 }
1575 }
Rob Clarka6a91822011-12-09 23:26:08 -06001576
Laurent Pinchartf4302742015-12-14 22:39:34 +02001577 priv->usergart = usergart;
Rob Clarka6a91822011-12-09 23:26:08 -06001578 priv->has_dmm = true;
Rob Clarkf7f9f452011-12-05 19:19:22 -06001579}
1580
1581void omap_gem_deinit(struct drm_device *dev)
1582{
Laurent Pinchartf4302742015-12-14 22:39:34 +02001583 struct omap_drm_private *priv = dev->dev_private;
1584
Rob Clarkf7f9f452011-12-05 19:19:22 -06001585 /* I believe we can rely on there being no more outstanding GEM
1586 * objects which could depend on usergart/dmm at this point.
1587 */
Laurent Pinchartf4302742015-12-14 22:39:34 +02001588 kfree(priv->usergart);
Rob Clarkf7f9f452011-12-05 19:19:22 -06001589}